]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/domain.h>
62 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/signalvar.h>
71 #include <sys/sysctl.h>
75 * Primitive routines for operating on sockets and socket buffers
78 u_long sb_max
= SB_MAX
; /* XXX should be static */
80 static u_long sb_efficiency
= 8; /* parameter for sbreserve() */
82 char netcon
[] = "netcon";
85 * Procedures to manipulate state flags of socket
86 * and do appropriate wakeups. Normal sequence from the
87 * active (originating) side is that soisconnecting() is
88 * called during processing of connect() call,
89 * resulting in an eventual call to soisconnected() if/when the
90 * connection is established. When the connection is torn down
91 * soisdisconnecting() is called during processing of disconnect() call,
92 * and soisdisconnected() is called when the connection to the peer
93 * is totally severed. The semantics of these routines are such that
94 * connectionless protocols can call soisconnected() and soisdisconnected()
95 * only, bypassing the in-progress calls when setting up a ``connection''
98 * From the passive side, a socket is created with
99 * two queues of sockets: so_incomp for connections in progress
100 * and so_comp for connections already made and awaiting user acceptance.
101 * As a protocol is preparing incoming connections, it creates a socket
102 * structure queued on so_incomp by calling sonewconn(). When the connection
103 * is established, soisconnected() is called, and transfers the
104 * socket structure to so_comp, making it available to accept().
106 * If a socket is closed with sockets on either
107 * so_incomp or so_comp, these sockets are dropped.
109 * If higher level protocols are implemented in
110 * the kernel, the wakeups done here will sometimes
111 * cause software-interrupt process scheduling.
116 register struct socket
*so
;
119 so
->so_state
&= ~(SS_ISCONNECTED
|SS_ISDISCONNECTING
);
120 so
->so_state
|= SS_ISCONNECTING
;
125 register struct socket
*so
;
126 { register struct kextcb
*kp
;
127 register struct socket
*head
= so
->so_head
;
131 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisconnected
)
132 { if ((*kp
->e_soif
->sf_soisconnected
)(so
, kp
))
138 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISDISCONNECTING
|SS_ISCONFIRMING
);
139 so
->so_state
|= SS_ISCONNECTED
;
140 if (head
&& (so
->so_state
& SS_INCOMP
)) {
141 postevent(head
,0,EV_RCONN
);
142 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
144 so
->so_state
&= ~SS_INCOMP
;
145 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
146 so
->so_state
|= SS_COMP
;
148 wakeup((caddr_t
)&head
->so_timeo
);
150 postevent(so
,0,EV_WCONN
);
151 wakeup((caddr_t
)&so
->so_timeo
);
158 soisdisconnecting(so
)
159 register struct socket
*so
;
160 { register struct kextcb
*kp
;
164 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnecting
)
165 { if ((*kp
->e_soif
->sf_soisdisconnecting
)(so
, kp
))
171 so
->so_state
&= ~SS_ISCONNECTING
;
172 so
->so_state
|= (SS_ISDISCONNECTING
|SS_CANTRCVMORE
|SS_CANTSENDMORE
);
173 wakeup((caddr_t
)&so
->so_timeo
);
180 register struct socket
*so
;
181 { register struct kextcb
*kp
;
185 { if (kp
->e_soif
&& kp
->e_soif
->sf_soisdisconnected
)
186 { if ((*kp
->e_soif
->sf_soisdisconnected
)(so
, kp
))
192 so
->so_state
&= ~(SS_ISCONNECTING
|SS_ISCONNECTED
|SS_ISDISCONNECTING
);
193 so
->so_state
|= (SS_CANTRCVMORE
|SS_CANTSENDMORE
);
194 wakeup((caddr_t
)&so
->so_timeo
);
200 * Return a random connection that hasn't been serviced yet and
201 * is eligible for discard. There is a one in qlen chance that
202 * we will return a null, saying that there are no dropable
203 * requests. In this case, the protocol specific code should drop
204 * the new request. This insures fairness.
206 * This may be used in conjunction with protocol specific queue
207 * congestion routines.
211 register struct socket
*head
;
213 register struct socket
*so
;
214 unsigned int i
, j
, qlen
;
216 static struct timeval old_runtime
;
217 static unsigned int cur_cnt
, old_cnt
;
221 if ((i
= (tv
.tv_sec
- old_runtime
.tv_sec
)) != 0) {
223 old_cnt
= cur_cnt
/ i
;
227 so
= TAILQ_FIRST(&head
->so_incomp
);
231 qlen
= head
->so_incqlen
;
232 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
233 rnd
= (314159 * rnd
+ 66329) & 0xffff;
234 j
= ((qlen
+ 1) * rnd
) >> 16;
237 so
= TAILQ_NEXT(so
, so_list
);
244 * When an attempt at a new connection is noted on a socket
245 * which accepts connections, sonewconn is called. If the
246 * connection is possible (subject to space constraints, etc.)
247 * then we allocate a new structure, propoerly linked into the
248 * data structure of the original socket, and return this.
249 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
252 sonewconn(head
, connstatus
)
253 register struct socket
*head
;
256 register struct socket
*so
;
257 register struct kextcb
*kp
;
259 if (head
->so_qlen
> 3 * head
->so_qlimit
/ 2)
260 return ((struct socket
*)0);
261 so
= soalloc(0, head
->so_proto
->pr_domain
->dom_family
, head
->so_type
);
263 return ((struct socket
*)0);
267 { if (kp
->e_soif
&& kp
->e_soif
->sf_sonewconn1
)
268 { if ((*kp
->e_soif
->sf_sonewconn1
)(so
, connstatus
, kp
))
275 so
->so_type
= head
->so_type
;
276 so
->so_options
= head
->so_options
&~ SO_ACCEPTCONN
;
277 so
->so_linger
= head
->so_linger
;
278 so
->so_state
= head
->so_state
| SS_NOFDREF
;
279 so
->so_proto
= head
->so_proto
;
280 so
->so_timeo
= head
->so_timeo
;
281 so
->so_pgid
= head
->so_pgid
;
282 so
->so_uid
= head
->so_uid
;
283 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
284 (void) soreserve(so
, head
->so_snd
.sb_hiwat
, head
->so_rcv
.sb_hiwat
);
286 if (so
->so_proto
->pr_sfilter
.tqh_first
)
287 error
= sfilter_init(so
);
288 if (error
== 0 && (*so
->so_proto
->pr_usrreqs
->pru_attach
)(so
, 0, NULL
)) {
291 return ((struct socket
*)0);
293 so
->so_proto
->pr_domain
->dom_refs
++;
296 TAILQ_INSERT_TAIL(&head
->so_comp
, so
, so_list
);
297 so
->so_state
|= SS_COMP
;
299 TAILQ_INSERT_TAIL(&head
->so_incomp
, so
, so_list
);
300 so
->so_state
|= SS_INCOMP
;
306 wakeup((caddr_t
)&head
->so_timeo
);
307 so
->so_state
|= connstatus
;
309 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
310 TAILQ_INIT(&so
->so_evlist
);
315 * Socantsendmore indicates that no more data will be sent on the
316 * socket; it would normally be applied to a socket when the user
317 * informs the system that no more data is to be sent, by the protocol
318 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
319 * will be received, and will normally be applied to the socket by a
320 * protocol when it detects that the peer will send no more data.
321 * Data queued for reading in the socket may yet be read.
327 { register struct kextcb
*kp
;
331 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantsendmore
)
332 { if ((*kp
->e_soif
->sf_socantsendmore
)(so
, kp
))
339 so
->so_state
|= SS_CANTSENDMORE
;
346 { register struct kextcb
*kp
;
350 { if (kp
->e_soif
&& kp
->e_soif
->sf_socantrcvmore
)
351 { if ((*kp
->e_soif
->sf_socantrcvmore
)(so
, kp
))
358 so
->so_state
|= SS_CANTRCVMORE
;
363 * Wait for data to arrive at/drain from a socket buffer.
370 sb
->sb_flags
|= SB_WAIT
;
371 return (tsleep((caddr_t
)&sb
->sb_cc
,
372 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
| PCATCH
, "sbwait",
377 * Lock a sockbuf already known to be locked;
378 * return any error returned from sleep (EINTR).
382 register struct sockbuf
*sb
;
386 while (sb
->sb_flags
& SB_LOCK
) {
387 sb
->sb_flags
|= SB_WANT
;
388 error
= tsleep((caddr_t
)&sb
->sb_flags
,
389 (sb
->sb_flags
& SB_NOINTR
) ? PSOCK
: PSOCK
|PCATCH
,
394 sb
->sb_flags
|= SB_LOCK
;
399 * Wakeup processes waiting on a socket buffer.
400 * Do asynchronous notification via SIGIO
401 * if the socket has the SS_ASYNC flag set.
405 register struct socket
*so
;
406 register struct sockbuf
*sb
;
408 struct proc
*p
= current_proc();
412 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
413 sb
->sb_sel
.si_flags
&= ~SI_SBSEL
;
414 selwakeup(&sb
->sb_sel
);
415 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
417 if (sb
->sb_flags
& SB_WAIT
) {
418 sb
->sb_flags
&= ~SB_WAIT
;
419 wakeup((caddr_t
)&sb
->sb_cc
);
421 if (so
->so_state
& SS_ASYNC
) {
423 gsignal(-so
->so_pgid
, SIGIO
);
424 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
428 if (sb
->sb_flags
& SB_UPCALL
)
429 (*so
->so_upcall
)(so
, so
->so_upcallarg
, M_DONTWAIT
);
433 * Socket buffer (struct sockbuf) utility routines.
435 * Each socket contains two socket buffers: one for sending data and
436 * one for receiving data. Each buffer contains a queue of mbufs,
437 * information about the number of mbufs and amount of data in the
438 * queue, and other fields allowing select() statements and notification
439 * on data availability to be implemented.
441 * Data stored in a socket buffer is maintained as a list of records.
442 * Each record is a list of mbufs chained together with the m_next
443 * field. Records are chained together with the m_nextpkt field. The upper
444 * level routine soreceive() expects the following conventions to be
445 * observed when placing information in the receive buffer:
447 * 1. If the protocol requires each message be preceded by the sender's
448 * name, then a record containing that name must be present before
449 * any associated data (mbuf's must be of type MT_SONAME).
450 * 2. If the protocol supports the exchange of ``access rights'' (really
451 * just additional data associated with the message), and there are
452 * ``rights'' to be received, then a record containing this data
453 * should be present (mbuf's must be of type MT_RIGHTS).
454 * 3. If a name or rights record exists, then it must be followed by
455 * a data record, perhaps of zero length.
457 * Before using a new socket structure it is first necessary to reserve
458 * buffer space to the socket, by calling sbreserve(). This should commit
459 * some of the available buffer space in the system buffer pool for the
460 * socket (currently, it does nothing but enforce limits). The space
461 * should be released by calling sbrelease() when the socket is destroyed.
465 soreserve(so
, sndcc
, rcvcc
)
466 register struct socket
*so
;
469 register struct kextcb
*kp
;
473 { if (kp
->e_soif
&& kp
->e_soif
->sf_soreserve
)
474 { if ((*kp
->e_soif
->sf_soreserve
)(so
, sndcc
, rcvcc
, kp
))
480 if (sbreserve(&so
->so_snd
, sndcc
) == 0)
482 if (sbreserve(&so
->so_rcv
, rcvcc
) == 0)
484 if (so
->so_rcv
.sb_lowat
== 0)
485 so
->so_rcv
.sb_lowat
= 1;
486 if (so
->so_snd
.sb_lowat
== 0)
487 so
->so_snd
.sb_lowat
= MCLBYTES
;
488 if (so
->so_snd
.sb_lowat
> so
->so_snd
.sb_hiwat
)
489 so
->so_snd
.sb_lowat
= so
->so_snd
.sb_hiwat
;
492 sbrelease(&so
->so_snd
);
498 * Allot mbufs to a sockbuf.
499 * Attempt to scale mbmax so that mbcnt doesn't become limiting
500 * if buffering efficiency is near the normal case.
507 if ((u_quad_t
)cc
> (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
))
510 sb
->sb_mbmax
= min(cc
* sb_efficiency
, sb_max
);
511 if (sb
->sb_lowat
> sb
->sb_hiwat
)
512 sb
->sb_lowat
= sb
->sb_hiwat
;
517 * Free mbufs held by a socket, and reserved mbuf space.
525 sb
->sb_hiwat
= sb
->sb_mbmax
= 0;
528 int oldpri
= splimp();
529 selthreadclear(&sb
->sb_sel
);
535 * Routines to add and remove
536 * data from an mbuf queue.
538 * The routines sbappend() or sbappendrecord() are normally called to
539 * append new mbufs to a socket buffer, after checking that adequate
540 * space is available, comparing the function sbspace() with the amount
541 * of data to be added. sbappendrecord() differs from sbappend() in
542 * that data supplied is treated as the beginning of a new record.
543 * To place a sender's address, optional access rights, and data in a
544 * socket receive buffer, sbappendaddr() should be used. To place
545 * access rights and data in a socket receive buffer, sbappendrights()
546 * should be used. In either case, the new data begins a new record.
547 * Note that unlike sbappend() and sbappendrecord(), these routines check
548 * for the caller that there will be enough space to store the data.
549 * Each fails if there is not enough space, or if it cannot find mbufs
550 * to store additional information in.
552 * Reliable protocols may use the socket send buffer to hold data
553 * awaiting acknowledgement. Data is normally copied from a socket
554 * send buffer in a protocol with m_copy for output to a peer,
555 * and then removing the data from the socket buffer with sbdrop()
556 * or sbdroprecord() when the data is acknowledged by the peer.
560 * Append mbuf chain m to the last record in the
561 * socket buffer sb. The additional space associated
562 * the mbuf chain is recorded in sb. Empty mbufs are
563 * discarded and mbufs are compacted where possible.
569 { register struct kextcb
*kp
;
570 register struct mbuf
*n
;
574 kp
= sotokextcb(sbtoso(sb
));
576 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappend
)
577 { if ((*kp
->e_sout
->su_sbappend
)(sb
, m
, kp
))
587 if (n
->m_flags
& M_EOR
) {
588 sbappendrecord(sb
, m
); /* XXXXXX!!!! */
591 } while (n
->m_next
&& (n
= n
->m_next
));
593 sbcompress(sb
, m
, n
);
599 register struct sockbuf
*sb
;
601 register struct mbuf
*m
;
602 register struct mbuf
*n
= 0;
603 register u_long len
= 0, mbcnt
= 0;
605 for (m
= sb
->sb_mb
; m
; m
= n
) {
607 for (; m
; m
= m
->m_next
) {
610 if (m
->m_flags
& M_EXT
) /*XXX*/ /* pretty sure this is bogus */
611 mbcnt
+= m
->m_ext
.ext_size
;
613 panic("sbcheck nextpkt");
615 if (len
!= sb
->sb_cc
|| mbcnt
!= sb
->sb_mbcnt
) {
616 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len
, sb
->sb_cc
,
617 mbcnt
, sb
->sb_mbcnt
);
624 * As above, except the mbuf chain
625 * begins a new record.
628 sbappendrecord(sb
, m0
)
629 register struct sockbuf
*sb
;
630 register struct mbuf
*m0
;
632 register struct mbuf
*m
;
633 register struct kextcb
*kp
;
638 kp
= sotokextcb(sbtoso(sb
));
640 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendrecord
)
641 { if ((*kp
->e_sout
->su_sbappendrecord
)(sb
, m0
, kp
))
652 * Put the first mbuf on the queue.
653 * Note this permits zero length records.
662 if (m
&& (m0
->m_flags
& M_EOR
)) {
663 m0
->m_flags
&= ~M_EOR
;
666 sbcompress(sb
, m
, m0
);
670 * As above except that OOB data
671 * is inserted at the beginning of the sockbuf,
672 * but after any other OOB data.
676 register struct sockbuf
*sb
;
677 register struct mbuf
*m0
;
679 register struct mbuf
*m
;
680 register struct mbuf
**mp
;
681 register struct kextcb
*kp
;
686 kp
= sotokextcb(sbtoso(sb
));
688 { if (kp
->e_sout
&& kp
->e_sout
->su_sbinsertoob
)
689 { if ((*kp
->e_sout
->su_sbinsertoob
)(sb
, m0
, kp
))
695 for (mp
= &sb
->sb_mb
; *mp
; mp
= &((*mp
)->m_nextpkt
)) {
701 continue; /* WANT next train */
706 goto again
; /* inspect THIS train further */
711 * Put the first mbuf on the queue.
712 * Note this permits zero length records.
719 if (m
&& (m0
->m_flags
& M_EOR
)) {
720 m0
->m_flags
&= ~M_EOR
;
723 sbcompress(sb
, m
, m0
);
727 * Append address and data, and optionally, control (ancillary) data
728 * to the receive queue of a socket. If present,
729 * m0 must include a packet header with total length.
730 * Returns 0 if no space in sockbuf or insufficient mbufs.
733 sbappendaddr(sb
, asa
, m0
, control
)
734 register struct sockbuf
*sb
;
735 struct sockaddr
*asa
;
736 struct mbuf
*m0
, *control
;
738 register struct mbuf
*m
, *n
;
739 int space
= asa
->sa_len
;
740 register struct kextcb
*kp
;
742 if (m0
&& (m0
->m_flags
& M_PKTHDR
) == 0)
743 panic("sbappendaddr");
745 kp
= sotokextcb(sbtoso(sb
));
747 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendaddr
)
748 { if ((*kp
->e_sout
->su_sbappendaddr
)(sb
, asa
, m0
, control
, kp
))
755 space
+= m0
->m_pkthdr
.len
;
756 for (n
= control
; n
; n
= n
->m_next
) {
758 if (n
->m_next
== 0) /* keep pointer to last control buf */
761 if (space
> sbspace(sb
))
763 if (asa
->sa_len
> MLEN
)
765 MGET(m
, M_DONTWAIT
, MT_SONAME
);
768 m
->m_len
= asa
->sa_len
;
769 bcopy((caddr_t
)asa
, mtod(m
, caddr_t
), asa
->sa_len
);
771 n
->m_next
= m0
; /* concatenate data to control */
775 for (n
= m
; n
; n
= n
->m_next
)
784 postevent(0,sb
,EV_RWBYTES
);
789 sbappendcontrol(sb
, m0
, control
)
791 struct mbuf
*control
, *m0
;
793 register struct mbuf
*m
, *n
;
795 register struct kextcb
*kp
;
798 panic("sbappendcontrol");
800 kp
= sotokextcb(sbtoso(sb
));
802 { if (kp
->e_sout
&& kp
->e_sout
->su_sbappendcontrol
)
803 { if ((*kp
->e_sout
->su_sbappendcontrol
)(sb
, m0
, control
, kp
))
809 for (m
= control
; ; m
= m
->m_next
) {
814 n
= m
; /* save pointer to last control buffer */
815 for (m
= m0
; m
; m
= m
->m_next
)
817 if (space
> sbspace(sb
))
819 n
->m_next
= m0
; /* concatenate data to control */
820 for (m
= control
; m
; m
= m
->m_next
)
826 n
->m_nextpkt
= control
;
829 postevent(0,sb
,EV_RWBYTES
);
834 * Compress mbuf chain m into the socket
835 * buffer sb following mbuf n. If n
836 * is null, the buffer is presumed empty.
840 register struct sockbuf
*sb
;
841 register struct mbuf
*m
, *n
;
843 register int eor
= 0;
844 register struct mbuf
*o
;
847 eor
|= m
->m_flags
& M_EOR
;
850 (((o
= m
->m_next
) || (o
= n
)) &&
851 o
->m_type
== m
->m_type
))) {
855 if (n
&& (n
->m_flags
& (M_EXT
| M_EOR
)) == 0 &&
856 (n
->m_data
+ n
->m_len
+ m
->m_len
) < &n
->m_dat
[MLEN
] &&
857 n
->m_type
== m
->m_type
) {
858 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
) + n
->m_len
,
860 n
->m_len
+= m
->m_len
;
861 sb
->sb_cc
+= m
->m_len
;
871 m
->m_flags
&= ~M_EOR
;
879 printf("semi-panic: sbcompress\n");
881 postevent(0,sb
, EV_RWBYTES
);
885 * Free all mbufs in a sockbuf.
886 * Check that all resources are reclaimed.
890 register struct sockbuf
*sb
;
892 register struct kextcb
*kp
;
894 kp
= sotokextcb(sbtoso(sb
));
896 { if (kp
->e_sout
&& kp
->e_sout
->su_sbflush
)
897 { if ((*kp
->e_sout
->su_sbflush
)(sb
, kp
))
903 if (sb
->sb_flags
& SB_LOCK
)
904 panic("sbflush: locked");
905 while (sb
->sb_mbcnt
&& sb
->sb_cc
)
906 sbdrop(sb
, (int)sb
->sb_cc
);
907 if (sb
->sb_cc
|| sb
->sb_mb
|| sb
->sb_mbcnt
)
908 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb
->sb_cc
, (void *)sb
->sb_mb
, sb
->sb_mbcnt
);
909 postevent(0, sb
, EV_RWBYTES
);
913 * Drop data from (the front of) a sockbuf.
917 register struct sockbuf
*sb
;
920 register struct mbuf
*m
, *mn
;
922 register struct kextcb
*kp
;
924 kp
= sotokextcb(sbtoso(sb
));
926 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdrop
)
927 { if ((*kp
->e_sout
->su_sbdrop
)(sb
, len
, kp
))
933 next
= (m
= sb
->sb_mb
) ? m
->m_nextpkt
: 0;
942 if (m
->m_len
> len
) {
953 while (m
&& m
->m_len
== 0) {
963 postevent(0, sb
, EV_RWBYTES
);
967 * Drop a record off the front of a sockbuf
968 * and move the next record to the front.
972 register struct sockbuf
*sb
;
974 register struct mbuf
*m
, *mn
;
975 register struct kextcb
*kp
;
977 kp
= sotokextcb(sbtoso(sb
));
979 { if (kp
->e_sout
&& kp
->e_sout
->su_sbdroprecord
)
980 { if ((*kp
->e_sout
->su_sbdroprecord
)(sb
, kp
))
988 sb
->sb_mb
= m
->m_nextpkt
;
994 postevent(0, sb
, EV_RWBYTES
);
998 * Create a "control" mbuf containing the specified data
999 * with the specified type for presentation on a socket buffer.
1002 sbcreatecontrol(p
, size
, type
, level
)
1007 register struct cmsghdr
*cp
;
1010 if ((m
= m_get(M_DONTWAIT
, MT_CONTROL
)) == NULL
)
1011 return ((struct mbuf
*) NULL
);
1012 cp
= mtod(m
, struct cmsghdr
*);
1013 /* XXX check size? */
1014 (void)memcpy(CMSG_DATA(cp
), p
, size
);
1015 size
+= sizeof(*cp
);
1017 cp
->cmsg_len
= size
;
1018 cp
->cmsg_level
= level
;
1019 cp
->cmsg_type
= type
;
1024 * Some routines that return EOPNOTSUPP for entry points that are not
1025 * supported by a protocol. Fill in as needed.
1028 pru_abort_notsupp(struct socket
*so
)
1035 pru_accept_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1041 pru_attach_notsupp(struct socket
*so
, int proto
, struct proc
*p
)
1047 pru_bind_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1053 pru_connect_notsupp(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1059 pru_connect2_notsupp(struct socket
*so1
, struct socket
*so2
)
1065 pru_control_notsupp(struct socket
*so
, u_long cmd
, caddr_t data
,
1066 struct ifnet
*ifp
, struct proc
*p
)
1072 pru_detach_notsupp(struct socket
*so
)
1078 pru_disconnect_notsupp(struct socket
*so
)
1084 pru_listen_notsupp(struct socket
*so
, struct proc
*p
)
1090 pru_peeraddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1096 pru_rcvd_notsupp(struct socket
*so
, int flags
)
1102 pru_rcvoob_notsupp(struct socket
*so
, struct mbuf
*m
, int flags
)
1108 pru_send_notsupp(struct socket
*so
, int flags
, struct mbuf
*m
,
1109 struct sockaddr
*addr
, struct mbuf
*control
,
1118 * This isn't really a ``null'' operation, but it's the default one
1119 * and doesn't do anything destructive.
1122 pru_sense_null(struct socket
*so
, struct stat
*sb
)
1124 sb
->st_blksize
= so
->so_snd
.sb_hiwat
;
1129 int pru_sosend_notsupp(struct socket
*so
, struct sockaddr
*addr
,
1130 struct uio
*uio
, struct mbuf
*top
,
1131 struct mbuf
*control
, int flags
)
1137 int pru_soreceive_notsupp(struct socket
*so
,
1138 struct sockaddr
**paddr
,
1139 struct uio
*uio
, struct mbuf
**mp0
,
1140 struct mbuf
**controlp
, int *flagsp
)
1147 pru_shutdown_notsupp(struct socket
*so
)
1153 pru_sockaddr_notsupp(struct socket
*so
, struct sockaddr
**nam
)
1158 int pru_sosend(struct socket
*so
, struct sockaddr
*addr
,
1159 struct uio
*uio
, struct mbuf
*top
,
1160 struct mbuf
*control
, int flags
)
1165 int pru_soreceive(struct socket
*so
,
1166 struct sockaddr
**paddr
,
1167 struct uio
*uio
, struct mbuf
**mp0
,
1168 struct mbuf
**controlp
, int *flagsp
)
1174 int pru_sopoll_notsupp(struct socket
*so
, int events
,
1183 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1186 dup_sockaddr(sa
, canwait
)
1187 struct sockaddr
*sa
;
1190 struct sockaddr
*sa2
;
1192 MALLOC(sa2
, struct sockaddr
*, sa
->sa_len
, M_SONAME
,
1193 canwait
? M_WAITOK
: M_NOWAIT
);
1195 bcopy(sa
, sa2
, sa
->sa_len
);
1200 * Create an external-format (``xsocket'') structure using the information
1201 * in the kernel-format socket structure pointed to by so. This is done
1202 * to reduce the spew of irrelevant information over this interface,
1203 * to isolate user code from changes in the kernel structure, and
1204 * potentially to provide information-hiding if we decide that
1205 * some of this information should be hidden from users.
1208 sotoxsocket(struct socket
*so
, struct xsocket
*xso
)
1210 xso
->xso_len
= sizeof *xso
;
1212 xso
->so_type
= so
->so_type
;
1213 xso
->so_options
= so
->so_options
;
1214 xso
->so_linger
= so
->so_linger
;
1215 xso
->so_state
= so
->so_state
;
1216 xso
->so_pcb
= so
->so_pcb
;
1217 xso
->xso_protocol
= so
->so_proto
->pr_protocol
;
1218 xso
->xso_family
= so
->so_proto
->pr_domain
->dom_family
;
1219 xso
->so_qlen
= so
->so_qlen
;
1220 xso
->so_incqlen
= so
->so_incqlen
;
1221 xso
->so_qlimit
= so
->so_qlimit
;
1222 xso
->so_timeo
= so
->so_timeo
;
1223 xso
->so_error
= so
->so_error
;
1224 xso
->so_pgid
= so
->so_pgid
;
1225 xso
->so_oobmark
= so
->so_oobmark
;
1226 sbtoxsockbuf(&so
->so_snd
, &xso
->so_snd
);
1227 sbtoxsockbuf(&so
->so_rcv
, &xso
->so_rcv
);
1228 xso
->so_uid
= so
->so_uid
;
1232 * This does the same for sockbufs. Note that the xsockbuf structure,
1233 * since it is always embedded in a socket, does not include a self
1234 * pointer nor a length. We make this entry point public in case
1235 * some other mechanism needs it.
1238 sbtoxsockbuf(struct sockbuf
*sb
, struct xsockbuf
*xsb
)
1240 xsb
->sb_cc
= sb
->sb_cc
;
1241 xsb
->sb_hiwat
= sb
->sb_hiwat
;
1242 xsb
->sb_mbcnt
= sb
->sb_mbcnt
;
1243 xsb
->sb_mbmax
= sb
->sb_mbmax
;
1244 xsb
->sb_lowat
= sb
->sb_lowat
;
1245 xsb
->sb_flags
= sb
->sb_flags
;
1246 xsb
->sb_timeo
= sb
->sb_timeo
;
1250 * Here is the definition of some of the basic objects in the kern.ipc
1251 * branch of the MIB.
1255 SYSCTL_NODE(_kern
, KERN_IPC
, ipc
, CTLFLAG_RW
, 0, "IPC");
1257 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1259 SYSCTL_INT(_kern
, KERN_DUMMY
, dummy
, CTLFLAG_RW
, &dummy
, 0, "");
1261 SYSCTL_INT(_kern_ipc
, KIPC_MAXSOCKBUF
, maxsockbuf
, CTLFLAG_RW
, &sb_max
, 0, "");
1262 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxsockets
, CTLFLAG_RD
, &maxsockets
, 0, "");
1263 SYSCTL_INT(_kern_ipc
, KIPC_SOCKBUF_WASTE
, sockbuf_waste_factor
, CTLFLAG_RW
,
1264 &sb_efficiency
, 0, "");
1265 SYSCTL_INT(_kern_ipc
, KIPC_NMBCLUSTERS
, nmbclusters
, CTLFLAG_RD
, &nmbclusters
, 0, "");