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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.18 2005/04/20 21:37:06 hsu Exp $
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>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/resourcevar.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>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
64 * Primitive routines for operating on sockets and socket buffers
67 u_long sb_max = SB_MAX;
69 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
74 * Procedures to manipulate state flags of socket
75 * and do appropriate wakeups. Normal sequence from the
76 * active (originating) side is that soisconnecting() is
77 * called during processing of connect() call,
78 * resulting in an eventual call to soisconnected() if/when the
79 * connection is established. When the connection is torn down
80 * soisdisconnecting() is called during processing of disconnect() call,
81 * and soisdisconnected() is called when the connection to the peer
82 * is totally severed. The semantics of these routines are such that
83 * connectionless protocols can call soisconnected() and soisdisconnected()
84 * only, bypassing the in-progress calls when setting up a ``connection''
87 * From the passive side, a socket is created with
88 * two queues of sockets: so_incomp for connections in progress
89 * and so_comp for connections already made and awaiting user acceptance.
90 * As a protocol is preparing incoming connections, it creates a socket
91 * structure queued on so_incomp by calling sonewconn(). When the connection
92 * is established, soisconnected() is called, and transfers the
93 * socket structure to so_comp, making it available to accept().
95 * If a socket is closed with sockets on either
96 * so_incomp or so_comp, these sockets are dropped.
98 * If higher level protocols are implemented in
99 * the kernel, the wakeups done here will sometimes
100 * cause software-interrupt process scheduling.
108 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
109 so->so_state |= SS_ISCONNECTING;
116 struct socket *head = so->so_head;
118 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
119 so->so_state |= SS_ISCONNECTED;
120 if (head && (so->so_state & SS_INCOMP)) {
121 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
122 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
123 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
124 so->so_rcv.sb_flags |= SB_UPCALL;
125 so->so_options &= ~SO_ACCEPTFILTER;
126 so->so_upcall(so, so->so_upcallarg, 0);
129 TAILQ_REMOVE(&head->so_incomp, so, so_list);
131 so->so_state &= ~SS_INCOMP;
132 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
134 so->so_state |= SS_COMP;
136 wakeup_one(&head->so_timeo);
138 wakeup(&so->so_timeo);
145 soisdisconnecting(so)
149 so->so_state &= ~SS_ISCONNECTING;
150 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
151 wakeup((caddr_t)&so->so_timeo);
161 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
162 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
163 wakeup((caddr_t)&so->so_timeo);
164 sbdrop(&so->so_snd, so->so_snd.sb_cc);
170 * When an attempt at a new connection is noted on a socket
171 * which accepts connections, sonewconn is called. If the
172 * connection is possible (subject to space constraints, etc.)
173 * then we allocate a new structure, propoerly linked into the
174 * data structure of the original socket, and return this.
175 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
178 sonewconn(struct socket *head, int connstatus)
181 struct pru_attach_info ai;
183 if (head->so_qlen > 3 * head->so_qlimit / 2)
184 return ((struct socket *)0);
187 return ((struct socket *)0);
188 if ((head->so_options & SO_ACCEPTFILTER) != 0)
191 so->so_type = head->so_type;
192 so->so_options = head->so_options &~ SO_ACCEPTCONN;
193 so->so_linger = head->so_linger;
194 so->so_state = head->so_state | SS_NOFDREF;
195 so->so_proto = head->so_proto;
196 so->so_timeo = head->so_timeo;
197 so->so_cred = crhold(head->so_cred);
200 ai.fd_rdir = NULL; /* jail code cruft XXX JH */
201 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat, NULL) ||
202 /* Directly call function since we're already at protocol level. */
203 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) {
205 return ((struct socket *)0);
209 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
210 so->so_state |= SS_COMP;
213 if (head->so_incqlen > head->so_qlimit) {
215 sp = TAILQ_FIRST(&head->so_incomp);
218 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
219 so->so_state |= SS_INCOMP;
224 wakeup((caddr_t)&head->so_timeo);
225 so->so_state |= connstatus;
231 * Socantsendmore indicates that no more data will be sent on the
232 * socket; it would normally be applied to a socket when the user
233 * informs the system that no more data is to be sent, by the protocol
234 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
235 * will be received, and will normally be applied to the socket by a
236 * protocol when it detects that the peer will send no more data.
237 * Data queued for reading in the socket may yet be read.
245 so->so_state |= SS_CANTSENDMORE;
254 so->so_state |= SS_CANTRCVMORE;
259 * Wait for data to arrive at/drain from a socket buffer.
266 sb->sb_flags |= SB_WAIT;
267 return (tsleep((caddr_t)&sb->sb_cc,
268 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
274 * Lock a sockbuf already known to be locked;
275 * return any error returned from sleep (EINTR).
283 while (sb->sb_flags & SB_LOCK) {
284 sb->sb_flags |= SB_WANT;
285 error = tsleep((caddr_t)&sb->sb_flags,
286 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
291 sb->sb_flags |= SB_LOCK;
296 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
297 * via SIGIO if the socket has the SS_ASYNC flag set.
304 struct selinfo *selinfo = &sb->sb_sel;
307 sb->sb_flags &= ~SB_SEL;
308 if (sb->sb_flags & SB_WAIT) {
309 sb->sb_flags &= ~SB_WAIT;
310 wakeup((caddr_t)&sb->sb_cc);
312 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
313 pgsigio(so->so_sigio, SIGIO, 0);
314 if (sb->sb_flags & SB_UPCALL)
315 (*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT);
316 if (sb->sb_flags & SB_AIO)
318 KNOTE(&selinfo->si_note, 0);
319 if (sb->sb_flags & SB_MEVENT) {
320 struct netmsg_so_notify *msg, *nmsg;
322 TAILQ_FOREACH_MUTABLE(msg, &selinfo->si_mlist, nm_list, nmsg) {
323 if (msg->nm_predicate((struct netmsg *)msg)) {
324 TAILQ_REMOVE(&selinfo->si_mlist, msg, nm_list);
325 lwkt_replymsg(&msg->nm_lmsg,
326 msg->nm_lmsg.ms_error);
329 if (TAILQ_EMPTY(&sb->sb_sel.si_mlist))
330 sb->sb_flags &= ~SB_MEVENT;
335 * Socket buffer (struct sockbuf) utility routines.
337 * Each socket contains two socket buffers: one for sending data and
338 * one for receiving data. Each buffer contains a queue of mbufs,
339 * information about the number of mbufs and amount of data in the
340 * queue, and other fields allowing select() statements and notification
341 * on data availability to be implemented.
343 * Data stored in a socket buffer is maintained as a list of records.
344 * Each record is a list of mbufs chained together with the m_next
345 * field. Records are chained together with the m_nextpkt field. The upper
346 * level routine soreceive() expects the following conventions to be
347 * observed when placing information in the receive buffer:
349 * 1. If the protocol requires each message be preceded by the sender's
350 * name, then a record containing that name must be present before
351 * any associated data (mbuf's must be of type MT_SONAME).
352 * 2. If the protocol supports the exchange of ``access rights'' (really
353 * just additional data associated with the message), and there are
354 * ``rights'' to be received, then a record containing this data
355 * should be present (mbuf's must be of type MT_RIGHTS).
356 * 3. If a name or rights record exists, then it must be followed by
357 * a data record, perhaps of zero length.
359 * Before using a new socket structure it is first necessary to reserve
360 * buffer space to the socket, by calling sbreserve(). This should commit
361 * some of the available buffer space in the system buffer pool for the
362 * socket (currently, it does nothing but enforce limits). The space
363 * should be released by calling sbrelease() when the socket is destroyed.
367 soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl)
369 if (sbreserve(&so->so_snd, sndcc, so, rl) == 0)
371 if (sbreserve(&so->so_rcv, rcvcc, so, rl) == 0)
373 if (so->so_rcv.sb_lowat == 0)
374 so->so_rcv.sb_lowat = 1;
375 if (so->so_snd.sb_lowat == 0)
376 so->so_snd.sb_lowat = MCLBYTES;
377 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
378 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
381 sbrelease(&so->so_snd, so);
387 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
390 u_long old_sb_max = sb_max;
392 error = SYSCTL_OUT(req, arg1, sizeof(int));
393 if (error || !req->newptr)
395 error = SYSCTL_IN(req, arg1, sizeof(int));
398 if (sb_max < MSIZE + MCLBYTES) {
402 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
407 * Allot mbufs to a sockbuf.
408 * Attempt to scale mbmax so that mbcnt doesn't become limiting
409 * if buffering efficiency is near the normal case.
412 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, struct rlimit *rl)
416 * rl will only be NULL when we're in an interrupt (eg, in tcp_input)
417 * or when called from netgraph (ie, ngd_attach)
421 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
422 rl ? rl->rlim_cur : RLIM_INFINITY)) {
425 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
426 if (sb->sb_lowat > sb->sb_hiwat)
427 sb->sb_lowat = sb->sb_hiwat;
432 * Free mbufs held by a socket, and reserved mbuf space.
441 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
447 * Routines to add and remove
448 * data from an mbuf queue.
450 * The routines sbappend() or sbappendrecord() are normally called to
451 * append new mbufs to a socket buffer, after checking that adequate
452 * space is available, comparing the function sbspace() with the amount
453 * of data to be added. sbappendrecord() differs from sbappend() in
454 * that data supplied is treated as the beginning of a new record.
455 * To place a sender's address, optional access rights, and data in a
456 * socket receive buffer, sbappendaddr() should be used. To place
457 * access rights and data in a socket receive buffer, sbappendrights()
458 * should be used. In either case, the new data begins a new record.
459 * Note that unlike sbappend() and sbappendrecord(), these routines check
460 * for the caller that there will be enough space to store the data.
461 * Each fails if there is not enough space, or if it cannot find mbufs
462 * to store additional information in.
464 * Reliable protocols may use the socket send buffer to hold data
465 * awaiting acknowledgement. Data is normally copied from a socket
466 * send buffer in a protocol with m_copy for output to a peer,
467 * and then removing the data from the socket buffer with sbdrop()
468 * or sbdroprecord() when the data is acknowledged by the peer.
472 * Append mbuf chain m to the last record in the
473 * socket buffer sb. The additional space associated
474 * the mbuf chain is recorded in sb. Empty mbufs are
475 * discarded and mbufs are compacted where possible.
478 sbappend(struct sockbuf *sb, struct mbuf *m)
489 if (n->m_flags & M_EOR) {
490 sbappendrecord(sb, m); /* XXXXXX!!!! */
493 } while (n->m_next && (n = n->m_next));
495 sbcompress(sb, m, n);
497 sb->sb_lastrecord = sb->sb_mb;
501 * sbappendstream() is an optimized form of sbappend() for protocols
502 * such as TCP that only have one record in the socket buffer, are
503 * not PR_ATOMIC, nor allow MT_CONTROL data. A protocol that uses
504 * sbappendstream() must use sbappendstream() exclusively.
507 sbappendstream(struct sockbuf *sb, struct mbuf *m)
509 KKASSERT(m->m_nextpkt == NULL);
510 sbcompress(sb, m, sb->sb_lastmbuf);
520 u_long len = 0, mbcnt = 0;
522 for (m = sb->sb_mb; m; m = n) {
524 for (; m; m = m->m_next) {
527 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
528 mbcnt += m->m_ext.ext_size;
531 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
532 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
533 mbcnt, sb->sb_mbcnt);
540 * Same as sbappend(), except the mbuf chain begins a new record.
543 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
545 struct mbuf *firstmbuf;
546 struct mbuf *secondmbuf;
552 * Break the first mbuf off from the rest of the mbuf chain.
555 secondmbuf = m0->m_next;
559 * Insert the first mbuf of the m0 mbuf chain as the last record of
560 * the sockbuf. Note this permits zero length records!
562 if (sb->sb_mb == NULL)
563 sb->sb_mb = firstmbuf;
565 sb->sb_lastrecord->m_nextpkt = firstmbuf;
566 sb->sb_lastrecord = firstmbuf; /* update hint for new last record */
568 if ((firstmbuf->m_flags & M_EOR) && (secondmbuf != NULL)) {
569 /* propagate the EOR flag */
570 firstmbuf->m_flags &= ~M_EOR;
571 secondmbuf->m_flags |= M_EOR;
575 * The succeeding call to sbcompress() omits accounting for
576 * the first mbuf, so do it here.
578 sballoc(sb, firstmbuf);
580 /* Compact the rest of the mbuf chain in after the first mbuf. */
581 sbcompress(sb, secondmbuf, firstmbuf);
585 * As above except that OOB data is inserted at the beginning of the sockbuf,
586 * but after any other OOB data.
589 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
596 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
602 continue; /* WANT next train */
607 goto again; /* inspect THIS train further */
612 * Put the first mbuf on the queue.
613 * Note this permits zero length records.
618 if (m0->m_nextpkt == NULL)
619 sb->sb_lastrecord = m0;
623 if (m && (m0->m_flags & M_EOR)) {
624 m0->m_flags &= ~M_EOR;
627 sbcompress(sb, m, m0);
631 * Append address and data, and optionally, control (ancillary) data
632 * to the receive queue of a socket. If present,
633 * m0 must include a packet header with total length.
634 * Returns 0 if no space in sockbuf or insufficient mbufs.
637 sbappendaddr(sb, asa, m0, control)
639 const struct sockaddr *asa;
640 struct mbuf *m0, *control;
643 int space = asa->sa_len;
645 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
646 panic("sbappendaddr");
649 space += m0->m_pkthdr.len;
650 for (n = control; n; n = n->m_next) {
652 if (n->m_next == 0) /* keep pointer to last control buf */
655 if (space > sbspace(sb))
657 if (asa->sa_len > MLEN)
659 MGET(m, MB_DONTWAIT, MT_SONAME);
662 m->m_len = asa->sa_len;
663 bcopy(asa, mtod(m, caddr_t), asa->sa_len);
665 n->m_next = m0; /* concatenate data to control */
669 for (n = m; n; n = n->m_next)
672 if (sb->sb_mb == NULL)
675 sb->sb_lastrecord->m_nextpkt = m;
676 sb->sb_lastrecord = m;
682 * Append control information followed by data.
683 * control must be non-null.
686 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
689 u_int length, cmbcnt, m0mbcnt;
691 KASSERT(control != NULL, ("sbappendcontrol"));
693 length = m_countm(control, &n, &cmbcnt) + m_countm(m0, NULL, &m0mbcnt);
694 if (length > sbspace(sb))
697 n->m_next = m0; /* concatenate data to control */
699 if (sb->sb_mb == NULL)
702 sb->sb_lastrecord->m_nextpkt = control;
703 sb->sb_lastrecord = control;
706 sb->sb_mbcnt += cmbcnt + m0mbcnt;
712 * Compress mbuf chain m into the socket buffer sb following mbuf tailm.
713 * If tailm is null, the buffer is presumed empty. Also, as a side-effect,
714 * increment the sockbuf counts for each mbuf in the chain.
717 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *tailm)
724 eor |= m->m_flags & M_EOR;
726 * Disregard empty mbufs as long as we don't encounter
727 * an end-of-record or there is a trailing mbuf of
728 * the same type to propagate the EOR flag to.
732 (((o = m->m_next) || (o = tailm)) &&
733 o->m_type == m->m_type))) {
738 /* See if we can coalesce with preceding mbuf. */
739 if (tailm && !(tailm->m_flags & M_EOR) && M_WRITABLE(tailm) &&
740 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
741 m->m_len <= M_TRAILINGSPACE(tailm) &&
742 tailm->m_type == m->m_type) {
743 bcopy(mtod(m, caddr_t),
744 mtod(tailm, caddr_t) + tailm->m_len,
746 tailm->m_len += m->m_len;
747 sb->sb_cc += m->m_len; /* update sb counter */
752 /* Insert whole mbuf. */
754 KASSERT(sb->sb_mb == NULL,
755 ("sbcompress: sb_mb not NULL"));
756 sb->sb_mb = m; /* put at front of sockbuf */
758 tailm->m_next = m; /* tack m on following tailm */
760 sb->sb_lastmbuf = m; /* update last mbuf hint */
762 tailm = m; /* just inserted mbuf becomes the new tail */
763 m = m->m_next; /* advance to next mbuf */
764 tailm->m_next = NULL; /* split inserted mbuf off from chain */
766 /* update sb counters for just added mbuf */
769 /* clear EOR on intermediate mbufs */
770 tailm->m_flags &= ~M_EOR;
775 tailm->m_flags |= eor; /* propagate EOR to last mbuf */
777 printf("semi-panic: sbcompress");
782 * Free all mbufs in a sockbuf.
783 * Check that all resources are reclaimed.
790 if (sb->sb_flags & SB_LOCK)
791 panic("sbflush: locked");
792 while (sb->sb_mbcnt) {
794 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
795 * we would loop forever. Panic instead.
797 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
799 sbdrop(sb, (int)sb->sb_cc);
801 KASSERT(!(sb->sb_cc || sb->sb_mb || sb->sb_mbcnt || sb->sb_lastmbuf),
802 ("sbflush: cc %ld || mb %p || mbcnt %ld || lastmbuf %p",
803 sb->sb_cc, sb->sb_mb, sb->sb_mbcnt, sb->sb_lastmbuf));
807 * Drop data from (the front of) a sockbuf.
817 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
826 if (m->m_len > len) {
836 while (m && m->m_len == 0) {
845 sb->sb_lastmbuf = NULL;
850 * Drop a record off the front of a sockbuf
851 * and move the next record to the front.
861 sb->sb_mb = m->m_nextpkt;
870 * Create a "control" mbuf containing the specified data
871 * with the specified type for presentation on a socket buffer.
874 sbcreatecontrol(p, size, type, level)
882 if (CMSG_SPACE((u_int)size) > MCLBYTES)
883 return ((struct mbuf *) NULL);
884 if ((m = m_get(MB_DONTWAIT, MT_CONTROL)) == NULL)
885 return ((struct mbuf *) NULL);
886 if (CMSG_SPACE((u_int)size) > MLEN) {
887 MCLGET(m, MB_DONTWAIT);
888 if ((m->m_flags & M_EXT) == 0) {
890 return ((struct mbuf *) NULL);
893 cp = mtod(m, struct cmsghdr *);
895 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
896 ("sbcreatecontrol: short mbuf"));
898 (void)memcpy(CMSG_DATA(cp), p, size);
899 m->m_len = CMSG_SPACE(size);
900 cp->cmsg_len = CMSG_LEN(size);
901 cp->cmsg_level = level;
902 cp->cmsg_type = type;
907 * Some routines that return EOPNOTSUPP for entry points that are not
908 * supported by a protocol. Fill in as needed.
911 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
917 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
923 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
929 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
930 struct ifnet *ifp, struct thread *td)
936 pru_listen_notsupp(struct socket *so, struct thread *td)
942 pru_rcvd_notsupp(struct socket *so, int flags)
948 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
954 * This isn't really a ``null'' operation, but it's the default one
955 * and doesn't do anything destructive.
958 pru_sense_null(struct socket *so, struct stat *sb)
960 sb->st_blksize = so->so_snd.sb_hiwat;
965 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. Callers
966 * of this routine assume that it always succeeds, so we have to use a
967 * blockable allocation even though we might be called from a critical thread.
970 dup_sockaddr(const struct sockaddr *sa)
972 struct sockaddr *sa2;
974 sa2 = malloc(sa->sa_len, M_SONAME, M_INTWAIT);
975 bcopy(sa, sa2, sa->sa_len);
980 * Create an external-format (``xsocket'') structure using the information
981 * in the kernel-format socket structure pointed to by so. This is done
982 * to reduce the spew of irrelevant information over this interface,
983 * to isolate user code from changes in the kernel structure, and
984 * potentially to provide information-hiding if we decide that
985 * some of this information should be hidden from users.
988 sotoxsocket(struct socket *so, struct xsocket *xso)
990 xso->xso_len = sizeof *xso;
992 xso->so_type = so->so_type;
993 xso->so_options = so->so_options;
994 xso->so_linger = so->so_linger;
995 xso->so_state = so->so_state;
996 xso->so_pcb = so->so_pcb;
997 xso->xso_protocol = so->so_proto->pr_protocol;
998 xso->xso_family = so->so_proto->pr_domain->dom_family;
999 xso->so_qlen = so->so_qlen;
1000 xso->so_incqlen = so->so_incqlen;
1001 xso->so_qlimit = so->so_qlimit;
1002 xso->so_timeo = so->so_timeo;
1003 xso->so_error = so->so_error;
1004 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
1005 xso->so_oobmark = so->so_oobmark;
1006 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1007 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1008 xso->so_uid = so->so_cred->cr_uid;
1012 * This does the same for sockbufs. Note that the xsockbuf structure,
1013 * since it is always embedded in a socket, does not include a self
1014 * pointer nor a length. We make this entry point public in case
1015 * some other mechanism needs it.
1018 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1020 xsb->sb_cc = sb->sb_cc;
1021 xsb->sb_hiwat = sb->sb_hiwat;
1022 xsb->sb_mbcnt = sb->sb_mbcnt;
1023 xsb->sb_mbmax = sb->sb_mbmax;
1024 xsb->sb_lowat = sb->sb_lowat;
1025 xsb->sb_flags = sb->sb_flags;
1026 xsb->sb_timeo = sb->sb_timeo;
1030 * Here is the definition of some of the basic objects in the kern.ipc
1031 * branch of the MIB.
1033 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1035 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1037 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1038 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
1039 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
1040 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
1041 &maxsockets, 0, "Maximum number of sockets avaliable");
1042 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1043 &sb_efficiency, 0, "");
1046 * Initialise maxsockets
1048 static void init_maxsockets(void *ignored)
1050 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1051 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1053 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);