1 /* $OpenBSD: pf_norm.c,v 1.114 2009/01/29 14:11:45 henning Exp $ */
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "opt_inet6.h"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #define NPFLOG DEV_PFLOG
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/filio.h>
49 #include <sys/fcntl.h>
50 #include <sys/socket.h>
51 #include <sys/kernel.h>
56 #include <dev/rndvar.h>
59 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <net/if_pflog.h>
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcp_seq.h>
71 #include <netinet/udp.h>
72 #include <netinet/ip_icmp.h>
75 #include <netinet/ip6.h>
78 #include <net/pfvar.h>
82 LIST_ENTRY(pf_frent) fr_next;
88 LIST_ENTRY(pf_frcache) fr_next;
94 #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */
95 #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */
96 #define PFFRAG_DROP 0x0004 /* Drop all fragments */
97 #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER))
101 RB_ENTRY(pf_fragment) fr_entry;
102 TAILQ_ENTRY(pf_fragment) frag_next;
103 struct in_addr fr_src;
104 struct in_addr fr_dst;
105 u_int8_t fr_p; /* protocol of this fragment */
106 u_int8_t fr_flags; /* status flags */
107 u_int16_t fr_id; /* fragment id for reassemble */
108 u_int16_t fr_max; /* fragment data max */
109 u_int32_t fr_timeout;
110 #define fr_queue fr_u.fru_queue
111 #define fr_cache fr_u.fru_cache
113 LIST_HEAD(pf_fragq, pf_frent) fru_queue; /* buffering */
114 LIST_HEAD(pf_cacheq, pf_frcache) fru_cache; /* non-buf */
120 TAILQ_HEAD(pf_fragqueue, pf_fragment);
121 TAILQ_HEAD(pf_cachequeue, pf_fragment);
122 VNET_DEFINE(struct pf_fragqueue, pf_fragqueue);
123 #define V_pf_fragqueue VNET(pf_fragqueue)
124 VNET_DEFINE(struct pf_cachequeue, pf_cachequeue);
125 #define V_pf_cachequeue VNET(pf_cachequeue)
127 TAILQ_HEAD(pf_fragqueue, pf_fragment) pf_fragqueue;
128 TAILQ_HEAD(pf_cachequeue, pf_fragment) pf_cachequeue;
132 static __inline int pf_frag_compare(struct pf_fragment *,
133 struct pf_fragment *);
135 static int pf_frag_compare(struct pf_fragment *,
136 struct pf_fragment *);
140 RB_HEAD(pf_frag_tree, pf_fragment);
141 VNET_DEFINE(struct pf_frag_tree, pf_frag_tree);
142 #define V_pf_frag_tree VNET(pf_frag_tree)
143 VNET_DEFINE(struct pf_frag_tree, pf_cache_tree);
144 #define V_pf_cache_tree VNET(pf_cache_tree)
146 RB_HEAD(pf_frag_tree, pf_fragment) pf_frag_tree, pf_cache_tree;
148 RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
149 RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
151 /* Private prototypes */
152 void pf_ip2key(struct pf_fragment *, struct ip *);
153 void pf_remove_fragment(struct pf_fragment *);
154 void pf_flush_fragments(void);
155 void pf_free_fragment(struct pf_fragment *);
156 struct pf_fragment *pf_find_fragment(struct ip *, struct pf_frag_tree *);
157 struct mbuf *pf_reassemble(struct mbuf **, struct pf_fragment **,
158 struct pf_frent *, int);
159 struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
160 struct pf_fragment **, int, int, int *);
161 int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
162 struct tcphdr *, int, sa_family_t);
163 void pf_scrub_ip(struct mbuf **, u_int32_t, u_int8_t,
166 void pf_scrub_ip6(struct mbuf **, u_int8_t);
169 #define DPFPRINTF(x) do { \
170 if (V_pf_status.debug >= PF_DEBUG_MISC) { \
171 printf("%s: ", __func__); \
176 #define DPFPRINTF(x) do { \
177 if (pf_status.debug >= PF_DEBUG_MISC) { \
178 printf("%s: ", __func__); \
186 VNET_DEFINE(uma_zone_t, pf_frent_pl);
187 VNET_DEFINE(uma_zone_t, pf_frag_pl);
188 VNET_DEFINE(uma_zone_t, pf_cache_pl);
189 VNET_DEFINE(uma_zone_t, pf_cent_pl);
190 VNET_DEFINE(uma_zone_t, pf_state_scrub_pl);
192 VNET_DEFINE(int, pf_nfrents);
193 #define V_pf_nfrents VNET(pf_nfrents)
194 VNET_DEFINE(int, pf_ncache);
195 #define V_pf_ncache VNET(pf_ncache)
197 struct pool pf_frent_pl, pf_frag_pl, pf_cache_pl, pf_cent_pl;
198 struct pool pf_state_scrub_pl;
199 int pf_nfrents, pf_ncache;
203 pf_normalize_init(void)
208 * No high water mark support(It's hint not hard limit).
209 * uma_zone_set_max(pf_frag_pl, PFFRAG_FRAG_HIWAT);
211 uma_zone_set_max(V_pf_frent_pl, PFFRAG_FRENT_HIWAT);
212 uma_zone_set_max(V_pf_cache_pl, PFFRAG_FRCACHE_HIWAT);
213 uma_zone_set_max(V_pf_cent_pl, PFFRAG_FRCENT_HIWAT);
215 pool_init(&pf_frent_pl, sizeof(struct pf_frent), 0, 0, 0, "pffrent",
217 pool_init(&pf_frag_pl, sizeof(struct pf_fragment), 0, 0, 0, "pffrag",
219 pool_init(&pf_cache_pl, sizeof(struct pf_fragment), 0, 0, 0,
221 pool_init(&pf_cent_pl, sizeof(struct pf_frcache), 0, 0, 0, "pffrcent",
223 pool_init(&pf_state_scrub_pl, sizeof(struct pf_state_scrub), 0, 0, 0,
226 pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT);
227 pool_sethardlimit(&pf_frent_pl, PFFRAG_FRENT_HIWAT, NULL, 0);
228 pool_sethardlimit(&pf_cache_pl, PFFRAG_FRCACHE_HIWAT, NULL, 0);
229 pool_sethardlimit(&pf_cent_pl, PFFRAG_FRCENT_HIWAT, NULL, 0);
233 TAILQ_INIT(&V_pf_fragqueue);
234 TAILQ_INIT(&V_pf_cachequeue);
236 TAILQ_INIT(&pf_fragqueue);
237 TAILQ_INIT(&pf_cachequeue);
246 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
250 if ((diff = a->fr_id - b->fr_id))
252 else if ((diff = a->fr_p - b->fr_p))
254 else if (a->fr_src.s_addr < b->fr_src.s_addr)
256 else if (a->fr_src.s_addr > b->fr_src.s_addr)
258 else if (a->fr_dst.s_addr < b->fr_dst.s_addr)
260 else if (a->fr_dst.s_addr > b->fr_dst.s_addr)
266 pf_purge_expired_fragments(void)
268 struct pf_fragment *frag;
270 u_int32_t expire = time_second -
271 V_pf_default_rule.timeout[PFTM_FRAG];
273 u_int32_t expire = time_second -
274 pf_default_rule.timeout[PFTM_FRAG];
278 while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) {
279 KASSERT((BUFFER_FRAGMENTS(frag)),
280 ("BUFFER_FRAGMENTS(frag) == 0: %s", __FUNCTION__));
282 while ((frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue)) != NULL) {
283 KASSERT(BUFFER_FRAGMENTS(frag));
285 if (frag->fr_timeout > expire)
288 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
289 pf_free_fragment(frag);
293 while ((frag = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue)) != NULL) {
294 KASSERT((!BUFFER_FRAGMENTS(frag)),
295 ("BUFFER_FRAGMENTS(frag) != 0: %s", __FUNCTION__));
297 while ((frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue)) != NULL) {
298 KASSERT(!BUFFER_FRAGMENTS(frag));
300 if (frag->fr_timeout > expire)
303 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
304 pf_free_fragment(frag);
306 KASSERT((TAILQ_EMPTY(&V_pf_cachequeue) ||
307 TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue) != frag),
308 ("!(TAILQ_EMPTY() || TAILQ_LAST() == farg): %s",
311 KASSERT(TAILQ_EMPTY(&pf_cachequeue) ||
312 TAILQ_LAST(&pf_cachequeue, pf_cachequeue) != frag);
318 * Try to flush old fragments to make space for new ones
322 pf_flush_fragments(void)
324 struct pf_fragment *frag;
328 goal = V_pf_nfrents * 9 / 10;
329 DPFPRINTF(("trying to free > %d frents\n",
330 V_pf_nfrents - goal));
331 while (goal < V_pf_nfrents) {
333 goal = pf_nfrents * 9 / 10;
334 DPFPRINTF(("trying to free > %d frents\n",
336 while (goal < pf_nfrents) {
339 frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue);
341 frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue);
345 pf_free_fragment(frag);
350 goal = V_pf_ncache * 9 / 10;
351 DPFPRINTF(("trying to free > %d cache entries\n",
352 V_pf_ncache - goal));
353 while (goal < V_pf_ncache) {
355 goal = pf_ncache * 9 / 10;
356 DPFPRINTF(("trying to free > %d cache entries\n",
358 while (goal < pf_ncache) {
361 frag = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue);
363 frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue);
367 pf_free_fragment(frag);
371 /* Frees the fragments and all associated entries */
374 pf_free_fragment(struct pf_fragment *frag)
376 struct pf_frent *frent;
377 struct pf_frcache *frcache;
379 /* Free all fragments */
380 if (BUFFER_FRAGMENTS(frag)) {
381 for (frent = LIST_FIRST(&frag->fr_queue); frent;
382 frent = LIST_FIRST(&frag->fr_queue)) {
383 LIST_REMOVE(frent, fr_next);
385 m_freem(frent->fr_m);
387 pool_put(&V_pf_frent_pl, frent);
390 pool_put(&pf_frent_pl, frent);
395 for (frcache = LIST_FIRST(&frag->fr_cache); frcache;
396 frcache = LIST_FIRST(&frag->fr_cache)) {
397 LIST_REMOVE(frcache, fr_next);
400 KASSERT((LIST_EMPTY(&frag->fr_cache) ||
401 LIST_FIRST(&frag->fr_cache)->fr_off >
403 ("! (LIST_EMPTY() || LIST_FIRST()->fr_off >"
404 " frcache->fr_end): %s", __FUNCTION__));
406 pool_put(&V_pf_cent_pl, frcache);
409 KASSERT(LIST_EMPTY(&frag->fr_cache) ||
410 LIST_FIRST(&frag->fr_cache)->fr_off >
413 pool_put(&pf_cent_pl, frcache);
419 pf_remove_fragment(frag);
423 pf_ip2key(struct pf_fragment *key, struct ip *ip)
425 key->fr_p = ip->ip_p;
426 key->fr_id = ip->ip_id;
427 key->fr_src.s_addr = ip->ip_src.s_addr;
428 key->fr_dst.s_addr = ip->ip_dst.s_addr;
432 pf_find_fragment(struct ip *ip, struct pf_frag_tree *tree)
434 struct pf_fragment key;
435 struct pf_fragment *frag;
439 frag = RB_FIND(pf_frag_tree, tree, &key);
441 /* XXX Are we sure we want to update the timeout? */
442 frag->fr_timeout = time_second;
443 if (BUFFER_FRAGMENTS(frag)) {
445 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
446 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
448 TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
449 TAILQ_INSERT_HEAD(&pf_fragqueue, frag, frag_next);
453 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
454 TAILQ_INSERT_HEAD(&V_pf_cachequeue, frag, frag_next);
456 TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
457 TAILQ_INSERT_HEAD(&pf_cachequeue, frag, frag_next);
465 /* Removes a fragment from the fragment queue and frees the fragment */
468 pf_remove_fragment(struct pf_fragment *frag)
470 if (BUFFER_FRAGMENTS(frag)) {
472 RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag);
473 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
474 pool_put(&V_pf_frag_pl, frag);
476 RB_REMOVE(pf_frag_tree, &pf_frag_tree, frag);
477 TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
478 pool_put(&pf_frag_pl, frag);
482 RB_REMOVE(pf_frag_tree, &V_pf_cache_tree, frag);
483 TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
484 pool_put(&V_pf_cache_pl, frag);
486 RB_REMOVE(pf_frag_tree, &pf_cache_tree, frag);
487 TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
488 pool_put(&pf_cache_pl, frag);
493 #define FR_IP_OFF(fr) ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
495 pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
496 struct pf_frent *frent, int mff)
498 struct mbuf *m = *m0, *m2;
499 struct pf_frent *frea, *next;
500 struct pf_frent *frep = NULL;
501 struct ip *ip = frent->fr_ip;
502 int hlen = ip->ip_hl << 2;
503 u_int16_t off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
504 u_int16_t ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
505 u_int16_t max = ip_len + off;
508 KASSERT((*frag == NULL || BUFFER_FRAGMENTS(*frag)),
509 ("! (*frag == NULL || BUFFER_FRAGMENTS(*frag)): %s", __FUNCTION__));
511 KASSERT(*frag == NULL || BUFFER_FRAGMENTS(*frag));
514 /* Strip off ip header */
518 /* Create a new reassembly queue for this packet */
521 *frag = pool_get(&V_pf_frag_pl, PR_NOWAIT);
523 *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
526 pf_flush_fragments();
528 *frag = pool_get(&V_pf_frag_pl, PR_NOWAIT);
530 *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
536 (*frag)->fr_flags = 0;
538 (*frag)->fr_src = frent->fr_ip->ip_src;
539 (*frag)->fr_dst = frent->fr_ip->ip_dst;
540 (*frag)->fr_p = frent->fr_ip->ip_p;
541 (*frag)->fr_id = frent->fr_ip->ip_id;
542 (*frag)->fr_timeout = time_second;
543 LIST_INIT(&(*frag)->fr_queue);
546 RB_INSERT(pf_frag_tree, &V_pf_frag_tree, *frag);
547 TAILQ_INSERT_HEAD(&V_pf_fragqueue, *frag, frag_next);
549 RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag);
550 TAILQ_INSERT_HEAD(&pf_fragqueue, *frag, frag_next);
553 /* We do not have a previous fragment */
559 * Find a fragment after the current one:
560 * - off contains the real shifted offset.
562 LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
563 if (FR_IP_OFF(frea) > off)
569 KASSERT((frep != NULL || frea != NULL),
570 ("!(frep != NULL || frea != NULL): %s", __FUNCTION__));;
572 KASSERT(frep != NULL || frea != NULL);
576 FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl *
581 precut = FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) -
582 frep->fr_ip->ip_hl * 4 - off;
583 if (precut >= ip_len)
585 m_adj(frent->fr_m, precut);
586 DPFPRINTF(("overlap -%d\n", precut));
587 /* Enforce 8 byte boundaries */
588 ip->ip_off = htons(ntohs(ip->ip_off) + (precut >> 3));
589 off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
591 ip->ip_len = htons(ip_len);
594 for (; frea != NULL && ip_len + off > FR_IP_OFF(frea);
599 aftercut = ip_len + off - FR_IP_OFF(frea);
600 DPFPRINTF(("adjust overlap %d\n", aftercut));
601 if (aftercut < ntohs(frea->fr_ip->ip_len) - frea->fr_ip->ip_hl
604 frea->fr_ip->ip_len =
605 htons(ntohs(frea->fr_ip->ip_len) - aftercut);
606 frea->fr_ip->ip_off = htons(ntohs(frea->fr_ip->ip_off) +
608 m_adj(frea->fr_m, aftercut);
612 /* This fragment is completely overlapped, lose it */
613 next = LIST_NEXT(frea, fr_next);
615 LIST_REMOVE(frea, fr_next);
617 pool_put(&V_pf_frent_pl, frea);
620 pool_put(&pf_frent_pl, frea);
626 /* Update maximum data size */
627 if ((*frag)->fr_max < max)
628 (*frag)->fr_max = max;
629 /* This is the last segment */
631 (*frag)->fr_flags |= PFFRAG_SEENLAST;
634 LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
636 LIST_INSERT_AFTER(frep, frent, fr_next);
638 /* Check if we are completely reassembled */
639 if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
642 /* Check if we have all the data */
644 for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) {
645 next = LIST_NEXT(frep, fr_next);
647 off += ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl * 4;
648 if (off < (*frag)->fr_max &&
649 (next == NULL || FR_IP_OFF(next) != off))
651 DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
652 off, next == NULL ? -1 : FR_IP_OFF(next),
657 DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
658 if (off < (*frag)->fr_max)
661 /* We have all the data */
662 frent = LIST_FIRST(&(*frag)->fr_queue);
664 KASSERT((frent != NULL), ("frent == NULL: %s", __FUNCTION__));
666 KASSERT(frent != NULL);
668 if ((frent->fr_ip->ip_hl << 2) + off > IP_MAXPACKET) {
669 DPFPRINTF(("drop: too big: %d\n", off));
670 pf_free_fragment(*frag);
674 next = LIST_NEXT(frent, fr_next);
676 /* Magic from ip_input */
683 pool_put(&V_pf_frent_pl, frent);
686 pool_put(&pf_frent_pl, frent);
689 for (frent = next; frent != NULL; frent = next) {
690 next = LIST_NEXT(frent, fr_next);
694 pool_put(&V_pf_frent_pl, frent);
697 pool_put(&pf_frent_pl, frent);
701 m->m_pkthdr.csum_flags &= m2->m_pkthdr.csum_flags;
702 m->m_pkthdr.csum_data += m2->m_pkthdr.csum_data;
708 while (m->m_pkthdr.csum_data & 0xffff0000)
709 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
710 (m->m_pkthdr.csum_data >> 16);
712 ip->ip_src = (*frag)->fr_src;
713 ip->ip_dst = (*frag)->fr_dst;
715 /* Remove from fragment queue */
716 pf_remove_fragment(*frag);
719 hlen = ip->ip_hl << 2;
720 ip->ip_len = htons(off + hlen);
724 /* some debugging cruft by sklower, below, will go away soon */
725 /* XXX this should be done elsewhere */
726 if (m->m_flags & M_PKTHDR) {
728 for (m2 = m; m2; m2 = m2->m_next)
730 m->m_pkthdr.len = plen;
733 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
737 /* Oops - fail safe - drop packet */
739 pool_put(&V_pf_frent_pl, frent);
742 pool_put(&pf_frent_pl, frent);
750 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
751 int drop, int *nomem)
753 struct mbuf *m = *m0;
754 struct pf_frcache *frp, *fra, *cur = NULL;
755 int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
756 u_int16_t off = ntohs(h->ip_off) << 3;
757 u_int16_t max = ip_len + off;
761 KASSERT((*frag == NULL || !BUFFER_FRAGMENTS(*frag)),
762 ("!(*frag == NULL || !BUFFER_FRAGMENTS(*frag)): %s", __FUNCTION__));
764 KASSERT(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
767 /* Create a new range queue for this packet */
770 *frag = pool_get(&V_pf_cache_pl, PR_NOWAIT);
772 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
775 pf_flush_fragments();
777 *frag = pool_get(&V_pf_cache_pl, PR_NOWAIT);
779 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
785 /* Get an entry for the queue */
787 cur = pool_get(&V_pf_cent_pl, PR_NOWAIT);
789 pool_put(&V_pf_cache_pl, *frag);
791 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
793 pool_put(&pf_cache_pl, *frag);
804 (*frag)->fr_flags = PFFRAG_NOBUFFER;
806 (*frag)->fr_src = h->ip_src;
807 (*frag)->fr_dst = h->ip_dst;
808 (*frag)->fr_p = h->ip_p;
809 (*frag)->fr_id = h->ip_id;
810 (*frag)->fr_timeout = time_second;
814 LIST_INIT(&(*frag)->fr_cache);
815 LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
818 RB_INSERT(pf_frag_tree, &V_pf_cache_tree, *frag);
819 TAILQ_INSERT_HEAD(&V_pf_cachequeue, *frag, frag_next);
821 RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag);
822 TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next);
825 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
831 * Find a fragment after the current one:
832 * - off contains the real shifted offset.
835 LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
836 if (fra->fr_off > off)
842 KASSERT((frp != NULL || fra != NULL),
843 ("!(frp != NULL || fra != NULL): %s", __FUNCTION__));
845 KASSERT(frp != NULL || fra != NULL);
851 precut = frp->fr_end - off;
852 if (precut >= ip_len) {
853 /* Fragment is entirely a duplicate */
854 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
855 h->ip_id, frp->fr_off, frp->fr_end, off, max));
859 /* They are adjacent. Fixup cache entry */
860 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
861 h->ip_id, frp->fr_off, frp->fr_end, off, max));
863 } else if (precut > 0) {
864 /* The first part of this payload overlaps with a
865 * fragment that has already been passed.
866 * Need to trim off the first part of the payload.
867 * But to do so easily, we need to create another
868 * mbuf to throw the original header into.
871 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
872 h->ip_id, precut, frp->fr_off, frp->fr_end, off,
877 /* Update the previous frag to encompass this one */
881 /* XXX Optimization opportunity
882 * This is a very heavy way to trim the payload.
883 * we could do it much faster by diddling mbuf
884 * internals but that would be even less legible
885 * than this mbuf magic. For my next trick,
886 * I'll pull a rabbit out of my laptop.
889 *m0 = m_dup(m, M_DONTWAIT);
891 *m0 = m_copym2(m, 0, h->ip_hl << 2, M_NOWAIT);
896 /* From KAME Project : We have missed this! */
897 m_adj(*m0, (h->ip_hl << 2) -
898 (*m0)->m_pkthdr.len);
900 KASSERT(((*m0)->m_next == NULL),
901 ("(*m0)->m_next != NULL: %s",
904 KASSERT((*m0)->m_next == NULL);
906 m_adj(m, precut + (h->ip_hl << 2));
909 if (m->m_flags & M_PKTHDR) {
912 for (t = m; t; t = t->m_next)
914 m->m_pkthdr.len = plen;
918 h = mtod(m, struct ip *);
921 KASSERT(((int)m->m_len ==
922 ntohs(h->ip_len) - precut),
923 ("m->m_len != ntohs(h->ip_len) - precut: %s",
926 KASSERT((int)m->m_len ==
927 ntohs(h->ip_len) - precut);
929 h->ip_off = htons(ntohs(h->ip_off) +
931 h->ip_len = htons(ntohs(h->ip_len) - precut);
936 /* There is a gap between fragments */
938 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
939 h->ip_id, -precut, frp->fr_off, frp->fr_end, off,
943 cur = pool_get(&V_pf_cent_pl, PR_NOWAIT);
945 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
957 LIST_INSERT_AFTER(frp, cur, fr_next);
965 aftercut = max - fra->fr_off;
967 /* Adjacent fragments */
968 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
969 h->ip_id, off, max, fra->fr_off, fra->fr_end));
972 } else if (aftercut > 0) {
973 /* Need to chop off the tail of this fragment */
974 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
975 h->ip_id, aftercut, off, max, fra->fr_off,
984 if (m->m_flags & M_PKTHDR) {
987 for (t = m; t; t = t->m_next)
989 m->m_pkthdr.len = plen;
991 h = mtod(m, struct ip *);
993 KASSERT(((int)m->m_len == ntohs(h->ip_len) - aftercut),
994 ("m->m_len != ntohs(h->ip_len) - aftercut: %s",
997 KASSERT((int)m->m_len ==
998 ntohs(h->ip_len) - aftercut);
1000 h->ip_len = htons(ntohs(h->ip_len) - aftercut);
1004 } else if (frp == NULL) {
1005 /* There is a gap between fragments */
1006 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
1007 h->ip_id, -aftercut, off, max, fra->fr_off,
1011 cur = pool_get(&V_pf_cent_pl, PR_NOWAIT);
1013 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
1025 LIST_INSERT_BEFORE(fra, cur, fr_next);
1029 /* Need to glue together two separate fragment descriptors */
1031 if (cur && fra->fr_off <= cur->fr_end) {
1032 /* Need to merge in a previous 'cur' */
1033 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1034 "%d-%d) %d-%d (%d-%d)\n",
1035 h->ip_id, cur->fr_off, cur->fr_end, off,
1036 max, fra->fr_off, fra->fr_end));
1037 fra->fr_off = cur->fr_off;
1038 LIST_REMOVE(cur, fr_next);
1040 pool_put(&V_pf_cent_pl, cur);
1043 pool_put(&pf_cent_pl, cur);
1048 } else if (frp && fra->fr_off <= frp->fr_end) {
1049 /* Need to merge in a modified 'frp' */
1051 KASSERT((cur == NULL), ("cur != NULL: %s",
1054 KASSERT(cur == NULL);
1056 DPFPRINTF(("fragcache[%d]: adjacent(merge "
1057 "%d-%d) %d-%d (%d-%d)\n",
1058 h->ip_id, frp->fr_off, frp->fr_end, off,
1059 max, fra->fr_off, fra->fr_end));
1060 fra->fr_off = frp->fr_off;
1061 LIST_REMOVE(frp, fr_next);
1063 pool_put(&V_pf_cent_pl, frp);
1066 pool_put(&pf_cent_pl, frp);
1077 * We must keep tracking the overall fragment even when
1078 * we're going to drop it anyway so that we know when to
1079 * free the overall descriptor. Thus we drop the frag late.
1086 /* Update maximum data size */
1087 if ((*frag)->fr_max < max)
1088 (*frag)->fr_max = max;
1090 /* This is the last segment */
1092 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1094 /* Check if we are completely reassembled */
1095 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
1096 LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
1097 LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) {
1098 /* Remove from fragment queue */
1099 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
1101 pf_free_fragment(*frag);
1110 /* Still need to pay attention to !IP_MF */
1111 if (!mff && *frag != NULL)
1112 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1119 /* Still need to pay attention to !IP_MF */
1120 if (!mff && *frag != NULL)
1121 (*frag)->fr_flags |= PFFRAG_SEENLAST;
1124 /* This fragment has been deemed bad. Don't reass */
1125 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
1126 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
1128 (*frag)->fr_flags |= PFFRAG_DROP;
1137 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
1138 struct pf_pdesc *pd)
1140 struct mbuf *m = *m0;
1142 struct pf_frent *frent;
1143 struct pf_fragment *frag = NULL;
1144 struct ip *h = mtod(m, struct ip *);
1145 int mff = (ntohs(h->ip_off) & IP_MF);
1146 int hlen = h->ip_hl << 2;
1147 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1153 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1156 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1157 r = r->skip[PF_SKIP_IFP].ptr;
1158 else if (r->direction && r->direction != dir)
1159 r = r->skip[PF_SKIP_DIR].ptr;
1160 else if (r->af && r->af != AF_INET)
1161 r = r->skip[PF_SKIP_AF].ptr;
1162 else if (r->proto && r->proto != h->ip_p)
1163 r = r->skip[PF_SKIP_PROTO].ptr;
1164 else if (PF_MISMATCHAW(&r->src.addr,
1165 (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
1166 r->src.neg, kif, M_GETFIB(m)))
1167 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1168 else if (PF_MISMATCHAW(&r->dst.addr,
1169 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
1170 r->dst.neg, NULL, M_GETFIB(m)))
1171 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1173 else if (r->match_tag && !pf_match_tag(m, r, &tag, pd->pf_mtag))
1175 else if (r->match_tag && !pf_match_tag(m, r, &tag))
1177 r = TAILQ_NEXT(r, entries);
1182 if (r == NULL || r->action == PF_NOSCRUB)
1185 r->packets[dir == PF_OUT]++;
1186 r->bytes[dir == PF_OUT] += pd->tot_len;
1189 /* Check for illegal packets */
1190 if (hlen < (int)sizeof(struct ip))
1193 if (hlen > ntohs(h->ip_len))
1196 /* Clear IP_DF if the rule uses the no-df option */
1197 if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
1198 u_int16_t ip_off = h->ip_off;
1200 h->ip_off &= htons(~IP_DF);
1201 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1204 /* We will need other tests here */
1205 if (!fragoff && !mff)
1208 /* We're dealing with a fragment now. Don't allow fragments
1209 * with IP_DF to enter the cache. If the flag was cleared by
1210 * no-df above, fine. Otherwise drop it.
1212 if (h->ip_off & htons(IP_DF)) {
1213 DPFPRINTF(("IP_DF\n"));
1217 ip_len = ntohs(h->ip_len) - hlen;
1218 ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1220 /* All fragments are 8 byte aligned */
1221 if (mff && (ip_len & 0x7)) {
1222 DPFPRINTF(("mff and %d\n", ip_len));
1226 /* Respect maximum length */
1227 if (fragoff + ip_len > IP_MAXPACKET) {
1228 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
1231 max = fragoff + ip_len;
1233 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
1234 /* Fully buffer all of the fragments */
1237 frag = pf_find_fragment(h, &V_pf_frag_tree);
1239 frag = pf_find_fragment(h, &pf_frag_tree);
1242 /* Check if we saw the last fragment already */
1243 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1247 /* Get an entry for the fragment queue */
1249 frent = pool_get(&V_pf_frent_pl, PR_NOWAIT);
1251 frent = pool_get(&pf_frent_pl, PR_NOWAIT);
1253 if (frent == NULL) {
1254 REASON_SET(reason, PFRES_MEMORY);
1265 /* Might return a completely reassembled mbuf, or NULL */
1266 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
1267 *m0 = m = pf_reassemble(m0, &frag, frent, mff);
1272 /* use mtag from concatenated mbuf chain */
1273 pd->pf_mtag = pf_find_mtag(m);
1275 if (pd->pf_mtag == NULL) {
1276 printf("%s: pf_find_mtag returned NULL(1)\n", __func__);
1277 if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
1284 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1287 h = mtod(m, struct ip *);
1289 /* non-buffering fragment cache (drops or masks overlaps) */
1293 if (dir == PF_OUT && pd->pf_mtag->flags & PF_TAG_FRAGCACHE) {
1295 if (dir == PF_OUT && m->m_pkthdr.pf.flags & PF_TAG_FRAGCACHE) {
1298 * Already passed the fragment cache in the
1299 * input direction. If we continued, it would
1300 * appear to be a dup and would be dropped.
1306 frag = pf_find_fragment(h, &V_pf_cache_tree);
1308 frag = pf_find_fragment(h, &pf_cache_tree);
1311 /* Check if we saw the last fragment already */
1312 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1313 max > frag->fr_max) {
1314 if (r->rule_flag & PFRULE_FRAGDROP)
1315 frag->fr_flags |= PFFRAG_DROP;
1319 *m0 = m = pf_fragcache(m0, h, &frag, mff,
1320 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
1327 /* use mtag from copied and trimmed mbuf chain */
1328 pd->pf_mtag = pf_find_mtag(m);
1330 if (pd->pf_mtag == NULL) {
1331 printf("%s: pf_find_mtag returned NULL(2)\n", __func__);
1332 if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) {
1341 pd->pf_mtag->flags |= PF_TAG_FRAGCACHE;
1343 m->m_pkthdr.pf.flags |= PF_TAG_FRAGCACHE;
1346 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1352 /* At this point, only IP_DF is allowed in ip_off */
1353 if (h->ip_off & ~htons(IP_DF)) {
1354 u_int16_t ip_off = h->ip_off;
1356 h->ip_off &= htons(IP_DF);
1357 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1360 /* not missing a return here */
1363 pf_scrub_ip(&m, r->rule_flag, r->min_ttl, r->set_tos);
1365 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1366 pd->flags |= PFDESC_IP_REAS;
1370 REASON_SET(reason, PFRES_MEMORY);
1371 if (r != NULL && r->log)
1372 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1376 REASON_SET(reason, PFRES_NORM);
1377 if (r != NULL && r->log)
1378 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1382 DPFPRINTF(("dropping bad fragment\n"));
1384 /* Free associated fragments */
1386 pf_free_fragment(frag);
1388 REASON_SET(reason, PFRES_FRAG);
1389 if (r != NULL && r->log)
1390 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd);
1398 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1399 u_short *reason, struct pf_pdesc *pd)
1401 struct mbuf *m = *m0;
1403 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1407 struct ip6_opt_jumbo jumbo;
1408 struct ip6_frag frag;
1409 u_int32_t jumbolen = 0, plen;
1410 u_int16_t fragoff = 0;
1416 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1419 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1420 r = r->skip[PF_SKIP_IFP].ptr;
1421 else if (r->direction && r->direction != dir)
1422 r = r->skip[PF_SKIP_DIR].ptr;
1423 else if (r->af && r->af != AF_INET6)
1424 r = r->skip[PF_SKIP_AF].ptr;
1425 #if 0 /* header chain! */
1426 else if (r->proto && r->proto != h->ip6_nxt)
1427 r = r->skip[PF_SKIP_PROTO].ptr;
1429 else if (PF_MISMATCHAW(&r->src.addr,
1430 (struct pf_addr *)&h->ip6_src, AF_INET6,
1431 r->src.neg, kif, M_GETFIB(m)))
1432 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1433 else if (PF_MISMATCHAW(&r->dst.addr,
1434 (struct pf_addr *)&h->ip6_dst, AF_INET6,
1435 r->dst.neg, NULL, M_GETFIB(m)))
1436 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1441 if (r == NULL || r->action == PF_NOSCRUB)
1444 r->packets[dir == PF_OUT]++;
1445 r->bytes[dir == PF_OUT] += pd->tot_len;
1448 /* Check for illegal packets */
1449 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1452 off = sizeof(struct ip6_hdr);
1457 case IPPROTO_FRAGMENT:
1461 case IPPROTO_ROUTING:
1462 case IPPROTO_DSTOPTS:
1463 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1466 if (proto == IPPROTO_AH)
1467 off += (ext.ip6e_len + 2) * 4;
1469 off += (ext.ip6e_len + 1) * 8;
1470 proto = ext.ip6e_nxt;
1472 case IPPROTO_HOPOPTS:
1473 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1476 optend = off + (ext.ip6e_len + 1) * 8;
1477 ooff = off + sizeof(ext);
1479 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1480 sizeof(opt.ip6o_type), NULL, NULL,
1483 if (opt.ip6o_type == IP6OPT_PAD1) {
1487 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1488 NULL, NULL, AF_INET6))
1490 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1492 switch (opt.ip6o_type) {
1494 if (h->ip6_plen != 0)
1496 if (!pf_pull_hdr(m, ooff, &jumbo,
1497 sizeof(jumbo), NULL, NULL,
1500 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1502 jumbolen = ntohl(jumbolen);
1503 if (jumbolen <= IPV6_MAXPACKET)
1505 if (sizeof(struct ip6_hdr) + jumbolen !=
1512 ooff += sizeof(opt) + opt.ip6o_len;
1513 } while (ooff < optend);
1516 proto = ext.ip6e_nxt;
1522 } while (!terminal);
1524 /* jumbo payload option must be present, or plen > 0 */
1525 if (ntohs(h->ip6_plen) == 0)
1528 plen = ntohs(h->ip6_plen);
1531 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1534 pf_scrub_ip6(&m, r->min_ttl);
1539 if (ntohs(h->ip6_plen) == 0 || jumbolen)
1541 plen = ntohs(h->ip6_plen);
1543 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1545 fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
1546 if (fragoff + (plen - off - sizeof(frag)) > IPV6_MAXPACKET)
1549 /* do something about it */
1550 /* remember to set pd->flags |= PFDESC_IP_REAS */
1554 REASON_SET(reason, PFRES_SHORT);
1555 if (r != NULL && r->log)
1556 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1560 REASON_SET(reason, PFRES_NORM);
1561 if (r != NULL && r->log)
1562 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1566 REASON_SET(reason, PFRES_FRAG);
1567 if (r != NULL && r->log)
1568 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL, pd);
1574 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1575 int off, void *h, struct pf_pdesc *pd)
1577 struct pf_rule *r, *rm = NULL;
1578 struct tcphdr *th = pd->hdr.tcp;
1582 sa_family_t af = pd->af;
1584 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1587 if (pfi_kif_match(r->kif, kif) == r->ifnot)
1588 r = r->skip[PF_SKIP_IFP].ptr;
1589 else if (r->direction && r->direction != dir)
1590 r = r->skip[PF_SKIP_DIR].ptr;
1591 else if (r->af && r->af != af)
1592 r = r->skip[PF_SKIP_AF].ptr;
1593 else if (r->proto && r->proto != pd->proto)
1594 r = r->skip[PF_SKIP_PROTO].ptr;
1595 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1596 r->src.neg, kif, M_GETFIB(m)))
1597 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1598 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1599 r->src.port[0], r->src.port[1], th->th_sport))
1600 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1601 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1602 r->dst.neg, NULL, M_GETFIB(m)))
1603 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1604 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1605 r->dst.port[0], r->dst.port[1], th->th_dport))
1606 r = r->skip[PF_SKIP_DST_PORT].ptr;
1607 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1608 pf_osfp_fingerprint(pd, m, off, th),
1610 r = TAILQ_NEXT(r, entries);
1617 if (rm == NULL || rm->action == PF_NOSCRUB)
1620 r->packets[dir == PF_OUT]++;
1621 r->bytes[dir == PF_OUT] += pd->tot_len;
1624 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1625 pd->flags |= PFDESC_TCP_NORM;
1627 flags = th->th_flags;
1628 if (flags & TH_SYN) {
1629 /* Illegal packet */
1636 /* Illegal packet */
1637 if (!(flags & (TH_ACK|TH_RST)))
1641 if (!(flags & TH_ACK)) {
1642 /* These flags are only valid if ACK is set */
1643 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1647 /* Check for illegal header length */
1648 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1651 /* If flags changed, or reserved data set, then adjust */
1652 if (flags != th->th_flags || th->th_x2 != 0) {
1655 ov = *(u_int16_t *)(&th->th_ack + 1);
1656 th->th_flags = flags;
1658 nv = *(u_int16_t *)(&th->th_ack + 1);
1660 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1664 /* Remove urgent pointer, if TH_URG is not set */
1665 if (!(flags & TH_URG) && th->th_urp) {
1666 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1671 /* Process options */
1672 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af))
1675 /* copy back packet headers if we sanitized */
1678 m_copyback(m, off, sizeof(*th), (caddr_t)th);
1680 m_copyback(m, off, sizeof(*th), th);
1686 REASON_SET(&reason, PFRES_NORM);
1687 if (rm != NULL && r->log)
1688 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL, pd);
1693 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1694 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1696 u_int32_t tsval, tsecr;
1701 KASSERT((src->scrub == NULL),
1702 ("pf_normalize_tcp_init: src->scrub != NULL"));
1704 src->scrub = pool_get(&V_pf_state_scrub_pl, PR_NOWAIT);
1706 KASSERT(src->scrub == NULL);
1708 src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
1710 if (src->scrub == NULL)
1712 bzero(src->scrub, sizeof(*src->scrub));
1717 struct ip *h = mtod(m, struct ip *);
1718 src->scrub->pfss_ttl = h->ip_ttl;
1724 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1725 src->scrub->pfss_ttl = h->ip6_hlim;
1733 * All normalizations below are only begun if we see the start of
1734 * the connections. They must all set an enabled bit in pfss_flags
1736 if ((th->th_flags & TH_SYN) == 0)
1740 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1741 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1742 /* Diddle with TCP options */
1744 opt = hdr + sizeof(struct tcphdr);
1745 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1746 while (hlen >= TCPOLEN_TIMESTAMP) {
1748 case TCPOPT_EOL: /* FALLTHROUGH */
1753 case TCPOPT_TIMESTAMP:
1754 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1755 src->scrub->pfss_flags |=
1757 src->scrub->pfss_ts_mod =
1758 htonl(arc4random());
1760 /* note PFSS_PAWS not set yet */
1761 memcpy(&tsval, &opt[2],
1763 memcpy(&tsecr, &opt[6],
1765 src->scrub->pfss_tsval0 = ntohl(tsval);
1766 src->scrub->pfss_tsval = ntohl(tsval);
1767 src->scrub->pfss_tsecr = ntohl(tsecr);
1768 getmicrouptime(&src->scrub->pfss_last);
1772 hlen -= MAX(opt[1], 2);
1773 opt += MAX(opt[1], 2);
1783 pf_normalize_tcp_cleanup(struct pf_state *state)
1786 if (state->src.scrub)
1787 pool_put(&V_pf_state_scrub_pl, state->src.scrub);
1788 if (state->dst.scrub)
1789 pool_put(&V_pf_state_scrub_pl, state->dst.scrub);
1791 if (state->src.scrub)
1792 pool_put(&pf_state_scrub_pl, state->src.scrub);
1793 if (state->dst.scrub)
1794 pool_put(&pf_state_scrub_pl, state->dst.scrub);
1797 /* Someday... flush the TCP segment reassembly descriptors. */
1801 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1802 u_short *reason, struct tcphdr *th, struct pf_state *state,
1803 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1805 struct timeval uptime;
1806 u_int32_t tsval, tsecr;
1807 u_int tsval_from_last;
1814 KASSERT((src->scrub || dst->scrub),
1815 ("pf_normalize_tcp_statefull: src->scrub && dst->scrub!"));
1817 KASSERT(src->scrub || dst->scrub);
1821 * Enforce the minimum TTL seen for this connection. Negate a common
1822 * technique to evade an intrusion detection system and confuse
1823 * firewall state code.
1829 struct ip *h = mtod(m, struct ip *);
1830 if (h->ip_ttl > src->scrub->pfss_ttl)
1831 src->scrub->pfss_ttl = h->ip_ttl;
1832 h->ip_ttl = src->scrub->pfss_ttl;
1840 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1841 if (h->ip6_hlim > src->scrub->pfss_ttl)
1842 src->scrub->pfss_ttl = h->ip6_hlim;
1843 h->ip6_hlim = src->scrub->pfss_ttl;
1850 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1851 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1852 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1853 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1854 /* Diddle with TCP options */
1856 opt = hdr + sizeof(struct tcphdr);
1857 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1858 while (hlen >= TCPOLEN_TIMESTAMP) {
1860 case TCPOPT_EOL: /* FALLTHROUGH */
1865 case TCPOPT_TIMESTAMP:
1866 /* Modulate the timestamps. Can be used for
1867 * NAT detection, OS uptime determination or
1872 /* Huh? Multiple timestamps!? */
1874 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1876 if (pf_status.debug >= PF_DEBUG_MISC) {
1878 DPFPRINTF(("multiple TS??"));
1879 pf_print_state(state);
1882 REASON_SET(reason, PFRES_TS);
1885 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1886 memcpy(&tsval, &opt[2],
1888 if (tsval && src->scrub &&
1889 (src->scrub->pfss_flags &
1891 tsval = ntohl(tsval);
1892 pf_change_a(&opt[2],
1895 src->scrub->pfss_ts_mod),
1900 /* Modulate TS reply iff valid (!0) */
1901 memcpy(&tsecr, &opt[6],
1903 if (tsecr && dst->scrub &&
1904 (dst->scrub->pfss_flags &
1906 tsecr = ntohl(tsecr)
1907 - dst->scrub->pfss_ts_mod;
1908 pf_change_a(&opt[6],
1909 &th->th_sum, htonl(tsecr),
1917 hlen -= MAX(opt[1], 2);
1918 opt += MAX(opt[1], 2);
1923 /* Copyback the options, caller copys back header */
1925 m_copyback(m, off + sizeof(struct tcphdr),
1926 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1927 sizeof(struct tcphdr));
1933 * Must invalidate PAWS checks on connections idle for too long.
1934 * The fastest allowed timestamp clock is 1ms. That turns out to
1935 * be about 24 days before it wraps. XXX Right now our lowerbound
1936 * TS echo check only works for the first 12 days of a connection
1937 * when the TS has exhausted half its 32bit space
1939 #define TS_MAX_IDLE (24*24*60*60)
1940 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1942 getmicrouptime(&uptime);
1943 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1944 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1945 time_second - state->creation > TS_MAX_CONN)) {
1947 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1949 if (pf_status.debug >= PF_DEBUG_MISC) {
1951 DPFPRINTF(("src idled out of PAWS\n"));
1952 pf_print_state(state);
1955 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1958 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1959 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1961 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1963 if (pf_status.debug >= PF_DEBUG_MISC) {
1965 DPFPRINTF(("dst idled out of PAWS\n"));
1966 pf_print_state(state);
1969 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1973 if (got_ts && src->scrub && dst->scrub &&
1974 (src->scrub->pfss_flags & PFSS_PAWS) &&
1975 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1976 /* Validate that the timestamps are "in-window".
1977 * RFC1323 describes TCP Timestamp options that allow
1978 * measurement of RTT (round trip time) and PAWS
1979 * (protection against wrapped sequence numbers). PAWS
1980 * gives us a set of rules for rejecting packets on
1981 * long fat pipes (packets that were somehow delayed
1982 * in transit longer than the time it took to send the
1983 * full TCP sequence space of 4Gb). We can use these
1984 * rules and infer a few others that will let us treat
1985 * the 32bit timestamp and the 32bit echoed timestamp
1986 * as sequence numbers to prevent a blind attacker from
1987 * inserting packets into a connection.
1990 * - The timestamp on this packet must be greater than
1991 * or equal to the last value echoed by the other
1992 * endpoint. The RFC says those will be discarded
1993 * since it is a dup that has already been acked.
1994 * This gives us a lowerbound on the timestamp.
1995 * timestamp >= other last echoed timestamp
1996 * - The timestamp will be less than or equal to
1997 * the last timestamp plus the time between the
1998 * last packet and now. The RFC defines the max
1999 * clock rate as 1ms. We will allow clocks to be
2000 * up to 10% fast and will allow a total difference
2001 * or 30 seconds due to a route change. And this
2002 * gives us an upperbound on the timestamp.
2003 * timestamp <= last timestamp + max ticks
2004 * We have to be careful here. Windows will send an
2005 * initial timestamp of zero and then initialize it
2006 * to a random value after the 3whs; presumably to
2007 * avoid a DoS by having to call an expensive RNG
2008 * during a SYN flood. Proof MS has at least one
2009 * good security geek.
2011 * - The TCP timestamp option must also echo the other
2012 * endpoints timestamp. The timestamp echoed is the
2013 * one carried on the earliest unacknowledged segment
2014 * on the left edge of the sequence window. The RFC
2015 * states that the host will reject any echoed
2016 * timestamps that were larger than any ever sent.
2017 * This gives us an upperbound on the TS echo.
2018 * tescr <= largest_tsval
2019 * - The lowerbound on the TS echo is a little more
2020 * tricky to determine. The other endpoint's echoed
2021 * values will not decrease. But there may be
2022 * network conditions that re-order packets and
2023 * cause our view of them to decrease. For now the
2024 * only lowerbound we can safely determine is that
2025 * the TS echo will never be less than the original
2026 * TS. XXX There is probably a better lowerbound.
2027 * Remove TS_MAX_CONN with better lowerbound check.
2028 * tescr >= other original TS
2030 * It is also important to note that the fastest
2031 * timestamp clock of 1ms will wrap its 32bit space in
2032 * 24 days. So we just disable TS checking after 24
2033 * days of idle time. We actually must use a 12d
2034 * connection limit until we can come up with a better
2035 * lowerbound to the TS echo check.
2037 struct timeval delta_ts;
2042 * PFTM_TS_DIFF is how many seconds of leeway to allow
2043 * a host's timestamp. This can happen if the previous
2044 * packet got delayed in transit for much longer than
2047 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
2049 ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF];
2051 ts_fudge = pf_default_rule.timeout[PFTM_TS_DIFF];
2055 /* Calculate max ticks since the last timestamp */
2056 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
2057 #define TS_MICROSECS 1000000 /* microseconds per second */
2060 #define timersub(tvp, uvp, vvp) \
2062 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2063 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2064 if ((vvp)->tv_usec < 0) { \
2066 (vvp)->tv_usec += 1000000; \
2071 timersub(&uptime, &src->scrub->pfss_last, &delta_ts);
2072 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
2073 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
2076 if ((src->state >= TCPS_ESTABLISHED &&
2077 dst->state >= TCPS_ESTABLISHED) &&
2078 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
2079 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
2080 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
2081 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
2082 /* Bad RFC1323 implementation or an insertion attack.
2084 * - Solaris 2.6 and 2.7 are known to send another ACK
2085 * after the FIN,FIN|ACK,ACK closing that carries
2089 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
2090 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
2091 SEQ_GT(tsval, src->scrub->pfss_tsval +
2092 tsval_from_last) ? '1' : ' ',
2093 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
2094 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
2096 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u "
2097 "idle: %jus %lums\n",
2098 tsval, tsecr, tsval_from_last,
2099 (uintmax_t)delta_ts.tv_sec,
2100 delta_ts.tv_usec / 1000));
2101 DPFPRINTF((" src->tsval: %u tsecr: %u\n",
2102 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
2103 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u"
2104 "\n", dst->scrub->pfss_tsval,
2105 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
2107 DPFPRINTF((" tsval: %lu tsecr: %lu +ticks: %lu "
2108 "idle: %lus %lums\n",
2109 tsval, tsecr, tsval_from_last, delta_ts.tv_sec,
2110 delta_ts.tv_usec / 1000));
2111 DPFPRINTF((" src->tsval: %lu tsecr: %lu\n",
2112 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
2113 DPFPRINTF((" dst->tsval: %lu tsecr: %lu tsval0: %lu"
2114 "\n", dst->scrub->pfss_tsval,
2115 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
2118 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2120 if (pf_status.debug >= PF_DEBUG_MISC) {
2122 pf_print_state(state);
2123 pf_print_flags(th->th_flags);
2126 REASON_SET(reason, PFRES_TS);
2130 /* XXX I'd really like to require tsecr but it's optional */
2132 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
2133 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
2134 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
2135 src->scrub && dst->scrub &&
2136 (src->scrub->pfss_flags & PFSS_PAWS) &&
2137 (dst->scrub->pfss_flags & PFSS_PAWS)) {
2138 /* Didn't send a timestamp. Timestamps aren't really useful
2140 * - connection opening or closing (often not even sent).
2141 * but we must not let an attacker to put a FIN on a
2142 * data packet to sneak it through our ESTABLISHED check.
2143 * - on a TCP reset. RFC suggests not even looking at TS.
2144 * - on an empty ACK. The TS will not be echoed so it will
2145 * probably not help keep the RTT calculation in sync and
2146 * there isn't as much danger when the sequence numbers
2147 * got wrapped. So some stacks don't include TS on empty
2150 * To minimize the disruption to mostly RFC1323 conformant
2151 * stacks, we will only require timestamps on data packets.
2153 * And what do ya know, we cannot require timestamps on data
2154 * packets. There appear to be devices that do legitimate
2155 * TCP connection hijacking. There are HTTP devices that allow
2156 * a 3whs (with timestamps) and then buffer the HTTP request.
2157 * If the intermediate device has the HTTP response cache, it
2158 * will spoof the response but not bother timestamping its
2159 * packets. So we can look for the presence of a timestamp in
2160 * the first data packet and if there, require it in all future
2164 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
2166 * Hey! Someone tried to sneak a packet in. Or the
2167 * stack changed its RFC1323 behavior?!?!
2170 if (V_pf_status.debug >= PF_DEBUG_MISC) {
2172 if (pf_status.debug >= PF_DEBUG_MISC) {
2174 DPFPRINTF(("Did not receive expected RFC1323 "
2176 pf_print_state(state);
2177 pf_print_flags(th->th_flags);
2180 REASON_SET(reason, PFRES_TS);
2187 * We will note if a host sends his data packets with or without
2188 * timestamps. And require all data packets to contain a timestamp
2189 * if the first does. PAWS implicitly requires that all data packets be
2190 * timestamped. But I think there are middle-man devices that hijack
2191 * TCP streams immediately after the 3whs and don't timestamp their
2192 * packets (seen in a WWW accelerator or cache).
2194 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
2195 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
2197 src->scrub->pfss_flags |= PFSS_DATA_TS;
2199 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
2201 if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
2203 if (pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
2205 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
2206 /* Don't warn if other host rejected RFC1323 */
2207 DPFPRINTF(("Broken RFC1323 stack did not "
2208 "timestamp data packet. Disabled PAWS "
2210 pf_print_state(state);
2211 pf_print_flags(th->th_flags);
2219 * Update PAWS values
2221 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
2222 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
2223 getmicrouptime(&src->scrub->pfss_last);
2224 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
2225 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2226 src->scrub->pfss_tsval = tsval;
2229 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
2230 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2231 src->scrub->pfss_tsecr = tsecr;
2233 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
2234 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
2235 src->scrub->pfss_tsval0 == 0)) {
2236 /* tsval0 MUST be the lowest timestamp */
2237 src->scrub->pfss_tsval0 = tsval;
2240 /* Only fully initialized after a TS gets echoed */
2241 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
2242 src->scrub->pfss_flags |= PFSS_PAWS;
2246 /* I have a dream.... TCP segment reassembly.... */
2251 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
2252 int off, sa_family_t af)
2256 int opt, cnt, optlen = 0;
2259 u_char opts[TCP_MAXOLEN];
2261 u_char opts[MAX_TCPOPTLEN];
2263 u_char *optp = opts;
2265 thoff = th->th_off << 2;
2266 cnt = thoff - sizeof(struct tcphdr);
2268 if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
2272 for (; cnt > 0; cnt -= optlen, optp += optlen) {
2274 if (opt == TCPOPT_EOL)
2276 if (opt == TCPOPT_NOP)
2282 if (optlen < 2 || optlen > cnt)
2287 mss = (u_int16_t *)(optp + 2);
2288 if ((ntohs(*mss)) > r->max_mss) {
2289 th->th_sum = pf_cksum_fixup(th->th_sum,
2290 *mss, htons(r->max_mss), 0);
2291 *mss = htons(r->max_mss);
2301 m_copyback(m, off + sizeof(*th), thoff - sizeof(*th), opts);
2307 pf_scrub_ip(struct mbuf **m0, u_int32_t flags, u_int8_t min_ttl, u_int8_t tos)
2309 struct mbuf *m = *m0;
2310 struct ip *h = mtod(m, struct ip *);
2312 /* Clear IP_DF if no-df was requested */
2313 if (flags & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
2314 u_int16_t ip_off = h->ip_off;
2316 h->ip_off &= htons(~IP_DF);
2317 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
2320 /* Enforce a minimum ttl, may cause endless packet loops */
2321 if (min_ttl && h->ip_ttl < min_ttl) {
2322 u_int16_t ip_ttl = h->ip_ttl;
2324 h->ip_ttl = min_ttl;
2325 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
2329 if (flags & PFRULE_SET_TOS) {
2332 ov = *(u_int16_t *)h;
2334 nv = *(u_int16_t *)h;
2336 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
2339 /* random-id, but not for fragments */
2340 if (flags & PFRULE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) {
2341 u_int16_t ip_id = h->ip_id;
2343 h->ip_id = ip_randomid();
2344 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
2350 pf_scrub_ip6(struct mbuf **m0, u_int8_t min_ttl)
2352 struct mbuf *m = *m0;
2353 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
2355 /* Enforce a minimum ttl, may cause endless packet loops */
2356 if (min_ttl && h->ip6_hlim < min_ttl)
2357 h->ip6_hlim = min_ttl;