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>
37 #include <sys/kernel.h>
40 #include <sys/mutex.h>
41 #include <sys/refcount.h>
42 #include <sys/rwlock.h>
43 #include <sys/socket.h>
47 #include <net/pfvar.h>
48 #include <net/if_pflog.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_seq.h>
59 #include <netinet/ip6.h>
63 TAILQ_ENTRY(pf_frent) fr_next;
65 uint16_t fe_hdrlen; /* ipv4 header lenght with ip options
66 ipv6, extension, fragment header */
67 uint16_t fe_extoff; /* last extension header offset or 0 */
68 uint16_t fe_len; /* fragment length */
69 uint16_t fe_off; /* fragment offset */
70 uint16_t fe_mff; /* more fragment flag */
73 struct pf_fragment_cmp {
74 struct pf_addr frc_src;
75 struct pf_addr frc_dst;
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
89 RB_ENTRY(pf_fragment) fr_entry;
90 TAILQ_ENTRY(pf_fragment) frag_next;
91 uint8_t fr_flags; /* status flags */
92 #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */
93 #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */
94 #define PFFRAG_DROP 0x0004 /* Drop all fragments */
95 #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER))
96 uint16_t fr_max; /* fragment data max */
98 uint16_t fr_maxlen; /* maximum length of single fragment */
99 TAILQ_HEAD(pf_fragq, pf_frent) fr_queue;
102 struct pf_fragment_tag {
103 uint16_t ft_hdrlen; /* header length of reassembled pkt */
104 uint16_t ft_extoff; /* last extension header offset or 0 */
105 uint16_t ft_maxlen; /* maximum fragment payload length */
106 uint32_t ft_id; /* fragment id */
109 static struct mtx pf_frag_mtx;
110 MTX_SYSINIT(pf_frag_mtx, &pf_frag_mtx, "pf fragments", MTX_DEF);
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 static void pf_flush_fragments(void);
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);
143 static struct pf_frent *pf_create_fragment(u_short *);
144 static struct pf_fragment *pf_find_fragment(struct pf_fragment_cmp *key,
145 struct pf_frag_tree *tree);
146 static struct pf_fragment *pf_fillup_fragment(struct pf_fragment_cmp *,
147 struct pf_frent *, u_short *);
148 static int pf_isfull_fragment(struct pf_fragment *);
149 static struct mbuf *pf_join_fragment(struct pf_fragment *);
151 static void pf_scrub_ip(struct mbuf **, uint32_t, uint8_t, uint8_t);
152 static int pf_reassemble(struct mbuf **, struct ip *, int, u_short *);
153 static struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
154 struct pf_fragment **, int, int, int *);
157 static int pf_reassemble6(struct mbuf **, struct ip6_hdr *,
158 struct ip6_frag *, uint16_t, uint16_t, u_short *);
159 static void pf_scrub_ip6(struct mbuf **, uint8_t);
162 #define DPFPRINTF(x) do { \
163 if (V_pf_status.debug >= PF_DEBUG_MISC) { \
164 printf("%s: ", __func__); \
171 pf_ip2key(struct ip *ip, int dir, struct pf_fragment_cmp *key)
174 key->frc_src.v4 = ip->ip_src;
175 key->frc_dst.v4 = ip->ip_dst;
176 key->frc_af = AF_INET;
177 key->frc_proto = ip->ip_p;
178 key->frc_id = ip->ip_id;
183 pf_normalize_init(void)
186 V_pf_frag_z = uma_zcreate("pf frags", sizeof(struct pf_fragment),
187 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
188 V_pf_frent_z = uma_zcreate("pf frag entries", sizeof(struct pf_frent),
189 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
190 V_pf_state_scrub_z = uma_zcreate("pf state scrubs",
191 sizeof(struct pf_state_scrub), NULL, NULL, NULL, NULL,
194 V_pf_limits[PF_LIMIT_FRAGS].zone = V_pf_frent_z;
195 V_pf_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
196 uma_zone_set_max(V_pf_frent_z, PFFRAG_FRENT_HIWAT);
197 uma_zone_set_warning(V_pf_frent_z, "PF frag entries limit reached");
199 TAILQ_INIT(&V_pf_fragqueue);
200 TAILQ_INIT(&V_pf_cachequeue);
204 pf_normalize_cleanup(void)
207 uma_zdestroy(V_pf_state_scrub_z);
208 uma_zdestroy(V_pf_frent_z);
209 uma_zdestroy(V_pf_frag_z);
213 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
217 if ((diff = a->fr_id - b->fr_id) != 0)
219 if ((diff = a->fr_proto - b->fr_proto) != 0)
221 if ((diff = a->fr_af - b->fr_af) != 0)
223 if ((diff = pf_addr_cmp(&a->fr_src, &b->fr_src, a->fr_af)) != 0)
225 if ((diff = pf_addr_cmp(&a->fr_dst, &b->fr_dst, a->fr_af)) != 0)
231 pf_purge_expired_fragments(void)
233 struct pf_fragment *frag;
234 u_int32_t expire = time_uptime -
235 V_pf_default_rule.timeout[PFTM_FRAG];
238 while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) {
239 KASSERT((BUFFER_FRAGMENTS(frag)),
240 ("BUFFER_FRAGMENTS(frag) == 0: %s", __FUNCTION__));
241 if (frag->fr_timeout > expire)
244 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
245 pf_free_fragment(frag);
248 while ((frag = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue)) != NULL) {
249 KASSERT((!BUFFER_FRAGMENTS(frag)),
250 ("BUFFER_FRAGMENTS(frag) != 0: %s", __FUNCTION__));
251 if (frag->fr_timeout > expire)
254 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
255 pf_free_fragment(frag);
256 KASSERT((TAILQ_EMPTY(&V_pf_cachequeue) ||
257 TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue) != frag),
258 ("!(TAILQ_EMPTY() || TAILQ_LAST() == farg): %s",
265 * Try to flush old fragments to make space for new ones
268 pf_flush_fragments(void)
270 struct pf_fragment *frag, *cache;
275 goal = uma_zone_get_cur(V_pf_frent_z) * 9 / 10;
276 DPFPRINTF(("trying to free %d frag entriess\n", goal));
277 while (goal < uma_zone_get_cur(V_pf_frent_z)) {
278 frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue);
280 pf_free_fragment(frag);
281 cache = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue);
283 pf_free_fragment(cache);
284 if (frag == NULL && cache == NULL)
289 /* Frees the fragments and all associated entries */
291 pf_free_fragment(struct pf_fragment *frag)
293 struct pf_frent *frent;
297 /* Free all fragments */
298 if (BUFFER_FRAGMENTS(frag)) {
299 for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
300 frent = TAILQ_FIRST(&frag->fr_queue)) {
301 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
303 m_freem(frent->fe_m);
304 uma_zfree(V_pf_frent_z, frent);
307 for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
308 frent = TAILQ_FIRST(&frag->fr_queue)) {
309 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
311 KASSERT((TAILQ_EMPTY(&frag->fr_queue) ||
312 TAILQ_FIRST(&frag->fr_queue)->fe_off >
314 ("! (TAILQ_EMPTY() || TAILQ_FIRST()->fe_off >"
315 " frent->fe_len): %s", __func__));
317 uma_zfree(V_pf_frent_z, frent);
321 pf_remove_fragment(frag);
324 static struct pf_fragment *
325 pf_find_fragment(struct pf_fragment_cmp *key, struct pf_frag_tree *tree)
327 struct pf_fragment *frag;
331 frag = RB_FIND(pf_frag_tree, tree, (struct pf_fragment *)key);
333 /* XXX Are we sure we want to update the timeout? */
334 frag->fr_timeout = time_uptime;
335 if (BUFFER_FRAGMENTS(frag)) {
336 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
337 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
339 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
340 TAILQ_INSERT_HEAD(&V_pf_cachequeue, frag, frag_next);
347 /* Removes a fragment from the fragment queue and frees the fragment */
349 pf_remove_fragment(struct pf_fragment *frag)
354 if (BUFFER_FRAGMENTS(frag)) {
355 RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag);
356 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
357 uma_zfree(V_pf_frag_z, frag);
359 RB_REMOVE(pf_frag_tree, &V_pf_cache_tree, frag);
360 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
361 uma_zfree(V_pf_frag_z, frag);
365 static struct pf_frent *
366 pf_create_fragment(u_short *reason)
368 struct pf_frent *frent;
372 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
374 pf_flush_fragments();
375 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
377 REASON_SET(reason, PFRES_MEMORY);
385 static struct pf_fragment *
386 pf_fillup_fragment(struct pf_fragment_cmp *key, struct pf_frent *frent,
389 struct pf_frent *after, *next, *prev;
390 struct pf_fragment *frag;
395 /* No empty fragments. */
396 if (frent->fe_len == 0) {
397 DPFPRINTF(("bad fragment: len 0"));
401 /* All fragments are 8 byte aligned. */
402 if (frent->fe_mff && (frent->fe_len & 0x7)) {
403 DPFPRINTF(("bad fragment: mff and len %d", frent->fe_len));
407 /* Respect maximum length, IP_MAXPACKET == IPV6_MAXPACKET. */
408 if (frent->fe_off + frent->fe_len > IP_MAXPACKET) {
409 DPFPRINTF(("bad fragment: max packet %d",
410 frent->fe_off + frent->fe_len));
414 DPFPRINTF((key->frc_af == AF_INET ?
415 "reass frag %d @ %d-%d" : "reass frag %#08x @ %d-%d",
416 key->frc_id, frent->fe_off, frent->fe_off + frent->fe_len));
418 /* Fully buffer all of the fragments in this fragment queue. */
419 frag = pf_find_fragment(key, &V_pf_frag_tree);
421 /* Create a new reassembly queue for this packet. */
423 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
425 pf_flush_fragments();
426 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
428 REASON_SET(reason, PFRES_MEMORY);
433 *(struct pf_fragment_cmp *)frag = *key;
434 frag->fr_timeout = time_second;
435 frag->fr_maxlen = frent->fe_len;
436 TAILQ_INIT(&frag->fr_queue);
438 RB_INSERT(pf_frag_tree, &V_pf_frag_tree, frag);
439 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
441 /* We do not have a previous fragment. */
442 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
447 KASSERT(!TAILQ_EMPTY(&frag->fr_queue), ("!TAILQ_EMPTY()->fr_queue"));
449 /* Remember maximum fragment len for refragmentation. */
450 if (frent->fe_len > frag->fr_maxlen)
451 frag->fr_maxlen = frent->fe_len;
453 /* Maximum data we have seen already. */
454 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
455 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
457 /* Non terminal fragments must have more fragments flag. */
458 if (frent->fe_off + frent->fe_len < total && !frent->fe_mff)
461 /* Check if we saw the last fragment already. */
462 if (!TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff) {
463 if (frent->fe_off + frent->fe_len > total ||
464 (frent->fe_off + frent->fe_len == total && frent->fe_mff))
467 if (frent->fe_off + frent->fe_len == total && !frent->fe_mff)
471 /* Find a fragment after the current one. */
473 TAILQ_FOREACH(after, &frag->fr_queue, fr_next) {
474 if (after->fe_off > frent->fe_off)
479 KASSERT(prev != NULL || after != NULL,
480 ("prev != NULL || after != NULL"));
482 if (prev != NULL && prev->fe_off + prev->fe_len > frent->fe_off) {
485 precut = prev->fe_off + prev->fe_len - frent->fe_off;
486 if (precut >= frent->fe_len)
488 DPFPRINTF(("overlap -%d", precut));
489 m_adj(frent->fe_m, precut);
490 frent->fe_off += precut;
491 frent->fe_len -= precut;
494 for (; after != NULL && frent->fe_off + frent->fe_len > after->fe_off;
498 aftercut = frent->fe_off + frent->fe_len - after->fe_off;
499 DPFPRINTF(("adjust overlap %d", aftercut));
500 if (aftercut < after->fe_len) {
501 m_adj(after->fe_m, aftercut);
502 after->fe_off += aftercut;
503 after->fe_len -= aftercut;
507 /* This fragment is completely overlapped, lose it. */
508 next = TAILQ_NEXT(after, fr_next);
509 m_freem(after->fe_m);
510 TAILQ_REMOVE(&frag->fr_queue, after, fr_next);
511 uma_zfree(V_pf_frent_z, after);
515 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
517 TAILQ_INSERT_AFTER(&frag->fr_queue, prev, frent, fr_next);
522 REASON_SET(reason, PFRES_FRAG);
524 uma_zfree(V_pf_frent_z, frent);
529 pf_isfull_fragment(struct pf_fragment *frag)
531 struct pf_frent *frent, *next;
534 /* Check if we are completely reassembled */
535 if (TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff)
538 /* Maximum data we have seen already */
539 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
540 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
542 /* Check if we have all the data */
544 for (frent = TAILQ_FIRST(&frag->fr_queue); frent; frent = next) {
545 next = TAILQ_NEXT(frent, fr_next);
547 off += frent->fe_len;
548 if (off < total && (next == NULL || next->fe_off != off)) {
549 DPFPRINTF(("missing fragment at %d, next %d, total %d",
550 off, next == NULL ? -1 : next->fe_off, total));
554 DPFPRINTF(("%d < %d?", off, total));
557 KASSERT(off == total, ("off == total"));
563 pf_join_fragment(struct pf_fragment *frag)
566 struct pf_frent *frent, *next;
568 frent = TAILQ_FIRST(&frag->fr_queue);
569 next = TAILQ_NEXT(frent, fr_next);
572 m_adj(m, (frent->fe_hdrlen + frent->fe_len) - m->m_pkthdr.len);
573 uma_zfree(V_pf_frent_z, frent);
574 for (frent = next; frent != NULL; frent = next) {
575 next = TAILQ_NEXT(frent, fr_next);
578 /* Strip off ip header. */
579 m_adj(m2, frent->fe_hdrlen);
580 /* Strip off any trailing bytes. */
581 m_adj(m2, frent->fe_len - m2->m_pkthdr.len);
583 uma_zfree(V_pf_frent_z, frent);
587 /* Remove from fragment queue. */
588 pf_remove_fragment(frag);
595 pf_reassemble(struct mbuf **m0, struct ip *ip, int dir, u_short *reason)
597 struct mbuf *m = *m0;
598 struct pf_frent *frent;
599 struct pf_fragment *frag;
600 struct pf_fragment_cmp key;
601 uint16_t total, hdrlen;
603 /* Get an entry for the fragment queue */
604 if ((frent = pf_create_fragment(reason)) == NULL)
608 frent->fe_hdrlen = ip->ip_hl << 2;
609 frent->fe_extoff = 0;
610 frent->fe_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
611 frent->fe_off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
612 frent->fe_mff = ntohs(ip->ip_off) & IP_MF;
614 pf_ip2key(ip, dir, &key);
616 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL)
619 /* The mbuf is part of the fragment entry, no direct free or access */
622 if (!pf_isfull_fragment(frag))
623 return (PF_PASS); /* drop because *m0 is NULL, no error */
625 /* We have all the data */
626 frent = TAILQ_FIRST(&frag->fr_queue);
627 KASSERT(frent != NULL, ("frent != NULL"));
628 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
629 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
630 hdrlen = frent->fe_hdrlen;
632 m = *m0 = pf_join_fragment(frag);
635 if (m->m_flags & M_PKTHDR) {
637 for (m = *m0; m; m = m->m_next)
640 m->m_pkthdr.len = plen;
643 ip = mtod(m, struct ip *);
644 ip->ip_len = htons(hdrlen + total);
645 ip->ip_off &= ~(IP_MF|IP_OFFMASK);
647 if (hdrlen + total > IP_MAXPACKET) {
648 DPFPRINTF(("drop: too big: %d", total));
650 REASON_SET(reason, PFRES_SHORT);
651 /* PF_DROP requires a valid mbuf *m0 in pf_test() */
655 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
662 pf_reassemble6(struct mbuf **m0, struct ip6_hdr *ip6, struct ip6_frag *fraghdr,
663 uint16_t hdrlen, uint16_t extoff, u_short *reason)
665 struct mbuf *m = *m0;
666 struct pf_frent *frent;
667 struct pf_fragment *frag;
668 struct pf_fragment_cmp key;
670 struct pf_fragment_tag *ftag;
673 uint16_t total, maxlen;
678 /* Get an entry for the fragment queue. */
679 if ((frent = pf_create_fragment(reason)) == NULL) {
685 frent->fe_hdrlen = hdrlen;
686 frent->fe_extoff = extoff;
687 frent->fe_len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - hdrlen;
688 frent->fe_off = ntohs(fraghdr->ip6f_offlg & IP6F_OFF_MASK);
689 frent->fe_mff = fraghdr->ip6f_offlg & IP6F_MORE_FRAG;
691 key.frc_src.v6 = ip6->ip6_src;
692 key.frc_dst.v6 = ip6->ip6_dst;
693 key.frc_af = AF_INET6;
694 /* Only the first fragment's protocol is relevant. */
696 key.frc_id = fraghdr->ip6f_ident;
698 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) {
703 /* The mbuf is part of the fragment entry, no direct free or access. */
706 if (!pf_isfull_fragment(frag)) {
708 return (PF_PASS); /* Drop because *m0 is NULL, no error. */
711 /* We have all the data. */
712 extoff = frent->fe_extoff;
713 maxlen = frag->fr_maxlen;
714 frag_id = frag->fr_id;
715 frent = TAILQ_FIRST(&frag->fr_queue);
716 KASSERT(frent != NULL, ("frent != NULL"));
717 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
718 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
719 hdrlen = frent->fe_hdrlen - sizeof(struct ip6_frag);
721 m = *m0 = pf_join_fragment(frag);
726 /* Take protocol from first fragment header. */
727 m = m_getptr(m, hdrlen + offsetof(struct ip6_frag, ip6f_nxt), &off);
728 KASSERT(m, ("%s: short mbuf chain", __func__));
729 proto = *(mtod(m, caddr_t) + off);
732 /* Delete frag6 header */
733 if (ip6_deletefraghdr(m, hdrlen, M_NOWAIT) != 0)
736 if (m->m_flags & M_PKTHDR) {
738 for (m = *m0; m; m = m->m_next)
741 m->m_pkthdr.len = plen;
744 if ((mtag = m_tag_get(PF_REASSEMBLED, sizeof(struct pf_fragment_tag),
747 ftag = (struct pf_fragment_tag *)(mtag + 1);
748 ftag->ft_hdrlen = hdrlen;
749 ftag->ft_extoff = extoff;
750 ftag->ft_maxlen = maxlen;
751 ftag->ft_id = frag_id;
752 m_tag_prepend(m, mtag);
754 ip6 = mtod(m, struct ip6_hdr *);
755 ip6->ip6_plen = htons(hdrlen - sizeof(struct ip6_hdr) + total);
757 /* Write protocol into next field of last extension header. */
758 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
760 KASSERT(m, ("%s: short mbuf chain", __func__));
761 *(mtod(m, char *) + off) = proto;
764 ip6->ip6_nxt = proto;
766 if (hdrlen - sizeof(struct ip6_hdr) + total > IPV6_MAXPACKET) {
767 DPFPRINTF(("drop: too big: %d", total));
769 REASON_SET(reason, PFRES_SHORT);
770 /* PF_DROP requires a valid mbuf *m0 in pf_test6(). */
774 DPFPRINTF(("complete: %p(%d)", m, ntohs(ip6->ip6_plen)));
778 REASON_SET(reason, PFRES_MEMORY);
779 /* PF_DROP requires a valid mbuf *m0 in pf_test6(), will free later. */
786 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
787 int drop, int *nomem)
789 struct mbuf *m = *m0;
790 struct pf_frent *frp, *fra, *cur = NULL;
791 int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
792 u_int16_t off = ntohs(h->ip_off) << 3;
793 u_int16_t max = ip_len + off;
797 KASSERT((*frag == NULL || !BUFFER_FRAGMENTS(*frag)),
798 ("!(*frag == NULL || !BUFFER_FRAGMENTS(*frag)): %s", __FUNCTION__));
800 /* Create a new range queue for this packet */
802 *frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
804 pf_flush_fragments();
805 *frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
810 /* Get an entry for the queue */
811 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
813 uma_zfree(V_pf_frag_z, *frag);
818 (*frag)->fr_flags = PFFRAG_NOBUFFER;
820 (*frag)->fr_src.v4 = h->ip_src;
821 (*frag)->fr_dst.v4 = h->ip_dst;
822 (*frag)->fr_af = AF_INET;
823 (*frag)->fr_proto = h->ip_p;
824 (*frag)->fr_id = h->ip_id;
825 (*frag)->fr_timeout = time_uptime;
828 cur->fe_len = max; /* TODO: fe_len = max - off ? */
829 TAILQ_INIT(&(*frag)->fr_queue);
830 TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
832 RB_INSERT(pf_frag_tree, &V_pf_cache_tree, *frag);
833 TAILQ_INSERT_HEAD(&V_pf_cachequeue, *frag, frag_next);
835 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
841 * Find a fragment after the current one:
842 * - off contains the real shifted offset.
845 TAILQ_FOREACH(fra, &(*frag)->fr_queue, fr_next) {
846 if (fra->fe_off > off)
851 KASSERT((frp != NULL || fra != NULL),
852 ("!(frp != NULL || fra != NULL): %s", __FUNCTION__));
857 precut = frp->fe_len - off;
858 if (precut >= ip_len) {
859 /* Fragment is entirely a duplicate */
860 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
861 h->ip_id, frp->fe_off, frp->fe_len, off, max));
865 /* They are adjacent. Fixup cache entry */
866 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
867 h->ip_id, frp->fe_off, frp->fe_len, off, max));
869 } else if (precut > 0) {
870 /* The first part of this payload overlaps with a
871 * fragment that has already been passed.
872 * Need to trim off the first part of the payload.
873 * But to do so easily, we need to create another
874 * mbuf to throw the original header into.
877 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
878 h->ip_id, precut, frp->fe_off, frp->fe_len, off,
883 /* Update the previous frag to encompass this one */
887 /* XXX Optimization opportunity
888 * This is a very heavy way to trim the payload.
889 * we could do it much faster by diddling mbuf
890 * internals but that would be even less legible
891 * than this mbuf magic. For my next trick,
892 * I'll pull a rabbit out of my laptop.
894 *m0 = m_dup(m, M_NOWAIT);
897 /* From KAME Project : We have missed this! */
898 m_adj(*m0, (h->ip_hl << 2) -
899 (*m0)->m_pkthdr.len);
901 KASSERT(((*m0)->m_next == NULL),
902 ("(*m0)->m_next != NULL: %s",
904 m_adj(m, precut + (h->ip_hl << 2));
907 if (m->m_flags & M_PKTHDR) {
910 for (t = m; t; t = t->m_next)
912 m->m_pkthdr.len = plen;
916 h = mtod(m, struct ip *);
918 KASSERT(((int)m->m_len ==
919 ntohs(h->ip_len) - precut),
920 ("m->m_len != ntohs(h->ip_len) - precut: %s",
922 h->ip_off = htons(ntohs(h->ip_off) +
924 h->ip_len = htons(ntohs(h->ip_len) - precut);
929 /* There is a gap between fragments */
931 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
932 h->ip_id, -precut, frp->fe_off, frp->fe_len, off,
935 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
941 TAILQ_INSERT_AFTER(&(*frag)->fr_queue, frp, cur, fr_next);
949 aftercut = max - fra->fe_off;
951 /* Adjacent fragments */
952 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
953 h->ip_id, off, max, fra->fe_off, fra->fe_len));
956 } else if (aftercut > 0) {
957 /* Need to chop off the tail of this fragment */
958 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
959 h->ip_id, aftercut, off, max, fra->fe_off,
968 if (m->m_flags & M_PKTHDR) {
971 for (t = m; t; t = t->m_next)
973 m->m_pkthdr.len = plen;
975 h = mtod(m, struct ip *);
976 KASSERT(((int)m->m_len == ntohs(h->ip_len) - aftercut),
977 ("m->m_len != ntohs(h->ip_len) - aftercut: %s",
979 h->ip_len = htons(ntohs(h->ip_len) - aftercut);
983 } else if (frp == NULL) {
984 /* There is a gap between fragments */
985 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
986 h->ip_id, -aftercut, off, max, fra->fe_off,
989 cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
995 TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
999 /* Need to glue together two separate fragment descriptors */
1001 if (cur && fra->fe_off <= cur->fe_len) {
1002 /* Need to merge in a previous 'cur' */
1003 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1004 "%d-%d) %d-%d (%d-%d)\n",
1005 h->ip_id, cur->fe_off, cur->fe_len, off,
1006 max, fra->fe_off, fra->fe_len));
1007 fra->fe_off = cur->fe_off;
1008 TAILQ_REMOVE(&(*frag)->fr_queue, cur, fr_next);
1009 uma_zfree(V_pf_frent_z, cur);
1012 } else if (frp && fra->fe_off <= frp->fe_len) {
1013 /* Need to merge in a modified 'frp' */
1014 KASSERT((cur == NULL), ("cur != NULL: %s",
1016 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1017 "%d-%d) %d-%d (%d-%d)\n",
1018 h->ip_id, frp->fe_off, frp->fe_len, off,
1019 max, fra->fe_off, fra->fe_len));
1020 fra->fe_off = frp->fe_off;
1021 TAILQ_REMOVE(&(*frag)->fr_queue, frp, fr_next);
1022 uma_zfree(V_pf_frent_z, frp);
1031 * We must keep tracking the overall fragment even when
1032 * we're going to drop it anyway so that we know when to
1033 * free the overall descriptor. Thus we drop the frag late.
1040 /* Update maximum data size */
1041 if ((*frag)->fr_max < max)
1042 (*frag)->fr_max = max;
1044 /* This is the last segment */
1046 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1048 /* Check if we are completely reassembled */
1049 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
1050 TAILQ_FIRST(&(*frag)->fr_queue)->fe_off == 0 &&
1051 TAILQ_FIRST(&(*frag)->fr_queue)->fe_len == (*frag)->fr_max) {
1052 /* Remove from fragment queue */
1053 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
1055 pf_free_fragment(*frag);
1064 /* Still need to pay attention to !IP_MF */
1065 if (!mff && *frag != NULL)
1066 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1073 /* Still need to pay attention to !IP_MF */
1074 if (!mff && *frag != NULL)
1075 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1078 /* This fragment has been deemed bad. Don't reass */
1079 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
1080 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
1082 (*frag)->fr_flags |= PFFRAG_DROP;
1092 pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag)
1094 struct mbuf *m = *m0, *t;
1095 struct pf_fragment_tag *ftag = (struct pf_fragment_tag *)(mtag + 1);
1098 uint16_t hdrlen, extoff, maxlen;
1102 hdrlen = ftag->ft_hdrlen;
1103 extoff = ftag->ft_extoff;
1104 maxlen = ftag->ft_maxlen;
1105 frag_id = ftag->ft_id;
1106 m_tag_delete(m, mtag);
1113 /* Use protocol from next field of last extension header */
1114 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
1116 KASSERT((m != NULL), ("pf_refragment6: short mbuf chain"));
1117 proto = *(mtod(m, caddr_t) + off);
1118 *(mtod(m, char *) + off) = IPPROTO_FRAGMENT;
1121 struct ip6_hdr *hdr;
1123 hdr = mtod(m, struct ip6_hdr *);
1124 proto = hdr->ip6_nxt;
1125 hdr->ip6_nxt = IPPROTO_FRAGMENT;
1129 * Maxlen may be less than 8 if there was only a single
1130 * fragment. As it was fragmented before, add a fragment
1131 * header also for a single fragment. If total or maxlen
1132 * is less than 8, ip6_fragment() will return EMSGSIZE and
1133 * we drop the packet.
1135 error = ip6_fragment(ifp, m, hdrlen, proto, maxlen, frag_id);
1136 m = (*m0)->m_nextpkt;
1137 (*m0)->m_nextpkt = NULL;
1139 /* The first mbuf contains the unfragmented packet. */
1144 /* Drop expects an mbuf to free. */
1145 DPFPRINTF(("refragment error %d", error));
1148 for (t = m; m; m = t) {
1150 m->m_nextpkt = NULL;
1151 m->m_flags |= M_SKIP_FIREWALL;
1152 memset(&pd, 0, sizeof(pd));
1153 pd.pf_mtag = pf_find_mtag(m);
1166 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
1167 struct pf_pdesc *pd)
1169 struct mbuf *m = *m0;
1171 struct pf_fragment *frag = NULL;
1172 struct pf_fragment_cmp key;
1173 struct ip *h = mtod(m, struct ip *);
1174 int mff = (ntohs(h->ip_off) & IP_MF);
1175 int hlen = h->ip_hl << 2;
1176 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1185 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1188 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1189 r = r->skip[PF_SKIP_IFP].ptr;
1190 else if (r->direction && r->direction != dir)
1191 r = r->skip[PF_SKIP_DIR].ptr;
1192 else if (r->af && r->af != AF_INET)
1193 r = r->skip[PF_SKIP_AF].ptr;
1194 else if (r->proto && r->proto != h->ip_p)
1195 r = r->skip[PF_SKIP_PROTO].ptr;
1196 else if (PF_MISMATCHAW(&r->src.addr,
1197 (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
1198 r->src.neg, kif, M_GETFIB(m)))
1199 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1200 else if (PF_MISMATCHAW(&r->dst.addr,
1201 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
1202 r->dst.neg, NULL, M_GETFIB(m)))
1203 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1204 else if (r->match_tag && !pf_match_tag(m, r, &tag,
1205 pd->pf_mtag ? pd->pf_mtag->tag : 0))
1206 r = TAILQ_NEXT(r, entries);
1211 if (r == NULL || r->action == PF_NOSCRUB)
1214 r->packets[dir == PF_OUT]++;
1215 r->bytes[dir == PF_OUT] += pd->tot_len;
1218 /* Check for illegal packets */
1219 if (hlen < (int)sizeof(struct ip))
1222 if (hlen > ntohs(h->ip_len))
1225 /* Clear IP_DF if the rule uses the no-df option */
1226 if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
1227 u_int16_t ip_off = h->ip_off;
1229 h->ip_off &= htons(~IP_DF);
1230 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1233 /* We will need other tests here */
1234 if (!fragoff && !mff)
1237 /* We're dealing with a fragment now. Don't allow fragments
1238 * with IP_DF to enter the cache. If the flag was cleared by
1239 * no-df above, fine. Otherwise drop it.
1241 if (h->ip_off & htons(IP_DF)) {
1242 DPFPRINTF(("IP_DF\n"));
1246 ip_len = ntohs(h->ip_len) - hlen;
1247 ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1249 /* All fragments are 8 byte aligned */
1250 if (mff && (ip_len & 0x7)) {
1251 DPFPRINTF(("mff and %d\n", ip_len));
1255 /* Respect maximum length */
1256 if (fragoff + ip_len > IP_MAXPACKET) {
1257 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
1260 max = fragoff + ip_len;
1262 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
1264 /* Fully buffer all of the fragments */
1267 pf_ip2key(h, dir, &key);
1268 frag = pf_find_fragment(&key, &V_pf_frag_tree);
1270 /* Check if we saw the last fragment already */
1271 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1275 /* Might return a completely reassembled mbuf, or NULL */
1276 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
1277 verdict = pf_reassemble(m0, h, dir, reason);
1280 if (verdict != PF_PASS)
1287 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1290 h = mtod(m, struct ip *);
1292 /* non-buffering fragment cache (drops or masks overlaps) */
1295 if (dir == PF_OUT && pd->pf_mtag &&
1296 pd->pf_mtag->flags & PF_TAG_FRAGCACHE) {
1298 * Already passed the fragment cache in the
1299 * input direction. If we continued, it would
1300 * appear to be a dup and would be dropped.
1306 pf_ip2key(h, dir, &key);
1307 frag = pf_find_fragment(&key, &V_pf_cache_tree);
1309 /* Check if we saw the last fragment already */
1310 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1311 max > frag->fr_max) {
1312 if (r->rule_flag & PFRULE_FRAGDROP)
1313 frag->fr_flags |= PFFRAG_DROP;
1317 *m0 = m = pf_fragcache(m0, h, &frag, mff,
1318 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
1327 /* Use mtag from copied and trimmed mbuf chain. */
1328 pd->pf_mtag = pf_get_mtag(m);
1329 if (pd->pf_mtag == NULL) {
1334 pd->pf_mtag->flags |= PF_TAG_FRAGCACHE;
1337 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1343 /* At this point, only IP_DF is allowed in ip_off */
1344 if (h->ip_off & ~htons(IP_DF)) {
1345 u_int16_t ip_off = h->ip_off;
1347 h->ip_off &= htons(IP_DF);
1348 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1351 /* not missing a return here */
1354 pf_scrub_ip(&m, r->rule_flag, r->min_ttl, r->set_tos);
1356 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1357 pd->flags |= PFDESC_IP_REAS;
1361 REASON_SET(reason, PFRES_MEMORY);
1362 if (r != NULL && r->log)
1363 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1368 REASON_SET(reason, PFRES_NORM);
1369 if (r != NULL && r->log)
1370 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1375 DPFPRINTF(("dropping bad fragment\n"));
1377 /* Free associated fragments */
1379 pf_free_fragment(frag);
1383 REASON_SET(reason, PFRES_FRAG);
1384 if (r != NULL && r->log)
1385 PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1394 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1395 u_short *reason, struct pf_pdesc *pd)
1397 struct mbuf *m = *m0;
1399 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1404 struct ip6_opt_jumbo jumbo;
1405 struct ip6_frag frag;
1406 u_int32_t jumbolen = 0, plen;
1414 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1417 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1418 r = r->skip[PF_SKIP_IFP].ptr;
1419 else if (r->direction && r->direction != dir)
1420 r = r->skip[PF_SKIP_DIR].ptr;
1421 else if (r->af && r->af != AF_INET6)
1422 r = r->skip[PF_SKIP_AF].ptr;
1423 #if 0 /* header chain! */
1424 else if (r->proto && r->proto != h->ip6_nxt)
1425 r = r->skip[PF_SKIP_PROTO].ptr;
1427 else if (PF_MISMATCHAW(&r->src.addr,
1428 (struct pf_addr *)&h->ip6_src, AF_INET6,
1429 r->src.neg, kif, M_GETFIB(m)))
1430 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1431 else if (PF_MISMATCHAW(&r->dst.addr,
1432 (struct pf_addr *)&h->ip6_dst, AF_INET6,
1433 r->dst.neg, NULL, M_GETFIB(m)))
1434 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1439 if (r == NULL || r->action == PF_NOSCRUB)
1442 r->packets[dir == PF_OUT]++;
1443 r->bytes[dir == PF_OUT] += pd->tot_len;
1446 /* Check for illegal packets */
1447 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1451 off = sizeof(struct ip6_hdr);
1456 case IPPROTO_FRAGMENT:
1460 case IPPROTO_ROUTING:
1461 case IPPROTO_DSTOPTS:
1462 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1466 if (proto == IPPROTO_AH)
1467 off += (ext.ip6e_len + 2) * 4;
1469 off += (ext.ip6e_len + 1) * 8;
1470 proto = ext.ip6e_nxt;
1472 case IPPROTO_HOPOPTS:
1473 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1477 optend = off + (ext.ip6e_len + 1) * 8;
1478 ooff = off + sizeof(ext);
1480 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1481 sizeof(opt.ip6o_type), NULL, NULL,
1484 if (opt.ip6o_type == IP6OPT_PAD1) {
1488 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1489 NULL, NULL, AF_INET6))
1491 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1493 switch (opt.ip6o_type) {
1495 if (h->ip6_plen != 0)
1497 if (!pf_pull_hdr(m, ooff, &jumbo,
1498 sizeof(jumbo), NULL, NULL,
1501 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1503 jumbolen = ntohl(jumbolen);
1504 if (jumbolen <= IPV6_MAXPACKET)
1506 if (sizeof(struct ip6_hdr) + jumbolen !=
1513 ooff += sizeof(opt) + opt.ip6o_len;
1514 } while (ooff < optend);
1517 proto = ext.ip6e_nxt;
1523 } while (!terminal);
1525 /* jumbo payload option must be present, or plen > 0 */
1526 if (ntohs(h->ip6_plen) == 0)
1529 plen = ntohs(h->ip6_plen);
1532 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1535 pf_scrub_ip6(&m, r->min_ttl);
1540 /* Jumbo payload packets cannot be fragmented. */
1541 plen = ntohs(h->ip6_plen);
1542 if (plen == 0 || jumbolen)
1544 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1547 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1550 /* Offset now points to data portion. */
1551 off += sizeof(frag);
1553 /* Returns PF_DROP or *m0 is NULL or completely reassembled mbuf. */
1554 if (pf_reassemble6(m0, h, &frag, off, extoff, reason) != PF_PASS)
1560 pd->flags |= PFDESC_IP_REAS;
1564 REASON_SET(reason, PFRES_SHORT);
1565 if (r != NULL && r->log)
1566 PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1571 REASON_SET(reason, PFRES_NORM);
1572 if (r != NULL && r->log)
1573 PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1580 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1581 int off, void *h, struct pf_pdesc *pd)
1583 struct pf_rule *r, *rm = NULL;
1584 struct tcphdr *th = pd->hdr.tcp;
1588 sa_family_t af = pd->af;
1592 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1595 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1596 r = r->skip[PF_SKIP_IFP].ptr;
1597 else if (r->direction && r->direction != dir)
1598 r = r->skip[PF_SKIP_DIR].ptr;
1599 else if (r->af && r->af != af)
1600 r = r->skip[PF_SKIP_AF].ptr;
1601 else if (r->proto && r->proto != pd->proto)
1602 r = r->skip[PF_SKIP_PROTO].ptr;
1603 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1604 r->src.neg, kif, M_GETFIB(m)))
1605 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1606 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1607 r->src.port[0], r->src.port[1], th->th_sport))
1608 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1609 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1610 r->dst.neg, NULL, M_GETFIB(m)))
1611 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1612 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1613 r->dst.port[0], r->dst.port[1], th->th_dport))
1614 r = r->skip[PF_SKIP_DST_PORT].ptr;
1615 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1616 pf_osfp_fingerprint(pd, m, off, th),
1618 r = TAILQ_NEXT(r, entries);
1625 if (rm == NULL || rm->action == PF_NOSCRUB)
1628 r->packets[dir == PF_OUT]++;
1629 r->bytes[dir == PF_OUT] += pd->tot_len;
1632 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1633 pd->flags |= PFDESC_TCP_NORM;
1635 flags = th->th_flags;
1636 if (flags & TH_SYN) {
1637 /* Illegal packet */
1644 /* Illegal packet */
1645 if (!(flags & (TH_ACK|TH_RST)))
1649 if (!(flags & TH_ACK)) {
1650 /* These flags are only valid if ACK is set */
1651 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1655 /* Check for illegal header length */
1656 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1659 /* If flags changed, or reserved data set, then adjust */
1660 if (flags != th->th_flags || th->th_x2 != 0) {
1663 ov = *(u_int16_t *)(&th->th_ack + 1);
1664 th->th_flags = flags;
1666 nv = *(u_int16_t *)(&th->th_ack + 1);
1668 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1672 /* Remove urgent pointer, if TH_URG is not set */
1673 if (!(flags & TH_URG) && th->th_urp) {
1674 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1679 /* Process options */
1680 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af))
1683 /* copy back packet headers if we sanitized */
1685 m_copyback(m, off, sizeof(*th), (caddr_t)th);
1690 REASON_SET(&reason, PFRES_NORM);
1691 if (rm != NULL && r->log)
1692 PFLOG_PACKET(kif, m, AF_INET, dir, reason, r, NULL, NULL, pd,
1698 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1699 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1701 u_int32_t tsval, tsecr;
1705 KASSERT((src->scrub == NULL),
1706 ("pf_normalize_tcp_init: src->scrub != NULL"));
1708 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1709 if (src->scrub == NULL)
1715 struct ip *h = mtod(m, struct ip *);
1716 src->scrub->pfss_ttl = h->ip_ttl;
1722 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1723 src->scrub->pfss_ttl = h->ip6_hlim;
1731 * All normalizations below are only begun if we see the start of
1732 * the connections. They must all set an enabled bit in pfss_flags
1734 if ((th->th_flags & TH_SYN) == 0)
1738 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1739 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1740 /* Diddle with TCP options */
1742 opt = hdr + sizeof(struct tcphdr);
1743 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1744 while (hlen >= TCPOLEN_TIMESTAMP) {
1746 case TCPOPT_EOL: /* FALLTHROUGH */
1751 case TCPOPT_TIMESTAMP:
1752 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1753 src->scrub->pfss_flags |=
1755 src->scrub->pfss_ts_mod =
1756 htonl(arc4random());
1758 /* note PFSS_PAWS not set yet */
1759 memcpy(&tsval, &opt[2],
1761 memcpy(&tsecr, &opt[6],
1763 src->scrub->pfss_tsval0 = ntohl(tsval);
1764 src->scrub->pfss_tsval = ntohl(tsval);
1765 src->scrub->pfss_tsecr = ntohl(tsecr);
1766 getmicrouptime(&src->scrub->pfss_last);
1770 hlen -= MAX(opt[1], 2);
1771 opt += MAX(opt[1], 2);
1781 pf_normalize_tcp_cleanup(struct pf_state *state)
1783 if (state->src.scrub)
1784 uma_zfree(V_pf_state_scrub_z, state->src.scrub);
1785 if (state->dst.scrub)
1786 uma_zfree(V_pf_state_scrub_z, state->dst.scrub);
1788 /* Someday... flush the TCP segment reassembly descriptors. */
1792 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1793 u_short *reason, struct tcphdr *th, struct pf_state *state,
1794 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1796 struct timeval uptime;
1797 u_int32_t tsval, tsecr;
1798 u_int tsval_from_last;
1804 KASSERT((src->scrub || dst->scrub),
1805 ("%s: src->scrub && dst->scrub!", __func__));
1808 * Enforce the minimum TTL seen for this connection. Negate a common
1809 * technique to evade an intrusion detection system and confuse
1810 * firewall state code.
1816 struct ip *h = mtod(m, struct ip *);
1817 if (h->ip_ttl > src->scrub->pfss_ttl)
1818 src->scrub->pfss_ttl = h->ip_ttl;
1819 h->ip_ttl = src->scrub->pfss_ttl;
1827 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1828 if (h->ip6_hlim > src->scrub->pfss_ttl)
1829 src->scrub->pfss_ttl = h->ip6_hlim;
1830 h->ip6_hlim = src->scrub->pfss_ttl;
1837 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1838 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1839 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1840 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1841 /* Diddle with TCP options */
1843 opt = hdr + sizeof(struct tcphdr);
1844 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1845 while (hlen >= TCPOLEN_TIMESTAMP) {
1847 case TCPOPT_EOL: /* FALLTHROUGH */
1852 case TCPOPT_TIMESTAMP:
1853 /* Modulate the timestamps. Can be used for
1854 * NAT detection, OS uptime determination or
1859 /* Huh? Multiple timestamps!? */
1860 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1861 DPFPRINTF(("multiple TS??"));
1862 pf_print_state(state);
1865 REASON_SET(reason, PFRES_TS);
1868 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1869 memcpy(&tsval, &opt[2],
1871 if (tsval && src->scrub &&
1872 (src->scrub->pfss_flags &
1874 tsval = ntohl(tsval);
1875 pf_change_a(&opt[2],
1878 src->scrub->pfss_ts_mod),
1883 /* Modulate TS reply iff valid (!0) */
1884 memcpy(&tsecr, &opt[6],
1886 if (tsecr && dst->scrub &&
1887 (dst->scrub->pfss_flags &
1889 tsecr = ntohl(tsecr)
1890 - dst->scrub->pfss_ts_mod;
1891 pf_change_a(&opt[6],
1892 &th->th_sum, htonl(tsecr),
1900 hlen -= MAX(opt[1], 2);
1901 opt += MAX(opt[1], 2);
1906 /* Copyback the options, caller copys back header */
1908 m_copyback(m, off + sizeof(struct tcphdr),
1909 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1910 sizeof(struct tcphdr));
1916 * Must invalidate PAWS checks on connections idle for too long.
1917 * The fastest allowed timestamp clock is 1ms. That turns out to
1918 * be about 24 days before it wraps. XXX Right now our lowerbound
1919 * TS echo check only works for the first 12 days of a connection
1920 * when the TS has exhausted half its 32bit space
1922 #define TS_MAX_IDLE (24*24*60*60)
1923 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1925 getmicrouptime(&uptime);
1926 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1927 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1928 time_uptime - state->creation > TS_MAX_CONN)) {
1929 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1930 DPFPRINTF(("src idled out of PAWS\n"));
1931 pf_print_state(state);
1934 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1937 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1938 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1939 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1940 DPFPRINTF(("dst idled out of PAWS\n"));
1941 pf_print_state(state);
1944 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1948 if (got_ts && src->scrub && dst->scrub &&
1949 (src->scrub->pfss_flags & PFSS_PAWS) &&
1950 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1951 /* Validate that the timestamps are "in-window".
1952 * RFC1323 describes TCP Timestamp options that allow
1953 * measurement of RTT (round trip time) and PAWS
1954 * (protection against wrapped sequence numbers). PAWS
1955 * gives us a set of rules for rejecting packets on
1956 * long fat pipes (packets that were somehow delayed
1957 * in transit longer than the time it took to send the
1958 * full TCP sequence space of 4Gb). We can use these
1959 * rules and infer a few others that will let us treat
1960 * the 32bit timestamp and the 32bit echoed timestamp
1961 * as sequence numbers to prevent a blind attacker from
1962 * inserting packets into a connection.
1965 * - The timestamp on this packet must be greater than
1966 * or equal to the last value echoed by the other
1967 * endpoint. The RFC says those will be discarded
1968 * since it is a dup that has already been acked.
1969 * This gives us a lowerbound on the timestamp.
1970 * timestamp >= other last echoed timestamp
1971 * - The timestamp will be less than or equal to
1972 * the last timestamp plus the time between the
1973 * last packet and now. The RFC defines the max
1974 * clock rate as 1ms. We will allow clocks to be
1975 * up to 10% fast and will allow a total difference
1976 * or 30 seconds due to a route change. And this
1977 * gives us an upperbound on the timestamp.
1978 * timestamp <= last timestamp + max ticks
1979 * We have to be careful here. Windows will send an
1980 * initial timestamp of zero and then initialize it
1981 * to a random value after the 3whs; presumably to
1982 * avoid a DoS by having to call an expensive RNG
1983 * during a SYN flood. Proof MS has at least one
1984 * good security geek.
1986 * - The TCP timestamp option must also echo the other
1987 * endpoints timestamp. The timestamp echoed is the
1988 * one carried on the earliest unacknowledged segment
1989 * on the left edge of the sequence window. The RFC
1990 * states that the host will reject any echoed
1991 * timestamps that were larger than any ever sent.
1992 * This gives us an upperbound on the TS echo.
1993 * tescr <= largest_tsval
1994 * - The lowerbound on the TS echo is a little more
1995 * tricky to determine. The other endpoint's echoed
1996 * values will not decrease. But there may be
1997 * network conditions that re-order packets and
1998 * cause our view of them to decrease. For now the
1999 * only lowerbound we can safely determine is that
2000 * the TS echo will never be less than the original
2001 * TS. XXX There is probably a better lowerbound.
2002 * Remove TS_MAX_CONN with better lowerbound check.
2003 * tescr >= other original TS
2005 * It is also important to note that the fastest
2006 * timestamp clock of 1ms will wrap its 32bit space in
2007 * 24 days. So we just disable TS checking after 24
2008 * days of idle time. We actually must use a 12d
2009 * connection limit until we can come up with a better
2010 * lowerbound to the TS echo check.
2012 struct timeval delta_ts;
2017 * PFTM_TS_DIFF is how many seconds of leeway to allow
2018 * a host's timestamp. This can happen if the previous
2019 * packet got delayed in transit for much longer than
2022 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
2023 ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF];
2025 /* Calculate max ticks since the last timestamp */
2026 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
2027 #define TS_MICROSECS 1000000 /* microseconds per second */
2029 timevalsub(&delta_ts, &src->scrub->pfss_last);
2030 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
2031 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
2033 if ((src->state >= TCPS_ESTABLISHED &&
2034 dst->state >= TCPS_ESTABLISHED) &&
2035 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
2036 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
2037 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
2038 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
2039 /* Bad RFC1323 implementation or an insertion attack.
2041 * - Solaris 2.6 and 2.7 are known to send another ACK
2042 * after the FIN,FIN|ACK,ACK closing that carries
2046 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
2047 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
2048 SEQ_GT(tsval, src->scrub->pfss_tsval +
2049 tsval_from_last) ? '1' : ' ',
2050 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
2051 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
2052 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u "
2053 "idle: %jus %lums\n",
2054 tsval, tsecr, tsval_from_last,
2055 (uintmax_t)delta_ts.tv_sec,
2056 delta_ts.tv_usec / 1000));
2057 DPFPRINTF((" src->tsval: %u tsecr: %u\n",
2058 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
2059 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u"
2060 "\n", dst->scrub->pfss_tsval,
2061 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
2062 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2063 pf_print_state(state);
2064 pf_print_flags(th->th_flags);
2067 REASON_SET(reason, PFRES_TS);
2071 /* XXX I'd really like to require tsecr but it's optional */
2073 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
2074 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
2075 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
2076 src->scrub && dst->scrub &&
2077 (src->scrub->pfss_flags & PFSS_PAWS) &&
2078 (dst->scrub->pfss_flags & PFSS_PAWS)) {
2079 /* Didn't send a timestamp. Timestamps aren't really useful
2081 * - connection opening or closing (often not even sent).
2082 * but we must not let an attacker to put a FIN on a
2083 * data packet to sneak it through our ESTABLISHED check.
2084 * - on a TCP reset. RFC suggests not even looking at TS.
2085 * - on an empty ACK. The TS will not be echoed so it will
2086 * probably not help keep the RTT calculation in sync and
2087 * there isn't as much danger when the sequence numbers
2088 * got wrapped. So some stacks don't include TS on empty
2091 * To minimize the disruption to mostly RFC1323 conformant
2092 * stacks, we will only require timestamps on data packets.
2094 * And what do ya know, we cannot require timestamps on data
2095 * packets. There appear to be devices that do legitimate
2096 * TCP connection hijacking. There are HTTP devices that allow
2097 * a 3whs (with timestamps) and then buffer the HTTP request.
2098 * If the intermediate device has the HTTP response cache, it
2099 * will spoof the response but not bother timestamping its
2100 * packets. So we can look for the presence of a timestamp in
2101 * the first data packet and if there, require it in all future
2105 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
2107 * Hey! Someone tried to sneak a packet in. Or the
2108 * stack changed its RFC1323 behavior?!?!
2110 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2111 DPFPRINTF(("Did not receive expected RFC1323 "
2113 pf_print_state(state);
2114 pf_print_flags(th->th_flags);
2117 REASON_SET(reason, PFRES_TS);
2124 * We will note if a host sends his data packets with or without
2125 * timestamps. And require all data packets to contain a timestamp
2126 * if the first does. PAWS implicitly requires that all data packets be
2127 * timestamped. But I think there are middle-man devices that hijack
2128 * TCP streams immediately after the 3whs and don't timestamp their
2129 * packets (seen in a WWW accelerator or cache).
2131 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
2132 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
2134 src->scrub->pfss_flags |= PFSS_DATA_TS;
2136 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
2137 if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
2138 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
2139 /* Don't warn if other host rejected RFC1323 */
2140 DPFPRINTF(("Broken RFC1323 stack did not "
2141 "timestamp data packet. Disabled PAWS "
2143 pf_print_state(state);
2144 pf_print_flags(th->th_flags);
2152 * Update PAWS values
2154 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
2155 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
2156 getmicrouptime(&src->scrub->pfss_last);
2157 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
2158 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2159 src->scrub->pfss_tsval = tsval;
2162 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
2163 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2164 src->scrub->pfss_tsecr = tsecr;
2166 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
2167 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
2168 src->scrub->pfss_tsval0 == 0)) {
2169 /* tsval0 MUST be the lowest timestamp */
2170 src->scrub->pfss_tsval0 = tsval;
2173 /* Only fully initialized after a TS gets echoed */
2174 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
2175 src->scrub->pfss_flags |= PFSS_PAWS;
2179 /* I have a dream.... TCP segment reassembly.... */
2184 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
2185 int off, sa_family_t af)
2189 int opt, cnt, optlen = 0;
2191 u_char opts[TCP_MAXOLEN];
2192 u_char *optp = opts;
2194 thoff = th->th_off << 2;
2195 cnt = thoff - sizeof(struct tcphdr);
2197 if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
2201 for (; cnt > 0; cnt -= optlen, optp += optlen) {
2203 if (opt == TCPOPT_EOL)
2205 if (opt == TCPOPT_NOP)
2211 if (optlen < 2 || optlen > cnt)
2216 mss = (u_int16_t *)(optp + 2);
2217 if ((ntohs(*mss)) > r->max_mss) {
2218 th->th_sum = pf_cksum_fixup(th->th_sum,
2219 *mss, htons(r->max_mss), 0);
2220 *mss = htons(r->max_mss);
2230 m_copyback(m, off + sizeof(*th), thoff - sizeof(*th), opts);
2237 pf_scrub_ip(struct mbuf **m0, u_int32_t flags, u_int8_t min_ttl, u_int8_t tos)
2239 struct mbuf *m = *m0;
2240 struct ip *h = mtod(m, struct ip *);
2242 /* Clear IP_DF if no-df was requested */
2243 if (flags & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
2244 u_int16_t ip_off = h->ip_off;
2246 h->ip_off &= htons(~IP_DF);
2247 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
2250 /* Enforce a minimum ttl, may cause endless packet loops */
2251 if (min_ttl && h->ip_ttl < min_ttl) {
2252 u_int16_t ip_ttl = h->ip_ttl;
2254 h->ip_ttl = min_ttl;
2255 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
2259 if (flags & PFRULE_SET_TOS) {
2262 ov = *(u_int16_t *)h;
2264 nv = *(u_int16_t *)h;
2266 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
2269 /* random-id, but not for fragments */
2270 if (flags & PFRULE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) {
2271 uint16_t ip_id = h->ip_id;
2274 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
2281 pf_scrub_ip6(struct mbuf **m0, u_int8_t min_ttl)
2283 struct mbuf *m = *m0;
2284 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
2286 /* Enforce a minimum ttl, may cause endless packet loops */
2287 if (min_ttl && h->ip6_hlim < min_ttl)
2288 h->ip6_hlim = min_ttl;