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