]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/pf_norm.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / net / pf_norm.c
index 69283ce6ebe0b03ccd3f5eb489d11955faede15f..9c28415dedc26a91ab246d8026b2a2e421ea8cd3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
+ * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  *
 #include <net/pfvar.h>
 
 struct pf_frent {
-       LIST_ENTRY(pf_frent)    fr_next;
-       struct mbuf             *fr_m;
-#define fr_ip          fr_u.fru_ipv4
-#define fr_ip6         fr_u.fru_ipv6
+       LIST_ENTRY(pf_frent)    fr_next;
+       struct mbuf             *fr_m;
+#define fr_ip           fr_u.fru_ipv4
+#define fr_ip6          fr_u.fru_ipv6
        union {
-               struct ip       *fru_ipv4;
-               struct ip6_hdr  *fru_ipv6;
+               struct ip       *fru_ipv4;
+               struct ip6_hdr  *fru_ipv6;
        } fr_u;
-       struct ip6_frag         fr_ip6f_opt;
-       int                     fr_ip6f_hlen;
+       struct ip6_frag         fr_ip6f_opt;
+       int                     fr_ip6f_hlen;
 };
 
 struct pf_frcache {
        LIST_ENTRY(pf_frcache) fr_next;
-       uint16_t        fr_off;
-       uint16_t        fr_end;
+       uint16_t        fr_off;
+       uint16_t        fr_end;
 };
 
-#define PFFRAG_SEENLAST        0x0001          /* Seen the last fragment for this */
-#define PFFRAG_NOBUFFER        0x0002          /* Non-buffering fragment cache */
-#define PFFRAG_DROP    0x0004          /* Drop all fragments */
-#define BUFFER_FRAGMENTS(fr)   (!((fr)->fr_flags & PFFRAG_NOBUFFER))
+#define PFFRAG_SEENLAST 0x0001          /* Seen the last fragment for this */
+#define PFFRAG_NOBUFFER 0x0002          /* Non-buffering fragment cache */
+#define PFFRAG_DROP     0x0004          /* Drop all fragments */
+#define BUFFER_FRAGMENTS(fr)    (!((fr)->fr_flags & PFFRAG_NOBUFFER))
 
 struct pf_fragment {
        RB_ENTRY(pf_fragment) fr_entry;
        TAILQ_ENTRY(pf_fragment) frag_next;
-       struct pf_addr  fr_srcx;
-       struct pf_addr  fr_dstx;
-       u_int8_t        fr_p;           /* protocol of this fragment */
-       u_int8_t        fr_flags;       /* status flags */
-       u_int16_t       fr_max;         /* fragment data max */
-#define fr_id          fr_uid.fru_id4
-#define fr_id6         fr_uid.fru_id6
+       struct pf_addr  fr_srcx;
+       struct pf_addr  fr_dstx;
+       u_int8_t        fr_p;           /* protocol of this fragment */
+       u_int8_t        fr_flags;       /* status flags */
+       u_int16_t       fr_max;         /* fragment data max */
+#define fr_id           fr_uid.fru_id4
+#define fr_id6          fr_uid.fru_id6
        union {
-               u_int16_t       fru_id4;
-               u_int32_t       fru_id6;
+               u_int16_t       fru_id4;
+               u_int32_t       fru_id6;
        } fr_uid;
-       int             fr_af;
-       u_int32_t       fr_timeout;
-#define fr_queue       fr_u.fru_queue
-#define fr_cache       fr_u.fru_cache
+       int             fr_af;
+       u_int32_t       fr_timeout;
+#define fr_queue        fr_u.fru_queue
+#define fr_cache        fr_u.fru_cache
        union {
-               LIST_HEAD(pf_fragq, pf_frent) fru_queue;        /* buffering */
-               LIST_HEAD(pf_cacheq, pf_frcache) fru_cache;     /* non-buf */
+               LIST_HEAD(pf_fragq, pf_frent) fru_queue;        /* buffering */
+               LIST_HEAD(pf_cacheq, pf_frcache) fru_cache;     /* non-buf */
        } fr_u;
+       uint32_t        fr_csum_flags;  /* checksum flags */
+       uint32_t        fr_csum;        /* partial checksum value */
 };
 
-static TAILQ_HEAD(pf_fragqueue, pf_fragment)   pf_fragqueue;
-static TAILQ_HEAD(pf_cachequeue, pf_fragment)  pf_cachequeue;
+static TAILQ_HEAD(pf_fragqueue, pf_fragment)    pf_fragqueue;
+static TAILQ_HEAD(pf_cachequeue, pf_fragment)   pf_cachequeue;
 
 static __inline int  pf_frag_compare(struct pf_fragment *,
     struct pf_fragment *);
-static RB_HEAD(pf_frag_tree, pf_fragment)      pf_frag_tree, pf_cache_tree;
+static RB_HEAD(pf_frag_tree, pf_fragment)       pf_frag_tree, pf_cache_tree;
 RB_PROTOTYPE_SC(__private_extern__, pf_frag_tree, pf_fragment, fr_entry,
     pf_frag_compare);
 RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
@@ -156,11 +158,11 @@ static void pf_free_fragment(struct pf_fragment *);
 static struct pf_fragment *pf_find_fragment_by_key(struct pf_fragment *,
     struct pf_frag_tree *);
 static __inline struct pf_fragment *
-    pf_find_fragment_by_ipv4_header(struct ip *, struct pf_frag_tree *);
+pf_find_fragment_by_ipv4_header(struct ip *, struct pf_frag_tree *);
 static __inline struct pf_fragment *
-    pf_find_fragment_by_ipv6_header(struct ip6_hdr *, struct ip6_frag *,
+pf_find_fragment_by_ipv6_header(struct ip6_hdr *, struct ip6_frag *,
     struct pf_frag_tree *);
-static struct mbuf *pf_reassemble(struct mbuf **, struct pf_fragment **,
+static struct mbuf *pf_reassemble(struct mbuf *, struct pf_fragment **,
     struct pf_frent *, int);
 static struct mbuf *pf_fragcache(struct mbuf **, struct ip *,
     struct pf_fragment **, int, int, int *);
@@ -169,34 +171,34 @@ static struct mbuf *pf_reassemble6(struct mbuf **, struct pf_fragment **,
 static struct mbuf *pf_frag6cache(struct mbuf **, struct ip6_hdr*,
     struct ip6_frag *, struct pf_fragment **, int, int, int, int *);
 static int pf_normalize_tcpopt(struct pf_rule *, int, struct pfi_kif *,
-    struct pf_pdesc *, struct mbuf *, struct tcphdr *, int, int *);
+    struct pf_pdesc *, pbuf_t *, struct tcphdr *, int, int *);
 
-#define        DPFPRINTF(x) do {                               \
-       if (pf_status.debug >= PF_DEBUG_MISC) {         \
-               printf("%s: ", __func__);               \
-               printf x ;                              \
-       }                                               \
+#define DPFPRINTF(x) do {                               \
+       if (pf_status.debug >= PF_DEBUG_MISC) {         \
+               printf("%s: ", __func__);               \
+               printf x ;                              \
+       }                                               \
 } while (0)
 
 /* Globals */
-struct pool             pf_frent_pl, pf_frag_pl;
-static struct pool      pf_cache_pl, pf_cent_pl;
-struct pool             pf_state_scrub_pl;
+struct pool              pf_frent_pl, pf_frag_pl;
+static struct pool       pf_cache_pl, pf_cent_pl;
+struct pool              pf_state_scrub_pl;
 
-static int              pf_nfrents, pf_ncache;
+static int               pf_nfrents, pf_ncache;
 
 void
 pf_normalize_init(void)
 {
-       pool_init(&pf_frent_pl, sizeof (struct pf_frent), 0, 0, 0, "pffrent",
+       pool_init(&pf_frent_pl, sizeof(struct pf_frent), 0, 0, 0, "pffrent",
            NULL);
-       pool_init(&pf_frag_pl, sizeof (struct pf_fragment), 0, 0, 0, "pffrag",
+       pool_init(&pf_frag_pl, sizeof(struct pf_fragment), 0, 0, 0, "pffrag",
            NULL);
-       pool_init(&pf_cache_pl, sizeof (struct pf_fragment), 0, 0, 0,
+       pool_init(&pf_cache_pl, sizeof(struct pf_fragment), 0, 0, 0,
            "pffrcache", NULL);
-       pool_init(&pf_cent_pl, sizeof (struct pf_frcache), 0, 0, 0, "pffrcent",
+       pool_init(&pf_cent_pl, sizeof(struct pf_frcache), 0, 0, 0, "pffrcent",
            NULL);
-       pool_init(&pf_state_scrub_pl, sizeof (struct pf_state_scrub), 0, 0, 0,
+       pool_init(&pf_state_scrub_pl, sizeof(struct pf_state_scrub), 0, 0, 0,
            "pfstscr", NULL);
 
        pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT);
@@ -223,75 +225,77 @@ pf_normalize_destroy(void)
 int
 pf_normalize_isempty(void)
 {
-       return (TAILQ_EMPTY(&pf_fragqueue) && TAILQ_EMPTY(&pf_cachequeue));
+       return TAILQ_EMPTY(&pf_fragqueue) && TAILQ_EMPTY(&pf_cachequeue);
 }
 
 static __inline int
 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
 {
-       int     diff;
+       int     diff;
 
-       if ((diff = a->fr_af - b->fr_af))
-               return (diff);
-       else if ((diff = a->fr_p - b->fr_p))
-               return (diff);
-       else {
+       if ((diff = a->fr_af - b->fr_af)) {
+               return diff;
+       } else if ((diff = a->fr_p - b->fr_p)) {
+               return diff;
+       else {
                struct pf_addr *sa = &a->fr_srcx;
                struct pf_addr *sb = &b->fr_srcx;
                struct pf_addr *da = &a->fr_dstx;
                struct pf_addr *db = &b->fr_dstx;
-               
+
                switch (a->fr_af) {
 #ifdef INET
                case AF_INET:
-                       if ((diff = a->fr_id - b->fr_id))
-                               return (diff);
-                       else if (sa->v4.s_addr < sb->v4.s_addr)
-                               return (-1);
-                       else if (sa->v4.s_addr > sb->v4.s_addr)
-                               return (1);
-                       else if (da->v4.s_addr < db->v4.s_addr)
-                               return (-1);
-                       else if (da->v4.s_addr > db->v4.s_addr)
-                               return (1);
+                       if ((diff = a->fr_id - b->fr_id)) {
+                               return diff;
+                       } else if (sa->v4addr.s_addr < sb->v4addr.s_addr) {
+                               return -1;
+                       } else if (sa->v4addr.s_addr > sb->v4addr.s_addr) {
+                               return 1;
+                       } else if (da->v4addr.s_addr < db->v4addr.s_addr) {
+                               return -1;
+                       } else if (da->v4addr.s_addr > db->v4addr.s_addr) {
+                               return 1;
+                       }
                        break;
 #endif
 #ifdef INET6
                case AF_INET6:
-                       if ((diff = a->fr_id6 - b->fr_id6))
-                               return (diff);
-                       else if (sa->addr32[3] < sb->addr32[3])
-                               return (-1);
-                       else if (sa->addr32[3] > sb->addr32[3])
-                               return (1);
-                       else if (sa->addr32[2] < sb->addr32[2])
-                               return (-1);
-                       else if (sa->addr32[2] > sb->addr32[2])
-                               return (1);
-                       else if (sa->addr32[1] < sb->addr32[1])
-                               return (-1);
-                       else if (sa->addr32[1] > sb->addr32[1])
-                               return (1);
-                       else if (sa->addr32[0] < sb->addr32[0])
-                               return (-1);
-                       else if (sa->addr32[0] > sb->addr32[0])
-                               return (1);
-                       else if (da->addr32[3] < db->addr32[3])
-                               return (-1);
-                       else if (da->addr32[3] > db->addr32[3])
-                               return (1);
-                       else if (da->addr32[2] < db->addr32[2])
-                               return (-1);
-                       else if (da->addr32[2] > db->addr32[2])
-                               return (1);
-                       else if (da->addr32[1] < db->addr32[1])
-                               return (-1);
-                       else if (da->addr32[1] > db->addr32[1])
-                               return (1);
-                       else if (da->addr32[0] < db->addr32[0])
-                               return (-1);
-                       else if (da->addr32[0] > db->addr32[0])
-                               return (1);
+                       if ((diff = a->fr_id6 - b->fr_id6)) {
+                               return diff;
+                       } else if (sa->addr32[3] < sb->addr32[3]) {
+                               return -1;
+                       } else if (sa->addr32[3] > sb->addr32[3]) {
+                               return 1;
+                       } else if (sa->addr32[2] < sb->addr32[2]) {
+                               return -1;
+                       } else if (sa->addr32[2] > sb->addr32[2]) {
+                               return 1;
+                       } else if (sa->addr32[1] < sb->addr32[1]) {
+                               return -1;
+                       } else if (sa->addr32[1] > sb->addr32[1]) {
+                               return 1;
+                       } else if (sa->addr32[0] < sb->addr32[0]) {
+                               return -1;
+                       } else if (sa->addr32[0] > sb->addr32[0]) {
+                               return 1;
+                       } else if (da->addr32[3] < db->addr32[3]) {
+                               return -1;
+                       } else if (da->addr32[3] > db->addr32[3]) {
+                               return 1;
+                       } else if (da->addr32[2] < db->addr32[2]) {
+                               return -1;
+                       } else if (da->addr32[2] > db->addr32[2]) {
+                               return 1;
+                       } else if (da->addr32[1] < db->addr32[1]) {
+                               return -1;
+                       } else if (da->addr32[1] > db->addr32[1]) {
+                               return 1;
+                       } else if (da->addr32[0] < db->addr32[0]) {
+                               return -1;
+                       } else if (da->addr32[0] > db->addr32[0]) {
+                               return 1;
+                       }
                        break;
 #endif
                default:
@@ -299,7 +303,7 @@ pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
                        break;
                }
        }
-       return (0);
+       return 0;
 }
 
 void
@@ -311,42 +315,48 @@ pf_purge_expired_fragments(void)
 
        while ((frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue)) != NULL) {
                VERIFY(BUFFER_FRAGMENTS(frag));
-               if (frag->fr_timeout > expire)
+               if (frag->fr_timeout > expire) {
                        break;
+               }
 
                switch (frag->fr_af) {
                case AF_INET:
-                     DPFPRINTF(("expiring IPv4 %d(%p) from queue.\n",
-                         ntohs(frag->fr_id), frag));
-                     break;
+                       DPFPRINTF(("expiring IPv4 %d(0x%llx) from queue.\n",
+                           ntohs(frag->fr_id),
+                           (uint64_t)VM_KERNEL_ADDRPERM(frag)));
+                       break;
                case AF_INET6:
-                     DPFPRINTF(("expiring IPv6 %d(%p) from queue.\n",
-                         ntohl(frag->fr_id6), frag));
-                     break;
+                       DPFPRINTF(("expiring IPv6 %d(0x%llx) from queue.\n",
+                           ntohl(frag->fr_id6),
+                           (uint64_t)VM_KERNEL_ADDRPERM(frag)));
+                       break;
                default:
-                     VERIFY(0 && "only IPv4 and IPv6 supported");
-                     break;
+                       VERIFY(0 && "only IPv4 and IPv6 supported");
+                       break;
                }
                pf_free_fragment(frag);
        }
 
        while ((frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue)) != NULL) {
                VERIFY(!BUFFER_FRAGMENTS(frag));
-               if (frag->fr_timeout > expire)
+               if (frag->fr_timeout > expire) {
                        break;
+               }
 
                switch (frag->fr_af) {
                case AF_INET:
-                     DPFPRINTF(("expiring IPv4 %d(%p) from cache.\n",
-                         ntohs(frag->fr_id), frag));
-                     break;
+                       DPFPRINTF(("expiring IPv4 %d(0x%llx) from cache.\n",
+                           ntohs(frag->fr_id),
+                           (uint64_t)VM_KERNEL_ADDRPERM(frag)));
+                       break;
                case AF_INET6:
-                     DPFPRINTF(("expiring IPv6 %d(%p) from cache.\n",
-                         ntohl(frag->fr_id6), frag));
-                     break;
+                       DPFPRINTF(("expiring IPv6 %d(0x%llx) from cache.\n",
+                           ntohl(frag->fr_id6),
+                           (uint64_t)VM_KERNEL_ADDRPERM(frag)));
+                       break;
                default:
-                     VERIFY(0 && "only IPv4 and IPv6 supported");
-                     break;
+                       VERIFY(0 && "only IPv4 and IPv6 supported");
+                       break;
                }
                pf_free_fragment(frag);
                VERIFY(TAILQ_EMPTY(&pf_cachequeue) ||
@@ -361,16 +371,17 @@ pf_purge_expired_fragments(void)
 static void
 pf_flush_fragments(void)
 {
-       struct pf_fragment      *frag;
-       int                      goal;
+       struct pf_fragment      *frag;
+       int                      goal;
 
        goal = pf_nfrents * 9 / 10;
        DPFPRINTF(("trying to free > %d frents\n",
            pf_nfrents - goal));
        while (goal < pf_nfrents) {
                frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue);
-               if (frag == NULL)
+               if (frag == NULL) {
                        break;
+               }
                pf_free_fragment(frag);
        }
 
@@ -380,8 +391,9 @@ pf_flush_fragments(void)
            pf_ncache - goal));
        while (goal < pf_ncache) {
                frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue);
-               if (frag == NULL)
+               if (frag == NULL) {
                        break;
+               }
                pf_free_fragment(frag);
        }
 }
@@ -391,8 +403,8 @@ pf_flush_fragments(void)
 static void
 pf_free_fragment(struct pf_fragment *frag)
 {
-       struct pf_frent         *frent;
-       struct pf_frcache       *frcache;
+       struct pf_frent         *frent;
+       struct pf_frcache       *frcache;
 
        /* Free all fragments */
        if (BUFFER_FRAGMENTS(frag)) {
@@ -428,25 +440,25 @@ pf_ip6hdr2key(struct pf_fragment *key, struct ip6_hdr *ip6,
        key->fr_p = fh->ip6f_nxt;
        key->fr_id6 = fh->ip6f_ident;
        key->fr_af = AF_INET6;
-       key->fr_srcx.v6 = ip6->ip6_src;
-       key->fr_dstx.v6 = ip6->ip6_dst;
+       key->fr_srcx.v6addr = ip6->ip6_src;
+       key->fr_dstx.v6addr = ip6->ip6_dst;
 }
+
 static void
 pf_ip2key(struct pf_fragment *key, struct ip *ip)
 {
        key->fr_p = ip->ip_p;
        key->fr_id = ip->ip_id;
        key->fr_af = AF_INET;
-       key->fr_srcx.v4.s_addr = ip->ip_src.s_addr;
-       key->fr_dstx.v4.s_addr = ip->ip_dst.s_addr;
+       key->fr_srcx.v4addr.s_addr = ip->ip_src.s_addr;
+       key->fr_dstx.v4addr.s_addr = ip->ip_dst.s_addr;
 }
 
 static struct pf_fragment *
 pf_find_fragment_by_key(struct pf_fragment *key, struct pf_frag_tree *tree)
 {
        struct pf_fragment *frag;
-       
+
        frag = RB_FIND(pf_frag_tree, tree, key);
        if (frag != NULL) {
                /* XXX Are we sure we want to update the timeout? */
@@ -459,10 +471,10 @@ pf_find_fragment_by_key(struct pf_fragment *key, struct pf_frag_tree *tree)
                        TAILQ_INSERT_HEAD(&pf_cachequeue, frag, frag_next);
                }
        }
-       
-       return (frag);
+
+       return frag;
 }
-  
+
 static __inline struct pf_fragment *
 pf_find_fragment_by_ipv4_header(struct ip *ip, struct pf_frag_tree *tree)
 {
@@ -475,9 +487,9 @@ static __inline struct pf_fragment *
 pf_find_fragment_by_ipv6_header(struct ip6_hdr *ip6, struct ip6_frag *fh,
     struct pf_frag_tree *tree)
 {
-      struct pf_fragment key;
-      pf_ip6hdr2key(&key, ip6, fh);
-      return pf_find_fragment_by_key(&key, tree);
+       struct pf_fragment key;
+       pf_ip6hdr2key(&key, ip6, fh);
+       return pf_find_fragment_by_key(&key, tree);
 }
 
 /* Removes a fragment from the fragment queue and frees the fragment */
@@ -496,22 +508,84 @@ pf_remove_fragment(struct pf_fragment *frag)
        }
 }
 
-#define FR_IP_OFF(fr)  ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
+#define FR_IP_OFF(fr)   ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
 static struct mbuf *
-pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
+pf_reassemble(struct mbuf *m0, struct pf_fragment **frag,
     struct pf_frent *frent, int mff)
 {
-       struct mbuf     *m = *m0, *m2;
-       struct pf_frent *frea, *next;
-       struct pf_frent *frep = NULL;
-       struct ip       *ip = frent->fr_ip;
-       int              hlen = ip->ip_hl << 2;
-       u_int16_t        off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
-       u_int16_t        ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
-       u_int16_t        fr_max = ip_len + off;
+       struct mbuf     *m = m0, *m2;
+       struct pf_frent *frea, *next;
+       struct pf_frent *frep = NULL;
+       struct ip       *ip = frent->fr_ip;
+       uint32_t         hlen = ip->ip_hl << 2;
+       u_int16_t        off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
+       u_int16_t        ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
+       u_int16_t        fr_max = ip_len + off;
+       uint32_t         csum, csum_flags;
 
        VERIFY(*frag == NULL || BUFFER_FRAGMENTS(*frag));
 
+       /*
+        * Leverage partial checksum offload for IP fragments.  Narrow down
+        * the scope to cover only UDP without IP options, as that is the
+        * most common case.
+        *
+        * Perform 1's complement adjustment of octets that got included/
+        * excluded in the hardware-calculated checksum value.  Ignore cases
+        * where the value includes the entire IPv4 header span, as the sum
+        * for those octets would already be 0 by the time we get here; IP
+        * has already performed its header checksum validation.  Also take
+        * care of any trailing bytes and subtract out their partial sum.
+        */
+       if (ip->ip_p == IPPROTO_UDP && hlen == sizeof(struct ip) &&
+           (m->m_pkthdr.csum_flags &
+           (CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_PSEUDO_HDR)) ==
+           (CSUM_DATA_VALID | CSUM_PARTIAL)) {
+               uint32_t start = m->m_pkthdr.csum_rx_start;
+               int32_t trailer = (m_pktlen(m) - ntohs(ip->ip_len));
+               uint32_t swbytes = (uint32_t)trailer;
+
+               csum = m->m_pkthdr.csum_rx_val;
+
+               ASSERT(trailer >= 0);
+               if ((start != 0 && start != hlen) || trailer != 0) {
+#if BYTE_ORDER != BIG_ENDIAN
+                       if (start < hlen) {
+                               HTONS(ip->ip_len);
+                               HTONS(ip->ip_off);
+                       }
+#endif /* BYTE_ORDER != BIG_ENDIAN */
+                       /* callee folds in sum */
+                       csum = m_adj_sum16(m, start, hlen,
+                           (ip->ip_len - hlen), csum);
+                       if (hlen > start) {
+                               swbytes += (hlen - start);
+                       } else {
+                               swbytes += (start - hlen);
+                       }
+#if BYTE_ORDER != BIG_ENDIAN
+                       if (start < hlen) {
+                               NTOHS(ip->ip_off);
+                               NTOHS(ip->ip_len);
+                       }
+#endif /* BYTE_ORDER != BIG_ENDIAN */
+               }
+               csum_flags = m->m_pkthdr.csum_flags;
+
+               if (swbytes != 0) {
+                       udp_in_cksum_stats(swbytes);
+               }
+               if (trailer != 0) {
+                       m_adj(m, -trailer);
+               }
+       } else {
+               csum = 0;
+               csum_flags = 0;
+       }
+
+       /* Invalidate checksum */
+       m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
+
        /* Strip off ip header */
        m->m_data += hlen;
        m->m_len -= hlen;
@@ -522,18 +596,23 @@ pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
                if (*frag == NULL) {
                        pf_flush_fragments();
                        *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
-                       if (*frag == NULL)
+                       if (*frag == NULL) {
                                goto drop_fragment;
+                       }
                }
 
                (*frag)->fr_flags = 0;
                (*frag)->fr_max = 0;
                (*frag)->fr_af = AF_INET;
-               (*frag)->fr_srcx.v4 = frent->fr_ip->ip_src;
-               (*frag)->fr_dstx.v4 = frent->fr_ip->ip_dst;
+               (*frag)->fr_srcx.v4addr = frent->fr_ip->ip_src;
+               (*frag)->fr_dstx.v4addr = frent->fr_ip->ip_dst;
                (*frag)->fr_p = frent->fr_ip->ip_p;
                (*frag)->fr_id = frent->fr_ip->ip_id;
                (*frag)->fr_timeout = pf_time_second();
+               if (csum_flags != 0) {
+                       (*frag)->fr_csum_flags = csum_flags;
+                       (*frag)->fr_csum = csum;
+               }
                LIST_INIT(&(*frag)->fr_queue);
 
                RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag);
@@ -544,13 +623,25 @@ pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
                goto insert;
        }
 
+       /*
+        * If this fragment contains similar checksum offload info
+        * as that of the existing ones, accumulate checksum.  Otherwise,
+        * invalidate checksum offload info for the entire datagram.
+        */
+       if (csum_flags != 0 && csum_flags == (*frag)->fr_csum_flags) {
+               (*frag)->fr_csum += csum;
+       } else if ((*frag)->fr_csum_flags != 0) {
+               (*frag)->fr_csum_flags = 0;
+       }
+
        /*
         * Find a fragment after the current one:
         *  - off contains the real shifted offset.
         */
        LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
-               if (FR_IP_OFF(frea) > off)
+               if (FR_IP_OFF(frea) > off) {
                        break;
+               }
                frep = frea;
        }
 
@@ -559,12 +650,13 @@ pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
        if (frep != NULL &&
            FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl *
            4 > off) {
-               u_int16_t       precut;
+               u_int16_t       precut;
 
                precut = FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) -
                    frep->fr_ip->ip_hl * 4 - off;
-               if (precut >= ip_len)
+               if (precut >= ip_len) {
                        goto drop_fragment;
+               }
                m_adj(frent->fr_m, precut);
                DPFPRINTF(("overlap -%d\n", precut));
                /* Enforce 8 byte boundaries */
@@ -576,7 +668,7 @@ pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
 
        for (; frea != NULL && ip_len + off > FR_IP_OFF(frea);
            frea = next) {
-               u_int16_t       aftercut;
+               u_int16_t       aftercut;
 
                aftercut = ip_len + off - FR_IP_OFF(frea);
                DPFPRINTF(("adjust overlap %d\n", aftercut));
@@ -600,20 +692,24 @@ pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
 
 insert:
        /* Update maximum data size */
-       if ((*frag)->fr_max < fr_max)
+       if ((*frag)->fr_max < fr_max) {
                (*frag)->fr_max = fr_max;
+       }
        /* This is the last segment */
-       if (!mff)
+       if (!mff) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
+       }
 
-       if (frep == NULL)
+       if (frep == NULL) {
                LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
-       else
+       } else {
                LIST_INSERT_AFTER(frep, frent, fr_next);
+       }
 
        /* Check if we are completely reassembled */
-       if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
-               return (NULL);
+       if (!((*frag)->fr_flags & PFFRAG_SEENLAST)) {
+               return NULL;
+       }
 
        /* Check if we have all the data */
        off = 0;
@@ -626,12 +722,13 @@ insert:
                        DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
                            off, next == NULL ? -1 : FR_IP_OFF(next),
                            (*frag)->fr_max));
-                       return (NULL);
+                       return NULL;
                }
        }
        DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
-       if (off < (*frag)->fr_max)
-               return (NULL);
+       if (off < (*frag)->fr_max) {
+               return NULL;
+       }
 
        /* We have all the data */
        frent = LIST_FIRST(&(*frag)->fr_queue);
@@ -640,7 +737,7 @@ insert:
                DPFPRINTF(("drop: too big: %d\n", off));
                pf_free_fragment(*frag);
                *frag = NULL;
-               return (NULL);
+               return NULL;
        }
        next = LIST_NEXT(frent, fr_next);
 
@@ -661,8 +758,26 @@ insert:
                m_cat(m, m2);
        }
 
-       ip->ip_src = (*frag)->fr_srcx.v4;
-       ip->ip_dst = (*frag)->fr_dstx.v4;
+       ip->ip_src = (*frag)->fr_srcx.v4addr;
+       ip->ip_dst = (*frag)->fr_dstx.v4addr;
+
+       if ((*frag)->fr_csum_flags != 0) {
+               csum = (*frag)->fr_csum;
+
+               ADDCARRY(csum);
+
+               m->m_pkthdr.csum_rx_val = csum;
+               m->m_pkthdr.csum_rx_start = sizeof(struct ip);
+               m->m_pkthdr.csum_flags = (*frag)->fr_csum_flags;
+       } else if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
+           (m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
+               /* loopback checksums are always OK */
+               m->m_pkthdr.csum_data = 0xffff;
+               m->m_pkthdr.csum_flags &= ~CSUM_PARTIAL;
+               m->m_pkthdr.csum_flags =
+                   CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
+                   CSUM_IP_CHECKED | CSUM_IP_VALID;
+       }
 
        /* Remove from fragment queue */
        pf_remove_fragment(*frag);
@@ -677,32 +792,34 @@ insert:
        /* XXX this should be done elsewhere */
        if (m->m_flags & M_PKTHDR) {
                int plen = 0;
-               for (m2 = m; m2; m2 = m2->m_next)
+               for (m2 = m; m2; m2 = m2->m_next) {
                        plen += m2->m_len;
+               }
                m->m_pkthdr.len = plen;
        }
 
-       DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
-       return (m);
+       DPFPRINTF(("complete: 0x%llx(%d)\n",
+           (uint64_t)VM_KERNEL_ADDRPERM(m), ntohs(ip->ip_len)));
+       return m;
 
 drop_fragment:
        /* Oops - fail safe - drop packet */
        pool_put(&pf_frent_pl, frent);
        pf_nfrents--;
        m_freem(m);
-       return (NULL);
+       return NULL;
 }
 
 static struct mbuf *
 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
     int drop, int *nomem)
 {
-       struct mbuf             *m = *m0;
-       struct pf_frcache       *frp, *fra, *cur = NULL;
-       int                      ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
-       u_int16_t                off = ntohs(h->ip_off) << 3;
-       u_int16_t                fr_max = ip_len + off;
-       int                      hosed = 0;
+       struct mbuf             *m = *m0;
+       struct pf_frcache       *frp, *fra, *cur = NULL;
+       int                      ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
+       u_int16_t                off = ntohs(h->ip_off) << 3;
+       u_int16_t                fr_max = ip_len + off;
+       int                      hosed = 0;
 
        VERIFY(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
 
@@ -712,8 +829,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                if (*frag == NULL) {
                        pf_flush_fragments();
                        *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
-                       if (*frag == NULL)
+                       if (*frag == NULL) {
                                goto no_mem;
+                       }
                }
 
                /* Get an entry for the queue */
@@ -728,8 +846,8 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                (*frag)->fr_flags = PFFRAG_NOBUFFER;
                (*frag)->fr_max = 0;
                (*frag)->fr_af = AF_INET;
-               (*frag)->fr_srcx.v4 = h->ip_src;
-               (*frag)->fr_dstx.v4 = h->ip_dst;
+               (*frag)->fr_srcx.v4addr = h->ip_src;
+               (*frag)->fr_dstx.v4addr = h->ip_dst;
                (*frag)->fr_p = h->ip_p;
                (*frag)->fr_id = h->ip_id;
                (*frag)->fr_timeout = pf_time_second();
@@ -754,15 +872,16 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
         */
        frp = NULL;
        LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
-               if (fra->fr_off > off)
+               if (fra->fr_off > off) {
                        break;
+               }
                frp = fra;
        }
 
        VERIFY(frp != NULL || fra != NULL);
 
        if (frp != NULL) {
-               int     precut;
+               int     precut;
 
                precut = frp->fr_end - off;
                if (precut >= ip_len) {
@@ -804,8 +923,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                                 * I'll pull a rabbit out of my laptop.
                                 */
                                *m0 = m_copym(m, 0, h->ip_hl << 2, M_NOWAIT);
-                               if (*m0 == NULL)
+                               if (*m0 == NULL) {
                                        goto no_mem;
+                               }
                                VERIFY((*m0)->m_next == NULL);
                                m_adj(m, precut + (h->ip_hl << 2));
                                m_cat(*m0, m);
@@ -813,8 +933,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                                if (m->m_flags & M_PKTHDR) {
                                        int plen = 0;
                                        struct mbuf *t;
-                                       for (t = m; t; t = t->m_next)
+                                       for (t = m; t; t = t->m_next) {
                                                plen += t->m_len;
+                                       }
                                        m->m_pkthdr.len = plen;
                                }
 
@@ -838,8 +959,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                            fr_max));
 
                        cur = pool_get(&pf_cent_pl, PR_NOWAIT);
-                       if (cur == NULL)
+                       if (cur == NULL) {
                                goto no_mem;
+                       }
                        pf_ncache++;
 
                        cur->fr_off = off;
@@ -849,8 +971,8 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
        }
 
        if (fra != NULL) {
-               int     aftercut;
-               int     merge = 0;
+               int     aftercut;
+               int     merge = 0;
 
                aftercut = fr_max - fra->fr_off;
                if (aftercut == 0) {
@@ -874,8 +996,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                                if (m->m_flags & M_PKTHDR) {
                                        int plen = 0;
                                        struct mbuf *t;
-                                       for (t = m; t; t = t->m_next)
+                                       for (t = m; t; t = t->m_next) {
                                                plen += t->m_len;
+                                       }
                                        m->m_pkthdr.len = plen;
                                }
                                h = mtod(m, struct ip *);
@@ -892,8 +1015,9 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                            fra->fr_end));
 
                        cur = pool_get(&pf_cent_pl, PR_NOWAIT);
-                       if (cur == NULL)
+                       if (cur == NULL) {
                                goto no_mem;
+                       }
                        pf_ncache++;
 
                        cur->fr_off = off;
@@ -915,7 +1039,6 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                                pool_put(&pf_cent_pl, cur);
                                pf_ncache--;
                                cur = NULL;
-
                        } else if (frp && fra->fr_off <= frp->fr_end) {
                                /* Need to merge in a modified 'frp' */
                                VERIFY(cur == NULL);
@@ -928,7 +1051,6 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
                                pool_put(&pf_cent_pl, frp);
                                pf_ncache--;
                                frp = NULL;
-
                        }
                }
        }
@@ -945,12 +1067,14 @@ pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
 
 pass:
        /* Update maximum data size */
-       if ((*frag)->fr_max < fr_max)
+       if ((*frag)->fr_max < fr_max) {
                (*frag)->fr_max = fr_max;
+       }
 
        /* This is the last segment */
-       if (!mff)
+       if (!mff) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
+       }
 
        /* Check if we are completely reassembled */
        if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
@@ -963,34 +1087,37 @@ pass:
                *frag = NULL;
        }
 
-       return (m);
+       return m;
 
 no_mem:
        *nomem = 1;
 
        /* Still need to pay attention to !IP_MF */
-       if (!mff && *frag != NULL)
+       if (!mff && *frag != NULL) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
+       }
 
        m_freem(m);
-       return (NULL);
+       return NULL;
 
 drop_fragment:
 
        /* Still need to pay attention to !IP_MF */
-       if (!mff && *frag != NULL)
+       if (!mff && *frag != NULL) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
+       }
 
        if (drop) {
                /* This fragment has been deemed bad.  Don't reass */
-               if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
+               if (((*frag)->fr_flags & PFFRAG_DROP) == 0) {
                        DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
                            h->ip_id));
+               }
                (*frag)->fr_flags |= PFFRAG_DROP;
        }
 
        m_freem(m);
-       return (NULL);
+       return NULL;
 }
 
 #define FR_IP6_OFF(fr) \
@@ -1003,72 +1130,159 @@ pf_reassemble6(struct mbuf **m0, struct pf_fragment **frag,
        struct mbuf *m, *m2;
        struct pf_frent *frea, *frep, *next;
        struct ip6_hdr *ip6;
+       struct ip6_frag *ip6f;
        int plen, off, fr_max;
-       
+       uint32_t uoff, csum, csum_flags;
+
        VERIFY(*frag == NULL || BUFFER_FRAGMENTS(*frag));
        m = *m0;
        frep = NULL;
        ip6 = frent->fr_ip6;
+       ip6f = &frent->fr_ip6f_opt;
        off = FR_IP6_OFF(frent);
+       uoff = frent->fr_ip6f_hlen;
        plen = FR_IP6_PLEN(frent);
        fr_max = off + plen - (frent->fr_ip6f_hlen - sizeof *ip6);
 
-       DPFPRINTF(("%p IPv6 frag plen %u off %u fr_ip6f_hlen %u fr_max %u m_len %u\n", m, 
-               plen, off, frent->fr_ip6f_hlen, fr_max, m->m_len));
-       
+       DPFPRINTF(("0x%llx IPv6 frag plen %u off %u fr_ip6f_hlen %u "
+           "fr_max %u m_len %u\n", (uint64_t)VM_KERNEL_ADDRPERM(m), plen, off,
+           frent->fr_ip6f_hlen, fr_max, m->m_len));
+
+       /*
+        * Leverage partial checksum offload for simple UDP/IP fragments,
+        * as that is the most common case.
+        *
+        * Perform 1's complement adjustment of octets that got included/
+        * excluded in the hardware-calculated checksum value.  Also take
+        * care of any trailing bytes and subtract out their partial sum.
+        */
+       if (ip6f->ip6f_nxt == IPPROTO_UDP &&
+           uoff == (sizeof(*ip6) + sizeof(*ip6f)) &&
+           (m->m_pkthdr.csum_flags &
+           (CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_PSEUDO_HDR)) ==
+           (CSUM_DATA_VALID | CSUM_PARTIAL)) {
+               uint32_t start = m->m_pkthdr.csum_rx_start;
+               uint32_t ip_len = (sizeof(*ip6) + ntohs(ip6->ip6_plen));
+               int32_t trailer = (m_pktlen(m) - ip_len);
+               uint32_t swbytes = (uint32_t)trailer;
+
+               csum = m->m_pkthdr.csum_rx_val;
+
+               ASSERT(trailer >= 0);
+               if (start != uoff || trailer != 0) {
+                       uint16_t s = 0, d = 0;
+
+                       if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
+                               s = ip6->ip6_src.s6_addr16[1];
+                               ip6->ip6_src.s6_addr16[1] = 0;
+                       }
+                       if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
+                               d = ip6->ip6_dst.s6_addr16[1];
+                               ip6->ip6_dst.s6_addr16[1] = 0;
+                       }
+
+                       /* callee folds in sum */
+                       csum = m_adj_sum16(m, start, uoff,
+                           (ip_len - uoff), csum);
+                       if (uoff > start) {
+                               swbytes += (uoff - start);
+                       } else {
+                               swbytes += (start - uoff);
+                       }
+
+                       if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
+                               ip6->ip6_src.s6_addr16[1] = s;
+                       }
+                       if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
+                               ip6->ip6_dst.s6_addr16[1] = d;
+                       }
+               }
+               csum_flags = m->m_pkthdr.csum_flags;
+
+               if (swbytes != 0) {
+                       udp_in6_cksum_stats(swbytes);
+               }
+               if (trailer != 0) {
+                       m_adj(m, -trailer);
+               }
+       } else {
+               csum = 0;
+               csum_flags = 0;
+       }
+
+       /* Invalidate checksum */
+       m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
+
        /* strip off headers up to the fragment payload */
        m->m_data += frent->fr_ip6f_hlen;
        m->m_len -= frent->fr_ip6f_hlen;
-       
+
        /* Create a new reassembly queue for this packet */
        if (*frag == NULL) {
                *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
                if (*frag == NULL) {
                        pf_flush_fragments();
                        *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
-                       if (*frag == NULL)
+                       if (*frag == NULL) {
                                goto drop_fragment;
+                       }
                }
-               
+
                (*frag)->fr_flags = 0;
                (*frag)->fr_max = 0;
                (*frag)->fr_af = AF_INET6;
-               (*frag)->fr_srcx.v6 = frent->fr_ip6->ip6_src;
-               (*frag)->fr_dstx.v6 = frent->fr_ip6->ip6_dst;
+               (*frag)->fr_srcx.v6addr = frent->fr_ip6->ip6_src;
+               (*frag)->fr_dstx.v6addr = frent->fr_ip6->ip6_dst;
                (*frag)->fr_p = frent->fr_ip6f_opt.ip6f_nxt;
                (*frag)->fr_id6 = frent->fr_ip6f_opt.ip6f_ident;
                (*frag)->fr_timeout = pf_time_second();
+               if (csum_flags != 0) {
+                       (*frag)->fr_csum_flags = csum_flags;
+                       (*frag)->fr_csum = csum;
+               }
                LIST_INIT(&(*frag)->fr_queue);
-               
+
                RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag);
                TAILQ_INSERT_HEAD(&pf_fragqueue, *frag, frag_next);
-               
+
                /* We do not have a previous fragment */
                frep = NULL;
                goto insert;
        }
-       
+
+       /*
+        * If this fragment contains similar checksum offload info
+        * as that of the existing ones, accumulate checksum.  Otherwise,
+        * invalidate checksum offload info for the entire datagram.
+        */
+       if (csum_flags != 0 && csum_flags == (*frag)->fr_csum_flags) {
+               (*frag)->fr_csum += csum;
+       } else if ((*frag)->fr_csum_flags != 0) {
+               (*frag)->fr_csum_flags = 0;
+       }
+
        /*
         * Find a fragment after the current one:
         *  - off contains the real shifted offset.
         */
        LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
-               if (FR_IP6_OFF(frea) > off)
+               if (FR_IP6_OFF(frea) > off) {
                        break;
+               }
                frep = frea;
        }
-       
+
        VERIFY(frep != NULL || frea != NULL);
-       
+
        if (frep != NULL &&
-           FR_IP6_OFF(frep) + FR_IP6_PLEN(frep) - frep->fr_ip6f_hlen > off)
-       {
+           FR_IP6_OFF(frep) + FR_IP6_PLEN(frep) - frep->fr_ip6f_hlen > off) {
                u_int16_t precut;
-               
+
                precut = FR_IP6_OFF(frep) + FR_IP6_PLEN(frep) -
                    frep->fr_ip6f_hlen - off;
-               if (precut >= plen)
+               if (precut >= plen) {
                        goto drop_fragment;
+               }
                m_adj(frent->fr_m, precut);
                DPFPRINTF(("overlap -%d\n", precut));
                /* Enforce 8 byte boundaries */
@@ -1079,22 +1293,22 @@ pf_reassemble6(struct mbuf **m0, struct pf_fragment **frag,
                plen -= precut;
                ip6->ip6_plen = htons(plen);
        }
-       
+
        for (; frea != NULL && plen + off > FR_IP6_OFF(frea); frea = next) {
-               u_int16_t       aftercut;
-               
+               u_int16_t       aftercut;
+
                aftercut = plen + off - FR_IP6_OFF(frea);
                DPFPRINTF(("adjust overlap %d\n", aftercut));
                if (aftercut < FR_IP6_PLEN(frea) - frea->fr_ip6f_hlen) {
                        frea->fr_ip6->ip6_plen = htons(FR_IP6_PLEN(frea) -
-                               aftercut);
+                           aftercut);
                        frea->fr_ip6f_opt.ip6f_offlg =
                            htons(ntohs(frea->fr_ip6f_opt.ip6f_offlg) +
                            (aftercut >> 3));
                        m_adj(frea->fr_m, aftercut);
                        break;
                }
-               
+
                /* This fragment is completely overlapped, lose it */
                next = LIST_NEXT(frea, fr_next);
                m_freem(frea->fr_m);
@@ -1102,44 +1316,49 @@ pf_reassemble6(struct mbuf **m0, struct pf_fragment **frag,
                pool_put(&pf_frent_pl, frea);
                pf_nfrents--;
        }
-       
-  insert:
+
+insert:
        /* Update maximum data size */
-       if ((*frag)->fr_max < fr_max)
+       if ((*frag)->fr_max < fr_max) {
                (*frag)->fr_max = fr_max;
+       }
        /* This is the last segment */
-       if (!mff)
+       if (!mff) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
-       
-       if (frep == NULL)
+       }
+
+       if (frep == NULL) {
                LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
-       else
+       } else {
                LIST_INSERT_AFTER(frep, frent, fr_next);
-       
+       }
+
        /* Check if we are completely reassembled */
-       if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
-               return (NULL);
-       
+       if (!((*frag)->fr_flags & PFFRAG_SEENLAST)) {
+               return NULL;
+       }
+
        /* Check if we have all the data */
        off = 0;
        for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) {
                next = LIST_NEXT(frep, fr_next);
                off += FR_IP6_PLEN(frep) - (frent->fr_ip6f_hlen - sizeof *ip6);
                DPFPRINTF(("frep at %d, next %d, max %d\n",
-                       off, next == NULL ? -1 : FR_IP6_OFF(next),
-                       (*frag)->fr_max));
+                   off, next == NULL ? -1 : FR_IP6_OFF(next),
+                   (*frag)->fr_max));
                if (off < (*frag)->fr_max &&
                    (next == NULL || FR_IP6_OFF(next) != off)) {
                        DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
                            off, next == NULL ? -1 : FR_IP6_OFF(next),
                            (*frag)->fr_max));
-                       return (NULL);
+                       return NULL;
                }
        }
        DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
-       if (off < (*frag)->fr_max)
-               return (NULL);
-       
+       if (off < (*frag)->fr_max) {
+               return NULL;
+       }
+
        /* We have all the data */
        frent = LIST_FIRST(&(*frag)->fr_queue);
        VERIFY(frent != NULL);
@@ -1147,24 +1366,40 @@ pf_reassemble6(struct mbuf **m0, struct pf_fragment **frag,
                DPFPRINTF(("drop: too big: %d\n", off));
                pf_free_fragment(*frag);
                *frag = NULL;
-               return (NULL);
+               return NULL;
        }
-       
+
        ip6 = frent->fr_ip6;
        ip6->ip6_nxt = (*frag)->fr_p;
        ip6->ip6_plen = htons(off);
-       ip6->ip6_src = (*frag)->fr_srcx.v6;
-       ip6->ip6_dst = (*frag)->fr_dstx.v6;
-       
+       ip6->ip6_src = (*frag)->fr_srcx.v6addr;
+       ip6->ip6_dst = (*frag)->fr_dstx.v6addr;
+
+       if ((*frag)->fr_csum_flags != 0) {
+               csum = (*frag)->fr_csum;
+
+               ADDCARRY(csum);
+
+               m->m_pkthdr.csum_rx_val = csum;
+               m->m_pkthdr.csum_rx_start = sizeof(struct ip6_hdr);
+               m->m_pkthdr.csum_flags = (*frag)->fr_csum_flags;
+       } else if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
+           (m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
+               /* loopback checksums are always OK */
+               m->m_pkthdr.csum_data = 0xffff;
+               m->m_pkthdr.csum_flags &= ~CSUM_PARTIAL;
+               m->m_pkthdr.csum_flags = CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
+       }
+
        /* Remove from fragment queue */
        pf_remove_fragment(*frag);
        *frag = NULL;
-       
+
        m = frent->fr_m;
        m->m_len += sizeof(struct ip6_hdr);
        m->m_data -= sizeof(struct ip6_hdr);
        memmove(m->m_data, ip6, sizeof(struct ip6_hdr));
-       
+
        next = LIST_NEXT(frent, fr_next);
        pool_put(&pf_frent_pl, frent);
        pf_nfrents--;
@@ -1176,21 +1411,23 @@ pf_reassemble6(struct mbuf **m0, struct pf_fragment **frag,
                pool_put(&pf_frent_pl, frent);
                pf_nfrents--;
        }
-       
+
        /* XXX this should be done elsewhere */
        if (m->m_flags & M_PKTHDR) {
                int pktlen = 0;
-               for (m2 = m; m2; m2 = m2->m_next)
+               for (m2 = m; m2; m2 = m2->m_next) {
                        pktlen += m2->m_len;
+               }
                m->m_pkthdr.len = pktlen;
        }
-       
-       DPFPRINTF(("complete: %p ip6_plen %d m_pkthdr.len %d\n", 
-               m, ntohs(ip6->ip6_plen), m->m_pkthdr.len));
+
+       DPFPRINTF(("complete: 0x%llx ip6_plen %d m_pkthdr.len %d\n",
+           (uint64_t)VM_KERNEL_ADDRPERM(m), ntohs(ip6->ip6_plen),
+           m->m_pkthdr.len));
 
        return m;
-       
- drop_fragment:
+
+drop_fragment:
        /* Oops - fail safe - drop packet */
        pool_put(&pf_frent_pl, frent);
        --pf_nfrents;
@@ -1206,7 +1443,7 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
        u_int16_t plen, off, fr_max;
        struct pf_frcache *frp, *fra, *cur = NULL;
        int hosed = 0;
-       
+
        VERIFY(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
        m = *m0;
        off = ntohs(fh->ip6f_offlg & IP6F_OFF_MASK);
@@ -1214,7 +1451,7 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
 
        /*
         * Apple Modification: dimambro@apple.com. The hlen, being passed
-        * into this function Includes all the headers associated with 
+        * into this function Includes all the headers associated with
         * the packet, and may include routing headers, so to get to
         * the data payload as stored in the original IPv6 header we need
         * to subtract al those headers and the IP header.
@@ -1230,9 +1467,9 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
         *   of the IPv6 packet to the beginning of the data.
         */
        fr_max = off + plen;
-       
-       DPFPRINTF(("%p plen %u off %u fr_max %u\n", m, 
-               plen, off, fr_max));
+
+       DPFPRINTF(("0x%llx plen %u off %u fr_max %u\n",
+           (uint64_t)VM_KERNEL_ADDRPERM(m), plen, off, fr_max));
 
        /* Create a new range queue for this packet */
        if (*frag == NULL) {
@@ -1240,10 +1477,11 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                if (*frag == NULL) {
                        pf_flush_fragments();
                        *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
-                       if (*frag == NULL)
+                       if (*frag == NULL) {
                                goto no_mem;
+                       }
                }
-               
+
                /* Get an entry for the queue */
                cur = pool_get(&pf_cent_pl, PR_NOWAIT);
                if (cur == NULL) {
@@ -1252,46 +1490,47 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                        goto no_mem;
                }
                pf_ncache++;
-               
+
                (*frag)->fr_flags = PFFRAG_NOBUFFER;
                (*frag)->fr_max = 0;
                (*frag)->fr_af = AF_INET6;
-               (*frag)->fr_srcx.v6 = h->ip6_src;
-               (*frag)->fr_dstx.v6 = h->ip6_dst;
+               (*frag)->fr_srcx.v6addr = h->ip6_src;
+               (*frag)->fr_dstx.v6addr = h->ip6_dst;
                (*frag)->fr_p = fh->ip6f_nxt;
                (*frag)->fr_id6 = fh->ip6f_ident;
                (*frag)->fr_timeout = pf_time_second();
-               
+
                cur->fr_off = off;
                cur->fr_end = fr_max;
                LIST_INIT(&(*frag)->fr_cache);
                LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
-               
+
                RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag);
                TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next);
-               
+
                DPFPRINTF(("frag6cache[%d]: new %d-%d\n", ntohl(fh->ip6f_ident),
                    off, fr_max));
-               
+
                goto pass;
        }
-       
+
        /*
         * Find a fragment after the current one:
         *  - off contains the real shifted offset.
         */
        frp = NULL;
        LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
-               if (fra->fr_off > off)
+               if (fra->fr_off > off) {
                        break;
+               }
                frp = fra;
        }
-       
+
        VERIFY(frp != NULL || fra != NULL);
-       
+
        if (frp != NULL) {
                int precut;
-               
+
                precut = frp->fr_end - off;
                if (precut >= plen) {
                        /* Fragment is entirely a duplicate */
@@ -1313,16 +1552,16 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                         * But to do so easily, we need to create another
                         * mbuf to throw the original header into.
                         */
-                       
+
                        DPFPRINTF(("frag6cache[%u]: chop %d (%d-%d) %d-%d\n",
                            ntohl(fh->ip6f_ident), precut, frp->fr_off,
                            frp->fr_end, off, fr_max));
-                       
+
                        off += precut;
                        fr_max -= precut;
                        /* Update the previous frag to encompass this one */
                        frp->fr_end = fr_max;
-                       
+
                        if (!drop) {
                                /* XXX Optimization opportunity
                                 * This is a very heavy way to trim the payload.
@@ -1332,8 +1571,9 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                                 * I'll pull a rabbit out of my laptop.
                                 */
                                *m0 = m_copym(m, 0, hlen, M_NOWAIT);
-                               if (*m0 == NULL)
+                               if (*m0 == NULL) {
                                        goto no_mem;
+                               }
                                VERIFY((*m0)->m_next == NULL);
                                m_adj(m, precut + hlen);
                                m_cat(*m0, m);
@@ -1341,13 +1581,14 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                                if (m->m_flags & M_PKTHDR) {
                                        int pktlen = 0;
                                        struct mbuf *t;
-                                       for (t = m; t; t = t->m_next)
+                                       for (t = m; t; t = t->m_next) {
                                                pktlen += t->m_len;
+                                       }
                                        m->m_pkthdr.len = pktlen;
                                }
-                               
+
                                h = mtod(m, struct ip6_hdr *);
-                               
+
                                VERIFY((int)m->m_len ==
                                    ntohs(h->ip6_plen) - precut);
                                fh->ip6f_offlg &= ~IP6F_OFF_MASK;
@@ -1361,26 +1602,27 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                        }
                } else {
                        /* There is a gap between fragments */
-                       
+
                        DPFPRINTF(("frag6cache[%u]: gap %d (%d-%d) %d-%d\n",
                            ntohl(fh->ip6f_ident), -precut, frp->fr_off,
                            frp->fr_end, off, fr_max));
-                       
+
                        cur = pool_get(&pf_cent_pl, PR_NOWAIT);
-                       if (cur == NULL)
+                       if (cur == NULL) {
                                goto no_mem;
+                       }
                        pf_ncache++;
-                       
+
                        cur->fr_off = off;
                        cur->fr_end = fr_max;
                        LIST_INSERT_AFTER(frp, cur, fr_next);
                }
        }
-       
+
        if (fra != NULL) {
-               int     aftercut;
-               int     merge = 0;
-               
+               int     aftercut;
+               int     merge = 0;
+
                aftercut = fr_max - fra->fr_off;
                if (aftercut == 0) {
                        /* Adjacent fragments */
@@ -1396,16 +1638,17 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                            fra->fr_off, fra->fr_end));
                        fra->fr_off = off;
                        fr_max -= aftercut;
-                       
+
                        merge = 1;
-                       
+
                        if (!drop) {
                                m_adj(m, -aftercut);
                                if (m->m_flags & M_PKTHDR) {
                                        int pktlen = 0;
                                        struct mbuf *t;
-                                       for (t = m; t; t = t->m_next)
+                                       for (t = m; t; t = t->m_next) {
                                                pktlen += t->m_len;
+                                       }
                                        m->m_pkthdr.len = pktlen;
                                }
                                h = mtod(m, struct ip6_hdr *);
@@ -1421,17 +1664,18 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                        DPFPRINTF(("frag6cache[%u]: gap %d %d-%d (%d-%d)\n",
                            ntohl(fh->ip6f_ident), -aftercut, off, fr_max,
                            fra->fr_off, fra->fr_end));
-                       
+
                        cur = pool_get(&pf_cent_pl, PR_NOWAIT);
-                       if (cur == NULL)
+                       if (cur == NULL) {
                                goto no_mem;
+                       }
                        pf_ncache++;
-                       
+
                        cur->fr_off = off;
                        cur->fr_end = fr_max;
                        LIST_INSERT_BEFORE(fra, cur, fr_next);
                }
-               
+
                /* Need to glue together two separate fragment descriptors */
                if (merge) {
                        if (cur && fra->fr_off <= cur->fr_end) {
@@ -1462,7 +1706,7 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                        }
                }
        }
-       
+
        if (hosed) {
                /*
                 * We must keep tracking the overall fragment even when
@@ -1471,16 +1715,18 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                 */
                goto drop_fragment;
        }
-       
- pass:
+
+pass:
        /* Update maximum data size */
-       if ((*frag)->fr_max < fr_max)
+       if ((*frag)->fr_max < fr_max) {
                (*frag)->fr_max = fr_max;
-       
+       }
+
        /* This is the last segment */
-       if (!mff)
+       if (!mff) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
-       
+       }
+
        /* Check if we are completely reassembled */
        if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
            LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
@@ -1491,99 +1737,107 @@ pf_frag6cache(struct mbuf **m0, struct ip6_hdr *h, struct ip6_frag *fh,
                pf_free_fragment(*frag);
                *frag = NULL;
        }
-       
-       return (m);
-       
- no_mem:
+
+       return m;
+
+no_mem:
        *nomem = 1;
-       
+
        /* Still need to pay attention to !IP_MF */
-       if (!mff && *frag != NULL)
+       if (!mff && *frag != NULL) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
-       
+       }
+
        m_freem(m);
-       return (NULL);
-       
- drop_fragment:
-       
+       return NULL;
+
+drop_fragment:
+
        /* Still need to pay attention to !IP_MF */
-       if (!mff && *frag != NULL)
+       if (!mff && *frag != NULL) {
                (*frag)->fr_flags |= PFFRAG_SEENLAST;
-       
+       }
+
        if (drop) {
                /* This fragment has been deemed bad.  Don't reass */
-               if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
+               if (((*frag)->fr_flags & PFFRAG_DROP) == 0) {
                        DPFPRINTF(("frag6cache[%u]: dropping overall fragment\n",
                            ntohl(fh->ip6f_ident)));
+               }
                (*frag)->fr_flags |= PFFRAG_DROP;
        }
-       
+
        m_freem(m);
-       return (NULL);
+       return NULL;
 }
 
 int
-pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
+pf_normalize_ip(pbuf_t *pbuf, int dir, struct pfi_kif *kif, u_short *reason,
     struct pf_pdesc *pd)
 {
-       struct mbuf             *m = *m0;
-       struct pf_rule          *r;
-       struct pf_frent         *frent;
-       struct pf_fragment      *frag = NULL;
-       struct ip               *h = mtod(m, struct ip *);
-       int                      mff = (ntohs(h->ip_off) & IP_MF);
-       int                      hlen = h->ip_hl << 2;
-       u_int16_t                fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
-       u_int16_t                fr_max;
-       int                      ip_len;
-       int                      ip_off;
-       int                      asd = 0;
-       struct pf_ruleset       *ruleset = NULL;
+       struct mbuf             *m;
+       struct pf_rule          *r;
+       struct pf_frent         *frent;
+       struct pf_fragment      *frag = NULL;
+       struct ip               *h = pbuf->pb_data;
+       int                      mff = (ntohs(h->ip_off) & IP_MF);
+       int                      hlen = h->ip_hl << 2;
+       u_int16_t                fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
+       u_int16_t                fr_max;
+       int                      ip_len;
+       int                      ip_off;
+       int                      asd = 0;
+       struct pf_ruleset       *ruleset = NULL;
+       struct ifnet            *ifp = pbuf->pb_ifp;
 
        r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
        while (r != NULL) {
                r->evaluations++;
-               if (pfi_kif_match(r->kif, kif) == r->ifnot)
+               if (pfi_kif_match(r->kif, kif) == r->ifnot) {
                        r = r->skip[PF_SKIP_IFP].ptr;
-               else if (r->direction && r->direction != dir)
+               } else if (r->direction && r->direction != dir) {
                        r = r->skip[PF_SKIP_DIR].ptr;
-               else if (r->af && r->af != AF_INET)
+               } else if (r->af && r->af != AF_INET) {
                        r = r->skip[PF_SKIP_AF].ptr;
-               else if (r->proto && r->proto != h->ip_p)
+               } else if (r->proto && r->proto != h->ip_p) {
                        r = r->skip[PF_SKIP_PROTO].ptr;
-               else if (PF_MISMATCHAW(&r->src.addr,
+               else if (PF_MISMATCHAW(&r->src.addr,
                    (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
-                   r->src.neg, kif))
+                   r->src.neg, kif)) {
                        r = r->skip[PF_SKIP_SRC_ADDR].ptr;
-               else if (PF_MISMATCHAW(&r->dst.addr,
+               else if (PF_MISMATCHAW(&r->dst.addr,
                    (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
-                   r->dst.neg, NULL))
+                   r->dst.neg, NULL)) {
                        r = r->skip[PF_SKIP_DST_ADDR].ptr;
-               else {
-                       if (r->anchor == NULL)
+               else {
+                       if (r->anchor == NULL) {
                                break;
-                       else
+                       } else {
                                pf_step_into_anchor(&asd, &ruleset,
                                    PF_RULESET_SCRUB, &r, NULL, NULL);
+                       }
                }
                if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
-                   PF_RULESET_SCRUB, &r, NULL, NULL))
+                   PF_RULESET_SCRUB, &r, NULL, NULL)) {
                        break;
+               }
        }
 
-       if (r == NULL || r->action == PF_NOSCRUB)
-               return (PF_PASS);
-       else {
+       if (r == NULL || r->action == PF_NOSCRUB) {
+               return PF_PASS;
+       else {
                r->packets[dir == PF_OUT]++;
                r->bytes[dir == PF_OUT] += pd->tot_len;
        }
 
        /* Check for illegal packets */
-       if (hlen < (int)sizeof (struct ip))
+       if (hlen < (int)sizeof(struct ip)) {
                goto drop;
+       }
 
-       if (hlen > ntohs(h->ip_len))
+       if (hlen > ntohs(h->ip_len)) {
                goto drop;
+       }
 
        /* Clear IP_DF if the rule uses the no-df option */
        if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
@@ -1594,8 +1848,9 @@ pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
        }
 
        /* We will need other tests here */
-       if (!fragoff && !mff)
+       if (!fragoff && !mff) {
                goto no_fragment;
+       }
 
        /*
         * We're dealing with a fragment now. Don't allow fragments
@@ -1623,20 +1878,32 @@ pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
        }
        fr_max = fragoff + ip_len;
 
-       if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
+       if ((r->rule_flag & (PFRULE_FRAGCROP | PFRULE_FRAGDROP)) == 0) {
                /* Fully buffer all of the fragments */
 
                frag = pf_find_fragment_by_ipv4_header(h, &pf_frag_tree);
                /* Check if we saw the last fragment already */
                if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
-                   fr_max > frag->fr_max)
+                   fr_max > frag->fr_max) {
                        goto bad;
+               }
+
+               if ((m = pbuf_to_mbuf(pbuf, TRUE)) == NULL) {
+                       REASON_SET(reason, PFRES_MEMORY);
+                       return PF_DROP;
+               }
+
+               VERIFY(!pbuf_is_valid(pbuf));
+
+               /* Restore iph pointer after pbuf_to_mbuf() */
+               h = mtod(m, struct ip *);
 
                /* Get an entry for the fragment queue */
                frent = pool_get(&pf_frent_pl, PR_NOWAIT);
                if (frent == NULL) {
                        REASON_SET(reason, PFRES_MEMORY);
-                       return (PF_DROP);
+                       m_freem(m);
+                       return PF_DROP;
                }
                pf_nfrents++;
                frent->fr_ip = h;
@@ -1645,30 +1912,39 @@ pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
                /* Might return a completely reassembled mbuf, or NULL */
                DPFPRINTF(("reass IPv4 frag %d @ %d-%d\n", ntohs(h->ip_id),
                    fragoff, fr_max));
-               *m0 = m = pf_reassemble(m0, &frag, frent, mff);
+               m = pf_reassemble(m, &frag, frent, mff);
 
-               if (m == NULL)
-                       return (PF_DROP);
+               if (m == NULL) {
+                       return PF_DROP;
+               }
+
+               VERIFY(m->m_flags & M_PKTHDR);
+               pbuf_init_mbuf(pbuf, m, ifp);
 
                /* use mtag from concatenated mbuf chain */
-               pd->pf_mtag = pf_find_mtag(m);
-#ifdef DIAGNOSTIC
+               pd->pf_mtag = pf_find_mtag_pbuf(pbuf);
+#if 0
+// SCW: This check is superfluous
+#if DIAGNOSTIC
                if (pd->pf_mtag == NULL) {
                        printf("%s: pf_find_mtag returned NULL(1)\n", __func__);
                        if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
                                m_freem(m);
-                               *m0 = NULL;
+                               m = NULL;
                                goto no_mem;
                        }
                }
 #endif
-               if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
-                       goto drop;
+#endif
 
                h = mtod(m, struct ip *);
+
+               if (frag != NULL && (frag->fr_flags & PFFRAG_DROP)) {
+                       goto drop;
+               }
        } else {
                /* non-buffering fragment cache (drops or masks overlaps) */
-               int     nomem = 0;
+               int     nomem = 0;
 
                if (dir == PF_OUT && (pd->pf_mtag->pftag_flags & PF_TAG_FRAGCACHE)) {
                        /*
@@ -1684,36 +1960,58 @@ pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
                /* Check if we saw the last fragment already */
                if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
                    fr_max > frag->fr_max) {
-                       if (r->rule_flag & PFRULE_FRAGDROP)
+                       if (r->rule_flag & PFRULE_FRAGDROP) {
                                frag->fr_flags |= PFFRAG_DROP;
+                       }
                        goto bad;
                }
 
-               *m0 = m = pf_fragcache(m0, h, &frag, mff,
+               if ((m = pbuf_to_mbuf(pbuf, TRUE)) == NULL) {
+                       REASON_SET(reason, PFRES_MEMORY);
+                       goto bad;
+               }
+
+               VERIFY(!pbuf_is_valid(pbuf));
+
+               /* Restore iph pointer after pbuf_to_mbuf() */
+               h = mtod(m, struct ip *);
+
+               m = pf_fragcache(&m, h, &frag, mff,
                    (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
                if (m == NULL) {
-                       if (nomem)
+                       // Note: pf_fragcache() has already m_freem'd the mbuf
+                       if (nomem) {
                                goto no_mem;
+                       }
                        goto drop;
                }
 
+               VERIFY(m->m_flags & M_PKTHDR);
+               pbuf_init_mbuf(pbuf, m, ifp);
+
                /* use mtag from copied and trimmed mbuf chain */
-               pd->pf_mtag = pf_find_mtag(m);
-#ifdef DIAGNOSTIC
+               pd->pf_mtag = pf_find_mtag_pbuf(pbuf);
+#if 0
+// SCW: This check is superfluous
+#if DIAGNOSTIC
                if (pd->pf_mtag == NULL) {
                        printf("%s: pf_find_mtag returned NULL(2)\n", __func__);
                        if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
                                m_freem(m);
-                               *m0 = NULL;
+                               m = NULL;
                                goto no_mem;
                        }
                }
 #endif
-               if (dir == PF_IN)
+#endif
+               if (dir == PF_IN) {
                        pd->pf_mtag->pftag_flags |= PF_TAG_FRAGCACHE;
+               }
 
-               if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
+               if (frag != NULL && (frag->fr_flags & PFFRAG_DROP)) {
                        goto drop;
+               }
+
                goto fragment_pass;
        }
 
@@ -1733,18 +2031,21 @@ no_fragment:
                h->ip_ttl = r->min_ttl;
                h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
        }
-#if RANDOM_IP_ID
        if (r->rule_flag & PFRULE_RANDOMID) {
-               u_int16_t ip_id = h->ip_id;
+               u_int16_t oip_id = h->ip_id;
 
-               h->ip_id = ip_randomid();
-               h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
+               if (rfc6864 && IP_OFF_IS_ATOMIC(ntohs(h->ip_off))) {
+                       h->ip_id = 0;
+               } else {
+                       h->ip_id = ip_randomid();
+               }
+               h->ip_sum = pf_cksum_fixup(h->ip_sum, oip_id, h->ip_id, 0);
        }
-#endif /* RANDOM_IP_ID */
-       if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
+       if ((r->rule_flag & (PFRULE_FRAGCROP | PFRULE_FRAGDROP)) == 0) {
                pd->flags |= PFDESC_IP_REAS;
+       }
 
-       return (PF_PASS);
+       return PF_PASS;
 
 fragment_pass:
        /* Enforce a minimum ttl, may cause endless packet loops */
@@ -1754,112 +2055,124 @@ fragment_pass:
                h->ip_ttl = r->min_ttl;
                h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
        }
-       if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
+       if ((r->rule_flag & (PFRULE_FRAGCROP | PFRULE_FRAGDROP)) == 0) {
                pd->flags |= PFDESC_IP_REAS;
-       return (PF_PASS);
+       }
+       return PF_PASS;
 
 no_mem:
        REASON_SET(reason, PFRES_MEMORY);
-       if (r != NULL && r->log)
-               PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r,
+       if (r != NULL && r->log && pbuf_is_valid(pbuf)) {
+               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, *reason, r,
                    NULL, NULL, pd);
-       return (PF_DROP);
+       }
+       return PF_DROP;
 
 drop:
        REASON_SET(reason, PFRES_NORM);
-       if (r != NULL && r->log)
-               PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r,
+       if (r != NULL && r->log && pbuf_is_valid(pbuf)) {
+               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, *reason, r,
                    NULL, NULL, pd);
-       return (PF_DROP);
+       }
+       return PF_DROP;
 
 bad:
        DPFPRINTF(("dropping bad IPv4 fragment\n"));
 
        /* Free associated fragments */
-       if (frag != NULL)
+       if (frag != NULL) {
                pf_free_fragment(frag);
+       }
 
        REASON_SET(reason, PFRES_FRAG);
-       if (r != NULL && r->log)
-               PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
+       if (r != NULL && r->log && pbuf_is_valid(pbuf)) {
+               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, *reason, r, NULL, NULL, pd);
+       }
 
-       return (PF_DROP);
+       return PF_DROP;
 }
 
 #if INET6
 int
-pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
+pf_normalize_ip6(pbuf_t *pbuf, int dir, struct pfi_kif *kif,
     u_short *reason, struct pf_pdesc *pd)
 {
-       struct mbuf             *m = *m0;
-       struct pf_rule          *r;
-       struct ip6_hdr          *h = mtod(m, struct ip6_hdr *);
-       int                      off;
-       struct ip6_ext           ext;
+       struct mbuf             *m;
+       struct pf_rule          *r;
+       struct ip6_hdr          *h = pbuf->pb_data;
+       int                      off;
+       struct ip6_ext           ext;
 /* adi XXX */
 #if 0
-       struct ip6_opt           opt;
-       struct ip6_opt_jumbo     jumbo;
-       int                      optend;
-       int                      ooff;
+       struct ip6_opt           opt;
+       struct ip6_opt_jumbo     jumbo;
+       int                      optend;
+       int                      ooff;
 #endif
-       struct ip6_frag          frag;
-       u_int32_t                jumbolen = 0, plen;
-       u_int16_t                fragoff = 0;
-       u_int8_t                 proto;
-       int                      terminal;
-       struct pf_frent         *frent;
-       struct pf_fragment      *pff = NULL;
-       int                      mff = 0, rh_cnt = 0;
-       u_int16_t                fr_max;
-       int                      asd = 0;
-       struct pf_ruleset       *ruleset = NULL;
+       struct ip6_frag          frag;
+       u_int32_t                jumbolen = 0, plen;
+       u_int16_t                fragoff = 0;
+       u_int8_t                 proto;
+       int                      terminal;
+       struct pf_frent         *frent;
+       struct pf_fragment      *pff = NULL;
+       int                      mff = 0, rh_cnt = 0;
+       u_int16_t                fr_max;
+       int                      asd = 0;
+       struct pf_ruleset       *ruleset = NULL;
+       struct ifnet            *ifp = pbuf->pb_ifp;
 
        r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
        while (r != NULL) {
                r->evaluations++;
-               if (pfi_kif_match(r->kif, kif) == r->ifnot)
+               if (pfi_kif_match(r->kif, kif) == r->ifnot) {
                        r = r->skip[PF_SKIP_IFP].ptr;
-               else if (r->direction && r->direction != dir)
+               } else if (r->direction && r->direction != dir) {
                        r = r->skip[PF_SKIP_DIR].ptr;
-               else if (r->af && r->af != AF_INET6)
+               } else if (r->af && r->af != AF_INET6) {
                        r = r->skip[PF_SKIP_AF].ptr;
+               }
 #if 0 /* header chain! */
-               else if (r->proto && r->proto != h->ip6_nxt)
+               else if (r->proto && r->proto != h->ip6_nxt) {
                        r = r->skip[PF_SKIP_PROTO].ptr;
+               }
 #endif
                else if (PF_MISMATCHAW(&r->src.addr,
-                   (struct pf_addr *)&h->ip6_src, AF_INET6,
-                   r->src.neg, kif))
+                   (struct pf_addr *)(uintptr_t)&h->ip6_src, AF_INET6,
+                   r->src.neg, kif)) {
                        r = r->skip[PF_SKIP_SRC_ADDR].ptr;
-               else if (PF_MISMATCHAW(&r->dst.addr,
-                   (struct pf_addr *)&h->ip6_dst, AF_INET6,
-                   r->dst.neg, NULL))
+               else if (PF_MISMATCHAW(&r->dst.addr,
+                   (struct pf_addr *)(uintptr_t)&h->ip6_dst, AF_INET6,
+                   r->dst.neg, NULL)) {
                        r = r->skip[PF_SKIP_DST_ADDR].ptr;
-               else {
-                       if (r->anchor == NULL)
+               else {
+                       if (r->anchor == NULL) {
                                break;
-                       else
+                       } else {
                                pf_step_into_anchor(&asd, &ruleset,
                                    PF_RULESET_SCRUB, &r, NULL, NULL);
+                       }
                }
                if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
-                   PF_RULESET_SCRUB, &r, NULL, NULL))
+                   PF_RULESET_SCRUB, &r, NULL, NULL)) {
                        break;
+               }
        }
 
-       if (r == NULL || r->action == PF_NOSCRUB)
-               return (PF_PASS);
-       else {
+       if (r == NULL || r->action == PF_NOSCRUB) {
+               return PF_PASS;
+       else {
                r->packets[dir == PF_OUT]++;
                r->bytes[dir == PF_OUT] += pd->tot_len;
        }
 
        /* Check for illegal packets */
-       if ((int)(sizeof (struct ip6_hdr) + IPV6_MAXPACKET) < m->m_pkthdr.len)
+       if ((uint32_t)(sizeof(struct ip6_hdr) + IPV6_MAXPACKET) <
+           pbuf->pb_packet_len) {
                goto drop;
+       }
 
-       off = sizeof (struct ip6_hdr);
+       off = sizeof(struct ip6_hdr);
        proto = h->ip6_nxt;
        terminal = 0;
        do {
@@ -1867,13 +2180,13 @@ pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
                switch (proto) {
                case IPPROTO_FRAGMENT:
                        goto fragment;
-                       break;
                case IPPROTO_AH:
                case IPPROTO_ROUTING:
                case IPPROTO_DSTOPTS:
-                       if (!pf_pull_hdr(m, off, &ext, sizeof (ext), NULL,
-                           NULL, AF_INET6))
+                       if (!pf_pull_hdr(pbuf, off, &ext, sizeof(ext), NULL,
+                           NULL, AF_INET6)) {
                                goto shortpkt;
+                       }
                        /*
                         * <jhw@apple.com>
                         * Multiple routing headers not allowed.
@@ -1882,61 +2195,70 @@ pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
                        if (proto == IPPROTO_ROUTING) {
                                const struct ip6_rthdr *rh =
                                    (const struct ip6_rthdr *)&ext;
-                               if (rh_cnt++)
+                               if (rh_cnt++) {
                                        goto drop;
-                               if (rh->ip6r_type == IPV6_RTHDR_TYPE_0)
+                               }
+                               if (rh->ip6r_type == IPV6_RTHDR_TYPE_0) {
                                        goto drop;
-                       }
-                       else
-                       if (proto == IPPROTO_AH)
+                               }
+                       } else if (proto == IPPROTO_AH) {
                                off += (ext.ip6e_len + 2) * 4;
-                       else
+                       } else {
                                off += (ext.ip6e_len + 1) * 8;
+                       }
                        proto = ext.ip6e_nxt;
                        break;
                case IPPROTO_HOPOPTS:
 /* adi XXX */
 #if 0
-                       if (!pf_pull_hdr(m, off, &ext, sizeof (ext), NULL,
-                           NULL, AF_INET6))
+                       if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
+                           NULL, AF_INET6)) {
                                goto shortpkt;
+                       }
                        optend = off + (ext.ip6e_len + 1) * 8;
-                       ooff = off + sizeof (ext);
+                       ooff = off + sizeof(ext);
                        do {
                                if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
-                                   sizeof (opt.ip6o_type), NULL, NULL,
-                                   AF_INET6))
+                                   sizeof(opt.ip6o_type), NULL, NULL,
+                                   AF_INET6)) {
                                        goto shortpkt;
+                               }
                                if (opt.ip6o_type == IP6OPT_PAD1) {
                                        ooff++;
                                        continue;
                                }
-                               if (!pf_pull_hdr(m, ooff, &opt, sizeof (opt),
-                                   NULL, NULL, AF_INET6))
+                               if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
+                                   NULL, NULL, AF_INET6)) {
                                        goto shortpkt;
-                               if (ooff + sizeof (opt) + opt.ip6o_len > optend)
+                               }
+                               if (ooff + sizeof(opt) + opt.ip6o_len > optend) {
                                        goto drop;
+                               }
                                switch (opt.ip6o_type) {
                                case IP6OPT_JUMBO:
-                                       if (h->ip6_plen != 0)
+                                       if (h->ip6_plen != 0) {
                                                goto drop;
+                                       }
                                        if (!pf_pull_hdr(m, ooff, &jumbo,
-                                           sizeof (jumbo), NULL, NULL,
-                                           AF_INET6))
+                                           sizeof(jumbo), NULL, NULL,
+                                           AF_INET6)) {
                                                goto shortpkt;
+                                       }
                                        memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
-                                           sizeof (jumbolen));
+                                           sizeof(jumbolen));
                                        jumbolen = ntohl(jumbolen);
-                                       if (jumbolen <= IPV6_MAXPACKET)
+                                       if (jumbolen <= IPV6_MAXPACKET) {
                                                goto drop;
-                                       if (sizeof (struct ip6_hdr) +
-                                           jumbolen != m->m_pkthdr.len)
+                                       }
+                                       if (sizeof(struct ip6_hdr) +
+                                           jumbolen != m->m_pkthdr.len) {
                                                goto drop;
+                                       }
                                        break;
                                default:
                                        break;
                                }
-                               ooff += sizeof (opt) + opt.ip6o_len;
+                               ooff += sizeof(opt) + opt.ip6o_len;
                        } while (ooff < optend);
 
                        off = optend;
@@ -1950,149 +2272,189 @@ pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
        } while (!terminal);
 
        /* jumbo payload option must be present, or plen > 0 */
-       if (ntohs(h->ip6_plen) == 0)
+       if (ntohs(h->ip6_plen) == 0) {
                plen = jumbolen;
-       else
+       } else {
                plen = ntohs(h->ip6_plen);
-       if (plen == 0)
+       }
+       if (plen == 0) {
                goto drop;
-       if ((int)(sizeof (struct ip6_hdr) + plen) > m->m_pkthdr.len)
+       }
+       if ((uint32_t)(sizeof(struct ip6_hdr) + plen) > pbuf->pb_packet_len) {
                goto shortpkt;
+       }
 
        /* Enforce a minimum ttl, may cause endless packet loops */
-       if (r->min_ttl && h->ip6_hlim < r->min_ttl)
+       if (r->min_ttl && h->ip6_hlim < r->min_ttl) {
                h->ip6_hlim = r->min_ttl;
+       }
 
-       return (PF_PASS);
+       return PF_PASS;
 
 fragment:
-       if (ntohs(h->ip6_plen) == 0 || jumbolen)
+       if (ntohs(h->ip6_plen) == 0 || jumbolen) {
                goto drop;
+       }
        plen = ntohs(h->ip6_plen);
 
-       if (!pf_pull_hdr(m, off, &frag, sizeof (frag), NULL, NULL, AF_INET6))
+       if (!pf_pull_hdr(pbuf, off, &frag, sizeof(frag), NULL, NULL, AF_INET6)) {
                goto shortpkt;
+       }
        fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
        pd->proto = frag.ip6f_nxt;
        mff = ntohs(frag.ip6f_offlg & IP6F_MORE_FRAG);
        off += sizeof frag;
-       if (fragoff + (plen - off) > IPV6_MAXPACKET)
-              goto badfrag;
-       
+       if (fragoff + (plen - off) > IPV6_MAXPACKET) {
+               goto badfrag;
+       }
+
        fr_max = fragoff + plen - (off - sizeof(struct ip6_hdr));
-       DPFPRINTF(("%p IPv6 frag plen %u mff %d off %u fragoff %u fr_max %u\n", m, 
-               plen, mff, off, fragoff, fr_max));
-       
-       if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
+// XXX SCW: mbuf-specific
+//     DPFPRINTF(("0x%llx IPv6 frag plen %u mff %d off %u fragoff %u "
+//         "fr_max %u\n", (uint64_t)VM_KERNEL_ADDRPERM(m), plen, mff, off,
+//         fragoff, fr_max));
+
+       if ((r->rule_flag & (PFRULE_FRAGCROP | PFRULE_FRAGDROP)) == 0) {
                /* Fully buffer all of the fragments */
                pd->flags |= PFDESC_IP_REAS;
-               
+
                pff = pf_find_fragment_by_ipv6_header(h, &frag,
-                  &pf_frag_tree);
-               
+                   &pf_frag_tree);
+
                /* Check if we saw the last fragment already */
                if (pff != NULL && (pff->fr_flags & PFFRAG_SEENLAST) &&
-                   fr_max > pff->fr_max)
+                   fr_max > pff->fr_max) {
                        goto badfrag;
-               
+               }
+
+               if ((m = pbuf_to_mbuf(pbuf, TRUE)) == NULL) {
+                       REASON_SET(reason, PFRES_MEMORY);
+                       return PF_DROP;
+               }
+
+               /* Restore iph pointer after pbuf_to_mbuf() */
+               h = mtod(m, struct ip6_hdr *);
+
                /* Get an entry for the fragment queue */
                frent = pool_get(&pf_frent_pl, PR_NOWAIT);
                if (frent == NULL) {
                        REASON_SET(reason, PFRES_MEMORY);
-                       return (PF_DROP);
+                       return PF_DROP;
                }
+
                pf_nfrents++;
                frent->fr_ip6 = h;
                frent->fr_m = m;
                frent->fr_ip6f_opt = frag;
                frent->fr_ip6f_hlen = off;
-               
+
                /* Might return a completely reassembled mbuf, or NULL */
                DPFPRINTF(("reass IPv6 frag %d @ %d-%d\n",
-                    ntohl(frag.ip6f_ident), fragoff, fr_max));
-               *m0 = m = pf_reassemble6(m0, &pff, frent, mff);
-               
-               if (m == NULL)
-                       return (PF_DROP);
-               
-               if (pff != NULL && (pff->fr_flags & PFFRAG_DROP))
+                   ntohl(frag.ip6f_ident), fragoff, fr_max));
+               m = pf_reassemble6(&m, &pff, frent, mff);
+
+               if (m == NULL) {
+                       return PF_DROP;
+               }
+
+               pbuf_init_mbuf(pbuf, m, ifp);
+               h = pbuf->pb_data;
+
+               if (pff != NULL && (pff->fr_flags & PFFRAG_DROP)) {
                        goto drop;
-               
-               h = mtod(m, struct ip6_hdr *);
-       }
-       else if (dir == PF_IN || !(pd->pf_mtag->pftag_flags & PF_TAG_FRAGCACHE)) {
+               }
+       } else if (dir == PF_IN || !(pd->pf_mtag->pftag_flags & PF_TAG_FRAGCACHE)) {
                /* non-buffering fragment cache (overlaps: see RFC 5722) */
                int nomem = 0;
-               
+
                pff = pf_find_fragment_by_ipv6_header(h, &frag,
                    &pf_cache_tree);
-               
+
                /* Check if we saw the last fragment already */
                if (pff != NULL && (pff->fr_flags & PFFRAG_SEENLAST) &&
                    fr_max > pff->fr_max) {
-                      if (r->rule_flag & PFRULE_FRAGDROP)
+                       if (r->rule_flag & PFRULE_FRAGDROP) {
                                pff->fr_flags |= PFFRAG_DROP;
-                      goto badfrag;
+                       }
+                       goto badfrag;
                }
-               
-               *m0 = m = pf_frag6cache(m0, h, &frag, &pff, off, mff,
-                    (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
+
+               if ((m = pbuf_to_mbuf(pbuf, TRUE)) == NULL) {
+                       goto no_mem;
+               }
+
+               /* Restore iph pointer after pbuf_to_mbuf() */
+               h = mtod(m, struct ip6_hdr *);
+
+               m = pf_frag6cache(&m, h, &frag, &pff, off, mff,
+                   (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
                if (m == NULL) {
-                       if (nomem)
+                       // Note: pf_frag6cache() has already m_freem'd the mbuf
+                       if (nomem) {
                                goto no_mem;
+                       }
                        goto drop;
                }
-               
-               if (dir == PF_IN)
+
+               pbuf_init_mbuf(pbuf, m, ifp);
+               pd->pf_mtag = pf_find_mtag_pbuf(pbuf);
+               h = pbuf->pb_data;
+
+               if (dir == PF_IN) {
                        pd->pf_mtag->pftag_flags |= PF_TAG_FRAGCACHE;
-               
-               if (pff != NULL && (pff->fr_flags & PFFRAG_DROP))
+               }
+
+               if (pff != NULL && (pff->fr_flags & PFFRAG_DROP)) {
                        goto drop;
+               }
        }
-       
+
        /* Enforce a minimum ttl, may cause endless packet loops */
-       if (r->min_ttl && h->ip6_hlim < r->min_ttl)
+       if (r->min_ttl && h->ip6_hlim < r->min_ttl) {
                h->ip6_hlim = r->min_ttl;
-       return (PF_PASS);
+       }
+       return PF_PASS;
 
-  no_mem:
+no_mem:
        REASON_SET(reason, PFRES_MEMORY);
        goto dropout;
-       
-  shortpkt:
+
+shortpkt:
        REASON_SET(reason, PFRES_SHORT);
        goto dropout;
-       
-  drop:
+
+drop:
        REASON_SET(reason, PFRES_NORM);
        goto dropout;
-       
-  badfrag:
+
+badfrag:
        DPFPRINTF(("dropping bad IPv6 fragment\n"));
        REASON_SET(reason, PFRES_FRAG);
        goto dropout;
-       
-  dropout:
-       if (pff != NULL)
+
+dropout:
+       if (pff != NULL) {
                pf_free_fragment(pff);
-       if (r != NULL && r->log)
-               PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
-       return (PF_DROP);
+       }
+       if (r != NULL && r->log && pbuf_is_valid(pbuf)) {
+               PFLOG_PACKET(kif, h, pbuf, AF_INET6, dir, *reason, r, NULL, NULL, pd);
+       }
+       return PF_DROP;
 }
 #endif /* INET6 */
 
 int
-pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
+pf_normalize_tcp(int dir, struct pfi_kif *kif, pbuf_t *pbuf, int ipoff,
     int off, void *h, struct pf_pdesc *pd)
 {
 #pragma unused(ipoff, h)
-       struct pf_rule  *r, *rm = NULL;
-       struct tcphdr   *th = pd->hdr.tcp;
-       int              rewrite = 0;
-       int              asd = 0;
-       u_short          reason;
-       u_int8_t         flags;
-       sa_family_t      af = pd->af;
+       struct pf_rule  *r, *rm = NULL;
+       struct tcphdr   *th = pd->hdr.tcp;
+       int              rewrite = 0;
+       int              asd = 0;
+       u_short          reason;
+       u_int8_t         flags;
+       sa_family_t      af = pd->af;
        struct pf_ruleset *ruleset = NULL;
        union pf_state_xport sxport, dxport;
 
@@ -2102,33 +2464,33 @@ pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
        r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
        while (r != NULL) {
                r->evaluations++;
-               if (pfi_kif_match(r->kif, kif) == r->ifnot)
+               if (pfi_kif_match(r->kif, kif) == r->ifnot) {
                        r = r->skip[PF_SKIP_IFP].ptr;
-               else if (r->direction && r->direction != dir)
+               } else if (r->direction && r->direction != dir) {
                        r = r->skip[PF_SKIP_DIR].ptr;
-               else if (r->af && r->af != af)
+               } else if (r->af && r->af != af) {
                        r = r->skip[PF_SKIP_AF].ptr;
-               else if (r->proto && r->proto != pd->proto)
+               } else if (r->proto && r->proto != pd->proto) {
                        r = r->skip[PF_SKIP_PROTO].ptr;
-               else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
-                   r->src.neg, kif))
+               else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
+                   r->src.neg, kif)) {
                        r = r->skip[PF_SKIP_SRC_ADDR].ptr;
-               else if (r->src.xport.range.op &&
+               else if (r->src.xport.range.op &&
                    !pf_match_xport(r->src.xport.range.op, r->proto_variant,
-                   &r->src.xport, &sxport))
+                   &r->src.xport, &sxport)) {
                        r = r->skip[PF_SKIP_SRC_PORT].ptr;
-               else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
-                   r->dst.neg, NULL))
+               else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
+                   r->dst.neg, NULL)) {
                        r = r->skip[PF_SKIP_DST_ADDR].ptr;
-               else if (r->dst.xport.range.op &&
+               else if (r->dst.xport.range.op &&
                    !pf_match_xport(r->dst.xport.range.op, r->proto_variant,
-                   &r->dst.xport, &dxport))
+                   &r->dst.xport, &dxport)) {
                        r = r->skip[PF_SKIP_DST_PORT].ptr;
-               else if (r->os_fingerprint != PF_OSFP_ANY &&
-                   !pf_osfp_match(pf_osfp_fingerprint(pd, m, off, th),
-                   r->os_fingerprint))
+               else if (r->os_fingerprint != PF_OSFP_ANY &&
+                   !pf_osfp_match(pf_osfp_fingerprint(pd, pbuf, off, th),
+                   r->os_fingerprint)) {
                        r = TAILQ_NEXT(r, entries);
-               else {
+               else {
                        if (r->anchor == NULL) {
                                rm = r;
                                break;
@@ -2138,47 +2500,54 @@ pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
                        }
                }
                if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
-                   PF_RULESET_SCRUB, &r, NULL, NULL))
+                   PF_RULESET_SCRUB, &r, NULL, NULL)) {
                        break;
+               }
        }
 
-       if (rm == NULL || rm->action == PF_NOSCRUB)
-               return (PF_PASS);
-       else {
+       if (rm == NULL || rm->action == PF_NOSCRUB) {
+               return PF_PASS;
+       else {
                r->packets[dir == PF_OUT]++;
                r->bytes[dir == PF_OUT] += pd->tot_len;
        }
 
-       if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
+       if (rm->rule_flag & PFRULE_REASSEMBLE_TCP) {
                pd->flags |= PFDESC_TCP_NORM;
+       }
 
        flags = th->th_flags;
        if (flags & TH_SYN) {
                /* Illegal packet */
-               if (flags & TH_RST)
+               if (flags & TH_RST) {
                        goto tcp_drop;
+               }
 
-               if (flags & TH_FIN)
+               if (flags & TH_FIN) {
                        flags &= ~TH_FIN;
+               }
        } else {
                /* Illegal packet */
-               if (!(flags & (TH_ACK|TH_RST)))
+               if (!(flags & (TH_ACK | TH_RST))) {
                        goto tcp_drop;
+               }
        }
 
        if (!(flags & TH_ACK)) {
                /* These flags are only valid if ACK is set */
-               if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
+               if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG)) {
                        goto tcp_drop;
+               }
        }
 
        /* Check for illegal header length */
-       if (th->th_off < (sizeof (struct tcphdr) >> 2))
+       if (th->th_off < (sizeof(struct tcphdr) >> 2)) {
                goto tcp_drop;
+       }
 
        /* If flags changed, or reserved data set, then adjust */
        if (flags != th->th_flags || th->th_x2 != 0) {
-               u_int16_t       ov, nv;
+               u_int16_t       ov, nv;
 
                ov = *(u_int16_t *)(&th->th_ack + 1);
                th->th_flags = flags;
@@ -2199,38 +2568,40 @@ pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
        /* copy back packet headers if we sanitized */
        /* Process options */
        if (r->max_mss) {
-               int rv = pf_normalize_tcpopt(r, dir, kif, pd, m, th, off,
+               int rv = pf_normalize_tcpopt(r, dir, kif, pd, pbuf, th, off,
                    &rewrite);
-               if (rv == PF_DROP)
+               if (rv == PF_DROP) {
                        return rv;
-               m = pd->mp;
+               }
+               pbuf = pd->mp;
        }
 
        if (rewrite) {
-               struct mbuf *mw = pf_lazy_makewritable(pd, m,
-                   off + sizeof (*th));
-               if (!mw) {
+               if (pf_lazy_makewritable(pd, pbuf,
+                   off + sizeof(*th)) == NULL) {
                        REASON_SET(&reason, PFRES_MEMORY);
-                       if (r->log)
-                               PFLOG_PACKET(kif, h, m, AF_INET, dir, reason,
+                       if (r->log) {
+                               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, reason,
                                    r, 0, 0, pd);
+                       }
                        return PF_DROP;
                }
 
-               m_copyback(mw, off, sizeof (*th), th);
+               pbuf_copy_back(pbuf, off, sizeof(*th), th);
        }
 
-       return (PF_PASS);
+       return PF_PASS;
 
 tcp_drop:
        REASON_SET(&reason, PFRES_NORM);
-       if (rm != NULL && r->log)
-               PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL, pd);
-       return (PF_DROP);
+       if (rm != NULL && r->log) {
+               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, reason, r, NULL, NULL, pd);
+       }
+       return PF_DROP;
 }
 
 int
-pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
+pf_normalize_tcp_init(pbuf_t *pbuf, int off, struct pf_pdesc *pd,
     struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
 {
 #pragma unused(dst)
@@ -2241,21 +2612,22 @@ pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
        VERIFY(src->scrub == NULL);
 
        src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
-       if (src->scrub == NULL)
-               return (1);
-       bzero(src->scrub, sizeof (*src->scrub));
+       if (src->scrub == NULL) {
+               return 1;
+       }
+       bzero(src->scrub, sizeof(*src->scrub));
 
        switch (pd->af) {
 #if INET
        case AF_INET: {
-               struct ip *h = mtod(m, struct ip *);
+               struct ip *h = pbuf->pb_data;
                src->scrub->pfss_ttl = h->ip_ttl;
                break;
        }
 #endif /* INET */
 #if INET6
        case AF_INET6: {
-               struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
+               struct ip6_hdr *h = pbuf->pb_data;
                src->scrub->pfss_ttl = h->ip6_hlim;
                break;
        }
@@ -2267,19 +2639,20 @@ pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
         * All normalizations below are only begun if we see the start of
         * the connections.  They must all set an enabled bit in pfss_flags
         */
-       if ((th->th_flags & TH_SYN) == 0)
-               return (0);
+       if ((th->th_flags & TH_SYN) == 0) {
+               return 0;
+       }
 
 
-       if (th->th_off > (sizeof (struct tcphdr) >> 2) && src->scrub &&
-           pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
+       if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
+           pf_pull_hdr(pbuf, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
                /* Diddle with TCP options */
                int hlen;
-               opt = hdr + sizeof (struct tcphdr);
-               hlen = (th->th_off << 2) - sizeof (struct tcphdr);
+               opt = hdr + sizeof(struct tcphdr);
+               hlen = (th->th_off << 2) - sizeof(struct tcphdr);
                while (hlen >= TCPOLEN_TIMESTAMP) {
                        switch (*opt) {
-                       case TCPOPT_EOL:        /* FALLTHROUGH */
+                       case TCPOPT_EOL:        /* FALLTHROUGH */
                        case TCPOPT_NOP:
                                opt++;
                                hlen--;
@@ -2293,15 +2666,15 @@ pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
 
                                        /* note PFSS_PAWS not set yet */
                                        memcpy(&tsval, &opt[2],
-                                           sizeof (u_int32_t));
+                                           sizeof(u_int32_t));
                                        memcpy(&tsecr, &opt[6],
-                                           sizeof (u_int32_t));
+                                           sizeof(u_int32_t));
                                        src->scrub->pfss_tsval0 = ntohl(tsval);
                                        src->scrub->pfss_tsval = ntohl(tsval);
                                        src->scrub->pfss_tsecr = ntohl(tsecr);
                                        getmicrouptime(&src->scrub->pfss_last);
                                }
-                               /* FALLTHROUGH */
+                       /* FALLTHROUGH */
                        default:
                                hlen -= MAX(opt[1], 2);
                                opt += MAX(opt[1], 2);
@@ -2310,27 +2683,29 @@ pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
                }
        }
 
-       return (0);
+       return 0;
 }
 
 void
 pf_normalize_tcp_cleanup(struct pf_state *state)
 {
-       if (state->src.scrub)
+       if (state->src.scrub) {
                pool_put(&pf_state_scrub_pl, state->src.scrub);
-       if (state->dst.scrub)
+       }
+       if (state->dst.scrub) {
                pool_put(&pf_state_scrub_pl, state->dst.scrub);
+       }
 
        /* Someday... flush the TCP segment reassembly descriptors. */
 }
 
 int
-pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
+pf_normalize_tcp_stateful(pbuf_t *pbuf, int off, struct pf_pdesc *pd,
     u_short *reason, struct tcphdr *th, struct pf_state *state,
     struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
 {
        struct timeval uptime;
-       u_int32_t tsval, tsecr;
+       u_int32_t tsval = 0, tsecr = 0;
        u_int tsval_from_last;
        u_int8_t hdr[60];
        u_int8_t *opt;
@@ -2348,9 +2723,10 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
 #if INET
        case AF_INET: {
                if (src->scrub) {
-                       struct ip *h = mtod(m, struct ip *);
-                       if (h->ip_ttl > src->scrub->pfss_ttl)
+                       struct ip *h = pbuf->pb_data;
+                       if (h->ip_ttl > src->scrub->pfss_ttl) {
                                src->scrub->pfss_ttl = h->ip_ttl;
+                       }
                        h->ip_ttl = src->scrub->pfss_ttl;
                }
                break;
@@ -2359,9 +2735,10 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
 #if INET6
        case AF_INET6: {
                if (src->scrub) {
-                       struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
-                       if (h->ip6_hlim > src->scrub->pfss_ttl)
+                       struct ip6_hdr *h = pbuf->pb_data;
+                       if (h->ip6_hlim > src->scrub->pfss_ttl) {
                                src->scrub->pfss_ttl = h->ip6_hlim;
+                       }
                        h->ip6_hlim = src->scrub->pfss_ttl;
                }
                break;
@@ -2369,17 +2746,17 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
 #endif /* INET6 */
        }
 
-       if (th->th_off > (sizeof (struct tcphdr) >> 2) &&
+       if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
            ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
            (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
-           pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
+           pf_pull_hdr(pbuf, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
                /* Diddle with TCP options */
                int hlen;
-               opt = hdr + sizeof (struct tcphdr);
-               hlen = (th->th_off << 2) - sizeof (struct tcphdr);
+               opt = hdr + sizeof(struct tcphdr);
+               hlen = (th->th_off << 2) - sizeof(struct tcphdr);
                while (hlen >= TCPOLEN_TIMESTAMP) {
                        switch (*opt) {
-                       case TCPOPT_EOL:        /* FALLTHROUGH */
+                       case TCPOPT_EOL:        /* FALLTHROUGH */
                        case TCPOPT_NOP:
                                opt++;
                                hlen--;
@@ -2399,11 +2776,11 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                                                printf("\n");
                                        }
                                        REASON_SET(reason, PFRES_TS);
-                                       return (PF_DROP);
+                                       return PF_DROP;
                                }
                                if (opt[1] >= TCPOLEN_TIMESTAMP) {
                                        memcpy(&tsval, &opt[2],
-                                           sizeof (u_int32_t));
+                                           sizeof(u_int32_t));
                                        if (tsval && src->scrub &&
                                            (src->scrub->pfss_flags &
                                            PFSS_TIMESTAMP)) {
@@ -2418,7 +2795,7 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
 
                                        /* Modulate TS reply iff valid (!0) */
                                        memcpy(&tsecr, &opt[6],
-                                           sizeof (u_int32_t));
+                                           sizeof(u_int32_t));
                                        if (tsecr && dst->scrub &&
                                            (dst->scrub->pfss_flags &
                                            PFSS_TIMESTAMP)) {
@@ -2431,7 +2808,7 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                                        }
                                        got_ts = 1;
                                }
-                               /* FALLTHROUGH */
+                       /* FALLTHROUGH */
                        default:
                                hlen -= MAX(opt[1], 2);
                                opt += MAX(opt[1], 2);
@@ -2440,15 +2817,15 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                }
                if (copyback) {
                        /* Copyback the options, caller copys back header */
-                       int optoff = off + sizeof (*th);
-                       int optlen = (th->th_off << 2) - sizeof (*th);
-                       m = pf_lazy_makewritable(pd, m, optoff + optlen);
-                       if (!m) {
+                       int optoff = off + sizeof(*th);
+                       int optlen = (th->th_off << 2) - sizeof(*th);
+                       if (pf_lazy_makewritable(pd, pbuf, optoff + optlen) ==
+                           NULL) {
                                REASON_SET(reason, PFRES_MEMORY);
                                return PF_DROP;
                        }
                        *writeback = optoff + optlen;
-                       m_copyback(m, optoff, optlen, hdr + sizeof (*th));
+                       pbuf_copy_back(pbuf, optoff, optlen, hdr + sizeof(*th));
                }
        }
 
@@ -2460,13 +2837,13 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
         * TS echo check only works for the first 12 days of a connection
         * when the TS has exhausted half its 32bit space
         */
-#define TS_MAX_IDLE    (24*24*60*60)
-#define TS_MAX_CONN    (12*24*60*60)   /* XXX remove when better tsecr check */
+#define TS_MAX_IDLE     (24*24*60*60)
+#define TS_MAX_CONN     (12*24*60*60)   /* XXX remove when better tsecr check */
 
        getmicrouptime(&uptime);
        if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
            (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
-           pf_time_second() - state->creation > TS_MAX_CONN))  {
+           pf_time_second() - state->creation > TS_MAX_CONN)) {
                if (pf_status.debug >= PF_DEBUG_MISC) {
                        DPFPRINTF(("src idled out of PAWS\n"));
                        pf_print_state(state);
@@ -2561,16 +2938,17 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                 * packet got delayed in transit for much longer than
                 * this packet.
                 */
-               if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
+               if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0) {
                        ts_fudge = pf_default_rule.timeout[PFTM_TS_DIFF];
+               }
 
 
                /* Calculate max ticks since the last timestamp */
-#define TS_MAXFREQ     1100            /* RFC max TS freq of 1Khz + 10% skew */
-#define TS_MICROSECS   1000000         /* microseconds per second */
+#define TS_MAXFREQ      1100            /* RFC max TS freq of 1Khz + 10% skew */
+#define TS_MICROSECS    1000000         /* microseconds per second */
                timersub(&uptime, &src->scrub->pfss_last, &delta_ts);
                tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
-               tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
+               tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS / TS_MAXFREQ);
 
 
                if ((src->state >= TCPS_ESTABLISHED &&
@@ -2608,11 +2986,10 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                                printf("\n");
                        }
                        REASON_SET(reason, PFRES_TS);
-                       return (PF_DROP);
+                       return PF_DROP;
                }
 
                /* XXX I'd really like to require tsecr but it's optional */
-
        } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
            ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
            || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
@@ -2659,7 +3036,7 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                                printf("\n");
                        }
                        REASON_SET(reason, PFRES_TS);
-                       return (PF_DROP);
+                       return PF_DROP;
                }
        }
 
@@ -2673,10 +3050,10 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
         * packets (seen in a WWW accelerator or cache).
         */
        if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
-           (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
-               if (got_ts)
+           (PFSS_TIMESTAMP | PFSS_DATA_TS | PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
+               if (got_ts) {
                        src->scrub->pfss_flags |= PFSS_DATA_TS;
-               else {
+               else {
                        src->scrub->pfss_flags |= PFSS_DATA_NOTS;
                        if (pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
                            (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
@@ -2696,16 +3073,18 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
         * Update PAWS values
         */
        if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
-           (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
+           (PFSS_PAWS_IDLED | PFSS_TIMESTAMP))) {
                getmicrouptime(&src->scrub->pfss_last);
                if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
-                   (src->scrub->pfss_flags & PFSS_PAWS) == 0)
+                   (src->scrub->pfss_flags & PFSS_PAWS) == 0) {
                        src->scrub->pfss_tsval = tsval;
+               }
 
                if (tsecr) {
                        if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
-                           (src->scrub->pfss_flags & PFSS_PAWS) == 0)
+                           (src->scrub->pfss_flags & PFSS_PAWS) == 0) {
                                src->scrub->pfss_tsecr = tsecr;
+                       }
 
                        if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
                            (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
@@ -2715,48 +3094,53 @@ pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
                        }
 
                        /* Only fully initialized after a TS gets echoed */
-                       if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
+                       if ((src->scrub->pfss_flags & PFSS_PAWS) == 0) {
                                src->scrub->pfss_flags |= PFSS_PAWS;
+                       }
                }
        }
 
        /* I have a dream....  TCP segment reassembly.... */
-       return (0);
+       return 0;
 }
 
 static int
 pf_normalize_tcpopt(struct pf_rule *r, int dir, struct pfi_kif *kif,
-    struct pf_pdesc *pd, struct mbuf *m, struct tcphdr *th, int off,
+    struct pf_pdesc *pd, pbuf_t *pbuf, struct tcphdr *th, int off,
     int *rewrptr)
 {
 #pragma unused(dir, kif)
        sa_family_t af = pd->af;
-       u_int16_t       *mss;
-       int             thoff;
-       int             opt, cnt, optlen = 0;
-       int             rewrite = 0;
-       u_char          opts[MAX_TCPOPTLEN];
-       u_char          *optp = opts;
+       u_int16_t       *mss;
+       int             thoff;
+       int             opt, cnt, optlen = 0;
+       int             rewrite = 0;
+       u_char          opts[MAX_TCPOPTLEN];
+       u_char          *optp = opts;
 
        thoff = th->th_off << 2;
-       cnt = thoff - sizeof (struct tcphdr);
+       cnt = thoff - sizeof(struct tcphdr);
 
-       if (cnt > 0 && !pf_pull_hdr(m, off + sizeof (*th), opts, cnt,
-           NULL, NULL, af))
+       if (cnt > 0 && !pf_pull_hdr(pbuf, off + sizeof(*th), opts, cnt,
+           NULL, NULL, af)) {
                return PF_DROP;
+       }
 
        for (; cnt > 0; cnt -= optlen, optp += optlen) {
                opt = optp[0];
-               if (opt == TCPOPT_EOL)
+               if (opt == TCPOPT_EOL) {
                        break;
-               if (opt == TCPOPT_NOP)
+               }
+               if (opt == TCPOPT_NOP) {
                        optlen = 1;
-               else {
-                       if (cnt < 2)
+               else {
+                       if (cnt < 2) {
                                break;
+                       }
                        optlen = optp[1];
-                       if (optlen < 2 || optlen > cnt)
+                       if (optlen < 2 || optlen > cnt) {
                                break;
+                       }
                }
                switch (opt) {
                case TCPOPT_MAXSEG:
@@ -2767,10 +3151,11 @@ pf_normalize_tcpopt(struct pf_rule *r, int dir, struct pfi_kif *kif,
                                 *  Only do the TCP checksum fixup if delayed
                                 * checksum calculation will not be performed.
                                 */
-                               if (m->m_pkthdr.rcvif ||
-                                   !(m->m_pkthdr.csum_flags & CSUM_TCP))
+                               if (pbuf->pb_ifp ||
+                                   !(*pbuf->pb_csum_flags & CSUM_TCP)) {
                                        th->th_sum = pf_cksum_fixup(th->th_sum,
                                            *mss, htons(r->max_mss), 0);
+                               }
                                *mss = htons(r->max_mss);
                                rewrite = 1;
                        }
@@ -2781,21 +3166,22 @@ pf_normalize_tcpopt(struct pf_rule *r, int dir, struct pfi_kif *kif,
        }
 
        if (rewrite) {
-               struct mbuf *mw;
                u_short reason;
 
-               mw = pf_lazy_makewritable(pd, pd->mp,
-                   off + sizeof (*th) + thoff);
-               if (!mw) {
+               VERIFY(pbuf == pd->mp);
+
+               if (pf_lazy_makewritable(pd, pd->mp,
+                   off + sizeof(*th) + thoff) == NULL) {
                        REASON_SET(&reason, PFRES_MEMORY);
-                       if (r->log)
-                               PFLOG_PACKET(kif, h, m, AF_INET, dir, reason,
+                       if (r->log) {
+                               PFLOG_PACKET(kif, h, pbuf, AF_INET, dir, reason,
                                    r, 0, 0, pd);
+                       }
                        return PF_DROP;
                }
 
                *rewrptr = 1;
-               m_copyback(mw, off + sizeof (*th), thoff - sizeof (*th), opts);
+               pbuf_copy_back(pd->mp, off + sizeof(*th), thoff - sizeof(*th), opts);
        }
 
        return PF_PASS;