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