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