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