]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/uipc_socket2.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / kern / uipc_socket2.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
26/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27/*
28 * Copyright (c) 1982, 1986, 1988, 1990, 1993
29 * The Regents of the University of California. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
9bccf70c 60 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
1c79356b
A
61 */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/domain.h>
66#include <sys/kernel.h>
67#include <sys/proc.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <sys/protosw.h>
71#include <sys/stat.h>
72#include <sys/socket.h>
73#include <sys/socketvar.h>
74#include <sys/signalvar.h>
75#include <sys/sysctl.h>
76#include <sys/ev.h>
77
fa4905b1
A
78#include <sys/kdebug.h>
79
80#define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
81#define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
82
83
1c79356b
A
84/*
85 * Primitive routines for operating on sockets and socket buffers
86 */
87
88u_long sb_max = SB_MAX; /* XXX should be static */
89
90static u_long sb_efficiency = 8; /* parameter for sbreserve() */
91
1c79356b
A
92/*
93 * Procedures to manipulate state flags of socket
94 * and do appropriate wakeups. Normal sequence from the
95 * active (originating) side is that soisconnecting() is
96 * called during processing of connect() call,
97 * resulting in an eventual call to soisconnected() if/when the
98 * connection is established. When the connection is torn down
9bccf70c 99 * soisdisconnecting() is called during processing of disconnect() call,
1c79356b
A
100 * and soisdisconnected() is called when the connection to the peer
101 * is totally severed. The semantics of these routines are such that
102 * connectionless protocols can call soisconnected() and soisdisconnected()
103 * only, bypassing the in-progress calls when setting up a ``connection''
104 * takes no time.
105 *
106 * From the passive side, a socket is created with
e3027f41
A
107 * two queues of sockets: so_incomp for connections in progress
108 * and so_comp for connections already made and awaiting user acceptance.
9bccf70c 109 * As a protocol is preparing incoming connections, it creates a socket
e3027f41 110 * structure queued on so_incomp by calling sonewconn(). When the connection
1c79356b 111 * is established, soisconnected() is called, and transfers the
e3027f41 112 * socket structure to so_comp, making it available to accept().
1c79356b 113 *
9bccf70c 114 * If a socket is closed with sockets on either
e3027f41 115 * so_incomp or so_comp, these sockets are dropped.
9bccf70c 116 *
1c79356b
A
117 * If higher level protocols are implemented in
118 * the kernel, the wakeups done here will sometimes
119 * cause software-interrupt process scheduling.
120 */
121
122void
123soisconnecting(so)
124 register struct socket *so;
125{
126
127 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
128 so->so_state |= SS_ISCONNECTING;
129}
130
131void
132soisconnected(so)
9bccf70c
A
133 struct socket *so;
134{
135 struct socket *head = so->so_head;
136 struct kextcb *kp;
1c79356b
A
137
138 kp = sotokextcb(so);
9bccf70c
A
139 while (kp) {
140 if (kp->e_soif && kp->e_soif->sf_soisconnected) {
141 if ((*kp->e_soif->sf_soisconnected)(so, kp))
1c79356b
A
142 return;
143 }
144 kp = kp->e_next;
145 }
146
147 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
148 so->so_state |= SS_ISCONNECTED;
149 if (head && (so->so_state & SS_INCOMP)) {
150 postevent(head,0,EV_RCONN);
151 TAILQ_REMOVE(&head->so_incomp, so, so_list);
152 head->so_incqlen--;
153 so->so_state &= ~SS_INCOMP;
154 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
155 so->so_state |= SS_COMP;
156 sorwakeup(head);
9bccf70c 157 wakeup_one(&head->so_timeo);
1c79356b
A
158 } else {
159 postevent(so,0,EV_WCONN);
160 wakeup((caddr_t)&so->so_timeo);
161 sorwakeup(so);
162 sowwakeup(so);
163 }
164}
165
166void
167soisdisconnecting(so)
168 register struct socket *so;
9bccf70c
A
169{
170 register struct kextcb *kp;
1c79356b
A
171
172 kp = sotokextcb(so);
9bccf70c
A
173 while (kp) {
174 if (kp->e_soif && kp->e_soif->sf_soisdisconnecting) {
175 if ((*kp->e_soif->sf_soisdisconnecting)(so, kp))
1c79356b
A
176 return;
177 }
178 kp = kp->e_next;
179 }
180
181 so->so_state &= ~SS_ISCONNECTING;
182 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
183 wakeup((caddr_t)&so->so_timeo);
184 sowwakeup(so);
185 sorwakeup(so);
186}
187
188void
189soisdisconnected(so)
190 register struct socket *so;
9bccf70c
A
191{
192 register struct kextcb *kp;
1c79356b
A
193
194 kp = sotokextcb(so);
9bccf70c
A
195 while (kp) {
196 if (kp->e_soif && kp->e_soif->sf_soisdisconnected) {
197 if ((*kp->e_soif->sf_soisdisconnected)(so, kp))
1c79356b
A
198 return;
199 }
200 kp = kp->e_next;
201 }
202
203 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
9bccf70c 204 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
1c79356b
A
205 wakeup((caddr_t)&so->so_timeo);
206 sowwakeup(so);
207 sorwakeup(so);
208}
209
210/*
211 * Return a random connection that hasn't been serviced yet and
212 * is eligible for discard. There is a one in qlen chance that
213 * we will return a null, saying that there are no dropable
214 * requests. In this case, the protocol specific code should drop
215 * the new request. This insures fairness.
216 *
217 * This may be used in conjunction with protocol specific queue
218 * congestion routines.
219 */
220struct socket *
221sodropablereq(head)
222 register struct socket *head;
223{
224 register struct socket *so;
225 unsigned int i, j, qlen;
226 static int rnd;
227 static struct timeval old_runtime;
228 static unsigned int cur_cnt, old_cnt;
229 struct timeval tv;
230
231 microtime(&tv);
232 if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
233 old_runtime = tv;
234 old_cnt = cur_cnt / i;
235 cur_cnt = 0;
236 }
237
238 so = TAILQ_FIRST(&head->so_incomp);
239 if (!so)
240 return (so);
241
242 qlen = head->so_incqlen;
243 if (++cur_cnt > qlen || old_cnt > qlen) {
244 rnd = (314159 * rnd + 66329) & 0xffff;
245 j = ((qlen + 1) * rnd) >> 16;
246
247 while (j-- && so)
248 so = TAILQ_NEXT(so, so_list);
249 }
250
251 return (so);
252}
253
254/*
255 * When an attempt at a new connection is noted on a socket
256 * which accepts connections, sonewconn is called. If the
257 * connection is possible (subject to space constraints, etc.)
258 * then we allocate a new structure, propoerly linked into the
259 * data structure of the original socket, and return this.
260 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
261 */
262struct socket *
263sonewconn(head, connstatus)
264 register struct socket *head;
265 int connstatus;
9bccf70c
A
266{
267 int error = 0;
1c79356b
A
268 register struct socket *so;
269 register struct kextcb *kp;
270
271 if (head->so_qlen > 3 * head->so_qlimit / 2)
272 return ((struct socket *)0);
0b4e3aa0 273 so = soalloc(1, head->so_proto->pr_domain->dom_family, head->so_type);
1c79356b
A
274 if (so == NULL)
275 return ((struct socket *)0);
9bccf70c
A
276 /* check if head was closed during the soalloc */
277 if (head->so_proto == NULL) {
278 sodealloc(so);
279 return ((struct socket *)0);
1c79356b
A
280 }
281
282 so->so_head = head;
283 so->so_type = head->so_type;
284 so->so_options = head->so_options &~ SO_ACCEPTCONN;
285 so->so_linger = head->so_linger;
286 so->so_state = head->so_state | SS_NOFDREF;
287 so->so_proto = head->so_proto;
288 so->so_timeo = head->so_timeo;
289 so->so_pgid = head->so_pgid;
290 so->so_uid = head->so_uid;
1c79356b 291
9bccf70c 292 /* Attach socket filters for this protocol */
1c79356b
A
293 if (so->so_proto->pr_sfilter.tqh_first)
294 error = sfilter_init(so);
9bccf70c
A
295 if (error != 0) {
296 sodealloc(so);
297 return ((struct socket *)0);
298 }
299
300 /* Call socket filters' sonewconn1 function if set */
301 kp = sotokextcb(so);
302 while (kp) {
303 if (kp->e_soif && kp->e_soif->sf_sonewconn) {
304 error = (int)(*kp->e_soif->sf_sonewconn)(so, connstatus, kp);
305 if (error == EJUSTRETURN) {
306 return so;
307 } else if (error != 0) {
308 sodealloc(so);
309 return NULL;
310 }
311 }
312 kp = kp->e_next;
313 }
314
315 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
316 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
1c79356b
A
317 sfilter_term(so);
318 sodealloc(so);
319 return ((struct socket *)0);
320 }
9bccf70c 321#ifdef __APPLE__
1c79356b 322 so->so_proto->pr_domain->dom_refs++;
9bccf70c 323#endif
1c79356b
A
324
325 if (connstatus) {
326 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
327 so->so_state |= SS_COMP;
328 } else {
329 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
330 so->so_state |= SS_INCOMP;
331 head->so_incqlen++;
332 }
333 head->so_qlen++;
334 if (connstatus) {
335 sorwakeup(head);
336 wakeup((caddr_t)&head->so_timeo);
337 so->so_state |= connstatus;
338 }
9bccf70c 339#ifdef __APPLE__
1c79356b
A
340 so->so_rcv.sb_so = so->so_snd.sb_so = so;
341 TAILQ_INIT(&so->so_evlist);
9bccf70c 342#endif
1c79356b
A
343 return (so);
344}
345
346/*
347 * Socantsendmore indicates that no more data will be sent on the
348 * socket; it would normally be applied to a socket when the user
349 * informs the system that no more data is to be sent, by the protocol
350 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
351 * will be received, and will normally be applied to the socket by a
352 * protocol when it detects that the peer will send no more data.
353 * Data queued for reading in the socket may yet be read.
354 */
355
356void
357socantsendmore(so)
358 struct socket *so;
9bccf70c
A
359{
360 register struct kextcb *kp;
361
1c79356b 362 kp = sotokextcb(so);
9bccf70c
A
363 while (kp) {
364 if (kp->e_soif && kp->e_soif->sf_socantsendmore) {
365 if ((*kp->e_soif->sf_socantsendmore)(so, kp))
1c79356b
A
366 return;
367 }
368 kp = kp->e_next;
369 }
370
371
372 so->so_state |= SS_CANTSENDMORE;
373 sowwakeup(so);
374}
375
376void
377socantrcvmore(so)
378 struct socket *so;
9bccf70c
A
379{
380 register struct kextcb *kp;
1c79356b
A
381
382 kp = sotokextcb(so);
9bccf70c
A
383 while (kp) {
384 if (kp->e_soif && kp->e_soif->sf_socantrcvmore) {
385 if ((*kp->e_soif->sf_socantrcvmore)(so, kp))
1c79356b
A
386 return;
387 }
388 kp = kp->e_next;
389 }
390
391
392 so->so_state |= SS_CANTRCVMORE;
393 sorwakeup(so);
394}
395
396/*
397 * Wait for data to arrive at/drain from a socket buffer.
398 */
399int
400sbwait(sb)
401 struct sockbuf *sb;
402{
403
404 sb->sb_flags |= SB_WAIT;
405 return (tsleep((caddr_t)&sb->sb_cc,
406 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
407 sb->sb_timeo));
408}
409
410/*
411 * Lock a sockbuf already known to be locked;
412 * return any error returned from sleep (EINTR).
413 */
414int
415sb_lock(sb)
416 register struct sockbuf *sb;
417{
418 int error;
419
420 while (sb->sb_flags & SB_LOCK) {
421 sb->sb_flags |= SB_WANT;
422 error = tsleep((caddr_t)&sb->sb_flags,
423 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
424 "sblock", 0);
425 if (error)
426 return (error);
427 }
428 sb->sb_flags |= SB_LOCK;
429 return (0);
430}
431
432/*
433 * Wakeup processes waiting on a socket buffer.
434 * Do asynchronous notification via SIGIO
435 * if the socket has the SS_ASYNC flag set.
436 */
437void
438sowakeup(so, sb)
439 register struct socket *so;
440 register struct sockbuf *sb;
441{
442 struct proc *p = current_proc();
9bccf70c
A
443 /* We clear the flag before calling selwakeup. */
444 /* BSD calls selwakeup then sets the flag */
0b4e3aa0 445 sb->sb_flags &= ~SB_SEL;
1c79356b 446 selwakeup(&sb->sb_sel);
1c79356b
A
447 if (sb->sb_flags & SB_WAIT) {
448 sb->sb_flags &= ~SB_WAIT;
449 wakeup((caddr_t)&sb->sb_cc);
450 }
451 if (so->so_state & SS_ASYNC) {
452 if (so->so_pgid < 0)
453 gsignal(-so->so_pgid, SIGIO);
454 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
455 psignal(p, SIGIO);
456 }
1c79356b
A
457 if (sb->sb_flags & SB_UPCALL)
458 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
459}
460
461/*
462 * Socket buffer (struct sockbuf) utility routines.
463 *
464 * Each socket contains two socket buffers: one for sending data and
465 * one for receiving data. Each buffer contains a queue of mbufs,
466 * information about the number of mbufs and amount of data in the
467 * queue, and other fields allowing select() statements and notification
468 * on data availability to be implemented.
469 *
470 * Data stored in a socket buffer is maintained as a list of records.
471 * Each record is a list of mbufs chained together with the m_next
472 * field. Records are chained together with the m_nextpkt field. The upper
473 * level routine soreceive() expects the following conventions to be
474 * observed when placing information in the receive buffer:
475 *
476 * 1. If the protocol requires each message be preceded by the sender's
477 * name, then a record containing that name must be present before
478 * any associated data (mbuf's must be of type MT_SONAME).
479 * 2. If the protocol supports the exchange of ``access rights'' (really
480 * just additional data associated with the message), and there are
481 * ``rights'' to be received, then a record containing this data
482 * should be present (mbuf's must be of type MT_RIGHTS).
483 * 3. If a name or rights record exists, then it must be followed by
484 * a data record, perhaps of zero length.
485 *
486 * Before using a new socket structure it is first necessary to reserve
487 * buffer space to the socket, by calling sbreserve(). This should commit
488 * some of the available buffer space in the system buffer pool for the
489 * socket (currently, it does nothing but enforce limits). The space
490 * should be released by calling sbrelease() when the socket is destroyed.
491 */
492
493int
494soreserve(so, sndcc, rcvcc)
495 register struct socket *so;
496 u_long sndcc, rcvcc;
497{
498 register struct kextcb *kp;
499
500 kp = sotokextcb(so);
9bccf70c
A
501 while (kp) {
502 if (kp->e_soif && kp->e_soif->sf_soreserve) {
503 if ((*kp->e_soif->sf_soreserve)(so, sndcc, rcvcc, kp))
1c79356b
A
504 return;
505 }
506 kp = kp->e_next;
507 }
508
509 if (sbreserve(&so->so_snd, sndcc) == 0)
510 goto bad;
511 if (sbreserve(&so->so_rcv, rcvcc) == 0)
512 goto bad2;
513 if (so->so_rcv.sb_lowat == 0)
514 so->so_rcv.sb_lowat = 1;
515 if (so->so_snd.sb_lowat == 0)
516 so->so_snd.sb_lowat = MCLBYTES;
517 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
518 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
519 return (0);
520bad2:
9bccf70c 521#ifdef __APPLE__
0b4e3aa0 522 selthreadclear(&so->so_snd.sb_sel);
9bccf70c 523#endif
1c79356b
A
524 sbrelease(&so->so_snd);
525bad:
526 return (ENOBUFS);
527}
528
529/*
530 * Allot mbufs to a sockbuf.
531 * Attempt to scale mbmax so that mbcnt doesn't become limiting
532 * if buffering efficiency is near the normal case.
533 */
534int
535sbreserve(sb, cc)
536 struct sockbuf *sb;
537 u_long cc;
538{
539 if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
540 return (0);
541 sb->sb_hiwat = cc;
542 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
543 if (sb->sb_lowat > sb->sb_hiwat)
544 sb->sb_lowat = sb->sb_hiwat;
545 return (1);
546}
547
548/*
549 * Free mbufs held by a socket, and reserved mbuf space.
550 */
0b4e3aa0 551 /* WARNING needs to do selthreadclear() before calling this */
1c79356b
A
552void
553sbrelease(sb)
554 struct sockbuf *sb;
555{
556
557 sbflush(sb);
9bccf70c
A
558 sb->sb_hiwat = 0;
559 sb->sb_mbmax = 0;
560
1c79356b
A
561}
562
563/*
564 * Routines to add and remove
565 * data from an mbuf queue.
566 *
567 * The routines sbappend() or sbappendrecord() are normally called to
568 * append new mbufs to a socket buffer, after checking that adequate
569 * space is available, comparing the function sbspace() with the amount
570 * of data to be added. sbappendrecord() differs from sbappend() in
571 * that data supplied is treated as the beginning of a new record.
572 * To place a sender's address, optional access rights, and data in a
573 * socket receive buffer, sbappendaddr() should be used. To place
574 * access rights and data in a socket receive buffer, sbappendrights()
575 * should be used. In either case, the new data begins a new record.
576 * Note that unlike sbappend() and sbappendrecord(), these routines check
577 * for the caller that there will be enough space to store the data.
578 * Each fails if there is not enough space, or if it cannot find mbufs
579 * to store additional information in.
580 *
581 * Reliable protocols may use the socket send buffer to hold data
582 * awaiting acknowledgement. Data is normally copied from a socket
583 * send buffer in a protocol with m_copy for output to a peer,
584 * and then removing the data from the socket buffer with sbdrop()
585 * or sbdroprecord() when the data is acknowledged by the peer.
586 */
587
588/*
589 * Append mbuf chain m to the last record in the
590 * socket buffer sb. The additional space associated
591 * the mbuf chain is recorded in sb. Empty mbufs are
592 * discarded and mbufs are compacted where possible.
593 */
594void
595sbappend(sb, m)
596 struct sockbuf *sb;
597 struct mbuf *m;
9bccf70c
A
598{
599 struct kextcb *kp;
1c79356b
A
600 register struct mbuf *n;
601
fa4905b1
A
602
603 KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_START), sb, m->m_len, 0, 0, 0);
604
1c79356b
A
605 if (m == 0)
606 return;
607 kp = sotokextcb(sbtoso(sb));
9bccf70c
A
608 while (kp) {
609 if (kp->e_sout && kp->e_sout->su_sbappend) {
610 if ((*kp->e_sout->su_sbappend)(sb, m, kp))
1c79356b
A
611 return;
612 }
613 kp = kp->e_next;
614 }
9bccf70c
A
615 n = sb->sb_mb;
616 if (n) {
1c79356b
A
617 while (n->m_nextpkt)
618 n = n->m_nextpkt;
619 do {
620 if (n->m_flags & M_EOR) {
621 sbappendrecord(sb, m); /* XXXXXX!!!! */
622 return;
623 }
624 } while (n->m_next && (n = n->m_next));
625 }
626 sbcompress(sb, m, n);
fa4905b1
A
627
628 KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0);
1c79356b
A
629}
630
631#ifdef SOCKBUF_DEBUG
632void
633sbcheck(sb)
634 register struct sockbuf *sb;
635{
636 register struct mbuf *m;
637 register struct mbuf *n = 0;
638 register u_long len = 0, mbcnt = 0;
639
640 for (m = sb->sb_mb; m; m = n) {
641 n = m->m_nextpkt;
642 for (; m; m = m->m_next) {
9bccf70c
A
643 len += m->m_len;
644 mbcnt += MSIZE;
645 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
646 mbcnt += m->m_ext.ext_size;
647 }
1c79356b 648 }
9bccf70c
A
649#ifndef __APPLE__
650 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
651 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
652 mbcnt, sb->sb_mbcnt);
653 panic("sbcheck");
654 }
655#else
656 if (len != sb->sb_cc)
657 printf("sbcheck len %ld != sb_cc %ld\n", len, sb->sb_cc);
658 if (mbcnt != sb->sb_mbcnt)
659 printf("sbcheck mbcnt %ld != sb_mbcnt %ld\n", mbcnt, sb->sb_mbcnt);
660#endif
1c79356b
A
661}
662#endif
663
664/*
665 * As above, except the mbuf chain
666 * begins a new record.
667 */
668void
669sbappendrecord(sb, m0)
670 register struct sockbuf *sb;
671 register struct mbuf *m0;
672{
673 register struct mbuf *m;
674 register struct kextcb *kp;
9bccf70c 675
1c79356b
A
676 if (m0 == 0)
677 return;
678
679 kp = sotokextcb(sbtoso(sb));
680 while (kp)
681 { if (kp->e_sout && kp->e_sout->su_sbappendrecord)
682 { if ((*kp->e_sout->su_sbappendrecord)(sb, m0, kp))
683 return;
684 }
685 kp = kp->e_next;
686 }
687
688 m = sb->sb_mb;
689 if (m)
690 while (m->m_nextpkt)
691 m = m->m_nextpkt;
692 /*
693 * Put the first mbuf on the queue.
694 * Note this permits zero length records.
695 */
696 sballoc(sb, m0);
697 if (m)
698 m->m_nextpkt = m0;
699 else
700 sb->sb_mb = m0;
701 m = m0->m_next;
702 m0->m_next = 0;
703 if (m && (m0->m_flags & M_EOR)) {
704 m0->m_flags &= ~M_EOR;
705 m->m_flags |= M_EOR;
706 }
707 sbcompress(sb, m, m0);
708}
709
710/*
711 * As above except that OOB data
712 * is inserted at the beginning of the sockbuf,
713 * but after any other OOB data.
714 */
715void
716sbinsertoob(sb, m0)
717 register struct sockbuf *sb;
718 register struct mbuf *m0;
719{
720 register struct mbuf *m;
721 register struct mbuf **mp;
722 register struct kextcb *kp;
723
724 if (m0 == 0)
725 return;
726
727 kp = sotokextcb(sbtoso(sb));
728 while (kp)
729 { if (kp->e_sout && kp->e_sout->su_sbinsertoob)
730 { if ((*kp->e_sout->su_sbinsertoob)(sb, m0, kp))
731 return;
732 }
733 kp = kp->e_next;
734 }
735
736 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
737 m = *mp;
738 again:
739 switch (m->m_type) {
740
741 case MT_OOBDATA:
742 continue; /* WANT next train */
743
744 case MT_CONTROL:
745 m = m->m_next;
746 if (m)
747 goto again; /* inspect THIS train further */
748 }
749 break;
750 }
751 /*
752 * Put the first mbuf on the queue.
753 * Note this permits zero length records.
754 */
755 sballoc(sb, m0);
756 m0->m_nextpkt = *mp;
757 *mp = m0;
758 m = m0->m_next;
759 m0->m_next = 0;
760 if (m && (m0->m_flags & M_EOR)) {
761 m0->m_flags &= ~M_EOR;
762 m->m_flags |= M_EOR;
763 }
764 sbcompress(sb, m, m0);
765}
766
767/*
768 * Append address and data, and optionally, control (ancillary) data
769 * to the receive queue of a socket. If present,
770 * m0 must include a packet header with total length.
771 * Returns 0 if no space in sockbuf or insufficient mbufs.
772 */
773int
774sbappendaddr(sb, asa, m0, control)
775 register struct sockbuf *sb;
776 struct sockaddr *asa;
777 struct mbuf *m0, *control;
778{
779 register struct mbuf *m, *n;
780 int space = asa->sa_len;
781 register struct kextcb *kp;
782
783 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
784 panic("sbappendaddr");
785
786 kp = sotokextcb(sbtoso(sb));
787 while (kp)
788 { if (kp->e_sout && kp->e_sout->su_sbappendaddr)
789 { if ((*kp->e_sout->su_sbappendaddr)(sb, asa, m0, control, kp))
790 return 0;
791 }
792 kp = kp->e_next;
793 }
794
795 if (m0)
796 space += m0->m_pkthdr.len;
797 for (n = control; n; n = n->m_next) {
798 space += n->m_len;
799 if (n->m_next == 0) /* keep pointer to last control buf */
800 break;
801 }
802 if (space > sbspace(sb))
803 return (0);
804 if (asa->sa_len > MLEN)
805 return (0);
806 MGET(m, M_DONTWAIT, MT_SONAME);
807 if (m == 0)
808 return (0);
809 m->m_len = asa->sa_len;
810 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
811 if (n)
812 n->m_next = m0; /* concatenate data to control */
813 else
814 control = m0;
815 m->m_next = control;
816 for (n = m; n; n = n->m_next)
817 sballoc(sb, n);
818 n = sb->sb_mb;
819 if (n) {
820 while (n->m_nextpkt)
821 n = n->m_nextpkt;
822 n->m_nextpkt = m;
823 } else
824 sb->sb_mb = m;
825 postevent(0,sb,EV_RWBYTES);
826 return (1);
827}
828
829int
830sbappendcontrol(sb, m0, control)
831 struct sockbuf *sb;
832 struct mbuf *control, *m0;
833{
834 register struct mbuf *m, *n;
835 int space = 0;
836 register struct kextcb *kp;
837
838 if (control == 0)
839 panic("sbappendcontrol");
840
841 kp = sotokextcb(sbtoso(sb));
842 while (kp)
843 { if (kp->e_sout && kp->e_sout->su_sbappendcontrol)
844 { if ((*kp->e_sout->su_sbappendcontrol)(sb, m0, control, kp))
845 return 0;
846 }
847 kp = kp->e_next;
848 }
849
850 for (m = control; ; m = m->m_next) {
851 space += m->m_len;
852 if (m->m_next == 0)
853 break;
854 }
855 n = m; /* save pointer to last control buffer */
856 for (m = m0; m; m = m->m_next)
857 space += m->m_len;
858 if (space > sbspace(sb))
859 return (0);
860 n->m_next = m0; /* concatenate data to control */
861 for (m = control; m; m = m->m_next)
862 sballoc(sb, m);
863 n = sb->sb_mb;
864 if (n) {
865 while (n->m_nextpkt)
866 n = n->m_nextpkt;
867 n->m_nextpkt = control;
868 } else
869 sb->sb_mb = control;
870 postevent(0,sb,EV_RWBYTES);
871 return (1);
872}
873
874/*
875 * Compress mbuf chain m into the socket
876 * buffer sb following mbuf n. If n
877 * is null, the buffer is presumed empty.
878 */
879void
880sbcompress(sb, m, n)
881 register struct sockbuf *sb;
882 register struct mbuf *m, *n;
883{
884 register int eor = 0;
885 register struct mbuf *o;
886
887 while (m) {
888 eor |= m->m_flags & M_EOR;
889 if (m->m_len == 0 &&
890 (eor == 0 ||
891 (((o = m->m_next) || (o = n)) &&
892 o->m_type == m->m_type))) {
893 m = m_free(m);
894 continue;
895 }
9bccf70c
A
896 if (n && (n->m_flags & M_EOR) == 0 &&
897#ifndef __APPLE__
898 M_WRITABLE(n) &&
899#endif
900 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
901 m->m_len <= M_TRAILINGSPACE(n) &&
1c79356b
A
902 n->m_type == m->m_type) {
903 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
904 (unsigned)m->m_len);
905 n->m_len += m->m_len;
906 sb->sb_cc += m->m_len;
907 m = m_free(m);
908 continue;
909 }
910 if (n)
911 n->m_next = m;
912 else
913 sb->sb_mb = m;
914 sballoc(sb, m);
915 n = m;
916 m->m_flags &= ~M_EOR;
917 m = m->m_next;
918 n->m_next = 0;
919 }
920 if (eor) {
921 if (n)
922 n->m_flags |= eor;
923 else
924 printf("semi-panic: sbcompress\n");
925 }
926 postevent(0,sb, EV_RWBYTES);
927}
928
929/*
930 * Free all mbufs in a sockbuf.
931 * Check that all resources are reclaimed.
932 */
933void
934sbflush(sb)
935 register struct sockbuf *sb;
936{
937 register struct kextcb *kp;
938
939 kp = sotokextcb(sbtoso(sb));
9bccf70c
A
940 while (kp) {
941 if (kp->e_sout && kp->e_sout->su_sbflush) {
942 if ((*kp->e_sout->su_sbflush)(sb, kp))
1c79356b
A
943 return;
944 }
945 kp = kp->e_next;
946 }
947
948 if (sb->sb_flags & SB_LOCK)
9bccf70c
A
949 sb_lock(sb);
950 while (sb->sb_mbcnt) {
951 /*
952 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
953 * we would loop forever. Panic instead.
954 */
955 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
956 break;
1c79356b 957 sbdrop(sb, (int)sb->sb_cc);
9bccf70c 958 }
1c79356b
A
959 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
960 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
961 postevent(0, sb, EV_RWBYTES);
962}
963
964/*
965 * Drop data from (the front of) a sockbuf.
9bccf70c
A
966 * use m_freem_list to free the mbuf structures
967 * under a single lock... this is done by pruning
968 * the top of the tree from the body by keeping track
969 * of where we get to in the tree and then zeroing the
970 * two pertinent pointers m_nextpkt and m_next
971 * the socket buffer is then updated to point at the new
972 * top of the tree and the pruned area is released via
973 * m_freem_list.
1c79356b
A
974 */
975void
976sbdrop(sb, len)
977 register struct sockbuf *sb;
978 register int len;
979{
fa4905b1
A
980 register struct mbuf *m, *free_list, *ml;
981 struct mbuf *next, *last;
1c79356b
A
982 register struct kextcb *kp;
983
fa4905b1
A
984 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0);
985
1c79356b 986 kp = sotokextcb(sbtoso(sb));
9bccf70c
A
987 while (kp) {
988 if (kp->e_sout && kp->e_sout->su_sbdrop) {
989 if ((*kp->e_sout->su_sbdrop)(sb, len, kp))
1c79356b
A
990 return;
991 }
992 kp = kp->e_next;
993 }
1c79356b 994 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
fa4905b1
A
995 free_list = last = m;
996 ml = (struct mbuf *)0;
997
1c79356b
A
998 while (len > 0) {
999 if (m == 0) {
9bccf70c
A
1000 if (next == 0) {
1001 /* temporarily replacing this panic with printf because
1002 * it occurs occasionally when closing a socket when there
1003 * is no harm in ignoring it. This problem will be investigated
1004 * further.
1005 */
1006 /* panic("sbdrop"); */
1007 printf("sbdrop - count not zero\n");
1008 len = 0;
1009 /* zero the counts. if we have no mbufs, we have no data (PR-2986815) */
1010 sb->sb_cc = 0;
1011 sb->sb_mbcnt = 0;
1012 break;
1013 }
1014 m = last = next;
1015 next = m->m_nextpkt;
1016 continue;
1c79356b
A
1017 }
1018 if (m->m_len > len) {
1019 m->m_len -= len;
1020 m->m_data += len;
1021 sb->sb_cc -= len;
1022 break;
1023 }
1024 len -= m->m_len;
1025 sbfree(sb, m);
fa4905b1
A
1026
1027 ml = m;
1028 m = m->m_next;
1c79356b
A
1029 }
1030 while (m && m->m_len == 0) {
1031 sbfree(sb, m);
fa4905b1
A
1032
1033 ml = m;
1034 m = m->m_next;
1035 }
1036 if (ml) {
1037 ml->m_next = (struct mbuf *)0;
1038 last->m_nextpkt = (struct mbuf *)0;
1039 m_freem_list(free_list);
1c79356b
A
1040 }
1041 if (m) {
1042 sb->sb_mb = m;
1043 m->m_nextpkt = next;
1044 } else
1045 sb->sb_mb = next;
fa4905b1 1046
1c79356b 1047 postevent(0, sb, EV_RWBYTES);
fa4905b1
A
1048
1049 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0);
1c79356b
A
1050}
1051
1052/*
1053 * Drop a record off the front of a sockbuf
1054 * and move the next record to the front.
1055 */
1056void
1057sbdroprecord(sb)
1058 register struct sockbuf *sb;
1059{
1060 register struct mbuf *m, *mn;
1061 register struct kextcb *kp;
1062
1063 kp = sotokextcb(sbtoso(sb));
9bccf70c
A
1064 while (kp) {
1065 if (kp->e_sout && kp->e_sout->su_sbdroprecord) {
1066 if ((*kp->e_sout->su_sbdroprecord)(sb, kp))
1c79356b
A
1067 return;
1068 }
1069 kp = kp->e_next;
1070 }
1071
1072 m = sb->sb_mb;
1073 if (m) {
1074 sb->sb_mb = m->m_nextpkt;
1075 do {
1076 sbfree(sb, m);
1077 MFREE(m, mn);
9bccf70c
A
1078 m = mn;
1079 } while (m);
1c79356b
A
1080 }
1081 postevent(0, sb, EV_RWBYTES);
1082}
1083
1084/*
1085 * Create a "control" mbuf containing the specified data
1086 * with the specified type for presentation on a socket buffer.
1087 */
1088struct mbuf *
1089sbcreatecontrol(p, size, type, level)
1090 caddr_t p;
1091 register int size;
1092 int type, level;
1093{
1094 register struct cmsghdr *cp;
1095 struct mbuf *m;
1096
9bccf70c
A
1097 if (CMSG_SPACE((u_int)size) > MLEN)
1098 return ((struct mbuf *) NULL);
1c79356b
A
1099 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
1100 return ((struct mbuf *) NULL);
1101 cp = mtod(m, struct cmsghdr *);
1102 /* XXX check size? */
1103 (void)memcpy(CMSG_DATA(cp), p, size);
9bccf70c
A
1104 m->m_len = CMSG_SPACE(size);
1105 cp->cmsg_len = CMSG_LEN(size);
1c79356b
A
1106 cp->cmsg_level = level;
1107 cp->cmsg_type = type;
1108 return (m);
1109}
1110
1111/*
1112 * Some routines that return EOPNOTSUPP for entry points that are not
1113 * supported by a protocol. Fill in as needed.
1114 */
1115int
1116pru_abort_notsupp(struct socket *so)
1117{
1118 return EOPNOTSUPP;
1119}
1120
1121
1122int
1123pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1124{
1125 return EOPNOTSUPP;
1126}
1127
1128int
1129pru_attach_notsupp(struct socket *so, int proto, struct proc *p)
1130{
1131 return EOPNOTSUPP;
1132}
1133
1134int
1135pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
1136{
1137 return EOPNOTSUPP;
1138}
1139
1140int
1141pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
1142{
1143 return EOPNOTSUPP;
1144}
1145
1146int
1147pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1148{
1149 return EOPNOTSUPP;
1150}
1151
1152int
1153pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1154 struct ifnet *ifp, struct proc *p)
1155{
1156 return EOPNOTSUPP;
1157}
1158
1159int
1160pru_detach_notsupp(struct socket *so)
1161{
1162 return EOPNOTSUPP;
1163}
1164
1165int
1166pru_disconnect_notsupp(struct socket *so)
1167{
1168 return EOPNOTSUPP;
1169}
1170
1171int
1172pru_listen_notsupp(struct socket *so, struct proc *p)
1173{
1174 return EOPNOTSUPP;
1175}
1176
1177int
1178pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
1179{
1180 return EOPNOTSUPP;
1181}
1182
1183int
1184pru_rcvd_notsupp(struct socket *so, int flags)
1185{
1186 return EOPNOTSUPP;
1187}
1188
1189int
1190pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1191{
1192 return EOPNOTSUPP;
1193}
1194
1195int
1196pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
1197 struct sockaddr *addr, struct mbuf *control,
1198 struct proc *p)
1199
1200{
1201 return EOPNOTSUPP;
1202}
1203
1204
1205/*
1206 * This isn't really a ``null'' operation, but it's the default one
1207 * and doesn't do anything destructive.
1208 */
1209int
1210pru_sense_null(struct socket *so, struct stat *sb)
1211{
1212 sb->st_blksize = so->so_snd.sb_hiwat;
1213 return 0;
1214}
1215
1216
1217int pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
1218 struct uio *uio, struct mbuf *top,
1219 struct mbuf *control, int flags)
1220
1221{
1222 return EOPNOTSUPP;
1223}
1224
1225int pru_soreceive_notsupp(struct socket *so,
1226 struct sockaddr **paddr,
1227 struct uio *uio, struct mbuf **mp0,
1228 struct mbuf **controlp, int *flagsp)
1229{
1230 return EOPNOTSUPP;
1231}
1232
1233int
1234
1235pru_shutdown_notsupp(struct socket *so)
1236{
1237 return EOPNOTSUPP;
1238}
1239
1240int
1241pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
1242{
1243 return EOPNOTSUPP;
1244}
1245
1246int pru_sosend(struct socket *so, struct sockaddr *addr,
1247 struct uio *uio, struct mbuf *top,
1248 struct mbuf *control, int flags)
1249{
1250 return EOPNOTSUPP;
1251}
1252
1253int pru_soreceive(struct socket *so,
1254 struct sockaddr **paddr,
1255 struct uio *uio, struct mbuf **mp0,
1256 struct mbuf **controlp, int *flagsp)
1257{
1258 return EOPNOTSUPP;
1259}
1260
1261
1262int pru_sopoll_notsupp(struct socket *so, int events,
1263 struct ucred *cred)
1264{
1265 return EOPNOTSUPP;
1266}
1267
1268
9bccf70c
A
1269#ifdef __APPLE__
1270/*
1271 * The following are macros on BSD and functions on Darwin
1272 */
1c79356b 1273
0b4e3aa0
A
1274/*
1275 * Do we need to notify the other side when I/O is possible?
1276 */
1277
1278int
1279sb_notify(struct sockbuf *sb)
1280{
1281 return ((sb->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL)) != 0);
1282}
1283
1284/*
1285 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1286 * This is problematical if the fields are unsigned, as the space might
1287 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1288 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1289 */
1290long
1291sbspace(struct sockbuf *sb)
1292{
1293 return ((long) imin((int)(sb->sb_hiwat - sb->sb_cc),
1294 (int)(sb->sb_mbmax - sb->sb_mbcnt)));
1295}
1296
1297/* do we have to send all at once on a socket? */
1298int
1299sosendallatonce(struct socket *so)
1300{
1301 return (so->so_proto->pr_flags & PR_ATOMIC);
1302}
1303
1304/* can we read something from so? */
1305int
1306soreadable(struct socket *so)
1307{
1308 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1309 (so->so_state & SS_CANTRCVMORE) ||
1310 so->so_comp.tqh_first || so->so_error);
1311}
1312
1313/* can we write something to so? */
1314
1315int
1316sowriteable(struct socket *so)
1317{
1318 return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat &&
1319 ((so->so_state&SS_ISCONNECTED) ||
1320 (so->so_proto->pr_flags&PR_CONNREQUIRED)==0)) ||
1321 (so->so_state & SS_CANTSENDMORE) ||
1322 so->so_error);
1323}
1324
1325/* adjust counters in sb reflecting allocation of m */
1326
1327void
1328sballoc(struct sockbuf *sb, struct mbuf *m)
1329{
1330 sb->sb_cc += m->m_len;
1331 sb->sb_mbcnt += MSIZE;
1332 if (m->m_flags & M_EXT)
1333 sb->sb_mbcnt += m->m_ext.ext_size;
1334}
1335
1336/* adjust counters in sb reflecting freeing of m */
1337void
1338sbfree(struct sockbuf *sb, struct mbuf *m)
1339{
1340 sb->sb_cc -= m->m_len;
1341 sb->sb_mbcnt -= MSIZE;
1342 if (m->m_flags & M_EXT)
1343 sb->sb_mbcnt -= m->m_ext.ext_size;
1344}
1345
1346/*
1347 * Set lock on sockbuf sb; sleep if lock is already held.
1348 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1349 * Returns error without lock if sleep is interrupted.
1350 */
1351int
1352sblock(struct sockbuf *sb, int wf)
1353{
1354 return(sb->sb_flags & SB_LOCK ?
1355 ((wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) :
1356 (sb->sb_flags |= SB_LOCK), 0);
1357}
1358
1359/* release lock on sockbuf sb */
1360void
1361sbunlock(struct sockbuf *sb)
1362{
1363 sb->sb_flags &= ~SB_LOCK;
1364 if (sb->sb_flags & SB_WANT) {
1365 sb->sb_flags &= ~SB_WANT;
1366 wakeup((caddr_t)&(sb)->sb_flags);
1367 }
1368}
1369
1370void
1371sorwakeup(struct socket * so)
1372{
1373 if (sb_notify(&so->so_rcv))
1374 sowakeup(so, &so->so_rcv);
1375}
1376
1377void
1378sowwakeup(struct socket * so)
1379{
1380 if (sb_notify(&so->so_snd))
1381 sowakeup(so, &so->so_snd);
1382}
9bccf70c 1383#endif __APPLE__
0b4e3aa0 1384
1c79356b
A
1385/*
1386 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1387 */
1388struct sockaddr *
1389dup_sockaddr(sa, canwait)
1390 struct sockaddr *sa;
1391 int canwait;
1392{
1393 struct sockaddr *sa2;
1394
1395 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
1396 canwait ? M_WAITOK : M_NOWAIT);
1397 if (sa2)
1398 bcopy(sa, sa2, sa->sa_len);
1399 return sa2;
1400}
1401
1402/*
1403 * Create an external-format (``xsocket'') structure using the information
1404 * in the kernel-format socket structure pointed to by so. This is done
1405 * to reduce the spew of irrelevant information over this interface,
1406 * to isolate user code from changes in the kernel structure, and
1407 * potentially to provide information-hiding if we decide that
1408 * some of this information should be hidden from users.
1409 */
1410void
1411sotoxsocket(struct socket *so, struct xsocket *xso)
1412{
1413 xso->xso_len = sizeof *xso;
1414 xso->xso_so = so;
1415 xso->so_type = so->so_type;
1416 xso->so_options = so->so_options;
1417 xso->so_linger = so->so_linger;
1418 xso->so_state = so->so_state;
1419 xso->so_pcb = so->so_pcb;
1420 xso->xso_protocol = so->so_proto->pr_protocol;
1421 xso->xso_family = so->so_proto->pr_domain->dom_family;
1422 xso->so_qlen = so->so_qlen;
1423 xso->so_incqlen = so->so_incqlen;
1424 xso->so_qlimit = so->so_qlimit;
1425 xso->so_timeo = so->so_timeo;
1426 xso->so_error = so->so_error;
1427 xso->so_pgid = so->so_pgid;
1428 xso->so_oobmark = so->so_oobmark;
1429 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1430 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1431 xso->so_uid = so->so_uid;
1432}
1433
1434/*
1435 * This does the same for sockbufs. Note that the xsockbuf structure,
1436 * since it is always embedded in a socket, does not include a self
1437 * pointer nor a length. We make this entry point public in case
1438 * some other mechanism needs it.
1439 */
1440void
1441sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1442{
1443 xsb->sb_cc = sb->sb_cc;
1444 xsb->sb_hiwat = sb->sb_hiwat;
1445 xsb->sb_mbcnt = sb->sb_mbcnt;
1446 xsb->sb_mbmax = sb->sb_mbmax;
1447 xsb->sb_lowat = sb->sb_lowat;
1448 xsb->sb_flags = sb->sb_flags;
1449 xsb->sb_timeo = sb->sb_timeo;
1450}
1451
1452/*
1453 * Here is the definition of some of the basic objects in the kern.ipc
1454 * branch of the MIB.
1455 */
1c79356b
A
1456SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1457
1458/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1459static int dummy;
1460SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1461
9bccf70c
A
1462SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW,
1463 &sb_max, 0, "Maximum socket buffer size");
1464SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
1465 &maxsockets, 0, "Maximum number of sockets avaliable");
1c79356b
A
1466SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1467 &sb_efficiency, 0, "");
1468SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, &nmbclusters, 0, "");
1469