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