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