2 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2011 Alexander Bluhm <bluhm@openbsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $OpenBSD: pf_norm.c,v 1.114 2009/01/29 14:11:45 henning Exp $
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
36 #include <sys/param.h>
39 #include <sys/mutex.h>
40 #include <sys/refcount.h>
41 #include <sys/rwlock.h>
42 #include <sys/socket.h>
46 #include <net/pfvar.h>
47 #include <net/if_pflog.h>
49 #include <netinet/in.h>
50 #include <netinet/ip.h>
51 #include <netinet/ip_var.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/tcp_seq.h>
58 #include <netinet/ip6.h>
62 TAILQ_ENTRY(pf_frent) fr_next;
64 uint16_t fe_hdrlen; /* ipv4 header lenght with ip options
65 ipv6, extension, fragment header */
66 uint16_t fe_extoff; /* last extension header offset or 0 */
67 uint16_t fe_len; /* fragment length */
68 uint16_t fe_off; /* fragment offset */
69 uint16_t fe_mff; /* more fragment flag */
72 struct pf_fragment_cmp {
73 struct pf_addr frc_src;
74 struct pf_addr frc_dst;
78 uint8_t frc_direction;
82 struct pf_fragment_cmp fr_key;
83 #define fr_src fr_key.frc_src
84 #define fr_dst fr_key.frc_dst
85 #define fr_id fr_key.frc_id
86 #define fr_af fr_key.frc_af
87 #define fr_proto fr_key.frc_proto
88 #define fr_direction fr_key.frc_direction
90 RB_ENTRY(pf_fragment) fr_entry;
91 TAILQ_ENTRY(pf_fragment) frag_next;
92 uint8_t fr_flags; /* status flags */
93 #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */
94 #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */
95 #define PFFRAG_DROP 0x0004 /* Drop all fragments */
96 #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER))
97 uint16_t fr_max; /* fragment data max */
99 uint16_t fr_maxlen; /* maximum length of single fragment */
100 TAILQ_HEAD(pf_fragq, pf_frent) fr_queue;
103 struct pf_fragment_tag {
104 uint16_t ft_hdrlen; /* header length of reassembled pkt */
105 uint16_t ft_extoff; /* last extension header offset or 0 */
106 uint16_t ft_maxlen; /* maximum fragment payload length */
107 uint32_t ft_id; /* fragment id */
110 static struct mtx pf_frag_mtx;
111 #define PF_FRAG_LOCK() mtx_lock(&pf_frag_mtx)
112 #define PF_FRAG_UNLOCK() mtx_unlock(&pf_frag_mtx)
113 #define PF_FRAG_ASSERT() mtx_assert(&pf_frag_mtx, MA_OWNED)
115 VNET_DEFINE(uma_zone_t, pf_state_scrub_z); /* XXX: shared with pfsync */
117 static VNET_DEFINE(uma_zone_t, pf_frent_z);
118 #define V_pf_frent_z VNET(pf_frent_z)
119 static VNET_DEFINE(uma_zone_t, pf_frag_z);
120 #define V_pf_frag_z VNET(pf_frag_z)
122 TAILQ_HEAD(pf_fragqueue, pf_fragment);
123 TAILQ_HEAD(pf_cachequeue, pf_fragment);
124 static VNET_DEFINE(struct pf_fragqueue, pf_fragqueue);
125 #define V_pf_fragqueue VNET(pf_fragqueue)
126 static VNET_DEFINE(struct pf_cachequeue, pf_cachequeue);
127 #define V_pf_cachequeue VNET(pf_cachequeue)
128 RB_HEAD(pf_frag_tree, pf_fragment);
129 static VNET_DEFINE(struct pf_frag_tree, pf_frag_tree);
130 #define V_pf_frag_tree VNET(pf_frag_tree)
131 static VNET_DEFINE(struct pf_frag_tree, pf_cache_tree);
132 #define V_pf_cache_tree VNET(pf_cache_tree)
133 static int pf_frag_compare(struct pf_fragment *,
134 struct pf_fragment *);
135 static RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
136 static RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
138 /* Private prototypes */
139 static void pf_free_fragment(struct pf_fragment *);
140 static void pf_remove_fragment(struct pf_fragment *);
141 static int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
142 struct tcphdr *, int, sa_family_t);
144 static void pf_scrub_ip(struct mbuf **, u_int32_t, u_int8_t,
146 static void pf_flush_fragments(void);
147 static struct pf_fragment *pf_find_fragment(struct pf_fragment_cmp *key,
148 struct pf_frag_tree *tree);
149 struct pf_frent *pf_create_fragment(u_short *);
150 static int pf_reassemble(struct mbuf **, struct ip *, int,
152 int pf_reassemble6(struct mbuf **, struct ip6_hdr *,
153 struct ip6_frag *, uint16_t, uint16_t, int,
155 static struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
156 struct pf_fragment **, int, int, int *);
157 static struct pf_fragment *pf_fillup_fragment(struct pf_fragment_cmp *,
158 struct pf_frent *, u_short *);
159 int pf_isfull_fragment(struct pf_fragment *);
160 struct mbuf *pf_join_fragment(struct pf_fragment *);
165 static void pf_scrub_ip6(struct mbuf **, u_int8_t);
167 #define DPFPRINTF(x) do { \
168 if (V_pf_status.debug >= PF_DEBUG_MISC) { \
169 printf("%s: ", __func__); \
175 pf_ip2key(struct ip *ip, int dir, struct pf_fragment_cmp *key)
178 key->frc_src.v4 = ip->ip_src;
179 key->frc_dst.v4 = ip->ip_dst;
180 key->frc_af = AF_INET;
181 key->frc_proto = ip->ip_p;
182 key->frc_id = ip->ip_id;
183 key->frc_direction = dir;
187 pf_normalize_init(void)
190 V_pf_frag_z = uma_zcreate("pf frags", sizeof(struct pf_fragment),
191 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
192 V_pf_frent_z = uma_zcreate("pf frag entries", sizeof(struct pf_frent),
193 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
194 V_pf_state_scrub_z = uma_zcreate("pf state scrubs",
195 sizeof(struct pf_state_scrub), NULL, NULL, NULL, NULL,
198 V_pf_limits[PF_LIMIT_FRAGS].zone = V_pf_frent_z;
199 V_pf_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
200 uma_zone_set_max(V_pf_frent_z, PFFRAG_FRENT_HIWAT);
201 uma_zone_set_warning(V_pf_frent_z, "PF frag entries limit reached");
203 mtx_init(&pf_frag_mtx, "pf fragments", NULL, MTX_DEF);
205 TAILQ_INIT(&V_pf_fragqueue);
206 TAILQ_INIT(&V_pf_cachequeue);
210 pf_normalize_cleanup(void)
213 uma_zdestroy(V_pf_state_scrub_z);
214 uma_zdestroy(V_pf_frent_z);
215 uma_zdestroy(V_pf_frag_z);
217 mtx_destroy(&pf_frag_mtx);
221 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
225 if ((diff = a->fr_id - b->fr_id) != 0)
227 if ((diff = a->fr_proto - b->fr_proto) != 0)
229 if ((diff = a->fr_af - b->fr_af) != 0)
231 if ((diff = pf_addr_cmp(&a->fr_src, &b->fr_src, a->fr_af)) != 0)
233 if ((diff = pf_addr_cmp(&a->fr_dst, &b->fr_dst, a->fr_af)) != 0)
239 pf_purge_expired_fragments(void)
241 struct pf_fragment *frag;
242 u_int32_t expire = time_uptime -
243 V_pf_default_rule.timeout[PFTM_FRAG];
246 while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) {
247 KASSERT((BUFFER_FRAGMENTS(frag)),
248 ("BUFFER_FRAGMENTS(frag) == 0: %s", __FUNCTION__));
249 if (frag->fr_timeout > expire)
252 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
253 pf_free_fragment(frag);
256 while ((frag = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue)) != NULL) {
257 KASSERT((!BUFFER_FRAGMENTS(frag)),
258 ("BUFFER_FRAGMENTS(frag) != 0: %s", __FUNCTION__));
259 if (frag->fr_timeout > expire)
262 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
263 pf_free_fragment(frag);
264 KASSERT((TAILQ_EMPTY(&V_pf_cachequeue) ||
265 TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue) != frag),
266 ("!(TAILQ_EMPTY() || TAILQ_LAST() == farg): %s",
274 * Try to flush old fragments to make space for new ones
277 pf_flush_fragments(void)
279 struct pf_fragment *frag, *cache;
284 goal = uma_zone_get_cur(V_pf_frent_z) * 9 / 10;
285 DPFPRINTF(("trying to free %d frag entriess\n", goal));
286 while (goal < uma_zone_get_cur(V_pf_frent_z)) {
287 frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue);
289 pf_free_fragment(frag);
290 cache = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue);
292 pf_free_fragment(cache);
293 if (frag == NULL && cache == NULL)
299 /* Frees the fragments and all associated entries */
301 pf_free_fragment(struct pf_fragment *frag)
303 struct pf_frent *frent;
307 /* Free all fragments */
308 if (BUFFER_FRAGMENTS(frag)) {
309 for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
310 frent = TAILQ_FIRST(&frag->fr_queue)) {
311 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
313 m_freem(frent->fe_m);
314 uma_zfree(V_pf_frent_z, frent);
317 for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
318 frent = TAILQ_FIRST(&frag->fr_queue)) {
319 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
321 KASSERT((TAILQ_EMPTY(&frag->fr_queue) ||
322 TAILQ_FIRST(&frag->fr_queue)->fe_off >
324 ("! (TAILQ_EMPTY() || TAILQ_FIRST()->fe_off >"
325 " frent->fe_len): %s", __func__));
327 uma_zfree(V_pf_frent_z, frent);
331 pf_remove_fragment(frag);
335 static struct pf_fragment *
336 pf_find_fragment(struct pf_fragment_cmp *key, struct pf_frag_tree *tree)
338 struct pf_fragment *frag;
342 frag = RB_FIND(pf_frag_tree, tree, (struct pf_fragment *)key);
344 /* XXX Are we sure we want to update the timeout? */
345 frag->fr_timeout = time_uptime;
346 if (BUFFER_FRAGMENTS(frag)) {
347 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
348 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
350 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
351 TAILQ_INSERT_HEAD(&V_pf_cachequeue, frag, frag_next);
359 /* Removes a fragment from the fragment queue and frees the fragment */
362 pf_remove_fragment(struct pf_fragment *frag)
367 if (BUFFER_FRAGMENTS(frag)) {
368 RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag);
369 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
370 uma_zfree(V_pf_frag_z, frag);
372 RB_REMOVE(pf_frag_tree, &V_pf_cache_tree, frag);
373 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
374 uma_zfree(V_pf_frag_z, frag);
380 pf_create_fragment(u_short *reason)
382 struct pf_frent *frent;
386 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
388 pf_flush_fragments();
389 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
391 REASON_SET(reason, PFRES_MEMORY);
400 pf_fillup_fragment(struct pf_fragment_cmp *key, struct pf_frent *frent,
403 struct pf_frent *after, *next, *prev;
404 struct pf_fragment *frag;
409 /* No empty fragments. */
410 if (frent->fe_len == 0) {
411 DPFPRINTF(("bad fragment: len 0"));
415 /* All fragments are 8 byte aligned. */
416 if (frent->fe_mff && (frent->fe_len & 0x7)) {
417 DPFPRINTF(("bad fragment: mff and len %d", frent->fe_len));
421 /* Respect maximum length, IP_MAXPACKET == IPV6_MAXPACKET. */
422 if (frent->fe_off + frent->fe_len > IP_MAXPACKET) {
423 DPFPRINTF(("bad fragment: max packet %d",
424 frent->fe_off + frent->fe_len));
428 DPFPRINTF((key->frc_af == AF_INET ?
429 "reass frag %d @ %d-%d" : "reass frag %#08x @ %d-%d",
430 key->frc_id, frent->fe_off, frent->fe_off + frent->fe_len));
432 /* Fully buffer all of the fragments in this fragment queue. */
433 frag = pf_find_fragment(key, &V_pf_frag_tree);
435 /* Create a new reassembly queue for this packet. */
437 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
439 pf_flush_fragments();
440 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
442 REASON_SET(reason, PFRES_MEMORY);
447 *(struct pf_fragment_cmp *)frag = *key;
448 frag->fr_timeout = time_second;
449 frag->fr_maxlen = frent->fe_len;
450 TAILQ_INIT(&frag->fr_queue);
452 RB_INSERT(pf_frag_tree, &V_pf_frag_tree, frag);
453 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
455 /* We do not have a previous fragment. */
456 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
461 KASSERT(!TAILQ_EMPTY(&frag->fr_queue), ("!TAILQ_EMPTY()->fr_queue"));
463 /* Remember maximum fragment len for refragmentation. */
464 if (frent->fe_len > frag->fr_maxlen)
465 frag->fr_maxlen = frent->fe_len;
467 /* Maximum data we have seen already. */
468 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
469 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
471 /* Non terminal fragments must have more fragments flag. */
472 if (frent->fe_off + frent->fe_len < total && !frent->fe_mff)
475 /* Check if we saw the last fragment already. */
476 if (!TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff) {
477 if (frent->fe_off + frent->fe_len > total ||
478 (frent->fe_off + frent->fe_len == total && frent->fe_mff))
481 if (frent->fe_off + frent->fe_len == total && !frent->fe_mff)
485 /* Find a fragment after the current one. */
487 TAILQ_FOREACH(after, &frag->fr_queue, fr_next) {
488 if (after->fe_off > frent->fe_off)
493 KASSERT(prev != NULL || after != NULL,
494 ("prev != NULL || after != NULL"));
496 if (prev != NULL && prev->fe_off + prev->fe_len > frent->fe_off) {
499 precut = prev->fe_off + prev->fe_len - frent->fe_off;
500 if (precut >= frent->fe_len)
502 DPFPRINTF(("overlap -%d", precut));
503 m_adj(frent->fe_m, precut);
504 frent->fe_off += precut;
505 frent->fe_len -= precut;
508 for (; after != NULL && frent->fe_off + frent->fe_len > after->fe_off;
512 aftercut = frent->fe_off + frent->fe_len - after->fe_off;
513 DPFPRINTF(("adjust overlap %d", aftercut));
514 if (aftercut < after->fe_len) {
515 m_adj(after->fe_m, aftercut);
516 after->fe_off += aftercut;
517 after->fe_len -= aftercut;
521 /* This fragment is completely overlapped, lose it. */
522 next = TAILQ_NEXT(after, fr_next);
523 m_freem(after->fe_m);
524 TAILQ_REMOVE(&frag->fr_queue, after, fr_next);
525 uma_zfree(V_pf_frent_z, after);
529 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
531 TAILQ_INSERT_AFTER(&frag->fr_queue, prev, frent, fr_next);
536 REASON_SET(reason, PFRES_FRAG);
538 uma_zfree(V_pf_frent_z, frent);
543 pf_isfull_fragment(struct pf_fragment *frag)
545 struct pf_frent *frent, *next;
548 /* Check if we are completely reassembled */
549 if (TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff)
552 /* Maximum data we have seen already */
553 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
554 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
556 /* Check if we have all the data */
558 for (frent = TAILQ_FIRST(&frag->fr_queue); frent; frent = next) {
559 next = TAILQ_NEXT(frent, fr_next);
561 off += frent->fe_len;
562 if (off < total && (next == NULL || next->fe_off != off)) {
563 DPFPRINTF(("missing fragment at %d, next %d, total %d",
564 off, next == NULL ? -1 : next->fe_off, total));
568 DPFPRINTF(("%d < %d?", off, total));
571 KASSERT(off == total, ("off == total"));
577 pf_join_fragment(struct pf_fragment *frag)
580 struct pf_frent *frent, *next;
582 frent = TAILQ_FIRST(&frag->fr_queue);
583 next = TAILQ_NEXT(frent, fr_next);
585 /* Magic from ip_input. */
590 uma_zfree(V_pf_frent_z, frent);
591 for (frent = next; frent != NULL; frent = next) {
592 next = TAILQ_NEXT(frent, fr_next);
595 /* Strip off ip header. */
596 m_adj(m2, frent->fe_hdrlen);
597 uma_zfree(V_pf_frent_z, frent);
601 /* Remove from fragment queue. */
602 pf_remove_fragment(frag);
607 #define FR_IP_OFF(fr) ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
609 pf_reassemble(struct mbuf **m0, struct ip *ip, int dir, u_short *reason)
611 struct mbuf *m = *m0;
612 struct pf_frent *frent;
613 struct pf_fragment *frag;
614 struct pf_fragment_cmp key;
615 uint16_t total, hdrlen;
617 /* Get an entry for the fragment queue */
618 if ((frent = pf_create_fragment(reason)) == NULL)
622 frent->fe_hdrlen = ip->ip_hl << 2;
623 frent->fe_extoff = 0;
624 frent->fe_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
625 frent->fe_off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
626 frent->fe_mff = ntohs(ip->ip_off) & IP_MF;
628 pf_ip2key(ip, dir, &key);
630 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL)
633 /* The mbuf is part of the fragment entry, no direct free or access */
636 if (!pf_isfull_fragment(frag))
637 return (PF_PASS); /* drop because *m0 is NULL, no error */
639 /* We have all the data */
640 frent = TAILQ_FIRST(&frag->fr_queue);
641 KASSERT(frent != NULL, ("frent != NULL"));
642 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
643 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
644 hdrlen = frent->fe_hdrlen;
646 m = *m0 = pf_join_fragment(frag);
649 if (m->m_flags & M_PKTHDR) {
651 for (m = *m0; m; m = m->m_next)
654 m->m_pkthdr.len = plen;
657 ip = mtod(m, struct ip *);
658 ip->ip_len = htons(hdrlen + total);
659 ip->ip_off &= ~(IP_MF|IP_OFFMASK);
661 if (hdrlen + total > IP_MAXPACKET) {
662 DPFPRINTF(("drop: too big: %d", total));
664 REASON_SET(reason, PFRES_SHORT);
665 /* PF_DROP requires a valid mbuf *m0 in pf_test() */
669 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
675 pf_reassemble6(struct mbuf **m0, struct ip6_hdr *ip6, struct ip6_frag *fraghdr,
676 uint16_t hdrlen, uint16_t extoff, int dir, u_short *reason)
678 struct mbuf *m = *m0;
679 struct pf_frent *frent;
680 struct pf_fragment *frag;
681 struct pf_fragment_cmp key;
683 struct pf_fragment_tag *ftag;
686 uint16_t total, maxlen;
691 /* Get an entry for the fragment queue. */
692 if ((frent = pf_create_fragment(reason)) == NULL) {
698 frent->fe_hdrlen = hdrlen;
699 frent->fe_extoff = extoff;
700 frent->fe_len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - hdrlen;
701 frent->fe_off = ntohs(fraghdr->ip6f_offlg & IP6F_OFF_MASK);
702 frent->fe_mff = fraghdr->ip6f_offlg & IP6F_MORE_FRAG;
704 key.frc_src.v6 = ip6->ip6_src;
705 key.frc_dst.v6 = ip6->ip6_dst;
706 key.frc_af = AF_INET6;
707 /* Only the first fragment's protocol is relevant. */
709 key.frc_id = fraghdr->ip6f_ident;
710 key.frc_direction = dir;
712 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) {
717 /* The mbuf is part of the fragment entry, no direct free or access. */
720 if (!pf_isfull_fragment(frag)) {
722 return (PF_PASS); /* Drop because *m0 is NULL, no error. */
725 /* We have all the data. */
726 extoff = frent->fe_extoff;
727 maxlen = frag->fr_maxlen;
728 frag_id = frag->fr_id;
729 frent = TAILQ_FIRST(&frag->fr_queue);
730 KASSERT(frent != NULL, ("frent != NULL"));
731 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
732 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
733 hdrlen = frent->fe_hdrlen - sizeof(struct ip6_frag);
735 m = *m0 = pf_join_fragment(frag);
740 /* Take protocol from first fragment header. */
741 m = m_getptr(m, hdrlen + offsetof(struct ip6_frag, ip6f_nxt), &off);
742 KASSERT(m, ("%s: short mbuf chain", __func__));
743 proto = *(mtod(m, caddr_t) + off);
746 /* Delete frag6 header */
747 if (ip6_deletefraghdr(m, hdrlen, M_NOWAIT) != 0)
750 if (m->m_flags & M_PKTHDR) {
752 for (m = *m0; m; m = m->m_next)
755 m->m_pkthdr.len = plen;
758 if ((mtag = m_tag_get(PF_REASSEMBLED, sizeof(struct pf_fragment_tag),
761 ftag = (struct pf_fragment_tag *)(mtag + 1);
762 ftag->ft_hdrlen = hdrlen;
763 ftag->ft_extoff = extoff;
764 ftag->ft_maxlen = maxlen;
765 ftag->ft_id = frag_id;
766 m_tag_prepend(m, mtag);
768 ip6 = mtod(m, struct ip6_hdr *);
769 ip6->ip6_plen = htons(hdrlen - sizeof(struct ip6_hdr) + total);
771 /* Write protocol into next field of last extension header. */
772 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
774 KASSERT(m, ("%s: short mbuf chain", __func__));
775 *(mtod(m, char *) + off) = proto;
778 ip6->ip6_nxt = proto;
780 if (hdrlen - sizeof(struct ip6_hdr) + total > IPV6_MAXPACKET) {
781 DPFPRINTF(("drop: too big: %d", total));
783 REASON_SET(reason, PFRES_SHORT);
784 /* PF_DROP requires a valid mbuf *m0 in pf_test6(). */
788 DPFPRINTF(("complete: %p(%d)", m, ntohs(ip6->ip6_plen)));
792 REASON_SET(reason, PFRES_MEMORY);
793 /* PF_DROP requires a valid mbuf *m0 in pf_test6(), will free later. */
800 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
801 int drop, int *nomem)
803 struct mbuf *m = *m0;
804 struct pf_frent *frp, *fra, *cur = NULL;
805 int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
806 u_int16_t off = ntohs(h->ip_off) << 3;
807 u_int16_t max = ip_len + off;
811 KASSERT((*frag == NULL || !BUFFER_FRAGMENTS(*frag)),
812 ("!(*frag == NULL || !BUFFER_FRAGMENTS(*frag)): %s", __FUNCTION__));
814 /* Create a new range queue for this packet */
816 *frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
818 pf_flush_fragments();
819 *frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
824 /* Get an entry for the queue */
825 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
827 uma_zfree(V_pf_frag_z, *frag);
832 (*frag)->fr_flags = PFFRAG_NOBUFFER;
834 (*frag)->fr_src.v4 = h->ip_src;
835 (*frag)->fr_dst.v4 = h->ip_dst;
836 (*frag)->fr_id = h->ip_id;
837 (*frag)->fr_timeout = time_uptime;
840 cur->fe_len = max; /* TODO: fe_len = max - off ? */
841 TAILQ_INIT(&(*frag)->fr_queue);
842 TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
844 RB_INSERT(pf_frag_tree, &V_pf_cache_tree, *frag);
845 TAILQ_INSERT_HEAD(&V_pf_cachequeue, *frag, frag_next);
847 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
853 * Find a fragment after the current one:
854 * - off contains the real shifted offset.
857 TAILQ_FOREACH(fra, &(*frag)->fr_queue, fr_next) {
858 if (fra->fe_off > off)
863 KASSERT((frp != NULL || fra != NULL),
864 ("!(frp != NULL || fra != NULL): %s", __FUNCTION__));
869 precut = frp->fe_len - off;
870 if (precut >= ip_len) {
871 /* Fragment is entirely a duplicate */
872 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
873 h->ip_id, frp->fe_off, frp->fe_len, off, max));
877 /* They are adjacent. Fixup cache entry */
878 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
879 h->ip_id, frp->fe_off, frp->fe_len, off, max));
881 } else if (precut > 0) {
882 /* The first part of this payload overlaps with a
883 * fragment that has already been passed.
884 * Need to trim off the first part of the payload.
885 * But to do so easily, we need to create another
886 * mbuf to throw the original header into.
889 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
890 h->ip_id, precut, frp->fe_off, frp->fe_len, off,
895 /* Update the previous frag to encompass this one */
899 /* XXX Optimization opportunity
900 * This is a very heavy way to trim the payload.
901 * we could do it much faster by diddling mbuf
902 * internals but that would be even less legible
903 * than this mbuf magic. For my next trick,
904 * I'll pull a rabbit out of my laptop.
906 *m0 = m_dup(m, M_NOWAIT);
909 /* From KAME Project : We have missed this! */
910 m_adj(*m0, (h->ip_hl << 2) -
911 (*m0)->m_pkthdr.len);
913 KASSERT(((*m0)->m_next == NULL),
914 ("(*m0)->m_next != NULL: %s",
916 m_adj(m, precut + (h->ip_hl << 2));
919 if (m->m_flags & M_PKTHDR) {
922 for (t = m; t; t = t->m_next)
924 m->m_pkthdr.len = plen;
928 h = mtod(m, struct ip *);
930 KASSERT(((int)m->m_len ==
931 ntohs(h->ip_len) - precut),
932 ("m->m_len != ntohs(h->ip_len) - precut: %s",
934 h->ip_off = htons(ntohs(h->ip_off) +
936 h->ip_len = htons(ntohs(h->ip_len) - precut);
941 /* There is a gap between fragments */
943 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
944 h->ip_id, -precut, frp->fe_off, frp->fe_len, off,
947 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
953 TAILQ_INSERT_AFTER(&(*frag)->fr_queue, frp, cur, fr_next);
961 aftercut = max - fra->fe_off;
963 /* Adjacent fragments */
964 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
965 h->ip_id, off, max, fra->fe_off, fra->fe_len));
968 } else if (aftercut > 0) {
969 /* Need to chop off the tail of this fragment */
970 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
971 h->ip_id, aftercut, off, max, fra->fe_off,
980 if (m->m_flags & M_PKTHDR) {
983 for (t = m; t; t = t->m_next)
985 m->m_pkthdr.len = plen;
987 h = mtod(m, struct ip *);
988 KASSERT(((int)m->m_len == ntohs(h->ip_len) - aftercut),
989 ("m->m_len != ntohs(h->ip_len) - aftercut: %s",
991 h->ip_len = htons(ntohs(h->ip_len) - aftercut);
995 } else if (frp == NULL) {
996 /* There is a gap between fragments */
997 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
998 h->ip_id, -aftercut, off, max, fra->fe_off,
1001 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
1007 TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
1011 /* Need to glue together two separate fragment descriptors */
1013 if (cur && fra->fe_off <= cur->fe_len) {
1014 /* Need to merge in a previous 'cur' */
1015 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1016 "%d-%d) %d-%d (%d-%d)\n",
1017 h->ip_id, cur->fe_off, cur->fe_len, off,
1018 max, fra->fe_off, fra->fe_len));
1019 fra->fe_off = cur->fe_off;
1020 TAILQ_REMOVE(&(*frag)->fr_queue, cur, fr_next);
1021 uma_zfree(V_pf_frent_z, cur);
1024 } else if (frp && fra->fe_off <= frp->fe_len) {
1025 /* Need to merge in a modified 'frp' */
1026 KASSERT((cur == NULL), ("cur != NULL: %s",
1028 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1029 "%d-%d) %d-%d (%d-%d)\n",
1030 h->ip_id, frp->fe_off, frp->fe_len, off,
1031 max, fra->fe_off, fra->fe_len));
1032 fra->fe_off = frp->fe_off;
1033 TAILQ_REMOVE(&(*frag)->fr_queue, frp, fr_next);
1034 uma_zfree(V_pf_frent_z, frp);
1043 * We must keep tracking the overall fragment even when
1044 * we're going to drop it anyway so that we know when to
1045 * free the overall descriptor. Thus we drop the frag late.
1052 /* Update maximum data size */
1053 if ((*frag)->fr_max < max)
1054 (*frag)->fr_max = max;
1056 /* This is the last segment */
1058 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1060 /* Check if we are completely reassembled */
1061 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
1062 TAILQ_FIRST(&(*frag)->fr_queue)->fe_off == 0 &&
1063 TAILQ_FIRST(&(*frag)->fr_queue)->fe_len == (*frag)->fr_max) {
1064 /* Remove from fragment queue */
1065 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
1067 pf_free_fragment(*frag);
1076 /* Still need to pay attention to !IP_MF */
1077 if (!mff && *frag != NULL)
1078 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1085 /* Still need to pay attention to !IP_MF */
1086 if (!mff && *frag != NULL)
1087 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1090 /* This fragment has been deemed bad. Don't reass */
1091 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
1092 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
1094 (*frag)->fr_flags |= PFFRAG_DROP;
1102 pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag)
1104 struct mbuf *m = *m0, *t;
1105 struct pf_fragment_tag *ftag = (struct pf_fragment_tag *)(mtag + 1);
1108 uint16_t hdrlen, extoff, maxlen;
1112 hdrlen = ftag->ft_hdrlen;
1113 extoff = ftag->ft_extoff;
1114 maxlen = ftag->ft_maxlen;
1115 frag_id = ftag->ft_id;
1116 m_tag_delete(m, mtag);
1123 /* Use protocol from next field of last extension header */
1124 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
1126 KASSERT((m != NULL), ("pf_refragment6: short mbuf chain"));
1127 proto = *(mtod(m, caddr_t) + off);
1128 *(mtod(m, char *) + off) = IPPROTO_FRAGMENT;
1131 struct ip6_hdr *hdr;
1133 hdr = mtod(m, struct ip6_hdr *);
1134 proto = hdr->ip6_nxt;
1135 hdr->ip6_nxt = IPPROTO_FRAGMENT;
1139 * Maxlen may be less than 8 if there was only a single
1140 * fragment. As it was fragmented before, add a fragment
1141 * header also for a single fragment. If total or maxlen
1142 * is less than 8, ip6_fragment() will return EMSGSIZE and
1143 * we drop the packet.
1145 error = ip6_fragment(ifp, m, hdrlen, proto, maxlen, frag_id);
1146 m = (*m0)->m_nextpkt;
1147 (*m0)->m_nextpkt = NULL;
1149 /* The first mbuf contains the unfragmented packet. */
1154 /* Drop expects an mbuf to free. */
1155 DPFPRINTF(("refragment error %d", error));
1158 for (t = m; m; m = t) {
1160 m->m_nextpkt = NULL;
1161 memset(&pd, 0, sizeof(pd));
1162 pd.pf_mtag = pf_find_mtag(m);
1173 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
1174 struct pf_pdesc *pd)
1176 struct mbuf *m = *m0;
1178 struct pf_fragment *frag = NULL;
1179 struct pf_fragment_cmp key;
1180 struct ip *h = mtod(m, struct ip *);
1181 int mff = (ntohs(h->ip_off) & IP_MF);
1182 int hlen = h->ip_hl << 2;
1183 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1192 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1195 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1196 r = r->skip[PF_SKIP_IFP].ptr;
1197 else if (r->direction && r->direction != dir)
1198 r = r->skip[PF_SKIP_DIR].ptr;
1199 else if (r->af && r->af != AF_INET)
1200 r = r->skip[PF_SKIP_AF].ptr;
1201 else if (r->proto && r->proto != h->ip_p)
1202 r = r->skip[PF_SKIP_PROTO].ptr;
1203 else if (PF_MISMATCHAW(&r->src.addr,
1204 (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
1205 r->src.neg, kif, M_GETFIB(m)))
1206 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1207 else if (PF_MISMATCHAW(&r->dst.addr,
1208 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
1209 r->dst.neg, NULL, M_GETFIB(m)))
1210 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1211 else if (r->match_tag && !pf_match_tag(m, r, &tag,
1212 pd->pf_mtag ? pd->pf_mtag->tag : 0))
1213 r = TAILQ_NEXT(r, entries);
1218 if (r == NULL || r->action == PF_NOSCRUB)
1221 r->packets[dir == PF_OUT]++;
1222 r->bytes[dir == PF_OUT] += pd->tot_len;
1225 /* Check for illegal packets */
1226 if (hlen < (int)sizeof(struct ip))
1229 if (hlen > ntohs(h->ip_len))
1232 /* Clear IP_DF if the rule uses the no-df option */
1233 if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
1234 u_int16_t ip_off = h->ip_off;
1236 h->ip_off &= htons(~IP_DF);
1237 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1240 /* We will need other tests here */
1241 if (!fragoff && !mff)
1244 /* We're dealing with a fragment now. Don't allow fragments
1245 * with IP_DF to enter the cache. If the flag was cleared by
1246 * no-df above, fine. Otherwise drop it.
1248 if (h->ip_off & htons(IP_DF)) {
1249 DPFPRINTF(("IP_DF\n"));
1253 ip_len = ntohs(h->ip_len) - hlen;
1254 ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1256 /* All fragments are 8 byte aligned */
1257 if (mff && (ip_len & 0x7)) {
1258 DPFPRINTF(("mff and %d\n", ip_len));
1262 /* Respect maximum length */
1263 if (fragoff + ip_len > IP_MAXPACKET) {
1264 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
1267 max = fragoff + ip_len;
1269 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
1271 /* Fully buffer all of the fragments */
1274 pf_ip2key(h, dir, &key);
1275 frag = pf_find_fragment(&key, &V_pf_frag_tree);
1277 /* Check if we saw the last fragment already */
1278 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1282 /* Might return a completely reassembled mbuf, or NULL */
1283 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
1284 verdict = pf_reassemble(m0, h, dir, reason);
1287 if (verdict != PF_PASS)
1294 /* use mtag from concatenated mbuf chain */
1295 pd->pf_mtag = pf_find_mtag(m);
1297 if (pd->pf_mtag == NULL) {
1298 printf("%s: pf_find_mtag returned NULL(1)\n", __func__);
1299 if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
1306 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1309 h = mtod(m, struct ip *);
1311 /* non-buffering fragment cache (drops or masks overlaps) */
1314 if (dir == PF_OUT && pd->pf_mtag->flags & PF_TAG_FRAGCACHE) {
1316 * Already passed the fragment cache in the
1317 * input direction. If we continued, it would
1318 * appear to be a dup and would be dropped.
1324 pf_ip2key(h, dir, &key);
1325 frag = pf_find_fragment(&key, &V_pf_cache_tree);
1327 /* Check if we saw the last fragment already */
1328 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1329 max > frag->fr_max) {
1330 if (r->rule_flag & PFRULE_FRAGDROP)
1331 frag->fr_flags |= PFFRAG_DROP;
1335 *m0 = m = pf_fragcache(m0, h, &frag, mff,
1336 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
1344 /* use mtag from copied and trimmed mbuf chain */
1345 pd->pf_mtag = pf_find_mtag(m);
1347 if (pd->pf_mtag == NULL) {
1348 printf("%s: pf_find_mtag returned NULL(2)\n", __func__);
1349 if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
1357 pd->pf_mtag->flags |= PF_TAG_FRAGCACHE;
1359 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1365 /* At this point, only IP_DF is allowed in ip_off */
1366 if (h->ip_off & ~htons(IP_DF)) {
1367 u_int16_t ip_off = h->ip_off;
1369 h->ip_off &= htons(IP_DF);
1370 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1373 /* not missing a return here */
1376 pf_scrub_ip(&m, r->rule_flag, r->min_ttl, r->set_tos);
1378 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1379 pd->flags |= PFDESC_IP_REAS;
1383 REASON_SET(reason, PFRES_MEMORY);
1384 if (r != NULL && r->log)
1385 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1390 REASON_SET(reason, PFRES_NORM);
1391 if (r != NULL && r->log)
1392 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1397 DPFPRINTF(("dropping bad fragment\n"));
1399 /* Free associated fragments */
1401 pf_free_fragment(frag);
1405 REASON_SET(reason, PFRES_FRAG);
1406 if (r != NULL && r->log)
1407 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1416 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1417 u_short *reason, struct pf_pdesc *pd)
1419 struct mbuf *m = *m0;
1421 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1426 struct ip6_opt_jumbo jumbo;
1427 struct ip6_frag frag;
1428 u_int32_t jumbolen = 0, plen;
1436 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1439 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1440 r = r->skip[PF_SKIP_IFP].ptr;
1441 else if (r->direction && r->direction != dir)
1442 r = r->skip[PF_SKIP_DIR].ptr;
1443 else if (r->af && r->af != AF_INET6)
1444 r = r->skip[PF_SKIP_AF].ptr;
1445 #if 0 /* header chain! */
1446 else if (r->proto && r->proto != h->ip6_nxt)
1447 r = r->skip[PF_SKIP_PROTO].ptr;
1449 else if (PF_MISMATCHAW(&r->src.addr,
1450 (struct pf_addr *)&h->ip6_src, AF_INET6,
1451 r->src.neg, kif, M_GETFIB(m)))
1452 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1453 else if (PF_MISMATCHAW(&r->dst.addr,
1454 (struct pf_addr *)&h->ip6_dst, AF_INET6,
1455 r->dst.neg, NULL, M_GETFIB(m)))
1456 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1461 if (r == NULL || r->action == PF_NOSCRUB)
1464 r->packets[dir == PF_OUT]++;
1465 r->bytes[dir == PF_OUT] += pd->tot_len;
1468 /* Check for illegal packets */
1469 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1473 off = sizeof(struct ip6_hdr);
1478 case IPPROTO_FRAGMENT:
1482 case IPPROTO_ROUTING:
1483 case IPPROTO_DSTOPTS:
1484 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1488 if (proto == IPPROTO_AH)
1489 off += (ext.ip6e_len + 2) * 4;
1491 off += (ext.ip6e_len + 1) * 8;
1492 proto = ext.ip6e_nxt;
1494 case IPPROTO_HOPOPTS:
1495 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1499 optend = off + (ext.ip6e_len + 1) * 8;
1500 ooff = off + sizeof(ext);
1502 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1503 sizeof(opt.ip6o_type), NULL, NULL,
1506 if (opt.ip6o_type == IP6OPT_PAD1) {
1510 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1511 NULL, NULL, AF_INET6))
1513 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1515 switch (opt.ip6o_type) {
1517 if (h->ip6_plen != 0)
1519 if (!pf_pull_hdr(m, ooff, &jumbo,
1520 sizeof(jumbo), NULL, NULL,
1523 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1525 jumbolen = ntohl(jumbolen);
1526 if (jumbolen <= IPV6_MAXPACKET)
1528 if (sizeof(struct ip6_hdr) + jumbolen !=
1535 ooff += sizeof(opt) + opt.ip6o_len;
1536 } while (ooff < optend);
1539 proto = ext.ip6e_nxt;
1545 } while (!terminal);
1547 /* jumbo payload option must be present, or plen > 0 */
1548 if (ntohs(h->ip6_plen) == 0)
1551 plen = ntohs(h->ip6_plen);
1554 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1557 pf_scrub_ip6(&m, r->min_ttl);
1562 /* Jumbo payload packets cannot be fragmented. */
1563 plen = ntohs(h->ip6_plen);
1564 if (plen == 0 || jumbolen)
1566 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1569 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1572 /* Offset now points to data portion. */
1573 off += sizeof(frag);
1575 /* Returns PF_DROP or *m0 is NULL or completely reassembled mbuf. */
1576 if (pf_reassemble6(m0, h, &frag, off, extoff, dir, reason) != PF_PASS)
1582 pd->flags |= PFDESC_IP_REAS;
1586 REASON_SET(reason, PFRES_SHORT);
1587 if (r != NULL && r->log)
1588 PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1593 REASON_SET(reason, PFRES_NORM);
1594 if (r != NULL && r->log)
1595 PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1602 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1603 int off, void *h, struct pf_pdesc *pd)
1605 struct pf_rule *r, *rm = NULL;
1606 struct tcphdr *th = pd->hdr.tcp;
1610 sa_family_t af = pd->af;
1614 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1617 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1618 r = r->skip[PF_SKIP_IFP].ptr;
1619 else if (r->direction && r->direction != dir)
1620 r = r->skip[PF_SKIP_DIR].ptr;
1621 else if (r->af && r->af != af)
1622 r = r->skip[PF_SKIP_AF].ptr;
1623 else if (r->proto && r->proto != pd->proto)
1624 r = r->skip[PF_SKIP_PROTO].ptr;
1625 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1626 r->src.neg, kif, M_GETFIB(m)))
1627 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1628 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1629 r->src.port[0], r->src.port[1], th->th_sport))
1630 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1631 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1632 r->dst.neg, NULL, M_GETFIB(m)))
1633 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1634 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1635 r->dst.port[0], r->dst.port[1], th->th_dport))
1636 r = r->skip[PF_SKIP_DST_PORT].ptr;
1637 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1638 pf_osfp_fingerprint(pd, m, off, th),
1640 r = TAILQ_NEXT(r, entries);
1647 if (rm == NULL || rm->action == PF_NOSCRUB)
1650 r->packets[dir == PF_OUT]++;
1651 r->bytes[dir == PF_OUT] += pd->tot_len;
1654 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1655 pd->flags |= PFDESC_TCP_NORM;
1657 flags = th->th_flags;
1658 if (flags & TH_SYN) {
1659 /* Illegal packet */
1666 /* Illegal packet */
1667 if (!(flags & (TH_ACK|TH_RST)))
1671 if (!(flags & TH_ACK)) {
1672 /* These flags are only valid if ACK is set */
1673 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1677 /* Check for illegal header length */
1678 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1681 /* If flags changed, or reserved data set, then adjust */
1682 if (flags != th->th_flags || th->th_x2 != 0) {
1685 ov = *(u_int16_t *)(&th->th_ack + 1);
1686 th->th_flags = flags;
1688 nv = *(u_int16_t *)(&th->th_ack + 1);
1690 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1694 /* Remove urgent pointer, if TH_URG is not set */
1695 if (!(flags & TH_URG) && th->th_urp) {
1696 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1701 /* Process options */
1702 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af))
1705 /* copy back packet headers if we sanitized */
1707 m_copyback(m, off, sizeof(*th), (caddr_t)th);
1712 REASON_SET(&reason, PFRES_NORM);
1713 if (rm != NULL && r->log)
1714 PFLOG_PACKET(kif, m, AF_INET, dir, reason, r, NULL, NULL, pd,
1720 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1721 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1723 u_int32_t tsval, tsecr;
1727 KASSERT((src->scrub == NULL),
1728 ("pf_normalize_tcp_init: src->scrub != NULL"));
1730 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1731 if (src->scrub == NULL)
1737 struct ip *h = mtod(m, struct ip *);
1738 src->scrub->pfss_ttl = h->ip_ttl;
1744 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1745 src->scrub->pfss_ttl = h->ip6_hlim;
1753 * All normalizations below are only begun if we see the start of
1754 * the connections. They must all set an enabled bit in pfss_flags
1756 if ((th->th_flags & TH_SYN) == 0)
1760 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1761 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1762 /* Diddle with TCP options */
1764 opt = hdr + sizeof(struct tcphdr);
1765 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1766 while (hlen >= TCPOLEN_TIMESTAMP) {
1768 case TCPOPT_EOL: /* FALLTHROUGH */
1773 case TCPOPT_TIMESTAMP:
1774 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1775 src->scrub->pfss_flags |=
1777 src->scrub->pfss_ts_mod =
1778 htonl(arc4random());
1780 /* note PFSS_PAWS not set yet */
1781 memcpy(&tsval, &opt[2],
1783 memcpy(&tsecr, &opt[6],
1785 src->scrub->pfss_tsval0 = ntohl(tsval);
1786 src->scrub->pfss_tsval = ntohl(tsval);
1787 src->scrub->pfss_tsecr = ntohl(tsecr);
1788 getmicrouptime(&src->scrub->pfss_last);
1792 hlen -= MAX(opt[1], 2);
1793 opt += MAX(opt[1], 2);
1803 pf_normalize_tcp_cleanup(struct pf_state *state)
1805 if (state->src.scrub)
1806 uma_zfree(V_pf_state_scrub_z, state->src.scrub);
1807 if (state->dst.scrub)
1808 uma_zfree(V_pf_state_scrub_z, state->dst.scrub);
1810 /* Someday... flush the TCP segment reassembly descriptors. */
1814 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1815 u_short *reason, struct tcphdr *th, struct pf_state *state,
1816 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1818 struct timeval uptime;
1819 u_int32_t tsval, tsecr;
1820 u_int tsval_from_last;
1826 KASSERT((src->scrub || dst->scrub),
1827 ("%s: src->scrub && dst->scrub!", __func__));
1830 * Enforce the minimum TTL seen for this connection. Negate a common
1831 * technique to evade an intrusion detection system and confuse
1832 * firewall state code.
1838 struct ip *h = mtod(m, struct ip *);
1839 if (h->ip_ttl > src->scrub->pfss_ttl)
1840 src->scrub->pfss_ttl = h->ip_ttl;
1841 h->ip_ttl = src->scrub->pfss_ttl;
1849 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1850 if (h->ip6_hlim > src->scrub->pfss_ttl)
1851 src->scrub->pfss_ttl = h->ip6_hlim;
1852 h->ip6_hlim = src->scrub->pfss_ttl;
1859 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1860 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1861 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1862 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1863 /* Diddle with TCP options */
1865 opt = hdr + sizeof(struct tcphdr);
1866 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1867 while (hlen >= TCPOLEN_TIMESTAMP) {
1869 case TCPOPT_EOL: /* FALLTHROUGH */
1874 case TCPOPT_TIMESTAMP:
1875 /* Modulate the timestamps. Can be used for
1876 * NAT detection, OS uptime determination or
1881 /* Huh? Multiple timestamps!? */
1882 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1883 DPFPRINTF(("multiple TS??"));
1884 pf_print_state(state);
1887 REASON_SET(reason, PFRES_TS);
1890 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1891 memcpy(&tsval, &opt[2],
1893 if (tsval && src->scrub &&
1894 (src->scrub->pfss_flags &
1896 tsval = ntohl(tsval);
1897 pf_change_a(&opt[2],
1900 src->scrub->pfss_ts_mod),
1905 /* Modulate TS reply iff valid (!0) */
1906 memcpy(&tsecr, &opt[6],
1908 if (tsecr && dst->scrub &&
1909 (dst->scrub->pfss_flags &
1911 tsecr = ntohl(tsecr)
1912 - dst->scrub->pfss_ts_mod;
1913 pf_change_a(&opt[6],
1914 &th->th_sum, htonl(tsecr),
1922 hlen -= MAX(opt[1], 2);
1923 opt += MAX(opt[1], 2);
1928 /* Copyback the options, caller copys back header */
1930 m_copyback(m, off + sizeof(struct tcphdr),
1931 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1932 sizeof(struct tcphdr));
1938 * Must invalidate PAWS checks on connections idle for too long.
1939 * The fastest allowed timestamp clock is 1ms. That turns out to
1940 * be about 24 days before it wraps. XXX Right now our lowerbound
1941 * TS echo check only works for the first 12 days of a connection
1942 * when the TS has exhausted half its 32bit space
1944 #define TS_MAX_IDLE (24*24*60*60)
1945 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1947 getmicrouptime(&uptime);
1948 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1949 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1950 time_uptime - state->creation > TS_MAX_CONN)) {
1951 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1952 DPFPRINTF(("src idled out of PAWS\n"));
1953 pf_print_state(state);
1956 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1959 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1960 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1961 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1962 DPFPRINTF(("dst idled out of PAWS\n"));
1963 pf_print_state(state);
1966 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1970 if (got_ts && src->scrub && dst->scrub &&
1971 (src->scrub->pfss_flags & PFSS_PAWS) &&
1972 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1973 /* Validate that the timestamps are "in-window".
1974 * RFC1323 describes TCP Timestamp options that allow
1975 * measurement of RTT (round trip time) and PAWS
1976 * (protection against wrapped sequence numbers). PAWS
1977 * gives us a set of rules for rejecting packets on
1978 * long fat pipes (packets that were somehow delayed
1979 * in transit longer than the time it took to send the
1980 * full TCP sequence space of 4Gb). We can use these
1981 * rules and infer a few others that will let us treat
1982 * the 32bit timestamp and the 32bit echoed timestamp
1983 * as sequence numbers to prevent a blind attacker from
1984 * inserting packets into a connection.
1987 * - The timestamp on this packet must be greater than
1988 * or equal to the last value echoed by the other
1989 * endpoint. The RFC says those will be discarded
1990 * since it is a dup that has already been acked.
1991 * This gives us a lowerbound on the timestamp.
1992 * timestamp >= other last echoed timestamp
1993 * - The timestamp will be less than or equal to
1994 * the last timestamp plus the time between the
1995 * last packet and now. The RFC defines the max
1996 * clock rate as 1ms. We will allow clocks to be
1997 * up to 10% fast and will allow a total difference
1998 * or 30 seconds due to a route change. And this
1999 * gives us an upperbound on the timestamp.
2000 * timestamp <= last timestamp + max ticks
2001 * We have to be careful here. Windows will send an
2002 * initial timestamp of zero and then initialize it
2003 * to a random value after the 3whs; presumably to
2004 * avoid a DoS by having to call an expensive RNG
2005 * during a SYN flood. Proof MS has at least one
2006 * good security geek.
2008 * - The TCP timestamp option must also echo the other
2009 * endpoints timestamp. The timestamp echoed is the
2010 * one carried on the earliest unacknowledged segment
2011 * on the left edge of the sequence window. The RFC
2012 * states that the host will reject any echoed
2013 * timestamps that were larger than any ever sent.
2014 * This gives us an upperbound on the TS echo.
2015 * tescr <= largest_tsval
2016 * - The lowerbound on the TS echo is a little more
2017 * tricky to determine. The other endpoint's echoed
2018 * values will not decrease. But there may be
2019 * network conditions that re-order packets and
2020 * cause our view of them to decrease. For now the
2021 * only lowerbound we can safely determine is that
2022 * the TS echo will never be less than the original
2023 * TS. XXX There is probably a better lowerbound.
2024 * Remove TS_MAX_CONN with better lowerbound check.
2025 * tescr >= other original TS
2027 * It is also important to note that the fastest
2028 * timestamp clock of 1ms will wrap its 32bit space in
2029 * 24 days. So we just disable TS checking after 24
2030 * days of idle time. We actually must use a 12d
2031 * connection limit until we can come up with a better
2032 * lowerbound to the TS echo check.
2034 struct timeval delta_ts;
2039 * PFTM_TS_DIFF is how many seconds of leeway to allow
2040 * a host's timestamp. This can happen if the previous
2041 * packet got delayed in transit for much longer than
2044 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
2045 ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF];
2047 /* Calculate max ticks since the last timestamp */
2048 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
2049 #define TS_MICROSECS 1000000 /* microseconds per second */
2051 timevalsub(&delta_ts, &src->scrub->pfss_last);
2052 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
2053 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
2055 if ((src->state >= TCPS_ESTABLISHED &&
2056 dst->state >= TCPS_ESTABLISHED) &&
2057 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
2058 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
2059 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
2060 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
2061 /* Bad RFC1323 implementation or an insertion attack.
2063 * - Solaris 2.6 and 2.7 are known to send another ACK
2064 * after the FIN,FIN|ACK,ACK closing that carries
2068 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
2069 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
2070 SEQ_GT(tsval, src->scrub->pfss_tsval +
2071 tsval_from_last) ? '1' : ' ',
2072 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
2073 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
2074 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u "
2075 "idle: %jus %lums\n",
2076 tsval, tsecr, tsval_from_last,
2077 (uintmax_t)delta_ts.tv_sec,
2078 delta_ts.tv_usec / 1000));
2079 DPFPRINTF((" src->tsval: %u tsecr: %u\n",
2080 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
2081 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u"
2082 "\n", dst->scrub->pfss_tsval,
2083 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
2084 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2085 pf_print_state(state);
2086 pf_print_flags(th->th_flags);
2089 REASON_SET(reason, PFRES_TS);
2093 /* XXX I'd really like to require tsecr but it's optional */
2095 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
2096 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
2097 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
2098 src->scrub && dst->scrub &&
2099 (src->scrub->pfss_flags & PFSS_PAWS) &&
2100 (dst->scrub->pfss_flags & PFSS_PAWS)) {
2101 /* Didn't send a timestamp. Timestamps aren't really useful
2103 * - connection opening or closing (often not even sent).
2104 * but we must not let an attacker to put a FIN on a
2105 * data packet to sneak it through our ESTABLISHED check.
2106 * - on a TCP reset. RFC suggests not even looking at TS.
2107 * - on an empty ACK. The TS will not be echoed so it will
2108 * probably not help keep the RTT calculation in sync and
2109 * there isn't as much danger when the sequence numbers
2110 * got wrapped. So some stacks don't include TS on empty
2113 * To minimize the disruption to mostly RFC1323 conformant
2114 * stacks, we will only require timestamps on data packets.
2116 * And what do ya know, we cannot require timestamps on data
2117 * packets. There appear to be devices that do legitimate
2118 * TCP connection hijacking. There are HTTP devices that allow
2119 * a 3whs (with timestamps) and then buffer the HTTP request.
2120 * If the intermediate device has the HTTP response cache, it
2121 * will spoof the response but not bother timestamping its
2122 * packets. So we can look for the presence of a timestamp in
2123 * the first data packet and if there, require it in all future
2127 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
2129 * Hey! Someone tried to sneak a packet in. Or the
2130 * stack changed its RFC1323 behavior?!?!
2132 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2133 DPFPRINTF(("Did not receive expected RFC1323 "
2135 pf_print_state(state);
2136 pf_print_flags(th->th_flags);
2139 REASON_SET(reason, PFRES_TS);
2146 * We will note if a host sends his data packets with or without
2147 * timestamps. And require all data packets to contain a timestamp
2148 * if the first does. PAWS implicitly requires that all data packets be
2149 * timestamped. But I think there are middle-man devices that hijack
2150 * TCP streams immediately after the 3whs and don't timestamp their
2151 * packets (seen in a WWW accelerator or cache).
2153 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
2154 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
2156 src->scrub->pfss_flags |= PFSS_DATA_TS;
2158 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
2159 if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
2160 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
2161 /* Don't warn if other host rejected RFC1323 */
2162 DPFPRINTF(("Broken RFC1323 stack did not "
2163 "timestamp data packet. Disabled PAWS "
2165 pf_print_state(state);
2166 pf_print_flags(th->th_flags);
2174 * Update PAWS values
2176 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
2177 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
2178 getmicrouptime(&src->scrub->pfss_last);
2179 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
2180 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2181 src->scrub->pfss_tsval = tsval;
2184 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
2185 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2186 src->scrub->pfss_tsecr = tsecr;
2188 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
2189 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
2190 src->scrub->pfss_tsval0 == 0)) {
2191 /* tsval0 MUST be the lowest timestamp */
2192 src->scrub->pfss_tsval0 = tsval;
2195 /* Only fully initialized after a TS gets echoed */
2196 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
2197 src->scrub->pfss_flags |= PFSS_PAWS;
2201 /* I have a dream.... TCP segment reassembly.... */
2206 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
2207 int off, sa_family_t af)
2211 int opt, cnt, optlen = 0;
2213 u_char opts[TCP_MAXOLEN];
2214 u_char *optp = opts;
2216 thoff = th->th_off << 2;
2217 cnt = thoff - sizeof(struct tcphdr);
2219 if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
2223 for (; cnt > 0; cnt -= optlen, optp += optlen) {
2225 if (opt == TCPOPT_EOL)
2227 if (opt == TCPOPT_NOP)
2233 if (optlen < 2 || optlen > cnt)
2238 mss = (u_int16_t *)(optp + 2);
2239 if ((ntohs(*mss)) > r->max_mss) {
2240 th->th_sum = pf_cksum_fixup(th->th_sum,
2241 *mss, htons(r->max_mss), 0);
2242 *mss = htons(r->max_mss);
2252 m_copyback(m, off + sizeof(*th), thoff - sizeof(*th), opts);
2259 pf_scrub_ip(struct mbuf **m0, u_int32_t flags, u_int8_t min_ttl, u_int8_t tos)
2261 struct mbuf *m = *m0;
2262 struct ip *h = mtod(m, struct ip *);
2264 /* Clear IP_DF if no-df was requested */
2265 if (flags & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
2266 u_int16_t ip_off = h->ip_off;
2268 h->ip_off &= htons(~IP_DF);
2269 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
2272 /* Enforce a minimum ttl, may cause endless packet loops */
2273 if (min_ttl && h->ip_ttl < min_ttl) {
2274 u_int16_t ip_ttl = h->ip_ttl;
2276 h->ip_ttl = min_ttl;
2277 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
2281 if (flags & PFRULE_SET_TOS) {
2284 ov = *(u_int16_t *)h;
2286 nv = *(u_int16_t *)h;
2288 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
2291 /* random-id, but not for fragments */
2292 if (flags & PFRULE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) {
2293 u_int16_t ip_id = h->ip_id;
2295 h->ip_id = ip_randomid();
2296 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
2303 pf_scrub_ip6(struct mbuf **m0, u_int8_t min_ttl)
2305 struct mbuf *m = *m0;
2306 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
2308 /* Enforce a minimum ttl, may cause endless packet loops */
2309 if (min_ttl && h->ip6_hlim < min_ttl)
2310 h->ip6_hlim = min_ttl;