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