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