xref: /openbsd/sys/kern/uipc_socket2.c (revision 74ae6390)
1 /*	$OpenBSD: uipc_socket2.c,v 1.65 2016/09/02 13:28:21 bluhm 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 	rw_init(&so->so_rcv.sb_lock, "sbsndl");
189 	rw_init(&so->so_snd.sb_lock, "sbrcvl");
190 
191 	soqinsque(head, so, soqueue);
192 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, NULL, NULL, NULL,
193 	    curproc)) {
194 		(void) soqremque(so, soqueue);
195 		pool_put(&socket_pool, so);
196 		return (NULL);
197 	}
198 	if (connstatus) {
199 		sorwakeup(head);
200 		wakeup(&head->so_timeo);
201 		so->so_state |= connstatus;
202 	}
203 	return (so);
204 }
205 
206 void
207 soqinsque(struct socket *head, struct socket *so, int q)
208 {
209 
210 #ifdef DIAGNOSTIC
211 	if (so->so_onq != NULL)
212 		panic("soqinsque");
213 #endif
214 
215 	so->so_head = head;
216 	if (q == 0) {
217 		head->so_q0len++;
218 		so->so_onq = &head->so_q0;
219 	} else {
220 		head->so_qlen++;
221 		so->so_onq = &head->so_q;
222 	}
223 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
224 }
225 
226 int
227 soqremque(struct socket *so, int q)
228 {
229 	struct socket *head;
230 
231 	head = so->so_head;
232 	if (q == 0) {
233 		if (so->so_onq != &head->so_q0)
234 			return (0);
235 		head->so_q0len--;
236 	} else {
237 		if (so->so_onq != &head->so_q)
238 			return (0);
239 		head->so_qlen--;
240 	}
241 	TAILQ_REMOVE(so->so_onq, so, so_qe);
242 	so->so_onq = NULL;
243 	so->so_head = NULL;
244 	return (1);
245 }
246 
247 /*
248  * Socantsendmore indicates that no more data will be sent on the
249  * socket; it would normally be applied to a socket when the user
250  * informs the system that no more data is to be sent, by the protocol
251  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
252  * will be received, and will normally be applied to the socket by a
253  * protocol when it detects that the peer will send no more data.
254  * Data queued for reading in the socket may yet be read.
255  */
256 
257 void
258 socantsendmore(struct socket *so)
259 {
260 
261 	so->so_state |= SS_CANTSENDMORE;
262 	sowwakeup(so);
263 }
264 
265 void
266 socantrcvmore(struct socket *so)
267 {
268 
269 	so->so_state |= SS_CANTRCVMORE;
270 	sorwakeup(so);
271 }
272 
273 /*
274  * Wait for data to arrive at/drain from a socket buffer.
275  */
276 int
277 sbwait(struct sockbuf *sb)
278 {
279 	splsoftassert(IPL_SOFTNET);
280 
281 	sb->sb_flagsintr |= SB_WAIT;
282 	return (tsleep(&sb->sb_cc,
283 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "netio",
284 	    sb->sb_timeo));
285 }
286 
287 /*
288  * Lock a sockbuf already known to be locked;
289  * return any error returned from sleep (EINTR).
290  */
291 int
292 sblock(struct sockbuf *sb, int wf)
293 {
294 	int error;
295 
296 	error = rw_enter(&sb->sb_lock, RW_WRITE |
297 	    (sb->sb_flags & SB_NOINTR ? 0 : RW_INTR) |
298 	    (wf == M_WAITOK ? 0 : RW_NOSLEEP));
299 
300 	if (error == EBUSY)
301 		error = EWOULDBLOCK;
302 	return (error);
303 }
304 
305 void
306 sbunlock(struct sockbuf *sb)
307 {
308 	rw_exit(&sb->sb_lock);
309 }
310 
311 
312 /*
313  * Wakeup processes waiting on a socket buffer.
314  * Do asynchronous notification via SIGIO
315  * if the socket has the SS_ASYNC flag set.
316  */
317 void
318 sowakeup(struct socket *so, struct sockbuf *sb)
319 {
320 	int s = splsoftnet();
321 
322 	selwakeup(&sb->sb_sel);
323 	sb->sb_flagsintr &= ~SB_SEL;
324 	if (sb->sb_flagsintr & SB_WAIT) {
325 		sb->sb_flagsintr &= ~SB_WAIT;
326 		wakeup(&sb->sb_cc);
327 	}
328 	splx(s);
329 	if (so->so_state & SS_ASYNC)
330 		csignal(so->so_pgid, SIGIO, so->so_siguid, so->so_sigeuid);
331 }
332 
333 /*
334  * Socket buffer (struct sockbuf) utility routines.
335  *
336  * Each socket contains two socket buffers: one for sending data and
337  * one for receiving data.  Each buffer contains a queue of mbufs,
338  * information about the number of mbufs and amount of data in the
339  * queue, and other fields allowing select() statements and notification
340  * on data availability to be implemented.
341  *
342  * Data stored in a socket buffer is maintained as a list of records.
343  * Each record is a list of mbufs chained together with the m_next
344  * field.  Records are chained together with the m_nextpkt field. The upper
345  * level routine soreceive() expects the following conventions to be
346  * observed when placing information in the receive buffer:
347  *
348  * 1. If the protocol requires each message be preceded by the sender's
349  *    name, then a record containing that name must be present before
350  *    any associated data (mbuf's must be of type MT_SONAME).
351  * 2. If the protocol supports the exchange of ``access rights'' (really
352  *    just additional data associated with the message), and there are
353  *    ``rights'' to be received, then a record containing this data
354  *    should be present (mbuf's must be of type MT_CONTROL).
355  * 3. If a name or rights record exists, then it must be followed by
356  *    a data record, perhaps of zero length.
357  *
358  * Before using a new socket structure it is first necessary to reserve
359  * buffer space to the socket, by calling sbreserve().  This should commit
360  * some of the available buffer space in the system buffer pool for the
361  * socket (currently, it does nothing but enforce limits).  The space
362  * should be released by calling sbrelease() when the socket is destroyed.
363  */
364 
365 int
366 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
367 {
368 
369 	if (sbreserve(&so->so_snd, sndcc))
370 		goto bad;
371 	if (sbreserve(&so->so_rcv, rcvcc))
372 		goto bad2;
373 	so->so_snd.sb_wat = sndcc;
374 	so->so_rcv.sb_wat = rcvcc;
375 	if (so->so_rcv.sb_lowat == 0)
376 		so->so_rcv.sb_lowat = 1;
377 	if (so->so_snd.sb_lowat == 0)
378 		so->so_snd.sb_lowat = MCLBYTES;
379 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
380 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
381 	return (0);
382 bad2:
383 	sbrelease(&so->so_snd);
384 bad:
385 	return (ENOBUFS);
386 }
387 
388 /*
389  * Allot mbufs to a sockbuf.
390  * Attempt to scale mbmax so that mbcnt doesn't become limiting
391  * if buffering efficiency is near the normal case.
392  */
393 int
394 sbreserve(struct sockbuf *sb, u_long cc)
395 {
396 
397 	if (cc == 0 || cc > sb_max)
398 		return (1);
399 	sb->sb_hiwat = cc;
400 	sb->sb_mbmax = max(3 * MAXMCLBYTES,
401 	    min(cc * 2, sb_max + (sb_max / MCLBYTES) * MSIZE));
402 	if (sb->sb_lowat > sb->sb_hiwat)
403 		sb->sb_lowat = sb->sb_hiwat;
404 	return (0);
405 }
406 
407 /*
408  * In low memory situation, do not accept any greater than normal request.
409  */
410 int
411 sbcheckreserve(u_long cnt, u_long defcnt)
412 {
413 	if (cnt > defcnt && sbchecklowmem())
414 		return (ENOBUFS);
415 	return (0);
416 }
417 
418 int
419 sbchecklowmem(void)
420 {
421 	static int sblowmem;
422 
423 	if (mclpools[0].pr_nout < mclpools[0].pr_hardlimit * 60 / 100 ||
424 	    mbpool.pr_nout < mbpool.pr_hardlimit * 60 / 100)
425 		sblowmem = 0;
426 	if (mclpools[0].pr_nout > mclpools[0].pr_hardlimit * 80 / 100 ||
427 	    mbpool.pr_nout > mbpool.pr_hardlimit * 80 / 100)
428 		sblowmem = 1;
429 	return (sblowmem);
430 }
431 
432 /*
433  * Free mbufs held by a socket, and reserved mbuf space.
434  */
435 void
436 sbrelease(struct sockbuf *sb)
437 {
438 
439 	sbflush(sb);
440 	sb->sb_hiwat = sb->sb_mbmax = 0;
441 }
442 
443 /*
444  * Routines to add and remove
445  * data from an mbuf queue.
446  *
447  * The routines sbappend() or sbappendrecord() are normally called to
448  * append new mbufs to a socket buffer, after checking that adequate
449  * space is available, comparing the function sbspace() with the amount
450  * of data to be added.  sbappendrecord() differs from sbappend() in
451  * that data supplied is treated as the beginning of a new record.
452  * To place a sender's address, optional access rights, and data in a
453  * socket receive buffer, sbappendaddr() should be used.  To place
454  * access rights and data in a socket receive buffer, sbappendrights()
455  * should be used.  In either case, the new data begins a new record.
456  * Note that unlike sbappend() and sbappendrecord(), these routines check
457  * for the caller that there will be enough space to store the data.
458  * Each fails if there is not enough space, or if it cannot find mbufs
459  * to store additional information in.
460  *
461  * Reliable protocols may use the socket send buffer to hold data
462  * awaiting acknowledgement.  Data is normally copied from a socket
463  * send buffer in a protocol with m_copym for output to a peer,
464  * and then removing the data from the socket buffer with sbdrop()
465  * or sbdroprecord() when the data is acknowledged by the peer.
466  */
467 
468 #ifdef SOCKBUF_DEBUG
469 void
470 sblastrecordchk(struct sockbuf *sb, const char *where)
471 {
472 	struct mbuf *m = sb->sb_mb;
473 
474 	while (m && m->m_nextpkt)
475 		m = m->m_nextpkt;
476 
477 	if (m != sb->sb_lastrecord) {
478 		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
479 		    sb->sb_mb, sb->sb_lastrecord, m);
480 		printf("packet chain:\n");
481 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
482 			printf("\t%p\n", m);
483 		panic("sblastrecordchk from %s", where);
484 	}
485 }
486 
487 void
488 sblastmbufchk(struct sockbuf *sb, const char *where)
489 {
490 	struct mbuf *m = sb->sb_mb;
491 	struct mbuf *n;
492 
493 	while (m && m->m_nextpkt)
494 		m = m->m_nextpkt;
495 
496 	while (m && m->m_next)
497 		m = m->m_next;
498 
499 	if (m != sb->sb_mbtail) {
500 		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
501 		    sb->sb_mb, sb->sb_mbtail, m);
502 		printf("packet tree:\n");
503 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
504 			printf("\t");
505 			for (n = m; n != NULL; n = n->m_next)
506 				printf("%p ", n);
507 			printf("\n");
508 		}
509 		panic("sblastmbufchk from %s", where);
510 	}
511 }
512 #endif /* SOCKBUF_DEBUG */
513 
514 #define	SBLINKRECORD(sb, m0)						\
515 do {									\
516 	if ((sb)->sb_lastrecord != NULL)				\
517 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
518 	else								\
519 		(sb)->sb_mb = (m0);					\
520 	(sb)->sb_lastrecord = (m0);					\
521 } while (/*CONSTCOND*/0)
522 
523 /*
524  * Append mbuf chain m to the last record in the
525  * socket buffer sb.  The additional space associated
526  * the mbuf chain is recorded in sb.  Empty mbufs are
527  * discarded and mbufs are compacted where possible.
528  */
529 void
530 sbappend(struct sockbuf *sb, struct mbuf *m)
531 {
532 	struct mbuf *n;
533 
534 	if (m == NULL)
535 		return;
536 
537 	SBLASTRECORDCHK(sb, "sbappend 1");
538 
539 	if ((n = sb->sb_lastrecord) != NULL) {
540 		/*
541 		 * XXX Would like to simply use sb_mbtail here, but
542 		 * XXX I need to verify that I won't miss an EOR that
543 		 * XXX way.
544 		 */
545 		do {
546 			if (n->m_flags & M_EOR) {
547 				sbappendrecord(sb, m); /* XXXXXX!!!! */
548 				return;
549 			}
550 		} while (n->m_next && (n = n->m_next));
551 	} else {
552 		/*
553 		 * If this is the first record in the socket buffer, it's
554 		 * also the last record.
555 		 */
556 		sb->sb_lastrecord = m;
557 	}
558 	sbcompress(sb, m, n);
559 	SBLASTRECORDCHK(sb, "sbappend 2");
560 }
561 
562 /*
563  * This version of sbappend() should only be used when the caller
564  * absolutely knows that there will never be more than one record
565  * in the socket buffer, that is, a stream protocol (such as TCP).
566  */
567 void
568 sbappendstream(struct sockbuf *sb, struct mbuf *m)
569 {
570 
571 	KDASSERT(m->m_nextpkt == NULL);
572 	KASSERT(sb->sb_mb == sb->sb_lastrecord);
573 
574 	SBLASTMBUFCHK(sb, __func__);
575 
576 	sbcompress(sb, m, sb->sb_mbtail);
577 
578 	sb->sb_lastrecord = sb->sb_mb;
579 	SBLASTRECORDCHK(sb, __func__);
580 }
581 
582 #ifdef SOCKBUF_DEBUG
583 void
584 sbcheck(struct sockbuf *sb)
585 {
586 	struct mbuf *m, *n;
587 	u_long len = 0, mbcnt = 0;
588 
589 	for (m = sb->sb_mb; m; m = m->m_nextpkt) {
590 		for (n = m; n; n = n->m_next) {
591 			len += n->m_len;
592 			mbcnt += MSIZE;
593 			if (n->m_flags & M_EXT)
594 				mbcnt += n->m_ext.ext_size;
595 			if (m != n && n->m_nextpkt)
596 				panic("sbcheck nextpkt");
597 		}
598 	}
599 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
600 		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
601 		    mbcnt, sb->sb_mbcnt);
602 		panic("sbcheck");
603 	}
604 }
605 #endif
606 
607 /*
608  * As above, except the mbuf chain
609  * begins a new record.
610  */
611 void
612 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
613 {
614 	struct mbuf *m;
615 
616 	if (m0 == NULL)
617 		return;
618 
619 	/*
620 	 * Put the first mbuf on the queue.
621 	 * Note this permits zero length records.
622 	 */
623 	sballoc(sb, m0);
624 	SBLASTRECORDCHK(sb, "sbappendrecord 1");
625 	SBLINKRECORD(sb, m0);
626 	m = m0->m_next;
627 	m0->m_next = NULL;
628 	if (m && (m0->m_flags & M_EOR)) {
629 		m0->m_flags &= ~M_EOR;
630 		m->m_flags |= M_EOR;
631 	}
632 	sbcompress(sb, m, m0);
633 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
634 }
635 
636 /*
637  * As above except that OOB data
638  * is inserted at the beginning of the sockbuf,
639  * but after any other OOB data.
640  */
641 void
642 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
643 {
644 	struct mbuf *m, **mp;
645 
646 	if (m0 == NULL)
647 		return;
648 
649 	SBLASTRECORDCHK(sb, "sbinsertoob 1");
650 
651 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
652 	    again:
653 		switch (m->m_type) {
654 
655 		case MT_OOBDATA:
656 			continue;		/* WANT next train */
657 
658 		case MT_CONTROL:
659 			if ((m = m->m_next) != NULL)
660 				goto again;	/* inspect THIS train further */
661 		}
662 		break;
663 	}
664 	/*
665 	 * Put the first mbuf on the queue.
666 	 * Note this permits zero length records.
667 	 */
668 	sballoc(sb, m0);
669 	m0->m_nextpkt = *mp;
670 	if (*mp == NULL) {
671 		/* m0 is actually the new tail */
672 		sb->sb_lastrecord = m0;
673 	}
674 	*mp = m0;
675 	m = m0->m_next;
676 	m0->m_next = NULL;
677 	if (m && (m0->m_flags & M_EOR)) {
678 		m0->m_flags &= ~M_EOR;
679 		m->m_flags |= M_EOR;
680 	}
681 	sbcompress(sb, m, m0);
682 	SBLASTRECORDCHK(sb, "sbinsertoob 2");
683 }
684 
685 /*
686  * Append address and data, and optionally, control (ancillary) data
687  * to the receive queue of a socket.  If present,
688  * m0 must include a packet header with total length.
689  * Returns 0 if no space in sockbuf or insufficient mbufs.
690  */
691 int
692 sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0,
693     struct mbuf *control)
694 {
695 	struct mbuf *m, *n, *nlast;
696 	int space = asa->sa_len;
697 
698 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
699 		panic("sbappendaddr");
700 	if (m0)
701 		space += m0->m_pkthdr.len;
702 	for (n = control; n; n = n->m_next) {
703 		space += n->m_len;
704 		if (n->m_next == NULL)	/* keep pointer to last control buf */
705 			break;
706 	}
707 	if (space > sbspace(sb))
708 		return (0);
709 	if (asa->sa_len > MLEN)
710 		return (0);
711 	MGET(m, M_DONTWAIT, MT_SONAME);
712 	if (m == NULL)
713 		return (0);
714 	m->m_len = asa->sa_len;
715 	memcpy(mtod(m, caddr_t), asa, asa->sa_len);
716 	if (n)
717 		n->m_next = m0;		/* concatenate data to control */
718 	else
719 		control = m0;
720 	m->m_next = control;
721 
722 	SBLASTRECORDCHK(sb, "sbappendaddr 1");
723 
724 	for (n = m; n->m_next != NULL; n = n->m_next)
725 		sballoc(sb, n);
726 	sballoc(sb, n);
727 	nlast = n;
728 	SBLINKRECORD(sb, m);
729 
730 	sb->sb_mbtail = nlast;
731 	SBLASTMBUFCHK(sb, "sbappendaddr");
732 
733 	SBLASTRECORDCHK(sb, "sbappendaddr 2");
734 
735 	return (1);
736 }
737 
738 int
739 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
740 {
741 	struct mbuf *m, *mlast, *n;
742 	int space = 0;
743 
744 	if (control == NULL)
745 		panic("sbappendcontrol");
746 	for (m = control; ; m = m->m_next) {
747 		space += m->m_len;
748 		if (m->m_next == NULL)
749 			break;
750 	}
751 	n = m;			/* save pointer to last control buffer */
752 	for (m = m0; m; m = m->m_next)
753 		space += m->m_len;
754 	if (space > sbspace(sb))
755 		return (0);
756 	n->m_next = m0;			/* concatenate data to control */
757 
758 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
759 
760 	for (m = control; m->m_next != NULL; m = m->m_next)
761 		sballoc(sb, m);
762 	sballoc(sb, m);
763 	mlast = m;
764 	SBLINKRECORD(sb, control);
765 
766 	sb->sb_mbtail = mlast;
767 	SBLASTMBUFCHK(sb, "sbappendcontrol");
768 
769 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
770 
771 	return (1);
772 }
773 
774 /*
775  * Compress mbuf chain m into the socket
776  * buffer sb following mbuf n.  If n
777  * is null, the buffer is presumed empty.
778  */
779 void
780 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
781 {
782 	int eor = 0;
783 	struct mbuf *o;
784 
785 	while (m) {
786 		eor |= m->m_flags & M_EOR;
787 		if (m->m_len == 0 &&
788 		    (eor == 0 ||
789 		    (((o = m->m_next) || (o = n)) &&
790 		    o->m_type == m->m_type))) {
791 			if (sb->sb_lastrecord == m)
792 				sb->sb_lastrecord = m->m_next;
793 			m = m_free(m);
794 			continue;
795 		}
796 		if (n && (n->m_flags & M_EOR) == 0 &&
797 		    /* M_TRAILINGSPACE() checks buffer writeability */
798 		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
799 		    m->m_len <= M_TRAILINGSPACE(n) &&
800 		    n->m_type == m->m_type) {
801 			memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t),
802 			    m->m_len);
803 			n->m_len += m->m_len;
804 			sb->sb_cc += m->m_len;
805 			if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
806 				sb->sb_datacc += m->m_len;
807 			m = m_free(m);
808 			continue;
809 		}
810 		if (n)
811 			n->m_next = m;
812 		else
813 			sb->sb_mb = m;
814 		sb->sb_mbtail = m;
815 		sballoc(sb, m);
816 		n = m;
817 		m->m_flags &= ~M_EOR;
818 		m = m->m_next;
819 		n->m_next = NULL;
820 	}
821 	if (eor) {
822 		if (n)
823 			n->m_flags |= eor;
824 		else
825 			printf("semi-panic: sbcompress");
826 	}
827 	SBLASTMBUFCHK(sb, __func__);
828 }
829 
830 /*
831  * Free all mbufs in a sockbuf.
832  * Check that all resources are reclaimed.
833  */
834 void
835 sbflush(struct sockbuf *sb)
836 {
837 
838 	rw_assert_unlocked(&sb->sb_lock);
839 
840 	while (sb->sb_mbcnt)
841 		sbdrop(sb, (int)sb->sb_cc);
842 
843 	KASSERT(sb->sb_cc == 0);
844 	KASSERT(sb->sb_datacc == 0);
845 	KASSERT(sb->sb_mb == NULL);
846 	KASSERT(sb->sb_mbtail == NULL);
847 	KASSERT(sb->sb_lastrecord == NULL);
848 }
849 
850 /*
851  * Drop data from (the front of) a sockbuf.
852  */
853 void
854 sbdrop(struct sockbuf *sb, int len)
855 {
856 	struct mbuf *m, *mn;
857 	struct mbuf *next;
858 
859 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
860 	while (len > 0) {
861 		if (m == NULL) {
862 			if (next == NULL)
863 				panic("sbdrop");
864 			m = next;
865 			next = m->m_nextpkt;
866 			continue;
867 		}
868 		if (m->m_len > len) {
869 			m->m_len -= len;
870 			m->m_data += len;
871 			sb->sb_cc -= len;
872 			if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME)
873 				sb->sb_datacc -= len;
874 			break;
875 		}
876 		len -= m->m_len;
877 		sbfree(sb, m);
878 		mn = m_free(m);
879 		m = mn;
880 	}
881 	while (m && m->m_len == 0) {
882 		sbfree(sb, m);
883 		mn = m_free(m);
884 		m = mn;
885 	}
886 	if (m) {
887 		sb->sb_mb = m;
888 		m->m_nextpkt = next;
889 	} else
890 		sb->sb_mb = next;
891 	/*
892 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
893 	 * makes sure sb_lastrecord is up-to-date if we dropped
894 	 * part of the last record.
895 	 */
896 	m = sb->sb_mb;
897 	if (m == NULL) {
898 		sb->sb_mbtail = NULL;
899 		sb->sb_lastrecord = NULL;
900 	} else if (m->m_nextpkt == NULL)
901 		sb->sb_lastrecord = m;
902 }
903 
904 /*
905  * Drop a record off the front of a sockbuf
906  * and move the next record to the front.
907  */
908 void
909 sbdroprecord(struct sockbuf *sb)
910 {
911 	struct mbuf *m, *mn;
912 
913 	m = sb->sb_mb;
914 	if (m) {
915 		sb->sb_mb = m->m_nextpkt;
916 		do {
917 			sbfree(sb, m);
918 			mn = m_free(m);
919 		} while ((m = mn) != NULL);
920 	}
921 	SB_EMPTY_FIXUP(sb);
922 }
923 
924 /*
925  * Create a "control" mbuf containing the specified data
926  * with the specified type for presentation on a socket buffer.
927  */
928 struct mbuf *
929 sbcreatecontrol(caddr_t p, int size, int type, int level)
930 {
931 	struct cmsghdr *cp;
932 	struct mbuf *m;
933 
934 	if (CMSG_SPACE(size) > MCLBYTES) {
935 		printf("sbcreatecontrol: message too large %d\n", size);
936 		return NULL;
937 	}
938 
939 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
940 		return (NULL);
941 	if (CMSG_SPACE(size) > MLEN) {
942 		MCLGET(m, M_DONTWAIT);
943 		if ((m->m_flags & M_EXT) == 0) {
944 			m_free(m);
945 			return NULL;
946 		}
947 	}
948 	cp = mtod(m, struct cmsghdr *);
949 	memset(cp, 0, CMSG_SPACE(size));
950 	memcpy(CMSG_DATA(cp), p, size);
951 	m->m_len = CMSG_SPACE(size);
952 	cp->cmsg_len = CMSG_LEN(size);
953 	cp->cmsg_level = level;
954 	cp->cmsg_type = type;
955 	return (m);
956 }
957