xref: /dragonfly/sys/kern/uipc_socket2.c (revision a8ca8ac6)
1 /*
2  * Copyright (c) 2005 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 1982, 1986, 1988, 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
35  * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
36  * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.33 2008/09/02 16:17:52 dillon Exp $
37  */
38 
39 #include "opt_param.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/file.h>	/* for maxfiles */
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/protosw.h>
49 #include <sys/resourcevar.h>
50 #include <sys/stat.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/aio.h> /* for aio_swake proto */
56 #include <sys/event.h>
57 
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
60 
61 int	maxsockets;
62 
63 /*
64  * Primitive routines for operating on sockets and socket buffers
65  */
66 
67 u_long	sb_max = SB_MAX;
68 u_long	sb_max_adj =
69     SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
70 
71 static	u_long sb_efficiency = 8;	/* parameter for sbreserve() */
72 
73 /************************************************************************
74  * signalsockbuf procedures						*
75  ************************************************************************/
76 
77 /*
78  * Wait for data to arrive at/drain from a socket buffer.
79  */
80 int
81 ssb_wait(struct signalsockbuf *ssb)
82 {
83 
84 	ssb->ssb_flags |= SSB_WAIT;
85 	return (tsleep((caddr_t)&ssb->ssb_cc,
86 			((ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH),
87 			"sbwait",
88 			ssb->ssb_timeo));
89 }
90 
91 /*
92  * Lock a sockbuf already known to be locked;
93  * return any error returned from sleep (EINTR).
94  */
95 int
96 _ssb_lock(struct signalsockbuf *ssb)
97 {
98 	int error;
99 
100 	while (ssb->ssb_flags & SSB_LOCK) {
101 		ssb->ssb_flags |= SSB_WANT;
102 		error = tsleep((caddr_t)&ssb->ssb_flags,
103 			    ((ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH),
104 			    "sblock", 0);
105 		if (error)
106 			return (error);
107 	}
108 	ssb->ssb_flags |= SSB_LOCK;
109 	return (0);
110 }
111 
112 /*
113  * This does the same for sockbufs.  Note that the xsockbuf structure,
114  * since it is always embedded in a socket, does not include a self
115  * pointer nor a length.  We make this entry point public in case
116  * some other mechanism needs it.
117  */
118 void
119 ssbtoxsockbuf(struct signalsockbuf *ssb, struct xsockbuf *xsb)
120 {
121 	xsb->sb_cc = ssb->ssb_cc;
122 	xsb->sb_hiwat = ssb->ssb_hiwat;
123 	xsb->sb_mbcnt = ssb->ssb_mbcnt;
124 	xsb->sb_mbmax = ssb->ssb_mbmax;
125 	xsb->sb_lowat = ssb->ssb_lowat;
126 	xsb->sb_flags = ssb->ssb_flags;
127 	xsb->sb_timeo = ssb->ssb_timeo;
128 }
129 
130 
131 /************************************************************************
132  * Procedures which manipulate socket state flags, wakeups, etc.	*
133  ************************************************************************
134  *
135  * Normal sequence from the active (originating) side is that
136  * soisconnecting() is called during processing of connect() call, resulting
137  * in an eventual call to soisconnected() if/when the connection is
138  * established.  When the connection is torn down soisdisconnecting() is
139  * called during processing of disconnect() call, and soisdisconnected() is
140  * called when the connection to the peer is totally severed.
141  *
142  * The semantics of these routines are such that connectionless protocols
143  * can call soisconnected() and soisdisconnected() only, bypassing the
144  * in-progress calls when setting up a ``connection'' takes no time.
145  *
146  * From the passive side, a socket is created with two queues of sockets:
147  * so_incomp for connections in progress and so_comp for connections
148  * already made and awaiting user acceptance.  As a protocol is preparing
149  * incoming connections, it creates a socket structure queued on so_incomp
150  * by calling sonewconn().  When the connection is established,
151  * soisconnected() is called, and transfers the socket structure to so_comp,
152  * making it available to accept().
153  *
154  * If a socket is closed with sockets on either so_incomp or so_comp, these
155  * sockets are dropped.
156  *
157  * If higher level protocols are implemented in the kernel, the wakeups
158  * done here will sometimes cause software-interrupt process scheduling.
159  */
160 
161 void
162 soisconnecting(struct socket *so)
163 {
164 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
165 	so->so_state |= SS_ISCONNECTING;
166 }
167 
168 void
169 soisconnected(struct socket *so)
170 {
171 	struct socket *head = so->so_head;
172 
173 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
174 	so->so_state |= SS_ISCONNECTED;
175 	if (head && (so->so_state & SS_INCOMP)) {
176 		if ((so->so_options & SO_ACCEPTFILTER) != 0) {
177 			so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
178 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
179 			so->so_rcv.ssb_flags |= SSB_UPCALL;
180 			so->so_options &= ~SO_ACCEPTFILTER;
181 			so->so_upcall(so, so->so_upcallarg, 0);
182 			return;
183 		}
184 		TAILQ_REMOVE(&head->so_incomp, so, so_list);
185 		head->so_incqlen--;
186 		so->so_state &= ~SS_INCOMP;
187 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
188 		head->so_qlen++;
189 		so->so_state |= SS_COMP;
190 		sorwakeup(head);
191 		wakeup_one(&head->so_timeo);
192 	} else {
193 		wakeup(&so->so_timeo);
194 		sorwakeup(so);
195 		sowwakeup(so);
196 	}
197 }
198 
199 void
200 soisdisconnecting(struct socket *so)
201 {
202 	so->so_state &= ~SS_ISCONNECTING;
203 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
204 	wakeup((caddr_t)&so->so_timeo);
205 	sowwakeup(so);
206 	sorwakeup(so);
207 }
208 
209 void
210 soisdisconnected(struct socket *so)
211 {
212 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
213 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
214 	wakeup((caddr_t)&so->so_timeo);
215 	sbdrop(&so->so_snd.sb, so->so_snd.ssb_cc);
216 	sowwakeup(so);
217 	sorwakeup(so);
218 }
219 
220 void
221 soisreconnecting(struct socket *so)
222 {
223         so->so_state &= ~(SS_ISDISCONNECTING|SS_ISDISCONNECTED|SS_CANTRCVMORE|
224 			SS_CANTSENDMORE);
225 	so->so_state |= SS_ISCONNECTING;
226 }
227 
228 void
229 soisreconnected(struct socket *so)
230 {
231 	so->so_state &= ~(SS_ISDISCONNECTED|SS_CANTRCVMORE|SS_CANTSENDMORE);
232 	soisconnected(so);
233 }
234 
235 /*
236  * Set or change the message port a socket receives commands on.
237  *
238  * XXX
239  */
240 void
241 sosetport(struct socket *so, lwkt_port_t port)
242 {
243 	so->so_port = port;
244 }
245 
246 /*
247  * When an attempt at a new connection is noted on a socket
248  * which accepts connections, sonewconn is called.  If the
249  * connection is possible (subject to space constraints, etc.)
250  * then we allocate a new structure, propoerly linked into the
251  * data structure of the original socket, and return this.
252  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
253  */
254 struct socket *
255 sonewconn(struct socket *head, int connstatus)
256 {
257 	struct socket *so;
258 	struct socket *sp;
259 	struct pru_attach_info ai;
260 
261 	if (head->so_qlen > 3 * head->so_qlimit / 2)
262 		return (NULL);
263 	so = soalloc(1);
264 	if (so == NULL)
265 		return (NULL);
266 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
267 		connstatus = 0;
268 	so->so_head = head;
269 	so->so_type = head->so_type;
270 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
271 	so->so_linger = head->so_linger;
272 	so->so_state = head->so_state | SS_NOFDREF;
273 	so->so_proto = head->so_proto;
274 	so->so_cred = crhold(head->so_cred);
275 	ai.sb_rlimit = NULL;
276 	ai.p_ucred = NULL;
277 	ai.fd_rdir = NULL;		/* jail code cruft XXX JH */
278 	if (soreserve(so, head->so_snd.ssb_hiwat, head->so_rcv.ssb_hiwat, NULL) ||
279 	    /* Directly call function since we're already at protocol level. */
280 	    (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) {
281 		sodealloc(so);
282 		return (NULL);
283 	}
284 	KKASSERT(so->so_port != NULL);
285 	so->so_rcv.ssb_lowat = head->so_rcv.ssb_lowat;
286 	so->so_snd.ssb_lowat = head->so_snd.ssb_lowat;
287 	so->so_rcv.ssb_timeo = head->so_rcv.ssb_timeo;
288 	so->so_snd.ssb_timeo = head->so_snd.ssb_timeo;
289 	so->so_rcv.ssb_flags |= head->so_rcv.ssb_flags &
290 				(SSB_AUTOSIZE | SSB_AUTOLOWAT);
291 	so->so_snd.ssb_flags |= head->so_snd.ssb_flags &
292 				(SSB_AUTOSIZE | SSB_AUTOLOWAT);
293 	if (connstatus) {
294 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
295 		so->so_state |= SS_COMP;
296 		head->so_qlen++;
297 	} else {
298 		if (head->so_incqlen > head->so_qlimit) {
299 			sp = TAILQ_FIRST(&head->so_incomp);
300 			TAILQ_REMOVE(&head->so_incomp, sp, so_list);
301 			head->so_incqlen--;
302 			sp->so_state &= ~SS_INCOMP;
303 			sp->so_head = NULL;
304 			soaborta(sp);
305 		}
306 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
307 		so->so_state |= SS_INCOMP;
308 		head->so_incqlen++;
309 	}
310 	if (connstatus) {
311 		sorwakeup(head);
312 		wakeup((caddr_t)&head->so_timeo);
313 		so->so_state |= connstatus;
314 	}
315 	return (so);
316 }
317 
318 /*
319  * Socantsendmore indicates that no more data will be sent on the
320  * socket; it would normally be applied to a socket when the user
321  * informs the system that no more data is to be sent, by the protocol
322  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
323  * will be received, and will normally be applied to the socket by a
324  * protocol when it detects that the peer will send no more data.
325  * Data queued for reading in the socket may yet be read.
326  */
327 void
328 socantsendmore(struct socket *so)
329 {
330 	so->so_state |= SS_CANTSENDMORE;
331 	sowwakeup(so);
332 }
333 
334 void
335 socantrcvmore(struct socket *so)
336 {
337 	so->so_state |= SS_CANTRCVMORE;
338 	sorwakeup(so);
339 }
340 
341 /*
342  * Wakeup processes waiting on a socket buffer.  Do asynchronous notification
343  * via SIGIO if the socket has the SS_ASYNC flag set.
344  *
345  * For users waiting on send/recv try to avoid unnecessary context switch
346  * thrashing.  Particularly for senders of large buffers (needs to be
347  * extended to sel and aio? XXX)
348  */
349 void
350 sowakeup(struct socket *so, struct signalsockbuf *ssb)
351 {
352 	struct kqinfo *kqinfo = &ssb->ssb_kq;
353 
354 	if (ssb->ssb_flags & SSB_WAIT) {
355 		if ((ssb == &so->so_snd && ssb_space(ssb) >= ssb->ssb_lowat) ||
356 		    (ssb == &so->so_rcv && ssb->ssb_cc >= ssb->ssb_lowat) ||
357 		    (ssb == &so->so_snd && (so->so_state & SS_CANTSENDMORE)) ||
358 		    (ssb == &so->so_rcv && (so->so_state & SS_CANTRCVMORE))
359 		) {
360 			ssb->ssb_flags &= ~SSB_WAIT;
361 			wakeup((caddr_t)&ssb->ssb_cc);
362 		}
363 	}
364 	if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
365 		pgsigio(so->so_sigio, SIGIO, 0);
366 	if (ssb->ssb_flags & SSB_UPCALL)
367 		(*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT);
368 	if (ssb->ssb_flags & SSB_AIO)
369 		aio_swake(so, ssb);
370 	KNOTE(&kqinfo->ki_note, 0);
371 	if (ssb->ssb_flags & SSB_MEVENT) {
372 		struct netmsg_so_notify *msg, *nmsg;
373 
374 		TAILQ_FOREACH_MUTABLE(msg, &kqinfo->ki_mlist, nm_list, nmsg) {
375 			if (msg->nm_predicate(&msg->nm_netmsg)) {
376 				TAILQ_REMOVE(&kqinfo->ki_mlist, msg, nm_list);
377 				lwkt_replymsg(&msg->nm_netmsg.nm_lmsg,
378 					      msg->nm_netmsg.nm_lmsg.ms_error);
379 			}
380 		}
381 		if (TAILQ_EMPTY(&ssb->ssb_kq.ki_mlist))
382 			ssb->ssb_flags &= ~SSB_MEVENT;
383 	}
384 }
385 
386 /*
387  * Socket buffer (struct signalsockbuf) utility routines.
388  *
389  * Each socket contains two socket buffers: one for sending data and
390  * one for receiving data.  Each buffer contains a queue of mbufs,
391  * information about the number of mbufs and amount of data in the
392  * queue, and other fields allowing kevent()/select()/poll() statements
393  * and notification on data availability to be implemented.
394  *
395  * Data stored in a socket buffer is maintained as a list of records.
396  * Each record is a list of mbufs chained together with the m_next
397  * field.  Records are chained together with the m_nextpkt field. The upper
398  * level routine soreceive() expects the following conventions to be
399  * observed when placing information in the receive buffer:
400  *
401  * 1. If the protocol requires each message be preceded by the sender's
402  *    name, then a record containing that name must be present before
403  *    any associated data (mbuf's must be of type MT_SONAME).
404  * 2. If the protocol supports the exchange of ``access rights'' (really
405  *    just additional data associated with the message), and there are
406  *    ``rights'' to be received, then a record containing this data
407  *    should be present (mbuf's must be of type MT_RIGHTS).
408  * 3. If a name or rights record exists, then it must be followed by
409  *    a data record, perhaps of zero length.
410  *
411  * Before using a new socket structure it is first necessary to reserve
412  * buffer space to the socket, by calling sbreserve().  This should commit
413  * some of the available buffer space in the system buffer pool for the
414  * socket (currently, it does nothing but enforce limits).  The space
415  * should be released by calling ssb_release() when the socket is destroyed.
416  */
417 int
418 soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl)
419 {
420 	if (so->so_snd.ssb_lowat == 0)
421 		so->so_snd.ssb_flags |= SSB_AUTOLOWAT;
422 	if (ssb_reserve(&so->so_snd, sndcc, so, rl) == 0)
423 		goto bad;
424 	if (ssb_reserve(&so->so_rcv, rcvcc, so, rl) == 0)
425 		goto bad2;
426 	if (so->so_rcv.ssb_lowat == 0)
427 		so->so_rcv.ssb_lowat = 1;
428 	if (so->so_snd.ssb_lowat == 0)
429 		so->so_snd.ssb_lowat = MCLBYTES;
430 	if (so->so_snd.ssb_lowat > so->so_snd.ssb_hiwat)
431 		so->so_snd.ssb_lowat = so->so_snd.ssb_hiwat;
432 	return (0);
433 bad2:
434 	ssb_release(&so->so_snd, so);
435 bad:
436 	return (ENOBUFS);
437 }
438 
439 static int
440 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
441 {
442 	int error = 0;
443 	u_long old_sb_max = sb_max;
444 
445 	error = SYSCTL_OUT(req, arg1, sizeof(int));
446 	if (error || !req->newptr)
447 		return (error);
448 	error = SYSCTL_IN(req, arg1, sizeof(int));
449 	if (error)
450 		return (error);
451 	if (sb_max < MSIZE + MCLBYTES) {
452 		sb_max = old_sb_max;
453 		return (EINVAL);
454 	}
455 	sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
456 	return (0);
457 }
458 
459 /*
460  * Allot mbufs to a signalsockbuf.
461  *
462  * Attempt to scale mbmax so that mbcnt doesn't become limiting
463  * if buffering efficiency is near the normal case.
464  *
465  * sb_max only applies to user-sockets (where rl != NULL).  It does
466  * not apply to kernel sockets or kernel-controlled sockets.  Note
467  * that NFS overrides the sockbuf limits created when nfsd creates
468  * a socket.
469  */
470 int
471 ssb_reserve(struct signalsockbuf *ssb, u_long cc, struct socket *so,
472 	    struct rlimit *rl)
473 {
474 	/*
475 	 * rl will only be NULL when we're in an interrupt (eg, in tcp_input)
476 	 * or when called from netgraph (ie, ngd_attach)
477 	 */
478 	if (rl && cc > sb_max_adj)
479 		cc = sb_max_adj;
480 	if (!chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, cc,
481 		       rl ? rl->rlim_cur : RLIM_INFINITY)) {
482 		return (0);
483 	}
484 	if (rl)
485 		ssb->ssb_mbmax = min(cc * sb_efficiency, sb_max);
486 	else
487 		ssb->ssb_mbmax = cc * sb_efficiency;
488 
489 	/*
490 	 * AUTOLOWAT is set on send buffers and prevents large writes
491 	 * from generating a huge number of context switches.
492 	 */
493 	if (ssb->ssb_flags & SSB_AUTOLOWAT) {
494 		ssb->ssb_lowat = ssb->ssb_hiwat / 2;
495 		if (ssb->ssb_lowat < MCLBYTES)
496 			ssb->ssb_lowat = MCLBYTES;
497 	}
498 	if (ssb->ssb_lowat > ssb->ssb_hiwat)
499 		ssb->ssb_lowat = ssb->ssb_hiwat;
500 	return (1);
501 }
502 
503 /*
504  * Free mbufs held by a socket, and reserved mbuf space.
505  */
506 void
507 ssb_release(struct signalsockbuf *ssb, struct socket *so)
508 {
509 	sbflush(&ssb->sb);
510 	(void)chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, 0,
511 	    RLIM_INFINITY);
512 	ssb->ssb_mbmax = 0;
513 }
514 
515 /*
516  * Some routines that return EOPNOTSUPP for entry points that are not
517  * supported by a protocol.  Fill in as needed.
518  */
519 int
520 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
521 {
522 	return EOPNOTSUPP;
523 }
524 
525 int
526 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
527 {
528 	return EOPNOTSUPP;
529 }
530 
531 int
532 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
533 {
534 	return EOPNOTSUPP;
535 }
536 
537 int
538 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
539 {
540 	return EOPNOTSUPP;
541 }
542 
543 int
544 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
545 		    struct ifnet *ifp, struct thread *td)
546 {
547 	return EOPNOTSUPP;
548 }
549 
550 int
551 pru_disconnect_notsupp(struct socket *so)
552 {
553 	return EOPNOTSUPP;
554 }
555 
556 int
557 pru_listen_notsupp(struct socket *so, struct thread *td)
558 {
559 	return EOPNOTSUPP;
560 }
561 
562 int
563 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
564 {
565 	return EOPNOTSUPP;
566 }
567 
568 int
569 pru_rcvd_notsupp(struct socket *so, int flags)
570 {
571 	return EOPNOTSUPP;
572 }
573 
574 int
575 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
576 {
577 	return EOPNOTSUPP;
578 }
579 
580 int
581 pru_shutdown_notsupp(struct socket *so)
582 {
583 	return EOPNOTSUPP;
584 }
585 
586 int
587 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
588 {
589 	return EOPNOTSUPP;
590 }
591 
592 int
593 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
594 	   struct mbuf *top, struct mbuf *control, int flags,
595 	   struct thread *td)
596 {
597 	if (top)
598 		m_freem(top);
599 	if (control)
600 		m_freem(control);
601 	return (EOPNOTSUPP);
602 }
603 
604 int
605 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
606 		      struct uio *uio, struct sockbuf *sio,
607 		      struct mbuf **controlp, int *flagsp)
608 {
609 	return (EOPNOTSUPP);
610 }
611 
612 int
613 pru_ctloutput_notsupp(struct socket *so, struct sockopt *sopt)
614 {
615 	return (EOPNOTSUPP);
616 }
617 
618 /*
619  * This isn't really a ``null'' operation, but it's the default one
620  * and doesn't do anything destructive.
621  */
622 int
623 pru_sense_null(struct socket *so, struct stat *sb)
624 {
625 	sb->st_blksize = so->so_snd.ssb_hiwat;
626 	return 0;
627 }
628 
629 /*
630  * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.  Callers
631  * of this routine assume that it always succeeds, so we have to use a
632  * blockable allocation even though we might be called from a critical thread.
633  */
634 struct sockaddr *
635 dup_sockaddr(const struct sockaddr *sa)
636 {
637 	struct sockaddr *sa2;
638 
639 	sa2 = kmalloc(sa->sa_len, M_SONAME, M_INTWAIT);
640 	bcopy(sa, sa2, sa->sa_len);
641 	return (sa2);
642 }
643 
644 /*
645  * Create an external-format (``xsocket'') structure using the information
646  * in the kernel-format socket structure pointed to by so.  This is done
647  * to reduce the spew of irrelevant information over this interface,
648  * to isolate user code from changes in the kernel structure, and
649  * potentially to provide information-hiding if we decide that
650  * some of this information should be hidden from users.
651  */
652 void
653 sotoxsocket(struct socket *so, struct xsocket *xso)
654 {
655 	xso->xso_len = sizeof *xso;
656 	xso->xso_so = so;
657 	xso->so_type = so->so_type;
658 	xso->so_options = so->so_options;
659 	xso->so_linger = so->so_linger;
660 	xso->so_state = so->so_state;
661 	xso->so_pcb = so->so_pcb;
662 	xso->xso_protocol = so->so_proto->pr_protocol;
663 	xso->xso_family = so->so_proto->pr_domain->dom_family;
664 	xso->so_qlen = so->so_qlen;
665 	xso->so_incqlen = so->so_incqlen;
666 	xso->so_qlimit = so->so_qlimit;
667 	xso->so_timeo = so->so_timeo;
668 	xso->so_error = so->so_error;
669 	xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
670 	xso->so_oobmark = so->so_oobmark;
671 	ssbtoxsockbuf(&so->so_snd, &xso->so_snd);
672 	ssbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
673 	xso->so_uid = so->so_cred->cr_uid;
674 }
675 
676 /*
677  * Here is the definition of some of the basic objects in the kern.ipc
678  * branch of the MIB.
679  */
680 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
681 
682 /*
683  * This takes the place of kern.maxsockbuf, which moved to kern.ipc.
684  *
685  * NOTE! sb_max only applies to user-created socket buffers.
686  */
687 static int dummy;
688 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
689 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
690     &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
691 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
692     &maxsockets, 0, "Maximum number of sockets available");
693 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
694     &sb_efficiency, 0, "");
695 
696 /*
697  * Initialize maxsockets
698  */
699 static void
700 init_maxsockets(void *ignored)
701 {
702     TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
703     maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
704 }
705 SYSINIT(param, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
706 	init_maxsockets, NULL);
707 
708