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