xref: /freebsd/sys/kern/uipc_sockbuf.c (revision 3e0f6b97b257a96f7275e4442204263e44b16686)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/file.h>
42 #include <sys/buf.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/stat.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 
52 /*
53  * Primitive routines for operating on sockets and socket buffers
54  */
55 
56 u_long	sb_max = SB_MAX;		/* XXX should be static */
57 SYSCTL_INT(_kern, KERN_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, "")
58 
59 static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
60 SYSCTL_INT(_kern, OID_AUTO, sockbuf_waste_factor, CTLFLAG_RW, &sb_efficiency,
61 	   0, "");
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(so)
95 	register struct socket *so;
96 {
97 
98 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
99 	so->so_state |= SS_ISCONNECTING;
100 }
101 
102 void
103 soisconnected(so)
104 	register struct socket *so;
105 {
106 	register struct socket *head = so->so_head;
107 
108 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
109 	so->so_state |= SS_ISCONNECTED;
110 	if (head && (so->so_state & SS_INCOMP)) {
111 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
112 		head->so_incqlen--;
113 		so->so_state &= ~SS_INCOMP;
114 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
115 		so->so_state |= SS_COMP;
116 		sorwakeup(head);
117 		wakeup((caddr_t)&head->so_timeo);
118 	} else {
119 		wakeup((caddr_t)&so->so_timeo);
120 		sorwakeup(so);
121 		sowwakeup(so);
122 	}
123 }
124 
125 void
126 soisdisconnecting(so)
127 	register struct socket *so;
128 {
129 
130 	so->so_state &= ~SS_ISCONNECTING;
131 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
132 	wakeup((caddr_t)&so->so_timeo);
133 	sowwakeup(so);
134 	sorwakeup(so);
135 }
136 
137 void
138 soisdisconnected(so)
139 	register struct socket *so;
140 {
141 
142 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
143 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
144 	wakeup((caddr_t)&so->so_timeo);
145 	sowwakeup(so);
146 	sorwakeup(so);
147 }
148 
149 /*
150  * Return a random connection that hasn't been serviced yet and
151  * is eligible for discard.  There is a one in qlen chance that
152  * we will return a null, saying that there are no dropable
153  * requests.  In this case, the protocol specific code should drop
154  * the new request.  This insures fairness.
155  *
156  * This may be used in conjunction with protocol specific queue
157  * congestion routines.
158  */
159 struct socket *
160 sodropablereq(head)
161 	register struct socket *head;
162 {
163 	register struct socket *so;
164 	unsigned int i, j, qlen;
165 
166 	static int rnd;
167 	static long old_mono_secs;
168 	static unsigned int cur_cnt, old_cnt;
169 
170 	if ((i = (mono_time.tv_sec - old_mono_secs)) != 0) {
171 		old_mono_secs = mono_time.tv_sec;
172 		old_cnt = cur_cnt / i;
173 		cur_cnt = 0;
174 	}
175 
176 	so = TAILQ_FIRST(&head->so_incomp);
177 	if (!so)
178 		return (so);
179 
180 	qlen = head->so_incqlen;
181 	if (++cur_cnt > qlen || old_cnt > qlen) {
182 		rnd = (314159 * rnd + 66329) & 0xffff;
183 		j = ((qlen + 1) * rnd) >> 16;
184 
185 		while (j-- && so)
186 		    so = TAILQ_NEXT(so, so_list);
187 	}
188 
189 	return (so);
190 }
191 
192 /*
193  * When an attempt at a new connection is noted on a socket
194  * which accepts connections, sonewconn is called.  If the
195  * connection is possible (subject to space constraints, etc.)
196  * then we allocate a new structure, propoerly linked into the
197  * data structure of the original socket, and return this.
198  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
199  *
200  * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
201  * to catch calls that are missing the (new) second parameter.
202  */
203 struct socket *
204 sonewconn1(head, connstatus)
205 	register struct socket *head;
206 	int connstatus;
207 {
208 	register struct socket *so;
209 
210 	if (head->so_qlen > 3 * head->so_qlimit / 2)
211 		return ((struct socket *)0);
212 	MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
213 	if (so == NULL)
214 		return ((struct socket *)0);
215 	bzero((caddr_t)so, sizeof(*so));
216 	so->so_head = head;
217 	so->so_type = head->so_type;
218 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
219 	so->so_linger = head->so_linger;
220 	so->so_state = head->so_state | SS_NOFDREF;
221 	so->so_proto = head->so_proto;
222 	so->so_timeo = head->so_timeo;
223 	so->so_pgid = head->so_pgid;
224 	(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
225 	if (connstatus) {
226 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
227 		so->so_state |= SS_COMP;
228 	} else {
229 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
230 		so->so_state |= SS_INCOMP;
231 		head->so_incqlen++;
232 	}
233 	head->so_qlen++;
234 	if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0)) {
235 		if (so->so_state & SS_COMP) {
236 			TAILQ_REMOVE(&head->so_comp, so, so_list);
237 		} else {
238 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
239 			head->so_incqlen--;
240 		}
241 		head->so_qlen--;
242 		(void) free((caddr_t)so, M_SOCKET);
243 		return ((struct socket *)0);
244 	}
245 	if (connstatus) {
246 		sorwakeup(head);
247 		wakeup((caddr_t)&head->so_timeo);
248 		so->so_state |= connstatus;
249 	}
250 	return (so);
251 }
252 
253 /*
254  * Socantsendmore indicates that no more data will be sent on the
255  * socket; it would normally be applied to a socket when the user
256  * informs the system that no more data is to be sent, by the protocol
257  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
258  * will be received, and will normally be applied to the socket by a
259  * protocol when it detects that the peer will send no more data.
260  * Data queued for reading in the socket may yet be read.
261  */
262 
263 void
264 socantsendmore(so)
265 	struct socket *so;
266 {
267 
268 	so->so_state |= SS_CANTSENDMORE;
269 	sowwakeup(so);
270 }
271 
272 void
273 socantrcvmore(so)
274 	struct socket *so;
275 {
276 
277 	so->so_state |= SS_CANTRCVMORE;
278 	sorwakeup(so);
279 }
280 
281 /*
282  * Wait for data to arrive at/drain from a socket buffer.
283  */
284 int
285 sbwait(sb)
286 	struct sockbuf *sb;
287 {
288 
289 	sb->sb_flags |= SB_WAIT;
290 	return (tsleep((caddr_t)&sb->sb_cc,
291 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
292 	    sb->sb_timeo));
293 }
294 
295 /*
296  * Lock a sockbuf already known to be locked;
297  * return any error returned from sleep (EINTR).
298  */
299 int
300 sb_lock(sb)
301 	register struct sockbuf *sb;
302 {
303 	int error;
304 
305 	while (sb->sb_flags & SB_LOCK) {
306 		sb->sb_flags |= SB_WANT;
307 		error = tsleep((caddr_t)&sb->sb_flags,
308 		    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
309 		    "sblock", 0);
310 		if (error)
311 			return (error);
312 	}
313 	sb->sb_flags |= SB_LOCK;
314 	return (0);
315 }
316 
317 /*
318  * Wakeup processes waiting on a socket buffer.
319  * Do asynchronous notification via SIGIO
320  * if the socket has the SS_ASYNC flag set.
321  */
322 void
323 sowakeup(so, sb)
324 	register struct socket *so;
325 	register struct sockbuf *sb;
326 {
327 	struct proc *p;
328 
329 	selwakeup(&sb->sb_sel);
330 	sb->sb_flags &= ~SB_SEL;
331 	if (sb->sb_flags & SB_WAIT) {
332 		sb->sb_flags &= ~SB_WAIT;
333 		wakeup((caddr_t)&sb->sb_cc);
334 	}
335 	if (so->so_state & SS_ASYNC) {
336 		if (so->so_pgid < 0)
337 			gsignal(-so->so_pgid, SIGIO);
338 		else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
339 			psignal(p, SIGIO);
340 	}
341 }
342 
343 /*
344  * Socket buffer (struct sockbuf) utility routines.
345  *
346  * Each socket contains two socket buffers: one for sending data and
347  * one for receiving data.  Each buffer contains a queue of mbufs,
348  * information about the number of mbufs and amount of data in the
349  * queue, and other fields allowing select() statements and notification
350  * on data availability to be implemented.
351  *
352  * Data stored in a socket buffer is maintained as a list of records.
353  * Each record is a list of mbufs chained together with the m_next
354  * field.  Records are chained together with the m_nextpkt field. The upper
355  * level routine soreceive() expects the following conventions to be
356  * observed when placing information in the receive buffer:
357  *
358  * 1. If the protocol requires each message be preceded by the sender's
359  *    name, then a record containing that name must be present before
360  *    any associated data (mbuf's must be of type MT_SONAME).
361  * 2. If the protocol supports the exchange of ``access rights'' (really
362  *    just additional data associated with the message), and there are
363  *    ``rights'' to be received, then a record containing this data
364  *    should be present (mbuf's must be of type MT_RIGHTS).
365  * 3. If a name or rights record exists, then it must be followed by
366  *    a data record, perhaps of zero length.
367  *
368  * Before using a new socket structure it is first necessary to reserve
369  * buffer space to the socket, by calling sbreserve().  This should commit
370  * some of the available buffer space in the system buffer pool for the
371  * socket (currently, it does nothing but enforce limits).  The space
372  * should be released by calling sbrelease() when the socket is destroyed.
373  */
374 
375 int
376 soreserve(so, sndcc, rcvcc)
377 	register struct socket *so;
378 	u_long sndcc, rcvcc;
379 {
380 
381 	if (sbreserve(&so->so_snd, sndcc) == 0)
382 		goto bad;
383 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
384 		goto bad2;
385 	if (so->so_rcv.sb_lowat == 0)
386 		so->so_rcv.sb_lowat = 1;
387 	if (so->so_snd.sb_lowat == 0)
388 		so->so_snd.sb_lowat = MCLBYTES;
389 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
390 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
391 	return (0);
392 bad2:
393 	sbrelease(&so->so_snd);
394 bad:
395 	return (ENOBUFS);
396 }
397 
398 /*
399  * Allot mbufs to a sockbuf.
400  * Attempt to scale mbmax so that mbcnt doesn't become limiting
401  * if buffering efficiency is near the normal case.
402  */
403 int
404 sbreserve(sb, cc)
405 	struct sockbuf *sb;
406 	u_long cc;
407 {
408 	if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
409 		return (0);
410 	sb->sb_hiwat = cc;
411 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
412 	if (sb->sb_lowat > sb->sb_hiwat)
413 		sb->sb_lowat = sb->sb_hiwat;
414 	return (1);
415 }
416 
417 /*
418  * Free mbufs held by a socket, and reserved mbuf space.
419  */
420 void
421 sbrelease(sb)
422 	struct sockbuf *sb;
423 {
424 
425 	sbflush(sb);
426 	sb->sb_hiwat = sb->sb_mbmax = 0;
427 }
428 
429 /*
430  * Routines to add and remove
431  * data from an mbuf queue.
432  *
433  * The routines sbappend() or sbappendrecord() are normally called to
434  * append new mbufs to a socket buffer, after checking that adequate
435  * space is available, comparing the function sbspace() with the amount
436  * of data to be added.  sbappendrecord() differs from sbappend() in
437  * that data supplied is treated as the beginning of a new record.
438  * To place a sender's address, optional access rights, and data in a
439  * socket receive buffer, sbappendaddr() should be used.  To place
440  * access rights and data in a socket receive buffer, sbappendrights()
441  * should be used.  In either case, the new data begins a new record.
442  * Note that unlike sbappend() and sbappendrecord(), these routines check
443  * for the caller that there will be enough space to store the data.
444  * Each fails if there is not enough space, or if it cannot find mbufs
445  * to store additional information in.
446  *
447  * Reliable protocols may use the socket send buffer to hold data
448  * awaiting acknowledgement.  Data is normally copied from a socket
449  * send buffer in a protocol with m_copy for output to a peer,
450  * and then removing the data from the socket buffer with sbdrop()
451  * or sbdroprecord() when the data is acknowledged by the peer.
452  */
453 
454 /*
455  * Append mbuf chain m to the last record in the
456  * socket buffer sb.  The additional space associated
457  * the mbuf chain is recorded in sb.  Empty mbufs are
458  * discarded and mbufs are compacted where possible.
459  */
460 void
461 sbappend(sb, m)
462 	struct sockbuf *sb;
463 	struct mbuf *m;
464 {
465 	register struct mbuf *n;
466 
467 	if (m == 0)
468 		return;
469 	n = sb->sb_mb;
470 	if (n) {
471 		while (n->m_nextpkt)
472 			n = n->m_nextpkt;
473 		do {
474 			if (n->m_flags & M_EOR) {
475 				sbappendrecord(sb, m); /* XXXXXX!!!! */
476 				return;
477 			}
478 		} while (n->m_next && (n = n->m_next));
479 	}
480 	sbcompress(sb, m, n);
481 }
482 
483 #ifdef SOCKBUF_DEBUG
484 void
485 sbcheck(sb)
486 	register struct sockbuf *sb;
487 {
488 	register struct mbuf *m;
489 	register int len = 0, mbcnt = 0;
490 
491 	for (m = sb->sb_mb; m; m = m->m_next) {
492 		len += m->m_len;
493 		mbcnt += MSIZE;
494 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
495 			mbcnt += m->m_ext.ext_size;
496 		if (m->m_nextpkt)
497 			panic("sbcheck nextpkt");
498 	}
499 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
500 		printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
501 		    mbcnt, sb->sb_mbcnt);
502 		panic("sbcheck");
503 	}
504 }
505 #endif
506 
507 /*
508  * As above, except the mbuf chain
509  * begins a new record.
510  */
511 void
512 sbappendrecord(sb, m0)
513 	register struct sockbuf *sb;
514 	register struct mbuf *m0;
515 {
516 	register struct mbuf *m;
517 
518 	if (m0 == 0)
519 		return;
520 	m = sb->sb_mb;
521 	if (m)
522 		while (m->m_nextpkt)
523 			m = m->m_nextpkt;
524 	/*
525 	 * Put the first mbuf on the queue.
526 	 * Note this permits zero length records.
527 	 */
528 	sballoc(sb, m0);
529 	if (m)
530 		m->m_nextpkt = m0;
531 	else
532 		sb->sb_mb = m0;
533 	m = m0->m_next;
534 	m0->m_next = 0;
535 	if (m && (m0->m_flags & M_EOR)) {
536 		m0->m_flags &= ~M_EOR;
537 		m->m_flags |= M_EOR;
538 	}
539 	sbcompress(sb, m, m0);
540 }
541 
542 /*
543  * As above except that OOB data
544  * is inserted at the beginning of the sockbuf,
545  * but after any other OOB data.
546  */
547 void
548 sbinsertoob(sb, m0)
549 	register struct sockbuf *sb;
550 	register struct mbuf *m0;
551 {
552 	register struct mbuf *m;
553 	register struct mbuf **mp;
554 
555 	if (m0 == 0)
556 		return;
557 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
558 	    m = *mp;
559 	    again:
560 		switch (m->m_type) {
561 
562 		case MT_OOBDATA:
563 			continue;		/* WANT next train */
564 
565 		case MT_CONTROL:
566 			m = m->m_next;
567 			if (m)
568 				goto again;	/* inspect THIS train further */
569 		}
570 		break;
571 	}
572 	/*
573 	 * Put the first mbuf on the queue.
574 	 * Note this permits zero length records.
575 	 */
576 	sballoc(sb, m0);
577 	m0->m_nextpkt = *mp;
578 	*mp = m0;
579 	m = m0->m_next;
580 	m0->m_next = 0;
581 	if (m && (m0->m_flags & M_EOR)) {
582 		m0->m_flags &= ~M_EOR;
583 		m->m_flags |= M_EOR;
584 	}
585 	sbcompress(sb, m, m0);
586 }
587 
588 /*
589  * Append address and data, and optionally, control (ancillary) data
590  * to the receive queue of a socket.  If present,
591  * m0 must include a packet header with total length.
592  * Returns 0 if no space in sockbuf or insufficient mbufs.
593  */
594 int
595 sbappendaddr(sb, asa, m0, control)
596 	register struct sockbuf *sb;
597 	struct sockaddr *asa;
598 	struct mbuf *m0, *control;
599 {
600 	register struct mbuf *m, *n;
601 	int space = asa->sa_len;
602 
603 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
604 panic("sbappendaddr");
605 	if (m0)
606 		space += m0->m_pkthdr.len;
607 	for (n = control; n; n = n->m_next) {
608 		space += n->m_len;
609 		if (n->m_next == 0)	/* keep pointer to last control buf */
610 			break;
611 	}
612 	if (space > sbspace(sb))
613 		return (0);
614 	if (asa->sa_len > MLEN)
615 		return (0);
616 	MGET(m, M_DONTWAIT, MT_SONAME);
617 	if (m == 0)
618 		return (0);
619 	m->m_len = asa->sa_len;
620 	bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
621 	if (n)
622 		n->m_next = m0;		/* concatenate data to control */
623 	else
624 		control = m0;
625 	m->m_next = control;
626 	for (n = m; n; n = n->m_next)
627 		sballoc(sb, n);
628 	n = sb->sb_mb;
629 	if (n) {
630 		while (n->m_nextpkt)
631 			n = n->m_nextpkt;
632 		n->m_nextpkt = m;
633 	} else
634 		sb->sb_mb = m;
635 	return (1);
636 }
637 
638 int
639 sbappendcontrol(sb, m0, control)
640 	struct sockbuf *sb;
641 	struct mbuf *control, *m0;
642 {
643 	register struct mbuf *m, *n;
644 	int space = 0;
645 
646 	if (control == 0)
647 		panic("sbappendcontrol");
648 	for (m = control; ; m = m->m_next) {
649 		space += m->m_len;
650 		if (m->m_next == 0)
651 			break;
652 	}
653 	n = m;			/* save pointer to last control buffer */
654 	for (m = m0; m; m = m->m_next)
655 		space += m->m_len;
656 	if (space > sbspace(sb))
657 		return (0);
658 	n->m_next = m0;			/* concatenate data to control */
659 	for (m = control; m; m = m->m_next)
660 		sballoc(sb, m);
661 	n = sb->sb_mb;
662 	if (n) {
663 		while (n->m_nextpkt)
664 			n = n->m_nextpkt;
665 		n->m_nextpkt = control;
666 	} else
667 		sb->sb_mb = control;
668 	return (1);
669 }
670 
671 /*
672  * Compress mbuf chain m into the socket
673  * buffer sb following mbuf n.  If n
674  * is null, the buffer is presumed empty.
675  */
676 void
677 sbcompress(sb, m, n)
678 	register struct sockbuf *sb;
679 	register struct mbuf *m, *n;
680 {
681 	register int eor = 0;
682 	register struct mbuf *o;
683 
684 	while (m) {
685 		eor |= m->m_flags & M_EOR;
686 		if (m->m_len == 0 &&
687 		    (eor == 0 ||
688 		     (((o = m->m_next) || (o = n)) &&
689 		      o->m_type == m->m_type))) {
690 			m = m_free(m);
691 			continue;
692 		}
693 		if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
694 		    (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
695 		    n->m_type == m->m_type) {
696 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
697 			    (unsigned)m->m_len);
698 			n->m_len += m->m_len;
699 			sb->sb_cc += m->m_len;
700 			m = m_free(m);
701 			continue;
702 		}
703 		if (n)
704 			n->m_next = m;
705 		else
706 			sb->sb_mb = m;
707 		sballoc(sb, m);
708 		n = m;
709 		m->m_flags &= ~M_EOR;
710 		m = m->m_next;
711 		n->m_next = 0;
712 	}
713 	if (eor) {
714 		if (n)
715 			n->m_flags |= eor;
716 		else
717 			printf("semi-panic: sbcompress\n");
718 	}
719 }
720 
721 /*
722  * Free all mbufs in a sockbuf.
723  * Check that all resources are reclaimed.
724  */
725 void
726 sbflush(sb)
727 	register struct sockbuf *sb;
728 {
729 
730 	if (sb->sb_flags & SB_LOCK)
731 		panic("sbflush");
732 	while (sb->sb_mbcnt)
733 		sbdrop(sb, (int)sb->sb_cc);
734 	if (sb->sb_cc || sb->sb_mb)
735 		panic("sbflush 2");
736 }
737 
738 /*
739  * Drop data from (the front of) a sockbuf.
740  */
741 void
742 sbdrop(sb, len)
743 	register struct sockbuf *sb;
744 	register int len;
745 {
746 	register struct mbuf *m, *mn;
747 	struct mbuf *next;
748 
749 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
750 	while (len > 0) {
751 		if (m == 0) {
752 			if (next == 0)
753 				panic("sbdrop");
754 			m = next;
755 			next = m->m_nextpkt;
756 			continue;
757 		}
758 		if (m->m_len > len) {
759 			m->m_len -= len;
760 			m->m_data += len;
761 			sb->sb_cc -= len;
762 			break;
763 		}
764 		len -= m->m_len;
765 		sbfree(sb, m);
766 		MFREE(m, mn);
767 		m = mn;
768 	}
769 	while (m && m->m_len == 0) {
770 		sbfree(sb, m);
771 		MFREE(m, mn);
772 		m = mn;
773 	}
774 	if (m) {
775 		sb->sb_mb = m;
776 		m->m_nextpkt = next;
777 	} else
778 		sb->sb_mb = next;
779 }
780 
781 /*
782  * Drop a record off the front of a sockbuf
783  * and move the next record to the front.
784  */
785 void
786 sbdroprecord(sb)
787 	register struct sockbuf *sb;
788 {
789 	register struct mbuf *m, *mn;
790 
791 	m = sb->sb_mb;
792 	if (m) {
793 		sb->sb_mb = m->m_nextpkt;
794 		do {
795 			sbfree(sb, m);
796 			MFREE(m, mn);
797 			m = mn;
798 		} while (m);
799 	}
800 }
801 
802 /*
803  * Create a "control" mbuf containing the specified data
804  * with the specified type for presentation on a socket buffer.
805  */
806 struct mbuf *
807 sbcreatecontrol(p, size, type, level)
808 	caddr_t p;
809 	register int size;
810 	int type, level;
811 {
812 	register struct cmsghdr *cp;
813 	struct mbuf *m;
814 
815 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
816 		return ((struct mbuf *) NULL);
817 	cp = mtod(m, struct cmsghdr *);
818 	/* XXX check size? */
819 	(void)memcpy(CMSG_DATA(cp), p, size);
820 	size += sizeof(*cp);
821 	m->m_len = size;
822 	cp->cmsg_len = size;
823 	cp->cmsg_level = level;
824 	cp->cmsg_type = type;
825 	return (m);
826 }
827 
828 #ifdef PRU_OLDSTYLE
829 /*
830  * The following routines mediate between the old-style `pr_usrreq'
831  * protocol implementations and the new-style `struct pr_usrreqs'
832  * calling convention.
833  */
834 
835 /* syntactic sugar */
836 #define	nomb	(struct mbuf *)0
837 
838 static int
839 old_abort(struct socket *so)
840 {
841 	return so->so_proto->pr_ousrreq(so, PRU_ABORT, nomb, nomb, nomb);
842 }
843 
844 static int
845 old_accept(struct socket *so, struct mbuf *nam)
846 {
847 	return so->so_proto->pr_ousrreq(so, PRU_ACCEPT, nomb,  nam, nomb);
848 }
849 
850 static int
851 old_attach(struct socket *so, int proto)
852 {
853 	return so->so_proto->pr_ousrreq(so, PRU_ATTACH, nomb,
854 				       (struct mbuf *)proto, /* XXX */
855 				       nomb);
856 }
857 
858 static int
859 old_bind(struct socket *so, struct mbuf *nam)
860 {
861 	return so->so_proto->pr_ousrreq(so, PRU_BIND, nomb, nam, nomb);
862 }
863 
864 static int
865 old_connect(struct socket *so, struct mbuf *nam)
866 {
867 	return so->so_proto->pr_ousrreq(so, PRU_CONNECT, nomb, nam, nomb);
868 }
869 
870 static int
871 old_connect2(struct socket *so1, struct socket *so2)
872 {
873 	return so1->so_proto->pr_ousrreq(so1, PRU_CONNECT2, nomb,
874 				       (struct mbuf *)so2, nomb);
875 }
876 
877 static int
878 old_control(struct socket *so, int cmd, caddr_t data, struct ifnet *ifp)
879 {
880 	return so->so_proto->pr_ousrreq(so, PRU_CONTROL, (struct mbuf *)cmd,
881 				       (struct mbuf *)data,
882 				       (struct mbuf *)ifp);
883 }
884 
885 static int
886 old_detach(struct socket *so)
887 {
888 	return so->so_proto->pr_ousrreq(so, PRU_DETACH, nomb, nomb, nomb);
889 }
890 
891 static int
892 old_disconnect(struct socket *so)
893 {
894 	return so->so_proto->pr_ousrreq(so, PRU_DISCONNECT, nomb, nomb, nomb);
895 }
896 
897 static int
898 old_listen(struct socket *so)
899 {
900 	return so->so_proto->pr_ousrreq(so, PRU_LISTEN, nomb, nomb, nomb);
901 }
902 
903 static int
904 old_peeraddr(struct socket *so, struct mbuf *nam)
905 {
906 	return so->so_proto->pr_ousrreq(so, PRU_PEERADDR, nomb, nam, nomb);
907 }
908 
909 static int
910 old_rcvd(struct socket *so, int flags)
911 {
912 	return so->so_proto->pr_ousrreq(so, PRU_RCVD, nomb,
913 				       (struct mbuf *)flags, /* XXX */
914 				       nomb);
915 }
916 
917 static int
918 old_rcvoob(struct socket *so, struct mbuf *m, int flags)
919 {
920 	return so->so_proto->pr_ousrreq(so, PRU_RCVOOB, m,
921 				       (struct mbuf *)flags, /* XXX */
922 				       nomb);
923 }
924 
925 static int
926 old_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *addr,
927 	 struct mbuf *control)
928 {
929 	int req;
930 
931 	if (flags & PRUS_OOB) {
932 		req = PRU_SENDOOB;
933 	} else if(flags & PRUS_EOF) {
934 		req = PRU_SEND_EOF;
935 	} else {
936 		req = PRU_SEND;
937 	}
938 	return so->so_proto->pr_ousrreq(so, req, m, addr, control);
939 }
940 
941 static int
942 old_sense(struct socket *so, struct stat *sb)
943 {
944 	return so->so_proto->pr_ousrreq(so, PRU_SENSE, (struct mbuf *)sb,
945 				       nomb, nomb);
946 }
947 
948 static int
949 old_shutdown(struct socket *so)
950 {
951 	return so->so_proto->pr_ousrreq(so, PRU_SHUTDOWN, nomb, nomb, nomb);
952 }
953 
954 static int
955 old_sockaddr(struct socket *so, struct mbuf *nam)
956 {
957 	return so->so_proto->pr_ousrreq(so, PRU_SOCKADDR, nomb, nam, nomb);
958 }
959 
960 struct pr_usrreqs pru_oldstyle = {
961 	old_abort, old_accept, old_attach, old_bind, old_connect,
962 	old_connect2, old_control, old_detach, old_disconnect,
963 	old_listen, old_peeraddr, old_rcvd, old_rcvoob, old_send,
964 	old_sense, old_shutdown, old_sockaddr
965 };
966 
967 #endif /* PRU_OLDSTYLE */
968 
969 /*
970  * Some routines that return EOPNOTSUPP for entry points that are not
971  * supported by a protocol.  Fill in as needed.
972  */
973 int
974 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
975 {
976 	return EOPNOTSUPP;
977 }
978