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