Home | History | Annotate | Line # | Download | only in kern
uipc_socket2.c revision 1.33
      1  1.33   mycroft /*	$NetBSD: uipc_socket2.c,v 1.33 1999/08/04 22:33:20 mycroft 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.23      fvdl  *	@(#)uipc_socket2.c	8.2 (Berkeley) 2/14/95
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.5   mycroft #include <sys/param.h>
     39   1.5   mycroft #include <sys/systm.h>
     40   1.5   mycroft #include <sys/proc.h>
     41   1.5   mycroft #include <sys/file.h>
     42   1.5   mycroft #include <sys/buf.h>
     43   1.5   mycroft #include <sys/malloc.h>
     44   1.5   mycroft #include <sys/mbuf.h>
     45   1.5   mycroft #include <sys/protosw.h>
     46   1.5   mycroft #include <sys/socket.h>
     47   1.5   mycroft #include <sys/socketvar.h>
     48  1.11  christos #include <sys/signalvar.h>
     49   1.1       cgd 
     50   1.1       cgd /*
     51   1.1       cgd  * Primitive routines for operating on sockets and socket buffers
     52   1.1       cgd  */
     53   1.1       cgd 
     54   1.1       cgd /* strings for sleep message: */
     55  1.21   mycroft const char	netio[] = "netio";
     56  1.21   mycroft const char	netcon[] = "netcon";
     57  1.21   mycroft const char	netcls[] = "netcls";
     58   1.1       cgd 
     59   1.1       cgd /*
     60   1.1       cgd  * Procedures to manipulate state flags of socket
     61   1.1       cgd  * and do appropriate wakeups.  Normal sequence from the
     62   1.1       cgd  * active (originating) side is that soisconnecting() is
     63   1.1       cgd  * called during processing of connect() call,
     64   1.1       cgd  * resulting in an eventual call to soisconnected() if/when the
     65   1.1       cgd  * connection is established.  When the connection is torn down
     66   1.1       cgd  * soisdisconnecting() is called during processing of disconnect() call,
     67   1.1       cgd  * and soisdisconnected() is called when the connection to the peer
     68   1.1       cgd  * is totally severed.  The semantics of these routines are such that
     69   1.1       cgd  * connectionless protocols can call soisconnected() and soisdisconnected()
     70   1.1       cgd  * only, bypassing the in-progress calls when setting up a ``connection''
     71   1.1       cgd  * takes no time.
     72   1.1       cgd  *
     73   1.1       cgd  * From the passive side, a socket is created with
     74   1.1       cgd  * two queues of sockets: so_q0 for connections in progress
     75   1.1       cgd  * and so_q for connections already made and awaiting user acceptance.
     76   1.1       cgd  * As a protocol is preparing incoming connections, it creates a socket
     77   1.1       cgd  * structure queued on so_q0 by calling sonewconn().  When the connection
     78   1.1       cgd  * is established, soisconnected() is called, and transfers the
     79   1.1       cgd  * socket structure to so_q, making it available to accept().
     80   1.1       cgd  *
     81   1.1       cgd  * If a socket is closed with sockets on either
     82   1.1       cgd  * so_q0 or so_q, these sockets are dropped.
     83   1.1       cgd  *
     84   1.1       cgd  * If higher level protocols are implemented in
     85   1.1       cgd  * the kernel, the wakeups done here will sometimes
     86   1.1       cgd  * cause software-interrupt process scheduling.
     87   1.1       cgd  */
     88   1.1       cgd 
     89   1.7   mycroft void
     90   1.1       cgd soisconnecting(so)
     91   1.1       cgd 	register struct socket *so;
     92   1.1       cgd {
     93   1.1       cgd 
     94   1.1       cgd 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
     95   1.1       cgd 	so->so_state |= SS_ISCONNECTING;
     96   1.1       cgd }
     97   1.1       cgd 
     98   1.7   mycroft void
     99   1.1       cgd soisconnected(so)
    100   1.1       cgd 	register struct socket *so;
    101   1.1       cgd {
    102   1.1       cgd 	register struct socket *head = so->so_head;
    103   1.1       cgd 
    104   1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
    105   1.1       cgd 	so->so_state |= SS_ISCONNECTED;
    106   1.1       cgd 	if (head && soqremque(so, 0)) {
    107   1.1       cgd 		soqinsque(head, so, 1);
    108   1.1       cgd 		sorwakeup(head);
    109   1.1       cgd 		wakeup((caddr_t)&head->so_timeo);
    110   1.1       cgd 	} else {
    111   1.1       cgd 		wakeup((caddr_t)&so->so_timeo);
    112   1.1       cgd 		sorwakeup(so);
    113   1.1       cgd 		sowwakeup(so);
    114   1.1       cgd 	}
    115   1.1       cgd }
    116   1.1       cgd 
    117   1.7   mycroft void
    118   1.1       cgd soisdisconnecting(so)
    119   1.1       cgd 	register struct socket *so;
    120   1.1       cgd {
    121   1.1       cgd 
    122   1.1       cgd 	so->so_state &= ~SS_ISCONNECTING;
    123   1.1       cgd 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
    124   1.1       cgd 	wakeup((caddr_t)&so->so_timeo);
    125   1.1       cgd 	sowwakeup(so);
    126   1.1       cgd 	sorwakeup(so);
    127   1.1       cgd }
    128   1.1       cgd 
    129   1.7   mycroft void
    130   1.1       cgd soisdisconnected(so)
    131   1.1       cgd 	register struct socket *so;
    132   1.1       cgd {
    133   1.1       cgd 
    134   1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
    135  1.27   mycroft 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
    136   1.1       cgd 	wakeup((caddr_t)&so->so_timeo);
    137   1.1       cgd 	sowwakeup(so);
    138   1.1       cgd 	sorwakeup(so);
    139   1.1       cgd }
    140   1.1       cgd 
    141   1.1       cgd /*
    142   1.1       cgd  * When an attempt at a new connection is noted on a socket
    143   1.1       cgd  * which accepts connections, sonewconn is called.  If the
    144   1.1       cgd  * connection is possible (subject to space constraints, etc.)
    145   1.1       cgd  * then we allocate a new structure, propoerly linked into the
    146   1.1       cgd  * data structure of the original socket, and return this.
    147   1.1       cgd  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
    148   1.1       cgd  *
    149   1.1       cgd  * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
    150   1.1       cgd  * to catch calls that are missing the (new) second parameter.
    151   1.1       cgd  */
    152   1.1       cgd struct socket *
    153   1.1       cgd sonewconn1(head, connstatus)
    154   1.1       cgd 	register struct socket *head;
    155   1.1       cgd 	int connstatus;
    156   1.1       cgd {
    157   1.1       cgd 	register struct socket *so;
    158   1.1       cgd 	int soqueue = connstatus ? 1 : 0;
    159   1.1       cgd 
    160   1.1       cgd 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
    161   1.1       cgd 		return ((struct socket *)0);
    162  1.25   thorpej 	so = pool_get(&socket_pool, PR_NOWAIT);
    163   1.1       cgd 	if (so == NULL)
    164  1.25   thorpej 		return (NULL);
    165  1.26     perry 	memset((caddr_t)so, 0, sizeof(*so));
    166   1.1       cgd 	so->so_type = head->so_type;
    167   1.1       cgd 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
    168   1.1       cgd 	so->so_linger = head->so_linger;
    169   1.1       cgd 	so->so_state = head->so_state | SS_NOFDREF;
    170   1.1       cgd 	so->so_proto = head->so_proto;
    171   1.1       cgd 	so->so_timeo = head->so_timeo;
    172   1.1       cgd 	so->so_pgid = head->so_pgid;
    173  1.24      matt 	so->so_send = head->so_send;
    174  1.24      matt 	so->so_receive = head->so_receive;
    175  1.28     lukem 	so->so_uid = head->so_uid;
    176   1.1       cgd 	(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
    177   1.1       cgd 	soqinsque(head, so, soqueue);
    178   1.1       cgd 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
    179  1.12   mycroft 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
    180  1.12   mycroft 	    (struct proc *)0)) {
    181   1.1       cgd 		(void) soqremque(so, soqueue);
    182  1.25   thorpej 		pool_put(&socket_pool, so);
    183  1.25   thorpej 		return (NULL);
    184   1.1       cgd 	}
    185   1.1       cgd 	if (connstatus) {
    186   1.1       cgd 		sorwakeup(head);
    187   1.1       cgd 		wakeup((caddr_t)&head->so_timeo);
    188   1.1       cgd 		so->so_state |= connstatus;
    189   1.1       cgd 	}
    190   1.1       cgd 	return (so);
    191   1.1       cgd }
    192   1.1       cgd 
    193   1.7   mycroft void
    194   1.1       cgd soqinsque(head, so, q)
    195   1.1       cgd 	register struct socket *head, *so;
    196   1.1       cgd 	int q;
    197   1.1       cgd {
    198   1.1       cgd 
    199  1.22   thorpej #ifdef DIAGNOSTIC
    200  1.22   thorpej 	if (so->so_onq != NULL)
    201  1.22   thorpej 		panic("soqinsque");
    202  1.22   thorpej #endif
    203  1.22   thorpej 
    204   1.1       cgd 	so->so_head = head;
    205   1.1       cgd 	if (q == 0) {
    206   1.1       cgd 		head->so_q0len++;
    207  1.22   thorpej 		so->so_onq = &head->so_q0;
    208   1.1       cgd 	} else {
    209   1.1       cgd 		head->so_qlen++;
    210  1.22   thorpej 		so->so_onq = &head->so_q;
    211   1.1       cgd 	}
    212  1.22   thorpej 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
    213   1.1       cgd }
    214   1.1       cgd 
    215   1.7   mycroft int
    216   1.1       cgd soqremque(so, q)
    217   1.1       cgd 	register struct socket *so;
    218   1.1       cgd 	int q;
    219   1.1       cgd {
    220  1.22   thorpej 	struct socket *head = so->so_head;
    221   1.1       cgd 
    222  1.22   thorpej 	if (q == 0) {
    223  1.22   thorpej 		if (so->so_onq != &head->so_q0)
    224  1.17   thorpej 			return (0);
    225   1.1       cgd 		head->so_q0len--;
    226   1.1       cgd 	} else {
    227  1.22   thorpej 		if (so->so_onq != &head->so_q)
    228  1.22   thorpej 			return (0);
    229   1.1       cgd 		head->so_qlen--;
    230   1.1       cgd 	}
    231  1.22   thorpej 	TAILQ_REMOVE(so->so_onq, so, so_qe);
    232  1.22   thorpej 	so->so_onq = NULL;
    233  1.22   thorpej 	so->so_head = NULL;
    234   1.1       cgd 	return (1);
    235   1.1       cgd }
    236   1.1       cgd 
    237   1.1       cgd /*
    238   1.1       cgd  * Socantsendmore indicates that no more data will be sent on the
    239   1.1       cgd  * socket; it would normally be applied to a socket when the user
    240   1.1       cgd  * informs the system that no more data is to be sent, by the protocol
    241   1.1       cgd  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
    242   1.1       cgd  * will be received, and will normally be applied to the socket by a
    243   1.1       cgd  * protocol when it detects that the peer will send no more data.
    244   1.1       cgd  * Data queued for reading in the socket may yet be read.
    245   1.1       cgd  */
    246   1.1       cgd 
    247   1.4    andrew void
    248   1.1       cgd socantsendmore(so)
    249   1.1       cgd 	struct socket *so;
    250   1.1       cgd {
    251   1.1       cgd 
    252   1.1       cgd 	so->so_state |= SS_CANTSENDMORE;
    253   1.1       cgd 	sowwakeup(so);
    254   1.1       cgd }
    255   1.1       cgd 
    256   1.4    andrew void
    257   1.1       cgd socantrcvmore(so)
    258   1.1       cgd 	struct socket *so;
    259   1.1       cgd {
    260   1.1       cgd 
    261   1.1       cgd 	so->so_state |= SS_CANTRCVMORE;
    262   1.1       cgd 	sorwakeup(so);
    263   1.1       cgd }
    264   1.1       cgd 
    265   1.1       cgd /*
    266   1.1       cgd  * Wait for data to arrive at/drain from a socket buffer.
    267   1.1       cgd  */
    268   1.7   mycroft int
    269   1.1       cgd sbwait(sb)
    270   1.1       cgd 	struct sockbuf *sb;
    271   1.1       cgd {
    272   1.1       cgd 
    273   1.1       cgd 	sb->sb_flags |= SB_WAIT;
    274   1.1       cgd 	return (tsleep((caddr_t)&sb->sb_cc,
    275   1.1       cgd 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
    276   1.1       cgd 	    sb->sb_timeo));
    277   1.1       cgd }
    278   1.1       cgd 
    279   1.1       cgd /*
    280   1.1       cgd  * Lock a sockbuf already known to be locked;
    281   1.1       cgd  * return any error returned from sleep (EINTR).
    282   1.1       cgd  */
    283   1.7   mycroft int
    284   1.1       cgd sb_lock(sb)
    285   1.1       cgd 	register struct sockbuf *sb;
    286   1.1       cgd {
    287   1.1       cgd 	int error;
    288   1.1       cgd 
    289   1.1       cgd 	while (sb->sb_flags & SB_LOCK) {
    290   1.1       cgd 		sb->sb_flags |= SB_WANT;
    291  1.11  christos 		error = tsleep((caddr_t)&sb->sb_flags,
    292  1.11  christos 			       (sb->sb_flags & SB_NOINTR) ?
    293  1.11  christos 					PSOCK : PSOCK|PCATCH, netio, 0);
    294  1.11  christos 		if (error)
    295   1.1       cgd 			return (error);
    296   1.1       cgd 	}
    297   1.1       cgd 	sb->sb_flags |= SB_LOCK;
    298   1.1       cgd 	return (0);
    299   1.1       cgd }
    300   1.1       cgd 
    301   1.1       cgd /*
    302   1.1       cgd  * Wakeup processes waiting on a socket buffer.
    303   1.1       cgd  * Do asynchronous notification via SIGIO
    304   1.1       cgd  * if the socket has the SS_ASYNC flag set.
    305   1.1       cgd  */
    306   1.7   mycroft void
    307   1.1       cgd sowakeup(so, sb)
    308   1.1       cgd 	register struct socket *so;
    309   1.1       cgd 	register struct sockbuf *sb;
    310   1.1       cgd {
    311   1.1       cgd 	struct proc *p;
    312   1.1       cgd 
    313   1.3       cgd 	selwakeup(&sb->sb_sel);
    314   1.7   mycroft 	sb->sb_flags &= ~SB_SEL;
    315   1.1       cgd 	if (sb->sb_flags & SB_WAIT) {
    316   1.1       cgd 		sb->sb_flags &= ~SB_WAIT;
    317   1.1       cgd 		wakeup((caddr_t)&sb->sb_cc);
    318   1.1       cgd 	}
    319   1.1       cgd 	if (so->so_state & SS_ASYNC) {
    320   1.1       cgd 		if (so->so_pgid < 0)
    321   1.1       cgd 			gsignal(-so->so_pgid, SIGIO);
    322   1.1       cgd 		else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
    323   1.1       cgd 			psignal(p, SIGIO);
    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.1       cgd soreserve(so, sndcc, rcvcc)
    363   1.1       cgd 	register struct socket *so;
    364   1.1       cgd 	u_long sndcc, rcvcc;
    365   1.1       cgd {
    366   1.1       cgd 
    367   1.1       cgd 	if (sbreserve(&so->so_snd, sndcc) == 0)
    368   1.1       cgd 		goto bad;
    369   1.1       cgd 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
    370   1.1       cgd 		goto bad2;
    371   1.1       cgd 	if (so->so_rcv.sb_lowat == 0)
    372   1.1       cgd 		so->so_rcv.sb_lowat = 1;
    373   1.1       cgd 	if (so->so_snd.sb_lowat == 0)
    374   1.1       cgd 		so->so_snd.sb_lowat = MCLBYTES;
    375   1.1       cgd 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
    376   1.1       cgd 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
    377   1.1       cgd 	return (0);
    378   1.1       cgd bad2:
    379   1.1       cgd 	sbrelease(&so->so_snd);
    380   1.1       cgd bad:
    381   1.1       cgd 	return (ENOBUFS);
    382   1.1       cgd }
    383   1.1       cgd 
    384   1.1       cgd /*
    385   1.1       cgd  * Allot mbufs to a sockbuf.
    386   1.1       cgd  * Attempt to scale mbmax so that mbcnt doesn't become limiting
    387   1.1       cgd  * if buffering efficiency is near the normal case.
    388   1.1       cgd  */
    389   1.7   mycroft int
    390   1.1       cgd sbreserve(sb, cc)
    391   1.1       cgd 	struct sockbuf *sb;
    392   1.1       cgd 	u_long cc;
    393   1.1       cgd {
    394   1.1       cgd 
    395  1.18   thorpej 	if (cc == 0 || cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
    396   1.1       cgd 		return (0);
    397   1.1       cgd 	sb->sb_hiwat = cc;
    398   1.1       cgd 	sb->sb_mbmax = min(cc * 2, sb_max);
    399   1.1       cgd 	if (sb->sb_lowat > sb->sb_hiwat)
    400   1.1       cgd 		sb->sb_lowat = sb->sb_hiwat;
    401   1.1       cgd 	return (1);
    402   1.1       cgd }
    403   1.1       cgd 
    404   1.1       cgd /*
    405   1.1       cgd  * Free mbufs held by a socket, and reserved mbuf space.
    406   1.1       cgd  */
    407   1.7   mycroft void
    408   1.1       cgd sbrelease(sb)
    409   1.1       cgd 	struct sockbuf *sb;
    410   1.1       cgd {
    411   1.1       cgd 
    412   1.1       cgd 	sbflush(sb);
    413   1.1       cgd 	sb->sb_hiwat = sb->sb_mbmax = 0;
    414   1.1       cgd }
    415   1.1       cgd 
    416   1.1       cgd /*
    417   1.1       cgd  * Routines to add and remove
    418   1.1       cgd  * data from an mbuf queue.
    419   1.1       cgd  *
    420   1.1       cgd  * The routines sbappend() or sbappendrecord() are normally called to
    421   1.1       cgd  * append new mbufs to a socket buffer, after checking that adequate
    422   1.1       cgd  * space is available, comparing the function sbspace() with the amount
    423   1.1       cgd  * of data to be added.  sbappendrecord() differs from sbappend() in
    424   1.1       cgd  * that data supplied is treated as the beginning of a new record.
    425   1.1       cgd  * To place a sender's address, optional access rights, and data in a
    426   1.1       cgd  * socket receive buffer, sbappendaddr() should be used.  To place
    427   1.1       cgd  * access rights and data in a socket receive buffer, sbappendrights()
    428   1.1       cgd  * should be used.  In either case, the new data begins a new record.
    429   1.1       cgd  * Note that unlike sbappend() and sbappendrecord(), these routines check
    430   1.1       cgd  * for the caller that there will be enough space to store the data.
    431   1.1       cgd  * Each fails if there is not enough space, or if it cannot find mbufs
    432   1.1       cgd  * to store additional information in.
    433   1.1       cgd  *
    434   1.1       cgd  * Reliable protocols may use the socket send buffer to hold data
    435   1.1       cgd  * awaiting acknowledgement.  Data is normally copied from a socket
    436   1.1       cgd  * send buffer in a protocol with m_copy for output to a peer,
    437   1.1       cgd  * and then removing the data from the socket buffer with sbdrop()
    438   1.1       cgd  * or sbdroprecord() when the data is acknowledged by the peer.
    439   1.1       cgd  */
    440   1.1       cgd 
    441   1.1       cgd /*
    442   1.1       cgd  * Append mbuf chain m to the last record in the
    443   1.1       cgd  * socket buffer sb.  The additional space associated
    444   1.1       cgd  * the mbuf chain is recorded in sb.  Empty mbufs are
    445   1.1       cgd  * discarded and mbufs are compacted where possible.
    446   1.1       cgd  */
    447   1.7   mycroft void
    448   1.1       cgd sbappend(sb, m)
    449   1.1       cgd 	struct sockbuf *sb;
    450   1.1       cgd 	struct mbuf *m;
    451   1.1       cgd {
    452   1.1       cgd 	register struct mbuf *n;
    453   1.1       cgd 
    454   1.1       cgd 	if (m == 0)
    455   1.1       cgd 		return;
    456  1.11  christos 	if ((n = sb->sb_mb) != NULL) {
    457   1.1       cgd 		while (n->m_nextpkt)
    458   1.1       cgd 			n = n->m_nextpkt;
    459   1.1       cgd 		do {
    460   1.1       cgd 			if (n->m_flags & M_EOR) {
    461   1.1       cgd 				sbappendrecord(sb, m); /* XXXXXX!!!! */
    462   1.1       cgd 				return;
    463   1.1       cgd 			}
    464   1.1       cgd 		} while (n->m_next && (n = n->m_next));
    465   1.1       cgd 	}
    466   1.1       cgd 	sbcompress(sb, m, n);
    467   1.1       cgd }
    468   1.1       cgd 
    469   1.1       cgd #ifdef SOCKBUF_DEBUG
    470   1.7   mycroft void
    471   1.1       cgd sbcheck(sb)
    472   1.1       cgd 	register struct sockbuf *sb;
    473   1.1       cgd {
    474   1.1       cgd 	register struct mbuf *m;
    475   1.1       cgd 	register int len = 0, mbcnt = 0;
    476   1.1       cgd 
    477   1.1       cgd 	for (m = sb->sb_mb; m; m = m->m_next) {
    478   1.1       cgd 		len += m->m_len;
    479   1.1       cgd 		mbcnt += MSIZE;
    480   1.1       cgd 		if (m->m_flags & M_EXT)
    481   1.1       cgd 			mbcnt += m->m_ext.ext_size;
    482   1.1       cgd 		if (m->m_nextpkt)
    483   1.1       cgd 			panic("sbcheck nextpkt");
    484   1.1       cgd 	}
    485   1.1       cgd 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
    486  1.15  christos 		printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
    487   1.1       cgd 		    mbcnt, sb->sb_mbcnt);
    488   1.1       cgd 		panic("sbcheck");
    489   1.1       cgd 	}
    490   1.1       cgd }
    491   1.1       cgd #endif
    492   1.1       cgd 
    493   1.1       cgd /*
    494   1.1       cgd  * As above, except the mbuf chain
    495   1.1       cgd  * begins a new record.
    496   1.1       cgd  */
    497   1.7   mycroft void
    498   1.1       cgd sbappendrecord(sb, m0)
    499   1.1       cgd 	register struct sockbuf *sb;
    500   1.1       cgd 	register struct mbuf *m0;
    501   1.1       cgd {
    502   1.1       cgd 	register struct mbuf *m;
    503   1.1       cgd 
    504   1.1       cgd 	if (m0 == 0)
    505   1.1       cgd 		return;
    506  1.11  christos 	if ((m = sb->sb_mb) != NULL)
    507   1.1       cgd 		while (m->m_nextpkt)
    508   1.1       cgd 			m = m->m_nextpkt;
    509   1.1       cgd 	/*
    510   1.1       cgd 	 * Put the first mbuf on the queue.
    511   1.1       cgd 	 * Note this permits zero length records.
    512   1.1       cgd 	 */
    513   1.1       cgd 	sballoc(sb, m0);
    514   1.1       cgd 	if (m)
    515   1.1       cgd 		m->m_nextpkt = m0;
    516   1.1       cgd 	else
    517   1.1       cgd 		sb->sb_mb = m0;
    518   1.1       cgd 	m = m0->m_next;
    519   1.1       cgd 	m0->m_next = 0;
    520   1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    521   1.1       cgd 		m0->m_flags &= ~M_EOR;
    522   1.1       cgd 		m->m_flags |= M_EOR;
    523   1.1       cgd 	}
    524   1.1       cgd 	sbcompress(sb, m, m0);
    525   1.1       cgd }
    526   1.1       cgd 
    527   1.1       cgd /*
    528   1.1       cgd  * As above except that OOB data
    529   1.1       cgd  * is inserted at the beginning of the sockbuf,
    530   1.1       cgd  * but after any other OOB data.
    531   1.1       cgd  */
    532   1.7   mycroft void
    533   1.1       cgd sbinsertoob(sb, m0)
    534   1.1       cgd 	register struct sockbuf *sb;
    535   1.1       cgd 	register struct mbuf *m0;
    536   1.1       cgd {
    537   1.1       cgd 	register struct mbuf *m;
    538   1.1       cgd 	register struct mbuf **mp;
    539   1.1       cgd 
    540   1.1       cgd 	if (m0 == 0)
    541   1.1       cgd 		return;
    542  1.11  christos 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
    543   1.1       cgd 	    again:
    544   1.1       cgd 		switch (m->m_type) {
    545   1.1       cgd 
    546   1.1       cgd 		case MT_OOBDATA:
    547   1.1       cgd 			continue;		/* WANT next train */
    548   1.1       cgd 
    549   1.1       cgd 		case MT_CONTROL:
    550  1.11  christos 			if ((m = m->m_next) != NULL)
    551   1.1       cgd 				goto again;	/* inspect THIS train further */
    552   1.1       cgd 		}
    553   1.1       cgd 		break;
    554   1.1       cgd 	}
    555   1.1       cgd 	/*
    556   1.1       cgd 	 * Put the first mbuf on the queue.
    557   1.1       cgd 	 * Note this permits zero length records.
    558   1.1       cgd 	 */
    559   1.1       cgd 	sballoc(sb, m0);
    560   1.1       cgd 	m0->m_nextpkt = *mp;
    561   1.1       cgd 	*mp = m0;
    562   1.1       cgd 	m = m0->m_next;
    563   1.1       cgd 	m0->m_next = 0;
    564   1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    565   1.1       cgd 		m0->m_flags &= ~M_EOR;
    566   1.1       cgd 		m->m_flags |= M_EOR;
    567   1.1       cgd 	}
    568   1.1       cgd 	sbcompress(sb, m, m0);
    569   1.1       cgd }
    570   1.1       cgd 
    571   1.1       cgd /*
    572   1.1       cgd  * Append address and data, and optionally, control (ancillary) data
    573   1.1       cgd  * to the receive queue of a socket.  If present,
    574   1.1       cgd  * m0 must include a packet header with total length.
    575   1.1       cgd  * Returns 0 if no space in sockbuf or insufficient mbufs.
    576   1.1       cgd  */
    577   1.7   mycroft int
    578   1.1       cgd sbappendaddr(sb, asa, m0, control)
    579   1.1       cgd 	register struct sockbuf *sb;
    580   1.1       cgd 	struct sockaddr *asa;
    581   1.1       cgd 	struct mbuf *m0, *control;
    582   1.1       cgd {
    583   1.1       cgd 	register struct mbuf *m, *n;
    584   1.1       cgd 	int space = asa->sa_len;
    585   1.1       cgd 
    586   1.1       cgd if (m0 && (m0->m_flags & M_PKTHDR) == 0)
    587   1.1       cgd panic("sbappendaddr");
    588   1.1       cgd 	if (m0)
    589   1.1       cgd 		space += m0->m_pkthdr.len;
    590   1.1       cgd 	for (n = control; n; n = n->m_next) {
    591   1.1       cgd 		space += n->m_len;
    592   1.1       cgd 		if (n->m_next == 0)	/* keep pointer to last control buf */
    593   1.1       cgd 			break;
    594   1.1       cgd 	}
    595   1.1       cgd 	if (space > sbspace(sb))
    596   1.1       cgd 		return (0);
    597   1.1       cgd 	MGET(m, M_DONTWAIT, MT_SONAME);
    598   1.1       cgd 	if (m == 0)
    599   1.1       cgd 		return (0);
    600  1.20   thorpej 	if (asa->sa_len > MLEN) {
    601  1.20   thorpej 		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
    602  1.20   thorpej 		if ((m->m_flags & M_EXT) == 0) {
    603  1.20   thorpej 			m_free(m);
    604  1.20   thorpej 			return (0);
    605  1.20   thorpej 		}
    606  1.20   thorpej 	}
    607   1.1       cgd 	m->m_len = asa->sa_len;
    608  1.26     perry 	memcpy(mtod(m, caddr_t), (caddr_t)asa, asa->sa_len);
    609   1.1       cgd 	if (n)
    610   1.1       cgd 		n->m_next = m0;		/* concatenate data to control */
    611   1.1       cgd 	else
    612   1.1       cgd 		control = m0;
    613   1.1       cgd 	m->m_next = control;
    614   1.1       cgd 	for (n = m; n; n = n->m_next)
    615   1.1       cgd 		sballoc(sb, n);
    616  1.11  christos 	if ((n = sb->sb_mb) != NULL) {
    617   1.1       cgd 		while (n->m_nextpkt)
    618   1.1       cgd 			n = n->m_nextpkt;
    619   1.1       cgd 		n->m_nextpkt = m;
    620   1.1       cgd 	} else
    621   1.1       cgd 		sb->sb_mb = m;
    622   1.1       cgd 	return (1);
    623   1.1       cgd }
    624   1.1       cgd 
    625   1.7   mycroft int
    626   1.1       cgd sbappendcontrol(sb, m0, control)
    627   1.1       cgd 	struct sockbuf *sb;
    628   1.7   mycroft 	struct mbuf *m0, *control;
    629   1.1       cgd {
    630   1.1       cgd 	register struct mbuf *m, *n;
    631   1.1       cgd 	int space = 0;
    632   1.1       cgd 
    633   1.1       cgd 	if (control == 0)
    634   1.1       cgd 		panic("sbappendcontrol");
    635   1.1       cgd 	for (m = control; ; m = m->m_next) {
    636   1.1       cgd 		space += m->m_len;
    637   1.1       cgd 		if (m->m_next == 0)
    638   1.1       cgd 			break;
    639   1.1       cgd 	}
    640   1.1       cgd 	n = m;			/* save pointer to last control buffer */
    641   1.1       cgd 	for (m = m0; m; m = m->m_next)
    642   1.1       cgd 		space += m->m_len;
    643   1.1       cgd 	if (space > sbspace(sb))
    644   1.1       cgd 		return (0);
    645   1.1       cgd 	n->m_next = m0;			/* concatenate data to control */
    646   1.1       cgd 	for (m = control; m; m = m->m_next)
    647   1.1       cgd 		sballoc(sb, m);
    648  1.11  christos 	if ((n = sb->sb_mb) != NULL) {
    649   1.1       cgd 		while (n->m_nextpkt)
    650   1.1       cgd 			n = n->m_nextpkt;
    651   1.1       cgd 		n->m_nextpkt = control;
    652   1.1       cgd 	} else
    653   1.1       cgd 		sb->sb_mb = control;
    654   1.1       cgd 	return (1);
    655   1.1       cgd }
    656   1.1       cgd 
    657   1.1       cgd /*
    658   1.1       cgd  * Compress mbuf chain m into the socket
    659   1.1       cgd  * buffer sb following mbuf n.  If n
    660   1.1       cgd  * is null, the buffer is presumed empty.
    661   1.1       cgd  */
    662   1.7   mycroft void
    663   1.1       cgd sbcompress(sb, m, n)
    664   1.1       cgd 	register struct sockbuf *sb;
    665   1.1       cgd 	register struct mbuf *m, *n;
    666   1.1       cgd {
    667   1.1       cgd 	register int eor = 0;
    668   1.1       cgd 	register struct mbuf *o;
    669   1.1       cgd 
    670   1.1       cgd 	while (m) {
    671   1.1       cgd 		eor |= m->m_flags & M_EOR;
    672   1.1       cgd 		if (m->m_len == 0 &&
    673   1.1       cgd 		    (eor == 0 ||
    674   1.1       cgd 		     (((o = m->m_next) || (o = n)) &&
    675   1.1       cgd 		      o->m_type == m->m_type))) {
    676   1.1       cgd 			m = m_free(m);
    677   1.1       cgd 			continue;
    678   1.1       cgd 		}
    679  1.31   mycroft 		if (n && (n->m_flags & M_EOR) == 0 && n->m_type == m->m_type &&
    680  1.31   mycroft 		    (((n->m_flags & M_EXT) == 0 &&
    681  1.33   mycroft 		      n->m_data + n->m_len + m->m_len <= &n->m_dat[MLEN]) ||
    682  1.31   mycroft 		     ((~n->m_flags & (M_EXT|M_CLUSTER)) == 0 &&
    683  1.32      matt 		      !MCLISREFERENCED(n) &&
    684  1.33   mycroft 		      n->m_data + n->m_len + m->m_len <= &n->m_ext.ext_buf[MCLBYTES]))) {
    685  1.26     perry 			memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t),
    686   1.1       cgd 			    (unsigned)m->m_len);
    687   1.1       cgd 			n->m_len += m->m_len;
    688   1.1       cgd 			sb->sb_cc += m->m_len;
    689   1.1       cgd 			m = m_free(m);
    690   1.1       cgd 			continue;
    691   1.1       cgd 		}
    692   1.1       cgd 		if (n)
    693   1.1       cgd 			n->m_next = m;
    694   1.1       cgd 		else
    695   1.1       cgd 			sb->sb_mb = m;
    696   1.1       cgd 		sballoc(sb, m);
    697   1.1       cgd 		n = m;
    698   1.1       cgd 		m->m_flags &= ~M_EOR;
    699   1.1       cgd 		m = m->m_next;
    700   1.1       cgd 		n->m_next = 0;
    701   1.1       cgd 	}
    702   1.1       cgd 	if (eor) {
    703   1.1       cgd 		if (n)
    704   1.1       cgd 			n->m_flags |= eor;
    705   1.1       cgd 		else
    706  1.15  christos 			printf("semi-panic: sbcompress\n");
    707   1.1       cgd 	}
    708   1.1       cgd }
    709   1.1       cgd 
    710   1.1       cgd /*
    711   1.1       cgd  * Free all mbufs in a sockbuf.
    712   1.1       cgd  * Check that all resources are reclaimed.
    713   1.1       cgd  */
    714   1.7   mycroft void
    715   1.1       cgd sbflush(sb)
    716   1.1       cgd 	register struct sockbuf *sb;
    717   1.1       cgd {
    718   1.1       cgd 
    719   1.1       cgd 	if (sb->sb_flags & SB_LOCK)
    720   1.1       cgd 		panic("sbflush");
    721   1.1       cgd 	while (sb->sb_mbcnt)
    722   1.1       cgd 		sbdrop(sb, (int)sb->sb_cc);
    723   1.1       cgd 	if (sb->sb_cc || sb->sb_mb)
    724   1.1       cgd 		panic("sbflush 2");
    725   1.1       cgd }
    726   1.1       cgd 
    727   1.1       cgd /*
    728   1.1       cgd  * Drop data from (the front of) a sockbuf.
    729   1.1       cgd  */
    730   1.7   mycroft void
    731   1.1       cgd sbdrop(sb, len)
    732   1.1       cgd 	register struct sockbuf *sb;
    733   1.1       cgd 	register int len;
    734   1.1       cgd {
    735   1.1       cgd 	register struct mbuf *m, *mn;
    736   1.1       cgd 	struct mbuf *next;
    737   1.1       cgd 
    738   1.1       cgd 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
    739   1.1       cgd 	while (len > 0) {
    740   1.1       cgd 		if (m == 0) {
    741   1.1       cgd 			if (next == 0)
    742   1.1       cgd 				panic("sbdrop");
    743   1.1       cgd 			m = next;
    744   1.1       cgd 			next = m->m_nextpkt;
    745   1.1       cgd 			continue;
    746   1.1       cgd 		}
    747   1.1       cgd 		if (m->m_len > len) {
    748   1.1       cgd 			m->m_len -= len;
    749   1.1       cgd 			m->m_data += len;
    750   1.1       cgd 			sb->sb_cc -= len;
    751   1.1       cgd 			break;
    752   1.1       cgd 		}
    753   1.1       cgd 		len -= m->m_len;
    754   1.1       cgd 		sbfree(sb, m);
    755   1.1       cgd 		MFREE(m, mn);
    756   1.1       cgd 		m = mn;
    757   1.1       cgd 	}
    758   1.1       cgd 	while (m && m->m_len == 0) {
    759   1.1       cgd 		sbfree(sb, m);
    760   1.1       cgd 		MFREE(m, mn);
    761   1.1       cgd 		m = mn;
    762   1.1       cgd 	}
    763   1.1       cgd 	if (m) {
    764   1.1       cgd 		sb->sb_mb = m;
    765   1.1       cgd 		m->m_nextpkt = next;
    766   1.1       cgd 	} else
    767   1.1       cgd 		sb->sb_mb = next;
    768   1.1       cgd }
    769   1.1       cgd 
    770   1.1       cgd /*
    771   1.1       cgd  * Drop a record off the front of a sockbuf
    772   1.1       cgd  * and move the next record to the front.
    773   1.1       cgd  */
    774   1.7   mycroft void
    775   1.1       cgd sbdroprecord(sb)
    776   1.1       cgd 	register struct sockbuf *sb;
    777   1.1       cgd {
    778   1.1       cgd 	register struct mbuf *m, *mn;
    779   1.1       cgd 
    780   1.1       cgd 	m = sb->sb_mb;
    781   1.1       cgd 	if (m) {
    782   1.1       cgd 		sb->sb_mb = m->m_nextpkt;
    783   1.1       cgd 		do {
    784   1.1       cgd 			sbfree(sb, m);
    785   1.1       cgd 			MFREE(m, mn);
    786  1.11  christos 		} while ((m = mn) != NULL);
    787   1.1       cgd 	}
    788  1.19   thorpej }
    789  1.19   thorpej 
    790  1.19   thorpej /*
    791  1.19   thorpej  * Create a "control" mbuf containing the specified data
    792  1.19   thorpej  * with the specified type for presentation on a socket buffer.
    793  1.19   thorpej  */
    794  1.19   thorpej struct mbuf *
    795  1.19   thorpej sbcreatecontrol(p, size, type, level)
    796  1.19   thorpej 	caddr_t p;
    797  1.19   thorpej 	register int size;
    798  1.19   thorpej 	int type, level;
    799  1.19   thorpej {
    800  1.19   thorpej 	register struct cmsghdr *cp;
    801  1.19   thorpej 	struct mbuf *m;
    802  1.19   thorpej 
    803  1.30    itojun 	if (size + sizeof(*cp) > MCLBYTES) {
    804  1.30    itojun 		printf("sbcreatecontrol: message too large %d\n", size);
    805  1.30    itojun 		return NULL;
    806  1.30    itojun 	}
    807  1.30    itojun 
    808  1.19   thorpej 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
    809  1.19   thorpej 		return ((struct mbuf *) NULL);
    810  1.30    itojun 	if (size + sizeof(*cp) > MLEN) {
    811  1.30    itojun 		MCLGET(m, M_DONTWAIT);
    812  1.30    itojun 		if ((m->m_flags & M_EXT) == 0) {
    813  1.30    itojun 			m_free(m);
    814  1.30    itojun 			return NULL;
    815  1.30    itojun 		}
    816  1.30    itojun 	}
    817  1.19   thorpej 	cp = mtod(m, struct cmsghdr *);
    818  1.26     perry 	memcpy(CMSG_DATA(cp), p, size);
    819  1.19   thorpej 	size += sizeof(*cp);
    820  1.19   thorpej 	m->m_len = size;
    821  1.19   thorpej 	cp->cmsg_len = size;
    822  1.19   thorpej 	cp->cmsg_level = level;
    823  1.19   thorpej 	cp->cmsg_type = type;
    824  1.19   thorpej 	return (m);
    825   1.1       cgd }
    826