1 /* $OpenBSD: pf_norm.c,v 1.113 2008/05/07 07:07:29 markus Exp $ */
4 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
6 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/filio.h>
37 #include <sys/fcntl.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
43 #include <net/if_types.h>
45 #include <net/route.h>
46 #include <net/pf/if_pflog.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcp_seq.h>
55 #include <netinet/udp.h>
56 #include <netinet/ip_icmp.h>
59 #include <netinet/ip6.h>
62 #include <net/pf/pfvar.h>
64 #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */
65 #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */
66 #define PFFRAG_DROP 0x0004 /* Drop all fragments */
67 #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER))
70 TAILQ_HEAD(pf_fragqueue, pf_fragment) pf_fragqueue[MAXCPU];
71 TAILQ_HEAD(pf_cachequeue, pf_fragment) pf_cachequeue[MAXCPU];
73 static __inline int pf_frag_compare(struct pf_fragment *,
74 struct pf_fragment *);
75 RB_HEAD(pf_frag_tree, pf_fragment) pf_frag_tree[MAXCPU],
76 pf_cache_tree[MAXCPU];
77 RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
78 RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
80 /* Private prototypes */
81 void pf_ip2key(struct pf_fragment *, struct ip *);
82 void pf_remove_fragment(struct pf_fragment *);
83 void pf_flush_fragments(void);
84 void pf_free_fragment(struct pf_fragment *);
85 struct pf_fragment *pf_find_fragment(struct ip *, struct pf_frag_tree *);
86 struct mbuf *pf_reassemble(struct mbuf **, struct pf_fragment **,
87 struct pf_frent *, int);
88 struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
89 struct pf_fragment **, int, int, int *);
90 int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
91 struct tcphdr *, int, sa_family_t);
93 #define DPFPRINTF(x) do { \
94 if (pf_status.debug >= PF_DEBUG_MISC) { \
95 kprintf("%s: ", __func__); \
100 static MALLOC_DEFINE(M_PFFRAGPL, "pffrag", "pf fragment pool list");
101 static MALLOC_DEFINE(M_PFCACHEPL, "pffrcache", "pf fragment cache pool list");
102 static MALLOC_DEFINE(M_PFFRENTPL, "pffrent", "pf frent pool list");
103 static MALLOC_DEFINE(M_PFCENTPL, "pffrcent", "pf fragment cent pool list");
104 static MALLOC_DEFINE(M_PFSTATESCRUBPL, "pfstatescrub", "pf state scrub pool list");
107 struct malloc_type *pf_frent_pl, *pf_frag_pl, *pf_cache_pl, *pf_cent_pl;
108 struct malloc_type *pf_state_scrub_pl;
109 int pf_nfrents, pf_ncache;
112 pf_normalize_init(void)
117 pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT);
118 pool_sethardlimit(&pf_frent_pl, PFFRAG_FRENT_HIWAT, NULL, 0);
119 pool_sethardlimit(&pf_cache_pl, PFFRAG_FRCACHE_HIWAT, NULL, 0);
120 pool_sethardlimit(&pf_cent_pl, PFFRAG_FRCENT_HIWAT, NULL, 0);
123 for (n = 0; n < MAXCPU; ++n) {
124 TAILQ_INIT(&pf_fragqueue[n]);
125 TAILQ_INIT(&pf_cachequeue[n]);
126 RB_INIT(&pf_frag_tree[n]);
127 RB_INIT(&pf_cache_tree[n]);
132 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
136 if ((diff = a->fr_id - b->fr_id))
138 else if ((diff = a->fr_p - b->fr_p))
140 else if (a->fr_src.s_addr < b->fr_src.s_addr)
142 else if (a->fr_src.s_addr > b->fr_src.s_addr)
144 else if (a->fr_dst.s_addr < b->fr_dst.s_addr)
146 else if (a->fr_dst.s_addr > b->fr_dst.s_addr)
152 pf_purge_expired_fragments(void)
154 struct pf_fragment *frag;
156 int cpu = mycpu->gd_cpuid;
158 expire = time_second - pf_default_rule.timeout[PFTM_FRAG];
160 while ((frag = TAILQ_LAST(&pf_fragqueue[cpu], pf_fragqueue)) != NULL) {
161 KASSERT((BUFFER_FRAGMENTS(frag)),
162 ("BUFFER_FRAGMENTS(frag) == 0: %s", __func__));
163 if (frag->fr_timeout > expire)
166 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
167 pf_free_fragment(frag);
170 while ((frag = TAILQ_LAST(&pf_cachequeue[cpu], pf_cachequeue)) != NULL) {
171 KASSERT((!BUFFER_FRAGMENTS(frag)),
172 ("BUFFER_FRAGMENTS(frag) != 0: %s", __func__));
173 if (frag->fr_timeout > expire)
176 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
177 pf_free_fragment(frag);
178 KASSERT((TAILQ_EMPTY(&pf_cachequeue[cpu]) ||
179 TAILQ_LAST(&pf_cachequeue[cpu], pf_cachequeue) != frag),
180 ("!(TAILQ_EMPTY() || TAILQ_LAST() == farg): %s",
186 * Try to flush old fragments to make space for new ones
190 pf_flush_fragments(void)
192 struct pf_fragment *frag;
194 int cpu = mycpu->gd_cpuid;
196 goal = pf_nfrents * 9 / 10;
197 DPFPRINTF(("trying to free > %d frents\n",
199 while (goal < pf_nfrents) {
200 frag = TAILQ_LAST(&pf_fragqueue[cpu], pf_fragqueue);
203 pf_free_fragment(frag);
207 goal = pf_ncache * 9 / 10;
208 DPFPRINTF(("trying to free > %d cache entries\n",
210 while (goal < pf_ncache) {
211 frag = TAILQ_LAST(&pf_cachequeue[cpu], pf_cachequeue);
214 pf_free_fragment(frag);
218 /* Frees the fragments and all associated entries */
221 pf_free_fragment(struct pf_fragment *frag)
223 struct pf_frent *frent;
224 struct pf_frcache *frcache;
226 /* Free all fragments */
227 if (BUFFER_FRAGMENTS(frag)) {
228 for (frent = LIST_FIRST(&frag->fr_queue); frent;
229 frent = LIST_FIRST(&frag->fr_queue)) {
230 LIST_REMOVE(frent, fr_next);
232 m_freem(frent->fr_m);
233 kfree(frent, M_PFFRENTPL);
237 for (frcache = LIST_FIRST(&frag->fr_cache); frcache;
238 frcache = LIST_FIRST(&frag->fr_cache)) {
239 LIST_REMOVE(frcache, fr_next);
241 KASSERT((LIST_EMPTY(&frag->fr_cache) ||
242 LIST_FIRST(&frag->fr_cache)->fr_off >
244 ("! (LIST_EMPTY() || LIST_FIRST()->fr_off >"
245 " frcache->fr_end): %s", __func__));
247 kfree(frcache, M_PFCENTPL);
252 pf_remove_fragment(frag);
256 pf_ip2key(struct pf_fragment *key, struct ip *ip)
258 key->fr_p = ip->ip_p;
259 key->fr_id = ip->ip_id;
260 key->fr_src.s_addr = ip->ip_src.s_addr;
261 key->fr_dst.s_addr = ip->ip_dst.s_addr;
265 pf_find_fragment(struct ip *ip, struct pf_frag_tree *tree)
267 struct pf_fragment key;
268 struct pf_fragment *frag;
269 int cpu = mycpu->gd_cpuid;
273 frag = RB_FIND(pf_frag_tree, tree, &key);
275 /* XXX Are we sure we want to update the timeout? */
276 frag->fr_timeout = time_second;
277 if (BUFFER_FRAGMENTS(frag)) {
278 TAILQ_REMOVE(&pf_fragqueue[cpu], frag, frag_next);
279 TAILQ_INSERT_HEAD(&pf_fragqueue[cpu], frag, frag_next);
281 TAILQ_REMOVE(&pf_cachequeue[cpu], frag, frag_next);
282 TAILQ_INSERT_HEAD(&pf_cachequeue[cpu], frag, frag_next);
289 /* Removes a fragment from the fragment queue and frees the fragment */
292 pf_remove_fragment(struct pf_fragment *frag)
294 int cpu = mycpu->gd_cpuid;
296 if (BUFFER_FRAGMENTS(frag)) {
297 RB_REMOVE(pf_frag_tree, &pf_frag_tree[cpu], frag);
298 TAILQ_REMOVE(&pf_fragqueue[cpu], frag, frag_next);
299 kfree(frag, M_PFFRAGPL);
301 RB_REMOVE(pf_frag_tree, &pf_cache_tree[cpu], frag);
302 TAILQ_REMOVE(&pf_cachequeue[cpu], frag, frag_next);
303 kfree(frag, M_PFCACHEPL);
307 #define FR_IP_OFF(fr) (((fr)->fr_ip->ip_off & IP_OFFMASK) << 3)
309 pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
310 struct pf_frent *frent, int mff)
312 struct mbuf *m = *m0, *m2;
313 struct pf_frent *frea, *next;
314 struct pf_frent *frep = NULL;
315 struct ip *ip = frent->fr_ip;
316 int hlen = ip->ip_hl << 2;
317 u_int16_t off = (ip->ip_off & IP_OFFMASK) << 3;
318 u_int16_t ip_len = ip->ip_len - ip->ip_hl * 4;
319 u_int16_t max = ip_len + off;
320 int cpu = mycpu->gd_cpuid;
322 KASSERT((*frag == NULL || BUFFER_FRAGMENTS(*frag)),
323 ("! (*frag == NULL || BUFFER_FRAGMENTS(*frag)): %s", __func__));
325 /* Strip off ip header */
329 /* Create a new reassembly queue for this packet */
331 *frag = kmalloc(sizeof(struct pf_fragment), M_PFFRAGPL, M_NOWAIT);
333 pf_flush_fragments();
334 *frag = kmalloc(sizeof(struct pf_fragment), M_PFFRAGPL, M_NOWAIT);
339 (*frag)->fr_flags = 0;
341 (*frag)->fr_src = frent->fr_ip->ip_src;
342 (*frag)->fr_dst = frent->fr_ip->ip_dst;
343 (*frag)->fr_p = frent->fr_ip->ip_p;
344 (*frag)->fr_id = frent->fr_ip->ip_id;
345 (*frag)->fr_timeout = time_second;
346 LIST_INIT(&(*frag)->fr_queue);
348 RB_INSERT(pf_frag_tree, &pf_frag_tree[cpu], *frag);
349 TAILQ_INSERT_HEAD(&pf_fragqueue[cpu], *frag, frag_next);
351 /* We do not have a previous fragment */
357 * Find a fragment after the current one:
358 * - off contains the real shifted offset.
360 LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
361 if (FR_IP_OFF(frea) > off)
366 KASSERT((frep != NULL || frea != NULL),
367 ("!(frep != NULL || frea != NULL): %s", __func__));
370 FR_IP_OFF(frep) + frep->fr_ip->ip_len - frep->fr_ip->ip_hl *
375 precut = FR_IP_OFF(frep) + frep->fr_ip->ip_len -
376 frep->fr_ip->ip_hl * 4 - off;
377 if (precut >= ip_len)
379 m_adj(frent->fr_m, precut);
380 DPFPRINTF(("overlap -%d\n", precut));
381 /* Enforce 8 byte boundaries */
382 ip->ip_off = ip->ip_off + (precut >> 3);
383 off = (ip->ip_off & IP_OFFMASK) << 3;
388 for (; frea != NULL && ip_len + off > FR_IP_OFF(frea);
393 aftercut = ip_len + off - FR_IP_OFF(frea);
394 DPFPRINTF(("adjust overlap %d\n", aftercut));
395 if (aftercut < frea->fr_ip->ip_len - frea->fr_ip->ip_hl
398 frea->fr_ip->ip_len =
399 frea->fr_ip->ip_len - aftercut;
400 frea->fr_ip->ip_off = frea->fr_ip->ip_off +
402 m_adj(frea->fr_m, aftercut);
406 /* This fragment is completely overlapped, lose it */
407 next = LIST_NEXT(frea, fr_next);
409 LIST_REMOVE(frea, fr_next);
410 kfree(frea, M_PFFRENTPL);
415 /* Update maximum data size */
416 if ((*frag)->fr_max < max)
417 (*frag)->fr_max = max;
418 /* This is the last segment */
420 (*frag)->fr_flags |= PFFRAG_SEENLAST;
423 LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
425 LIST_INSERT_AFTER(frep, frent, fr_next);
427 /* Check if we are completely reassembled */
428 if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
431 /* Check if we have all the data */
433 for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) {
434 next = LIST_NEXT(frep, fr_next);
436 off += frep->fr_ip->ip_len - frep->fr_ip->ip_hl * 4;
437 if (off < (*frag)->fr_max &&
438 (next == NULL || FR_IP_OFF(next) != off))
440 DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
441 off, next == NULL ? -1 : FR_IP_OFF(next),
446 DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
447 if (off < (*frag)->fr_max)
450 /* We have all the data */
451 frent = LIST_FIRST(&(*frag)->fr_queue);
452 KASSERT((frent != NULL), ("frent == NULL: %s", __func__));
453 if ((frent->fr_ip->ip_hl << 2) + off > IP_MAXPACKET) {
454 DPFPRINTF(("drop: too big: %d\n", off));
455 pf_free_fragment(*frag);
459 next = LIST_NEXT(frent, fr_next);
461 /* Magic from ip_input */
467 kfree(frent, M_PFFRENTPL);
469 for (frent = next; frent != NULL; frent = next) {
470 next = LIST_NEXT(frent, fr_next);
473 kfree(frent, M_PFFRENTPL);
478 ip->ip_src = (*frag)->fr_src;
479 ip->ip_dst = (*frag)->fr_dst;
481 /* Remove from fragment queue */
482 pf_remove_fragment(*frag);
485 hlen = ip->ip_hl << 2;
486 ip->ip_len = off + hlen;
490 /* some debugging cruft by sklower, below, will go away soon */
491 /* XXX this should be done elsewhere */
492 if (m->m_flags & M_PKTHDR) {
494 for (m2 = m; m2; m2 = m2->m_next)
496 m->m_pkthdr.len = plen;
499 DPFPRINTF(("complete: %p(%d)\n", m, ip->ip_len));
503 /* Oops - fail safe - drop packet */
504 kfree(frent, M_PFFRENTPL);
511 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
512 int drop, int *nomem)
514 struct mbuf *m = *m0;
515 struct pf_frcache *frp, *fra, *cur = NULL;
516 int ip_len = h->ip_len - (h->ip_hl << 2);
517 u_int16_t off = h->ip_off << 3;
518 u_int16_t max = ip_len + off;
520 int cpu = mycpu->gd_cpuid;
522 KASSERT((*frag == NULL || !BUFFER_FRAGMENTS(*frag)),
523 ("!(*frag == NULL || !BUFFER_FRAGMENTS(*frag)): %s", __func__));
525 /* Create a new range queue for this packet */
527 *frag = kmalloc(sizeof(struct pf_fragment), M_PFCACHEPL, M_NOWAIT);
529 pf_flush_fragments();
530 *frag = kmalloc(sizeof(struct pf_fragment), M_PFCACHEPL, M_NOWAIT);
535 /* Get an entry for the queue */
536 cur = kmalloc(sizeof(struct pf_frcache), M_PFCENTPL, M_NOWAIT);
538 kfree(*frag, M_PFCACHEPL);
544 (*frag)->fr_flags = PFFRAG_NOBUFFER;
546 (*frag)->fr_src = h->ip_src;
547 (*frag)->fr_dst = h->ip_dst;
548 (*frag)->fr_p = h->ip_p;
549 (*frag)->fr_id = h->ip_id;
550 (*frag)->fr_timeout = time_second;
554 LIST_INIT(&(*frag)->fr_cache);
555 LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
557 RB_INSERT(pf_frag_tree, &pf_cache_tree[cpu], *frag);
558 TAILQ_INSERT_HEAD(&pf_cachequeue[cpu], *frag, frag_next);
560 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
566 * Find a fragment after the current one:
567 * - off contains the real shifted offset.
570 LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
571 if (fra->fr_off > off)
576 KASSERT((frp != NULL || fra != NULL),
577 ("!(frp != NULL || fra != NULL): %s", __func__));
582 precut = frp->fr_end - off;
583 if (precut >= ip_len) {
584 /* Fragment is entirely a duplicate */
585 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
586 h->ip_id, frp->fr_off, frp->fr_end, off, max));
590 /* They are adjacent. Fixup cache entry */
591 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
592 h->ip_id, frp->fr_off, frp->fr_end, off, max));
594 } else if (precut > 0) {
595 /* The first part of this payload overlaps with a
596 * fragment that has already been passed.
597 * Need to trim off the first part of the payload.
598 * But to do so easily, we need to create another
599 * mbuf to throw the original header into.
602 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
603 h->ip_id, precut, frp->fr_off, frp->fr_end, off,
608 /* Update the previous frag to encompass this one */
612 /* XXX Optimization opportunity
613 * This is a very heavy way to trim the payload.
614 * we could do it much faster by diddling mbuf
615 * internals but that would be even less legible
616 * than this mbuf magic. For my next trick,
617 * I'll pull a rabbit out of my laptop.
619 *m0 = m_dup(m, M_NOWAIT);
620 /* From KAME Project : We have missed this! */
621 m_adj(*m0, (h->ip_hl << 2) -
622 (*m0)->m_pkthdr.len);
625 KASSERT(((*m0)->m_next == NULL),
626 ("(*m0)->m_next != NULL: %s",
628 m_adj(m, precut + (h->ip_hl << 2));
631 if (m->m_flags & M_PKTHDR) {
634 for (t = m; t; t = t->m_next)
636 m->m_pkthdr.len = plen;
640 h = mtod(m, struct ip *);
642 KASSERT(((int)m->m_len ==
644 ("m->m_len != h->ip_len - precut: %s",
646 h->ip_off = h->ip_off +
648 h->ip_len = h->ip_len - precut;
653 /* There is a gap between fragments */
655 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
656 h->ip_id, -precut, frp->fr_off, frp->fr_end, off,
659 cur = kmalloc(sizeof(struct pf_frcache), M_PFCENTPL, M_NOWAIT);
666 LIST_INSERT_AFTER(frp, cur, fr_next);
674 aftercut = max - fra->fr_off;
676 /* Adjacent fragments */
677 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
678 h->ip_id, off, max, fra->fr_off, fra->fr_end));
681 } else if (aftercut > 0) {
682 /* Need to chop off the tail of this fragment */
683 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
684 h->ip_id, aftercut, off, max, fra->fr_off,
693 if (m->m_flags & M_PKTHDR) {
696 for (t = m; t; t = t->m_next)
698 m->m_pkthdr.len = plen;
700 h = mtod(m, struct ip *);
701 KASSERT(((int)m->m_len == h->ip_len - aftercut),
702 ("m->m_len != h->ip_len - aftercut: %s",
704 h->ip_len = h->ip_len - aftercut;
708 } else if (frp == NULL) {
709 /* There is a gap between fragments */
710 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
711 h->ip_id, -aftercut, off, max, fra->fr_off,
714 cur = kmalloc(sizeof(struct pf_frcache), M_PFCENTPL, M_NOWAIT);
721 LIST_INSERT_BEFORE(fra, cur, fr_next);
725 /* Need to glue together two separate fragment descriptors */
727 if (cur && fra->fr_off <= cur->fr_end) {
728 /* Need to merge in a previous 'cur' */
729 DPFPRINTF(("fragcache[%d]: adjacent(merge "
730 "%d-%d) %d-%d (%d-%d)\n",
731 h->ip_id, cur->fr_off, cur->fr_end, off,
732 max, fra->fr_off, fra->fr_end));
733 fra->fr_off = cur->fr_off;
734 LIST_REMOVE(cur, fr_next);
735 kfree(cur, M_PFCENTPL);
739 } else if (frp && fra->fr_off <= frp->fr_end) {
740 /* Need to merge in a modified 'frp' */
741 KASSERT((cur == NULL), ("cur != NULL: %s",
743 DPFPRINTF(("fragcache[%d]: adjacent(merge "
744 "%d-%d) %d-%d (%d-%d)\n",
745 h->ip_id, frp->fr_off, frp->fr_end, off,
746 max, fra->fr_off, fra->fr_end));
747 fra->fr_off = frp->fr_off;
748 LIST_REMOVE(frp, fr_next);
749 kfree(frp, M_PFCENTPL);
759 * We must keep tracking the overall fragment even when
760 * we're going to drop it anyway so that we know when to
761 * free the overall descriptor. Thus we drop the frag late.
768 /* Update maximum data size */
769 if ((*frag)->fr_max < max)
770 (*frag)->fr_max = max;
772 /* This is the last segment */
774 (*frag)->fr_flags |= PFFRAG_SEENLAST;
776 /* Check if we are completely reassembled */
777 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
778 LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
779 LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) {
780 /* Remove from fragment queue */
781 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
783 pf_free_fragment(*frag);
792 /* Still need to pay attention to !IP_MF */
793 if (!mff && *frag != NULL)
794 (*frag)->fr_flags |= PFFRAG_SEENLAST;
801 /* Still need to pay attention to !IP_MF */
802 if (!mff && *frag != NULL)
803 (*frag)->fr_flags |= PFFRAG_SEENLAST;
806 /* This fragment has been deemed bad. Don't reass */
807 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
808 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
810 (*frag)->fr_flags |= PFFRAG_DROP;
818 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
821 struct mbuf *m = *m0;
823 struct pf_frent *frent;
824 struct pf_fragment *frag = NULL;
825 struct ip *h = mtod(m, struct ip *);
826 int mff = (h->ip_off & IP_MF);
827 int hlen = h->ip_hl << 2;
828 u_int16_t fragoff = (h->ip_off & IP_OFFMASK) << 3;
832 int cpu = mycpu->gd_cpuid;
834 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
837 if (pfi_kif_match(r->kif, kif) == r->ifnot)
838 r = r->skip[PF_SKIP_IFP].ptr;
839 else if (r->direction && r->direction != dir)
840 r = r->skip[PF_SKIP_DIR].ptr;
841 else if (r->af && r->af != AF_INET)
842 r = r->skip[PF_SKIP_AF].ptr;
843 else if (r->proto && r->proto != h->ip_p)
844 r = r->skip[PF_SKIP_PROTO].ptr;
845 else if (PF_MISMATCHAW(&r->src.addr,
846 (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
848 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
849 else if (PF_MISMATCHAW(&r->dst.addr,
850 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
852 r = r->skip[PF_SKIP_DST_ADDR].ptr;
853 else if (r->match_tag && !pf_match_tag(m, r, &tag))
854 r = TAILQ_NEXT(r, entries);
859 if (r == NULL || r->action == PF_NOSCRUB)
862 r->packets[dir == PF_OUT]++;
863 r->bytes[dir == PF_OUT] += pd->tot_len;
866 /* Check for illegal packets */
867 if (hlen < (int)sizeof(struct ip))
870 if (hlen > h->ip_len)
873 /* Clear IP_DF if the rule uses the no-df option */
874 if (r->rule_flag & PFRULE_NODF && h->ip_off & IP_DF) {
875 u_int16_t ip_off = h->ip_off;
878 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
881 /* We will need other tests here */
882 if (!fragoff && !mff)
885 /* We're dealing with a fragment now. Don't allow fragments
886 * with IP_DF to enter the cache. If the flag was cleared by
887 * no-df above, fine. Otherwise drop it.
889 if (h->ip_off & IP_DF) {
890 DPFPRINTF(("IP_DF\n"));
894 ip_len = h->ip_len - hlen;
896 /* All fragments are 8 byte aligned */
897 if (mff && (ip_len & 0x7)) {
898 DPFPRINTF(("mff and %d\n", ip_len));
902 /* Respect maximum length */
903 if (fragoff + ip_len > IP_MAXPACKET) {
904 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
907 max = fragoff + ip_len;
909 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
910 /* Fully buffer all of the fragments */
912 frag = pf_find_fragment(h, &pf_frag_tree[cpu]);
914 /* Check if we saw the last fragment already */
915 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
919 /* Get an entry for the fragment queue */
920 frent = kmalloc(sizeof(struct pf_frent), M_PFFRENTPL, M_NOWAIT);
922 REASON_SET(reason, PFRES_MEMORY);
929 /* Might return a completely reassembled mbuf, or NULL */
930 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
931 *m0 = m = pf_reassemble(m0, &frag, frent, mff);
936 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
939 h = mtod(m, struct ip *);
941 /* non-buffering fragment cache (drops or masks overlaps) */
944 if (dir == PF_OUT && m->m_pkthdr.pf.flags & PF_TAG_FRAGCACHE) {
946 * Already passed the fragment cache in the
947 * input direction. If we continued, it would
948 * appear to be a dup and would be dropped.
953 frag = pf_find_fragment(h, &pf_cache_tree[cpu]);
955 /* Check if we saw the last fragment already */
956 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
957 max > frag->fr_max) {
958 if (r->rule_flag & PFRULE_FRAGDROP)
959 frag->fr_flags |= PFFRAG_DROP;
963 *m0 = m = pf_fragcache(m0, h, &frag, mff,
964 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
972 m->m_pkthdr.pf.flags |= PF_TAG_FRAGCACHE;
974 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
980 /* At this point, only IP_DF is allowed in ip_off */
981 if (h->ip_off & ~IP_DF) {
982 u_int16_t ip_off = h->ip_off;
985 h->ip_sum = pf_cksum_fixup(h->ip_sum, htons(ip_off), htons(h->ip_off), 0);
988 /* Enforce a minimum ttl, may cause endless packet loops */
989 if (r->min_ttl && h->ip_ttl < r->min_ttl) {
990 u_int16_t ip_ttl = h->ip_ttl;
992 h->ip_ttl = r->min_ttl;
993 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
997 if (r->rule_flag & PFRULE_SET_TOS) {
1000 ov = *(u_int16_t *)h;
1001 h->ip_tos = r->set_tos;
1002 nv = *(u_int16_t *)h;
1004 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
1007 if (r->rule_flag & PFRULE_RANDOMID) {
1008 u_int16_t ip_id = h->ip_id;
1010 h->ip_id = ip_randomid();
1011 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
1013 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1014 pd->flags |= PFDESC_IP_REAS;
1019 /* Enforce a minimum ttl, may cause endless packet loops */
1020 if (r->min_ttl && h->ip_ttl < r->min_ttl) {
1021 u_int16_t ip_ttl = h->ip_ttl;
1023 h->ip_ttl = r->min_ttl;
1024 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
1027 if (r->rule_flag & PFRULE_SET_TOS) {
1030 ov = *(u_int16_t *)h;
1031 h->ip_tos = r->set_tos;
1032 nv = *(u_int16_t *)h;
1034 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
1036 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1037 pd->flags |= PFDESC_IP_REAS;
1041 REASON_SET(reason, PFRES_MEMORY);
1042 if (r != NULL && r->log)
1043 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1047 REASON_SET(reason, PFRES_NORM);
1048 if (r != NULL && r->log)
1049 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1053 DPFPRINTF(("dropping bad fragment\n"));
1055 /* Free associated fragments */
1057 pf_free_fragment(frag);
1059 REASON_SET(reason, PFRES_FRAG);
1060 if (r != NULL && r->log)
1061 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1068 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1069 u_short *reason, struct pf_pdesc *pd)
1071 struct mbuf *m = *m0;
1073 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1077 struct ip6_opt_jumbo jumbo;
1078 struct ip6_frag frag;
1079 u_int32_t jumbolen = 0, plen;
1080 u_int16_t fragoff = 0;
1086 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1089 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1090 r = r->skip[PF_SKIP_IFP].ptr;
1091 else if (r->direction && r->direction != dir)
1092 r = r->skip[PF_SKIP_DIR].ptr;
1093 else if (r->af && r->af != AF_INET6)
1094 r = r->skip[PF_SKIP_AF].ptr;
1095 #if 0 /* header chain! */
1096 else if (r->proto && r->proto != h->ip6_nxt)
1097 r = r->skip[PF_SKIP_PROTO].ptr;
1099 else if (PF_MISMATCHAW(&r->src.addr,
1100 (struct pf_addr *)&h->ip6_src, AF_INET6,
1102 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1103 else if (PF_MISMATCHAW(&r->dst.addr,
1104 (struct pf_addr *)&h->ip6_dst, AF_INET6,
1106 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1111 if (r == NULL || r->action == PF_NOSCRUB)
1114 r->packets[dir == PF_OUT]++;
1115 r->bytes[dir == PF_OUT] += pd->tot_len;
1118 /* Check for illegal packets */
1119 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1122 off = sizeof(struct ip6_hdr);
1127 case IPPROTO_FRAGMENT:
1131 case IPPROTO_ROUTING:
1132 case IPPROTO_DSTOPTS:
1133 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1136 if (proto == IPPROTO_AH)
1137 off += (ext.ip6e_len + 2) * 4;
1139 off += (ext.ip6e_len + 1) * 8;
1140 proto = ext.ip6e_nxt;
1142 case IPPROTO_HOPOPTS:
1143 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1146 optend = off + (ext.ip6e_len + 1) * 8;
1147 ooff = off + sizeof(ext);
1149 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1150 sizeof(opt.ip6o_type), NULL, NULL,
1153 if (opt.ip6o_type == IP6OPT_PAD1) {
1157 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1158 NULL, NULL, AF_INET6))
1160 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1162 switch (opt.ip6o_type) {
1164 if (h->ip6_plen != 0)
1166 if (!pf_pull_hdr(m, ooff, &jumbo,
1167 sizeof(jumbo), NULL, NULL,
1170 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1172 jumbolen = ntohl(jumbolen);
1173 if (jumbolen <= IPV6_MAXPACKET)
1175 if (sizeof(struct ip6_hdr) + jumbolen !=
1182 ooff += sizeof(opt) + opt.ip6o_len;
1183 } while (ooff < optend);
1186 proto = ext.ip6e_nxt;
1192 } while (!terminal);
1194 /* jumbo payload option must be present, or plen > 0 */
1195 if (ntohs(h->ip6_plen) == 0)
1198 plen = ntohs(h->ip6_plen);
1201 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1204 /* Enforce a minimum ttl, may cause endless packet loops */
1205 if (r->min_ttl && h->ip6_hlim < r->min_ttl)
1206 h->ip6_hlim = r->min_ttl;
1211 if (ntohs(h->ip6_plen) == 0 || jumbolen)
1213 plen = ntohs(h->ip6_plen);
1215 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1217 fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
1218 if (fragoff + (plen - off - sizeof(frag)) > IPV6_MAXPACKET)
1221 /* do something about it */
1222 /* remember to set pd->flags |= PFDESC_IP_REAS */
1226 REASON_SET(reason, PFRES_SHORT);
1227 if (r != NULL && r->log)
1228 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1232 REASON_SET(reason, PFRES_NORM);
1233 if (r != NULL && r->log)
1234 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1238 REASON_SET(reason, PFRES_FRAG);
1239 if (r != NULL && r->log)
1240 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1246 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1247 int off, void *h, struct pf_pdesc *pd)
1249 struct pf_rule *r, *rm = NULL;
1250 struct tcphdr *th = pd->hdr.tcp;
1254 sa_family_t af = pd->af;
1256 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1259 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1260 r = r->skip[PF_SKIP_IFP].ptr;
1261 else if (r->direction && r->direction != dir)
1262 r = r->skip[PF_SKIP_DIR].ptr;
1263 else if (r->af && r->af != af)
1264 r = r->skip[PF_SKIP_AF].ptr;
1265 else if (r->proto && r->proto != pd->proto)
1266 r = r->skip[PF_SKIP_PROTO].ptr;
1267 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1269 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1270 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1271 r->src.port[0], r->src.port[1], th->th_sport))
1272 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1273 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1275 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1276 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1277 r->dst.port[0], r->dst.port[1], th->th_dport))
1278 r = r->skip[PF_SKIP_DST_PORT].ptr;
1279 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1280 pf_osfp_fingerprint(pd, m, off, th),
1282 r = TAILQ_NEXT(r, entries);
1289 if (rm == NULL || rm->action == PF_NOSCRUB)
1292 r->packets[dir == PF_OUT]++;
1293 r->bytes[dir == PF_OUT] += pd->tot_len;
1296 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1297 pd->flags |= PFDESC_TCP_NORM;
1299 flags = th->th_flags;
1300 if (flags & TH_SYN) {
1301 /* Illegal packet */
1308 /* Illegal packet */
1309 if (!(flags & (TH_ACK|TH_RST)))
1313 if (!(flags & TH_ACK)) {
1314 /* These flags are only valid if ACK is set */
1315 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1319 /* Check for illegal header length */
1320 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1323 /* If flags changed, or reserved data set, then adjust */
1324 if (flags != th->th_flags || th->th_x2 != 0) {
1327 ov = *(u_int16_t *)(&th->th_ack + 1);
1328 th->th_flags = flags;
1330 nv = *(u_int16_t *)(&th->th_ack + 1);
1332 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1336 /* Remove urgent pointer, if TH_URG is not set */
1337 if (!(flags & TH_URG) && th->th_urp) {
1338 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1343 /* Process options */
1344 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af))
1347 /* copy back packet headers if we sanitized */
1349 m_copyback(m, off, sizeof(*th), (caddr_t)th);
1354 REASON_SET(&reason, PFRES_NORM);
1355 if (rm != NULL && r->log)
1356 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL, pd);
1361 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1362 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1364 u_int32_t tsval, tsecr;
1368 KASSERT((src->scrub == NULL),
1369 ("pf_normalize_tcp_init: src->scrub != NULL"));
1371 src->scrub = kmalloc(sizeof(struct pf_state_scrub), M_PFSTATESCRUBPL,
1373 if (src->scrub == NULL)
1379 struct ip *h = mtod(m, struct ip *);
1380 src->scrub->pfss_ttl = h->ip_ttl;
1386 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1387 src->scrub->pfss_ttl = h->ip6_hlim;
1395 * All normalizations below are only begun if we see the start of
1396 * the connections. They must all set an enabled bit in pfss_flags
1398 if ((th->th_flags & TH_SYN) == 0)
1402 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1403 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1404 /* Diddle with TCP options */
1406 opt = hdr + sizeof(struct tcphdr);
1407 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1408 while (hlen >= TCPOLEN_TIMESTAMP) {
1410 case TCPOPT_EOL: /* FALLTHROUGH */
1415 case TCPOPT_TIMESTAMP:
1416 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1417 src->scrub->pfss_flags |=
1419 src->scrub->pfss_ts_mod = karc4random();
1421 /* note PFSS_PAWS not set yet */
1422 memcpy(&tsval, &opt[2],
1424 memcpy(&tsecr, &opt[6],
1426 src->scrub->pfss_tsval0 = ntohl(tsval);
1427 src->scrub->pfss_tsval = ntohl(tsval);
1428 src->scrub->pfss_tsecr = ntohl(tsecr);
1429 getmicrouptime(&src->scrub->pfss_last);
1433 hlen -= MAX(opt[1], 2);
1434 opt += MAX(opt[1], 2);
1444 pf_normalize_tcp_cleanup(struct pf_state *state)
1446 if (state->src.scrub)
1447 kfree(state->src.scrub, M_PFSTATESCRUBPL);
1448 if (state->dst.scrub)
1449 kfree(state->dst.scrub, M_PFSTATESCRUBPL);
1451 /* Someday... flush the TCP segment reassembly descriptors. */
1455 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1456 u_short *reason, struct tcphdr *th, struct pf_state *state,
1457 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1459 struct timeval uptime;
1460 u_int32_t tsval, tsecr;
1461 u_int tsval_from_last;
1467 KASSERT((src->scrub || dst->scrub),
1468 ("pf_normalize_tcp_statefull: src->scrub && dst->scrub!"));
1471 * Enforce the minimum TTL seen for this connection. Negate a common
1472 * technique to evade an intrusion detection system and confuse
1473 * firewall state code.
1479 struct ip *h = mtod(m, struct ip *);
1480 if (h->ip_ttl > src->scrub->pfss_ttl)
1481 src->scrub->pfss_ttl = h->ip_ttl;
1482 h->ip_ttl = src->scrub->pfss_ttl;
1490 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1491 if (h->ip6_hlim > src->scrub->pfss_ttl)
1492 src->scrub->pfss_ttl = h->ip6_hlim;
1493 h->ip6_hlim = src->scrub->pfss_ttl;
1500 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1501 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1502 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1503 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1504 /* Diddle with TCP options */
1506 opt = hdr + sizeof(struct tcphdr);
1507 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1508 while (hlen >= TCPOLEN_TIMESTAMP) {
1510 case TCPOPT_EOL: /* FALLTHROUGH */
1515 case TCPOPT_TIMESTAMP:
1516 /* Modulate the timestamps. Can be used for
1517 * NAT detection, OS uptime determination or
1522 /* Huh? Multiple timestamps!? */
1523 if (pf_status.debug >= PF_DEBUG_MISC) {
1524 DPFPRINTF(("multiple TS??"));
1525 pf_print_state(state);
1528 REASON_SET(reason, PFRES_TS);
1531 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1532 memcpy(&tsval, &opt[2],
1534 if (tsval && src->scrub &&
1535 (src->scrub->pfss_flags &
1537 tsval = ntohl(tsval);
1538 pf_change_a(&opt[2],
1541 src->scrub->pfss_ts_mod),
1546 /* Modulate TS reply iff valid (!0) */
1547 memcpy(&tsecr, &opt[6],
1549 if (tsecr && dst->scrub &&
1550 (dst->scrub->pfss_flags &
1552 tsecr = ntohl(tsecr)
1553 - dst->scrub->pfss_ts_mod;
1554 pf_change_a(&opt[6],
1555 &th->th_sum, htonl(tsecr),
1563 hlen -= MAX(opt[1], 2);
1564 opt += MAX(opt[1], 2);
1569 /* Copyback the options, caller copys back header */
1571 m_copyback(m, off + sizeof(struct tcphdr),
1572 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1573 sizeof(struct tcphdr));
1579 * Must invalidate PAWS checks on connections idle for too long.
1580 * The fastest allowed timestamp clock is 1ms. That turns out to
1581 * be about 24 days before it wraps. XXX Right now our lowerbound
1582 * TS echo check only works for the first 12 days of a connection
1583 * when the TS has exhausted half its 32bit space
1585 #define TS_MAX_IDLE (24*24*60*60)
1586 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1588 getmicrouptime(&uptime);
1589 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1590 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1591 time_second - state->creation > TS_MAX_CONN)) {
1592 if (pf_status.debug >= PF_DEBUG_MISC) {
1593 DPFPRINTF(("src idled out of PAWS\n"));
1594 pf_print_state(state);
1597 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1600 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1601 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1602 if (pf_status.debug >= PF_DEBUG_MISC) {
1603 DPFPRINTF(("dst idled out of PAWS\n"));
1604 pf_print_state(state);
1607 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1611 if (got_ts && src->scrub && dst->scrub &&
1612 (src->scrub->pfss_flags & PFSS_PAWS) &&
1613 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1614 /* Validate that the timestamps are "in-window".
1615 * RFC1323 describes TCP Timestamp options that allow
1616 * measurement of RTT (round trip time) and PAWS
1617 * (protection against wrapped sequence numbers). PAWS
1618 * gives us a set of rules for rejecting packets on
1619 * long fat pipes (packets that were somehow delayed
1620 * in transit longer than the time it took to send the
1621 * full TCP sequence space of 4Gb). We can use these
1622 * rules and infer a few others that will let us treat
1623 * the 32bit timestamp and the 32bit echoed timestamp
1624 * as sequence numbers to prevent a blind attacker from
1625 * inserting packets into a connection.
1628 * - The timestamp on this packet must be greater than
1629 * or equal to the last value echoed by the other
1630 * endpoint. The RFC says those will be discarded
1631 * since it is a dup that has already been acked.
1632 * This gives us a lowerbound on the timestamp.
1633 * timestamp >= other last echoed timestamp
1634 * - The timestamp will be less than or equal to
1635 * the last timestamp plus the time between the
1636 * last packet and now. The RFC defines the max
1637 * clock rate as 1ms. We will allow clocks to be
1638 * up to 10% fast and will allow a total difference
1639 * or 30 seconds due to a route change. And this
1640 * gives us an upperbound on the timestamp.
1641 * timestamp <= last timestamp + max ticks
1642 * We have to be careful here. Windows will send an
1643 * initial timestamp of zero and then initialize it
1644 * to a random value after the 3whs; presumably to
1645 * avoid a DoS by having to call an expensive RNG
1646 * during a SYN flood. Proof MS has at least one
1647 * good security geek.
1649 * - The TCP timestamp option must also echo the other
1650 * endpoints timestamp. The timestamp echoed is the
1651 * one carried on the earliest unacknowledged segment
1652 * on the left edge of the sequence window. The RFC
1653 * states that the host will reject any echoed
1654 * timestamps that were larger than any ever sent.
1655 * This gives us an upperbound on the TS echo.
1656 * tescr <= largest_tsval
1657 * - The lowerbound on the TS echo is a little more
1658 * tricky to determine. The other endpoint's echoed
1659 * values will not decrease. But there may be
1660 * network conditions that re-order packets and
1661 * cause our view of them to decrease. For now the
1662 * only lowerbound we can safely determine is that
1663 * the TS echo will never be less than the original
1664 * TS. XXX There is probably a better lowerbound.
1665 * Remove TS_MAX_CONN with better lowerbound check.
1666 * tescr >= other original TS
1668 * It is also important to note that the fastest
1669 * timestamp clock of 1ms will wrap its 32bit space in
1670 * 24 days. So we just disable TS checking after 24
1671 * days of idle time. We actually must use a 12d
1672 * connection limit until we can come up with a better
1673 * lowerbound to the TS echo check.
1675 struct timeval delta_ts;
1680 * PFTM_TS_DIFF is how many seconds of leeway to allow
1681 * a host's timestamp. This can happen if the previous
1682 * packet got delayed in transit for much longer than
1685 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
1686 ts_fudge = pf_default_rule.timeout[PFTM_TS_DIFF];
1689 /* Calculate max ticks since the last timestamp */
1690 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
1691 #define TS_MICROSECS 1000000 /* microseconds per second */
1693 #define timersub(tvp, uvp, vvp) \
1695 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
1696 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
1697 if ((vvp)->tv_usec < 0) { \
1699 (vvp)->tv_usec += 1000000; \
1704 timersub(&uptime, &src->scrub->pfss_last, &delta_ts);
1705 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
1706 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
1709 if ((src->state >= TCPS_ESTABLISHED &&
1710 dst->state >= TCPS_ESTABLISHED) &&
1711 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
1712 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
1713 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
1714 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
1715 /* Bad RFC1323 implementation or an insertion attack.
1717 * - Solaris 2.6 and 2.7 are known to send another ACK
1718 * after the FIN,FIN|ACK,ACK closing that carries
1722 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
1723 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
1724 SEQ_GT(tsval, src->scrub->pfss_tsval +
1725 tsval_from_last) ? '1' : ' ',
1726 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
1727 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
1728 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u "
1729 "idle: %lus %lums\n",
1730 tsval, tsecr, tsval_from_last, delta_ts.tv_sec,
1731 delta_ts.tv_usec / 1000));
1732 DPFPRINTF((" src->tsval: %u tsecr: %u\n",
1733 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
1734 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u"
1735 "\n", dst->scrub->pfss_tsval,
1736 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
1737 if (pf_status.debug >= PF_DEBUG_MISC) {
1738 pf_print_state(state);
1739 pf_print_flags(th->th_flags);
1742 REASON_SET(reason, PFRES_TS);
1746 /* XXX I'd really like to require tsecr but it's optional */
1748 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
1749 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
1750 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
1751 src->scrub && dst->scrub &&
1752 (src->scrub->pfss_flags & PFSS_PAWS) &&
1753 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1754 /* Didn't send a timestamp. Timestamps aren't really useful
1756 * - connection opening or closing (often not even sent).
1757 * but we must not let an attacker to put a FIN on a
1758 * data packet to sneak it through our ESTABLISHED check.
1759 * - on a TCP reset. RFC suggests not even looking at TS.
1760 * - on an empty ACK. The TS will not be echoed so it will
1761 * probably not help keep the RTT calculation in sync and
1762 * there isn't as much danger when the sequence numbers
1763 * got wrapped. So some stacks don't include TS on empty
1766 * To minimize the disruption to mostly RFC1323 conformant
1767 * stacks, we will only require timestamps on data packets.
1769 * And what do ya know, we cannot require timestamps on data
1770 * packets. There appear to be devices that do legitimate
1771 * TCP connection hijacking. There are HTTP devices that allow
1772 * a 3whs (with timestamps) and then buffer the HTTP request.
1773 * If the intermediate device has the HTTP response cache, it
1774 * will spoof the response but not bother timestamping its
1775 * packets. So we can look for the presence of a timestamp in
1776 * the first data packet and if there, require it in all future
1780 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
1782 * Hey! Someone tried to sneak a packet in. Or the
1783 * stack changed its RFC1323 behavior?!?!
1785 if (pf_status.debug >= PF_DEBUG_MISC) {
1786 DPFPRINTF(("Did not receive expected RFC1323 "
1788 pf_print_state(state);
1789 pf_print_flags(th->th_flags);
1792 REASON_SET(reason, PFRES_TS);
1799 * We will note if a host sends his data packets with or without
1800 * timestamps. And require all data packets to contain a timestamp
1801 * if the first does. PAWS implicitly requires that all data packets be
1802 * timestamped. But I think there are middle-man devices that hijack
1803 * TCP streams immediately after the 3whs and don't timestamp their
1804 * packets (seen in a WWW accelerator or cache).
1806 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
1807 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
1809 src->scrub->pfss_flags |= PFSS_DATA_TS;
1811 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
1812 if (pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
1813 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
1814 /* Don't warn if other host rejected RFC1323 */
1815 DPFPRINTF(("Broken RFC1323 stack did not "
1816 "timestamp data packet. Disabled PAWS "
1818 pf_print_state(state);
1819 pf_print_flags(th->th_flags);
1827 * Update PAWS values
1829 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
1830 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
1831 getmicrouptime(&src->scrub->pfss_last);
1832 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
1833 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1834 src->scrub->pfss_tsval = tsval;
1837 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
1838 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1839 src->scrub->pfss_tsecr = tsecr;
1841 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
1842 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
1843 src->scrub->pfss_tsval0 == 0)) {
1844 /* tsval0 MUST be the lowest timestamp */
1845 src->scrub->pfss_tsval0 = tsval;
1848 /* Only fully initialized after a TS gets echoed */
1849 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
1850 src->scrub->pfss_flags |= PFSS_PAWS;
1854 /* I have a dream.... TCP segment reassembly.... */
1859 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
1860 int off, sa_family_t af)
1864 int opt, cnt, optlen = 0;
1866 u_char opts[TCP_MAXOLEN];
1867 u_char *optp = opts;
1869 thoff = th->th_off << 2;
1870 cnt = thoff - sizeof(struct tcphdr);
1872 if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
1876 for (; cnt > 0; cnt -= optlen, optp += optlen) {
1878 if (opt == TCPOPT_EOL)
1880 if (opt == TCPOPT_NOP)
1886 if (optlen < 2 || optlen > cnt)
1891 mss = (u_int16_t *)(optp + 2);
1892 if ((ntohs(*mss)) > r->max_mss) {
1893 th->th_sum = pf_cksum_fixup(th->th_sum,
1894 *mss, htons(r->max_mss), 0);
1895 *mss = htons(r->max_mss);
1905 m_copyback(m, off + sizeof(*th), thoff - sizeof(*th), opts);