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