xref: /freebsd/sys/kern/uipc_sockbuf.c (revision bcd92649c9952c9c9e8845dbd34276a60dd16664)
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  * $Id: uipc_socket2.c,v 1.16 1996/10/11 19:26:35 pst Exp $
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 
409 	if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
410 		return (0);
411 	sb->sb_hiwat = cc;
412 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
413 	if (sb->sb_lowat > sb->sb_hiwat)
414 		sb->sb_lowat = sb->sb_hiwat;
415 	return (1);
416 }
417 
418 /*
419  * Free mbufs held by a socket, and reserved mbuf space.
420  */
421 void
422 sbrelease(sb)
423 	struct sockbuf *sb;
424 {
425 
426 	sbflush(sb);
427 	sb->sb_hiwat = sb->sb_mbmax = 0;
428 }
429 
430 /*
431  * Routines to add and remove
432  * data from an mbuf queue.
433  *
434  * The routines sbappend() or sbappendrecord() are normally called to
435  * append new mbufs to a socket buffer, after checking that adequate
436  * space is available, comparing the function sbspace() with the amount
437  * of data to be added.  sbappendrecord() differs from sbappend() in
438  * that data supplied is treated as the beginning of a new record.
439  * To place a sender's address, optional access rights, and data in a
440  * socket receive buffer, sbappendaddr() should be used.  To place
441  * access rights and data in a socket receive buffer, sbappendrights()
442  * should be used.  In either case, the new data begins a new record.
443  * Note that unlike sbappend() and sbappendrecord(), these routines check
444  * for the caller that there will be enough space to store the data.
445  * Each fails if there is not enough space, or if it cannot find mbufs
446  * to store additional information in.
447  *
448  * Reliable protocols may use the socket send buffer to hold data
449  * awaiting acknowledgement.  Data is normally copied from a socket
450  * send buffer in a protocol with m_copy for output to a peer,
451  * and then removing the data from the socket buffer with sbdrop()
452  * or sbdroprecord() when the data is acknowledged by the peer.
453  */
454 
455 /*
456  * Append mbuf chain m to the last record in the
457  * socket buffer sb.  The additional space associated
458  * the mbuf chain is recorded in sb.  Empty mbufs are
459  * discarded and mbufs are compacted where possible.
460  */
461 void
462 sbappend(sb, m)
463 	struct sockbuf *sb;
464 	struct mbuf *m;
465 {
466 	register struct mbuf *n;
467 
468 	if (m == 0)
469 		return;
470 	n = sb->sb_mb;
471 	if (n) {
472 		while (n->m_nextpkt)
473 			n = n->m_nextpkt;
474 		do {
475 			if (n->m_flags & M_EOR) {
476 				sbappendrecord(sb, m); /* XXXXXX!!!! */
477 				return;
478 			}
479 		} while (n->m_next && (n = n->m_next));
480 	}
481 	sbcompress(sb, m, n);
482 }
483 
484 #ifdef SOCKBUF_DEBUG
485 void
486 sbcheck(sb)
487 	register struct sockbuf *sb;
488 {
489 	register struct mbuf *m;
490 	register int len = 0, mbcnt = 0;
491 
492 	for (m = sb->sb_mb; m; m = m->m_next) {
493 		len += m->m_len;
494 		mbcnt += MSIZE;
495 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
496 			mbcnt += m->m_ext.ext_size;
497 		if (m->m_nextpkt)
498 			panic("sbcheck nextpkt");
499 	}
500 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
501 		printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
502 		    mbcnt, sb->sb_mbcnt);
503 		panic("sbcheck");
504 	}
505 }
506 #endif
507 
508 /*
509  * As above, except the mbuf chain
510  * begins a new record.
511  */
512 void
513 sbappendrecord(sb, m0)
514 	register struct sockbuf *sb;
515 	register struct mbuf *m0;
516 {
517 	register struct mbuf *m;
518 
519 	if (m0 == 0)
520 		return;
521 	m = sb->sb_mb;
522 	if (m)
523 		while (m->m_nextpkt)
524 			m = m->m_nextpkt;
525 	/*
526 	 * Put the first mbuf on the queue.
527 	 * Note this permits zero length records.
528 	 */
529 	sballoc(sb, m0);
530 	if (m)
531 		m->m_nextpkt = m0;
532 	else
533 		sb->sb_mb = m0;
534 	m = m0->m_next;
535 	m0->m_next = 0;
536 	if (m && (m0->m_flags & M_EOR)) {
537 		m0->m_flags &= ~M_EOR;
538 		m->m_flags |= M_EOR;
539 	}
540 	sbcompress(sb, m, m0);
541 }
542 
543 /*
544  * As above except that OOB data
545  * is inserted at the beginning of the sockbuf,
546  * but after any other OOB data.
547  */
548 void
549 sbinsertoob(sb, m0)
550 	register struct sockbuf *sb;
551 	register struct mbuf *m0;
552 {
553 	register struct mbuf *m;
554 	register struct mbuf **mp;
555 
556 	if (m0 == 0)
557 		return;
558 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
559 	    m = *mp;
560 	    again:
561 		switch (m->m_type) {
562 
563 		case MT_OOBDATA:
564 			continue;		/* WANT next train */
565 
566 		case MT_CONTROL:
567 			m = m->m_next;
568 			if (m)
569 				goto again;	/* inspect THIS train further */
570 		}
571 		break;
572 	}
573 	/*
574 	 * Put the first mbuf on the queue.
575 	 * Note this permits zero length records.
576 	 */
577 	sballoc(sb, m0);
578 	m0->m_nextpkt = *mp;
579 	*mp = m0;
580 	m = m0->m_next;
581 	m0->m_next = 0;
582 	if (m && (m0->m_flags & M_EOR)) {
583 		m0->m_flags &= ~M_EOR;
584 		m->m_flags |= M_EOR;
585 	}
586 	sbcompress(sb, m, m0);
587 }
588 
589 /*
590  * Append address and data, and optionally, control (ancillary) data
591  * to the receive queue of a socket.  If present,
592  * m0 must include a packet header with total length.
593  * Returns 0 if no space in sockbuf or insufficient mbufs.
594  */
595 int
596 sbappendaddr(sb, asa, m0, control)
597 	register struct sockbuf *sb;
598 	struct sockaddr *asa;
599 	struct mbuf *m0, *control;
600 {
601 	register struct mbuf *m, *n;
602 	int space = asa->sa_len;
603 
604 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
605 panic("sbappendaddr");
606 	if (m0)
607 		space += m0->m_pkthdr.len;
608 	for (n = control; n; n = n->m_next) {
609 		space += n->m_len;
610 		if (n->m_next == 0)	/* keep pointer to last control buf */
611 			break;
612 	}
613 	if (space > sbspace(sb))
614 		return (0);
615 	if (asa->sa_len > MLEN)
616 		return (0);
617 	MGET(m, M_DONTWAIT, MT_SONAME);
618 	if (m == 0)
619 		return (0);
620 	m->m_len = asa->sa_len;
621 	bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
622 	if (n)
623 		n->m_next = m0;		/* concatenate data to control */
624 	else
625 		control = m0;
626 	m->m_next = control;
627 	for (n = m; n; n = n->m_next)
628 		sballoc(sb, n);
629 	n = sb->sb_mb;
630 	if (n) {
631 		while (n->m_nextpkt)
632 			n = n->m_nextpkt;
633 		n->m_nextpkt = m;
634 	} else
635 		sb->sb_mb = m;
636 	return (1);
637 }
638 
639 int
640 sbappendcontrol(sb, m0, control)
641 	struct sockbuf *sb;
642 	struct mbuf *control, *m0;
643 {
644 	register struct mbuf *m, *n;
645 	int space = 0;
646 
647 	if (control == 0)
648 		panic("sbappendcontrol");
649 	for (m = control; ; m = m->m_next) {
650 		space += m->m_len;
651 		if (m->m_next == 0)
652 			break;
653 	}
654 	n = m;			/* save pointer to last control buffer */
655 	for (m = m0; m; m = m->m_next)
656 		space += m->m_len;
657 	if (space > sbspace(sb))
658 		return (0);
659 	n->m_next = m0;			/* concatenate data to control */
660 	for (m = control; m; m = m->m_next)
661 		sballoc(sb, m);
662 	n = sb->sb_mb;
663 	if (n) {
664 		while (n->m_nextpkt)
665 			n = n->m_nextpkt;
666 		n->m_nextpkt = control;
667 	} else
668 		sb->sb_mb = control;
669 	return (1);
670 }
671 
672 /*
673  * Compress mbuf chain m into the socket
674  * buffer sb following mbuf n.  If n
675  * is null, the buffer is presumed empty.
676  */
677 void
678 sbcompress(sb, m, n)
679 	register struct sockbuf *sb;
680 	register struct mbuf *m, *n;
681 {
682 	register int eor = 0;
683 	register struct mbuf *o;
684 
685 	while (m) {
686 		eor |= m->m_flags & M_EOR;
687 		if (m->m_len == 0 &&
688 		    (eor == 0 ||
689 		     (((o = m->m_next) || (o = n)) &&
690 		      o->m_type == m->m_type))) {
691 			m = m_free(m);
692 			continue;
693 		}
694 		if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
695 		    (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
696 		    n->m_type == m->m_type) {
697 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
698 			    (unsigned)m->m_len);
699 			n->m_len += m->m_len;
700 			sb->sb_cc += m->m_len;
701 			m = m_free(m);
702 			continue;
703 		}
704 		if (n)
705 			n->m_next = m;
706 		else
707 			sb->sb_mb = m;
708 		sballoc(sb, m);
709 		n = m;
710 		m->m_flags &= ~M_EOR;
711 		m = m->m_next;
712 		n->m_next = 0;
713 	}
714 	if (eor) {
715 		if (n)
716 			n->m_flags |= eor;
717 		else
718 			printf("semi-panic: sbcompress\n");
719 	}
720 }
721 
722 /*
723  * Free all mbufs in a sockbuf.
724  * Check that all resources are reclaimed.
725  */
726 void
727 sbflush(sb)
728 	register struct sockbuf *sb;
729 {
730 
731 	if (sb->sb_flags & SB_LOCK)
732 		panic("sbflush");
733 	while (sb->sb_mbcnt)
734 		sbdrop(sb, (int)sb->sb_cc);
735 	if (sb->sb_cc || sb->sb_mb)
736 		panic("sbflush 2");
737 }
738 
739 /*
740  * Drop data from (the front of) a sockbuf.
741  */
742 void
743 sbdrop(sb, len)
744 	register struct sockbuf *sb;
745 	register int len;
746 {
747 	register struct mbuf *m, *mn;
748 	struct mbuf *next;
749 
750 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
751 	while (len > 0) {
752 		if (m == 0) {
753 			if (next == 0)
754 				panic("sbdrop");
755 			m = next;
756 			next = m->m_nextpkt;
757 			continue;
758 		}
759 		if (m->m_len > len) {
760 			m->m_len -= len;
761 			m->m_data += len;
762 			sb->sb_cc -= len;
763 			break;
764 		}
765 		len -= m->m_len;
766 		sbfree(sb, m);
767 		MFREE(m, mn);
768 		m = mn;
769 	}
770 	while (m && m->m_len == 0) {
771 		sbfree(sb, m);
772 		MFREE(m, mn);
773 		m = mn;
774 	}
775 	if (m) {
776 		sb->sb_mb = m;
777 		m->m_nextpkt = next;
778 	} else
779 		sb->sb_mb = next;
780 }
781 
782 /*
783  * Drop a record off the front of a sockbuf
784  * and move the next record to the front.
785  */
786 void
787 sbdroprecord(sb)
788 	register struct sockbuf *sb;
789 {
790 	register struct mbuf *m, *mn;
791 
792 	m = sb->sb_mb;
793 	if (m) {
794 		sb->sb_mb = m->m_nextpkt;
795 		do {
796 			sbfree(sb, m);
797 			MFREE(m, mn);
798 			m = mn;
799 		} while (m);
800 	}
801 }
802 
803 /*
804  * Create a "control" mbuf containing the specified data
805  * with the specified type for presentation on a socket buffer.
806  */
807 struct mbuf *
808 sbcreatecontrol(p, size, type, level)
809 	caddr_t p;
810 	register int size;
811 	int type, level;
812 {
813 	register struct cmsghdr *cp;
814 	struct mbuf *m;
815 
816 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
817 		return ((struct mbuf *) NULL);
818 	cp = mtod(m, struct cmsghdr *);
819 	/* XXX check size? */
820 	(void)memcpy(CMSG_DATA(cp), p, size);
821 	size += sizeof(*cp);
822 	m->m_len = size;
823 	cp->cmsg_len = size;
824 	cp->cmsg_level = level;
825 	cp->cmsg_type = type;
826 	return (m);
827 }
828 
829 #ifdef PRU_OLDSTYLE
830 /*
831  * The following routines mediate between the old-style `pr_usrreq'
832  * protocol implementations and the new-style `struct pr_usrreqs'
833  * calling convention.
834  */
835 
836 /* syntactic sugar */
837 #define	nomb	(struct mbuf *)0
838 
839 static int
840 old_abort(struct socket *so)
841 {
842 	return so->so_proto->pr_ousrreq(so, PRU_ABORT, nomb, nomb, nomb);
843 }
844 
845 static int
846 old_accept(struct socket *so, struct mbuf *nam)
847 {
848 	return so->so_proto->pr_ousrreq(so, PRU_ACCEPT, nomb,  nam, nomb);
849 }
850 
851 static int
852 old_attach(struct socket *so, int proto)
853 {
854 	return so->so_proto->pr_ousrreq(so, PRU_ATTACH, nomb,
855 				       (struct mbuf *)proto, /* XXX */
856 				       nomb);
857 }
858 
859 static int
860 old_bind(struct socket *so, struct mbuf *nam)
861 {
862 	return so->so_proto->pr_ousrreq(so, PRU_BIND, nomb, nam, nomb);
863 }
864 
865 static int
866 old_connect(struct socket *so, struct mbuf *nam)
867 {
868 	return so->so_proto->pr_ousrreq(so, PRU_CONNECT, nomb, nam, nomb);
869 }
870 
871 static int
872 old_connect2(struct socket *so1, struct socket *so2)
873 {
874 	return so1->so_proto->pr_ousrreq(so1, PRU_CONNECT2, nomb,
875 				       (struct mbuf *)so2, nomb);
876 }
877 
878 static int
879 old_control(struct socket *so, int cmd, caddr_t data, struct ifnet *ifp)
880 {
881 	return so->so_proto->pr_ousrreq(so, PRU_CONTROL, (struct mbuf *)cmd,
882 				       (struct mbuf *)data,
883 				       (struct mbuf *)ifp);
884 }
885 
886 static int
887 old_detach(struct socket *so)
888 {
889 	return so->so_proto->pr_ousrreq(so, PRU_DETACH, nomb, nomb, nomb);
890 }
891 
892 static int
893 old_disconnect(struct socket *so)
894 {
895 	return so->so_proto->pr_ousrreq(so, PRU_DISCONNECT, nomb, nomb, nomb);
896 }
897 
898 static int
899 old_listen(struct socket *so)
900 {
901 	return so->so_proto->pr_ousrreq(so, PRU_LISTEN, nomb, nomb, nomb);
902 }
903 
904 static int
905 old_peeraddr(struct socket *so, struct mbuf *nam)
906 {
907 	return so->so_proto->pr_ousrreq(so, PRU_PEERADDR, nomb, nam, nomb);
908 }
909 
910 static int
911 old_rcvd(struct socket *so, int flags)
912 {
913 	return so->so_proto->pr_ousrreq(so, PRU_RCVD, nomb,
914 				       (struct mbuf *)flags, /* XXX */
915 				       nomb);
916 }
917 
918 static int
919 old_rcvoob(struct socket *so, struct mbuf *m, int flags)
920 {
921 	return so->so_proto->pr_ousrreq(so, PRU_RCVOOB, m,
922 				       (struct mbuf *)flags, /* XXX */
923 				       nomb);
924 }
925 
926 static int
927 old_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *addr,
928 	 struct mbuf *control)
929 {
930 	int req;
931 
932 	if (flags & PRUS_OOB) {
933 		req = PRU_SENDOOB;
934 	} else if(flags & PRUS_EOF) {
935 		req = PRU_SEND_EOF;
936 	} else {
937 		req = PRU_SEND;
938 	}
939 	return so->so_proto->pr_ousrreq(so, req, m, addr, control);
940 }
941 
942 static int
943 old_sense(struct socket *so, struct stat *sb)
944 {
945 	return so->so_proto->pr_ousrreq(so, PRU_SENSE, (struct mbuf *)sb,
946 				       nomb, nomb);
947 }
948 
949 static int
950 old_shutdown(struct socket *so)
951 {
952 	return so->so_proto->pr_ousrreq(so, PRU_SHUTDOWN, nomb, nomb, nomb);
953 }
954 
955 static int
956 old_sockaddr(struct socket *so, struct mbuf *nam)
957 {
958 	return so->so_proto->pr_ousrreq(so, PRU_SOCKADDR, nomb, nam, nomb);
959 }
960 
961 struct pr_usrreqs pru_oldstyle = {
962 	old_abort, old_accept, old_attach, old_bind, old_connect,
963 	old_connect2, old_control, old_detach, old_disconnect,
964 	old_listen, old_peeraddr, old_rcvd, old_rcvoob, old_send,
965 	old_sense, old_shutdown, old_sockaddr
966 };
967 
968 #endif /* PRU_OLDSTYLE */
969 
970 /*
971  * Some routines that return EOPNOTSUPP for entry points that are not
972  * supported by a protocol.  Fill in as needed.
973  */
974 int
975 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
976 {
977 	return EOPNOTSUPP;
978 }
979