1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004 The FreeBSD Foundation
5 * Copyright (c) 2004-2008 Robert N. M. Watson
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
33 */
34
35 /*
36 * Comments on the socket life cycle:
37 *
38 * soalloc() sets of socket layer state for a socket, called only by
39 * socreate() and sonewconn(). Socket layer private.
40 *
41 * sodealloc() tears down socket layer state for a socket, called only by
42 * sofree() and sonewconn(). Socket layer private.
43 *
44 * pru_attach() associates protocol layer state with an allocated socket;
45 * called only once, may fail, aborting socket allocation. This is called
46 * from socreate() and sonewconn(). Socket layer private.
47 *
48 * pru_detach() disassociates protocol layer state from an attached socket,
49 * and will be called exactly once for sockets in which pru_attach() has
50 * been successfully called. If pru_attach() returned an error,
51 * pru_detach() will not be called. Socket layer private.
52 *
53 * pru_abort() and pru_close() notify the protocol layer that the last
54 * consumer of a socket is starting to tear down the socket, and that the
55 * protocol should terminate the connection. Historically, pru_abort() also
56 * detached protocol state from the socket state, but this is no longer the
57 * case.
58 *
59 * socreate() creates a socket and attaches protocol state. This is a public
60 * interface that may be used by socket layer consumers to create new
61 * sockets.
62 *
63 * sonewconn() creates a socket and attaches protocol state. This is a
64 * public interface that may be used by protocols to create new sockets when
65 * a new connection is received and will be available for accept() on a
66 * listen socket.
67 *
68 * soclose() destroys a socket after possibly waiting for it to disconnect.
69 * This is a public interface that socket consumers should use to close and
70 * release a socket when done with it.
71 *
72 * soabort() destroys a socket without waiting for it to disconnect (used
73 * only for incoming connections that are already partially or fully
74 * connected). This is used internally by the socket layer when clearing
75 * listen socket queues (due to overflow or close on the listen socket), but
76 * is also a public interface protocols may use to abort connections in
77 * their incomplete listen queues should they no longer be required. Sockets
78 * placed in completed connection listen queues should not be aborted for
79 * reasons described in the comment above the soclose() implementation. This
80 * is not a general purpose close routine, and except in the specific
81 * circumstances described here, should not be used.
82 *
83 * sofree() will free a socket and its protocol state if all references on
84 * the socket have been released, and is the public interface to attempt to
85 * free a socket when a reference is removed. This is a socket layer private
86 * interface.
87 *
88 * NOTE: In addition to socreate() and soclose(), which provide a single
89 * socket reference to the consumer to be managed as required, there are two
90 * calls to explicitly manage socket references, soref(), and sorele().
91 * Currently, these are generally required only when transitioning a socket
92 * from a listen queue to a file descriptor, in order to prevent garbage
93 * collection of the socket at an untimely moment. For a number of reasons,
94 * these interfaces are not preferred, and should be avoided.
95 */
96
97 #include <sys/cdefs.h>
98 __FBSDID("$FreeBSD$");
99
100 #include "opt_inet.h"
101 #include "opt_inet6.h"
102 #include "opt_mac.h"
103 #include "opt_zero.h"
104 #include "opt_compat.h"
105
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/fcntl.h>
109 #include <sys/limits.h>
110 #include <sys/lock.h>
111 #include <sys/mac.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/mutex.h>
115 #include <sys/domain.h>
116 #include <sys/file.h> /* for struct knote */
117 #include <sys/kernel.h>
118 #include <sys/event.h>
119 #include <sys/eventhandler.h>
120 #include <sys/poll.h>
121 #include <sys/proc.h>
122 #include <sys/protosw.h>
123 #include <sys/socket.h>
124 #include <sys/socketvar.h>
125 #include <sys/resourcevar.h>
126 #include <net/route.h>
127 #include <sys/signalvar.h>
128 #include <sys/stat.h>
129 #include <sys/sx.h>
130 #include <sys/sysctl.h>
131 #include <sys/uio.h>
132 #include <sys/jail.h>
133
134 #include <security/mac/mac_framework.h>
135
136 #include <vm/uma.h>
137
138 #ifdef COMPAT_IA32
139 #include <sys/mount.h>
140 #include <compat/freebsd32/freebsd32.h>
141
142 extern struct sysentvec ia32_freebsd_sysvec;
143 #endif
144
145 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
146 int flags);
147
148 static void filt_sordetach(struct knote *kn);
149 static int filt_soread(struct knote *kn, long hint);
150 static void filt_sowdetach(struct knote *kn);
151 static int filt_sowrite(struct knote *kn, long hint);
152 static int filt_solisten(struct knote *kn, long hint);
153
154 static struct filterops solisten_filtops =
155 { 1, NULL, filt_sordetach, filt_solisten };
156 static struct filterops soread_filtops =
157 { 1, NULL, filt_sordetach, filt_soread };
158 static struct filterops sowrite_filtops =
159 { 1, NULL, filt_sowdetach, filt_sowrite };
160
161 uma_zone_t socket_zone;
162 so_gen_t so_gencnt; /* generation count for sockets */
163
164 int maxsockets;
165
166 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
167 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
168
169 static int somaxconn = SOMAXCONN;
170 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
171 /* XXX: we dont have SYSCTL_USHORT */
172 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
173 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
174 "queue size");
175 static int numopensockets;
176 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
177 &numopensockets, 0, "Number of open sockets");
178 #ifdef ZERO_COPY_SOCKETS
179 /* These aren't static because they're used in other files. */
180 int so_zero_copy_send = 1;
181 int so_zero_copy_receive = 1;
182 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
183 "Zero copy controls");
184 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
185 &so_zero_copy_receive, 0, "Enable zero copy receive");
186 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
187 &so_zero_copy_send, 0, "Enable zero copy send");
188 #endif /* ZERO_COPY_SOCKETS */
189
190 /*
191 * accept_mtx locks down per-socket fields relating to accept queues. See
192 * socketvar.h for an annotation of the protected fields of struct socket.
193 */
194 struct mtx accept_mtx;
195 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
196
197 /*
198 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
199 * so_gencnt field.
200 */
201 static struct mtx so_global_mtx;
202 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
203
204 /*
205 * General IPC sysctl name space, used by sockets and a variety of other IPC
206 * types.
207 */
208 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
209
210 /*
211 * Sysctl to get and set the maximum global sockets limit. Notify protocols
212 * of the change so that they can update their dependent limits as required.
213 */
214 static int
215 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
216 {
217 int error, newmaxsockets;
218
219 newmaxsockets = maxsockets;
220 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
221 if (error == 0 && req->newptr) {
222 if (newmaxsockets > maxsockets) {
223 maxsockets = newmaxsockets;
224 if (maxsockets > ((maxfiles / 4) * 3)) {
225 maxfiles = (maxsockets * 5) / 4;
226 maxfilesperproc = (maxfiles * 9) / 10;
227 }
228 EVENTHANDLER_INVOKE(maxsockets_change);
229 } else
230 error = EINVAL;
231 }
232 return (error);
233 }
234
235 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
236 &maxsockets, 0, sysctl_maxsockets, "IU",
237 "Maximum number of sockets avaliable");
238
239 /*
240 * Initialise maxsockets. This SYSINIT must be run after
241 * tunable_mbinit().
242 */
243 static void
244 init_maxsockets(void *ignored)
245 {
246
247 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
248 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
249 }
250 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
251
252 /*
253 * Socket operation routines. These routines are called by the routines in
254 * sys_socket.c or from a system process, and implement the semantics of
255 * socket operations by switching out to the protocol specific routines.
256 */
257
258 /*
259 * Get a socket structure from our zone, and initialize it. Note that it
260 * would probably be better to allocate socket and PCB at the same time, but
261 * I'm not convinced that all the protocols can be easily modified to do
262 * this.
263 *
264 * soalloc() returns a socket with a ref count of 0.
265 */
266 static struct socket *
267 soalloc(void)
268 {
269 struct socket *so;
270
271 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
272 if (so == NULL)
273 return (NULL);
274 #ifdef MAC
275 if (mac_init_socket(so, M_NOWAIT) != 0) {
276 uma_zfree(socket_zone, so);
277 return (NULL);
278 }
279 #endif
280 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
281 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
282 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
283 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
284 TAILQ_INIT(&so->so_aiojobq);
285 mtx_lock(&so_global_mtx);
286 so->so_gencnt = ++so_gencnt;
287 ++numopensockets;
288 mtx_unlock(&so_global_mtx);
289 return (so);
290 }
291
292 /*
293 * Free the storage associated with a socket at the socket layer, tear down
294 * locks, labels, etc. All protocol state is assumed already to have been
295 * torn down (and possibly never set up) by the caller.
296 */
297 static void
298 sodealloc(struct socket *so)
299 {
300
301 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
302 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
303
304 mtx_lock(&so_global_mtx);
305 so->so_gencnt = ++so_gencnt;
306 --numopensockets; /* Could be below, but faster here. */
307 mtx_unlock(&so_global_mtx);
308 if (so->so_rcv.sb_hiwat)
309 (void)chgsbsize(so->so_cred->cr_uidinfo,
310 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
311 if (so->so_snd.sb_hiwat)
312 (void)chgsbsize(so->so_cred->cr_uidinfo,
313 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
314 #ifdef INET
315 /* remove acccept filter if one is present. */
316 if (so->so_accf != NULL)
317 do_setopt_accept_filter(so, NULL);
318 #endif
319 #ifdef MAC
320 mac_destroy_socket(so);
321 #endif
322 crfree(so->so_cred);
323 sx_destroy(&so->so_snd.sb_sx);
324 sx_destroy(&so->so_rcv.sb_sx);
325 SOCKBUF_LOCK_DESTROY(&so->so_snd);
326 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
327 uma_zfree(socket_zone, so);
328 }
329
330 /*
331 * socreate returns a socket with a ref count of 1. The socket should be
332 * closed with soclose().
333 */
334 int
335 socreate(int dom, struct socket **aso, int type, int proto,
336 struct ucred *cred, struct thread *td)
337 {
338 struct protosw *prp;
339 struct socket *so;
340 int error;
341
342 if (proto)
343 prp = pffindproto(dom, proto, type);
344 else
345 prp = pffindtype(dom, type);
346
347 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
348 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
349 return (EPROTONOSUPPORT);
350
351 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
352 return (EPROTONOSUPPORT);
353
354 if (prp->pr_type != type)
355 return (EPROTOTYPE);
356 so = soalloc();
357 if (so == NULL)
358 return (ENOBUFS);
359
360 TAILQ_INIT(&so->so_incomp);
361 TAILQ_INIT(&so->so_comp);
362 so->so_type = type;
363 so->so_cred = crhold(cred);
364 if ((prp->pr_domain->dom_family == PF_INET) ||
365 (prp->pr_domain->dom_family == PF_ROUTE))
366 so->so_fibnum = td->td_proc->p_fibnum;
367 else
368 so->so_fibnum = 0;
369 so->so_proto = prp;
370 #ifdef MAC
371 mac_create_socket(cred, so);
372 #endif
373 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
374 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
375 so->so_count = 1;
376 /*
377 * Auto-sizing of socket buffers is managed by the protocols and
378 * the appropriate flags must be set in the pru_attach function.
379 */
380 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
381 if (error) {
382 KASSERT(so->so_count == 1, ("socreate: so_count %d",
383 so->so_count));
384 so->so_count = 0;
385 sodealloc(so);
386 return (error);
387 }
388 *aso = so;
389 return (0);
390 }
391
392 #ifdef REGRESSION
393 static int regression_sonewconn_earlytest = 1;
394 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
395 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
396 #endif
397
398 /*
399 * When an attempt at a new connection is noted on a socket which accepts
400 * connections, sonewconn is called. If the connection is possible (subject
401 * to space constraints, etc.) then we allocate a new structure, propoerly
402 * linked into the data structure of the original socket, and return this.
403 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
404 *
405 * Note: the ref count on the socket is 0 on return.
406 */
407 struct socket *
408 sonewconn(struct socket *head, int connstatus)
409 {
410 struct socket *so;
411 int over;
412
413 ACCEPT_LOCK();
414 over = (head->so_qlen > 3 * head->so_qlimit / 2);
415 ACCEPT_UNLOCK();
416 #ifdef REGRESSION
417 if (regression_sonewconn_earlytest && over)
418 #else
419 if (over)
420 #endif
421 return (NULL);
422 so = soalloc();
423 if (so == NULL)
424 return (NULL);
425 if ((head->so_options & SO_ACCEPTFILTER) != 0)
426 connstatus = 0;
427 so->so_head = head;
428 so->so_type = head->so_type;
429 so->so_options = head->so_options &~ SO_ACCEPTCONN;
430 so->so_linger = head->so_linger;
431 so->so_state = head->so_state | SS_NOFDREF;
432 so->so_fibnum = head->so_fibnum;
433 so->so_proto = head->so_proto;
434 so->so_cred = crhold(head->so_cred);
435 #ifdef MAC
436 SOCK_LOCK(head);
437 mac_create_socket_from_socket(head, so);
438 SOCK_UNLOCK(head);
439 #endif
440 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
441 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
442 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
443 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
444 sodealloc(so);
445 return (NULL);
446 }
447 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
448 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
449 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
450 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
451 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
452 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
453 so->so_state |= connstatus;
454 ACCEPT_LOCK();
455 if (connstatus) {
456 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
457 so->so_qstate |= SQ_COMP;
458 head->so_qlen++;
459 } else {
460 /*
461 * Keep removing sockets from the head until there's room for
462 * us to insert on the tail. In pre-locking revisions, this
463 * was a simple if(), but as we could be racing with other
464 * threads and soabort() requires dropping locks, we must
465 * loop waiting for the condition to be true.
466 */
467 while (head->so_incqlen > head->so_qlimit) {
468 struct socket *sp;
469 sp = TAILQ_FIRST(&head->so_incomp);
470 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
471 head->so_incqlen--;
472 sp->so_qstate &= ~SQ_INCOMP;
473 sp->so_head = NULL;
474 ACCEPT_UNLOCK();
475 soabort(sp);
476 ACCEPT_LOCK();
477 }
478 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
479 so->so_qstate |= SQ_INCOMP;
480 head->so_incqlen++;
481 }
482 ACCEPT_UNLOCK();
483 if (connstatus) {
484 sorwakeup(head);
485 wakeup_one(&head->so_timeo);
486 }
487 return (so);
488 }
489
490 int
491 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
492 {
493
494 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
495 }
496
497 /*
498 * solisten() transitions a socket from a non-listening state to a listening
499 * state, but can also be used to update the listen queue depth on an
500 * existing listen socket. The protocol will call back into the sockets
501 * layer using solisten_proto_check() and solisten_proto() to check and set
502 * socket-layer listen state. Call backs are used so that the protocol can
503 * acquire both protocol and socket layer locks in whatever order is required
504 * by the protocol.
505 *
506 * Protocol implementors are advised to hold the socket lock across the
507 * socket-layer test and set to avoid races at the socket layer.
508 */
509 int
510 solisten(struct socket *so, int backlog, struct thread *td)
511 {
512
513 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td));
514 }
515
516 int
517 solisten_proto_check(struct socket *so)
518 {
519
520 SOCK_LOCK_ASSERT(so);
521
522 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
523 SS_ISDISCONNECTING))
524 return (EINVAL);
525 return (0);
526 }
527
528 void
529 solisten_proto(struct socket *so, int backlog)
530 {
531
532 SOCK_LOCK_ASSERT(so);
533
534 if (backlog < 0 || backlog > somaxconn)
535 backlog = somaxconn;
536 so->so_qlimit = backlog;
537 so->so_options |= SO_ACCEPTCONN;
538 }
539
540 /*
541 * Attempt to free a socket. This should really be sotryfree().
542 *
543 * sofree() will succeed if:
544 *
545 * - There are no outstanding file descriptor references or related consumers
546 * (so_count == 0).
547 *
548 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
549 *
550 * - The protocol does not have an outstanding strong reference on the socket
551 * (SS_PROTOREF).
552 *
553 * - The socket is not in a completed connection queue, so a process has been
554 * notified that it is present. If it is removed, the user process may
555 * block in accept() despite select() saying the socket was ready.
556 *
557 * Otherwise, it will quietly abort so that a future call to sofree(), when
558 * conditions are right, can succeed.
559 */
560 void
561 sofree(struct socket *so)
562 {
563 struct protosw *pr = so->so_proto;
564 struct socket *head;
565
566 ACCEPT_LOCK_ASSERT();
567 SOCK_LOCK_ASSERT(so);
568
569 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
570 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
571 SOCK_UNLOCK(so);
572 ACCEPT_UNLOCK();
573 return;
574 }
575
576 head = so->so_head;
577 if (head != NULL) {
578 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
579 (so->so_qstate & SQ_INCOMP) != 0,
580 ("sofree: so_head != NULL, but neither SQ_COMP nor "
581 "SQ_INCOMP"));
582 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
583 (so->so_qstate & SQ_INCOMP) == 0,
584 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
585 TAILQ_REMOVE(&head->so_incomp, so, so_list);
586 head->so_incqlen--;
587 so->so_qstate &= ~SQ_INCOMP;
588 so->so_head = NULL;
589 }
590 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
591 (so->so_qstate & SQ_INCOMP) == 0,
592 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
593 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
594 if (so->so_options & SO_ACCEPTCONN) {
595 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
596 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
597 }
598 SOCK_UNLOCK(so);
599 ACCEPT_UNLOCK();
600
601 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
602 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
603 if (pr->pr_usrreqs->pru_detach != NULL)
604 (*pr->pr_usrreqs->pru_detach)(so);
605
606 /*
607 * From this point on, we assume that no other references to this
608 * socket exist anywhere else in the stack. Therefore, no locks need
609 * to be acquired or held.
610 *
611 * We used to do a lot of socket buffer and socket locking here, as
612 * well as invoke sorflush() and perform wakeups. The direct call to
613 * dom_dispose() and sbrelease_internal() are an inlining of what was
614 * necessary from sorflush().
615 *
616 * Notice that the socket buffer and kqueue state are torn down
617 * before calling pru_detach. This means that protocols shold not
618 * assume they can perform socket wakeups, etc, in their detach code.
619 */
620 sbdestroy(&so->so_snd, so);
621 sbdestroy(&so->so_rcv, so);
622 knlist_destroy(&so->so_rcv.sb_sel.si_note);
623 knlist_destroy(&so->so_snd.sb_sel.si_note);
624 sodealloc(so);
625 }
626
627 /*
628 * Close a socket on last file table reference removal. Initiate disconnect
629 * if connected. Free socket when disconnect complete.
630 *
631 * This function will sorele() the socket. Note that soclose() may be called
632 * prior to the ref count reaching zero. The actual socket structure will
633 * not be freed until the ref count reaches zero.
634 */
635 int
636 soclose(struct socket *so)
637 {
638 int error = 0;
639
640 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
641
642 funsetown(&so->so_sigio);
643 if (so->so_state & SS_ISCONNECTED) {
644 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
645 error = sodisconnect(so);
646 if (error)
647 goto drop;
648 }
649 if (so->so_options & SO_LINGER) {
650 if ((so->so_state & SS_ISDISCONNECTING) &&
651 (so->so_state & SS_NBIO))
652 goto drop;
653 while (so->so_state & SS_ISCONNECTED) {
654 error = tsleep(&so->so_timeo,
655 PSOCK | PCATCH, "soclos", so->so_linger * hz);
656 if (error)
657 break;
658 }
659 }
660 }
661
662 drop:
663 if (so->so_proto->pr_usrreqs->pru_close != NULL)
664 (*so->so_proto->pr_usrreqs->pru_close)(so);
665 if (so->so_options & SO_ACCEPTCONN) {
666 struct socket *sp;
667 ACCEPT_LOCK();
668 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
669 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
670 so->so_incqlen--;
671 sp->so_qstate &= ~SQ_INCOMP;
672 sp->so_head = NULL;
673 ACCEPT_UNLOCK();
674 soabort(sp);
675 ACCEPT_LOCK();
676 }
677 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
678 TAILQ_REMOVE(&so->so_comp, sp, so_list);
679 so->so_qlen--;
680 sp->so_qstate &= ~SQ_COMP;
681 sp->so_head = NULL;
682 ACCEPT_UNLOCK();
683 soabort(sp);
684 ACCEPT_LOCK();
685 }
686 ACCEPT_UNLOCK();
687 }
688 ACCEPT_LOCK();
689 SOCK_LOCK(so);
690 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
691 so->so_state |= SS_NOFDREF;
692 sorele(so);
693 return (error);
694 }
695
696 /*
697 * soabort() is used to abruptly tear down a connection, such as when a
698 * resource limit is reached (listen queue depth exceeded), or if a listen
699 * socket is closed while there are sockets waiting to be accepted.
700 *
701 * This interface is tricky, because it is called on an unreferenced socket,
702 * and must be called only by a thread that has actually removed the socket
703 * from the listen queue it was on, or races with other threads are risked.
704 *
705 * This interface will call into the protocol code, so must not be called
706 * with any socket locks held. Protocols do call it while holding their own
707 * recursible protocol mutexes, but this is something that should be subject
708 * to review in the future.
709 */
710 void
711 soabort(struct socket *so)
712 {
713
714 /*
715 * In as much as is possible, assert that no references to this
716 * socket are held. This is not quite the same as asserting that the
717 * current thread is responsible for arranging for no references, but
718 * is as close as we can get for now.
719 */
720 KASSERT(so->so_count == 0, ("soabort: so_count"));
721 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
722 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
723 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
724 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
725
726 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
727 (*so->so_proto->pr_usrreqs->pru_abort)(so);
728 ACCEPT_LOCK();
729 SOCK_LOCK(so);
730 sofree(so);
731 }
732
733 int
734 soaccept(struct socket *so, struct sockaddr **nam)
735 {
736 int error;
737
738 SOCK_LOCK(so);
739 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
740 so->so_state &= ~SS_NOFDREF;
741 SOCK_UNLOCK(so);
742 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
743 return (error);
744 }
745
746 int
747 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
748 {
749 int error;
750
751 if (so->so_options & SO_ACCEPTCONN)
752 return (EOPNOTSUPP);
753 /*
754 * If protocol is connection-based, can only connect once.
755 * Otherwise, if connected, try to disconnect first. This allows
756 * user to disconnect by connecting to, e.g., a null address.
757 */
758 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
759 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
760 (error = sodisconnect(so)))) {
761 error = EISCONN;
762 } else {
763 /*
764 * Prevent accumulated error from previous connection from
765 * biting us.
766 */
767 so->so_error = 0;
768 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
769 }
770
771 return (error);
772 }
773
774 int
775 soconnect2(struct socket *so1, struct socket *so2)
776 {
777
778 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
779 }
780
781 int
782 sodisconnect(struct socket *so)
783 {
784 int error;
785
786 if ((so->so_state & SS_ISCONNECTED) == 0)
787 return (ENOTCONN);
788 if (so->so_state & SS_ISDISCONNECTING)
789 return (EALREADY);
790 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
791 return (error);
792 }
793
794 #ifdef ZERO_COPY_SOCKETS
795 struct so_zerocopy_stats{
796 int size_ok;
797 int align_ok;
798 int found_ifp;
799 };
800 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
801 #include <netinet/in.h>
802 #include <net/route.h>
803 #include <netinet/in_pcb.h>
804 #include <vm/vm.h>
805 #include <vm/vm_page.h>
806 #include <vm/vm_object.h>
807
808 /*
809 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
810 * sosend_dgram() and sosend_generic() use m_uiotombuf().
811 *
812 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
813 * all of the data referenced by the uio. If desired, it uses zero-copy.
814 * *space will be updated to reflect data copied in.
815 *
816 * NB: If atomic I/O is requested, the caller must already have checked that
817 * space can hold resid bytes.
818 *
819 * NB: In the event of an error, the caller may need to free the partial
820 * chain pointed to by *mpp. The contents of both *uio and *space may be
821 * modified even in the case of an error.
822 */
823 static int
824 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
825 int flags)
826 {
827 struct mbuf *m, **mp, *top;
828 long len, resid;
829 int error;
830 #ifdef ZERO_COPY_SOCKETS
831 int cow_send;
832 #endif
833
834 *retmp = top = NULL;
835 mp = ⊤
836 len = 0;
837 resid = uio->uio_resid;
838 error = 0;
839 do {
840 #ifdef ZERO_COPY_SOCKETS
841 cow_send = 0;
842 #endif /* ZERO_COPY_SOCKETS */
843 if (resid >= MINCLSIZE) {
844 #ifdef ZERO_COPY_SOCKETS
845 if (top == NULL) {
846 m = m_gethdr(M_WAITOK, MT_DATA);
847 m->m_pkthdr.len = 0;
848 m->m_pkthdr.rcvif = NULL;
849 } else
850 m = m_get(M_WAITOK, MT_DATA);
851 if (so_zero_copy_send &&
852 resid>=PAGE_SIZE &&
853 *space>=PAGE_SIZE &&
854 uio->uio_iov->iov_len>=PAGE_SIZE) {
855 so_zerocp_stats.size_ok++;
856 so_zerocp_stats.align_ok++;
857 cow_send = socow_setup(m, uio);
858 len = cow_send;
859 }
860 if (!cow_send) {
861 m_clget(m, M_WAITOK);
862 len = min(min(MCLBYTES, resid), *space);
863 }
864 #else /* ZERO_COPY_SOCKETS */
865 if (top == NULL) {
866 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
867 m->m_pkthdr.len = 0;
868 m->m_pkthdr.rcvif = NULL;
869 } else
870 m = m_getcl(M_TRYWAIT, MT_DATA, 0);
871 len = min(min(MCLBYTES, resid), *space);
872 #endif /* ZERO_COPY_SOCKETS */
873 } else {
874 if (top == NULL) {
875 m = m_gethdr(M_TRYWAIT, MT_DATA);
876 m->m_pkthdr.len = 0;
877 m->m_pkthdr.rcvif = NULL;
878
879 len = min(min(MHLEN, resid), *space);
880 /*
881 * For datagram protocols, leave room
882 * for protocol headers in first mbuf.
883 */
884 if (atomic && m && len < MHLEN)
885 MH_ALIGN(m, len);
886 } else {
887 m = m_get(M_TRYWAIT, MT_DATA);
888 len = min(min(MLEN, resid), *space);
889 }
890 }
891 if (m == NULL) {
892 error = ENOBUFS;
893 goto out;
894 }
895
896 *space -= len;
897 #ifdef ZERO_COPY_SOCKETS
898 if (cow_send)
899 error = 0;
900 else
901 #endif /* ZERO_COPY_SOCKETS */
902 error = uiomove(mtod(m, void *), (int)len, uio);
903 resid = uio->uio_resid;
904 m->m_len = len;
905 *mp = m;
906 top->m_pkthdr.len += len;
907 if (error)
908 goto out;
909 mp = &m->m_next;
910 if (resid <= 0) {
911 if (flags & MSG_EOR)
912 top->m_flags |= M_EOR;
913 break;
914 }
915 } while (*space > 0 && atomic);
916 out:
917 *retmp = top;
918 return (error);
919 }
920 #endif /*ZERO_COPY_SOCKETS*/
921
922 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
923
924 int
925 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
926 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
927 {
928 long space, resid;
929 int clen = 0, error, dontroute;
930 #ifdef ZERO_COPY_SOCKETS
931 int atomic = sosendallatonce(so) || top;
932 #endif
933
934 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
935 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
936 ("sodgram_send: !PR_ATOMIC"));
937
938 if (uio != NULL)
939 resid = uio->uio_resid;
940 else
941 resid = top->m_pkthdr.len;
942 /*
943 * In theory resid should be unsigned. However, space must be
944 * signed, as it might be less than 0 if we over-committed, and we
945 * must use a signed comparison of space and resid. On the other
946 * hand, a negative resid causes us to loop sending 0-length
947 * segments to the protocol.
948 *
949 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
950 * type sockets since that's an error.
951 */
952 if (resid < 0) {
953 error = EINVAL;
954 goto out;
955 }
956
957 dontroute =
958 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
959 if (td != NULL)
960 td->td_ru.ru_msgsnd++;
961 if (control != NULL)
962 clen = control->m_len;
963
964 SOCKBUF_LOCK(&so->so_snd);
965 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
966 SOCKBUF_UNLOCK(&so->so_snd);
967 error = EPIPE;
968 goto out;
969 }
970 if (so->so_error) {
971 error = so->so_error;
972 so->so_error = 0;
973 SOCKBUF_UNLOCK(&so->so_snd);
974 goto out;
975 }
976 if ((so->so_state & SS_ISCONNECTED) == 0) {
977 /*
978 * `sendto' and `sendmsg' is allowed on a connection-based
979 * socket if it supports implied connect. Return ENOTCONN if
980 * not connected and no address is supplied.
981 */
982 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
983 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
984 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
985 !(resid == 0 && clen != 0)) {
986 SOCKBUF_UNLOCK(&so->so_snd);
987 error = ENOTCONN;
988 goto out;
989 }
990 } else if (addr == NULL) {
991 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
992 error = ENOTCONN;
993 else
994 error = EDESTADDRREQ;
995 SOCKBUF_UNLOCK(&so->so_snd);
996 goto out;
997 }
998 }
999
1000 /*
1001 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1002 * problem and need fixing.
1003 */
1004 space = sbspace(&so->so_snd);
1005 if (flags & MSG_OOB)
1006 space += 1024;
1007 space -= clen;
1008 SOCKBUF_UNLOCK(&so->so_snd);
1009 if (resid > space) {
1010 error = EMSGSIZE;
1011 goto out;
1012 }
1013 if (uio == NULL) {
1014 resid = 0;
1015 if (flags & MSG_EOR)
1016 top->m_flags |= M_EOR;
1017 } else {
1018 #ifdef ZERO_COPY_SOCKETS
1019 error = sosend_copyin(uio, &top, atomic, &space, flags);
1020 if (error)
1021 goto out;
1022 #else
1023 /*
1024 * Copy the data from userland into a mbuf chain.
1025 * If no data is to be copied in, a single empty mbuf
1026 * is returned.
1027 */
1028 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1029 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1030 if (top == NULL) {
1031 error = EFAULT; /* only possible error */
1032 goto out;
1033 }
1034 space -= resid - uio->uio_resid;
1035 #endif
1036 resid = uio->uio_resid;
1037 }
1038 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1039 /*
1040 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1041 * than with.
1042 */
1043 if (dontroute) {
1044 SOCK_LOCK(so);
1045 so->so_options |= SO_DONTROUTE;
1046 SOCK_UNLOCK(so);
1047 }
1048 /*
1049 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1050 * of date. We could have recieved a reset packet in an interrupt or
1051 * maybe we slept while doing page faults in uiomove() etc. We could
1052 * probably recheck again inside the locking protection here, but
1053 * there are probably other places that this also happens. We must
1054 * rethink this.
1055 */
1056 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1057 (flags & MSG_OOB) ? PRUS_OOB :
1058 /*
1059 * If the user set MSG_EOF, the protocol understands this flag and
1060 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1061 */
1062 ((flags & MSG_EOF) &&
1063 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1064 (resid <= 0)) ?
1065 PRUS_EOF :
1066 /* If there is more to send set PRUS_MORETOCOME */
1067 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1068 top, addr, control, td);
1069 if (dontroute) {
1070 SOCK_LOCK(so);
1071 so->so_options &= ~SO_DONTROUTE;
1072 SOCK_UNLOCK(so);
1073 }
1074 clen = 0;
1075 control = NULL;
1076 top = NULL;
1077 out:
1078 if (top != NULL)
1079 m_freem(top);
1080 if (control != NULL)
1081 m_freem(control);
1082 return (error);
1083 }
1084
1085 /*
1086 * Send on a socket. If send must go all at once and message is larger than
1087 * send buffering, then hard error. Lock against other senders. If must go
1088 * all at once and not enough room now, then inform user that this would
1089 * block and do nothing. Otherwise, if nonblocking, send as much as
1090 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1091 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1092 * in mbuf chain must be small enough to send all at once.
1093 *
1094 * Returns nonzero on error, timeout or signal; callers must check for short
1095 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1096 * on return.
1097 */
1098 int
1099 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1100 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1101 {
1102 long space, resid;
1103 int clen = 0, error, dontroute;
1104 int atomic = sosendallatonce(so) || top;
1105
1106 if (uio != NULL)
1107 resid = uio->uio_resid;
1108 else
1109 resid = top->m_pkthdr.len;
1110 /*
1111 * In theory resid should be unsigned. However, space must be
1112 * signed, as it might be less than 0 if we over-committed, and we
1113 * must use a signed comparison of space and resid. On the other
1114 * hand, a negative resid causes us to loop sending 0-length
1115 * segments to the protocol.
1116 *
1117 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1118 * type sockets since that's an error.
1119 */
1120 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1121 error = EINVAL;
1122 goto out;
1123 }
1124
1125 dontroute =
1126 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1127 (so->so_proto->pr_flags & PR_ATOMIC);
1128 if (td != NULL)
1129 td->td_ru.ru_msgsnd++;
1130 if (control != NULL)
1131 clen = control->m_len;
1132
1133 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1134 if (error)
1135 goto out;
1136
1137 restart:
1138 do {
1139 SOCKBUF_LOCK(&so->so_snd);
1140 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1141 SOCKBUF_UNLOCK(&so->so_snd);
1142 error = EPIPE;
1143 goto release;
1144 }
1145 if (so->so_error) {
1146 error = so->so_error;
1147 so->so_error = 0;
1148 SOCKBUF_UNLOCK(&so->so_snd);
1149 goto release;
1150 }
1151 if ((so->so_state & SS_ISCONNECTED) == 0) {
1152 /*
1153 * `sendto' and `sendmsg' is allowed on a connection-
1154 * based socket if it supports implied connect.
1155 * Return ENOTCONN if not connected and no address is
1156 * supplied.
1157 */
1158 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1159 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1160 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1161 !(resid == 0 && clen != 0)) {
1162 SOCKBUF_UNLOCK(&so->so_snd);
1163 error = ENOTCONN;
1164 goto release;
1165 }
1166 } else if (addr == NULL) {
1167 SOCKBUF_UNLOCK(&so->so_snd);
1168 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1169 error = ENOTCONN;
1170 else
1171 error = EDESTADDRREQ;
1172 goto release;
1173 }
1174 }
1175 space = sbspace(&so->so_snd);
1176 if (flags & MSG_OOB)
1177 space += 1024;
1178 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1179 clen > so->so_snd.sb_hiwat) {
1180 SOCKBUF_UNLOCK(&so->so_snd);
1181 error = EMSGSIZE;
1182 goto release;
1183 }
1184 if (space < resid + clen &&
1185 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1186 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1187 SOCKBUF_UNLOCK(&so->so_snd);
1188 error = EWOULDBLOCK;
1189 goto release;
1190 }
1191 error = sbwait(&so->so_snd);
1192 SOCKBUF_UNLOCK(&so->so_snd);
1193 if (error)
1194 goto release;
1195 goto restart;
1196 }
1197 SOCKBUF_UNLOCK(&so->so_snd);
1198 space -= clen;
1199 do {
1200 if (uio == NULL) {
1201 resid = 0;
1202 if (flags & MSG_EOR)
1203 top->m_flags |= M_EOR;
1204 } else {
1205 #ifdef ZERO_COPY_SOCKETS
1206 error = sosend_copyin(uio, &top, atomic,
1207 &space, flags);
1208 if (error != 0)
1209 goto release;
1210 #else
1211 /*
1212 * Copy the data from userland into a mbuf
1213 * chain. If no data is to be copied in,
1214 * a single empty mbuf is returned.
1215 */
1216 top = m_uiotombuf(uio, M_WAITOK, space,
1217 (atomic ? max_hdr : 0),
1218 (atomic ? M_PKTHDR : 0) |
1219 ((flags & MSG_EOR) ? M_EOR : 0));
1220 if (top == NULL) {
1221 error = EFAULT; /* only possible error */
1222 goto release;
1223 }
1224 space -= resid - uio->uio_resid;
1225 #endif
1226 resid = uio->uio_resid;
1227 }
1228 if (dontroute) {
1229 SOCK_LOCK(so);
1230 so->so_options |= SO_DONTROUTE;
1231 SOCK_UNLOCK(so);
1232 }
1233 /*
1234 * XXX all the SBS_CANTSENDMORE checks previously
1235 * done could be out of date. We could have recieved
1236 * a reset packet in an interrupt or maybe we slept
1237 * while doing page faults in uiomove() etc. We
1238 * could probably recheck again inside the locking
1239 * protection here, but there are probably other
1240 * places that this also happens. We must rethink
1241 * this.
1242 */
1243 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1244 (flags & MSG_OOB) ? PRUS_OOB :
1245 /*
1246 * If the user set MSG_EOF, the protocol understands
1247 * this flag and nothing left to send then use
1248 * PRU_SEND_EOF instead of PRU_SEND.
1249 */
1250 ((flags & MSG_EOF) &&
1251 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1252 (resid <= 0)) ?
1253 PRUS_EOF :
1254 /* If there is more to send set PRUS_MORETOCOME. */
1255 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1256 top, addr, control, td);
1257 if (dontroute) {
1258 SOCK_LOCK(so);
1259 so->so_options &= ~SO_DONTROUTE;
1260 SOCK_UNLOCK(so);
1261 }
1262 clen = 0;
1263 control = NULL;
1264 top = NULL;
1265 if (error)
1266 goto release;
1267 } while (resid && space > 0);
1268 } while (resid);
1269
1270 release:
1271 sbunlock(&so->so_snd);
1272 out:
1273 if (top != NULL)
1274 m_freem(top);
1275 if (control != NULL)
1276 m_freem(control);
1277 return (error);
1278 }
1279
1280 int
1281 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1282 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1283 {
1284
1285 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1286 control, flags, td));
1287 }
1288
1289 /*
1290 * The part of soreceive() that implements reading non-inline out-of-band
1291 * data from a socket. For more complete comments, see soreceive(), from
1292 * which this code originated.
1293 *
1294 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1295 * unable to return an mbuf chain to the caller.
1296 */
1297 static int
1298 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1299 {
1300 struct protosw *pr = so->so_proto;
1301 struct mbuf *m;
1302 int error;
1303
1304 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1305
1306 m = m_get(M_TRYWAIT, MT_DATA);
1307 if (m == NULL)
1308 return (ENOBUFS);
1309 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1310 if (error)
1311 goto bad;
1312 do {
1313 #ifdef ZERO_COPY_SOCKETS
1314 if (so_zero_copy_receive) {
1315 int disposable;
1316
1317 if ((m->m_flags & M_EXT)
1318 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1319 disposable = 1;
1320 else
1321 disposable = 0;
1322
1323 error = uiomoveco(mtod(m, void *),
1324 min(uio->uio_resid, m->m_len),
1325 uio, disposable);
1326 } else
1327 #endif /* ZERO_COPY_SOCKETS */
1328 error = uiomove(mtod(m, void *),
1329 (int) min(uio->uio_resid, m->m_len), uio);
1330 m = m_free(m);
1331 } while (uio->uio_resid && error == 0 && m);
1332 bad:
1333 if (m != NULL)
1334 m_freem(m);
1335 return (error);
1336 }
1337
1338 /*
1339 * Following replacement or removal of the first mbuf on the first mbuf chain
1340 * of a socket buffer, push necessary state changes back into the socket
1341 * buffer so that other consumers see the values consistently. 'nextrecord'
1342 * is the callers locally stored value of the original value of
1343 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1344 * NOTE: 'nextrecord' may be NULL.
1345 */
1346 static __inline void
1347 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1348 {
1349
1350 SOCKBUF_LOCK_ASSERT(sb);
1351 /*
1352 * First, update for the new value of nextrecord. If necessary, make
1353 * it the first record.
1354 */
1355 if (sb->sb_mb != NULL)
1356 sb->sb_mb->m_nextpkt = nextrecord;
1357 else
1358 sb->sb_mb = nextrecord;
1359
1360 /*
1361 * Now update any dependent socket buffer fields to reflect the new
1362 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1363 * addition of a second clause that takes care of the case where
1364 * sb_mb has been updated, but remains the last record.
1365 */
1366 if (sb->sb_mb == NULL) {
1367 sb->sb_mbtail = NULL;
1368 sb->sb_lastrecord = NULL;
1369 } else if (sb->sb_mb->m_nextpkt == NULL)
1370 sb->sb_lastrecord = sb->sb_mb;
1371 }
1372
1373
1374 /*
1375 * Implement receive operations on a socket. We depend on the way that
1376 * records are added to the sockbuf by sbappend. In particular, each record
1377 * (mbufs linked through m_next) must begin with an address if the protocol
1378 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1379 * data, and then zero or more mbufs of data. In order to allow parallelism
1380 * between network receive and copying to user space, as well as avoid
1381 * sleeping with a mutex held, we release the socket buffer mutex during the
1382 * user space copy. Although the sockbuf is locked, new data may still be
1383 * appended, and thus we must maintain consistency of the sockbuf during that
1384 * time.
1385 *
1386 * The caller may receive the data as a single mbuf chain by supplying an
1387 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1388 * the count in uio_resid.
1389 */
1390 int
1391 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1392 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1393 {
1394 struct mbuf *m, **mp;
1395 int flags, len, error, offset;
1396 struct protosw *pr = so->so_proto;
1397 struct mbuf *nextrecord;
1398 int moff, type = 0;
1399 int orig_resid = uio->uio_resid;
1400
1401 mp = mp0;
1402 if (psa != NULL)
1403 *psa = NULL;
1404 if (controlp != NULL)
1405 *controlp = NULL;
1406 if (flagsp != NULL)
1407 flags = *flagsp &~ MSG_EOR;
1408 else
1409 flags = 0;
1410 if (flags & MSG_OOB)
1411 return (soreceive_rcvoob(so, uio, flags));
1412 if (mp != NULL)
1413 *mp = NULL;
1414 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1415 && uio->uio_resid)
1416 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1417
1418 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1419 if (error)
1420 return (error);
1421
1422 restart:
1423 SOCKBUF_LOCK(&so->so_rcv);
1424 m = so->so_rcv.sb_mb;
1425 /*
1426 * If we have less data than requested, block awaiting more (subject
1427 * to any timeout) if:
1428 * 1. the current count is less than the low water mark, or
1429 * 2. MSG_WAITALL is set, and it is possible to do the entire
1430 * receive operation at once if we block (resid <= hiwat).
1431 * 3. MSG_DONTWAIT is not set
1432 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1433 * we have to do the receive in sections, and thus risk returning a
1434 * short count if a timeout or signal occurs after we start.
1435 */
1436 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1437 so->so_rcv.sb_cc < uio->uio_resid) &&
1438 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1439 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1440 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1441 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1442 ("receive: m == %p so->so_rcv.sb_cc == %u",
1443 m, so->so_rcv.sb_cc));
1444 if (so->so_error) {
1445 if (m != NULL)
1446 goto dontblock;
1447 error = so->so_error;
1448 if ((flags & MSG_PEEK) == 0)
1449 so->so_error = 0;
1450 SOCKBUF_UNLOCK(&so->so_rcv);
1451 goto release;
1452 }
1453 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1454 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1455 if (m == NULL) {
1456 SOCKBUF_UNLOCK(&so->so_rcv);
1457 goto release;
1458 } else
1459 goto dontblock;
1460 }
1461 for (; m != NULL; m = m->m_next)
1462 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1463 m = so->so_rcv.sb_mb;
1464 goto dontblock;
1465 }
1466 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1467 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1468 SOCKBUF_UNLOCK(&so->so_rcv);
1469 error = ENOTCONN;
1470 goto release;
1471 }
1472 if (uio->uio_resid == 0) {
1473 SOCKBUF_UNLOCK(&so->so_rcv);
1474 goto release;
1475 }
1476 if ((so->so_state & SS_NBIO) ||
1477 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1478 SOCKBUF_UNLOCK(&so->so_rcv);
1479 error = EWOULDBLOCK;
1480 goto release;
1481 }
1482 SBLASTRECORDCHK(&so->so_rcv);
1483 SBLASTMBUFCHK(&so->so_rcv);
1484 error = sbwait(&so->so_rcv);
1485 SOCKBUF_UNLOCK(&so->so_rcv);
1486 if (error)
1487 goto release;
1488 goto restart;
1489 }
1490 dontblock:
1491 /*
1492 * From this point onward, we maintain 'nextrecord' as a cache of the
1493 * pointer to the next record in the socket buffer. We must keep the
1494 * various socket buffer pointers and local stack versions of the
1495 * pointers in sync, pushing out modifications before dropping the
1496 * socket buffer mutex, and re-reading them when picking it up.
1497 *
1498 * Otherwise, we will race with the network stack appending new data
1499 * or records onto the socket buffer by using inconsistent/stale
1500 * versions of the field, possibly resulting in socket buffer
1501 * corruption.
1502 *
1503 * By holding the high-level sblock(), we prevent simultaneous
1504 * readers from pulling off the front of the socket buffer.
1505 */
1506 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1507 if (uio->uio_td)
1508 uio->uio_td->td_ru.ru_msgrcv++;
1509 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1510 SBLASTRECORDCHK(&so->so_rcv);
1511 SBLASTMBUFCHK(&so->so_rcv);
1512 nextrecord = m->m_nextpkt;
1513 if (pr->pr_flags & PR_ADDR) {
1514 KASSERT(m->m_type == MT_SONAME,
1515 ("m->m_type == %d", m->m_type));
1516 orig_resid = 0;
1517 if (psa != NULL)
1518 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1519 M_NOWAIT);
1520 if (flags & MSG_PEEK) {
1521 m = m->m_next;
1522 } else {
1523 sbfree(&so->so_rcv, m);
1524 so->so_rcv.sb_mb = m_free(m);
1525 m = so->so_rcv.sb_mb;
1526 sockbuf_pushsync(&so->so_rcv, nextrecord);
1527 }
1528 }
1529
1530 /*
1531 * Process one or more MT_CONTROL mbufs present before any data mbufs
1532 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1533 * just copy the data; if !MSG_PEEK, we call into the protocol to
1534 * perform externalization (or freeing if controlp == NULL).
1535 */
1536 if (m != NULL && m->m_type == MT_CONTROL) {
1537 struct mbuf *cm = NULL, *cmn;
1538 struct mbuf **cme = &cm;
1539
1540 do {
1541 if (flags & MSG_PEEK) {
1542 if (controlp != NULL) {
1543 *controlp = m_copy(m, 0, m->m_len);
1544 controlp = &(*controlp)->m_next;
1545 }
1546 m = m->m_next;
1547 } else {
1548 sbfree(&so->so_rcv, m);
1549 so->so_rcv.sb_mb = m->m_next;
1550 m->m_next = NULL;
1551 *cme = m;
1552 cme = &(*cme)->m_next;
1553 m = so->so_rcv.sb_mb;
1554 }
1555 } while (m != NULL && m->m_type == MT_CONTROL);
1556 if ((flags & MSG_PEEK) == 0)
1557 sockbuf_pushsync(&so->so_rcv, nextrecord);
1558 while (cm != NULL) {
1559 cmn = cm->m_next;
1560 cm->m_next = NULL;
1561 if (pr->pr_domain->dom_externalize != NULL) {
1562 SOCKBUF_UNLOCK(&so->so_rcv);
1563 error = (*pr->pr_domain->dom_externalize)
1564 (cm, controlp);
1565 SOCKBUF_LOCK(&so->so_rcv);
1566 } else if (controlp != NULL)
1567 *controlp = cm;
1568 else
1569 m_freem(cm);
1570 if (controlp != NULL) {
1571 orig_resid = 0;
1572 while (*controlp != NULL)
1573 controlp = &(*controlp)->m_next;
1574 }
1575 cm = cmn;
1576 }
1577 if (m != NULL)
1578 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1579 else
1580 nextrecord = so->so_rcv.sb_mb;
1581 orig_resid = 0;
1582 }
1583 if (m != NULL) {
1584 if ((flags & MSG_PEEK) == 0) {
1585 KASSERT(m->m_nextpkt == nextrecord,
1586 ("soreceive: post-control, nextrecord !sync"));
1587 if (nextrecord == NULL) {
1588 KASSERT(so->so_rcv.sb_mb == m,
1589 ("soreceive: post-control, sb_mb!=m"));
1590 KASSERT(so->so_rcv.sb_lastrecord == m,
1591 ("soreceive: post-control, lastrecord!=m"));
1592 }
1593 }
1594 type = m->m_type;
1595 if (type == MT_OOBDATA)
1596 flags |= MSG_OOB;
1597 } else {
1598 if ((flags & MSG_PEEK) == 0) {
1599 KASSERT(so->so_rcv.sb_mb == nextrecord,
1600 ("soreceive: sb_mb != nextrecord"));
1601 if (so->so_rcv.sb_mb == NULL) {
1602 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1603 ("soreceive: sb_lastercord != NULL"));
1604 }
1605 }
1606 }
1607 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1608 SBLASTRECORDCHK(&so->so_rcv);
1609 SBLASTMBUFCHK(&so->so_rcv);
1610
1611 /*
1612 * Now continue to read any data mbufs off of the head of the socket
1613 * buffer until the read request is satisfied. Note that 'type' is
1614 * used to store the type of any mbuf reads that have happened so far
1615 * such that soreceive() can stop reading if the type changes, which
1616 * causes soreceive() to return only one of regular data and inline
1617 * out-of-band data in a single socket receive operation.
1618 */
1619 moff = 0;
1620 offset = 0;
1621 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1622 /*
1623 * If the type of mbuf has changed since the last mbuf
1624 * examined ('type'), end the receive operation.
1625 */
1626 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1627 if (m->m_type == MT_OOBDATA) {
1628 if (type != MT_OOBDATA)
1629 break;
1630 } else if (type == MT_OOBDATA)
1631 break;
1632 else
1633 KASSERT(m->m_type == MT_DATA,
1634 ("m->m_type == %d", m->m_type));
1635 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1636 len = uio->uio_resid;
1637 if (so->so_oobmark && len > so->so_oobmark - offset)
1638 len = so->so_oobmark - offset;
1639 if (len > m->m_len - moff)
1640 len = m->m_len - moff;
1641 /*
1642 * If mp is set, just pass back the mbufs. Otherwise copy
1643 * them out via the uio, then free. Sockbuf must be
1644 * consistent here (points to current mbuf, it points to next
1645 * record) when we drop priority; we must note any additions
1646 * to the sockbuf when we block interrupts again.
1647 */
1648 if (mp == NULL) {
1649 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1650 SBLASTRECORDCHK(&so->so_rcv);
1651 SBLASTMBUFCHK(&so->so_rcv);
1652 SOCKBUF_UNLOCK(&so->so_rcv);
1653 #ifdef ZERO_COPY_SOCKETS
1654 if (so_zero_copy_receive) {
1655 int disposable;
1656
1657 if ((m->m_flags & M_EXT)
1658 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1659 disposable = 1;
1660 else
1661 disposable = 0;
1662
1663 error = uiomoveco(mtod(m, char *) + moff,
1664 (int)len, uio,
1665 disposable);
1666 } else
1667 #endif /* ZERO_COPY_SOCKETS */
1668 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1669 SOCKBUF_LOCK(&so->so_rcv);
1670 if (error) {
1671 /*
1672 * The MT_SONAME mbuf has already been removed
1673 * from the record, so it is necessary to
1674 * remove the data mbufs, if any, to preserve
1675 * the invariant in the case of PR_ADDR that
1676 * requires MT_SONAME mbufs at the head of
1677 * each record.
1678 */
1679 if (m && pr->pr_flags & PR_ATOMIC &&
1680 ((flags & MSG_PEEK) == 0))
1681 (void)sbdroprecord_locked(&so->so_rcv);
1682 SOCKBUF_UNLOCK(&so->so_rcv);
1683 goto release;
1684 }
1685 } else
1686 uio->uio_resid -= len;
1687 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1688 if (len == m->m_len - moff) {
1689 if (m->m_flags & M_EOR)
1690 flags |= MSG_EOR;
1691 if (flags & MSG_PEEK) {
1692 m = m->m_next;
1693 moff = 0;
1694 } else {
1695 nextrecord = m->m_nextpkt;
1696 sbfree(&so->so_rcv, m);
1697 if (mp != NULL) {
1698 *mp = m;
1699 mp = &m->m_next;
1700 so->so_rcv.sb_mb = m = m->m_next;
1701 *mp = NULL;
1702 } else {
1703 so->so_rcv.sb_mb = m_free(m);
1704 m = so->so_rcv.sb_mb;
1705 }
1706 sockbuf_pushsync(&so->so_rcv, nextrecord);
1707 SBLASTRECORDCHK(&so->so_rcv);
1708 SBLASTMBUFCHK(&so->so_rcv);
1709 }
1710 } else {
1711 if (flags & MSG_PEEK)
1712 moff += len;
1713 else {
1714 if (mp != NULL) {
1715 int copy_flag;
1716
1717 if (flags & MSG_DONTWAIT)
1718 copy_flag = M_DONTWAIT;
1719 else
1720 copy_flag = M_TRYWAIT;
1721 if (copy_flag == M_TRYWAIT)
1722 SOCKBUF_UNLOCK(&so->so_rcv);
1723 *mp = m_copym(m, 0, len, copy_flag);
1724 if (copy_flag == M_TRYWAIT)
1725 SOCKBUF_LOCK(&so->so_rcv);
1726 if (*mp == NULL) {
1727 /*
1728 * m_copym() couldn't
1729 * allocate an mbuf. Adjust
1730 * uio_resid back (it was
1731 * adjusted down by len
1732 * bytes, which we didn't end
1733 * up "copying" over).
1734 */
1735 uio->uio_resid += len;
1736 break;
1737 }
1738 }
1739 m->m_data += len;
1740 m->m_len -= len;
1741 so->so_rcv.sb_cc -= len;
1742 }
1743 }
1744 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1745 if (so->so_oobmark) {
1746 if ((flags & MSG_PEEK) == 0) {
1747 so->so_oobmark -= len;
1748 if (so->so_oobmark == 0) {
1749 so->so_rcv.sb_state |= SBS_RCVATMARK;
1750 break;
1751 }
1752 } else {
1753 offset += len;
1754 if (offset == so->so_oobmark)
1755 break;
1756 }
1757 }
1758 if (flags & MSG_EOR)
1759 break;
1760 /*
1761 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1762 * must not quit until "uio->uio_resid == 0" or an error
1763 * termination. If a signal/timeout occurs, return with a
1764 * short count but without error. Keep sockbuf locked
1765 * against other readers.
1766 */
1767 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1768 !sosendallatonce(so) && nextrecord == NULL) {
1769 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1770 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1771 break;
1772 /*
1773 * Notify the protocol that some data has been
1774 * drained before blocking.
1775 */
1776 if (pr->pr_flags & PR_WANTRCVD) {
1777 SOCKBUF_UNLOCK(&so->so_rcv);
1778 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1779 SOCKBUF_LOCK(&so->so_rcv);
1780 }
1781 SBLASTRECORDCHK(&so->so_rcv);
1782 SBLASTMBUFCHK(&so->so_rcv);
1783 error = sbwait(&so->so_rcv);
1784 if (error) {
1785 SOCKBUF_UNLOCK(&so->so_rcv);
1786 goto release;
1787 }
1788 m = so->so_rcv.sb_mb;
1789 if (m != NULL)
1790 nextrecord = m->m_nextpkt;
1791 }
1792 }
1793
1794 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1795 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1796 flags |= MSG_TRUNC;
1797 if ((flags & MSG_PEEK) == 0)
1798 (void) sbdroprecord_locked(&so->so_rcv);
1799 }
1800 if ((flags & MSG_PEEK) == 0) {
1801 if (m == NULL) {
1802 /*
1803 * First part is an inline SB_EMPTY_FIXUP(). Second
1804 * part makes sure sb_lastrecord is up-to-date if
1805 * there is still data in the socket buffer.
1806 */
1807 so->so_rcv.sb_mb = nextrecord;
1808 if (so->so_rcv.sb_mb == NULL) {
1809 so->so_rcv.sb_mbtail = NULL;
1810 so->so_rcv.sb_lastrecord = NULL;
1811 } else if (nextrecord->m_nextpkt == NULL)
1812 so->so_rcv.sb_lastrecord = nextrecord;
1813 }
1814 SBLASTRECORDCHK(&so->so_rcv);
1815 SBLASTMBUFCHK(&so->so_rcv);
1816 /*
1817 * If soreceive() is being done from the socket callback,
1818 * then don't need to generate ACK to peer to update window,
1819 * since ACK will be generated on return to TCP.
1820 */
1821 if (!(flags & MSG_SOCALLBCK) &&
1822 (pr->pr_flags & PR_WANTRCVD)) {
1823 SOCKBUF_UNLOCK(&so->so_rcv);
1824 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1825 SOCKBUF_LOCK(&so->so_rcv);
1826 }
1827 }
1828 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1829 if (orig_resid == uio->uio_resid && orig_resid &&
1830 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1831 SOCKBUF_UNLOCK(&so->so_rcv);
1832 goto restart;
1833 }
1834 SOCKBUF_UNLOCK(&so->so_rcv);
1835
1836 if (flagsp != NULL)
1837 *flagsp |= flags;
1838 release:
1839 sbunlock(&so->so_rcv);
1840 return (error);
1841 }
1842
1843 /*
1844 * Optimized version of soreceive() for simple datagram cases from userspace.
1845 * Unlike in the stream case, we're able to drop a datagram if copyout()
1846 * fails, and because we handle datagrams atomically, we don't need to use a
1847 * sleep lock to prevent I/O interlacing.
1848 */
1849 int
1850 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
1851 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1852 {
1853 struct mbuf *m, *m2;
1854 int flags, len, error;
1855 struct protosw *pr = so->so_proto;
1856 struct mbuf *nextrecord;
1857
1858 if (psa != NULL)
1859 *psa = NULL;
1860 if (controlp != NULL)
1861 *controlp = NULL;
1862 if (flagsp != NULL)
1863 flags = *flagsp &~ MSG_EOR;
1864 else
1865 flags = 0;
1866
1867 /*
1868 * For any complicated cases, fall back to the full
1869 * soreceive_generic().
1870 */
1871 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
1872 return (soreceive_generic(so, psa, uio, mp0, controlp,
1873 flagsp));
1874
1875 /*
1876 * Enforce restrictions on use.
1877 */
1878 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
1879 ("soreceive_dgram: wantrcvd"));
1880 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
1881 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
1882 ("soreceive_dgram: SBS_RCVATMARK"));
1883 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
1884 ("soreceive_dgram: P_CONNREQUIRED"));
1885
1886 /*
1887 * Loop blocking while waiting for a datagram.
1888 */
1889 SOCKBUF_LOCK(&so->so_rcv);
1890 while ((m = so->so_rcv.sb_mb) == NULL) {
1891 KASSERT(so->so_rcv.sb_cc == 0,
1892 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
1893 so->so_rcv.sb_cc));
1894 if (so->so_error) {
1895 error = so->so_error;
1896 so->so_error = 0;
1897 SOCKBUF_UNLOCK(&so->so_rcv);
1898 return (error);
1899 }
1900 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
1901 uio->uio_resid == 0) {
1902 SOCKBUF_UNLOCK(&so->so_rcv);
1903 return (0);
1904 }
1905 if ((so->so_state & SS_NBIO) ||
1906 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1907 SOCKBUF_UNLOCK(&so->so_rcv);
1908 return (EWOULDBLOCK);
1909 }
1910 SBLASTRECORDCHK(&so->so_rcv);
1911 SBLASTMBUFCHK(&so->so_rcv);
1912 error = sbwait(&so->so_rcv);
1913 if (error) {
1914 SOCKBUF_UNLOCK(&so->so_rcv);
1915 return (error);
1916 }
1917 }
1918 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1919
1920 if (uio->uio_td)
1921 uio->uio_td->td_ru.ru_msgrcv++;
1922 SBLASTRECORDCHK(&so->so_rcv);
1923 SBLASTMBUFCHK(&so->so_rcv);
1924 nextrecord = m->m_nextpkt;
1925 if (nextrecord == NULL) {
1926 KASSERT(so->so_rcv.sb_lastrecord == m,
1927 ("soreceive_dgram: lastrecord != m"));
1928 }
1929
1930 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
1931 ("soreceive_dgram: m_nextpkt != nextrecord"));
1932
1933 /*
1934 * Pull 'm' and its chain off the front of the packet queue.
1935 */
1936 so->so_rcv.sb_mb = NULL;
1937 sockbuf_pushsync(&so->so_rcv, nextrecord);
1938
1939 /*
1940 * Walk 'm's chain and free that many bytes from the socket buffer.
1941 */
1942 for (m2 = m; m2 != NULL; m2 = m2->m_next)
1943 sbfree(&so->so_rcv, m2);
1944
1945 /*
1946 * Do a few last checks before we let go of the lock.
1947 */
1948 SBLASTRECORDCHK(&so->so_rcv);
1949 SBLASTMBUFCHK(&so->so_rcv);
1950 SOCKBUF_UNLOCK(&so->so_rcv);
1951
1952 if (pr->pr_flags & PR_ADDR) {
1953 KASSERT(m->m_type == MT_SONAME,
1954 ("m->m_type == %d", m->m_type));
1955 if (psa != NULL)
1956 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1957 M_NOWAIT);
1958 m = m_free(m);
1959 }
1960 if (m == NULL) {
1961 /* XXXRW: Can this happen? */
1962 return (0);
1963 }
1964
1965 /*
1966 * Packet to copyout() is now in 'm' and it is disconnected from the
1967 * queue.
1968 *
1969 * Process one or more MT_CONTROL mbufs present before any data mbufs
1970 * in the first mbuf chain on the socket buffer. We call into the
1971 * protocol to perform externalization (or freeing if controlp ==
1972 * NULL).
1973 */
1974 if (m->m_type == MT_CONTROL) {
1975 struct mbuf *cm = NULL, *cmn;
1976 struct mbuf **cme = &cm;
1977
1978 do {
1979 m2 = m->m_next;
1980 m->m_next = NULL;
1981 *cme = m;
1982 cme = &(*cme)->m_next;
1983 m = m2;
1984 } while (m != NULL && m->m_type == MT_CONTROL);
1985 while (cm != NULL) {
1986 cmn = cm->m_next;
1987 cm->m_next = NULL;
1988 if (pr->pr_domain->dom_externalize != NULL) {
1989 error = (*pr->pr_domain->dom_externalize)
1990 (cm, controlp);
1991 } else if (controlp != NULL)
1992 *controlp = cm;
1993 else
1994 m_freem(cm);
1995 if (controlp != NULL) {
1996 while (*controlp != NULL)
1997 controlp = &(*controlp)->m_next;
1998 }
1999 cm = cmn;
2000 }
2001 }
2002 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2003
2004 while (m != NULL && uio->uio_resid > 0) {
2005 len = uio->uio_resid;
2006 if (len > m->m_len)
2007 len = m->m_len;
2008 error = uiomove(mtod(m, char *), (int)len, uio);
2009 if (error) {
2010 m_freem(m);
2011 return (error);
2012 }
2013 m = m_free(m);
2014 }
2015 if (m != NULL)
2016 flags |= MSG_TRUNC;
2017 m_freem(m);
2018 if (flagsp != NULL)
2019 *flagsp |= flags;
2020 return (0);
2021 }
2022
2023 int
2024 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2025 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2026 {
2027
2028 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2029 controlp, flagsp));
2030 }
2031
2032 int
2033 soshutdown(struct socket *so, int how)
2034 {
2035 struct protosw *pr = so->so_proto;
2036
2037 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2038 return (EINVAL);
2039 if (pr->pr_usrreqs->pru_flush != NULL) {
2040 (*pr->pr_usrreqs->pru_flush)(so, how);
2041 }
2042 if (how != SHUT_WR)
2043 sorflush(so);
2044 if (how != SHUT_RD)
2045 return ((*pr->pr_usrreqs->pru_shutdown)(so));
2046 return (0);
2047 }
2048
2049 void
2050 sorflush(struct socket *so)
2051 {
2052 struct sockbuf *sb = &so->so_rcv;
2053 struct protosw *pr = so->so_proto;
2054 struct sockbuf asb;
2055
2056 /*
2057 * In order to avoid calling dom_dispose with the socket buffer mutex
2058 * held, and in order to generally avoid holding the lock for a long
2059 * time, we make a copy of the socket buffer and clear the original
2060 * (except locks, state). The new socket buffer copy won't have
2061 * initialized locks so we can only call routines that won't use or
2062 * assert those locks.
2063 *
2064 * Dislodge threads currently blocked in receive and wait to acquire
2065 * a lock against other simultaneous readers before clearing the
2066 * socket buffer. Don't let our acquire be interrupted by a signal
2067 * despite any existing socket disposition on interruptable waiting.
2068 */
2069 socantrcvmore(so);
2070 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2071
2072 /*
2073 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2074 * and mutex data unchanged.
2075 */
2076 SOCKBUF_LOCK(sb);
2077 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2078 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2079 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2080 bzero(&sb->sb_startzero,
2081 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2082 SOCKBUF_UNLOCK(sb);
2083 sbunlock(sb);
2084
2085 /*
2086 * Dispose of special rights and flush the socket buffer. Don't call
2087 * any unsafe routines (that rely on locks being initialized) on asb.
2088 */
2089 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2090 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2091 sbrelease_internal(&asb, so);
2092 }
2093
2094 /*
2095 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2096 * additional variant to handle the case where the option value needs to be
2097 * some kind of integer, but not a specific size. In addition to their use
2098 * here, these functions are also called by the protocol-level pr_ctloutput()
2099 * routines.
2100 */
2101 int
2102 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2103 {
2104 size_t valsize;
2105
2106 /*
2107 * If the user gives us more than we wanted, we ignore it, but if we
2108 * don't get the minimum length the caller wants, we return EINVAL.
2109 * On success, sopt->sopt_valsize is set to however much we actually
2110 * retrieved.
2111 */
2112 if ((valsize = sopt->sopt_valsize) < minlen)
2113 return EINVAL;
2114 if (valsize > len)
2115 sopt->sopt_valsize = valsize = len;
2116
2117 if (sopt->sopt_td != NULL)
2118 return (copyin(sopt->sopt_val, buf, valsize));
2119
2120 bcopy(sopt->sopt_val, buf, valsize);
2121 return (0);
2122 }
2123
2124 /*
2125 * Kernel version of setsockopt(2).
2126 *
2127 * XXX: optlen is size_t, not socklen_t
2128 */
2129 int
2130 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2131 size_t optlen)
2132 {
2133 struct sockopt sopt;
2134
2135 sopt.sopt_level = level;
2136 sopt.sopt_name = optname;
2137 sopt.sopt_dir = SOPT_SET;
2138 sopt.sopt_val = optval;
2139 sopt.sopt_valsize = optlen;
2140 sopt.sopt_td = NULL;
2141 return (sosetopt(so, &sopt));
2142 }
2143
2144 int
2145 sosetopt(struct socket *so, struct sockopt *sopt)
2146 {
2147 int error, optval;
2148 struct linger l;
2149 struct timeval tv;
2150 u_long val;
2151 #ifdef MAC
2152 struct mac extmac;
2153 #endif
2154
2155 error = 0;
2156 if (sopt->sopt_level != SOL_SOCKET) {
2157 if (so->so_proto && so->so_proto->pr_ctloutput)
2158 return ((*so->so_proto->pr_ctloutput)
2159 (so, sopt));
2160 error = ENOPROTOOPT;
2161 } else {
2162 switch (sopt->sopt_name) {
2163 #ifdef INET
2164 case SO_ACCEPTFILTER:
2165 error = do_setopt_accept_filter(so, sopt);
2166 if (error)
2167 goto bad;
2168 break;
2169 #endif
2170 case SO_LINGER:
2171 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2172 if (error)
2173 goto bad;
2174
2175 SOCK_LOCK(so);
2176 so->so_linger = l.l_linger;
2177 if (l.l_onoff)
2178 so->so_options |= SO_LINGER;
2179 else
2180 so->so_options &= ~SO_LINGER;
2181 SOCK_UNLOCK(so);
2182 break;
2183
2184 case SO_DEBUG:
2185 case SO_KEEPALIVE:
2186 case SO_DONTROUTE:
2187 case SO_USELOOPBACK:
2188 case SO_BROADCAST:
2189 case SO_REUSEADDR:
2190 case SO_REUSEPORT:
2191 case SO_OOBINLINE:
2192 case SO_TIMESTAMP:
2193 case SO_BINTIME:
2194 case SO_NOSIGPIPE:
2195 error = sooptcopyin(sopt, &optval, sizeof optval,
2196 sizeof optval);
2197 if (error)
2198 goto bad;
2199 SOCK_LOCK(so);
2200 if (optval)
2201 so->so_options |= sopt->sopt_name;
2202 else
2203 so->so_options &= ~sopt->sopt_name;
2204 SOCK_UNLOCK(so);
2205 break;
2206
2207 case SO_SETFIB:
2208 error = sooptcopyin(sopt, &optval, sizeof optval,
2209 sizeof optval);
2210 if (optval < 0 || optval > rt_numfibs) {
2211 error = EINVAL;
2212 goto bad;
2213 }
2214 if (so->so_proto != NULL &&
2215 ((so->so_proto->pr_domain->dom_family == PF_INET) ||
2216 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
2217 so->so_fibnum = optval;
2218 /* Note: ignore error */
2219 if (so->so_proto->pr_ctloutput)
2220 (*so->so_proto->pr_ctloutput)(so, sopt);
2221 } else {
2222 so->so_fibnum = 0;
2223 }
2224 break;
2225 case SO_SNDBUF:
2226 case SO_RCVBUF:
2227 case SO_SNDLOWAT:
2228 case SO_RCVLOWAT:
2229 error = sooptcopyin(sopt, &optval, sizeof optval,
2230 sizeof optval);
2231 if (error)
2232 goto bad;
2233
2234 /*
2235 * Values < 1 make no sense for any of these options,
2236 * so disallow them.
2237 */
2238 if (optval < 1) {
2239 error = EINVAL;
2240 goto bad;
2241 }
2242
2243 switch (sopt->sopt_name) {
2244 case SO_SNDBUF:
2245 case SO_RCVBUF:
2246 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2247 &so->so_snd : &so->so_rcv, (u_long)optval,
2248 so, curthread) == 0) {
2249 error = ENOBUFS;
2250 goto bad;
2251 }
2252 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2253 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2254 break;
2255
2256 /*
2257 * Make sure the low-water is never greater than the
2258 * high-water.
2259 */
2260 case SO_SNDLOWAT:
2261 SOCKBUF_LOCK(&so->so_snd);
2262 so->so_snd.sb_lowat =
2263 (optval > so->so_snd.sb_hiwat) ?
2264 so->so_snd.sb_hiwat : optval;
2265 SOCKBUF_UNLOCK(&so->so_snd);
2266 break;
2267 case SO_RCVLOWAT:
2268 SOCKBUF_LOCK(&so->so_rcv);
2269 so->so_rcv.sb_lowat =
2270 (optval > so->so_rcv.sb_hiwat) ?
2271 so->so_rcv.sb_hiwat : optval;
2272 SOCKBUF_UNLOCK(&so->so_rcv);
2273 break;
2274 }
2275 break;
2276
2277 case SO_SNDTIMEO:
2278 case SO_RCVTIMEO:
2279 #ifdef COMPAT_IA32
2280 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
2281 struct timeval32 tv32;
2282
2283 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2284 sizeof tv32);
2285 CP(tv32, tv, tv_sec);
2286 CP(tv32, tv, tv_usec);
2287 } else
2288 #endif
2289 error = sooptcopyin(sopt, &tv, sizeof tv,
2290 sizeof tv);
2291 if (error)
2292 goto bad;
2293
2294 /* assert(hz > 0); */
2295 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2296 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2297 error = EDOM;
2298 goto bad;
2299 }
2300 /* assert(tick > 0); */
2301 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2302 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2303 if (val > INT_MAX) {
2304 error = EDOM;
2305 goto bad;
2306 }
2307 if (val == 0 && tv.tv_usec != 0)
2308 val = 1;
2309
2310 switch (sopt->sopt_name) {
2311 case SO_SNDTIMEO:
2312 so->so_snd.sb_timeo = val;
2313 break;
2314 case SO_RCVTIMEO:
2315 so->so_rcv.sb_timeo = val;
2316 break;
2317 }
2318 break;
2319
2320 case SO_LABEL:
2321 #ifdef MAC
2322 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2323 sizeof extmac);
2324 if (error)
2325 goto bad;
2326 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2327 so, &extmac);
2328 #else
2329 error = EOPNOTSUPP;
2330 #endif
2331 break;
2332
2333 default:
2334 error = ENOPROTOOPT;
2335 break;
2336 }
2337 if (error == 0 && so->so_proto != NULL &&
2338 so->so_proto->pr_ctloutput != NULL) {
2339 (void) ((*so->so_proto->pr_ctloutput)
2340 (so, sopt));
2341 }
2342 }
2343 bad:
2344 return (error);
2345 }
2346
2347 /*
2348 * Helper routine for getsockopt.
2349 */
2350 int
2351 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2352 {
2353 int error;
2354 size_t valsize;
2355
2356 error = 0;
2357
2358 /*
2359 * Documented get behavior is that we always return a value, possibly
2360 * truncated to fit in the user's buffer. Traditional behavior is
2361 * that we always tell the user precisely how much we copied, rather
2362 * than something useful like the total amount we had available for
2363 * her. Note that this interface is not idempotent; the entire
2364 * answer must generated ahead of time.
2365 */
2366 valsize = min(len, sopt->sopt_valsize);
2367 sopt->sopt_valsize = valsize;
2368 if (sopt->sopt_val != NULL) {
2369 if (sopt->sopt_td != NULL)
2370 error = copyout(buf, sopt->sopt_val, valsize);
2371 else
2372 bcopy(buf, sopt->sopt_val, valsize);
2373 }
2374 return (error);
2375 }
2376
2377 int
2378 sogetopt(struct socket *so, struct sockopt *sopt)
2379 {
2380 int error, optval;
2381 struct linger l;
2382 struct timeval tv;
2383 #ifdef MAC
2384 struct mac extmac;
2385 #endif
2386
2387 error = 0;
2388 if (sopt->sopt_level != SOL_SOCKET) {
2389 if (so->so_proto && so->so_proto->pr_ctloutput) {
2390 return ((*so->so_proto->pr_ctloutput)
2391 (so, sopt));
2392 } else
2393 return (ENOPROTOOPT);
2394 } else {
2395 switch (sopt->sopt_name) {
2396 #ifdef INET
2397 case SO_ACCEPTFILTER:
2398 error = do_getopt_accept_filter(so, sopt);
2399 break;
2400 #endif
2401 case SO_LINGER:
2402 SOCK_LOCK(so);
2403 l.l_onoff = so->so_options & SO_LINGER;
2404 l.l_linger = so->so_linger;
2405 SOCK_UNLOCK(so);
2406 error = sooptcopyout(sopt, &l, sizeof l);
2407 break;
2408
2409 case SO_USELOOPBACK:
2410 case SO_DONTROUTE:
2411 case SO_DEBUG:
2412 case SO_KEEPALIVE:
2413 case SO_REUSEADDR:
2414 case SO_REUSEPORT:
2415 case SO_BROADCAST:
2416 case SO_OOBINLINE:
2417 case SO_ACCEPTCONN:
2418 case SO_TIMESTAMP:
2419 case SO_BINTIME:
2420 case SO_NOSIGPIPE:
2421 optval = so->so_options & sopt->sopt_name;
2422 integer:
2423 error = sooptcopyout(sopt, &optval, sizeof optval);
2424 break;
2425
2426 case SO_TYPE:
2427 optval = so->so_type;
2428 goto integer;
2429
2430 case SO_ERROR:
2431 SOCK_LOCK(so);
2432 optval = so->so_error;
2433 so->so_error = 0;
2434 SOCK_UNLOCK(so);
2435 goto integer;
2436
2437 case SO_SNDBUF:
2438 optval = so->so_snd.sb_hiwat;
2439 goto integer;
2440
2441 case SO_RCVBUF:
2442 optval = so->so_rcv.sb_hiwat;
2443 goto integer;
2444
2445 case SO_SNDLOWAT:
2446 optval = so->so_snd.sb_lowat;
2447 goto integer;
2448
2449 case SO_RCVLOWAT:
2450 optval = so->so_rcv.sb_lowat;
2451 goto integer;
2452
2453 case SO_SNDTIMEO:
2454 case SO_RCVTIMEO:
2455 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2456 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2457
2458 tv.tv_sec = optval / hz;
2459 tv.tv_usec = (optval % hz) * tick;
2460 #ifdef COMPAT_IA32
2461 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) {
2462 struct timeval32 tv32;
2463
2464 CP(tv, tv32, tv_sec);
2465 CP(tv, tv32, tv_usec);
2466 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2467 } else
2468 #endif
2469 error = sooptcopyout(sopt, &tv, sizeof tv);
2470 break;
2471
2472 case SO_LABEL:
2473 #ifdef MAC
2474 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2475 sizeof(extmac));
2476 if (error)
2477 return (error);
2478 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2479 so, &extmac);
2480 if (error)
2481 return (error);
2482 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2483 #else
2484 error = EOPNOTSUPP;
2485 #endif
2486 break;
2487
2488 case SO_PEERLABEL:
2489 #ifdef MAC
2490 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2491 sizeof(extmac));
2492 if (error)
2493 return (error);
2494 error = mac_getsockopt_peerlabel(
2495 sopt->sopt_td->td_ucred, so, &extmac);
2496 if (error)
2497 return (error);
2498 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2499 #else
2500 error = EOPNOTSUPP;
2501 #endif
2502 break;
2503
2504 case SO_LISTENQLIMIT:
2505 optval = so->so_qlimit;
2506 goto integer;
2507
2508 case SO_LISTENQLEN:
2509 optval = so->so_qlen;
2510 goto integer;
2511
2512 case SO_LISTENINCQLEN:
2513 optval = so->so_incqlen;
2514 goto integer;
2515
2516 default:
2517 error = ENOPROTOOPT;
2518 break;
2519 }
2520 return (error);
2521 }
2522 }
2523
2524 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2525 int
2526 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2527 {
2528 struct mbuf *m, *m_prev;
2529 int sopt_size = sopt->sopt_valsize;
2530
2531 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
2532 if (m == NULL)
2533 return ENOBUFS;
2534 if (sopt_size > MLEN) {
2535 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
2536 if ((m->m_flags & M_EXT) == 0) {
2537 m_free(m);
2538 return ENOBUFS;
2539 }
2540 m->m_len = min(MCLBYTES, sopt_size);
2541 } else {
2542 m->m_len = min(MLEN, sopt_size);
2543 }
2544 sopt_size -= m->m_len;
2545 *mp = m;
2546 m_prev = m;
2547
2548 while (sopt_size) {
2549 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
2550 if (m == NULL) {
2551 m_freem(*mp);
2552 return ENOBUFS;
2553 }
2554 if (sopt_size > MLEN) {
2555 MCLGET(m, sopt->sopt_td != NULL ? M_TRYWAIT :
2556 M_DONTWAIT);
2557 if ((m->m_flags & M_EXT) == 0) {
2558 m_freem(m);
2559 m_freem(*mp);
2560 return ENOBUFS;
2561 }
2562 m->m_len = min(MCLBYTES, sopt_size);
2563 } else {
2564 m->m_len = min(MLEN, sopt_size);
2565 }
2566 sopt_size -= m->m_len;
2567 m_prev->m_next = m;
2568 m_prev = m;
2569 }
2570 return (0);
2571 }
2572
2573 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2574 int
2575 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2576 {
2577 struct mbuf *m0 = m;
2578
2579 if (sopt->sopt_val == NULL)
2580 return (0);
2581 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2582 if (sopt->sopt_td != NULL) {
2583 int error;
2584
2585 error = copyin(sopt->sopt_val, mtod(m, char *),
2586 m->m_len);
2587 if (error != 0) {
2588 m_freem(m0);
2589 return(error);
2590 }
2591 } else
2592 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2593 sopt->sopt_valsize -= m->m_len;
2594 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2595 m = m->m_next;
2596 }
2597 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2598 panic("ip6_sooptmcopyin");
2599 return (0);
2600 }
2601
2602 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2603 int
2604 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2605 {
2606 struct mbuf *m0 = m;
2607 size_t valsize = 0;
2608
2609 if (sopt->sopt_val == NULL)
2610 return (0);
2611 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2612 if (sopt->sopt_td != NULL) {
2613 int error;
2614
2615 error = copyout(mtod(m, char *), sopt->sopt_val,
2616 m->m_len);
2617 if (error != 0) {
2618 m_freem(m0);
2619 return(error);
2620 }
2621 } else
2622 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2623 sopt->sopt_valsize -= m->m_len;
2624 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2625 valsize += m->m_len;
2626 m = m->m_next;
2627 }
2628 if (m != NULL) {
2629 /* enough soopt buffer should be given from user-land */
2630 m_freem(m0);
2631 return(EINVAL);
2632 }
2633 sopt->sopt_valsize = valsize;
2634 return (0);
2635 }
2636
2637 /*
2638 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2639 * out-of-band data, which will then notify socket consumers.
2640 */
2641 void
2642 sohasoutofband(struct socket *so)
2643 {
2644
2645 if (so->so_sigio != NULL)
2646 pgsigio(&so->so_sigio, SIGURG, 0);
2647 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2648 }
2649
2650 int
2651 sopoll(struct socket *so, int events, struct ucred *active_cred,
2652 struct thread *td)
2653 {
2654
2655 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2656 td));
2657 }
2658
2659 int
2660 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2661 struct thread *td)
2662 {
2663 int revents = 0;
2664
2665 SOCKBUF_LOCK(&so->so_snd);
2666 SOCKBUF_LOCK(&so->so_rcv);
2667 if (events & (POLLIN | POLLRDNORM))
2668 if (soreadable(so))
2669 revents |= events & (POLLIN | POLLRDNORM);
2670
2671 if (events & POLLINIGNEOF)
2672 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
2673 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
2674 revents |= POLLINIGNEOF;
2675
2676 if (events & (POLLOUT | POLLWRNORM))
2677 if (sowriteable(so))
2678 revents |= events & (POLLOUT | POLLWRNORM);
2679
2680 if (events & (POLLPRI | POLLRDBAND))
2681 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2682 revents |= events & (POLLPRI | POLLRDBAND);
2683
2684 if (revents == 0) {
2685 if (events &
2686 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
2687 POLLRDBAND)) {
2688 selrecord(td, &so->so_rcv.sb_sel);
2689 so->so_rcv.sb_flags |= SB_SEL;
2690 }
2691
2692 if (events & (POLLOUT | POLLWRNORM)) {
2693 selrecord(td, &so->so_snd.sb_sel);
2694 so->so_snd.sb_flags |= SB_SEL;
2695 }
2696 }
2697
2698 SOCKBUF_UNLOCK(&so->so_rcv);
2699 SOCKBUF_UNLOCK(&so->so_snd);
2700 return (revents);
2701 }
2702
2703 int
2704 soo_kqfilter(struct file *fp, struct knote *kn)
2705 {
2706 struct socket *so = kn->kn_fp->f_data;
2707 struct sockbuf *sb;
2708
2709 switch (kn->kn_filter) {
2710 case EVFILT_READ:
2711 if (so->so_options & SO_ACCEPTCONN)
2712 kn->kn_fop = &solisten_filtops;
2713 else
2714 kn->kn_fop = &soread_filtops;
2715 sb = &so->so_rcv;
2716 break;
2717 case EVFILT_WRITE:
2718 kn->kn_fop = &sowrite_filtops;
2719 sb = &so->so_snd;
2720 break;
2721 default:
2722 return (EINVAL);
2723 }
2724
2725 SOCKBUF_LOCK(sb);
2726 knlist_add(&sb->sb_sel.si_note, kn, 1);
2727 sb->sb_flags |= SB_KNOTE;
2728 SOCKBUF_UNLOCK(sb);
2729 return (0);
2730 }
2731
2732 /*
2733 * Some routines that return EOPNOTSUPP for entry points that are not
2734 * supported by a protocol. Fill in as needed.
2735 */
2736 int
2737 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2738 {
2739
2740 return EOPNOTSUPP;
2741 }
2742
2743 int
2744 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
2745 {
2746
2747 return EOPNOTSUPP;
2748 }
2749
2750 int
2751 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2752 {
2753
2754 return EOPNOTSUPP;
2755 }
2756
2757 int
2758 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2759 {
2760
2761 return EOPNOTSUPP;
2762 }
2763
2764 int
2765 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2766 {
2767
2768 return EOPNOTSUPP;
2769 }
2770
2771 int
2772 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2773 struct ifnet *ifp, struct thread *td)
2774 {
2775
2776 return EOPNOTSUPP;
2777 }
2778
2779 int
2780 pru_disconnect_notsupp(struct socket *so)
2781 {
2782
2783 return EOPNOTSUPP;
2784 }
2785
2786 int
2787 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
2788 {
2789
2790 return EOPNOTSUPP;
2791 }
2792
2793 int
2794 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2795 {
2796
2797 return EOPNOTSUPP;
2798 }
2799
2800 int
2801 pru_rcvd_notsupp(struct socket *so, int flags)
2802 {
2803
2804 return EOPNOTSUPP;
2805 }
2806
2807 int
2808 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2809 {
2810
2811 return EOPNOTSUPP;
2812 }
2813
2814 int
2815 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2816 struct sockaddr *addr, struct mbuf *control, struct thread *td)
2817 {
2818
2819 return EOPNOTSUPP;
2820 }
2821
2822 /*
2823 * This isn't really a ``null'' operation, but it's the default one and
2824 * doesn't do anything destructive.
2825 */
2826 int
2827 pru_sense_null(struct socket *so, struct stat *sb)
2828 {
2829
2830 sb->st_blksize = so->so_snd.sb_hiwat;
2831 return 0;
2832 }
2833
2834 int
2835 pru_shutdown_notsupp(struct socket *so)
2836 {
2837
2838 return EOPNOTSUPP;
2839 }
2840
2841 int
2842 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2843 {
2844
2845 return EOPNOTSUPP;
2846 }
2847
2848 int
2849 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2850 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
2851 {
2852
2853 return EOPNOTSUPP;
2854 }
2855
2856 int
2857 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2858 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2859 {
2860
2861 return EOPNOTSUPP;
2862 }
2863
2864 int
2865 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
2866 struct thread *td)
2867 {
2868
2869 return EOPNOTSUPP;
2870 }
2871
2872 static void
2873 filt_sordetach(struct knote *kn)
2874 {
2875 struct socket *so = kn->kn_fp->f_data;
2876
2877 SOCKBUF_LOCK(&so->so_rcv);
2878 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
2879 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
2880 so->so_rcv.sb_flags &= ~SB_KNOTE;
2881 SOCKBUF_UNLOCK(&so->so_rcv);
2882 }
2883
2884 /*ARGSUSED*/
2885 static int
2886 filt_soread(struct knote *kn, long hint)
2887 {
2888 struct socket *so;
2889
2890 so = kn->kn_fp->f_data;
2891 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2892
2893 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
2894 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2895 kn->kn_flags |= EV_EOF;
2896 kn->kn_fflags = so->so_error;
2897 return (1);
2898 } else if (so->so_error) /* temporary udp error */
2899 return (1);
2900 else if (kn->kn_sfflags & NOTE_LOWAT)
2901 return (kn->kn_data >= kn->kn_sdata);
2902 else
2903 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
2904 }
2905
2906 static void
2907 filt_sowdetach(struct knote *kn)
2908 {
2909 struct socket *so = kn->kn_fp->f_data;
2910
2911 SOCKBUF_LOCK(&so->so_snd);
2912 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
2913 if (knlist_empty(&so->so_snd.sb_sel.si_note))
2914 so->so_snd.sb_flags &= ~SB_KNOTE;
2915 SOCKBUF_UNLOCK(&so->so_snd);
2916 }
2917
2918 /*ARGSUSED*/
2919 static int
2920 filt_sowrite(struct knote *kn, long hint)
2921 {
2922 struct socket *so;
2923
2924 so = kn->kn_fp->f_data;
2925 SOCKBUF_LOCK_ASSERT(&so->so_snd);
2926 kn->kn_data = sbspace(&so->so_snd);
2927 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2928 kn->kn_flags |= EV_EOF;
2929 kn->kn_fflags = so->so_error;
2930 return (1);
2931 } else if (so->so_error) /* temporary udp error */
2932 return (1);
2933 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
2934 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2935 return (0);
2936 else if (kn->kn_sfflags & NOTE_LOWAT)
2937 return (kn->kn_data >= kn->kn_sdata);
2938 else
2939 return (kn->kn_data >= so->so_snd.sb_lowat);
2940 }
2941
2942 /*ARGSUSED*/
2943 static int
2944 filt_solisten(struct knote *kn, long hint)
2945 {
2946 struct socket *so = kn->kn_fp->f_data;
2947
2948 kn->kn_data = so->so_qlen;
2949 return (! TAILQ_EMPTY(&so->so_comp));
2950 }
2951
2952 int
2953 socheckuid(struct socket *so, uid_t uid)
2954 {
2955
2956 if (so == NULL)
2957 return (EPERM);
2958 if (so->so_cred->cr_uid != uid)
2959 return (EPERM);
2960 return (0);
2961 }
2962
2963 static int
2964 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
2965 {
2966 int error;
2967 int val;
2968
2969 val = somaxconn;
2970 error = sysctl_handle_int(oidp, &val, 0, req);
2971 if (error || !req->newptr )
2972 return (error);
2973
2974 if (val < 1 || val > USHRT_MAX)
2975 return (EINVAL);
2976
2977 somaxconn = val;
2978 return (0);
2979 }
2980
2981 /*
2982 * These functions are used by protocols to notify the socket layer (and its
2983 * consumers) of state changes in the sockets driven by protocol-side events.
2984 */
2985
2986 /*
2987 * Procedures to manipulate state flags of socket and do appropriate wakeups.
2988 *
2989 * Normal sequence from the active (originating) side is that
2990 * soisconnecting() is called during processing of connect() call, resulting
2991 * in an eventual call to soisconnected() if/when the connection is
2992 * established. When the connection is torn down soisdisconnecting() is
2993 * called during processing of disconnect() call, and soisdisconnected() is
2994 * called when the connection to the peer is totally severed. The semantics
2995 * of these routines are such that connectionless protocols can call
2996 * soisconnected() and soisdisconnected() only, bypassing the in-progress
2997 * calls when setting up a ``connection'' takes no time.
2998 *
2999 * From the passive side, a socket is created with two queues of sockets:
3000 * so_incomp for connections in progress and so_comp for connections already
3001 * made and awaiting user acceptance. As a protocol is preparing incoming
3002 * connections, it creates a socket structure queued on so_incomp by calling
3003 * sonewconn(). When the connection is established, soisconnected() is
3004 * called, and transfers the socket structure to so_comp, making it available
3005 * to accept().
3006 *
3007 * If a socket is closed with sockets on either so_incomp or so_comp, these
3008 * sockets are dropped.
3009 *
3010 * If higher-level protocols are implemented in the kernel, the wakeups done
3011 * here will sometimes cause software-interrupt process scheduling.
3012 */
3013 void
3014 soisconnecting(struct socket *so)
3015 {
3016
3017 SOCK_LOCK(so);
3018 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3019 so->so_state |= SS_ISCONNECTING;
3020 SOCK_UNLOCK(so);
3021 }
3022
3023 void
3024 soisconnected(struct socket *so)
3025 {
3026 struct socket *head;
3027
3028 ACCEPT_LOCK();
3029 SOCK_LOCK(so);
3030 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3031 so->so_state |= SS_ISCONNECTED;
3032 head = so->so_head;
3033 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3034 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3035 SOCK_UNLOCK(so);
3036 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3037 head->so_incqlen--;
3038 so->so_qstate &= ~SQ_INCOMP;
3039 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3040 head->so_qlen++;
3041 so->so_qstate |= SQ_COMP;
3042 ACCEPT_UNLOCK();
3043 sorwakeup(head);
3044 wakeup_one(&head->so_timeo);
3045 } else {
3046 ACCEPT_UNLOCK();
3047 so->so_upcall =
3048 head->so_accf->so_accept_filter->accf_callback;
3049 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
3050 so->so_rcv.sb_flags |= SB_UPCALL;
3051 so->so_options &= ~SO_ACCEPTFILTER;
3052 SOCK_UNLOCK(so);
3053 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT);
3054 }
3055 return;
3056 }
3057 SOCK_UNLOCK(so);
3058 ACCEPT_UNLOCK();
3059 wakeup(&so->so_timeo);
3060 sorwakeup(so);
3061 sowwakeup(so);
3062 }
3063
3064 void
3065 soisdisconnecting(struct socket *so)
3066 {
3067
3068 /*
3069 * Note: This code assumes that SOCK_LOCK(so) and
3070 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3071 */
3072 SOCKBUF_LOCK(&so->so_rcv);
3073 so->so_state &= ~SS_ISCONNECTING;
3074 so->so_state |= SS_ISDISCONNECTING;
3075 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3076 sorwakeup_locked(so);
3077 SOCKBUF_LOCK(&so->so_snd);
3078 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3079 sowwakeup_locked(so);
3080 wakeup(&so->so_timeo);
3081 }
3082
3083 void
3084 soisdisconnected(struct socket *so)
3085 {
3086
3087 /*
3088 * Note: This code assumes that SOCK_LOCK(so) and
3089 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3090 */
3091 SOCKBUF_LOCK(&so->so_rcv);
3092 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3093 so->so_state |= SS_ISDISCONNECTED;
3094 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3095 sorwakeup_locked(so);
3096 SOCKBUF_LOCK(&so->so_snd);
3097 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3098 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3099 sowwakeup_locked(so);
3100 wakeup(&so->so_timeo);
3101 }
3102
3103 /*
3104 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3105 */
3106 struct sockaddr *
3107 sodupsockaddr(const struct sockaddr *sa, int mflags)
3108 {
3109 struct sockaddr *sa2;
3110
3111 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3112 if (sa2)
3113 bcopy(sa, sa2, sa->sa_len);
3114 return sa2;
3115 }
3116
3117 /*
3118 * Create an external-format (``xsocket'') structure using the information in
3119 * the kernel-format socket structure pointed to by so. This is done to
3120 * reduce the spew of irrelevant information over this interface, to isolate
3121 * user code from changes in the kernel structure, and potentially to provide
3122 * information-hiding if we decide that some of this information should be
3123 * hidden from users.
3124 */
3125 void
3126 sotoxsocket(struct socket *so, struct xsocket *xso)
3127 {
3128
3129 xso->xso_len = sizeof *xso;
3130 xso->xso_so = so;
3131 xso->so_type = so->so_type;
3132 xso->so_options = so->so_options;
3133 xso->so_linger = so->so_linger;
3134 xso->so_state = so->so_state;
3135 xso->so_pcb = so->so_pcb;
3136 xso->xso_protocol = so->so_proto->pr_protocol;
3137 xso->xso_family = so->so_proto->pr_domain->dom_family;
3138 xso->so_qlen = so->so_qlen;
3139 xso->so_incqlen = so->so_incqlen;
3140 xso->so_qlimit = so->so_qlimit;
3141 xso->so_timeo = so->so_timeo;
3142 xso->so_error = so->so_error;
3143 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3144 xso->so_oobmark = so->so_oobmark;
3145 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3146 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3147 xso->so_uid = so->so_cred->cr_uid;
3148 }
3149
3150
3151 /*
3152 * Socket accessor functions to provide external consumers with
3153 * a safe interface to socket state
3154 *
3155 */
3156
3157 void
3158 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3159 {
3160
3161 TAILQ_FOREACH(so, &so->so_comp, so_list)
3162 func(so, arg);
3163 }
3164
3165 struct sockbuf *
3166 so_sockbuf_rcv(struct socket *so)
3167 {
3168
3169 return (&so->so_rcv);
3170 }
3171
3172 struct sockbuf *
3173 so_sockbuf_snd(struct socket *so)
3174 {
3175
3176 return (&so->so_snd);
3177 }
3178
3179 int
3180 so_state_get(const struct socket *so)
3181 {
3182
3183 return (so->so_state);
3184 }
3185
3186 void
3187 so_state_set(struct socket *so, int val)
3188 {
3189
3190 so->so_state = val;
3191 }
3192
3193 int
3194 so_options_get(const struct socket *so)
3195 {
3196
3197 return (so->so_options);
3198 }
3199
3200 void
3201 so_options_set(struct socket *so, int val)
3202 {
3203
3204 so->so_options = val;
3205 }
3206
3207 int
3208 so_error_get(const struct socket *so)
3209 {
3210
3211 return (so->so_error);
3212 }
3213
3214 void
3215 so_error_set(struct socket *so, int val)
3216 {
3217
3218 so->so_error = val;
3219 }
3220
3221 int
3222 so_linger_get(const struct socket *so)
3223 {
3224
3225 return (so->so_linger);
3226 }
3227
3228 void
3229 so_linger_set(struct socket *so, int val)
3230 {
3231
3232 so->so_linger = val;
3233 }
3234
3235 struct protosw *
3236 so_protosw_get(const struct socket *so)
3237 {
3238
3239 return (so->so_proto);
3240 }
3241
3242 void
3243 so_protosw_set(struct socket *so, struct protosw *val)
3244 {
3245
3246 so->so_proto = val;
3247 }
3248
3249 void
3250 so_sorwakeup(struct socket *so)
3251 {
3252
3253 sorwakeup(so);
3254 }
3255
3256 void
3257 so_sowwakeup(struct socket *so)
3258 {
3259
3260 sowwakeup(so);
3261 }
3262
3263 void
3264 so_sorwakeup_locked(struct socket *so)
3265 {
3266
3267 sorwakeup_locked(so);
3268 }
3269
3270 void
3271 so_sowwakeup_locked(struct socket *so)
3272 {
3273
3274 sowwakeup_locked(so);
3275 }
3276
3277 void
3278 so_lock(struct socket *so)
3279 {
3280 SOCK_LOCK(so);
3281 }
3282
3283 void
3284 so_unlock(struct socket *so)
3285 {
3286 SOCK_UNLOCK(so);
3287 }
Cache object: 0ab9d3e1c0e16d01d0f6ffa39058b68e
|