source: rtems/cpukit/libnetworking/kern/uipc_socket2.c @ 0e16fa45

5
Last change on this file since 0e16fa45 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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