uipc_socket2.c revision 1.110
150476Speter/*	$NetBSD: uipc_socket2.c,v 1.110 2011/12/20 23:56:28 christos Exp $	*/
22006Swollman
32006Swollman/*-
4139103Sru * Copyright (c) 2008 The NetBSD Foundation, Inc.
52006Swollman * All rights reserved.
62006Swollman *
72006Swollman * Redistribution and use in source and binary forms, with or without
82006Swollman * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 1982, 1986, 1988, 1990, 1993
31 *	The Regents of the University of California.  All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 *    may be used to endorse or promote products derived from this software
43 *    without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 *	@(#)uipc_socket2.c	8.2 (Berkeley) 2/14/95
58 */
59
60#include <sys/cdefs.h>
61__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.110 2011/12/20 23:56:28 christos Exp $");
62
63#include "opt_mbuftrace.h"
64#include "opt_sb_max.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/proc.h>
69#include <sys/file.h>
70#include <sys/buf.h>
71#include <sys/mbuf.h>
72#include <sys/protosw.h>
73#include <sys/domain.h>
74#include <sys/poll.h>
75#include <sys/socket.h>
76#include <sys/socketvar.h>
77#include <sys/signalvar.h>
78#include <sys/kauth.h>
79#include <sys/pool.h>
80#include <sys/uidinfo.h>
81
82/*
83 * Primitive routines for operating on sockets and socket buffers.
84 *
85 * Locking rules and assumptions:
86 *
87 * o socket::so_lock can change on the fly.  The low level routines used
88 *   to lock sockets are aware of this.  When so_lock is acquired, the
89 *   routine locking must check to see if so_lock still points to the
90 *   lock that was acquired.  If so_lock has changed in the meantime, the
91 *   now irellevant lock that was acquired must be dropped and the lock
92 *   operation retried.  Although not proven here, this is completely safe
93 *   on a multiprocessor system, even with relaxed memory ordering, given
94 *   the next two rules:
95 *
96 * o In order to mutate so_lock, the lock pointed to by the current value
97 *   of so_lock must be held: i.e., the socket must be held locked by the
98 *   changing thread.  The thread must issue membar_exit() to prevent
99 *   memory accesses being reordered, and can set so_lock to the desired
100 *   value.  If the lock pointed to by the new value of so_lock is not
101 *   held by the changing thread, the socket must then be considered
102 *   unlocked.
103 *
104 * o If so_lock is mutated, and the previous lock referred to by so_lock
105 *   could still be visible to other threads in the system (e.g. via file
106 *   descriptor or protocol-internal reference), then the old lock must
107 *   remain valid until the socket and/or protocol control block has been
108 *   torn down.
109 *
110 * o If a socket has a non-NULL so_head value (i.e. is in the process of
111 *   connecting), then locking the socket must also lock the socket pointed
112 *   to by so_head: their lock pointers must match.
113 *
114 * o If a socket has connections in progress (so_q, so_q0 not empty) then
115 *   locking the socket must also lock the sockets attached to both queues.
116 *   Again, their lock pointers must match.
117 *
118 * o Beyond the initial lock assigment in socreate(), assigning locks to
119 *   sockets is the responsibility of the individual protocols / protocol
120 *   domains.
121 */
122
123static pool_cache_t socket_cache;
124
125u_long	sb_max = SB_MAX;	/* maximum socket buffer size */
126static u_long sb_max_adj;	/* adjusted sb_max */
127
128/*
129 * Procedures to manipulate state flags of socket
130 * and do appropriate wakeups.  Normal sequence from the
131 * active (originating) side is that soisconnecting() is
132 * called during processing of connect() call,
133 * resulting in an eventual call to soisconnected() if/when the
134 * connection is established.  When the connection is torn down
135 * soisdisconnecting() is called during processing of disconnect() call,
136 * and soisdisconnected() is called when the connection to the peer
137 * is totally severed.  The semantics of these routines are such that
138 * connectionless protocols can call soisconnected() and soisdisconnected()
139 * only, bypassing the in-progress calls when setting up a ``connection''
140 * takes no time.
141 *
142 * From the passive side, a socket is created with
143 * two queues of sockets: so_q0 for connections in progress
144 * and so_q for connections already made and awaiting user acceptance.
145 * As a protocol is preparing incoming connections, it creates a socket
146 * structure queued on so_q0 by calling sonewconn().  When the connection
147 * is established, soisconnected() is called, and transfers the
148 * socket structure to so_q, making it available to accept().
149 *
150 * If a socket is closed with sockets on either
151 * so_q0 or so_q, these sockets are dropped.
152 *
153 * If higher level protocols are implemented in
154 * the kernel, the wakeups done here will sometimes
155 * cause software-interrupt process scheduling.
156 */
157
158void
159soisconnecting(struct socket *so)
160{
161
162	KASSERT(solocked(so));
163
164	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
165	so->so_state |= SS_ISCONNECTING;
166}
167
168void
169soisconnected(struct socket *so)
170{
171	struct socket	*head;
172
173	head = so->so_head;
174
175	KASSERT(solocked(so));
176	KASSERT(head == NULL || solocked2(so, head));
177
178	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
179	so->so_state |= SS_ISCONNECTED;
180	if (head && so->so_onq == &head->so_q0) {
181		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
182			soqremque(so, 0);
183			soqinsque(head, so, 1);
184			sorwakeup(head);
185			cv_broadcast(&head->so_cv);
186		} else {
187			so->so_upcall =
188			    head->so_accf->so_accept_filter->accf_callback;
189			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
190			so->so_rcv.sb_flags |= SB_UPCALL;
191			so->so_options &= ~SO_ACCEPTFILTER;
192			(*so->so_upcall)(so, so->so_upcallarg,
193					 POLLIN|POLLRDNORM, M_DONTWAIT);
194		}
195	} else {
196		cv_broadcast(&so->so_cv);
197		sorwakeup(so);
198		sowwakeup(so);
199	}
200}
201
202void
203soisdisconnecting(struct socket *so)
204{
205
206	KASSERT(solocked(so));
207
208	so->so_state &= ~SS_ISCONNECTING;
209	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
210	cv_broadcast(&so->so_cv);
211	sowwakeup(so);
212	sorwakeup(so);
213}
214
215void
216soisdisconnected(struct socket *so)
217{
218
219	KASSERT(solocked(so));
220
221	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
222	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
223	cv_broadcast(&so->so_cv);
224	sowwakeup(so);
225	sorwakeup(so);
226}
227
228void
229soinit2(void)
230{
231
232	socket_cache = pool_cache_init(sizeof(struct socket), 0, 0, 0,
233	    "socket", NULL, IPL_SOFTNET, NULL, NULL, NULL);
234}
235
236/*
237 * When an attempt at a new connection is noted on a socket
238 * which accepts connections, sonewconn is called.  If the
239 * connection is possible (subject to space constraints, etc.)
240 * then we allocate a new structure, propoerly linked into the
241 * data structure of the original socket, and return this.
242 * Connstatus may be 0, SS_ISCONFIRMING, or SS_ISCONNECTED.
243 */
244struct socket *
245sonewconn(struct socket *head, int connstatus)
246{
247	struct socket	*so;
248	int		soqueue, error;
249
250	KASSERT(connstatus == 0 || connstatus == SS_ISCONFIRMING ||
251	    connstatus == SS_ISCONNECTED);
252	KASSERT(solocked(head));
253
254	if ((head->so_options & SO_ACCEPTFILTER) != 0)
255		connstatus = 0;
256	soqueue = connstatus ? 1 : 0;
257	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
258		return NULL;
259	so = soget(false);
260	if (so == NULL)
261		return NULL;
262	mutex_obj_hold(head->so_lock);
263	so->so_lock = head->so_lock;
264	so->so_type = head->so_type;
265	so->so_options = head->so_options &~ SO_ACCEPTCONN;
266	so->so_linger = head->so_linger;
267	so->so_state = head->so_state | SS_NOFDREF;
268	so->so_proto = head->so_proto;
269	so->so_timeo = head->so_timeo;
270	so->so_pgid = head->so_pgid;
271	so->so_send = head->so_send;
272	so->so_receive = head->so_receive;
273	so->so_uidinfo = head->so_uidinfo;
274	so->so_cpid = head->so_cpid;
275#ifdef MBUFTRACE
276	so->so_mowner = head->so_mowner;
277	so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
278	so->so_snd.sb_mowner = head->so_snd.sb_mowner;
279#endif
280	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) != 0)
281		goto out;
282	so->so_snd.sb_lowat = head->so_snd.sb_lowat;
283	so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
284	so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
285	so->so_snd.sb_timeo = head->so_snd.sb_timeo;
286	so->so_rcv.sb_flags |= head->so_rcv.sb_flags & (SB_AUTOSIZE | SB_ASYNC);
287	so->so_snd.sb_flags |= head->so_snd.sb_flags & (SB_AUTOSIZE | SB_ASYNC);
288	soqinsque(head, so, soqueue);
289	error = (*so->so_proto->pr_usrreq)(so, PRU_ATTACH, NULL, NULL,
290	    NULL, NULL);
291	KASSERT(solocked(so));
292	if (error != 0) {
293		(void) soqremque(so, soqueue);
294out:
295		/*
296		 * Remove acccept filter if one is present.
297		 * XXX Is this really needed?
298		 */
299		if (so->so_accf != NULL)
300			(void)accept_filt_clear(so);
301		soput(so);
302		return NULL;
303	}
304	if (connstatus) {
305		sorwakeup(head);
306		cv_broadcast(&head->so_cv);
307		so->so_state |= connstatus;
308	}
309	return so;
310}
311
312struct socket *
313soget(bool waitok)
314{
315	struct socket *so;
316
317	so = pool_cache_get(socket_cache, (waitok ? PR_WAITOK : PR_NOWAIT));
318	if (__predict_false(so == NULL))
319		return (NULL);
320	memset(so, 0, sizeof(*so));
321	TAILQ_INIT(&so->so_q0);
322	TAILQ_INIT(&so->so_q);
323	cv_init(&so->so_cv, "socket");
324	cv_init(&so->so_rcv.sb_cv, "netio");
325	cv_init(&so->so_snd.sb_cv, "netio");
326	selinit(&so->so_rcv.sb_sel);
327	selinit(&so->so_snd.sb_sel);
328	so->so_rcv.sb_so = so;
329	so->so_snd.sb_so = so;
330	return so;
331}
332
333void
334soput(struct socket *so)
335{
336
337	KASSERT(!cv_has_waiters(&so->so_cv));
338	KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv));
339	KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
340	seldestroy(&so->so_rcv.sb_sel);
341	seldestroy(&so->so_snd.sb_sel);
342	mutex_obj_free(so->so_lock);
343	cv_destroy(&so->so_cv);
344	cv_destroy(&so->so_rcv.sb_cv);
345	cv_destroy(&so->so_snd.sb_cv);
346	pool_cache_put(socket_cache, so);
347}
348
349void
350soqinsque(struct socket *head, struct socket *so, int q)
351{
352
353	KASSERT(solocked2(head, so));
354
355#ifdef DIAGNOSTIC
356	if (so->so_onq != NULL)
357		panic("soqinsque");
358#endif
359
360	so->so_head = head;
361	if (q == 0) {
362		head->so_q0len++;
363		so->so_onq = &head->so_q0;
364	} else {
365		head->so_qlen++;
366		so->so_onq = &head->so_q;
367	}
368	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
369}
370
371int
372soqremque(struct socket *so, int q)
373{
374	struct socket	*head;
375
376	head = so->so_head;
377
378	KASSERT(solocked(so));
379	if (q == 0) {
380		if (so->so_onq != &head->so_q0)
381			return (0);
382		head->so_q0len--;
383	} else {
384		if (so->so_onq != &head->so_q)
385			return (0);
386		head->so_qlen--;
387	}
388	KASSERT(solocked2(so, head));
389	TAILQ_REMOVE(so->so_onq, so, so_qe);
390	so->so_onq = NULL;
391	so->so_head = NULL;
392	return (1);
393}
394
395/*
396 * Socantsendmore indicates that no more data will be sent on the
397 * socket; it would normally be applied to a socket when the user
398 * informs the system that no more data is to be sent, by the protocol
399 * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
400 * will be received, and will normally be applied to the socket by a
401 * protocol when it detects that the peer will send no more data.
402 * Data queued for reading in the socket may yet be read.
403 */
404
405void
406socantsendmore(struct socket *so)
407{
408
409	KASSERT(solocked(so));
410
411	so->so_state |= SS_CANTSENDMORE;
412	sowwakeup(so);
413}
414
415void
416socantrcvmore(struct socket *so)
417{
418
419	KASSERT(solocked(so));
420
421	so->so_state |= SS_CANTRCVMORE;
422	sorwakeup(so);
423}
424
425/*
426 * Wait for data to arrive at/drain from a socket buffer.
427 */
428int
429sbwait(struct sockbuf *sb)
430{
431	struct socket *so;
432	kmutex_t *lock;
433	int error;
434
435	so = sb->sb_so;
436
437	KASSERT(solocked(so));
438
439	sb->sb_flags |= SB_NOTIFY;
440	lock = so->so_lock;
441	if ((sb->sb_flags & SB_NOINTR) != 0)
442		error = cv_timedwait(&sb->sb_cv, lock, sb->sb_timeo);
443	else
444		error = cv_timedwait_sig(&sb->sb_cv, lock, sb->sb_timeo);
445	if (__predict_false(lock != so->so_lock))
446		solockretry(so, lock);
447	return error;
448}
449
450/*
451 * Wakeup processes waiting on a socket buffer.
452 * Do asynchronous notification via SIGIO
453 * if the socket buffer has the SB_ASYNC flag set.
454 */
455void
456sowakeup(struct socket *so, struct sockbuf *sb, int code)
457{
458	int band;
459
460	KASSERT(solocked(so));
461	KASSERT(sb->sb_so == so);
462
463	if (code == POLL_IN)
464		band = POLLIN|POLLRDNORM;
465	else
466		band = POLLOUT|POLLWRNORM;
467	sb->sb_flags &= ~SB_NOTIFY;
468	selnotify(&sb->sb_sel, band, NOTE_SUBMIT);
469	cv_broadcast(&sb->sb_cv);
470	if (sb->sb_flags & SB_ASYNC)
471		fownsignal(so->so_pgid, SIGIO, code, band, so);
472	if (sb->sb_flags & SB_UPCALL)
473		(*so->so_upcall)(so, so->so_upcallarg, band, M_DONTWAIT);
474}
475
476/*
477 * Reset a socket's lock pointer.  Wake all threads waiting on the
478 * socket's condition variables so that they can restart their waits
479 * using the new lock.  The existing lock must be held.
480 */
481void
482solockreset(struct socket *so, kmutex_t *lock)
483{
484
485	KASSERT(solocked(so));
486
487	so->so_lock = lock;
488	cv_broadcast(&so->so_snd.sb_cv);
489	cv_broadcast(&so->so_rcv.sb_cv);
490	cv_broadcast(&so->so_cv);
491}
492
493/*
494 * Socket buffer (struct sockbuf) utility routines.
495 *
496 * Each socket contains two socket buffers: one for sending data and
497 * one for receiving data.  Each buffer contains a queue of mbufs,
498 * information about the number of mbufs and amount of data in the
499 * queue, and other fields allowing poll() statements and notification
500 * on data availability to be implemented.
501 *
502 * Data stored in a socket buffer is maintained as a list of records.
503 * Each record is a list of mbufs chained together with the m_next
504 * field.  Records are chained together with the m_nextpkt field. The upper
505 * level routine soreceive() expects the following conventions to be
506 * observed when placing information in the receive buffer:
507 *
508 * 1. If the protocol requires each message be preceded by the sender's
509 *    name, then a record containing that name must be present before
510 *    any associated data (mbuf's must be of type MT_SONAME).
511 * 2. If the protocol supports the exchange of ``access rights'' (really
512 *    just additional data associated with the message), and there are
513 *    ``rights'' to be received, then a record containing this data
514 *    should be present (mbuf's must be of type MT_CONTROL).
515 * 3. If a name or rights record exists, then it must be followed by
516 *    a data record, perhaps of zero length.
517 *
518 * Before using a new socket structure it is first necessary to reserve
519 * buffer space to the socket, by calling sbreserve().  This should commit
520 * some of the available buffer space in the system buffer pool for the
521 * socket (currently, it does nothing but enforce limits).  The space
522 * should be released by calling sbrelease() when the socket is destroyed.
523 */
524
525int
526sb_max_set(u_long new_sbmax)
527{
528	int s;
529
530	if (new_sbmax < (16 * 1024))
531		return (EINVAL);
532
533	s = splsoftnet();
534	sb_max = new_sbmax;
535	sb_max_adj = (u_quad_t)new_sbmax * MCLBYTES / (MSIZE + MCLBYTES);
536	splx(s);
537
538	return (0);
539}
540
541int
542soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
543{
544
545	KASSERT(so->so_lock == NULL || solocked(so));
546
547	/*
548	 * there's at least one application (a configure script of screen)
549	 * which expects a fifo is writable even if it has "some" bytes
550	 * in its buffer.
551	 * so we want to make sure (hiwat - lowat) >= (some bytes).
552	 *
553	 * PIPE_BUF here is an arbitrary value chosen as (some bytes) above.
554	 * we expect it's large enough for such applications.
555	 */
556	u_long  lowat = MAX(sock_loan_thresh, MCLBYTES);
557	u_long  hiwat = lowat + PIPE_BUF;
558
559	if (sndcc < hiwat)
560		sndcc = hiwat;
561	if (sbreserve(&so->so_snd, sndcc, so) == 0)
562		goto bad;
563	if (sbreserve(&so->so_rcv, rcvcc, so) == 0)
564		goto bad2;
565	if (so->so_rcv.sb_lowat == 0)
566		so->so_rcv.sb_lowat = 1;
567	if (so->so_snd.sb_lowat == 0)
568		so->so_snd.sb_lowat = lowat;
569	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
570		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
571	return (0);
572 bad2:
573	sbrelease(&so->so_snd, so);
574 bad:
575	return (ENOBUFS);
576}
577
578/*
579 * Allot mbufs to a sockbuf.
580 * Attempt to scale mbmax so that mbcnt doesn't become limiting
581 * if buffering efficiency is near the normal case.
582 */
583int
584sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
585{
586	struct lwp *l = curlwp; /* XXX */
587	rlim_t maxcc;
588	struct uidinfo *uidinfo;
589
590	KASSERT(so->so_lock == NULL || solocked(so));
591	KASSERT(sb->sb_so == so);
592	KASSERT(sb_max_adj != 0);
593
594	if (cc == 0 || cc > sb_max_adj)
595		return (0);
596
597	maxcc = l->l_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur;
598
599	uidinfo = so->so_uidinfo;
600	if (!chgsbsize(uidinfo, &sb->sb_hiwat, cc, maxcc))
601		return 0;
602	sb->sb_mbmax = min(cc * 2, sb_max);
603	if (sb->sb_lowat > sb->sb_hiwat)
604		sb->sb_lowat = sb->sb_hiwat;
605	return (1);
606}
607
608/*
609 * Free mbufs held by a socket, and reserved mbuf space.  We do not assert
610 * that the socket is held locked here: see sorflush().
611 */
612void
613sbrelease(struct sockbuf *sb, struct socket *so)
614{
615
616	KASSERT(sb->sb_so == so);
617
618	sbflush(sb);
619	(void)chgsbsize(so->so_uidinfo, &sb->sb_hiwat, 0, RLIM_INFINITY);
620	sb->sb_mbmax = 0;
621}
622
623/*
624 * Routines to add and remove
625 * data from an mbuf queue.
626 *
627 * The routines sbappend() or sbappendrecord() are normally called to
628 * append new mbufs to a socket buffer, after checking that adequate
629 * space is available, comparing the function sbspace() with the amount
630 * of data to be added.  sbappendrecord() differs from sbappend() in
631 * that data supplied is treated as the beginning of a new record.
632 * To place a sender's address, optional access rights, and data in a
633 * socket receive buffer, sbappendaddr() should be used.  To place
634 * access rights and data in a socket receive buffer, sbappendrights()
635 * should be used.  In either case, the new data begins a new record.
636 * Note that unlike sbappend() and sbappendrecord(), these routines check
637 * for the caller that there will be enough space to store the data.
638 * Each fails if there is not enough space, or if it cannot find mbufs
639 * to store additional information in.
640 *
641 * Reliable protocols may use the socket send buffer to hold data
642 * awaiting acknowledgement.  Data is normally copied from a socket
643 * send buffer in a protocol with m_copy for output to a peer,
644 * and then removing the data from the socket buffer with sbdrop()
645 * or sbdroprecord() when the data is acknowledged by the peer.
646 */
647
648#ifdef SOCKBUF_DEBUG
649void
650sblastrecordchk(struct sockbuf *sb, const char *where)
651{
652	struct mbuf *m = sb->sb_mb;
653
654	KASSERT(solocked(sb->sb_so));
655
656	while (m && m->m_nextpkt)
657		m = m->m_nextpkt;
658
659	if (m != sb->sb_lastrecord) {
660		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
661		    sb->sb_mb, sb->sb_lastrecord, m);
662		printf("packet chain:\n");
663		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
664			printf("\t%p\n", m);
665		panic("sblastrecordchk from %s", where);
666	}
667}
668
669void
670sblastmbufchk(struct sockbuf *sb, const char *where)
671{
672	struct mbuf *m = sb->sb_mb;
673	struct mbuf *n;
674
675	KASSERT(solocked(sb->sb_so));
676
677	while (m && m->m_nextpkt)
678		m = m->m_nextpkt;
679
680	while (m && m->m_next)
681		m = m->m_next;
682
683	if (m != sb->sb_mbtail) {
684		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
685		    sb->sb_mb, sb->sb_mbtail, m);
686		printf("packet tree:\n");
687		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
688			printf("\t");
689			for (n = m; n != NULL; n = n->m_next)
690				printf("%p ", n);
691			printf("\n");
692		}
693		panic("sblastmbufchk from %s", where);
694	}
695}
696#endif /* SOCKBUF_DEBUG */
697
698/*
699 * Link a chain of records onto a socket buffer
700 */
701#define	SBLINKRECORDCHAIN(sb, m0, mlast)				\
702do {									\
703	if ((sb)->sb_lastrecord != NULL)				\
704		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
705	else								\
706		(sb)->sb_mb = (m0);					\
707	(sb)->sb_lastrecord = (mlast);					\
708} while (/*CONSTCOND*/0)
709
710
711#define	SBLINKRECORD(sb, m0)						\
712    SBLINKRECORDCHAIN(sb, m0, m0)
713
714/*
715 * Append mbuf chain m to the last record in the
716 * socket buffer sb.  The additional space associated
717 * the mbuf chain is recorded in sb.  Empty mbufs are
718 * discarded and mbufs are compacted where possible.
719 */
720void
721sbappend(struct sockbuf *sb, struct mbuf *m)
722{
723	struct mbuf	*n;
724
725	KASSERT(solocked(sb->sb_so));
726
727	if (m == 0)
728		return;
729
730#ifdef MBUFTRACE
731	m_claimm(m, sb->sb_mowner);
732#endif
733
734	SBLASTRECORDCHK(sb, "sbappend 1");
735
736	if ((n = sb->sb_lastrecord) != NULL) {
737		/*
738		 * XXX Would like to simply use sb_mbtail here, but
739		 * XXX I need to verify that I won't miss an EOR that
740		 * XXX way.
741		 */
742		do {
743			if (n->m_flags & M_EOR) {
744				sbappendrecord(sb, m); /* XXXXXX!!!! */
745				return;
746			}
747		} while (n->m_next && (n = n->m_next));
748	} else {
749		/*
750		 * If this is the first record in the socket buffer, it's
751		 * also the last record.
752		 */
753		sb->sb_lastrecord = m;
754	}
755	sbcompress(sb, m, n);
756	SBLASTRECORDCHK(sb, "sbappend 2");
757}
758
759/*
760 * This version of sbappend() should only be used when the caller
761 * absolutely knows that there will never be more than one record
762 * in the socket buffer, that is, a stream protocol (such as TCP).
763 */
764void
765sbappendstream(struct sockbuf *sb, struct mbuf *m)
766{
767
768	KASSERT(solocked(sb->sb_so));
769	KDASSERT(m->m_nextpkt == NULL);
770	KASSERT(sb->sb_mb == sb->sb_lastrecord);
771
772	SBLASTMBUFCHK(sb, __func__);
773
774#ifdef MBUFTRACE
775	m_claimm(m, sb->sb_mowner);
776#endif
777
778	sbcompress(sb, m, sb->sb_mbtail);
779
780	sb->sb_lastrecord = sb->sb_mb;
781	SBLASTRECORDCHK(sb, __func__);
782}
783
784#ifdef SOCKBUF_DEBUG
785void
786sbcheck(struct sockbuf *sb)
787{
788	struct mbuf	*m, *m2;
789	u_long		len, mbcnt;
790
791	KASSERT(solocked(sb->sb_so));
792
793	len = 0;
794	mbcnt = 0;
795	for (m = sb->sb_mb; m; m = m->m_nextpkt) {
796		for (m2 = m; m2 != NULL; m2 = m2->m_next) {
797			len += m2->m_len;
798			mbcnt += MSIZE;
799			if (m2->m_flags & M_EXT)
800				mbcnt += m2->m_ext.ext_size;
801			if (m2->m_nextpkt != NULL)
802				panic("sbcheck nextpkt");
803		}
804	}
805	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
806		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
807		    mbcnt, sb->sb_mbcnt);
808		panic("sbcheck");
809	}
810}
811#endif
812
813/*
814 * As above, except the mbuf chain
815 * begins a new record.
816 */
817void
818sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
819{
820	struct mbuf	*m;
821
822	KASSERT(solocked(sb->sb_so));
823
824	if (m0 == 0)
825		return;
826
827#ifdef MBUFTRACE
828	m_claimm(m0, sb->sb_mowner);
829#endif
830	/*
831	 * Put the first mbuf on the queue.
832	 * Note this permits zero length records.
833	 */
834	sballoc(sb, m0);
835	SBLASTRECORDCHK(sb, "sbappendrecord 1");
836	SBLINKRECORD(sb, m0);
837	m = m0->m_next;
838	m0->m_next = 0;
839	if (m && (m0->m_flags & M_EOR)) {
840		m0->m_flags &= ~M_EOR;
841		m->m_flags |= M_EOR;
842	}
843	sbcompress(sb, m, m0);
844	SBLASTRECORDCHK(sb, "sbappendrecord 2");
845}
846
847/*
848 * As above except that OOB data
849 * is inserted at the beginning of the sockbuf,
850 * but after any other OOB data.
851 */
852void
853sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
854{
855	struct mbuf	*m, **mp;
856
857	KASSERT(solocked(sb->sb_so));
858
859	if (m0 == 0)
860		return;
861
862	SBLASTRECORDCHK(sb, "sbinsertoob 1");
863
864	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
865	    again:
866		switch (m->m_type) {
867
868		case MT_OOBDATA:
869			continue;		/* WANT next train */
870
871		case MT_CONTROL:
872			if ((m = m->m_next) != NULL)
873				goto again;	/* inspect THIS train further */
874		}
875		break;
876	}
877	/*
878	 * Put the first mbuf on the queue.
879	 * Note this permits zero length records.
880	 */
881	sballoc(sb, m0);
882	m0->m_nextpkt = *mp;
883	if (*mp == NULL) {
884		/* m0 is actually the new tail */
885		sb->sb_lastrecord = m0;
886	}
887	*mp = m0;
888	m = m0->m_next;
889	m0->m_next = 0;
890	if (m && (m0->m_flags & M_EOR)) {
891		m0->m_flags &= ~M_EOR;
892		m->m_flags |= M_EOR;
893	}
894	sbcompress(sb, m, m0);
895	SBLASTRECORDCHK(sb, "sbinsertoob 2");
896}
897
898/*
899 * Append address and data, and optionally, control (ancillary) data
900 * to the receive queue of a socket.  If present,
901 * m0 must include a packet header with total length.
902 * Returns 0 if no space in sockbuf or insufficient mbufs.
903 */
904int
905sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0,
906	struct mbuf *control)
907{
908	struct mbuf	*m, *n, *nlast;
909	int		space, len;
910
911	KASSERT(solocked(sb->sb_so));
912
913	space = asa->sa_len;
914
915	if (m0 != NULL) {
916		if ((m0->m_flags & M_PKTHDR) == 0)
917			panic("sbappendaddr");
918		space += m0->m_pkthdr.len;
919#ifdef MBUFTRACE
920		m_claimm(m0, sb->sb_mowner);
921#endif
922	}
923	for (n = control; n; n = n->m_next) {
924		space += n->m_len;
925		MCLAIM(n, sb->sb_mowner);
926		if (n->m_next == 0)	/* keep pointer to last control buf */
927			break;
928	}
929	if (space > sbspace(sb))
930		return (0);
931	MGET(m, M_DONTWAIT, MT_SONAME);
932	if (m == 0)
933		return (0);
934	MCLAIM(m, sb->sb_mowner);
935	/*
936	 * XXX avoid 'comparison always true' warning which isn't easily
937	 * avoided.
938	 */
939	len = asa->sa_len;
940	if (len > MLEN) {
941		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
942		if ((m->m_flags & M_EXT) == 0) {
943			m_free(m);
944			return (0);
945		}
946	}
947	m->m_len = asa->sa_len;
948	memcpy(mtod(m, void *), asa, asa->sa_len);
949	if (n)
950		n->m_next = m0;		/* concatenate data to control */
951	else
952		control = m0;
953	m->m_next = control;
954
955	SBLASTRECORDCHK(sb, "sbappendaddr 1");
956
957	for (n = m; n->m_next != NULL; n = n->m_next)
958		sballoc(sb, n);
959	sballoc(sb, n);
960	nlast = n;
961	SBLINKRECORD(sb, m);
962
963	sb->sb_mbtail = nlast;
964	SBLASTMBUFCHK(sb, "sbappendaddr");
965	SBLASTRECORDCHK(sb, "sbappendaddr 2");
966
967	return (1);
968}
969
970/*
971 * Helper for sbappendchainaddr: prepend a struct sockaddr* to
972 * an mbuf chain.
973 */
974static inline struct mbuf *
975m_prepend_sockaddr(struct sockbuf *sb, struct mbuf *m0,
976		   const struct sockaddr *asa)
977{
978	struct mbuf *m;
979	const int salen = asa->sa_len;
980
981	KASSERT(solocked(sb->sb_so));
982
983	/* only the first in each chain need be a pkthdr */
984	MGETHDR(m, M_DONTWAIT, MT_SONAME);
985	if (m == 0)
986		return (0);
987	MCLAIM(m, sb->sb_mowner);
988#ifdef notyet
989	if (salen > MHLEN) {
990		MEXTMALLOC(m, salen, M_NOWAIT);
991		if ((m->m_flags & M_EXT) == 0) {
992			m_free(m);
993			return (0);
994		}
995	}
996#else
997	KASSERT(salen <= MHLEN);
998#endif
999	m->m_len = salen;
1000	memcpy(mtod(m, void *), asa, salen);
1001	m->m_next = m0;
1002	m->m_pkthdr.len = salen + m0->m_pkthdr.len;
1003
1004	return m;
1005}
1006
1007int
1008sbappendaddrchain(struct sockbuf *sb, const struct sockaddr *asa,
1009		  struct mbuf *m0, int sbprio)
1010{
1011	int space;
1012	struct mbuf *m, *n, *n0, *nlast;
1013	int error;
1014
1015	KASSERT(solocked(sb->sb_so));
1016
1017	/*
1018	 * XXX sbprio reserved for encoding priority of this* request:
1019	 *  SB_PRIO_NONE --> honour normal sb limits
1020	 *  SB_PRIO_ONESHOT_OVERFLOW --> if socket has any space,
1021	 *	take whole chain. Intended for large requests
1022	 *      that should be delivered atomically (all, or none).
1023	 * SB_PRIO_OVERDRAFT -- allow a small (2*MLEN) overflow
1024	 *       over normal socket limits, for messages indicating
1025	 *       buffer overflow in earlier normal/lower-priority messages
1026	 * SB_PRIO_BESTEFFORT -->  ignore limits entirely.
1027	 *       Intended for  kernel-generated messages only.
1028	 *        Up to generator to avoid total mbuf resource exhaustion.
1029	 */
1030	(void)sbprio;
1031
1032	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1033		panic("sbappendaddrchain");
1034
1035	space = sbspace(sb);
1036
1037#ifdef notyet
1038	/*
1039	 * Enforce SB_PRIO_* limits as described above.
1040	 */
1041#endif
1042
1043	n0 = NULL;
1044	nlast = NULL;
1045	for (m = m0; m; m = m->m_nextpkt) {
1046		struct mbuf *np;
1047
1048#ifdef MBUFTRACE
1049		m_claimm(m, sb->sb_mowner);
1050#endif
1051
1052		/* Prepend sockaddr to this record (m) of input chain m0 */
1053	  	n = m_prepend_sockaddr(sb, m, asa);
1054		if (n == NULL) {
1055			error = ENOBUFS;
1056			goto bad;
1057		}
1058
1059		/* Append record (asa+m) to end of new chain n0 */
1060		if (n0 == NULL) {
1061			n0 = n;
1062		} else {
1063			nlast->m_nextpkt = n;
1064		}
1065		/* Keep track of last record on new chain */
1066		nlast = n;
1067
1068		for (np = n; np; np = np->m_next)
1069			sballoc(sb, np);
1070	}
1071
1072	SBLASTRECORDCHK(sb, "sbappendaddrchain 1");
1073
1074	/* Drop the entire chain of (asa+m) records onto the socket */
1075	SBLINKRECORDCHAIN(sb, n0, nlast);
1076
1077	SBLASTRECORDCHK(sb, "sbappendaddrchain 2");
1078
1079	for (m = nlast; m->m_next; m = m->m_next)
1080		;
1081	sb->sb_mbtail = m;
1082	SBLASTMBUFCHK(sb, "sbappendaddrchain");
1083
1084	return (1);
1085
1086bad:
1087	/*
1088	 * On error, free the prepended addreseses. For consistency
1089	 * with sbappendaddr(), leave it to our caller to free
1090	 * the input record chain passed to us as m0.
1091	 */
1092	while ((n = n0) != NULL) {
1093	  	struct mbuf *np;
1094
1095		/* Undo the sballoc() of this record */
1096		for (np = n; np; np = np->m_next)
1097			sbfree(sb, np);
1098
1099		n0 = n->m_nextpkt;	/* iterate at next prepended address */
1100		MFREE(n, np);		/* free prepended address (not data) */
1101	}
1102	return 0;
1103}
1104
1105
1106int
1107sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
1108{
1109	struct mbuf	*m, *mlast, *n;
1110	int		space;
1111
1112	KASSERT(solocked(sb->sb_so));
1113
1114	space = 0;
1115	if (control == 0)
1116		panic("sbappendcontrol");
1117	for (m = control; ; m = m->m_next) {
1118		space += m->m_len;
1119		MCLAIM(m, sb->sb_mowner);
1120		if (m->m_next == 0)
1121			break;
1122	}
1123	n = m;			/* save pointer to last control buffer */
1124	for (m = m0; m; m = m->m_next) {
1125		MCLAIM(m, sb->sb_mowner);
1126		space += m->m_len;
1127	}
1128	if (space > sbspace(sb))
1129		return (0);
1130	n->m_next = m0;			/* concatenate data to control */
1131
1132	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
1133
1134	for (m = control; m->m_next != NULL; m = m->m_next)
1135		sballoc(sb, m);
1136	sballoc(sb, m);
1137	mlast = m;
1138	SBLINKRECORD(sb, control);
1139
1140	sb->sb_mbtail = mlast;
1141	SBLASTMBUFCHK(sb, "sbappendcontrol");
1142	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
1143
1144	return (1);
1145}
1146
1147/*
1148 * Compress mbuf chain m into the socket
1149 * buffer sb following mbuf n.  If n
1150 * is null, the buffer is presumed empty.
1151 */
1152void
1153sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1154{
1155	int		eor;
1156	struct mbuf	*o;
1157
1158	KASSERT(solocked(sb->sb_so));
1159
1160	eor = 0;
1161	while (m) {
1162		eor |= m->m_flags & M_EOR;
1163		if (m->m_len == 0 &&
1164		    (eor == 0 ||
1165		     (((o = m->m_next) || (o = n)) &&
1166		      o->m_type == m->m_type))) {
1167			if (sb->sb_lastrecord == m)
1168				sb->sb_lastrecord = m->m_next;
1169			m = m_free(m);
1170			continue;
1171		}
1172		if (n && (n->m_flags & M_EOR) == 0 &&
1173		    /* M_TRAILINGSPACE() checks buffer writeability */
1174		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
1175		    m->m_len <= M_TRAILINGSPACE(n) &&
1176		    n->m_type == m->m_type) {
1177			memcpy(mtod(n, char *) + n->m_len, mtod(m, void *),
1178			    (unsigned)m->m_len);
1179			n->m_len += m->m_len;
1180			sb->sb_cc += m->m_len;
1181			m = m_free(m);
1182			continue;
1183		}
1184		if (n)
1185			n->m_next = m;
1186		else
1187			sb->sb_mb = m;
1188		sb->sb_mbtail = m;
1189		sballoc(sb, m);
1190		n = m;
1191		m->m_flags &= ~M_EOR;
1192		m = m->m_next;
1193		n->m_next = 0;
1194	}
1195	if (eor) {
1196		if (n)
1197			n->m_flags |= eor;
1198		else
1199			printf("semi-panic: sbcompress\n");
1200	}
1201	SBLASTMBUFCHK(sb, __func__);
1202}
1203
1204/*
1205 * Free all mbufs in a sockbuf.
1206 * Check that all resources are reclaimed.
1207 */
1208void
1209sbflush(struct sockbuf *sb)
1210{
1211
1212	KASSERT(solocked(sb->sb_so));
1213	KASSERT((sb->sb_flags & SB_LOCK) == 0);
1214
1215	while (sb->sb_mbcnt)
1216		sbdrop(sb, (int)sb->sb_cc);
1217
1218	KASSERT(sb->sb_cc == 0);
1219	KASSERT(sb->sb_mb == NULL);
1220	KASSERT(sb->sb_mbtail == NULL);
1221	KASSERT(sb->sb_lastrecord == NULL);
1222}
1223
1224/*
1225 * Drop data from (the front of) a sockbuf.
1226 */
1227void
1228sbdrop(struct sockbuf *sb, int len)
1229{
1230	struct mbuf	*m, *mn, *next;
1231
1232	KASSERT(solocked(sb->sb_so));
1233
1234	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1235	while (len > 0) {
1236		if (m == 0) {
1237			if (next == 0)
1238				panic("sbdrop");
1239			m = next;
1240			next = m->m_nextpkt;
1241			continue;
1242		}
1243		if (m->m_len > len) {
1244			m->m_len -= len;
1245			m->m_data += len;
1246			sb->sb_cc -= len;
1247			break;
1248		}
1249		len -= m->m_len;
1250		sbfree(sb, m);
1251		MFREE(m, mn);
1252		m = mn;
1253	}
1254	while (m && m->m_len == 0) {
1255		sbfree(sb, m);
1256		MFREE(m, mn);
1257		m = mn;
1258	}
1259	if (m) {
1260		sb->sb_mb = m;
1261		m->m_nextpkt = next;
1262	} else
1263		sb->sb_mb = next;
1264	/*
1265	 * First part is an inline SB_EMPTY_FIXUP().  Second part
1266	 * makes sure sb_lastrecord is up-to-date if we dropped
1267	 * part of the last record.
1268	 */
1269	m = sb->sb_mb;
1270	if (m == NULL) {
1271		sb->sb_mbtail = NULL;
1272		sb->sb_lastrecord = NULL;
1273	} else if (m->m_nextpkt == NULL)
1274		sb->sb_lastrecord = m;
1275}
1276
1277/*
1278 * Drop a record off the front of a sockbuf
1279 * and move the next record to the front.
1280 */
1281void
1282sbdroprecord(struct sockbuf *sb)
1283{
1284	struct mbuf	*m, *mn;
1285
1286	KASSERT(solocked(sb->sb_so));
1287
1288	m = sb->sb_mb;
1289	if (m) {
1290		sb->sb_mb = m->m_nextpkt;
1291		do {
1292			sbfree(sb, m);
1293			MFREE(m, mn);
1294		} while ((m = mn) != NULL);
1295	}
1296	SB_EMPTY_FIXUP(sb);
1297}
1298
1299/*
1300 * Create a "control" mbuf containing the specified data
1301 * with the specified type for presentation on a socket buffer.
1302 */
1303struct mbuf *
1304sbcreatecontrol(void *p, int size, int type, int level)
1305{
1306	struct cmsghdr	*cp;
1307	struct mbuf	*m;
1308
1309	if (CMSG_SPACE(size) > MCLBYTES) {
1310		printf("sbcreatecontrol: message too large %d\n", size);
1311		return NULL;
1312	}
1313
1314	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
1315		return (NULL);
1316	if (CMSG_SPACE(size) > MLEN) {
1317		MCLGET(m, M_DONTWAIT);
1318		if ((m->m_flags & M_EXT) == 0) {
1319			m_free(m);
1320			return NULL;
1321		}
1322	}
1323	cp = mtod(m, struct cmsghdr *);
1324	memcpy(CMSG_DATA(cp), p, size);
1325	m->m_len = CMSG_SPACE(size);
1326	cp->cmsg_len = CMSG_LEN(size);
1327	cp->cmsg_level = level;
1328	cp->cmsg_type = type;
1329	return (m);
1330}
1331
1332void
1333solockretry(struct socket *so, kmutex_t *lock)
1334{
1335
1336	while (lock != so->so_lock) {
1337		mutex_exit(lock);
1338		lock = so->so_lock;
1339		mutex_enter(lock);
1340	}
1341}
1342
1343bool
1344solocked(struct socket *so)
1345{
1346
1347	return mutex_owned(so->so_lock);
1348}
1349
1350bool
1351solocked2(struct socket *so1, struct socket *so2)
1352{
1353	kmutex_t *lock;
1354
1355	lock = so1->so_lock;
1356	if (lock != so2->so_lock)
1357		return false;
1358	return mutex_owned(lock);
1359}
1360
1361/*
1362 * Assign a default lock to a new socket.  For PRU_ATTACH, and done by
1363 * protocols that do not have special locking requirements.
1364 */
1365void
1366sosetlock(struct socket *so)
1367{
1368	kmutex_t *lock;
1369
1370	if (so->so_lock == NULL) {
1371		lock = softnet_lock;
1372		so->so_lock = lock;
1373		mutex_obj_hold(lock);
1374		mutex_enter(lock);
1375	}
1376
1377	/* In all cases, lock must be held on return from PRU_ATTACH. */
1378	KASSERT(solocked(so));
1379}
1380
1381/*
1382 * Set lock on sockbuf sb; sleep if lock is already held.
1383 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1384 * Returns error without lock if sleep is interrupted.
1385 */
1386int
1387sblock(struct sockbuf *sb, int wf)
1388{
1389	struct socket *so;
1390	kmutex_t *lock;
1391	int error;
1392
1393	KASSERT(solocked(sb->sb_so));
1394
1395	for (;;) {
1396		if (__predict_true((sb->sb_flags & SB_LOCK) == 0)) {
1397			sb->sb_flags |= SB_LOCK;
1398			return 0;
1399		}
1400		if (wf != M_WAITOK)
1401			return EWOULDBLOCK;
1402		so = sb->sb_so;
1403		lock = so->so_lock;
1404		if ((sb->sb_flags & SB_NOINTR) != 0) {
1405			cv_wait(&so->so_cv, lock);
1406			error = 0;
1407		} else
1408			error = cv_wait_sig(&so->so_cv, lock);
1409		if (__predict_false(lock != so->so_lock))
1410			solockretry(so, lock);
1411		if (error != 0)
1412			return error;
1413	}
1414}
1415
1416void
1417sbunlock(struct sockbuf *sb)
1418{
1419	struct socket *so;
1420
1421	so = sb->sb_so;
1422
1423	KASSERT(solocked(so));
1424	KASSERT((sb->sb_flags & SB_LOCK) != 0);
1425
1426	sb->sb_flags &= ~SB_LOCK;
1427	cv_broadcast(&so->so_cv);
1428}
1429
1430int
1431sowait(struct socket *so, bool catch, int timo)
1432{
1433	kmutex_t *lock;
1434	int error;
1435
1436	KASSERT(solocked(so));
1437	KASSERT(catch || timo != 0);
1438
1439	lock = so->so_lock;
1440	if (catch)
1441		error = cv_timedwait_sig(&so->so_cv, lock, timo);
1442	else
1443		error = cv_timedwait(&so->so_cv, lock, timo);
1444	if (__predict_false(lock != so->so_lock))
1445		solockretry(so, lock);
1446	return error;
1447}
1448