From 861daeff3af49bd379094d63e307eed58dbfe004 Mon Sep 17 00:00:00 2001
From: Andre Oppermann <andre@FreeBSD.org>
Date: Fri, 18 Nov 2005 14:40:43 +0000
Subject: [PATCH] Add KASSERTs to M_ALIGN() and MH_ALIGN() to prevent usage on
 wrong mbuf types.

Sponsored by:	TCP/IP Optimization Fundraise 2005
---
 sys/sys/mbuf.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index 4a28e025ff1c..08dfb6020686 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -455,6 +455,10 @@ m_chtype(struct mbuf *m, short new_type)
  * an object of the specified size at the end of the mbuf, longword aligned.
  */
 #define	M_ALIGN(m, len) do {						\
+	KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)),			\
+		("%s: M_ALIGN not normal mbuf", __func__));		\
+	KASSERT((m)->m_data == (m)->m_dat,				\
+		("%s: M_ALIGN not a virgin mbuf", __func__));		\
 	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
 } while (0)
 
@@ -463,6 +467,10 @@ m_chtype(struct mbuf *m, short new_type)
  * or initialized by M_COPY_PKTHDR.
  */
 #define	MH_ALIGN(m, len) do {						\
+	KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT),	\
+		("%s: MH_ALIGN not PKTHDR mbuf", __func__));		\
+	KASSERT((m)->m_data == (m)->m_pktdat,				\
+		("%s: MH_ALIGN not a virgin mbuf", __func__));		\
 	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
 } while (0)