Home | History | Annotate | Line # | Download | only in netipsec
ipsec_mbuf.c revision 1.3
      1  1.3  jonathan /*	$NetBSD: ipsec_mbuf.c,v 1.3 2003/08/20 22:33:40 jonathan Exp $	*/
      2  1.1  jonathan /*	$FreeBSD: src/sys/netipsec/ipsec_mbuf.c,v 1.5.2.1 2003/01/24 05:11:35 sam Exp $	*/
      3  1.1  jonathan 
      4  1.1  jonathan #include <sys/cdefs.h>
      5  1.3  jonathan __KERNEL_RCSID(0, "$NetBSD: ipsec_mbuf.c,v 1.3 2003/08/20 22:33:40 jonathan Exp $");
      6  1.1  jonathan 
      7  1.1  jonathan /*
      8  1.1  jonathan  * IPsec-specific mbuf routines.
      9  1.1  jonathan  */
     10  1.1  jonathan 
     11  1.3  jonathan #ifdef __FreeBSD__
     12  1.1  jonathan #include "opt_param.h"
     13  1.3  jonathan #endif
     14  1.1  jonathan 
     15  1.1  jonathan #include <sys/param.h>
     16  1.1  jonathan #include <sys/systm.h>
     17  1.1  jonathan #include <sys/mbuf.h>
     18  1.1  jonathan #include <sys/socket.h>
     19  1.1  jonathan 
     20  1.1  jonathan #include <net/route.h>
     21  1.1  jonathan #include <netinet/in.h>
     22  1.1  jonathan 
     23  1.1  jonathan #include <netipsec/ipsec.h>
     24  1.1  jonathan 
     25  1.1  jonathan #include <netipsec/ipsec_osdep.h>
     26  1.1  jonathan #include <net/net_osdep.h>
     27  1.1  jonathan 
     28  1.1  jonathan extern	struct mbuf *m_getptr(struct mbuf *, int, int *);
     29  1.1  jonathan 
     30  1.1  jonathan /*
     31  1.1  jonathan  * Create a writable copy of the mbuf chain.  While doing this
     32  1.1  jonathan  * we compact the chain with a goal of producing a chain with
     33  1.1  jonathan  * at most two mbufs.  The second mbuf in this chain is likely
     34  1.1  jonathan  * to be a cluster.  The primary purpose of this work is to create
     35  1.1  jonathan  * a writable packet for encryption, compression, etc.  The
     36  1.1  jonathan  * secondary goal is to linearize the data so the data can be
     37  1.1  jonathan  * passed to crypto hardware in the most efficient manner possible.
     38  1.1  jonathan  */
     39  1.1  jonathan struct mbuf *
     40  1.1  jonathan m_clone(struct mbuf *m0)
     41  1.1  jonathan {
     42  1.1  jonathan 	struct mbuf *m, *mprev;
     43  1.1  jonathan 	struct mbuf *n, *mfirst, *mlast;
     44  1.1  jonathan 	int len, off;
     45  1.1  jonathan 
     46  1.1  jonathan 	IPSEC_ASSERT(m0 != NULL, ("m_clone: null mbuf"));
     47  1.1  jonathan 
     48  1.1  jonathan 	mprev = NULL;
     49  1.1  jonathan 	for (m = m0; m != NULL; m = mprev->m_next) {
     50  1.1  jonathan 		/*
     51  1.1  jonathan 		 * Regular mbufs are ignored unless there's a cluster
     52  1.1  jonathan 		 * in front of it that we can use to coalesce.  We do
     53  1.1  jonathan 		 * the latter mainly so later clusters can be coalesced
     54  1.1  jonathan 		 * also w/o having to handle them specially (i.e. convert
     55  1.1  jonathan 		 * mbuf+cluster -> cluster).  This optimization is heavily
     56  1.1  jonathan 		 * influenced by the assumption that we're running over
     57  1.1  jonathan 		 * Ethernet where MCLBYTES is large enough that the max
     58  1.1  jonathan 		 * packet size will permit lots of coalescing into a
     59  1.1  jonathan 		 * single cluster.  This in turn permits efficient
     60  1.1  jonathan 		 * crypto operations, especially when using hardware.
     61  1.1  jonathan 		 */
     62  1.1  jonathan 		if ((m->m_flags & M_EXT) == 0) {
     63  1.1  jonathan 			if (mprev && (mprev->m_flags & M_EXT) &&
     64  1.1  jonathan 			    m->m_len <= M_TRAILINGSPACE(mprev)) {
     65  1.1  jonathan 				/* XXX: this ignores mbuf types */
     66  1.1  jonathan 				memcpy(mtod(mprev, caddr_t) + mprev->m_len,
     67  1.1  jonathan 				       mtod(m, caddr_t), m->m_len);
     68  1.1  jonathan 				mprev->m_len += m->m_len;
     69  1.1  jonathan 				mprev->m_next = m->m_next;	/* unlink from chain */
     70  1.1  jonathan 				m_free(m);			/* reclaim mbuf */
     71  1.1  jonathan 				newipsecstat.ips_mbcoalesced++;
     72  1.1  jonathan 			} else {
     73  1.1  jonathan 				mprev = m;
     74  1.1  jonathan 			}
     75  1.1  jonathan 			continue;
     76  1.1  jonathan 		}
     77  1.1  jonathan 		/*
     78  1.1  jonathan 		 * Writable mbufs are left alone (for now).  Note
     79  1.1  jonathan 		 * that for 4.x systems it's not possible to identify
     80  1.1  jonathan 		 * whether or not mbufs with external buffers are
     81  1.1  jonathan 		 * writable unless they use clusters.
     82  1.1  jonathan 		 */
     83  1.1  jonathan 		if (M_EXT_WRITABLE(m)) {
     84  1.1  jonathan 			mprev = m;
     85  1.1  jonathan 			continue;
     86  1.1  jonathan 		}
     87  1.1  jonathan 
     88  1.1  jonathan 		/*
     89  1.1  jonathan 		 * Not writable, replace with a copy or coalesce with
     90  1.1  jonathan 		 * the previous mbuf if possible (since we have to copy
     91  1.1  jonathan 		 * it anyway, we try to reduce the number of mbufs and
     92  1.1  jonathan 		 * clusters so that future work is easier).
     93  1.1  jonathan 		 */
     94  1.1  jonathan 		IPSEC_ASSERT(m->m_flags & M_EXT,
     95  1.1  jonathan 			("m_clone: m_flags 0x%x", m->m_flags));
     96  1.1  jonathan 		/* NB: we only coalesce into a cluster or larger */
     97  1.1  jonathan 		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
     98  1.1  jonathan 		    m->m_len <= M_TRAILINGSPACE(mprev)) {
     99  1.1  jonathan 			/* XXX: this ignores mbuf types */
    100  1.1  jonathan 			memcpy(mtod(mprev, caddr_t) + mprev->m_len,
    101  1.1  jonathan 			       mtod(m, caddr_t), m->m_len);
    102  1.1  jonathan 			mprev->m_len += m->m_len;
    103  1.1  jonathan 			mprev->m_next = m->m_next;	/* unlink from chain */
    104  1.1  jonathan 			m_free(m);			/* reclaim mbuf */
    105  1.1  jonathan 			newipsecstat.ips_clcoalesced++;
    106  1.1  jonathan 			continue;
    107  1.1  jonathan 		}
    108  1.1  jonathan 
    109  1.1  jonathan 		/*
    110  1.1  jonathan 		 * Allocate new space to hold the copy...
    111  1.1  jonathan 		 */
    112  1.1  jonathan 		/* XXX why can M_PKTHDR be set past the first mbuf? */
    113  1.1  jonathan 		if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
    114  1.1  jonathan 			/*
    115  1.1  jonathan 			 * NB: if a packet header is present we must
    116  1.1  jonathan 			 * allocate the mbuf separately from any cluster
    117  1.1  jonathan 			 * because M_MOVE_PKTHDR will smash the data
    118  1.1  jonathan 			 * pointer and drop the M_EXT marker.
    119  1.1  jonathan 			 */
    120  1.1  jonathan 			MGETHDR(n, M_DONTWAIT, m->m_type);
    121  1.1  jonathan 			if (n == NULL) {
    122  1.1  jonathan 				m_freem(m0);
    123  1.1  jonathan 				return (NULL);
    124  1.1  jonathan 			}
    125  1.1  jonathan 			M_MOVE_PKTHDR(n, m);
    126  1.1  jonathan 			MCLGET(n, M_DONTWAIT);
    127  1.1  jonathan 			if ((n->m_flags & M_EXT) == 0) {
    128  1.1  jonathan 				m_free(n);
    129  1.1  jonathan 				m_freem(m0);
    130  1.1  jonathan 				return (NULL);
    131  1.1  jonathan 			}
    132  1.1  jonathan 		} else {
    133  1.1  jonathan 			n = m_getcl(M_DONTWAIT, m->m_type, m->m_flags);
    134  1.1  jonathan 			if (n == NULL) {
    135  1.1  jonathan 				m_freem(m0);
    136  1.1  jonathan 				return (NULL);
    137  1.1  jonathan 			}
    138  1.1  jonathan 		}
    139  1.1  jonathan 		/*
    140  1.1  jonathan 		 * ... and copy the data.  We deal with jumbo mbufs
    141  1.1  jonathan 		 * (i.e. m_len > MCLBYTES) by splitting them into
    142  1.1  jonathan 		 * clusters.  We could just malloc a buffer and make
    143  1.1  jonathan 		 * it external but too many device drivers don't know
    144  1.1  jonathan 		 * how to break up the non-contiguous memory when
    145  1.1  jonathan 		 * doing DMA.
    146  1.1  jonathan 		 */
    147  1.1  jonathan 		len = m->m_len;
    148  1.1  jonathan 		off = 0;
    149  1.1  jonathan 		mfirst = n;
    150  1.1  jonathan 		mlast = NULL;
    151  1.1  jonathan 		for (;;) {
    152  1.1  jonathan 			int cc = min(len, MCLBYTES);
    153  1.1  jonathan 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
    154  1.1  jonathan 			n->m_len = cc;
    155  1.1  jonathan 			if (mlast != NULL)
    156  1.1  jonathan 				mlast->m_next = n;
    157  1.1  jonathan 			mlast = n;
    158  1.1  jonathan 			newipsecstat.ips_clcopied++;
    159  1.1  jonathan 
    160  1.1  jonathan 			len -= cc;
    161  1.1  jonathan 			if (len <= 0)
    162  1.1  jonathan 				break;
    163  1.1  jonathan 			off += cc;
    164  1.1  jonathan 
    165  1.1  jonathan 			n = m_getcl(M_DONTWAIT, m->m_type, m->m_flags);
    166  1.1  jonathan 			if (n == NULL) {
    167  1.1  jonathan 				m_freem(mfirst);
    168  1.1  jonathan 				m_freem(m0);
    169  1.1  jonathan 				return (NULL);
    170  1.1  jonathan 			}
    171  1.1  jonathan 		}
    172  1.1  jonathan 		n->m_next = m->m_next;
    173  1.1  jonathan 		if (mprev == NULL)
    174  1.1  jonathan 			m0 = mfirst;		/* new head of chain */
    175  1.1  jonathan 		else
    176  1.1  jonathan 			mprev->m_next = mfirst;	/* replace old mbuf */
    177  1.1  jonathan 		m_free(m);			/* release old mbuf */
    178  1.1  jonathan 		mprev = mfirst;
    179  1.1  jonathan 	}
    180  1.1  jonathan 	return (m0);
    181  1.1  jonathan }
    182  1.1  jonathan 
    183  1.1  jonathan /*
    184  1.1  jonathan  * Make space for a new header of length hlen at skip bytes
    185  1.1  jonathan  * into the packet.  When doing this we allocate new mbufs only
    186  1.1  jonathan  * when absolutely necessary.  The mbuf where the new header
    187  1.1  jonathan  * is to go is returned together with an offset into the mbuf.
    188  1.1  jonathan  * If NULL is returned then the mbuf chain may have been modified;
    189  1.1  jonathan  * the caller is assumed to always free the chain.
    190  1.1  jonathan  */
    191  1.1  jonathan struct mbuf *
    192  1.1  jonathan m_makespace(struct mbuf *m0, int skip, int hlen, int *off)
    193  1.1  jonathan {
    194  1.1  jonathan 	struct mbuf *m;
    195  1.1  jonathan 	unsigned remain;
    196  1.1  jonathan 
    197  1.1  jonathan 	IPSEC_ASSERT(m0 != NULL, ("m_dmakespace: null mbuf"));
    198  1.1  jonathan 	IPSEC_ASSERT(hlen < MHLEN, ("m_makespace: hlen too big: %u", hlen));
    199  1.1  jonathan 
    200  1.1  jonathan 	for (m = m0; m && skip > m->m_len; m = m->m_next)
    201  1.1  jonathan 		skip -= m->m_len;
    202  1.1  jonathan 	if (m == NULL)
    203  1.1  jonathan 		return (NULL);
    204  1.1  jonathan 	/*
    205  1.1  jonathan 	 * At this point skip is the offset into the mbuf m
    206  1.1  jonathan 	 * where the new header should be placed.  Figure out
    207  1.1  jonathan 	 * if there's space to insert the new header.  If so,
    208  1.1  jonathan 	 * and copying the remainder makese sense then do so.
    209  1.1  jonathan 	 * Otherwise insert a new mbuf in the chain, splitting
    210  1.1  jonathan 	 * the contents of m as needed.
    211  1.1  jonathan 	 */
    212  1.1  jonathan 	remain = m->m_len - skip;		/* data to move */
    213  1.1  jonathan 	if (hlen > M_TRAILINGSPACE(m)) {
    214  1.1  jonathan 		struct mbuf *n;
    215  1.1  jonathan 
    216  1.1  jonathan 		/* XXX code doesn't handle clusters XXX */
    217  1.1  jonathan 		IPSEC_ASSERT(remain < MLEN,
    218  1.1  jonathan 			("m_makespace: remainder too big: %u", remain));
    219  1.1  jonathan 		/*
    220  1.1  jonathan 		 * Not enough space in m, split the contents
    221  1.1  jonathan 		 * of m, inserting new mbufs as required.
    222  1.1  jonathan 		 *
    223  1.1  jonathan 		 * NB: this ignores mbuf types.
    224  1.1  jonathan 		 */
    225  1.1  jonathan 		MGET(n, M_DONTWAIT, MT_DATA);
    226  1.1  jonathan 		if (n == NULL)
    227  1.1  jonathan 			return (NULL);
    228  1.1  jonathan 		n->m_next = m->m_next;		/* splice new mbuf */
    229  1.1  jonathan 		m->m_next = n;
    230  1.1  jonathan 		newipsecstat.ips_mbinserted++;
    231  1.1  jonathan 		if (hlen <= M_TRAILINGSPACE(m) + remain) {
    232  1.1  jonathan 			/*
    233  1.1  jonathan 			 * New header fits in the old mbuf if we copy
    234  1.1  jonathan 			 * the remainder; just do the copy to the new
    235  1.1  jonathan 			 * mbuf and we're good to go.
    236  1.1  jonathan 			 */
    237  1.1  jonathan 			memcpy(mtod(n, caddr_t),
    238  1.1  jonathan 			       mtod(m, caddr_t) + skip, remain);
    239  1.1  jonathan 			n->m_len = remain;
    240  1.1  jonathan 			m->m_len = skip + hlen;
    241  1.1  jonathan 			*off = skip;
    242  1.1  jonathan 		} else {
    243  1.1  jonathan 			/*
    244  1.1  jonathan 			 * No space in the old mbuf for the new header.
    245  1.1  jonathan 			 * Make space in the new mbuf and check the
    246  1.1  jonathan 			 * remainder'd data fits too.  If not then we
    247  1.1  jonathan 			 * must allocate an additional mbuf (yech).
    248  1.1  jonathan 			 */
    249  1.1  jonathan 			n->m_len = 0;
    250  1.1  jonathan 			if (remain + hlen > M_TRAILINGSPACE(n)) {
    251  1.1  jonathan 				struct mbuf *n2;
    252  1.1  jonathan 
    253  1.1  jonathan 				MGET(n2, M_DONTWAIT, MT_DATA);
    254  1.1  jonathan 				/* NB: new mbuf is on chain, let caller free */
    255  1.1  jonathan 				if (n2 == NULL)
    256  1.1  jonathan 					return (NULL);
    257  1.1  jonathan 				n2->m_len = 0;
    258  1.1  jonathan 				memcpy(mtod(n2, caddr_t),
    259  1.1  jonathan 				       mtod(m, caddr_t) + skip, remain);
    260  1.1  jonathan 				n2->m_len = remain;
    261  1.1  jonathan 				/* splice in second mbuf */
    262  1.1  jonathan 				n2->m_next = n->m_next;
    263  1.1  jonathan 				n->m_next = n2;
    264  1.1  jonathan 				newipsecstat.ips_mbinserted++;
    265  1.1  jonathan 			} else {
    266  1.1  jonathan 				memcpy(mtod(n, caddr_t) + hlen,
    267  1.1  jonathan 				       mtod(m, caddr_t) + skip, remain);
    268  1.1  jonathan 				n->m_len += remain;
    269  1.1  jonathan 			}
    270  1.1  jonathan 			m->m_len -= remain;
    271  1.1  jonathan 			n->m_len += hlen;
    272  1.1  jonathan 			m = n;			/* header is at front ... */
    273  1.1  jonathan 			*off = 0;		/* ... of new mbuf */
    274  1.1  jonathan 		}
    275  1.1  jonathan 	} else {
    276  1.1  jonathan 		/*
    277  1.1  jonathan 		 * Copy the remainder to the back of the mbuf
    278  1.1  jonathan 		 * so there's space to write the new header.
    279  1.1  jonathan 		 */
    280  1.1  jonathan 		/* XXX can this be memcpy? does it handle overlap? */
    281  1.1  jonathan 		ovbcopy(mtod(m, caddr_t) + skip,
    282  1.1  jonathan 			mtod(m, caddr_t) + skip + hlen, remain);
    283  1.1  jonathan 		m->m_len += hlen;
    284  1.1  jonathan 		*off = skip;
    285  1.1  jonathan 	}
    286  1.1  jonathan 	m0->m_pkthdr.len += hlen;		/* adjust packet length */
    287  1.1  jonathan 	return m;
    288  1.1  jonathan }
    289  1.1  jonathan 
    290  1.1  jonathan /*
    291  1.1  jonathan  * m_pad(m, n) pads <m> with <n> bytes at the end. The packet header
    292  1.1  jonathan  * length is updated, and a pointer to the first byte of the padding
    293  1.1  jonathan  * (which is guaranteed to be all in one mbuf) is returned.
    294  1.1  jonathan  */
    295  1.1  jonathan caddr_t
    296  1.1  jonathan m_pad(struct mbuf *m, int n)
    297  1.1  jonathan {
    298  1.1  jonathan 	register struct mbuf *m0, *m1;
    299  1.1  jonathan 	register int len, pad;
    300  1.1  jonathan 	caddr_t retval;
    301  1.1  jonathan 
    302  1.1  jonathan 	if (n <= 0) {  /* No stupid arguments. */
    303  1.1  jonathan 		DPRINTF(("m_pad: pad length invalid (%d)\n", n));
    304  1.1  jonathan 		m_freem(m);
    305  1.1  jonathan 		return NULL;
    306  1.1  jonathan 	}
    307  1.1  jonathan 
    308  1.1  jonathan 	len = m->m_pkthdr.len;
    309  1.1  jonathan 	pad = n;
    310  1.1  jonathan 	m0 = m;
    311  1.1  jonathan 
    312  1.1  jonathan 	while (m0->m_len < len) {
    313  1.1  jonathan IPSEC_ASSERT(m0->m_next != NULL, ("m_pad: m0 null, len %u m_len %u", len, m0->m_len));/*XXX*/
    314  1.1  jonathan 		len -= m0->m_len;
    315  1.1  jonathan 		m0 = m0->m_next;
    316  1.1  jonathan 	}
    317  1.1  jonathan 
    318  1.1  jonathan 	if (m0->m_len != len) {
    319  1.1  jonathan 		DPRINTF(("m_pad: length mismatch (should be %d instead of %d)\n",
    320  1.1  jonathan 		    m->m_pkthdr.len, m->m_pkthdr.len + m0->m_len - len));
    321  1.1  jonathan 
    322  1.1  jonathan 		m_freem(m);
    323  1.1  jonathan 		return NULL;
    324  1.1  jonathan 	}
    325  1.1  jonathan 
    326  1.1  jonathan 	/* Check for zero-length trailing mbufs, and find the last one. */
    327  1.1  jonathan 	for (m1 = m0; m1->m_next; m1 = m1->m_next) {
    328  1.1  jonathan 		if (m1->m_next->m_len != 0) {
    329  1.1  jonathan 			DPRINTF(("m_pad: length mismatch (should be %d "
    330  1.1  jonathan 			    "instead of %d)\n",
    331  1.1  jonathan 			    m->m_pkthdr.len,
    332  1.1  jonathan 			    m->m_pkthdr.len + m1->m_next->m_len));
    333  1.1  jonathan 
    334  1.1  jonathan 			m_freem(m);
    335  1.1  jonathan 			return NULL;
    336  1.1  jonathan 		}
    337  1.1  jonathan 
    338  1.1  jonathan 		m0 = m1->m_next;
    339  1.1  jonathan 	}
    340  1.1  jonathan 
    341  1.1  jonathan 	if (pad > M_TRAILINGSPACE(m0)) {
    342  1.1  jonathan 		/* Add an mbuf to the chain. */
    343  1.1  jonathan 		MGET(m1, M_DONTWAIT, MT_DATA);
    344  1.1  jonathan 		if (m1 == 0) {
    345  1.1  jonathan 			m_freem(m0);
    346  1.1  jonathan 			DPRINTF(("m_pad: unable to get extra mbuf\n"));
    347  1.1  jonathan 			return NULL;
    348  1.1  jonathan 		}
    349  1.1  jonathan 
    350  1.1  jonathan 		m0->m_next = m1;
    351  1.1  jonathan 		m0 = m1;
    352  1.1  jonathan 		m0->m_len = 0;
    353  1.1  jonathan 	}
    354  1.1  jonathan 
    355  1.1  jonathan 	retval = m0->m_data + m0->m_len;
    356  1.1  jonathan 	m0->m_len += pad;
    357  1.1  jonathan 	m->m_pkthdr.len += pad;
    358  1.1  jonathan 
    359  1.1  jonathan 	return retval;
    360  1.1  jonathan }
    361  1.1  jonathan 
    362  1.1  jonathan /*
    363  1.1  jonathan  * Remove hlen data at offset skip in the packet.  This is used by
    364  1.1  jonathan  * the protocols strip protocol headers and associated data (e.g. IV,
    365  1.1  jonathan  * authenticator) on input.
    366  1.1  jonathan  */
    367  1.1  jonathan int
    368  1.1  jonathan m_striphdr(struct mbuf *m, int skip, int hlen)
    369  1.1  jonathan {
    370  1.1  jonathan 	struct mbuf *m1;
    371  1.1  jonathan 	int roff;
    372  1.1  jonathan 
    373  1.1  jonathan 	/* Find beginning of header */
    374  1.1  jonathan 	m1 = m_getptr(m, skip, &roff);
    375  1.1  jonathan 	if (m1 == NULL)
    376  1.1  jonathan 		return (EINVAL);
    377  1.1  jonathan 
    378  1.1  jonathan 	/* Remove the header and associated data from the mbuf. */
    379  1.1  jonathan 	if (roff == 0) {
    380  1.1  jonathan 		/* The header was at the beginning of the mbuf */
    381  1.1  jonathan 		newipsecstat.ips_input_front++;
    382  1.1  jonathan 		m_adj(m1, hlen);
    383  1.1  jonathan 		if ((m1->m_flags & M_PKTHDR) == 0)
    384  1.1  jonathan 			m->m_pkthdr.len -= hlen;
    385  1.1  jonathan 	} else if (roff + hlen >= m1->m_len) {
    386  1.1  jonathan 		struct mbuf *mo;
    387  1.1  jonathan 
    388  1.1  jonathan 		/*
    389  1.1  jonathan 		 * Part or all of the header is at the end of this mbuf,
    390  1.1  jonathan 		 * so first let's remove the remainder of the header from
    391  1.1  jonathan 		 * the beginning of the remainder of the mbuf chain, if any.
    392  1.1  jonathan 		 */
    393  1.1  jonathan 		newipsecstat.ips_input_end++;
    394  1.1  jonathan 		if (roff + hlen > m1->m_len) {
    395  1.1  jonathan 			/* Adjust the next mbuf by the remainder */
    396  1.1  jonathan 			m_adj(m1->m_next, roff + hlen - m1->m_len);
    397  1.1  jonathan 
    398  1.1  jonathan 			/* The second mbuf is guaranteed not to have a pkthdr... */
    399  1.1  jonathan 			m->m_pkthdr.len -= (roff + hlen - m1->m_len);
    400  1.1  jonathan 		}
    401  1.1  jonathan 
    402  1.1  jonathan 		/* Now, let's unlink the mbuf chain for a second...*/
    403  1.1  jonathan 		mo = m1->m_next;
    404  1.1  jonathan 		m1->m_next = NULL;
    405  1.1  jonathan 
    406  1.1  jonathan 		/* ...and trim the end of the first part of the chain...sick */
    407  1.1  jonathan 		m_adj(m1, -(m1->m_len - roff));
    408  1.1  jonathan 		if ((m1->m_flags & M_PKTHDR) == 0)
    409  1.1  jonathan 			m->m_pkthdr.len -= (m1->m_len - roff);
    410  1.1  jonathan 
    411  1.1  jonathan 		/* Finally, let's relink */
    412  1.1  jonathan 		m1->m_next = mo;
    413  1.1  jonathan 	} else {
    414  1.1  jonathan 		/*
    415  1.1  jonathan 		 * The header lies in the "middle" of the mbuf; copy
    416  1.1  jonathan 		 * the remainder of the mbuf down over the header.
    417  1.1  jonathan 		 */
    418  1.1  jonathan 		newipsecstat.ips_input_middle++;
    419  1.2  jonathan 		ovbcopy(mtod(m1, u_char *) + roff + hlen,
    420  1.1  jonathan 		      mtod(m1, u_char *) + roff,
    421  1.1  jonathan 		      m1->m_len - (roff + hlen));
    422  1.1  jonathan 		m1->m_len -= hlen;
    423  1.1  jonathan 		m->m_pkthdr.len -= hlen;
    424  1.1  jonathan 	}
    425  1.1  jonathan 	return (0);
    426  1.1  jonathan }
    427  1.1  jonathan 
    428  1.1  jonathan /*
    429  1.1  jonathan  * Diagnostic routine to check mbuf alignment as required by the
    430  1.1  jonathan  * crypto device drivers (that use DMA).
    431  1.1  jonathan  */
    432  1.1  jonathan void
    433  1.1  jonathan m_checkalignment(const char* where, struct mbuf *m0, int off, int len)
    434  1.1  jonathan {
    435  1.1  jonathan 	int roff;
    436  1.1  jonathan 	struct mbuf *m = m_getptr(m0, off, &roff);
    437  1.1  jonathan 	caddr_t addr;
    438  1.1  jonathan 
    439  1.1  jonathan 	if (m == NULL)
    440  1.1  jonathan 		return;
    441  1.1  jonathan 	printf("%s (off %u len %u): ", where, off, len);
    442  1.1  jonathan 	addr = mtod(m, caddr_t) + roff;
    443  1.1  jonathan 	do {
    444  1.1  jonathan 		int mlen;
    445  1.1  jonathan 
    446  1.1  jonathan 		if (((uintptr_t) addr) & 3) {
    447  1.1  jonathan 			printf("addr misaligned %p,", addr);
    448  1.1  jonathan 			break;
    449  1.1  jonathan 		}
    450  1.1  jonathan 		mlen = m->m_len;
    451  1.1  jonathan 		if (mlen > len)
    452  1.1  jonathan 			mlen = len;
    453  1.1  jonathan 		len -= mlen;
    454  1.1  jonathan 		if (len && (mlen & 3)) {
    455  1.1  jonathan 			printf("len mismatch %u,", mlen);
    456  1.1  jonathan 			break;
    457  1.1  jonathan 		}
    458  1.1  jonathan 		m = m->m_next;
    459  1.1  jonathan 		addr = m ? mtod(m, caddr_t) : NULL;
    460  1.1  jonathan 	} while (m && len > 0);
    461  1.1  jonathan 	for (m = m0; m; m = m->m_next)
    462  1.1  jonathan 		printf(" [%p:%u]", mtod(m, caddr_t), m->m_len);
    463  1.1  jonathan 	printf("\n");
    464  1.1  jonathan }
    465