xref: /freebsd/sys/kern/uipc_sockbuf.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 #include "opt_param.h"
37 
38 #include <sys/param.h>
39 #include <sys/aio.h> /* for aio_swake proto */
40 #include <sys/domain.h>
41 #include <sys/event.h>
42 #include <sys/file.h>	/* for maxfiles */
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/resourcevar.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/stat.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
58 
59 int	maxsockets;
60 
61 void (*aio_swake)(struct socket *, struct sockbuf *);
62 
63 /*
64  * Primitive routines for operating on sockets and socket buffers
65  */
66 
67 u_long	sb_max = SB_MAX;
68 static	u_long sb_max_adj =
69     SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
70 
71 static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
72 
73 /*
74  * Procedures to manipulate state flags of socket
75  * and do appropriate wakeups.  Normal sequence from the
76  * active (originating) side is that soisconnecting() is
77  * called during processing of connect() call,
78  * resulting in an eventual call to soisconnected() if/when the
79  * connection is established.  When the connection is torn down
80  * soisdisconnecting() is called during processing of disconnect() call,
81  * and soisdisconnected() is called when the connection to the peer
82  * is totally severed.  The semantics of these routines are such that
83  * connectionless protocols can call soisconnected() and soisdisconnected()
84  * only, bypassing the in-progress calls when setting up a ``connection''
85  * takes no time.
86  *
87  * From the passive side, a socket is created with
88  * two queues of sockets: so_incomp for connections in progress
89  * and so_comp for connections already made and awaiting user acceptance.
90  * As a protocol is preparing incoming connections, it creates a socket
91  * structure queued on so_incomp by calling sonewconn().  When the connection
92  * is established, soisconnected() is called, and transfers the
93  * socket structure to so_comp, making it available to accept().
94  *
95  * If a socket is closed with sockets on either
96  * so_incomp or so_comp, these sockets are dropped.
97  *
98  * If higher level protocols are implemented in
99  * the kernel, the wakeups done here will sometimes
100  * cause software-interrupt process scheduling.
101  */
102 
103 void
104 soisconnecting(so)
105 	register struct socket *so;
106 {
107 
108 	SOCK_LOCK(so);
109 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
110 	so->so_state |= SS_ISCONNECTING;
111 	SOCK_UNLOCK(so);
112 }
113 
114 void
115 soisconnected(so)
116 	struct socket *so;
117 {
118 	struct socket *head;
119 
120 	SOCK_LOCK(so);
121 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
122 	so->so_state |= SS_ISCONNECTED;
123 	SOCK_UNLOCK(so);
124 	ACCEPT_LOCK();
125 	head = so->so_head;
126 	if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
127 		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
128 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
129 			head->so_incqlen--;
130 			so->so_qstate &= ~SQ_INCOMP;
131 			TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
132 			head->so_qlen++;
133 			so->so_qstate |= SQ_COMP;
134 			ACCEPT_UNLOCK();
135 			sorwakeup(head);
136 			wakeup_one(&head->so_timeo);
137 		} else {
138 			ACCEPT_UNLOCK();
139 			SOCK_LOCK(so);
140 			so->so_upcall =
141 			    head->so_accf->so_accept_filter->accf_callback;
142 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
143 			so->so_rcv.sb_flags |= SB_UPCALL;
144 			so->so_options &= ~SO_ACCEPTFILTER;
145 			SOCK_UNLOCK(so);
146 			so->so_upcall(so, so->so_upcallarg, M_TRYWAIT);
147 		}
148 		return;
149 	}
150 	ACCEPT_UNLOCK();
151 	wakeup(&so->so_timeo);
152 	sorwakeup(so);
153 	sowwakeup(so);
154 }
155 
156 void
157 soisdisconnecting(so)
158 	register struct socket *so;
159 {
160 
161 	/*
162 	 * XXXRW: This code separately acquires SOCK_LOCK(so) and
163 	 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
164 	 * avoid introducing the assumption  that they are the same.
165 	 */
166 	SOCK_LOCK(so);
167 	so->so_state &= ~SS_ISCONNECTING;
168 	so->so_state |= SS_ISDISCONNECTING;
169 	SOCK_UNLOCK(so);
170 	SOCKBUF_LOCK(&so->so_rcv);
171 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
172 	sorwakeup_locked(so);
173 	SOCKBUF_LOCK(&so->so_snd);
174 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
175 	sowwakeup_locked(so);
176 	wakeup(&so->so_timeo);
177 }
178 
179 void
180 soisdisconnected(so)
181 	register struct socket *so;
182 {
183 
184 	/*
185 	 * XXXRW: This code separately acquires SOCK_LOCK(so) and
186 	 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same mutex to
187 	 * avoid introducing the assumption  that they are the same.
188 	 */
189 	/* XXXRW: so_state locking? */
190 	SOCK_LOCK(so);
191 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
192 	so->so_state |= SS_ISDISCONNECTED;
193 	SOCK_UNLOCK(so);
194 	SOCKBUF_LOCK(&so->so_rcv);
195 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
196 	sorwakeup_locked(so);
197 	SOCKBUF_LOCK(&so->so_snd);
198 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
199 	sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
200 	sowwakeup_locked(so);
201 	wakeup(&so->so_timeo);
202 }
203 
204 /*
205  * When an attempt at a new connection is noted on a socket
206  * which accepts connections, sonewconn is called.  If the
207  * connection is possible (subject to space constraints, etc.)
208  * then we allocate a new structure, propoerly linked into the
209  * data structure of the original socket, and return this.
210  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
211  *
212  * note: the ref count on the socket is 0 on return
213  */
214 struct socket *
215 sonewconn(head, connstatus)
216 	register struct socket *head;
217 	int connstatus;
218 {
219 	register struct socket *so;
220 	int over;
221 
222 	ACCEPT_LOCK();
223 	over = (head->so_qlen > 3 * head->so_qlimit / 2);
224 	ACCEPT_UNLOCK();
225 	if (over)
226 		return ((struct socket *)0);
227 	so = soalloc(M_NOWAIT);
228 	if (so == NULL)
229 		return ((struct socket *)0);
230 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
231 		connstatus = 0;
232 	so->so_head = head;
233 	so->so_type = head->so_type;
234 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
235 	so->so_linger = head->so_linger;
236 	so->so_state = head->so_state | SS_NOFDREF;
237 	so->so_proto = head->so_proto;
238 	so->so_timeo = head->so_timeo;
239 	so->so_cred = crhold(head->so_cred);
240 #ifdef MAC
241 	SOCK_LOCK(head);
242 	mac_create_socket_from_socket(head, so);
243 	SOCK_UNLOCK(head);
244 #endif
245 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
246 	    (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
247 		sodealloc(so);
248 		return ((struct socket *)0);
249 	}
250 	ACCEPT_LOCK();
251 	if (connstatus) {
252 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
253 		so->so_qstate |= SQ_COMP;
254 		head->so_qlen++;
255 	} else {
256 		/*
257 		 * XXXRW: Keep removing sockets from the head until there's
258 		 * room for us to insert on the tail.  In pre-locking
259 		 * revisions, this was a simple if(), but as we could be
260 		 * racing with other threads and soabort() requires dropping
261 		 * locks, we must loop waiting for the condition to be true.
262 		 */
263 		while (head->so_incqlen > head->so_qlimit) {
264 			struct socket *sp;
265 			sp = TAILQ_FIRST(&head->so_incomp);
266 			TAILQ_REMOVE(&so->so_incomp, sp, so_list);
267 			head->so_incqlen--;
268 			sp->so_qstate &= ~SQ_INCOMP;
269 			sp->so_head = NULL;
270 			ACCEPT_UNLOCK();
271 			(void) soabort(sp);
272 			ACCEPT_LOCK();
273 		}
274 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
275 		so->so_qstate |= SQ_INCOMP;
276 		head->so_incqlen++;
277 	}
278 	ACCEPT_UNLOCK();
279 	if (connstatus) {
280 		so->so_state |= connstatus;
281 		sorwakeup(head);
282 		wakeup_one(&head->so_timeo);
283 	}
284 	return (so);
285 }
286 
287 /*
288  * Socantsendmore indicates that no more data will be sent on the
289  * socket; it would normally be applied to a socket when the user
290  * informs the system that no more data is to be sent, by the protocol
291  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
292  * will be received, and will normally be applied to the socket by a
293  * protocol when it detects that the peer will send no more data.
294  * Data queued for reading in the socket may yet be read.
295  */
296 void
297 socantsendmore_locked(so)
298 	struct socket *so;
299 {
300 
301 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
302 
303 	so->so_snd.sb_state |= SBS_CANTSENDMORE;
304 	sowwakeup_locked(so);
305 	mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
306 }
307 
308 void
309 socantsendmore(so)
310 	struct socket *so;
311 {
312 
313 	SOCKBUF_LOCK(&so->so_snd);
314 	socantsendmore_locked(so);
315 	mtx_assert(SOCKBUF_MTX(&so->so_snd), MA_NOTOWNED);
316 }
317 
318 void
319 socantrcvmore_locked(so)
320 	struct socket *so;
321 {
322 
323 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
324 
325 	so->so_rcv.sb_state |= SBS_CANTRCVMORE;
326 	sorwakeup_locked(so);
327 	mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
328 }
329 
330 void
331 socantrcvmore(so)
332 	struct socket *so;
333 {
334 
335 	SOCKBUF_LOCK(&so->so_rcv);
336 	socantrcvmore_locked(so);
337 	mtx_assert(SOCKBUF_MTX(&so->so_rcv), MA_NOTOWNED);
338 }
339 
340 /*
341  * Wait for data to arrive at/drain from a socket buffer.
342  */
343 int
344 sbwait(sb)
345 	struct sockbuf *sb;
346 {
347 
348 	SOCKBUF_LOCK_ASSERT(sb);
349 
350 	sb->sb_flags |= SB_WAIT;
351 	return (msleep(&sb->sb_cc, &sb->sb_mtx,
352 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
353 	    sb->sb_timeo));
354 }
355 
356 /*
357  * Lock a sockbuf already known to be locked;
358  * return any error returned from sleep (EINTR).
359  */
360 int
361 sb_lock(sb)
362 	register struct sockbuf *sb;
363 {
364 	int error;
365 
366 	SOCKBUF_LOCK_ASSERT(sb);
367 
368 	while (sb->sb_flags & SB_LOCK) {
369 		sb->sb_flags |= SB_WANT;
370 		error = msleep(&sb->sb_flags, &sb->sb_mtx,
371 		    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
372 		    "sblock", 0);
373 		if (error)
374 			return (error);
375 	}
376 	sb->sb_flags |= SB_LOCK;
377 	return (0);
378 }
379 
380 /*
381  * Wakeup processes waiting on a socket buffer.  Do asynchronous
382  * notification via SIGIO if the socket has the SS_ASYNC flag set.
383  *
384  * Called with the socket buffer lock held; will release the lock by the end
385  * of the function.  This allows the caller to acquire the socket buffer lock
386  * while testing for the need for various sorts of wakeup and hold it through
387  * to the point where it's no longer required.  We currently hold the lock
388  * through calls out to other subsystems (with the exception of kqueue), and
389  * then release it to avoid lock order issues.  It's not clear that's
390  * correct.
391  */
392 void
393 sowakeup(so, sb)
394 	register struct socket *so;
395 	register struct sockbuf *sb;
396 {
397 
398 	SOCKBUF_LOCK_ASSERT(sb);
399 
400 	selwakeuppri(&sb->sb_sel, PSOCK);
401 	sb->sb_flags &= ~SB_SEL;
402 	if (sb->sb_flags & SB_WAIT) {
403 		sb->sb_flags &= ~SB_WAIT;
404 		wakeup(&sb->sb_cc);
405 	}
406 	KNOTE(&sb->sb_sel.si_note, 0);
407 	SOCKBUF_UNLOCK(sb);
408 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
409 		pgsigio(&so->so_sigio, SIGIO, 0);
410 	if (sb->sb_flags & SB_UPCALL)
411 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
412 	if (sb->sb_flags & SB_AIO)
413 		aio_swake(so, sb);
414 	mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
415 }
416 
417 /*
418  * Socket buffer (struct sockbuf) utility routines.
419  *
420  * Each socket contains two socket buffers: one for sending data and
421  * one for receiving data.  Each buffer contains a queue of mbufs,
422  * information about the number of mbufs and amount of data in the
423  * queue, and other fields allowing select() statements and notification
424  * on data availability to be implemented.
425  *
426  * Data stored in a socket buffer is maintained as a list of records.
427  * Each record is a list of mbufs chained together with the m_next
428  * field.  Records are chained together with the m_nextpkt field. The upper
429  * level routine soreceive() expects the following conventions to be
430  * observed when placing information in the receive buffer:
431  *
432  * 1. If the protocol requires each message be preceded by the sender's
433  *    name, then a record containing that name must be present before
434  *    any associated data (mbuf's must be of type MT_SONAME).
435  * 2. If the protocol supports the exchange of ``access rights'' (really
436  *    just additional data associated with the message), and there are
437  *    ``rights'' to be received, then a record containing this data
438  *    should be present (mbuf's must be of type MT_RIGHTS).
439  * 3. If a name or rights record exists, then it must be followed by
440  *    a data record, perhaps of zero length.
441  *
442  * Before using a new socket structure it is first necessary to reserve
443  * buffer space to the socket, by calling sbreserve().  This should commit
444  * some of the available buffer space in the system buffer pool for the
445  * socket (currently, it does nothing but enforce limits).  The space
446  * should be released by calling sbrelease() when the socket is destroyed.
447  */
448 
449 int
450 soreserve(so, sndcc, rcvcc)
451 	register struct socket *so;
452 	u_long sndcc, rcvcc;
453 {
454 	struct thread *td = curthread;
455 
456 	SOCKBUF_LOCK(&so->so_snd);
457 	SOCKBUF_LOCK(&so->so_rcv);
458 	if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
459 		goto bad;
460 	if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
461 		goto bad2;
462 	if (so->so_rcv.sb_lowat == 0)
463 		so->so_rcv.sb_lowat = 1;
464 	if (so->so_snd.sb_lowat == 0)
465 		so->so_snd.sb_lowat = MCLBYTES;
466 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
467 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
468 	SOCKBUF_UNLOCK(&so->so_rcv);
469 	SOCKBUF_UNLOCK(&so->so_snd);
470 	return (0);
471 bad2:
472 	sbrelease_locked(&so->so_snd, so);
473 bad:
474 	SOCKBUF_UNLOCK(&so->so_rcv);
475 	SOCKBUF_UNLOCK(&so->so_snd);
476 	return (ENOBUFS);
477 }
478 
479 static int
480 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
481 {
482 	int error = 0;
483 	u_long old_sb_max = sb_max;
484 
485 	error = SYSCTL_OUT(req, arg1, sizeof(u_long));
486 	if (error || !req->newptr)
487 		return (error);
488 	error = SYSCTL_IN(req, arg1, sizeof(u_long));
489 	if (error)
490 		return (error);
491 	if (sb_max < MSIZE + MCLBYTES) {
492 		sb_max = old_sb_max;
493 		return (EINVAL);
494 	}
495 	sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
496 	return (0);
497 }
498 
499 /*
500  * Allot mbufs to a sockbuf.
501  * Attempt to scale mbmax so that mbcnt doesn't become limiting
502  * if buffering efficiency is near the normal case.
503  */
504 int
505 sbreserve_locked(sb, cc, so, td)
506 	struct sockbuf *sb;
507 	u_long cc;
508 	struct socket *so;
509 	struct thread *td;
510 {
511 	rlim_t sbsize_limit;
512 
513 	SOCKBUF_LOCK_ASSERT(sb);
514 
515 	/*
516 	 * td will only be NULL when we're in an interrupt
517 	 * (e.g. in tcp_input())
518 	 */
519 	if (cc > sb_max_adj)
520 		return (0);
521 	if (td != NULL) {
522 		PROC_LOCK(td->td_proc);
523 		sbsize_limit = lim_cur(td->td_proc, RLIMIT_SBSIZE);
524 		PROC_UNLOCK(td->td_proc);
525 	} else
526 		sbsize_limit = RLIM_INFINITY;
527 	if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
528 	    sbsize_limit))
529 		return (0);
530 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
531 	if (sb->sb_lowat > sb->sb_hiwat)
532 		sb->sb_lowat = sb->sb_hiwat;
533 	return (1);
534 }
535 
536 int
537 sbreserve(sb, cc, so, td)
538 	struct sockbuf *sb;
539 	u_long cc;
540 	struct socket *so;
541 	struct thread *td;
542 {
543 	int error;
544 
545 	SOCKBUF_LOCK(sb);
546 	error = sbreserve_locked(sb, cc, so, td);
547 	SOCKBUF_UNLOCK(sb);
548 	return (error);
549 }
550 
551 /*
552  * Free mbufs held by a socket, and reserved mbuf space.
553  */
554 void
555 sbrelease_locked(sb, so)
556 	struct sockbuf *sb;
557 	struct socket *so;
558 {
559 
560 	SOCKBUF_LOCK_ASSERT(sb);
561 
562 	sbflush_locked(sb);
563 	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
564 	    RLIM_INFINITY);
565 	sb->sb_mbmax = 0;
566 }
567 
568 void
569 sbrelease(sb, so)
570 	struct sockbuf *sb;
571 	struct socket *so;
572 {
573 
574 	SOCKBUF_LOCK(sb);
575 	sbrelease_locked(sb, so);
576 	SOCKBUF_UNLOCK(sb);
577 }
578 /*
579  * Routines to add and remove
580  * data from an mbuf queue.
581  *
582  * The routines sbappend() or sbappendrecord() are normally called to
583  * append new mbufs to a socket buffer, after checking that adequate
584  * space is available, comparing the function sbspace() with the amount
585  * of data to be added.  sbappendrecord() differs from sbappend() in
586  * that data supplied is treated as the beginning of a new record.
587  * To place a sender's address, optional access rights, and data in a
588  * socket receive buffer, sbappendaddr() should be used.  To place
589  * access rights and data in a socket receive buffer, sbappendrights()
590  * should be used.  In either case, the new data begins a new record.
591  * Note that unlike sbappend() and sbappendrecord(), these routines check
592  * for the caller that there will be enough space to store the data.
593  * Each fails if there is not enough space, or if it cannot find mbufs
594  * to store additional information in.
595  *
596  * Reliable protocols may use the socket send buffer to hold data
597  * awaiting acknowledgement.  Data is normally copied from a socket
598  * send buffer in a protocol with m_copy for output to a peer,
599  * and then removing the data from the socket buffer with sbdrop()
600  * or sbdroprecord() when the data is acknowledged by the peer.
601  */
602 
603 #ifdef SOCKBUF_DEBUG
604 void
605 sblastrecordchk(struct sockbuf *sb, const char *file, int line)
606 {
607 	struct mbuf *m = sb->sb_mb;
608 
609 	SOCKBUF_LOCK_ASSERT(sb);
610 
611 	while (m && m->m_nextpkt)
612 		m = m->m_nextpkt;
613 
614 	if (m != sb->sb_lastrecord) {
615 		printf("%s: sb_mb %p sb_lastrecord %p last %p\n",
616 			__func__, sb->sb_mb, sb->sb_lastrecord, m);
617 		printf("packet chain:\n");
618 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
619 			printf("\t%p\n", m);
620 		panic("%s from %s:%u", __func__, file, line);
621 	}
622 }
623 
624 void
625 sblastmbufchk(struct sockbuf *sb, const char *file, int line)
626 {
627 	struct mbuf *m = sb->sb_mb;
628 	struct mbuf *n;
629 
630 	SOCKBUF_LOCK_ASSERT(sb);
631 
632 	while (m && m->m_nextpkt)
633 		m = m->m_nextpkt;
634 
635 	while (m && m->m_next)
636 		m = m->m_next;
637 
638 	if (m != sb->sb_mbtail) {
639 		printf("%s: sb_mb %p sb_mbtail %p last %p\n",
640 			__func__, sb->sb_mb, sb->sb_mbtail, m);
641 		printf("packet tree:\n");
642 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
643 			printf("\t");
644 			for (n = m; n != NULL; n = n->m_next)
645 				printf("%p ", n);
646 			printf("\n");
647 		}
648 		panic("%s from %s:%u", __func__, file, line);
649 	}
650 }
651 #endif /* SOCKBUF_DEBUG */
652 
653 #define SBLINKRECORD(sb, m0) do {					\
654 	SOCKBUF_LOCK_ASSERT(sb);					\
655 	if ((sb)->sb_lastrecord != NULL)				\
656 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
657 	else								\
658 		(sb)->sb_mb = (m0);					\
659 	(sb)->sb_lastrecord = (m0);					\
660 } while (/*CONSTCOND*/0)
661 
662 /*
663  * Append mbuf chain m to the last record in the
664  * socket buffer sb.  The additional space associated
665  * the mbuf chain is recorded in sb.  Empty mbufs are
666  * discarded and mbufs are compacted where possible.
667  */
668 void
669 sbappend_locked(sb, m)
670 	struct sockbuf *sb;
671 	struct mbuf *m;
672 {
673 	register struct mbuf *n;
674 
675 	SOCKBUF_LOCK_ASSERT(sb);
676 
677 	if (m == 0)
678 		return;
679 
680 	SBLASTRECORDCHK(sb);
681 	n = sb->sb_mb;
682 	if (n) {
683 		while (n->m_nextpkt)
684 			n = n->m_nextpkt;
685 		do {
686 			if (n->m_flags & M_EOR) {
687 				sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
688 				return;
689 			}
690 		} while (n->m_next && (n = n->m_next));
691 	} else {
692 		/*
693 		 * XXX Would like to simply use sb_mbtail here, but
694 		 * XXX I need to verify that I won't miss an EOR that
695 		 * XXX way.
696 		 */
697 		if ((n = sb->sb_lastrecord) != NULL) {
698 			do {
699 				if (n->m_flags & M_EOR) {
700 					sbappendrecord_locked(sb, m); /* XXXXXX!!!! */
701 					return;
702 				}
703 			} while (n->m_next && (n = n->m_next));
704 		} else {
705 			/*
706 			 * If this is the first record in the socket buffer,
707 			 * it's also the last record.
708 			 */
709 			sb->sb_lastrecord = m;
710 		}
711 	}
712 	sbcompress(sb, m, n);
713 	SBLASTRECORDCHK(sb);
714 }
715 
716 /*
717  * Append mbuf chain m to the last record in the
718  * socket buffer sb.  The additional space associated
719  * the mbuf chain is recorded in sb.  Empty mbufs are
720  * discarded and mbufs are compacted where possible.
721  */
722 void
723 sbappend(sb, m)
724 	struct sockbuf *sb;
725 	struct mbuf *m;
726 {
727 
728 	SOCKBUF_LOCK(sb);
729 	sbappend_locked(sb, m);
730 	SOCKBUF_UNLOCK(sb);
731 }
732 
733 /*
734  * This version of sbappend() should only be used when the caller
735  * absolutely knows that there will never be more than one record
736  * in the socket buffer, that is, a stream protocol (such as TCP).
737  */
738 void
739 sbappendstream_locked(struct sockbuf *sb, struct mbuf *m)
740 {
741 	SOCKBUF_LOCK_ASSERT(sb);
742 
743 	KASSERT(m->m_nextpkt == NULL,("sbappendstream 0"));
744 	KASSERT(sb->sb_mb == sb->sb_lastrecord,("sbappendstream 1"));
745 
746 	SBLASTMBUFCHK(sb);
747 
748 	sbcompress(sb, m, sb->sb_mbtail);
749 
750 	sb->sb_lastrecord = sb->sb_mb;
751 	SBLASTRECORDCHK(sb);
752 }
753 
754 /*
755  * This version of sbappend() should only be used when the caller
756  * absolutely knows that there will never be more than one record
757  * in the socket buffer, that is, a stream protocol (such as TCP).
758  */
759 void
760 sbappendstream(struct sockbuf *sb, struct mbuf *m)
761 {
762 
763 	SOCKBUF_LOCK(sb);
764 	sbappendstream_locked(sb, m);
765 	SOCKBUF_UNLOCK(sb);
766 }
767 
768 #ifdef SOCKBUF_DEBUG
769 void
770 sbcheck(sb)
771 	struct sockbuf *sb;
772 {
773 	struct mbuf *m;
774 	struct mbuf *n = 0;
775 	u_long len = 0, mbcnt = 0;
776 
777 	SOCKBUF_LOCK_ASSERT(sb);
778 
779 	for (m = sb->sb_mb; m; m = n) {
780 	    n = m->m_nextpkt;
781 	    for (; m; m = m->m_next) {
782 		len += m->m_len;
783 		mbcnt += MSIZE;
784 		if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
785 			mbcnt += m->m_ext.ext_size;
786 	    }
787 	}
788 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
789 		printf("cc %ld != %u || mbcnt %ld != %u\n", len, sb->sb_cc,
790 		    mbcnt, sb->sb_mbcnt);
791 		panic("sbcheck");
792 	}
793 }
794 #endif
795 
796 /*
797  * As above, except the mbuf chain
798  * begins a new record.
799  */
800 void
801 sbappendrecord_locked(sb, m0)
802 	register struct sockbuf *sb;
803 	register struct mbuf *m0;
804 {
805 	register struct mbuf *m;
806 
807 	SOCKBUF_LOCK_ASSERT(sb);
808 
809 	if (m0 == 0)
810 		return;
811 	m = sb->sb_mb;
812 	if (m)
813 		while (m->m_nextpkt)
814 			m = m->m_nextpkt;
815 	/*
816 	 * Put the first mbuf on the queue.
817 	 * Note this permits zero length records.
818 	 */
819 	sballoc(sb, m0);
820 	SBLASTRECORDCHK(sb);
821 	SBLINKRECORD(sb, m0);
822 	if (m)
823 		m->m_nextpkt = m0;
824 	else
825 		sb->sb_mb = m0;
826 	m = m0->m_next;
827 	m0->m_next = 0;
828 	if (m && (m0->m_flags & M_EOR)) {
829 		m0->m_flags &= ~M_EOR;
830 		m->m_flags |= M_EOR;
831 	}
832 	sbcompress(sb, m, m0);
833 }
834 
835 /*
836  * As above, except the mbuf chain
837  * begins a new record.
838  */
839 void
840 sbappendrecord(sb, m0)
841 	register struct sockbuf *sb;
842 	register struct mbuf *m0;
843 {
844 
845 	SOCKBUF_LOCK(sb);
846 	sbappendrecord_locked(sb, m0);
847 	SOCKBUF_UNLOCK(sb);
848 }
849 
850 /*
851  * As above except that OOB data
852  * is inserted at the beginning of the sockbuf,
853  * but after any other OOB data.
854  */
855 void
856 sbinsertoob_locked(sb, m0)
857 	register struct sockbuf *sb;
858 	register struct mbuf *m0;
859 {
860 	register struct mbuf *m;
861 	register struct mbuf **mp;
862 
863 	SOCKBUF_LOCK_ASSERT(sb);
864 
865 	if (m0 == 0)
866 		return;
867 	for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
868 	    m = *mp;
869 	    again:
870 		switch (m->m_type) {
871 
872 		case MT_OOBDATA:
873 			continue;		/* WANT next train */
874 
875 		case MT_CONTROL:
876 			m = m->m_next;
877 			if (m)
878 				goto again;	/* inspect THIS train further */
879 		}
880 		break;
881 	}
882 	/*
883 	 * Put the first mbuf on the queue.
884 	 * Note this permits zero length records.
885 	 */
886 	sballoc(sb, m0);
887 	m0->m_nextpkt = *mp;
888 	*mp = m0;
889 	m = m0->m_next;
890 	m0->m_next = 0;
891 	if (m && (m0->m_flags & M_EOR)) {
892 		m0->m_flags &= ~M_EOR;
893 		m->m_flags |= M_EOR;
894 	}
895 	sbcompress(sb, m, m0);
896 }
897 
898 /*
899  * As above except that OOB data
900  * is inserted at the beginning of the sockbuf,
901  * but after any other OOB data.
902  */
903 void
904 sbinsertoob(sb, m0)
905 	register struct sockbuf *sb;
906 	register struct mbuf *m0;
907 {
908 
909 	SOCKBUF_LOCK(sb);
910 	sbinsertoob_locked(sb, m0);
911 	SOCKBUF_UNLOCK(sb);
912 }
913 
914 /*
915  * Append address and data, and optionally, control (ancillary) data
916  * to the receive queue of a socket.  If present,
917  * m0 must include a packet header with total length.
918  * Returns 0 if no space in sockbuf or insufficient mbufs.
919  */
920 int
921 sbappendaddr_locked(sb, asa, m0, control)
922 	struct sockbuf *sb;
923 	const struct sockaddr *asa;
924 	struct mbuf *m0, *control;
925 {
926 	struct mbuf *m, *n, *nlast;
927 	int space = asa->sa_len;
928 
929 	SOCKBUF_LOCK_ASSERT(sb);
930 
931 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
932 		panic("sbappendaddr_locked");
933 	if (m0)
934 		space += m0->m_pkthdr.len;
935 	space += m_length(control, &n);
936 
937 	if (space > sbspace(sb))
938 		return (0);
939 #if MSIZE <= 256
940 	if (asa->sa_len > MLEN)
941 		return (0);
942 #endif
943 	MGET(m, M_DONTWAIT, MT_SONAME);
944 	if (m == 0)
945 		return (0);
946 	m->m_len = asa->sa_len;
947 	bcopy(asa, mtod(m, caddr_t), asa->sa_len);
948 	if (n)
949 		n->m_next = m0;		/* concatenate data to control */
950 	else
951 		control = m0;
952 	m->m_next = control;
953 	for (n = m; n->m_next != NULL; n = n->m_next)
954 		sballoc(sb, n);
955 	sballoc(sb, n);
956 	nlast = n;
957 	SBLINKRECORD(sb, m);
958 
959 	sb->sb_mbtail = nlast;
960 	SBLASTMBUFCHK(sb);
961 
962 	SBLASTRECORDCHK(sb);
963 	return (1);
964 }
965 
966 /*
967  * Append address and data, and optionally, control (ancillary) data
968  * to the receive queue of a socket.  If present,
969  * m0 must include a packet header with total length.
970  * Returns 0 if no space in sockbuf or insufficient mbufs.
971  */
972 int
973 sbappendaddr(sb, asa, m0, control)
974 	struct sockbuf *sb;
975 	const struct sockaddr *asa;
976 	struct mbuf *m0, *control;
977 {
978 	int retval;
979 
980 	SOCKBUF_LOCK(sb);
981 	retval = sbappendaddr_locked(sb, asa, m0, control);
982 	SOCKBUF_UNLOCK(sb);
983 	return (retval);
984 }
985 
986 int
987 sbappendcontrol_locked(sb, m0, control)
988 	struct sockbuf *sb;
989 	struct mbuf *control, *m0;
990 {
991 	struct mbuf *m, *n, *mlast;
992 	int space;
993 
994 	SOCKBUF_LOCK_ASSERT(sb);
995 
996 	if (control == 0)
997 		panic("sbappendcontrol_locked");
998 	space = m_length(control, &n) + m_length(m0, NULL);
999 
1000 	if (space > sbspace(sb))
1001 		return (0);
1002 	n->m_next = m0;			/* concatenate data to control */
1003 
1004 	SBLASTRECORDCHK(sb);
1005 
1006 	for (m = control; m->m_next; m = m->m_next)
1007 		sballoc(sb, m);
1008 	sballoc(sb, m);
1009 	mlast = m;
1010 	SBLINKRECORD(sb, control);
1011 
1012 	sb->sb_mbtail = mlast;
1013 	SBLASTMBUFCHK(sb);
1014 
1015 	SBLASTRECORDCHK(sb);
1016 	return (1);
1017 }
1018 
1019 int
1020 sbappendcontrol(sb, m0, control)
1021 	struct sockbuf *sb;
1022 	struct mbuf *control, *m0;
1023 {
1024 	int retval;
1025 
1026 	SOCKBUF_LOCK(sb);
1027 	retval = sbappendcontrol_locked(sb, m0, control);
1028 	SOCKBUF_UNLOCK(sb);
1029 	return (retval);
1030 }
1031 
1032 /*
1033  * Compress mbuf chain m into the socket
1034  * buffer sb following mbuf n.  If n
1035  * is null, the buffer is presumed empty.
1036  */
1037 void
1038 sbcompress(sb, m, n)
1039 	register struct sockbuf *sb;
1040 	register struct mbuf *m, *n;
1041 {
1042 	register int eor = 0;
1043 	register struct mbuf *o;
1044 
1045 	SOCKBUF_LOCK_ASSERT(sb);
1046 
1047 	while (m) {
1048 		eor |= m->m_flags & M_EOR;
1049 		if (m->m_len == 0 &&
1050 		    (eor == 0 ||
1051 		     (((o = m->m_next) || (o = n)) &&
1052 		      o->m_type == m->m_type))) {
1053 			if (sb->sb_lastrecord == m)
1054 				sb->sb_lastrecord = m->m_next;
1055 			m = m_free(m);
1056 			continue;
1057 		}
1058 		if (n && (n->m_flags & M_EOR) == 0 &&
1059 		    M_WRITABLE(n) &&
1060 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1061 		    m->m_len <= M_TRAILINGSPACE(n) &&
1062 		    n->m_type == m->m_type) {
1063 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1064 			    (unsigned)m->m_len);
1065 			n->m_len += m->m_len;
1066 			sb->sb_cc += m->m_len;
1067 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1068 			    m->m_type != MT_OOBDATA)
1069 				/* XXX: Probably don't need.*/
1070 				sb->sb_ctl += m->m_len;
1071 			m = m_free(m);
1072 			continue;
1073 		}
1074 		if (n)
1075 			n->m_next = m;
1076 		else
1077 			sb->sb_mb = m;
1078 		sb->sb_mbtail = m;
1079 		sballoc(sb, m);
1080 		n = m;
1081 		m->m_flags &= ~M_EOR;
1082 		m = m->m_next;
1083 		n->m_next = 0;
1084 	}
1085 	if (eor) {
1086 		if (n)
1087 			n->m_flags |= eor;
1088 		else
1089 			printf("semi-panic: sbcompress\n");
1090 	}
1091 	SBLASTMBUFCHK(sb);
1092 }
1093 
1094 /*
1095  * Free all mbufs in a sockbuf.
1096  * Check that all resources are reclaimed.
1097  */
1098 void
1099 sbflush_locked(sb)
1100 	register struct sockbuf *sb;
1101 {
1102 
1103 	SOCKBUF_LOCK_ASSERT(sb);
1104 
1105 	if (sb->sb_flags & SB_LOCK)
1106 		panic("sbflush_locked: locked");
1107 	while (sb->sb_mbcnt) {
1108 		/*
1109 		 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1110 		 * we would loop forever. Panic instead.
1111 		 */
1112 		if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
1113 			break;
1114 		sbdrop_locked(sb, (int)sb->sb_cc);
1115 	}
1116 	if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
1117 		panic("sbflush_locked: cc %u || mb %p || mbcnt %u", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
1118 }
1119 
1120 void
1121 sbflush(sb)
1122 	register struct sockbuf *sb;
1123 {
1124 
1125 	SOCKBUF_LOCK(sb);
1126 	sbflush_locked(sb);
1127 	SOCKBUF_UNLOCK(sb);
1128 }
1129 
1130 /*
1131  * Drop data from (the front of) a sockbuf.
1132  */
1133 void
1134 sbdrop_locked(sb, len)
1135 	register struct sockbuf *sb;
1136 	register int len;
1137 {
1138 	register struct mbuf *m;
1139 	struct mbuf *next;
1140 
1141 	SOCKBUF_LOCK_ASSERT(sb);
1142 
1143 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1144 	while (len > 0) {
1145 		if (m == 0) {
1146 			if (next == 0)
1147 				panic("sbdrop");
1148 			m = next;
1149 			next = m->m_nextpkt;
1150 			continue;
1151 		}
1152 		if (m->m_len > len) {
1153 			m->m_len -= len;
1154 			m->m_data += len;
1155 			sb->sb_cc -= len;
1156 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1157 			    m->m_type != MT_OOBDATA)
1158 				sb->sb_ctl -= len;
1159 			break;
1160 		}
1161 		len -= m->m_len;
1162 		sbfree(sb, m);
1163 		m = m_free(m);
1164 	}
1165 	while (m && m->m_len == 0) {
1166 		sbfree(sb, m);
1167 		m = m_free(m);
1168 	}
1169 	if (m) {
1170 		sb->sb_mb = m;
1171 		m->m_nextpkt = next;
1172 	} else
1173 		sb->sb_mb = next;
1174 	/*
1175 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
1176 	 * makes sure sb_lastrecord is up-to-date if we dropped
1177 	 * part of the last record.
1178 	 */
1179 	m = sb->sb_mb;
1180 	if (m == NULL) {
1181 		sb->sb_mbtail = NULL;
1182 		sb->sb_lastrecord = NULL;
1183 	} else if (m->m_nextpkt == NULL) {
1184 		sb->sb_lastrecord = m;
1185 	}
1186 }
1187 
1188 /*
1189  * Drop data from (the front of) a sockbuf.
1190  */
1191 void
1192 sbdrop(sb, len)
1193 	register struct sockbuf *sb;
1194 	register int len;
1195 {
1196 
1197 	SOCKBUF_LOCK(sb);
1198 	sbdrop_locked(sb, len);
1199 	SOCKBUF_UNLOCK(sb);
1200 }
1201 
1202 /*
1203  * Drop a record off the front of a sockbuf
1204  * and move the next record to the front.
1205  */
1206 void
1207 sbdroprecord_locked(sb)
1208 	register struct sockbuf *sb;
1209 {
1210 	register struct mbuf *m;
1211 
1212 	SOCKBUF_LOCK_ASSERT(sb);
1213 
1214 	m = sb->sb_mb;
1215 	if (m) {
1216 		sb->sb_mb = m->m_nextpkt;
1217 		do {
1218 			sbfree(sb, m);
1219 			m = m_free(m);
1220 		} while (m);
1221 	}
1222 	SB_EMPTY_FIXUP(sb);
1223 }
1224 
1225 /*
1226  * Drop a record off the front of a sockbuf
1227  * and move the next record to the front.
1228  */
1229 void
1230 sbdroprecord(sb)
1231 	register struct sockbuf *sb;
1232 {
1233 
1234 	SOCKBUF_LOCK(sb);
1235 	sbdroprecord_locked(sb);
1236 	SOCKBUF_UNLOCK(sb);
1237 }
1238 
1239 /*
1240  * Create a "control" mbuf containing the specified data
1241  * with the specified type for presentation on a socket buffer.
1242  */
1243 struct mbuf *
1244 sbcreatecontrol(p, size, type, level)
1245 	caddr_t p;
1246 	register int size;
1247 	int type, level;
1248 {
1249 	register struct cmsghdr *cp;
1250 	struct mbuf *m;
1251 
1252 	if (CMSG_SPACE((u_int)size) > MCLBYTES)
1253 		return ((struct mbuf *) NULL);
1254 	if (CMSG_SPACE((u_int)size > MLEN))
1255 		m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
1256 	else
1257 		m = m_get(M_DONTWAIT, MT_CONTROL);
1258 	if (m == NULL)
1259 		return ((struct mbuf *) NULL);
1260 	cp = mtod(m, struct cmsghdr *);
1261 	m->m_len = 0;
1262 	KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
1263 	    ("sbcreatecontrol: short mbuf"));
1264 	if (p != NULL)
1265 		(void)memcpy(CMSG_DATA(cp), p, size);
1266 	m->m_len = CMSG_SPACE(size);
1267 	cp->cmsg_len = CMSG_LEN(size);
1268 	cp->cmsg_level = level;
1269 	cp->cmsg_type = type;
1270 	return (m);
1271 }
1272 
1273 /*
1274  * Some routines that return EOPNOTSUPP for entry points that are not
1275  * supported by a protocol.  Fill in as needed.
1276  */
1277 int
1278 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1279 {
1280 	return EOPNOTSUPP;
1281 }
1282 
1283 int
1284 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
1285 {
1286 	return EOPNOTSUPP;
1287 }
1288 
1289 int
1290 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1291 {
1292 	return EOPNOTSUPP;
1293 }
1294 
1295 int
1296 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1297 		    struct ifnet *ifp, struct thread *td)
1298 {
1299 	return EOPNOTSUPP;
1300 }
1301 
1302 int
1303 pru_listen_notsupp(struct socket *so, struct thread *td)
1304 {
1305 	return EOPNOTSUPP;
1306 }
1307 
1308 int
1309 pru_rcvd_notsupp(struct socket *so, int flags)
1310 {
1311 	return EOPNOTSUPP;
1312 }
1313 
1314 int
1315 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1316 {
1317 	return EOPNOTSUPP;
1318 }
1319 
1320 /*
1321  * This isn't really a ``null'' operation, but it's the default one
1322  * and doesn't do anything destructive.
1323  */
1324 int
1325 pru_sense_null(struct socket *so, struct stat *sb)
1326 {
1327 	sb->st_blksize = so->so_snd.sb_hiwat;
1328 	return 0;
1329 }
1330 
1331 /*
1332  * For protocol types that don't keep cached copies of labels in their
1333  * pcbs, provide a null sosetlabel that does a NOOP.
1334  */
1335 void
1336 pru_sosetlabel_null(struct socket *so)
1337 {
1338 
1339 }
1340 
1341 /*
1342  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1343  */
1344 struct sockaddr *
1345 sodupsockaddr(const struct sockaddr *sa, int mflags)
1346 {
1347 	struct sockaddr *sa2;
1348 
1349 	sa2 = malloc(sa->sa_len, M_SONAME, mflags);
1350 	if (sa2)
1351 		bcopy(sa, sa2, sa->sa_len);
1352 	return sa2;
1353 }
1354 
1355 /*
1356  * Create an external-format (``xsocket'') structure using the information
1357  * in the kernel-format socket structure pointed to by so.  This is done
1358  * to reduce the spew of irrelevant information over this interface,
1359  * to isolate user code from changes in the kernel structure, and
1360  * potentially to provide information-hiding if we decide that
1361  * some of this information should be hidden from users.
1362  */
1363 void
1364 sotoxsocket(struct socket *so, struct xsocket *xso)
1365 {
1366 	xso->xso_len = sizeof *xso;
1367 	xso->xso_so = so;
1368 	xso->so_type = so->so_type;
1369 	xso->so_options = so->so_options;
1370 	xso->so_linger = so->so_linger;
1371 	xso->so_state = so->so_state;
1372 	xso->so_pcb = so->so_pcb;
1373 	xso->xso_protocol = so->so_proto->pr_protocol;
1374 	xso->xso_family = so->so_proto->pr_domain->dom_family;
1375 	xso->so_qlen = so->so_qlen;
1376 	xso->so_incqlen = so->so_incqlen;
1377 	xso->so_qlimit = so->so_qlimit;
1378 	xso->so_timeo = so->so_timeo;
1379 	xso->so_error = so->so_error;
1380 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1381 	xso->so_oobmark = so->so_oobmark;
1382 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1383 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1384 	xso->so_uid = so->so_cred->cr_uid;
1385 }
1386 
1387 /*
1388  * This does the same for sockbufs.  Note that the xsockbuf structure,
1389  * since it is always embedded in a socket, does not include a self
1390  * pointer nor a length.  We make this entry point public in case
1391  * some other mechanism needs it.
1392  */
1393 void
1394 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1395 {
1396 	xsb->sb_cc = sb->sb_cc;
1397 	xsb->sb_hiwat = sb->sb_hiwat;
1398 	xsb->sb_mbcnt = sb->sb_mbcnt;
1399 	xsb->sb_mbmax = sb->sb_mbmax;
1400 	xsb->sb_lowat = sb->sb_lowat;
1401 	xsb->sb_flags = sb->sb_flags;
1402 	xsb->sb_timeo = sb->sb_timeo;
1403 }
1404 
1405 /*
1406  * Here is the definition of some of the basic objects in the kern.ipc
1407  * branch of the MIB.
1408  */
1409 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1410 
1411 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1412 static int dummy;
1413 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1414 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_ULONG|CTLFLAG_RW,
1415     &sb_max, 0, sysctl_handle_sb_max, "LU", "Maximum socket buffer size");
1416 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RDTUN,
1417     &maxsockets, 0, "Maximum number of sockets avaliable");
1418 SYSCTL_ULONG(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1419     &sb_efficiency, 0, "");
1420 
1421 /*
1422  * Initialise maxsockets
1423  */
1424 static void init_maxsockets(void *ignored)
1425 {
1426 	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1427 	maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1428 }
1429 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
1430