2004-04-22 04:10:48 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski
|
|
|
|
*
|
|
|
|
* Please read the file COPYRIGHT for the
|
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mbuf's in SLiRP are much simpler than the real mbufs in
|
|
|
|
* FreeBSD. They are fixed size, determined by the MTU,
|
|
|
|
* so that one whole packet can fit. Mbuf's cannot be
|
|
|
|
* chained together. If there's more data than the mbuf
|
2017-02-05 02:08:34 +03:00
|
|
|
* could hold, an external g_malloced buffer is pointed to
|
2004-04-22 04:10:48 +04:00
|
|
|
* by m_ext (and the data pointers) and M_EXT is set in
|
|
|
|
* the flags
|
|
|
|
*/
|
|
|
|
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "slirp.h"
|
2004-04-22 04:10:48 +04:00
|
|
|
|
2007-10-26 23:01:16 +04:00
|
|
|
#define MBUF_THRESH 30
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a nice value for msize
|
|
|
|
*/
|
2016-03-15 12:31:20 +03:00
|
|
|
#define SLIRP_MSIZE\
|
|
|
|
(offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
|
2004-04-22 04:10:48 +04:00
|
|
|
|
|
|
|
void
|
2009-06-24 16:42:31 +04:00
|
|
|
m_init(Slirp *slirp)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
2016-02-23 00:29:21 +03:00
|
|
|
slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
|
|
|
|
slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &slirp->m_usedlist;
|
2004-04-22 04:10:48 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 22:14:23 +04:00
|
|
|
void m_cleanup(Slirp *slirp)
|
|
|
|
{
|
|
|
|
struct mbuf *m, *next;
|
|
|
|
|
2016-02-23 00:29:21 +03:00
|
|
|
m = (struct mbuf *) slirp->m_usedlist.qh_link;
|
|
|
|
while ((struct quehead *) m != &slirp->m_usedlist) {
|
2012-02-29 22:14:23 +04:00
|
|
|
next = m->m_next;
|
|
|
|
if (m->m_flags & M_EXT) {
|
2017-02-05 02:08:34 +03:00
|
|
|
g_free(m->m_ext);
|
2012-02-29 22:14:23 +04:00
|
|
|
}
|
2017-02-05 02:08:34 +03:00
|
|
|
g_free(m);
|
2012-02-29 22:14:23 +04:00
|
|
|
m = next;
|
|
|
|
}
|
2016-02-23 00:29:21 +03:00
|
|
|
m = (struct mbuf *) slirp->m_freelist.qh_link;
|
|
|
|
while ((struct quehead *) m != &slirp->m_freelist) {
|
2012-02-29 22:14:23 +04:00
|
|
|
next = m->m_next;
|
2017-02-05 02:08:34 +03:00
|
|
|
g_free(m);
|
2012-02-29 22:14:23 +04:00
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
/*
|
|
|
|
* Get an mbuf from the free list, if there are none
|
2017-02-05 02:08:34 +03:00
|
|
|
* allocate one
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-04-22 04:10:48 +04:00
|
|
|
* Because fragmentation can occur if we alloc new mbufs and
|
|
|
|
* free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
|
2017-02-05 02:08:34 +03:00
|
|
|
* which tells m_free to actually g_free() it
|
2004-04-22 04:10:48 +04:00
|
|
|
*/
|
|
|
|
struct mbuf *
|
2009-06-24 16:42:31 +04:00
|
|
|
m_get(Slirp *slirp)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
|
|
|
register struct mbuf *m;
|
|
|
|
int flags = 0;
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
DEBUG_CALL("m_get");
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2016-02-23 00:29:21 +03:00
|
|
|
if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
|
2017-02-05 02:08:34 +03:00
|
|
|
m = g_malloc(SLIRP_MSIZE);
|
2009-06-24 16:42:31 +04:00
|
|
|
slirp->mbuf_alloced++;
|
|
|
|
if (slirp->mbuf_alloced > MBUF_THRESH)
|
2004-04-22 04:10:48 +04:00
|
|
|
flags = M_DOFREE;
|
2009-06-24 16:42:31 +04:00
|
|
|
m->slirp = slirp;
|
2004-04-22 04:10:48 +04:00
|
|
|
} else {
|
2016-02-23 00:29:21 +03:00
|
|
|
m = (struct mbuf *) slirp->m_freelist.qh_link;
|
2004-04-22 04:10:48 +04:00
|
|
|
remque(m);
|
|
|
|
}
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
/* Insert it in the used list */
|
2009-06-24 16:42:31 +04:00
|
|
|
insque(m,&slirp->m_usedlist);
|
2004-04-22 04:10:48 +04:00
|
|
|
m->m_flags = (flags | M_USEDLIST);
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
/* Initialise it */
|
2011-02-14 23:24:24 +03:00
|
|
|
m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
|
2004-04-22 04:10:48 +04:00
|
|
|
m->m_data = m->m_dat;
|
|
|
|
m->m_len = 0;
|
2009-03-07 18:32:56 +03:00
|
|
|
m->m_nextpkt = NULL;
|
|
|
|
m->m_prevpkt = NULL;
|
2015-12-20 00:24:56 +03:00
|
|
|
m->resolution_requested = false;
|
2011-08-01 20:18:37 +04:00
|
|
|
m->expiration_date = (uint64_t)-1;
|
2015-08-29 10:12:35 +03:00
|
|
|
DEBUG_ARG("m = %p", m);
|
2004-04-22 04:10:48 +04:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-07 18:32:56 +03:00
|
|
|
m_free(struct mbuf *m)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
DEBUG_CALL("m_free");
|
2015-08-29 10:12:35 +03:00
|
|
|
DEBUG_ARG("m = %p", m);
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
if(m) {
|
|
|
|
/* Remove from m_usedlist */
|
|
|
|
if (m->m_flags & M_USEDLIST)
|
|
|
|
remque(m);
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
/* If it's M_EXT, free() it */
|
2017-02-05 02:08:34 +03:00
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
g_free(m->m_ext);
|
|
|
|
}
|
2004-04-22 04:10:48 +04:00
|
|
|
/*
|
|
|
|
* Either free() it or put it on the free list
|
|
|
|
*/
|
|
|
|
if (m->m_flags & M_DOFREE) {
|
2009-06-24 16:42:31 +04:00
|
|
|
m->slirp->mbuf_alloced--;
|
2017-02-05 02:08:34 +03:00
|
|
|
g_free(m);
|
2004-04-22 04:10:48 +04:00
|
|
|
} else if ((m->m_flags & M_FREELIST) == 0) {
|
2009-06-24 16:42:31 +04:00
|
|
|
insque(m,&m->slirp->m_freelist);
|
2004-04-22 04:10:48 +04:00
|
|
|
m->m_flags = M_FREELIST; /* Clobber other flags */
|
|
|
|
}
|
|
|
|
} /* if(m) */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy data from one mbuf to the end of
|
2017-02-05 02:08:34 +03:00
|
|
|
* the other.. if result is too big for one mbuf, allocate
|
2004-04-22 04:10:48 +04:00
|
|
|
* an M_EXT data segment
|
|
|
|
*/
|
|
|
|
void
|
2009-03-07 18:32:56 +03:00
|
|
|
m_cat(struct mbuf *m, struct mbuf *n)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If there's no room, realloc
|
|
|
|
*/
|
|
|
|
if (M_FREEROOM(m) < n->m_len)
|
2018-06-05 21:08:35 +03:00
|
|
|
m_inc(m, m->m_len + n->m_len);
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
|
|
|
|
m->m_len += n->m_len;
|
|
|
|
|
|
|
|
m_free(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-05 21:08:35 +03:00
|
|
|
/* make m 'size' bytes large from m_data */
|
2004-04-22 04:10:48 +04:00
|
|
|
void
|
2009-03-07 18:32:56 +03:00
|
|
|
m_inc(struct mbuf *m, int size)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
2018-08-10 00:52:59 +03:00
|
|
|
int gapsize;
|
2006-05-01 14:59:02 +04:00
|
|
|
|
2018-06-05 21:08:35 +03:00
|
|
|
/* some compilers throw up on gotos. This one we can fake. */
|
2018-08-07 14:45:01 +03:00
|
|
|
if (M_ROOM(m) > size) {
|
2018-06-05 21:08:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2004-04-22 04:10:48 +04:00
|
|
|
|
2018-06-05 21:08:35 +03:00
|
|
|
if (m->m_flags & M_EXT) {
|
2018-08-10 00:52:59 +03:00
|
|
|
gapsize = m->m_data - m->m_ext;
|
|
|
|
m->m_ext = g_realloc(m->m_ext, size + gapsize);
|
2018-06-05 21:08:35 +03:00
|
|
|
} else {
|
2018-08-10 00:52:59 +03:00
|
|
|
gapsize = m->m_data - m->m_dat;
|
|
|
|
m->m_ext = g_malloc(size + gapsize);
|
2018-06-05 21:08:35 +03:00
|
|
|
memcpy(m->m_ext, m->m_dat, m->m_size);
|
|
|
|
m->m_flags |= M_EXT;
|
|
|
|
}
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2018-08-10 00:52:59 +03:00
|
|
|
m->m_data = m->m_ext + gapsize;
|
|
|
|
m->m_size = size + gapsize;
|
2004-04-22 04:10:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-03-07 18:32:56 +03:00
|
|
|
m_adj(struct mbuf *m, int len)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
if (len >= 0) {
|
|
|
|
/* Trim from head */
|
|
|
|
m->m_data += len;
|
|
|
|
m->m_len -= len;
|
|
|
|
} else {
|
|
|
|
/* Trim from tail */
|
|
|
|
len = -len;
|
|
|
|
m->m_len -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy len bytes from m, starting off bytes into n
|
|
|
|
*/
|
|
|
|
int
|
2009-03-07 18:32:56 +03:00
|
|
|
m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
|
|
|
if (len > M_FREEROOM(n))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy((n->m_data + n->m_len), (m->m_data + off), len);
|
|
|
|
n->m_len += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer into an mbuf, return the mbuf
|
|
|
|
* XXX This is a kludge, I should eliminate the need for it
|
|
|
|
* Fortunately, it's not used often
|
|
|
|
*/
|
|
|
|
struct mbuf *
|
2009-06-24 16:42:31 +04:00
|
|
|
dtom(Slirp *slirp, void *dat)
|
2004-04-22 04:10:48 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
DEBUG_CALL("dtom");
|
2015-08-29 10:12:35 +03:00
|
|
|
DEBUG_ARG("dat = %p", dat);
|
2004-04-22 04:10:48 +04:00
|
|
|
|
|
|
|
/* bug corrected for M_EXT buffers */
|
2016-02-23 00:29:21 +03:00
|
|
|
for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
|
|
|
|
(struct quehead *) m != &slirp->m_usedlist;
|
2009-06-24 16:42:31 +04:00
|
|
|
m = m->m_next) {
|
2004-04-22 04:10:48 +04:00
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
|
|
|
|
return m;
|
|
|
|
} else {
|
|
|
|
if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2018-11-14 15:36:31 +03:00
|
|
|
DEBUG_ERROR("dtom failed");
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2004-04-22 04:10:48 +04:00
|
|
|
return (struct mbuf *)0;
|
|
|
|
}
|