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