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