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