source: rtems/cpukit/libnetworking/kern/uipc_socket2.c @ d506dff

4.104.114.84.9
Last change on this file since d506dff was d506dff, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2007 at 4:48:01 AM

Reflect changes to sys/protosw.h.

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