]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
4026fe6773f4c75e2a16b27bb7d7127c25bb1bb2
[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 */
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/domain.h>
62 #include <sys/kernel.h>
63 #include <sys/proc.h>
64 #include <sys/malloc.h>
65 #include <sys/mbuf.h>
66 #include <sys/protosw.h>
67 #include <sys/stat.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/signalvar.h>
71 #include <sys/sysctl.h>
72 #include <sys/ev.h>
73
74 /*
75 * Primitive routines for operating on sockets and socket buffers
76 */
77
78 u_long sb_max = SB_MAX; /* XXX should be static */
79
80 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
81
82 char netcon[] = "netcon";
83
84 /*
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''
96 * takes no time.
97 *
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().
105 *
106 * If a socket is closed with sockets on either
107 * so_incomp or so_comp, these sockets are dropped.
108 *
109 * If higher level protocols are implemented in
110 * the kernel, the wakeups done here will sometimes
111 * cause software-interrupt process scheduling.
112 */
113
114 void
115 soisconnecting(so)
116 register struct socket *so;
117 {
118
119 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
120 so->so_state |= SS_ISCONNECTING;
121 }
122
123 void
124 soisconnected(so)
125 register struct socket *so;
126 { register struct kextcb *kp;
127 register struct socket *head = so->so_head;
128
129 kp = sotokextcb(so);
130 while (kp)
131 { if (kp->e_soif && kp->e_soif->sf_soisconnected)
132 { if ((*kp->e_soif->sf_soisconnected)(so, kp))
133 return;
134 }
135 kp = kp->e_next;
136 }
137
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);
143 head->so_incqlen--;
144 so->so_state &= ~SS_INCOMP;
145 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
146 so->so_state |= SS_COMP;
147 sorwakeup(head);
148 wakeup((caddr_t)&head->so_timeo);
149 } else {
150 postevent(so,0,EV_WCONN);
151 wakeup((caddr_t)&so->so_timeo);
152 sorwakeup(so);
153 sowwakeup(so);
154 }
155 }
156
157 void
158 soisdisconnecting(so)
159 register struct socket *so;
160 { register struct kextcb *kp;
161
162 kp = sotokextcb(so);
163 while (kp)
164 { if (kp->e_soif && kp->e_soif->sf_soisdisconnecting)
165 { if ((*kp->e_soif->sf_soisdisconnecting)(so, kp))
166 return;
167 }
168 kp = kp->e_next;
169 }
170
171 so->so_state &= ~SS_ISCONNECTING;
172 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
173 wakeup((caddr_t)&so->so_timeo);
174 sowwakeup(so);
175 sorwakeup(so);
176 }
177
178 void
179 soisdisconnected(so)
180 register struct socket *so;
181 { register struct kextcb *kp;
182
183 kp = sotokextcb(so);
184 while (kp)
185 { if (kp->e_soif && kp->e_soif->sf_soisdisconnected)
186 { if ((*kp->e_soif->sf_soisdisconnected)(so, kp))
187 return;
188 }
189 kp = kp->e_next;
190 }
191
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);
195 sowwakeup(so);
196 sorwakeup(so);
197 }
198
199 /*
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.
205 *
206 * This may be used in conjunction with protocol specific queue
207 * congestion routines.
208 */
209 struct socket *
210 sodropablereq(head)
211 register struct socket *head;
212 {
213 register struct socket *so;
214 unsigned int i, j, qlen;
215 static int rnd;
216 static struct timeval old_runtime;
217 static unsigned int cur_cnt, old_cnt;
218 struct timeval tv;
219
220 microtime(&tv);
221 if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
222 old_runtime = tv;
223 old_cnt = cur_cnt / i;
224 cur_cnt = 0;
225 }
226
227 so = TAILQ_FIRST(&head->so_incomp);
228 if (!so)
229 return (so);
230
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;
235
236 while (j-- && so)
237 so = TAILQ_NEXT(so, so_list);
238 }
239
240 return (so);
241 }
242
243 /*
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.
250 */
251 struct socket *
252 sonewconn(head, connstatus)
253 register struct socket *head;
254 int connstatus;
255 { int error = 0;
256 register struct socket *so;
257 register struct kextcb *kp;
258
259 if (head->so_qlen > 3 * head->so_qlimit / 2)
260 return ((struct socket *)0);
261 so = soalloc(1, head->so_proto->pr_domain->dom_family, head->so_type);
262 if (so == NULL)
263 return ((struct socket *)0);
264
265 kp = sotokextcb(so);
266 while (kp)
267 { if (kp->e_soif && kp->e_soif->sf_sonewconn1)
268 { if ((*kp->e_soif->sf_sonewconn1)(so, connstatus, kp))
269 return;
270 }
271 kp = kp->e_next;
272 }
273
274 so->so_head = head;
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);
285
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)) {
289 sfilter_term(so);
290 sodealloc(so);
291 return ((struct socket *)0);
292 }
293 so->so_proto->pr_domain->dom_refs++;
294
295 if (connstatus) {
296 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
297 so->so_state |= SS_COMP;
298 } else {
299 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
300 so->so_state |= SS_INCOMP;
301 head->so_incqlen++;
302 }
303 head->so_qlen++;
304 if (connstatus) {
305 sorwakeup(head);
306 wakeup((caddr_t)&head->so_timeo);
307 so->so_state |= connstatus;
308 }
309 so->so_rcv.sb_so = so->so_snd.sb_so = so;
310 TAILQ_INIT(&so->so_evlist);
311 return (so);
312 }
313
314 /*
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.
322 */
323
324 void
325 socantsendmore(so)
326 struct socket *so;
327 { register struct kextcb *kp;
328
329 kp = sotokextcb(so);
330 while (kp)
331 { if (kp->e_soif && kp->e_soif->sf_socantsendmore)
332 { if ((*kp->e_soif->sf_socantsendmore)(so, kp))
333 return;
334 }
335 kp = kp->e_next;
336 }
337
338
339 so->so_state |= SS_CANTSENDMORE;
340 sowwakeup(so);
341 }
342
343 void
344 socantrcvmore(so)
345 struct socket *so;
346 { register struct kextcb *kp;
347
348 kp = sotokextcb(so);
349 while (kp)
350 { if (kp->e_soif && kp->e_soif->sf_socantrcvmore)
351 { if ((*kp->e_soif->sf_socantrcvmore)(so, kp))
352 return;
353 }
354 kp = kp->e_next;
355 }
356
357
358 so->so_state |= SS_CANTRCVMORE;
359 sorwakeup(so);
360 }
361
362 /*
363 * Wait for data to arrive at/drain from a socket buffer.
364 */
365 int
366 sbwait(sb)
367 struct sockbuf *sb;
368 {
369
370 sb->sb_flags |= SB_WAIT;
371 return (tsleep((caddr_t)&sb->sb_cc,
372 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
373 sb->sb_timeo));
374 }
375
376 /*
377 * Lock a sockbuf already known to be locked;
378 * return any error returned from sleep (EINTR).
379 */
380 int
381 sb_lock(sb)
382 register struct sockbuf *sb;
383 {
384 int error;
385
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,
390 "sblock", 0);
391 if (error)
392 return (error);
393 }
394 sb->sb_flags |= SB_LOCK;
395 return (0);
396 }
397
398 /*
399 * Wakeup processes waiting on a socket buffer.
400 * Do asynchronous notification via SIGIO
401 * if the socket has the SS_ASYNC flag set.
402 */
403 void
404 sowakeup(so, sb)
405 register struct socket *so;
406 register struct sockbuf *sb;
407 {
408 struct proc *p = current_proc();
409
410
411
412
413 sb->sb_flags &= ~SB_SEL;
414 selwakeup(&sb->sb_sel);
415
416 if (sb->sb_flags & SB_WAIT) {
417 sb->sb_flags &= ~SB_WAIT;
418 wakeup((caddr_t)&sb->sb_cc);
419 }
420 if (so->so_state & SS_ASYNC) {
421 if (so->so_pgid < 0)
422 gsignal(-so->so_pgid, SIGIO);
423 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
424 psignal(p, SIGIO);
425 }
426
427 if (sb->sb_flags & SB_UPCALL)
428 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
429 }
430
431 /*
432 * Socket buffer (struct sockbuf) utility routines.
433 *
434 * Each socket contains two socket buffers: one for sending data and
435 * one for receiving data. Each buffer contains a queue of mbufs,
436 * information about the number of mbufs and amount of data in the
437 * queue, and other fields allowing select() statements and notification
438 * on data availability to be implemented.
439 *
440 * Data stored in a socket buffer is maintained as a list of records.
441 * Each record is a list of mbufs chained together with the m_next
442 * field. Records are chained together with the m_nextpkt field. The upper
443 * level routine soreceive() expects the following conventions to be
444 * observed when placing information in the receive buffer:
445 *
446 * 1. If the protocol requires each message be preceded by the sender's
447 * name, then a record containing that name must be present before
448 * any associated data (mbuf's must be of type MT_SONAME).
449 * 2. If the protocol supports the exchange of ``access rights'' (really
450 * just additional data associated with the message), and there are
451 * ``rights'' to be received, then a record containing this data
452 * should be present (mbuf's must be of type MT_RIGHTS).
453 * 3. If a name or rights record exists, then it must be followed by
454 * a data record, perhaps of zero length.
455 *
456 * Before using a new socket structure it is first necessary to reserve
457 * buffer space to the socket, by calling sbreserve(). This should commit
458 * some of the available buffer space in the system buffer pool for the
459 * socket (currently, it does nothing but enforce limits). The space
460 * should be released by calling sbrelease() when the socket is destroyed.
461 */
462
463 int
464 soreserve(so, sndcc, rcvcc)
465 register struct socket *so;
466 u_long sndcc, rcvcc;
467 {
468 register struct kextcb *kp;
469
470 kp = sotokextcb(so);
471 while (kp)
472 { if (kp->e_soif && kp->e_soif->sf_soreserve)
473 { if ((*kp->e_soif->sf_soreserve)(so, sndcc, rcvcc, kp))
474 return;
475 }
476 kp = kp->e_next;
477 }
478
479 if (sbreserve(&so->so_snd, sndcc) == 0)
480 goto bad;
481 if (sbreserve(&so->so_rcv, rcvcc) == 0)
482 goto bad2;
483 if (so->so_rcv.sb_lowat == 0)
484 so->so_rcv.sb_lowat = 1;
485 if (so->so_snd.sb_lowat == 0)
486 so->so_snd.sb_lowat = MCLBYTES;
487 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
488 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
489 return (0);
490 bad2:
491 selthreadclear(&so->so_snd.sb_sel);
492 sbrelease(&so->so_snd);
493 bad:
494 return (ENOBUFS);
495 }
496
497 /*
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.
501 */
502 int
503 sbreserve(sb, cc)
504 struct sockbuf *sb;
505 u_long cc;
506 {
507 if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
508 return (0);
509 sb->sb_hiwat = cc;
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;
513 return (1);
514 }
515
516 /*
517 * Free mbufs held by a socket, and reserved mbuf space.
518 */
519 /* WARNING needs to do selthreadclear() before calling this */
520 void
521 sbrelease(sb)
522 struct sockbuf *sb;
523 {
524
525 sbflush(sb);
526 sb->sb_hiwat = sb->sb_mbmax = 0;
527 #if 0
528 /* this is getting called with bzeroed sb in sorflush */
529 {
530 int oldpri = splimp();
531 selthreadclear(&sb->sb_sel);
532 splx(oldpri);
533 }
534 #endif
535 }
536
537 /*
538 * Routines to add and remove
539 * data from an mbuf queue.
540 *
541 * The routines sbappend() or sbappendrecord() are normally called to
542 * append new mbufs to a socket buffer, after checking that adequate
543 * space is available, comparing the function sbspace() with the amount
544 * of data to be added. sbappendrecord() differs from sbappend() in
545 * that data supplied is treated as the beginning of a new record.
546 * To place a sender's address, optional access rights, and data in a
547 * socket receive buffer, sbappendaddr() should be used. To place
548 * access rights and data in a socket receive buffer, sbappendrights()
549 * should be used. In either case, the new data begins a new record.
550 * Note that unlike sbappend() and sbappendrecord(), these routines check
551 * for the caller that there will be enough space to store the data.
552 * Each fails if there is not enough space, or if it cannot find mbufs
553 * to store additional information in.
554 *
555 * Reliable protocols may use the socket send buffer to hold data
556 * awaiting acknowledgement. Data is normally copied from a socket
557 * send buffer in a protocol with m_copy for output to a peer,
558 * and then removing the data from the socket buffer with sbdrop()
559 * or sbdroprecord() when the data is acknowledged by the peer.
560 */
561
562 /*
563 * Append mbuf chain m to the last record in the
564 * socket buffer sb. The additional space associated
565 * the mbuf chain is recorded in sb. Empty mbufs are
566 * discarded and mbufs are compacted where possible.
567 */
568 void
569 sbappend(sb, m)
570 struct sockbuf *sb;
571 struct mbuf *m;
572 { register struct kextcb *kp;
573 register struct mbuf *n;
574
575 if (m == 0)
576 return;
577 kp = sotokextcb(sbtoso(sb));
578 while (kp)
579 { if (kp->e_sout && kp->e_sout->su_sbappend)
580 { if ((*kp->e_sout->su_sbappend)(sb, m, kp))
581 return;
582 }
583 kp = kp->e_next;
584 }
585
586 if (n = sb->sb_mb) {
587 while (n->m_nextpkt)
588 n = n->m_nextpkt;
589 do {
590 if (n->m_flags & M_EOR) {
591 sbappendrecord(sb, m); /* XXXXXX!!!! */
592 return;
593 }
594 } while (n->m_next && (n = n->m_next));
595 }
596 sbcompress(sb, m, n);
597 }
598
599 #ifdef SOCKBUF_DEBUG
600 void
601 sbcheck(sb)
602 register struct sockbuf *sb;
603 {
604 register struct mbuf *m;
605 register struct mbuf *n = 0;
606 register u_long len = 0, mbcnt = 0;
607
608 for (m = sb->sb_mb; m; m = n) {
609 n = m->m_nextpkt;
610 for (; m; m = m->m_next) {
611 len += m->m_len;
612 mbcnt += MSIZE;
613 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
614 mbcnt += m->m_ext.ext_size;
615 if (m->m_nextpkt)
616 panic("sbcheck nextpkt");
617 }
618 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
619 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
620 mbcnt, sb->sb_mbcnt);
621 panic("sbcheck");
622 }
623 }
624 #endif
625
626 /*
627 * As above, except the mbuf chain
628 * begins a new record.
629 */
630 void
631 sbappendrecord(sb, m0)
632 register struct sockbuf *sb;
633 register struct mbuf *m0;
634 {
635 register struct mbuf *m;
636 register struct kextcb *kp;
637
638 if (m0 == 0)
639 return;
640
641 kp = sotokextcb(sbtoso(sb));
642 while (kp)
643 { if (kp->e_sout && kp->e_sout->su_sbappendrecord)
644 { if ((*kp->e_sout->su_sbappendrecord)(sb, m0, kp))
645 return;
646 }
647 kp = kp->e_next;
648 }
649
650 m = sb->sb_mb;
651 if (m)
652 while (m->m_nextpkt)
653 m = m->m_nextpkt;
654 /*
655 * Put the first mbuf on the queue.
656 * Note this permits zero length records.
657 */
658 sballoc(sb, m0);
659 if (m)
660 m->m_nextpkt = m0;
661 else
662 sb->sb_mb = m0;
663 m = m0->m_next;
664 m0->m_next = 0;
665 if (m && (m0->m_flags & M_EOR)) {
666 m0->m_flags &= ~M_EOR;
667 m->m_flags |= M_EOR;
668 }
669 sbcompress(sb, m, m0);
670 }
671
672 /*
673 * As above except that OOB data
674 * is inserted at the beginning of the sockbuf,
675 * but after any other OOB data.
676 */
677 void
678 sbinsertoob(sb, m0)
679 register struct sockbuf *sb;
680 register struct mbuf *m0;
681 {
682 register struct mbuf *m;
683 register struct mbuf **mp;
684 register struct kextcb *kp;
685
686 if (m0 == 0)
687 return;
688
689 kp = sotokextcb(sbtoso(sb));
690 while (kp)
691 { if (kp->e_sout && kp->e_sout->su_sbinsertoob)
692 { if ((*kp->e_sout->su_sbinsertoob)(sb, m0, kp))
693 return;
694 }
695 kp = kp->e_next;
696 }
697
698 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
699 m = *mp;
700 again:
701 switch (m->m_type) {
702
703 case MT_OOBDATA:
704 continue; /* WANT next train */
705
706 case MT_CONTROL:
707 m = m->m_next;
708 if (m)
709 goto again; /* inspect THIS train further */
710 }
711 break;
712 }
713 /*
714 * Put the first mbuf on the queue.
715 * Note this permits zero length records.
716 */
717 sballoc(sb, m0);
718 m0->m_nextpkt = *mp;
719 *mp = m0;
720 m = m0->m_next;
721 m0->m_next = 0;
722 if (m && (m0->m_flags & M_EOR)) {
723 m0->m_flags &= ~M_EOR;
724 m->m_flags |= M_EOR;
725 }
726 sbcompress(sb, m, m0);
727 }
728
729 /*
730 * Append address and data, and optionally, control (ancillary) data
731 * to the receive queue of a socket. If present,
732 * m0 must include a packet header with total length.
733 * Returns 0 if no space in sockbuf or insufficient mbufs.
734 */
735 int
736 sbappendaddr(sb, asa, m0, control)
737 register struct sockbuf *sb;
738 struct sockaddr *asa;
739 struct mbuf *m0, *control;
740 {
741 register struct mbuf *m, *n;
742 int space = asa->sa_len;
743 register struct kextcb *kp;
744
745 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
746 panic("sbappendaddr");
747
748 kp = sotokextcb(sbtoso(sb));
749 while (kp)
750 { if (kp->e_sout && kp->e_sout->su_sbappendaddr)
751 { if ((*kp->e_sout->su_sbappendaddr)(sb, asa, m0, control, kp))
752 return 0;
753 }
754 kp = kp->e_next;
755 }
756
757 if (m0)
758 space += m0->m_pkthdr.len;
759 for (n = control; n; n = n->m_next) {
760 space += n->m_len;
761 if (n->m_next == 0) /* keep pointer to last control buf */
762 break;
763 }
764 if (space > sbspace(sb))
765 return (0);
766 if (asa->sa_len > MLEN)
767 return (0);
768 MGET(m, M_DONTWAIT, MT_SONAME);
769 if (m == 0)
770 return (0);
771 m->m_len = asa->sa_len;
772 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
773 if (n)
774 n->m_next = m0; /* concatenate data to control */
775 else
776 control = m0;
777 m->m_next = control;
778 for (n = m; n; n = n->m_next)
779 sballoc(sb, n);
780 n = sb->sb_mb;
781 if (n) {
782 while (n->m_nextpkt)
783 n = n->m_nextpkt;
784 n->m_nextpkt = m;
785 } else
786 sb->sb_mb = m;
787 postevent(0,sb,EV_RWBYTES);
788 return (1);
789 }
790
791 int
792 sbappendcontrol(sb, m0, control)
793 struct sockbuf *sb;
794 struct mbuf *control, *m0;
795 {
796 register struct mbuf *m, *n;
797 int space = 0;
798 register struct kextcb *kp;
799
800 if (control == 0)
801 panic("sbappendcontrol");
802
803 kp = sotokextcb(sbtoso(sb));
804 while (kp)
805 { if (kp->e_sout && kp->e_sout->su_sbappendcontrol)
806 { if ((*kp->e_sout->su_sbappendcontrol)(sb, m0, control, kp))
807 return 0;
808 }
809 kp = kp->e_next;
810 }
811
812 for (m = control; ; m = m->m_next) {
813 space += m->m_len;
814 if (m->m_next == 0)
815 break;
816 }
817 n = m; /* save pointer to last control buffer */
818 for (m = m0; m; m = m->m_next)
819 space += m->m_len;
820 if (space > sbspace(sb))
821 return (0);
822 n->m_next = m0; /* concatenate data to control */
823 for (m = control; m; m = m->m_next)
824 sballoc(sb, m);
825 n = sb->sb_mb;
826 if (n) {
827 while (n->m_nextpkt)
828 n = n->m_nextpkt;
829 n->m_nextpkt = control;
830 } else
831 sb->sb_mb = control;
832 postevent(0,sb,EV_RWBYTES);
833 return (1);
834 }
835
836 /*
837 * Compress mbuf chain m into the socket
838 * buffer sb following mbuf n. If n
839 * is null, the buffer is presumed empty.
840 */
841 void
842 sbcompress(sb, m, n)
843 register struct sockbuf *sb;
844 register struct mbuf *m, *n;
845 {
846 register int eor = 0;
847 register struct mbuf *o;
848
849 while (m) {
850 eor |= m->m_flags & M_EOR;
851 if (m->m_len == 0 &&
852 (eor == 0 ||
853 (((o = m->m_next) || (o = n)) &&
854 o->m_type == m->m_type))) {
855 m = m_free(m);
856 continue;
857 }
858 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
859 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
860 n->m_type == m->m_type) {
861 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
862 (unsigned)m->m_len);
863 n->m_len += m->m_len;
864 sb->sb_cc += m->m_len;
865 m = m_free(m);
866 continue;
867 }
868 if (n)
869 n->m_next = m;
870 else
871 sb->sb_mb = m;
872 sballoc(sb, m);
873 n = m;
874 m->m_flags &= ~M_EOR;
875 m = m->m_next;
876 n->m_next = 0;
877 }
878 if (eor) {
879 if (n)
880 n->m_flags |= eor;
881 else
882 printf("semi-panic: sbcompress\n");
883 }
884 postevent(0,sb, EV_RWBYTES);
885 }
886
887 /*
888 * Free all mbufs in a sockbuf.
889 * Check that all resources are reclaimed.
890 */
891 void
892 sbflush(sb)
893 register struct sockbuf *sb;
894 {
895 register struct kextcb *kp;
896
897 kp = sotokextcb(sbtoso(sb));
898 while (kp)
899 { if (kp->e_sout && kp->e_sout->su_sbflush)
900 { if ((*kp->e_sout->su_sbflush)(sb, kp))
901 return;
902 }
903 kp = kp->e_next;
904 }
905
906 if (sb->sb_flags & SB_LOCK)
907 panic("sbflush: locked");
908 while (sb->sb_mbcnt && sb->sb_cc)
909 sbdrop(sb, (int)sb->sb_cc);
910 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
911 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
912 postevent(0, sb, EV_RWBYTES);
913 }
914
915 /*
916 * Drop data from (the front of) a sockbuf.
917 */
918 void
919 sbdrop(sb, len)
920 register struct sockbuf *sb;
921 register int len;
922 {
923 register struct mbuf *m, *mn;
924 struct mbuf *next;
925 register struct kextcb *kp;
926
927 kp = sotokextcb(sbtoso(sb));
928 while (kp)
929 { if (kp->e_sout && kp->e_sout->su_sbdrop)
930 { if ((*kp->e_sout->su_sbdrop)(sb, len, kp))
931 return;
932 }
933 kp = kp->e_next;
934 }
935
936 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
937 while (len > 0) {
938 if (m == 0) {
939 if (next == 0)
940 panic("sbdrop");
941 m = next;
942 next = m->m_nextpkt;
943 continue;
944 }
945 if (m->m_len > len) {
946 m->m_len -= len;
947 m->m_data += len;
948 sb->sb_cc -= len;
949 break;
950 }
951 len -= m->m_len;
952 sbfree(sb, m);
953 MFREE(m, mn);
954 m = mn;
955 }
956 while (m && m->m_len == 0) {
957 sbfree(sb, m);
958 MFREE(m, mn);
959 m = mn;
960 }
961 if (m) {
962 sb->sb_mb = m;
963 m->m_nextpkt = next;
964 } else
965 sb->sb_mb = next;
966 postevent(0, sb, EV_RWBYTES);
967 }
968
969 /*
970 * Drop a record off the front of a sockbuf
971 * and move the next record to the front.
972 */
973 void
974 sbdroprecord(sb)
975 register struct sockbuf *sb;
976 {
977 register struct mbuf *m, *mn;
978 register struct kextcb *kp;
979
980 kp = sotokextcb(sbtoso(sb));
981 while (kp)
982 { if (kp->e_sout && kp->e_sout->su_sbdroprecord)
983 { if ((*kp->e_sout->su_sbdroprecord)(sb, kp))
984 return;
985 }
986 kp = kp->e_next;
987 }
988
989 m = sb->sb_mb;
990 if (m) {
991 sb->sb_mb = m->m_nextpkt;
992 do {
993 sbfree(sb, m);
994 MFREE(m, mn);
995 } while (m = mn);
996 }
997 postevent(0, sb, EV_RWBYTES);
998 }
999
1000 /*
1001 * Create a "control" mbuf containing the specified data
1002 * with the specified type for presentation on a socket buffer.
1003 */
1004 struct mbuf *
1005 sbcreatecontrol(p, size, type, level)
1006 caddr_t p;
1007 register int size;
1008 int type, level;
1009 {
1010 register struct cmsghdr *cp;
1011 struct mbuf *m;
1012
1013 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
1014 return ((struct mbuf *) NULL);
1015 cp = mtod(m, struct cmsghdr *);
1016 /* XXX check size? */
1017 (void)memcpy(CMSG_DATA(cp), p, size);
1018 size += sizeof(*cp);
1019 m->m_len = size;
1020 cp->cmsg_len = size;
1021 cp->cmsg_level = level;
1022 cp->cmsg_type = type;
1023 return (m);
1024 }
1025
1026 /*
1027 * Some routines that return EOPNOTSUPP for entry points that are not
1028 * supported by a protocol. Fill in as needed.
1029 */
1030 int
1031 pru_abort_notsupp(struct socket *so)
1032 {
1033 return EOPNOTSUPP;
1034 }
1035
1036
1037 int
1038 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1039 {
1040 return EOPNOTSUPP;
1041 }
1042
1043 int
1044 pru_attach_notsupp(struct socket *so, int proto, struct proc *p)
1045 {
1046 return EOPNOTSUPP;
1047 }
1048
1049 int
1050 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
1051 {
1052 return EOPNOTSUPP;
1053 }
1054
1055 int
1056 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
1057 {
1058 return EOPNOTSUPP;
1059 }
1060
1061 int
1062 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
1063 {
1064 return EOPNOTSUPP;
1065 }
1066
1067 int
1068 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
1069 struct ifnet *ifp, struct proc *p)
1070 {
1071 return EOPNOTSUPP;
1072 }
1073
1074 int
1075 pru_detach_notsupp(struct socket *so)
1076 {
1077 return EOPNOTSUPP;
1078 }
1079
1080 int
1081 pru_disconnect_notsupp(struct socket *so)
1082 {
1083 return EOPNOTSUPP;
1084 }
1085
1086 int
1087 pru_listen_notsupp(struct socket *so, struct proc *p)
1088 {
1089 return EOPNOTSUPP;
1090 }
1091
1092 int
1093 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
1094 {
1095 return EOPNOTSUPP;
1096 }
1097
1098 int
1099 pru_rcvd_notsupp(struct socket *so, int flags)
1100 {
1101 return EOPNOTSUPP;
1102 }
1103
1104 int
1105 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1106 {
1107 return EOPNOTSUPP;
1108 }
1109
1110 int
1111 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
1112 struct sockaddr *addr, struct mbuf *control,
1113 struct proc *p)
1114
1115 {
1116 return EOPNOTSUPP;
1117 }
1118
1119
1120 /*
1121 * This isn't really a ``null'' operation, but it's the default one
1122 * and doesn't do anything destructive.
1123 */
1124 int
1125 pru_sense_null(struct socket *so, struct stat *sb)
1126 {
1127 sb->st_blksize = so->so_snd.sb_hiwat;
1128 return 0;
1129 }
1130
1131
1132 int pru_sosend_notsupp(struct socket *so, struct sockaddr *addr,
1133 struct uio *uio, struct mbuf *top,
1134 struct mbuf *control, int flags)
1135
1136 {
1137 return EOPNOTSUPP;
1138 }
1139
1140 int pru_soreceive_notsupp(struct socket *so,
1141 struct sockaddr **paddr,
1142 struct uio *uio, struct mbuf **mp0,
1143 struct mbuf **controlp, int *flagsp)
1144 {
1145 return EOPNOTSUPP;
1146 }
1147
1148 int
1149
1150 pru_shutdown_notsupp(struct socket *so)
1151 {
1152 return EOPNOTSUPP;
1153 }
1154
1155 int
1156 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
1157 {
1158 return EOPNOTSUPP;
1159 }
1160
1161 int pru_sosend(struct socket *so, struct sockaddr *addr,
1162 struct uio *uio, struct mbuf *top,
1163 struct mbuf *control, int flags)
1164 {
1165 return EOPNOTSUPP;
1166 }
1167
1168 int pru_soreceive(struct socket *so,
1169 struct sockaddr **paddr,
1170 struct uio *uio, struct mbuf **mp0,
1171 struct mbuf **controlp, int *flagsp)
1172 {
1173 return EOPNOTSUPP;
1174 }
1175
1176
1177 int pru_sopoll_notsupp(struct socket *so, int events,
1178 struct ucred *cred)
1179 {
1180 return EOPNOTSUPP;
1181 }
1182
1183
1184
1185 /*
1186 * Do we need to notify the other side when I/O is possible?
1187 */
1188
1189 int
1190 sb_notify(struct sockbuf *sb)
1191 {
1192 return ((sb->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL)) != 0);
1193 }
1194
1195 /*
1196 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1197 * This is problematical if the fields are unsigned, as the space might
1198 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1199 * overflow and return 0. Should use "lmin" but it doesn't exist now.
1200 */
1201 long
1202 sbspace(struct sockbuf *sb)
1203 {
1204 return ((long) imin((int)(sb->sb_hiwat - sb->sb_cc),
1205 (int)(sb->sb_mbmax - sb->sb_mbcnt)));
1206 }
1207
1208 /* do we have to send all at once on a socket? */
1209 int
1210 sosendallatonce(struct socket *so)
1211 {
1212 return (so->so_proto->pr_flags & PR_ATOMIC);
1213 }
1214
1215 /* can we read something from so? */
1216 int
1217 soreadable(struct socket *so)
1218 {
1219 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1220 (so->so_state & SS_CANTRCVMORE) ||
1221 so->so_comp.tqh_first || so->so_error);
1222 }
1223
1224 /* can we write something to so? */
1225
1226 int
1227 sowriteable(struct socket *so)
1228 {
1229 return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat &&
1230 ((so->so_state&SS_ISCONNECTED) ||
1231 (so->so_proto->pr_flags&PR_CONNREQUIRED)==0)) ||
1232 (so->so_state & SS_CANTSENDMORE) ||
1233 so->so_error);
1234 }
1235
1236 /* adjust counters in sb reflecting allocation of m */
1237
1238 void
1239 sballoc(struct sockbuf *sb, struct mbuf *m)
1240 {
1241 sb->sb_cc += m->m_len;
1242 sb->sb_mbcnt += MSIZE;
1243 if (m->m_flags & M_EXT)
1244 sb->sb_mbcnt += m->m_ext.ext_size;
1245 }
1246
1247 /* adjust counters in sb reflecting freeing of m */
1248 void
1249 sbfree(struct sockbuf *sb, struct mbuf *m)
1250 {
1251 sb->sb_cc -= m->m_len;
1252 sb->sb_mbcnt -= MSIZE;
1253 if (m->m_flags & M_EXT)
1254 sb->sb_mbcnt -= m->m_ext.ext_size;
1255 }
1256
1257 /*
1258 * Set lock on sockbuf sb; sleep if lock is already held.
1259 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1260 * Returns error without lock if sleep is interrupted.
1261 */
1262 int
1263 sblock(struct sockbuf *sb, int wf)
1264 {
1265 return(sb->sb_flags & SB_LOCK ?
1266 ((wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) :
1267 (sb->sb_flags |= SB_LOCK), 0);
1268 }
1269
1270 /* release lock on sockbuf sb */
1271 void
1272 sbunlock(struct sockbuf *sb)
1273 {
1274 sb->sb_flags &= ~SB_LOCK;
1275 if (sb->sb_flags & SB_WANT) {
1276 sb->sb_flags &= ~SB_WANT;
1277 wakeup((caddr_t)&(sb)->sb_flags);
1278 }
1279 }
1280
1281 void
1282 sorwakeup(struct socket * so)
1283 {
1284 if (sb_notify(&so->so_rcv))
1285 sowakeup(so, &so->so_rcv);
1286 }
1287
1288 void
1289 sowwakeup(struct socket * so)
1290 {
1291 if (sb_notify(&so->so_snd))
1292 sowakeup(so, &so->so_snd);
1293 }
1294
1295 /*
1296 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1297 */
1298 struct sockaddr *
1299 dup_sockaddr(sa, canwait)
1300 struct sockaddr *sa;
1301 int canwait;
1302 {
1303 struct sockaddr *sa2;
1304
1305 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
1306 canwait ? M_WAITOK : M_NOWAIT);
1307 if (sa2)
1308 bcopy(sa, sa2, sa->sa_len);
1309 return sa2;
1310 }
1311
1312 /*
1313 * Create an external-format (``xsocket'') structure using the information
1314 * in the kernel-format socket structure pointed to by so. This is done
1315 * to reduce the spew of irrelevant information over this interface,
1316 * to isolate user code from changes in the kernel structure, and
1317 * potentially to provide information-hiding if we decide that
1318 * some of this information should be hidden from users.
1319 */
1320 void
1321 sotoxsocket(struct socket *so, struct xsocket *xso)
1322 {
1323 xso->xso_len = sizeof *xso;
1324 xso->xso_so = so;
1325 xso->so_type = so->so_type;
1326 xso->so_options = so->so_options;
1327 xso->so_linger = so->so_linger;
1328 xso->so_state = so->so_state;
1329 xso->so_pcb = so->so_pcb;
1330 xso->xso_protocol = so->so_proto->pr_protocol;
1331 xso->xso_family = so->so_proto->pr_domain->dom_family;
1332 xso->so_qlen = so->so_qlen;
1333 xso->so_incqlen = so->so_incqlen;
1334 xso->so_qlimit = so->so_qlimit;
1335 xso->so_timeo = so->so_timeo;
1336 xso->so_error = so->so_error;
1337 xso->so_pgid = so->so_pgid;
1338 xso->so_oobmark = so->so_oobmark;
1339 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
1340 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
1341 xso->so_uid = so->so_uid;
1342 }
1343
1344 /*
1345 * This does the same for sockbufs. Note that the xsockbuf structure,
1346 * since it is always embedded in a socket, does not include a self
1347 * pointer nor a length. We make this entry point public in case
1348 * some other mechanism needs it.
1349 */
1350 void
1351 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
1352 {
1353 xsb->sb_cc = sb->sb_cc;
1354 xsb->sb_hiwat = sb->sb_hiwat;
1355 xsb->sb_mbcnt = sb->sb_mbcnt;
1356 xsb->sb_mbmax = sb->sb_mbmax;
1357 xsb->sb_lowat = sb->sb_lowat;
1358 xsb->sb_flags = sb->sb_flags;
1359 xsb->sb_timeo = sb->sb_timeo;
1360 }
1361
1362 /*
1363 * Here is the definition of some of the basic objects in the kern.ipc
1364 * branch of the MIB.
1365 */
1366
1367
1368 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
1369
1370 /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
1371 static int dummy;
1372 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
1373
1374 SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, "");
1375 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, &maxsockets, 0, "");
1376 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1377 &sb_efficiency, 0, "");
1378 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, &nmbclusters, 0, "");
1379