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