1 /* $FreeBSD: src/sys/contrib/pf/net/pf.c,v 1.19 2004/09/11 11:18:25 mlaier Exp $ */
2 /* $OpenBSD: pf.c,v 1.433.2.2 2004/07/17 03:22:34 brad Exp $ */
3 /* add $OpenBSD: pf.c,v 1.448 2004/05/11 07:34:11 dhartmei Exp $ */
4 /* $DragonFly: src/sys/net/pf/pf.c,v 1.4 2005/02/11 22:25:57 joerg Exp $ */
7 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
9 * Copyright (c) 2001 Daniel Hartmeier
10 * Copyright (c) 2002,2003 Henning Brauer
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
37 * Effort sponsored in part by the Defense Advanced Research Projects
38 * Agency (DARPA) and Air Force Research Laboratory, Air Force
39 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
44 #include "opt_inet6.h"
45 #include "use_pfsync.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/filio.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/kernel.h>
56 #include <sys/sysctl.h>
57 #include <sys/endian.h>
58 #include <vm/vm_zone.h>
60 #include <machine/inttypes.h>
63 #include <net/if_types.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_seq.h>
74 #include <netinet/udp.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/udp_var.h>
80 #include <netinet/icmp_var.h>
82 #include <net/pf/pfvar.h>
83 #include <net/pf/if_pflog.h>
86 #include <net/pf/if_pfsync.h>
87 #endif /* NPFSYNC > 0 */
90 #include <netinet/ip6.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/in6_pcb.h>
98 #include <sys/in_cksum.h>
99 #include <machine/limits.h>
100 #include <sys/msgport2.h>
101 #include <sys/ucred.h>
103 extern int ip_optcopy(struct ip *, struct ip *);
105 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
111 struct pf_anchorqueue pf_anchors;
112 struct pf_ruleset pf_main_ruleset;
113 struct pf_altqqueue pf_altqs[2];
114 struct pf_palist pf_pabuf;
115 struct pf_altqqueue *pf_altqs_active;
116 struct pf_altqqueue *pf_altqs_inactive;
117 struct pf_status pf_status;
119 u_int32_t ticket_altqs_active;
120 u_int32_t ticket_altqs_inactive;
121 int altqs_inactive_open;
122 u_int32_t ticket_pabuf;
124 struct callout pf_expire_to; /* expire timeout */
126 vm_zone_t pf_src_tree_pl, pf_rule_pl;
127 vm_zone_t pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
129 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
130 void pf_print_state(struct pf_state *);
131 void pf_print_flags(u_int8_t);
133 u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
135 void pf_change_ap(struct pf_addr *, u_int16_t *,
136 u_int16_t *, u_int16_t *, struct pf_addr *,
137 u_int16_t, u_int8_t, sa_family_t);
139 void pf_change_a6(struct pf_addr *, u_int16_t *,
140 struct pf_addr *, u_int8_t);
142 void pf_change_icmp(struct pf_addr *, u_int16_t *,
143 struct pf_addr *, struct pf_addr *, u_int16_t,
144 u_int16_t *, u_int16_t *, u_int16_t *,
145 u_int16_t *, u_int8_t, sa_family_t);
146 void pf_send_tcp(const struct pf_rule *, sa_family_t,
147 const struct pf_addr *, const struct pf_addr *,
148 u_int16_t, u_int16_t, u_int32_t, u_int32_t,
149 u_int8_t, u_int16_t, u_int16_t, u_int8_t);
150 void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
151 sa_family_t, struct pf_rule *);
152 struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *,
153 int, int, struct pfi_kif *,
154 struct pf_addr *, u_int16_t, struct pf_addr *,
156 struct pf_rule *pf_get_translation(struct pf_pdesc *, struct mbuf *,
157 int, int, struct pfi_kif *, struct pf_src_node **,
158 struct pf_addr *, u_int16_t,
159 struct pf_addr *, u_int16_t,
160 struct pf_addr *, u_int16_t *);
161 int pf_test_tcp(struct pf_rule **, struct pf_state **,
162 int, struct pfi_kif *, struct mbuf *, int,
163 void *, struct pf_pdesc *, struct pf_rule **,
164 struct pf_ruleset **);
165 int pf_test_udp(struct pf_rule **, struct pf_state **,
166 int, struct pfi_kif *, struct mbuf *, int,
167 void *, struct pf_pdesc *, struct pf_rule **,
168 struct pf_ruleset **);
169 int pf_test_icmp(struct pf_rule **, struct pf_state **,
170 int, struct pfi_kif *, struct mbuf *, int,
171 void *, struct pf_pdesc *, struct pf_rule **,
172 struct pf_ruleset **);
173 int pf_test_other(struct pf_rule **, struct pf_state **,
174 int, struct pfi_kif *, struct mbuf *, int, void *,
175 struct pf_pdesc *, struct pf_rule **,
176 struct pf_ruleset **);
177 int pf_test_fragment(struct pf_rule **, int,
178 struct pfi_kif *, struct mbuf *, void *,
179 struct pf_pdesc *, struct pf_rule **,
180 struct pf_ruleset **);
181 int pf_test_state_tcp(struct pf_state **, int,
182 struct pfi_kif *, struct mbuf *, int,
183 void *, struct pf_pdesc *, u_short *);
184 int pf_test_state_udp(struct pf_state **, int,
185 struct pfi_kif *, struct mbuf *, int,
186 void *, struct pf_pdesc *);
187 int pf_test_state_icmp(struct pf_state **, int,
188 struct pfi_kif *, struct mbuf *, int,
189 void *, struct pf_pdesc *);
190 int pf_test_state_other(struct pf_state **, int,
191 struct pfi_kif *, struct pf_pdesc *);
192 static int pf_match_tag(struct mbuf *, struct pf_rule *,
193 struct pf_rule *, int *);
194 void pf_hash(struct pf_addr *, struct pf_addr *,
195 struct pf_poolhashkey *, sa_family_t);
196 int pf_map_addr(u_int8_t, struct pf_rule *,
197 struct pf_addr *, struct pf_addr *,
198 struct pf_addr *, struct pf_src_node **);
199 int pf_get_sport(sa_family_t, u_int8_t, struct pf_rule *,
200 struct pf_addr *, struct pf_addr *, u_int16_t,
201 struct pf_addr *, u_int16_t*, u_int16_t, u_int16_t,
202 struct pf_src_node **);
203 void pf_route(struct mbuf **, struct pf_rule *, int,
204 struct ifnet *, struct pf_state *);
205 void pf_route6(struct mbuf **, struct pf_rule *, int,
206 struct ifnet *, struct pf_state *);
207 int pf_socket_lookup(uid_t *, gid_t *,
208 int, struct pf_pdesc *);
209 u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t,
211 u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t,
213 u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t,
215 void pf_set_rt_ifp(struct pf_state *,
217 int pf_check_proto_cksum(struct mbuf *, int, int,
218 u_int8_t, sa_family_t);
219 int pf_addr_wrap_neq(struct pf_addr_wrap *,
220 struct pf_addr_wrap *);
221 struct pf_state *pf_find_state_recurse(struct pfi_kif *,
222 struct pf_state *, u_int8_t);
224 struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
226 #define STATE_LOOKUP() \
228 if (direction == PF_IN) \
229 *state = pf_find_state_recurse( \
230 kif, &key, PF_EXT_GWY); \
232 *state = pf_find_state_recurse( \
233 kif, &key, PF_LAN_EXT); \
234 if (*state == NULL) \
236 if (direction == PF_OUT && \
237 (((*state)->rule.ptr->rt == PF_ROUTETO && \
238 (*state)->rule.ptr->direction == PF_OUT) || \
239 ((*state)->rule.ptr->rt == PF_REPLYTO && \
240 (*state)->rule.ptr->direction == PF_IN)) && \
241 (*state)->rt_kif != NULL && \
242 (*state)->rt_kif != kif) \
246 #define STATE_TRANSLATE(s) \
247 (s)->lan.addr.addr32[0] != (s)->gwy.addr.addr32[0] || \
248 ((s)->af == AF_INET6 && \
249 ((s)->lan.addr.addr32[1] != (s)->gwy.addr.addr32[1] || \
250 (s)->lan.addr.addr32[2] != (s)->gwy.addr.addr32[2] || \
251 (s)->lan.addr.addr32[3] != (s)->gwy.addr.addr32[3])) || \
252 (s)->lan.port != (s)->gwy.port
254 #define BOUND_IFACE(r, k) (((r)->rule_flag & PFRULE_IFBOUND) ? (k) : \
255 ((r)->rule_flag & PFRULE_GRBOUND) ? (k)->pfik_parent : \
256 (k)->pfik_parent->pfik_parent)
258 static int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
259 static int pf_state_compare_lan_ext(struct pf_state *,
261 static int pf_state_compare_ext_gwy(struct pf_state *,
263 static int pf_state_compare_id(struct pf_state *,
266 struct pf_src_tree tree_src_tracking;
268 struct pf_state_tree_id tree_id;
269 struct pf_state_queue state_updates;
271 RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
272 RB_GENERATE(pf_state_tree_lan_ext, pf_state,
273 u.s.entry_lan_ext, pf_state_compare_lan_ext);
274 RB_GENERATE(pf_state_tree_ext_gwy, pf_state,
275 u.s.entry_ext_gwy, pf_state_compare_ext_gwy);
276 RB_GENERATE(pf_state_tree_id, pf_state,
277 u.s.entry_id, pf_state_compare_id);
280 pf_src_compare(struct pf_src_node *a, struct pf_src_node *b)
284 if (a->rule.ptr > b->rule.ptr)
286 if (a->rule.ptr < b->rule.ptr)
288 if ((diff = a->af - b->af) != 0)
293 if (a->addr.addr32[0] > b->addr.addr32[0])
295 if (a->addr.addr32[0] < b->addr.addr32[0])
301 if (a->addr.addr32[3] > b->addr.addr32[3])
303 if (a->addr.addr32[3] < b->addr.addr32[3])
305 if (a->addr.addr32[2] > b->addr.addr32[2])
307 if (a->addr.addr32[2] < b->addr.addr32[2])
309 if (a->addr.addr32[1] > b->addr.addr32[1])
311 if (a->addr.addr32[1] < b->addr.addr32[1])
313 if (a->addr.addr32[0] > b->addr.addr32[0])
315 if (a->addr.addr32[0] < b->addr.addr32[0])
324 pf_state_compare_lan_ext(struct pf_state *a, struct pf_state *b)
328 if ((diff = a->proto - b->proto) != 0)
330 if ((diff = a->af - b->af) != 0)
335 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
337 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
339 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
341 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
347 if (a->lan.addr.addr32[3] > b->lan.addr.addr32[3])
349 if (a->lan.addr.addr32[3] < b->lan.addr.addr32[3])
351 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
353 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
355 if (a->lan.addr.addr32[2] > b->lan.addr.addr32[2])
357 if (a->lan.addr.addr32[2] < b->lan.addr.addr32[2])
359 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
361 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
363 if (a->lan.addr.addr32[1] > b->lan.addr.addr32[1])
365 if (a->lan.addr.addr32[1] < b->lan.addr.addr32[1])
367 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
369 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
371 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
373 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
375 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
377 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
383 if ((diff = a->lan.port - b->lan.port) != 0)
385 if ((diff = a->ext.port - b->ext.port) != 0)
392 pf_state_compare_ext_gwy(struct pf_state *a, struct pf_state *b)
396 if ((diff = a->proto - b->proto) != 0)
398 if ((diff = a->af - b->af) != 0)
403 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
405 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
407 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
409 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
415 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
417 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
419 if (a->gwy.addr.addr32[3] > b->gwy.addr.addr32[3])
421 if (a->gwy.addr.addr32[3] < b->gwy.addr.addr32[3])
423 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
425 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
427 if (a->gwy.addr.addr32[2] > b->gwy.addr.addr32[2])
429 if (a->gwy.addr.addr32[2] < b->gwy.addr.addr32[2])
431 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
433 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
435 if (a->gwy.addr.addr32[1] > b->gwy.addr.addr32[1])
437 if (a->gwy.addr.addr32[1] < b->gwy.addr.addr32[1])
439 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
441 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
443 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
445 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
451 if ((diff = a->ext.port - b->ext.port) != 0)
453 if ((diff = a->gwy.port - b->gwy.port) != 0)
460 pf_state_compare_id(struct pf_state *a, struct pf_state *b)
466 if (a->creatorid > b->creatorid)
468 if (a->creatorid < b->creatorid)
476 pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
481 dst->addr32[0] = src->addr32[0];
485 dst->addr32[0] = src->addr32[0];
486 dst->addr32[1] = src->addr32[1];
487 dst->addr32[2] = src->addr32[2];
488 dst->addr32[3] = src->addr32[3];
495 pf_find_state_byid(struct pf_state *key)
497 pf_status.fcounters[FCNT_STATE_SEARCH]++;
498 return (RB_FIND(pf_state_tree_id, &tree_id, key));
502 pf_find_state_recurse(struct pfi_kif *kif, struct pf_state *key, u_int8_t tree)
506 pf_status.fcounters[FCNT_STATE_SEARCH]++;
510 for (; kif != NULL; kif = kif->pfik_parent) {
511 s = RB_FIND(pf_state_tree_lan_ext,
512 &kif->pfik_lan_ext, key);
518 for (; kif != NULL; kif = kif->pfik_parent) {
519 s = RB_FIND(pf_state_tree_ext_gwy,
520 &kif->pfik_ext_gwy, key);
526 panic("pf_find_state_recurse");
531 pf_find_state_all(struct pf_state *key, u_int8_t tree, int *more)
533 struct pf_state *s, *ss = NULL;
536 pf_status.fcounters[FCNT_STATE_SEARCH]++;
540 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
541 s = RB_FIND(pf_state_tree_lan_ext,
542 &kif->pfik_lan_ext, key);
552 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
553 s = RB_FIND(pf_state_tree_ext_gwy,
554 &kif->pfik_ext_gwy, key);
564 panic("pf_find_state_all");
569 pf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule,
570 struct pf_addr *src, sa_family_t af)
572 struct pf_src_node k;
576 PF_ACPY(&k.addr, src, af);
577 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
578 rule->rpool.opts & PF_POOL_STICKYADDR)
582 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
583 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
586 if (!rule->max_src_nodes ||
587 rule->src_nodes < rule->max_src_nodes)
588 (*sn) = pool_get(&pf_src_tree_pl, PR_NOWAIT);
591 bzero(*sn, sizeof(struct pf_src_node));
593 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
594 rule->rpool.opts & PF_POOL_STICKYADDR)
595 (*sn)->rule.ptr = rule;
597 (*sn)->rule.ptr = NULL;
598 PF_ACPY(&(*sn)->addr, src, af);
599 if (RB_INSERT(pf_src_tree,
600 &tree_src_tracking, *sn) != NULL) {
601 if (pf_status.debug >= PF_DEBUG_MISC) {
602 printf("pf: src_tree insert failed: ");
603 pf_print_host(&(*sn)->addr, 0, af);
606 pool_put(&pf_src_tree_pl, *sn);
609 (*sn)->creation = time_second;
610 (*sn)->ruletype = rule->action;
611 if ((*sn)->rule.ptr != NULL)
612 (*sn)->rule.ptr->src_nodes++;
613 pf_status.scounters[SCNT_SRC_NODE_INSERT]++;
614 pf_status.src_nodes++;
616 if (rule->max_src_states &&
617 (*sn)->states >= rule->max_src_states)
624 pf_insert_state(struct pfi_kif *kif, struct pf_state *state)
626 /* Thou MUST NOT insert multiple duplicate keys */
627 state->u.s.kif = kif;
628 if (RB_INSERT(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state)) {
629 if (pf_status.debug >= PF_DEBUG_MISC) {
630 printf("pf: state insert failed: tree_lan_ext");
632 pf_print_host(&state->lan.addr, state->lan.port,
635 pf_print_host(&state->gwy.addr, state->gwy.port,
638 pf_print_host(&state->ext.addr, state->ext.port,
640 if (state->sync_flags & PFSTATE_FROMSYNC)
641 printf(" (from sync)");
647 if (RB_INSERT(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state)) {
648 if (pf_status.debug >= PF_DEBUG_MISC) {
649 printf("pf: state insert failed: tree_ext_gwy");
651 pf_print_host(&state->lan.addr, state->lan.port,
654 pf_print_host(&state->gwy.addr, state->gwy.port,
657 pf_print_host(&state->ext.addr, state->ext.port,
659 if (state->sync_flags & PFSTATE_FROMSYNC)
660 printf(" (from sync)");
663 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
667 if (state->id == 0 && state->creatorid == 0) {
668 state->id = htobe64(pf_status.stateid++);
669 state->creatorid = pf_status.hostid;
671 if (RB_INSERT(pf_state_tree_id, &tree_id, state) != NULL) {
672 if (pf_status.debug >= PF_DEBUG_MISC) {
673 printf("pf: state insert failed: "
674 "id: %016" PRIx64 " creatorid: %08" PRIx32,
675 be64toh(state->id), ntohl(state->creatorid));
676 if (state->sync_flags & PFSTATE_FROMSYNC)
677 printf(" (from sync)");
680 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
681 RB_REMOVE(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state);
684 TAILQ_INSERT_HEAD(&state_updates, state, u.s.entry_updates);
686 pf_status.fcounters[FCNT_STATE_INSERT]++;
688 pfi_attach_state(kif);
690 pfsync_insert_state(state);
696 pf_purge_timeout(void *arg)
698 struct callout *to = arg;
702 pf_purge_expired_states();
703 pf_purge_expired_fragments();
704 pf_purge_expired_src_nodes();
707 callout_reset(to, pf_default_rule.timeout[PFTM_INTERVAL] * hz,
708 pf_purge_timeout, to);
712 pf_state_expires(const struct pf_state *state)
719 /* handle all PFTM_* > PFTM_MAX here */
720 if (state->timeout == PFTM_PURGE)
721 return (time_second);
722 if (state->timeout == PFTM_UNTIL_PACKET)
724 KASSERT((state->timeout < PFTM_MAX),
725 ("pf_state_expires: timeout > PFTM_MAX"));
726 timeout = state->rule.ptr->timeout[state->timeout];
728 timeout = pf_default_rule.timeout[state->timeout];
729 start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
731 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
732 states = state->rule.ptr->states;
734 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
735 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
736 states = pf_status.states;
738 if (end && states > start && start < end) {
740 return (state->expire + timeout * (end - states) /
743 return (time_second);
745 return (state->expire + timeout);
749 pf_purge_expired_src_nodes(void)
751 struct pf_src_node *cur, *next;
753 for (cur = RB_MIN(pf_src_tree, &tree_src_tracking); cur; cur = next) {
754 next = RB_NEXT(pf_src_tree, &tree_src_tracking, cur);
756 if (cur->states <= 0 && cur->expire <= time_second) {
757 if (cur->rule.ptr != NULL) {
758 cur->rule.ptr->src_nodes--;
759 if (cur->rule.ptr->states <= 0 &&
760 cur->rule.ptr->max_src_nodes <= 0)
761 pf_rm_rule(NULL, cur->rule.ptr);
763 RB_REMOVE(pf_src_tree, &tree_src_tracking, cur);
764 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
765 pf_status.src_nodes--;
766 pool_put(&pf_src_tree_pl, cur);
772 pf_src_tree_remove_state(struct pf_state *s)
776 if (s->src_node != NULL) {
777 if (--s->src_node->states <= 0) {
778 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
781 pf_default_rule.timeout[PFTM_SRC_NODE];
782 s->src_node->expire = time_second + timeout;
785 if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
786 if (--s->nat_src_node->states <= 0) {
787 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
790 pf_default_rule.timeout[PFTM_SRC_NODE];
791 s->nat_src_node->expire = time_second + timeout;
794 s->src_node = s->nat_src_node = NULL;
798 pf_purge_expired_states(void)
800 struct pf_state *cur, *next;
802 for (cur = RB_MIN(pf_state_tree_id, &tree_id);
804 next = RB_NEXT(pf_state_tree_id, &tree_id, cur);
806 if (pf_state_expires(cur) <= time_second) {
807 if (cur->src.state == PF_TCPS_PROXY_DST)
808 pf_send_tcp(cur->rule.ptr, cur->af,
809 &cur->ext.addr, &cur->lan.addr,
810 cur->ext.port, cur->lan.port,
811 cur->src.seqhi, cur->src.seqlo + 1, 0,
812 TH_RST|TH_ACK, 0, 0);
813 RB_REMOVE(pf_state_tree_ext_gwy,
814 &cur->u.s.kif->pfik_ext_gwy, cur);
815 RB_REMOVE(pf_state_tree_lan_ext,
816 &cur->u.s.kif->pfik_lan_ext, cur);
817 RB_REMOVE(pf_state_tree_id, &tree_id, cur);
819 pfsync_delete_state(cur);
821 pf_src_tree_remove_state(cur);
822 if (--cur->rule.ptr->states <= 0 &&
823 cur->rule.ptr->src_nodes <= 0)
824 pf_rm_rule(NULL, cur->rule.ptr);
825 if (cur->nat_rule.ptr != NULL)
826 if (--cur->nat_rule.ptr->states <= 0 &&
827 cur->nat_rule.ptr->src_nodes <= 0)
828 pf_rm_rule(NULL, cur->nat_rule.ptr);
829 if (cur->anchor.ptr != NULL)
830 if (--cur->anchor.ptr->states <= 0)
831 pf_rm_rule(NULL, cur->anchor.ptr);
832 pf_normalize_tcp_cleanup(cur);
833 pfi_detach_state(cur->u.s.kif);
834 TAILQ_REMOVE(&state_updates, cur, u.s.entry_updates);
835 pool_put(&pf_state_pl, cur);
836 pf_status.fcounters[FCNT_STATE_REMOVALS]++;
843 pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
845 if (aw->type != PF_ADDR_TABLE)
847 if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
853 pf_tbladdr_remove(struct pf_addr_wrap *aw)
855 if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
857 pfr_detach_table(aw->p.tbl);
862 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
864 struct pfr_ktable *kt = aw->p.tbl;
866 if (aw->type != PF_ADDR_TABLE || kt == NULL)
868 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
871 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
876 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
881 u_int32_t a = ntohl(addr->addr32[0]);
882 printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
894 u_int8_t i, curstart = 255, curend = 0,
895 maxstart = 0, maxend = 0;
896 for (i = 0; i < 8; i++) {
897 if (!addr->addr16[i]) {
904 if ((curend - curstart) >
905 (maxend - maxstart)) {
913 for (i = 0; i < 8; i++) {
914 if (i >= maxstart && i <= maxend) {
923 b = ntohs(addr->addr16[i]);
940 pf_print_state(struct pf_state *s)
956 printf("%u ", s->proto);
959 pf_print_host(&s->lan.addr, s->lan.port, s->af);
961 pf_print_host(&s->gwy.addr, s->gwy.port, s->af);
963 pf_print_host(&s->ext.addr, s->ext.port, s->af);
964 printf(" [lo=%u high=%u win=%u modulator=%u", s->src.seqlo,
965 s->src.seqhi, s->src.max_win, s->src.seqdiff);
966 if (s->src.wscale && s->dst.wscale)
967 printf(" wscale=%u", s->src.wscale & PF_WSCALE_MASK);
969 printf(" [lo=%u high=%u win=%u modulator=%u", s->dst.seqlo,
970 s->dst.seqhi, s->dst.max_win, s->dst.seqdiff);
971 if (s->src.wscale && s->dst.wscale)
972 printf(" wscale=%u", s->dst.wscale & PF_WSCALE_MASK);
974 printf(" %u:%u", s->src.state, s->dst.state);
978 pf_print_flags(u_int8_t f)
1000 #define PF_SET_SKIP_STEPS(i) \
1002 while (head[i] != cur) { \
1003 head[i]->skip[i].ptr = cur; \
1004 head[i] = TAILQ_NEXT(head[i], entries); \
1009 pf_calc_skip_steps(struct pf_rulequeue *rules)
1011 struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1014 cur = TAILQ_FIRST(rules);
1016 for (i = 0; i < PF_SKIP_COUNT; ++i)
1018 while (cur != NULL) {
1020 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1021 PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1022 if (cur->direction != prev->direction)
1023 PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1024 if (cur->af != prev->af)
1025 PF_SET_SKIP_STEPS(PF_SKIP_AF);
1026 if (cur->proto != prev->proto)
1027 PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
1028 if (cur->src.not != prev->src.not ||
1029 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1030 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1031 if (cur->src.port[0] != prev->src.port[0] ||
1032 cur->src.port[1] != prev->src.port[1] ||
1033 cur->src.port_op != prev->src.port_op)
1034 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
1035 if (cur->dst.not != prev->dst.not ||
1036 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1037 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1038 if (cur->dst.port[0] != prev->dst.port[0] ||
1039 cur->dst.port[1] != prev->dst.port[1] ||
1040 cur->dst.port_op != prev->dst.port_op)
1041 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1044 cur = TAILQ_NEXT(cur, entries);
1046 for (i = 0; i < PF_SKIP_COUNT; ++i)
1047 PF_SET_SKIP_STEPS(i);
1051 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1053 if (aw1->type != aw2->type)
1055 switch (aw1->type) {
1056 case PF_ADDR_ADDRMASK:
1057 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1059 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1062 case PF_ADDR_DYNIFTL:
1063 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1064 case PF_ADDR_NOROUTE:
1067 return (aw1->p.tbl != aw2->p.tbl);
1069 printf("invalid address type: %d\n", aw1->type);
1075 pf_update_anchor_rules()
1077 struct pf_rule *rule;
1080 for (i = 0; i < PF_RULESET_MAX; ++i)
1081 TAILQ_FOREACH(rule, pf_main_ruleset.rules[i].active.ptr,
1083 if (rule->anchorname[0])
1084 rule->anchor = pf_find_anchor(rule->anchorname);
1086 rule->anchor = NULL;
1090 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1096 l = cksum + old - new;
1097 l = (l >> 16) + (l & 65535);
1105 pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1106 struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1111 PF_ACPY(&ao, a, af);
1119 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1120 ao.addr16[0], an->addr16[0], 0),
1121 ao.addr16[1], an->addr16[1], 0);
1123 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1124 ao.addr16[0], an->addr16[0], u),
1125 ao.addr16[1], an->addr16[1], u),
1131 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1132 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1133 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1134 ao.addr16[0], an->addr16[0], u),
1135 ao.addr16[1], an->addr16[1], u),
1136 ao.addr16[2], an->addr16[2], u),
1137 ao.addr16[3], an->addr16[3], u),
1138 ao.addr16[4], an->addr16[4], u),
1139 ao.addr16[5], an->addr16[5], u),
1140 ao.addr16[6], an->addr16[6], u),
1141 ao.addr16[7], an->addr16[7], u),
1149 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1151 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1155 memcpy(&ao, a, sizeof(ao));
1156 memcpy(a, &an, sizeof(u_int32_t));
1157 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1158 ao % 65536, an % 65536, u);
1163 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1167 PF_ACPY(&ao, a, AF_INET6);
1168 PF_ACPY(a, an, AF_INET6);
1170 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1171 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1172 pf_cksum_fixup(pf_cksum_fixup(*c,
1173 ao.addr16[0], an->addr16[0], u),
1174 ao.addr16[1], an->addr16[1], u),
1175 ao.addr16[2], an->addr16[2], u),
1176 ao.addr16[3], an->addr16[3], u),
1177 ao.addr16[4], an->addr16[4], u),
1178 ao.addr16[5], an->addr16[5], u),
1179 ao.addr16[6], an->addr16[6], u),
1180 ao.addr16[7], an->addr16[7], u);
1185 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1186 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1187 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1189 struct pf_addr oia, ooa;
1191 PF_ACPY(&oia, ia, af);
1192 PF_ACPY(&ooa, oa, af);
1194 /* Change inner protocol port, fix inner protocol checksum. */
1196 u_int16_t oip = *ip;
1203 *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1204 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1206 *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1208 /* Change inner ip address, fix inner ip and icmp checksums. */
1209 PF_ACPY(ia, na, af);
1213 u_int32_t oh2c = *h2c;
1215 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1216 oia.addr16[0], ia->addr16[0], 0),
1217 oia.addr16[1], ia->addr16[1], 0);
1218 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1219 oia.addr16[0], ia->addr16[0], 0),
1220 oia.addr16[1], ia->addr16[1], 0);
1221 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
1227 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1228 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1229 pf_cksum_fixup(pf_cksum_fixup(*ic,
1230 oia.addr16[0], ia->addr16[0], u),
1231 oia.addr16[1], ia->addr16[1], u),
1232 oia.addr16[2], ia->addr16[2], u),
1233 oia.addr16[3], ia->addr16[3], u),
1234 oia.addr16[4], ia->addr16[4], u),
1235 oia.addr16[5], ia->addr16[5], u),
1236 oia.addr16[6], ia->addr16[6], u),
1237 oia.addr16[7], ia->addr16[7], u);
1241 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1242 PF_ACPY(oa, na, af);
1246 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
1247 ooa.addr16[0], oa->addr16[0], 0),
1248 ooa.addr16[1], oa->addr16[1], 0);
1253 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1254 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1255 pf_cksum_fixup(pf_cksum_fixup(*ic,
1256 ooa.addr16[0], oa->addr16[0], u),
1257 ooa.addr16[1], oa->addr16[1], u),
1258 ooa.addr16[2], oa->addr16[2], u),
1259 ooa.addr16[3], oa->addr16[3], u),
1260 ooa.addr16[4], oa->addr16[4], u),
1261 ooa.addr16[5], oa->addr16[5], u),
1262 ooa.addr16[6], oa->addr16[6], u),
1263 ooa.addr16[7], oa->addr16[7], u);
1270 pf_send_tcp(const struct pf_rule *r, sa_family_t af,
1271 const struct pf_addr *saddr, const struct pf_addr *daddr,
1272 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
1273 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl)
1278 struct ip *h = NULL;
1281 struct ip6_hdr *h6 = NULL;
1283 struct tcphdr *th = NULL;
1286 /* maximum segment size tcp option */
1287 tlen = sizeof(struct tcphdr);
1294 len = sizeof(struct ip) + tlen;
1299 len = sizeof(struct ip6_hdr) + tlen;
1304 /* create outgoing mbuf */
1305 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1308 m->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
1310 if (r != NULL && r->qid) {
1311 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1312 m->m_pkthdr.altq_qid = r->qid;
1313 m->m_pkthdr.ecn_af = af;
1314 m->m_pkthdr.header = mtod(m, struct ip *);
1317 m->m_data += max_linkhdr;
1318 m->m_pkthdr.len = m->m_len = len;
1319 m->m_pkthdr.rcvif = NULL;
1320 bzero(m->m_data, len);
1324 h = mtod(m, struct ip *);
1326 /* IP header fields included in the TCP checksum */
1327 h->ip_p = IPPROTO_TCP;
1329 h->ip_src.s_addr = saddr->v4.s_addr;
1330 h->ip_dst.s_addr = daddr->v4.s_addr;
1332 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
1337 h6 = mtod(m, struct ip6_hdr *);
1339 /* IP header fields included in the TCP checksum */
1340 h6->ip6_nxt = IPPROTO_TCP;
1341 h6->ip6_plen = htons(tlen);
1342 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
1343 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
1345 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
1351 th->th_sport = sport;
1352 th->th_dport = dport;
1353 th->th_seq = htonl(seq);
1354 th->th_ack = htonl(ack);
1355 th->th_off = tlen >> 2;
1356 th->th_flags = flags;
1357 th->th_win = htons(win);
1360 opt = (char *)(th + 1);
1361 opt[0] = TCPOPT_MAXSEG;
1364 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
1371 th->th_sum = in_cksum(m, len);
1373 /* Finish the IP header */
1375 h->ip_hl = sizeof(*h) >> 2;
1376 h->ip_tos = IPTOS_LOWDELAY;
1378 h->ip_off = path_mtu_discovery ? IP_DF : 0;
1379 h->ip_ttl = ttl ? ttl : ip_defttl;
1381 ip_output(m, (void *)NULL, (void *)NULL, 0, (void *)NULL,
1388 th->th_sum = in6_cksum(m, IPPROTO_TCP,
1389 sizeof(struct ip6_hdr), tlen);
1391 h6->ip6_vfc |= IPV6_VERSION;
1392 h6->ip6_hlim = IPV6_DEFHLIM;
1394 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1401 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
1406 m0 = m_copypacket(m, MB_DONTWAIT);
1409 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
1413 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1414 m->m_pkthdr.altq_qid = r->qid;
1415 m->m_pkthdr.ecn_af = af;
1416 m->m_pkthdr.header = mtod(m0, struct ip *);
1423 icmp_error(m0, type, code, 0, (void *)NULL);
1428 icmp6_error(m0, type, code, 0);
1435 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
1436 * If n is 0, they match if they are equal. If n is != 0, they match if they
1440 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
1441 struct pf_addr *b, sa_family_t af)
1448 if ((a->addr32[0] & m->addr32[0]) ==
1449 (b->addr32[0] & m->addr32[0]))
1455 if (((a->addr32[0] & m->addr32[0]) ==
1456 (b->addr32[0] & m->addr32[0])) &&
1457 ((a->addr32[1] & m->addr32[1]) ==
1458 (b->addr32[1] & m->addr32[1])) &&
1459 ((a->addr32[2] & m->addr32[2]) ==
1460 (b->addr32[2] & m->addr32[2])) &&
1461 ((a->addr32[3] & m->addr32[3]) ==
1462 (b->addr32[3] & m->addr32[3])))
1481 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
1485 return ((p > a1) && (p < a2));
1487 return ((p < a1) || (p > a2));
1489 return ((p >= a1) && (p <= a2));
1503 return (0); /* never reached */
1507 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
1512 return (pf_match(op, a1, a2, p));
1516 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
1518 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1520 return (pf_match(op, a1, a2, u));
1524 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
1526 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1528 return (pf_match(op, a1, a2, g));
1532 pf_match_tag(struct mbuf *m, struct pf_rule *r, struct pf_rule *nat_rule,
1535 if (*tag == -1) { /* find mbuf tag */
1536 if (nat_rule != NULL && nat_rule->tag)
1537 *tag = nat_rule->tag;
1538 else if (m->m_pkthdr.fw_flags & PF_MBUF_TAGGED)
1539 *tag = m->m_pkthdr.pf_tag;
1544 return ((!r->match_tag_not && r->match_tag == *tag) ||
1545 (r->match_tag_not && r->match_tag != *tag));
1549 pf_tag_packet(struct mbuf *m, int tag)
1554 m->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
1555 m->m_pkthdr.pf_tag = tag;
1558 #define PF_STEP_INTO_ANCHOR(r, a, s, n) \
1560 if ((r) == NULL || (r)->anchor == NULL || \
1561 (s) != NULL || (a) != NULL) \
1562 panic("PF_STEP_INTO_ANCHOR"); \
1564 (s) = TAILQ_FIRST(&(r)->anchor->rulesets); \
1566 while ((s) != NULL && ((r) = \
1567 TAILQ_FIRST((s)->rules[n].active.ptr)) == NULL) \
1568 (s) = TAILQ_NEXT((s), entries); \
1569 if ((r) == NULL) { \
1570 (r) = TAILQ_NEXT((a), entries); \
1575 #define PF_STEP_OUT_OF_ANCHOR(r, a, s, n) \
1577 if ((r) != NULL || (a) == NULL || (s) == NULL) \
1578 panic("PF_STEP_OUT_OF_ANCHOR"); \
1579 (s) = TAILQ_NEXT((s), entries); \
1580 while ((s) != NULL && ((r) = \
1581 TAILQ_FIRST((s)->rules[n].active.ptr)) == NULL) \
1582 (s) = TAILQ_NEXT((s), entries); \
1583 if ((r) == NULL) { \
1584 (r) = TAILQ_NEXT((a), entries); \
1591 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
1592 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
1597 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1598 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1602 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1603 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1604 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
1605 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
1606 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
1607 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
1608 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
1609 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
1615 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
1620 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
1624 if (addr->addr32[3] == 0xffffffff) {
1625 addr->addr32[3] = 0;
1626 if (addr->addr32[2] == 0xffffffff) {
1627 addr->addr32[2] = 0;
1628 if (addr->addr32[1] == 0xffffffff) {
1629 addr->addr32[1] = 0;
1631 htonl(ntohl(addr->addr32[0]) + 1);
1634 htonl(ntohl(addr->addr32[1]) + 1);
1637 htonl(ntohl(addr->addr32[2]) + 1);
1640 htonl(ntohl(addr->addr32[3]) + 1);
1646 #define mix(a,b,c) \
1648 a -= b; a -= c; a ^= (c >> 13); \
1649 b -= c; b -= a; b ^= (a << 8); \
1650 c -= a; c -= b; c ^= (b >> 13); \
1651 a -= b; a -= c; a ^= (c >> 12); \
1652 b -= c; b -= a; b ^= (a << 16); \
1653 c -= a; c -= b; c ^= (b >> 5); \
1654 a -= b; a -= c; a ^= (c >> 3); \
1655 b -= c; b -= a; b ^= (a << 10); \
1656 c -= a; c -= b; c ^= (b >> 15); \
1660 * hash function based on bridge_hash in if_bridge.c
1663 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
1664 struct pf_poolhashkey *key, sa_family_t af)
1666 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
1671 a += inaddr->addr32[0];
1674 hash->addr32[0] = c + key->key32[2];
1679 a += inaddr->addr32[0];
1680 b += inaddr->addr32[2];
1682 hash->addr32[0] = c;
1683 a += inaddr->addr32[1];
1684 b += inaddr->addr32[3];
1687 hash->addr32[1] = c;
1688 a += inaddr->addr32[2];
1689 b += inaddr->addr32[1];
1692 hash->addr32[2] = c;
1693 a += inaddr->addr32[3];
1694 b += inaddr->addr32[0];
1697 hash->addr32[3] = c;
1704 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
1705 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
1707 unsigned char hash[16];
1708 struct pf_pool *rpool = &r->rpool;
1709 struct pf_addr *raddr = &rpool->cur->addr.v.a.addr;
1710 struct pf_addr *rmask = &rpool->cur->addr.v.a.mask;
1711 struct pf_pooladdr *acur = rpool->cur;
1712 struct pf_src_node k;
1714 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
1715 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1717 PF_ACPY(&k.addr, saddr, af);
1718 if (r->rule_flag & PFRULE_RULESRCTRACK ||
1719 r->rpool.opts & PF_POOL_STICKYADDR)
1723 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
1724 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
1725 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
1726 PF_ACPY(naddr, &(*sn)->raddr, af);
1727 if (pf_status.debug >= PF_DEBUG_MISC) {
1728 printf("pf_map_addr: src tracking maps ");
1729 pf_print_host(&k.addr, 0, af);
1731 pf_print_host(naddr, 0, af);
1738 if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
1740 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1741 if (af == AF_INET) {
1742 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
1743 (rpool->opts & PF_POOL_TYPEMASK) !=
1746 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
1747 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
1749 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
1750 (rpool->opts & PF_POOL_TYPEMASK) !=
1753 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
1754 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
1756 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1757 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
1758 return (1); /* unsupported */
1760 raddr = &rpool->cur->addr.v.a.addr;
1761 rmask = &rpool->cur->addr.v.a.mask;
1764 switch (rpool->opts & PF_POOL_TYPEMASK) {
1766 PF_ACPY(naddr, raddr, af);
1768 case PF_POOL_BITMASK:
1769 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
1771 case PF_POOL_RANDOM:
1772 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
1776 rpool->counter.addr32[0] = arc4random();
1781 if (rmask->addr32[3] != 0xffffffff)
1782 rpool->counter.addr32[3] = arc4random();
1785 if (rmask->addr32[2] != 0xffffffff)
1786 rpool->counter.addr32[2] = arc4random();
1789 if (rmask->addr32[1] != 0xffffffff)
1790 rpool->counter.addr32[1] = arc4random();
1793 if (rmask->addr32[0] != 0xffffffff)
1794 rpool->counter.addr32[0] = arc4random();
1798 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1799 PF_ACPY(init_addr, naddr, af);
1802 PF_AINC(&rpool->counter, af);
1803 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1806 case PF_POOL_SRCHASH:
1807 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
1808 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
1810 case PF_POOL_ROUNDROBIN:
1811 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1812 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
1813 &rpool->tblidx, &rpool->counter,
1814 &raddr, &rmask, af))
1816 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1817 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1818 &rpool->tblidx, &rpool->counter,
1819 &raddr, &rmask, af))
1821 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
1825 if ((rpool->cur = TAILQ_NEXT(rpool->cur, entries)) == NULL)
1826 rpool->cur = TAILQ_FIRST(&rpool->list);
1827 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1829 if (pfr_pool_get(rpool->cur->addr.p.tbl,
1830 &rpool->tblidx, &rpool->counter,
1831 &raddr, &rmask, af)) {
1832 /* table contains no address of type 'af' */
1833 if (rpool->cur != acur)
1837 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1839 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1840 &rpool->tblidx, &rpool->counter,
1841 &raddr, &rmask, af)) {
1842 /* table contains no address of type 'af' */
1843 if (rpool->cur != acur)
1848 raddr = &rpool->cur->addr.v.a.addr;
1849 rmask = &rpool->cur->addr.v.a.mask;
1850 PF_ACPY(&rpool->counter, raddr, af);
1854 PF_ACPY(naddr, &rpool->counter, af);
1855 PF_AINC(&rpool->counter, af);
1859 PF_ACPY(&(*sn)->raddr, naddr, af);
1861 if (pf_status.debug >= PF_DEBUG_MISC &&
1862 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1863 printf("pf_map_addr: selected address ");
1864 pf_print_host(naddr, 0, af);
1872 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
1873 struct pf_addr *saddr, struct pf_addr *daddr, u_int16_t dport,
1874 struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
1875 struct pf_src_node **sn)
1877 struct pf_state key;
1878 struct pf_addr init_addr;
1881 bzero(&init_addr, sizeof(init_addr));
1882 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1888 PF_ACPY(&key.ext.addr, daddr, key.af);
1889 PF_ACPY(&key.gwy.addr, naddr, key.af);
1890 key.ext.port = dport;
1893 * port search; start random, step;
1894 * similar 2 portloop in in_pcbbind
1896 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP)) {
1898 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1900 } else if (low == 0 && high == 0) {
1901 key.gwy.port = *nport;
1902 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1904 } else if (low == high) {
1905 key.gwy.port = htons(low);
1906 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL) {
1907 *nport = htons(low);
1919 cut = arc4random() % (1 + high - low) + low;
1920 /* low <= cut <= high */
1921 for (tmp = cut; tmp <= high; ++(tmp)) {
1922 key.gwy.port = htons(tmp);
1923 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1925 *nport = htons(tmp);
1929 for (tmp = cut - 1; tmp >= low; --(tmp)) {
1930 key.gwy.port = htons(tmp);
1931 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1933 *nport = htons(tmp);
1939 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
1940 case PF_POOL_RANDOM:
1941 case PF_POOL_ROUNDROBIN:
1942 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1946 case PF_POOL_SRCHASH:
1947 case PF_POOL_BITMASK:
1951 } while (! PF_AEQ(&init_addr, naddr, af) );
1953 return (1); /* none available */
1957 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
1958 int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
1959 struct pf_addr *daddr, u_int16_t dport, int rs_num)
1961 struct pf_rule *r, *rm = NULL, *anchorrule = NULL;
1962 struct pf_ruleset *ruleset = NULL;
1964 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1965 while (r && rm == NULL) {
1966 struct pf_rule_addr *src = NULL, *dst = NULL;
1967 struct pf_addr_wrap *xdst = NULL;
1969 if (r->action == PF_BINAT && direction == PF_IN) {
1971 if (r->rpool.cur != NULL)
1972 xdst = &r->rpool.cur->addr;
1979 if (r->kif != NULL &&
1980 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1981 r = r->skip[PF_SKIP_IFP].ptr;
1982 else if (r->direction && r->direction != direction)
1983 r = r->skip[PF_SKIP_DIR].ptr;
1984 else if (r->af && r->af != pd->af)
1985 r = r->skip[PF_SKIP_AF].ptr;
1986 else if (r->proto && r->proto != pd->proto)
1987 r = r->skip[PF_SKIP_PROTO].ptr;
1988 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af, src->not))
1989 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
1990 PF_SKIP_DST_ADDR].ptr;
1991 else if (src->port_op && !pf_match_port(src->port_op,
1992 src->port[0], src->port[1], sport))
1993 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
1994 PF_SKIP_DST_PORT].ptr;
1995 else if (dst != NULL &&
1996 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->not))
1997 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1998 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af, 0))
1999 r = TAILQ_NEXT(r, entries);
2000 else if (dst != NULL && dst->port_op &&
2001 !pf_match_port(dst->port_op, dst->port[0],
2002 dst->port[1], dport))
2003 r = r->skip[PF_SKIP_DST_PORT].ptr;
2004 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
2005 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
2006 off, pd->hdr.tcp), r->os_fingerprint)))
2007 r = TAILQ_NEXT(r, entries);
2008 else if (r->anchorname[0] && r->anchor == NULL)
2009 r = TAILQ_NEXT(r, entries);
2010 else if (r->anchor == NULL)
2013 PF_STEP_INTO_ANCHOR(r, anchorrule, ruleset, rs_num);
2014 if (r == NULL && anchorrule != NULL)
2015 PF_STEP_OUT_OF_ANCHOR(r, anchorrule, ruleset,
2018 if (rm != NULL && (rm->action == PF_NONAT ||
2019 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2025 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
2026 struct pfi_kif *kif, struct pf_src_node **sn,
2027 struct pf_addr *saddr, u_int16_t sport,
2028 struct pf_addr *daddr, u_int16_t dport,
2029 struct pf_addr *naddr, u_int16_t *nport)
2031 struct pf_rule *r = NULL;
2033 if (direction == PF_OUT) {
2034 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2035 sport, daddr, dport, PF_RULESET_BINAT);
2037 r = pf_match_translation(pd, m, off, direction, kif,
2038 saddr, sport, daddr, dport, PF_RULESET_NAT);
2040 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2041 sport, daddr, dport, PF_RULESET_RDR);
2043 r = pf_match_translation(pd, m, off, direction, kif,
2044 saddr, sport, daddr, dport, PF_RULESET_BINAT);
2048 switch (r->action) {
2054 if (pf_get_sport(pd->af, pd->proto, r, saddr,
2055 daddr, dport, naddr, nport, r->rpool.proxy_port[0],
2056 r->rpool.proxy_port[1], sn)) {
2057 DPFPRINTF(PF_DEBUG_MISC,
2058 ("pf: NAT proxy port allocation "
2060 r->rpool.proxy_port[0],
2061 r->rpool.proxy_port[1]));
2066 switch (direction) {
2068 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
2069 if (pd->af == AF_INET) {
2070 if (r->rpool.cur->addr.p.dyn->
2074 &r->rpool.cur->addr.p.dyn->
2076 &r->rpool.cur->addr.p.dyn->
2080 if (r->rpool.cur->addr.p.dyn->
2084 &r->rpool.cur->addr.p.dyn->
2086 &r->rpool.cur->addr.p.dyn->
2092 &r->rpool.cur->addr.v.a.addr,
2093 &r->rpool.cur->addr.v.a.mask,
2097 if (r->src.addr.type == PF_ADDR_DYNIFTL){
2098 if (pd->af == AF_INET) {
2099 if (r->src.addr.p.dyn->
2103 &r->src.addr.p.dyn->
2105 &r->src.addr.p.dyn->
2109 if (r->src.addr.p.dyn->
2113 &r->src.addr.p.dyn->
2115 &r->src.addr.p.dyn->
2121 &r->src.addr.v.a.addr,
2122 &r->src.addr.v.a.mask, daddr,
2128 if (pf_map_addr(r->af, r, saddr, naddr, NULL, sn))
2131 if (r->rpool.proxy_port[1]) {
2132 u_int32_t tmp_nport;
2134 tmp_nport = ((ntohs(dport) -
2135 ntohs(r->dst.port[0])) %
2136 (r->rpool.proxy_port[1] -
2137 r->rpool.proxy_port[0] + 1)) +
2138 r->rpool.proxy_port[0];
2140 /* wrap around if necessary */
2141 if (tmp_nport > 65535)
2143 *nport = htons((u_int16_t)tmp_nport);
2144 } else if (r->rpool.proxy_port[0])
2145 *nport = htons(r->rpool.proxy_port[0]);
2157 struct netmsg_hashlookup {
2158 struct lwkt_msg nm_lmsg;
2159 struct inpcb **nm_pinp;
2160 struct inpcbinfo *nm_pcbinfo;
2161 struct pf_addr *nm_saddr;
2162 struct pf_addr *nm_daddr;
2169 in_pcblookup_hash_handler(struct lwkt_msg *msg0)
2171 struct netmsg_hashlookup *msg = (struct netmsg_hashlookup *)msg0;
2173 if (msg->nm_af == AF_INET)
2174 *msg->nm_pinp = in_pcblookup_hash(msg->nm_pcbinfo,
2175 msg->nm_saddr->v4, msg->nm_sport, msg->nm_daddr->v4,
2176 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2179 *msg->nm_pinp = in6_pcblookup_hash(msg->nm_pcbinfo,
2180 &msg->nm_saddr->v6, msg->nm_sport, &msg->nm_daddr->v6,
2181 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2183 lwkt_replymsg(&msg->nm_lmsg, 0);
2189 pf_socket_lookup(uid_t *uid, gid_t *gid, int direction, struct pf_pdesc *pd)
2191 struct pf_addr *saddr, *daddr;
2192 u_int16_t sport, dport;
2193 struct inpcbinfo *pi;
2196 struct netmsg_hashlookup *msg = NULL;
2202 if (direction == PF_IN) {
2209 switch (pd->proto) {
2211 sport = pd->hdr.tcp->th_sport;
2212 dport = pd->hdr.tcp->th_dport;
2214 pi_cpu = tcp_addrcpu(saddr->v4.s_addr, sport, daddr->v4.s_addr, dport);
2215 pi = &tcbinfo[pi_cpu];
2218 * Our netstack runs lockless on MP systems
2219 * (only for TCP connections at the moment).
2221 * As we are not allowed to read another CPU's tcbinfo,
2222 * we have to ask that CPU via remote call to search the
2225 * Prepare a msg iff data belongs to another CPU.
2227 if (pi_cpu != mycpu->gd_cpuid) {
2228 msg = malloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
2229 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
2230 lwkt_cmd_func(in_pcblookup_hash_handler),
2232 msg->nm_pinp = &inp;
2233 msg->nm_pcbinfo = pi;
2234 msg->nm_saddr = saddr;
2235 msg->nm_sport = sport;
2236 msg->nm_daddr = daddr;
2237 msg->nm_dport = dport;
2238 msg->nm_af = pd->af;
2243 sport = pd->hdr.udp->uh_sport;
2244 dport = pd->hdr.udp->uh_dport;
2250 if (direction != PF_IN) {
2262 * Query other CPU, second part
2264 * msg only gets initialized when:
2266 * 2) the info belongs to another CPU
2268 * Use some switch/case magic to avoid code duplication.
2273 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
2274 &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
2280 /* FALLTHROUGH if SMP and on other CPU */
2285 lwkt_sendmsg(tcp_cport(pi_cpu), &msg->nm_lmsg);
2289 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
2290 dport, INPLOOKUP_WILDCARD, NULL);
2299 *uid = inp->inp_socket->so_cred->cr_uid;
2300 *gid = inp->inp_socket->so_cred->cr_groups[0];
2305 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2309 u_int8_t *opt, optlen;
2310 u_int8_t wscale = 0;
2312 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2313 if (hlen <= sizeof(struct tcphdr))
2315 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2317 opt = hdr + sizeof(struct tcphdr);
2318 hlen -= sizeof(struct tcphdr);
2328 if (wscale > TCP_MAX_WINSHIFT)
2329 wscale = TCP_MAX_WINSHIFT;
2330 wscale |= PF_WSCALE_FLAG;
2345 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2349 u_int8_t *opt, optlen;
2350 u_int16_t mss = tcp_mssdflt;
2352 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2353 if (hlen <= sizeof(struct tcphdr))
2355 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2357 opt = hdr + sizeof(struct tcphdr);
2358 hlen -= sizeof(struct tcphdr);
2359 while (hlen >= TCPOLEN_MAXSEG) {
2367 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
2382 pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
2385 struct sockaddr_in *dst;
2389 struct sockaddr_in6 *dst6;
2390 struct route_in6 ro6;
2392 struct rtentry *rt = NULL;
2394 u_int16_t mss = tcp_mssdflt;
2399 hlen = sizeof(struct ip);
2400 bzero(&ro, sizeof(ro));
2401 dst = (struct sockaddr_in *)&ro.ro_dst;
2402 dst->sin_family = AF_INET;
2403 dst->sin_len = sizeof(*dst);
2404 dst->sin_addr = addr->v4;
2405 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
2411 hlen = sizeof(struct ip6_hdr);
2412 bzero(&ro6, sizeof(ro6));
2413 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
2414 dst6->sin6_family = AF_INET6;
2415 dst6->sin6_len = sizeof(*dst6);
2416 dst6->sin6_addr = addr->v6;
2417 rtalloc_ign((struct route *)&ro6, (RTF_CLONING | RTF_PRCLONING));
2423 if (rt && rt->rt_ifp) {
2424 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
2425 mss = max(tcp_mssdflt, mss);
2428 mss = min(mss, offer);
2429 mss = max(mss, 64); /* sanity - at least max opt space */
2434 pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
2436 struct pf_rule *r = s->rule.ptr;
2439 if (!r->rt || r->rt == PF_FASTROUTE)
2444 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
2446 s->rt_kif = r->rpool.cur->kif;
2451 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
2453 s->rt_kif = r->rpool.cur->kif;
2460 pf_test_tcp(struct pf_rule **rm, struct pf_state **sm, int direction,
2461 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2462 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2464 struct pf_rule *nr = NULL;
2465 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2466 struct tcphdr *th = pd->hdr.tcp;
2467 u_int16_t bport, nport = 0;
2468 sa_family_t af = pd->af;
2472 struct pf_rule *r, *a = NULL;
2473 struct pf_ruleset *ruleset = NULL;
2474 struct pf_src_node *nsn = NULL;
2478 u_int16_t mss = tcp_mssdflt;
2480 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2482 if (direction == PF_OUT) {
2483 bport = nport = th->th_sport;
2484 /* check outgoing packet for BINAT/NAT */
2485 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2486 saddr, th->th_sport, daddr, th->th_dport,
2487 &pd->naddr, &nport)) != NULL) {
2488 PF_ACPY(&pd->baddr, saddr, af);
2489 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2490 &th->th_sum, &pd->naddr, nport, 0, af);
2497 bport = nport = th->th_dport;
2498 /* check incoming packet for BINAT/RDR */
2499 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2500 saddr, th->th_sport, daddr, th->th_dport,
2501 &pd->naddr, &nport)) != NULL) {
2502 PF_ACPY(&pd->baddr, daddr, af);
2503 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2504 &th->th_sum, &pd->naddr, nport, 0, af);
2514 if (r->kif != NULL &&
2515 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2516 r = r->skip[PF_SKIP_IFP].ptr;
2517 else if (r->direction && r->direction != direction)
2518 r = r->skip[PF_SKIP_DIR].ptr;
2519 else if (r->af && r->af != af)
2520 r = r->skip[PF_SKIP_AF].ptr;
2521 else if (r->proto && r->proto != IPPROTO_TCP)
2522 r = r->skip[PF_SKIP_PROTO].ptr;
2523 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2524 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2525 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2526 r->src.port[0], r->src.port[1], th->th_sport))
2527 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2528 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2529 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2530 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2531 r->dst.port[0], r->dst.port[1], th->th_dport))
2532 r = r->skip[PF_SKIP_DST_PORT].ptr;
2533 else if (r->tos && !(r->tos & pd->tos))
2534 r = TAILQ_NEXT(r, entries);
2535 else if (r->rule_flag & PFRULE_FRAGMENT)
2536 r = TAILQ_NEXT(r, entries);
2537 else if ((r->flagset & th->th_flags) != r->flags)
2538 r = TAILQ_NEXT(r, entries);
2539 else if (r->uid.op && (lookup != -1 || (lookup =
2540 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2541 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2543 r = TAILQ_NEXT(r, entries);
2544 else if (r->gid.op && (lookup != -1 || (lookup =
2545 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2546 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2548 r = TAILQ_NEXT(r, entries);
2549 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2550 r = TAILQ_NEXT(r, entries);
2551 else if (r->anchorname[0] && r->anchor == NULL)
2552 r = TAILQ_NEXT(r, entries);
2553 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
2554 pf_osfp_fingerprint(pd, m, off, th), r->os_fingerprint))
2555 r = TAILQ_NEXT(r, entries);
2559 if (r->anchor == NULL) {
2565 r = TAILQ_NEXT(r, entries);
2567 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2570 if (r == NULL && a != NULL)
2571 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2578 REASON_SET(&reason, PFRES_MATCH);
2582 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2583 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2586 if ((r->action == PF_DROP) &&
2587 ((r->rule_flag & PFRULE_RETURNRST) ||
2588 (r->rule_flag & PFRULE_RETURNICMP) ||
2589 (r->rule_flag & PFRULE_RETURN))) {
2590 /* undo NAT changes, if they have taken place */
2592 if (direction == PF_OUT) {
2593 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2594 &th->th_sum, &pd->baddr, bport, 0, af);
2597 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2598 &th->th_sum, &pd->baddr, bport, 0, af);
2602 if (((r->rule_flag & PFRULE_RETURNRST) ||
2603 (r->rule_flag & PFRULE_RETURN)) &&
2604 !(th->th_flags & TH_RST)) {
2605 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
2607 if (th->th_flags & TH_SYN)
2609 if (th->th_flags & TH_FIN)
2611 pf_send_tcp(r, af, pd->dst,
2612 pd->src, th->th_dport, th->th_sport,
2613 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
2615 } else if ((af == AF_INET) && r->return_icmp)
2616 pf_send_icmp(m, r->return_icmp >> 8,
2617 r->return_icmp & 255, af, r);
2618 else if ((af == AF_INET6) && r->return_icmp6)
2619 pf_send_icmp(m, r->return_icmp6 >> 8,
2620 r->return_icmp6 & 255, af, r);
2623 if (r->action == PF_DROP)
2626 pf_tag_packet(m, tag);
2628 if (r->keep_state || nr != NULL ||
2629 (pd->flags & PFDESC_TCP_NORM)) {
2630 /* create new state */
2632 struct pf_state *s = NULL;
2633 struct pf_src_node *sn = NULL;
2635 len = pd->tot_len - off - (th->th_off << 2);
2637 /* check maximums */
2638 if (r->max_states && (r->states >= r->max_states))
2640 /* src node for flter rule */
2641 if ((r->rule_flag & PFRULE_SRCTRACK ||
2642 r->rpool.opts & PF_POOL_STICKYADDR) &&
2643 pf_insert_src_node(&sn, r, saddr, af) != 0)
2645 /* src node for translation rule */
2646 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2647 ((direction == PF_OUT &&
2648 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2649 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2651 s = pool_get(&pf_state_pl, PR_NOWAIT);
2654 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
2655 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
2656 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2657 pf_status.src_nodes--;
2658 pool_put(&pf_src_tree_pl, sn);
2660 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
2662 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
2663 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2664 pf_status.src_nodes--;
2665 pool_put(&pf_src_tree_pl, nsn);
2667 REASON_SET(&reason, PFRES_MEMORY);
2670 bzero(s, sizeof(*s));
2675 s->nat_rule.ptr = nr;
2676 if (s->nat_rule.ptr != NULL)
2677 s->nat_rule.ptr->states++;
2679 s->allow_opts = r->allow_opts;
2680 s->log = r->log & 2;
2681 s->proto = IPPROTO_TCP;
2682 s->direction = direction;
2684 if (direction == PF_OUT) {
2685 PF_ACPY(&s->gwy.addr, saddr, af);
2686 s->gwy.port = th->th_sport; /* sport */
2687 PF_ACPY(&s->ext.addr, daddr, af);
2688 s->ext.port = th->th_dport;
2690 PF_ACPY(&s->lan.addr, &pd->baddr, af);
2691 s->lan.port = bport;
2693 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
2694 s->lan.port = s->gwy.port;
2697 PF_ACPY(&s->lan.addr, daddr, af);
2698 s->lan.port = th->th_dport;
2699 PF_ACPY(&s->ext.addr, saddr, af);
2700 s->ext.port = th->th_sport;
2702 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
2703 s->gwy.port = bport;
2705 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
2706 s->gwy.port = s->lan.port;
2710 s->src.seqlo = ntohl(th->th_seq);
2711 s->src.seqhi = s->src.seqlo + len + 1;
2712 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2713 r->keep_state == PF_STATE_MODULATE) {
2714 /* Generate sequence number modulator */
2715 while ((s->src.seqdiff = arc4random()) == 0)
2717 pf_change_a(&th->th_seq, &th->th_sum,
2718 htonl(s->src.seqlo + s->src.seqdiff), 0);
2722 if (th->th_flags & TH_SYN) {
2724 s->src.wscale = pf_get_wscale(m, off, th->th_off, af);
2726 s->src.max_win = MAX(ntohs(th->th_win), 1);
2727 if (s->src.wscale & PF_WSCALE_MASK) {
2728 /* Remove scale factor from initial window */
2729 int win = s->src.max_win;
2730 win += 1 << (s->src.wscale & PF_WSCALE_MASK);
2731 s->src.max_win = (win - 1) >>
2732 (s->src.wscale & PF_WSCALE_MASK);
2734 if (th->th_flags & TH_FIN)
2738 s->src.state = TCPS_SYN_SENT;
2739 s->dst.state = TCPS_CLOSED;
2740 s->creation = time_second;
2741 s->expire = time_second;
2742 s->timeout = PFTM_TCP_FIRST_PACKET;
2743 pf_set_rt_ifp(s, saddr);
2746 s->src_node->states++;
2749 PF_ACPY(&nsn->raddr, &pd->naddr, af);
2750 s->nat_src_node = nsn;
2751 s->nat_src_node->states++;
2753 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
2754 off, pd, th, &s->src, &s->dst)) {
2755 REASON_SET(&reason, PFRES_MEMORY);
2756 pf_src_tree_remove_state(s);
2757 pool_put(&pf_state_pl, s);
2760 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
2761 pf_normalize_tcp_stateful(m, off, pd, &reason, th, &s->src,
2762 &s->dst, &rewrite)) {
2763 pf_normalize_tcp_cleanup(s);
2764 pf_src_tree_remove_state(s);
2765 pool_put(&pf_state_pl, s);
2768 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
2769 pf_normalize_tcp_cleanup(s);
2770 REASON_SET(&reason, PFRES_MEMORY);
2771 pf_src_tree_remove_state(s);
2772 pool_put(&pf_state_pl, s);
2776 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2777 r->keep_state == PF_STATE_SYNPROXY) {
2778 s->src.state = PF_TCPS_PROXY_SRC;
2780 if (direction == PF_OUT) {
2781 pf_change_ap(saddr, &th->th_sport,
2782 pd->ip_sum, &th->th_sum, &pd->baddr,
2785 pf_change_ap(daddr, &th->th_dport,
2786 pd->ip_sum, &th->th_sum, &pd->baddr,
2790 s->src.seqhi = arc4random();
2791 /* Find mss option */
2792 mss = pf_get_mss(m, off, th->th_off, af);
2793 mss = pf_calc_mss(saddr, af, mss);
2794 mss = pf_calc_mss(daddr, af, mss);
2796 pf_send_tcp(r, af, daddr, saddr, th->th_dport,
2797 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
2798 TH_SYN|TH_ACK, 0, s->src.mss, 0);
2799 return (PF_SYNPROXY_DROP);
2803 /* copy back packet headers if we performed NAT operations */
2805 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2811 pf_test_udp(struct pf_rule **rm, struct pf_state **sm, int direction,
2812 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2813 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2815 struct pf_rule *nr = NULL;
2816 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2817 struct udphdr *uh = pd->hdr.udp;
2818 u_int16_t bport, nport = 0;
2819 sa_family_t af = pd->af;
2823 struct pf_rule *r, *a = NULL;
2824 struct pf_ruleset *ruleset = NULL;
2825 struct pf_src_node *nsn = NULL;
2830 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2832 if (direction == PF_OUT) {
2833 bport = nport = uh->uh_sport;
2834 /* check outgoing packet for BINAT/NAT */
2835 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2836 saddr, uh->uh_sport, daddr, uh->uh_dport,
2837 &pd->naddr, &nport)) != NULL) {
2838 PF_ACPY(&pd->baddr, saddr, af);
2839 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2840 &uh->uh_sum, &pd->naddr, nport, 1, af);
2847 bport = nport = uh->uh_dport;
2848 /* check incoming packet for BINAT/RDR */
2849 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2850 saddr, uh->uh_sport, daddr, uh->uh_dport, &pd->naddr,
2852 PF_ACPY(&pd->baddr, daddr, af);
2853 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2854 &uh->uh_sum, &pd->naddr, nport, 1, af);
2864 if (r->kif != NULL &&
2865 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2866 r = r->skip[PF_SKIP_IFP].ptr;
2867 else if (r->direction && r->direction != direction)
2868 r = r->skip[PF_SKIP_DIR].ptr;
2869 else if (r->af && r->af != af)
2870 r = r->skip[PF_SKIP_AF].ptr;
2871 else if (r->proto && r->proto != IPPROTO_UDP)
2872 r = r->skip[PF_SKIP_PROTO].ptr;
2873 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2874 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2875 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2876 r->src.port[0], r->src.port[1], uh->uh_sport))
2877 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2878 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2879 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2880 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2881 r->dst.port[0], r->dst.port[1], uh->uh_dport))
2882 r = r->skip[PF_SKIP_DST_PORT].ptr;
2883 else if (r->tos && !(r->tos & pd->tos))
2884 r = TAILQ_NEXT(r, entries);
2885 else if (r->rule_flag & PFRULE_FRAGMENT)
2886 r = TAILQ_NEXT(r, entries);
2887 else if (r->uid.op && (lookup != -1 || (lookup =
2888 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2889 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2891 r = TAILQ_NEXT(r, entries);
2892 else if (r->gid.op && (lookup != -1 || (lookup =
2893 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2894 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2896 r = TAILQ_NEXT(r, entries);
2897 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2898 r = TAILQ_NEXT(r, entries);
2899 else if (r->anchorname[0] && r->anchor == NULL)
2900 r = TAILQ_NEXT(r, entries);
2901 else if (r->os_fingerprint != PF_OSFP_ANY)
2902 r = TAILQ_NEXT(r, entries);
2906 if (r->anchor == NULL) {
2912 r = TAILQ_NEXT(r, entries);
2914 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2917 if (r == NULL && a != NULL)
2918 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2925 REASON_SET(&reason, PFRES_MATCH);
2929 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
2930 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2933 if ((r->action == PF_DROP) &&
2934 ((r->rule_flag & PFRULE_RETURNICMP) ||
2935 (r->rule_flag & PFRULE_RETURN))) {
2936 /* undo NAT changes, if they have taken place */
2938 if (direction == PF_OUT) {
2939 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2940 &uh->uh_sum, &pd->baddr, bport, 1, af);
2943 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2944 &uh->uh_sum, &pd->baddr, bport, 1, af);
2948 if ((af == AF_INET) && r->return_icmp)
2949 pf_send_icmp(m, r->return_icmp >> 8,
2950 r->return_icmp & 255, af, r);
2951 else if ((af == AF_INET6) && r->return_icmp6)
2952 pf_send_icmp(m, r->return_icmp6 >> 8,
2953 r->return_icmp6 & 255, af, r);
2956 if (r->action == PF_DROP)
2959 pf_tag_packet(m, tag);
2961 if (r->keep_state || nr != NULL) {
2962 /* create new state */
2963 struct pf_state *s = NULL;
2964 struct pf_src_node *sn = NULL;
2966 /* check maximums */
2967 if (r->max_states && (r->states >= r->max_states))
2969 /* src node for flter rule */
2970 if ((r->rule_flag & PFRULE_SRCTRACK ||
2971 r->rpool.opts & PF_POOL_STICKYADDR) &&
2972 pf_insert_src_node(&sn, r, saddr, af) != 0)
2974 /* src node for translation rule */
2975 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2976 ((direction == PF_OUT &&
2977 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2978 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2980 s = pool_get(&pf_state_pl, PR_NOWAIT);
2983 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
2984 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
2985 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2986 pf_status.src_nodes--;
2987 pool_put(&pf_src_tree_pl, sn);
2989 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
2991 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
2992 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2993 pf_status.src_nodes--;
2994 pool_put(&pf_src_tree_pl, nsn);
2996 REASON_SET(&reason, PFRES_MEMORY);
2999 bzero(s, sizeof(*s));
3004 s->nat_rule.ptr = nr;
3005 if (s->nat_rule.ptr != NULL)
3006 s->nat_rule.ptr->states++;
3008 s->allow_opts = r->allow_opts;
3009 s->log = r->log & 2;
3010 s->proto = IPPROTO_UDP;
3011 s->direction = direction;
3013 if (direction == PF_OUT) {
3014 PF_ACPY(&s->gwy.addr, saddr, af);
3015 s->gwy.port = uh->uh_sport;
3016 PF_ACPY(&s->ext.addr, daddr, af);
3017 s->ext.port = uh->uh_dport;
3019 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3020 s->lan.port = bport;
3022 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3023 s->lan.port = s->gwy.port;
3026 PF_ACPY(&s->lan.addr, daddr, af);
3027 s->lan.port = uh->uh_dport;
3028 PF_ACPY(&s->ext.addr, saddr, af);
3029 s->ext.port = uh->uh_sport;
3031 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3032 s->gwy.port = bport;
3034 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3035 s->gwy.port = s->lan.port;
3038 s->src.state = PFUDPS_SINGLE;
3039 s->dst.state = PFUDPS_NO_TRAFFIC;
3040 s->creation = time_second;
3041 s->expire = time_second;
3042 s->timeout = PFTM_UDP_FIRST_PACKET;
3043 pf_set_rt_ifp(s, saddr);
3046 s->src_node->states++;
3049 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3050 s->nat_src_node = nsn;
3051 s->nat_src_node->states++;
3053 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3054 REASON_SET(&reason, PFRES_MEMORY);
3055 pf_src_tree_remove_state(s);
3056 pool_put(&pf_state_pl, s);
3062 /* copy back packet headers if we performed NAT operations */
3064 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
3070 pf_test_icmp(struct pf_rule **rm, struct pf_state **sm, int direction,
3071 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3072 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
3074 struct pf_rule *nr = NULL;
3075 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3076 struct pf_rule *r, *a = NULL;
3077 struct pf_ruleset *ruleset = NULL;
3078 struct pf_src_node *nsn = NULL;
3080 u_int16_t icmpid = 0;
3081 sa_family_t af = pd->af;
3082 u_int8_t icmptype = 0, icmpcode = 0;
3089 switch (pd->proto) {
3092 icmptype = pd->hdr.icmp->icmp_type;
3093 icmpcode = pd->hdr.icmp->icmp_code;
3094 icmpid = pd->hdr.icmp->icmp_id;
3096 if (icmptype == ICMP_UNREACH ||
3097 icmptype == ICMP_SOURCEQUENCH ||
3098 icmptype == ICMP_REDIRECT ||
3099 icmptype == ICMP_TIMXCEED ||
3100 icmptype == ICMP_PARAMPROB)
3105 case IPPROTO_ICMPV6:
3106 icmptype = pd->hdr.icmp6->icmp6_type;
3107 icmpcode = pd->hdr.icmp6->icmp6_code;
3108 icmpid = pd->hdr.icmp6->icmp6_id;
3110 if (icmptype == ICMP6_DST_UNREACH ||
3111 icmptype == ICMP6_PACKET_TOO_BIG ||
3112 icmptype == ICMP6_TIME_EXCEEDED ||
3113 icmptype == ICMP6_PARAM_PROB)
3119 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3121 if (direction == PF_OUT) {
3122 /* check outgoing packet for BINAT/NAT */
3123 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3124 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3125 PF_ACPY(&pd->baddr, saddr, af);
3129 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3130 pd->naddr.v4.s_addr, 0);
3135 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3146 /* check incoming packet for BINAT/RDR */
3147 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3148 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3149 PF_ACPY(&pd->baddr, daddr, af);
3153 pf_change_a(&daddr->v4.s_addr,
3154 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3159 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3173 if (r->kif != NULL &&
3174 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3175 r = r->skip[PF_SKIP_IFP].ptr;
3176 else if (r->direction && r->direction != direction)
3177 r = r->skip[PF_SKIP_DIR].ptr;
3178 else if (r->af && r->af != af)
3179 r = r->skip[PF_SKIP_AF].ptr;
3180 else if (r->proto && r->proto != pd->proto)
3181 r = r->skip[PF_SKIP_PROTO].ptr;
3182 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
3183 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3184 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
3185 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3186 else if (r->type && r->type != icmptype + 1)
3187 r = TAILQ_NEXT(r, entries);
3188 else if (r->code && r->code != icmpcode + 1)
3189 r = TAILQ_NEXT(r, entries);
3190 else if (r->tos && !(r->tos & pd->tos))
3191 r = TAILQ_NEXT(r, entries);
3192 else if (r->rule_flag & PFRULE_FRAGMENT)
3193 r = TAILQ_NEXT(r, entries);
3194 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3195 r = TAILQ_NEXT(r, entries);
3196 else if (r->anchorname[0] && r->anchor == NULL)
3197 r = TAILQ_NEXT(r, entries);
3198 else if (r->os_fingerprint != PF_OSFP_ANY)
3199 r = TAILQ_NEXT(r, entries);
3203 if (r->anchor == NULL) {
3209 r = TAILQ_NEXT(r, entries);
3211 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3214 if (r == NULL && a != NULL)
3215 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3222 REASON_SET(&reason, PFRES_MATCH);
3227 m_copyback(m, off, sizeof(struct icmp6_hdr),
3228 (caddr_t)pd->hdr.icmp6);
3230 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3233 if (r->action != PF_PASS)
3236 pf_tag_packet(m, tag);
3238 if (!state_icmp && (r->keep_state || nr != NULL)) {
3239 /* create new state */
3240 struct pf_state *s = NULL;
3241 struct pf_src_node *sn = NULL;
3243 /* check maximums */
3244 if (r->max_states && (r->states >= r->max_states))
3246 /* src node for flter rule */
3247 if ((r->rule_flag & PFRULE_SRCTRACK ||
3248 r->rpool.opts & PF_POOL_STICKYADDR) &&
3249 pf_insert_src_node(&sn, r, saddr, af) != 0)
3251 /* src node for translation rule */
3252 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3253 ((direction == PF_OUT &&
3254 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3255 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3257 s = pool_get(&pf_state_pl, PR_NOWAIT);
3260 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3261 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3262 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3263 pf_status.src_nodes--;
3264 pool_put(&pf_src_tree_pl, sn);
3266 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3268 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3269 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3270 pf_status.src_nodes--;
3271 pool_put(&pf_src_tree_pl, nsn);
3273 REASON_SET(&reason, PFRES_MEMORY);
3276 bzero(s, sizeof(*s));
3281 s->nat_rule.ptr = nr;
3282 if (s->nat_rule.ptr != NULL)
3283 s->nat_rule.ptr->states++;
3285 s->allow_opts = r->allow_opts;
3286 s->log = r->log & 2;
3287 s->proto = pd->proto;
3288 s->direction = direction;
3290 if (direction == PF_OUT) {
3291 PF_ACPY(&s->gwy.addr, saddr, af);
3292 s->gwy.port = icmpid;
3293 PF_ACPY(&s->ext.addr, daddr, af);
3294 s->ext.port = icmpid;
3296 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3298 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3299 s->lan.port = icmpid;
3301 PF_ACPY(&s->lan.addr, daddr, af);
3302 s->lan.port = icmpid;
3303 PF_ACPY(&s->ext.addr, saddr, af);
3304 s->ext.port = icmpid;
3306 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3308 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3309 s->gwy.port = icmpid;
3311 s->creation = time_second;
3312 s->expire = time_second;
3313 s->timeout = PFTM_ICMP_FIRST_PACKET;
3314 pf_set_rt_ifp(s, saddr);
3317 s->src_node->states++;
3320 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3321 s->nat_src_node = nsn;
3322 s->nat_src_node->states++;
3324 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3325 REASON_SET(&reason, PFRES_MEMORY);
3326 pf_src_tree_remove_state(s);
3327 pool_put(&pf_state_pl, s);
3334 /* copy back packet headers if we performed IPv6 NAT operations */
3336 m_copyback(m, off, sizeof(struct icmp6_hdr),
3337 (caddr_t)pd->hdr.icmp6);
3344 pf_test_other(struct pf_rule **rm, struct pf_state **sm, int direction,
3345 struct pfi_kif *kif, struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3346 struct pf_rule **am, struct pf_ruleset **rsm)
3348 struct pf_rule *nr = NULL;
3349 struct pf_rule *r, *a = NULL;
3350 struct pf_ruleset *ruleset = NULL;
3351 struct pf_src_node *nsn = NULL;
3352 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3353 sa_family_t af = pd->af;
3357 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3359 if (direction == PF_OUT) {
3360 /* check outgoing packet for BINAT/NAT */
3361 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3362 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3363 PF_ACPY(&pd->baddr, saddr, af);
3367 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3368 pd->naddr.v4.s_addr, 0);
3373 PF_ACPY(saddr, &pd->naddr, af);
3382 /* check incoming packet for BINAT/RDR */
3383 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3384 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3385 PF_ACPY(&pd->baddr, daddr, af);
3389 pf_change_a(&daddr->v4.s_addr,
3390 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3395 PF_ACPY(daddr, &pd->naddr, af);
3407 if (r->kif != NULL &&
3408 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3409 r = r->skip[PF_SKIP_IFP].ptr;
3410 else if (r->direction && r->direction != direction)
3411 r = r->skip[PF_SKIP_DIR].ptr;
3412 else if (r->af && r->af != af)
3413 r = r->skip[PF_SKIP_AF].ptr;
3414 else if (r->proto && r->proto != pd->proto)
3415 r = r->skip[PF_SKIP_PROTO].ptr;
3416 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3417 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3418 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3419 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3420 else if (r->tos && !(r->tos & pd->tos))
3421 r = TAILQ_NEXT(r, entries);
3422 else if (r->rule_flag & PFRULE_FRAGMENT)
3423 r = TAILQ_NEXT(r, entries);
3424 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3425 r = TAILQ_NEXT(r, entries);
3426 else if (r->anchorname[0] && r->anchor == NULL)
3427 r = TAILQ_NEXT(r, entries);
3428 else if (r->os_fingerprint != PF_OSFP_ANY)
3429 r = TAILQ_NEXT(r, entries);
3433 if (r->anchor == NULL) {
3439 r = TAILQ_NEXT(r, entries);
3441 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3444 if (r == NULL && a != NULL)
3445 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3452 REASON_SET(&reason, PFRES_MATCH);
3455 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3457 if ((r->action == PF_DROP) &&
3458 ((r->rule_flag & PFRULE_RETURNICMP) ||
3459 (r->rule_flag & PFRULE_RETURN))) {
3460 struct pf_addr *a = NULL;
3463 if (direction == PF_OUT)
3472 pf_change_a(&a->v4.s_addr, pd->ip_sum,
3473 pd->baddr.v4.s_addr, 0);
3478 PF_ACPY(a, &pd->baddr, af);
3483 if ((af == AF_INET) && r->return_icmp)
3484 pf_send_icmp(m, r->return_icmp >> 8,
3485 r->return_icmp & 255, af, r);
3486 else if ((af == AF_INET6) && r->return_icmp6)
3487 pf_send_icmp(m, r->return_icmp6 >> 8,
3488 r->return_icmp6 & 255, af, r);
3491 if (r->action != PF_PASS)
3494 pf_tag_packet(m, tag);
3496 if (r->keep_state || nr != NULL) {
3497 /* create new state */
3498 struct pf_state *s = NULL;
3499 struct pf_src_node *sn = NULL;
3501 /* check maximums */
3502 if (r->max_states && (r->states >= r->max_states))
3504 /* src node for flter rule */
3505 if ((r->rule_flag & PFRULE_SRCTRACK ||
3506 r->rpool.opts & PF_POOL_STICKYADDR) &&
3507 pf_insert_src_node(&sn, r, saddr, af) != 0)
3509 /* src node for translation rule */
3510 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3511 ((direction == PF_OUT &&
3512 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3513 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3515 s = pool_get(&pf_state_pl, PR_NOWAIT);
3518 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3519 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3520 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3521 pf_status.src_nodes--;
3522 pool_put(&pf_src_tree_pl, sn);
3524 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3526 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3527 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3528 pf_status.src_nodes--;
3529 pool_put(&pf_src_tree_pl, nsn);
3531 REASON_SET(&reason, PFRES_MEMORY);
3534 bzero(s, sizeof(*s));
3539 s->nat_rule.ptr = nr;
3540 if (s->nat_rule.ptr != NULL)
3541 s->nat_rule.ptr->states++;
3543 s->allow_opts = r->allow_opts;
3544 s->log = r->log & 2;
3545 s->proto = pd->proto;
3546 s->direction = direction;
3548 if (direction == PF_OUT) {
3549 PF_ACPY(&s->gwy.addr, saddr, af);
3550 PF_ACPY(&s->ext.addr, daddr, af);
3552 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3554 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3556 PF_ACPY(&s->lan.addr, daddr, af);
3557 PF_ACPY(&s->ext.addr, saddr, af);
3559 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3561 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3563 s->src.state = PFOTHERS_SINGLE;
3564 s->dst.state = PFOTHERS_NO_TRAFFIC;
3565 s->creation = time_second;
3566 s->expire = time_second;
3567 s->timeout = PFTM_OTHER_FIRST_PACKET;
3568 pf_set_rt_ifp(s, saddr);
3571 s->src_node->states++;
3574 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3575 s->nat_src_node = nsn;
3576 s->nat_src_node->states++;
3578 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3579 REASON_SET(&reason, PFRES_MEMORY);
3580 pf_src_tree_remove_state(s);
3581 pool_put(&pf_state_pl, s);
3591 pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
3592 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
3593 struct pf_ruleset **rsm)
3595 struct pf_rule *r, *a = NULL;
3596 struct pf_ruleset *ruleset = NULL;
3597 sa_family_t af = pd->af;
3601 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3604 if (r->kif != NULL &&
3605 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3606 r = r->skip[PF_SKIP_IFP].ptr;
3607 else if (r->direction && r->direction != direction)
3608 r = r->skip[PF_SKIP_DIR].ptr;
3609 else if (r->af && r->af != af)
3610 r = r->skip[PF_SKIP_AF].ptr;
3611 else if (r->proto && r->proto != pd->proto)
3612 r = r->skip[PF_SKIP_PROTO].ptr;
3613 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3614 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3615 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3616 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3617 else if (r->tos && !(r->tos & pd->tos))
3618 r = TAILQ_NEXT(r, entries);
3619 else if (r->src.port_op || r->dst.port_op ||
3620 r->flagset || r->type || r->code ||
3621 r->os_fingerprint != PF_OSFP_ANY)
3622 r = TAILQ_NEXT(r, entries);
3623 else if (r->match_tag && !pf_match_tag(m, r, NULL, &tag))
3624 r = TAILQ_NEXT(r, entries);
3625 else if (r->anchorname[0] && r->anchor == NULL)
3626 r = TAILQ_NEXT(r, entries);
3628 if (r->anchor == NULL) {
3634 r = TAILQ_NEXT(r, entries);
3636 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3639 if (r == NULL && a != NULL)
3640 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3647 REASON_SET(&reason, PFRES_MATCH);
3650 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3652 if (r->action != PF_PASS)
3655 pf_tag_packet(m, tag);
3661 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
3662 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3665 struct pf_state key;
3666 struct tcphdr *th = pd->hdr.tcp;
3667 u_int16_t win = ntohs(th->th_win);
3668 u_int32_t ack, end, seq;
3672 struct pf_state_peer *src, *dst;
3675 key.proto = IPPROTO_TCP;
3676 if (direction == PF_IN) {
3677 PF_ACPY(&key.ext.addr, pd->src, key.af);
3678 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
3679 key.ext.port = th->th_sport;
3680 key.gwy.port = th->th_dport;
3682 PF_ACPY(&key.lan.addr, pd->src, key.af);
3683 PF_ACPY(&key.ext.addr, pd->dst, key.af);
3684 key.lan.port = th->th_sport;
3685 key.ext.port = th->th_dport;
3690 if (direction == (*state)->direction) {
3691 src = &(*state)->src;
3692 dst = &(*state)->dst;
3694 src = &(*state)->dst;
3695 dst = &(*state)->src;
3698 if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
3699 if (direction != (*state)->direction)
3700 return (PF_SYNPROXY_DROP);
3701 if (th->th_flags & TH_SYN) {
3702 if (ntohl(th->th_seq) != (*state)->src.seqlo)
3704 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3705 pd->src, th->th_dport, th->th_sport,
3706 (*state)->src.seqhi, ntohl(th->th_seq) + 1,
3707 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0);
3708 return (PF_SYNPROXY_DROP);
3709 } else if (!(th->th_flags & TH_ACK) ||
3710 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3711 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3714 (*state)->src.state = PF_TCPS_PROXY_DST;
3716 if ((*state)->src.state == PF_TCPS_PROXY_DST) {
3717 struct pf_state_host *src, *dst;
3719 if (direction == PF_OUT) {
3720 src = &(*state)->gwy;
3721 dst = &(*state)->ext;
3723 src = &(*state)->ext;
3724 dst = &(*state)->lan;
3726 if (direction == (*state)->direction) {
3727 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
3728 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3729 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3731 (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
3732 if ((*state)->dst.seqhi == 1)
3733 (*state)->dst.seqhi = arc4random();
3734 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3735 &dst->addr, src->port, dst->port,
3736 (*state)->dst.seqhi, 0, TH_SYN, 0,
3737 (*state)->src.mss, 0);
3738 return (PF_SYNPROXY_DROP);
3739 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
3741 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1))
3744 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
3745 (*state)->dst.seqlo = ntohl(th->th_seq);
3746 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3747 pd->src, th->th_dport, th->th_sport,
3748 ntohl(th->th_ack), ntohl(th->th_seq) + 1,
3749 TH_ACK, (*state)->src.max_win, 0, 0);
3750 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3751 &dst->addr, src->port, dst->port,
3752 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
3753 TH_ACK, (*state)->dst.max_win, 0, 0);
3754 (*state)->src.seqdiff = (*state)->dst.seqhi -
3755 (*state)->src.seqlo;
3756 (*state)->dst.seqdiff = (*state)->src.seqhi -
3757 (*state)->dst.seqlo;
3758 (*state)->src.seqhi = (*state)->src.seqlo +
3759 (*state)->src.max_win;
3760 (*state)->dst.seqhi = (*state)->dst.seqlo +
3761 (*state)->dst.max_win;
3762 (*state)->src.wscale = (*state)->dst.wscale = 0;
3763 (*state)->src.state = (*state)->dst.state =
3765 return (PF_SYNPROXY_DROP);
3769 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
3770 sws = src->wscale & PF_WSCALE_MASK;
3771 dws = dst->wscale & PF_WSCALE_MASK;
3776 * Sequence tracking algorithm from Guido van Rooij's paper:
3777 * http://www.madison-gurkha.com/publications/tcp_filtering/
3781 seq = ntohl(th->th_seq);
3782 if (src->seqlo == 0) {
3783 /* First packet from this end. Set its state */
3785 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
3786 src->scrub == NULL) {
3787 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
3788 REASON_SET(reason, PFRES_MEMORY);
3793 /* Deferred generation of sequence number modulator */
3794 if (dst->seqdiff && !src->seqdiff) {
3795 while ((src->seqdiff = arc4random()) == 0)
3797 ack = ntohl(th->th_ack) - dst->seqdiff;
3798 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3800 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3803 ack = ntohl(th->th_ack);
3806 end = seq + pd->p_len;
3807 if (th->th_flags & TH_SYN) {
3809 if (dst->wscale & PF_WSCALE_FLAG) {
3810 src->wscale = pf_get_wscale(m, off, th->th_off,
3812 if (src->wscale & PF_WSCALE_FLAG) {
3813 /* Remove scale factor from initial
3815 sws = src->wscale & PF_WSCALE_MASK;
3816 win = ((u_int32_t)win + (1 << sws) - 1)
3818 dws = dst->wscale & PF_WSCALE_MASK;
3820 /* fixup other window */
3821 dst->max_win <<= dst->wscale &
3823 /* in case of a retrans SYN|ACK */
3828 if (th->th_flags & TH_FIN)
3832 if (src->state < TCPS_SYN_SENT)
3833 src->state = TCPS_SYN_SENT;
3836 * May need to slide the window (seqhi may have been set by
3837 * the crappy stack check or if we picked up the connection
3838 * after establishment)
3840 if (src->seqhi == 1 ||
3841 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
3842 src->seqhi = end + MAX(1, dst->max_win << dws);
3843 if (win > src->max_win)
3847 ack = ntohl(th->th_ack) - dst->seqdiff;
3849 /* Modulate sequence numbers */
3850 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3852 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3855 end = seq + pd->p_len;
3856 if (th->th_flags & TH_SYN)
3858 if (th->th_flags & TH_FIN)
3862 if ((th->th_flags & TH_ACK) == 0) {
3863 /* Let it pass through the ack skew check */
3865 } else if ((ack == 0 &&
3866 (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
3867 /* broken tcp stacks do not set ack */
3868 (dst->state < TCPS_SYN_SENT)) {
3870 * Many stacks (ours included) will set the ACK number in an
3871 * FIN|ACK if the SYN times out -- no sequence to ACK.
3877 /* Ease sequencing restrictions on no data packets */
3882 ackskew = dst->seqlo - ack;
3884 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
3885 if (SEQ_GEQ(src->seqhi, end) &&
3886 /* Last octet inside other's window space */
3887 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
3888 /* Retrans: not more than one window back */
3889 (ackskew >= -MAXACKWINDOW) &&
3890 /* Acking not more than one reassembled fragment backwards */
3891 (ackskew <= (MAXACKWINDOW << sws))) {
3892 /* Acking not more than one window forward */
3894 /* update max window */
3895 if (src->max_win < win)
3897 /* synchronize sequencing */
3898 if (SEQ_GT(end, src->seqlo))
3900 /* slide the window of what the other end can send */
3901 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
3902 dst->seqhi = ack + MAX((win << sws), 1);
3906 if (th->th_flags & TH_SYN)
3907 if (src->state < TCPS_SYN_SENT)
3908 src->state = TCPS_SYN_SENT;
3909 if (th->th_flags & TH_FIN)
3910 if (src->state < TCPS_CLOSING)
3911 src->state = TCPS_CLOSING;
3912 if (th->th_flags & TH_ACK) {
3913 if (dst->state == TCPS_SYN_SENT)
3914 dst->state = TCPS_ESTABLISHED;
3915 else if (dst->state == TCPS_CLOSING)
3916 dst->state = TCPS_FIN_WAIT_2;
3918 if (th->th_flags & TH_RST)
3919 src->state = dst->state = TCPS_TIME_WAIT;
3921 /* update expire time */
3922 (*state)->expire = time_second;
3923 if (src->state >= TCPS_FIN_WAIT_2 &&
3924 dst->state >= TCPS_FIN_WAIT_2)
3925 (*state)->timeout = PFTM_TCP_CLOSED;
3926 else if (src->state >= TCPS_FIN_WAIT_2 ||
3927 dst->state >= TCPS_FIN_WAIT_2)
3928 (*state)->timeout = PFTM_TCP_FIN_WAIT;
3929 else if (src->state < TCPS_ESTABLISHED ||
3930 dst->state < TCPS_ESTABLISHED)
3931 (*state)->timeout = PFTM_TCP_OPENING;
3932 else if (src->state >= TCPS_CLOSING ||
3933 dst->state >= TCPS_CLOSING)
3934 (*state)->timeout = PFTM_TCP_CLOSING;
3936 (*state)->timeout = PFTM_TCP_ESTABLISHED;
3938 /* Fall through to PASS packet */
3940 } else if ((dst->state < TCPS_SYN_SENT ||
3941 dst->state >= TCPS_FIN_WAIT_2 ||
3942 src->state >= TCPS_FIN_WAIT_2) &&
3943 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
3944 /* Within a window forward of the originating packet */
3945 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
3946 /* Within a window backward of the originating packet */
3949 * This currently handles three situations:
3950 * 1) Stupid stacks will shotgun SYNs before their peer
3952 * 2) When PF catches an already established stream (the
3953 * firewall rebooted, the state table was flushed, routes
3955 * 3) Packets get funky immediately after the connection
3956 * closes (this should catch Solaris spurious ACK|FINs
3957 * that web servers like to spew after a close)
3959 * This must be a little more careful than the above code
3960 * since packet floods will also be caught here. We don't
3961 * update the TTL here to mitigate the damage of a packet
3962 * flood and so the same code can handle awkward establishment
3963 * and a loosened connection close.
3964 * In the establishment case, a correct peer response will
3965 * validate the connection, go through the normal state code
3966 * and keep updating the state TTL.
3969 if (pf_status.debug >= PF_DEBUG_MISC) {
3970 printf("pf: loose state match: ");
3971 pf_print_state(*state);
3972 pf_print_flags(th->th_flags);
3973 printf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d\n",
3974 seq, ack, pd->p_len, ackskew,
3975 (*state)->packets[0], (*state)->packets[1]);
3978 /* update max window */
3979 if (src->max_win < win)
3981 /* synchronize sequencing */
3982 if (SEQ_GT(end, src->seqlo))
3984 /* slide the window of what the other end can send */
3985 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
3986 dst->seqhi = ack + MAX((win << sws), 1);
3989 * Cannot set dst->seqhi here since this could be a shotgunned
3990 * SYN and not an already established connection.
3993 if (th->th_flags & TH_FIN)
3994 if (src->state < TCPS_CLOSING)
3995 src->state = TCPS_CLOSING;
3996 if (th->th_flags & TH_RST)
3997 src->state = dst->state = TCPS_TIME_WAIT;
3999 /* Fall through to PASS packet */
4002 if ((*state)->dst.state == TCPS_SYN_SENT &&
4003 (*state)->src.state == TCPS_SYN_SENT) {
4004 /* Send RST for state mismatches during handshake */
4005 if (!(th->th_flags & TH_RST)) {
4006 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
4008 if (th->th_flags & TH_SYN)
4010 if (th->th_flags & TH_FIN)
4012 pf_send_tcp((*state)->rule.ptr, pd->af,
4013 pd->dst, pd->src, th->th_dport,
4014 th->th_sport, ntohl(th->th_ack), ack,
4015 TH_RST|TH_ACK, 0, 0,
4016 (*state)->rule.ptr->return_ttl);
4021 } else if (pf_status.debug >= PF_DEBUG_MISC) {
4022 printf("pf: BAD state: ");
4023 pf_print_state(*state);
4024 pf_print_flags(th->th_flags);
4025 printf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d "
4026 "dir=%s,%s\n", seq, ack, pd->p_len, ackskew,
4027 (*state)->packets[0], (*state)->packets[1],
4028 direction == PF_IN ? "in" : "out",
4029 direction == (*state)->direction ? "fwd" : "rev");
4030 printf("pf: State failure on: %c %c %c %c | %c %c\n",
4031 SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
4032 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
4034 (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
4035 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
4036 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
4037 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
4042 if (dst->scrub || src->scrub) {
4043 if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4044 src, dst, ©back))
4048 /* Any packets which have gotten here are to be passed */
4050 /* translate source/destination address, if necessary */
4051 if (STATE_TRANSLATE(*state)) {
4052 if (direction == PF_OUT)
4053 pf_change_ap(pd->src, &th->th_sport, pd->ip_sum,
4054 &th->th_sum, &(*state)->gwy.addr,
4055 (*state)->gwy.port, 0, pd->af);
4057 pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum,
4058 &th->th_sum, &(*state)->lan.addr,
4059 (*state)->lan.port, 0, pd->af);
4060 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4061 } else if (copyback) {
4062 /* Copyback sequence modulation or stateful scrub changes */
4063 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4070 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif,
4071 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4073 struct pf_state_peer *src, *dst;
4074 struct pf_state key;
4075 struct udphdr *uh = pd->hdr.udp;
4078 key.proto = IPPROTO_UDP;
4079 if (direction == PF_IN) {
4080 PF_ACPY(&key.ext.addr, pd->src, key.af);
4081 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4082 key.ext.port = uh->uh_sport;
4083 key.gwy.port = uh->uh_dport;
4085 PF_ACPY(&key.lan.addr, pd->src, key.af);
4086 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4087 key.lan.port = uh->uh_sport;
4088 key.ext.port = uh->uh_dport;
4093 if (direction == (*state)->direction) {
4094 src = &(*state)->src;
4095 dst = &(*state)->dst;
4097 src = &(*state)->dst;
4098 dst = &(*state)->src;
4102 if (src->state < PFUDPS_SINGLE)
4103 src->state = PFUDPS_SINGLE;
4104 if (dst->state == PFUDPS_SINGLE)
4105 dst->state = PFUDPS_MULTIPLE;
4107 /* update expire time */
4108 (*state)->expire = time_second;
4109 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
4110 (*state)->timeout = PFTM_UDP_MULTIPLE;
4112 (*state)->timeout = PFTM_UDP_SINGLE;
4114 /* translate source/destination address, if necessary */
4115 if (STATE_TRANSLATE(*state)) {
4116 if (direction == PF_OUT)
4117 pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum,
4118 &uh->uh_sum, &(*state)->gwy.addr,
4119 (*state)->gwy.port, 1, pd->af);
4121 pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum,
4122 &uh->uh_sum, &(*state)->lan.addr,
4123 (*state)->lan.port, 1, pd->af);
4124 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
4131 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif,
4132 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4134 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
4135 u_int16_t icmpid = 0;
4136 u_int16_t *icmpsum = NULL;
4137 u_int8_t icmptype = 0;
4140 switch (pd->proto) {
4143 icmptype = pd->hdr.icmp->icmp_type;
4144 icmpid = pd->hdr.icmp->icmp_id;
4145 icmpsum = &pd->hdr.icmp->icmp_cksum;
4147 if (icmptype == ICMP_UNREACH ||
4148 icmptype == ICMP_SOURCEQUENCH ||
4149 icmptype == ICMP_REDIRECT ||
4150 icmptype == ICMP_TIMXCEED ||
4151 icmptype == ICMP_PARAMPROB)
4156 case IPPROTO_ICMPV6:
4157 icmptype = pd->hdr.icmp6->icmp6_type;
4158 icmpid = pd->hdr.icmp6->icmp6_id;
4159 icmpsum = &pd->hdr.icmp6->icmp6_cksum;
4161 if (icmptype == ICMP6_DST_UNREACH ||
4162 icmptype == ICMP6_PACKET_TOO_BIG ||
4163 icmptype == ICMP6_TIME_EXCEEDED ||
4164 icmptype == ICMP6_PARAM_PROB)
4173 * ICMP query/reply message not related to a TCP/UDP packet.
4174 * Search for an ICMP state.
4176 struct pf_state key;
4179 key.proto = pd->proto;
4180 if (direction == PF_IN) {
4181 PF_ACPY(&key.ext.addr, pd->src, key.af);
4182 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4183 key.ext.port = icmpid;
4184 key.gwy.port = icmpid;
4186 PF_ACPY(&key.lan.addr, pd->src, key.af);
4187 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4188 key.lan.port = icmpid;
4189 key.ext.port = icmpid;
4194 (*state)->expire = time_second;
4195 (*state)->timeout = PFTM_ICMP_ERROR_REPLY;
4197 /* translate source/destination address, if necessary */
4198 if (PF_ANEQ(&(*state)->lan.addr, &(*state)->gwy.addr, pd->af)) {
4199 if (direction == PF_OUT) {
4203 pf_change_a(&saddr->v4.s_addr,
4205 (*state)->gwy.addr.v4.s_addr, 0);
4211 &pd->hdr.icmp6->icmp6_cksum,
4212 &(*state)->gwy.addr, 0);
4214 sizeof(struct icmp6_hdr),
4215 (caddr_t)pd->hdr.icmp6);
4223 pf_change_a(&daddr->v4.s_addr,
4225 (*state)->lan.addr.v4.s_addr, 0);
4231 &pd->hdr.icmp6->icmp6_cksum,
4232 &(*state)->lan.addr, 0);
4234 sizeof(struct icmp6_hdr),
4235 (caddr_t)pd->hdr.icmp6);
4246 * ICMP error message in response to a TCP/UDP packet.
4247 * Extract the inner TCP/UDP header and search for that state.
4250 struct pf_pdesc pd2;
4255 struct ip6_hdr h2_6;
4265 /* offset of h2 in mbuf chain */
4266 ipoff2 = off + ICMP_MINLEN;
4268 if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
4269 NULL, NULL, pd2.af)) {
4270 DPFPRINTF(PF_DEBUG_MISC,
4271 ("pf: ICMP error message too short "
4276 * ICMP error messages don't refer to non-first
4280 * Note: We are dealing with an encapsulated
4281 * header. This means ip_off/ip_len are not
4282 * in host byte order!
4284 if (h2.ip_off & htons(IP_OFFMASK))
4287 /* offset of protocol header that follows h2 */
4288 off2 = ipoff2 + (h2.ip_hl << 2);
4290 pd2.proto = h2.ip_p;
4291 pd2.src = (struct pf_addr *)&h2.ip_src;
4292 pd2.dst = (struct pf_addr *)&h2.ip_dst;
4293 pd2.ip_sum = &h2.ip_sum;
4298 ipoff2 = off + sizeof(struct icmp6_hdr);
4300 if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
4301 NULL, NULL, pd2.af)) {
4302 DPFPRINTF(PF_DEBUG_MISC,
4303 ("pf: ICMP error message too short "
4307 pd2.proto = h2_6.ip6_nxt;
4308 pd2.src = (struct pf_addr *)&h2_6.ip6_src;
4309 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
4311 off2 = ipoff2 + sizeof(h2_6);
4313 switch (pd2.proto) {
4314 case IPPROTO_FRAGMENT:
4316 * ICMPv6 error messages for
4317 * non-first fragments
4321 case IPPROTO_HOPOPTS:
4322 case IPPROTO_ROUTING:
4323 case IPPROTO_DSTOPTS: {
4324 /* get next header and header length */
4325 struct ip6_ext opt6;
4327 if (!pf_pull_hdr(m, off2, &opt6,
4328 sizeof(opt6), NULL, NULL, pd2.af)) {
4329 DPFPRINTF(PF_DEBUG_MISC,
4330 ("pf: ICMPv6 short opt\n"));
4333 if (pd2.proto == IPPROTO_AH)
4334 off2 += (opt6.ip6e_len + 2) * 4;
4336 off2 += (opt6.ip6e_len + 1) * 8;
4337 pd2.proto = opt6.ip6e_nxt;
4338 /* goto the next header */
4345 } while (!terminal);
4350 switch (pd2.proto) {
4354 struct pf_state key;
4355 struct pf_state_peer *src, *dst;
4360 * Only the first 8 bytes of the TCP header can be
4361 * expected. Don't access any TCP header fields after
4362 * th_seq, an ackskew test is not possible.
4364 if (!pf_pull_hdr(m, off2, &th, 8, NULL, NULL, pd2.af)) {
4365 DPFPRINTF(PF_DEBUG_MISC,
4366 ("pf: ICMP error message too short "
4372 key.proto = IPPROTO_TCP;
4373 if (direction == PF_IN) {
4374 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4375 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4376 key.ext.port = th.th_dport;
4377 key.gwy.port = th.th_sport;
4379 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4380 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4381 key.lan.port = th.th_dport;
4382 key.ext.port = th.th_sport;
4387 if (direction == (*state)->direction) {
4388 src = &(*state)->dst;
4389 dst = &(*state)->src;
4391 src = &(*state)->src;
4392 dst = &(*state)->dst;
4395 if (src->wscale && dst->wscale &&
4396 !(th.th_flags & TH_SYN))
4397 dws = dst->wscale & PF_WSCALE_MASK;
4401 /* Demodulate sequence number */
4402 seq = ntohl(th.th_seq) - src->seqdiff;
4404 pf_change_a(&th.th_seq, icmpsum,
4409 if (!SEQ_GEQ(src->seqhi, seq) ||
4410 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws))) {
4411 if (pf_status.debug >= PF_DEBUG_MISC) {
4412 printf("pf: BAD ICMP %d:%d ",
4413 icmptype, pd->hdr.icmp->icmp_code);
4414 pf_print_host(pd->src, 0, pd->af);
4416 pf_print_host(pd->dst, 0, pd->af);
4418 pf_print_state(*state);
4419 printf(" seq=%u\n", seq);
4424 if (STATE_TRANSLATE(*state)) {
4425 if (direction == PF_IN) {
4426 pf_change_icmp(pd2.src, &th.th_sport,
4427 daddr, &(*state)->lan.addr,
4428 (*state)->lan.port, NULL,
4429 pd2.ip_sum, icmpsum,
4430 pd->ip_sum, 0, pd2.af);
4432 pf_change_icmp(pd2.dst, &th.th_dport,
4433 saddr, &(*state)->gwy.addr,
4434 (*state)->gwy.port, NULL,
4435 pd2.ip_sum, icmpsum,
4436 pd->ip_sum, 0, pd2.af);
4445 m_copyback(m, off, ICMP_MINLEN,
4446 (caddr_t)pd->hdr.icmp);
4447 m_copyback(m, ipoff2, sizeof(h2),
4454 sizeof(struct icmp6_hdr),
4455 (caddr_t)pd->hdr.icmp6);
4456 m_copyback(m, ipoff2, sizeof(h2_6),
4461 m_copyback(m, off2, 8, (caddr_t)&th);
4469 struct pf_state key;
4471 if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
4472 NULL, NULL, pd2.af)) {
4473 DPFPRINTF(PF_DEBUG_MISC,
4474 ("pf: ICMP error message too short "
4480 key.proto = IPPROTO_UDP;
4481 if (direction == PF_IN) {
4482 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4483 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4484 key.ext.port = uh.uh_dport;
4485 key.gwy.port = uh.uh_sport;
4487 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4488 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4489 key.lan.port = uh.uh_dport;
4490 key.ext.port = uh.uh_sport;
4495 if (STATE_TRANSLATE(*state)) {
4496 if (direction == PF_IN) {
4497 pf_change_icmp(pd2.src, &uh.uh_sport,
4498 daddr, &(*state)->lan.addr,
4499 (*state)->lan.port, &uh.uh_sum,
4500 pd2.ip_sum, icmpsum,
4501 pd->ip_sum, 1, pd2.af);
4503 pf_change_icmp(pd2.dst, &uh.uh_dport,
4504 saddr, &(*state)->gwy.addr,
4505 (*state)->gwy.port, &uh.uh_sum,
4506 pd2.ip_sum, icmpsum,
4507 pd->ip_sum, 1, pd2.af);
4512 m_copyback(m, off, ICMP_MINLEN,
4513 (caddr_t)pd->hdr.icmp);
4514 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4520 sizeof(struct icmp6_hdr),
4521 (caddr_t)pd->hdr.icmp6);
4522 m_copyback(m, ipoff2, sizeof(h2_6),
4527 m_copyback(m, off2, sizeof(uh), (caddr_t)&uh);
4534 case IPPROTO_ICMP: {
4536 struct pf_state key;
4538 if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
4539 NULL, NULL, pd2.af)) {
4540 DPFPRINTF(PF_DEBUG_MISC,
4541 ("pf: ICMP error message too short i"
4547 key.proto = IPPROTO_ICMP;
4548 if (direction == PF_IN) {
4549 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4550 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4551 key.ext.port = iih.icmp_id;
4552 key.gwy.port = iih.icmp_id;
4554 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4555 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4556 key.lan.port = iih.icmp_id;
4557 key.ext.port = iih.icmp_id;
4562 if (STATE_TRANSLATE(*state)) {
4563 if (direction == PF_IN) {
4564 pf_change_icmp(pd2.src, &iih.icmp_id,
4565 daddr, &(*state)->lan.addr,
4566 (*state)->lan.port, NULL,
4567 pd2.ip_sum, icmpsum,
4568 pd->ip_sum, 0, AF_INET);
4570 pf_change_icmp(pd2.dst, &iih.icmp_id,
4571 saddr, &(*state)->gwy.addr,
4572 (*state)->gwy.port, NULL,
4573 pd2.ip_sum, icmpsum,
4574 pd->ip_sum, 0, AF_INET);
4576 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
4577 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4578 m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih);
4586 case IPPROTO_ICMPV6: {
4587 struct icmp6_hdr iih;
4588 struct pf_state key;
4590 if (!pf_pull_hdr(m, off2, &iih,
4591 sizeof(struct icmp6_hdr), NULL, NULL, pd2.af)) {
4592 DPFPRINTF(PF_DEBUG_MISC,
4593 ("pf: ICMP error message too short "
4599 key.proto = IPPROTO_ICMPV6;
4600 if (direction == PF_IN) {
4601 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4602 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4603 key.ext.port = iih.icmp6_id;
4604 key.gwy.port = iih.icmp6_id;
4606 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4607 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4608 key.lan.port = iih.icmp6_id;
4609 key.ext.port = iih.icmp6_id;
4614 if (STATE_TRANSLATE(*state)) {
4615 if (direction == PF_IN) {
4616 pf_change_icmp(pd2.src, &iih.icmp6_id,
4617 daddr, &(*state)->lan.addr,
4618 (*state)->lan.port, NULL,
4619 pd2.ip_sum, icmpsum,
4620 pd->ip_sum, 0, AF_INET6);
4622 pf_change_icmp(pd2.dst, &iih.icmp6_id,
4623 saddr, &(*state)->gwy.addr,
4624 (*state)->gwy.port, NULL,
4625 pd2.ip_sum, icmpsum,
4626 pd->ip_sum, 0, AF_INET6);
4628 m_copyback(m, off, sizeof(struct icmp6_hdr),
4629 (caddr_t)pd->hdr.icmp6);
4630 m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6);
4631 m_copyback(m, off2, sizeof(struct icmp6_hdr),
4640 struct pf_state key;
4643 key.proto = pd2.proto;
4644 if (direction == PF_IN) {
4645 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4646 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4650 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4651 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4658 if (STATE_TRANSLATE(*state)) {
4659 if (direction == PF_IN) {
4660 pf_change_icmp(pd2.src, NULL,
4661 daddr, &(*state)->lan.addr,
4663 pd2.ip_sum, icmpsum,
4664 pd->ip_sum, 0, pd2.af);
4666 pf_change_icmp(pd2.dst, NULL,
4667 saddr, &(*state)->gwy.addr,
4669 pd2.ip_sum, icmpsum,
4670 pd->ip_sum, 0, pd2.af);
4675 m_copyback(m, off, ICMP_MINLEN,
4676 (caddr_t)pd->hdr.icmp);
4677 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4683 sizeof(struct icmp6_hdr),
4684 (caddr_t)pd->hdr.icmp6);
4685 m_copyback(m, ipoff2, sizeof(h2_6),
4700 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif,
4701 struct pf_pdesc *pd)
4703 struct pf_state_peer *src, *dst;
4704 struct pf_state key;
4707 key.proto = pd->proto;
4708 if (direction == PF_IN) {
4709 PF_ACPY(&key.ext.addr, pd->src, key.af);
4710 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4714 PF_ACPY(&key.lan.addr, pd->src, key.af);
4715 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4722 if (direction == (*state)->direction) {
4723 src = &(*state)->src;
4724 dst = &(*state)->dst;
4726 src = &(*state)->dst;
4727 dst = &(*state)->src;
4731 if (src->state < PFOTHERS_SINGLE)
4732 src->state = PFOTHERS_SINGLE;
4733 if (dst->state == PFOTHERS_SINGLE)
4734 dst->state = PFOTHERS_MULTIPLE;
4736 /* update expire time */
4737 (*state)->expire = time_second;
4738 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
4739 (*state)->timeout = PFTM_OTHER_MULTIPLE;
4741 (*state)->timeout = PFTM_OTHER_SINGLE;
4743 /* translate source/destination address, if necessary */
4744 if (STATE_TRANSLATE(*state)) {
4745 if (direction == PF_OUT)
4749 pf_change_a(&pd->src->v4.s_addr,
4750 pd->ip_sum, (*state)->gwy.addr.v4.s_addr,
4756 PF_ACPY(pd->src, &(*state)->gwy.addr, pd->af);
4764 pf_change_a(&pd->dst->v4.s_addr,
4765 pd->ip_sum, (*state)->lan.addr.v4.s_addr,
4771 PF_ACPY(pd->dst, &(*state)->lan.addr, pd->af);
4781 * ipoff and off are measured from the start of the mbuf chain.
4782 * h must be at "ipoff" on the mbuf chain.
4785 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
4786 u_short *actionp, u_short *reasonp, sa_family_t af)
4791 struct ip *h = mtod(m, struct ip *);
4792 u_int16_t fragoff = (h->ip_off & IP_OFFMASK) << 3;
4796 ACTION_SET(actionp, PF_PASS);
4798 ACTION_SET(actionp, PF_DROP);
4799 REASON_SET(reasonp, PFRES_FRAG);
4803 if (m->m_pkthdr.len < off + len ||
4804 h->ip_len < off + len) {
4805 ACTION_SET(actionp, PF_DROP);
4806 REASON_SET(reasonp, PFRES_SHORT);
4814 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
4816 if (m->m_pkthdr.len < off + len ||
4817 (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
4818 (unsigned)(off + len)) {
4819 ACTION_SET(actionp, PF_DROP);
4820 REASON_SET(reasonp, PFRES_SHORT);
4827 m_copydata(m, off, len, p);
4832 pf_routable(struct pf_addr *addr, sa_family_t af)
4834 struct sockaddr_in *dst;
4838 bzero(&ro, sizeof(ro));
4839 dst = satosin(&ro.ro_dst);
4840 dst->sin_family = af;
4841 dst->sin_len = sizeof(*dst);
4842 dst->sin_addr = addr->v4;
4843 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
4845 if (ro.ro_rt != NULL) {
4855 pf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
4858 struct mbuf *m0, *m1;
4859 struct route iproute;
4860 struct route *ro = NULL;
4861 struct sockaddr_in *dst;
4863 struct ifnet *ifp = NULL;
4864 struct pf_addr naddr;
4865 struct pf_src_node *sn = NULL;
4869 if (m == NULL || *m == NULL || r == NULL ||
4870 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
4871 panic("pf_route: invalid parameters");
4873 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
4874 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
4875 (*m)->m_pkthdr.pf_routed = 1;
4877 if ((*m)->m_pkthdr.pf_routed > 3) {
4882 (*m)->m_pkthdr.pf_routed++;
4885 if (r->rt == PF_DUPTO) {
4886 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
4889 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
4894 if (m0->m_len < sizeof(struct ip))
4895 panic("pf_route: m0->m_len < sizeof(struct ip)");
4896 ip = mtod(m0, struct ip *);
4899 bzero((caddr_t)ro, sizeof(*ro));
4900 dst = satosin(&ro->ro_dst);
4901 dst->sin_family = AF_INET;
4902 dst->sin_len = sizeof(*dst);
4903 dst->sin_addr = ip->ip_dst;
4905 if (r->rt == PF_FASTROUTE) {
4907 if (ro->ro_rt == 0) {
4908 ipstat.ips_noroute++;
4912 ifp = ro->ro_rt->rt_ifp;
4913 ro->ro_rt->rt_use++;
4915 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
4916 dst = satosin(ro->ro_rt->rt_gateway);
4918 if (TAILQ_EMPTY(&r->rpool.list))
4919 panic("pf_route: TAILQ_EMPTY(&r->rpool.list)");
4921 pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
4923 if (!PF_AZERO(&naddr, AF_INET))
4924 dst->sin_addr.s_addr = naddr.v4.s_addr;
4925 ifp = r->rpool.cur->kif ?
4926 r->rpool.cur->kif->pfik_ifp : NULL;
4928 if (!PF_AZERO(&s->rt_addr, AF_INET))
4929 dst->sin_addr.s_addr =
4930 s->rt_addr.v4.s_addr;
4931 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
4938 if (pf_test(PF_OUT, ifp, &m0) != PF_PASS)
4940 else if (m0 == NULL)
4942 if (m0->m_len < sizeof(struct ip))
4943 panic("pf_route: m0->m_len < sizeof(struct ip)");
4944 ip = mtod(m0, struct ip *);
4947 /* Copied from ip_output. */
4948 m0->m_pkthdr.csum_flags |= CSUM_IP;
4949 sw_csum = m0->m_pkthdr.csum_flags & ~ifp->if_hwassist;
4950 if (sw_csum & CSUM_DELAY_DATA) {
4951 in_delayed_cksum(m0);
4952 sw_csum &= ~CSUM_DELAY_DATA;
4954 m0->m_pkthdr.csum_flags &= ifp->if_hwassist;
4957 * If small enough for interface, or the interface will take
4958 * care of the fragmentation for us, can just send directly.
4960 if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) &&
4961 (ip->ip_off & IP_DF) == 0)) {
4962 ip->ip_len = htons(ip->ip_len);
4963 ip->ip_off = htons(ip->ip_off);
4965 if (sw_csum & CSUM_DELAY_IP) {
4967 if (ip->ip_v == IPVERSION &&
4968 (ip->ip_hl << 2) == sizeof(*ip)) {
4969 ip->ip_sum = in_cksum_hdr(ip);
4971 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
4975 error = (*ifp->if_output)(ifp, m0, sintosa(dst), ro->ro_rt);
4980 * Too large for interface; fragment if possible.
4981 * Must be able to put at least 8 bytes per fragment.
4983 if (ip->ip_off & IP_DF) {
4984 ipstat.ips_cantfrag++;
4985 if (r->rt != PF_DUPTO) {
4986 icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
4994 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist, sw_csum);
4998 for (m0 = m1; m0; m0 = m1) {
5002 error = (*ifp->if_output)(ifp, m0, sintosa(dst),
5009 ipstat.ips_fragmented++;
5012 if (r->rt != PF_DUPTO)
5014 if (ro == &iproute && ro->ro_rt)
5026 pf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
5030 struct route_in6 ip6route;
5031 struct route_in6 *ro;
5032 struct sockaddr_in6 *dst;
5033 struct ip6_hdr *ip6;
5034 struct ifnet *ifp = NULL;
5035 struct pf_addr naddr;
5036 struct pf_src_node *sn = NULL;
5039 if (m == NULL || *m == NULL || r == NULL ||
5040 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
5041 panic("pf_route6: invalid parameters");
5043 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
5044 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
5045 (*m)->m_pkthdr.pf_routed = 1;
5047 if ((*m)->m_pkthdr.pf_routed > 3) {
5052 (*m)->m_pkthdr.pf_routed++;
5055 if (r->rt == PF_DUPTO) {
5056 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
5059 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
5064 if (m0->m_len < sizeof(struct ip6_hdr))
5065 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5066 ip6 = mtod(m0, struct ip6_hdr *);
5069 bzero((caddr_t)ro, sizeof(*ro));
5070 dst = (struct sockaddr_in6 *)&ro->ro_dst;
5071 dst->sin6_family = AF_INET6;
5072 dst->sin6_len = sizeof(*dst);
5073 dst->sin6_addr = ip6->ip6_dst;
5076 if (r->rt == PF_FASTROUTE) {
5077 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
5078 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
5082 if (TAILQ_EMPTY(&r->rpool.list))
5083 panic("pf_route6: TAILQ_EMPTY(&r->rpool.list)");
5085 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
5087 if (!PF_AZERO(&naddr, AF_INET6))
5088 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5090 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
5092 if (!PF_AZERO(&s->rt_addr, AF_INET6))
5093 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5094 &s->rt_addr, AF_INET6);
5095 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5101 if (pf_test6(PF_OUT, ifp, &m0) != PF_PASS)
5103 else if (m0 == NULL)
5105 if (m0->m_len < sizeof(struct ip6_hdr))
5106 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5107 ip6 = mtod(m0, struct ip6_hdr *);
5111 * If the packet is too large for the outgoing interface,
5112 * send back an icmp6 error.
5114 if (IN6_IS_ADDR_LINKLOCAL(&dst->sin6_addr))
5115 dst->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
5116 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
5117 error = nd6_output(ifp, ifp, m0, dst, NULL);
5119 in6_ifstat_inc(ifp, ifs6_in_toobig);
5120 if (r->rt != PF_DUPTO)
5121 icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
5127 if (r->rt != PF_DUPTO)
5139 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
5140 * off is the offset where the protocol header starts
5141 * len is the total length of protocol header plus payload
5142 * returns 0 when the checksum is valid, otherwise returns 1.
5146 * FreeBSD supports cksum offload for the following drivers.
5147 * em(4), gx(4), lge(4), nge(4), ti(4), xl(4)
5148 * If we can make full use of it we would outperform ipfw/ipfilter in
5149 * very heavy traffic.
5150 * I have not tested 'cause I don't have NICs that supports cksum offload.
5151 * (There might be problems. Typical phenomena would be
5152 * 1. No route message for UDP packet.
5153 * 2. No connection acceptance from external hosts regardless of rule set.)
5156 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p,
5163 if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
5165 if (m->m_pkthdr.len < off + len)
5171 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
5172 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
5173 sum = m->m_pkthdr.csum_data;
5175 ip = mtod(m, struct ip *);
5176 sum = in_pseudo(ip->ip_src.s_addr,
5177 ip->ip_dst.s_addr, htonl((u_short)len +
5178 m->m_pkthdr.csum_data + p));
5186 case IPPROTO_ICMPV6:
5196 if (p == IPPROTO_ICMP) {
5201 sum = in_cksum(m, len);
5205 if (m->m_len < sizeof(struct ip))
5207 sum = in_cksum_range(m, p, off, len);
5209 m->m_pkthdr.csum_flags |=
5212 m->m_pkthdr.csum_data = 0xffff;
5218 if (m->m_len < sizeof(struct ip6_hdr))
5220 sum = in6_cksum(m, p, off, len);
5223 * IPv6 H/W cksum off-load not supported yet!
5226 * m->m_pkthdr.csum_flags |=
5227 * (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
5228 * m->m_pkthdr.csum_data = 0xffff;
5240 tcpstat.tcps_rcvbadsum++;
5243 udpstat.udps_badsum++;
5246 icmpstat.icps_checksum++;
5249 case IPPROTO_ICMPV6:
5250 icmp6stat.icp6s_checksum++;
5261 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0)
5263 struct pfi_kif *kif;
5264 u_short action, reason = 0, log = 0;
5265 struct mbuf *m = *m0;
5266 struct ip *h = NULL;
5267 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5268 struct pf_state *s = NULL;
5269 struct pf_ruleset *ruleset = NULL;
5271 int off, dirndx, pqid = 0;
5273 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5276 kif = pfi_index2kif[ifp->if_index];
5281 if ((m->m_flags & M_PKTHDR) == 0)
5282 panic("non-M_PKTHDR is passed to pf_test");
5285 memset(&pd, 0, sizeof(pd));
5286 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5288 REASON_SET(&reason, PFRES_SHORT);
5293 /* We do IP header normalization and packet reassembly here */
5294 if (pf_normalize_ip(m0, dir, kif, &reason) != PF_PASS) {
5299 h = mtod(m, struct ip *);
5301 off = h->ip_hl << 2;
5302 if (off < (int)sizeof(*h)) {
5304 REASON_SET(&reason, PFRES_SHORT);
5309 pd.src = (struct pf_addr *)&h->ip_src;
5310 pd.dst = (struct pf_addr *)&h->ip_dst;
5311 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET);
5312 pd.ip_sum = &h->ip_sum;
5316 pd.tot_len = h->ip_len;
5318 /* handle fragments that didn't get reassembled by normalization */
5319 if (h->ip_off & (IP_MF | IP_OFFMASK)) {
5320 action = pf_test_fragment(&r, dir, kif, m, h,
5331 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5332 &action, &reason, AF_INET)) {
5333 log = action != PF_PASS;
5336 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5337 h->ip_len - off, IPPROTO_TCP, AF_INET)) {
5341 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5342 if ((th.th_flags & TH_ACK) && pd.p_len == 0)
5344 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5345 if (action == PF_DROP)
5347 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5349 if (action == PF_PASS) {
5351 pfsync_update_state(s);
5356 } else if (s == NULL)
5357 action = pf_test_tcp(&r, &s, dir, kif,
5358 m, off, h, &pd, &a, &ruleset);
5366 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5367 &action, &reason, AF_INET)) {
5368 log = action != PF_PASS;
5371 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5372 off, h->ip_len - off, IPPROTO_UDP, AF_INET)) {
5376 if (uh.uh_dport == 0 ||
5377 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5378 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5382 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5383 if (action == PF_PASS) {
5385 pfsync_update_state(s);
5390 } else if (s == NULL)
5391 action = pf_test_udp(&r, &s, dir, kif,
5392 m, off, h, &pd, &a, &ruleset);
5396 case IPPROTO_ICMP: {
5400 if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
5401 &action, &reason, AF_INET)) {
5402 log = action != PF_PASS;
5405 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5406 h->ip_len - off, IPPROTO_ICMP, AF_INET)) {
5410 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd);
5411 if (action == PF_PASS) {
5413 pfsync_update_state(s);
5418 } else if (s == NULL)
5419 action = pf_test_icmp(&r, &s, dir, kif,
5420 m, off, h, &pd, &a, &ruleset);
5425 action = pf_test_state_other(&s, dir, kif, &pd);
5426 if (action == PF_PASS) {
5428 pfsync_update_state(s);
5433 } else if (s == NULL)
5434 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5440 if (action == PF_PASS && h->ip_hl > 5 &&
5441 !((s && s->allow_opts) || r->allow_opts)) {
5443 REASON_SET(&reason, PFRES_SHORT);
5445 DPFPRINTF(PF_DEBUG_MISC,
5446 ("pf: dropping packet with ip options\n"));
5450 if (action == PF_PASS && r->qid) {
5451 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5452 if (pd.tos == IPTOS_LOWDELAY)
5453 m->m_pkthdr.altq_qid = r->pqid;
5455 m->m_pkthdr.altq_qid = r->qid;
5456 m->m_pkthdr.ecn_af = AF_INET;
5457 m->m_pkthdr.header = h;
5462 * connections redirected to loopback should not match sockets
5463 * bound specifically to loopback due to security implications,
5464 * see tcp_input() and in_pcblookup_listen().
5466 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5467 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5468 (s->nat_rule.ptr->action == PF_RDR ||
5469 s->nat_rule.ptr->action == PF_BINAT) &&
5470 (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
5472 REASON_SET(&reason, PFRES_MEMORY);
5475 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5478 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, a, ruleset);
5480 kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5481 kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++;
5483 if (action == PF_PASS || r->action == PF_DROP) {
5485 r->bytes += pd.tot_len;
5488 a->bytes += pd.tot_len;
5491 dirndx = (dir == s->direction) ? 0 : 1;
5492 s->packets[dirndx]++;
5493 s->bytes[dirndx] += pd.tot_len;
5494 if (s->nat_rule.ptr != NULL) {
5495 s->nat_rule.ptr->packets++;
5496 s->nat_rule.ptr->bytes += pd.tot_len;
5498 if (s->src_node != NULL) {
5499 s->src_node->packets++;
5500 s->src_node->bytes += pd.tot_len;
5502 if (s->nat_src_node != NULL) {
5503 s->nat_src_node->packets++;
5504 s->nat_src_node->bytes += pd.tot_len;
5508 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5512 * XXX: we need to make sure that the addresses
5513 * passed to pfr_update_stats() are the same than
5514 * the addresses used during matching (pfr_match)
5516 if (r == &pf_default_rule) {
5518 x = (s == NULL || s->direction == dir) ?
5519 &pd.baddr : &pd.naddr;
5521 x = (s == NULL || s->direction == dir) ?
5522 &pd.naddr : &pd.baddr;
5523 if (x == &pd.baddr || s == NULL) {
5524 /* we need to change the address */
5531 if (tr->src.addr.type == PF_ADDR_TABLE)
5532 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5533 s->direction == dir) ? pd.src : pd.dst, pd.af,
5534 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5536 if (tr->dst.addr.type == PF_ADDR_TABLE)
5537 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5538 s->direction == dir) ? pd.dst : pd.src, pd.af,
5539 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5544 if (action == PF_SYNPROXY_DROP) {
5549 /* pf_route can free the mbuf causing *m0 to become NULL */
5550 pf_route(m0, r, dir, ifp, s);
5558 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0)
5560 struct pfi_kif *kif;
5561 u_short action, reason = 0, log = 0;
5562 struct mbuf *m = *m0;
5563 struct ip6_hdr *h = NULL;
5564 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5565 struct pf_state *s = NULL;
5566 struct pf_ruleset *ruleset = NULL;
5568 int off, terminal = 0, dirndx;
5570 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5573 kif = pfi_index2kif[ifp->if_index];
5578 if ((m->m_flags & M_PKTHDR) == 0)
5579 panic("non-M_PKTHDR is passed to pf_test");
5582 memset(&pd, 0, sizeof(pd));
5583 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5585 REASON_SET(&reason, PFRES_SHORT);
5590 /* We do IP header normalization and packet reassembly here */
5591 if (pf_normalize_ip6(m0, dir, kif, &reason) != PF_PASS) {
5596 h = mtod(m, struct ip6_hdr *);
5598 pd.src = (struct pf_addr *)&h->ip6_src;
5599 pd.dst = (struct pf_addr *)&h->ip6_dst;
5600 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET6);
5604 pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
5606 off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
5607 pd.proto = h->ip6_nxt;
5610 case IPPROTO_FRAGMENT:
5611 action = pf_test_fragment(&r, dir, kif, m, h,
5613 if (action == PF_DROP)
5614 REASON_SET(&reason, PFRES_FRAG);
5617 case IPPROTO_HOPOPTS:
5618 case IPPROTO_ROUTING:
5619 case IPPROTO_DSTOPTS: {
5620 /* get next header and header length */
5621 struct ip6_ext opt6;
5623 if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
5624 NULL, NULL, pd.af)) {
5625 DPFPRINTF(PF_DEBUG_MISC,
5626 ("pf: IPv6 short opt\n"));
5628 REASON_SET(&reason, PFRES_SHORT);
5632 if (pd.proto == IPPROTO_AH)
5633 off += (opt6.ip6e_len + 2) * 4;
5635 off += (opt6.ip6e_len + 1) * 8;
5636 pd.proto = opt6.ip6e_nxt;
5637 /* goto the next header */
5644 } while (!terminal);
5652 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5653 &action, &reason, AF_INET6)) {
5654 log = action != PF_PASS;
5657 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5658 ntohs(h->ip6_plen), IPPROTO_TCP, AF_INET6)) {
5662 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5663 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5664 if (action == PF_DROP)
5666 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5668 if (action == PF_PASS) {
5670 pfsync_update_state(s);
5675 } else if (s == NULL)
5676 action = pf_test_tcp(&r, &s, dir, kif,
5677 m, off, h, &pd, &a, &ruleset);
5685 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5686 &action, &reason, AF_INET6)) {
5687 log = action != PF_PASS;
5690 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5691 off, ntohs(h->ip6_plen), IPPROTO_UDP, AF_INET6)) {
5695 if (uh.uh_dport == 0 ||
5696 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5697 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5701 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5702 if (action == PF_PASS) {
5704 pfsync_update_state(s);
5709 } else if (s == NULL)
5710 action = pf_test_udp(&r, &s, dir, kif,
5711 m, off, h, &pd, &a, &ruleset);
5715 case IPPROTO_ICMPV6: {
5716 struct icmp6_hdr ih;
5719 if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
5720 &action, &reason, AF_INET6)) {
5721 log = action != PF_PASS;
5724 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5725 ntohs(h->ip6_plen), IPPROTO_ICMPV6, AF_INET6)) {
5729 action = pf_test_state_icmp(&s, dir, kif,
5731 if (action == PF_PASS) {
5733 pfsync_update_state(s);
5738 } else if (s == NULL)
5739 action = pf_test_icmp(&r, &s, dir, kif,
5740 m, off, h, &pd, &a, &ruleset);
5745 action = pf_test_state_other(&s, dir, kif, &pd);
5746 if (action == PF_PASS) {
5750 } else if (s == NULL)
5751 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5757 /* XXX handle IPv6 options, if not allowed. not implemented. */
5760 if (action == PF_PASS && r->qid) {
5761 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5762 if (pd.tos == IPTOS_LOWDELAY)
5763 m->m_pkthdr.altq_qid = r->pqid;
5765 m->m_pkthdr.altq_qid = r->qid;
5766 m->m_pkthdr.ecn_af = AF_INET6;
5767 m->m_pkthdr.header = h;
5771 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5772 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5773 (s->nat_rule.ptr->action == PF_RDR ||
5774 s->nat_rule.ptr->action == PF_BINAT) &&
5775 IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) {
5777 REASON_SET(&reason, PFRES_MEMORY);
5780 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5783 PFLOG_PACKET(kif, h, m, AF_INET6, dir, reason, r, a, ruleset);
5785 kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5786 kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++;
5788 if (action == PF_PASS || r->action == PF_DROP) {
5790 r->bytes += pd.tot_len;
5793 a->bytes += pd.tot_len;
5796 dirndx = (dir == s->direction) ? 0 : 1;
5797 s->packets[dirndx]++;
5798 s->bytes[dirndx] += pd.tot_len;
5799 if (s->nat_rule.ptr != NULL) {
5800 s->nat_rule.ptr->packets++;
5801 s->nat_rule.ptr->bytes += pd.tot_len;
5803 if (s->src_node != NULL) {
5804 s->src_node->packets++;
5805 s->src_node->bytes += pd.tot_len;
5807 if (s->nat_src_node != NULL) {
5808 s->nat_src_node->packets++;
5809 s->nat_src_node->bytes += pd.tot_len;
5813 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5817 * XXX: we need to make sure that the addresses
5818 * passed to pfr_update_stats() are the same than
5819 * the addresses used during matching (pfr_match)
5821 if (r == &pf_default_rule) {
5823 x = (s == NULL || s->direction == dir) ?
5824 &pd.baddr : &pd.naddr;
5826 x = (s == NULL || s->direction == dir) ?
5827 &pd.naddr : &pd.baddr;
5829 if (x == &pd.baddr || s == NULL) {
5836 if (tr->src.addr.type == PF_ADDR_TABLE)
5837 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5838 s->direction == dir) ? pd.src : pd.dst, pd.af,
5839 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5841 if (tr->dst.addr.type == PF_ADDR_TABLE)
5842 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5843 s->direction == dir) ? pd.dst : pd.src, pd.af,
5844 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5849 if (action == PF_SYNPROXY_DROP) {
5854 /* pf_route6 can free the mbuf causing *m0 to become NULL */
5855 pf_route6(m0, r, dir, ifp, s);