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