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