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