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