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