xref: /freebsd/sys/netpfil/pf/pf.c (revision 550cb4ab85c7e514629c8bacbbb07085b81d916b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002 - 2008 Henning Brauer
6  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    - Redistributions of source code must retain the above copyright
14  *      notice, this list of conditions and the following disclaimer.
15  *    - Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials provided
18  *      with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Effort sponsored in part by the Defense Advanced Research Projects
34  * Agency (DARPA) and Air Force Research Laboratory, Air Force
35  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36  *
37  *	$OpenBSD: pf.c,v 1.634 2009/02/27 12:37:45 henning Exp $
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_bpf.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_pf.h"
47 #include "opt_sctp.h"
48 
49 #include <sys/param.h>
50 #include <sys/bus.h>
51 #include <sys/endian.h>
52 #include <sys/gsb_crc32.h>
53 #include <sys/hash.h>
54 #include <sys/interrupt.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
57 #include <sys/limits.h>
58 #include <sys/mbuf.h>
59 #include <sys/md5.h>
60 #include <sys/random.h>
61 #include <sys/refcount.h>
62 #include <sys/sdt.h>
63 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/taskqueue.h>
66 #include <sys/ucred.h>
67 
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_types.h>
71 #include <net/if_vlan_var.h>
72 #include <net/route.h>
73 #include <net/route/nhop.h>
74 #include <net/vnet.h>
75 
76 #include <net/pfil.h>
77 #include <net/pfvar.h>
78 #include <net/if_pflog.h>
79 #include <net/if_pfsync.h>
80 
81 #include <netinet/in_pcb.h>
82 #include <netinet/in_var.h>
83 #include <netinet/in_fib.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_fw.h>
86 #include <netinet/ip_icmp.h>
87 #include <netinet/icmp_var.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/tcp.h>
90 #include <netinet/tcp_fsm.h>
91 #include <netinet/tcp_seq.h>
92 #include <netinet/tcp_timer.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/udp.h>
95 #include <netinet/udp_var.h>
96 
97 #ifdef INET6
98 #include <netinet/ip6.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/in6_fib.h>
104 #include <netinet6/scope6_var.h>
105 #endif /* INET6 */
106 
107 #if defined(SCTP) || defined(SCTP_SUPPORT)
108 #include <netinet/sctp_crc32.h>
109 #endif
110 
111 #include <machine/in_cksum.h>
112 #include <security/mac/mac_framework.h>
113 
114 #define	DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
115 
116 SDT_PROVIDER_DEFINE(pf);
117 SDT_PROBE_DEFINE4(pf, ip, test, done, "int", "int", "struct pf_krule *",
118     "struct pf_state *");
119 SDT_PROBE_DEFINE4(pf, ip, test6, done, "int", "int", "struct pf_krule *",
120     "struct pf_state *");
121 SDT_PROBE_DEFINE5(pf, ip, state, lookup, "struct pfi_kkif *",
122     "struct pf_state_key_cmp *", "int", "struct pf_pdesc *",
123     "struct pf_state *");
124 
125 /*
126  * Global variables
127  */
128 
129 /* state tables */
130 VNET_DEFINE(struct pf_altqqueue,	 pf_altqs[4]);
131 VNET_DEFINE(struct pf_kpalist,		 pf_pabuf);
132 VNET_DEFINE(struct pf_altqqueue *,	 pf_altqs_active);
133 VNET_DEFINE(struct pf_altqqueue *,	 pf_altq_ifs_active);
134 VNET_DEFINE(struct pf_altqqueue *,	 pf_altqs_inactive);
135 VNET_DEFINE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
136 VNET_DEFINE(struct pf_kstatus,		 pf_status);
137 
138 VNET_DEFINE(u_int32_t,			 ticket_altqs_active);
139 VNET_DEFINE(u_int32_t,			 ticket_altqs_inactive);
140 VNET_DEFINE(int,			 altqs_inactive_open);
141 VNET_DEFINE(u_int32_t,			 ticket_pabuf);
142 
143 VNET_DEFINE(MD5_CTX,			 pf_tcp_secret_ctx);
144 #define	V_pf_tcp_secret_ctx		 VNET(pf_tcp_secret_ctx)
145 VNET_DEFINE(u_char,			 pf_tcp_secret[16]);
146 #define	V_pf_tcp_secret			 VNET(pf_tcp_secret)
147 VNET_DEFINE(int,			 pf_tcp_secret_init);
148 #define	V_pf_tcp_secret_init		 VNET(pf_tcp_secret_init)
149 VNET_DEFINE(int,			 pf_tcp_iss_off);
150 #define	V_pf_tcp_iss_off		 VNET(pf_tcp_iss_off)
151 VNET_DECLARE(int,			 pf_vnet_active);
152 #define	V_pf_vnet_active		 VNET(pf_vnet_active)
153 
154 VNET_DEFINE_STATIC(uint32_t, pf_purge_idx);
155 #define V_pf_purge_idx	VNET(pf_purge_idx)
156 
157 /*
158  * Queue for pf_intr() sends.
159  */
160 static MALLOC_DEFINE(M_PFTEMP, "pf_temp", "pf(4) temporary allocations");
161 struct pf_send_entry {
162 	STAILQ_ENTRY(pf_send_entry)	pfse_next;
163 	struct mbuf			*pfse_m;
164 	enum {
165 		PFSE_IP,
166 		PFSE_IP6,
167 		PFSE_ICMP,
168 		PFSE_ICMP6,
169 	}				pfse_type;
170 	struct {
171 		int		type;
172 		int		code;
173 		int		mtu;
174 	} icmpopts;
175 };
176 
177 STAILQ_HEAD(pf_send_head, pf_send_entry);
178 VNET_DEFINE_STATIC(struct pf_send_head, pf_sendqueue);
179 #define	V_pf_sendqueue	VNET(pf_sendqueue)
180 
181 static struct mtx pf_sendqueue_mtx;
182 MTX_SYSINIT(pf_sendqueue_mtx, &pf_sendqueue_mtx, "pf send queue", MTX_DEF);
183 #define	PF_SENDQ_LOCK()		mtx_lock(&pf_sendqueue_mtx)
184 #define	PF_SENDQ_UNLOCK()	mtx_unlock(&pf_sendqueue_mtx)
185 
186 /*
187  * Queue for pf_overload_task() tasks.
188  */
189 struct pf_overload_entry {
190 	SLIST_ENTRY(pf_overload_entry)	next;
191 	struct pf_addr  		addr;
192 	sa_family_t			af;
193 	uint8_t				dir;
194 	struct pf_krule  		*rule;
195 };
196 
197 SLIST_HEAD(pf_overload_head, pf_overload_entry);
198 VNET_DEFINE_STATIC(struct pf_overload_head, pf_overloadqueue);
199 #define V_pf_overloadqueue	VNET(pf_overloadqueue)
200 VNET_DEFINE_STATIC(struct task, pf_overloadtask);
201 #define	V_pf_overloadtask	VNET(pf_overloadtask)
202 
203 static struct mtx pf_overloadqueue_mtx;
204 MTX_SYSINIT(pf_overloadqueue_mtx, &pf_overloadqueue_mtx,
205     "pf overload/flush queue", MTX_DEF);
206 #define	PF_OVERLOADQ_LOCK()	mtx_lock(&pf_overloadqueue_mtx)
207 #define	PF_OVERLOADQ_UNLOCK()	mtx_unlock(&pf_overloadqueue_mtx)
208 
209 VNET_DEFINE(struct pf_krulequeue, pf_unlinked_rules);
210 struct mtx pf_unlnkdrules_mtx;
211 MTX_SYSINIT(pf_unlnkdrules_mtx, &pf_unlnkdrules_mtx, "pf unlinked rules",
212     MTX_DEF);
213 
214 VNET_DEFINE_STATIC(uma_zone_t,	pf_sources_z);
215 #define	V_pf_sources_z	VNET(pf_sources_z)
216 uma_zone_t		pf_mtag_z;
217 VNET_DEFINE(uma_zone_t,	 pf_state_z);
218 VNET_DEFINE(uma_zone_t,	 pf_state_key_z);
219 
220 VNET_DEFINE(uint64_t, pf_stateid[MAXCPU]);
221 #define	PFID_CPUBITS	8
222 #define	PFID_CPUSHIFT	(sizeof(uint64_t) * NBBY - PFID_CPUBITS)
223 #define	PFID_CPUMASK	((uint64_t)((1 << PFID_CPUBITS) - 1) <<	PFID_CPUSHIFT)
224 #define	PFID_MAXID	(~PFID_CPUMASK)
225 CTASSERT((1 << PFID_CPUBITS) >= MAXCPU);
226 
227 static void		 pf_src_tree_remove_state(struct pf_state *);
228 static void		 pf_init_threshold(struct pf_threshold *, u_int32_t,
229 			    u_int32_t);
230 static void		 pf_add_threshold(struct pf_threshold *);
231 static int		 pf_check_threshold(struct pf_threshold *);
232 
233 static void		 pf_change_ap(struct mbuf *, struct pf_addr *, u_int16_t *,
234 			    u_int16_t *, u_int16_t *, struct pf_addr *,
235 			    u_int16_t, u_int8_t, sa_family_t);
236 static int		 pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *,
237 			    struct tcphdr *, struct pf_state_peer *);
238 static void		 pf_change_icmp(struct pf_addr *, u_int16_t *,
239 			    struct pf_addr *, struct pf_addr *, u_int16_t,
240 			    u_int16_t *, u_int16_t *, u_int16_t *,
241 			    u_int16_t *, u_int8_t, sa_family_t);
242 static void		 pf_send_tcp(struct mbuf *,
243 			    const struct pf_krule *, sa_family_t,
244 			    const struct pf_addr *, const struct pf_addr *,
245 			    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
246 			    u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
247 			    u_int16_t, struct ifnet *);
248 static void		 pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
249 			    sa_family_t, struct pf_krule *);
250 static void		 pf_detach_state(struct pf_state *);
251 static int		 pf_state_key_attach(struct pf_state_key *,
252 			    struct pf_state_key *, struct pf_state *);
253 static void		 pf_state_key_detach(struct pf_state *, int);
254 static int		 pf_state_key_ctor(void *, int, void *, int);
255 static u_int32_t	 pf_tcp_iss(struct pf_pdesc *);
256 static int		 pf_test_rule(struct pf_krule **, struct pf_state **,
257 			    int, struct pfi_kkif *, struct mbuf *, int,
258 			    struct pf_pdesc *, struct pf_krule **,
259 			    struct pf_kruleset **, struct inpcb *);
260 static int		 pf_create_state(struct pf_krule *, struct pf_krule *,
261 			    struct pf_krule *, struct pf_pdesc *,
262 			    struct pf_ksrc_node *, struct pf_state_key *,
263 			    struct pf_state_key *, struct mbuf *, int,
264 			    u_int16_t, u_int16_t, int *, struct pfi_kkif *,
265 			    struct pf_state **, int, u_int16_t, u_int16_t,
266 			    int);
267 static int		 pf_test_fragment(struct pf_krule **, int,
268 			    struct pfi_kkif *, struct mbuf *, void *,
269 			    struct pf_pdesc *, struct pf_krule **,
270 			    struct pf_kruleset **);
271 static int		 pf_tcp_track_full(struct pf_state_peer *,
272 			    struct pf_state_peer *, struct pf_state **,
273 			    struct pfi_kkif *, struct mbuf *, int,
274 			    struct pf_pdesc *, u_short *, int *);
275 static int		 pf_tcp_track_sloppy(struct pf_state_peer *,
276 			    struct pf_state_peer *, struct pf_state **,
277 			    struct pf_pdesc *, u_short *);
278 static int		 pf_test_state_tcp(struct pf_state **, int,
279 			    struct pfi_kkif *, struct mbuf *, int,
280 			    void *, struct pf_pdesc *, u_short *);
281 static int		 pf_test_state_udp(struct pf_state **, int,
282 			    struct pfi_kkif *, struct mbuf *, int,
283 			    void *, struct pf_pdesc *);
284 static int		 pf_test_state_icmp(struct pf_state **, int,
285 			    struct pfi_kkif *, struct mbuf *, int,
286 			    void *, struct pf_pdesc *, u_short *);
287 static int		 pf_test_state_other(struct pf_state **, int,
288 			    struct pfi_kkif *, struct mbuf *, struct pf_pdesc *);
289 static u_int8_t		 pf_get_wscale(struct mbuf *, int, u_int16_t,
290 			    sa_family_t);
291 static u_int16_t	 pf_get_mss(struct mbuf *, int, u_int16_t,
292 			    sa_family_t);
293 static u_int16_t	 pf_calc_mss(struct pf_addr *, sa_family_t,
294 				int, u_int16_t);
295 static int		 pf_check_proto_cksum(struct mbuf *, int, int,
296 			    u_int8_t, sa_family_t);
297 static void		 pf_print_state_parts(struct pf_state *,
298 			    struct pf_state_key *, struct pf_state_key *);
299 static int		 pf_addr_wrap_neq(struct pf_addr_wrap *,
300 			    struct pf_addr_wrap *);
301 static void		 pf_patch_8(struct mbuf *, u_int16_t *, u_int8_t *, u_int8_t,
302 			    bool, u_int8_t);
303 static struct pf_state	*pf_find_state(struct pfi_kkif *,
304 			    struct pf_state_key_cmp *, u_int);
305 static int		 pf_src_connlimit(struct pf_state **);
306 static void		 pf_overload_task(void *v, int pending);
307 static int		 pf_insert_src_node(struct pf_ksrc_node **,
308 			    struct pf_krule *, struct pf_addr *, sa_family_t);
309 static u_int		 pf_purge_expired_states(u_int, int);
310 static void		 pf_purge_unlinked_rules(void);
311 static int		 pf_mtag_uminit(void *, int, int);
312 static void		 pf_mtag_free(struct m_tag *);
313 #ifdef INET
314 static void		 pf_route(struct mbuf **, struct pf_krule *, int,
315 			    struct ifnet *, struct pf_state *,
316 			    struct pf_pdesc *, struct inpcb *);
317 #endif /* INET */
318 #ifdef INET6
319 static void		 pf_change_a6(struct pf_addr *, u_int16_t *,
320 			    struct pf_addr *, u_int8_t);
321 static void		 pf_route6(struct mbuf **, struct pf_krule *, int,
322 			    struct ifnet *, struct pf_state *,
323 			    struct pf_pdesc *, struct inpcb *);
324 #endif /* INET6 */
325 
326 int in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len);
327 
328 extern int pf_end_threads;
329 extern struct proc *pf_purge_proc;
330 
331 VNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
332 
333 #define	PACKET_LOOPED(pd)	((pd)->pf_mtag &&			\
334 				 (pd)->pf_mtag->flags & PF_PACKET_LOOPED)
335 
336 #define	STATE_LOOKUP(i, k, d, s, pd)					\
337 	do {								\
338 		(s) = pf_find_state((i), (k), (d));			\
339 		SDT_PROBE5(pf, ip, state, lookup, i, k, d, pd, (s));	\
340 		if ((s) == NULL)					\
341 			return (PF_DROP);				\
342 		if (PACKET_LOOPED(pd))					\
343 			return (PF_PASS);				\
344 		if ((d) == PF_OUT &&					\
345 		    (s)->rule.ptr->rt == PF_ROUTETO &&			\
346 		    (s)->rule.ptr->direction == PF_OUT &&		\
347 		    (s)->rt_kif != NULL &&				\
348 		    (s)->rt_kif != (i))					\
349 			return (PF_PASS);				\
350 	} while (0)
351 
352 #define	BOUND_IFACE(r, k) \
353 	((r)->rule_flag & PFRULE_IFBOUND) ? (k) : V_pfi_all
354 
355 #define	STATE_INC_COUNTERS(s)						\
356 	do {								\
357 		counter_u64_add(s->rule.ptr->states_cur, 1);		\
358 		counter_u64_add(s->rule.ptr->states_tot, 1);		\
359 		if (s->anchor.ptr != NULL) {				\
360 			counter_u64_add(s->anchor.ptr->states_cur, 1);	\
361 			counter_u64_add(s->anchor.ptr->states_tot, 1);	\
362 		}							\
363 		if (s->nat_rule.ptr != NULL) {				\
364 			counter_u64_add(s->nat_rule.ptr->states_cur, 1);\
365 			counter_u64_add(s->nat_rule.ptr->states_tot, 1);\
366 		}							\
367 	} while (0)
368 
369 #define	STATE_DEC_COUNTERS(s)						\
370 	do {								\
371 		if (s->nat_rule.ptr != NULL)				\
372 			counter_u64_add(s->nat_rule.ptr->states_cur, -1);\
373 		if (s->anchor.ptr != NULL)				\
374 			counter_u64_add(s->anchor.ptr->states_cur, -1);	\
375 		counter_u64_add(s->rule.ptr->states_cur, -1);		\
376 	} while (0)
377 
378 MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures");
379 VNET_DEFINE(struct pf_keyhash *, pf_keyhash);
380 VNET_DEFINE(struct pf_idhash *, pf_idhash);
381 VNET_DEFINE(struct pf_srchash *, pf_srchash);
382 
383 SYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
384     "pf(4)");
385 
386 u_long	pf_hashmask;
387 u_long	pf_srchashmask;
388 static u_long	pf_hashsize;
389 static u_long	pf_srchashsize;
390 u_long	pf_ioctl_maxcount = 65535;
391 
392 SYSCTL_ULONG(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_RDTUN,
393     &pf_hashsize, 0, "Size of pf(4) states hashtable");
394 SYSCTL_ULONG(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_RDTUN,
395     &pf_srchashsize, 0, "Size of pf(4) source nodes hashtable");
396 SYSCTL_ULONG(_net_pf, OID_AUTO, request_maxcount, CTLFLAG_RWTUN,
397     &pf_ioctl_maxcount, 0, "Maximum number of tables, addresses, ... in a single ioctl() call");
398 
399 VNET_DEFINE(void *, pf_swi_cookie);
400 VNET_DEFINE(struct intr_event *, pf_swi_ie);
401 
402 VNET_DEFINE(uint32_t, pf_hashseed);
403 #define	V_pf_hashseed	VNET(pf_hashseed)
404 
405 int
406 pf_addr_cmp(struct pf_addr *a, struct pf_addr *b, sa_family_t af)
407 {
408 
409 	switch (af) {
410 #ifdef INET
411 	case AF_INET:
412 		if (a->addr32[0] > b->addr32[0])
413 			return (1);
414 		if (a->addr32[0] < b->addr32[0])
415 			return (-1);
416 		break;
417 #endif /* INET */
418 #ifdef INET6
419 	case AF_INET6:
420 		if (a->addr32[3] > b->addr32[3])
421 			return (1);
422 		if (a->addr32[3] < b->addr32[3])
423 			return (-1);
424 		if (a->addr32[2] > b->addr32[2])
425 			return (1);
426 		if (a->addr32[2] < b->addr32[2])
427 			return (-1);
428 		if (a->addr32[1] > b->addr32[1])
429 			return (1);
430 		if (a->addr32[1] < b->addr32[1])
431 			return (-1);
432 		if (a->addr32[0] > b->addr32[0])
433 			return (1);
434 		if (a->addr32[0] < b->addr32[0])
435 			return (-1);
436 		break;
437 #endif /* INET6 */
438 	default:
439 		panic("%s: unknown address family %u", __func__, af);
440 	}
441 	return (0);
442 }
443 
444 static __inline uint32_t
445 pf_hashkey(struct pf_state_key *sk)
446 {
447 	uint32_t h;
448 
449 	h = murmur3_32_hash32((uint32_t *)sk,
450 	    sizeof(struct pf_state_key_cmp)/sizeof(uint32_t),
451 	    V_pf_hashseed);
452 
453 	return (h & pf_hashmask);
454 }
455 
456 static __inline uint32_t
457 pf_hashsrc(struct pf_addr *addr, sa_family_t af)
458 {
459 	uint32_t h;
460 
461 	switch (af) {
462 	case AF_INET:
463 		h = murmur3_32_hash32((uint32_t *)&addr->v4,
464 		    sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed);
465 		break;
466 	case AF_INET6:
467 		h = murmur3_32_hash32((uint32_t *)&addr->v6,
468 		    sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed);
469 		break;
470 	default:
471 		panic("%s: unknown address family %u", __func__, af);
472 	}
473 
474 	return (h & pf_srchashmask);
475 }
476 
477 #ifdef ALTQ
478 static int
479 pf_state_hash(struct pf_state *s)
480 {
481 	u_int32_t hv = (intptr_t)s / sizeof(*s);
482 
483 	hv ^= crc32(&s->src, sizeof(s->src));
484 	hv ^= crc32(&s->dst, sizeof(s->dst));
485 	if (hv == 0)
486 		hv = 1;
487 	return (hv);
488 }
489 #endif
490 
491 #ifdef INET6
492 void
493 pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
494 {
495 	switch (af) {
496 #ifdef INET
497 	case AF_INET:
498 		dst->addr32[0] = src->addr32[0];
499 		break;
500 #endif /* INET */
501 	case AF_INET6:
502 		dst->addr32[0] = src->addr32[0];
503 		dst->addr32[1] = src->addr32[1];
504 		dst->addr32[2] = src->addr32[2];
505 		dst->addr32[3] = src->addr32[3];
506 		break;
507 	}
508 }
509 #endif /* INET6 */
510 
511 static void
512 pf_init_threshold(struct pf_threshold *threshold,
513     u_int32_t limit, u_int32_t seconds)
514 {
515 	threshold->limit = limit * PF_THRESHOLD_MULT;
516 	threshold->seconds = seconds;
517 	threshold->count = 0;
518 	threshold->last = time_uptime;
519 }
520 
521 static void
522 pf_add_threshold(struct pf_threshold *threshold)
523 {
524 	u_int32_t t = time_uptime, diff = t - threshold->last;
525 
526 	if (diff >= threshold->seconds)
527 		threshold->count = 0;
528 	else
529 		threshold->count -= threshold->count * diff /
530 		    threshold->seconds;
531 	threshold->count += PF_THRESHOLD_MULT;
532 	threshold->last = t;
533 }
534 
535 static int
536 pf_check_threshold(struct pf_threshold *threshold)
537 {
538 	return (threshold->count > threshold->limit);
539 }
540 
541 static int
542 pf_src_connlimit(struct pf_state **state)
543 {
544 	struct pf_overload_entry *pfoe;
545 	int bad = 0;
546 
547 	PF_STATE_LOCK_ASSERT(*state);
548 
549 	(*state)->src_node->conn++;
550 	(*state)->src.tcp_est = 1;
551 	pf_add_threshold(&(*state)->src_node->conn_rate);
552 
553 	if ((*state)->rule.ptr->max_src_conn &&
554 	    (*state)->rule.ptr->max_src_conn <
555 	    (*state)->src_node->conn) {
556 		counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONN], 1);
557 		bad++;
558 	}
559 
560 	if ((*state)->rule.ptr->max_src_conn_rate.limit &&
561 	    pf_check_threshold(&(*state)->src_node->conn_rate)) {
562 		counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONNRATE], 1);
563 		bad++;
564 	}
565 
566 	if (!bad)
567 		return (0);
568 
569 	/* Kill this state. */
570 	(*state)->timeout = PFTM_PURGE;
571 	(*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
572 
573 	if ((*state)->rule.ptr->overload_tbl == NULL)
574 		return (1);
575 
576 	/* Schedule overloading and flushing task. */
577 	pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT);
578 	if (pfoe == NULL)
579 		return (1);	/* too bad :( */
580 
581 	bcopy(&(*state)->src_node->addr, &pfoe->addr, sizeof(pfoe->addr));
582 	pfoe->af = (*state)->key[PF_SK_WIRE]->af;
583 	pfoe->rule = (*state)->rule.ptr;
584 	pfoe->dir = (*state)->direction;
585 	PF_OVERLOADQ_LOCK();
586 	SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next);
587 	PF_OVERLOADQ_UNLOCK();
588 	taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask);
589 
590 	return (1);
591 }
592 
593 static void
594 pf_overload_task(void *v, int pending)
595 {
596 	struct pf_overload_head queue;
597 	struct pfr_addr p;
598 	struct pf_overload_entry *pfoe, *pfoe1;
599 	uint32_t killed = 0;
600 
601 	CURVNET_SET((struct vnet *)v);
602 
603 	PF_OVERLOADQ_LOCK();
604 	queue = V_pf_overloadqueue;
605 	SLIST_INIT(&V_pf_overloadqueue);
606 	PF_OVERLOADQ_UNLOCK();
607 
608 	bzero(&p, sizeof(p));
609 	SLIST_FOREACH(pfoe, &queue, next) {
610 		counter_u64_add(V_pf_status.lcounters[LCNT_OVERLOAD_TABLE], 1);
611 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
612 			printf("%s: blocking address ", __func__);
613 			pf_print_host(&pfoe->addr, 0, pfoe->af);
614 			printf("\n");
615 		}
616 
617 		p.pfra_af = pfoe->af;
618 		switch (pfoe->af) {
619 #ifdef INET
620 		case AF_INET:
621 			p.pfra_net = 32;
622 			p.pfra_ip4addr = pfoe->addr.v4;
623 			break;
624 #endif
625 #ifdef INET6
626 		case AF_INET6:
627 			p.pfra_net = 128;
628 			p.pfra_ip6addr = pfoe->addr.v6;
629 			break;
630 #endif
631 		}
632 
633 		PF_RULES_WLOCK();
634 		pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second);
635 		PF_RULES_WUNLOCK();
636 	}
637 
638 	/*
639 	 * Remove those entries, that don't need flushing.
640 	 */
641 	SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1)
642 		if (pfoe->rule->flush == 0) {
643 			SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next);
644 			free(pfoe, M_PFTEMP);
645 		} else
646 			counter_u64_add(
647 			    V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH], 1);
648 
649 	/* If nothing to flush, return. */
650 	if (SLIST_EMPTY(&queue)) {
651 		CURVNET_RESTORE();
652 		return;
653 	}
654 
655 	for (int i = 0; i <= pf_hashmask; i++) {
656 		struct pf_idhash *ih = &V_pf_idhash[i];
657 		struct pf_state_key *sk;
658 		struct pf_state *s;
659 
660 		PF_HASHROW_LOCK(ih);
661 		LIST_FOREACH(s, &ih->states, entry) {
662 		    sk = s->key[PF_SK_WIRE];
663 		    SLIST_FOREACH(pfoe, &queue, next)
664 			if (sk->af == pfoe->af &&
665 			    ((pfoe->rule->flush & PF_FLUSH_GLOBAL) ||
666 			    pfoe->rule == s->rule.ptr) &&
667 			    ((pfoe->dir == PF_OUT &&
668 			    PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) ||
669 			    (pfoe->dir == PF_IN &&
670 			    PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) {
671 				s->timeout = PFTM_PURGE;
672 				s->src.state = s->dst.state = TCPS_CLOSED;
673 				killed++;
674 			}
675 		}
676 		PF_HASHROW_UNLOCK(ih);
677 	}
678 	SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1)
679 		free(pfoe, M_PFTEMP);
680 	if (V_pf_status.debug >= PF_DEBUG_MISC)
681 		printf("%s: %u states killed", __func__, killed);
682 
683 	CURVNET_RESTORE();
684 }
685 
686 /*
687  * Can return locked on failure, so that we can consistently
688  * allocate and insert a new one.
689  */
690 struct pf_ksrc_node *
691 pf_find_src_node(struct pf_addr *src, struct pf_krule *rule, sa_family_t af,
692 	int returnlocked)
693 {
694 	struct pf_srchash *sh;
695 	struct pf_ksrc_node *n;
696 
697 	counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_SEARCH], 1);
698 
699 	sh = &V_pf_srchash[pf_hashsrc(src, af)];
700 	PF_HASHROW_LOCK(sh);
701 	LIST_FOREACH(n, &sh->nodes, entry)
702 		if (n->rule.ptr == rule && n->af == af &&
703 		    ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) ||
704 		    (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0)))
705 			break;
706 	if (n != NULL) {
707 		n->states++;
708 		PF_HASHROW_UNLOCK(sh);
709 	} else if (returnlocked == 0)
710 		PF_HASHROW_UNLOCK(sh);
711 
712 	return (n);
713 }
714 
715 static void
716 pf_free_src_node(struct pf_ksrc_node *sn)
717 {
718 
719 	for (int i = 0; i < 2; i++) {
720 		counter_u64_free(sn->bytes[i]);
721 		counter_u64_free(sn->packets[i]);
722 	}
723 	uma_zfree(V_pf_sources_z, sn);
724 }
725 
726 static int
727 pf_insert_src_node(struct pf_ksrc_node **sn, struct pf_krule *rule,
728     struct pf_addr *src, sa_family_t af)
729 {
730 
731 	KASSERT((rule->rule_flag & PFRULE_SRCTRACK ||
732 	    rule->rpool.opts & PF_POOL_STICKYADDR),
733 	    ("%s for non-tracking rule %p", __func__, rule));
734 
735 	if (*sn == NULL)
736 		*sn = pf_find_src_node(src, rule, af, 1);
737 
738 	if (*sn == NULL) {
739 		struct pf_srchash *sh = &V_pf_srchash[pf_hashsrc(src, af)];
740 
741 		PF_HASHROW_ASSERT(sh);
742 
743 		if (!rule->max_src_nodes ||
744 		    counter_u64_fetch(rule->src_nodes) < rule->max_src_nodes)
745 			(*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO);
746 		else
747 			counter_u64_add(V_pf_status.lcounters[LCNT_SRCNODES],
748 			    1);
749 		if ((*sn) == NULL) {
750 			PF_HASHROW_UNLOCK(sh);
751 			return (-1);
752 		}
753 
754 		for (int i = 0; i < 2; i++) {
755 			(*sn)->bytes[i] = counter_u64_alloc(M_NOWAIT);
756 			(*sn)->packets[i] = counter_u64_alloc(M_NOWAIT);
757 
758 			if ((*sn)->bytes[i] == NULL || (*sn)->packets[i] == NULL) {
759 				pf_free_src_node(*sn);
760 				PF_HASHROW_UNLOCK(sh);
761 				return (-1);
762 			}
763 		}
764 
765 		pf_init_threshold(&(*sn)->conn_rate,
766 		    rule->max_src_conn_rate.limit,
767 		    rule->max_src_conn_rate.seconds);
768 
769 		(*sn)->af = af;
770 		(*sn)->rule.ptr = rule;
771 		PF_ACPY(&(*sn)->addr, src, af);
772 		LIST_INSERT_HEAD(&sh->nodes, *sn, entry);
773 		(*sn)->creation = time_uptime;
774 		(*sn)->ruletype = rule->action;
775 		(*sn)->states = 1;
776 		if ((*sn)->rule.ptr != NULL)
777 			counter_u64_add((*sn)->rule.ptr->src_nodes, 1);
778 		PF_HASHROW_UNLOCK(sh);
779 		counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_INSERT], 1);
780 	} else {
781 		if (rule->max_src_states &&
782 		    (*sn)->states >= rule->max_src_states) {
783 			counter_u64_add(V_pf_status.lcounters[LCNT_SRCSTATES],
784 			    1);
785 			return (-1);
786 		}
787 	}
788 	return (0);
789 }
790 
791 void
792 pf_unlink_src_node(struct pf_ksrc_node *src)
793 {
794 
795 	PF_HASHROW_ASSERT(&V_pf_srchash[pf_hashsrc(&src->addr, src->af)]);
796 	LIST_REMOVE(src, entry);
797 	if (src->rule.ptr)
798 		counter_u64_add(src->rule.ptr->src_nodes, -1);
799 }
800 
801 u_int
802 pf_free_src_nodes(struct pf_ksrc_node_list *head)
803 {
804 	struct pf_ksrc_node *sn, *tmp;
805 	u_int count = 0;
806 
807 	LIST_FOREACH_SAFE(sn, head, entry, tmp) {
808 		pf_free_src_node(sn);
809 		count++;
810 	}
811 
812 	counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], count);
813 
814 	return (count);
815 }
816 
817 void
818 pf_mtag_initialize()
819 {
820 
821 	pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) +
822 	    sizeof(struct pf_mtag), NULL, NULL, pf_mtag_uminit, NULL,
823 	    UMA_ALIGN_PTR, 0);
824 }
825 
826 /* Per-vnet data storage structures initialization. */
827 void
828 pf_initialize()
829 {
830 	struct pf_keyhash	*kh;
831 	struct pf_idhash	*ih;
832 	struct pf_srchash	*sh;
833 	u_int i;
834 
835 	if (pf_hashsize == 0 || !powerof2(pf_hashsize))
836 		pf_hashsize = PF_HASHSIZ;
837 	if (pf_srchashsize == 0 || !powerof2(pf_srchashsize))
838 		pf_srchashsize = PF_SRCHASHSIZ;
839 
840 	V_pf_hashseed = arc4random();
841 
842 	/* States and state keys storage. */
843 	V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_state),
844 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
845 	V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z;
846 	uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT);
847 	uma_zone_set_warning(V_pf_state_z, "PF states limit reached");
848 
849 	V_pf_state_key_z = uma_zcreate("pf state keys",
850 	    sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL,
851 	    UMA_ALIGN_PTR, 0);
852 
853 	V_pf_keyhash = mallocarray(pf_hashsize, sizeof(struct pf_keyhash),
854 	    M_PFHASH, M_NOWAIT | M_ZERO);
855 	V_pf_idhash = mallocarray(pf_hashsize, sizeof(struct pf_idhash),
856 	    M_PFHASH, M_NOWAIT | M_ZERO);
857 	if (V_pf_keyhash == NULL || V_pf_idhash == NULL) {
858 		printf("pf: Unable to allocate memory for "
859 		    "state_hashsize %lu.\n", pf_hashsize);
860 
861 		free(V_pf_keyhash, M_PFHASH);
862 		free(V_pf_idhash, M_PFHASH);
863 
864 		pf_hashsize = PF_HASHSIZ;
865 		V_pf_keyhash = mallocarray(pf_hashsize,
866 		    sizeof(struct pf_keyhash), M_PFHASH, M_WAITOK | M_ZERO);
867 		V_pf_idhash = mallocarray(pf_hashsize,
868 		    sizeof(struct pf_idhash), M_PFHASH, M_WAITOK | M_ZERO);
869 	}
870 
871 	pf_hashmask = pf_hashsize - 1;
872 	for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= pf_hashmask;
873 	    i++, kh++, ih++) {
874 		mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK);
875 		mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF);
876 	}
877 
878 	/* Source nodes. */
879 	V_pf_sources_z = uma_zcreate("pf source nodes",
880 	    sizeof(struct pf_ksrc_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
881 	    0);
882 	V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z;
883 	uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT);
884 	uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached");
885 
886 	V_pf_srchash = mallocarray(pf_srchashsize,
887 	    sizeof(struct pf_srchash), M_PFHASH, M_NOWAIT | M_ZERO);
888 	if (V_pf_srchash == NULL) {
889 		printf("pf: Unable to allocate memory for "
890 		    "source_hashsize %lu.\n", pf_srchashsize);
891 
892 		pf_srchashsize = PF_SRCHASHSIZ;
893 		V_pf_srchash = mallocarray(pf_srchashsize,
894 		    sizeof(struct pf_srchash), M_PFHASH, M_WAITOK | M_ZERO);
895 	}
896 
897 	pf_srchashmask = pf_srchashsize - 1;
898 	for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; i++, sh++)
899 		mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF);
900 
901 	/* ALTQ */
902 	TAILQ_INIT(&V_pf_altqs[0]);
903 	TAILQ_INIT(&V_pf_altqs[1]);
904 	TAILQ_INIT(&V_pf_altqs[2]);
905 	TAILQ_INIT(&V_pf_altqs[3]);
906 	TAILQ_INIT(&V_pf_pabuf);
907 	V_pf_altqs_active = &V_pf_altqs[0];
908 	V_pf_altq_ifs_active = &V_pf_altqs[1];
909 	V_pf_altqs_inactive = &V_pf_altqs[2];
910 	V_pf_altq_ifs_inactive = &V_pf_altqs[3];
911 
912 	/* Send & overload+flush queues. */
913 	STAILQ_INIT(&V_pf_sendqueue);
914 	SLIST_INIT(&V_pf_overloadqueue);
915 	TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, curvnet);
916 
917 	/* Unlinked, but may be referenced rules. */
918 	TAILQ_INIT(&V_pf_unlinked_rules);
919 }
920 
921 void
922 pf_mtag_cleanup()
923 {
924 
925 	uma_zdestroy(pf_mtag_z);
926 }
927 
928 void
929 pf_cleanup()
930 {
931 	struct pf_keyhash	*kh;
932 	struct pf_idhash	*ih;
933 	struct pf_srchash	*sh;
934 	struct pf_send_entry	*pfse, *next;
935 	u_int i;
936 
937 	for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= pf_hashmask;
938 	    i++, kh++, ih++) {
939 		KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty",
940 		    __func__));
941 		KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty",
942 		    __func__));
943 		mtx_destroy(&kh->lock);
944 		mtx_destroy(&ih->lock);
945 	}
946 	free(V_pf_keyhash, M_PFHASH);
947 	free(V_pf_idhash, M_PFHASH);
948 
949 	for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; i++, sh++) {
950 		KASSERT(LIST_EMPTY(&sh->nodes),
951 		    ("%s: source node hash not empty", __func__));
952 		mtx_destroy(&sh->lock);
953 	}
954 	free(V_pf_srchash, M_PFHASH);
955 
956 	STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) {
957 		m_freem(pfse->pfse_m);
958 		free(pfse, M_PFTEMP);
959 	}
960 
961 	uma_zdestroy(V_pf_sources_z);
962 	uma_zdestroy(V_pf_state_z);
963 	uma_zdestroy(V_pf_state_key_z);
964 }
965 
966 static int
967 pf_mtag_uminit(void *mem, int size, int how)
968 {
969 	struct m_tag *t;
970 
971 	t = (struct m_tag *)mem;
972 	t->m_tag_cookie = MTAG_ABI_COMPAT;
973 	t->m_tag_id = PACKET_TAG_PF;
974 	t->m_tag_len = sizeof(struct pf_mtag);
975 	t->m_tag_free = pf_mtag_free;
976 
977 	return (0);
978 }
979 
980 static void
981 pf_mtag_free(struct m_tag *t)
982 {
983 
984 	uma_zfree(pf_mtag_z, t);
985 }
986 
987 struct pf_mtag *
988 pf_get_mtag(struct mbuf *m)
989 {
990 	struct m_tag *mtag;
991 
992 	if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL)
993 		return ((struct pf_mtag *)(mtag + 1));
994 
995 	mtag = uma_zalloc(pf_mtag_z, M_NOWAIT);
996 	if (mtag == NULL)
997 		return (NULL);
998 	bzero(mtag + 1, sizeof(struct pf_mtag));
999 	m_tag_prepend(m, mtag);
1000 
1001 	return ((struct pf_mtag *)(mtag + 1));
1002 }
1003 
1004 static int
1005 pf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks,
1006     struct pf_state *s)
1007 {
1008 	struct pf_keyhash	*khs, *khw, *kh;
1009 	struct pf_state_key	*sk, *cur;
1010 	struct pf_state		*si, *olds = NULL;
1011 	int idx;
1012 
1013 	KASSERT(s->refs == 0, ("%s: state not pristine", __func__));
1014 	KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__));
1015 	KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__));
1016 
1017 	/*
1018 	 * We need to lock hash slots of both keys. To avoid deadlock
1019 	 * we always lock the slot with lower address first. Unlock order
1020 	 * isn't important.
1021 	 *
1022 	 * We also need to lock ID hash slot before dropping key
1023 	 * locks. On success we return with ID hash slot locked.
1024 	 */
1025 
1026 	if (skw == sks) {
1027 		khs = khw = &V_pf_keyhash[pf_hashkey(skw)];
1028 		PF_HASHROW_LOCK(khs);
1029 	} else {
1030 		khs = &V_pf_keyhash[pf_hashkey(sks)];
1031 		khw = &V_pf_keyhash[pf_hashkey(skw)];
1032 		if (khs == khw) {
1033 			PF_HASHROW_LOCK(khs);
1034 		} else if (khs < khw) {
1035 			PF_HASHROW_LOCK(khs);
1036 			PF_HASHROW_LOCK(khw);
1037 		} else {
1038 			PF_HASHROW_LOCK(khw);
1039 			PF_HASHROW_LOCK(khs);
1040 		}
1041 	}
1042 
1043 #define	KEYS_UNLOCK()	do {			\
1044 	if (khs != khw) {			\
1045 		PF_HASHROW_UNLOCK(khs);		\
1046 		PF_HASHROW_UNLOCK(khw);		\
1047 	} else					\
1048 		PF_HASHROW_UNLOCK(khs);		\
1049 } while (0)
1050 
1051 	/*
1052 	 * First run: start with wire key.
1053 	 */
1054 	sk = skw;
1055 	kh = khw;
1056 	idx = PF_SK_WIRE;
1057 
1058 keyattach:
1059 	LIST_FOREACH(cur, &kh->keys, entry)
1060 		if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0)
1061 			break;
1062 
1063 	if (cur != NULL) {
1064 		/* Key exists. Check for same kif, if none, add to key. */
1065 		TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) {
1066 			struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)];
1067 
1068 			PF_HASHROW_LOCK(ih);
1069 			if (si->kif == s->kif &&
1070 			    si->direction == s->direction) {
1071 				if (sk->proto == IPPROTO_TCP &&
1072 				    si->src.state >= TCPS_FIN_WAIT_2 &&
1073 				    si->dst.state >= TCPS_FIN_WAIT_2) {
1074 					/*
1075 					 * New state matches an old >FIN_WAIT_2
1076 					 * state. We can't drop key hash locks,
1077 					 * thus we can't unlink it properly.
1078 					 *
1079 					 * As a workaround we drop it into
1080 					 * TCPS_CLOSED state, schedule purge
1081 					 * ASAP and push it into the very end
1082 					 * of the slot TAILQ, so that it won't
1083 					 * conflict with our new state.
1084 					 */
1085 					si->src.state = si->dst.state =
1086 					    TCPS_CLOSED;
1087 					si->timeout = PFTM_PURGE;
1088 					olds = si;
1089 				} else {
1090 					if (V_pf_status.debug >= PF_DEBUG_MISC) {
1091 						printf("pf: %s key attach "
1092 						    "failed on %s: ",
1093 						    (idx == PF_SK_WIRE) ?
1094 						    "wire" : "stack",
1095 						    s->kif->pfik_name);
1096 						pf_print_state_parts(s,
1097 						    (idx == PF_SK_WIRE) ?
1098 						    sk : NULL,
1099 						    (idx == PF_SK_STACK) ?
1100 						    sk : NULL);
1101 						printf(", existing: ");
1102 						pf_print_state_parts(si,
1103 						    (idx == PF_SK_WIRE) ?
1104 						    sk : NULL,
1105 						    (idx == PF_SK_STACK) ?
1106 						    sk : NULL);
1107 						printf("\n");
1108 					}
1109 					PF_HASHROW_UNLOCK(ih);
1110 					KEYS_UNLOCK();
1111 					uma_zfree(V_pf_state_key_z, sk);
1112 					if (idx == PF_SK_STACK)
1113 						pf_detach_state(s);
1114 					return (EEXIST); /* collision! */
1115 				}
1116 			}
1117 			PF_HASHROW_UNLOCK(ih);
1118 		}
1119 		uma_zfree(V_pf_state_key_z, sk);
1120 		s->key[idx] = cur;
1121 	} else {
1122 		LIST_INSERT_HEAD(&kh->keys, sk, entry);
1123 		s->key[idx] = sk;
1124 	}
1125 
1126 stateattach:
1127 	/* List is sorted, if-bound states before floating. */
1128 	if (s->kif == V_pfi_all)
1129 		TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]);
1130 	else
1131 		TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]);
1132 
1133 	if (olds) {
1134 		TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]);
1135 		TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds,
1136 		    key_list[idx]);
1137 		olds = NULL;
1138 	}
1139 
1140 	/*
1141 	 * Attach done. See how should we (or should not?)
1142 	 * attach a second key.
1143 	 */
1144 	if (sks == skw) {
1145 		s->key[PF_SK_STACK] = s->key[PF_SK_WIRE];
1146 		idx = PF_SK_STACK;
1147 		sks = NULL;
1148 		goto stateattach;
1149 	} else if (sks != NULL) {
1150 		/*
1151 		 * Continue attaching with stack key.
1152 		 */
1153 		sk = sks;
1154 		kh = khs;
1155 		idx = PF_SK_STACK;
1156 		sks = NULL;
1157 		goto keyattach;
1158 	}
1159 
1160 	PF_STATE_LOCK(s);
1161 	KEYS_UNLOCK();
1162 
1163 	KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL,
1164 	    ("%s failure", __func__));
1165 
1166 	return (0);
1167 #undef	KEYS_UNLOCK
1168 }
1169 
1170 static void
1171 pf_detach_state(struct pf_state *s)
1172 {
1173 	struct pf_state_key *sks = s->key[PF_SK_STACK];
1174 	struct pf_keyhash *kh;
1175 
1176 	if (sks != NULL) {
1177 		kh = &V_pf_keyhash[pf_hashkey(sks)];
1178 		PF_HASHROW_LOCK(kh);
1179 		if (s->key[PF_SK_STACK] != NULL)
1180 			pf_state_key_detach(s, PF_SK_STACK);
1181 		/*
1182 		 * If both point to same key, then we are done.
1183 		 */
1184 		if (sks == s->key[PF_SK_WIRE]) {
1185 			pf_state_key_detach(s, PF_SK_WIRE);
1186 			PF_HASHROW_UNLOCK(kh);
1187 			return;
1188 		}
1189 		PF_HASHROW_UNLOCK(kh);
1190 	}
1191 
1192 	if (s->key[PF_SK_WIRE] != NULL) {
1193 		kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])];
1194 		PF_HASHROW_LOCK(kh);
1195 		if (s->key[PF_SK_WIRE] != NULL)
1196 			pf_state_key_detach(s, PF_SK_WIRE);
1197 		PF_HASHROW_UNLOCK(kh);
1198 	}
1199 }
1200 
1201 static void
1202 pf_state_key_detach(struct pf_state *s, int idx)
1203 {
1204 	struct pf_state_key *sk = s->key[idx];
1205 #ifdef INVARIANTS
1206 	struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)];
1207 
1208 	PF_HASHROW_ASSERT(kh);
1209 #endif
1210 	TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]);
1211 	s->key[idx] = NULL;
1212 
1213 	if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) {
1214 		LIST_REMOVE(sk, entry);
1215 		uma_zfree(V_pf_state_key_z, sk);
1216 	}
1217 }
1218 
1219 static int
1220 pf_state_key_ctor(void *mem, int size, void *arg, int flags)
1221 {
1222 	struct pf_state_key *sk = mem;
1223 
1224 	bzero(sk, sizeof(struct pf_state_key_cmp));
1225 	TAILQ_INIT(&sk->states[PF_SK_WIRE]);
1226 	TAILQ_INIT(&sk->states[PF_SK_STACK]);
1227 
1228 	return (0);
1229 }
1230 
1231 struct pf_state_key *
1232 pf_state_key_setup(struct pf_pdesc *pd, struct pf_addr *saddr,
1233 	struct pf_addr *daddr, u_int16_t sport, u_int16_t dport)
1234 {
1235 	struct pf_state_key *sk;
1236 
1237 	sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT);
1238 	if (sk == NULL)
1239 		return (NULL);
1240 
1241 	PF_ACPY(&sk->addr[pd->sidx], saddr, pd->af);
1242 	PF_ACPY(&sk->addr[pd->didx], daddr, pd->af);
1243 	sk->port[pd->sidx] = sport;
1244 	sk->port[pd->didx] = dport;
1245 	sk->proto = pd->proto;
1246 	sk->af = pd->af;
1247 
1248 	return (sk);
1249 }
1250 
1251 struct pf_state_key *
1252 pf_state_key_clone(struct pf_state_key *orig)
1253 {
1254 	struct pf_state_key *sk;
1255 
1256 	sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT);
1257 	if (sk == NULL)
1258 		return (NULL);
1259 
1260 	bcopy(orig, sk, sizeof(struct pf_state_key_cmp));
1261 
1262 	return (sk);
1263 }
1264 
1265 int
1266 pf_state_insert(struct pfi_kkif *kif, struct pf_state_key *skw,
1267     struct pf_state_key *sks, struct pf_state *s)
1268 {
1269 	struct pf_idhash *ih;
1270 	struct pf_state *cur;
1271 	int error;
1272 
1273 	KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]),
1274 	    ("%s: sks not pristine", __func__));
1275 	KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]),
1276 	    ("%s: skw not pristine", __func__));
1277 	KASSERT(s->refs == 0, ("%s: state not pristine", __func__));
1278 
1279 	s->kif = kif;
1280 
1281 	if (s->id == 0 && s->creatorid == 0) {
1282 		/* XXX: should be atomic, but probability of collision low */
1283 		if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID)
1284 			V_pf_stateid[curcpu] = 1;
1285 		s->id |= (uint64_t )curcpu << PFID_CPUSHIFT;
1286 		s->id = htobe64(s->id);
1287 		s->creatorid = V_pf_status.hostid;
1288 	}
1289 
1290 	/* Returns with ID locked on success. */
1291 	if ((error = pf_state_key_attach(skw, sks, s)) != 0)
1292 		return (error);
1293 
1294 	ih = &V_pf_idhash[PF_IDHASH(s)];
1295 	PF_HASHROW_ASSERT(ih);
1296 	LIST_FOREACH(cur, &ih->states, entry)
1297 		if (cur->id == s->id && cur->creatorid == s->creatorid)
1298 			break;
1299 
1300 	if (cur != NULL) {
1301 		PF_HASHROW_UNLOCK(ih);
1302 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
1303 			printf("pf: state ID collision: "
1304 			    "id: %016llx creatorid: %08x\n",
1305 			    (unsigned long long)be64toh(s->id),
1306 			    ntohl(s->creatorid));
1307 		}
1308 		pf_detach_state(s);
1309 		return (EEXIST);
1310 	}
1311 	LIST_INSERT_HEAD(&ih->states, s, entry);
1312 	/* One for keys, one for ID hash. */
1313 	refcount_init(&s->refs, 2);
1314 
1315 	counter_u64_add(V_pf_status.fcounters[FCNT_STATE_INSERT], 1);
1316 	if (V_pfsync_insert_state_ptr != NULL)
1317 		V_pfsync_insert_state_ptr(s);
1318 
1319 	/* Returns locked. */
1320 	return (0);
1321 }
1322 
1323 /*
1324  * Find state by ID: returns with locked row on success.
1325  */
1326 struct pf_state *
1327 pf_find_state_byid(uint64_t id, uint32_t creatorid)
1328 {
1329 	struct pf_idhash *ih;
1330 	struct pf_state *s;
1331 
1332 	counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1);
1333 
1334 	ih = &V_pf_idhash[(be64toh(id) % (pf_hashmask + 1))];
1335 
1336 	PF_HASHROW_LOCK(ih);
1337 	LIST_FOREACH(s, &ih->states, entry)
1338 		if (s->id == id && s->creatorid == creatorid)
1339 			break;
1340 
1341 	if (s == NULL)
1342 		PF_HASHROW_UNLOCK(ih);
1343 
1344 	return (s);
1345 }
1346 
1347 /*
1348  * Find state by key.
1349  * Returns with ID hash slot locked on success.
1350  */
1351 static struct pf_state *
1352 pf_find_state(struct pfi_kkif *kif, struct pf_state_key_cmp *key, u_int dir)
1353 {
1354 	struct pf_keyhash	*kh;
1355 	struct pf_state_key	*sk;
1356 	struct pf_state		*s;
1357 	int idx;
1358 
1359 	counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1);
1360 
1361 	kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)];
1362 
1363 	PF_HASHROW_LOCK(kh);
1364 	LIST_FOREACH(sk, &kh->keys, entry)
1365 		if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0)
1366 			break;
1367 	if (sk == NULL) {
1368 		PF_HASHROW_UNLOCK(kh);
1369 		return (NULL);
1370 	}
1371 
1372 	idx = (dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK);
1373 
1374 	/* List is sorted, if-bound states before floating ones. */
1375 	TAILQ_FOREACH(s, &sk->states[idx], key_list[idx])
1376 		if (s->kif == V_pfi_all || s->kif == kif) {
1377 			PF_STATE_LOCK(s);
1378 			PF_HASHROW_UNLOCK(kh);
1379 			if (s->timeout >= PFTM_MAX) {
1380 				/*
1381 				 * State is either being processed by
1382 				 * pf_unlink_state() in an other thread, or
1383 				 * is scheduled for immediate expiry.
1384 				 */
1385 				PF_STATE_UNLOCK(s);
1386 				return (NULL);
1387 			}
1388 			return (s);
1389 		}
1390 	PF_HASHROW_UNLOCK(kh);
1391 
1392 	return (NULL);
1393 }
1394 
1395 struct pf_state *
1396 pf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more)
1397 {
1398 	struct pf_keyhash	*kh;
1399 	struct pf_state_key	*sk;
1400 	struct pf_state		*s, *ret = NULL;
1401 	int			 idx, inout = 0;
1402 
1403 	counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1);
1404 
1405 	kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)];
1406 
1407 	PF_HASHROW_LOCK(kh);
1408 	LIST_FOREACH(sk, &kh->keys, entry)
1409 		if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0)
1410 			break;
1411 	if (sk == NULL) {
1412 		PF_HASHROW_UNLOCK(kh);
1413 		return (NULL);
1414 	}
1415 	switch (dir) {
1416 	case PF_IN:
1417 		idx = PF_SK_WIRE;
1418 		break;
1419 	case PF_OUT:
1420 		idx = PF_SK_STACK;
1421 		break;
1422 	case PF_INOUT:
1423 		idx = PF_SK_WIRE;
1424 		inout = 1;
1425 		break;
1426 	default:
1427 		panic("%s: dir %u", __func__, dir);
1428 	}
1429 second_run:
1430 	TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) {
1431 		if (more == NULL) {
1432 			PF_HASHROW_UNLOCK(kh);
1433 			return (s);
1434 		}
1435 
1436 		if (ret)
1437 			(*more)++;
1438 		else
1439 			ret = s;
1440 	}
1441 	if (inout == 1) {
1442 		inout = 0;
1443 		idx = PF_SK_STACK;
1444 		goto second_run;
1445 	}
1446 	PF_HASHROW_UNLOCK(kh);
1447 
1448 	return (ret);
1449 }
1450 
1451 /* END state table stuff */
1452 
1453 static void
1454 pf_send(struct pf_send_entry *pfse)
1455 {
1456 
1457 	PF_SENDQ_LOCK();
1458 	STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next);
1459 	PF_SENDQ_UNLOCK();
1460 	swi_sched(V_pf_swi_cookie, 0);
1461 }
1462 
1463 void
1464 pf_intr(void *v)
1465 {
1466 	struct epoch_tracker et;
1467 	struct pf_send_head queue;
1468 	struct pf_send_entry *pfse, *next;
1469 
1470 	CURVNET_SET((struct vnet *)v);
1471 
1472 	PF_SENDQ_LOCK();
1473 	queue = V_pf_sendqueue;
1474 	STAILQ_INIT(&V_pf_sendqueue);
1475 	PF_SENDQ_UNLOCK();
1476 
1477 	NET_EPOCH_ENTER(et);
1478 
1479 	STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) {
1480 		switch (pfse->pfse_type) {
1481 #ifdef INET
1482 		case PFSE_IP:
1483 			ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL);
1484 			break;
1485 		case PFSE_ICMP:
1486 			icmp_error(pfse->pfse_m, pfse->icmpopts.type,
1487 			    pfse->icmpopts.code, 0, pfse->icmpopts.mtu);
1488 			break;
1489 #endif /* INET */
1490 #ifdef INET6
1491 		case PFSE_IP6:
1492 			ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL,
1493 			    NULL);
1494 			break;
1495 		case PFSE_ICMP6:
1496 			icmp6_error(pfse->pfse_m, pfse->icmpopts.type,
1497 			    pfse->icmpopts.code, pfse->icmpopts.mtu);
1498 			break;
1499 #endif /* INET6 */
1500 		default:
1501 			panic("%s: unknown type", __func__);
1502 		}
1503 		free(pfse, M_PFTEMP);
1504 	}
1505 	NET_EPOCH_EXIT(et);
1506 	CURVNET_RESTORE();
1507 }
1508 
1509 void
1510 pf_purge_thread(void *unused __unused)
1511 {
1512 	VNET_ITERATOR_DECL(vnet_iter);
1513 
1514 	sx_xlock(&pf_end_lock);
1515 	while (pf_end_threads == 0) {
1516 		sx_sleep(pf_purge_thread, &pf_end_lock, 0, "pftm", hz / 10);
1517 
1518 		VNET_LIST_RLOCK();
1519 		VNET_FOREACH(vnet_iter) {
1520 			CURVNET_SET(vnet_iter);
1521 
1522 			/* Wait until V_pf_default_rule is initialized. */
1523 			if (V_pf_vnet_active == 0) {
1524 				CURVNET_RESTORE();
1525 				continue;
1526 			}
1527 
1528 			/*
1529 			 *  Process 1/interval fraction of the state
1530 			 * table every run.
1531 			 */
1532 			V_pf_purge_idx =
1533 			    pf_purge_expired_states(V_pf_purge_idx, pf_hashmask /
1534 			    (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10));
1535 
1536 			/*
1537 			 * Purge other expired types every
1538 			 * PFTM_INTERVAL seconds.
1539 			 */
1540 			if (V_pf_purge_idx == 0) {
1541 				/*
1542 				 * Order is important:
1543 				 * - states and src nodes reference rules
1544 				 * - states and rules reference kifs
1545 				 */
1546 				pf_purge_expired_fragments();
1547 				pf_purge_expired_src_nodes();
1548 				pf_purge_unlinked_rules();
1549 				pfi_kkif_purge();
1550 			}
1551 			CURVNET_RESTORE();
1552 		}
1553 		VNET_LIST_RUNLOCK();
1554 	}
1555 
1556 	pf_end_threads++;
1557 	sx_xunlock(&pf_end_lock);
1558 	kproc_exit(0);
1559 }
1560 
1561 void
1562 pf_unload_vnet_purge(void)
1563 {
1564 
1565 	/*
1566 	 * To cleanse up all kifs and rules we need
1567 	 * two runs: first one clears reference flags,
1568 	 * then pf_purge_expired_states() doesn't
1569 	 * raise them, and then second run frees.
1570 	 */
1571 	pf_purge_unlinked_rules();
1572 	pfi_kkif_purge();
1573 
1574 	/*
1575 	 * Now purge everything.
1576 	 */
1577 	pf_purge_expired_states(0, pf_hashmask);
1578 	pf_purge_fragments(UINT_MAX);
1579 	pf_purge_expired_src_nodes();
1580 
1581 	/*
1582 	 * Now all kifs & rules should be unreferenced,
1583 	 * thus should be successfully freed.
1584 	 */
1585 	pf_purge_unlinked_rules();
1586 	pfi_kkif_purge();
1587 }
1588 
1589 u_int32_t
1590 pf_state_expires(const struct pf_state *state)
1591 {
1592 	u_int32_t	timeout;
1593 	u_int32_t	start;
1594 	u_int32_t	end;
1595 	u_int32_t	states;
1596 
1597 	/* handle all PFTM_* > PFTM_MAX here */
1598 	if (state->timeout == PFTM_PURGE)
1599 		return (time_uptime);
1600 	KASSERT(state->timeout != PFTM_UNLINKED,
1601 	    ("pf_state_expires: timeout == PFTM_UNLINKED"));
1602 	KASSERT((state->timeout < PFTM_MAX),
1603 	    ("pf_state_expires: timeout > PFTM_MAX"));
1604 	timeout = state->rule.ptr->timeout[state->timeout];
1605 	if (!timeout)
1606 		timeout = V_pf_default_rule.timeout[state->timeout];
1607 	start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
1608 	if (start && state->rule.ptr != &V_pf_default_rule) {
1609 		end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
1610 		states = counter_u64_fetch(state->rule.ptr->states_cur);
1611 	} else {
1612 		start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START];
1613 		end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END];
1614 		states = V_pf_status.states;
1615 	}
1616 	if (end && states > start && start < end) {
1617 		if (states < end) {
1618 			timeout = (u_int64_t)timeout * (end - states) /
1619 			    (end - start);
1620 			return (state->expire + timeout);
1621 		}
1622 		else
1623 			return (time_uptime);
1624 	}
1625 	return (state->expire + timeout);
1626 }
1627 
1628 void
1629 pf_purge_expired_src_nodes()
1630 {
1631 	struct pf_ksrc_node_list	 freelist;
1632 	struct pf_srchash	*sh;
1633 	struct pf_ksrc_node	*cur, *next;
1634 	int i;
1635 
1636 	LIST_INIT(&freelist);
1637 	for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; i++, sh++) {
1638 	    PF_HASHROW_LOCK(sh);
1639 	    LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next)
1640 		if (cur->states == 0 && cur->expire <= time_uptime) {
1641 			pf_unlink_src_node(cur);
1642 			LIST_INSERT_HEAD(&freelist, cur, entry);
1643 		} else if (cur->rule.ptr != NULL)
1644 			cur->rule.ptr->rule_flag |= PFRULE_REFS;
1645 	    PF_HASHROW_UNLOCK(sh);
1646 	}
1647 
1648 	pf_free_src_nodes(&freelist);
1649 
1650 	V_pf_status.src_nodes = uma_zone_get_cur(V_pf_sources_z);
1651 }
1652 
1653 static void
1654 pf_src_tree_remove_state(struct pf_state *s)
1655 {
1656 	struct pf_ksrc_node *sn;
1657 	struct pf_srchash *sh;
1658 	uint32_t timeout;
1659 
1660 	timeout = s->rule.ptr->timeout[PFTM_SRC_NODE] ?
1661 	    s->rule.ptr->timeout[PFTM_SRC_NODE] :
1662 	    V_pf_default_rule.timeout[PFTM_SRC_NODE];
1663 
1664 	if (s->src_node != NULL) {
1665 		sn = s->src_node;
1666 		sh = &V_pf_srchash[pf_hashsrc(&sn->addr, sn->af)];
1667 	    	PF_HASHROW_LOCK(sh);
1668 		if (s->src.tcp_est)
1669 			--sn->conn;
1670 		if (--sn->states == 0)
1671 			sn->expire = time_uptime + timeout;
1672 	    	PF_HASHROW_UNLOCK(sh);
1673 	}
1674 	if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
1675 		sn = s->nat_src_node;
1676 		sh = &V_pf_srchash[pf_hashsrc(&sn->addr, sn->af)];
1677 	    	PF_HASHROW_LOCK(sh);
1678 		if (--sn->states == 0)
1679 			sn->expire = time_uptime + timeout;
1680 	    	PF_HASHROW_UNLOCK(sh);
1681 	}
1682 	s->src_node = s->nat_src_node = NULL;
1683 }
1684 
1685 /*
1686  * Unlink and potentilly free a state. Function may be
1687  * called with ID hash row locked, but always returns
1688  * unlocked, since it needs to go through key hash locking.
1689  */
1690 int
1691 pf_unlink_state(struct pf_state *s, u_int flags)
1692 {
1693 	struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)];
1694 
1695 	if ((flags & PF_ENTER_LOCKED) == 0)
1696 		PF_HASHROW_LOCK(ih);
1697 	else
1698 		PF_HASHROW_ASSERT(ih);
1699 
1700 	if (s->timeout == PFTM_UNLINKED) {
1701 		/*
1702 		 * State is being processed
1703 		 * by pf_unlink_state() in
1704 		 * an other thread.
1705 		 */
1706 		PF_HASHROW_UNLOCK(ih);
1707 		return (0);	/* XXXGL: undefined actually */
1708 	}
1709 
1710 	if (s->src.state == PF_TCPS_PROXY_DST) {
1711 		/* XXX wire key the right one? */
1712 		pf_send_tcp(NULL, s->rule.ptr, s->key[PF_SK_WIRE]->af,
1713 		    &s->key[PF_SK_WIRE]->addr[1],
1714 		    &s->key[PF_SK_WIRE]->addr[0],
1715 		    s->key[PF_SK_WIRE]->port[1],
1716 		    s->key[PF_SK_WIRE]->port[0],
1717 		    s->src.seqhi, s->src.seqlo + 1,
1718 		    TH_RST|TH_ACK, 0, 0, 0, 1, s->tag, NULL);
1719 	}
1720 
1721 	LIST_REMOVE(s, entry);
1722 	pf_src_tree_remove_state(s);
1723 
1724 	if (V_pfsync_delete_state_ptr != NULL)
1725 		V_pfsync_delete_state_ptr(s);
1726 
1727 	STATE_DEC_COUNTERS(s);
1728 
1729 	s->timeout = PFTM_UNLINKED;
1730 
1731 	PF_HASHROW_UNLOCK(ih);
1732 
1733 	pf_detach_state(s);
1734 	/* pf_state_insert() initialises refs to 2, so we can never release the
1735 	 * last reference here, only in pf_release_state(). */
1736 	(void)refcount_release(&s->refs);
1737 
1738 	return (pf_release_state(s));
1739 }
1740 
1741 void
1742 pf_free_state(struct pf_state *cur)
1743 {
1744 
1745 	KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur));
1746 	KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__,
1747 	    cur->timeout));
1748 
1749 	for (int i = 0; i < 2; i++) {
1750 		counter_u64_free(cur->bytes[i]);
1751 		counter_u64_free(cur->packets[i]);
1752 	}
1753 
1754 	pf_normalize_tcp_cleanup(cur);
1755 	uma_zfree(V_pf_state_z, cur);
1756 	counter_u64_add(V_pf_status.fcounters[FCNT_STATE_REMOVALS], 1);
1757 }
1758 
1759 /*
1760  * Called only from pf_purge_thread(), thus serialized.
1761  */
1762 static u_int
1763 pf_purge_expired_states(u_int i, int maxcheck)
1764 {
1765 	struct pf_idhash *ih;
1766 	struct pf_state *s;
1767 
1768 	V_pf_status.states = uma_zone_get_cur(V_pf_state_z);
1769 
1770 	/*
1771 	 * Go through hash and unlink states that expire now.
1772 	 */
1773 	while (maxcheck > 0) {
1774 		ih = &V_pf_idhash[i];
1775 
1776 		/* only take the lock if we expect to do work */
1777 		if (!LIST_EMPTY(&ih->states)) {
1778 relock:
1779 			PF_HASHROW_LOCK(ih);
1780 			LIST_FOREACH(s, &ih->states, entry) {
1781 				if (pf_state_expires(s) <= time_uptime) {
1782 					V_pf_status.states -=
1783 					    pf_unlink_state(s, PF_ENTER_LOCKED);
1784 					goto relock;
1785 				}
1786 				s->rule.ptr->rule_flag |= PFRULE_REFS;
1787 				if (s->nat_rule.ptr != NULL)
1788 					s->nat_rule.ptr->rule_flag |= PFRULE_REFS;
1789 				if (s->anchor.ptr != NULL)
1790 					s->anchor.ptr->rule_flag |= PFRULE_REFS;
1791 				s->kif->pfik_flags |= PFI_IFLAG_REFS;
1792 				if (s->rt_kif)
1793 					s->rt_kif->pfik_flags |= PFI_IFLAG_REFS;
1794 			}
1795 			PF_HASHROW_UNLOCK(ih);
1796 		}
1797 
1798 		/* Return when we hit end of hash. */
1799 		if (++i > pf_hashmask) {
1800 			V_pf_status.states = uma_zone_get_cur(V_pf_state_z);
1801 			return (0);
1802 		}
1803 
1804 		maxcheck--;
1805 	}
1806 
1807 	V_pf_status.states = uma_zone_get_cur(V_pf_state_z);
1808 
1809 	return (i);
1810 }
1811 
1812 static void
1813 pf_purge_unlinked_rules()
1814 {
1815 	struct pf_krulequeue tmpq;
1816 	struct pf_krule *r, *r1;
1817 
1818 	/*
1819 	 * If we have overloading task pending, then we'd
1820 	 * better skip purging this time. There is a tiny
1821 	 * probability that overloading task references
1822 	 * an already unlinked rule.
1823 	 */
1824 	PF_OVERLOADQ_LOCK();
1825 	if (!SLIST_EMPTY(&V_pf_overloadqueue)) {
1826 		PF_OVERLOADQ_UNLOCK();
1827 		return;
1828 	}
1829 	PF_OVERLOADQ_UNLOCK();
1830 
1831 	/*
1832 	 * Do naive mark-and-sweep garbage collecting of old rules.
1833 	 * Reference flag is raised by pf_purge_expired_states()
1834 	 * and pf_purge_expired_src_nodes().
1835 	 *
1836 	 * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK,
1837 	 * use a temporary queue.
1838 	 */
1839 	TAILQ_INIT(&tmpq);
1840 	PF_UNLNKDRULES_LOCK();
1841 	TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) {
1842 		if (!(r->rule_flag & PFRULE_REFS)) {
1843 			TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries);
1844 			TAILQ_INSERT_TAIL(&tmpq, r, entries);
1845 		} else
1846 			r->rule_flag &= ~PFRULE_REFS;
1847 	}
1848 	PF_UNLNKDRULES_UNLOCK();
1849 
1850 	if (!TAILQ_EMPTY(&tmpq)) {
1851 		PF_RULES_WLOCK();
1852 		TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) {
1853 			TAILQ_REMOVE(&tmpq, r, entries);
1854 			pf_free_rule(r);
1855 		}
1856 		PF_RULES_WUNLOCK();
1857 	}
1858 }
1859 
1860 void
1861 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
1862 {
1863 	switch (af) {
1864 #ifdef INET
1865 	case AF_INET: {
1866 		u_int32_t a = ntohl(addr->addr32[0]);
1867 		printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
1868 		    (a>>8)&255, a&255);
1869 		if (p) {
1870 			p = ntohs(p);
1871 			printf(":%u", p);
1872 		}
1873 		break;
1874 	}
1875 #endif /* INET */
1876 #ifdef INET6
1877 	case AF_INET6: {
1878 		u_int16_t b;
1879 		u_int8_t i, curstart, curend, maxstart, maxend;
1880 		curstart = curend = maxstart = maxend = 255;
1881 		for (i = 0; i < 8; i++) {
1882 			if (!addr->addr16[i]) {
1883 				if (curstart == 255)
1884 					curstart = i;
1885 				curend = i;
1886 			} else {
1887 				if ((curend - curstart) >
1888 				    (maxend - maxstart)) {
1889 					maxstart = curstart;
1890 					maxend = curend;
1891 				}
1892 				curstart = curend = 255;
1893 			}
1894 		}
1895 		if ((curend - curstart) >
1896 		    (maxend - maxstart)) {
1897 			maxstart = curstart;
1898 			maxend = curend;
1899 		}
1900 		for (i = 0; i < 8; i++) {
1901 			if (i >= maxstart && i <= maxend) {
1902 				if (i == 0)
1903 					printf(":");
1904 				if (i == maxend)
1905 					printf(":");
1906 			} else {
1907 				b = ntohs(addr->addr16[i]);
1908 				printf("%x", b);
1909 				if (i < 7)
1910 					printf(":");
1911 			}
1912 		}
1913 		if (p) {
1914 			p = ntohs(p);
1915 			printf("[%u]", p);
1916 		}
1917 		break;
1918 	}
1919 #endif /* INET6 */
1920 	}
1921 }
1922 
1923 void
1924 pf_print_state(struct pf_state *s)
1925 {
1926 	pf_print_state_parts(s, NULL, NULL);
1927 }
1928 
1929 static void
1930 pf_print_state_parts(struct pf_state *s,
1931     struct pf_state_key *skwp, struct pf_state_key *sksp)
1932 {
1933 	struct pf_state_key *skw, *sks;
1934 	u_int8_t proto, dir;
1935 
1936 	/* Do our best to fill these, but they're skipped if NULL */
1937 	skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL);
1938 	sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL);
1939 	proto = skw ? skw->proto : (sks ? sks->proto : 0);
1940 	dir = s ? s->direction : 0;
1941 
1942 	switch (proto) {
1943 	case IPPROTO_IPV4:
1944 		printf("IPv4");
1945 		break;
1946 	case IPPROTO_IPV6:
1947 		printf("IPv6");
1948 		break;
1949 	case IPPROTO_TCP:
1950 		printf("TCP");
1951 		break;
1952 	case IPPROTO_UDP:
1953 		printf("UDP");
1954 		break;
1955 	case IPPROTO_ICMP:
1956 		printf("ICMP");
1957 		break;
1958 	case IPPROTO_ICMPV6:
1959 		printf("ICMPv6");
1960 		break;
1961 	default:
1962 		printf("%u", proto);
1963 		break;
1964 	}
1965 	switch (dir) {
1966 	case PF_IN:
1967 		printf(" in");
1968 		break;
1969 	case PF_OUT:
1970 		printf(" out");
1971 		break;
1972 	}
1973 	if (skw) {
1974 		printf(" wire: ");
1975 		pf_print_host(&skw->addr[0], skw->port[0], skw->af);
1976 		printf(" ");
1977 		pf_print_host(&skw->addr[1], skw->port[1], skw->af);
1978 	}
1979 	if (sks) {
1980 		printf(" stack: ");
1981 		if (sks != skw) {
1982 			pf_print_host(&sks->addr[0], sks->port[0], sks->af);
1983 			printf(" ");
1984 			pf_print_host(&sks->addr[1], sks->port[1], sks->af);
1985 		} else
1986 			printf("-");
1987 	}
1988 	if (s) {
1989 		if (proto == IPPROTO_TCP) {
1990 			printf(" [lo=%u high=%u win=%u modulator=%u",
1991 			    s->src.seqlo, s->src.seqhi,
1992 			    s->src.max_win, s->src.seqdiff);
1993 			if (s->src.wscale && s->dst.wscale)
1994 				printf(" wscale=%u",
1995 				    s->src.wscale & PF_WSCALE_MASK);
1996 			printf("]");
1997 			printf(" [lo=%u high=%u win=%u modulator=%u",
1998 			    s->dst.seqlo, s->dst.seqhi,
1999 			    s->dst.max_win, s->dst.seqdiff);
2000 			if (s->src.wscale && s->dst.wscale)
2001 				printf(" wscale=%u",
2002 				s->dst.wscale & PF_WSCALE_MASK);
2003 			printf("]");
2004 		}
2005 		printf(" %u:%u", s->src.state, s->dst.state);
2006 	}
2007 }
2008 
2009 void
2010 pf_print_flags(u_int8_t f)
2011 {
2012 	if (f)
2013 		printf(" ");
2014 	if (f & TH_FIN)
2015 		printf("F");
2016 	if (f & TH_SYN)
2017 		printf("S");
2018 	if (f & TH_RST)
2019 		printf("R");
2020 	if (f & TH_PUSH)
2021 		printf("P");
2022 	if (f & TH_ACK)
2023 		printf("A");
2024 	if (f & TH_URG)
2025 		printf("U");
2026 	if (f & TH_ECE)
2027 		printf("E");
2028 	if (f & TH_CWR)
2029 		printf("W");
2030 }
2031 
2032 #define	PF_SET_SKIP_STEPS(i)					\
2033 	do {							\
2034 		while (head[i] != cur) {			\
2035 			head[i]->skip[i].ptr = cur;		\
2036 			head[i] = TAILQ_NEXT(head[i], entries);	\
2037 		}						\
2038 	} while (0)
2039 
2040 void
2041 pf_calc_skip_steps(struct pf_krulequeue *rules)
2042 {
2043 	struct pf_krule *cur, *prev, *head[PF_SKIP_COUNT];
2044 	int i;
2045 
2046 	cur = TAILQ_FIRST(rules);
2047 	prev = cur;
2048 	for (i = 0; i < PF_SKIP_COUNT; ++i)
2049 		head[i] = cur;
2050 	while (cur != NULL) {
2051 		if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
2052 			PF_SET_SKIP_STEPS(PF_SKIP_IFP);
2053 		if (cur->direction != prev->direction)
2054 			PF_SET_SKIP_STEPS(PF_SKIP_DIR);
2055 		if (cur->af != prev->af)
2056 			PF_SET_SKIP_STEPS(PF_SKIP_AF);
2057 		if (cur->proto != prev->proto)
2058 			PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
2059 		if (cur->src.neg != prev->src.neg ||
2060 		    pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
2061 			PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
2062 		if (cur->src.port[0] != prev->src.port[0] ||
2063 		    cur->src.port[1] != prev->src.port[1] ||
2064 		    cur->src.port_op != prev->src.port_op)
2065 			PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
2066 		if (cur->dst.neg != prev->dst.neg ||
2067 		    pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
2068 			PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
2069 		if (cur->dst.port[0] != prev->dst.port[0] ||
2070 		    cur->dst.port[1] != prev->dst.port[1] ||
2071 		    cur->dst.port_op != prev->dst.port_op)
2072 			PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
2073 
2074 		prev = cur;
2075 		cur = TAILQ_NEXT(cur, entries);
2076 	}
2077 	for (i = 0; i < PF_SKIP_COUNT; ++i)
2078 		PF_SET_SKIP_STEPS(i);
2079 }
2080 
2081 static int
2082 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
2083 {
2084 	if (aw1->type != aw2->type)
2085 		return (1);
2086 	switch (aw1->type) {
2087 	case PF_ADDR_ADDRMASK:
2088 	case PF_ADDR_RANGE:
2089 		if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, AF_INET6))
2090 			return (1);
2091 		if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, AF_INET6))
2092 			return (1);
2093 		return (0);
2094 	case PF_ADDR_DYNIFTL:
2095 		return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
2096 	case PF_ADDR_NOROUTE:
2097 	case PF_ADDR_URPFFAILED:
2098 		return (0);
2099 	case PF_ADDR_TABLE:
2100 		return (aw1->p.tbl != aw2->p.tbl);
2101 	default:
2102 		printf("invalid address type: %d\n", aw1->type);
2103 		return (1);
2104 	}
2105 }
2106 
2107 /**
2108  * Checksum updates are a little complicated because the checksum in the TCP/UDP
2109  * header isn't always a full checksum. In some cases (i.e. output) it's a
2110  * pseudo-header checksum, which is a partial checksum over src/dst IP
2111  * addresses, protocol number and length.
2112  *
2113  * That means we have the following cases:
2114  *  * Input or forwarding: we don't have TSO, the checksum fields are full
2115  *  	checksums, we need to update the checksum whenever we change anything.
2116  *  * Output (i.e. the checksum is a pseudo-header checksum):
2117  *  	x The field being updated is src/dst address or affects the length of
2118  *  	the packet. We need to update the pseudo-header checksum (note that this
2119  *  	checksum is not ones' complement).
2120  *  	x Some other field is being modified (e.g. src/dst port numbers): We
2121  *  	don't have to update anything.
2122  **/
2123 u_int16_t
2124 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
2125 {
2126 	u_int32_t x;
2127 
2128 	x = cksum + old - new;
2129 	x = (x + (x >> 16)) & 0xffff;
2130 
2131 	/* optimise: eliminate a branch when not udp */
2132 	if (udp && cksum == 0x0000)
2133 		return cksum;
2134 	if (udp && x == 0x0000)
2135 		x = 0xffff;
2136 
2137 	return (u_int16_t)(x);
2138 }
2139 
2140 static void
2141 pf_patch_8(struct mbuf *m, u_int16_t *cksum, u_int8_t *f, u_int8_t v, bool hi,
2142     u_int8_t udp)
2143 {
2144 	u_int16_t old = htons(hi ? (*f << 8) : *f);
2145 	u_int16_t new = htons(hi ? ( v << 8) :  v);
2146 
2147 	if (*f == v)
2148 		return;
2149 
2150 	*f = v;
2151 
2152 	if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6))
2153 		return;
2154 
2155 	*cksum = pf_cksum_fixup(*cksum, old, new, udp);
2156 }
2157 
2158 void
2159 pf_patch_16_unaligned(struct mbuf *m, u_int16_t *cksum, void *f, u_int16_t v,
2160     bool hi, u_int8_t udp)
2161 {
2162 	u_int8_t *fb = (u_int8_t *)f;
2163 	u_int8_t *vb = (u_int8_t *)&v;
2164 
2165 	pf_patch_8(m, cksum, fb++, *vb++, hi, udp);
2166 	pf_patch_8(m, cksum, fb++, *vb++, !hi, udp);
2167 }
2168 
2169 void
2170 pf_patch_32_unaligned(struct mbuf *m, u_int16_t *cksum, void *f, u_int32_t v,
2171     bool hi, u_int8_t udp)
2172 {
2173 	u_int8_t *fb = (u_int8_t *)f;
2174 	u_int8_t *vb = (u_int8_t *)&v;
2175 
2176 	pf_patch_8(m, cksum, fb++, *vb++, hi, udp);
2177 	pf_patch_8(m, cksum, fb++, *vb++, !hi, udp);
2178 	pf_patch_8(m, cksum, fb++, *vb++, hi, udp);
2179 	pf_patch_8(m, cksum, fb++, *vb++, !hi, udp);
2180 }
2181 
2182 u_int16_t
2183 pf_proto_cksum_fixup(struct mbuf *m, u_int16_t cksum, u_int16_t old,
2184         u_int16_t new, u_int8_t udp)
2185 {
2186 	if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6))
2187 		return (cksum);
2188 
2189 	return (pf_cksum_fixup(cksum, old, new, udp));
2190 }
2191 
2192 static void
2193 pf_change_ap(struct mbuf *m, struct pf_addr *a, u_int16_t *p, u_int16_t *ic,
2194         u_int16_t *pc, struct pf_addr *an, u_int16_t pn, u_int8_t u,
2195         sa_family_t af)
2196 {
2197 	struct pf_addr	ao;
2198 	u_int16_t	po = *p;
2199 
2200 	PF_ACPY(&ao, a, af);
2201 	PF_ACPY(a, an, af);
2202 
2203 	if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6))
2204 		*pc = ~*pc;
2205 
2206 	*p = pn;
2207 
2208 	switch (af) {
2209 #ifdef INET
2210 	case AF_INET:
2211 		*ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
2212 		    ao.addr16[0], an->addr16[0], 0),
2213 		    ao.addr16[1], an->addr16[1], 0);
2214 		*p = pn;
2215 
2216 		*pc = pf_cksum_fixup(pf_cksum_fixup(*pc,
2217 		    ao.addr16[0], an->addr16[0], u),
2218 		    ao.addr16[1], an->addr16[1], u);
2219 
2220 		*pc = pf_proto_cksum_fixup(m, *pc, po, pn, u);
2221 		break;
2222 #endif /* INET */
2223 #ifdef INET6
2224 	case AF_INET6:
2225 		*pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2226 		    pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2227 		    pf_cksum_fixup(pf_cksum_fixup(*pc,
2228 		    ao.addr16[0], an->addr16[0], u),
2229 		    ao.addr16[1], an->addr16[1], u),
2230 		    ao.addr16[2], an->addr16[2], u),
2231 		    ao.addr16[3], an->addr16[3], u),
2232 		    ao.addr16[4], an->addr16[4], u),
2233 		    ao.addr16[5], an->addr16[5], u),
2234 		    ao.addr16[6], an->addr16[6], u),
2235 		    ao.addr16[7], an->addr16[7], u);
2236 
2237 		*pc = pf_proto_cksum_fixup(m, *pc, po, pn, u);
2238 		break;
2239 #endif /* INET6 */
2240 	}
2241 
2242 	if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA |
2243 	    CSUM_DELAY_DATA_IPV6)) {
2244 		*pc = ~*pc;
2245 		if (! *pc)
2246 			*pc = 0xffff;
2247 	}
2248 }
2249 
2250 /* Changes a u_int32_t.  Uses a void * so there are no align restrictions */
2251 void
2252 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
2253 {
2254 	u_int32_t	ao;
2255 
2256 	memcpy(&ao, a, sizeof(ao));
2257 	memcpy(a, &an, sizeof(u_int32_t));
2258 	*c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
2259 	    ao % 65536, an % 65536, u);
2260 }
2261 
2262 void
2263 pf_change_proto_a(struct mbuf *m, void *a, u_int16_t *c, u_int32_t an, u_int8_t udp)
2264 {
2265 	u_int32_t	ao;
2266 
2267 	memcpy(&ao, a, sizeof(ao));
2268 	memcpy(a, &an, sizeof(u_int32_t));
2269 
2270 	*c = pf_proto_cksum_fixup(m,
2271 	    pf_proto_cksum_fixup(m, *c, ao / 65536, an / 65536, udp),
2272 	    ao % 65536, an % 65536, udp);
2273 }
2274 
2275 #ifdef INET6
2276 static void
2277 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
2278 {
2279 	struct pf_addr	ao;
2280 
2281 	PF_ACPY(&ao, a, AF_INET6);
2282 	PF_ACPY(a, an, AF_INET6);
2283 
2284 	*c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2285 	    pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2286 	    pf_cksum_fixup(pf_cksum_fixup(*c,
2287 	    ao.addr16[0], an->addr16[0], u),
2288 	    ao.addr16[1], an->addr16[1], u),
2289 	    ao.addr16[2], an->addr16[2], u),
2290 	    ao.addr16[3], an->addr16[3], u),
2291 	    ao.addr16[4], an->addr16[4], u),
2292 	    ao.addr16[5], an->addr16[5], u),
2293 	    ao.addr16[6], an->addr16[6], u),
2294 	    ao.addr16[7], an->addr16[7], u);
2295 }
2296 #endif /* INET6 */
2297 
2298 static void
2299 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
2300     struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
2301     u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
2302 {
2303 	struct pf_addr	oia, ooa;
2304 
2305 	PF_ACPY(&oia, ia, af);
2306 	if (oa)
2307 		PF_ACPY(&ooa, oa, af);
2308 
2309 	/* Change inner protocol port, fix inner protocol checksum. */
2310 	if (ip != NULL) {
2311 		u_int16_t	oip = *ip;
2312 		u_int32_t	opc;
2313 
2314 		if (pc != NULL)
2315 			opc = *pc;
2316 		*ip = np;
2317 		if (pc != NULL)
2318 			*pc = pf_cksum_fixup(*pc, oip, *ip, u);
2319 		*ic = pf_cksum_fixup(*ic, oip, *ip, 0);
2320 		if (pc != NULL)
2321 			*ic = pf_cksum_fixup(*ic, opc, *pc, 0);
2322 	}
2323 	/* Change inner ip address, fix inner ip and icmp checksums. */
2324 	PF_ACPY(ia, na, af);
2325 	switch (af) {
2326 #ifdef INET
2327 	case AF_INET: {
2328 		u_int32_t	 oh2c = *h2c;
2329 
2330 		*h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
2331 		    oia.addr16[0], ia->addr16[0], 0),
2332 		    oia.addr16[1], ia->addr16[1], 0);
2333 		*ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
2334 		    oia.addr16[0], ia->addr16[0], 0),
2335 		    oia.addr16[1], ia->addr16[1], 0);
2336 		*ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
2337 		break;
2338 	}
2339 #endif /* INET */
2340 #ifdef INET6
2341 	case AF_INET6:
2342 		*ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2343 		    pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2344 		    pf_cksum_fixup(pf_cksum_fixup(*ic,
2345 		    oia.addr16[0], ia->addr16[0], u),
2346 		    oia.addr16[1], ia->addr16[1], u),
2347 		    oia.addr16[2], ia->addr16[2], u),
2348 		    oia.addr16[3], ia->addr16[3], u),
2349 		    oia.addr16[4], ia->addr16[4], u),
2350 		    oia.addr16[5], ia->addr16[5], u),
2351 		    oia.addr16[6], ia->addr16[6], u),
2352 		    oia.addr16[7], ia->addr16[7], u);
2353 		break;
2354 #endif /* INET6 */
2355 	}
2356 	/* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */
2357 	if (oa) {
2358 		PF_ACPY(oa, na, af);
2359 		switch (af) {
2360 #ifdef INET
2361 		case AF_INET:
2362 			*hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
2363 			    ooa.addr16[0], oa->addr16[0], 0),
2364 			    ooa.addr16[1], oa->addr16[1], 0);
2365 			break;
2366 #endif /* INET */
2367 #ifdef INET6
2368 		case AF_INET6:
2369 			*ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2370 			    pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2371 			    pf_cksum_fixup(pf_cksum_fixup(*ic,
2372 			    ooa.addr16[0], oa->addr16[0], u),
2373 			    ooa.addr16[1], oa->addr16[1], u),
2374 			    ooa.addr16[2], oa->addr16[2], u),
2375 			    ooa.addr16[3], oa->addr16[3], u),
2376 			    ooa.addr16[4], oa->addr16[4], u),
2377 			    ooa.addr16[5], oa->addr16[5], u),
2378 			    ooa.addr16[6], oa->addr16[6], u),
2379 			    ooa.addr16[7], oa->addr16[7], u);
2380 			break;
2381 #endif /* INET6 */
2382 		}
2383 	}
2384 }
2385 
2386 /*
2387  * Need to modulate the sequence numbers in the TCP SACK option
2388  * (credits to Krzysztof Pfaff for report and patch)
2389  */
2390 static int
2391 pf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd,
2392     struct tcphdr *th, struct pf_state_peer *dst)
2393 {
2394 	int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen;
2395 	u_int8_t opts[TCP_MAXOLEN], *opt = opts;
2396 	int copyback = 0, i, olen;
2397 	struct sackblk sack;
2398 
2399 #define	TCPOLEN_SACKLEN	(TCPOLEN_SACK + 2)
2400 	if (hlen < TCPOLEN_SACKLEN ||
2401 	    !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af))
2402 		return 0;
2403 
2404 	while (hlen >= TCPOLEN_SACKLEN) {
2405 		size_t startoff = opt - opts;
2406 		olen = opt[1];
2407 		switch (*opt) {
2408 		case TCPOPT_EOL:	/* FALLTHROUGH */
2409 		case TCPOPT_NOP:
2410 			opt++;
2411 			hlen--;
2412 			break;
2413 		case TCPOPT_SACK:
2414 			if (olen > hlen)
2415 				olen = hlen;
2416 			if (olen >= TCPOLEN_SACKLEN) {
2417 				for (i = 2; i + TCPOLEN_SACK <= olen;
2418 				    i += TCPOLEN_SACK) {
2419 					memcpy(&sack, &opt[i], sizeof(sack));
2420 					pf_patch_32_unaligned(m,
2421 					    &th->th_sum, &sack.start,
2422 					    htonl(ntohl(sack.start) - dst->seqdiff),
2423 					    PF_ALGNMNT(startoff),
2424 					    0);
2425 					pf_patch_32_unaligned(m, &th->th_sum,
2426 					    &sack.end,
2427 					    htonl(ntohl(sack.end) - dst->seqdiff),
2428 					    PF_ALGNMNT(startoff),
2429 					    0);
2430 					memcpy(&opt[i], &sack, sizeof(sack));
2431 				}
2432 				copyback = 1;
2433 			}
2434 			/* FALLTHROUGH */
2435 		default:
2436 			if (olen < 2)
2437 				olen = 2;
2438 			hlen -= olen;
2439 			opt += olen;
2440 		}
2441 	}
2442 
2443 	if (copyback)
2444 		m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts);
2445 	return (copyback);
2446 }
2447 
2448 static void
2449 pf_send_tcp(struct mbuf *replyto, const struct pf_krule *r, sa_family_t af,
2450     const struct pf_addr *saddr, const struct pf_addr *daddr,
2451     u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
2452     u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag,
2453     u_int16_t rtag, struct ifnet *ifp)
2454 {
2455 	struct pf_send_entry *pfse;
2456 	struct mbuf	*m;
2457 	int		 len, tlen;
2458 #ifdef INET
2459 	struct ip	*h = NULL;
2460 #endif /* INET */
2461 #ifdef INET6
2462 	struct ip6_hdr	*h6 = NULL;
2463 #endif /* INET6 */
2464 	struct tcphdr	*th;
2465 	char		*opt;
2466 	struct pf_mtag  *pf_mtag;
2467 
2468 	len = 0;
2469 	th = NULL;
2470 
2471 	/* maximum segment size tcp option */
2472 	tlen = sizeof(struct tcphdr);
2473 	if (mss)
2474 		tlen += 4;
2475 
2476 	switch (af) {
2477 #ifdef INET
2478 	case AF_INET:
2479 		len = sizeof(struct ip) + tlen;
2480 		break;
2481 #endif /* INET */
2482 #ifdef INET6
2483 	case AF_INET6:
2484 		len = sizeof(struct ip6_hdr) + tlen;
2485 		break;
2486 #endif /* INET6 */
2487 	default:
2488 		panic("%s: unsupported af %d", __func__, af);
2489 	}
2490 
2491 	/* Allocate outgoing queue entry, mbuf and mbuf tag. */
2492 	pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT);
2493 	if (pfse == NULL)
2494 		return;
2495 	m = m_gethdr(M_NOWAIT, MT_DATA);
2496 	if (m == NULL) {
2497 		free(pfse, M_PFTEMP);
2498 		return;
2499 	}
2500 #ifdef MAC
2501 	mac_netinet_firewall_send(m);
2502 #endif
2503 	if ((pf_mtag = pf_get_mtag(m)) == NULL) {
2504 		free(pfse, M_PFTEMP);
2505 		m_freem(m);
2506 		return;
2507 	}
2508 	if (tag)
2509 		m->m_flags |= M_SKIP_FIREWALL;
2510 	pf_mtag->tag = rtag;
2511 
2512 	if (r != NULL && r->rtableid >= 0)
2513 		M_SETFIB(m, r->rtableid);
2514 
2515 #ifdef ALTQ
2516 	if (r != NULL && r->qid) {
2517 		pf_mtag->qid = r->qid;
2518 
2519 		/* add hints for ecn */
2520 		pf_mtag->hdr = mtod(m, struct ip *);
2521 	}
2522 #endif /* ALTQ */
2523 	m->m_data += max_linkhdr;
2524 	m->m_pkthdr.len = m->m_len = len;
2525 	m->m_pkthdr.rcvif = NULL;
2526 	bzero(m->m_data, len);
2527 	switch (af) {
2528 #ifdef INET
2529 	case AF_INET:
2530 		h = mtod(m, struct ip *);
2531 
2532 		/* IP header fields included in the TCP checksum */
2533 		h->ip_p = IPPROTO_TCP;
2534 		h->ip_len = htons(tlen);
2535 		h->ip_src.s_addr = saddr->v4.s_addr;
2536 		h->ip_dst.s_addr = daddr->v4.s_addr;
2537 
2538 		th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
2539 		break;
2540 #endif /* INET */
2541 #ifdef INET6
2542 	case AF_INET6:
2543 		h6 = mtod(m, struct ip6_hdr *);
2544 
2545 		/* IP header fields included in the TCP checksum */
2546 		h6->ip6_nxt = IPPROTO_TCP;
2547 		h6->ip6_plen = htons(tlen);
2548 		memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
2549 		memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
2550 
2551 		th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
2552 		break;
2553 #endif /* INET6 */
2554 	}
2555 
2556 	/* TCP header */
2557 	th->th_sport = sport;
2558 	th->th_dport = dport;
2559 	th->th_seq = htonl(seq);
2560 	th->th_ack = htonl(ack);
2561 	th->th_off = tlen >> 2;
2562 	th->th_flags = flags;
2563 	th->th_win = htons(win);
2564 
2565 	if (mss) {
2566 		opt = (char *)(th + 1);
2567 		opt[0] = TCPOPT_MAXSEG;
2568 		opt[1] = 4;
2569 		HTONS(mss);
2570 		bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
2571 	}
2572 
2573 	switch (af) {
2574 #ifdef INET
2575 	case AF_INET:
2576 		/* TCP checksum */
2577 		th->th_sum = in_cksum(m, len);
2578 
2579 		/* Finish the IP header */
2580 		h->ip_v = 4;
2581 		h->ip_hl = sizeof(*h) >> 2;
2582 		h->ip_tos = IPTOS_LOWDELAY;
2583 		h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
2584 		h->ip_len = htons(len);
2585 		h->ip_ttl = ttl ? ttl : V_ip_defttl;
2586 		h->ip_sum = 0;
2587 
2588 		pfse->pfse_type = PFSE_IP;
2589 		break;
2590 #endif /* INET */
2591 #ifdef INET6
2592 	case AF_INET6:
2593 		/* TCP checksum */
2594 		th->th_sum = in6_cksum(m, IPPROTO_TCP,
2595 		    sizeof(struct ip6_hdr), tlen);
2596 
2597 		h6->ip6_vfc |= IPV6_VERSION;
2598 		h6->ip6_hlim = IPV6_DEFHLIM;
2599 
2600 		pfse->pfse_type = PFSE_IP6;
2601 		break;
2602 #endif /* INET6 */
2603 	}
2604 	pfse->pfse_m = m;
2605 	pf_send(pfse);
2606 }
2607 
2608 static void
2609 pf_return(struct pf_krule *r, struct pf_krule *nr, struct pf_pdesc *pd,
2610     struct pf_state_key *sk, int off, struct mbuf *m, struct tcphdr *th,
2611     struct pfi_kkif *kif, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen,
2612     u_short *reason)
2613 {
2614 	struct pf_addr	* const saddr = pd->src;
2615 	struct pf_addr	* const daddr = pd->dst;
2616 	sa_family_t	 af = pd->af;
2617 
2618 	/* undo NAT changes, if they have taken place */
2619 	if (nr != NULL) {
2620 		PF_ACPY(saddr, &sk->addr[pd->sidx], af);
2621 		PF_ACPY(daddr, &sk->addr[pd->didx], af);
2622 		if (pd->sport)
2623 			*pd->sport = sk->port[pd->sidx];
2624 		if (pd->dport)
2625 			*pd->dport = sk->port[pd->didx];
2626 		if (pd->proto_sum)
2627 			*pd->proto_sum = bproto_sum;
2628 		if (pd->ip_sum)
2629 			*pd->ip_sum = bip_sum;
2630 		m_copyback(m, off, hdrlen, pd->hdr.any);
2631 	}
2632 	if (pd->proto == IPPROTO_TCP &&
2633 	    ((r->rule_flag & PFRULE_RETURNRST) ||
2634 	    (r->rule_flag & PFRULE_RETURN)) &&
2635 	    !(th->th_flags & TH_RST)) {
2636 		u_int32_t	 ack = ntohl(th->th_seq) + pd->p_len;
2637 		int		 len = 0;
2638 #ifdef INET
2639 		struct ip	*h4;
2640 #endif
2641 #ifdef INET6
2642 		struct ip6_hdr	*h6;
2643 #endif
2644 
2645 		switch (af) {
2646 #ifdef INET
2647 		case AF_INET:
2648 			h4 = mtod(m, struct ip *);
2649 			len = ntohs(h4->ip_len) - off;
2650 			break;
2651 #endif
2652 #ifdef INET6
2653 		case AF_INET6:
2654 			h6 = mtod(m, struct ip6_hdr *);
2655 			len = ntohs(h6->ip6_plen) - (off - sizeof(*h6));
2656 			break;
2657 #endif
2658 		}
2659 
2660 		if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af))
2661 			REASON_SET(reason, PFRES_PROTCKSUM);
2662 		else {
2663 			if (th->th_flags & TH_SYN)
2664 				ack++;
2665 			if (th->th_flags & TH_FIN)
2666 				ack++;
2667 			pf_send_tcp(m, r, af, pd->dst,
2668 				pd->src, th->th_dport, th->th_sport,
2669 				ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
2670 				r->return_ttl, 1, 0, kif->pfik_ifp);
2671 		}
2672 	} else if (pd->proto != IPPROTO_ICMP && af == AF_INET &&
2673 		r->return_icmp)
2674 		pf_send_icmp(m, r->return_icmp >> 8,
2675 			r->return_icmp & 255, af, r);
2676 	else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 &&
2677 		r->return_icmp6)
2678 		pf_send_icmp(m, r->return_icmp6 >> 8,
2679 			r->return_icmp6 & 255, af, r);
2680 }
2681 
2682 static int
2683 pf_ieee8021q_setpcp(struct mbuf *m, u_int8_t prio)
2684 {
2685 	struct m_tag *mtag;
2686 
2687 	KASSERT(prio <= PF_PRIO_MAX,
2688 	    ("%s with invalid pcp", __func__));
2689 
2690 	mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL);
2691 	if (mtag == NULL) {
2692 		mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_OUT,
2693 		    sizeof(uint8_t), M_NOWAIT);
2694 		if (mtag == NULL)
2695 			return (ENOMEM);
2696 		m_tag_prepend(m, mtag);
2697 	}
2698 
2699 	*(uint8_t *)(mtag + 1) = prio;
2700 	return (0);
2701 }
2702 
2703 static int
2704 pf_match_ieee8021q_pcp(u_int8_t prio, struct mbuf *m)
2705 {
2706 	struct m_tag *mtag;
2707 	u_int8_t mpcp;
2708 
2709 	mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
2710 	if (mtag == NULL)
2711 		return (0);
2712 
2713 	if (prio == PF_PRIO_ZERO)
2714 		prio = 0;
2715 
2716 	mpcp = *(uint8_t *)(mtag + 1);
2717 
2718 	return (mpcp == prio);
2719 }
2720 
2721 static void
2722 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
2723     struct pf_krule *r)
2724 {
2725 	struct pf_send_entry *pfse;
2726 	struct mbuf *m0;
2727 	struct pf_mtag *pf_mtag;
2728 
2729 	/* Allocate outgoing queue entry, mbuf and mbuf tag. */
2730 	pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT);
2731 	if (pfse == NULL)
2732 		return;
2733 
2734 	if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) {
2735 		free(pfse, M_PFTEMP);
2736 		return;
2737 	}
2738 
2739 	if ((pf_mtag = pf_get_mtag(m0)) == NULL) {
2740 		free(pfse, M_PFTEMP);
2741 		return;
2742 	}
2743 	/* XXX: revisit */
2744 	m0->m_flags |= M_SKIP_FIREWALL;
2745 
2746 	if (r->rtableid >= 0)
2747 		M_SETFIB(m0, r->rtableid);
2748 
2749 #ifdef ALTQ
2750 	if (r->qid) {
2751 		pf_mtag->qid = r->qid;
2752 		/* add hints for ecn */
2753 		pf_mtag->hdr = mtod(m0, struct ip *);
2754 	}
2755 #endif /* ALTQ */
2756 
2757 	switch (af) {
2758 #ifdef INET
2759 	case AF_INET:
2760 		pfse->pfse_type = PFSE_ICMP;
2761 		break;
2762 #endif /* INET */
2763 #ifdef INET6
2764 	case AF_INET6:
2765 		pfse->pfse_type = PFSE_ICMP6;
2766 		break;
2767 #endif /* INET6 */
2768 	}
2769 	pfse->pfse_m = m0;
2770 	pfse->icmpopts.type = type;
2771 	pfse->icmpopts.code = code;
2772 	pf_send(pfse);
2773 }
2774 
2775 /*
2776  * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2777  * If n is 0, they match if they are equal. If n is != 0, they match if they
2778  * are different.
2779  */
2780 int
2781 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
2782     struct pf_addr *b, sa_family_t af)
2783 {
2784 	int	match = 0;
2785 
2786 	switch (af) {
2787 #ifdef INET
2788 	case AF_INET:
2789 		if ((a->addr32[0] & m->addr32[0]) ==
2790 		    (b->addr32[0] & m->addr32[0]))
2791 			match++;
2792 		break;
2793 #endif /* INET */
2794 #ifdef INET6
2795 	case AF_INET6:
2796 		if (((a->addr32[0] & m->addr32[0]) ==
2797 		     (b->addr32[0] & m->addr32[0])) &&
2798 		    ((a->addr32[1] & m->addr32[1]) ==
2799 		     (b->addr32[1] & m->addr32[1])) &&
2800 		    ((a->addr32[2] & m->addr32[2]) ==
2801 		     (b->addr32[2] & m->addr32[2])) &&
2802 		    ((a->addr32[3] & m->addr32[3]) ==
2803 		     (b->addr32[3] & m->addr32[3])))
2804 			match++;
2805 		break;
2806 #endif /* INET6 */
2807 	}
2808 	if (match) {
2809 		if (n)
2810 			return (0);
2811 		else
2812 			return (1);
2813 	} else {
2814 		if (n)
2815 			return (1);
2816 		else
2817 			return (0);
2818 	}
2819 }
2820 
2821 /*
2822  * Return 1 if b <= a <= e, otherwise return 0.
2823  */
2824 int
2825 pf_match_addr_range(struct pf_addr *b, struct pf_addr *e,
2826     struct pf_addr *a, sa_family_t af)
2827 {
2828 	switch (af) {
2829 #ifdef INET
2830 	case AF_INET:
2831 		if ((ntohl(a->addr32[0]) < ntohl(b->addr32[0])) ||
2832 		    (ntohl(a->addr32[0]) > ntohl(e->addr32[0])))
2833 			return (0);
2834 		break;
2835 #endif /* INET */
2836 #ifdef INET6
2837 	case AF_INET6: {
2838 		int	i;
2839 
2840 		/* check a >= b */
2841 		for (i = 0; i < 4; ++i)
2842 			if (ntohl(a->addr32[i]) > ntohl(b->addr32[i]))
2843 				break;
2844 			else if (ntohl(a->addr32[i]) < ntohl(b->addr32[i]))
2845 				return (0);
2846 		/* check a <= e */
2847 		for (i = 0; i < 4; ++i)
2848 			if (ntohl(a->addr32[i]) < ntohl(e->addr32[i]))
2849 				break;
2850 			else if (ntohl(a->addr32[i]) > ntohl(e->addr32[i]))
2851 				return (0);
2852 		break;
2853 	}
2854 #endif /* INET6 */
2855 	}
2856 	return (1);
2857 }
2858 
2859 static int
2860 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
2861 {
2862 	switch (op) {
2863 	case PF_OP_IRG:
2864 		return ((p > a1) && (p < a2));
2865 	case PF_OP_XRG:
2866 		return ((p < a1) || (p > a2));
2867 	case PF_OP_RRG:
2868 		return ((p >= a1) && (p <= a2));
2869 	case PF_OP_EQ:
2870 		return (p == a1);
2871 	case PF_OP_NE:
2872 		return (p != a1);
2873 	case PF_OP_LT:
2874 		return (p < a1);
2875 	case PF_OP_LE:
2876 		return (p <= a1);
2877 	case PF_OP_GT:
2878 		return (p > a1);
2879 	case PF_OP_GE:
2880 		return (p >= a1);
2881 	}
2882 	return (0); /* never reached */
2883 }
2884 
2885 int
2886 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
2887 {
2888 	NTOHS(a1);
2889 	NTOHS(a2);
2890 	NTOHS(p);
2891 	return (pf_match(op, a1, a2, p));
2892 }
2893 
2894 static int
2895 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
2896 {
2897 	if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2898 		return (0);
2899 	return (pf_match(op, a1, a2, u));
2900 }
2901 
2902 static int
2903 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
2904 {
2905 	if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2906 		return (0);
2907 	return (pf_match(op, a1, a2, g));
2908 }
2909 
2910 int
2911 pf_match_tag(struct mbuf *m, struct pf_krule *r, int *tag, int mtag)
2912 {
2913 	if (*tag == -1)
2914 		*tag = mtag;
2915 
2916 	return ((!r->match_tag_not && r->match_tag == *tag) ||
2917 	    (r->match_tag_not && r->match_tag != *tag));
2918 }
2919 
2920 int
2921 pf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag)
2922 {
2923 
2924 	KASSERT(tag > 0, ("%s: tag %d", __func__, tag));
2925 
2926 	if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL))
2927 		return (ENOMEM);
2928 
2929 	pd->pf_mtag->tag = tag;
2930 
2931 	return (0);
2932 }
2933 
2934 #define	PF_ANCHOR_STACKSIZE	32
2935 struct pf_kanchor_stackframe {
2936 	struct pf_kruleset	*rs;
2937 	struct pf_krule		*r;	/* XXX: + match bit */
2938 	struct pf_kanchor	*child;
2939 };
2940 
2941 /*
2942  * XXX: We rely on malloc(9) returning pointer aligned addresses.
2943  */
2944 #define	PF_ANCHORSTACK_MATCH	0x00000001
2945 #define	PF_ANCHORSTACK_MASK	(PF_ANCHORSTACK_MATCH)
2946 
2947 #define	PF_ANCHOR_MATCH(f)	((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH)
2948 #define	PF_ANCHOR_RULE(f)	(struct pf_krule *)			\
2949 				((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK)
2950 #define	PF_ANCHOR_SET_MATCH(f)	do { (f)->r = (void *) 			\
2951 				((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH);  \
2952 } while (0)
2953 
2954 void
2955 pf_step_into_anchor(struct pf_kanchor_stackframe *stack, int *depth,
2956     struct pf_kruleset **rs, int n, struct pf_krule **r, struct pf_krule **a,
2957     int *match)
2958 {
2959 	struct pf_kanchor_stackframe	*f;
2960 
2961 	PF_RULES_RASSERT();
2962 
2963 	if (match)
2964 		*match = 0;
2965 	if (*depth >= PF_ANCHOR_STACKSIZE) {
2966 		printf("%s: anchor stack overflow on %s\n",
2967 		    __func__, (*r)->anchor->name);
2968 		*r = TAILQ_NEXT(*r, entries);
2969 		return;
2970 	} else if (*depth == 0 && a != NULL)
2971 		*a = *r;
2972 	f = stack + (*depth)++;
2973 	f->rs = *rs;
2974 	f->r = *r;
2975 	if ((*r)->anchor_wildcard) {
2976 		struct pf_kanchor_node *parent = &(*r)->anchor->children;
2977 
2978 		if ((f->child = RB_MIN(pf_kanchor_node, parent)) == NULL) {
2979 			*r = NULL;
2980 			return;
2981 		}
2982 		*rs = &f->child->ruleset;
2983 	} else {
2984 		f->child = NULL;
2985 		*rs = &(*r)->anchor->ruleset;
2986 	}
2987 	*r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
2988 }
2989 
2990 int
2991 pf_step_out_of_anchor(struct pf_kanchor_stackframe *stack, int *depth,
2992     struct pf_kruleset **rs, int n, struct pf_krule **r, struct pf_krule **a,
2993     int *match)
2994 {
2995 	struct pf_kanchor_stackframe	*f;
2996 	struct pf_krule *fr;
2997 	int quick = 0;
2998 
2999 	PF_RULES_RASSERT();
3000 
3001 	do {
3002 		if (*depth <= 0)
3003 			break;
3004 		f = stack + *depth - 1;
3005 		fr = PF_ANCHOR_RULE(f);
3006 		if (f->child != NULL) {
3007 			struct pf_kanchor_node *parent;
3008 
3009 			/*
3010 			 * This block traverses through
3011 			 * a wildcard anchor.
3012 			 */
3013 			parent = &fr->anchor->children;
3014 			if (match != NULL && *match) {
3015 				/*
3016 				 * If any of "*" matched, then
3017 				 * "foo/ *" matched, mark frame
3018 				 * appropriately.
3019 				 */
3020 				PF_ANCHOR_SET_MATCH(f);
3021 				*match = 0;
3022 			}
3023 			f->child = RB_NEXT(pf_kanchor_node, parent, f->child);
3024 			if (f->child != NULL) {
3025 				*rs = &f->child->ruleset;
3026 				*r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
3027 				if (*r == NULL)
3028 					continue;
3029 				else
3030 					break;
3031 			}
3032 		}
3033 		(*depth)--;
3034 		if (*depth == 0 && a != NULL)
3035 			*a = NULL;
3036 		*rs = f->rs;
3037 		if (PF_ANCHOR_MATCH(f) || (match != NULL && *match))
3038 			quick = fr->quick;
3039 		*r = TAILQ_NEXT(fr, entries);
3040 	} while (*r == NULL);
3041 
3042 	return (quick);
3043 }
3044 
3045 #ifdef INET6
3046 void
3047 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
3048     struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
3049 {
3050 	switch (af) {
3051 #ifdef INET
3052 	case AF_INET:
3053 		naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
3054 		((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
3055 		break;
3056 #endif /* INET */
3057 	case AF_INET6:
3058 		naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
3059 		((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
3060 		naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
3061 		((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
3062 		naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
3063 		((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
3064 		naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
3065 		((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
3066 		break;
3067 	}
3068 }
3069 
3070 void
3071 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
3072 {
3073 	switch (af) {
3074 #ifdef INET
3075 	case AF_INET:
3076 		addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
3077 		break;
3078 #endif /* INET */
3079 	case AF_INET6:
3080 		if (addr->addr32[3] == 0xffffffff) {
3081 			addr->addr32[3] = 0;
3082 			if (addr->addr32[2] == 0xffffffff) {
3083 				addr->addr32[2] = 0;
3084 				if (addr->addr32[1] == 0xffffffff) {
3085 					addr->addr32[1] = 0;
3086 					addr->addr32[0] =
3087 					    htonl(ntohl(addr->addr32[0]) + 1);
3088 				} else
3089 					addr->addr32[1] =
3090 					    htonl(ntohl(addr->addr32[1]) + 1);
3091 			} else
3092 				addr->addr32[2] =
3093 				    htonl(ntohl(addr->addr32[2]) + 1);
3094 		} else
3095 			addr->addr32[3] =
3096 			    htonl(ntohl(addr->addr32[3]) + 1);
3097 		break;
3098 	}
3099 }
3100 #endif /* INET6 */
3101 
3102 int
3103 pf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m)
3104 {
3105 	struct pf_addr		*saddr, *daddr;
3106 	u_int16_t		 sport, dport;
3107 	struct inpcbinfo	*pi;
3108 	struct inpcb		*inp;
3109 
3110 	pd->lookup.uid = UID_MAX;
3111 	pd->lookup.gid = GID_MAX;
3112 
3113 	switch (pd->proto) {
3114 	case IPPROTO_TCP:
3115 		if (pd->hdr.tcp == NULL)
3116 			return (-1);
3117 		sport = pd->hdr.tcp->th_sport;
3118 		dport = pd->hdr.tcp->th_dport;
3119 		pi = &V_tcbinfo;
3120 		break;
3121 	case IPPROTO_UDP:
3122 		if (pd->hdr.udp == NULL)
3123 			return (-1);
3124 		sport = pd->hdr.udp->uh_sport;
3125 		dport = pd->hdr.udp->uh_dport;
3126 		pi = &V_udbinfo;
3127 		break;
3128 	default:
3129 		return (-1);
3130 	}
3131 	if (direction == PF_IN) {
3132 		saddr = pd->src;
3133 		daddr = pd->dst;
3134 	} else {
3135 		u_int16_t	p;
3136 
3137 		p = sport;
3138 		sport = dport;
3139 		dport = p;
3140 		saddr = pd->dst;
3141 		daddr = pd->src;
3142 	}
3143 	switch (pd->af) {
3144 #ifdef INET
3145 	case AF_INET:
3146 		inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4,
3147 		    dport, INPLOOKUP_RLOCKPCB, NULL, m);
3148 		if (inp == NULL) {
3149 			inp = in_pcblookup_mbuf(pi, saddr->v4, sport,
3150 			   daddr->v4, dport, INPLOOKUP_WILDCARD |
3151 			   INPLOOKUP_RLOCKPCB, NULL, m);
3152 			if (inp == NULL)
3153 				return (-1);
3154 		}
3155 		break;
3156 #endif /* INET */
3157 #ifdef INET6
3158 	case AF_INET6:
3159 		inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6,
3160 		    dport, INPLOOKUP_RLOCKPCB, NULL, m);
3161 		if (inp == NULL) {
3162 			inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport,
3163 			    &daddr->v6, dport, INPLOOKUP_WILDCARD |
3164 			    INPLOOKUP_RLOCKPCB, NULL, m);
3165 			if (inp == NULL)
3166 				return (-1);
3167 		}
3168 		break;
3169 #endif /* INET6 */
3170 
3171 	default:
3172 		return (-1);
3173 	}
3174 	INP_RLOCK_ASSERT(inp);
3175 	pd->lookup.uid = inp->inp_cred->cr_uid;
3176 	pd->lookup.gid = inp->inp_cred->cr_groups[0];
3177 	INP_RUNLOCK(inp);
3178 
3179 	return (1);
3180 }
3181 
3182 static u_int8_t
3183 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3184 {
3185 	int		 hlen;
3186 	u_int8_t	 hdr[60];
3187 	u_int8_t	*opt, optlen;
3188 	u_int8_t	 wscale = 0;
3189 
3190 	hlen = th_off << 2;		/* hlen <= sizeof(hdr) */
3191 	if (hlen <= sizeof(struct tcphdr))
3192 		return (0);
3193 	if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3194 		return (0);
3195 	opt = hdr + sizeof(struct tcphdr);
3196 	hlen -= sizeof(struct tcphdr);
3197 	while (hlen >= 3) {
3198 		switch (*opt) {
3199 		case TCPOPT_EOL:
3200 		case TCPOPT_NOP:
3201 			++opt;
3202 			--hlen;
3203 			break;
3204 		case TCPOPT_WINDOW:
3205 			wscale = opt[2];
3206 			if (wscale > TCP_MAX_WINSHIFT)
3207 				wscale = TCP_MAX_WINSHIFT;
3208 			wscale |= PF_WSCALE_FLAG;
3209 			/* FALLTHROUGH */
3210 		default:
3211 			optlen = opt[1];
3212 			if (optlen < 2)
3213 				optlen = 2;
3214 			hlen -= optlen;
3215 			opt += optlen;
3216 			break;
3217 		}
3218 	}
3219 	return (wscale);
3220 }
3221 
3222 static u_int16_t
3223 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3224 {
3225 	int		 hlen;
3226 	u_int8_t	 hdr[60];
3227 	u_int8_t	*opt, optlen;
3228 	u_int16_t	 mss = V_tcp_mssdflt;
3229 
3230 	hlen = th_off << 2;	/* hlen <= sizeof(hdr) */
3231 	if (hlen <= sizeof(struct tcphdr))
3232 		return (0);
3233 	if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3234 		return (0);
3235 	opt = hdr + sizeof(struct tcphdr);
3236 	hlen -= sizeof(struct tcphdr);
3237 	while (hlen >= TCPOLEN_MAXSEG) {
3238 		switch (*opt) {
3239 		case TCPOPT_EOL:
3240 		case TCPOPT_NOP:
3241 			++opt;
3242 			--hlen;
3243 			break;
3244 		case TCPOPT_MAXSEG:
3245 			bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
3246 			NTOHS(mss);
3247 			/* FALLTHROUGH */
3248 		default:
3249 			optlen = opt[1];
3250 			if (optlen < 2)
3251 				optlen = 2;
3252 			hlen -= optlen;
3253 			opt += optlen;
3254 			break;
3255 		}
3256 	}
3257 	return (mss);
3258 }
3259 
3260 static u_int16_t
3261 pf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer)
3262 {
3263 	struct nhop_object *nh;
3264 #ifdef INET6
3265 	struct in6_addr		dst6;
3266 	uint32_t		scopeid;
3267 #endif /* INET6 */
3268 	int			 hlen = 0;
3269 	uint16_t		 mss = 0;
3270 
3271 	NET_EPOCH_ASSERT();
3272 
3273 	switch (af) {
3274 #ifdef INET
3275 	case AF_INET:
3276 		hlen = sizeof(struct ip);
3277 		nh = fib4_lookup(rtableid, addr->v4, 0, 0, 0);
3278 		if (nh != NULL)
3279 			mss = nh->nh_mtu - hlen - sizeof(struct tcphdr);
3280 		break;
3281 #endif /* INET */
3282 #ifdef INET6
3283 	case AF_INET6:
3284 		hlen = sizeof(struct ip6_hdr);
3285 		in6_splitscope(&addr->v6, &dst6, &scopeid);
3286 		nh = fib6_lookup(rtableid, &dst6, scopeid, 0, 0);
3287 		if (nh != NULL)
3288 			mss = nh->nh_mtu - hlen - sizeof(struct tcphdr);
3289 		break;
3290 #endif /* INET6 */
3291 	}
3292 
3293 	mss = max(V_tcp_mssdflt, mss);
3294 	mss = min(mss, offer);
3295 	mss = max(mss, 64);		/* sanity - at least max opt space */
3296 	return (mss);
3297 }
3298 
3299 static u_int32_t
3300 pf_tcp_iss(struct pf_pdesc *pd)
3301 {
3302 	MD5_CTX ctx;
3303 	u_int32_t digest[4];
3304 
3305 	if (V_pf_tcp_secret_init == 0) {
3306 		arc4random_buf(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret));
3307 		MD5Init(&V_pf_tcp_secret_ctx);
3308 		MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret,
3309 		    sizeof(V_pf_tcp_secret));
3310 		V_pf_tcp_secret_init = 1;
3311 	}
3312 
3313 	ctx = V_pf_tcp_secret_ctx;
3314 
3315 	MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short));
3316 	MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short));
3317 	if (pd->af == AF_INET6) {
3318 		MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr));
3319 		MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr));
3320 	} else {
3321 		MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr));
3322 		MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr));
3323 	}
3324 	MD5Final((u_char *)digest, &ctx);
3325 	V_pf_tcp_iss_off += 4096;
3326 #define	ISN_RANDOM_INCREMENT (4096 - 1)
3327 	return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) +
3328 	    V_pf_tcp_iss_off);
3329 #undef	ISN_RANDOM_INCREMENT
3330 }
3331 
3332 static int
3333 pf_test_rule(struct pf_krule **rm, struct pf_state **sm, int direction,
3334     struct pfi_kkif *kif, struct mbuf *m, int off, struct pf_pdesc *pd,
3335     struct pf_krule **am, struct pf_kruleset **rsm, struct inpcb *inp)
3336 {
3337 	struct pf_krule		*nr = NULL;
3338 	struct pf_addr		* const saddr = pd->src;
3339 	struct pf_addr		* const daddr = pd->dst;
3340 	sa_family_t		 af = pd->af;
3341 	struct pf_krule		*r, *a = NULL;
3342 	struct pf_kruleset	*ruleset = NULL;
3343 	struct pf_ksrc_node	*nsn = NULL;
3344 	struct tcphdr		*th = pd->hdr.tcp;
3345 	struct pf_state_key	*sk = NULL, *nk = NULL;
3346 	u_short			 reason;
3347 	int			 rewrite = 0, hdrlen = 0;
3348 	int			 tag = -1, rtableid = -1;
3349 	int			 asd = 0;
3350 	int			 match = 0;
3351 	int			 state_icmp = 0;
3352 	u_int16_t		 sport = 0, dport = 0;
3353 	u_int16_t		 bproto_sum = 0, bip_sum = 0;
3354 	u_int8_t		 icmptype = 0, icmpcode = 0;
3355 	struct pf_kanchor_stackframe	anchor_stack[PF_ANCHOR_STACKSIZE];
3356 
3357 	PF_RULES_RASSERT();
3358 
3359 	if (inp != NULL) {
3360 		INP_LOCK_ASSERT(inp);
3361 		pd->lookup.uid = inp->inp_cred->cr_uid;
3362 		pd->lookup.gid = inp->inp_cred->cr_groups[0];
3363 		pd->lookup.done = 1;
3364 	}
3365 
3366 	switch (pd->proto) {
3367 	case IPPROTO_TCP:
3368 		sport = th->th_sport;
3369 		dport = th->th_dport;
3370 		hdrlen = sizeof(*th);
3371 		break;
3372 	case IPPROTO_UDP:
3373 		sport = pd->hdr.udp->uh_sport;
3374 		dport = pd->hdr.udp->uh_dport;
3375 		hdrlen = sizeof(*pd->hdr.udp);
3376 		break;
3377 #ifdef INET
3378 	case IPPROTO_ICMP:
3379 		if (pd->af != AF_INET)
3380 			break;
3381 		sport = dport = pd->hdr.icmp->icmp_id;
3382 		hdrlen = sizeof(*pd->hdr.icmp);
3383 		icmptype = pd->hdr.icmp->icmp_type;
3384 		icmpcode = pd->hdr.icmp->icmp_code;
3385 
3386 		if (icmptype == ICMP_UNREACH ||
3387 		    icmptype == ICMP_SOURCEQUENCH ||
3388 		    icmptype == ICMP_REDIRECT ||
3389 		    icmptype == ICMP_TIMXCEED ||
3390 		    icmptype == ICMP_PARAMPROB)
3391 			state_icmp++;
3392 		break;
3393 #endif /* INET */
3394 #ifdef INET6
3395 	case IPPROTO_ICMPV6:
3396 		if (af != AF_INET6)
3397 			break;
3398 		sport = dport = pd->hdr.icmp6->icmp6_id;
3399 		hdrlen = sizeof(*pd->hdr.icmp6);
3400 		icmptype = pd->hdr.icmp6->icmp6_type;
3401 		icmpcode = pd->hdr.icmp6->icmp6_code;
3402 
3403 		if (icmptype == ICMP6_DST_UNREACH ||
3404 		    icmptype == ICMP6_PACKET_TOO_BIG ||
3405 		    icmptype == ICMP6_TIME_EXCEEDED ||
3406 		    icmptype == ICMP6_PARAM_PROB)
3407 			state_icmp++;
3408 		break;
3409 #endif /* INET6 */
3410 	default:
3411 		sport = dport = hdrlen = 0;
3412 		break;
3413 	}
3414 
3415 	r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3416 
3417 	/* check packet for BINAT/NAT/RDR */
3418 	if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk,
3419 	    &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) {
3420 		KASSERT(sk != NULL, ("%s: null sk", __func__));
3421 		KASSERT(nk != NULL, ("%s: null nk", __func__));
3422 
3423 		if (pd->ip_sum)
3424 			bip_sum = *pd->ip_sum;
3425 
3426 		switch (pd->proto) {
3427 		case IPPROTO_TCP:
3428 			bproto_sum = th->th_sum;
3429 			pd->proto_sum = &th->th_sum;
3430 
3431 			if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) ||
3432 			    nk->port[pd->sidx] != sport) {
3433 				pf_change_ap(m, saddr, &th->th_sport, pd->ip_sum,
3434 				    &th->th_sum, &nk->addr[pd->sidx],
3435 				    nk->port[pd->sidx], 0, af);
3436 				pd->sport = &th->th_sport;
3437 				sport = th->th_sport;
3438 			}
3439 
3440 			if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) ||
3441 			    nk->port[pd->didx] != dport) {
3442 				pf_change_ap(m, daddr, &th->th_dport, pd->ip_sum,
3443 				    &th->th_sum, &nk->addr[pd->didx],
3444 				    nk->port[pd->didx], 0, af);
3445 				dport = th->th_dport;
3446 				pd->dport = &th->th_dport;
3447 			}
3448 			rewrite++;
3449 			break;
3450 		case IPPROTO_UDP:
3451 			bproto_sum = pd->hdr.udp->uh_sum;
3452 			pd->proto_sum = &pd->hdr.udp->uh_sum;
3453 
3454 			if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) ||
3455 			    nk->port[pd->sidx] != sport) {
3456 				pf_change_ap(m, saddr, &pd->hdr.udp->uh_sport,
3457 				    pd->ip_sum, &pd->hdr.udp->uh_sum,
3458 				    &nk->addr[pd->sidx],
3459 				    nk->port[pd->sidx], 1, af);
3460 				sport = pd->hdr.udp->uh_sport;
3461 				pd->sport = &pd->hdr.udp->uh_sport;
3462 			}
3463 
3464 			if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) ||
3465 			    nk->port[pd->didx] != dport) {
3466 				pf_change_ap(m, daddr, &pd->hdr.udp->uh_dport,
3467 				    pd->ip_sum, &pd->hdr.udp->uh_sum,
3468 				    &nk->addr[pd->didx],
3469 				    nk->port[pd->didx], 1, af);
3470 				dport = pd->hdr.udp->uh_dport;
3471 				pd->dport = &pd->hdr.udp->uh_dport;
3472 			}
3473 			rewrite++;
3474 			break;
3475 #ifdef INET
3476 		case IPPROTO_ICMP:
3477 			nk->port[0] = nk->port[1];
3478 			if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET))
3479 				pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3480 				    nk->addr[pd->sidx].v4.s_addr, 0);
3481 
3482 			if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET))
3483 				pf_change_a(&daddr->v4.s_addr, pd->ip_sum,
3484 				    nk->addr[pd->didx].v4.s_addr, 0);
3485 
3486 			if (nk->port[1] != pd->hdr.icmp->icmp_id) {
3487 				pd->hdr.icmp->icmp_cksum = pf_cksum_fixup(
3488 				    pd->hdr.icmp->icmp_cksum, sport,
3489 				    nk->port[1], 0);
3490 				pd->hdr.icmp->icmp_id = nk->port[1];
3491 				pd->sport = &pd->hdr.icmp->icmp_id;
3492 			}
3493 			m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
3494 			break;
3495 #endif /* INET */
3496 #ifdef INET6
3497 		case IPPROTO_ICMPV6:
3498 			nk->port[0] = nk->port[1];
3499 			if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6))
3500 				pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3501 				    &nk->addr[pd->sidx], 0);
3502 
3503 			if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6))
3504 				pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3505 				    &nk->addr[pd->didx], 0);
3506 			rewrite++;
3507 			break;
3508 #endif /* INET */
3509 		default:
3510 			switch (af) {
3511 #ifdef INET
3512 			case AF_INET:
3513 				if (PF_ANEQ(saddr,
3514 				    &nk->addr[pd->sidx], AF_INET))
3515 					pf_change_a(&saddr->v4.s_addr,
3516 					    pd->ip_sum,
3517 					    nk->addr[pd->sidx].v4.s_addr, 0);
3518 
3519 				if (PF_ANEQ(daddr,
3520 				    &nk->addr[pd->didx], AF_INET))
3521 					pf_change_a(&daddr->v4.s_addr,
3522 					    pd->ip_sum,
3523 					    nk->addr[pd->didx].v4.s_addr, 0);
3524 				break;
3525 #endif /* INET */
3526 #ifdef INET6
3527 			case AF_INET6:
3528 				if (PF_ANEQ(saddr,
3529 				    &nk->addr[pd->sidx], AF_INET6))
3530 					PF_ACPY(saddr, &nk->addr[pd->sidx], af);
3531 
3532 				if (PF_ANEQ(daddr,
3533 				    &nk->addr[pd->didx], AF_INET6))
3534 					PF_ACPY(daddr, &nk->addr[pd->didx], af);
3535 				break;
3536 #endif /* INET */
3537 			}
3538 			break;
3539 		}
3540 		if (nr->natpass)
3541 			r = NULL;
3542 		pd->nat_rule = nr;
3543 	}
3544 
3545 	while (r != NULL) {
3546 		counter_u64_add(r->evaluations, 1);
3547 		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
3548 			r = r->skip[PF_SKIP_IFP].ptr;
3549 		else if (r->direction && r->direction != direction)
3550 			r = r->skip[PF_SKIP_DIR].ptr;
3551 		else if (r->af && r->af != af)
3552 			r = r->skip[PF_SKIP_AF].ptr;
3553 		else if (r->proto && r->proto != pd->proto)
3554 			r = r->skip[PF_SKIP_PROTO].ptr;
3555 		else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
3556 		    r->src.neg, kif, M_GETFIB(m)))
3557 			r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3558 		/* tcp/udp only. port_op always 0 in other cases */
3559 		else if (r->src.port_op && !pf_match_port(r->src.port_op,
3560 		    r->src.port[0], r->src.port[1], sport))
3561 			r = r->skip[PF_SKIP_SRC_PORT].ptr;
3562 		else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
3563 		    r->dst.neg, NULL, M_GETFIB(m)))
3564 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
3565 		/* tcp/udp only. port_op always 0 in other cases */
3566 		else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
3567 		    r->dst.port[0], r->dst.port[1], dport))
3568 			r = r->skip[PF_SKIP_DST_PORT].ptr;
3569 		/* icmp only. type always 0 in other cases */
3570 		else if (r->type && r->type != icmptype + 1)
3571 			r = TAILQ_NEXT(r, entries);
3572 		/* icmp only. type always 0 in other cases */
3573 		else if (r->code && r->code != icmpcode + 1)
3574 			r = TAILQ_NEXT(r, entries);
3575 		else if (r->tos && !(r->tos == pd->tos))
3576 			r = TAILQ_NEXT(r, entries);
3577 		else if (r->rule_flag & PFRULE_FRAGMENT)
3578 			r = TAILQ_NEXT(r, entries);
3579 		else if (pd->proto == IPPROTO_TCP &&
3580 		    (r->flagset & th->th_flags) != r->flags)
3581 			r = TAILQ_NEXT(r, entries);
3582 		/* tcp/udp only. uid.op always 0 in other cases */
3583 		else if (r->uid.op && (pd->lookup.done || (pd->lookup.done =
3584 		    pf_socket_lookup(direction, pd, m), 1)) &&
3585 		    !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
3586 		    pd->lookup.uid))
3587 			r = TAILQ_NEXT(r, entries);
3588 		/* tcp/udp only. gid.op always 0 in other cases */
3589 		else if (r->gid.op && (pd->lookup.done || (pd->lookup.done =
3590 		    pf_socket_lookup(direction, pd, m), 1)) &&
3591 		    !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
3592 		    pd->lookup.gid))
3593 			r = TAILQ_NEXT(r, entries);
3594 		else if (r->prio &&
3595 		    !pf_match_ieee8021q_pcp(r->prio, m))
3596 			r = TAILQ_NEXT(r, entries);
3597 		else if (r->prob &&
3598 		    r->prob <= arc4random())
3599 			r = TAILQ_NEXT(r, entries);
3600 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
3601 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
3602 			r = TAILQ_NEXT(r, entries);
3603 		else if (r->os_fingerprint != PF_OSFP_ANY &&
3604 		    (pd->proto != IPPROTO_TCP || !pf_osfp_match(
3605 		    pf_osfp_fingerprint(pd, m, off, th),
3606 		    r->os_fingerprint)))
3607 			r = TAILQ_NEXT(r, entries);
3608 		else {
3609 			if (r->tag)
3610 				tag = r->tag;
3611 			if (r->rtableid >= 0)
3612 				rtableid = r->rtableid;
3613 			if (r->anchor == NULL) {
3614 				match = 1;
3615 				*rm = r;
3616 				*am = a;
3617 				*rsm = ruleset;
3618 				if ((*rm)->quick)
3619 					break;
3620 				r = TAILQ_NEXT(r, entries);
3621 			} else
3622 				pf_step_into_anchor(anchor_stack, &asd,
3623 				    &ruleset, PF_RULESET_FILTER, &r, &a,
3624 				    &match);
3625 		}
3626 		if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd,
3627 		    &ruleset, PF_RULESET_FILTER, &r, &a, &match))
3628 			break;
3629 	}
3630 	r = *rm;
3631 	a = *am;
3632 	ruleset = *rsm;
3633 
3634 	REASON_SET(&reason, PFRES_MATCH);
3635 
3636 	if (r->log || (nr != NULL && nr->log)) {
3637 		if (rewrite)
3638 			m_copyback(m, off, hdrlen, pd->hdr.any);
3639 		PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a,
3640 		    ruleset, pd, 1);
3641 	}
3642 
3643 	if ((r->action == PF_DROP) &&
3644 	    ((r->rule_flag & PFRULE_RETURNRST) ||
3645 	    (r->rule_flag & PFRULE_RETURNICMP) ||
3646 	    (r->rule_flag & PFRULE_RETURN))) {
3647 		pf_return(r, nr, pd, sk, off, m, th, kif, bproto_sum,
3648 		    bip_sum, hdrlen, &reason);
3649 	}
3650 
3651 	if (r->action == PF_DROP)
3652 		goto cleanup;
3653 
3654 	if (tag > 0 && pf_tag_packet(m, pd, tag)) {
3655 		REASON_SET(&reason, PFRES_MEMORY);
3656 		goto cleanup;
3657 	}
3658 	if (rtableid >= 0)
3659 		M_SETFIB(m, rtableid);
3660 
3661 	if (!state_icmp && (r->keep_state || nr != NULL ||
3662 	    (pd->flags & PFDESC_TCP_NORM))) {
3663 		int action;
3664 		action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off,
3665 		    sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum,
3666 		    hdrlen);
3667 		if (action != PF_PASS) {
3668 			if (action == PF_DROP &&
3669 			    (r->rule_flag & PFRULE_RETURN))
3670 				pf_return(r, nr, pd, sk, off, m, th, kif,
3671 				    bproto_sum, bip_sum, hdrlen, &reason);
3672 			return (action);
3673 		}
3674 	} else {
3675 		if (sk != NULL)
3676 			uma_zfree(V_pf_state_key_z, sk);
3677 		if (nk != NULL)
3678 			uma_zfree(V_pf_state_key_z, nk);
3679 	}
3680 
3681 	/* copy back packet headers if we performed NAT operations */
3682 	if (rewrite)
3683 		m_copyback(m, off, hdrlen, pd->hdr.any);
3684 
3685 	if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) &&
3686 	    direction == PF_OUT &&
3687 	    V_pfsync_defer_ptr != NULL && V_pfsync_defer_ptr(*sm, m))
3688 		/*
3689 		 * We want the state created, but we dont
3690 		 * want to send this in case a partner
3691 		 * firewall has to know about it to allow
3692 		 * replies through it.
3693 		 */
3694 		return (PF_DEFER);
3695 
3696 	return (PF_PASS);
3697 
3698 cleanup:
3699 	if (sk != NULL)
3700 		uma_zfree(V_pf_state_key_z, sk);
3701 	if (nk != NULL)
3702 		uma_zfree(V_pf_state_key_z, nk);
3703 	return (PF_DROP);
3704 }
3705 
3706 static int
3707 pf_create_state(struct pf_krule *r, struct pf_krule *nr, struct pf_krule *a,
3708     struct pf_pdesc *pd, struct pf_ksrc_node *nsn, struct pf_state_key *nk,
3709     struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport,
3710     u_int16_t dport, int *rewrite, struct pfi_kkif *kif, struct pf_state **sm,
3711     int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen)
3712 {
3713 	struct pf_state		*s = NULL;
3714 	struct pf_ksrc_node	*sn = NULL;
3715 	struct tcphdr		*th = pd->hdr.tcp;
3716 	u_int16_t		 mss = V_tcp_mssdflt;
3717 	u_short			 reason;
3718 
3719 	/* check maximums */
3720 	if (r->max_states &&
3721 	    (counter_u64_fetch(r->states_cur) >= r->max_states)) {
3722 		counter_u64_add(V_pf_status.lcounters[LCNT_STATES], 1);
3723 		REASON_SET(&reason, PFRES_MAXSTATES);
3724 		goto csfailed;
3725 	}
3726 	/* src node for filter rule */
3727 	if ((r->rule_flag & PFRULE_SRCTRACK ||
3728 	    r->rpool.opts & PF_POOL_STICKYADDR) &&
3729 	    pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) {
3730 		REASON_SET(&reason, PFRES_SRCLIMIT);
3731 		goto csfailed;
3732 	}
3733 	/* src node for translation rule */
3734 	if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3735 	    pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) {
3736 		REASON_SET(&reason, PFRES_SRCLIMIT);
3737 		goto csfailed;
3738 	}
3739 	s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO);
3740 	if (s == NULL) {
3741 		REASON_SET(&reason, PFRES_MEMORY);
3742 		goto csfailed;
3743 	}
3744 	for (int i = 0; i < 2; i++) {
3745 		s->bytes[i] = counter_u64_alloc(M_NOWAIT);
3746 		s->packets[i] = counter_u64_alloc(M_NOWAIT);
3747 
3748 		if (s->bytes[i] == NULL || s->packets[i] == NULL) {
3749 			pf_free_state(s);
3750 			REASON_SET(&reason, PFRES_MEMORY);
3751 			goto csfailed;
3752 		}
3753 	}
3754 	s->rule.ptr = r;
3755 	s->nat_rule.ptr = nr;
3756 	s->anchor.ptr = a;
3757 	STATE_INC_COUNTERS(s);
3758 	if (r->allow_opts)
3759 		s->state_flags |= PFSTATE_ALLOWOPTS;
3760 	if (r->rule_flag & PFRULE_STATESLOPPY)
3761 		s->state_flags |= PFSTATE_SLOPPY;
3762 	s->log = r->log & PF_LOG_ALL;
3763 	s->sync_state = PFSYNC_S_NONE;
3764 	if (nr != NULL)
3765 		s->log |= nr->log & PF_LOG_ALL;
3766 	switch (pd->proto) {
3767 	case IPPROTO_TCP:
3768 		s->src.seqlo = ntohl(th->th_seq);
3769 		s->src.seqhi = s->src.seqlo + pd->p_len + 1;
3770 		if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
3771 		    r->keep_state == PF_STATE_MODULATE) {
3772 			/* Generate sequence number modulator */
3773 			if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) ==
3774 			    0)
3775 				s->src.seqdiff = 1;
3776 			pf_change_proto_a(m, &th->th_seq, &th->th_sum,
3777 			    htonl(s->src.seqlo + s->src.seqdiff), 0);
3778 			*rewrite = 1;
3779 		} else
3780 			s->src.seqdiff = 0;
3781 		if (th->th_flags & TH_SYN) {
3782 			s->src.seqhi++;
3783 			s->src.wscale = pf_get_wscale(m, off,
3784 			    th->th_off, pd->af);
3785 		}
3786 		s->src.max_win = MAX(ntohs(th->th_win), 1);
3787 		if (s->src.wscale & PF_WSCALE_MASK) {
3788 			/* Remove scale factor from initial window */
3789 			int win = s->src.max_win;
3790 			win += 1 << (s->src.wscale & PF_WSCALE_MASK);
3791 			s->src.max_win = (win - 1) >>
3792 			    (s->src.wscale & PF_WSCALE_MASK);
3793 		}
3794 		if (th->th_flags & TH_FIN)
3795 			s->src.seqhi++;
3796 		s->dst.seqhi = 1;
3797 		s->dst.max_win = 1;
3798 		s->src.state = TCPS_SYN_SENT;
3799 		s->dst.state = TCPS_CLOSED;
3800 		s->timeout = PFTM_TCP_FIRST_PACKET;
3801 		break;
3802 	case IPPROTO_UDP:
3803 		s->src.state = PFUDPS_SINGLE;
3804 		s->dst.state = PFUDPS_NO_TRAFFIC;
3805 		s->timeout = PFTM_UDP_FIRST_PACKET;
3806 		break;
3807 	case IPPROTO_ICMP:
3808 #ifdef INET6
3809 	case IPPROTO_ICMPV6:
3810 #endif
3811 		s->timeout = PFTM_ICMP_FIRST_PACKET;
3812 		break;
3813 	default:
3814 		s->src.state = PFOTHERS_SINGLE;
3815 		s->dst.state = PFOTHERS_NO_TRAFFIC;
3816 		s->timeout = PFTM_OTHER_FIRST_PACKET;
3817 	}
3818 
3819 	if (r->rt) {
3820 		if (pf_map_addr(pd->af, r, pd->src, &s->rt_addr, NULL, &sn)) {
3821 			REASON_SET(&reason, PFRES_MAPFAILED);
3822 			pf_src_tree_remove_state(s);
3823 			STATE_DEC_COUNTERS(s);
3824 			uma_zfree(V_pf_state_z, s);
3825 			goto csfailed;
3826 		}
3827 		s->rt_kif = r->rpool.cur->kif;
3828 	}
3829 
3830 	s->creation = time_uptime;
3831 	s->expire = time_uptime;
3832 
3833 	if (sn != NULL)
3834 		s->src_node = sn;
3835 	if (nsn != NULL) {
3836 		/* XXX We only modify one side for now. */
3837 		PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af);
3838 		s->nat_src_node = nsn;
3839 	}
3840 	if (pd->proto == IPPROTO_TCP) {
3841 		if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
3842 		    off, pd, th, &s->src, &s->dst)) {
3843 			REASON_SET(&reason, PFRES_MEMORY);
3844 			pf_src_tree_remove_state(s);
3845 			STATE_DEC_COUNTERS(s);
3846 			uma_zfree(V_pf_state_z, s);
3847 			return (PF_DROP);
3848 		}
3849 		if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
3850 		    pf_normalize_tcp_stateful(m, off, pd, &reason, th, s,
3851 		    &s->src, &s->dst, rewrite)) {
3852 			/* This really shouldn't happen!!! */
3853 			DPFPRINTF(PF_DEBUG_URGENT,
3854 			    ("pf_normalize_tcp_stateful failed on first "
3855 			     "pkt\n"));
3856 			pf_normalize_tcp_cleanup(s);
3857 			pf_src_tree_remove_state(s);
3858 			STATE_DEC_COUNTERS(s);
3859 			uma_zfree(V_pf_state_z, s);
3860 			return (PF_DROP);
3861 		}
3862 	}
3863 	s->direction = pd->dir;
3864 
3865 	/*
3866 	 * sk/nk could already been setup by pf_get_translation().
3867 	 */
3868 	if (nr == NULL) {
3869 		KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p",
3870 		    __func__, nr, sk, nk));
3871 		sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport);
3872 		if (sk == NULL)
3873 			goto csfailed;
3874 		nk = sk;
3875 	} else
3876 		KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p",
3877 		    __func__, nr, sk, nk));
3878 
3879 	/* Swap sk/nk for PF_OUT. */
3880 	if (pf_state_insert(BOUND_IFACE(r, kif),
3881 	    (pd->dir == PF_IN) ? sk : nk,
3882 	    (pd->dir == PF_IN) ? nk : sk, s)) {
3883 		if (pd->proto == IPPROTO_TCP)
3884 			pf_normalize_tcp_cleanup(s);
3885 		REASON_SET(&reason, PFRES_STATEINS);
3886 		pf_src_tree_remove_state(s);
3887 		STATE_DEC_COUNTERS(s);
3888 		uma_zfree(V_pf_state_z, s);
3889 		return (PF_DROP);
3890 	} else
3891 		*sm = s;
3892 
3893 	if (tag > 0)
3894 		s->tag = tag;
3895 	if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) ==
3896 	    TH_SYN && r->keep_state == PF_STATE_SYNPROXY) {
3897 		s->src.state = PF_TCPS_PROXY_SRC;
3898 		/* undo NAT changes, if they have taken place */
3899 		if (nr != NULL) {
3900 			struct pf_state_key *skt = s->key[PF_SK_WIRE];
3901 			if (pd->dir == PF_OUT)
3902 				skt = s->key[PF_SK_STACK];
3903 			PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af);
3904 			PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af);
3905 			if (pd->sport)
3906 				*pd->sport = skt->port[pd->sidx];
3907 			if (pd->dport)
3908 				*pd->dport = skt->port[pd->didx];
3909 			if (pd->proto_sum)
3910 				*pd->proto_sum = bproto_sum;
3911 			if (pd->ip_sum)
3912 				*pd->ip_sum = bip_sum;
3913 			m_copyback(m, off, hdrlen, pd->hdr.any);
3914 		}
3915 		s->src.seqhi = htonl(arc4random());
3916 		/* Find mss option */
3917 		int rtid = M_GETFIB(m);
3918 		mss = pf_get_mss(m, off, th->th_off, pd->af);
3919 		mss = pf_calc_mss(pd->src, pd->af, rtid, mss);
3920 		mss = pf_calc_mss(pd->dst, pd->af, rtid, mss);
3921 		s->src.mss = mss;
3922 		pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport,
3923 		    th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
3924 		    TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL);
3925 		REASON_SET(&reason, PFRES_SYNPROXY);
3926 		return (PF_SYNPROXY_DROP);
3927 	}
3928 
3929 	return (PF_PASS);
3930 
3931 csfailed:
3932 	if (sk != NULL)
3933 		uma_zfree(V_pf_state_key_z, sk);
3934 	if (nk != NULL)
3935 		uma_zfree(V_pf_state_key_z, nk);
3936 
3937 	if (sn != NULL) {
3938 		struct pf_srchash *sh;
3939 
3940 		sh = &V_pf_srchash[pf_hashsrc(&sn->addr, sn->af)];
3941 		PF_HASHROW_LOCK(sh);
3942 		if (--sn->states == 0 && sn->expire == 0) {
3943 			pf_unlink_src_node(sn);
3944 			uma_zfree(V_pf_sources_z, sn);
3945 			counter_u64_add(
3946 			    V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1);
3947 		}
3948 		PF_HASHROW_UNLOCK(sh);
3949 	}
3950 
3951 	if (nsn != sn && nsn != NULL) {
3952 		struct pf_srchash *sh;
3953 
3954 		sh = &V_pf_srchash[pf_hashsrc(&nsn->addr, nsn->af)];
3955 		PF_HASHROW_LOCK(sh);
3956 		if (--nsn->states == 0 && nsn->expire == 0) {
3957 			pf_unlink_src_node(nsn);
3958 			uma_zfree(V_pf_sources_z, nsn);
3959 			counter_u64_add(
3960 			    V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1);
3961 		}
3962 		PF_HASHROW_UNLOCK(sh);
3963 	}
3964 
3965 	return (PF_DROP);
3966 }
3967 
3968 static int
3969 pf_test_fragment(struct pf_krule **rm, int direction, struct pfi_kkif *kif,
3970     struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_krule **am,
3971     struct pf_kruleset **rsm)
3972 {
3973 	struct pf_krule		*r, *a = NULL;
3974 	struct pf_kruleset	*ruleset = NULL;
3975 	sa_family_t		 af = pd->af;
3976 	u_short			 reason;
3977 	int			 tag = -1;
3978 	int			 asd = 0;
3979 	int			 match = 0;
3980 	struct pf_kanchor_stackframe	anchor_stack[PF_ANCHOR_STACKSIZE];
3981 
3982 	PF_RULES_RASSERT();
3983 
3984 	r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3985 	while (r != NULL) {
3986 		counter_u64_add(r->evaluations, 1);
3987 		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
3988 			r = r->skip[PF_SKIP_IFP].ptr;
3989 		else if (r->direction && r->direction != direction)
3990 			r = r->skip[PF_SKIP_DIR].ptr;
3991 		else if (r->af && r->af != af)
3992 			r = r->skip[PF_SKIP_AF].ptr;
3993 		else if (r->proto && r->proto != pd->proto)
3994 			r = r->skip[PF_SKIP_PROTO].ptr;
3995 		else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
3996 		    r->src.neg, kif, M_GETFIB(m)))
3997 			r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3998 		else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
3999 		    r->dst.neg, NULL, M_GETFIB(m)))
4000 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
4001 		else if (r->tos && !(r->tos == pd->tos))
4002 			r = TAILQ_NEXT(r, entries);
4003 		else if (r->os_fingerprint != PF_OSFP_ANY)
4004 			r = TAILQ_NEXT(r, entries);
4005 		else if (pd->proto == IPPROTO_UDP &&
4006 		    (r->src.port_op || r->dst.port_op))
4007 			r = TAILQ_NEXT(r, entries);
4008 		else if (pd->proto == IPPROTO_TCP &&
4009 		    (r->src.port_op || r->dst.port_op || r->flagset))
4010 			r = TAILQ_NEXT(r, entries);
4011 		else if ((pd->proto == IPPROTO_ICMP ||
4012 		    pd->proto == IPPROTO_ICMPV6) &&
4013 		    (r->type || r->code))
4014 			r = TAILQ_NEXT(r, entries);
4015 		else if (r->prio &&
4016 		    !pf_match_ieee8021q_pcp(r->prio, m))
4017 			r = TAILQ_NEXT(r, entries);
4018 		else if (r->prob && r->prob <=
4019 		    (arc4random() % (UINT_MAX - 1) + 1))
4020 			r = TAILQ_NEXT(r, entries);
4021 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
4022 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
4023 			r = TAILQ_NEXT(r, entries);
4024 		else {
4025 			if (r->anchor == NULL) {
4026 				match = 1;
4027 				*rm = r;
4028 				*am = a;
4029 				*rsm = ruleset;
4030 				if ((*rm)->quick)
4031 					break;
4032 				r = TAILQ_NEXT(r, entries);
4033 			} else
4034 				pf_step_into_anchor(anchor_stack, &asd,
4035 				    &ruleset, PF_RULESET_FILTER, &r, &a,
4036 				    &match);
4037 		}
4038 		if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd,
4039 		    &ruleset, PF_RULESET_FILTER, &r, &a, &match))
4040 			break;
4041 	}
4042 	r = *rm;
4043 	a = *am;
4044 	ruleset = *rsm;
4045 
4046 	REASON_SET(&reason, PFRES_MATCH);
4047 
4048 	if (r->log)
4049 		PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd,
4050 		    1);
4051 
4052 	if (r->action != PF_PASS)
4053 		return (PF_DROP);
4054 
4055 	if (tag > 0 && pf_tag_packet(m, pd, tag)) {
4056 		REASON_SET(&reason, PFRES_MEMORY);
4057 		return (PF_DROP);
4058 	}
4059 
4060 	return (PF_PASS);
4061 }
4062 
4063 static int
4064 pf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst,
4065 	struct pf_state **state, struct pfi_kkif *kif, struct mbuf *m, int off,
4066 	struct pf_pdesc *pd, u_short *reason, int *copyback)
4067 {
4068 	struct tcphdr		*th = pd->hdr.tcp;
4069 	u_int16_t		 win = ntohs(th->th_win);
4070 	u_int32_t		 ack, end, seq, orig_seq;
4071 	u_int8_t		 sws, dws;
4072 	int			 ackskew;
4073 
4074 	if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
4075 		sws = src->wscale & PF_WSCALE_MASK;
4076 		dws = dst->wscale & PF_WSCALE_MASK;
4077 	} else
4078 		sws = dws = 0;
4079 
4080 	/*
4081 	 * Sequence tracking algorithm from Guido van Rooij's paper:
4082 	 *   http://www.madison-gurkha.com/publications/tcp_filtering/
4083 	 *	tcp_filtering.ps
4084 	 */
4085 
4086 	orig_seq = seq = ntohl(th->th_seq);
4087 	if (src->seqlo == 0) {
4088 		/* First packet from this end. Set its state */
4089 
4090 		if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
4091 		    src->scrub == NULL) {
4092 			if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
4093 				REASON_SET(reason, PFRES_MEMORY);
4094 				return (PF_DROP);
4095 			}
4096 		}
4097 
4098 		/* Deferred generation of sequence number modulator */
4099 		if (dst->seqdiff && !src->seqdiff) {
4100 			/* use random iss for the TCP server */
4101 			while ((src->seqdiff = arc4random() - seq) == 0)
4102 				;
4103 			ack = ntohl(th->th_ack) - dst->seqdiff;
4104 			pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq +
4105 			    src->seqdiff), 0);
4106 			pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0);
4107 			*copyback = 1;
4108 		} else {
4109 			ack = ntohl(th->th_ack);
4110 		}
4111 
4112 		end = seq + pd->p_len;
4113 		if (th->th_flags & TH_SYN) {
4114 			end++;
4115 			if (dst->wscale & PF_WSCALE_FLAG) {
4116 				src->wscale = pf_get_wscale(m, off, th->th_off,
4117 				    pd->af);
4118 				if (src->wscale & PF_WSCALE_FLAG) {
4119 					/* Remove scale factor from initial
4120 					 * window */
4121 					sws = src->wscale & PF_WSCALE_MASK;
4122 					win = ((u_int32_t)win + (1 << sws) - 1)
4123 					    >> sws;
4124 					dws = dst->wscale & PF_WSCALE_MASK;
4125 				} else {
4126 					/* fixup other window */
4127 					dst->max_win <<= dst->wscale &
4128 					    PF_WSCALE_MASK;
4129 					/* in case of a retrans SYN|ACK */
4130 					dst->wscale = 0;
4131 				}
4132 			}
4133 		}
4134 		if (th->th_flags & TH_FIN)
4135 			end++;
4136 
4137 		src->seqlo = seq;
4138 		if (src->state < TCPS_SYN_SENT)
4139 			src->state = TCPS_SYN_SENT;
4140 
4141 		/*
4142 		 * May need to slide the window (seqhi may have been set by
4143 		 * the crappy stack check or if we picked up the connection
4144 		 * after establishment)
4145 		 */
4146 		if (src->seqhi == 1 ||
4147 		    SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
4148 			src->seqhi = end + MAX(1, dst->max_win << dws);
4149 		if (win > src->max_win)
4150 			src->max_win = win;
4151 
4152 	} else {
4153 		ack = ntohl(th->th_ack) - dst->seqdiff;
4154 		if (src->seqdiff) {
4155 			/* Modulate sequence numbers */
4156 			pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq +
4157 			    src->seqdiff), 0);
4158 			pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0);
4159 			*copyback = 1;
4160 		}
4161 		end = seq + pd->p_len;
4162 		if (th->th_flags & TH_SYN)
4163 			end++;
4164 		if (th->th_flags & TH_FIN)
4165 			end++;
4166 	}
4167 
4168 	if ((th->th_flags & TH_ACK) == 0) {
4169 		/* Let it pass through the ack skew check */
4170 		ack = dst->seqlo;
4171 	} else if ((ack == 0 &&
4172 	    (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
4173 	    /* broken tcp stacks do not set ack */
4174 	    (dst->state < TCPS_SYN_SENT)) {
4175 		/*
4176 		 * Many stacks (ours included) will set the ACK number in an
4177 		 * FIN|ACK if the SYN times out -- no sequence to ACK.
4178 		 */
4179 		ack = dst->seqlo;
4180 	}
4181 
4182 	if (seq == end) {
4183 		/* Ease sequencing restrictions on no data packets */
4184 		seq = src->seqlo;
4185 		end = seq;
4186 	}
4187 
4188 	ackskew = dst->seqlo - ack;
4189 
4190 	/*
4191 	 * Need to demodulate the sequence numbers in any TCP SACK options
4192 	 * (Selective ACK). We could optionally validate the SACK values
4193 	 * against the current ACK window, either forwards or backwards, but
4194 	 * I'm not confident that SACK has been implemented properly
4195 	 * everywhere. It wouldn't surprise me if several stacks accidentally
4196 	 * SACK too far backwards of previously ACKed data. There really aren't
4197 	 * any security implications of bad SACKing unless the target stack
4198 	 * doesn't validate the option length correctly. Someone trying to
4199 	 * spoof into a TCP connection won't bother blindly sending SACK
4200 	 * options anyway.
4201 	 */
4202 	if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) {
4203 		if (pf_modulate_sack(m, off, pd, th, dst))
4204 			*copyback = 1;
4205 	}
4206 
4207 #define	MAXACKWINDOW (0xffff + 1500)	/* 1500 is an arbitrary fudge factor */
4208 	if (SEQ_GEQ(src->seqhi, end) &&
4209 	    /* Last octet inside other's window space */
4210 	    SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
4211 	    /* Retrans: not more than one window back */
4212 	    (ackskew >= -MAXACKWINDOW) &&
4213 	    /* Acking not more than one reassembled fragment backwards */
4214 	    (ackskew <= (MAXACKWINDOW << sws)) &&
4215 	    /* Acking not more than one window forward */
4216 	    ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo ||
4217 	    (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) ||
4218 	    (pd->flags & PFDESC_IP_REAS) == 0)) {
4219 	    /* Require an exact/+1 sequence match on resets when possible */
4220 
4221 		if (dst->scrub || src->scrub) {
4222 			if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4223 			    *state, src, dst, copyback))
4224 				return (PF_DROP);
4225 		}
4226 
4227 		/* update max window */
4228 		if (src->max_win < win)
4229 			src->max_win = win;
4230 		/* synchronize sequencing */
4231 		if (SEQ_GT(end, src->seqlo))
4232 			src->seqlo = end;
4233 		/* slide the window of what the other end can send */
4234 		if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4235 			dst->seqhi = ack + MAX((win << sws), 1);
4236 
4237 		/* update states */
4238 		if (th->th_flags & TH_SYN)
4239 			if (src->state < TCPS_SYN_SENT)
4240 				src->state = TCPS_SYN_SENT;
4241 		if (th->th_flags & TH_FIN)
4242 			if (src->state < TCPS_CLOSING)
4243 				src->state = TCPS_CLOSING;
4244 		if (th->th_flags & TH_ACK) {
4245 			if (dst->state == TCPS_SYN_SENT) {
4246 				dst->state = TCPS_ESTABLISHED;
4247 				if (src->state == TCPS_ESTABLISHED &&
4248 				    (*state)->src_node != NULL &&
4249 				    pf_src_connlimit(state)) {
4250 					REASON_SET(reason, PFRES_SRCLIMIT);
4251 					return (PF_DROP);
4252 				}
4253 			} else if (dst->state == TCPS_CLOSING)
4254 				dst->state = TCPS_FIN_WAIT_2;
4255 		}
4256 		if (th->th_flags & TH_RST)
4257 			src->state = dst->state = TCPS_TIME_WAIT;
4258 
4259 		/* update expire time */
4260 		(*state)->expire = time_uptime;
4261 		if (src->state >= TCPS_FIN_WAIT_2 &&
4262 		    dst->state >= TCPS_FIN_WAIT_2)
4263 			(*state)->timeout = PFTM_TCP_CLOSED;
4264 		else if (src->state >= TCPS_CLOSING &&
4265 		    dst->state >= TCPS_CLOSING)
4266 			(*state)->timeout = PFTM_TCP_FIN_WAIT;
4267 		else if (src->state < TCPS_ESTABLISHED ||
4268 		    dst->state < TCPS_ESTABLISHED)
4269 			(*state)->timeout = PFTM_TCP_OPENING;
4270 		else if (src->state >= TCPS_CLOSING ||
4271 		    dst->state >= TCPS_CLOSING)
4272 			(*state)->timeout = PFTM_TCP_CLOSING;
4273 		else
4274 			(*state)->timeout = PFTM_TCP_ESTABLISHED;
4275 
4276 		/* Fall through to PASS packet */
4277 
4278 	} else if ((dst->state < TCPS_SYN_SENT ||
4279 		dst->state >= TCPS_FIN_WAIT_2 ||
4280 		src->state >= TCPS_FIN_WAIT_2) &&
4281 	    SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
4282 	    /* Within a window forward of the originating packet */
4283 	    SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
4284 	    /* Within a window backward of the originating packet */
4285 
4286 		/*
4287 		 * This currently handles three situations:
4288 		 *  1) Stupid stacks will shotgun SYNs before their peer
4289 		 *     replies.
4290 		 *  2) When PF catches an already established stream (the
4291 		 *     firewall rebooted, the state table was flushed, routes
4292 		 *     changed...)
4293 		 *  3) Packets get funky immediately after the connection
4294 		 *     closes (this should catch Solaris spurious ACK|FINs
4295 		 *     that web servers like to spew after a close)
4296 		 *
4297 		 * This must be a little more careful than the above code
4298 		 * since packet floods will also be caught here. We don't
4299 		 * update the TTL here to mitigate the damage of a packet
4300 		 * flood and so the same code can handle awkward establishment
4301 		 * and a loosened connection close.
4302 		 * In the establishment case, a correct peer response will
4303 		 * validate the connection, go through the normal state code
4304 		 * and keep updating the state TTL.
4305 		 */
4306 
4307 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
4308 			printf("pf: loose state match: ");
4309 			pf_print_state(*state);
4310 			pf_print_flags(th->th_flags);
4311 			printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4312 			    "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack,
4313 			    pd->p_len, ackskew,
4314 			    (unsigned long long)counter_u64_fetch((*state)->packets[0]),
4315 			    (unsigned long long)counter_u64_fetch((*state)->packets[1]),
4316 			    pd->dir == PF_IN ? "in" : "out",
4317 			    pd->dir == (*state)->direction ? "fwd" : "rev");
4318 		}
4319 
4320 		if (dst->scrub || src->scrub) {
4321 			if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4322 			    *state, src, dst, copyback))
4323 				return (PF_DROP);
4324 		}
4325 
4326 		/* update max window */
4327 		if (src->max_win < win)
4328 			src->max_win = win;
4329 		/* synchronize sequencing */
4330 		if (SEQ_GT(end, src->seqlo))
4331 			src->seqlo = end;
4332 		/* slide the window of what the other end can send */
4333 		if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4334 			dst->seqhi = ack + MAX((win << sws), 1);
4335 
4336 		/*
4337 		 * Cannot set dst->seqhi here since this could be a shotgunned
4338 		 * SYN and not an already established connection.
4339 		 */
4340 
4341 		if (th->th_flags & TH_FIN)
4342 			if (src->state < TCPS_CLOSING)
4343 				src->state = TCPS_CLOSING;
4344 		if (th->th_flags & TH_RST)
4345 			src->state = dst->state = TCPS_TIME_WAIT;
4346 
4347 		/* Fall through to PASS packet */
4348 
4349 	} else {
4350 		if ((*state)->dst.state == TCPS_SYN_SENT &&
4351 		    (*state)->src.state == TCPS_SYN_SENT) {
4352 			/* Send RST for state mismatches during handshake */
4353 			if (!(th->th_flags & TH_RST))
4354 				pf_send_tcp(NULL, (*state)->rule.ptr, pd->af,
4355 				    pd->dst, pd->src, th->th_dport,
4356 				    th->th_sport, ntohl(th->th_ack), 0,
4357 				    TH_RST, 0, 0,
4358 				    (*state)->rule.ptr->return_ttl, 1, 0,
4359 				    kif->pfik_ifp);
4360 			src->seqlo = 0;
4361 			src->seqhi = 1;
4362 			src->max_win = 1;
4363 		} else if (V_pf_status.debug >= PF_DEBUG_MISC) {
4364 			printf("pf: BAD state: ");
4365 			pf_print_state(*state);
4366 			pf_print_flags(th->th_flags);
4367 			printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4368 			    "pkts=%llu:%llu dir=%s,%s\n",
4369 			    seq, orig_seq, ack, pd->p_len, ackskew,
4370 			    (unsigned long long)counter_u64_fetch((*state)->packets[0]),
4371 			    (unsigned long long)counter_u64_fetch((*state)->packets[1]),
4372 			    pd->dir == PF_IN ? "in" : "out",
4373 			    pd->dir == (*state)->direction ? "fwd" : "rev");
4374 			printf("pf: State failure on: %c %c %c %c | %c %c\n",
4375 			    SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
4376 			    SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
4377 			    ' ': '2',
4378 			    (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
4379 			    (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
4380 			    SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
4381 			    SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
4382 		}
4383 		REASON_SET(reason, PFRES_BADSTATE);
4384 		return (PF_DROP);
4385 	}
4386 
4387 	return (PF_PASS);
4388 }
4389 
4390 static int
4391 pf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst,
4392 	struct pf_state **state, struct pf_pdesc *pd, u_short *reason)
4393 {
4394 	struct tcphdr		*th = pd->hdr.tcp;
4395 
4396 	if (th->th_flags & TH_SYN)
4397 		if (src->state < TCPS_SYN_SENT)
4398 			src->state = TCPS_SYN_SENT;
4399 	if (th->th_flags & TH_FIN)
4400 		if (src->state < TCPS_CLOSING)
4401 			src->state = TCPS_CLOSING;
4402 	if (th->th_flags & TH_ACK) {
4403 		if (dst->state == TCPS_SYN_SENT) {
4404 			dst->state = TCPS_ESTABLISHED;
4405 			if (src->state == TCPS_ESTABLISHED &&
4406 			    (*state)->src_node != NULL &&
4407 			    pf_src_connlimit(state)) {
4408 				REASON_SET(reason, PFRES_SRCLIMIT);
4409 				return (PF_DROP);
4410 			}
4411 		} else if (dst->state == TCPS_CLOSING) {
4412 			dst->state = TCPS_FIN_WAIT_2;
4413 		} else if (src->state == TCPS_SYN_SENT &&
4414 		    dst->state < TCPS_SYN_SENT) {
4415 			/*
4416 			 * Handle a special sloppy case where we only see one
4417 			 * half of the connection. If there is a ACK after
4418 			 * the initial SYN without ever seeing a packet from
4419 			 * the destination, set the connection to established.
4420 			 */
4421 			dst->state = src->state = TCPS_ESTABLISHED;
4422 			if ((*state)->src_node != NULL &&
4423 			    pf_src_connlimit(state)) {
4424 				REASON_SET(reason, PFRES_SRCLIMIT);
4425 				return (PF_DROP);
4426 			}
4427 		} else if (src->state == TCPS_CLOSING &&
4428 		    dst->state == TCPS_ESTABLISHED &&
4429 		    dst->seqlo == 0) {
4430 			/*
4431 			 * Handle the closing of half connections where we
4432 			 * don't see the full bidirectional FIN/ACK+ACK
4433 			 * handshake.
4434 			 */
4435 			dst->state = TCPS_CLOSING;
4436 		}
4437 	}
4438 	if (th->th_flags & TH_RST)
4439 		src->state = dst->state = TCPS_TIME_WAIT;
4440 
4441 	/* update expire time */
4442 	(*state)->expire = time_uptime;
4443 	if (src->state >= TCPS_FIN_WAIT_2 &&
4444 	    dst->state >= TCPS_FIN_WAIT_2)
4445 		(*state)->timeout = PFTM_TCP_CLOSED;
4446 	else if (src->state >= TCPS_CLOSING &&
4447 	    dst->state >= TCPS_CLOSING)
4448 		(*state)->timeout = PFTM_TCP_FIN_WAIT;
4449 	else if (src->state < TCPS_ESTABLISHED ||
4450 	    dst->state < TCPS_ESTABLISHED)
4451 		(*state)->timeout = PFTM_TCP_OPENING;
4452 	else if (src->state >= TCPS_CLOSING ||
4453 	    dst->state >= TCPS_CLOSING)
4454 		(*state)->timeout = PFTM_TCP_CLOSING;
4455 	else
4456 		(*state)->timeout = PFTM_TCP_ESTABLISHED;
4457 
4458 	return (PF_PASS);
4459 }
4460 
4461 static int
4462 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kkif *kif,
4463     struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
4464     u_short *reason)
4465 {
4466 	struct pf_state_key_cmp	 key;
4467 	struct tcphdr		*th = pd->hdr.tcp;
4468 	int			 copyback = 0;
4469 	struct pf_state_peer	*src, *dst;
4470 	struct pf_state_key	*sk;
4471 
4472 	bzero(&key, sizeof(key));
4473 	key.af = pd->af;
4474 	key.proto = IPPROTO_TCP;
4475 	if (direction == PF_IN)	{	/* wire side, straight */
4476 		PF_ACPY(&key.addr[0], pd->src, key.af);
4477 		PF_ACPY(&key.addr[1], pd->dst, key.af);
4478 		key.port[0] = th->th_sport;
4479 		key.port[1] = th->th_dport;
4480 	} else {			/* stack side, reverse */
4481 		PF_ACPY(&key.addr[1], pd->src, key.af);
4482 		PF_ACPY(&key.addr[0], pd->dst, key.af);
4483 		key.port[1] = th->th_sport;
4484 		key.port[0] = th->th_dport;
4485 	}
4486 
4487 	STATE_LOOKUP(kif, &key, direction, *state, pd);
4488 
4489 	if (direction == (*state)->direction) {
4490 		src = &(*state)->src;
4491 		dst = &(*state)->dst;
4492 	} else {
4493 		src = &(*state)->dst;
4494 		dst = &(*state)->src;
4495 	}
4496 
4497 	sk = (*state)->key[pd->didx];
4498 
4499 	if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
4500 		if (direction != (*state)->direction) {
4501 			REASON_SET(reason, PFRES_SYNPROXY);
4502 			return (PF_SYNPROXY_DROP);
4503 		}
4504 		if (th->th_flags & TH_SYN) {
4505 			if (ntohl(th->th_seq) != (*state)->src.seqlo) {
4506 				REASON_SET(reason, PFRES_SYNPROXY);
4507 				return (PF_DROP);
4508 			}
4509 			pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst,
4510 			    pd->src, th->th_dport, th->th_sport,
4511 			    (*state)->src.seqhi, ntohl(th->th_seq) + 1,
4512 			    TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL);
4513 			REASON_SET(reason, PFRES_SYNPROXY);
4514 			return (PF_SYNPROXY_DROP);
4515 		} else if ((th->th_flags & (TH_ACK|TH_RST|TH_FIN)) != TH_ACK ||
4516 		    (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
4517 		    (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
4518 			REASON_SET(reason, PFRES_SYNPROXY);
4519 			return (PF_DROP);
4520 		} else if ((*state)->src_node != NULL &&
4521 		    pf_src_connlimit(state)) {
4522 			REASON_SET(reason, PFRES_SRCLIMIT);
4523 			return (PF_DROP);
4524 		} else
4525 			(*state)->src.state = PF_TCPS_PROXY_DST;
4526 	}
4527 	if ((*state)->src.state == PF_TCPS_PROXY_DST) {
4528 		if (direction == (*state)->direction) {
4529 			if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
4530 			    (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
4531 			    (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
4532 				REASON_SET(reason, PFRES_SYNPROXY);
4533 				return (PF_DROP);
4534 			}
4535 			(*state)->src.max_win = MAX(ntohs(th->th_win), 1);
4536 			if ((*state)->dst.seqhi == 1)
4537 				(*state)->dst.seqhi = htonl(arc4random());
4538 			pf_send_tcp(NULL, (*state)->rule.ptr, pd->af,
4539 			    &sk->addr[pd->sidx], &sk->addr[pd->didx],
4540 			    sk->port[pd->sidx], sk->port[pd->didx],
4541 			    (*state)->dst.seqhi, 0, TH_SYN, 0,
4542 			    (*state)->src.mss, 0, 0, (*state)->tag, NULL);
4543 			REASON_SET(reason, PFRES_SYNPROXY);
4544 			return (PF_SYNPROXY_DROP);
4545 		} else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
4546 		    (TH_SYN|TH_ACK)) ||
4547 		    (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) {
4548 			REASON_SET(reason, PFRES_SYNPROXY);
4549 			return (PF_DROP);
4550 		} else {
4551 			(*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
4552 			(*state)->dst.seqlo = ntohl(th->th_seq);
4553 			pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst,
4554 			    pd->src, th->th_dport, th->th_sport,
4555 			    ntohl(th->th_ack), ntohl(th->th_seq) + 1,
4556 			    TH_ACK, (*state)->src.max_win, 0, 0, 0,
4557 			    (*state)->tag, NULL);
4558 			pf_send_tcp(NULL, (*state)->rule.ptr, pd->af,
4559 			    &sk->addr[pd->sidx], &sk->addr[pd->didx],
4560 			    sk->port[pd->sidx], sk->port[pd->didx],
4561 			    (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
4562 			    TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL);
4563 			(*state)->src.seqdiff = (*state)->dst.seqhi -
4564 			    (*state)->src.seqlo;
4565 			(*state)->dst.seqdiff = (*state)->src.seqhi -
4566 			    (*state)->dst.seqlo;
4567 			(*state)->src.seqhi = (*state)->src.seqlo +
4568 			    (*state)->dst.max_win;
4569 			(*state)->dst.seqhi = (*state)->dst.seqlo +
4570 			    (*state)->src.max_win;
4571 			(*state)->src.wscale = (*state)->dst.wscale = 0;
4572 			(*state)->src.state = (*state)->dst.state =
4573 			    TCPS_ESTABLISHED;
4574 			REASON_SET(reason, PFRES_SYNPROXY);
4575 			return (PF_SYNPROXY_DROP);
4576 		}
4577 	}
4578 
4579 	if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) &&
4580 	    dst->state >= TCPS_FIN_WAIT_2 &&
4581 	    src->state >= TCPS_FIN_WAIT_2) {
4582 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
4583 			printf("pf: state reuse ");
4584 			pf_print_state(*state);
4585 			pf_print_flags(th->th_flags);
4586 			printf("\n");
4587 		}
4588 		/* XXX make sure it's the same direction ?? */
4589 		(*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
4590 		pf_unlink_state(*state, PF_ENTER_LOCKED);
4591 		*state = NULL;
4592 		return (PF_DROP);
4593 	}
4594 
4595 	if ((*state)->state_flags & PFSTATE_SLOPPY) {
4596 		if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP)
4597 			return (PF_DROP);
4598 	} else {
4599 		if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason,
4600 		    &copyback) == PF_DROP)
4601 			return (PF_DROP);
4602 	}
4603 
4604 	/* translate source/destination address, if necessary */
4605 	if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
4606 		struct pf_state_key *nk = (*state)->key[pd->didx];
4607 
4608 		if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) ||
4609 		    nk->port[pd->sidx] != th->th_sport)
4610 			pf_change_ap(m, pd->src, &th->th_sport,
4611 			    pd->ip_sum, &th->th_sum, &nk->addr[pd->sidx],
4612 			    nk->port[pd->sidx], 0, pd->af);
4613 
4614 		if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) ||
4615 		    nk->port[pd->didx] != th->th_dport)
4616 			pf_change_ap(m, pd->dst, &th->th_dport,
4617 			    pd->ip_sum, &th->th_sum, &nk->addr[pd->didx],
4618 			    nk->port[pd->didx], 0, pd->af);
4619 		copyback = 1;
4620 	}
4621 
4622 	/* Copyback sequence modulation or stateful scrub changes if needed */
4623 	if (copyback)
4624 		m_copyback(m, off, sizeof(*th), (caddr_t)th);
4625 
4626 	return (PF_PASS);
4627 }
4628 
4629 static int
4630 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kkif *kif,
4631     struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4632 {
4633 	struct pf_state_peer	*src, *dst;
4634 	struct pf_state_key_cmp	 key;
4635 	struct udphdr		*uh = pd->hdr.udp;
4636 
4637 	bzero(&key, sizeof(key));
4638 	key.af = pd->af;
4639 	key.proto = IPPROTO_UDP;
4640 	if (direction == PF_IN)	{	/* wire side, straight */
4641 		PF_ACPY(&key.addr[0], pd->src, key.af);
4642 		PF_ACPY(&key.addr[1], pd->dst, key.af);
4643 		key.port[0] = uh->uh_sport;
4644 		key.port[1] = uh->uh_dport;
4645 	} else {			/* stack side, reverse */
4646 		PF_ACPY(&key.addr[1], pd->src, key.af);
4647 		PF_ACPY(&key.addr[0], pd->dst, key.af);
4648 		key.port[1] = uh->uh_sport;
4649 		key.port[0] = uh->uh_dport;
4650 	}
4651 
4652 	STATE_LOOKUP(kif, &key, direction, *state, pd);
4653 
4654 	if (direction == (*state)->direction) {
4655 		src = &(*state)->src;
4656 		dst = &(*state)->dst;
4657 	} else {
4658 		src = &(*state)->dst;
4659 		dst = &(*state)->src;
4660 	}
4661 
4662 	/* update states */
4663 	if (src->state < PFUDPS_SINGLE)
4664 		src->state = PFUDPS_SINGLE;
4665 	if (dst->state == PFUDPS_SINGLE)
4666 		dst->state = PFUDPS_MULTIPLE;
4667 
4668 	/* update expire time */
4669 	(*state)->expire = time_uptime;
4670 	if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
4671 		(*state)->timeout = PFTM_UDP_MULTIPLE;
4672 	else
4673 		(*state)->timeout = PFTM_UDP_SINGLE;
4674 
4675 	/* translate source/destination address, if necessary */
4676 	if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
4677 		struct pf_state_key *nk = (*state)->key[pd->didx];
4678 
4679 		if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) ||
4680 		    nk->port[pd->sidx] != uh->uh_sport)
4681 			pf_change_ap(m, pd->src, &uh->uh_sport, pd->ip_sum,
4682 			    &uh->uh_sum, &nk->addr[pd->sidx],
4683 			    nk->port[pd->sidx], 1, pd->af);
4684 
4685 		if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) ||
4686 		    nk->port[pd->didx] != uh->uh_dport)
4687 			pf_change_ap(m, pd->dst, &uh->uh_dport, pd->ip_sum,
4688 			    &uh->uh_sum, &nk->addr[pd->didx],
4689 			    nk->port[pd->didx], 1, pd->af);
4690 		m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
4691 	}
4692 
4693 	return (PF_PASS);
4694 }
4695 
4696 static int
4697 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kkif *kif,
4698     struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason)
4699 {
4700 	struct pf_addr  *saddr = pd->src, *daddr = pd->dst;
4701 	u_int16_t	 icmpid = 0, *icmpsum;
4702 	u_int8_t	 icmptype, icmpcode;
4703 	int		 state_icmp = 0;
4704 	struct pf_state_key_cmp key;
4705 
4706 	bzero(&key, sizeof(key));
4707 	switch (pd->proto) {
4708 #ifdef INET
4709 	case IPPROTO_ICMP:
4710 		icmptype = pd->hdr.icmp->icmp_type;
4711 		icmpcode = pd->hdr.icmp->icmp_code;
4712 		icmpid = pd->hdr.icmp->icmp_id;
4713 		icmpsum = &pd->hdr.icmp->icmp_cksum;
4714 
4715 		if (icmptype == ICMP_UNREACH ||
4716 		    icmptype == ICMP_SOURCEQUENCH ||
4717 		    icmptype == ICMP_REDIRECT ||
4718 		    icmptype == ICMP_TIMXCEED ||
4719 		    icmptype == ICMP_PARAMPROB)
4720 			state_icmp++;
4721 		break;
4722 #endif /* INET */
4723 #ifdef INET6
4724 	case IPPROTO_ICMPV6:
4725 		icmptype = pd->hdr.icmp6->icmp6_type;
4726 		icmpcode = pd->hdr.icmp6->icmp6_code;
4727 		icmpid = pd->hdr.icmp6->icmp6_id;
4728 		icmpsum = &pd->hdr.icmp6->icmp6_cksum;
4729 
4730 		if (icmptype == ICMP6_DST_UNREACH ||
4731 		    icmptype == ICMP6_PACKET_TOO_BIG ||
4732 		    icmptype == ICMP6_TIME_EXCEEDED ||
4733 		    icmptype == ICMP6_PARAM_PROB)
4734 			state_icmp++;
4735 		break;
4736 #endif /* INET6 */
4737 	}
4738 
4739 	if (!state_icmp) {
4740 		/*
4741 		 * ICMP query/reply message not related to a TCP/UDP packet.
4742 		 * Search for an ICMP state.
4743 		 */
4744 		key.af = pd->af;
4745 		key.proto = pd->proto;
4746 		key.port[0] = key.port[1] = icmpid;
4747 		if (direction == PF_IN)	{	/* wire side, straight */
4748 			PF_ACPY(&key.addr[0], pd->src, key.af);
4749 			PF_ACPY(&key.addr[1], pd->dst, key.af);
4750 		} else {			/* stack side, reverse */
4751 			PF_ACPY(&key.addr[1], pd->src, key.af);
4752 			PF_ACPY(&key.addr[0], pd->dst, key.af);
4753 		}
4754 
4755 		STATE_LOOKUP(kif, &key, direction, *state, pd);
4756 
4757 		(*state)->expire = time_uptime;
4758 		(*state)->timeout = PFTM_ICMP_ERROR_REPLY;
4759 
4760 		/* translate source/destination address, if necessary */
4761 		if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
4762 			struct pf_state_key *nk = (*state)->key[pd->didx];
4763 
4764 			switch (pd->af) {
4765 #ifdef INET
4766 			case AF_INET:
4767 				if (PF_ANEQ(pd->src,
4768 				    &nk->addr[pd->sidx], AF_INET))
4769 					pf_change_a(&saddr->v4.s_addr,
4770 					    pd->ip_sum,
4771 					    nk->addr[pd->sidx].v4.s_addr, 0);
4772 
4773 				if (PF_ANEQ(pd->dst, &nk->addr[pd->didx],
4774 				    AF_INET))
4775 					pf_change_a(&daddr->v4.s_addr,
4776 					    pd->ip_sum,
4777 					    nk->addr[pd->didx].v4.s_addr, 0);
4778 
4779 				if (nk->port[0] !=
4780 				    pd->hdr.icmp->icmp_id) {
4781 					pd->hdr.icmp->icmp_cksum =
4782 					    pf_cksum_fixup(
4783 					    pd->hdr.icmp->icmp_cksum, icmpid,
4784 					    nk->port[pd->sidx], 0);
4785 					pd->hdr.icmp->icmp_id =
4786 					    nk->port[pd->sidx];
4787 				}
4788 
4789 				m_copyback(m, off, ICMP_MINLEN,
4790 				    (caddr_t )pd->hdr.icmp);
4791 				break;
4792 #endif /* INET */
4793 #ifdef INET6
4794 			case AF_INET6:
4795 				if (PF_ANEQ(pd->src,
4796 				    &nk->addr[pd->sidx], AF_INET6))
4797 					pf_change_a6(saddr,
4798 					    &pd->hdr.icmp6->icmp6_cksum,
4799 					    &nk->addr[pd->sidx], 0);
4800 
4801 				if (PF_ANEQ(pd->dst,
4802 				    &nk->addr[pd->didx], AF_INET6))
4803 					pf_change_a6(daddr,
4804 					    &pd->hdr.icmp6->icmp6_cksum,
4805 					    &nk->addr[pd->didx], 0);
4806 
4807 				m_copyback(m, off, sizeof(struct icmp6_hdr),
4808 				    (caddr_t )pd->hdr.icmp6);
4809 				break;
4810 #endif /* INET6 */
4811 			}
4812 		}
4813 		return (PF_PASS);
4814 
4815 	} else {
4816 		/*
4817 		 * ICMP error message in response to a TCP/UDP packet.
4818 		 * Extract the inner TCP/UDP header and search for that state.
4819 		 */
4820 
4821 		struct pf_pdesc	pd2;
4822 		bzero(&pd2, sizeof pd2);
4823 #ifdef INET
4824 		struct ip	h2;
4825 #endif /* INET */
4826 #ifdef INET6
4827 		struct ip6_hdr	h2_6;
4828 		int		terminal = 0;
4829 #endif /* INET6 */
4830 		int		ipoff2 = 0;
4831 		int		off2 = 0;
4832 
4833 		pd2.af = pd->af;
4834 		/* Payload packet is from the opposite direction. */
4835 		pd2.sidx = (direction == PF_IN) ? 1 : 0;
4836 		pd2.didx = (direction == PF_IN) ? 0 : 1;
4837 		switch (pd->af) {
4838 #ifdef INET
4839 		case AF_INET:
4840 			/* offset of h2 in mbuf chain */
4841 			ipoff2 = off + ICMP_MINLEN;
4842 
4843 			if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
4844 			    NULL, reason, pd2.af)) {
4845 				DPFPRINTF(PF_DEBUG_MISC,
4846 				    ("pf: ICMP error message too short "
4847 				    "(ip)\n"));
4848 				return (PF_DROP);
4849 			}
4850 			/*
4851 			 * ICMP error messages don't refer to non-first
4852 			 * fragments
4853 			 */
4854 			if (h2.ip_off & htons(IP_OFFMASK)) {
4855 				REASON_SET(reason, PFRES_FRAG);
4856 				return (PF_DROP);
4857 			}
4858 
4859 			/* offset of protocol header that follows h2 */
4860 			off2 = ipoff2 + (h2.ip_hl << 2);
4861 
4862 			pd2.proto = h2.ip_p;
4863 			pd2.src = (struct pf_addr *)&h2.ip_src;
4864 			pd2.dst = (struct pf_addr *)&h2.ip_dst;
4865 			pd2.ip_sum = &h2.ip_sum;
4866 			break;
4867 #endif /* INET */
4868 #ifdef INET6
4869 		case AF_INET6:
4870 			ipoff2 = off + sizeof(struct icmp6_hdr);
4871 
4872 			if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
4873 			    NULL, reason, pd2.af)) {
4874 				DPFPRINTF(PF_DEBUG_MISC,
4875 				    ("pf: ICMP error message too short "
4876 				    "(ip6)\n"));
4877 				return (PF_DROP);
4878 			}
4879 			pd2.proto = h2_6.ip6_nxt;
4880 			pd2.src = (struct pf_addr *)&h2_6.ip6_src;
4881 			pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
4882 			pd2.ip_sum = NULL;
4883 			off2 = ipoff2 + sizeof(h2_6);
4884 			do {
4885 				switch (pd2.proto) {
4886 				case IPPROTO_FRAGMENT:
4887 					/*
4888 					 * ICMPv6 error messages for
4889 					 * non-first fragments
4890 					 */
4891 					REASON_SET(reason, PFRES_FRAG);
4892 					return (PF_DROP);
4893 				case IPPROTO_AH:
4894 				case IPPROTO_HOPOPTS:
4895 				case IPPROTO_ROUTING:
4896 				case IPPROTO_DSTOPTS: {
4897 					/* get next header and header length */
4898 					struct ip6_ext opt6;
4899 
4900 					if (!pf_pull_hdr(m, off2, &opt6,
4901 					    sizeof(opt6), NULL, reason,
4902 					    pd2.af)) {
4903 						DPFPRINTF(PF_DEBUG_MISC,
4904 						    ("pf: ICMPv6 short opt\n"));
4905 						return (PF_DROP);
4906 					}
4907 					if (pd2.proto == IPPROTO_AH)
4908 						off2 += (opt6.ip6e_len + 2) * 4;
4909 					else
4910 						off2 += (opt6.ip6e_len + 1) * 8;
4911 					pd2.proto = opt6.ip6e_nxt;
4912 					/* goto the next header */
4913 					break;
4914 				}
4915 				default:
4916 					terminal++;
4917 					break;
4918 				}
4919 			} while (!terminal);
4920 			break;
4921 #endif /* INET6 */
4922 		}
4923 
4924 		if (PF_ANEQ(pd->dst, pd2.src, pd->af)) {
4925 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
4926 				printf("pf: BAD ICMP %d:%d outer dst: ",
4927 				    icmptype, icmpcode);
4928 				pf_print_host(pd->src, 0, pd->af);
4929 				printf(" -> ");
4930 				pf_print_host(pd->dst, 0, pd->af);
4931 				printf(" inner src: ");
4932 				pf_print_host(pd2.src, 0, pd2.af);
4933 				printf(" -> ");
4934 				pf_print_host(pd2.dst, 0, pd2.af);
4935 				printf("\n");
4936 			}
4937 			REASON_SET(reason, PFRES_BADSTATE);
4938 			return (PF_DROP);
4939 		}
4940 
4941 		switch (pd2.proto) {
4942 		case IPPROTO_TCP: {
4943 			struct tcphdr		 th;
4944 			u_int32_t		 seq;
4945 			struct pf_state_peer	*src, *dst;
4946 			u_int8_t		 dws;
4947 			int			 copyback = 0;
4948 
4949 			/*
4950 			 * Only the first 8 bytes of the TCP header can be
4951 			 * expected. Don't access any TCP header fields after
4952 			 * th_seq, an ackskew test is not possible.
4953 			 */
4954 			if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason,
4955 			    pd2.af)) {
4956 				DPFPRINTF(PF_DEBUG_MISC,
4957 				    ("pf: ICMP error message too short "
4958 				    "(tcp)\n"));
4959 				return (PF_DROP);
4960 			}
4961 
4962 			key.af = pd2.af;
4963 			key.proto = IPPROTO_TCP;
4964 			PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
4965 			PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
4966 			key.port[pd2.sidx] = th.th_sport;
4967 			key.port[pd2.didx] = th.th_dport;
4968 
4969 			STATE_LOOKUP(kif, &key, direction, *state, pd);
4970 
4971 			if (direction == (*state)->direction) {
4972 				src = &(*state)->dst;
4973 				dst = &(*state)->src;
4974 			} else {
4975 				src = &(*state)->src;
4976 				dst = &(*state)->dst;
4977 			}
4978 
4979 			if (src->wscale && dst->wscale)
4980 				dws = dst->wscale & PF_WSCALE_MASK;
4981 			else
4982 				dws = 0;
4983 
4984 			/* Demodulate sequence number */
4985 			seq = ntohl(th.th_seq) - src->seqdiff;
4986 			if (src->seqdiff) {
4987 				pf_change_a(&th.th_seq, icmpsum,
4988 				    htonl(seq), 0);
4989 				copyback = 1;
4990 			}
4991 
4992 			if (!((*state)->state_flags & PFSTATE_SLOPPY) &&
4993 			    (!SEQ_GEQ(src->seqhi, seq) ||
4994 			    !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) {
4995 				if (V_pf_status.debug >= PF_DEBUG_MISC) {
4996 					printf("pf: BAD ICMP %d:%d ",
4997 					    icmptype, icmpcode);
4998 					pf_print_host(pd->src, 0, pd->af);
4999 					printf(" -> ");
5000 					pf_print_host(pd->dst, 0, pd->af);
5001 					printf(" state: ");
5002 					pf_print_state(*state);
5003 					printf(" seq=%u\n", seq);
5004 				}
5005 				REASON_SET(reason, PFRES_BADSTATE);
5006 				return (PF_DROP);
5007 			} else {
5008 				if (V_pf_status.debug >= PF_DEBUG_MISC) {
5009 					printf("pf: OK ICMP %d:%d ",
5010 					    icmptype, icmpcode);
5011 					pf_print_host(pd->src, 0, pd->af);
5012 					printf(" -> ");
5013 					pf_print_host(pd->dst, 0, pd->af);
5014 					printf(" state: ");
5015 					pf_print_state(*state);
5016 					printf(" seq=%u\n", seq);
5017 				}
5018 			}
5019 
5020 			/* translate source/destination address, if necessary */
5021 			if ((*state)->key[PF_SK_WIRE] !=
5022 			    (*state)->key[PF_SK_STACK]) {
5023 				struct pf_state_key *nk =
5024 				    (*state)->key[pd->didx];
5025 
5026 				if (PF_ANEQ(pd2.src,
5027 				    &nk->addr[pd2.sidx], pd2.af) ||
5028 				    nk->port[pd2.sidx] != th.th_sport)
5029 					pf_change_icmp(pd2.src, &th.th_sport,
5030 					    daddr, &nk->addr[pd2.sidx],
5031 					    nk->port[pd2.sidx], NULL,
5032 					    pd2.ip_sum, icmpsum,
5033 					    pd->ip_sum, 0, pd2.af);
5034 
5035 				if (PF_ANEQ(pd2.dst,
5036 				    &nk->addr[pd2.didx], pd2.af) ||
5037 				    nk->port[pd2.didx] != th.th_dport)
5038 					pf_change_icmp(pd2.dst, &th.th_dport,
5039 					    saddr, &nk->addr[pd2.didx],
5040 					    nk->port[pd2.didx], NULL,
5041 					    pd2.ip_sum, icmpsum,
5042 					    pd->ip_sum, 0, pd2.af);
5043 				copyback = 1;
5044 			}
5045 
5046 			if (copyback) {
5047 				switch (pd2.af) {
5048 #ifdef INET
5049 				case AF_INET:
5050 					m_copyback(m, off, ICMP_MINLEN,
5051 					    (caddr_t )pd->hdr.icmp);
5052 					m_copyback(m, ipoff2, sizeof(h2),
5053 					    (caddr_t )&h2);
5054 					break;
5055 #endif /* INET */
5056 #ifdef INET6
5057 				case AF_INET6:
5058 					m_copyback(m, off,
5059 					    sizeof(struct icmp6_hdr),
5060 					    (caddr_t )pd->hdr.icmp6);
5061 					m_copyback(m, ipoff2, sizeof(h2_6),
5062 					    (caddr_t )&h2_6);
5063 					break;
5064 #endif /* INET6 */
5065 				}
5066 				m_copyback(m, off2, 8, (caddr_t)&th);
5067 			}
5068 
5069 			return (PF_PASS);
5070 			break;
5071 		}
5072 		case IPPROTO_UDP: {
5073 			struct udphdr		uh;
5074 
5075 			if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
5076 			    NULL, reason, pd2.af)) {
5077 				DPFPRINTF(PF_DEBUG_MISC,
5078 				    ("pf: ICMP error message too short "
5079 				    "(udp)\n"));
5080 				return (PF_DROP);
5081 			}
5082 
5083 			key.af = pd2.af;
5084 			key.proto = IPPROTO_UDP;
5085 			PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5086 			PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5087 			key.port[pd2.sidx] = uh.uh_sport;
5088 			key.port[pd2.didx] = uh.uh_dport;
5089 
5090 			STATE_LOOKUP(kif, &key, direction, *state, pd);
5091 
5092 			/* translate source/destination address, if necessary */
5093 			if ((*state)->key[PF_SK_WIRE] !=
5094 			    (*state)->key[PF_SK_STACK]) {
5095 				struct pf_state_key *nk =
5096 				    (*state)->key[pd->didx];
5097 
5098 				if (PF_ANEQ(pd2.src,
5099 				    &nk->addr[pd2.sidx], pd2.af) ||
5100 				    nk->port[pd2.sidx] != uh.uh_sport)
5101 					pf_change_icmp(pd2.src, &uh.uh_sport,
5102 					    daddr, &nk->addr[pd2.sidx],
5103 					    nk->port[pd2.sidx], &uh.uh_sum,
5104 					    pd2.ip_sum, icmpsum,
5105 					    pd->ip_sum, 1, pd2.af);
5106 
5107 				if (PF_ANEQ(pd2.dst,
5108 				    &nk->addr[pd2.didx], pd2.af) ||
5109 				    nk->port[pd2.didx] != uh.uh_dport)
5110 					pf_change_icmp(pd2.dst, &uh.uh_dport,
5111 					    saddr, &nk->addr[pd2.didx],
5112 					    nk->port[pd2.didx], &uh.uh_sum,
5113 					    pd2.ip_sum, icmpsum,
5114 					    pd->ip_sum, 1, pd2.af);
5115 
5116 				switch (pd2.af) {
5117 #ifdef INET
5118 				case AF_INET:
5119 					m_copyback(m, off, ICMP_MINLEN,
5120 					    (caddr_t )pd->hdr.icmp);
5121 					m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5122 					break;
5123 #endif /* INET */
5124 #ifdef INET6
5125 				case AF_INET6:
5126 					m_copyback(m, off,
5127 					    sizeof(struct icmp6_hdr),
5128 					    (caddr_t )pd->hdr.icmp6);
5129 					m_copyback(m, ipoff2, sizeof(h2_6),
5130 					    (caddr_t )&h2_6);
5131 					break;
5132 #endif /* INET6 */
5133 				}
5134 				m_copyback(m, off2, sizeof(uh), (caddr_t)&uh);
5135 			}
5136 			return (PF_PASS);
5137 			break;
5138 		}
5139 #ifdef INET
5140 		case IPPROTO_ICMP: {
5141 			struct icmp		iih;
5142 
5143 			if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
5144 			    NULL, reason, pd2.af)) {
5145 				DPFPRINTF(PF_DEBUG_MISC,
5146 				    ("pf: ICMP error message too short i"
5147 				    "(icmp)\n"));
5148 				return (PF_DROP);
5149 			}
5150 
5151 			key.af = pd2.af;
5152 			key.proto = IPPROTO_ICMP;
5153 			PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5154 			PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5155 			key.port[0] = key.port[1] = iih.icmp_id;
5156 
5157 			STATE_LOOKUP(kif, &key, direction, *state, pd);
5158 
5159 			/* translate source/destination address, if necessary */
5160 			if ((*state)->key[PF_SK_WIRE] !=
5161 			    (*state)->key[PF_SK_STACK]) {
5162 				struct pf_state_key *nk =
5163 				    (*state)->key[pd->didx];
5164 
5165 				if (PF_ANEQ(pd2.src,
5166 				    &nk->addr[pd2.sidx], pd2.af) ||
5167 				    nk->port[pd2.sidx] != iih.icmp_id)
5168 					pf_change_icmp(pd2.src, &iih.icmp_id,
5169 					    daddr, &nk->addr[pd2.sidx],
5170 					    nk->port[pd2.sidx], NULL,
5171 					    pd2.ip_sum, icmpsum,
5172 					    pd->ip_sum, 0, AF_INET);
5173 
5174 				if (PF_ANEQ(pd2.dst,
5175 				    &nk->addr[pd2.didx], pd2.af) ||
5176 				    nk->port[pd2.didx] != iih.icmp_id)
5177 					pf_change_icmp(pd2.dst, &iih.icmp_id,
5178 					    saddr, &nk->addr[pd2.didx],
5179 					    nk->port[pd2.didx], NULL,
5180 					    pd2.ip_sum, icmpsum,
5181 					    pd->ip_sum, 0, AF_INET);
5182 
5183 				m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
5184 				m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5185 				m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih);
5186 			}
5187 			return (PF_PASS);
5188 			break;
5189 		}
5190 #endif /* INET */
5191 #ifdef INET6
5192 		case IPPROTO_ICMPV6: {
5193 			struct icmp6_hdr	iih;
5194 
5195 			if (!pf_pull_hdr(m, off2, &iih,
5196 			    sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) {
5197 				DPFPRINTF(PF_DEBUG_MISC,
5198 				    ("pf: ICMP error message too short "
5199 				    "(icmp6)\n"));
5200 				return (PF_DROP);
5201 			}
5202 
5203 			key.af = pd2.af;
5204 			key.proto = IPPROTO_ICMPV6;
5205 			PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5206 			PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5207 			key.port[0] = key.port[1] = iih.icmp6_id;
5208 
5209 			STATE_LOOKUP(kif, &key, direction, *state, pd);
5210 
5211 			/* translate source/destination address, if necessary */
5212 			if ((*state)->key[PF_SK_WIRE] !=
5213 			    (*state)->key[PF_SK_STACK]) {
5214 				struct pf_state_key *nk =
5215 				    (*state)->key[pd->didx];
5216 
5217 				if (PF_ANEQ(pd2.src,
5218 				    &nk->addr[pd2.sidx], pd2.af) ||
5219 				    nk->port[pd2.sidx] != iih.icmp6_id)
5220 					pf_change_icmp(pd2.src, &iih.icmp6_id,
5221 					    daddr, &nk->addr[pd2.sidx],
5222 					    nk->port[pd2.sidx], NULL,
5223 					    pd2.ip_sum, icmpsum,
5224 					    pd->ip_sum, 0, AF_INET6);
5225 
5226 				if (PF_ANEQ(pd2.dst,
5227 				    &nk->addr[pd2.didx], pd2.af) ||
5228 				    nk->port[pd2.didx] != iih.icmp6_id)
5229 					pf_change_icmp(pd2.dst, &iih.icmp6_id,
5230 					    saddr, &nk->addr[pd2.didx],
5231 					    nk->port[pd2.didx], NULL,
5232 					    pd2.ip_sum, icmpsum,
5233 					    pd->ip_sum, 0, AF_INET6);
5234 
5235 				m_copyback(m, off, sizeof(struct icmp6_hdr),
5236 				    (caddr_t)pd->hdr.icmp6);
5237 				m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6);
5238 				m_copyback(m, off2, sizeof(struct icmp6_hdr),
5239 				    (caddr_t)&iih);
5240 			}
5241 			return (PF_PASS);
5242 			break;
5243 		}
5244 #endif /* INET6 */
5245 		default: {
5246 			key.af = pd2.af;
5247 			key.proto = pd2.proto;
5248 			PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5249 			PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5250 			key.port[0] = key.port[1] = 0;
5251 
5252 			STATE_LOOKUP(kif, &key, direction, *state, pd);
5253 
5254 			/* translate source/destination address, if necessary */
5255 			if ((*state)->key[PF_SK_WIRE] !=
5256 			    (*state)->key[PF_SK_STACK]) {
5257 				struct pf_state_key *nk =
5258 				    (*state)->key[pd->didx];
5259 
5260 				if (PF_ANEQ(pd2.src,
5261 				    &nk->addr[pd2.sidx], pd2.af))
5262 					pf_change_icmp(pd2.src, NULL, daddr,
5263 					    &nk->addr[pd2.sidx], 0, NULL,
5264 					    pd2.ip_sum, icmpsum,
5265 					    pd->ip_sum, 0, pd2.af);
5266 
5267 				if (PF_ANEQ(pd2.dst,
5268 				    &nk->addr[pd2.didx], pd2.af))
5269 					pf_change_icmp(pd2.dst, NULL, saddr,
5270 					    &nk->addr[pd2.didx], 0, NULL,
5271 					    pd2.ip_sum, icmpsum,
5272 					    pd->ip_sum, 0, pd2.af);
5273 
5274 				switch (pd2.af) {
5275 #ifdef INET
5276 				case AF_INET:
5277 					m_copyback(m, off, ICMP_MINLEN,
5278 					    (caddr_t)pd->hdr.icmp);
5279 					m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5280 					break;
5281 #endif /* INET */
5282 #ifdef INET6
5283 				case AF_INET6:
5284 					m_copyback(m, off,
5285 					    sizeof(struct icmp6_hdr),
5286 					    (caddr_t )pd->hdr.icmp6);
5287 					m_copyback(m, ipoff2, sizeof(h2_6),
5288 					    (caddr_t )&h2_6);
5289 					break;
5290 #endif /* INET6 */
5291 				}
5292 			}
5293 			return (PF_PASS);
5294 			break;
5295 		}
5296 		}
5297 	}
5298 }
5299 
5300 static int
5301 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kkif *kif,
5302     struct mbuf *m, struct pf_pdesc *pd)
5303 {
5304 	struct pf_state_peer	*src, *dst;
5305 	struct pf_state_key_cmp	 key;
5306 
5307 	bzero(&key, sizeof(key));
5308 	key.af = pd->af;
5309 	key.proto = pd->proto;
5310 	if (direction == PF_IN)	{
5311 		PF_ACPY(&key.addr[0], pd->src, key.af);
5312 		PF_ACPY(&key.addr[1], pd->dst, key.af);
5313 		key.port[0] = key.port[1] = 0;
5314 	} else {
5315 		PF_ACPY(&key.addr[1], pd->src, key.af);
5316 		PF_ACPY(&key.addr[0], pd->dst, key.af);
5317 		key.port[1] = key.port[0] = 0;
5318 	}
5319 
5320 	STATE_LOOKUP(kif, &key, direction, *state, pd);
5321 
5322 	if (direction == (*state)->direction) {
5323 		src = &(*state)->src;
5324 		dst = &(*state)->dst;
5325 	} else {
5326 		src = &(*state)->dst;
5327 		dst = &(*state)->src;
5328 	}
5329 
5330 	/* update states */
5331 	if (src->state < PFOTHERS_SINGLE)
5332 		src->state = PFOTHERS_SINGLE;
5333 	if (dst->state == PFOTHERS_SINGLE)
5334 		dst->state = PFOTHERS_MULTIPLE;
5335 
5336 	/* update expire time */
5337 	(*state)->expire = time_uptime;
5338 	if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
5339 		(*state)->timeout = PFTM_OTHER_MULTIPLE;
5340 	else
5341 		(*state)->timeout = PFTM_OTHER_SINGLE;
5342 
5343 	/* translate source/destination address, if necessary */
5344 	if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
5345 		struct pf_state_key *nk = (*state)->key[pd->didx];
5346 
5347 		KASSERT(nk, ("%s: nk is null", __func__));
5348 		KASSERT(pd, ("%s: pd is null", __func__));
5349 		KASSERT(pd->src, ("%s: pd->src is null", __func__));
5350 		KASSERT(pd->dst, ("%s: pd->dst is null", __func__));
5351 		switch (pd->af) {
5352 #ifdef INET
5353 		case AF_INET:
5354 			if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET))
5355 				pf_change_a(&pd->src->v4.s_addr,
5356 				    pd->ip_sum,
5357 				    nk->addr[pd->sidx].v4.s_addr,
5358 				    0);
5359 
5360 			if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET))
5361 				pf_change_a(&pd->dst->v4.s_addr,
5362 				    pd->ip_sum,
5363 				    nk->addr[pd->didx].v4.s_addr,
5364 				    0);
5365 
5366 			break;
5367 #endif /* INET */
5368 #ifdef INET6
5369 		case AF_INET6:
5370 			if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET))
5371 				PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af);
5372 
5373 			if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET))
5374 				PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af);
5375 #endif /* INET6 */
5376 		}
5377 	}
5378 	return (PF_PASS);
5379 }
5380 
5381 /*
5382  * ipoff and off are measured from the start of the mbuf chain.
5383  * h must be at "ipoff" on the mbuf chain.
5384  */
5385 void *
5386 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
5387     u_short *actionp, u_short *reasonp, sa_family_t af)
5388 {
5389 	switch (af) {
5390 #ifdef INET
5391 	case AF_INET: {
5392 		struct ip	*h = mtod(m, struct ip *);
5393 		u_int16_t	 fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
5394 
5395 		if (fragoff) {
5396 			if (fragoff >= len)
5397 				ACTION_SET(actionp, PF_PASS);
5398 			else {
5399 				ACTION_SET(actionp, PF_DROP);
5400 				REASON_SET(reasonp, PFRES_FRAG);
5401 			}
5402 			return (NULL);
5403 		}
5404 		if (m->m_pkthdr.len < off + len ||
5405 		    ntohs(h->ip_len) < off + len) {
5406 			ACTION_SET(actionp, PF_DROP);
5407 			REASON_SET(reasonp, PFRES_SHORT);
5408 			return (NULL);
5409 		}
5410 		break;
5411 	}
5412 #endif /* INET */
5413 #ifdef INET6
5414 	case AF_INET6: {
5415 		struct ip6_hdr	*h = mtod(m, struct ip6_hdr *);
5416 
5417 		if (m->m_pkthdr.len < off + len ||
5418 		    (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
5419 		    (unsigned)(off + len)) {
5420 			ACTION_SET(actionp, PF_DROP);
5421 			REASON_SET(reasonp, PFRES_SHORT);
5422 			return (NULL);
5423 		}
5424 		break;
5425 	}
5426 #endif /* INET6 */
5427 	}
5428 	m_copydata(m, off, len, p);
5429 	return (p);
5430 }
5431 
5432 int
5433 pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *kif,
5434     int rtableid)
5435 {
5436 	struct ifnet		*ifp;
5437 
5438 	/*
5439 	 * Skip check for addresses with embedded interface scope,
5440 	 * as they would always match anyway.
5441 	 */
5442 	if (af == AF_INET6 && IN6_IS_SCOPE_EMBED(&addr->v6))
5443 		return (1);
5444 
5445 	if (af != AF_INET && af != AF_INET6)
5446 		return (0);
5447 
5448 	/* Skip checks for ipsec interfaces */
5449 	if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC)
5450 		return (1);
5451 
5452 	ifp = (kif != NULL) ? kif->pfik_ifp : NULL;
5453 
5454 	switch (af) {
5455 #ifdef INET6
5456 	case AF_INET6:
5457 		return (fib6_check_urpf(rtableid, &addr->v6, 0, NHR_NONE,
5458 		    ifp));
5459 #endif
5460 #ifdef INET
5461 	case AF_INET:
5462 		return (fib4_check_urpf(rtableid, addr->v4, 0, NHR_NONE,
5463 		    ifp));
5464 #endif
5465 	}
5466 
5467 	return (0);
5468 }
5469 
5470 #ifdef INET
5471 static void
5472 pf_route(struct mbuf **m, struct pf_krule *r, int dir, struct ifnet *oifp,
5473     struct pf_state *s, struct pf_pdesc *pd, struct inpcb *inp)
5474 {
5475 	struct mbuf		*m0, *m1;
5476 	struct sockaddr_in	dst;
5477 	struct ip		*ip;
5478 	struct ifnet		*ifp = NULL;
5479 	struct pf_addr		 naddr;
5480 	struct pf_ksrc_node	*sn = NULL;
5481 	int			 error = 0;
5482 	uint16_t		 ip_len, ip_off;
5483 
5484 	KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__));
5485 	KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction",
5486 	    __func__));
5487 
5488 	if ((pd->pf_mtag == NULL &&
5489 	    ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) ||
5490 	    pd->pf_mtag->routed++ > 3) {
5491 		m0 = *m;
5492 		*m = NULL;
5493 		goto bad_locked;
5494 	}
5495 
5496 	if (r->rt == PF_DUPTO) {
5497 		if ((pd->pf_mtag->flags & PF_DUPLICATED)) {
5498 			if (s == NULL) {
5499 				ifp = r->rpool.cur->kif ?
5500 				    r->rpool.cur->kif->pfik_ifp : NULL;
5501 			} else {
5502 				ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5503 				PF_STATE_UNLOCK(s);
5504 			}
5505 			if (ifp == oifp) {
5506 				/* When the 2nd interface is not skipped */
5507 				return;
5508 			} else {
5509 				m0 = *m;
5510 				*m = NULL;
5511 				goto bad;
5512 			}
5513 		} else {
5514 			pd->pf_mtag->flags |= PF_DUPLICATED;
5515 			if (((m0 = m_dup(*m, M_NOWAIT)) == NULL)) {
5516 				if (s)
5517 					PF_STATE_UNLOCK(s);
5518 				return;
5519 			}
5520 		}
5521 	} else {
5522 		if ((r->rt == PF_REPLYTO) == (r->direction == dir)) {
5523 			if (s)
5524 				PF_STATE_UNLOCK(s);
5525 			return;
5526 		}
5527 		m0 = *m;
5528 	}
5529 
5530 	ip = mtod(m0, struct ip *);
5531 
5532 	bzero(&dst, sizeof(dst));
5533 	dst.sin_family = AF_INET;
5534 	dst.sin_len = sizeof(dst);
5535 	dst.sin_addr = ip->ip_dst;
5536 
5537 	bzero(&naddr, sizeof(naddr));
5538 
5539 	if (TAILQ_EMPTY(&r->rpool.list)) {
5540 		DPFPRINTF(PF_DEBUG_URGENT,
5541 		    ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__));
5542 		goto bad_locked;
5543 	}
5544 	if (s == NULL) {
5545 		pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
5546 		    &naddr, NULL, &sn);
5547 		if (!PF_AZERO(&naddr, AF_INET))
5548 			dst.sin_addr.s_addr = naddr.v4.s_addr;
5549 		ifp = r->rpool.cur->kif ?
5550 		    r->rpool.cur->kif->pfik_ifp : NULL;
5551 	} else {
5552 		if (!PF_AZERO(&s->rt_addr, AF_INET))
5553 			dst.sin_addr.s_addr =
5554 			    s->rt_addr.v4.s_addr;
5555 		ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5556 		PF_STATE_UNLOCK(s);
5557 	}
5558 	if (ifp == NULL)
5559 		goto bad;
5560 
5561 	if (dir == PF_IN) {
5562 		if (pf_test(PF_OUT, 0, ifp, &m0, inp) != PF_PASS)
5563 			goto bad;
5564 		else if (m0 == NULL)
5565 			goto done;
5566 		if (m0->m_len < sizeof(struct ip)) {
5567 			DPFPRINTF(PF_DEBUG_URGENT,
5568 			    ("%s: m0->m_len < sizeof(struct ip)\n", __func__));
5569 			goto bad;
5570 		}
5571 		ip = mtod(m0, struct ip *);
5572 	}
5573 
5574 	if (ifp->if_flags & IFF_LOOPBACK)
5575 		m0->m_flags |= M_SKIP_FIREWALL;
5576 
5577 	ip_len = ntohs(ip->ip_len);
5578 	ip_off = ntohs(ip->ip_off);
5579 
5580 	/* Copied from FreeBSD 10.0-CURRENT ip_output. */
5581 	m0->m_pkthdr.csum_flags |= CSUM_IP;
5582 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
5583 		m0 = mb_unmapped_to_ext(m0);
5584 		if (m0 == NULL)
5585 			goto done;
5586 		in_delayed_cksum(m0);
5587 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
5588 	}
5589 #if defined(SCTP) || defined(SCTP_SUPPORT)
5590 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
5591 		m0 = mb_unmapped_to_ext(m0);
5592 		if (m0 == NULL)
5593 			goto done;
5594 		sctp_delayed_cksum(m0, (uint32_t)(ip->ip_hl << 2));
5595 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
5596 	}
5597 #endif
5598 
5599 	/*
5600 	 * If small enough for interface, or the interface will take
5601 	 * care of the fragmentation for us, we can just send directly.
5602 	 */
5603 	if (ip_len <= ifp->if_mtu ||
5604 	    (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
5605 		ip->ip_sum = 0;
5606 		if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
5607 			ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
5608 			m0->m_pkthdr.csum_flags &= ~CSUM_IP;
5609 		}
5610 		m_clrprotoflags(m0);	/* Avoid confusing lower layers. */
5611 		error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL);
5612 		goto done;
5613 	}
5614 
5615 	/* Balk when DF bit is set or the interface didn't support TSO. */
5616 	if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) {
5617 		error = EMSGSIZE;
5618 		KMOD_IPSTAT_INC(ips_cantfrag);
5619 		if (r->rt != PF_DUPTO) {
5620 			icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
5621 			    ifp->if_mtu);
5622 			goto done;
5623 		} else
5624 			goto bad;
5625 	}
5626 
5627 	error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist);
5628 	if (error)
5629 		goto bad;
5630 
5631 	for (; m0; m0 = m1) {
5632 		m1 = m0->m_nextpkt;
5633 		m0->m_nextpkt = NULL;
5634 		if (error == 0) {
5635 			m_clrprotoflags(m0);
5636 			error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL);
5637 		} else
5638 			m_freem(m0);
5639 	}
5640 
5641 	if (error == 0)
5642 		KMOD_IPSTAT_INC(ips_fragmented);
5643 
5644 done:
5645 	if (r->rt != PF_DUPTO)
5646 		*m = NULL;
5647 	return;
5648 
5649 bad_locked:
5650 	if (s)
5651 		PF_STATE_UNLOCK(s);
5652 bad:
5653 	m_freem(m0);
5654 	goto done;
5655 }
5656 #endif /* INET */
5657 
5658 #ifdef INET6
5659 static void
5660 pf_route6(struct mbuf **m, struct pf_krule *r, int dir, struct ifnet *oifp,
5661     struct pf_state *s, struct pf_pdesc *pd, struct inpcb *inp)
5662 {
5663 	struct mbuf		*m0;
5664 	struct sockaddr_in6	dst;
5665 	struct ip6_hdr		*ip6;
5666 	struct ifnet		*ifp = NULL;
5667 	struct pf_addr		 naddr;
5668 	struct pf_ksrc_node	*sn = NULL;
5669 
5670 	KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__));
5671 	KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction",
5672 	    __func__));
5673 
5674 	if ((pd->pf_mtag == NULL &&
5675 	    ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) ||
5676 	    pd->pf_mtag->routed++ > 3) {
5677 		m0 = *m;
5678 		*m = NULL;
5679 		goto bad_locked;
5680 	}
5681 
5682 	if (r->rt == PF_DUPTO) {
5683 		if ((pd->pf_mtag->flags & PF_DUPLICATED)) {
5684 			if (s == NULL) {
5685 				ifp = r->rpool.cur->kif ?
5686 				    r->rpool.cur->kif->pfik_ifp : NULL;
5687 			} else {
5688 				ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5689 				PF_STATE_UNLOCK(s);
5690 			}
5691 			if (ifp == oifp) {
5692 				/* When the 2nd interface is not skipped */
5693 				return;
5694 			} else {
5695 				m0 = *m;
5696 				*m = NULL;
5697 				goto bad;
5698 			}
5699 		} else {
5700 			pd->pf_mtag->flags |= PF_DUPLICATED;
5701 			if (((m0 = m_dup(*m, M_NOWAIT)) == NULL)) {
5702 				if (s)
5703 					PF_STATE_UNLOCK(s);
5704 				return;
5705 			}
5706 		}
5707 	} else {
5708 		if ((r->rt == PF_REPLYTO) == (r->direction == dir)) {
5709 			if (s)
5710 				PF_STATE_UNLOCK(s);
5711 			return;
5712 		}
5713 		m0 = *m;
5714 	}
5715 
5716 	ip6 = mtod(m0, struct ip6_hdr *);
5717 
5718 	bzero(&dst, sizeof(dst));
5719 	dst.sin6_family = AF_INET6;
5720 	dst.sin6_len = sizeof(dst);
5721 	dst.sin6_addr = ip6->ip6_dst;
5722 
5723 	bzero(&naddr, sizeof(naddr));
5724 
5725 	if (TAILQ_EMPTY(&r->rpool.list)) {
5726 		DPFPRINTF(PF_DEBUG_URGENT,
5727 		    ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__));
5728 		goto bad_locked;
5729 	}
5730 	if (s == NULL) {
5731 		pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
5732 		    &naddr, NULL, &sn);
5733 		if (!PF_AZERO(&naddr, AF_INET6))
5734 			PF_ACPY((struct pf_addr *)&dst.sin6_addr,
5735 			    &naddr, AF_INET6);
5736 		ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
5737 	} else {
5738 		if (!PF_AZERO(&s->rt_addr, AF_INET6))
5739 			PF_ACPY((struct pf_addr *)&dst.sin6_addr,
5740 			    &s->rt_addr, AF_INET6);
5741 		ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5742 	}
5743 
5744 	if (s)
5745 		PF_STATE_UNLOCK(s);
5746 
5747 	if (ifp == NULL)
5748 		goto bad;
5749 
5750 	if (dir == PF_IN) {
5751 		if (pf_test6(PF_OUT, PFIL_FWD, ifp, &m0, inp) != PF_PASS)
5752 			goto bad;
5753 		else if (m0 == NULL)
5754 			goto done;
5755 		if (m0->m_len < sizeof(struct ip6_hdr)) {
5756 			DPFPRINTF(PF_DEBUG_URGENT,
5757 			    ("%s: m0->m_len < sizeof(struct ip6_hdr)\n",
5758 			    __func__));
5759 			goto bad;
5760 		}
5761 		ip6 = mtod(m0, struct ip6_hdr *);
5762 	}
5763 
5764 	if (ifp->if_flags & IFF_LOOPBACK)
5765 		m0->m_flags |= M_SKIP_FIREWALL;
5766 
5767 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 &
5768 	    ~ifp->if_hwassist) {
5769 		uint32_t plen = m0->m_pkthdr.len - sizeof(*ip6);
5770 		m0 = mb_unmapped_to_ext(m0);
5771 		if (m0 == NULL)
5772 			goto done;
5773 		in6_delayed_cksum(m0, plen, sizeof(struct ip6_hdr));
5774 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
5775 	}
5776 
5777 	/*
5778 	 * If the packet is too large for the outgoing interface,
5779 	 * send back an icmp6 error.
5780 	 */
5781 	if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr))
5782 		dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
5783 	if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu)
5784 		nd6_output_ifp(ifp, ifp, m0, &dst, NULL);
5785 	else {
5786 		in6_ifstat_inc(ifp, ifs6_in_toobig);
5787 		if (r->rt != PF_DUPTO)
5788 			icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
5789 		else
5790 			goto bad;
5791 	}
5792 
5793 done:
5794 	if (r->rt != PF_DUPTO)
5795 		*m = NULL;
5796 	return;
5797 
5798 bad_locked:
5799 	if (s)
5800 		PF_STATE_UNLOCK(s);
5801 bad:
5802 	m_freem(m0);
5803 	goto done;
5804 }
5805 #endif /* INET6 */
5806 
5807 /*
5808  * FreeBSD supports cksum offloads for the following drivers.
5809  *  em(4), fxp(4), lge(4), nge(4), re(4), ti(4), txp(4), xl(4)
5810  *
5811  * CSUM_DATA_VALID | CSUM_PSEUDO_HDR :
5812  *  network driver performed cksum including pseudo header, need to verify
5813  *   csum_data
5814  * CSUM_DATA_VALID :
5815  *  network driver performed cksum, needs to additional pseudo header
5816  *  cksum computation with partial csum_data(i.e. lack of H/W support for
5817  *  pseudo header, for instance sk(4) and possibly gem(4))
5818  *
5819  * After validating the cksum of packet, set both flag CSUM_DATA_VALID and
5820  * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper
5821  * TCP/UDP layer.
5822  * Also, set csum_data to 0xffff to force cksum validation.
5823  */
5824 static int
5825 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af)
5826 {
5827 	u_int16_t sum = 0;
5828 	int hw_assist = 0;
5829 	struct ip *ip;
5830 
5831 	if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
5832 		return (1);
5833 	if (m->m_pkthdr.len < off + len)
5834 		return (1);
5835 
5836 	switch (p) {
5837 	case IPPROTO_TCP:
5838 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
5839 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
5840 				sum = m->m_pkthdr.csum_data;
5841 			} else {
5842 				ip = mtod(m, struct ip *);
5843 				sum = in_pseudo(ip->ip_src.s_addr,
5844 				ip->ip_dst.s_addr, htonl((u_short)len +
5845 				m->m_pkthdr.csum_data + IPPROTO_TCP));
5846 			}
5847 			sum ^= 0xffff;
5848 			++hw_assist;
5849 		}
5850 		break;
5851 	case IPPROTO_UDP:
5852 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
5853 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
5854 				sum = m->m_pkthdr.csum_data;
5855 			} else {
5856 				ip = mtod(m, struct ip *);
5857 				sum = in_pseudo(ip->ip_src.s_addr,
5858 				ip->ip_dst.s_addr, htonl((u_short)len +
5859 				m->m_pkthdr.csum_data + IPPROTO_UDP));
5860 			}
5861 			sum ^= 0xffff;
5862 			++hw_assist;
5863 		}
5864 		break;
5865 	case IPPROTO_ICMP:
5866 #ifdef INET6
5867 	case IPPROTO_ICMPV6:
5868 #endif /* INET6 */
5869 		break;
5870 	default:
5871 		return (1);
5872 	}
5873 
5874 	if (!hw_assist) {
5875 		switch (af) {
5876 		case AF_INET:
5877 			if (p == IPPROTO_ICMP) {
5878 				if (m->m_len < off)
5879 					return (1);
5880 				m->m_data += off;
5881 				m->m_len -= off;
5882 				sum = in_cksum(m, len);
5883 				m->m_data -= off;
5884 				m->m_len += off;
5885 			} else {
5886 				if (m->m_len < sizeof(struct ip))
5887 					return (1);
5888 				sum = in4_cksum(m, p, off, len);
5889 			}
5890 			break;
5891 #ifdef INET6
5892 		case AF_INET6:
5893 			if (m->m_len < sizeof(struct ip6_hdr))
5894 				return (1);
5895 			sum = in6_cksum(m, p, off, len);
5896 			break;
5897 #endif /* INET6 */
5898 		default:
5899 			return (1);
5900 		}
5901 	}
5902 	if (sum) {
5903 		switch (p) {
5904 		case IPPROTO_TCP:
5905 		    {
5906 			KMOD_TCPSTAT_INC(tcps_rcvbadsum);
5907 			break;
5908 		    }
5909 		case IPPROTO_UDP:
5910 		    {
5911 			KMOD_UDPSTAT_INC(udps_badsum);
5912 			break;
5913 		    }
5914 #ifdef INET
5915 		case IPPROTO_ICMP:
5916 		    {
5917 			KMOD_ICMPSTAT_INC(icps_checksum);
5918 			break;
5919 		    }
5920 #endif
5921 #ifdef INET6
5922 		case IPPROTO_ICMPV6:
5923 		    {
5924 			KMOD_ICMP6STAT_INC(icp6s_checksum);
5925 			break;
5926 		    }
5927 #endif /* INET6 */
5928 		}
5929 		return (1);
5930 	} else {
5931 		if (p == IPPROTO_TCP || p == IPPROTO_UDP) {
5932 			m->m_pkthdr.csum_flags |=
5933 			    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
5934 			m->m_pkthdr.csum_data = 0xffff;
5935 		}
5936 	}
5937 	return (0);
5938 }
5939 
5940 #ifdef INET
5941 int
5942 pf_test(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp)
5943 {
5944 	struct pfi_kkif		*kif;
5945 	u_short			 action, reason = 0, log = 0;
5946 	struct mbuf		*m = *m0;
5947 	struct ip		*h = NULL;
5948 	struct m_tag		*ipfwtag;
5949 	struct pf_krule		*a = NULL, *r = &V_pf_default_rule, *tr, *nr;
5950 	struct pf_state		*s = NULL;
5951 	struct pf_kruleset	*ruleset = NULL;
5952 	struct pf_pdesc		 pd;
5953 	int			 off, dirndx, pqid = 0;
5954 
5955 	PF_RULES_RLOCK_TRACKER;
5956 
5957 	M_ASSERTPKTHDR(m);
5958 
5959 	if (!V_pf_status.running)
5960 		return (PF_PASS);
5961 
5962 	memset(&pd, 0, sizeof(pd));
5963 
5964 	kif = (struct pfi_kkif *)ifp->if_pf_kif;
5965 
5966 	if (kif == NULL) {
5967 		DPFPRINTF(PF_DEBUG_URGENT,
5968 		    ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname));
5969 		return (PF_DROP);
5970 	}
5971 	if (kif->pfik_flags & PFI_IFLAG_SKIP)
5972 		return (PF_PASS);
5973 
5974 	if (m->m_flags & M_SKIP_FIREWALL)
5975 		return (PF_PASS);
5976 
5977 	pd.pf_mtag = pf_find_mtag(m);
5978 
5979 	PF_RULES_RLOCK();
5980 
5981 	if (ip_divert_ptr != NULL &&
5982 	    ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) {
5983 		struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1);
5984 		if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) {
5985 			if (pd.pf_mtag == NULL &&
5986 			    ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) {
5987 				action = PF_DROP;
5988 				goto done;
5989 			}
5990 			pd.pf_mtag->flags |= PF_PACKET_LOOPED;
5991 			m_tag_delete(m, ipfwtag);
5992 		}
5993 		if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) {
5994 			m->m_flags |= M_FASTFWD_OURS;
5995 			pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT;
5996 		}
5997 	} else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) {
5998 		/* We do IP header normalization and packet reassembly here */
5999 		action = PF_DROP;
6000 		goto done;
6001 	}
6002 	m = *m0;	/* pf_normalize messes with m0 */
6003 	h = mtod(m, struct ip *);
6004 
6005 	off = h->ip_hl << 2;
6006 	if (off < (int)sizeof(struct ip)) {
6007 		action = PF_DROP;
6008 		REASON_SET(&reason, PFRES_SHORT);
6009 		log = 1;
6010 		goto done;
6011 	}
6012 
6013 	pd.src = (struct pf_addr *)&h->ip_src;
6014 	pd.dst = (struct pf_addr *)&h->ip_dst;
6015 	pd.sport = pd.dport = NULL;
6016 	pd.ip_sum = &h->ip_sum;
6017 	pd.proto_sum = NULL;
6018 	pd.proto = h->ip_p;
6019 	pd.dir = dir;
6020 	pd.sidx = (dir == PF_IN) ? 0 : 1;
6021 	pd.didx = (dir == PF_IN) ? 1 : 0;
6022 	pd.af = AF_INET;
6023 	pd.tos = h->ip_tos & ~IPTOS_ECN_MASK;
6024 	pd.tot_len = ntohs(h->ip_len);
6025 
6026 	/* handle fragments that didn't get reassembled by normalization */
6027 	if (h->ip_off & htons(IP_MF | IP_OFFMASK)) {
6028 		action = pf_test_fragment(&r, dir, kif, m, h,
6029 		    &pd, &a, &ruleset);
6030 		goto done;
6031 	}
6032 
6033 	switch (h->ip_p) {
6034 	case IPPROTO_TCP: {
6035 		struct tcphdr	th;
6036 
6037 		pd.hdr.tcp = &th;
6038 		if (!pf_pull_hdr(m, off, &th, sizeof(th),
6039 		    &action, &reason, AF_INET)) {
6040 			log = action != PF_PASS;
6041 			goto done;
6042 		}
6043 		pd.p_len = pd.tot_len - off - (th.th_off << 2);
6044 		if ((th.th_flags & TH_ACK) && pd.p_len == 0)
6045 			pqid = 1;
6046 		action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
6047 		if (action == PF_DROP)
6048 			goto done;
6049 		action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
6050 		    &reason);
6051 		if (action == PF_PASS) {
6052 			if (V_pfsync_update_state_ptr != NULL)
6053 				V_pfsync_update_state_ptr(s);
6054 			r = s->rule.ptr;
6055 			a = s->anchor.ptr;
6056 			log = s->log;
6057 		} else if (s == NULL)
6058 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6059 			    &a, &ruleset, inp);
6060 		break;
6061 	}
6062 
6063 	case IPPROTO_UDP: {
6064 		struct udphdr	uh;
6065 
6066 		pd.hdr.udp = &uh;
6067 		if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
6068 		    &action, &reason, AF_INET)) {
6069 			log = action != PF_PASS;
6070 			goto done;
6071 		}
6072 		if (uh.uh_dport == 0 ||
6073 		    ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
6074 		    ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
6075 			action = PF_DROP;
6076 			REASON_SET(&reason, PFRES_SHORT);
6077 			goto done;
6078 		}
6079 		action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
6080 		if (action == PF_PASS) {
6081 			if (V_pfsync_update_state_ptr != NULL)
6082 				V_pfsync_update_state_ptr(s);
6083 			r = s->rule.ptr;
6084 			a = s->anchor.ptr;
6085 			log = s->log;
6086 		} else if (s == NULL)
6087 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6088 			    &a, &ruleset, inp);
6089 		break;
6090 	}
6091 
6092 	case IPPROTO_ICMP: {
6093 		struct icmp	ih;
6094 
6095 		pd.hdr.icmp = &ih;
6096 		if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
6097 		    &action, &reason, AF_INET)) {
6098 			log = action != PF_PASS;
6099 			goto done;
6100 		}
6101 		action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd,
6102 		    &reason);
6103 		if (action == PF_PASS) {
6104 			if (V_pfsync_update_state_ptr != NULL)
6105 				V_pfsync_update_state_ptr(s);
6106 			r = s->rule.ptr;
6107 			a = s->anchor.ptr;
6108 			log = s->log;
6109 		} else if (s == NULL)
6110 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6111 			    &a, &ruleset, inp);
6112 		break;
6113 	}
6114 
6115 #ifdef INET6
6116 	case IPPROTO_ICMPV6: {
6117 		action = PF_DROP;
6118 		DPFPRINTF(PF_DEBUG_MISC,
6119 		    ("pf: dropping IPv4 packet with ICMPv6 payload\n"));
6120 		goto done;
6121 	}
6122 #endif
6123 
6124 	default:
6125 		action = pf_test_state_other(&s, dir, kif, m, &pd);
6126 		if (action == PF_PASS) {
6127 			if (V_pfsync_update_state_ptr != NULL)
6128 				V_pfsync_update_state_ptr(s);
6129 			r = s->rule.ptr;
6130 			a = s->anchor.ptr;
6131 			log = s->log;
6132 		} else if (s == NULL)
6133 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6134 			    &a, &ruleset, inp);
6135 		break;
6136 	}
6137 
6138 done:
6139 	PF_RULES_RUNLOCK();
6140 	if (action == PF_PASS && h->ip_hl > 5 &&
6141 	    !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
6142 		action = PF_DROP;
6143 		REASON_SET(&reason, PFRES_IPOPTIONS);
6144 		log = r->log;
6145 		DPFPRINTF(PF_DEBUG_MISC,
6146 		    ("pf: dropping packet with ip options\n"));
6147 	}
6148 
6149 	if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) {
6150 		action = PF_DROP;
6151 		REASON_SET(&reason, PFRES_MEMORY);
6152 	}
6153 	if (r->rtableid >= 0)
6154 		M_SETFIB(m, r->rtableid);
6155 
6156 	if (r->scrub_flags & PFSTATE_SETPRIO) {
6157 		if (pd.tos & IPTOS_LOWDELAY)
6158 			pqid = 1;
6159 		if (pf_ieee8021q_setpcp(m, r->set_prio[pqid])) {
6160 			action = PF_DROP;
6161 			REASON_SET(&reason, PFRES_MEMORY);
6162 			log = 1;
6163 			DPFPRINTF(PF_DEBUG_MISC,
6164 			    ("pf: failed to allocate 802.1q mtag\n"));
6165 		}
6166 	}
6167 
6168 #ifdef ALTQ
6169 	if (action == PF_PASS && r->qid) {
6170 		if (pd.pf_mtag == NULL &&
6171 		    ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) {
6172 			action = PF_DROP;
6173 			REASON_SET(&reason, PFRES_MEMORY);
6174 		} else {
6175 			if (s != NULL)
6176 				pd.pf_mtag->qid_hash = pf_state_hash(s);
6177 			if (pqid || (pd.tos & IPTOS_LOWDELAY))
6178 				pd.pf_mtag->qid = r->pqid;
6179 			else
6180 				pd.pf_mtag->qid = r->qid;
6181 			/* Add hints for ecn. */
6182 			pd.pf_mtag->hdr = h;
6183 		}
6184 	}
6185 #endif /* ALTQ */
6186 
6187 	/*
6188 	 * connections redirected to loopback should not match sockets
6189 	 * bound specifically to loopback due to security implications,
6190 	 * see tcp_input() and in_pcblookup_listen().
6191 	 */
6192 	if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
6193 	    pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
6194 	    (s->nat_rule.ptr->action == PF_RDR ||
6195 	    s->nat_rule.ptr->action == PF_BINAT) &&
6196 	    IN_LOOPBACK(ntohl(pd.dst->v4.s_addr)))
6197 		m->m_flags |= M_SKIP_FIREWALL;
6198 
6199 	if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL &&
6200 	    !PACKET_LOOPED(&pd)) {
6201 		ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0,
6202 		    sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO);
6203 		if (ipfwtag != NULL) {
6204 			((struct ipfw_rule_ref *)(ipfwtag+1))->info =
6205 			    ntohs(r->divert.port);
6206 			((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir;
6207 
6208 			if (s)
6209 				PF_STATE_UNLOCK(s);
6210 
6211 			m_tag_prepend(m, ipfwtag);
6212 			if (m->m_flags & M_FASTFWD_OURS) {
6213 				if (pd.pf_mtag == NULL &&
6214 				    ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) {
6215 					action = PF_DROP;
6216 					REASON_SET(&reason, PFRES_MEMORY);
6217 					log = 1;
6218 					DPFPRINTF(PF_DEBUG_MISC,
6219 					    ("pf: failed to allocate tag\n"));
6220 				} else {
6221 					pd.pf_mtag->flags |=
6222 					    PF_FASTFWD_OURS_PRESENT;
6223 					m->m_flags &= ~M_FASTFWD_OURS;
6224 				}
6225 			}
6226 			ip_divert_ptr(*m0, dir == PF_IN);
6227 			*m0 = NULL;
6228 
6229 			return (action);
6230 		} else {
6231 			/* XXX: ipfw has the same behaviour! */
6232 			action = PF_DROP;
6233 			REASON_SET(&reason, PFRES_MEMORY);
6234 			log = 1;
6235 			DPFPRINTF(PF_DEBUG_MISC,
6236 			    ("pf: failed to allocate divert tag\n"));
6237 		}
6238 	}
6239 
6240 	if (log) {
6241 		struct pf_krule *lr;
6242 
6243 		if (s != NULL && s->nat_rule.ptr != NULL &&
6244 		    s->nat_rule.ptr->log & PF_LOG_ALL)
6245 			lr = s->nat_rule.ptr;
6246 		else
6247 			lr = r;
6248 		PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd,
6249 		    (s == NULL));
6250 	}
6251 
6252 	counter_u64_add(kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS],
6253 	    pd.tot_len);
6254 	counter_u64_add(kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS],
6255 	    1);
6256 
6257 	if (action == PF_PASS || r->action == PF_DROP) {
6258 		dirndx = (dir == PF_OUT);
6259 		counter_u64_add(r->packets[dirndx], 1);
6260 		counter_u64_add(r->bytes[dirndx], pd.tot_len);
6261 		if (a != NULL) {
6262 			counter_u64_add(a->packets[dirndx], 1);
6263 			counter_u64_add(a->bytes[dirndx], pd.tot_len);
6264 		}
6265 		if (s != NULL) {
6266 			if (s->nat_rule.ptr != NULL) {
6267 				counter_u64_add(s->nat_rule.ptr->packets[dirndx],
6268 				    1);
6269 				counter_u64_add(s->nat_rule.ptr->bytes[dirndx],
6270 				    pd.tot_len);
6271 			}
6272 			if (s->src_node != NULL) {
6273 				counter_u64_add(s->src_node->packets[dirndx],
6274 				    1);
6275 				counter_u64_add(s->src_node->bytes[dirndx],
6276 				    pd.tot_len);
6277 			}
6278 			if (s->nat_src_node != NULL) {
6279 				counter_u64_add(s->nat_src_node->packets[dirndx],
6280 				    1);
6281 				counter_u64_add(s->nat_src_node->bytes[dirndx],
6282 				    pd.tot_len);
6283 			}
6284 			dirndx = (dir == s->direction) ? 0 : 1;
6285 			counter_u64_add(s->packets[dirndx], 1);
6286 			counter_u64_add(s->bytes[dirndx], pd.tot_len);
6287 		}
6288 		tr = r;
6289 		nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
6290 		if (nr != NULL && r == &V_pf_default_rule)
6291 			tr = nr;
6292 		if (tr->src.addr.type == PF_ADDR_TABLE)
6293 			pfr_update_stats(tr->src.addr.p.tbl,
6294 			    (s == NULL) ? pd.src :
6295 			    &s->key[(s->direction == PF_IN)]->
6296 				addr[(s->direction == PF_OUT)],
6297 			    pd.af, pd.tot_len, dir == PF_OUT,
6298 			    r->action == PF_PASS, tr->src.neg);
6299 		if (tr->dst.addr.type == PF_ADDR_TABLE)
6300 			pfr_update_stats(tr->dst.addr.p.tbl,
6301 			    (s == NULL) ? pd.dst :
6302 			    &s->key[(s->direction == PF_IN)]->
6303 				addr[(s->direction == PF_IN)],
6304 			    pd.af, pd.tot_len, dir == PF_OUT,
6305 			    r->action == PF_PASS, tr->dst.neg);
6306 	}
6307 
6308 	switch (action) {
6309 	case PF_SYNPROXY_DROP:
6310 		m_freem(*m0);
6311 	case PF_DEFER:
6312 		*m0 = NULL;
6313 		action = PF_PASS;
6314 		break;
6315 	case PF_DROP:
6316 		m_freem(*m0);
6317 		*m0 = NULL;
6318 		break;
6319 	default:
6320 		/* pf_route() returns unlocked. */
6321 		if (r->rt) {
6322 			pf_route(m0, r, dir, kif->pfik_ifp, s, &pd, inp);
6323 			return (action);
6324 		}
6325 		break;
6326 	}
6327 	if (s)
6328 		PF_STATE_UNLOCK(s);
6329 
6330 	SDT_PROBE4(pf, ip, test, done, action, reason, r, s);
6331 
6332 	return (action);
6333 }
6334 #endif /* INET */
6335 
6336 #ifdef INET6
6337 int
6338 pf_test6(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp)
6339 {
6340 	struct pfi_kkif		*kif;
6341 	u_short			 action, reason = 0, log = 0;
6342 	struct mbuf		*m = *m0, *n = NULL;
6343 	struct m_tag		*mtag;
6344 	struct ip6_hdr		*h = NULL;
6345 	struct pf_krule		*a = NULL, *r = &V_pf_default_rule, *tr, *nr;
6346 	struct pf_state		*s = NULL;
6347 	struct pf_kruleset	*ruleset = NULL;
6348 	struct pf_pdesc		 pd;
6349 	int			 off, terminal = 0, dirndx, rh_cnt = 0, pqid = 0;
6350 
6351 	PF_RULES_RLOCK_TRACKER;
6352 	M_ASSERTPKTHDR(m);
6353 
6354 	if (!V_pf_status.running)
6355 		return (PF_PASS);
6356 
6357 	memset(&pd, 0, sizeof(pd));
6358 	pd.pf_mtag = pf_find_mtag(m);
6359 
6360 	if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED)
6361 		return (PF_PASS);
6362 
6363 	kif = (struct pfi_kkif *)ifp->if_pf_kif;
6364 	if (kif == NULL) {
6365 		DPFPRINTF(PF_DEBUG_URGENT,
6366 		    ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname));
6367 		return (PF_DROP);
6368 	}
6369 	if (kif->pfik_flags & PFI_IFLAG_SKIP)
6370 		return (PF_PASS);
6371 
6372 	if (m->m_flags & M_SKIP_FIREWALL)
6373 		return (PF_PASS);
6374 
6375 	PF_RULES_RLOCK();
6376 
6377 	/* We do IP header normalization and packet reassembly here */
6378 	if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) {
6379 		action = PF_DROP;
6380 		goto done;
6381 	}
6382 	m = *m0;	/* pf_normalize messes with m0 */
6383 	h = mtod(m, struct ip6_hdr *);
6384 
6385 	/*
6386 	 * we do not support jumbogram.  if we keep going, zero ip6_plen
6387 	 * will do something bad, so drop the packet for now.
6388 	 */
6389 	if (htons(h->ip6_plen) == 0) {
6390 		action = PF_DROP;
6391 		REASON_SET(&reason, PFRES_NORM);	/*XXX*/
6392 		goto done;
6393 	}
6394 
6395 	pd.src = (struct pf_addr *)&h->ip6_src;
6396 	pd.dst = (struct pf_addr *)&h->ip6_dst;
6397 	pd.sport = pd.dport = NULL;
6398 	pd.ip_sum = NULL;
6399 	pd.proto_sum = NULL;
6400 	pd.dir = dir;
6401 	pd.sidx = (dir == PF_IN) ? 0 : 1;
6402 	pd.didx = (dir == PF_IN) ? 1 : 0;
6403 	pd.af = AF_INET6;
6404 	pd.tos = IPV6_DSCP(h);
6405 	pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
6406 
6407 	off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
6408 	pd.proto = h->ip6_nxt;
6409 	do {
6410 		switch (pd.proto) {
6411 		case IPPROTO_FRAGMENT:
6412 			action = pf_test_fragment(&r, dir, kif, m, h,
6413 			    &pd, &a, &ruleset);
6414 			if (action == PF_DROP)
6415 				REASON_SET(&reason, PFRES_FRAG);
6416 			goto done;
6417 		case IPPROTO_ROUTING: {
6418 			struct ip6_rthdr rthdr;
6419 
6420 			if (rh_cnt++) {
6421 				DPFPRINTF(PF_DEBUG_MISC,
6422 				    ("pf: IPv6 more than one rthdr\n"));
6423 				action = PF_DROP;
6424 				REASON_SET(&reason, PFRES_IPOPTIONS);
6425 				log = 1;
6426 				goto done;
6427 			}
6428 			if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL,
6429 			    &reason, pd.af)) {
6430 				DPFPRINTF(PF_DEBUG_MISC,
6431 				    ("pf: IPv6 short rthdr\n"));
6432 				action = PF_DROP;
6433 				REASON_SET(&reason, PFRES_SHORT);
6434 				log = 1;
6435 				goto done;
6436 			}
6437 			if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
6438 				DPFPRINTF(PF_DEBUG_MISC,
6439 				    ("pf: IPv6 rthdr0\n"));
6440 				action = PF_DROP;
6441 				REASON_SET(&reason, PFRES_IPOPTIONS);
6442 				log = 1;
6443 				goto done;
6444 			}
6445 			/* FALLTHROUGH */
6446 		}
6447 		case IPPROTO_AH:
6448 		case IPPROTO_HOPOPTS:
6449 		case IPPROTO_DSTOPTS: {
6450 			/* get next header and header length */
6451 			struct ip6_ext	opt6;
6452 
6453 			if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
6454 			    NULL, &reason, pd.af)) {
6455 				DPFPRINTF(PF_DEBUG_MISC,
6456 				    ("pf: IPv6 short opt\n"));
6457 				action = PF_DROP;
6458 				log = 1;
6459 				goto done;
6460 			}
6461 			if (pd.proto == IPPROTO_AH)
6462 				off += (opt6.ip6e_len + 2) * 4;
6463 			else
6464 				off += (opt6.ip6e_len + 1) * 8;
6465 			pd.proto = opt6.ip6e_nxt;
6466 			/* goto the next header */
6467 			break;
6468 		}
6469 		default:
6470 			terminal++;
6471 			break;
6472 		}
6473 	} while (!terminal);
6474 
6475 	/* if there's no routing header, use unmodified mbuf for checksumming */
6476 	if (!n)
6477 		n = m;
6478 
6479 	switch (pd.proto) {
6480 	case IPPROTO_TCP: {
6481 		struct tcphdr	th;
6482 
6483 		pd.hdr.tcp = &th;
6484 		if (!pf_pull_hdr(m, off, &th, sizeof(th),
6485 		    &action, &reason, AF_INET6)) {
6486 			log = action != PF_PASS;
6487 			goto done;
6488 		}
6489 		pd.p_len = pd.tot_len - off - (th.th_off << 2);
6490 		action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
6491 		if (action == PF_DROP)
6492 			goto done;
6493 		action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
6494 		    &reason);
6495 		if (action == PF_PASS) {
6496 			if (V_pfsync_update_state_ptr != NULL)
6497 				V_pfsync_update_state_ptr(s);
6498 			r = s->rule.ptr;
6499 			a = s->anchor.ptr;
6500 			log = s->log;
6501 		} else if (s == NULL)
6502 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6503 			    &a, &ruleset, inp);
6504 		break;
6505 	}
6506 
6507 	case IPPROTO_UDP: {
6508 		struct udphdr	uh;
6509 
6510 		pd.hdr.udp = &uh;
6511 		if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
6512 		    &action, &reason, AF_INET6)) {
6513 			log = action != PF_PASS;
6514 			goto done;
6515 		}
6516 		if (uh.uh_dport == 0 ||
6517 		    ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
6518 		    ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
6519 			action = PF_DROP;
6520 			REASON_SET(&reason, PFRES_SHORT);
6521 			goto done;
6522 		}
6523 		action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
6524 		if (action == PF_PASS) {
6525 			if (V_pfsync_update_state_ptr != NULL)
6526 				V_pfsync_update_state_ptr(s);
6527 			r = s->rule.ptr;
6528 			a = s->anchor.ptr;
6529 			log = s->log;
6530 		} else if (s == NULL)
6531 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6532 			    &a, &ruleset, inp);
6533 		break;
6534 	}
6535 
6536 	case IPPROTO_ICMP: {
6537 		action = PF_DROP;
6538 		DPFPRINTF(PF_DEBUG_MISC,
6539 		    ("pf: dropping IPv6 packet with ICMPv4 payload\n"));
6540 		goto done;
6541 	}
6542 
6543 	case IPPROTO_ICMPV6: {
6544 		struct icmp6_hdr	ih;
6545 
6546 		pd.hdr.icmp6 = &ih;
6547 		if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
6548 		    &action, &reason, AF_INET6)) {
6549 			log = action != PF_PASS;
6550 			goto done;
6551 		}
6552 		action = pf_test_state_icmp(&s, dir, kif,
6553 		    m, off, h, &pd, &reason);
6554 		if (action == PF_PASS) {
6555 			if (V_pfsync_update_state_ptr != NULL)
6556 				V_pfsync_update_state_ptr(s);
6557 			r = s->rule.ptr;
6558 			a = s->anchor.ptr;
6559 			log = s->log;
6560 		} else if (s == NULL)
6561 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6562 			    &a, &ruleset, inp);
6563 		break;
6564 	}
6565 
6566 	default:
6567 		action = pf_test_state_other(&s, dir, kif, m, &pd);
6568 		if (action == PF_PASS) {
6569 			if (V_pfsync_update_state_ptr != NULL)
6570 				V_pfsync_update_state_ptr(s);
6571 			r = s->rule.ptr;
6572 			a = s->anchor.ptr;
6573 			log = s->log;
6574 		} else if (s == NULL)
6575 			action = pf_test_rule(&r, &s, dir, kif, m, off, &pd,
6576 			    &a, &ruleset, inp);
6577 		break;
6578 	}
6579 
6580 done:
6581 	PF_RULES_RUNLOCK();
6582 	if (n != m) {
6583 		m_freem(n);
6584 		n = NULL;
6585 	}
6586 
6587 	/* handle dangerous IPv6 extension headers. */
6588 	if (action == PF_PASS && rh_cnt &&
6589 	    !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
6590 		action = PF_DROP;
6591 		REASON_SET(&reason, PFRES_IPOPTIONS);
6592 		log = r->log;
6593 		DPFPRINTF(PF_DEBUG_MISC,
6594 		    ("pf: dropping packet with dangerous v6 headers\n"));
6595 	}
6596 
6597 	if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) {
6598 		action = PF_DROP;
6599 		REASON_SET(&reason, PFRES_MEMORY);
6600 	}
6601 	if (r->rtableid >= 0)
6602 		M_SETFIB(m, r->rtableid);
6603 
6604 	if (r->scrub_flags & PFSTATE_SETPRIO) {
6605 		if (pd.tos & IPTOS_LOWDELAY)
6606 			pqid = 1;
6607 		if (pf_ieee8021q_setpcp(m, r->set_prio[pqid])) {
6608 			action = PF_DROP;
6609 			REASON_SET(&reason, PFRES_MEMORY);
6610 			log = 1;
6611 			DPFPRINTF(PF_DEBUG_MISC,
6612 			    ("pf: failed to allocate 802.1q mtag\n"));
6613 		}
6614 	}
6615 
6616 #ifdef ALTQ
6617 	if (action == PF_PASS && r->qid) {
6618 		if (pd.pf_mtag == NULL &&
6619 		    ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) {
6620 			action = PF_DROP;
6621 			REASON_SET(&reason, PFRES_MEMORY);
6622 		} else {
6623 			if (s != NULL)
6624 				pd.pf_mtag->qid_hash = pf_state_hash(s);
6625 			if (pd.tos & IPTOS_LOWDELAY)
6626 				pd.pf_mtag->qid = r->pqid;
6627 			else
6628 				pd.pf_mtag->qid = r->qid;
6629 			/* Add hints for ecn. */
6630 			pd.pf_mtag->hdr = h;
6631 		}
6632 	}
6633 #endif /* ALTQ */
6634 
6635 	if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
6636 	    pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
6637 	    (s->nat_rule.ptr->action == PF_RDR ||
6638 	    s->nat_rule.ptr->action == PF_BINAT) &&
6639 	    IN6_IS_ADDR_LOOPBACK(&pd.dst->v6))
6640 		m->m_flags |= M_SKIP_FIREWALL;
6641 
6642 	/* XXX: Anybody working on it?! */
6643 	if (r->divert.port)
6644 		printf("pf: divert(9) is not supported for IPv6\n");
6645 
6646 	if (log) {
6647 		struct pf_krule *lr;
6648 
6649 		if (s != NULL && s->nat_rule.ptr != NULL &&
6650 		    s->nat_rule.ptr->log & PF_LOG_ALL)
6651 			lr = s->nat_rule.ptr;
6652 		else
6653 			lr = r;
6654 		PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset,
6655 		    &pd, (s == NULL));
6656 	}
6657 
6658 	counter_u64_add(kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS],
6659 	    pd.tot_len);
6660 	counter_u64_add(kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS],
6661 	    1);
6662 
6663 	if (action == PF_PASS || r->action == PF_DROP) {
6664 		dirndx = (dir == PF_OUT);
6665 		counter_u64_add(r->packets[dirndx], 1);
6666 		counter_u64_add(r->bytes[dirndx], pd.tot_len);
6667 		if (a != NULL) {
6668 			counter_u64_add(a->packets[dirndx], 1);
6669 			counter_u64_add(a->bytes[dirndx], pd.tot_len);
6670 		}
6671 		if (s != NULL) {
6672 			if (s->nat_rule.ptr != NULL) {
6673 				counter_u64_add(s->nat_rule.ptr->packets[dirndx],
6674 				    1);
6675 				counter_u64_add(s->nat_rule.ptr->bytes[dirndx],
6676 				    pd.tot_len);
6677 			}
6678 			if (s->src_node != NULL) {
6679 				counter_u64_add(s->src_node->packets[dirndx],
6680 				    1);
6681 				counter_u64_add(s->src_node->bytes[dirndx],
6682 				    pd.tot_len);
6683 			}
6684 			if (s->nat_src_node != NULL) {
6685 				counter_u64_add(s->nat_src_node->packets[dirndx],
6686 				    1);
6687 				counter_u64_add(s->nat_src_node->bytes[dirndx],
6688 				    pd.tot_len);
6689 			}
6690 			dirndx = (dir == s->direction) ? 0 : 1;
6691 			counter_u64_add(s->packets[dirndx], 1);
6692 			counter_u64_add(s->bytes[dirndx], pd.tot_len);
6693 		}
6694 		tr = r;
6695 		nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
6696 		if (nr != NULL && r == &V_pf_default_rule)
6697 			tr = nr;
6698 		if (tr->src.addr.type == PF_ADDR_TABLE)
6699 			pfr_update_stats(tr->src.addr.p.tbl,
6700 			    (s == NULL) ? pd.src :
6701 			    &s->key[(s->direction == PF_IN)]->addr[0],
6702 			    pd.af, pd.tot_len, dir == PF_OUT,
6703 			    r->action == PF_PASS, tr->src.neg);
6704 		if (tr->dst.addr.type == PF_ADDR_TABLE)
6705 			pfr_update_stats(tr->dst.addr.p.tbl,
6706 			    (s == NULL) ? pd.dst :
6707 			    &s->key[(s->direction == PF_IN)]->addr[1],
6708 			    pd.af, pd.tot_len, dir == PF_OUT,
6709 			    r->action == PF_PASS, tr->dst.neg);
6710 	}
6711 
6712 	switch (action) {
6713 	case PF_SYNPROXY_DROP:
6714 		m_freem(*m0);
6715 	case PF_DEFER:
6716 		*m0 = NULL;
6717 		action = PF_PASS;
6718 		break;
6719 	case PF_DROP:
6720 		m_freem(*m0);
6721 		*m0 = NULL;
6722 		break;
6723 	default:
6724 		/* pf_route6() returns unlocked. */
6725 		if (r->rt) {
6726 			pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd, inp);
6727 			return (action);
6728 		}
6729 		break;
6730 	}
6731 
6732 	if (s)
6733 		PF_STATE_UNLOCK(s);
6734 
6735 	/* If reassembled packet passed, create new fragments. */
6736 	if (action == PF_PASS && *m0 && (pflags & PFIL_FWD) &&
6737 	    (mtag = m_tag_find(m, PF_REASSEMBLED, NULL)) != NULL)
6738 		action = pf_refragment6(ifp, m0, mtag);
6739 
6740 	SDT_PROBE4(pf, ip, test6, done, action, reason, r, s);
6741 
6742 	return (action);
6743 }
6744 #endif /* INET6 */
6745