2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
2001-06-13 21:52:19 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <sys/types.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
2002-08-27 20:11:58 +00:00
|
|
|
#include <stdarg.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-06-15 19:06:25 +00:00
|
|
|
#include "defs.h"
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "main.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-12-20 20:29:51 +00:00
|
|
|
#define BUCKET_CHUNK 20
|
|
|
|
#define BUCKET_HASH 256
|
|
|
|
|
|
|
|
struct mbucket;
|
|
|
|
|
|
|
|
struct mfree {
|
|
|
|
struct mbucket *next;
|
|
|
|
size_t count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mbucket {
|
|
|
|
union {
|
|
|
|
struct mbuf m;
|
|
|
|
struct mfree f;
|
|
|
|
} u;
|
|
|
|
} *bucket[(M_MAXLEN + sizeof(struct mbuf)) / BUCKET_HASH];
|
|
|
|
|
|
|
|
#define M_BINDEX(sz) (((sz) + sizeof(struct mbuf) - 1) / BUCKET_HASH)
|
|
|
|
#define M_BUCKET(sz) (bucket + M_BINDEX(sz))
|
|
|
|
#define M_ROUNDUP(sz) ((M_BINDEX(sz) + 1) * BUCKET_HASH)
|
|
|
|
|
1998-01-21 02:15:33 +00:00
|
|
|
static struct memmap {
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mbuf *queue;
|
1999-12-20 20:29:47 +00:00
|
|
|
size_t fragments;
|
|
|
|
size_t octets;
|
1999-06-02 15:59:09 +00:00
|
|
|
} MemMap[MB_MAX + 1];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
static unsigned long long mbuf_Mallocs, mbuf_Frees;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2004-09-05 01:46:52 +00:00
|
|
|
size_t
|
1999-12-20 20:29:47 +00:00
|
|
|
m_length(struct mbuf *bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2004-09-05 01:46:52 +00:00
|
|
|
size_t len;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
for (len = 0; bp; bp = bp->m_next)
|
|
|
|
len += bp->m_len;
|
1999-05-09 20:02:29 +00:00
|
|
|
return len;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
2000-10-30 00:15:27 +00:00
|
|
|
static const char *
|
2000-10-27 05:34:40 +00:00
|
|
|
mbuftype(int type)
|
|
|
|
{
|
2002-06-15 08:03:30 +00:00
|
|
|
static const char * const mbufdesc[MB_MAX] = {
|
2001-08-14 16:05:52 +00:00
|
|
|
"ip in", "ip out", "ipv6 in", "ipv6 out", "nat in", "nat out",
|
|
|
|
"mp in", "mp out", "vj in", "vj out", "icompd in", "icompd out",
|
|
|
|
"compd in", "compd out", "lqr in", "lqr out", "echo in", "echo out",
|
|
|
|
"proto in", "proto out", "acf in", "acf out", "sync in", "sync out",
|
|
|
|
"hdlc in", "hdlc out", "async in", "async out", "cbcp in", "cbcp out",
|
|
|
|
"chap in", "chap out", "pap in", "pap out", "ccp in", "ccp out",
|
|
|
|
"ipcp in", "ipcp out", "ipv6cp in", "ipv6cp out", "lcp in", "lcp out"
|
2000-10-27 05:34:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return type < 0 || type >= MB_MAX ? "unknown" : mbufdesc[type];
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mbuf *
|
1999-12-20 20:29:47 +00:00
|
|
|
m_get(size_t m_len, int type)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-12-20 20:29:51 +00:00
|
|
|
struct mbucket **mb;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mbuf *bp;
|
1999-12-20 20:29:51 +00:00
|
|
|
size_t size;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-06-02 15:59:09 +00:00
|
|
|
if (type > MB_MAX) {
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogERROR, "Bad mbuf type %d\n", type);
|
1999-06-02 15:59:09 +00:00
|
|
|
type = MB_UNKNOWN;
|
|
|
|
}
|
2002-06-15 08:03:30 +00:00
|
|
|
|
1999-12-20 20:29:51 +00:00
|
|
|
if (m_len > M_MAXLEN || m_len == 0) {
|
2000-10-27 05:34:40 +00:00
|
|
|
log_Printf(LogERROR, "Request for mbuf size %lu (\"%s\") denied !\n",
|
|
|
|
(u_long)m_len, mbuftype(type));
|
1998-05-21 21:49:08 +00:00
|
|
|
AbortProgram(EX_OSERR);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1999-12-20 20:29:51 +00:00
|
|
|
|
|
|
|
mb = M_BUCKET(m_len);
|
|
|
|
size = M_ROUNDUP(m_len);
|
|
|
|
|
|
|
|
if (*mb) {
|
|
|
|
/* We've got some free blocks of the right size */
|
|
|
|
bp = &(*mb)->u.m;
|
|
|
|
if (--(*mb)->u.f.count == 0)
|
|
|
|
*mb = (*mb)->u.f.next;
|
|
|
|
else {
|
|
|
|
((struct mbucket *)((char *)*mb + size))->u.f.count = (*mb)->u.f.count;
|
|
|
|
*mb = (struct mbucket *)((char *)*mb + size);
|
|
|
|
(*mb)->u.f.next = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Allocate another chunk of mbufs, use the first and put the rest on
|
|
|
|
* the free list
|
|
|
|
*/
|
|
|
|
*mb = (struct mbucket *)malloc(BUCKET_CHUNK * size);
|
|
|
|
if (*mb == NULL) {
|
2000-03-14 01:47:19 +00:00
|
|
|
log_Printf(LogALERT, "Failed to allocate memory (%lu)\n",
|
|
|
|
(unsigned long)BUCKET_CHUNK * size);
|
1999-12-20 20:29:51 +00:00
|
|
|
AbortProgram(EX_OSERR);
|
|
|
|
}
|
|
|
|
bp = &(*mb)->u.m;
|
|
|
|
*mb = (struct mbucket *)((char *)*mb + size);
|
|
|
|
(*mb)->u.f.count = BUCKET_CHUNK - 1;
|
|
|
|
(*mb)->u.f.next = NULL;
|
|
|
|
}
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Mallocs++;
|
1999-12-20 20:29:51 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(bp, '\0', sizeof(struct mbuf));
|
1999-12-20 20:29:51 +00:00
|
|
|
bp->m_size = size - sizeof *bp;
|
|
|
|
bp->m_len = m_len;
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_type = type;
|
1999-12-20 20:29:51 +00:00
|
|
|
|
|
|
|
MemMap[type].fragments++;
|
|
|
|
MemMap[type].octets += bp->m_size;
|
|
|
|
|
1999-03-29 08:21:28 +00:00
|
|
|
return bp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
1999-12-20 20:29:47 +00:00
|
|
|
m_free(struct mbuf *bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-12-20 20:29:51 +00:00
|
|
|
struct mbucket **mb, *f;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mbuf *nbp;
|
|
|
|
|
1999-12-20 20:29:51 +00:00
|
|
|
if ((f = (struct mbucket *)bp) != NULL) {
|
1999-12-20 20:29:47 +00:00
|
|
|
MemMap[bp->m_type].fragments--;
|
|
|
|
MemMap[bp->m_type].octets -= bp->m_size;
|
1999-12-20 20:29:51 +00:00
|
|
|
|
|
|
|
nbp = bp->m_next;
|
|
|
|
mb = M_BUCKET(bp->m_size);
|
|
|
|
f->u.f.next = *mb;
|
|
|
|
f->u.f.count = 1;
|
|
|
|
*mb = f;
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Frees++;
|
1999-03-29 08:21:28 +00:00
|
|
|
bp = nbp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-03-29 08:21:28 +00:00
|
|
|
|
|
|
|
return bp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(struct mbuf *bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
while (bp)
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_free(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Read(struct mbuf *bp, void *v, size_t len)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int nb;
|
1999-05-08 11:07:56 +00:00
|
|
|
u_char *ptr = v;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
while (bp && len > 0) {
|
1999-12-20 20:29:47 +00:00
|
|
|
if (len > bp->m_len)
|
|
|
|
nb = bp->m_len;
|
1995-01-31 06:29:58 +00:00
|
|
|
else
|
|
|
|
nb = len;
|
1999-05-08 11:07:56 +00:00
|
|
|
if (nb) {
|
|
|
|
memcpy(ptr, MBUF_CTOP(bp), nb);
|
|
|
|
ptr += nb;
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_len -= nb;
|
1999-05-08 11:07:56 +00:00
|
|
|
len -= nb;
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_offset += nb;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
if (bp->m_len == 0)
|
|
|
|
bp = m_free(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
while (bp && bp->m_len == 0)
|
|
|
|
bp = m_free(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
mbuf_View(struct mbuf *bp, void *v, size_t len)
|
|
|
|
{
|
|
|
|
size_t nb, l = len;
|
|
|
|
u_char *ptr = v;
|
|
|
|
|
|
|
|
while (bp && l > 0) {
|
1999-12-20 20:29:47 +00:00
|
|
|
if (l > bp->m_len)
|
|
|
|
nb = bp->m_len;
|
1999-05-08 11:07:56 +00:00
|
|
|
else
|
|
|
|
nb = l;
|
|
|
|
memcpy(ptr, MBUF_CTOP(bp), nb);
|
|
|
|
ptr += nb;
|
|
|
|
l -= nb;
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = bp->m_next;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len - l;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
2004-09-05 01:46:52 +00:00
|
|
|
m_prepend(struct mbuf *bp, const void *ptr, size_t len, u_short extra)
|
1999-05-08 11:07:56 +00:00
|
|
|
{
|
|
|
|
struct mbuf *head;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
if (bp && bp->m_offset) {
|
|
|
|
if (bp->m_offset >= len) {
|
|
|
|
bp->m_offset -= len;
|
|
|
|
bp->m_len += len;
|
2004-04-17 00:29:17 +00:00
|
|
|
if (ptr)
|
|
|
|
memcpy(MBUF_CTOP(bp), ptr, len);
|
1999-05-08 11:07:56 +00:00
|
|
|
return bp;
|
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
len -= bp->m_offset;
|
2004-04-17 00:29:17 +00:00
|
|
|
if (ptr)
|
|
|
|
memcpy(bp + 1, (const char *)ptr + len, bp->m_offset);
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_len += bp->m_offset;
|
|
|
|
bp->m_offset = 0;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
head = m_get(len + extra, bp ? bp->m_type : MB_UNKNOWN);
|
|
|
|
head->m_offset = extra;
|
|
|
|
head->m_len -= extra;
|
2000-01-03 20:09:23 +00:00
|
|
|
if (ptr)
|
|
|
|
memcpy(MBUF_CTOP(head), ptr, len);
|
1999-12-20 20:29:47 +00:00
|
|
|
head->m_next = bp;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
1999-12-20 20:29:47 +00:00
|
|
|
m_adj(struct mbuf *bp, ssize_t n)
|
1999-05-08 11:07:56 +00:00
|
|
|
{
|
1999-12-20 20:29:47 +00:00
|
|
|
if (n > 0) {
|
|
|
|
while (bp) {
|
2004-09-05 01:46:52 +00:00
|
|
|
if ((size_t)n < bp->m_len) {
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_len = n;
|
|
|
|
bp->m_offset += n;
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
n -= bp->m_len;
|
|
|
|
bp = m_free(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
} else {
|
|
|
|
if ((n = m_length(bp) + n) <= 0) {
|
|
|
|
m_freem(bp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (; bp; bp = bp->m_next, n -= bp->m_len)
|
2004-09-05 01:46:52 +00:00
|
|
|
if ((size_t)n < bp->m_len) {
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_len = n;
|
|
|
|
m_freem(bp->m_next);
|
|
|
|
bp->m_next = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
return bp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1999-12-20 20:29:47 +00:00
|
|
|
mbuf_Write(struct mbuf *bp, const void *ptr, size_t m_len)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2004-09-05 01:46:52 +00:00
|
|
|
size_t plen;
|
1995-01-31 06:29:58 +00:00
|
|
|
int nb;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
plen = m_length(bp);
|
|
|
|
if (plen < m_len)
|
|
|
|
m_len = plen;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
while (m_len > 0) {
|
|
|
|
nb = (m_len < bp->m_len) ? m_len : bp->m_len;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), ptr, nb);
|
1999-12-20 20:29:47 +00:00
|
|
|
m_len -= bp->m_len;
|
|
|
|
bp = bp->m_next;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-21 21:49:08 +00:00
|
|
|
mbuf_Show(struct cmdargs const *arg)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1998-08-21 18:09:57 +00:00
|
|
|
prompt_Printf(arg->prompt, "Fragments (octets) in use:\n");
|
1999-06-02 15:59:09 +00:00
|
|
|
for (i = 0; i < MB_MAX; i += 2)
|
1999-12-20 20:29:47 +00:00
|
|
|
prompt_Printf(arg->prompt, "%10.10s: %04lu (%06lu)\t"
|
|
|
|
"%10.10s: %04lu (%06lu)\n",
|
2000-10-27 05:34:40 +00:00
|
|
|
mbuftype(i), (u_long)MemMap[i].fragments,
|
|
|
|
(u_long)MemMap[i].octets, mbuftype(i+1),
|
1999-12-20 20:29:47 +00:00
|
|
|
(u_long)MemMap[i+1].fragments, (u_long)MemMap[i+1].octets);
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
if (i == MB_MAX)
|
1999-12-20 20:29:47 +00:00
|
|
|
prompt_Printf(arg->prompt, "%10.10s: %04lu (%06lu)\n",
|
2000-10-27 05:34:40 +00:00
|
|
|
mbuftype(i), (u_long)MemMap[i].fragments,
|
1999-12-20 20:29:47 +00:00
|
|
|
(u_long)MemMap[i].octets);
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1999-08-09 22:56:18 +00:00
|
|
|
prompt_Printf(arg->prompt, "Mallocs: %llu, Frees: %llu\n",
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Mallocs, mbuf_Frees);
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
struct mbuf *
|
1999-12-20 20:29:47 +00:00
|
|
|
m_dequeue(struct mqueue *q)
|
1998-05-21 21:49:08 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
2002-06-15 08:03:30 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
log_Printf(LogDEBUG, "m_dequeue: queue len = %lu\n", (u_long)q->len);
|
1998-05-21 21:49:08 +00:00
|
|
|
bp = q->top;
|
|
|
|
if (bp) {
|
1999-12-20 20:29:47 +00:00
|
|
|
q->top = q->top->m_nextpkt;
|
|
|
|
q->len--;
|
1998-05-21 21:49:08 +00:00
|
|
|
if (q->top == NULL) {
|
|
|
|
q->last = q->top;
|
1999-12-20 20:29:47 +00:00
|
|
|
if (q->len)
|
|
|
|
log_Printf(LogERROR, "m_dequeue: Not zero (%lu)!!!\n",
|
|
|
|
(u_long)q->len);
|
1998-05-21 21:49:08 +00:00
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
bp->m_nextpkt = NULL;
|
1998-05-21 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-12-20 20:29:47 +00:00
|
|
|
m_enqueue(struct mqueue *queue, struct mbuf *bp)
|
1998-05-21 21:49:08 +00:00
|
|
|
{
|
1999-05-09 20:02:29 +00:00
|
|
|
if (bp != NULL) {
|
|
|
|
if (queue->last) {
|
1999-12-20 20:29:47 +00:00
|
|
|
queue->last->m_nextpkt = bp;
|
1999-05-09 20:02:29 +00:00
|
|
|
queue->last = bp;
|
|
|
|
} else
|
|
|
|
queue->last = queue->top = bp;
|
1999-12-20 20:29:47 +00:00
|
|
|
queue->len++;
|
2000-03-14 01:47:19 +00:00
|
|
|
log_Printf(LogDEBUG, "m_enqueue: len = %lu\n", (unsigned long)queue->len);
|
1999-05-09 20:02:29 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-03-29 08:21:28 +00:00
|
|
|
|
|
|
|
struct mbuf *
|
1999-12-20 20:29:47 +00:00
|
|
|
m_pullup(struct mbuf *bp)
|
1999-03-29 08:21:28 +00:00
|
|
|
{
|
|
|
|
/* Put it all in one contigous (aligned) mbuf */
|
|
|
|
|
1999-05-09 20:02:29 +00:00
|
|
|
if (bp != NULL) {
|
1999-12-20 20:29:47 +00:00
|
|
|
if (bp->m_next != NULL) {
|
1999-05-09 20:02:29 +00:00
|
|
|
struct mbuf *nbp;
|
|
|
|
u_char *cp;
|
1999-03-29 08:21:28 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
nbp = m_get(m_length(bp), bp->m_type);
|
1999-03-29 08:21:28 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
for (cp = MBUF_CTOP(nbp); bp; bp = m_free(bp)) {
|
|
|
|
memcpy(cp, MBUF_CTOP(bp), bp->m_len);
|
|
|
|
cp += bp->m_len;
|
1999-05-09 20:02:29 +00:00
|
|
|
}
|
|
|
|
bp = nbp;
|
1999-03-29 08:21:28 +00:00
|
|
|
}
|
|
|
|
#ifndef __i386__ /* Do any other archs not care about alignment ? */
|
1999-12-20 20:29:47 +00:00
|
|
|
else if ((bp->m_offset & (sizeof(long) - 1)) != 0) {
|
|
|
|
bcopy(MBUF_CTOP(bp), bp + 1, bp->m_len);
|
|
|
|
bp->m_offset = 0;
|
1999-05-09 20:02:29 +00:00
|
|
|
}
|
1999-03-29 08:21:28 +00:00
|
|
|
#endif
|
1999-05-09 20:02:29 +00:00
|
|
|
}
|
1999-03-29 08:21:28 +00:00
|
|
|
|
|
|
|
return bp;
|
|
|
|
}
|
1999-06-02 15:59:09 +00:00
|
|
|
|
|
|
|
void
|
1999-12-20 20:29:47 +00:00
|
|
|
m_settype(struct mbuf *bp, int type)
|
1999-06-02 15:59:09 +00:00
|
|
|
{
|
1999-12-20 20:29:47 +00:00
|
|
|
for (; bp; bp = bp->m_next)
|
|
|
|
if (type != bp->m_type) {
|
|
|
|
MemMap[bp->m_type].fragments--;
|
|
|
|
MemMap[bp->m_type].octets -= bp->m_size;
|
|
|
|
bp->m_type = type;
|
1999-06-02 15:59:09 +00:00
|
|
|
MemMap[type].fragments++;
|
1999-12-20 20:29:47 +00:00
|
|
|
MemMap[type].octets += bp->m_size;
|
1999-06-02 15:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
2000-01-03 20:09:23 +00:00
|
|
|
|
|
|
|
struct mbuf *
|
2000-05-07 10:08:24 +00:00
|
|
|
m_append(struct mbuf *bp, const void *v, size_t sz)
|
2000-01-03 20:09:23 +00:00
|
|
|
{
|
2000-05-07 10:08:24 +00:00
|
|
|
struct mbuf *m = bp;
|
|
|
|
|
2000-01-03 20:09:23 +00:00
|
|
|
if (m) {
|
|
|
|
while (m->m_next)
|
|
|
|
m = m->m_next;
|
2004-04-17 00:29:17 +00:00
|
|
|
if (m->m_size - m->m_len >= sz) {
|
|
|
|
if (v)
|
|
|
|
memcpy((char *)(m + 1) + m->m_len, v, sz);
|
2000-01-03 20:09:23 +00:00
|
|
|
m->m_len += sz;
|
2004-04-17 00:29:17 +00:00
|
|
|
} else
|
2000-01-03 20:09:23 +00:00
|
|
|
m->m_next = m_prepend(NULL, v, sz, 0);
|
|
|
|
} else
|
2000-05-07 10:08:24 +00:00
|
|
|
bp = m_prepend(NULL, v, sz, 0);
|
2000-01-03 20:09:23 +00:00
|
|
|
|
2000-05-07 10:08:24 +00:00
|
|
|
return bp;
|
2000-01-03 20:09:23 +00:00
|
|
|
}
|