xref: /freebsd/sys/netpfil/pf/pf_norm.c (revision 5f0216bd883edee71bf81051e3c20505e4820903)
1 /*-
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * Copyright 2011 Alexander Bluhm <bluhm@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  *	$OpenBSD: pf_norm.c,v 1.114 2009/01/29 14:11:45 henning Exp $
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_pf.h"
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/mutex.h>
41 #include <sys/refcount.h>
42 #include <sys/rwlock.h>
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/vnet.h>
47 #include <net/pfvar.h>
48 #include <net/if_pflog.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_seq.h>
57 
58 #ifdef INET6
59 #include <netinet/ip6.h>
60 #endif /* INET6 */
61 
62 struct pf_frent {
63 	TAILQ_ENTRY(pf_frent)	fr_next;
64 	struct mbuf	*fe_m;
65 	uint16_t	fe_hdrlen;	/* ipv4 header lenght with ip options
66 					   ipv6, extension, fragment header */
67 	uint16_t	fe_extoff;	/* last extension header offset or 0 */
68 	uint16_t	fe_len;		/* fragment length */
69 	uint16_t	fe_off;		/* fragment offset */
70 	uint16_t	fe_mff;		/* more fragment flag */
71 };
72 
73 struct pf_fragment_cmp {
74 	struct pf_addr	frc_src;
75 	struct pf_addr	frc_dst;
76 	uint32_t	frc_id;
77 	sa_family_t	frc_af;
78 	uint8_t		frc_proto;
79 	uint8_t		frc_direction;
80 };
81 
82 struct pf_fragment {
83 	struct pf_fragment_cmp	fr_key;
84 #define fr_src	fr_key.frc_src
85 #define fr_dst	fr_key.frc_dst
86 #define fr_id	fr_key.frc_id
87 #define fr_af	fr_key.frc_af
88 #define fr_proto	fr_key.frc_proto
89 #define fr_direction	fr_key.frc_direction
90 
91 	RB_ENTRY(pf_fragment) fr_entry;
92 	TAILQ_ENTRY(pf_fragment) frag_next;
93 	uint8_t		fr_flags;	/* status flags */
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))
98 	uint16_t	fr_max;		/* fragment data max */
99 	uint32_t	fr_timeout;
100 	uint16_t	fr_maxlen;	/* maximum length of single fragment */
101 	TAILQ_HEAD(pf_fragq, pf_frent) fr_queue;
102 };
103 
104 struct pf_fragment_tag {
105 	uint16_t	ft_hdrlen;	/* header length of reassembled pkt */
106 	uint16_t	ft_extoff;	/* last extension header offset or 0 */
107 	uint16_t	ft_maxlen;	/* maximum fragment payload length */
108 	uint32_t	ft_id;		/* fragment id */
109 };
110 
111 static struct mtx pf_frag_mtx;
112 MTX_SYSINIT(pf_frag_mtx, &pf_frag_mtx, "pf fragments", MTX_DEF);
113 #define PF_FRAG_LOCK()		mtx_lock(&pf_frag_mtx)
114 #define PF_FRAG_UNLOCK()	mtx_unlock(&pf_frag_mtx)
115 #define PF_FRAG_ASSERT()	mtx_assert(&pf_frag_mtx, MA_OWNED)
116 
117 VNET_DEFINE(uma_zone_t, pf_state_scrub_z);	/* XXX: shared with pfsync */
118 
119 static VNET_DEFINE(uma_zone_t, pf_frent_z);
120 #define	V_pf_frent_z	VNET(pf_frent_z)
121 static VNET_DEFINE(uma_zone_t, pf_frag_z);
122 #define	V_pf_frag_z	VNET(pf_frag_z)
123 
124 TAILQ_HEAD(pf_fragqueue, pf_fragment);
125 TAILQ_HEAD(pf_cachequeue, pf_fragment);
126 static VNET_DEFINE(struct pf_fragqueue,	pf_fragqueue);
127 #define	V_pf_fragqueue			VNET(pf_fragqueue)
128 static VNET_DEFINE(struct pf_cachequeue,	pf_cachequeue);
129 #define	V_pf_cachequeue			VNET(pf_cachequeue)
130 RB_HEAD(pf_frag_tree, pf_fragment);
131 static VNET_DEFINE(struct pf_frag_tree,	pf_frag_tree);
132 #define	V_pf_frag_tree			VNET(pf_frag_tree)
133 static VNET_DEFINE(struct pf_frag_tree,	pf_cache_tree);
134 #define	V_pf_cache_tree			VNET(pf_cache_tree)
135 static int		 pf_frag_compare(struct pf_fragment *,
136 			    struct pf_fragment *);
137 static RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
138 static RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare);
139 
140 static void	pf_flush_fragments(void);
141 static void	pf_free_fragment(struct pf_fragment *);
142 static void	pf_remove_fragment(struct pf_fragment *);
143 static int	pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
144 		    struct tcphdr *, int, sa_family_t);
145 static struct pf_frent *pf_create_fragment(u_short *);
146 static struct pf_fragment *pf_find_fragment(struct pf_fragment_cmp *key,
147 		    struct pf_frag_tree *tree);
148 static struct pf_fragment *pf_fillup_fragment(struct pf_fragment_cmp *,
149 		    struct pf_frent *, u_short *);
150 static int	pf_isfull_fragment(struct pf_fragment *);
151 static struct mbuf *pf_join_fragment(struct pf_fragment *);
152 #ifdef INET
153 static void	pf_scrub_ip(struct mbuf **, uint32_t, uint8_t, uint8_t);
154 static int	pf_reassemble(struct mbuf **, struct ip *, int, u_short *);
155 static struct mbuf *pf_fragcache(struct mbuf **, struct ip*,
156 		    struct pf_fragment **, int, int, int *);
157 #endif	/* INET */
158 #ifdef INET6
159 static int	pf_reassemble6(struct mbuf **, struct ip6_hdr *,
160 		    struct ip6_frag *, uint16_t, uint16_t, int, u_short *);
161 static void	pf_scrub_ip6(struct mbuf **, uint8_t);
162 #endif	/* INET6 */
163 
164 #define	DPFPRINTF(x) do {				\
165 	if (V_pf_status.debug >= PF_DEBUG_MISC) {	\
166 		printf("%s: ", __func__);		\
167 		printf x ;				\
168 	}						\
169 } while(0)
170 
171 #ifdef INET
172 static void
173 pf_ip2key(struct ip *ip, int dir, struct pf_fragment_cmp *key)
174 {
175 
176 	key->frc_src.v4 = ip->ip_src;
177 	key->frc_dst.v4 = ip->ip_dst;
178 	key->frc_af = AF_INET;
179 	key->frc_proto = ip->ip_p;
180 	key->frc_id = ip->ip_id;
181 	key->frc_direction = dir;
182 }
183 #endif	/* INET */
184 
185 void
186 pf_normalize_init(void)
187 {
188 
189 	V_pf_frag_z = uma_zcreate("pf frags", sizeof(struct pf_fragment),
190 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
191 	V_pf_frent_z = uma_zcreate("pf frag entries", sizeof(struct pf_frent),
192 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
193 	V_pf_state_scrub_z = uma_zcreate("pf state scrubs",
194 	    sizeof(struct pf_state_scrub),  NULL, NULL, NULL, NULL,
195 	    UMA_ALIGN_PTR, 0);
196 
197 	V_pf_limits[PF_LIMIT_FRAGS].zone = V_pf_frent_z;
198 	V_pf_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
199 	uma_zone_set_max(V_pf_frent_z, PFFRAG_FRENT_HIWAT);
200 	uma_zone_set_warning(V_pf_frent_z, "PF frag entries limit reached");
201 
202 	TAILQ_INIT(&V_pf_fragqueue);
203 	TAILQ_INIT(&V_pf_cachequeue);
204 }
205 
206 void
207 pf_normalize_cleanup(void)
208 {
209 
210 	uma_zdestroy(V_pf_state_scrub_z);
211 	uma_zdestroy(V_pf_frent_z);
212 	uma_zdestroy(V_pf_frag_z);
213 }
214 
215 static int
216 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
217 {
218 	int	diff;
219 
220 	if ((diff = a->fr_id - b->fr_id) != 0)
221 		return (diff);
222 	if ((diff = a->fr_proto - b->fr_proto) != 0)
223 		return (diff);
224 	if ((diff = a->fr_af - b->fr_af) != 0)
225 		return (diff);
226 	if ((diff = pf_addr_cmp(&a->fr_src, &b->fr_src, a->fr_af)) != 0)
227 		return (diff);
228 	if ((diff = pf_addr_cmp(&a->fr_dst, &b->fr_dst, a->fr_af)) != 0)
229 		return (diff);
230 	return (0);
231 }
232 
233 void
234 pf_purge_expired_fragments(void)
235 {
236 	struct pf_fragment	*frag;
237 	u_int32_t		 expire = time_uptime -
238 				    V_pf_default_rule.timeout[PFTM_FRAG];
239 
240 	PF_FRAG_LOCK();
241 	while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) {
242 		KASSERT((BUFFER_FRAGMENTS(frag)),
243 		    ("BUFFER_FRAGMENTS(frag) == 0: %s", __FUNCTION__));
244 		if (frag->fr_timeout > expire)
245 			break;
246 
247 		DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
248 		pf_free_fragment(frag);
249 	}
250 
251 	while ((frag = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue)) != NULL) {
252 		KASSERT((!BUFFER_FRAGMENTS(frag)),
253 		    ("BUFFER_FRAGMENTS(frag) != 0: %s", __FUNCTION__));
254 		if (frag->fr_timeout > expire)
255 			break;
256 
257 		DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
258 		pf_free_fragment(frag);
259 		KASSERT((TAILQ_EMPTY(&V_pf_cachequeue) ||
260 		    TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue) != frag),
261 		    ("!(TAILQ_EMPTY() || TAILQ_LAST() == farg): %s",
262 		    __FUNCTION__));
263 	}
264 	PF_FRAG_UNLOCK();
265 }
266 
267 /*
268  * Try to flush old fragments to make space for new ones
269  */
270 static void
271 pf_flush_fragments(void)
272 {
273 	struct pf_fragment	*frag, *cache;
274 	int			 goal;
275 
276 	PF_FRAG_ASSERT();
277 
278 	goal = uma_zone_get_cur(V_pf_frent_z) * 9 / 10;
279 	DPFPRINTF(("trying to free %d frag entriess\n", goal));
280 	while (goal < uma_zone_get_cur(V_pf_frent_z)) {
281 		frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue);
282 		if (frag)
283 			pf_free_fragment(frag);
284 		cache = TAILQ_LAST(&V_pf_cachequeue, pf_cachequeue);
285 		if (cache)
286 			pf_free_fragment(cache);
287 		if (frag == NULL && cache == NULL)
288 			break;
289 	}
290 }
291 
292 /* Frees the fragments and all associated entries */
293 static void
294 pf_free_fragment(struct pf_fragment *frag)
295 {
296 	struct pf_frent		*frent;
297 
298 	PF_FRAG_ASSERT();
299 
300 	/* Free all fragments */
301 	if (BUFFER_FRAGMENTS(frag)) {
302 		for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
303 		    frent = TAILQ_FIRST(&frag->fr_queue)) {
304 			TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
305 
306 			m_freem(frent->fe_m);
307 			uma_zfree(V_pf_frent_z, frent);
308 		}
309 	} else {
310 		for (frent = TAILQ_FIRST(&frag->fr_queue); frent;
311 		    frent = TAILQ_FIRST(&frag->fr_queue)) {
312 			TAILQ_REMOVE(&frag->fr_queue, frent, fr_next);
313 
314 			KASSERT((TAILQ_EMPTY(&frag->fr_queue) ||
315 			    TAILQ_FIRST(&frag->fr_queue)->fe_off >
316 			    frent->fe_len),
317 			    ("! (TAILQ_EMPTY() || TAILQ_FIRST()->fe_off >"
318 			    " frent->fe_len): %s", __func__));
319 
320 			uma_zfree(V_pf_frent_z, frent);
321 		}
322 	}
323 
324 	pf_remove_fragment(frag);
325 }
326 
327 static struct pf_fragment *
328 pf_find_fragment(struct pf_fragment_cmp *key, struct pf_frag_tree *tree)
329 {
330 	struct pf_fragment	*frag;
331 
332 	PF_FRAG_ASSERT();
333 
334 	frag = RB_FIND(pf_frag_tree, tree, (struct pf_fragment *)key);
335 	if (frag != NULL) {
336 		/* XXX Are we sure we want to update the timeout? */
337 		frag->fr_timeout = time_uptime;
338 		if (BUFFER_FRAGMENTS(frag)) {
339 			TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
340 			TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
341 		} else {
342 			TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
343 			TAILQ_INSERT_HEAD(&V_pf_cachequeue, frag, frag_next);
344 		}
345 	}
346 
347 	return (frag);
348 }
349 
350 /* Removes a fragment from the fragment queue and frees the fragment */
351 static void
352 pf_remove_fragment(struct pf_fragment *frag)
353 {
354 
355 	PF_FRAG_ASSERT();
356 
357 	if (BUFFER_FRAGMENTS(frag)) {
358 		RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag);
359 		TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next);
360 		uma_zfree(V_pf_frag_z, frag);
361 	} else {
362 		RB_REMOVE(pf_frag_tree, &V_pf_cache_tree, frag);
363 		TAILQ_REMOVE(&V_pf_cachequeue, frag, frag_next);
364 		uma_zfree(V_pf_frag_z, frag);
365 	}
366 }
367 
368 static struct pf_frent *
369 pf_create_fragment(u_short *reason)
370 {
371 	struct pf_frent *frent;
372 
373 	PF_FRAG_ASSERT();
374 
375 	frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
376 	if (frent == NULL) {
377 		pf_flush_fragments();
378 		frent = uma_zalloc(V_pf_frent_z, M_NOWAIT);
379 		if (frent == NULL) {
380 			REASON_SET(reason, PFRES_MEMORY);
381 			return (NULL);
382 		}
383 	}
384 
385 	return (frent);
386 }
387 
388 static struct pf_fragment *
389 pf_fillup_fragment(struct pf_fragment_cmp *key, struct pf_frent *frent,
390 		u_short *reason)
391 {
392 	struct pf_frent		*after, *next, *prev;
393 	struct pf_fragment	*frag;
394 	uint16_t		total;
395 
396 	PF_FRAG_ASSERT();
397 
398 	/* No empty fragments. */
399 	if (frent->fe_len == 0) {
400 		DPFPRINTF(("bad fragment: len 0"));
401 		goto bad_fragment;
402 	}
403 
404 	/* All fragments are 8 byte aligned. */
405 	if (frent->fe_mff && (frent->fe_len & 0x7)) {
406 		DPFPRINTF(("bad fragment: mff and len %d", frent->fe_len));
407 		goto bad_fragment;
408 	}
409 
410 	/* Respect maximum length, IP_MAXPACKET == IPV6_MAXPACKET. */
411 	if (frent->fe_off + frent->fe_len > IP_MAXPACKET) {
412 		DPFPRINTF(("bad fragment: max packet %d",
413 		    frent->fe_off + frent->fe_len));
414 		goto bad_fragment;
415 	}
416 
417 	DPFPRINTF((key->frc_af == AF_INET ?
418 	    "reass frag %d @ %d-%d" : "reass frag %#08x @ %d-%d",
419 	    key->frc_id, frent->fe_off, frent->fe_off + frent->fe_len));
420 
421 	/* Fully buffer all of the fragments in this fragment queue. */
422 	frag = pf_find_fragment(key, &V_pf_frag_tree);
423 
424 	/* Create a new reassembly queue for this packet. */
425 	if (frag == NULL) {
426 		frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
427 		if (frag == NULL) {
428 			pf_flush_fragments();
429 			frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
430 			if (frag == NULL) {
431 				REASON_SET(reason, PFRES_MEMORY);
432 				goto drop_fragment;
433 			}
434 		}
435 
436 		*(struct pf_fragment_cmp *)frag = *key;
437 		frag->fr_timeout = time_second;
438 		frag->fr_maxlen = frent->fe_len;
439 		TAILQ_INIT(&frag->fr_queue);
440 
441 		RB_INSERT(pf_frag_tree, &V_pf_frag_tree, frag);
442 		TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next);
443 
444 		/* We do not have a previous fragment. */
445 		TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
446 
447 		return (frag);
448 	}
449 
450 	KASSERT(!TAILQ_EMPTY(&frag->fr_queue), ("!TAILQ_EMPTY()->fr_queue"));
451 
452 	/* Remember maximum fragment len for refragmentation. */
453 	if (frent->fe_len > frag->fr_maxlen)
454 		frag->fr_maxlen = frent->fe_len;
455 
456 	/* Maximum data we have seen already. */
457 	total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
458 		TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
459 
460 	/* Non terminal fragments must have more fragments flag. */
461 	if (frent->fe_off + frent->fe_len < total && !frent->fe_mff)
462 		goto bad_fragment;
463 
464 	/* Check if we saw the last fragment already. */
465 	if (!TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff) {
466 		if (frent->fe_off + frent->fe_len > total ||
467 		    (frent->fe_off + frent->fe_len == total && frent->fe_mff))
468 			goto bad_fragment;
469 	} else {
470 		if (frent->fe_off + frent->fe_len == total && !frent->fe_mff)
471 			goto bad_fragment;
472 	}
473 
474 	/* Find a fragment after the current one. */
475 	prev = NULL;
476 	TAILQ_FOREACH(after, &frag->fr_queue, fr_next) {
477 		if (after->fe_off > frent->fe_off)
478 			break;
479 		prev = after;
480 	}
481 
482 	KASSERT(prev != NULL || after != NULL,
483 	    ("prev != NULL || after != NULL"));
484 
485 	if (prev != NULL && prev->fe_off + prev->fe_len > frent->fe_off) {
486 		uint16_t precut;
487 
488 		precut = prev->fe_off + prev->fe_len - frent->fe_off;
489 		if (precut >= frent->fe_len)
490 			goto bad_fragment;
491 		DPFPRINTF(("overlap -%d", precut));
492 		m_adj(frent->fe_m, precut);
493 		frent->fe_off += precut;
494 		frent->fe_len -= precut;
495 	}
496 
497 	for (; after != NULL && frent->fe_off + frent->fe_len > after->fe_off;
498 	    after = next) {
499 		uint16_t aftercut;
500 
501 		aftercut = frent->fe_off + frent->fe_len - after->fe_off;
502 		DPFPRINTF(("adjust overlap %d", aftercut));
503 		if (aftercut < after->fe_len) {
504 			m_adj(after->fe_m, aftercut);
505 			after->fe_off += aftercut;
506 			after->fe_len -= aftercut;
507 			break;
508 		}
509 
510 		/* This fragment is completely overlapped, lose it. */
511 		next = TAILQ_NEXT(after, fr_next);
512 		m_freem(after->fe_m);
513 		TAILQ_REMOVE(&frag->fr_queue, after, fr_next);
514 		uma_zfree(V_pf_frent_z, after);
515 	}
516 
517 	if (prev == NULL)
518 		TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next);
519 	else
520 		TAILQ_INSERT_AFTER(&frag->fr_queue, prev, frent, fr_next);
521 
522 	return (frag);
523 
524 bad_fragment:
525 	REASON_SET(reason, PFRES_FRAG);
526 drop_fragment:
527 	uma_zfree(V_pf_frent_z, frent);
528 	return (NULL);
529 }
530 
531 static int
532 pf_isfull_fragment(struct pf_fragment *frag)
533 {
534 	struct pf_frent	*frent, *next;
535 	uint16_t off, total;
536 
537 	/* Check if we are completely reassembled */
538 	if (TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff)
539 		return (0);
540 
541 	/* Maximum data we have seen already */
542 	total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
543 		TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
544 
545 	/* Check if we have all the data */
546 	off = 0;
547 	for (frent = TAILQ_FIRST(&frag->fr_queue); frent; frent = next) {
548 		next = TAILQ_NEXT(frent, fr_next);
549 
550 		off += frent->fe_len;
551 		if (off < total && (next == NULL || next->fe_off != off)) {
552 			DPFPRINTF(("missing fragment at %d, next %d, total %d",
553 			    off, next == NULL ? -1 : next->fe_off, total));
554 			return (0);
555 		}
556 	}
557 	DPFPRINTF(("%d < %d?", off, total));
558 	if (off < total)
559 		return (0);
560 	KASSERT(off == total, ("off == total"));
561 
562 	return (1);
563 }
564 
565 static struct mbuf *
566 pf_join_fragment(struct pf_fragment *frag)
567 {
568 	struct mbuf *m, *m2;
569 	struct pf_frent	*frent, *next;
570 
571 	frent = TAILQ_FIRST(&frag->fr_queue);
572 	next = TAILQ_NEXT(frent, fr_next);
573 
574 	m = frent->fe_m;
575 	m_adj(m, (frent->fe_hdrlen + frent->fe_len) - m->m_pkthdr.len);
576 	uma_zfree(V_pf_frent_z, frent);
577 	for (frent = next; frent != NULL; frent = next) {
578 		next = TAILQ_NEXT(frent, fr_next);
579 
580 		m2 = frent->fe_m;
581 		/* Strip off ip header. */
582 		m_adj(m2, frent->fe_hdrlen);
583 		/* Strip off any trailing bytes. */
584 		m_adj(m2, frent->fe_len - m2->m_pkthdr.len);
585 
586 		uma_zfree(V_pf_frent_z, frent);
587 		m_cat(m, m2);
588 	}
589 
590 	/* Remove from fragment queue. */
591 	pf_remove_fragment(frag);
592 
593 	return (m);
594 }
595 
596 #ifdef INET
597 static int
598 pf_reassemble(struct mbuf **m0, struct ip *ip, int dir, u_short *reason)
599 {
600 	struct mbuf		*m = *m0;
601 	struct pf_frent		*frent;
602 	struct pf_fragment	*frag;
603 	struct pf_fragment_cmp	key;
604 	uint16_t		total, hdrlen;
605 
606 	/* Get an entry for the fragment queue */
607 	if ((frent = pf_create_fragment(reason)) == NULL)
608 		return (PF_DROP);
609 
610 	frent->fe_m = m;
611 	frent->fe_hdrlen = ip->ip_hl << 2;
612 	frent->fe_extoff = 0;
613 	frent->fe_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
614 	frent->fe_off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
615 	frent->fe_mff = ntohs(ip->ip_off) & IP_MF;
616 
617 	pf_ip2key(ip, dir, &key);
618 
619 	if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL)
620 		return (PF_DROP);
621 
622 	/* The mbuf is part of the fragment entry, no direct free or access */
623 	m = *m0 = NULL;
624 
625 	if (!pf_isfull_fragment(frag))
626 		return (PF_PASS);  /* drop because *m0 is NULL, no error */
627 
628 	/* We have all the data */
629 	frent = TAILQ_FIRST(&frag->fr_queue);
630 	KASSERT(frent != NULL, ("frent != NULL"));
631 	total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
632 		TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
633 	hdrlen = frent->fe_hdrlen;
634 
635 	m = *m0 = pf_join_fragment(frag);
636 	frag = NULL;
637 
638 	if (m->m_flags & M_PKTHDR) {
639 		int plen = 0;
640 		for (m = *m0; m; m = m->m_next)
641 			plen += m->m_len;
642 		m = *m0;
643 		m->m_pkthdr.len = plen;
644 	}
645 
646 	ip = mtod(m, struct ip *);
647 	ip->ip_len = htons(hdrlen + total);
648 	ip->ip_off &= ~(IP_MF|IP_OFFMASK);
649 
650 	if (hdrlen + total > IP_MAXPACKET) {
651 		DPFPRINTF(("drop: too big: %d", total));
652 		ip->ip_len = 0;
653 		REASON_SET(reason, PFRES_SHORT);
654 		/* PF_DROP requires a valid mbuf *m0 in pf_test() */
655 		return (PF_DROP);
656 	}
657 
658 	DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
659 	return (PF_PASS);
660 }
661 #endif	/* INET */
662 
663 #ifdef INET6
664 static int
665 pf_reassemble6(struct mbuf **m0, struct ip6_hdr *ip6, struct ip6_frag *fraghdr,
666     uint16_t hdrlen, uint16_t extoff, int dir, u_short *reason)
667 {
668 	struct mbuf		*m = *m0;
669 	struct pf_frent		*frent;
670 	struct pf_fragment	*frag;
671 	struct pf_fragment_cmp	 key;
672 	struct m_tag		*mtag;
673 	struct pf_fragment_tag	*ftag;
674 	int			 off;
675 	uint32_t		 frag_id;
676 	uint16_t		 total, maxlen;
677 	uint8_t			 proto;
678 
679 	PF_FRAG_LOCK();
680 
681 	/* Get an entry for the fragment queue. */
682 	if ((frent = pf_create_fragment(reason)) == NULL) {
683 		PF_FRAG_UNLOCK();
684 		return (PF_DROP);
685 	}
686 
687 	frent->fe_m = m;
688 	frent->fe_hdrlen = hdrlen;
689 	frent->fe_extoff = extoff;
690 	frent->fe_len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - hdrlen;
691 	frent->fe_off = ntohs(fraghdr->ip6f_offlg & IP6F_OFF_MASK);
692 	frent->fe_mff = fraghdr->ip6f_offlg & IP6F_MORE_FRAG;
693 
694 	key.frc_src.v6 = ip6->ip6_src;
695 	key.frc_dst.v6 = ip6->ip6_dst;
696 	key.frc_af = AF_INET6;
697 	/* Only the first fragment's protocol is relevant. */
698 	key.frc_proto = 0;
699 	key.frc_id = fraghdr->ip6f_ident;
700 	key.frc_direction = dir;
701 
702 	if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) {
703 		PF_FRAG_UNLOCK();
704 		return (PF_DROP);
705 	}
706 
707 	/* The mbuf is part of the fragment entry, no direct free or access. */
708 	m = *m0 = NULL;
709 
710 	if (!pf_isfull_fragment(frag)) {
711 		PF_FRAG_UNLOCK();
712 		return (PF_PASS);  /* Drop because *m0 is NULL, no error. */
713 	}
714 
715 	/* We have all the data. */
716 	extoff = frent->fe_extoff;
717 	maxlen = frag->fr_maxlen;
718 	frag_id = frag->fr_id;
719 	frent = TAILQ_FIRST(&frag->fr_queue);
720 	KASSERT(frent != NULL, ("frent != NULL"));
721 	total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off +
722 		TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len;
723 	hdrlen = frent->fe_hdrlen - sizeof(struct ip6_frag);
724 
725 	m = *m0 = pf_join_fragment(frag);
726 	frag = NULL;
727 
728 	PF_FRAG_UNLOCK();
729 
730 	/* Take protocol from first fragment header. */
731 	m = m_getptr(m, hdrlen + offsetof(struct ip6_frag, ip6f_nxt), &off);
732 	KASSERT(m, ("%s: short mbuf chain", __func__));
733 	proto = *(mtod(m, caddr_t) + off);
734 	m = *m0;
735 
736 	/* Delete frag6 header */
737 	if (ip6_deletefraghdr(m, hdrlen, M_NOWAIT) != 0)
738 		goto fail;
739 
740 	if (m->m_flags & M_PKTHDR) {
741 		int plen = 0;
742 		for (m = *m0; m; m = m->m_next)
743 			plen += m->m_len;
744 		m = *m0;
745 		m->m_pkthdr.len = plen;
746 	}
747 
748 	if ((mtag = m_tag_get(PF_REASSEMBLED, sizeof(struct pf_fragment_tag),
749 	    M_NOWAIT)) == NULL)
750 		goto fail;
751 	ftag = (struct pf_fragment_tag *)(mtag + 1);
752 	ftag->ft_hdrlen = hdrlen;
753 	ftag->ft_extoff = extoff;
754 	ftag->ft_maxlen = maxlen;
755 	ftag->ft_id = frag_id;
756 	m_tag_prepend(m, mtag);
757 
758 	ip6 = mtod(m, struct ip6_hdr *);
759 	ip6->ip6_plen = htons(hdrlen - sizeof(struct ip6_hdr) + total);
760 	if (extoff) {
761 		/* Write protocol into next field of last extension header. */
762 		m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
763 		    &off);
764 		KASSERT(m, ("%s: short mbuf chain", __func__));
765 		*(mtod(m, char *) + off) = proto;
766 		m = *m0;
767 	} else
768 		ip6->ip6_nxt = proto;
769 
770 	if (hdrlen - sizeof(struct ip6_hdr) + total > IPV6_MAXPACKET) {
771 		DPFPRINTF(("drop: too big: %d", total));
772 		ip6->ip6_plen = 0;
773 		REASON_SET(reason, PFRES_SHORT);
774 		/* PF_DROP requires a valid mbuf *m0 in pf_test6(). */
775 		return (PF_DROP);
776 	}
777 
778 	DPFPRINTF(("complete: %p(%d)", m, ntohs(ip6->ip6_plen)));
779 	return (PF_PASS);
780 
781 fail:
782 	REASON_SET(reason, PFRES_MEMORY);
783 	/* PF_DROP requires a valid mbuf *m0 in pf_test6(), will free later. */
784 	return (PF_DROP);
785 }
786 #endif	/* INET6 */
787 
788 #ifdef INET
789 static struct mbuf *
790 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
791     int drop, int *nomem)
792 {
793 	struct mbuf		*m = *m0;
794 	struct pf_frent		*frp, *fra, *cur = NULL;
795 	int			 ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
796 	u_int16_t		 off = ntohs(h->ip_off) << 3;
797 	u_int16_t		 max = ip_len + off;
798 	int			 hosed = 0;
799 
800 	PF_FRAG_ASSERT();
801 	KASSERT((*frag == NULL || !BUFFER_FRAGMENTS(*frag)),
802 	    ("!(*frag == NULL || !BUFFER_FRAGMENTS(*frag)): %s", __FUNCTION__));
803 
804 	/* Create a new range queue for this packet */
805 	if (*frag == NULL) {
806 		*frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
807 		if (*frag == NULL) {
808 			pf_flush_fragments();
809 			*frag = uma_zalloc(V_pf_frag_z, M_NOWAIT);
810 			if (*frag == NULL)
811 				goto no_mem;
812 		}
813 
814 		/* Get an entry for the queue */
815 		cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
816 		if (cur == NULL) {
817 			uma_zfree(V_pf_frag_z, *frag);
818 			*frag = NULL;
819 			goto no_mem;
820 		}
821 
822 		(*frag)->fr_flags = PFFRAG_NOBUFFER;
823 		(*frag)->fr_max = 0;
824 		(*frag)->fr_src.v4 = h->ip_src;
825 		(*frag)->fr_dst.v4 = h->ip_dst;
826 		(*frag)->fr_af = AF_INET;
827 		(*frag)->fr_id = h->ip_id;
828 		(*frag)->fr_timeout = time_uptime;
829 
830 		cur->fe_off = off;
831 		cur->fe_len = max; /* TODO: fe_len = max - off ? */
832 		TAILQ_INIT(&(*frag)->fr_queue);
833 		TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
834 
835 		RB_INSERT(pf_frag_tree, &V_pf_cache_tree, *frag);
836 		TAILQ_INSERT_HEAD(&V_pf_cachequeue, *frag, frag_next);
837 
838 		DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
839 
840 		goto pass;
841 	}
842 
843 	/*
844 	 * Find a fragment after the current one:
845 	 *  - off contains the real shifted offset.
846 	 */
847 	frp = NULL;
848 	TAILQ_FOREACH(fra, &(*frag)->fr_queue, fr_next) {
849 		if (fra->fe_off > off)
850 			break;
851 		frp = fra;
852 	}
853 
854 	KASSERT((frp != NULL || fra != NULL),
855 	    ("!(frp != NULL || fra != NULL): %s", __FUNCTION__));
856 
857 	if (frp != NULL) {
858 		int	precut;
859 
860 		precut = frp->fe_len - off;
861 		if (precut >= ip_len) {
862 			/* Fragment is entirely a duplicate */
863 			DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
864 			    h->ip_id, frp->fe_off, frp->fe_len, off, max));
865 			goto drop_fragment;
866 		}
867 		if (precut == 0) {
868 			/* They are adjacent.  Fixup cache entry */
869 			DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
870 			    h->ip_id, frp->fe_off, frp->fe_len, off, max));
871 			frp->fe_len = max;
872 		} else if (precut > 0) {
873 			/* The first part of this payload overlaps with a
874 			 * fragment that has already been passed.
875 			 * Need to trim off the first part of the payload.
876 			 * But to do so easily, we need to create another
877 			 * mbuf to throw the original header into.
878 			 */
879 
880 			DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
881 			    h->ip_id, precut, frp->fe_off, frp->fe_len, off,
882 			    max));
883 
884 			off += precut;
885 			max -= precut;
886 			/* Update the previous frag to encompass this one */
887 			frp->fe_len = max;
888 
889 			if (!drop) {
890 				/* XXX Optimization opportunity
891 				 * This is a very heavy way to trim the payload.
892 				 * we could do it much faster by diddling mbuf
893 				 * internals but that would be even less legible
894 				 * than this mbuf magic.  For my next trick,
895 				 * I'll pull a rabbit out of my laptop.
896 				 */
897 				*m0 = m_dup(m, M_NOWAIT);
898 				if (*m0 == NULL)
899 					goto no_mem;
900 				/* From KAME Project : We have missed this! */
901 				m_adj(*m0, (h->ip_hl << 2) -
902 				    (*m0)->m_pkthdr.len);
903 
904 				KASSERT(((*m0)->m_next == NULL),
905 				    ("(*m0)->m_next != NULL: %s",
906 				    __FUNCTION__));
907 				m_adj(m, precut + (h->ip_hl << 2));
908 				m_cat(*m0, m);
909 				m = *m0;
910 				if (m->m_flags & M_PKTHDR) {
911 					int plen = 0;
912 					struct mbuf *t;
913 					for (t = m; t; t = t->m_next)
914 						plen += t->m_len;
915 					m->m_pkthdr.len = plen;
916 				}
917 
918 
919 				h = mtod(m, struct ip *);
920 
921 				KASSERT(((int)m->m_len ==
922 				    ntohs(h->ip_len) - precut),
923 				    ("m->m_len != ntohs(h->ip_len) - precut: %s",
924 				    __FUNCTION__));
925 				h->ip_off = htons(ntohs(h->ip_off) +
926 				    (precut >> 3));
927 				h->ip_len = htons(ntohs(h->ip_len) - precut);
928 			} else {
929 				hosed++;
930 			}
931 		} else {
932 			/* There is a gap between fragments */
933 
934 			DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
935 			    h->ip_id, -precut, frp->fe_off, frp->fe_len, off,
936 			    max));
937 
938 			cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
939 			if (cur == NULL)
940 				goto no_mem;
941 
942 			cur->fe_off = off;
943 			cur->fe_len = max;
944 			TAILQ_INSERT_AFTER(&(*frag)->fr_queue, frp, cur, fr_next);
945 		}
946 	}
947 
948 	if (fra != NULL) {
949 		int	aftercut;
950 		int	merge = 0;
951 
952 		aftercut = max - fra->fe_off;
953 		if (aftercut == 0) {
954 			/* Adjacent fragments */
955 			DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
956 			    h->ip_id, off, max, fra->fe_off, fra->fe_len));
957 			fra->fe_off = off;
958 			merge = 1;
959 		} else if (aftercut > 0) {
960 			/* Need to chop off the tail of this fragment */
961 			DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
962 			    h->ip_id, aftercut, off, max, fra->fe_off,
963 			    fra->fe_len));
964 			fra->fe_off = off;
965 			max -= aftercut;
966 
967 			merge = 1;
968 
969 			if (!drop) {
970 				m_adj(m, -aftercut);
971 				if (m->m_flags & M_PKTHDR) {
972 					int plen = 0;
973 					struct mbuf *t;
974 					for (t = m; t; t = t->m_next)
975 						plen += t->m_len;
976 					m->m_pkthdr.len = plen;
977 				}
978 				h = mtod(m, struct ip *);
979 				KASSERT(((int)m->m_len == ntohs(h->ip_len) - aftercut),
980 				    ("m->m_len != ntohs(h->ip_len) - aftercut: %s",
981 				    __FUNCTION__));
982 				h->ip_len = htons(ntohs(h->ip_len) - aftercut);
983 			} else {
984 				hosed++;
985 			}
986 		} else if (frp == NULL) {
987 			/* There is a gap between fragments */
988 			DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
989 			    h->ip_id, -aftercut, off, max, fra->fe_off,
990 			    fra->fe_len));
991 
992 			cur = uma_zalloc(V_pf_frent_z, M_NOWAIT);
993 			if (cur == NULL)
994 				goto no_mem;
995 
996 			cur->fe_off = off;
997 			cur->fe_len = max;
998 			TAILQ_INSERT_HEAD(&(*frag)->fr_queue, cur, fr_next);
999 		}
1000 
1001 
1002 		/* Need to glue together two separate fragment descriptors */
1003 		if (merge) {
1004 			if (cur && fra->fe_off <= cur->fe_len) {
1005 				/* Need to merge in a previous 'cur' */
1006 				DPFPRINTF(("fragcache[%d]: adjacent(merge "
1007 				    "%d-%d) %d-%d (%d-%d)\n",
1008 				    h->ip_id, cur->fe_off, cur->fe_len, off,
1009 				    max, fra->fe_off, fra->fe_len));
1010 				fra->fe_off = cur->fe_off;
1011 				TAILQ_REMOVE(&(*frag)->fr_queue, cur, fr_next);
1012 				uma_zfree(V_pf_frent_z, cur);
1013 				cur = NULL;
1014 
1015 			} else if (frp && fra->fe_off <= frp->fe_len) {
1016 				/* Need to merge in a modified 'frp' */
1017 				KASSERT((cur == NULL), ("cur != NULL: %s",
1018 				    __FUNCTION__));
1019 				DPFPRINTF(("fragcache[%d]: adjacent(merge "
1020 				    "%d-%d) %d-%d (%d-%d)\n",
1021 				    h->ip_id, frp->fe_off, frp->fe_len, off,
1022 				    max, fra->fe_off, fra->fe_len));
1023 				fra->fe_off = frp->fe_off;
1024 				TAILQ_REMOVE(&(*frag)->fr_queue, frp, fr_next);
1025 				uma_zfree(V_pf_frent_z, frp);
1026 				frp = NULL;
1027 
1028 			}
1029 		}
1030 	}
1031 
1032 	if (hosed) {
1033 		/*
1034 		 * We must keep tracking the overall fragment even when
1035 		 * we're going to drop it anyway so that we know when to
1036 		 * free the overall descriptor.  Thus we drop the frag late.
1037 		 */
1038 		goto drop_fragment;
1039 	}
1040 
1041 
1042  pass:
1043 	/* Update maximum data size */
1044 	if ((*frag)->fr_max < max)
1045 		(*frag)->fr_max = max;
1046 
1047 	/* This is the last segment */
1048 	if (!mff)
1049 		(*frag)->fr_flags |= PFFRAG_SEENLAST;
1050 
1051 	/* Check if we are completely reassembled */
1052 	if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
1053 	    TAILQ_FIRST(&(*frag)->fr_queue)->fe_off == 0 &&
1054 	    TAILQ_FIRST(&(*frag)->fr_queue)->fe_len == (*frag)->fr_max) {
1055 		/* Remove from fragment queue */
1056 		DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
1057 		    (*frag)->fr_max));
1058 		pf_free_fragment(*frag);
1059 		*frag = NULL;
1060 	}
1061 
1062 	return (m);
1063 
1064  no_mem:
1065 	*nomem = 1;
1066 
1067 	/* Still need to pay attention to !IP_MF */
1068 	if (!mff && *frag != NULL)
1069 		(*frag)->fr_flags |= PFFRAG_SEENLAST;
1070 
1071 	m_freem(m);
1072 	return (NULL);
1073 
1074  drop_fragment:
1075 
1076 	/* Still need to pay attention to !IP_MF */
1077 	if (!mff && *frag != NULL)
1078 		(*frag)->fr_flags |= PFFRAG_SEENLAST;
1079 
1080 	if (drop) {
1081 		/* This fragment has been deemed bad.  Don't reass */
1082 		if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
1083 			DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
1084 			    h->ip_id));
1085 		(*frag)->fr_flags |= PFFRAG_DROP;
1086 	}
1087 
1088 	m_freem(m);
1089 	return (NULL);
1090 }
1091 #endif	/* INET */
1092 
1093 #ifdef INET6
1094 int
1095 pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag)
1096 {
1097 	struct mbuf		*m = *m0, *t;
1098 	struct pf_fragment_tag	*ftag = (struct pf_fragment_tag *)(mtag + 1);
1099 	struct pf_pdesc		 pd;
1100 	uint32_t		 frag_id;
1101 	uint16_t		 hdrlen, extoff, maxlen;
1102 	uint8_t			 proto;
1103 	int			 error, action;
1104 
1105 	hdrlen = ftag->ft_hdrlen;
1106 	extoff = ftag->ft_extoff;
1107 	maxlen = ftag->ft_maxlen;
1108 	frag_id = ftag->ft_id;
1109 	m_tag_delete(m, mtag);
1110 	mtag = NULL;
1111 	ftag = NULL;
1112 
1113 	if (extoff) {
1114 		int off;
1115 
1116 		/* Use protocol from next field of last extension header */
1117 		m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt),
1118 		    &off);
1119 		KASSERT((m != NULL), ("pf_refragment6: short mbuf chain"));
1120 		proto = *(mtod(m, caddr_t) + off);
1121 		*(mtod(m, char *) + off) = IPPROTO_FRAGMENT;
1122 		m = *m0;
1123 	} else {
1124 		struct ip6_hdr *hdr;
1125 
1126 		hdr = mtod(m, struct ip6_hdr *);
1127 		proto = hdr->ip6_nxt;
1128 		hdr->ip6_nxt = IPPROTO_FRAGMENT;
1129 	}
1130 
1131 	/*
1132 	 * Maxlen may be less than 8 if there was only a single
1133 	 * fragment.  As it was fragmented before, add a fragment
1134 	 * header also for a single fragment.  If total or maxlen
1135 	 * is less than 8, ip6_fragment() will return EMSGSIZE and
1136 	 * we drop the packet.
1137 	 */
1138 	error = ip6_fragment(ifp, m, hdrlen, proto, maxlen, frag_id);
1139 	m = (*m0)->m_nextpkt;
1140 	(*m0)->m_nextpkt = NULL;
1141 	if (error == 0) {
1142 		/* The first mbuf contains the unfragmented packet. */
1143 		m_freem(*m0);
1144 		*m0 = NULL;
1145 		action = PF_PASS;
1146 	} else {
1147 		/* Drop expects an mbuf to free. */
1148 		DPFPRINTF(("refragment error %d", error));
1149 		action = PF_DROP;
1150 	}
1151 	for (t = m; m; m = t) {
1152 		t = m->m_nextpkt;
1153 		m->m_nextpkt = NULL;
1154 		m->m_flags |= M_SKIP_FIREWALL;
1155 		memset(&pd, 0, sizeof(pd));
1156 		pd.pf_mtag = pf_find_mtag(m);
1157 		if (error == 0)
1158 			ip6_forward(m, 0);
1159 		else
1160 			m_freem(m);
1161 	}
1162 
1163 	return (action);
1164 }
1165 #endif /* INET6 */
1166 
1167 #ifdef INET
1168 int
1169 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
1170     struct pf_pdesc *pd)
1171 {
1172 	struct mbuf		*m = *m0;
1173 	struct pf_rule		*r;
1174 	struct pf_fragment	*frag = NULL;
1175 	struct pf_fragment_cmp	key;
1176 	struct ip		*h = mtod(m, struct ip *);
1177 	int			 mff = (ntohs(h->ip_off) & IP_MF);
1178 	int			 hlen = h->ip_hl << 2;
1179 	u_int16_t		 fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1180 	u_int16_t		 max;
1181 	int			 ip_len;
1182 	int			 ip_off;
1183 	int			 tag = -1;
1184 	int			 verdict;
1185 
1186 	PF_RULES_RASSERT();
1187 
1188 	r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1189 	while (r != NULL) {
1190 		r->evaluations++;
1191 		if (pfi_kif_match(r->kif, kif) == r->ifnot)
1192 			r = r->skip[PF_SKIP_IFP].ptr;
1193 		else if (r->direction && r->direction != dir)
1194 			r = r->skip[PF_SKIP_DIR].ptr;
1195 		else if (r->af && r->af != AF_INET)
1196 			r = r->skip[PF_SKIP_AF].ptr;
1197 		else if (r->proto && r->proto != h->ip_p)
1198 			r = r->skip[PF_SKIP_PROTO].ptr;
1199 		else if (PF_MISMATCHAW(&r->src.addr,
1200 		    (struct pf_addr *)&h->ip_src.s_addr, AF_INET,
1201 		    r->src.neg, kif, M_GETFIB(m)))
1202 			r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1203 		else if (PF_MISMATCHAW(&r->dst.addr,
1204 		    (struct pf_addr *)&h->ip_dst.s_addr, AF_INET,
1205 		    r->dst.neg, NULL, M_GETFIB(m)))
1206 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1207 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
1208 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
1209 			r = TAILQ_NEXT(r, entries);
1210 		else
1211 			break;
1212 	}
1213 
1214 	if (r == NULL || r->action == PF_NOSCRUB)
1215 		return (PF_PASS);
1216 	else {
1217 		r->packets[dir == PF_OUT]++;
1218 		r->bytes[dir == PF_OUT] += pd->tot_len;
1219 	}
1220 
1221 	/* Check for illegal packets */
1222 	if (hlen < (int)sizeof(struct ip))
1223 		goto drop;
1224 
1225 	if (hlen > ntohs(h->ip_len))
1226 		goto drop;
1227 
1228 	/* Clear IP_DF if the rule uses the no-df option */
1229 	if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
1230 		u_int16_t ip_off = h->ip_off;
1231 
1232 		h->ip_off &= htons(~IP_DF);
1233 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1234 	}
1235 
1236 	/* We will need other tests here */
1237 	if (!fragoff && !mff)
1238 		goto no_fragment;
1239 
1240 	/* We're dealing with a fragment now. Don't allow fragments
1241 	 * with IP_DF to enter the cache. If the flag was cleared by
1242 	 * no-df above, fine. Otherwise drop it.
1243 	 */
1244 	if (h->ip_off & htons(IP_DF)) {
1245 		DPFPRINTF(("IP_DF\n"));
1246 		goto bad;
1247 	}
1248 
1249 	ip_len = ntohs(h->ip_len) - hlen;
1250 	ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
1251 
1252 	/* All fragments are 8 byte aligned */
1253 	if (mff && (ip_len & 0x7)) {
1254 		DPFPRINTF(("mff and %d\n", ip_len));
1255 		goto bad;
1256 	}
1257 
1258 	/* Respect maximum length */
1259 	if (fragoff + ip_len > IP_MAXPACKET) {
1260 		DPFPRINTF(("max packet %d\n", fragoff + ip_len));
1261 		goto bad;
1262 	}
1263 	max = fragoff + ip_len;
1264 
1265 	if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
1266 
1267 		/* Fully buffer all of the fragments */
1268 		PF_FRAG_LOCK();
1269 
1270 		pf_ip2key(h, dir, &key);
1271 		frag = pf_find_fragment(&key, &V_pf_frag_tree);
1272 
1273 		/* Check if we saw the last fragment already */
1274 		if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1275 		    max > frag->fr_max)
1276 			goto bad;
1277 
1278 		/* Might return a completely reassembled mbuf, or NULL */
1279 		DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
1280 		verdict = pf_reassemble(m0, h, dir, reason);
1281 		PF_FRAG_UNLOCK();
1282 
1283 		if (verdict != PF_PASS)
1284 			return (PF_DROP);
1285 
1286 		m = *m0;
1287 		if (m == NULL)
1288 			return (PF_DROP);
1289 
1290 		if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1291 			goto drop;
1292 
1293 		h = mtod(m, struct ip *);
1294 	} else {
1295 		/* non-buffering fragment cache (drops or masks overlaps) */
1296 		int	nomem = 0;
1297 
1298 		if (dir == PF_OUT && pd->pf_mtag &&
1299 		    pd->pf_mtag->flags & PF_TAG_FRAGCACHE) {
1300 			/*
1301 			 * Already passed the fragment cache in the
1302 			 * input direction.  If we continued, it would
1303 			 * appear to be a dup and would be dropped.
1304 			 */
1305 			goto fragment_pass;
1306 		}
1307 
1308 		PF_FRAG_LOCK();
1309 		pf_ip2key(h, dir, &key);
1310 		frag = pf_find_fragment(&key, &V_pf_cache_tree);
1311 
1312 		/* Check if we saw the last fragment already */
1313 		if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
1314 		    max > frag->fr_max) {
1315 			if (r->rule_flag & PFRULE_FRAGDROP)
1316 				frag->fr_flags |= PFFRAG_DROP;
1317 			goto bad;
1318 		}
1319 
1320 		*m0 = m = pf_fragcache(m0, h, &frag, mff,
1321 		    (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
1322 		PF_FRAG_UNLOCK();
1323 		if (m == NULL) {
1324 			if (nomem)
1325 				goto no_mem;
1326 			goto drop;
1327 		}
1328 
1329 		if (dir == PF_IN) {
1330 			/* Use mtag from copied and trimmed mbuf chain. */
1331 			pd->pf_mtag = pf_get_mtag(m);
1332 			if (pd->pf_mtag == NULL) {
1333 				m_freem(m);
1334 				*m0 = NULL;
1335 				goto no_mem;
1336 			}
1337 			pd->pf_mtag->flags |= PF_TAG_FRAGCACHE;
1338 		}
1339 
1340 		if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1341 			goto drop;
1342 		goto fragment_pass;
1343 	}
1344 
1345  no_fragment:
1346 	/* At this point, only IP_DF is allowed in ip_off */
1347 	if (h->ip_off & ~htons(IP_DF)) {
1348 		u_int16_t ip_off = h->ip_off;
1349 
1350 		h->ip_off &= htons(IP_DF);
1351 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
1352 	}
1353 
1354 	/* not missing a return here */
1355 
1356  fragment_pass:
1357 	pf_scrub_ip(&m, r->rule_flag, r->min_ttl, r->set_tos);
1358 
1359 	if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1360 		pd->flags |= PFDESC_IP_REAS;
1361 	return (PF_PASS);
1362 
1363  no_mem:
1364 	REASON_SET(reason, PFRES_MEMORY);
1365 	if (r != NULL && r->log)
1366 		PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1367 		    1);
1368 	return (PF_DROP);
1369 
1370  drop:
1371 	REASON_SET(reason, PFRES_NORM);
1372 	if (r != NULL && r->log)
1373 		PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1374 		    1);
1375 	return (PF_DROP);
1376 
1377  bad:
1378 	DPFPRINTF(("dropping bad fragment\n"));
1379 
1380 	/* Free associated fragments */
1381 	if (frag != NULL) {
1382 		pf_free_fragment(frag);
1383 		PF_FRAG_UNLOCK();
1384 	}
1385 
1386 	REASON_SET(reason, PFRES_FRAG);
1387 	if (r != NULL && r->log)
1388 		PFLOG_PACKET(kif, m, AF_INET, dir, *reason, r, NULL, NULL, pd,
1389 		    1);
1390 
1391 	return (PF_DROP);
1392 }
1393 #endif
1394 
1395 #ifdef INET6
1396 int
1397 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1398     u_short *reason, struct pf_pdesc *pd)
1399 {
1400 	struct mbuf		*m = *m0;
1401 	struct pf_rule		*r;
1402 	struct ip6_hdr		*h = mtod(m, struct ip6_hdr *);
1403 	int			 extoff;
1404 	int			 off;
1405 	struct ip6_ext		 ext;
1406 	struct ip6_opt		 opt;
1407 	struct ip6_opt_jumbo	 jumbo;
1408 	struct ip6_frag		 frag;
1409 	u_int32_t		 jumbolen = 0, plen;
1410 	int			 optend;
1411 	int			 ooff;
1412 	u_int8_t		 proto;
1413 	int			 terminal;
1414 
1415 	PF_RULES_RASSERT();
1416 
1417 	r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1418 	while (r != NULL) {
1419 		r->evaluations++;
1420 		if (pfi_kif_match(r->kif, kif) == r->ifnot)
1421 			r = r->skip[PF_SKIP_IFP].ptr;
1422 		else if (r->direction && r->direction != dir)
1423 			r = r->skip[PF_SKIP_DIR].ptr;
1424 		else if (r->af && r->af != AF_INET6)
1425 			r = r->skip[PF_SKIP_AF].ptr;
1426 #if 0 /* header chain! */
1427 		else if (r->proto && r->proto != h->ip6_nxt)
1428 			r = r->skip[PF_SKIP_PROTO].ptr;
1429 #endif
1430 		else if (PF_MISMATCHAW(&r->src.addr,
1431 		    (struct pf_addr *)&h->ip6_src, AF_INET6,
1432 		    r->src.neg, kif, M_GETFIB(m)))
1433 			r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1434 		else if (PF_MISMATCHAW(&r->dst.addr,
1435 		    (struct pf_addr *)&h->ip6_dst, AF_INET6,
1436 		    r->dst.neg, NULL, M_GETFIB(m)))
1437 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1438 		else
1439 			break;
1440 	}
1441 
1442 	if (r == NULL || r->action == PF_NOSCRUB)
1443 		return (PF_PASS);
1444 	else {
1445 		r->packets[dir == PF_OUT]++;
1446 		r->bytes[dir == PF_OUT] += pd->tot_len;
1447 	}
1448 
1449 	/* Check for illegal packets */
1450 	if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1451 		goto drop;
1452 
1453 	extoff = 0;
1454 	off = sizeof(struct ip6_hdr);
1455 	proto = h->ip6_nxt;
1456 	terminal = 0;
1457 	do {
1458 		switch (proto) {
1459 		case IPPROTO_FRAGMENT:
1460 			goto fragment;
1461 			break;
1462 		case IPPROTO_AH:
1463 		case IPPROTO_ROUTING:
1464 		case IPPROTO_DSTOPTS:
1465 			if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1466 			    NULL, AF_INET6))
1467 				goto shortpkt;
1468 			extoff = off;
1469 			if (proto == IPPROTO_AH)
1470 				off += (ext.ip6e_len + 2) * 4;
1471 			else
1472 				off += (ext.ip6e_len + 1) * 8;
1473 			proto = ext.ip6e_nxt;
1474 			break;
1475 		case IPPROTO_HOPOPTS:
1476 			if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1477 			    NULL, AF_INET6))
1478 				goto shortpkt;
1479 			extoff = off;
1480 			optend = off + (ext.ip6e_len + 1) * 8;
1481 			ooff = off + sizeof(ext);
1482 			do {
1483 				if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1484 				    sizeof(opt.ip6o_type), NULL, NULL,
1485 				    AF_INET6))
1486 					goto shortpkt;
1487 				if (opt.ip6o_type == IP6OPT_PAD1) {
1488 					ooff++;
1489 					continue;
1490 				}
1491 				if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1492 				    NULL, NULL, AF_INET6))
1493 					goto shortpkt;
1494 				if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1495 					goto drop;
1496 				switch (opt.ip6o_type) {
1497 				case IP6OPT_JUMBO:
1498 					if (h->ip6_plen != 0)
1499 						goto drop;
1500 					if (!pf_pull_hdr(m, ooff, &jumbo,
1501 					    sizeof(jumbo), NULL, NULL,
1502 					    AF_INET6))
1503 						goto shortpkt;
1504 					memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1505 					    sizeof(jumbolen));
1506 					jumbolen = ntohl(jumbolen);
1507 					if (jumbolen <= IPV6_MAXPACKET)
1508 						goto drop;
1509 					if (sizeof(struct ip6_hdr) + jumbolen !=
1510 					    m->m_pkthdr.len)
1511 						goto drop;
1512 					break;
1513 				default:
1514 					break;
1515 				}
1516 				ooff += sizeof(opt) + opt.ip6o_len;
1517 			} while (ooff < optend);
1518 
1519 			off = optend;
1520 			proto = ext.ip6e_nxt;
1521 			break;
1522 		default:
1523 			terminal = 1;
1524 			break;
1525 		}
1526 	} while (!terminal);
1527 
1528 	/* jumbo payload option must be present, or plen > 0 */
1529 	if (ntohs(h->ip6_plen) == 0)
1530 		plen = jumbolen;
1531 	else
1532 		plen = ntohs(h->ip6_plen);
1533 	if (plen == 0)
1534 		goto drop;
1535 	if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1536 		goto shortpkt;
1537 
1538 	pf_scrub_ip6(&m, r->min_ttl);
1539 
1540 	return (PF_PASS);
1541 
1542  fragment:
1543 	/* Jumbo payload packets cannot be fragmented. */
1544 	plen = ntohs(h->ip6_plen);
1545 	if (plen == 0 || jumbolen)
1546 		goto drop;
1547 	if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1548 		goto shortpkt;
1549 
1550 	if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1551 		goto shortpkt;
1552 
1553 	/* Offset now points to data portion. */
1554 	off += sizeof(frag);
1555 
1556 	/* Returns PF_DROP or *m0 is NULL or completely reassembled mbuf. */
1557 	if (pf_reassemble6(m0, h, &frag, off, extoff, dir, reason) != PF_PASS)
1558 		return (PF_DROP);
1559 	m = *m0;
1560 	if (m == NULL)
1561 		return (PF_DROP);
1562 
1563 	pd->flags |= PFDESC_IP_REAS;
1564 	return (PF_PASS);
1565 
1566  shortpkt:
1567 	REASON_SET(reason, PFRES_SHORT);
1568 	if (r != NULL && r->log)
1569 		PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1570 		    1);
1571 	return (PF_DROP);
1572 
1573  drop:
1574 	REASON_SET(reason, PFRES_NORM);
1575 	if (r != NULL && r->log)
1576 		PFLOG_PACKET(kif, m, AF_INET6, dir, *reason, r, NULL, NULL, pd,
1577 		    1);
1578 	return (PF_DROP);
1579 }
1580 #endif /* INET6 */
1581 
1582 int
1583 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1584     int off, void *h, struct pf_pdesc *pd)
1585 {
1586 	struct pf_rule	*r, *rm = NULL;
1587 	struct tcphdr	*th = pd->hdr.tcp;
1588 	int		 rewrite = 0;
1589 	u_short		 reason;
1590 	u_int8_t	 flags;
1591 	sa_family_t	 af = pd->af;
1592 
1593 	PF_RULES_RASSERT();
1594 
1595 	r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1596 	while (r != NULL) {
1597 		r->evaluations++;
1598 		if (pfi_kif_match(r->kif, kif) == r->ifnot)
1599 			r = r->skip[PF_SKIP_IFP].ptr;
1600 		else if (r->direction && r->direction != dir)
1601 			r = r->skip[PF_SKIP_DIR].ptr;
1602 		else if (r->af && r->af != af)
1603 			r = r->skip[PF_SKIP_AF].ptr;
1604 		else if (r->proto && r->proto != pd->proto)
1605 			r = r->skip[PF_SKIP_PROTO].ptr;
1606 		else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
1607 		    r->src.neg, kif, M_GETFIB(m)))
1608 			r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1609 		else if (r->src.port_op && !pf_match_port(r->src.port_op,
1610 			    r->src.port[0], r->src.port[1], th->th_sport))
1611 			r = r->skip[PF_SKIP_SRC_PORT].ptr;
1612 		else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
1613 		    r->dst.neg, NULL, M_GETFIB(m)))
1614 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1615 		else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1616 			    r->dst.port[0], r->dst.port[1], th->th_dport))
1617 			r = r->skip[PF_SKIP_DST_PORT].ptr;
1618 		else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1619 			    pf_osfp_fingerprint(pd, m, off, th),
1620 			    r->os_fingerprint))
1621 			r = TAILQ_NEXT(r, entries);
1622 		else {
1623 			rm = r;
1624 			break;
1625 		}
1626 	}
1627 
1628 	if (rm == NULL || rm->action == PF_NOSCRUB)
1629 		return (PF_PASS);
1630 	else {
1631 		r->packets[dir == PF_OUT]++;
1632 		r->bytes[dir == PF_OUT] += pd->tot_len;
1633 	}
1634 
1635 	if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1636 		pd->flags |= PFDESC_TCP_NORM;
1637 
1638 	flags = th->th_flags;
1639 	if (flags & TH_SYN) {
1640 		/* Illegal packet */
1641 		if (flags & TH_RST)
1642 			goto tcp_drop;
1643 
1644 		if (flags & TH_FIN)
1645 			goto tcp_drop;
1646 	} else {
1647 		/* Illegal packet */
1648 		if (!(flags & (TH_ACK|TH_RST)))
1649 			goto tcp_drop;
1650 	}
1651 
1652 	if (!(flags & TH_ACK)) {
1653 		/* These flags are only valid if ACK is set */
1654 		if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1655 			goto tcp_drop;
1656 	}
1657 
1658 	/* Check for illegal header length */
1659 	if (th->th_off < (sizeof(struct tcphdr) >> 2))
1660 		goto tcp_drop;
1661 
1662 	/* If flags changed, or reserved data set, then adjust */
1663 	if (flags != th->th_flags || th->th_x2 != 0) {
1664 		u_int16_t	ov, nv;
1665 
1666 		ov = *(u_int16_t *)(&th->th_ack + 1);
1667 		th->th_flags = flags;
1668 		th->th_x2 = 0;
1669 		nv = *(u_int16_t *)(&th->th_ack + 1);
1670 
1671 		th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1672 		rewrite = 1;
1673 	}
1674 
1675 	/* Remove urgent pointer, if TH_URG is not set */
1676 	if (!(flags & TH_URG) && th->th_urp) {
1677 		th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1678 		th->th_urp = 0;
1679 		rewrite = 1;
1680 	}
1681 
1682 	/* Process options */
1683 	if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af))
1684 		rewrite = 1;
1685 
1686 	/* copy back packet headers if we sanitized */
1687 	if (rewrite)
1688 		m_copyback(m, off, sizeof(*th), (caddr_t)th);
1689 
1690 	return (PF_PASS);
1691 
1692  tcp_drop:
1693 	REASON_SET(&reason, PFRES_NORM);
1694 	if (rm != NULL && r->log)
1695 		PFLOG_PACKET(kif, m, AF_INET, dir, reason, r, NULL, NULL, pd,
1696 		    1);
1697 	return (PF_DROP);
1698 }
1699 
1700 int
1701 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1702     struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1703 {
1704 	u_int32_t tsval, tsecr;
1705 	u_int8_t hdr[60];
1706 	u_int8_t *opt;
1707 
1708 	KASSERT((src->scrub == NULL),
1709 	    ("pf_normalize_tcp_init: src->scrub != NULL"));
1710 
1711 	src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT);
1712 	if (src->scrub == NULL)
1713 		return (1);
1714 
1715 	switch (pd->af) {
1716 #ifdef INET
1717 	case AF_INET: {
1718 		struct ip *h = mtod(m, struct ip *);
1719 		src->scrub->pfss_ttl = h->ip_ttl;
1720 		break;
1721 	}
1722 #endif /* INET */
1723 #ifdef INET6
1724 	case AF_INET6: {
1725 		struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1726 		src->scrub->pfss_ttl = h->ip6_hlim;
1727 		break;
1728 	}
1729 #endif /* INET6 */
1730 	}
1731 
1732 
1733 	/*
1734 	 * All normalizations below are only begun if we see the start of
1735 	 * the connections.  They must all set an enabled bit in pfss_flags
1736 	 */
1737 	if ((th->th_flags & TH_SYN) == 0)
1738 		return (0);
1739 
1740 
1741 	if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1742 	    pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1743 		/* Diddle with TCP options */
1744 		int hlen;
1745 		opt = hdr + sizeof(struct tcphdr);
1746 		hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1747 		while (hlen >= TCPOLEN_TIMESTAMP) {
1748 			switch (*opt) {
1749 			case TCPOPT_EOL:	/* FALLTHROUGH */
1750 			case TCPOPT_NOP:
1751 				opt++;
1752 				hlen--;
1753 				break;
1754 			case TCPOPT_TIMESTAMP:
1755 				if (opt[1] >= TCPOLEN_TIMESTAMP) {
1756 					src->scrub->pfss_flags |=
1757 					    PFSS_TIMESTAMP;
1758 					src->scrub->pfss_ts_mod =
1759 					    htonl(arc4random());
1760 
1761 					/* note PFSS_PAWS not set yet */
1762 					memcpy(&tsval, &opt[2],
1763 					    sizeof(u_int32_t));
1764 					memcpy(&tsecr, &opt[6],
1765 					    sizeof(u_int32_t));
1766 					src->scrub->pfss_tsval0 = ntohl(tsval);
1767 					src->scrub->pfss_tsval = ntohl(tsval);
1768 					src->scrub->pfss_tsecr = ntohl(tsecr);
1769 					getmicrouptime(&src->scrub->pfss_last);
1770 				}
1771 				/* FALLTHROUGH */
1772 			default:
1773 				hlen -= MAX(opt[1], 2);
1774 				opt += MAX(opt[1], 2);
1775 				break;
1776 			}
1777 		}
1778 	}
1779 
1780 	return (0);
1781 }
1782 
1783 void
1784 pf_normalize_tcp_cleanup(struct pf_state *state)
1785 {
1786 	if (state->src.scrub)
1787 		uma_zfree(V_pf_state_scrub_z, state->src.scrub);
1788 	if (state->dst.scrub)
1789 		uma_zfree(V_pf_state_scrub_z, state->dst.scrub);
1790 
1791 	/* Someday... flush the TCP segment reassembly descriptors. */
1792 }
1793 
1794 int
1795 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1796     u_short *reason, struct tcphdr *th, struct pf_state *state,
1797     struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1798 {
1799 	struct timeval uptime;
1800 	u_int32_t tsval, tsecr;
1801 	u_int tsval_from_last;
1802 	u_int8_t hdr[60];
1803 	u_int8_t *opt;
1804 	int copyback = 0;
1805 	int got_ts = 0;
1806 
1807 	KASSERT((src->scrub || dst->scrub),
1808 	    ("%s: src->scrub && dst->scrub!", __func__));
1809 
1810 	/*
1811 	 * Enforce the minimum TTL seen for this connection.  Negate a common
1812 	 * technique to evade an intrusion detection system and confuse
1813 	 * firewall state code.
1814 	 */
1815 	switch (pd->af) {
1816 #ifdef INET
1817 	case AF_INET: {
1818 		if (src->scrub) {
1819 			struct ip *h = mtod(m, struct ip *);
1820 			if (h->ip_ttl > src->scrub->pfss_ttl)
1821 				src->scrub->pfss_ttl = h->ip_ttl;
1822 			h->ip_ttl = src->scrub->pfss_ttl;
1823 		}
1824 		break;
1825 	}
1826 #endif /* INET */
1827 #ifdef INET6
1828 	case AF_INET6: {
1829 		if (src->scrub) {
1830 			struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1831 			if (h->ip6_hlim > src->scrub->pfss_ttl)
1832 				src->scrub->pfss_ttl = h->ip6_hlim;
1833 			h->ip6_hlim = src->scrub->pfss_ttl;
1834 		}
1835 		break;
1836 	}
1837 #endif /* INET6 */
1838 	}
1839 
1840 	if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1841 	    ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1842 	    (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1843 	    pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1844 		/* Diddle with TCP options */
1845 		int hlen;
1846 		opt = hdr + sizeof(struct tcphdr);
1847 		hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1848 		while (hlen >= TCPOLEN_TIMESTAMP) {
1849 			switch (*opt) {
1850 			case TCPOPT_EOL:	/* FALLTHROUGH */
1851 			case TCPOPT_NOP:
1852 				opt++;
1853 				hlen--;
1854 				break;
1855 			case TCPOPT_TIMESTAMP:
1856 				/* Modulate the timestamps.  Can be used for
1857 				 * NAT detection, OS uptime determination or
1858 				 * reboot detection.
1859 				 */
1860 
1861 				if (got_ts) {
1862 					/* Huh?  Multiple timestamps!? */
1863 					if (V_pf_status.debug >= PF_DEBUG_MISC) {
1864 						DPFPRINTF(("multiple TS??"));
1865 						pf_print_state(state);
1866 						printf("\n");
1867 					}
1868 					REASON_SET(reason, PFRES_TS);
1869 					return (PF_DROP);
1870 				}
1871 				if (opt[1] >= TCPOLEN_TIMESTAMP) {
1872 					memcpy(&tsval, &opt[2],
1873 					    sizeof(u_int32_t));
1874 					if (tsval && src->scrub &&
1875 					    (src->scrub->pfss_flags &
1876 					    PFSS_TIMESTAMP)) {
1877 						tsval = ntohl(tsval);
1878 						pf_change_a(&opt[2],
1879 						    &th->th_sum,
1880 						    htonl(tsval +
1881 						    src->scrub->pfss_ts_mod),
1882 						    0);
1883 						copyback = 1;
1884 					}
1885 
1886 					/* Modulate TS reply iff valid (!0) */
1887 					memcpy(&tsecr, &opt[6],
1888 					    sizeof(u_int32_t));
1889 					if (tsecr && dst->scrub &&
1890 					    (dst->scrub->pfss_flags &
1891 					    PFSS_TIMESTAMP)) {
1892 						tsecr = ntohl(tsecr)
1893 						    - dst->scrub->pfss_ts_mod;
1894 						pf_change_a(&opt[6],
1895 						    &th->th_sum, htonl(tsecr),
1896 						    0);
1897 						copyback = 1;
1898 					}
1899 					got_ts = 1;
1900 				}
1901 				/* FALLTHROUGH */
1902 			default:
1903 				hlen -= MAX(opt[1], 2);
1904 				opt += MAX(opt[1], 2);
1905 				break;
1906 			}
1907 		}
1908 		if (copyback) {
1909 			/* Copyback the options, caller copys back header */
1910 			*writeback = 1;
1911 			m_copyback(m, off + sizeof(struct tcphdr),
1912 			    (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1913 			    sizeof(struct tcphdr));
1914 		}
1915 	}
1916 
1917 
1918 	/*
1919 	 * Must invalidate PAWS checks on connections idle for too long.
1920 	 * The fastest allowed timestamp clock is 1ms.  That turns out to
1921 	 * be about 24 days before it wraps.  XXX Right now our lowerbound
1922 	 * TS echo check only works for the first 12 days of a connection
1923 	 * when the TS has exhausted half its 32bit space
1924 	 */
1925 #define TS_MAX_IDLE	(24*24*60*60)
1926 #define TS_MAX_CONN	(12*24*60*60)	/* XXX remove when better tsecr check */
1927 
1928 	getmicrouptime(&uptime);
1929 	if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1930 	    (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1931 	    time_uptime - state->creation > TS_MAX_CONN))  {
1932 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
1933 			DPFPRINTF(("src idled out of PAWS\n"));
1934 			pf_print_state(state);
1935 			printf("\n");
1936 		}
1937 		src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1938 		    | PFSS_PAWS_IDLED;
1939 	}
1940 	if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1941 	    uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1942 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
1943 			DPFPRINTF(("dst idled out of PAWS\n"));
1944 			pf_print_state(state);
1945 			printf("\n");
1946 		}
1947 		dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1948 		    | PFSS_PAWS_IDLED;
1949 	}
1950 
1951 	if (got_ts && src->scrub && dst->scrub &&
1952 	    (src->scrub->pfss_flags & PFSS_PAWS) &&
1953 	    (dst->scrub->pfss_flags & PFSS_PAWS)) {
1954 		/* Validate that the timestamps are "in-window".
1955 		 * RFC1323 describes TCP Timestamp options that allow
1956 		 * measurement of RTT (round trip time) and PAWS
1957 		 * (protection against wrapped sequence numbers).  PAWS
1958 		 * gives us a set of rules for rejecting packets on
1959 		 * long fat pipes (packets that were somehow delayed
1960 		 * in transit longer than the time it took to send the
1961 		 * full TCP sequence space of 4Gb).  We can use these
1962 		 * rules and infer a few others that will let us treat
1963 		 * the 32bit timestamp and the 32bit echoed timestamp
1964 		 * as sequence numbers to prevent a blind attacker from
1965 		 * inserting packets into a connection.
1966 		 *
1967 		 * RFC1323 tells us:
1968 		 *  - The timestamp on this packet must be greater than
1969 		 *    or equal to the last value echoed by the other
1970 		 *    endpoint.  The RFC says those will be discarded
1971 		 *    since it is a dup that has already been acked.
1972 		 *    This gives us a lowerbound on the timestamp.
1973 		 *        timestamp >= other last echoed timestamp
1974 		 *  - The timestamp will be less than or equal to
1975 		 *    the last timestamp plus the time between the
1976 		 *    last packet and now.  The RFC defines the max
1977 		 *    clock rate as 1ms.  We will allow clocks to be
1978 		 *    up to 10% fast and will allow a total difference
1979 		 *    or 30 seconds due to a route change.  And this
1980 		 *    gives us an upperbound on the timestamp.
1981 		 *        timestamp <= last timestamp + max ticks
1982 		 *    We have to be careful here.  Windows will send an
1983 		 *    initial timestamp of zero and then initialize it
1984 		 *    to a random value after the 3whs; presumably to
1985 		 *    avoid a DoS by having to call an expensive RNG
1986 		 *    during a SYN flood.  Proof MS has at least one
1987 		 *    good security geek.
1988 		 *
1989 		 *  - The TCP timestamp option must also echo the other
1990 		 *    endpoints timestamp.  The timestamp echoed is the
1991 		 *    one carried on the earliest unacknowledged segment
1992 		 *    on the left edge of the sequence window.  The RFC
1993 		 *    states that the host will reject any echoed
1994 		 *    timestamps that were larger than any ever sent.
1995 		 *    This gives us an upperbound on the TS echo.
1996 		 *        tescr <= largest_tsval
1997 		 *  - The lowerbound on the TS echo is a little more
1998 		 *    tricky to determine.  The other endpoint's echoed
1999 		 *    values will not decrease.  But there may be
2000 		 *    network conditions that re-order packets and
2001 		 *    cause our view of them to decrease.  For now the
2002 		 *    only lowerbound we can safely determine is that
2003 		 *    the TS echo will never be less than the original
2004 		 *    TS.  XXX There is probably a better lowerbound.
2005 		 *    Remove TS_MAX_CONN with better lowerbound check.
2006 		 *        tescr >= other original TS
2007 		 *
2008 		 * It is also important to note that the fastest
2009 		 * timestamp clock of 1ms will wrap its 32bit space in
2010 		 * 24 days.  So we just disable TS checking after 24
2011 		 * days of idle time.  We actually must use a 12d
2012 		 * connection limit until we can come up with a better
2013 		 * lowerbound to the TS echo check.
2014 		 */
2015 		struct timeval delta_ts;
2016 		int ts_fudge;
2017 
2018 
2019 		/*
2020 		 * PFTM_TS_DIFF is how many seconds of leeway to allow
2021 		 * a host's timestamp.  This can happen if the previous
2022 		 * packet got delayed in transit for much longer than
2023 		 * this packet.
2024 		 */
2025 		if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
2026 			ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF];
2027 
2028 		/* Calculate max ticks since the last timestamp */
2029 #define TS_MAXFREQ	1100		/* RFC max TS freq of 1Khz + 10% skew */
2030 #define TS_MICROSECS	1000000		/* microseconds per second */
2031 		delta_ts = uptime;
2032 		timevalsub(&delta_ts, &src->scrub->pfss_last);
2033 		tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
2034 		tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
2035 
2036 		if ((src->state >= TCPS_ESTABLISHED &&
2037 		    dst->state >= TCPS_ESTABLISHED) &&
2038 		    (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
2039 		    SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
2040 		    (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
2041 		    SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
2042 			/* Bad RFC1323 implementation or an insertion attack.
2043 			 *
2044 			 * - Solaris 2.6 and 2.7 are known to send another ACK
2045 			 *   after the FIN,FIN|ACK,ACK closing that carries
2046 			 *   an old timestamp.
2047 			 */
2048 
2049 			DPFPRINTF(("Timestamp failed %c%c%c%c\n",
2050 			    SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
2051 			    SEQ_GT(tsval, src->scrub->pfss_tsval +
2052 			    tsval_from_last) ? '1' : ' ',
2053 			    SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
2054 			    SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
2055 			DPFPRINTF((" tsval: %u  tsecr: %u  +ticks: %u  "
2056 			    "idle: %jus %lums\n",
2057 			    tsval, tsecr, tsval_from_last,
2058 			    (uintmax_t)delta_ts.tv_sec,
2059 			    delta_ts.tv_usec / 1000));
2060 			DPFPRINTF((" src->tsval: %u  tsecr: %u\n",
2061 			    src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
2062 			DPFPRINTF((" dst->tsval: %u  tsecr: %u  tsval0: %u"
2063 			    "\n", dst->scrub->pfss_tsval,
2064 			    dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
2065 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
2066 				pf_print_state(state);
2067 				pf_print_flags(th->th_flags);
2068 				printf("\n");
2069 			}
2070 			REASON_SET(reason, PFRES_TS);
2071 			return (PF_DROP);
2072 		}
2073 
2074 		/* XXX I'd really like to require tsecr but it's optional */
2075 
2076 	} else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
2077 	    ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
2078 	    || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
2079 	    src->scrub && dst->scrub &&
2080 	    (src->scrub->pfss_flags & PFSS_PAWS) &&
2081 	    (dst->scrub->pfss_flags & PFSS_PAWS)) {
2082 		/* Didn't send a timestamp.  Timestamps aren't really useful
2083 		 * when:
2084 		 *  - connection opening or closing (often not even sent).
2085 		 *    but we must not let an attacker to put a FIN on a
2086 		 *    data packet to sneak it through our ESTABLISHED check.
2087 		 *  - on a TCP reset.  RFC suggests not even looking at TS.
2088 		 *  - on an empty ACK.  The TS will not be echoed so it will
2089 		 *    probably not help keep the RTT calculation in sync and
2090 		 *    there isn't as much danger when the sequence numbers
2091 		 *    got wrapped.  So some stacks don't include TS on empty
2092 		 *    ACKs :-(
2093 		 *
2094 		 * To minimize the disruption to mostly RFC1323 conformant
2095 		 * stacks, we will only require timestamps on data packets.
2096 		 *
2097 		 * And what do ya know, we cannot require timestamps on data
2098 		 * packets.  There appear to be devices that do legitimate
2099 		 * TCP connection hijacking.  There are HTTP devices that allow
2100 		 * a 3whs (with timestamps) and then buffer the HTTP request.
2101 		 * If the intermediate device has the HTTP response cache, it
2102 		 * will spoof the response but not bother timestamping its
2103 		 * packets.  So we can look for the presence of a timestamp in
2104 		 * the first data packet and if there, require it in all future
2105 		 * packets.
2106 		 */
2107 
2108 		if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
2109 			/*
2110 			 * Hey!  Someone tried to sneak a packet in.  Or the
2111 			 * stack changed its RFC1323 behavior?!?!
2112 			 */
2113 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
2114 				DPFPRINTF(("Did not receive expected RFC1323 "
2115 				    "timestamp\n"));
2116 				pf_print_state(state);
2117 				pf_print_flags(th->th_flags);
2118 				printf("\n");
2119 			}
2120 			REASON_SET(reason, PFRES_TS);
2121 			return (PF_DROP);
2122 		}
2123 	}
2124 
2125 
2126 	/*
2127 	 * We will note if a host sends his data packets with or without
2128 	 * timestamps.  And require all data packets to contain a timestamp
2129 	 * if the first does.  PAWS implicitly requires that all data packets be
2130 	 * timestamped.  But I think there are middle-man devices that hijack
2131 	 * TCP streams immediately after the 3whs and don't timestamp their
2132 	 * packets (seen in a WWW accelerator or cache).
2133 	 */
2134 	if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
2135 	    (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
2136 		if (got_ts)
2137 			src->scrub->pfss_flags |= PFSS_DATA_TS;
2138 		else {
2139 			src->scrub->pfss_flags |= PFSS_DATA_NOTS;
2140 			if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
2141 			    (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
2142 				/* Don't warn if other host rejected RFC1323 */
2143 				DPFPRINTF(("Broken RFC1323 stack did not "
2144 				    "timestamp data packet. Disabled PAWS "
2145 				    "security.\n"));
2146 				pf_print_state(state);
2147 				pf_print_flags(th->th_flags);
2148 				printf("\n");
2149 			}
2150 		}
2151 	}
2152 
2153 
2154 	/*
2155 	 * Update PAWS values
2156 	 */
2157 	if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
2158 	    (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
2159 		getmicrouptime(&src->scrub->pfss_last);
2160 		if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
2161 		    (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2162 			src->scrub->pfss_tsval = tsval;
2163 
2164 		if (tsecr) {
2165 			if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
2166 			    (src->scrub->pfss_flags & PFSS_PAWS) == 0)
2167 				src->scrub->pfss_tsecr = tsecr;
2168 
2169 			if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
2170 			    (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
2171 			    src->scrub->pfss_tsval0 == 0)) {
2172 				/* tsval0 MUST be the lowest timestamp */
2173 				src->scrub->pfss_tsval0 = tsval;
2174 			}
2175 
2176 			/* Only fully initialized after a TS gets echoed */
2177 			if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
2178 				src->scrub->pfss_flags |= PFSS_PAWS;
2179 		}
2180 	}
2181 
2182 	/* I have a dream....  TCP segment reassembly.... */
2183 	return (0);
2184 }
2185 
2186 static int
2187 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
2188     int off, sa_family_t af)
2189 {
2190 	u_int16_t	*mss;
2191 	int		 thoff;
2192 	int		 opt, cnt, optlen = 0;
2193 	int		 rewrite = 0;
2194 	u_char		 opts[TCP_MAXOLEN];
2195 	u_char		*optp = opts;
2196 
2197 	thoff = th->th_off << 2;
2198 	cnt = thoff - sizeof(struct tcphdr);
2199 
2200 	if (cnt > 0 && !pf_pull_hdr(m, off + sizeof(*th), opts, cnt,
2201 	    NULL, NULL, af))
2202 		return (rewrite);
2203 
2204 	for (; cnt > 0; cnt -= optlen, optp += optlen) {
2205 		opt = optp[0];
2206 		if (opt == TCPOPT_EOL)
2207 			break;
2208 		if (opt == TCPOPT_NOP)
2209 			optlen = 1;
2210 		else {
2211 			if (cnt < 2)
2212 				break;
2213 			optlen = optp[1];
2214 			if (optlen < 2 || optlen > cnt)
2215 				break;
2216 		}
2217 		switch (opt) {
2218 		case TCPOPT_MAXSEG:
2219 			mss = (u_int16_t *)(optp + 2);
2220 			if ((ntohs(*mss)) > r->max_mss) {
2221 				th->th_sum = pf_cksum_fixup(th->th_sum,
2222 				    *mss, htons(r->max_mss), 0);
2223 				*mss = htons(r->max_mss);
2224 				rewrite = 1;
2225 			}
2226 			break;
2227 		default:
2228 			break;
2229 		}
2230 	}
2231 
2232 	if (rewrite)
2233 		m_copyback(m, off + sizeof(*th), thoff - sizeof(*th), opts);
2234 
2235 	return (rewrite);
2236 }
2237 
2238 #ifdef INET
2239 static void
2240 pf_scrub_ip(struct mbuf **m0, u_int32_t flags, u_int8_t min_ttl, u_int8_t tos)
2241 {
2242 	struct mbuf		*m = *m0;
2243 	struct ip		*h = mtod(m, struct ip *);
2244 
2245 	/* Clear IP_DF if no-df was requested */
2246 	if (flags & PFRULE_NODF && h->ip_off & htons(IP_DF)) {
2247 		u_int16_t ip_off = h->ip_off;
2248 
2249 		h->ip_off &= htons(~IP_DF);
2250 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0);
2251 	}
2252 
2253 	/* Enforce a minimum ttl, may cause endless packet loops */
2254 	if (min_ttl && h->ip_ttl < min_ttl) {
2255 		u_int16_t ip_ttl = h->ip_ttl;
2256 
2257 		h->ip_ttl = min_ttl;
2258 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
2259 	}
2260 
2261 	/* Enforce tos */
2262 	if (flags & PFRULE_SET_TOS) {
2263 		u_int16_t	ov, nv;
2264 
2265 		ov = *(u_int16_t *)h;
2266 		h->ip_tos = tos;
2267 		nv = *(u_int16_t *)h;
2268 
2269 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
2270 	}
2271 
2272 	/* random-id, but not for fragments */
2273 	if (flags & PFRULE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) {
2274 		uint16_t ip_id = h->ip_id;
2275 
2276 		ip_fillid(h);
2277 		h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
2278 	}
2279 }
2280 #endif /* INET */
2281 
2282 #ifdef INET6
2283 static void
2284 pf_scrub_ip6(struct mbuf **m0, u_int8_t min_ttl)
2285 {
2286 	struct mbuf		*m = *m0;
2287 	struct ip6_hdr		*h = mtod(m, struct ip6_hdr *);
2288 
2289 	/* Enforce a minimum ttl, may cause endless packet loops */
2290 	if (min_ttl && h->ip6_hlim < min_ttl)
2291 		h->ip6_hlim = min_ttl;
2292 }
2293 #endif
2294