Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / uipc_socket2.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
35 */
36
37#include "opt_param.h"
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/domain.h>
41#include <sys/file.h> /* for maxfiles */
42#include <sys/kernel.h>
43#include <sys/proc.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/protosw.h>
47#include <sys/resourcevar.h>
48#include <sys/stat.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/signalvar.h>
52#include <sys/sysctl.h>
53#include <sys/aio.h> /* for aio_swake proto */
54#include <sys/event.h>
55
56int maxsockets;
57
58/*
59 * Primitive routines for operating on sockets and socket buffers
60 */
61
62u_long sb_max = SB_MAX;
63u_long sb_max_adj =
64 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
65
66static u_long sb_efficiency = 8; /* parameter for sbreserve() */
67
68/*
69 * Procedures to manipulate state flags of socket
70 * and do appropriate wakeups. Normal sequence from the
71 * active (originating) side is that soisconnecting() is
72 * called during processing of connect() call,
73 * resulting in an eventual call to soisconnected() if/when the
74 * connection is established. When the connection is torn down
75 * soisdisconnecting() is called during processing of disconnect() call,
76 * and soisdisconnected() is called when the connection to the peer
77 * is totally severed. The semantics of these routines are such that
78 * connectionless protocols can call soisconnected() and soisdisconnected()
79 * only, bypassing the in-progress calls when setting up a ``connection''
80 * takes no time.
81 *
82 * From the passive side, a socket is created with
83 * two queues of sockets: so_incomp for connections in progress
84 * and so_comp for connections already made and awaiting user acceptance.
85 * As a protocol is preparing incoming connections, it creates a socket
86 * structure queued on so_incomp by calling sonewconn(). When the connection
87 * is established, soisconnected() is called, and transfers the
88 * socket structure to so_comp, making it available to accept().
89 *
90 * If a socket is closed with sockets on either
91 * so_incomp or so_comp, these sockets are dropped.
92 *
93 * If higher level protocols are implemented in
94 * the kernel, the wakeups done here will sometimes
95 * cause software-interrupt process scheduling.
96 */
97
98void
99soisconnecting(so)
100 register struct socket *so;
101{
102
103 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
104 so->so_state |= SS_ISCONNECTING;
105}
106
107void
108soisconnected(so)
109 struct socket *so;
110{
111 struct socket *head = so->so_head;
112
113 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
114 so->so_state |= SS_ISCONNECTED;
115 if (head && (so->so_state & SS_INCOMP)) {
116 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
117 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
118 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
119 so->so_rcv.sb_flags |= SB_UPCALL;
120 so->so_options &= ~SO_ACCEPTFILTER;
121 so->so_upcall(so, so->so_upcallarg, 0);
122 return;
123 }
124 TAILQ_REMOVE(&head->so_incomp, so, so_list);
125 head->so_incqlen--;
126 so->so_state &= ~SS_INCOMP;
127 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
128 head->so_qlen++;
129 so->so_state |= SS_COMP;
130 sorwakeup(head);
131 wakeup_one(&head->so_timeo);
132 } else {
133 wakeup(&so->so_timeo);
134 sorwakeup(so);
135 sowwakeup(so);
136 }
137}
138
139void
140soisdisconnecting(so)
141 register struct socket *so;
142{
143
144 so->so_state &= ~SS_ISCONNECTING;
145 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
146 wakeup((caddr_t)&so->so_timeo);
147 sowwakeup(so);
148 sorwakeup(so);
149}
150
151void
152soisdisconnected(so)
153 register struct socket *so;
154{
155
156 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
157 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
158 wakeup((caddr_t)&so->so_timeo);
159 sbdrop(&so->so_snd, so->so_snd.sb_cc);
160 sowwakeup(so);
161 sorwakeup(so);
162}
163
164/*
165 * When an attempt at a new connection is noted on a socket
166 * which accepts connections, sonewconn is called. If the
167 * connection is possible (subject to space constraints, etc.)
168 * then we allocate a new structure, propoerly linked into the
169 * data structure of the original socket, and return this.
170 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
171 */
172struct socket *
173sonewconn(head, connstatus)
174 register struct socket *head;
175 int connstatus;
176{
177
178 return (sonewconn3(head, connstatus, NULL));
179}
180
181struct socket *
182sonewconn3(head, connstatus, p)
183 register struct socket *head;
184 int connstatus;
185 struct proc *p;
186{
187 register struct socket *so;
188
189 if (head->so_qlen > 3 * head->so_qlimit / 2)
190 return ((struct socket *)0);
191 so = soalloc(0);
192 if (so == NULL)
193 return ((struct socket *)0);
194 if ((head->so_options & SO_ACCEPTFILTER) != 0)
195 connstatus = 0;
196 so->so_head = head;
197 so->so_type = head->so_type;
198 so->so_options = head->so_options &~ SO_ACCEPTCONN;
199 so->so_linger = head->so_linger;
200 so->so_state = head->so_state | SS_NOFDREF;
201 so->so_proto = head->so_proto;
202 so->so_timeo = head->so_timeo;
203 so->so_cred = p ? p->p_ucred : head->so_cred;
204 crhold(so->so_cred);
205 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
206 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
207 sodealloc(so);
208 return ((struct socket *)0);
209 }
210
211 if (connstatus) {
212 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
213 so->so_state |= SS_COMP;
214 head->so_qlen++;
215 } else {
216 if (head->so_incqlen > head->so_qlimit) {
217 struct socket *sp;
218 sp = TAILQ_FIRST(&head->so_incomp);
219 (void) soabort(sp);
220 }
221 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
222 so->so_state |= SS_INCOMP;
223 head->so_incqlen++;
224 }
225 if (connstatus) {
226 sorwakeup(head);
227 wakeup((caddr_t)&head->so_timeo);
228 so->so_state |= connstatus;
229 }
230 return (so);
231}
232
233/*
234 * Socantsendmore indicates that no more data will be sent on the
235 * socket; it would normally be applied to a socket when the user
236 * informs the system that no more data is to be sent, by the protocol
237 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
238 * will be received, and will normally be applied to the socket by a
239 * protocol when it detects that the peer will send no more data.
240 * Data queued for reading in the socket may yet be read.
241 */
242
243void
244socantsendmore(so)
245 struct socket *so;
246{
247
248 so->so_state |= SS_CANTSENDMORE;
249 sowwakeup(so);
250}
251
252void
253socantrcvmore(so)
254 struct socket *so;
255{
256
257 so->so_state |= SS_CANTRCVMORE;
258 sorwakeup(so);
259}
260
261/*
262 * Wait for data to arrive at/drain from a socket buffer.
263 */
264int
265sbwait(sb)
266 struct sockbuf *sb;
267{
268
269 sb->sb_flags |= SB_WAIT;
270 return (tsleep((caddr_t)&sb->sb_cc,
271 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
272 sb->sb_timeo));
273}
274
275/*
276 * Lock a sockbuf already known to be locked;
277 * return any error returned from sleep (EINTR).
278 */
279int
280sb_lock(sb)
281 register struct sockbuf *sb;
282{
283 int error;
284
285 while (sb->sb_flags & SB_LOCK) {
286 sb->sb_flags |= SB_WANT;
287 error = tsleep((caddr_t)&sb->sb_flags,
288 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
289 "sblock", 0);
290 if (error)
291 return (error);
292 }
293 sb->sb_flags |= SB_LOCK;
294 return (0);
295}
296
297/*
298 * Wakeup processes waiting on a socket buffer.
299 * Do asynchronous notification via SIGIO
300 * if the socket has the SS_ASYNC flag set.
301 */
302void
303sowakeup(so, sb)
304 register struct socket *so;
305 register struct sockbuf *sb;
306{
307 selwakeup(&sb->sb_sel);
308 sb->sb_flags &= ~SB_SEL;
309 if (sb->sb_flags & SB_WAIT) {
310 sb->sb_flags &= ~SB_WAIT;
311 wakeup((caddr_t)&sb->sb_cc);
312 }
313 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
314 pgsigio(so->so_sigio, SIGIO, 0);
315 if (sb->sb_flags & SB_UPCALL)
316 (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
317 if (sb->sb_flags & SB_AIO)
318 aio_swake(so, sb);
319 KNOTE(&sb->sb_sel.si_note, 0);
320}
321
322/*
323 * Socket buffer (struct sockbuf) utility routines.
324 *
325 * Each socket contains two socket buffers: one for sending data and
326 * one for receiving data. Each buffer contains a queue of mbufs,
327 * information about the number of mbufs and amount of data in the
328 * queue, and other fields allowing select() statements and notification
329 * on data availability to be implemented.
330 *
331 * Data stored in a socket buffer is maintained as a list of records.
332 * Each record is a list of mbufs chained together with the m_next
333 * field. Records are chained together with the m_nextpkt field. The upper
334 * level routine soreceive() expects the following conventions to be
335 * observed when placing information in the receive buffer:
336 *
337 * 1. If the protocol requires each message be preceded by the sender's
338 * name, then a record containing that name must be present before
339 * any associated data (mbuf's must be of type MT_SONAME).
340 * 2. If the protocol supports the exchange of ``access rights'' (really
341 * just additional data associated with the message), and there are
342 * ``rights'' to be received, then a record containing this data
343 * should be present (mbuf's must be of type MT_RIGHTS).
344 * 3. If a name or rights record exists, then it must be followed by
345 * a data record, perhaps of zero length.
346 *
347 * Before using a new socket structure it is first necessary to reserve
348 * buffer space to the socket, by calling sbreserve(). This should commit
349 * some of the available buffer space in the system buffer pool for the
350 * socket (currently, it does nothing but enforce limits). The space
351 * should be released by calling sbrelease() when the socket is destroyed.
352 */
353
354int
355soreserve(so, sndcc, rcvcc)
356 register struct socket *so;
357 u_long sndcc, rcvcc;
358{
359 struct proc *p = curproc;
360
361 if (sbreserve(&so->so_snd, sndcc, so, p) == 0)
362 goto bad;
363 if (sbreserve(&so->so_rcv, rcvcc, so, p) == 0)
364 goto bad2;
365 if (so->so_rcv.sb_lowat == 0)
366 so->so_rcv.sb_lowat = 1;
367 if (so->so_snd.sb_lowat == 0)
368 so->so_snd.sb_lowat = MCLBYTES;
369 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
370 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
371 return (0);
372bad2:
373 sbrelease(&so->so_snd, so);
374bad:
375 return (ENOBUFS);
376}
377
378static int
379sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
380{
381 int error = 0;
382 u_long old_sb_max = sb_max;
383
384 error = SYSCTL_OUT(req, arg1, sizeof(int));
385 if (error || !req->newptr)
386 return (error);
387 error = SYSCTL_IN(req, arg1, sizeof(int));
388 if (error)
389 return (error);
390 if (sb_max < MSIZE + MCLBYTES) {
391 sb_max = old_sb_max;
392 return (EINVAL);
393 }
394 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
395 return (0);
396}
397
398/*
399 * Allot mbufs to a sockbuf.
400 * Attempt to scale mbmax so that mbcnt doesn't become limiting
401 * if buffering efficiency is near the normal case.
402 */
403int
404sbreserve(sb, cc, so, p)
405 struct sockbuf *sb;
406 u_long cc;
407 struct socket *so;
408 struct proc *p;
409{
410
411 /*
412 * p will only be NULL when we're in an interrupt
413 * (e.g. in tcp_input())
414 */
415 if (cc > sb_max_adj)
416 return (0);
417 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
418 p ? p->p_rlimit[RLIMIT_SBSIZE].rlim_cur : RLIM_INFINITY)) {
419 return (0);
420 }
421 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
422 if (sb->sb_lowat > sb->sb_hiwat)
423 sb->sb_lowat = sb->sb_hiwat;
424 return (1);
425}
426
427/*
428 * Free mbufs held by a socket, and reserved mbuf space.
429 */
430void
431sbrelease(sb, so)
432 struct sockbuf *sb;
433 struct socket *so;
434{
435
436 sbflush(sb);
437 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
438 RLIM_INFINITY);
439 sb->sb_mbmax = 0;
440}
441
442/*
443 * Routines to add and remove
444 * data from an mbuf queue.
445 *
446 * The routines sbappend() or sbappendrecord() are normally called to
447 * append new mbufs to a socket buffer, after checking that adequate
448 * space is available, comparing the function sbspace() with the amount
449 * of data to be added. sbappendrecord() differs from sbappend() in
450 * that data supplied is treated as the beginning of a new record.
451 * To place a sender's address, optional access rights, and data in a
452 * socket receive buffer, sbappendaddr() should be used. To place
453 * access rights and data in a socket receive buffer, sbappendrights()
454 * should be used. In either case, the new data begins a new record.
455 * Note that unlike sbappend() and sbappendrecord(), these routines check
456 * for the caller that there will be enough space to store the data.
457 * Each fails if there is not enough space, or if it cannot find mbufs
458 * to store additional information in.
459 *
460 * Reliable protocols may use the socket send buffer to hold data
461 * awaiting acknowledgement. Data is normally copied from a socket
462 * send buffer in a protocol with m_copy for output to a peer,
463 * and then removing the data from the socket buffer with sbdrop()
464 * or sbdroprecord() when the data is acknowledged by the peer.
465 */
466
467/*
468 * Append mbuf chain m to the last record in the
469 * socket buffer sb. The additional space associated
470 * the mbuf chain is recorded in sb. Empty mbufs are
471 * discarded and mbufs are compacted where possible.
472 */
473void
474sbappend(sb, m)
475 struct sockbuf *sb;
476 struct mbuf *m;
477{
478 register struct mbuf *n;
479
480 if (m == 0)
481 return;
482 n = sb->sb_mb;
483 if (n) {
484 while (n->m_nextpkt)
485 n = n->m_nextpkt;
486 do {
487 if (n->m_flags & M_EOR) {
488 sbappendrecord(sb, m); /* XXXXXX!!!! */
489 return;
490 }
491 } while (n->m_next && (n = n->m_next));
492 }
493 sbcompress(sb, m, n);
494}
495
496#ifdef SOCKBUF_DEBUG
497void
498sbcheck(sb)
499 register struct sockbuf *sb;
500{
501 register struct mbuf *m;
502 register struct mbuf *n = 0;
503 register u_long len = 0, mbcnt = 0;
504
505 for (m = sb->sb_mb; m; m = n) {
506 n = m->m_nextpkt;
507 for (; m; m = m->m_next) {
508 len += m->m_len;
509 mbcnt += MSIZE;
510 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
511 mbcnt += m->m_ext.ext_size;
512 }
513 }
514 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
515 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
516 mbcnt, sb->sb_mbcnt);
517 panic("sbcheck");
518 }
519}
520#endif
521
522/*
523 * As above, except the mbuf chain
524 * begins a new record.
525 */
526void
527sbappendrecord(sb, m0)
528 register struct sockbuf *sb;
529 register struct mbuf *m0;
530{
531 register struct mbuf *m;
532
533 if (m0 == 0)
534 return;
535 m = sb->sb_mb;
536 if (m)
537 while (m->m_nextpkt)
538 m = m->m_nextpkt;
539 /*
540 * Put the first mbuf on the queue.
541 * Note this permits zero length records.
542 */
543 sballoc(sb, m0);
544 if (m)
545 m->m_nextpkt = m0;
546 else
547 sb->sb_mb = m0;
548 m = m0->m_next;
549 m0->m_next = 0;
550 if (m && (m0->m_flags & M_EOR)) {
551 m0->m_flags &= ~M_EOR;
552 m->m_flags |= M_EOR;
553 }
554 sbcompress(sb, m, m0);
555}
556
557/*
558 * As above except that OOB data
559 * is inserted at the beginning of the sockbuf,
560 * but after any other OOB data.
561 */
562void
563sbinsertoob(sb, m0)
564 register struct sockbuf *sb;
565 register struct mbuf *m0;
566{
567 register struct mbuf *m;
568 register struct mbuf **mp;
569
570 if (m0 == 0)
571 return;
572 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
573 m = *mp;
574 again:
575 switch (m->m_type) {
576
577 case MT_OOBDATA:
578 continue; /* WANT next train */
579
580 case MT_CONTROL:
581 m = m->m_next;
582 if (m)
583 goto again; /* inspect THIS train further */
584 }
585 break;
586 }
587 /*
588 * Put the first mbuf on the queue.
589 * Note this permits zero length records.
590 */
591 sballoc(sb, m0);
592 m0->m_nextpkt = *mp;
593 *mp = m0;
594 m = m0->m_next;
595 m0->m_next = 0;
596 if (m && (m0->m_flags & M_EOR)) {
597 m0->m_flags &= ~M_EOR;
598 m->m_flags |= M_EOR;
599 }
600 sbcompress(sb, m, m0);
601}
602
603/*
604 * Append address and data, and optionally, control (ancillary) data
605 * to the receive queue of a socket. If present,
606 * m0 must include a packet header with total length.
607 * Returns 0 if no space in sockbuf or insufficient mbufs.
608 */
609int
610sbappendaddr(sb, asa, m0, control)
611 register struct sockbuf *sb;
612 struct sockaddr *asa;
613 struct mbuf *m0, *control;
614{
615 register struct mbuf *m, *n;
616 int space = asa->sa_len;
617
618if (m0 && (m0->m_flags & M_PKTHDR) == 0)
619panic("sbappendaddr");
620 if (m0)
621 space += m0->m_pkthdr.len;
622 for (n = control; n; n = n->m_next) {
623 space += n->m_len;
624 if (n->m_next == 0) /* keep pointer to last control buf */
625 break;
626 }
627 if (space > sbspace(sb))
628 return (0);
629 if (asa->sa_len > MLEN)
630 return (0);
631 MGET(m, M_DONTWAIT, MT_SONAME);
632 if (m == 0)
633 return (0);
634 m->m_len = asa->sa_len;
635 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
636 if (n)
637 n->m_next = m0; /* concatenate data to control */
638 else
639 control = m0;
640 m->m_next = control;
641 for (n = m; n; n = n->m_next)
642 sballoc(sb, n);
643 n = sb->sb_mb;
644 if (n) {
645 while (n->m_nextpkt)
646 n = n->m_nextpkt;
647 n->m_nextpkt = m;
648 } else
649 sb->sb_mb = m;
650 return (1);
651}
652
653int
654sbappendcontrol(sb, m0, control)
655 struct sockbuf *sb;
656 struct mbuf *control, *m0;
657{
658 register struct mbuf *m, *n;
659 int space = 0;
660
661 if (control == 0)
662 panic("sbappendcontrol");
663 for (m = control; ; m = m->m_next) {
664 space += m->m_len;
665 if (m->m_next == 0)
666 break;
667 }
668 n = m; /* save pointer to last control buffer */
669 for (m = m0; m; m = m->m_next)
670 space += m->m_len;
671 if (space > sbspace(sb))
672 return (0);
673 n->m_next = m0; /* concatenate data to control */
674 for (m = control; m; m = m->m_next)
675 sballoc(sb, m);
676 n = sb->sb_mb;
677 if (n) {
678 while (n->m_nextpkt)
679 n = n->m_nextpkt;
680 n->m_nextpkt = control;
681 } else
682 sb->sb_mb = control;
683 return (1);
684}
685
686/*
687 * Compress mbuf chain m into the socket
688 * buffer sb following mbuf n. If n
689 * is null, the buffer is presumed empty.
690 */
691void
692sbcompress(sb, m, n)
693 register struct sockbuf *sb;
694 register struct mbuf *m, *n;
695{
696 register int eor = 0;
697 register struct mbuf *o;
698
699 while (m) {
700 eor |= m->m_flags & M_EOR;
701 if (m->m_len == 0 &&
702 (eor == 0 ||
703 (((o = m->m_next) || (o = n)) &&
704 o->m_type == m->m_type))) {
705 m = m_free(m);
706 continue;
707 }
708 if (n && (n->m_flags & M_EOR) == 0 &&
709 M_WRITABLE(n) &&
710 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
711 m->m_len <= M_TRAILINGSPACE(n) &&
712 n->m_type == m->m_type) {
713 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
714 (unsigned)m->m_len);
715 n->m_len += m->m_len;
716 sb->sb_cc += m->m_len;
717 m = m_free(m);
718 continue;
719 }
720 if (n)
721 n->m_next = m;
722 else
723 sb->sb_mb = m;
724 sballoc(sb, m);
725 n = m;
726 m->m_flags &= ~M_EOR;
727 m = m->m_next;
728 n->m_next = 0;
729 }
730 if (eor) {
731 if (n)
732 n->m_flags |= eor;
733 else
734 printf("semi-panic: sbcompress\n");
735 }
736}
737
738/*
739 * Free all mbufs in a sockbuf.
740 * Check that all resources are reclaimed.
741 */
742void
743sbflush(sb)
744 register struct sockbuf *sb;
745{
746
747 if (sb->sb_flags & SB_LOCK)
748 panic("sbflush: locked");
749 while (sb->sb_mbcnt) {
750 /*
751 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
752 * we would loop forever. Panic instead.
753 */
754 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
755 break;
756 sbdrop(sb, (int)sb->sb_cc);
757 }
758 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
759 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
760}
761
762/*
763 * Drop data from (the front of) a sockbuf.
764 */
765void
766sbdrop(sb, len)
767 register struct sockbuf *sb;
768 register int len;
769{
770 register struct mbuf *m;
771 struct mbuf *next;
772
773 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
774 while (len > 0) {
775 if (m == 0) {
776 if (next == 0)
777 panic("sbdrop");
778 m = next;
779 next = m->m_nextpkt;
780 continue;
781 }
782 if (m->m_len > len) {
783 m->m_len -= len;
784 m->m_data += len;
785 sb->sb_cc -= len;
786 break;
787 }
788 len -= m->m_len;
789 sbfree(sb, m);
790 m = m_free(m);
791 }
792 while (m && m->m_len == 0) {
793 sbfree(sb, m);
794 m = m_free(m);
795 }
796 if (m) {
797 sb->sb_mb = m;
798 m->m_nextpkt = next;
799 } else
800 sb->sb_mb = next;
801}
802
803/*
804 * Drop a record off the front of a sockbuf
805 * and move the next record to the front.
806 */
807void
808sbdroprecord(sb)
809 register struct sockbuf *sb;
810{
811 register struct mbuf *m;
812
813 m = sb->sb_mb;
814 if (m) {
815 sb->sb_mb = m->m_nextpkt;
816 do {
817 sbfree(sb, m);
818 m = m_free(m);
819 } while (m);
820 }
821}
822
823/*
824 * Create a "control" mbuf containing the specified data
825 * with the specified type for presentation on a socket buffer.
826 */
827struct mbuf *
828sbcreatecontrol(p, size, type, level)
829 caddr_t p;
830 register int size;
831 int type, level;
832{
833 register struct cmsghdr *cp;
834 struct mbuf *m;
835
836 if (CMSG_SPACE((u_int)size) > MCLBYTES)
837 return ((struct mbuf *) NULL);
838 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
839 return ((struct mbuf *) NULL);
840 if (CMSG_SPACE((u_int)size) > MLEN) {
841 MCLGET(m, M_DONTWAIT);
842 if ((m->m_flags & M_EXT) == 0) {
843 m_free(m);
844 return ((struct mbuf *) NULL);
845 }
846 }
847 cp = mtod(m, struct cmsghdr *);
848 m->m_len = 0;
849 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
850 ("sbcreatecontrol: short mbuf"));
851 if (p != NULL)
852 (void)memcpy(CMSG_DATA(cp), p, size);
853 m->m_len = CMSG_SPACE(size);
854 cp->cmsg_len = CMSG_LEN(size);
855 cp->cmsg_level = level;
856 cp->cmsg_type = type;
857 return (m);
858}
859
860/*
861 * Some routines that return EOPNOTSUPP for entry points that are not
862 * supported by a protocol. Fill in as needed.
863 */
864int
865pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
866{
867 return EOPNOTSUPP;
868}
869
870int
871pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
872{
873 return EOPNOTSUPP;
874}
875
876int
877pru_connect2_notsupp(struct socket *so1, struct socket *so2)
878{
879 return EOPNOTSUPP;
880}
881
882int
883pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
884 struct ifnet *ifp, struct proc *p)
885{
886 return EOPNOTSUPP;
887}
888
889int
890pru_listen_notsupp(struct socket *so, struct proc *p)
891{
892 return EOPNOTSUPP;
893}
894
895int
896pru_rcvd_notsupp(struct socket *so, int flags)
897{
898 return EOPNOTSUPP;
899}
900
901int
902pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
903{
904 return EOPNOTSUPP;
905}
906
907/*
908 * This isn't really a ``null'' operation, but it's the default one
909 * and doesn't do anything destructive.
910 */
911int
912pru_sense_null(struct socket *so, struct stat *sb)
913{
914 sb->st_blksize = so->so_snd.sb_hiwat;
915 return 0;
916}
917
918/*
919 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
920 */
921struct sockaddr *
922dup_sockaddr(sa, canwait)
923 struct sockaddr *sa;
924 int canwait;
925{
926 struct sockaddr *sa2;
927
928 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
929 canwait ? M_WAITOK : M_NOWAIT);
930 if (sa2)
931 bcopy(sa, sa2, sa->sa_len);
932 return sa2;
933}
934
935/*
936 * Create an external-format (``xsocket'') structure using the information
937 * in the kernel-format socket structure pointed to by so. This is done
938 * to reduce the spew of irrelevant information over this interface,
939 * to isolate user code from changes in the kernel structure, and
940 * potentially to provide information-hiding if we decide that
941 * some of this information should be hidden from users.
942 */
943void
944sotoxsocket(struct socket *so, struct xsocket *xso)
945{
946 xso->xso_len = sizeof *xso;
947 xso->xso_so = so;
948 xso->so_type = so->so_type;
949 xso->so_options = so->so_options;
950 xso->so_linger = so->so_linger;
951 xso->so_state = so->so_state;
952 xso->so_pcb = so->so_pcb;
953 xso->xso_protocol = so->so_proto->pr_protocol;
954 xso->xso_family = so->so_proto->pr_domain->dom_family;
955 xso->so_qlen = so->so_qlen;
956 xso->so_incqlen = so->so_incqlen;
957 xso->so_qlimit = so->so_qlimit;
958 xso->so_timeo = so->so_timeo;
959 xso->so_error = so->so_error;
960 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
961 xso->so_oobmark = so->so_oobmark;
962 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
963 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
964 xso->so_uid = so->so_cred->cr_uid;
965}
966
967/*
968 * This does the same for sockbufs. Note that the xsockbuf structure,
969 * since it is always embedded in a socket, does not include a self
970 * pointer nor a length. We make this entry point public in case
971 * some other mechanism needs it.
972 */
973void
974sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
975{
976 xsb->sb_cc = sb->sb_cc;
977 xsb->sb_hiwat = sb->sb_hiwat;
978 xsb->sb_mbcnt = sb->sb_mbcnt;
979 xsb->sb_mbmax = sb->sb_mbmax;
980 xsb->sb_lowat = sb->sb_lowat;
981 xsb->sb_flags = sb->sb_flags;
982 xsb->sb_timeo = sb->sb_timeo;
983}
984
985/*
986 * Here is the definition of some of the basic objects in the kern.ipc
987 * branch of the MIB.
988 */
989SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
990
991/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
992static int dummy;
993SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
994SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
995 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
996SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
997 &maxsockets, 0, "Maximum number of sockets avaliable");
998SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
999 &sb_efficiency, 0, "");
1000
1001/*
1002 * Initialise maxsockets
1003 */
1004static void init_maxsockets(void *ignored)
1005{
1006 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1007 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1008}
1009SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);