2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2011-2018 Alexander Bluhm <bluhm@openbsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $OpenBSD: pf_norm.c,v 1.114 2009/01/29 14:11:45 henning Exp $
31 #include <sys/cdefs.h>
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/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 <netinet6/scope6_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_seq.h>
57 #include <netinet/sctp_constants.h>
58 #include <netinet/sctp_header.h>
61 #include <netinet/ip6.h>
65 TAILQ_ENTRY(pf_frent) fr_next;
67 uint16_t fe_hdrlen; /* ipv4 header length with ip options
68 ipv6, extension, fragment header */
69 uint16_t fe_extoff; /* last extension header offset or 0 */
70 uint16_t fe_len; /* fragment length */
71 uint16_t fe_off; /* fragment offset */
72 uint16_t fe_mff; /* more fragment flag */
75 struct pf_fragment_cmp {
76 struct pf_addr frc_src;
77 struct pf_addr frc_dst;
84 struct pf_fragment_cmp fr_key;
85 #define fr_src fr_key.frc_src
86 #define fr_dst fr_key.frc_dst
87 #define fr_id fr_key.frc_id
88 #define fr_af fr_key.frc_af
89 #define fr_proto fr_key.frc_proto
91 /* pointers to queue element */
92 struct pf_frent *fr_firstoff[PF_FRAG_ENTRY_POINTS];
93 /* count entries between pointers */
94 uint8_t fr_entries[PF_FRAG_ENTRY_POINTS];
95 RB_ENTRY(pf_fragment) fr_entry;
96 TAILQ_ENTRY(pf_fragment) frag_next;
98 uint16_t fr_maxlen; /* maximum length of single fragment */
99 u_int16_t fr_holes; /* number of holes in the queue */
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 VNET_DEFINE_STATIC(struct mtx, pf_frag_mtx);
111 #define V_pf_frag_mtx VNET(pf_frag_mtx)
112 #define PF_FRAG_LOCK() mtx_lock(&V_pf_frag_mtx)
113 #define PF_FRAG_UNLOCK() mtx_unlock(&V_pf_frag_mtx)
114 #define PF_FRAG_ASSERT() mtx_assert(&V_pf_frag_mtx, MA_OWNED)
116 VNET_DEFINE(uma_zone_t, pf_state_scrub_z); /* XXX: shared with pfsync */
118 VNET_DEFINE_STATIC(uma_zone_t, pf_frent_z);
119 #define V_pf_frent_z VNET(pf_frent_z)
120 VNET_DEFINE_STATIC(uma_zone_t, pf_frag_z);
121 #define V_pf_frag_z VNET(pf_frag_z)
123 TAILQ_HEAD(pf_fragqueue, pf_fragment);
124 TAILQ_HEAD(pf_cachequeue, pf_fragment);
125 VNET_DEFINE_STATIC(struct pf_fragqueue, pf_fragqueue);
126 #define V_pf_fragqueue VNET(pf_fragqueue)
127 RB_HEAD(pf_frag_tree, pf_fragment);
128 VNET_DEFINE_STATIC(struct pf_frag_tree, pf_frag_tree);
129 #define V_pf_frag_tree VNET(pf_frag_tree)
130 static int pf_frag_compare(struct pf_fragment *,
131 struct pf_fragment *);
132 static RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
133 static RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
135 static void pf_flush_fragments(void);
136 static void pf_free_fragment(struct pf_fragment *);
137 static void pf_remove_fragment(struct pf_fragment *);
139 static struct pf_frent *pf_create_fragment(u_short *);
140 static int pf_frent_holes(struct pf_frent *frent);
141 static struct pf_fragment *pf_find_fragment(struct pf_fragment_cmp *key,
142 struct pf_frag_tree *tree);
143 static inline int pf_frent_index(struct pf_frent *);
144 static int pf_frent_insert(struct pf_fragment *,
145 struct pf_frent *, struct pf_frent *);
146 void pf_frent_remove(struct pf_fragment *,
148 struct pf_frent *pf_frent_previous(struct pf_fragment *,
150 static struct pf_fragment *pf_fillup_fragment(struct pf_fragment_cmp *,
151 struct pf_frent *, u_short *);
152 static struct mbuf *pf_join_fragment(struct pf_fragment *);
154 static int pf_reassemble(struct mbuf **, struct ip *, int, u_short *);
157 static int pf_reassemble6(struct mbuf **, struct ip6_hdr *,
158 struct ip6_frag *, uint16_t, uint16_t, u_short *);
161 #define DPFPRINTF(x) do { \
162 if (V_pf_status.debug >= PF_DEBUG_MISC) { \
163 printf("%s: ", __func__); \
170 pf_ip2key(struct ip *ip, int dir, struct pf_fragment_cmp *key)
173 key->frc_src.v4 = ip->ip_src;
174 key->frc_dst.v4 = ip->ip_dst;
175 key->frc_af = AF_INET;
176 key->frc_proto = ip->ip_p;
177 key->frc_id = ip->ip_id;
182 pf_normalize_init(void)
185 V_pf_frag_z = uma_zcreate("pf frags", sizeof(struct pf_fragment),
186 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
187 V_pf_frent_z = uma_zcreate("pf frag entries", sizeof(struct pf_frent),
188 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
189 V_pf_state_scrub_z = uma_zcreate("pf state scrubs",
190 sizeof(struct pf_state_scrub), NULL, NULL, NULL, NULL,
193 mtx_init(&V_pf_frag_mtx, "pf fragments", NULL, MTX_DEF);
195 V_pf_limits[PF_LIMIT_FRAGS].zone = V_pf_frent_z;
196 V_pf_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
197 uma_zone_set_max(V_pf_frent_z, PFFRAG_FRENT_HIWAT);
198 uma_zone_set_warning(V_pf_frent_z, "PF frag entries limit reached");
200 TAILQ_INIT(&V_pf_fragqueue);
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);
211 mtx_destroy(&V_pf_frag_mtx);
215 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
219 if ((diff = a->fr_id - b->fr_id) != 0)
221 if ((diff = a->fr_proto - b->fr_proto) != 0)
223 if ((diff = a->fr_af - b->fr_af) != 0)
225 if ((diff = pf_addr_cmp(&a->fr_src, &b->fr_src, a->fr_af)) != 0)
227 if ((diff = pf_addr_cmp(&a->fr_dst, &b->fr_dst, a->fr_af)) != 0)
233 pf_purge_expired_fragments(void)
235 u_int32_t expire = time_uptime -
236 V_pf_default_rule.timeout[PFTM_FRAG];
238 pf_purge_fragments(expire);
242 pf_purge_fragments(uint32_t expire)
244 struct pf_fragment *frag;
247 while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) {
248 if (frag->fr_timeout > expire)
251 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
252 pf_free_fragment(frag);
259 * Try to flush old fragments to make space for new ones
262 pf_flush_fragments(void)
264 struct pf_fragment *frag;
269 goal = uma_zone_get_cur(V_pf_frent_z) * 9 / 10;
270 DPFPRINTF(("trying to free %d frag entriess\n", goal));
271 while (goal < uma_zone_get_cur(V_pf_frent_z)) {
272 frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue);
274 pf_free_fragment(frag);
280 /* Frees the fragments and all associated entries */
282 pf_free_fragment(struct pf_fragment *frag)
284 struct pf_frent *frent;
288 /* Free all fragments */
289 for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
290 frent = TAILQ_FIRST(&frag->fr_queue)) {
291 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
293 m_freem(frent->fe_m);
294 uma_zfree(V_pf_frent_z, frent);
297 pf_remove_fragment(frag);
300 static struct pf_fragment *
301 pf_find_fragment(struct pf_fragment_cmp *key, struct pf_frag_tree *tree)
303 struct pf_fragment *frag;
307 frag = RB_FIND(pf_frag_tree, tree, (struct pf_fragment *)key);
309 /* XXX Are we sure we want to update the timeout? */
310 frag->fr_timeout = time_uptime;
311 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
312 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
318 /* Removes a fragment from the fragment queue and frees the fragment */
320 pf_remove_fragment(struct pf_fragment *frag)
324 KASSERT(frag, ("frag != NULL"));
326 RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag);
327 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
328 uma_zfree(V_pf_frag_z, frag);
331 static struct pf_frent *
332 pf_create_fragment(u_short *reason)
334 struct pf_frent *frent;
338 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
340 pf_flush_fragments();
341 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
343 REASON_SET(reason, PFRES_MEMORY);
352 * Calculate the additional holes that were created in the fragment
353 * queue by inserting this fragment. A fragment in the middle
354 * creates one more hole by splitting. For each connected side,
356 * Fragment entry must be in the queue when calling this function.
359 pf_frent_holes(struct pf_frent *frent)
361 struct pf_frent *prev = TAILQ_PREV(frent, pf_fragq, fr_next);
362 struct pf_frent *next = TAILQ_NEXT(frent, fr_next);
366 if (frent->fe_off == 0)
369 KASSERT(frent->fe_off != 0, ("frent->fe_off != 0"));
370 if (frent->fe_off == prev->fe_off + prev->fe_len)
377 KASSERT(frent->fe_mff, ("frent->fe_mff"));
378 if (next->fe_off == frent->fe_off + frent->fe_len)
385 pf_frent_index(struct pf_frent *frent)
388 * We have an array of 16 entry points to the queue. A full size
389 * 65535 octet IP packet can have 8192 fragments. So the queue
390 * traversal length is at most 512 and at most 16 entry points are
391 * checked. We need 128 additional bytes on a 64 bit architecture.
393 CTASSERT(((u_int16_t)0xffff &~ 7) / (0x10000 / PF_FRAG_ENTRY_POINTS) ==
395 CTASSERT(((u_int16_t)0xffff >> 3) / PF_FRAG_ENTRY_POINTS == 512 - 1);
397 return frent->fe_off / (0x10000 / PF_FRAG_ENTRY_POINTS);
401 pf_frent_insert(struct pf_fragment *frag, struct pf_frent *frent,
402 struct pf_frent *prev)
406 CTASSERT(PF_FRAG_ENTRY_LIMIT <= 0xff);
409 * A packet has at most 65536 octets. With 16 entry points, each one
410 * spawns 4096 octets. We limit these to 64 fragments each, which
411 * means on average every fragment must have at least 64 octets.
413 index = pf_frent_index(frent);
414 if (frag->fr_entries[index] >= PF_FRAG_ENTRY_LIMIT)
416 frag->fr_entries[index]++;
419 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
421 KASSERT(prev->fe_off + prev->fe_len <= frent->fe_off,
422 ("overlapping fragment"));
423 TAILQ_INSERT_AFTER(&frag->fr_queue, prev, frent, fr_next);
426 if (frag->fr_firstoff[index] == NULL) {
427 KASSERT(prev == NULL || pf_frent_index(prev) < index,
428 ("prev == NULL || pf_frent_index(pref) < index"));
429 frag->fr_firstoff[index] = frent;
431 if (frent->fe_off < frag->fr_firstoff[index]->fe_off) {
432 KASSERT(prev == NULL || pf_frent_index(prev) < index,
433 ("prev == NULL || pf_frent_index(pref) < index"));
434 frag->fr_firstoff[index] = frent;
436 KASSERT(prev != NULL, ("prev != NULL"));
437 KASSERT(pf_frent_index(prev) == index,
438 ("pf_frent_index(prev) == index"));
442 frag->fr_holes += pf_frent_holes(frent);
448 pf_frent_remove(struct pf_fragment *frag, struct pf_frent *frent)
451 struct pf_frent *prev = TAILQ_PREV(frent, pf_fragq, fr_next);
453 struct pf_frent *next = TAILQ_NEXT(frent, fr_next);
456 frag->fr_holes -= pf_frent_holes(frent);
458 index = pf_frent_index(frent);
459 KASSERT(frag->fr_firstoff[index] != NULL, ("frent not found"));
460 if (frag->fr_firstoff[index]->fe_off == frent->fe_off) {
462 frag->fr_firstoff[index] = NULL;
464 KASSERT(frent->fe_off + frent->fe_len <= next->fe_off,
465 ("overlapping fragment"));
466 if (pf_frent_index(next) == index) {
467 frag->fr_firstoff[index] = next;
469 frag->fr_firstoff[index] = NULL;
473 KASSERT(frag->fr_firstoff[index]->fe_off < frent->fe_off,
474 ("frag->fr_firstoff[index]->fe_off < frent->fe_off"));
475 KASSERT(prev != NULL, ("prev != NULL"));
476 KASSERT(prev->fe_off + prev->fe_len <= frent->fe_off,
477 ("overlapping fragment"));
478 KASSERT(pf_frent_index(prev) == index,
479 ("pf_frent_index(prev) == index"));
482 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
484 KASSERT(frag->fr_entries[index] > 0, ("No fragments remaining"));
485 frag->fr_entries[index]--;
489 pf_frent_previous(struct pf_fragment *frag, struct pf_frent *frent)
491 struct pf_frent *prev, *next;
495 * If there are no fragments after frag, take the final one. Assume
496 * that the global queue is not empty.
498 prev = TAILQ_LAST(&frag->fr_queue, pf_fragq);
499 KASSERT(prev != NULL, ("prev != NULL"));
500 if (prev->fe_off <= frent->fe_off)
503 * We want to find a fragment entry that is before frag, but still
504 * close to it. Find the first fragment entry that is in the same
505 * entry point or in the first entry point after that. As we have
506 * already checked that there are entries behind frag, this will
509 for (index = pf_frent_index(frent); index < PF_FRAG_ENTRY_POINTS;
511 prev = frag->fr_firstoff[index];
515 KASSERT(prev != NULL, ("prev != NULL"));
517 * In prev we may have a fragment from the same entry point that is
518 * before frent, or one that is just one position behind frent.
519 * In the latter case, we go back one step and have the predecessor.
520 * There may be none if the new fragment will be the first one.
522 if (prev->fe_off > frent->fe_off) {
523 prev = TAILQ_PREV(prev, pf_fragq, fr_next);
526 KASSERT(prev->fe_off <= frent->fe_off,
527 ("prev->fe_off <= frent->fe_off"));
531 * In prev is the first fragment of the entry point. The offset
532 * of frag is behind it. Find the closest previous fragment.
534 for (next = TAILQ_NEXT(prev, fr_next); next != NULL;
535 next = TAILQ_NEXT(next, fr_next)) {
536 if (next->fe_off > frent->fe_off)
543 static struct pf_fragment *
544 pf_fillup_fragment(struct pf_fragment_cmp *key, struct pf_frent *frent,
547 struct pf_frent *after, *next, *prev;
548 struct pf_fragment *frag;
550 int old_index, new_index;
554 /* No empty fragments. */
555 if (frent->fe_len == 0) {
556 DPFPRINTF(("bad fragment: len 0\n"));
560 /* All fragments are 8 byte aligned. */
561 if (frent->fe_mff && (frent->fe_len & 0x7)) {
562 DPFPRINTF(("bad fragment: mff and len %d\n", frent->fe_len));
566 /* Respect maximum length, IP_MAXPACKET == IPV6_MAXPACKET. */
567 if (frent->fe_off + frent->fe_len > IP_MAXPACKET) {
568 DPFPRINTF(("bad fragment: max packet %d\n",
569 frent->fe_off + frent->fe_len));
573 DPFPRINTF((key->frc_af == AF_INET ?
574 "reass frag %d @ %d-%d\n" : "reass frag %#08x @ %d-%d\n",
575 key->frc_id, frent->fe_off, frent->fe_off + frent->fe_len));
577 /* Fully buffer all of the fragments in this fragment queue. */
578 frag = pf_find_fragment(key, &V_pf_frag_tree);
580 /* Create a new reassembly queue for this packet. */
582 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
584 pf_flush_fragments();
585 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
587 REASON_SET(reason, PFRES_MEMORY);
592 *(struct pf_fragment_cmp *)frag = *key;
593 memset(frag->fr_firstoff, 0, sizeof(frag->fr_firstoff));
594 memset(frag->fr_entries, 0, sizeof(frag->fr_entries));
595 frag->fr_timeout = time_uptime;
596 frag->fr_maxlen = frent->fe_len;
598 TAILQ_INIT(&frag->fr_queue);
600 RB_INSERT(pf_frag_tree, &V_pf_frag_tree, frag);
601 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
603 /* We do not have a previous fragment, cannot fail. */
604 pf_frent_insert(frag, frent, NULL);
609 KASSERT(!TAILQ_EMPTY(&frag->fr_queue), ("!TAILQ_EMPTY()->fr_queue"));
611 /* Remember maximum fragment len for refragmentation. */
612 if (frent->fe_len > frag->fr_maxlen)
613 frag->fr_maxlen = frent->fe_len;
615 /* Maximum data we have seen already. */
616 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
617 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
619 /* Non terminal fragments must have more fragments flag. */
620 if (frent->fe_off + frent->fe_len < total && !frent->fe_mff)
623 /* Check if we saw the last fragment already. */
624 if (!TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff) {
625 if (frent->fe_off + frent->fe_len > total ||
626 (frent->fe_off + frent->fe_len == total && frent->fe_mff))
629 if (frent->fe_off + frent->fe_len == total && !frent->fe_mff)
633 /* Find neighbors for newly inserted fragment */
634 prev = pf_frent_previous(frag, frent);
636 after = TAILQ_FIRST(&frag->fr_queue);
637 KASSERT(after != NULL, ("after != NULL"));
639 after = TAILQ_NEXT(prev, fr_next);
642 if (prev != NULL && prev->fe_off + prev->fe_len > frent->fe_off) {
645 precut = prev->fe_off + prev->fe_len - frent->fe_off;
646 if (precut >= frent->fe_len)
648 DPFPRINTF(("overlap -%d\n", precut));
649 m_adj(frent->fe_m, precut);
650 frent->fe_off += precut;
651 frent->fe_len -= precut;
654 for (; after != NULL && frent->fe_off + frent->fe_len > after->fe_off;
658 aftercut = frent->fe_off + frent->fe_len - after->fe_off;
659 DPFPRINTF(("adjust overlap %d\n", aftercut));
660 if (aftercut < after->fe_len) {
661 m_adj(after->fe_m, aftercut);
662 old_index = pf_frent_index(after);
663 after->fe_off += aftercut;
664 after->fe_len -= aftercut;
665 new_index = pf_frent_index(after);
666 if (old_index != new_index) {
667 DPFPRINTF(("frag index %d, new %d",
668 old_index, new_index));
669 /* Fragment switched queue as fe_off changed */
670 after->fe_off -= aftercut;
671 after->fe_len += aftercut;
672 /* Remove restored fragment from old queue */
673 pf_frent_remove(frag, after);
674 after->fe_off += aftercut;
675 after->fe_len -= aftercut;
676 /* Insert into correct queue */
677 if (pf_frent_insert(frag, after, prev)) {
679 ("fragment requeue limit exceeded"));
680 m_freem(after->fe_m);
681 uma_zfree(V_pf_frent_z, after);
682 /* There is not way to recover */
689 /* This fragment is completely overlapped, lose it. */
690 next = TAILQ_NEXT(after, fr_next);
691 pf_frent_remove(frag, after);
692 m_freem(after->fe_m);
693 uma_zfree(V_pf_frent_z, after);
696 /* If part of the queue gets too long, there is not way to recover. */
697 if (pf_frent_insert(frag, frent, prev)) {
698 DPFPRINTF(("fragment queue limit exceeded\n"));
705 REASON_SET(reason, PFRES_FRAG);
707 uma_zfree(V_pf_frent_z, frent);
712 pf_join_fragment(struct pf_fragment *frag)
715 struct pf_frent *frent, *next;
717 frent = TAILQ_FIRST(&frag->fr_queue);
718 next = TAILQ_NEXT(frent, fr_next);
721 m_adj(m, (frent->fe_hdrlen + frent->fe_len) - m->m_pkthdr.len);
722 uma_zfree(V_pf_frent_z, frent);
723 for (frent = next; frent != NULL; frent = next) {
724 next = TAILQ_NEXT(frent, fr_next);
727 /* Strip off ip header. */
728 m_adj(m2, frent->fe_hdrlen);
729 /* Strip off any trailing bytes. */
730 m_adj(m2, frent->fe_len - m2->m_pkthdr.len);
732 uma_zfree(V_pf_frent_z, frent);
736 /* Remove from fragment queue. */
737 pf_remove_fragment(frag);
744 pf_reassemble(struct mbuf **m0, struct ip *ip, int dir, u_short *reason)
746 struct mbuf *m = *m0;
747 struct pf_frent *frent;
748 struct pf_fragment *frag;
749 struct pf_fragment_cmp key;
750 uint16_t total, hdrlen;
752 /* Get an entry for the fragment queue */
753 if ((frent = pf_create_fragment(reason)) == NULL)
757 frent->fe_hdrlen = ip->ip_hl << 2;
758 frent->fe_extoff = 0;
759 frent->fe_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
760 frent->fe_off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
761 frent->fe_mff = ntohs(ip->ip_off) & IP_MF;
763 pf_ip2key(ip, dir, &key);
765 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL)
768 /* The mbuf is part of the fragment entry, no direct free or access */
771 if (frag->fr_holes) {
772 DPFPRINTF(("frag %d, holes %d\n", frag->fr_id, frag->fr_holes));
773 return (PF_PASS); /* drop because *m0 is NULL, no error */
776 /* We have all the data */
777 frent = TAILQ_FIRST(&frag->fr_queue);
778 KASSERT(frent != NULL, ("frent != NULL"));
779 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
780 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
781 hdrlen = frent->fe_hdrlen;
783 m = *m0 = pf_join_fragment(frag);
786 if (m->m_flags & M_PKTHDR) {
788 for (m = *m0; m; m = m->m_next)
791 m->m_pkthdr.len = plen;
794 ip = mtod(m, struct ip *);
795 ip->ip_sum = pf_cksum_fixup(ip->ip_sum, ip->ip_len,
796 htons(hdrlen + total), 0);
797 ip->ip_len = htons(hdrlen + total);
798 ip->ip_sum = pf_cksum_fixup(ip->ip_sum, ip->ip_off,
799 ip->ip_off & ~(IP_MF|IP_OFFMASK), 0);
800 ip->ip_off &= ~(IP_MF|IP_OFFMASK);
802 if (hdrlen + total > IP_MAXPACKET) {
803 DPFPRINTF(("drop: too big: %d\n", total));
805 REASON_SET(reason, PFRES_SHORT);
806 /* PF_DROP requires a valid mbuf *m0 in pf_test() */
810 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
817 pf_reassemble6(struct mbuf **m0, struct ip6_hdr *ip6, struct ip6_frag *fraghdr,
818 uint16_t hdrlen, uint16_t extoff, u_short *reason)
820 struct mbuf *m = *m0;
821 struct pf_frent *frent;
822 struct pf_fragment *frag;
823 struct pf_fragment_cmp key;
825 struct pf_fragment_tag *ftag;
828 uint16_t total, maxlen;
833 /* Get an entry for the fragment queue. */
834 if ((frent = pf_create_fragment(reason)) == NULL) {
840 frent->fe_hdrlen = hdrlen;
841 frent->fe_extoff = extoff;
842 frent->fe_len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - hdrlen;
843 frent->fe_off = ntohs(fraghdr->ip6f_offlg & IP6F_OFF_MASK);
844 frent->fe_mff = fraghdr->ip6f_offlg & IP6F_MORE_FRAG;
846 key.frc_src.v6 = ip6->ip6_src;
847 key.frc_dst.v6 = ip6->ip6_dst;
848 key.frc_af = AF_INET6;
849 /* Only the first fragment's protocol is relevant. */
851 key.frc_id = fraghdr->ip6f_ident;
853 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) {
858 /* The mbuf is part of the fragment entry, no direct free or access. */
861 if (frag->fr_holes) {
862 DPFPRINTF(("frag %d, holes %d\n", frag->fr_id,
865 return (PF_PASS); /* Drop because *m0 is NULL, no error. */
868 /* We have all the data. */
869 frent = TAILQ_FIRST(&frag->fr_queue);
870 KASSERT(frent != NULL, ("frent != NULL"));
871 extoff = frent->fe_extoff;
872 maxlen = frag->fr_maxlen;
873 frag_id = frag->fr_id;
874 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
875 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
876 hdrlen = frent->fe_hdrlen - sizeof(struct ip6_frag);
878 m = *m0 = pf_join_fragment(frag);
883 /* Take protocol from first fragment header. */
884 m = m_getptr(m, hdrlen + offsetof(struct ip6_frag, ip6f_nxt), &off);
885 KASSERT(m, ("%s: short mbuf chain", __func__));
886 proto = *(mtod(m, uint8_t *) + off);
889 /* Delete frag6 header */
890 if (ip6_deletefraghdr(m, hdrlen, M_NOWAIT) != 0)
893 if (m->m_flags & M_PKTHDR) {
895 for (m = *m0; m; m = m->m_next)
898 m->m_pkthdr.len = plen;
901 if ((mtag = m_tag_get(PACKET_TAG_PF_REASSEMBLED,
902 sizeof(struct pf_fragment_tag), M_NOWAIT)) == NULL)
904 ftag = (struct pf_fragment_tag *)(mtag + 1);
905 ftag->ft_hdrlen = hdrlen;
906 ftag->ft_extoff = extoff;
907 ftag->ft_maxlen = maxlen;
908 ftag->ft_id = frag_id;
909 m_tag_prepend(m, mtag);
911 ip6 = mtod(m, struct ip6_hdr *);
912 ip6->ip6_plen = htons(hdrlen - sizeof(struct ip6_hdr) + total);
914 /* Write protocol into next field of last extension header. */
915 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
917 KASSERT(m, ("%s: short mbuf chain", __func__));
918 *(mtod(m, char *) + off) = proto;
921 ip6->ip6_nxt = proto;
923 if (hdrlen - sizeof(struct ip6_hdr) + total > IPV6_MAXPACKET) {
924 DPFPRINTF(("drop: too big: %d\n", total));
926 REASON_SET(reason, PFRES_SHORT);
927 /* PF_DROP requires a valid mbuf *m0 in pf_test6(). */
931 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip6->ip6_plen)));
935 REASON_SET(reason, PFRES_MEMORY);
936 /* PF_DROP requires a valid mbuf *m0 in pf_test6(), will free later. */
943 pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag,
946 struct mbuf *m = *m0, *t;
948 struct pf_fragment_tag *ftag = (struct pf_fragment_tag *)(mtag + 1);
951 uint16_t hdrlen, extoff, maxlen;
955 hdrlen = ftag->ft_hdrlen;
956 extoff = ftag->ft_extoff;
957 maxlen = ftag->ft_maxlen;
958 frag_id = ftag->ft_id;
959 m_tag_delete(m, mtag);
966 /* Use protocol from next field of last extension header */
967 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
969 KASSERT((m != NULL), ("pf_refragment6: short mbuf chain"));
970 proto = *(mtod(m, uint8_t *) + off);
971 *(mtod(m, char *) + off) = IPPROTO_FRAGMENT;
974 hdr = mtod(m, struct ip6_hdr *);
975 proto = hdr->ip6_nxt;
976 hdr->ip6_nxt = IPPROTO_FRAGMENT;
979 /* In case of link-local traffic we'll need a scope set. */
980 hdr = mtod(m, struct ip6_hdr *);
982 in6_setscope(&hdr->ip6_src, ifp, NULL);
983 in6_setscope(&hdr->ip6_dst, ifp, NULL);
985 /* The MTU must be a multiple of 8 bytes, or we risk doing the
986 * fragmentation wrong. */
987 maxlen = maxlen & ~7;
990 * Maxlen may be less than 8 if there was only a single
991 * fragment. As it was fragmented before, add a fragment
992 * header also for a single fragment. If total or maxlen
993 * is less than 8, ip6_fragment() will return EMSGSIZE and
994 * we drop the packet.
996 error = ip6_fragment(ifp, m, hdrlen, proto, maxlen, frag_id);
997 m = (*m0)->m_nextpkt;
998 (*m0)->m_nextpkt = NULL;
1000 /* The first mbuf contains the unfragmented packet. */
1005 /* Drop expects an mbuf to free. */
1006 DPFPRINTF(("refragment error %d\n", error));
1011 m->m_nextpkt = NULL;
1012 m->m_flags |= M_SKIP_FIREWALL;
1013 memset(&pd, 0, sizeof(pd));
1014 pd.pf_mtag = pf_find_mtag(m);
1017 MPASS(m->m_pkthdr.rcvif != NULL);
1020 (void)ip6_output(m, NULL, NULL, 0, NULL, NULL,
1033 pf_normalize_ip(struct mbuf **m0, struct pfi_kkif *kif, u_short *reason,
1034 struct pf_pdesc *pd)
1036 struct mbuf *m = *m0;
1038 struct ip *h = mtod(m, struct ip *);
1039 int mff = (ntohs(h->ip_off) & IP_MF);
1040 int hlen = h->ip_hl << 2;
1041 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1050 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1052 * Check if there are any scrub rules, matching or not.
1053 * Lack of scrub rules means:
1054 * - enforced packet normalization operation just like in OpenBSD
1055 * - fragment reassembly depends on V_pf_status.reass
1057 * - packet normalization is performed if there is a matching scrub rule
1058 * - fragment reassembly is performed if the matching rule has no
1059 * PFRULE_FRAGMENT_NOREASS flag
1061 scrub_compat = (r != NULL);
1063 pf_counter_u64_add(&r->evaluations, 1);
1064 if (pfi_kkif_match(r->kif, kif) == r->ifnot)
1065 r = r->skip[PF_SKIP_IFP].ptr;
1066 else if (r->direction && r->direction != pd->dir)
1067 r = r->skip[PF_SKIP_DIR].ptr;
1068 else if (r->af && r->af != AF_INET)
1069 r = r->skip[PF_SKIP_AF].ptr;
1070 else if (r->proto && r->proto != h->ip_p)
1071 r = r->skip[PF_SKIP_PROTO].ptr;
1072 else if (PF_MISMATCHAW(&r->src.addr,
1073 (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
1074 r->src.neg, kif, M_GETFIB(m)))
1075 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1076 else if (PF_MISMATCHAW(&r->dst.addr,
1077 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
1078 r->dst.neg, NULL, M_GETFIB(m)))
1079 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1080 else if (r->match_tag && !pf_match_tag(m, r, &tag,
1081 pd->pf_mtag ? pd->pf_mtag->tag : 0))
1082 r = TAILQ_NEXT(r, entries);
1088 /* With scrub rules present IPv4 normalization happens only
1089 * if one of rules has matched and it's not a "no scrub" rule */
1090 if (r == NULL || r->action == PF_NOSCRUB)
1093 pf_counter_u64_critical_enter();
1094 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1);
1095 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len);
1096 pf_counter_u64_critical_exit();
1097 pf_rule_to_actions(r, &pd->act);
1100 /* Check for illegal packets */
1101 if (hlen < (int)sizeof(struct ip)) {
1102 REASON_SET(reason, PFRES_NORM);
1106 if (hlen > ntohs(h->ip_len)) {
1107 REASON_SET(reason, PFRES_NORM);
1111 /* Clear IP_DF if the rule uses the no-df option or we're in no-df mode */
1112 if (((!scrub_compat && V_pf_status.reass & PF_REASS_NODF) ||
1113 (r != NULL && r->rule_flag & PFRULE_NODF)) &&
1114 (h->ip_off & htons(IP_DF))
1116 u_int16_t ip_off = h->ip_off;
1118 h->ip_off &= htons(~IP_DF);
1119 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1122 /* We will need other tests here */
1123 if (!fragoff && !mff)
1126 /* We're dealing with a fragment now. Don't allow fragments
1127 * with IP_DF to enter the cache. If the flag was cleared by
1128 * no-df above, fine. Otherwise drop it.
1130 if (h->ip_off & htons(IP_DF)) {
1131 DPFPRINTF(("IP_DF\n"));
1135 ip_len = ntohs(h->ip_len) - hlen;
1137 /* All fragments are 8 byte aligned */
1138 if (mff && (ip_len & 0x7)) {
1139 DPFPRINTF(("mff and %d\n", ip_len));
1143 /* Respect maximum length */
1144 if (fragoff + ip_len > IP_MAXPACKET) {
1145 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
1149 if ((!scrub_compat && V_pf_status.reass) ||
1150 (r != NULL && !(r->rule_flag & PFRULE_FRAGMENT_NOREASS))
1152 max = fragoff + ip_len;
1154 /* Fully buffer all of the fragments
1155 * Might return a completely reassembled mbuf, or NULL */
1157 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
1158 verdict = pf_reassemble(m0, h, pd->dir, reason);
1161 if (verdict != PF_PASS)
1168 h = mtod(m, struct ip *);
1171 /* At this point, only IP_DF is allowed in ip_off */
1172 if (h->ip_off & ~htons(IP_DF)) {
1173 u_int16_t ip_off = h->ip_off;
1175 h->ip_off &= htons(IP_DF);
1176 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1183 DPFPRINTF(("dropping bad fragment\n"));
1184 REASON_SET(reason, PFRES_FRAG);
1186 if (r != NULL && r->log)
1187 PFLOG_PACKET(kif, m, AF_INET, *reason, r, NULL, NULL, pd, 1);
1195 pf_normalize_ip6(struct mbuf **m0, struct pfi_kkif *kif,
1196 u_short *reason, struct pf_pdesc *pd)
1198 struct mbuf *m = *m0;
1200 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1205 struct ip6_frag frag;
1215 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1217 * Check if there are any scrub rules, matching or not.
1218 * Lack of scrub rules means:
1219 * - enforced packet normalization operation just like in OpenBSD
1221 * - packet normalization is performed if there is a matching scrub rule
1222 * XXX: Fragment reassembly always performed for IPv6!
1224 scrub_compat = (r != NULL);
1226 pf_counter_u64_add(&r->evaluations, 1);
1227 if (pfi_kkif_match(r->kif, kif) == r->ifnot)
1228 r = r->skip[PF_SKIP_IFP].ptr;
1229 else if (r->direction && r->direction != pd->dir)
1230 r = r->skip[PF_SKIP_DIR].ptr;
1231 else if (r->af && r->af != AF_INET6)
1232 r = r->skip[PF_SKIP_AF].ptr;
1233 #if 0 /* header chain! */
1234 else if (r->proto && r->proto != h->ip6_nxt)
1235 r = r->skip[PF_SKIP_PROTO].ptr;
1237 else if (PF_MISMATCHAW(&r->src.addr,
1238 (struct pf_addr *)&h->ip6_src, AF_INET6,
1239 r->src.neg, kif, M_GETFIB(m)))
1240 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1241 else if (PF_MISMATCHAW(&r->dst.addr,
1242 (struct pf_addr *)&h->ip6_dst, AF_INET6,
1243 r->dst.neg, NULL, M_GETFIB(m)))
1244 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1250 /* With scrub rules present IPv6 normalization happens only
1251 * if one of rules has matched and it's not a "no scrub" rule */
1252 if (r == NULL || r->action == PF_NOSCRUB)
1255 pf_counter_u64_critical_enter();
1256 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1);
1257 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len);
1258 pf_counter_u64_critical_exit();
1259 pf_rule_to_actions(r, &pd->act);
1262 /* Check for illegal packets */
1263 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1267 h = mtod(m, struct ip6_hdr *);
1268 plen = ntohs(h->ip6_plen);
1269 /* jumbo payload option not supported */
1274 off = sizeof(struct ip6_hdr);
1279 case IPPROTO_FRAGMENT:
1283 case IPPROTO_ROUTING:
1284 case IPPROTO_DSTOPTS:
1285 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1289 if (proto == IPPROTO_AH)
1290 off += (ext.ip6e_len + 2) * 4;
1292 off += (ext.ip6e_len + 1) * 8;
1293 proto = ext.ip6e_nxt;
1295 case IPPROTO_HOPOPTS:
1296 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1300 optend = off + (ext.ip6e_len + 1) * 8;
1301 ooff = off + sizeof(ext);
1303 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1304 sizeof(opt.ip6o_type), NULL, NULL,
1307 if (opt.ip6o_type == IP6OPT_PAD1) {
1311 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1312 NULL, NULL, AF_INET6))
1314 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1316 if (opt.ip6o_type == IP6OPT_JUMBO)
1318 ooff += sizeof(opt) + opt.ip6o_len;
1319 } while (ooff < optend);
1322 proto = ext.ip6e_nxt;
1328 } while (!terminal);
1330 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1336 if (pd->flags & PFDESC_IP_REAS)
1338 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1341 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1344 /* Offset now points to data portion. */
1345 off += sizeof(frag);
1347 /* Returns PF_DROP or *m0 is NULL or completely reassembled mbuf. */
1348 if (pf_reassemble6(m0, h, &frag, off, extoff, reason) != PF_PASS)
1354 pd->flags |= PFDESC_IP_REAS;
1358 REASON_SET(reason, PFRES_SHORT);
1359 if (r != NULL && r->log)
1360 PFLOG_PACKET(kif, m, AF_INET6, *reason, r, NULL, NULL, pd, 1);
1364 REASON_SET(reason, PFRES_NORM);
1365 if (r != NULL && r->log)
1366 PFLOG_PACKET(kif, m, AF_INET6, *reason, r, NULL, NULL, pd, 1);
1372 pf_normalize_tcp(struct pfi_kkif *kif, struct mbuf *m, int ipoff,
1373 int off, void *h, struct pf_pdesc *pd)
1375 struct pf_krule *r, *rm = NULL;
1376 struct tcphdr *th = &pd->hdr.tcp;
1380 sa_family_t af = pd->af;
1385 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1386 /* Check if there any scrub rules. Lack of scrub rules means enforced
1387 * packet normalization operation just like in OpenBSD. */
1390 pf_counter_u64_add(&r->evaluations, 1);
1391 if (pfi_kkif_match(r->kif, kif) == r->ifnot)
1392 r = r->skip[PF_SKIP_IFP].ptr;
1393 else if (r->direction && r->direction != pd->dir)
1394 r = r->skip[PF_SKIP_DIR].ptr;
1395 else if (r->af && r->af != af)
1396 r = r->skip[PF_SKIP_AF].ptr;
1397 else if (r->proto && r->proto != pd->proto)
1398 r = r->skip[PF_SKIP_PROTO].ptr;
1399 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1400 r->src.neg, kif, M_GETFIB(m)))
1401 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1402 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1403 r->src.port[0], r->src.port[1], th->th_sport))
1404 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1405 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1406 r->dst.neg, NULL, M_GETFIB(m)))
1407 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1408 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1409 r->dst.port[0], r->dst.port[1], th->th_dport))
1410 r = r->skip[PF_SKIP_DST_PORT].ptr;
1411 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1412 pf_osfp_fingerprint(pd, m, off, th),
1414 r = TAILQ_NEXT(r, entries);
1422 /* With scrub rules present TCP normalization happens only
1423 * if one of rules has matched and it's not a "no scrub" rule */
1424 if (rm == NULL || rm->action == PF_NOSCRUB)
1427 pf_counter_u64_critical_enter();
1428 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1);
1429 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len);
1430 pf_counter_u64_critical_exit();
1431 pf_rule_to_actions(rm, &pd->act);
1434 if (rm && rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1435 pd->flags |= PFDESC_TCP_NORM;
1437 flags = th->th_flags;
1438 if (flags & TH_SYN) {
1439 /* Illegal packet */
1446 /* Illegal packet */
1447 if (!(flags & (TH_ACK|TH_RST)))
1451 if (!(flags & TH_ACK)) {
1452 /* These flags are only valid if ACK is set */
1453 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1457 /* Check for illegal header length */
1458 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1461 /* If flags changed, or reserved data set, then adjust */
1462 if (flags != th->th_flags || th->th_x2 != 0) {
1465 ov = *(u_int16_t *)(&th->th_ack + 1);
1466 th->th_flags = flags;
1468 nv = *(u_int16_t *)(&th->th_ack + 1);
1470 th->th_sum = pf_proto_cksum_fixup(m, th->th_sum, ov, nv, 0);
1474 /* Remove urgent pointer, if TH_URG is not set */
1475 if (!(flags & TH_URG) && th->th_urp) {
1476 th->th_sum = pf_proto_cksum_fixup(m, th->th_sum, th->th_urp,
1482 /* copy back packet headers if we sanitized */
1484 m_copyback(m, off, sizeof(*th), (caddr_t)th);
1489 REASON_SET(&reason, PFRES_NORM);
1490 if (rm != NULL && r->log)
1491 PFLOG_PACKET(kif, m, AF_INET, reason, r, NULL, NULL, pd, 1);
1496 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1497 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1499 u_int32_t tsval, tsecr;
1503 KASSERT((src->scrub == NULL),
1504 ("pf_normalize_tcp_init: src->scrub != NULL"));
1506 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1507 if (src->scrub == NULL)
1513 struct ip *h = mtod(m, struct ip *);
1514 src->scrub->pfss_ttl = h->ip_ttl;
1520 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1521 src->scrub->pfss_ttl = h->ip6_hlim;
1528 * All normalizations below are only begun if we see the start of
1529 * the connections. They must all set an enabled bit in pfss_flags
1531 if ((th->th_flags & TH_SYN) == 0)
1534 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1535 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1536 /* Diddle with TCP options */
1538 opt = hdr + sizeof(struct tcphdr);
1539 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1540 while (hlen >= TCPOLEN_TIMESTAMP) {
1542 case TCPOPT_EOL: /* FALLTHROUGH */
1547 case TCPOPT_TIMESTAMP:
1548 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1549 src->scrub->pfss_flags |=
1551 src->scrub->pfss_ts_mod =
1552 htonl(arc4random());
1554 /* note PFSS_PAWS not set yet */
1555 memcpy(&tsval, &opt[2],
1557 memcpy(&tsecr, &opt[6],
1559 src->scrub->pfss_tsval0 = ntohl(tsval);
1560 src->scrub->pfss_tsval = ntohl(tsval);
1561 src->scrub->pfss_tsecr = ntohl(tsecr);
1562 getmicrouptime(&src->scrub->pfss_last);
1566 hlen -= MAX(opt[1], 2);
1567 opt += MAX(opt[1], 2);
1577 pf_normalize_tcp_cleanup(struct pf_kstate *state)
1579 /* XXX Note: this also cleans up SCTP. */
1580 uma_zfree(V_pf_state_scrub_z, state->src.scrub);
1581 uma_zfree(V_pf_state_scrub_z, state->dst.scrub);
1583 /* Someday... flush the TCP segment reassembly descriptors. */
1586 pf_normalize_sctp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1587 struct pf_state_peer *src, struct pf_state_peer *dst)
1589 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1590 if (src->scrub == NULL)
1593 dst->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1594 if (dst->scrub == NULL) {
1595 uma_zfree(V_pf_state_scrub_z, src);
1599 dst->scrub->pfss_v_tag = pd->sctp_initiate_tag;
1605 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1606 u_short *reason, struct tcphdr *th, struct pf_kstate *state,
1607 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1609 struct timeval uptime;
1610 u_int32_t tsval, tsecr;
1611 u_int tsval_from_last;
1618 KASSERT((src->scrub || dst->scrub),
1619 ("%s: src->scrub && dst->scrub!", __func__));
1622 * Enforce the minimum TTL seen for this connection. Negate a common
1623 * technique to evade an intrusion detection system and confuse
1624 * firewall state code.
1630 struct ip *h = mtod(m, struct ip *);
1631 if (h->ip_ttl > src->scrub->pfss_ttl)
1632 src->scrub->pfss_ttl = h->ip_ttl;
1633 h->ip_ttl = src->scrub->pfss_ttl;
1641 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1642 if (h->ip6_hlim > src->scrub->pfss_ttl)
1643 src->scrub->pfss_ttl = h->ip6_hlim;
1644 h->ip6_hlim = src->scrub->pfss_ttl;
1651 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1652 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1653 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1654 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1655 /* Diddle with TCP options */
1657 opt = hdr + sizeof(struct tcphdr);
1658 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1659 while (hlen >= TCPOLEN_TIMESTAMP) {
1660 startoff = opt - (hdr + sizeof(struct tcphdr));
1662 case TCPOPT_EOL: /* FALLTHROUGH */
1667 case TCPOPT_TIMESTAMP:
1668 /* Modulate the timestamps. Can be used for
1669 * NAT detection, OS uptime determination or
1674 /* Huh? Multiple timestamps!? */
1675 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1676 DPFPRINTF(("multiple TS??\n"));
1677 pf_print_state(state);
1680 REASON_SET(reason, PFRES_TS);
1683 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1684 memcpy(&tsval, &opt[2],
1686 if (tsval && src->scrub &&
1687 (src->scrub->pfss_flags &
1689 tsval = ntohl(tsval);
1690 pf_patch_32_unaligned(m,
1694 src->scrub->pfss_ts_mod),
1695 PF_ALGNMNT(startoff),
1700 /* Modulate TS reply iff valid (!0) */
1701 memcpy(&tsecr, &opt[6],
1703 if (tsecr && dst->scrub &&
1704 (dst->scrub->pfss_flags &
1706 tsecr = ntohl(tsecr)
1707 - dst->scrub->pfss_ts_mod;
1708 pf_patch_32_unaligned(m,
1712 PF_ALGNMNT(startoff),
1720 hlen -= MAX(opt[1], 2);
1721 opt += MAX(opt[1], 2);
1726 /* Copyback the options, caller copys back header */
1728 m_copyback(m, off + sizeof(struct tcphdr),
1729 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1730 sizeof(struct tcphdr));
1735 * Must invalidate PAWS checks on connections idle for too long.
1736 * The fastest allowed timestamp clock is 1ms. That turns out to
1737 * be about 24 days before it wraps. XXX Right now our lowerbound
1738 * TS echo check only works for the first 12 days of a connection
1739 * when the TS has exhausted half its 32bit space
1741 #define TS_MAX_IDLE (24*24*60*60)
1742 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1744 getmicrouptime(&uptime);
1745 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1746 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1747 time_uptime - state->creation > TS_MAX_CONN)) {
1748 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1749 DPFPRINTF(("src idled out of PAWS\n"));
1750 pf_print_state(state);
1753 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1756 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1757 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1758 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1759 DPFPRINTF(("dst idled out of PAWS\n"));
1760 pf_print_state(state);
1763 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1767 if (got_ts && src->scrub && dst->scrub &&
1768 (src->scrub->pfss_flags & PFSS_PAWS) &&
1769 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1770 /* Validate that the timestamps are "in-window".
1771 * RFC1323 describes TCP Timestamp options that allow
1772 * measurement of RTT (round trip time) and PAWS
1773 * (protection against wrapped sequence numbers). PAWS
1774 * gives us a set of rules for rejecting packets on
1775 * long fat pipes (packets that were somehow delayed
1776 * in transit longer than the time it took to send the
1777 * full TCP sequence space of 4Gb). We can use these
1778 * rules and infer a few others that will let us treat
1779 * the 32bit timestamp and the 32bit echoed timestamp
1780 * as sequence numbers to prevent a blind attacker from
1781 * inserting packets into a connection.
1784 * - The timestamp on this packet must be greater than
1785 * or equal to the last value echoed by the other
1786 * endpoint. The RFC says those will be discarded
1787 * since it is a dup that has already been acked.
1788 * This gives us a lowerbound on the timestamp.
1789 * timestamp >= other last echoed timestamp
1790 * - The timestamp will be less than or equal to
1791 * the last timestamp plus the time between the
1792 * last packet and now. The RFC defines the max
1793 * clock rate as 1ms. We will allow clocks to be
1794 * up to 10% fast and will allow a total difference
1795 * or 30 seconds due to a route change. And this
1796 * gives us an upperbound on the timestamp.
1797 * timestamp <= last timestamp + max ticks
1798 * We have to be careful here. Windows will send an
1799 * initial timestamp of zero and then initialize it
1800 * to a random value after the 3whs; presumably to
1801 * avoid a DoS by having to call an expensive RNG
1802 * during a SYN flood. Proof MS has at least one
1803 * good security geek.
1805 * - The TCP timestamp option must also echo the other
1806 * endpoints timestamp. The timestamp echoed is the
1807 * one carried on the earliest unacknowledged segment
1808 * on the left edge of the sequence window. The RFC
1809 * states that the host will reject any echoed
1810 * timestamps that were larger than any ever sent.
1811 * This gives us an upperbound on the TS echo.
1812 * tescr <= largest_tsval
1813 * - The lowerbound on the TS echo is a little more
1814 * tricky to determine. The other endpoint's echoed
1815 * values will not decrease. But there may be
1816 * network conditions that re-order packets and
1817 * cause our view of them to decrease. For now the
1818 * only lowerbound we can safely determine is that
1819 * the TS echo will never be less than the original
1820 * TS. XXX There is probably a better lowerbound.
1821 * Remove TS_MAX_CONN with better lowerbound check.
1822 * tescr >= other original TS
1824 * It is also important to note that the fastest
1825 * timestamp clock of 1ms will wrap its 32bit space in
1826 * 24 days. So we just disable TS checking after 24
1827 * days of idle time. We actually must use a 12d
1828 * connection limit until we can come up with a better
1829 * lowerbound to the TS echo check.
1831 struct timeval delta_ts;
1835 * PFTM_TS_DIFF is how many seconds of leeway to allow
1836 * a host's timestamp. This can happen if the previous
1837 * packet got delayed in transit for much longer than
1840 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
1841 ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF];
1843 /* Calculate max ticks since the last timestamp */
1844 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
1845 #define TS_MICROSECS 1000000 /* microseconds per second */
1847 timevalsub(&delta_ts, &src->scrub->pfss_last);
1848 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
1849 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
1851 if ((src->state >= TCPS_ESTABLISHED &&
1852 dst->state >= TCPS_ESTABLISHED) &&
1853 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
1854 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
1855 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
1856 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
1857 /* Bad RFC1323 implementation or an insertion attack.
1859 * - Solaris 2.6 and 2.7 are known to send another ACK
1860 * after the FIN,FIN|ACK,ACK closing that carries
1864 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
1865 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
1866 SEQ_GT(tsval, src->scrub->pfss_tsval +
1867 tsval_from_last) ? '1' : ' ',
1868 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
1869 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
1870 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u "
1871 "idle: %jus %lums\n",
1872 tsval, tsecr, tsval_from_last,
1873 (uintmax_t)delta_ts.tv_sec,
1874 delta_ts.tv_usec / 1000));
1875 DPFPRINTF((" src->tsval: %u tsecr: %u\n",
1876 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
1877 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u"
1878 "\n", dst->scrub->pfss_tsval,
1879 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
1880 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1881 pf_print_state(state);
1882 pf_print_flags(th->th_flags);
1885 REASON_SET(reason, PFRES_TS);
1889 /* XXX I'd really like to require tsecr but it's optional */
1891 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
1892 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
1893 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
1894 src->scrub && dst->scrub &&
1895 (src->scrub->pfss_flags & PFSS_PAWS) &&
1896 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1897 /* Didn't send a timestamp. Timestamps aren't really useful
1899 * - connection opening or closing (often not even sent).
1900 * but we must not let an attacker to put a FIN on a
1901 * data packet to sneak it through our ESTABLISHED check.
1902 * - on a TCP reset. RFC suggests not even looking at TS.
1903 * - on an empty ACK. The TS will not be echoed so it will
1904 * probably not help keep the RTT calculation in sync and
1905 * there isn't as much danger when the sequence numbers
1906 * got wrapped. So some stacks don't include TS on empty
1909 * To minimize the disruption to mostly RFC1323 conformant
1910 * stacks, we will only require timestamps on data packets.
1912 * And what do ya know, we cannot require timestamps on data
1913 * packets. There appear to be devices that do legitimate
1914 * TCP connection hijacking. There are HTTP devices that allow
1915 * a 3whs (with timestamps) and then buffer the HTTP request.
1916 * If the intermediate device has the HTTP response cache, it
1917 * will spoof the response but not bother timestamping its
1918 * packets. So we can look for the presence of a timestamp in
1919 * the first data packet and if there, require it in all future
1923 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
1925 * Hey! Someone tried to sneak a packet in. Or the
1926 * stack changed its RFC1323 behavior?!?!
1928 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1929 DPFPRINTF(("Did not receive expected RFC1323 "
1931 pf_print_state(state);
1932 pf_print_flags(th->th_flags);
1935 REASON_SET(reason, PFRES_TS);
1941 * We will note if a host sends his data packets with or without
1942 * timestamps. And require all data packets to contain a timestamp
1943 * if the first does. PAWS implicitly requires that all data packets be
1944 * timestamped. But I think there are middle-man devices that hijack
1945 * TCP streams immediately after the 3whs and don't timestamp their
1946 * packets (seen in a WWW accelerator or cache).
1948 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
1949 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
1951 src->scrub->pfss_flags |= PFSS_DATA_TS;
1953 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
1954 if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
1955 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
1956 /* Don't warn if other host rejected RFC1323 */
1957 DPFPRINTF(("Broken RFC1323 stack did not "
1958 "timestamp data packet. Disabled PAWS "
1960 pf_print_state(state);
1961 pf_print_flags(th->th_flags);
1968 * Update PAWS values
1970 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
1971 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
1972 getmicrouptime(&src->scrub->pfss_last);
1973 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
1974 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1975 src->scrub->pfss_tsval = tsval;
1978 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
1979 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1980 src->scrub->pfss_tsecr = tsecr;
1982 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
1983 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
1984 src->scrub->pfss_tsval0 == 0)) {
1985 /* tsval0 MUST be the lowest timestamp */
1986 src->scrub->pfss_tsval0 = tsval;
1989 /* Only fully initialized after a TS gets echoed */
1990 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
1991 src->scrub->pfss_flags |= PFSS_PAWS;
1995 /* I have a dream.... TCP segment reassembly.... */
2000 pf_normalize_mss(struct mbuf *m, int off, struct pf_pdesc *pd)
2002 struct tcphdr *th = &pd->hdr.tcp;
2005 int opt, cnt, optlen = 0;
2006 u_char opts[TCP_MAXOLEN];
2007 u_char *optp = opts;
2010 thoff = th->th_off << 2;
2011 cnt = thoff - sizeof(struct tcphdr);
2013 if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
2014 NULL, NULL, pd->af))
2017 for (; cnt > 0; cnt -= optlen, optp += optlen) {
2018 startoff = optp - opts;
2020 if (opt == TCPOPT_EOL)
2022 if (opt == TCPOPT_NOP)
2028 if (optlen < 2 || optlen > cnt)
2033 mss = (u_int16_t *)(optp + 2);
2034 if ((ntohs(*mss)) > pd->act.max_mss) {
2035 pf_patch_16_unaligned(m,
2037 mss, htons(pd->act.max_mss),
2038 PF_ALGNMNT(startoff),
2040 m_copyback(m, off + sizeof(*th),
2041 thoff - sizeof(*th), opts);
2042 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2054 pf_scan_sctp(struct mbuf *m, int ipoff, int off, struct pf_pdesc *pd,
2055 struct pfi_kkif *kif)
2057 struct sctp_chunkhdr ch = { };
2058 int chunk_off = sizeof(struct sctphdr);
2062 while (off + chunk_off < pd->tot_len) {
2063 if (!pf_pull_hdr(m, off + chunk_off, &ch, sizeof(ch), NULL,
2067 /* Length includes the header, this must be at least 4. */
2068 if (ntohs(ch.chunk_length) < 4)
2071 chunk_start = chunk_off;
2072 chunk_off += roundup(ntohs(ch.chunk_length), 4);
2074 switch (ch.chunk_type) {
2075 case SCTP_INITIATION:
2076 case SCTP_INITIATION_ACK: {
2077 struct sctp_init_chunk init;
2079 if (!pf_pull_hdr(m, off + chunk_start, &init,
2080 sizeof(init), NULL, NULL, pd->af))
2084 * RFC 9620, Section 3.3.2, "The Initiate Tag is allowed to have
2085 * any value except 0."
2087 if (init.init.initiate_tag == 0)
2089 if (init.init.num_inbound_streams == 0)
2091 if (init.init.num_outbound_streams == 0)
2093 if (ntohl(init.init.a_rwnd) < SCTP_MIN_RWND)
2097 * RFC 9260, Section 3.1, INIT chunks MUST have zero
2100 if (ch.chunk_type == SCTP_INITIATION &&
2101 pd->hdr.sctp.v_tag != 0)
2104 pd->sctp_initiate_tag = init.init.initiate_tag;
2106 if (ch.chunk_type == SCTP_INITIATION)
2107 pd->sctp_flags |= PFDESC_SCTP_INIT;
2109 pd->sctp_flags |= PFDESC_SCTP_INIT_ACK;
2111 ret = pf_multihome_scan_init(m, off + chunk_start,
2112 ntohs(init.ch.chunk_length), pd, kif);
2118 case SCTP_ABORT_ASSOCIATION:
2119 pd->sctp_flags |= PFDESC_SCTP_ABORT;
2122 case SCTP_SHUTDOWN_ACK:
2123 pd->sctp_flags |= PFDESC_SCTP_SHUTDOWN;
2125 case SCTP_SHUTDOWN_COMPLETE:
2126 pd->sctp_flags |= PFDESC_SCTP_SHUTDOWN_COMPLETE;
2128 case SCTP_COOKIE_ECHO:
2129 case SCTP_COOKIE_ACK:
2130 pd->sctp_flags |= PFDESC_SCTP_COOKIE;
2133 pd->sctp_flags |= PFDESC_SCTP_DATA;
2136 pd->sctp_flags |= PFDESC_SCTP_ASCONF;
2138 ret = pf_multihome_scan_asconf(m, off + chunk_start,
2139 ntohs(ch.chunk_length), pd, kif);
2144 pd->sctp_flags |= PFDESC_SCTP_OTHER;
2149 /* Validate chunk lengths vs. packet length. */
2150 if (off + chunk_off != pd->tot_len)
2154 * INIT, INIT_ACK or SHUTDOWN_COMPLETE chunks must always be the only
2157 if ((pd->sctp_flags & PFDESC_SCTP_INIT) &&
2158 (pd->sctp_flags & ~PFDESC_SCTP_INIT))
2160 if ((pd->sctp_flags & PFDESC_SCTP_INIT_ACK) &&
2161 (pd->sctp_flags & ~PFDESC_SCTP_INIT_ACK))
2163 if ((pd->sctp_flags & PFDESC_SCTP_SHUTDOWN_COMPLETE) &&
2164 (pd->sctp_flags & ~PFDESC_SCTP_SHUTDOWN_COMPLETE))
2171 pf_normalize_sctp(int dir, struct pfi_kkif *kif, struct mbuf *m, int ipoff,
2172 int off, void *h, struct pf_pdesc *pd)
2174 struct pf_krule *r, *rm = NULL;
2175 struct sctphdr *sh = &pd->hdr.sctp;
2177 sa_family_t af = pd->af;
2182 /* Unconditionally scan the SCTP packet, because we need to look for
2183 * things like shutdown and asconf chunks. */
2184 if (pf_scan_sctp(m, ipoff, off, pd, kif) != PF_PASS)
2187 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
2188 /* Check if there any scrub rules. Lack of scrub rules means enforced
2189 * packet normalization operation just like in OpenBSD. */
2192 pf_counter_u64_add(&r->evaluations, 1);
2193 if (pfi_kkif_match(r->kif, kif) == r->ifnot)
2194 r = r->skip[PF_SKIP_IFP].ptr;
2195 else if (r->direction && r->direction != dir)
2196 r = r->skip[PF_SKIP_DIR].ptr;
2197 else if (r->af && r->af != af)
2198 r = r->skip[PF_SKIP_AF].ptr;
2199 else if (r->proto && r->proto != pd->proto)
2200 r = r->skip[PF_SKIP_PROTO].ptr;
2201 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
2202 r->src.neg, kif, M_GETFIB(m)))
2203 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2204 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2205 r->src.port[0], r->src.port[1], sh->src_port))
2206 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2207 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
2208 r->dst.neg, NULL, M_GETFIB(m)))
2209 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2210 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2211 r->dst.port[0], r->dst.port[1], sh->dest_port))
2212 r = r->skip[PF_SKIP_DST_PORT].ptr;
2220 /* With scrub rules present SCTP normalization happens only
2221 * if one of rules has matched and it's not a "no scrub" rule */
2222 if (rm == NULL || rm->action == PF_NOSCRUB)
2225 pf_counter_u64_critical_enter();
2226 pf_counter_u64_add_protected(&r->packets[dir == PF_OUT], 1);
2227 pf_counter_u64_add_protected(&r->bytes[dir == PF_OUT], pd->tot_len);
2228 pf_counter_u64_critical_exit();
2231 /* Verify we're a multiple of 4 bytes long */
2232 if ((pd->tot_len - off - sizeof(struct sctphdr)) % 4)
2235 /* INIT chunk needs to be the only chunk */
2236 if (pd->sctp_flags & PFDESC_SCTP_INIT)
2237 if (pd->sctp_flags & ~PFDESC_SCTP_INIT)
2243 REASON_SET(&reason, PFRES_NORM);
2244 if (rm != NULL && r->log)
2245 PFLOG_PACKET(kif, m, AF_INET, reason, r, NULL, NULL, pd,
2253 pf_scrub_ip(struct mbuf **m0, struct pf_pdesc *pd)
2255 struct mbuf *m = *m0;
2256 struct ip *h = mtod(m, struct ip *);
2258 /* Clear IP_DF if no-df was requested */
2259 if (pd->act.flags & PFSTATE_NODF && h->ip_off & htons(IP_DF)) {
2260 u_int16_t ip_off = h->ip_off;
2262 h->ip_off &= htons(~IP_DF);
2263 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
2266 /* Enforce a minimum ttl, may cause endless packet loops */
2267 if (pd->act.min_ttl && h->ip_ttl < pd->act.min_ttl) {
2268 u_int16_t ip_ttl = h->ip_ttl;
2270 h->ip_ttl = pd->act.min_ttl;
2271 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
2275 if (pd->act.flags & PFSTATE_SETTOS) {
2278 ov = *(u_int16_t *)h;
2279 h->ip_tos = pd->act.set_tos | (h->ip_tos & IPTOS_ECN_MASK);
2280 nv = *(u_int16_t *)h;
2282 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
2285 /* random-id, but not for fragments */
2286 if (pd->act.flags & PFSTATE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) {
2287 uint16_t ip_id = h->ip_id;
2290 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
2297 pf_scrub_ip6(struct mbuf **m0, struct pf_pdesc *pd)
2299 struct mbuf *m = *m0;
2300 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
2302 /* Enforce a minimum ttl, may cause endless packet loops */
2303 if (pd->act.min_ttl && h->ip6_hlim < pd->act.min_ttl)
2304 h->ip6_hlim = pd->act.min_ttl;
2306 /* Enforce tos. Set traffic class bits */
2307 if (pd->act.flags & PFSTATE_SETTOS) {
2308 h->ip6_flow &= IPV6_FLOWLABEL_MASK | IPV6_VERSION_MASK;
2309 h->ip6_flow |= htonl((pd->act.set_tos | IPV6_ECN(h)) << 20);