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.11 2006/09/05 00:55:47 dillon 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;
701 pf_purge_expired_states();
702 pf_purge_expired_fragments();
703 pf_purge_expired_src_nodes();
706 callout_reset(to, pf_default_rule.timeout[PFTM_INTERVAL] * hz,
707 pf_purge_timeout, to);
711 pf_state_expires(const struct pf_state *state)
718 /* handle all PFTM_* > PFTM_MAX here */
719 if (state->timeout == PFTM_PURGE)
720 return (time_second);
721 if (state->timeout == PFTM_UNTIL_PACKET)
723 KASSERT((state->timeout < PFTM_MAX),
724 ("pf_state_expires: timeout > PFTM_MAX"));
725 timeout = state->rule.ptr->timeout[state->timeout];
727 timeout = pf_default_rule.timeout[state->timeout];
728 start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
730 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
731 states = state->rule.ptr->states;
733 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
734 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
735 states = pf_status.states;
737 if (end && states > start && start < end) {
739 return (state->expire + timeout * (end - states) /
742 return (time_second);
744 return (state->expire + timeout);
748 pf_purge_expired_src_nodes(void)
750 struct pf_src_node *cur, *next;
752 for (cur = RB_MIN(pf_src_tree, &tree_src_tracking); cur; cur = next) {
753 next = RB_NEXT(pf_src_tree, &tree_src_tracking, cur);
755 if (cur->states <= 0 && cur->expire <= time_second) {
756 if (cur->rule.ptr != NULL) {
757 cur->rule.ptr->src_nodes--;
758 if (cur->rule.ptr->states <= 0 &&
759 cur->rule.ptr->max_src_nodes <= 0)
760 pf_rm_rule(NULL, cur->rule.ptr);
762 RB_REMOVE(pf_src_tree, &tree_src_tracking, cur);
763 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
764 pf_status.src_nodes--;
765 pool_put(&pf_src_tree_pl, cur);
771 pf_src_tree_remove_state(struct pf_state *s)
775 if (s->src_node != NULL) {
776 if (--s->src_node->states <= 0) {
777 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
780 pf_default_rule.timeout[PFTM_SRC_NODE];
781 s->src_node->expire = time_second + timeout;
784 if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
785 if (--s->nat_src_node->states <= 0) {
786 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
789 pf_default_rule.timeout[PFTM_SRC_NODE];
790 s->nat_src_node->expire = time_second + timeout;
793 s->src_node = s->nat_src_node = NULL;
797 pf_purge_expired_states_callback(struct pf_state *cur, void *data __unused)
799 if (pf_state_expires(cur) <= time_second) {
800 RB_REMOVE(pf_state_tree_ext_gwy,
801 &cur->u.s.kif->pfik_ext_gwy, cur);
802 RB_REMOVE(pf_state_tree_lan_ext,
803 &cur->u.s.kif->pfik_lan_ext, cur);
804 RB_REMOVE(pf_state_tree_id, &tree_id, cur);
805 if (cur->src.state == PF_TCPS_PROXY_DST) {
806 pf_send_tcp(cur->rule.ptr, cur->af,
807 &cur->ext.addr, &cur->lan.addr,
808 cur->ext.port, cur->lan.port,
809 cur->src.seqhi, cur->src.seqlo + 1, 0,
810 TH_RST|TH_ACK, 0, 0);
813 pfsync_delete_state(cur);
815 pf_src_tree_remove_state(cur);
816 if (--cur->rule.ptr->states <= 0 &&
817 cur->rule.ptr->src_nodes <= 0)
818 pf_rm_rule(NULL, cur->rule.ptr);
819 if (cur->nat_rule.ptr != NULL)
820 if (--cur->nat_rule.ptr->states <= 0 &&
821 cur->nat_rule.ptr->src_nodes <= 0)
822 pf_rm_rule(NULL, cur->nat_rule.ptr);
823 if (cur->anchor.ptr != NULL)
824 if (--cur->anchor.ptr->states <= 0)
825 pf_rm_rule(NULL, cur->anchor.ptr);
826 pf_normalize_tcp_cleanup(cur);
827 pfi_detach_state(cur->u.s.kif);
828 TAILQ_REMOVE(&state_updates, cur, u.s.entry_updates);
829 pool_put(&pf_state_pl, cur);
830 pf_status.fcounters[FCNT_STATE_REMOVALS]++;
837 pf_purge_expired_states(void)
839 RB_SCAN(pf_state_tree_id, &tree_id, NULL,
840 pf_purge_expired_states_callback, NULL);
845 pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
847 if (aw->type != PF_ADDR_TABLE)
849 if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
855 pf_tbladdr_remove(struct pf_addr_wrap *aw)
857 if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
859 pfr_detach_table(aw->p.tbl);
864 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
866 struct pfr_ktable *kt = aw->p.tbl;
868 if (aw->type != PF_ADDR_TABLE || kt == NULL)
870 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
873 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
878 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
883 u_int32_t a = ntohl(addr->addr32[0]);
884 printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
896 u_int8_t i, curstart = 255, curend = 0,
897 maxstart = 0, maxend = 0;
898 for (i = 0; i < 8; i++) {
899 if (!addr->addr16[i]) {
906 if ((curend - curstart) >
907 (maxend - maxstart)) {
915 for (i = 0; i < 8; i++) {
916 if (i >= maxstart && i <= maxend) {
925 b = ntohs(addr->addr16[i]);
942 pf_print_state(struct pf_state *s)
958 printf("%u ", s->proto);
961 pf_print_host(&s->lan.addr, s->lan.port, s->af);
963 pf_print_host(&s->gwy.addr, s->gwy.port, s->af);
965 pf_print_host(&s->ext.addr, s->ext.port, s->af);
966 printf(" [lo=%u high=%u win=%u modulator=%u", s->src.seqlo,
967 s->src.seqhi, s->src.max_win, s->src.seqdiff);
968 if (s->src.wscale && s->dst.wscale)
969 printf(" wscale=%u", s->src.wscale & PF_WSCALE_MASK);
971 printf(" [lo=%u high=%u win=%u modulator=%u", s->dst.seqlo,
972 s->dst.seqhi, s->dst.max_win, s->dst.seqdiff);
973 if (s->src.wscale && s->dst.wscale)
974 printf(" wscale=%u", s->dst.wscale & PF_WSCALE_MASK);
976 printf(" %u:%u", s->src.state, s->dst.state);
980 pf_print_flags(u_int8_t f)
1002 #define PF_SET_SKIP_STEPS(i) \
1004 while (head[i] != cur) { \
1005 head[i]->skip[i].ptr = cur; \
1006 head[i] = TAILQ_NEXT(head[i], entries); \
1011 pf_calc_skip_steps(struct pf_rulequeue *rules)
1013 struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1016 cur = TAILQ_FIRST(rules);
1018 for (i = 0; i < PF_SKIP_COUNT; ++i)
1020 while (cur != NULL) {
1022 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1023 PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1024 if (cur->direction != prev->direction)
1025 PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1026 if (cur->af != prev->af)
1027 PF_SET_SKIP_STEPS(PF_SKIP_AF);
1028 if (cur->proto != prev->proto)
1029 PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
1030 if (cur->src.not != prev->src.not ||
1031 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1032 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1033 if (cur->src.port[0] != prev->src.port[0] ||
1034 cur->src.port[1] != prev->src.port[1] ||
1035 cur->src.port_op != prev->src.port_op)
1036 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
1037 if (cur->dst.not != prev->dst.not ||
1038 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1039 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1040 if (cur->dst.port[0] != prev->dst.port[0] ||
1041 cur->dst.port[1] != prev->dst.port[1] ||
1042 cur->dst.port_op != prev->dst.port_op)
1043 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1046 cur = TAILQ_NEXT(cur, entries);
1048 for (i = 0; i < PF_SKIP_COUNT; ++i)
1049 PF_SET_SKIP_STEPS(i);
1053 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1055 if (aw1->type != aw2->type)
1057 switch (aw1->type) {
1058 case PF_ADDR_ADDRMASK:
1059 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1061 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1064 case PF_ADDR_DYNIFTL:
1065 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1066 case PF_ADDR_NOROUTE:
1069 return (aw1->p.tbl != aw2->p.tbl);
1071 printf("invalid address type: %d\n", aw1->type);
1077 pf_update_anchor_rules(void)
1079 struct pf_rule *rule;
1082 for (i = 0; i < PF_RULESET_MAX; ++i)
1083 TAILQ_FOREACH(rule, pf_main_ruleset.rules[i].active.ptr,
1085 if (rule->anchorname[0])
1086 rule->anchor = pf_find_anchor(rule->anchorname);
1088 rule->anchor = NULL;
1092 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1098 l = cksum + old - new;
1099 l = (l >> 16) + (l & 65535);
1107 pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1108 struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1113 PF_ACPY(&ao, a, af);
1121 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1122 ao.addr16[0], an->addr16[0], 0),
1123 ao.addr16[1], an->addr16[1], 0);
1125 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1126 ao.addr16[0], an->addr16[0], u),
1127 ao.addr16[1], an->addr16[1], u),
1133 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1134 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1135 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1136 ao.addr16[0], an->addr16[0], u),
1137 ao.addr16[1], an->addr16[1], u),
1138 ao.addr16[2], an->addr16[2], u),
1139 ao.addr16[3], an->addr16[3], u),
1140 ao.addr16[4], an->addr16[4], u),
1141 ao.addr16[5], an->addr16[5], u),
1142 ao.addr16[6], an->addr16[6], u),
1143 ao.addr16[7], an->addr16[7], u),
1151 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1153 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1157 memcpy(&ao, a, sizeof(ao));
1158 memcpy(a, &an, sizeof(u_int32_t));
1159 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1160 ao % 65536, an % 65536, u);
1165 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1169 PF_ACPY(&ao, a, AF_INET6);
1170 PF_ACPY(a, an, AF_INET6);
1172 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1173 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1174 pf_cksum_fixup(pf_cksum_fixup(*c,
1175 ao.addr16[0], an->addr16[0], u),
1176 ao.addr16[1], an->addr16[1], u),
1177 ao.addr16[2], an->addr16[2], u),
1178 ao.addr16[3], an->addr16[3], u),
1179 ao.addr16[4], an->addr16[4], u),
1180 ao.addr16[5], an->addr16[5], u),
1181 ao.addr16[6], an->addr16[6], u),
1182 ao.addr16[7], an->addr16[7], u);
1187 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1188 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1189 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1191 struct pf_addr oia, ooa;
1193 PF_ACPY(&oia, ia, af);
1194 PF_ACPY(&ooa, oa, af);
1196 /* Change inner protocol port, fix inner protocol checksum. */
1198 u_int16_t oip = *ip;
1205 *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1206 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1208 *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1210 /* Change inner ip address, fix inner ip and icmp checksums. */
1211 PF_ACPY(ia, na, af);
1215 u_int32_t oh2c = *h2c;
1217 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1218 oia.addr16[0], ia->addr16[0], 0),
1219 oia.addr16[1], ia->addr16[1], 0);
1220 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1221 oia.addr16[0], ia->addr16[0], 0),
1222 oia.addr16[1], ia->addr16[1], 0);
1223 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
1229 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1230 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1231 pf_cksum_fixup(pf_cksum_fixup(*ic,
1232 oia.addr16[0], ia->addr16[0], u),
1233 oia.addr16[1], ia->addr16[1], u),
1234 oia.addr16[2], ia->addr16[2], u),
1235 oia.addr16[3], ia->addr16[3], u),
1236 oia.addr16[4], ia->addr16[4], u),
1237 oia.addr16[5], ia->addr16[5], u),
1238 oia.addr16[6], ia->addr16[6], u),
1239 oia.addr16[7], ia->addr16[7], u);
1243 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1244 PF_ACPY(oa, na, af);
1248 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
1249 ooa.addr16[0], oa->addr16[0], 0),
1250 ooa.addr16[1], oa->addr16[1], 0);
1255 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1256 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1257 pf_cksum_fixup(pf_cksum_fixup(*ic,
1258 ooa.addr16[0], oa->addr16[0], u),
1259 ooa.addr16[1], oa->addr16[1], u),
1260 ooa.addr16[2], oa->addr16[2], u),
1261 ooa.addr16[3], oa->addr16[3], u),
1262 ooa.addr16[4], oa->addr16[4], u),
1263 ooa.addr16[5], oa->addr16[5], u),
1264 ooa.addr16[6], oa->addr16[6], u),
1265 ooa.addr16[7], oa->addr16[7], u);
1272 pf_send_tcp(const struct pf_rule *r, sa_family_t af,
1273 const struct pf_addr *saddr, const struct pf_addr *daddr,
1274 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
1275 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl)
1280 struct ip *h = NULL;
1283 struct ip6_hdr *h6 = NULL;
1285 struct tcphdr *th = NULL;
1288 /* maximum segment size tcp option */
1289 tlen = sizeof(struct tcphdr);
1296 len = sizeof(struct ip) + tlen;
1301 len = sizeof(struct ip6_hdr) + tlen;
1306 /* create outgoing mbuf */
1307 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1310 m->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
1312 if (r != NULL && r->qid) {
1313 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1314 m->m_pkthdr.altq_qid = r->qid;
1315 m->m_pkthdr.ecn_af = af;
1316 m->m_pkthdr.header = mtod(m, struct ip *);
1319 m->m_data += max_linkhdr;
1320 m->m_pkthdr.len = m->m_len = len;
1321 m->m_pkthdr.rcvif = NULL;
1322 bzero(m->m_data, len);
1326 h = mtod(m, struct ip *);
1328 /* IP header fields included in the TCP checksum */
1329 h->ip_p = IPPROTO_TCP;
1331 h->ip_src.s_addr = saddr->v4.s_addr;
1332 h->ip_dst.s_addr = daddr->v4.s_addr;
1334 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
1339 h6 = mtod(m, struct ip6_hdr *);
1341 /* IP header fields included in the TCP checksum */
1342 h6->ip6_nxt = IPPROTO_TCP;
1343 h6->ip6_plen = htons(tlen);
1344 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
1345 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
1347 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
1353 th->th_sport = sport;
1354 th->th_dport = dport;
1355 th->th_seq = htonl(seq);
1356 th->th_ack = htonl(ack);
1357 th->th_off = tlen >> 2;
1358 th->th_flags = flags;
1359 th->th_win = htons(win);
1362 opt = (char *)(th + 1);
1363 opt[0] = TCPOPT_MAXSEG;
1366 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
1373 th->th_sum = in_cksum(m, len);
1375 /* Finish the IP header */
1377 h->ip_hl = sizeof(*h) >> 2;
1378 h->ip_tos = IPTOS_LOWDELAY;
1380 h->ip_off = path_mtu_discovery ? IP_DF : 0;
1381 h->ip_ttl = ttl ? ttl : ip_defttl;
1383 ip_output(m, (void *)NULL, (void *)NULL, 0, (void *)NULL,
1390 th->th_sum = in6_cksum(m, IPPROTO_TCP,
1391 sizeof(struct ip6_hdr), tlen);
1393 h6->ip6_vfc |= IPV6_VERSION;
1394 h6->ip6_hlim = IPV6_DEFHLIM;
1396 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1403 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
1408 m0 = m_copypacket(m, MB_DONTWAIT);
1411 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
1415 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1416 m->m_pkthdr.altq_qid = r->qid;
1417 m->m_pkthdr.ecn_af = af;
1418 m->m_pkthdr.header = mtod(m0, struct ip *);
1425 icmp_error(m0, type, code, 0, 0);
1430 icmp6_error(m0, type, code, 0);
1437 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
1438 * If n is 0, they match if they are equal. If n is != 0, they match if they
1442 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
1443 struct pf_addr *b, sa_family_t af)
1450 if ((a->addr32[0] & m->addr32[0]) ==
1451 (b->addr32[0] & m->addr32[0]))
1457 if (((a->addr32[0] & m->addr32[0]) ==
1458 (b->addr32[0] & m->addr32[0])) &&
1459 ((a->addr32[1] & m->addr32[1]) ==
1460 (b->addr32[1] & m->addr32[1])) &&
1461 ((a->addr32[2] & m->addr32[2]) ==
1462 (b->addr32[2] & m->addr32[2])) &&
1463 ((a->addr32[3] & m->addr32[3]) ==
1464 (b->addr32[3] & m->addr32[3])))
1483 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
1487 return ((p > a1) && (p < a2));
1489 return ((p < a1) || (p > a2));
1491 return ((p >= a1) && (p <= a2));
1505 return (0); /* never reached */
1509 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
1514 return (pf_match(op, a1, a2, p));
1518 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
1520 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1522 return (pf_match(op, a1, a2, u));
1526 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
1528 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1530 return (pf_match(op, a1, a2, g));
1534 pf_match_tag(struct mbuf *m, struct pf_rule *r, struct pf_rule *nat_rule,
1537 if (*tag == -1) { /* find mbuf tag */
1538 if (nat_rule != NULL && nat_rule->tag)
1539 *tag = nat_rule->tag;
1540 else if (m->m_pkthdr.fw_flags & PF_MBUF_TAGGED)
1541 *tag = m->m_pkthdr.pf_tag;
1546 return ((!r->match_tag_not && r->match_tag == *tag) ||
1547 (r->match_tag_not && r->match_tag != *tag));
1551 pf_tag_packet(struct mbuf *m, int tag)
1556 m->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
1557 m->m_pkthdr.pf_tag = tag;
1560 #define PF_STEP_INTO_ANCHOR(r, a, s, n) \
1562 if ((r) == NULL || (r)->anchor == NULL || \
1563 (s) != NULL || (a) != NULL) \
1564 panic("PF_STEP_INTO_ANCHOR"); \
1566 (s) = TAILQ_FIRST(&(r)->anchor->rulesets); \
1568 while ((s) != NULL && ((r) = \
1569 TAILQ_FIRST((s)->rules[n].active.ptr)) == NULL) \
1570 (s) = TAILQ_NEXT((s), entries); \
1571 if ((r) == NULL) { \
1572 (r) = TAILQ_NEXT((a), entries); \
1577 #define PF_STEP_OUT_OF_ANCHOR(r, a, s, n) \
1579 if ((r) != NULL || (a) == NULL || (s) == NULL) \
1580 panic("PF_STEP_OUT_OF_ANCHOR"); \
1581 (s) = TAILQ_NEXT((s), entries); \
1582 while ((s) != NULL && ((r) = \
1583 TAILQ_FIRST((s)->rules[n].active.ptr)) == NULL) \
1584 (s) = TAILQ_NEXT((s), entries); \
1585 if ((r) == NULL) { \
1586 (r) = TAILQ_NEXT((a), entries); \
1593 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
1594 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
1599 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1600 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1604 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1605 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1606 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
1607 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
1608 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
1609 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
1610 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
1611 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
1617 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
1622 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
1626 if (addr->addr32[3] == 0xffffffff) {
1627 addr->addr32[3] = 0;
1628 if (addr->addr32[2] == 0xffffffff) {
1629 addr->addr32[2] = 0;
1630 if (addr->addr32[1] == 0xffffffff) {
1631 addr->addr32[1] = 0;
1633 htonl(ntohl(addr->addr32[0]) + 1);
1636 htonl(ntohl(addr->addr32[1]) + 1);
1639 htonl(ntohl(addr->addr32[2]) + 1);
1642 htonl(ntohl(addr->addr32[3]) + 1);
1648 #define mix(a,b,c) \
1650 a -= b; a -= c; a ^= (c >> 13); \
1651 b -= c; b -= a; b ^= (a << 8); \
1652 c -= a; c -= b; c ^= (b >> 13); \
1653 a -= b; a -= c; a ^= (c >> 12); \
1654 b -= c; b -= a; b ^= (a << 16); \
1655 c -= a; c -= b; c ^= (b >> 5); \
1656 a -= b; a -= c; a ^= (c >> 3); \
1657 b -= c; b -= a; b ^= (a << 10); \
1658 c -= a; c -= b; c ^= (b >> 15); \
1662 * hash function based on bridge_hash in if_bridge.c
1665 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
1666 struct pf_poolhashkey *key, sa_family_t af)
1668 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
1673 a += inaddr->addr32[0];
1676 hash->addr32[0] = c + key->key32[2];
1681 a += inaddr->addr32[0];
1682 b += inaddr->addr32[2];
1684 hash->addr32[0] = c;
1685 a += inaddr->addr32[1];
1686 b += inaddr->addr32[3];
1689 hash->addr32[1] = c;
1690 a += inaddr->addr32[2];
1691 b += inaddr->addr32[1];
1694 hash->addr32[2] = c;
1695 a += inaddr->addr32[3];
1696 b += inaddr->addr32[0];
1699 hash->addr32[3] = c;
1706 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
1707 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
1709 unsigned char hash[16];
1710 struct pf_pool *rpool = &r->rpool;
1711 struct pf_addr *raddr = &rpool->cur->addr.v.a.addr;
1712 struct pf_addr *rmask = &rpool->cur->addr.v.a.mask;
1713 struct pf_pooladdr *acur = rpool->cur;
1714 struct pf_src_node k;
1716 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
1717 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1719 PF_ACPY(&k.addr, saddr, af);
1720 if (r->rule_flag & PFRULE_RULESRCTRACK ||
1721 r->rpool.opts & PF_POOL_STICKYADDR)
1725 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
1726 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
1727 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
1728 PF_ACPY(naddr, &(*sn)->raddr, af);
1729 if (pf_status.debug >= PF_DEBUG_MISC) {
1730 printf("pf_map_addr: src tracking maps ");
1731 pf_print_host(&k.addr, 0, af);
1733 pf_print_host(naddr, 0, af);
1740 if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
1742 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1743 if (af == AF_INET) {
1744 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
1745 (rpool->opts & PF_POOL_TYPEMASK) !=
1748 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
1749 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
1751 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
1752 (rpool->opts & PF_POOL_TYPEMASK) !=
1755 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
1756 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
1758 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1759 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
1760 return (1); /* unsupported */
1762 raddr = &rpool->cur->addr.v.a.addr;
1763 rmask = &rpool->cur->addr.v.a.mask;
1766 switch (rpool->opts & PF_POOL_TYPEMASK) {
1768 PF_ACPY(naddr, raddr, af);
1770 case PF_POOL_BITMASK:
1771 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
1773 case PF_POOL_RANDOM:
1774 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
1778 rpool->counter.addr32[0] = karc4random();
1783 if (rmask->addr32[3] != 0xffffffff)
1784 rpool->counter.addr32[3] = karc4random();
1787 if (rmask->addr32[2] != 0xffffffff)
1788 rpool->counter.addr32[2] = karc4random();
1791 if (rmask->addr32[1] != 0xffffffff)
1792 rpool->counter.addr32[1] = karc4random();
1795 if (rmask->addr32[0] != 0xffffffff)
1796 rpool->counter.addr32[0] = karc4random();
1800 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1801 PF_ACPY(init_addr, naddr, af);
1804 PF_AINC(&rpool->counter, af);
1805 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1808 case PF_POOL_SRCHASH:
1809 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
1810 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
1812 case PF_POOL_ROUNDROBIN:
1813 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1814 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
1815 &rpool->tblidx, &rpool->counter,
1816 &raddr, &rmask, af))
1818 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1819 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1820 &rpool->tblidx, &rpool->counter,
1821 &raddr, &rmask, af))
1823 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
1827 if ((rpool->cur = TAILQ_NEXT(rpool->cur, entries)) == NULL)
1828 rpool->cur = TAILQ_FIRST(&rpool->list);
1829 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1831 if (pfr_pool_get(rpool->cur->addr.p.tbl,
1832 &rpool->tblidx, &rpool->counter,
1833 &raddr, &rmask, af)) {
1834 /* table contains no address of type 'af' */
1835 if (rpool->cur != acur)
1839 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1841 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1842 &rpool->tblidx, &rpool->counter,
1843 &raddr, &rmask, af)) {
1844 /* table contains no address of type 'af' */
1845 if (rpool->cur != acur)
1850 raddr = &rpool->cur->addr.v.a.addr;
1851 rmask = &rpool->cur->addr.v.a.mask;
1852 PF_ACPY(&rpool->counter, raddr, af);
1856 PF_ACPY(naddr, &rpool->counter, af);
1857 PF_AINC(&rpool->counter, af);
1861 PF_ACPY(&(*sn)->raddr, naddr, af);
1863 if (pf_status.debug >= PF_DEBUG_MISC &&
1864 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1865 printf("pf_map_addr: selected address ");
1866 pf_print_host(naddr, 0, af);
1874 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
1875 struct pf_addr *saddr, struct pf_addr *daddr, u_int16_t dport,
1876 struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
1877 struct pf_src_node **sn)
1879 struct pf_state key;
1880 struct pf_addr init_addr;
1883 bzero(&init_addr, sizeof(init_addr));
1884 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1890 PF_ACPY(&key.ext.addr, daddr, key.af);
1891 PF_ACPY(&key.gwy.addr, naddr, key.af);
1892 key.ext.port = dport;
1895 * port search; start random, step;
1896 * similar 2 portloop in in_pcbbind
1898 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP)) {
1900 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1902 } else if (low == 0 && high == 0) {
1903 key.gwy.port = *nport;
1904 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1906 } else if (low == high) {
1907 key.gwy.port = htons(low);
1908 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL) {
1909 *nport = htons(low);
1921 cut = karc4random() % (1 + high - low) + low;
1922 /* low <= cut <= high */
1923 for (tmp = cut; tmp <= high; ++(tmp)) {
1924 key.gwy.port = htons(tmp);
1925 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1927 *nport = htons(tmp);
1931 for (tmp = cut - 1; tmp >= low; --(tmp)) {
1932 key.gwy.port = htons(tmp);
1933 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1935 *nport = htons(tmp);
1941 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
1942 case PF_POOL_RANDOM:
1943 case PF_POOL_ROUNDROBIN:
1944 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1948 case PF_POOL_SRCHASH:
1949 case PF_POOL_BITMASK:
1953 } while (! PF_AEQ(&init_addr, naddr, af) );
1955 return (1); /* none available */
1959 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
1960 int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
1961 struct pf_addr *daddr, u_int16_t dport, int rs_num)
1963 struct pf_rule *r, *rm = NULL, *anchorrule = NULL;
1964 struct pf_ruleset *ruleset = NULL;
1966 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1967 while (r && rm == NULL) {
1968 struct pf_rule_addr *src = NULL, *dst = NULL;
1969 struct pf_addr_wrap *xdst = NULL;
1971 if (r->action == PF_BINAT && direction == PF_IN) {
1973 if (r->rpool.cur != NULL)
1974 xdst = &r->rpool.cur->addr;
1981 if (r->kif != NULL &&
1982 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1983 r = r->skip[PF_SKIP_IFP].ptr;
1984 else if (r->direction && r->direction != direction)
1985 r = r->skip[PF_SKIP_DIR].ptr;
1986 else if (r->af && r->af != pd->af)
1987 r = r->skip[PF_SKIP_AF].ptr;
1988 else if (r->proto && r->proto != pd->proto)
1989 r = r->skip[PF_SKIP_PROTO].ptr;
1990 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af, src->not))
1991 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
1992 PF_SKIP_DST_ADDR].ptr;
1993 else if (src->port_op && !pf_match_port(src->port_op,
1994 src->port[0], src->port[1], sport))
1995 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
1996 PF_SKIP_DST_PORT].ptr;
1997 else if (dst != NULL &&
1998 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->not))
1999 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2000 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af, 0))
2001 r = TAILQ_NEXT(r, entries);
2002 else if (dst != NULL && dst->port_op &&
2003 !pf_match_port(dst->port_op, dst->port[0],
2004 dst->port[1], dport))
2005 r = r->skip[PF_SKIP_DST_PORT].ptr;
2006 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
2007 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
2008 off, pd->hdr.tcp), r->os_fingerprint)))
2009 r = TAILQ_NEXT(r, entries);
2010 else if (r->anchorname[0] && r->anchor == NULL)
2011 r = TAILQ_NEXT(r, entries);
2012 else if (r->anchor == NULL)
2015 PF_STEP_INTO_ANCHOR(r, anchorrule, ruleset, rs_num);
2016 if (r == NULL && anchorrule != NULL)
2017 PF_STEP_OUT_OF_ANCHOR(r, anchorrule, ruleset,
2020 if (rm != NULL && (rm->action == PF_NONAT ||
2021 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2027 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
2028 struct pfi_kif *kif, struct pf_src_node **sn,
2029 struct pf_addr *saddr, u_int16_t sport,
2030 struct pf_addr *daddr, u_int16_t dport,
2031 struct pf_addr *naddr, u_int16_t *nport)
2033 struct pf_rule *r = NULL;
2035 if (direction == PF_OUT) {
2036 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2037 sport, daddr, dport, PF_RULESET_BINAT);
2039 r = pf_match_translation(pd, m, off, direction, kif,
2040 saddr, sport, daddr, dport, PF_RULESET_NAT);
2042 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2043 sport, daddr, dport, PF_RULESET_RDR);
2045 r = pf_match_translation(pd, m, off, direction, kif,
2046 saddr, sport, daddr, dport, PF_RULESET_BINAT);
2050 switch (r->action) {
2056 if (pf_get_sport(pd->af, pd->proto, r, saddr,
2057 daddr, dport, naddr, nport, r->rpool.proxy_port[0],
2058 r->rpool.proxy_port[1], sn)) {
2059 DPFPRINTF(PF_DEBUG_MISC,
2060 ("pf: NAT proxy port allocation "
2062 r->rpool.proxy_port[0],
2063 r->rpool.proxy_port[1]));
2068 switch (direction) {
2070 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
2071 if (pd->af == AF_INET) {
2072 if (r->rpool.cur->addr.p.dyn->
2076 &r->rpool.cur->addr.p.dyn->
2078 &r->rpool.cur->addr.p.dyn->
2082 if (r->rpool.cur->addr.p.dyn->
2086 &r->rpool.cur->addr.p.dyn->
2088 &r->rpool.cur->addr.p.dyn->
2094 &r->rpool.cur->addr.v.a.addr,
2095 &r->rpool.cur->addr.v.a.mask,
2099 if (r->src.addr.type == PF_ADDR_DYNIFTL){
2100 if (pd->af == AF_INET) {
2101 if (r->src.addr.p.dyn->
2105 &r->src.addr.p.dyn->
2107 &r->src.addr.p.dyn->
2111 if (r->src.addr.p.dyn->
2115 &r->src.addr.p.dyn->
2117 &r->src.addr.p.dyn->
2123 &r->src.addr.v.a.addr,
2124 &r->src.addr.v.a.mask, daddr,
2130 if (pf_map_addr(r->af, r, saddr, naddr, NULL, sn))
2133 if (r->rpool.proxy_port[1]) {
2134 u_int32_t tmp_nport;
2136 tmp_nport = ((ntohs(dport) -
2137 ntohs(r->dst.port[0])) %
2138 (r->rpool.proxy_port[1] -
2139 r->rpool.proxy_port[0] + 1)) +
2140 r->rpool.proxy_port[0];
2142 /* wrap around if necessary */
2143 if (tmp_nport > 65535)
2145 *nport = htons((u_int16_t)tmp_nport);
2146 } else if (r->rpool.proxy_port[0])
2147 *nport = htons(r->rpool.proxy_port[0]);
2159 struct netmsg_hashlookup {
2160 struct lwkt_msg nm_lmsg;
2161 struct inpcb **nm_pinp;
2162 struct inpcbinfo *nm_pcbinfo;
2163 struct pf_addr *nm_saddr;
2164 struct pf_addr *nm_daddr;
2171 in_pcblookup_hash_handler(struct lwkt_msg *msg0)
2173 struct netmsg_hashlookup *msg = (struct netmsg_hashlookup *)msg0;
2175 if (msg->nm_af == AF_INET)
2176 *msg->nm_pinp = in_pcblookup_hash(msg->nm_pcbinfo,
2177 msg->nm_saddr->v4, msg->nm_sport, msg->nm_daddr->v4,
2178 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2181 *msg->nm_pinp = in6_pcblookup_hash(msg->nm_pcbinfo,
2182 &msg->nm_saddr->v6, msg->nm_sport, &msg->nm_daddr->v6,
2183 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2185 lwkt_replymsg(&msg->nm_lmsg, 0);
2191 pf_socket_lookup(uid_t *uid, gid_t *gid, int direction, struct pf_pdesc *pd)
2193 struct pf_addr *saddr, *daddr;
2194 u_int16_t sport, dport;
2195 struct inpcbinfo *pi;
2198 struct netmsg_hashlookup *msg = NULL;
2204 if (direction == PF_IN) {
2211 switch (pd->proto) {
2213 sport = pd->hdr.tcp->th_sport;
2214 dport = pd->hdr.tcp->th_dport;
2216 pi_cpu = tcp_addrcpu(saddr->v4.s_addr, sport, daddr->v4.s_addr, dport);
2217 pi = &tcbinfo[pi_cpu];
2220 * Our netstack runs lockless on MP systems
2221 * (only for TCP connections at the moment).
2223 * As we are not allowed to read another CPU's tcbinfo,
2224 * we have to ask that CPU via remote call to search the
2227 * Prepare a msg iff data belongs to another CPU.
2229 if (pi_cpu != mycpu->gd_cpuid) {
2230 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
2231 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
2232 lwkt_cmd_func(in_pcblookup_hash_handler),
2234 msg->nm_pinp = &inp;
2235 msg->nm_pcbinfo = pi;
2236 msg->nm_saddr = saddr;
2237 msg->nm_sport = sport;
2238 msg->nm_daddr = daddr;
2239 msg->nm_dport = dport;
2240 msg->nm_af = pd->af;
2245 sport = pd->hdr.udp->uh_sport;
2246 dport = pd->hdr.udp->uh_dport;
2252 if (direction != PF_IN) {
2264 * Query other CPU, second part
2266 * msg only gets initialized when:
2268 * 2) the info belongs to another CPU
2270 * Use some switch/case magic to avoid code duplication.
2275 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
2276 &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
2282 /* FALLTHROUGH if SMP and on other CPU */
2287 lwkt_sendmsg(tcp_cport(pi_cpu), &msg->nm_lmsg);
2291 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
2292 dport, INPLOOKUP_WILDCARD, NULL);
2301 *uid = inp->inp_socket->so_cred->cr_uid;
2302 *gid = inp->inp_socket->so_cred->cr_groups[0];
2307 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2311 u_int8_t *opt, optlen;
2312 u_int8_t wscale = 0;
2314 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2315 if (hlen <= sizeof(struct tcphdr))
2317 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2319 opt = hdr + sizeof(struct tcphdr);
2320 hlen -= sizeof(struct tcphdr);
2330 if (wscale > TCP_MAX_WINSHIFT)
2331 wscale = TCP_MAX_WINSHIFT;
2332 wscale |= PF_WSCALE_FLAG;
2347 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2351 u_int8_t *opt, optlen;
2352 u_int16_t mss = tcp_mssdflt;
2354 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2355 if (hlen <= sizeof(struct tcphdr))
2357 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2359 opt = hdr + sizeof(struct tcphdr);
2360 hlen -= sizeof(struct tcphdr);
2361 while (hlen >= TCPOLEN_MAXSEG) {
2369 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
2384 pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
2387 struct sockaddr_in *dst;
2391 struct sockaddr_in6 *dst6;
2392 struct route_in6 ro6;
2394 struct rtentry *rt = NULL;
2396 u_int16_t mss = tcp_mssdflt;
2401 hlen = sizeof(struct ip);
2402 bzero(&ro, sizeof(ro));
2403 dst = (struct sockaddr_in *)&ro.ro_dst;
2404 dst->sin_family = AF_INET;
2405 dst->sin_len = sizeof(*dst);
2406 dst->sin_addr = addr->v4;
2407 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
2413 hlen = sizeof(struct ip6_hdr);
2414 bzero(&ro6, sizeof(ro6));
2415 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
2416 dst6->sin6_family = AF_INET6;
2417 dst6->sin6_len = sizeof(*dst6);
2418 dst6->sin6_addr = addr->v6;
2419 rtalloc_ign((struct route *)&ro6, (RTF_CLONING | RTF_PRCLONING));
2425 if (rt && rt->rt_ifp) {
2426 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
2427 mss = max(tcp_mssdflt, mss);
2430 mss = min(mss, offer);
2431 mss = max(mss, 64); /* sanity - at least max opt space */
2436 pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
2438 struct pf_rule *r = s->rule.ptr;
2441 if (!r->rt || r->rt == PF_FASTROUTE)
2446 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
2448 s->rt_kif = r->rpool.cur->kif;
2453 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
2455 s->rt_kif = r->rpool.cur->kif;
2462 pf_test_tcp(struct pf_rule **rm, struct pf_state **sm, int direction,
2463 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2464 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2466 struct pf_rule *nr = NULL;
2467 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2468 struct tcphdr *th = pd->hdr.tcp;
2469 u_int16_t bport, nport = 0;
2470 sa_family_t af = pd->af;
2474 struct pf_rule *r, *a = NULL;
2475 struct pf_ruleset *ruleset = NULL;
2476 struct pf_src_node *nsn = NULL;
2480 u_int16_t mss = tcp_mssdflt;
2482 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2484 if (direction == PF_OUT) {
2485 bport = nport = th->th_sport;
2486 /* check outgoing packet for BINAT/NAT */
2487 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2488 saddr, th->th_sport, daddr, th->th_dport,
2489 &pd->naddr, &nport)) != NULL) {
2490 PF_ACPY(&pd->baddr, saddr, af);
2491 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2492 &th->th_sum, &pd->naddr, nport, 0, af);
2499 bport = nport = th->th_dport;
2500 /* check incoming packet for BINAT/RDR */
2501 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2502 saddr, th->th_sport, daddr, th->th_dport,
2503 &pd->naddr, &nport)) != NULL) {
2504 PF_ACPY(&pd->baddr, daddr, af);
2505 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2506 &th->th_sum, &pd->naddr, nport, 0, af);
2516 if (r->kif != NULL &&
2517 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2518 r = r->skip[PF_SKIP_IFP].ptr;
2519 else if (r->direction && r->direction != direction)
2520 r = r->skip[PF_SKIP_DIR].ptr;
2521 else if (r->af && r->af != af)
2522 r = r->skip[PF_SKIP_AF].ptr;
2523 else if (r->proto && r->proto != IPPROTO_TCP)
2524 r = r->skip[PF_SKIP_PROTO].ptr;
2525 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2526 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2527 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2528 r->src.port[0], r->src.port[1], th->th_sport))
2529 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2530 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2531 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2532 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2533 r->dst.port[0], r->dst.port[1], th->th_dport))
2534 r = r->skip[PF_SKIP_DST_PORT].ptr;
2535 else if (r->tos && !(r->tos & pd->tos))
2536 r = TAILQ_NEXT(r, entries);
2537 else if (r->rule_flag & PFRULE_FRAGMENT)
2538 r = TAILQ_NEXT(r, entries);
2539 else if ((r->flagset & th->th_flags) != r->flags)
2540 r = TAILQ_NEXT(r, entries);
2541 else if (r->uid.op && (lookup != -1 || (lookup =
2542 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2543 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2545 r = TAILQ_NEXT(r, entries);
2546 else if (r->gid.op && (lookup != -1 || (lookup =
2547 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2548 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2550 r = TAILQ_NEXT(r, entries);
2551 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2552 r = TAILQ_NEXT(r, entries);
2553 else if (r->anchorname[0] && r->anchor == NULL)
2554 r = TAILQ_NEXT(r, entries);
2555 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
2556 pf_osfp_fingerprint(pd, m, off, th), r->os_fingerprint))
2557 r = TAILQ_NEXT(r, entries);
2561 if (r->anchor == NULL) {
2567 r = TAILQ_NEXT(r, entries);
2569 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2572 if (r == NULL && a != NULL)
2573 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2580 REASON_SET(&reason, PFRES_MATCH);
2584 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2585 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2588 if ((r->action == PF_DROP) &&
2589 ((r->rule_flag & PFRULE_RETURNRST) ||
2590 (r->rule_flag & PFRULE_RETURNICMP) ||
2591 (r->rule_flag & PFRULE_RETURN))) {
2592 /* undo NAT changes, if they have taken place */
2594 if (direction == PF_OUT) {
2595 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2596 &th->th_sum, &pd->baddr, bport, 0, af);
2599 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2600 &th->th_sum, &pd->baddr, bport, 0, af);
2604 if (((r->rule_flag & PFRULE_RETURNRST) ||
2605 (r->rule_flag & PFRULE_RETURN)) &&
2606 !(th->th_flags & TH_RST)) {
2607 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
2609 if (th->th_flags & TH_SYN)
2611 if (th->th_flags & TH_FIN)
2613 pf_send_tcp(r, af, pd->dst,
2614 pd->src, th->th_dport, th->th_sport,
2615 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
2617 } else if ((af == AF_INET) && r->return_icmp)
2618 pf_send_icmp(m, r->return_icmp >> 8,
2619 r->return_icmp & 255, af, r);
2620 else if ((af == AF_INET6) && r->return_icmp6)
2621 pf_send_icmp(m, r->return_icmp6 >> 8,
2622 r->return_icmp6 & 255, af, r);
2625 if (r->action == PF_DROP)
2628 pf_tag_packet(m, tag);
2630 if (r->keep_state || nr != NULL ||
2631 (pd->flags & PFDESC_TCP_NORM)) {
2632 /* create new state */
2634 struct pf_state *s = NULL;
2635 struct pf_src_node *sn = NULL;
2637 len = pd->tot_len - off - (th->th_off << 2);
2639 /* check maximums */
2640 if (r->max_states && (r->states >= r->max_states))
2642 /* src node for flter rule */
2643 if ((r->rule_flag & PFRULE_SRCTRACK ||
2644 r->rpool.opts & PF_POOL_STICKYADDR) &&
2645 pf_insert_src_node(&sn, r, saddr, af) != 0)
2647 /* src node for translation rule */
2648 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2649 ((direction == PF_OUT &&
2650 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2651 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2653 s = pool_get(&pf_state_pl, PR_NOWAIT);
2656 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
2657 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
2658 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2659 pf_status.src_nodes--;
2660 pool_put(&pf_src_tree_pl, sn);
2662 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
2664 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
2665 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2666 pf_status.src_nodes--;
2667 pool_put(&pf_src_tree_pl, nsn);
2669 REASON_SET(&reason, PFRES_MEMORY);
2672 bzero(s, sizeof(*s));
2677 s->nat_rule.ptr = nr;
2678 if (s->nat_rule.ptr != NULL)
2679 s->nat_rule.ptr->states++;
2681 s->allow_opts = r->allow_opts;
2682 s->log = r->log & 2;
2683 s->proto = IPPROTO_TCP;
2684 s->direction = direction;
2686 if (direction == PF_OUT) {
2687 PF_ACPY(&s->gwy.addr, saddr, af);
2688 s->gwy.port = th->th_sport; /* sport */
2689 PF_ACPY(&s->ext.addr, daddr, af);
2690 s->ext.port = th->th_dport;
2692 PF_ACPY(&s->lan.addr, &pd->baddr, af);
2693 s->lan.port = bport;
2695 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
2696 s->lan.port = s->gwy.port;
2699 PF_ACPY(&s->lan.addr, daddr, af);
2700 s->lan.port = th->th_dport;
2701 PF_ACPY(&s->ext.addr, saddr, af);
2702 s->ext.port = th->th_sport;
2704 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
2705 s->gwy.port = bport;
2707 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
2708 s->gwy.port = s->lan.port;
2712 s->src.seqlo = ntohl(th->th_seq);
2713 s->src.seqhi = s->src.seqlo + len + 1;
2714 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2715 r->keep_state == PF_STATE_MODULATE) {
2716 /* Generate sequence number modulator */
2717 while ((s->src.seqdiff = karc4random()) == 0)
2719 pf_change_a(&th->th_seq, &th->th_sum,
2720 htonl(s->src.seqlo + s->src.seqdiff), 0);
2724 if (th->th_flags & TH_SYN) {
2726 s->src.wscale = pf_get_wscale(m, off, th->th_off, af);
2728 s->src.max_win = MAX(ntohs(th->th_win), 1);
2729 if (s->src.wscale & PF_WSCALE_MASK) {
2730 /* Remove scale factor from initial window */
2731 int win = s->src.max_win;
2732 win += 1 << (s->src.wscale & PF_WSCALE_MASK);
2733 s->src.max_win = (win - 1) >>
2734 (s->src.wscale & PF_WSCALE_MASK);
2736 if (th->th_flags & TH_FIN)
2740 s->src.state = TCPS_SYN_SENT;
2741 s->dst.state = TCPS_CLOSED;
2742 s->creation = time_second;
2743 s->expire = time_second;
2744 s->timeout = PFTM_TCP_FIRST_PACKET;
2745 pf_set_rt_ifp(s, saddr);
2748 s->src_node->states++;
2751 PF_ACPY(&nsn->raddr, &pd->naddr, af);
2752 s->nat_src_node = nsn;
2753 s->nat_src_node->states++;
2755 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
2756 off, pd, th, &s->src, &s->dst)) {
2757 REASON_SET(&reason, PFRES_MEMORY);
2758 pf_src_tree_remove_state(s);
2759 pool_put(&pf_state_pl, s);
2762 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
2763 pf_normalize_tcp_stateful(m, off, pd, &reason, th, &s->src,
2764 &s->dst, &rewrite)) {
2765 pf_normalize_tcp_cleanup(s);
2766 pf_src_tree_remove_state(s);
2767 pool_put(&pf_state_pl, s);
2770 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
2771 pf_normalize_tcp_cleanup(s);
2772 REASON_SET(&reason, PFRES_MEMORY);
2773 pf_src_tree_remove_state(s);
2774 pool_put(&pf_state_pl, s);
2778 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2779 r->keep_state == PF_STATE_SYNPROXY) {
2780 s->src.state = PF_TCPS_PROXY_SRC;
2782 if (direction == PF_OUT) {
2783 pf_change_ap(saddr, &th->th_sport,
2784 pd->ip_sum, &th->th_sum, &pd->baddr,
2787 pf_change_ap(daddr, &th->th_dport,
2788 pd->ip_sum, &th->th_sum, &pd->baddr,
2792 s->src.seqhi = karc4random();
2793 /* Find mss option */
2794 mss = pf_get_mss(m, off, th->th_off, af);
2795 mss = pf_calc_mss(saddr, af, mss);
2796 mss = pf_calc_mss(daddr, af, mss);
2798 pf_send_tcp(r, af, daddr, saddr, th->th_dport,
2799 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
2800 TH_SYN|TH_ACK, 0, s->src.mss, 0);
2801 return (PF_SYNPROXY_DROP);
2805 /* copy back packet headers if we performed NAT operations */
2807 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2813 pf_test_udp(struct pf_rule **rm, struct pf_state **sm, int direction,
2814 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2815 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2817 struct pf_rule *nr = NULL;
2818 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2819 struct udphdr *uh = pd->hdr.udp;
2820 u_int16_t bport, nport = 0;
2821 sa_family_t af = pd->af;
2825 struct pf_rule *r, *a = NULL;
2826 struct pf_ruleset *ruleset = NULL;
2827 struct pf_src_node *nsn = NULL;
2832 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2834 if (direction == PF_OUT) {
2835 bport = nport = uh->uh_sport;
2836 /* check outgoing packet for BINAT/NAT */
2837 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2838 saddr, uh->uh_sport, daddr, uh->uh_dport,
2839 &pd->naddr, &nport)) != NULL) {
2840 PF_ACPY(&pd->baddr, saddr, af);
2841 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2842 &uh->uh_sum, &pd->naddr, nport, 1, af);
2849 bport = nport = uh->uh_dport;
2850 /* check incoming packet for BINAT/RDR */
2851 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2852 saddr, uh->uh_sport, daddr, uh->uh_dport, &pd->naddr,
2854 PF_ACPY(&pd->baddr, daddr, af);
2855 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2856 &uh->uh_sum, &pd->naddr, nport, 1, af);
2866 if (r->kif != NULL &&
2867 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2868 r = r->skip[PF_SKIP_IFP].ptr;
2869 else if (r->direction && r->direction != direction)
2870 r = r->skip[PF_SKIP_DIR].ptr;
2871 else if (r->af && r->af != af)
2872 r = r->skip[PF_SKIP_AF].ptr;
2873 else if (r->proto && r->proto != IPPROTO_UDP)
2874 r = r->skip[PF_SKIP_PROTO].ptr;
2875 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2876 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2877 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2878 r->src.port[0], r->src.port[1], uh->uh_sport))
2879 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2880 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2881 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2882 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2883 r->dst.port[0], r->dst.port[1], uh->uh_dport))
2884 r = r->skip[PF_SKIP_DST_PORT].ptr;
2885 else if (r->tos && !(r->tos & pd->tos))
2886 r = TAILQ_NEXT(r, entries);
2887 else if (r->rule_flag & PFRULE_FRAGMENT)
2888 r = TAILQ_NEXT(r, entries);
2889 else if (r->uid.op && (lookup != -1 || (lookup =
2890 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2891 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2893 r = TAILQ_NEXT(r, entries);
2894 else if (r->gid.op && (lookup != -1 || (lookup =
2895 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2896 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2898 r = TAILQ_NEXT(r, entries);
2899 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2900 r = TAILQ_NEXT(r, entries);
2901 else if (r->anchorname[0] && r->anchor == NULL)
2902 r = TAILQ_NEXT(r, entries);
2903 else if (r->os_fingerprint != PF_OSFP_ANY)
2904 r = TAILQ_NEXT(r, entries);
2908 if (r->anchor == NULL) {
2914 r = TAILQ_NEXT(r, entries);
2916 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2919 if (r == NULL && a != NULL)
2920 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2927 REASON_SET(&reason, PFRES_MATCH);
2931 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
2932 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2935 if ((r->action == PF_DROP) &&
2936 ((r->rule_flag & PFRULE_RETURNICMP) ||
2937 (r->rule_flag & PFRULE_RETURN))) {
2938 /* undo NAT changes, if they have taken place */
2940 if (direction == PF_OUT) {
2941 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2942 &uh->uh_sum, &pd->baddr, bport, 1, af);
2945 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2946 &uh->uh_sum, &pd->baddr, bport, 1, af);
2950 if ((af == AF_INET) && r->return_icmp)
2951 pf_send_icmp(m, r->return_icmp >> 8,
2952 r->return_icmp & 255, af, r);
2953 else if ((af == AF_INET6) && r->return_icmp6)
2954 pf_send_icmp(m, r->return_icmp6 >> 8,
2955 r->return_icmp6 & 255, af, r);
2958 if (r->action == PF_DROP)
2961 pf_tag_packet(m, tag);
2963 if (r->keep_state || nr != NULL) {
2964 /* create new state */
2965 struct pf_state *s = NULL;
2966 struct pf_src_node *sn = NULL;
2968 /* check maximums */
2969 if (r->max_states && (r->states >= r->max_states))
2971 /* src node for flter rule */
2972 if ((r->rule_flag & PFRULE_SRCTRACK ||
2973 r->rpool.opts & PF_POOL_STICKYADDR) &&
2974 pf_insert_src_node(&sn, r, saddr, af) != 0)
2976 /* src node for translation rule */
2977 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2978 ((direction == PF_OUT &&
2979 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2980 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2982 s = pool_get(&pf_state_pl, PR_NOWAIT);
2985 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
2986 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
2987 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2988 pf_status.src_nodes--;
2989 pool_put(&pf_src_tree_pl, sn);
2991 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
2993 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
2994 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2995 pf_status.src_nodes--;
2996 pool_put(&pf_src_tree_pl, nsn);
2998 REASON_SET(&reason, PFRES_MEMORY);
3001 bzero(s, sizeof(*s));
3006 s->nat_rule.ptr = nr;
3007 if (s->nat_rule.ptr != NULL)
3008 s->nat_rule.ptr->states++;
3010 s->allow_opts = r->allow_opts;
3011 s->log = r->log & 2;
3012 s->proto = IPPROTO_UDP;
3013 s->direction = direction;
3015 if (direction == PF_OUT) {
3016 PF_ACPY(&s->gwy.addr, saddr, af);
3017 s->gwy.port = uh->uh_sport;
3018 PF_ACPY(&s->ext.addr, daddr, af);
3019 s->ext.port = uh->uh_dport;
3021 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3022 s->lan.port = bport;
3024 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3025 s->lan.port = s->gwy.port;
3028 PF_ACPY(&s->lan.addr, daddr, af);
3029 s->lan.port = uh->uh_dport;
3030 PF_ACPY(&s->ext.addr, saddr, af);
3031 s->ext.port = uh->uh_sport;
3033 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3034 s->gwy.port = bport;
3036 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3037 s->gwy.port = s->lan.port;
3040 s->src.state = PFUDPS_SINGLE;
3041 s->dst.state = PFUDPS_NO_TRAFFIC;
3042 s->creation = time_second;
3043 s->expire = time_second;
3044 s->timeout = PFTM_UDP_FIRST_PACKET;
3045 pf_set_rt_ifp(s, saddr);
3048 s->src_node->states++;
3051 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3052 s->nat_src_node = nsn;
3053 s->nat_src_node->states++;
3055 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3056 REASON_SET(&reason, PFRES_MEMORY);
3057 pf_src_tree_remove_state(s);
3058 pool_put(&pf_state_pl, s);
3064 /* copy back packet headers if we performed NAT operations */
3066 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
3072 pf_test_icmp(struct pf_rule **rm, struct pf_state **sm, int direction,
3073 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3074 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
3076 struct pf_rule *nr = NULL;
3077 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3078 struct pf_rule *r, *a = NULL;
3079 struct pf_ruleset *ruleset = NULL;
3080 struct pf_src_node *nsn = NULL;
3082 u_int16_t icmpid = 0;
3083 sa_family_t af = pd->af;
3084 u_int8_t icmptype = 0, icmpcode = 0;
3091 switch (pd->proto) {
3094 icmptype = pd->hdr.icmp->icmp_type;
3095 icmpcode = pd->hdr.icmp->icmp_code;
3096 icmpid = pd->hdr.icmp->icmp_id;
3098 if (icmptype == ICMP_UNREACH ||
3099 icmptype == ICMP_SOURCEQUENCH ||
3100 icmptype == ICMP_REDIRECT ||
3101 icmptype == ICMP_TIMXCEED ||
3102 icmptype == ICMP_PARAMPROB)
3107 case IPPROTO_ICMPV6:
3108 icmptype = pd->hdr.icmp6->icmp6_type;
3109 icmpcode = pd->hdr.icmp6->icmp6_code;
3110 icmpid = pd->hdr.icmp6->icmp6_id;
3112 if (icmptype == ICMP6_DST_UNREACH ||
3113 icmptype == ICMP6_PACKET_TOO_BIG ||
3114 icmptype == ICMP6_TIME_EXCEEDED ||
3115 icmptype == ICMP6_PARAM_PROB)
3121 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3123 if (direction == PF_OUT) {
3124 /* check outgoing packet for BINAT/NAT */
3125 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3126 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3127 PF_ACPY(&pd->baddr, saddr, af);
3131 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3132 pd->naddr.v4.s_addr, 0);
3137 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3148 /* check incoming packet for BINAT/RDR */
3149 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3150 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3151 PF_ACPY(&pd->baddr, daddr, af);
3155 pf_change_a(&daddr->v4.s_addr,
3156 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3161 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3175 if (r->kif != NULL &&
3176 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3177 r = r->skip[PF_SKIP_IFP].ptr;
3178 else if (r->direction && r->direction != direction)
3179 r = r->skip[PF_SKIP_DIR].ptr;
3180 else if (r->af && r->af != af)
3181 r = r->skip[PF_SKIP_AF].ptr;
3182 else if (r->proto && r->proto != pd->proto)
3183 r = r->skip[PF_SKIP_PROTO].ptr;
3184 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
3185 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3186 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
3187 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3188 else if (r->type && r->type != icmptype + 1)
3189 r = TAILQ_NEXT(r, entries);
3190 else if (r->code && r->code != icmpcode + 1)
3191 r = TAILQ_NEXT(r, entries);
3192 else if (r->tos && !(r->tos & pd->tos))
3193 r = TAILQ_NEXT(r, entries);
3194 else if (r->rule_flag & PFRULE_FRAGMENT)
3195 r = TAILQ_NEXT(r, entries);
3196 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3197 r = TAILQ_NEXT(r, entries);
3198 else if (r->anchorname[0] && r->anchor == NULL)
3199 r = TAILQ_NEXT(r, entries);
3200 else if (r->os_fingerprint != PF_OSFP_ANY)
3201 r = TAILQ_NEXT(r, entries);
3205 if (r->anchor == NULL) {
3211 r = TAILQ_NEXT(r, entries);
3213 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3216 if (r == NULL && a != NULL)
3217 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3224 REASON_SET(&reason, PFRES_MATCH);
3229 m_copyback(m, off, sizeof(struct icmp6_hdr),
3230 (caddr_t)pd->hdr.icmp6);
3232 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3235 if (r->action != PF_PASS)
3238 pf_tag_packet(m, tag);
3240 if (!state_icmp && (r->keep_state || nr != NULL)) {
3241 /* create new state */
3242 struct pf_state *s = NULL;
3243 struct pf_src_node *sn = NULL;
3245 /* check maximums */
3246 if (r->max_states && (r->states >= r->max_states))
3248 /* src node for flter rule */
3249 if ((r->rule_flag & PFRULE_SRCTRACK ||
3250 r->rpool.opts & PF_POOL_STICKYADDR) &&
3251 pf_insert_src_node(&sn, r, saddr, af) != 0)
3253 /* src node for translation rule */
3254 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3255 ((direction == PF_OUT &&
3256 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3257 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3259 s = pool_get(&pf_state_pl, PR_NOWAIT);
3262 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3263 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3264 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3265 pf_status.src_nodes--;
3266 pool_put(&pf_src_tree_pl, sn);
3268 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3270 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3271 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3272 pf_status.src_nodes--;
3273 pool_put(&pf_src_tree_pl, nsn);
3275 REASON_SET(&reason, PFRES_MEMORY);
3278 bzero(s, sizeof(*s));
3283 s->nat_rule.ptr = nr;
3284 if (s->nat_rule.ptr != NULL)
3285 s->nat_rule.ptr->states++;
3287 s->allow_opts = r->allow_opts;
3288 s->log = r->log & 2;
3289 s->proto = pd->proto;
3290 s->direction = direction;
3292 if (direction == PF_OUT) {
3293 PF_ACPY(&s->gwy.addr, saddr, af);
3294 s->gwy.port = icmpid;
3295 PF_ACPY(&s->ext.addr, daddr, af);
3296 s->ext.port = icmpid;
3298 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3300 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3301 s->lan.port = icmpid;
3303 PF_ACPY(&s->lan.addr, daddr, af);
3304 s->lan.port = icmpid;
3305 PF_ACPY(&s->ext.addr, saddr, af);
3306 s->ext.port = icmpid;
3308 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3310 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3311 s->gwy.port = icmpid;
3313 s->creation = time_second;
3314 s->expire = time_second;
3315 s->timeout = PFTM_ICMP_FIRST_PACKET;
3316 pf_set_rt_ifp(s, saddr);
3319 s->src_node->states++;
3322 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3323 s->nat_src_node = nsn;
3324 s->nat_src_node->states++;
3326 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3327 REASON_SET(&reason, PFRES_MEMORY);
3328 pf_src_tree_remove_state(s);
3329 pool_put(&pf_state_pl, s);
3336 /* copy back packet headers if we performed IPv6 NAT operations */
3338 m_copyback(m, off, sizeof(struct icmp6_hdr),
3339 (caddr_t)pd->hdr.icmp6);
3346 pf_test_other(struct pf_rule **rm, struct pf_state **sm, int direction,
3347 struct pfi_kif *kif, struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3348 struct pf_rule **am, struct pf_ruleset **rsm)
3350 struct pf_rule *nr = NULL;
3351 struct pf_rule *r, *a = NULL;
3352 struct pf_ruleset *ruleset = NULL;
3353 struct pf_src_node *nsn = NULL;
3354 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3355 sa_family_t af = pd->af;
3359 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3361 if (direction == PF_OUT) {
3362 /* check outgoing packet for BINAT/NAT */
3363 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3364 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3365 PF_ACPY(&pd->baddr, saddr, af);
3369 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3370 pd->naddr.v4.s_addr, 0);
3375 PF_ACPY(saddr, &pd->naddr, af);
3384 /* check incoming packet for BINAT/RDR */
3385 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3386 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3387 PF_ACPY(&pd->baddr, daddr, af);
3391 pf_change_a(&daddr->v4.s_addr,
3392 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3397 PF_ACPY(daddr, &pd->naddr, af);
3409 if (r->kif != NULL &&
3410 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3411 r = r->skip[PF_SKIP_IFP].ptr;
3412 else if (r->direction && r->direction != direction)
3413 r = r->skip[PF_SKIP_DIR].ptr;
3414 else if (r->af && r->af != af)
3415 r = r->skip[PF_SKIP_AF].ptr;
3416 else if (r->proto && r->proto != pd->proto)
3417 r = r->skip[PF_SKIP_PROTO].ptr;
3418 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3419 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3420 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3421 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3422 else if (r->tos && !(r->tos & pd->tos))
3423 r = TAILQ_NEXT(r, entries);
3424 else if (r->rule_flag & PFRULE_FRAGMENT)
3425 r = TAILQ_NEXT(r, entries);
3426 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3427 r = TAILQ_NEXT(r, entries);
3428 else if (r->anchorname[0] && r->anchor == NULL)
3429 r = TAILQ_NEXT(r, entries);
3430 else if (r->os_fingerprint != PF_OSFP_ANY)
3431 r = TAILQ_NEXT(r, entries);
3435 if (r->anchor == NULL) {
3441 r = TAILQ_NEXT(r, entries);
3443 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3446 if (r == NULL && a != NULL)
3447 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3454 REASON_SET(&reason, PFRES_MATCH);
3457 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3459 if ((r->action == PF_DROP) &&
3460 ((r->rule_flag & PFRULE_RETURNICMP) ||
3461 (r->rule_flag & PFRULE_RETURN))) {
3462 struct pf_addr *a = NULL;
3465 if (direction == PF_OUT)
3474 pf_change_a(&a->v4.s_addr, pd->ip_sum,
3475 pd->baddr.v4.s_addr, 0);
3480 PF_ACPY(a, &pd->baddr, af);
3485 if ((af == AF_INET) && r->return_icmp)
3486 pf_send_icmp(m, r->return_icmp >> 8,
3487 r->return_icmp & 255, af, r);
3488 else if ((af == AF_INET6) && r->return_icmp6)
3489 pf_send_icmp(m, r->return_icmp6 >> 8,
3490 r->return_icmp6 & 255, af, r);
3493 if (r->action != PF_PASS)
3496 pf_tag_packet(m, tag);
3498 if (r->keep_state || nr != NULL) {
3499 /* create new state */
3500 struct pf_state *s = NULL;
3501 struct pf_src_node *sn = NULL;
3503 /* check maximums */
3504 if (r->max_states && (r->states >= r->max_states))
3506 /* src node for flter rule */
3507 if ((r->rule_flag & PFRULE_SRCTRACK ||
3508 r->rpool.opts & PF_POOL_STICKYADDR) &&
3509 pf_insert_src_node(&sn, r, saddr, af) != 0)
3511 /* src node for translation rule */
3512 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3513 ((direction == PF_OUT &&
3514 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3515 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3517 s = pool_get(&pf_state_pl, PR_NOWAIT);
3520 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3521 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3522 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3523 pf_status.src_nodes--;
3524 pool_put(&pf_src_tree_pl, sn);
3526 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3528 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3529 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3530 pf_status.src_nodes--;
3531 pool_put(&pf_src_tree_pl, nsn);
3533 REASON_SET(&reason, PFRES_MEMORY);
3536 bzero(s, sizeof(*s));
3541 s->nat_rule.ptr = nr;
3542 if (s->nat_rule.ptr != NULL)
3543 s->nat_rule.ptr->states++;
3545 s->allow_opts = r->allow_opts;
3546 s->log = r->log & 2;
3547 s->proto = pd->proto;
3548 s->direction = direction;
3550 if (direction == PF_OUT) {
3551 PF_ACPY(&s->gwy.addr, saddr, af);
3552 PF_ACPY(&s->ext.addr, daddr, af);
3554 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3556 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3558 PF_ACPY(&s->lan.addr, daddr, af);
3559 PF_ACPY(&s->ext.addr, saddr, af);
3561 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3563 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3565 s->src.state = PFOTHERS_SINGLE;
3566 s->dst.state = PFOTHERS_NO_TRAFFIC;
3567 s->creation = time_second;
3568 s->expire = time_second;
3569 s->timeout = PFTM_OTHER_FIRST_PACKET;
3570 pf_set_rt_ifp(s, saddr);
3573 s->src_node->states++;
3576 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3577 s->nat_src_node = nsn;
3578 s->nat_src_node->states++;
3580 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3581 REASON_SET(&reason, PFRES_MEMORY);
3582 pf_src_tree_remove_state(s);
3583 pool_put(&pf_state_pl, s);
3593 pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
3594 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
3595 struct pf_ruleset **rsm)
3597 struct pf_rule *r, *a = NULL;
3598 struct pf_ruleset *ruleset = NULL;
3599 sa_family_t af = pd->af;
3603 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3606 if (r->kif != NULL &&
3607 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3608 r = r->skip[PF_SKIP_IFP].ptr;
3609 else if (r->direction && r->direction != direction)
3610 r = r->skip[PF_SKIP_DIR].ptr;
3611 else if (r->af && r->af != af)
3612 r = r->skip[PF_SKIP_AF].ptr;
3613 else if (r->proto && r->proto != pd->proto)
3614 r = r->skip[PF_SKIP_PROTO].ptr;
3615 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3616 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3617 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3618 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3619 else if (r->tos && !(r->tos & pd->tos))
3620 r = TAILQ_NEXT(r, entries);
3621 else if (r->src.port_op || r->dst.port_op ||
3622 r->flagset || r->type || r->code ||
3623 r->os_fingerprint != PF_OSFP_ANY)
3624 r = TAILQ_NEXT(r, entries);
3625 else if (r->match_tag && !pf_match_tag(m, r, NULL, &tag))
3626 r = TAILQ_NEXT(r, entries);
3627 else if (r->anchorname[0] && r->anchor == NULL)
3628 r = TAILQ_NEXT(r, entries);
3630 if (r->anchor == NULL) {
3636 r = TAILQ_NEXT(r, entries);
3638 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3641 if (r == NULL && a != NULL)
3642 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3649 REASON_SET(&reason, PFRES_MATCH);
3652 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3654 if (r->action != PF_PASS)
3657 pf_tag_packet(m, tag);
3663 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
3664 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3667 struct pf_state key;
3668 struct tcphdr *th = pd->hdr.tcp;
3669 u_int16_t win = ntohs(th->th_win);
3670 u_int32_t ack, end, seq;
3674 struct pf_state_peer *src, *dst;
3677 key.proto = IPPROTO_TCP;
3678 if (direction == PF_IN) {
3679 PF_ACPY(&key.ext.addr, pd->src, key.af);
3680 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
3681 key.ext.port = th->th_sport;
3682 key.gwy.port = th->th_dport;
3684 PF_ACPY(&key.lan.addr, pd->src, key.af);
3685 PF_ACPY(&key.ext.addr, pd->dst, key.af);
3686 key.lan.port = th->th_sport;
3687 key.ext.port = th->th_dport;
3692 if (direction == (*state)->direction) {
3693 src = &(*state)->src;
3694 dst = &(*state)->dst;
3696 src = &(*state)->dst;
3697 dst = &(*state)->src;
3700 if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
3701 if (direction != (*state)->direction)
3702 return (PF_SYNPROXY_DROP);
3703 if (th->th_flags & TH_SYN) {
3704 if (ntohl(th->th_seq) != (*state)->src.seqlo)
3706 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3707 pd->src, th->th_dport, th->th_sport,
3708 (*state)->src.seqhi, ntohl(th->th_seq) + 1,
3709 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0);
3710 return (PF_SYNPROXY_DROP);
3711 } else if (!(th->th_flags & TH_ACK) ||
3712 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3713 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3716 (*state)->src.state = PF_TCPS_PROXY_DST;
3718 if ((*state)->src.state == PF_TCPS_PROXY_DST) {
3719 struct pf_state_host *src, *dst;
3721 if (direction == PF_OUT) {
3722 src = &(*state)->gwy;
3723 dst = &(*state)->ext;
3725 src = &(*state)->ext;
3726 dst = &(*state)->lan;
3728 if (direction == (*state)->direction) {
3729 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
3730 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3731 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3733 (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
3734 if ((*state)->dst.seqhi == 1)
3735 (*state)->dst.seqhi = karc4random();
3736 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3737 &dst->addr, src->port, dst->port,
3738 (*state)->dst.seqhi, 0, TH_SYN, 0,
3739 (*state)->src.mss, 0);
3740 return (PF_SYNPROXY_DROP);
3741 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
3743 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1))
3746 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
3747 (*state)->dst.seqlo = ntohl(th->th_seq);
3748 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3749 pd->src, th->th_dport, th->th_sport,
3750 ntohl(th->th_ack), ntohl(th->th_seq) + 1,
3751 TH_ACK, (*state)->src.max_win, 0, 0);
3752 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3753 &dst->addr, src->port, dst->port,
3754 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
3755 TH_ACK, (*state)->dst.max_win, 0, 0);
3756 (*state)->src.seqdiff = (*state)->dst.seqhi -
3757 (*state)->src.seqlo;
3758 (*state)->dst.seqdiff = (*state)->src.seqhi -
3759 (*state)->dst.seqlo;
3760 (*state)->src.seqhi = (*state)->src.seqlo +
3761 (*state)->src.max_win;
3762 (*state)->dst.seqhi = (*state)->dst.seqlo +
3763 (*state)->dst.max_win;
3764 (*state)->src.wscale = (*state)->dst.wscale = 0;
3765 (*state)->src.state = (*state)->dst.state =
3767 return (PF_SYNPROXY_DROP);
3771 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
3772 sws = src->wscale & PF_WSCALE_MASK;
3773 dws = dst->wscale & PF_WSCALE_MASK;
3778 * Sequence tracking algorithm from Guido van Rooij's paper:
3779 * http://www.madison-gurkha.com/publications/tcp_filtering/
3783 seq = ntohl(th->th_seq);
3784 if (src->seqlo == 0) {
3785 /* First packet from this end. Set its state */
3787 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
3788 src->scrub == NULL) {
3789 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
3790 REASON_SET(reason, PFRES_MEMORY);
3795 /* Deferred generation of sequence number modulator */
3796 if (dst->seqdiff && !src->seqdiff) {
3797 while ((src->seqdiff = karc4random()) == 0)
3799 ack = ntohl(th->th_ack) - dst->seqdiff;
3800 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3802 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3805 ack = ntohl(th->th_ack);
3808 end = seq + pd->p_len;
3809 if (th->th_flags & TH_SYN) {
3811 if (dst->wscale & PF_WSCALE_FLAG) {
3812 src->wscale = pf_get_wscale(m, off, th->th_off,
3814 if (src->wscale & PF_WSCALE_FLAG) {
3815 /* Remove scale factor from initial
3817 sws = src->wscale & PF_WSCALE_MASK;
3818 win = ((u_int32_t)win + (1 << sws) - 1)
3820 dws = dst->wscale & PF_WSCALE_MASK;
3822 /* fixup other window */
3823 dst->max_win <<= dst->wscale &
3825 /* in case of a retrans SYN|ACK */
3830 if (th->th_flags & TH_FIN)
3834 if (src->state < TCPS_SYN_SENT)
3835 src->state = TCPS_SYN_SENT;
3838 * May need to slide the window (seqhi may have been set by
3839 * the crappy stack check or if we picked up the connection
3840 * after establishment)
3842 if (src->seqhi == 1 ||
3843 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
3844 src->seqhi = end + MAX(1, dst->max_win << dws);
3845 if (win > src->max_win)
3849 ack = ntohl(th->th_ack) - dst->seqdiff;
3851 /* Modulate sequence numbers */
3852 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3854 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3857 end = seq + pd->p_len;
3858 if (th->th_flags & TH_SYN)
3860 if (th->th_flags & TH_FIN)
3864 if ((th->th_flags & TH_ACK) == 0) {
3865 /* Let it pass through the ack skew check */
3867 } else if ((ack == 0 &&
3868 (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
3869 /* broken tcp stacks do not set ack */
3870 (dst->state < TCPS_SYN_SENT)) {
3872 * Many stacks (ours included) will set the ACK number in an
3873 * FIN|ACK if the SYN times out -- no sequence to ACK.
3879 /* Ease sequencing restrictions on no data packets */
3884 ackskew = dst->seqlo - ack;
3886 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
3887 if (SEQ_GEQ(src->seqhi, end) &&
3888 /* Last octet inside other's window space */
3889 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
3890 /* Retrans: not more than one window back */
3891 (ackskew >= -MAXACKWINDOW) &&
3892 /* Acking not more than one reassembled fragment backwards */
3893 (ackskew <= (MAXACKWINDOW << sws))) {
3894 /* Acking not more than one window forward */
3896 /* update max window */
3897 if (src->max_win < win)
3899 /* synchronize sequencing */
3900 if (SEQ_GT(end, src->seqlo))
3902 /* slide the window of what the other end can send */
3903 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
3904 dst->seqhi = ack + MAX((win << sws), 1);
3908 if (th->th_flags & TH_SYN)
3909 if (src->state < TCPS_SYN_SENT)
3910 src->state = TCPS_SYN_SENT;
3911 if (th->th_flags & TH_FIN)
3912 if (src->state < TCPS_CLOSING)
3913 src->state = TCPS_CLOSING;
3914 if (th->th_flags & TH_ACK) {
3915 if (dst->state == TCPS_SYN_SENT)
3916 dst->state = TCPS_ESTABLISHED;
3917 else if (dst->state == TCPS_CLOSING)
3918 dst->state = TCPS_FIN_WAIT_2;
3920 if (th->th_flags & TH_RST)
3921 src->state = dst->state = TCPS_TIME_WAIT;
3923 /* update expire time */
3924 (*state)->expire = time_second;
3925 if (src->state >= TCPS_FIN_WAIT_2 &&
3926 dst->state >= TCPS_FIN_WAIT_2)
3927 (*state)->timeout = PFTM_TCP_CLOSED;
3928 else if (src->state >= TCPS_FIN_WAIT_2 ||
3929 dst->state >= TCPS_FIN_WAIT_2)
3930 (*state)->timeout = PFTM_TCP_FIN_WAIT;
3931 else if (src->state < TCPS_ESTABLISHED ||
3932 dst->state < TCPS_ESTABLISHED)
3933 (*state)->timeout = PFTM_TCP_OPENING;
3934 else if (src->state >= TCPS_CLOSING ||
3935 dst->state >= TCPS_CLOSING)
3936 (*state)->timeout = PFTM_TCP_CLOSING;
3938 (*state)->timeout = PFTM_TCP_ESTABLISHED;
3940 /* Fall through to PASS packet */
3942 } else if ((dst->state < TCPS_SYN_SENT ||
3943 dst->state >= TCPS_FIN_WAIT_2 ||
3944 src->state >= TCPS_FIN_WAIT_2) &&
3945 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
3946 /* Within a window forward of the originating packet */
3947 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
3948 /* Within a window backward of the originating packet */
3951 * This currently handles three situations:
3952 * 1) Stupid stacks will shotgun SYNs before their peer
3954 * 2) When PF catches an already established stream (the
3955 * firewall rebooted, the state table was flushed, routes
3957 * 3) Packets get funky immediately after the connection
3958 * closes (this should catch Solaris spurious ACK|FINs
3959 * that web servers like to spew after a close)
3961 * This must be a little more careful than the above code
3962 * since packet floods will also be caught here. We don't
3963 * update the TTL here to mitigate the damage of a packet
3964 * flood and so the same code can handle awkward establishment
3965 * and a loosened connection close.
3966 * In the establishment case, a correct peer response will
3967 * validate the connection, go through the normal state code
3968 * and keep updating the state TTL.
3971 if (pf_status.debug >= PF_DEBUG_MISC) {
3972 printf("pf: loose state match: ");
3973 pf_print_state(*state);
3974 pf_print_flags(th->th_flags);
3975 printf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d\n",
3976 seq, ack, pd->p_len, ackskew,
3977 (*state)->packets[0], (*state)->packets[1]);
3980 /* update max window */
3981 if (src->max_win < win)
3983 /* synchronize sequencing */
3984 if (SEQ_GT(end, src->seqlo))
3986 /* slide the window of what the other end can send */
3987 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
3988 dst->seqhi = ack + MAX((win << sws), 1);
3991 * Cannot set dst->seqhi here since this could be a shotgunned
3992 * SYN and not an already established connection.
3995 if (th->th_flags & TH_FIN)
3996 if (src->state < TCPS_CLOSING)
3997 src->state = TCPS_CLOSING;
3998 if (th->th_flags & TH_RST)
3999 src->state = dst->state = TCPS_TIME_WAIT;
4001 /* Fall through to PASS packet */
4004 if ((*state)->dst.state == TCPS_SYN_SENT &&
4005 (*state)->src.state == TCPS_SYN_SENT) {
4006 /* Send RST for state mismatches during handshake */
4007 if (!(th->th_flags & TH_RST)) {
4008 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
4010 if (th->th_flags & TH_SYN)
4012 if (th->th_flags & TH_FIN)
4014 pf_send_tcp((*state)->rule.ptr, pd->af,
4015 pd->dst, pd->src, th->th_dport,
4016 th->th_sport, ntohl(th->th_ack), ack,
4017 TH_RST|TH_ACK, 0, 0,
4018 (*state)->rule.ptr->return_ttl);
4023 } else if (pf_status.debug >= PF_DEBUG_MISC) {
4024 printf("pf: BAD state: ");
4025 pf_print_state(*state);
4026 pf_print_flags(th->th_flags);
4027 printf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d "
4028 "dir=%s,%s\n", seq, ack, pd->p_len, ackskew,
4029 (*state)->packets[0], (*state)->packets[1],
4030 direction == PF_IN ? "in" : "out",
4031 direction == (*state)->direction ? "fwd" : "rev");
4032 printf("pf: State failure on: %c %c %c %c | %c %c\n",
4033 SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
4034 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
4036 (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
4037 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
4038 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
4039 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
4044 if (dst->scrub || src->scrub) {
4045 if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4046 src, dst, ©back))
4050 /* Any packets which have gotten here are to be passed */
4052 /* translate source/destination address, if necessary */
4053 if (STATE_TRANSLATE(*state)) {
4054 if (direction == PF_OUT)
4055 pf_change_ap(pd->src, &th->th_sport, pd->ip_sum,
4056 &th->th_sum, &(*state)->gwy.addr,
4057 (*state)->gwy.port, 0, pd->af);
4059 pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum,
4060 &th->th_sum, &(*state)->lan.addr,
4061 (*state)->lan.port, 0, pd->af);
4062 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4063 } else if (copyback) {
4064 /* Copyback sequence modulation or stateful scrub changes */
4065 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4072 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif,
4073 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4075 struct pf_state_peer *src, *dst;
4076 struct pf_state key;
4077 struct udphdr *uh = pd->hdr.udp;
4080 key.proto = IPPROTO_UDP;
4081 if (direction == PF_IN) {
4082 PF_ACPY(&key.ext.addr, pd->src, key.af);
4083 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4084 key.ext.port = uh->uh_sport;
4085 key.gwy.port = uh->uh_dport;
4087 PF_ACPY(&key.lan.addr, pd->src, key.af);
4088 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4089 key.lan.port = uh->uh_sport;
4090 key.ext.port = uh->uh_dport;
4095 if (direction == (*state)->direction) {
4096 src = &(*state)->src;
4097 dst = &(*state)->dst;
4099 src = &(*state)->dst;
4100 dst = &(*state)->src;
4104 if (src->state < PFUDPS_SINGLE)
4105 src->state = PFUDPS_SINGLE;
4106 if (dst->state == PFUDPS_SINGLE)
4107 dst->state = PFUDPS_MULTIPLE;
4109 /* update expire time */
4110 (*state)->expire = time_second;
4111 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
4112 (*state)->timeout = PFTM_UDP_MULTIPLE;
4114 (*state)->timeout = PFTM_UDP_SINGLE;
4116 /* translate source/destination address, if necessary */
4117 if (STATE_TRANSLATE(*state)) {
4118 if (direction == PF_OUT)
4119 pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum,
4120 &uh->uh_sum, &(*state)->gwy.addr,
4121 (*state)->gwy.port, 1, pd->af);
4123 pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum,
4124 &uh->uh_sum, &(*state)->lan.addr,
4125 (*state)->lan.port, 1, pd->af);
4126 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
4133 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif,
4134 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4136 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
4137 u_int16_t icmpid = 0;
4138 u_int16_t *icmpsum = NULL;
4139 u_int8_t icmptype = 0;
4142 switch (pd->proto) {
4145 icmptype = pd->hdr.icmp->icmp_type;
4146 icmpid = pd->hdr.icmp->icmp_id;
4147 icmpsum = &pd->hdr.icmp->icmp_cksum;
4149 if (icmptype == ICMP_UNREACH ||
4150 icmptype == ICMP_SOURCEQUENCH ||
4151 icmptype == ICMP_REDIRECT ||
4152 icmptype == ICMP_TIMXCEED ||
4153 icmptype == ICMP_PARAMPROB)
4158 case IPPROTO_ICMPV6:
4159 icmptype = pd->hdr.icmp6->icmp6_type;
4160 icmpid = pd->hdr.icmp6->icmp6_id;
4161 icmpsum = &pd->hdr.icmp6->icmp6_cksum;
4163 if (icmptype == ICMP6_DST_UNREACH ||
4164 icmptype == ICMP6_PACKET_TOO_BIG ||
4165 icmptype == ICMP6_TIME_EXCEEDED ||
4166 icmptype == ICMP6_PARAM_PROB)
4175 * ICMP query/reply message not related to a TCP/UDP packet.
4176 * Search for an ICMP state.
4178 struct pf_state key;
4181 key.proto = pd->proto;
4182 if (direction == PF_IN) {
4183 PF_ACPY(&key.ext.addr, pd->src, key.af);
4184 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4185 key.ext.port = icmpid;
4186 key.gwy.port = icmpid;
4188 PF_ACPY(&key.lan.addr, pd->src, key.af);
4189 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4190 key.lan.port = icmpid;
4191 key.ext.port = icmpid;
4196 (*state)->expire = time_second;
4197 (*state)->timeout = PFTM_ICMP_ERROR_REPLY;
4199 /* translate source/destination address, if necessary */
4200 if (PF_ANEQ(&(*state)->lan.addr, &(*state)->gwy.addr, pd->af)) {
4201 if (direction == PF_OUT) {
4205 pf_change_a(&saddr->v4.s_addr,
4207 (*state)->gwy.addr.v4.s_addr, 0);
4213 &pd->hdr.icmp6->icmp6_cksum,
4214 &(*state)->gwy.addr, 0);
4216 sizeof(struct icmp6_hdr),
4217 (caddr_t)pd->hdr.icmp6);
4225 pf_change_a(&daddr->v4.s_addr,
4227 (*state)->lan.addr.v4.s_addr, 0);
4233 &pd->hdr.icmp6->icmp6_cksum,
4234 &(*state)->lan.addr, 0);
4236 sizeof(struct icmp6_hdr),
4237 (caddr_t)pd->hdr.icmp6);
4248 * ICMP error message in response to a TCP/UDP packet.
4249 * Extract the inner TCP/UDP header and search for that state.
4252 struct pf_pdesc pd2;
4257 struct ip6_hdr h2_6;
4267 /* offset of h2 in mbuf chain */
4268 ipoff2 = off + ICMP_MINLEN;
4270 if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
4271 NULL, NULL, pd2.af)) {
4272 DPFPRINTF(PF_DEBUG_MISC,
4273 ("pf: ICMP error message too short "
4278 * ICMP error messages don't refer to non-first
4282 * Note: We are dealing with an encapsulated
4283 * header. This means ip_off/ip_len are not
4284 * in host byte order!
4286 if (h2.ip_off & htons(IP_OFFMASK))
4289 /* offset of protocol header that follows h2 */
4290 off2 = ipoff2 + (h2.ip_hl << 2);
4292 pd2.proto = h2.ip_p;
4293 pd2.src = (struct pf_addr *)&h2.ip_src;
4294 pd2.dst = (struct pf_addr *)&h2.ip_dst;
4295 pd2.ip_sum = &h2.ip_sum;
4300 ipoff2 = off + sizeof(struct icmp6_hdr);
4302 if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
4303 NULL, NULL, pd2.af)) {
4304 DPFPRINTF(PF_DEBUG_MISC,
4305 ("pf: ICMP error message too short "
4309 pd2.proto = h2_6.ip6_nxt;
4310 pd2.src = (struct pf_addr *)&h2_6.ip6_src;
4311 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
4313 off2 = ipoff2 + sizeof(h2_6);
4315 switch (pd2.proto) {
4316 case IPPROTO_FRAGMENT:
4318 * ICMPv6 error messages for
4319 * non-first fragments
4323 case IPPROTO_HOPOPTS:
4324 case IPPROTO_ROUTING:
4325 case IPPROTO_DSTOPTS: {
4326 /* get next header and header length */
4327 struct ip6_ext opt6;
4329 if (!pf_pull_hdr(m, off2, &opt6,
4330 sizeof(opt6), NULL, NULL, pd2.af)) {
4331 DPFPRINTF(PF_DEBUG_MISC,
4332 ("pf: ICMPv6 short opt\n"));
4335 if (pd2.proto == IPPROTO_AH)
4336 off2 += (opt6.ip6e_len + 2) * 4;
4338 off2 += (opt6.ip6e_len + 1) * 8;
4339 pd2.proto = opt6.ip6e_nxt;
4340 /* goto the next header */
4347 } while (!terminal);
4352 switch (pd2.proto) {
4356 struct pf_state key;
4357 struct pf_state_peer *src, *dst;
4362 * Only the first 8 bytes of the TCP header can be
4363 * expected. Don't access any TCP header fields after
4364 * th_seq, an ackskew test is not possible.
4366 if (!pf_pull_hdr(m, off2, &th, 8, NULL, NULL, pd2.af)) {
4367 DPFPRINTF(PF_DEBUG_MISC,
4368 ("pf: ICMP error message too short "
4374 key.proto = IPPROTO_TCP;
4375 if (direction == PF_IN) {
4376 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4377 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4378 key.ext.port = th.th_dport;
4379 key.gwy.port = th.th_sport;
4381 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4382 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4383 key.lan.port = th.th_dport;
4384 key.ext.port = th.th_sport;
4389 if (direction == (*state)->direction) {
4390 src = &(*state)->dst;
4391 dst = &(*state)->src;
4393 src = &(*state)->src;
4394 dst = &(*state)->dst;
4397 if (src->wscale && dst->wscale &&
4398 !(th.th_flags & TH_SYN))
4399 dws = dst->wscale & PF_WSCALE_MASK;
4403 /* Demodulate sequence number */
4404 seq = ntohl(th.th_seq) - src->seqdiff;
4406 pf_change_a(&th.th_seq, icmpsum,
4411 if (!SEQ_GEQ(src->seqhi, seq) ||
4412 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws))) {
4413 if (pf_status.debug >= PF_DEBUG_MISC) {
4414 printf("pf: BAD ICMP %d:%d ",
4415 icmptype, pd->hdr.icmp->icmp_code);
4416 pf_print_host(pd->src, 0, pd->af);
4418 pf_print_host(pd->dst, 0, pd->af);
4420 pf_print_state(*state);
4421 printf(" seq=%u\n", seq);
4426 if (STATE_TRANSLATE(*state)) {
4427 if (direction == PF_IN) {
4428 pf_change_icmp(pd2.src, &th.th_sport,
4429 daddr, &(*state)->lan.addr,
4430 (*state)->lan.port, NULL,
4431 pd2.ip_sum, icmpsum,
4432 pd->ip_sum, 0, pd2.af);
4434 pf_change_icmp(pd2.dst, &th.th_dport,
4435 saddr, &(*state)->gwy.addr,
4436 (*state)->gwy.port, NULL,
4437 pd2.ip_sum, icmpsum,
4438 pd->ip_sum, 0, pd2.af);
4447 m_copyback(m, off, ICMP_MINLEN,
4448 (caddr_t)pd->hdr.icmp);
4449 m_copyback(m, ipoff2, sizeof(h2),
4456 sizeof(struct icmp6_hdr),
4457 (caddr_t)pd->hdr.icmp6);
4458 m_copyback(m, ipoff2, sizeof(h2_6),
4463 m_copyback(m, off2, 8, (caddr_t)&th);
4471 struct pf_state key;
4473 if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
4474 NULL, NULL, pd2.af)) {
4475 DPFPRINTF(PF_DEBUG_MISC,
4476 ("pf: ICMP error message too short "
4482 key.proto = IPPROTO_UDP;
4483 if (direction == PF_IN) {
4484 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4485 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4486 key.ext.port = uh.uh_dport;
4487 key.gwy.port = uh.uh_sport;
4489 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4490 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4491 key.lan.port = uh.uh_dport;
4492 key.ext.port = uh.uh_sport;
4497 if (STATE_TRANSLATE(*state)) {
4498 if (direction == PF_IN) {
4499 pf_change_icmp(pd2.src, &uh.uh_sport,
4500 daddr, &(*state)->lan.addr,
4501 (*state)->lan.port, &uh.uh_sum,
4502 pd2.ip_sum, icmpsum,
4503 pd->ip_sum, 1, pd2.af);
4505 pf_change_icmp(pd2.dst, &uh.uh_dport,
4506 saddr, &(*state)->gwy.addr,
4507 (*state)->gwy.port, &uh.uh_sum,
4508 pd2.ip_sum, icmpsum,
4509 pd->ip_sum, 1, pd2.af);
4514 m_copyback(m, off, ICMP_MINLEN,
4515 (caddr_t)pd->hdr.icmp);
4516 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4522 sizeof(struct icmp6_hdr),
4523 (caddr_t)pd->hdr.icmp6);
4524 m_copyback(m, ipoff2, sizeof(h2_6),
4529 m_copyback(m, off2, sizeof(uh), (caddr_t)&uh);
4536 case IPPROTO_ICMP: {
4538 struct pf_state key;
4540 if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
4541 NULL, NULL, pd2.af)) {
4542 DPFPRINTF(PF_DEBUG_MISC,
4543 ("pf: ICMP error message too short i"
4549 key.proto = IPPROTO_ICMP;
4550 if (direction == PF_IN) {
4551 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4552 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4553 key.ext.port = iih.icmp_id;
4554 key.gwy.port = iih.icmp_id;
4556 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4557 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4558 key.lan.port = iih.icmp_id;
4559 key.ext.port = iih.icmp_id;
4564 if (STATE_TRANSLATE(*state)) {
4565 if (direction == PF_IN) {
4566 pf_change_icmp(pd2.src, &iih.icmp_id,
4567 daddr, &(*state)->lan.addr,
4568 (*state)->lan.port, NULL,
4569 pd2.ip_sum, icmpsum,
4570 pd->ip_sum, 0, AF_INET);
4572 pf_change_icmp(pd2.dst, &iih.icmp_id,
4573 saddr, &(*state)->gwy.addr,
4574 (*state)->gwy.port, NULL,
4575 pd2.ip_sum, icmpsum,
4576 pd->ip_sum, 0, AF_INET);
4578 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
4579 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4580 m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih);
4588 case IPPROTO_ICMPV6: {
4589 struct icmp6_hdr iih;
4590 struct pf_state key;
4592 if (!pf_pull_hdr(m, off2, &iih,
4593 sizeof(struct icmp6_hdr), NULL, NULL, pd2.af)) {
4594 DPFPRINTF(PF_DEBUG_MISC,
4595 ("pf: ICMP error message too short "
4601 key.proto = IPPROTO_ICMPV6;
4602 if (direction == PF_IN) {
4603 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4604 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4605 key.ext.port = iih.icmp6_id;
4606 key.gwy.port = iih.icmp6_id;
4608 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4609 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4610 key.lan.port = iih.icmp6_id;
4611 key.ext.port = iih.icmp6_id;
4616 if (STATE_TRANSLATE(*state)) {
4617 if (direction == PF_IN) {
4618 pf_change_icmp(pd2.src, &iih.icmp6_id,
4619 daddr, &(*state)->lan.addr,
4620 (*state)->lan.port, NULL,
4621 pd2.ip_sum, icmpsum,
4622 pd->ip_sum, 0, AF_INET6);
4624 pf_change_icmp(pd2.dst, &iih.icmp6_id,
4625 saddr, &(*state)->gwy.addr,
4626 (*state)->gwy.port, NULL,
4627 pd2.ip_sum, icmpsum,
4628 pd->ip_sum, 0, AF_INET6);
4630 m_copyback(m, off, sizeof(struct icmp6_hdr),
4631 (caddr_t)pd->hdr.icmp6);
4632 m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6);
4633 m_copyback(m, off2, sizeof(struct icmp6_hdr),
4642 struct pf_state key;
4645 key.proto = pd2.proto;
4646 if (direction == PF_IN) {
4647 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4648 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4652 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4653 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4660 if (STATE_TRANSLATE(*state)) {
4661 if (direction == PF_IN) {
4662 pf_change_icmp(pd2.src, NULL,
4663 daddr, &(*state)->lan.addr,
4665 pd2.ip_sum, icmpsum,
4666 pd->ip_sum, 0, pd2.af);
4668 pf_change_icmp(pd2.dst, NULL,
4669 saddr, &(*state)->gwy.addr,
4671 pd2.ip_sum, icmpsum,
4672 pd->ip_sum, 0, pd2.af);
4677 m_copyback(m, off, ICMP_MINLEN,
4678 (caddr_t)pd->hdr.icmp);
4679 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4685 sizeof(struct icmp6_hdr),
4686 (caddr_t)pd->hdr.icmp6);
4687 m_copyback(m, ipoff2, sizeof(h2_6),
4702 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif,
4703 struct pf_pdesc *pd)
4705 struct pf_state_peer *src, *dst;
4706 struct pf_state key;
4709 key.proto = pd->proto;
4710 if (direction == PF_IN) {
4711 PF_ACPY(&key.ext.addr, pd->src, key.af);
4712 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4716 PF_ACPY(&key.lan.addr, pd->src, key.af);
4717 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4724 if (direction == (*state)->direction) {
4725 src = &(*state)->src;
4726 dst = &(*state)->dst;
4728 src = &(*state)->dst;
4729 dst = &(*state)->src;
4733 if (src->state < PFOTHERS_SINGLE)
4734 src->state = PFOTHERS_SINGLE;
4735 if (dst->state == PFOTHERS_SINGLE)
4736 dst->state = PFOTHERS_MULTIPLE;
4738 /* update expire time */
4739 (*state)->expire = time_second;
4740 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
4741 (*state)->timeout = PFTM_OTHER_MULTIPLE;
4743 (*state)->timeout = PFTM_OTHER_SINGLE;
4745 /* translate source/destination address, if necessary */
4746 if (STATE_TRANSLATE(*state)) {
4747 if (direction == PF_OUT)
4751 pf_change_a(&pd->src->v4.s_addr,
4752 pd->ip_sum, (*state)->gwy.addr.v4.s_addr,
4758 PF_ACPY(pd->src, &(*state)->gwy.addr, pd->af);
4766 pf_change_a(&pd->dst->v4.s_addr,
4767 pd->ip_sum, (*state)->lan.addr.v4.s_addr,
4773 PF_ACPY(pd->dst, &(*state)->lan.addr, pd->af);
4783 * ipoff and off are measured from the start of the mbuf chain.
4784 * h must be at "ipoff" on the mbuf chain.
4787 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
4788 u_short *actionp, u_short *reasonp, sa_family_t af)
4793 struct ip *h = mtod(m, struct ip *);
4794 u_int16_t fragoff = (h->ip_off & IP_OFFMASK) << 3;
4798 ACTION_SET(actionp, PF_PASS);
4800 ACTION_SET(actionp, PF_DROP);
4801 REASON_SET(reasonp, PFRES_FRAG);
4805 if (m->m_pkthdr.len < off + len ||
4806 h->ip_len < off + len) {
4807 ACTION_SET(actionp, PF_DROP);
4808 REASON_SET(reasonp, PFRES_SHORT);
4816 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
4818 if (m->m_pkthdr.len < off + len ||
4819 (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
4820 (unsigned)(off + len)) {
4821 ACTION_SET(actionp, PF_DROP);
4822 REASON_SET(reasonp, PFRES_SHORT);
4829 m_copydata(m, off, len, p);
4834 pf_routable(struct pf_addr *addr, sa_family_t af)
4836 struct sockaddr_in *dst;
4840 bzero(&ro, sizeof(ro));
4841 dst = satosin(&ro.ro_dst);
4842 dst->sin_family = af;
4843 dst->sin_len = sizeof(*dst);
4844 dst->sin_addr = addr->v4;
4845 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
4847 if (ro.ro_rt != NULL) {
4857 pf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
4860 struct mbuf *m0, *m1;
4861 struct route iproute;
4862 struct route *ro = NULL;
4863 struct sockaddr_in *dst;
4865 struct ifnet *ifp = NULL;
4866 struct pf_addr naddr;
4867 struct pf_src_node *sn = NULL;
4871 if (m == NULL || *m == NULL || r == NULL ||
4872 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
4873 panic("pf_route: invalid parameters");
4875 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
4876 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
4877 (*m)->m_pkthdr.pf_routed = 1;
4879 if ((*m)->m_pkthdr.pf_routed > 3) {
4884 (*m)->m_pkthdr.pf_routed++;
4887 if (r->rt == PF_DUPTO) {
4888 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
4891 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
4896 if (m0->m_len < sizeof(struct ip))
4897 panic("pf_route: m0->m_len < sizeof(struct ip)");
4898 ip = mtod(m0, struct ip *);
4901 bzero((caddr_t)ro, sizeof(*ro));
4902 dst = satosin(&ro->ro_dst);
4903 dst->sin_family = AF_INET;
4904 dst->sin_len = sizeof(*dst);
4905 dst->sin_addr = ip->ip_dst;
4907 if (r->rt == PF_FASTROUTE) {
4909 if (ro->ro_rt == 0) {
4910 ipstat.ips_noroute++;
4914 ifp = ro->ro_rt->rt_ifp;
4915 ro->ro_rt->rt_use++;
4917 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
4918 dst = satosin(ro->ro_rt->rt_gateway);
4920 if (TAILQ_EMPTY(&r->rpool.list))
4921 panic("pf_route: TAILQ_EMPTY(&r->rpool.list)");
4923 pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
4925 if (!PF_AZERO(&naddr, AF_INET))
4926 dst->sin_addr.s_addr = naddr.v4.s_addr;
4927 ifp = r->rpool.cur->kif ?
4928 r->rpool.cur->kif->pfik_ifp : NULL;
4930 if (!PF_AZERO(&s->rt_addr, AF_INET))
4931 dst->sin_addr.s_addr =
4932 s->rt_addr.v4.s_addr;
4933 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
4940 if (pf_test(PF_OUT, ifp, &m0) != PF_PASS)
4942 else if (m0 == NULL)
4944 if (m0->m_len < sizeof(struct ip))
4945 panic("pf_route: m0->m_len < sizeof(struct ip)");
4946 ip = mtod(m0, struct ip *);
4949 /* Copied from ip_output. */
4950 m0->m_pkthdr.csum_flags |= CSUM_IP;
4951 sw_csum = m0->m_pkthdr.csum_flags & ~ifp->if_hwassist;
4952 if (sw_csum & CSUM_DELAY_DATA) {
4953 in_delayed_cksum(m0);
4954 sw_csum &= ~CSUM_DELAY_DATA;
4956 m0->m_pkthdr.csum_flags &= ifp->if_hwassist;
4959 * If small enough for interface, or the interface will take
4960 * care of the fragmentation for us, can just send directly.
4962 if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) &&
4963 (ip->ip_off & IP_DF) == 0)) {
4964 ip->ip_len = htons(ip->ip_len);
4965 ip->ip_off = htons(ip->ip_off);
4967 if (sw_csum & CSUM_DELAY_IP) {
4969 if (ip->ip_v == IPVERSION &&
4970 (ip->ip_hl << 2) == sizeof(*ip)) {
4971 ip->ip_sum = in_cksum_hdr(ip);
4973 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
4977 lwkt_serialize_enter(ifp->if_serializer);
4978 error = (*ifp->if_output)(ifp, m0, sintosa(dst), ro->ro_rt);
4979 lwkt_serialize_exit(ifp->if_serializer);
4984 * Too large for interface; fragment if possible.
4985 * Must be able to put at least 8 bytes per fragment.
4987 if (ip->ip_off & IP_DF) {
4988 ipstat.ips_cantfrag++;
4989 if (r->rt != PF_DUPTO) {
4990 icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
4998 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist, sw_csum);
5002 for (m0 = m1; m0; m0 = m1) {
5006 lwkt_serialize_enter(ifp->if_serializer);
5007 error = (*ifp->if_output)(ifp, m0, sintosa(dst),
5009 lwkt_serialize_exit(ifp->if_serializer);
5016 ipstat.ips_fragmented++;
5019 if (r->rt != PF_DUPTO)
5021 if (ro == &iproute && ro->ro_rt)
5033 pf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
5037 struct route_in6 ip6route;
5038 struct route_in6 *ro;
5039 struct sockaddr_in6 *dst;
5040 struct ip6_hdr *ip6;
5041 struct ifnet *ifp = NULL;
5042 struct pf_addr naddr;
5043 struct pf_src_node *sn = NULL;
5046 if (m == NULL || *m == NULL || r == NULL ||
5047 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
5048 panic("pf_route6: invalid parameters");
5050 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
5051 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
5052 (*m)->m_pkthdr.pf_routed = 1;
5054 if ((*m)->m_pkthdr.pf_routed > 3) {
5059 (*m)->m_pkthdr.pf_routed++;
5062 if (r->rt == PF_DUPTO) {
5063 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
5066 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
5071 if (m0->m_len < sizeof(struct ip6_hdr))
5072 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5073 ip6 = mtod(m0, struct ip6_hdr *);
5076 bzero((caddr_t)ro, sizeof(*ro));
5077 dst = (struct sockaddr_in6 *)&ro->ro_dst;
5078 dst->sin6_family = AF_INET6;
5079 dst->sin6_len = sizeof(*dst);
5080 dst->sin6_addr = ip6->ip6_dst;
5083 if (r->rt == PF_FASTROUTE) {
5084 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
5085 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
5089 if (TAILQ_EMPTY(&r->rpool.list))
5090 panic("pf_route6: TAILQ_EMPTY(&r->rpool.list)");
5092 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
5094 if (!PF_AZERO(&naddr, AF_INET6))
5095 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5097 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
5099 if (!PF_AZERO(&s->rt_addr, AF_INET6))
5100 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5101 &s->rt_addr, AF_INET6);
5102 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5108 if (pf_test6(PF_OUT, ifp, &m0) != PF_PASS)
5110 else if (m0 == NULL)
5112 if (m0->m_len < sizeof(struct ip6_hdr))
5113 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5114 ip6 = mtod(m0, struct ip6_hdr *);
5118 * If the packet is too large for the outgoing interface,
5119 * send back an icmp6 error.
5121 if (IN6_IS_ADDR_LINKLOCAL(&dst->sin6_addr))
5122 dst->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
5123 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
5124 error = nd6_output(ifp, ifp, m0, dst, NULL);
5126 in6_ifstat_inc(ifp, ifs6_in_toobig);
5127 if (r->rt != PF_DUPTO)
5128 icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
5134 if (r->rt != PF_DUPTO)
5146 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
5147 * off is the offset where the protocol header starts
5148 * len is the total length of protocol header plus payload
5149 * returns 0 when the checksum is valid, otherwise returns 1.
5153 * FreeBSD supports cksum offload for the following drivers.
5154 * em(4), gx(4), lge(4), nge(4), ti(4), xl(4)
5155 * If we can make full use of it we would outperform ipfw/ipfilter in
5156 * very heavy traffic.
5157 * I have not tested 'cause I don't have NICs that supports cksum offload.
5158 * (There might be problems. Typical phenomena would be
5159 * 1. No route message for UDP packet.
5160 * 2. No connection acceptance from external hosts regardless of rule set.)
5163 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p,
5170 if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
5172 if (m->m_pkthdr.len < off + len)
5178 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
5179 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
5180 sum = m->m_pkthdr.csum_data;
5182 ip = mtod(m, struct ip *);
5183 sum = in_pseudo(ip->ip_src.s_addr,
5184 ip->ip_dst.s_addr, htonl((u_short)len +
5185 m->m_pkthdr.csum_data + p));
5193 case IPPROTO_ICMPV6:
5203 if (p == IPPROTO_ICMP) {
5208 sum = in_cksum(m, len);
5212 if (m->m_len < sizeof(struct ip))
5214 sum = in_cksum_range(m, p, off, len);
5216 m->m_pkthdr.csum_flags |=
5219 m->m_pkthdr.csum_data = 0xffff;
5225 if (m->m_len < sizeof(struct ip6_hdr))
5227 sum = in6_cksum(m, p, off, len);
5230 * IPv6 H/W cksum off-load not supported yet!
5233 * m->m_pkthdr.csum_flags |=
5234 * (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
5235 * m->m_pkthdr.csum_data = 0xffff;
5247 tcpstat.tcps_rcvbadsum++;
5250 udpstat.udps_badsum++;
5253 icmpstat.icps_checksum++;
5256 case IPPROTO_ICMPV6:
5257 icmp6stat.icp6s_checksum++;
5268 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0)
5270 struct pfi_kif *kif;
5271 u_short action, reason = 0, log = 0;
5272 struct mbuf *m = *m0;
5273 struct ip *h = NULL;
5274 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5275 struct pf_state *s = NULL;
5276 struct pf_ruleset *ruleset = NULL;
5278 int off, dirndx, pqid = 0;
5280 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5283 kif = pfi_index2kif[ifp->if_index];
5288 if ((m->m_flags & M_PKTHDR) == 0)
5289 panic("non-M_PKTHDR is passed to pf_test");
5292 memset(&pd, 0, sizeof(pd));
5293 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5295 REASON_SET(&reason, PFRES_SHORT);
5300 /* We do IP header normalization and packet reassembly here */
5301 if (pf_normalize_ip(m0, dir, kif, &reason) != PF_PASS) {
5306 h = mtod(m, struct ip *);
5308 off = h->ip_hl << 2;
5309 if (off < (int)sizeof(*h)) {
5311 REASON_SET(&reason, PFRES_SHORT);
5316 pd.src = (struct pf_addr *)&h->ip_src;
5317 pd.dst = (struct pf_addr *)&h->ip_dst;
5318 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET);
5319 pd.ip_sum = &h->ip_sum;
5323 pd.tot_len = h->ip_len;
5325 /* handle fragments that didn't get reassembled by normalization */
5326 if (h->ip_off & (IP_MF | IP_OFFMASK)) {
5327 action = pf_test_fragment(&r, dir, kif, m, h,
5338 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5339 &action, &reason, AF_INET)) {
5340 log = action != PF_PASS;
5343 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5344 h->ip_len - off, IPPROTO_TCP, AF_INET)) {
5348 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5349 if ((th.th_flags & TH_ACK) && pd.p_len == 0)
5351 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5352 if (action == PF_DROP)
5354 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5356 if (action == PF_PASS) {
5358 pfsync_update_state(s);
5363 } else if (s == NULL)
5364 action = pf_test_tcp(&r, &s, dir, kif,
5365 m, off, h, &pd, &a, &ruleset);
5373 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5374 &action, &reason, AF_INET)) {
5375 log = action != PF_PASS;
5378 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5379 off, h->ip_len - off, IPPROTO_UDP, AF_INET)) {
5383 if (uh.uh_dport == 0 ||
5384 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5385 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5389 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5390 if (action == PF_PASS) {
5392 pfsync_update_state(s);
5397 } else if (s == NULL)
5398 action = pf_test_udp(&r, &s, dir, kif,
5399 m, off, h, &pd, &a, &ruleset);
5403 case IPPROTO_ICMP: {
5407 if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
5408 &action, &reason, AF_INET)) {
5409 log = action != PF_PASS;
5412 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5413 h->ip_len - off, IPPROTO_ICMP, AF_INET)) {
5417 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd);
5418 if (action == PF_PASS) {
5420 pfsync_update_state(s);
5425 } else if (s == NULL)
5426 action = pf_test_icmp(&r, &s, dir, kif,
5427 m, off, h, &pd, &a, &ruleset);
5432 action = pf_test_state_other(&s, dir, kif, &pd);
5433 if (action == PF_PASS) {
5435 pfsync_update_state(s);
5440 } else if (s == NULL)
5441 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5447 if (action == PF_PASS && h->ip_hl > 5 &&
5448 !((s && s->allow_opts) || r->allow_opts)) {
5450 REASON_SET(&reason, PFRES_SHORT);
5452 DPFPRINTF(PF_DEBUG_MISC,
5453 ("pf: dropping packet with ip options\n"));
5457 if (action == PF_PASS && r->qid) {
5458 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5459 if (pd.tos == IPTOS_LOWDELAY)
5460 m->m_pkthdr.altq_qid = r->pqid;
5462 m->m_pkthdr.altq_qid = r->qid;
5463 m->m_pkthdr.ecn_af = AF_INET;
5464 m->m_pkthdr.header = h;
5469 * connections redirected to loopback should not match sockets
5470 * bound specifically to loopback due to security implications,
5471 * see tcp_input() and in_pcblookup_listen().
5473 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5474 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5475 (s->nat_rule.ptr->action == PF_RDR ||
5476 s->nat_rule.ptr->action == PF_BINAT) &&
5477 (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
5479 REASON_SET(&reason, PFRES_MEMORY);
5482 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5485 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, a, ruleset);
5487 kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5488 kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++;
5490 if (action == PF_PASS || r->action == PF_DROP) {
5492 r->bytes += pd.tot_len;
5495 a->bytes += pd.tot_len;
5498 dirndx = (dir == s->direction) ? 0 : 1;
5499 s->packets[dirndx]++;
5500 s->bytes[dirndx] += pd.tot_len;
5501 if (s->nat_rule.ptr != NULL) {
5502 s->nat_rule.ptr->packets++;
5503 s->nat_rule.ptr->bytes += pd.tot_len;
5505 if (s->src_node != NULL) {
5506 s->src_node->packets++;
5507 s->src_node->bytes += pd.tot_len;
5509 if (s->nat_src_node != NULL) {
5510 s->nat_src_node->packets++;
5511 s->nat_src_node->bytes += pd.tot_len;
5515 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5519 * XXX: we need to make sure that the addresses
5520 * passed to pfr_update_stats() are the same than
5521 * the addresses used during matching (pfr_match)
5523 if (r == &pf_default_rule) {
5525 x = (s == NULL || s->direction == dir) ?
5526 &pd.baddr : &pd.naddr;
5528 x = (s == NULL || s->direction == dir) ?
5529 &pd.naddr : &pd.baddr;
5530 if (x == &pd.baddr || s == NULL) {
5531 /* we need to change the address */
5538 if (tr->src.addr.type == PF_ADDR_TABLE)
5539 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5540 s->direction == dir) ? pd.src : pd.dst, pd.af,
5541 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5543 if (tr->dst.addr.type == PF_ADDR_TABLE)
5544 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5545 s->direction == dir) ? pd.dst : pd.src, pd.af,
5546 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5551 if (action == PF_SYNPROXY_DROP) {
5556 /* pf_route can free the mbuf causing *m0 to become NULL */
5557 pf_route(m0, r, dir, ifp, s);
5565 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0)
5567 struct pfi_kif *kif;
5568 u_short action, reason = 0, log = 0;
5569 struct mbuf *m = *m0;
5570 struct ip6_hdr *h = NULL;
5571 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5572 struct pf_state *s = NULL;
5573 struct pf_ruleset *ruleset = NULL;
5575 int off, terminal = 0, dirndx;
5577 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5580 kif = pfi_index2kif[ifp->if_index];
5585 if ((m->m_flags & M_PKTHDR) == 0)
5586 panic("non-M_PKTHDR is passed to pf_test");
5589 memset(&pd, 0, sizeof(pd));
5590 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5592 REASON_SET(&reason, PFRES_SHORT);
5597 /* We do IP header normalization and packet reassembly here */
5598 if (pf_normalize_ip6(m0, dir, kif, &reason) != PF_PASS) {
5603 h = mtod(m, struct ip6_hdr *);
5605 pd.src = (struct pf_addr *)&h->ip6_src;
5606 pd.dst = (struct pf_addr *)&h->ip6_dst;
5607 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET6);
5611 pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
5613 off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
5614 pd.proto = h->ip6_nxt;
5617 case IPPROTO_FRAGMENT:
5618 action = pf_test_fragment(&r, dir, kif, m, h,
5620 if (action == PF_DROP)
5621 REASON_SET(&reason, PFRES_FRAG);
5624 case IPPROTO_HOPOPTS:
5625 case IPPROTO_ROUTING:
5626 case IPPROTO_DSTOPTS: {
5627 /* get next header and header length */
5628 struct ip6_ext opt6;
5630 if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
5631 NULL, NULL, pd.af)) {
5632 DPFPRINTF(PF_DEBUG_MISC,
5633 ("pf: IPv6 short opt\n"));
5635 REASON_SET(&reason, PFRES_SHORT);
5639 if (pd.proto == IPPROTO_AH)
5640 off += (opt6.ip6e_len + 2) * 4;
5642 off += (opt6.ip6e_len + 1) * 8;
5643 pd.proto = opt6.ip6e_nxt;
5644 /* goto the next header */
5651 } while (!terminal);
5659 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5660 &action, &reason, AF_INET6)) {
5661 log = action != PF_PASS;
5664 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5665 ntohs(h->ip6_plen), IPPROTO_TCP, AF_INET6)) {
5669 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5670 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5671 if (action == PF_DROP)
5673 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5675 if (action == PF_PASS) {
5677 pfsync_update_state(s);
5682 } else if (s == NULL)
5683 action = pf_test_tcp(&r, &s, dir, kif,
5684 m, off, h, &pd, &a, &ruleset);
5692 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5693 &action, &reason, AF_INET6)) {
5694 log = action != PF_PASS;
5697 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5698 off, ntohs(h->ip6_plen), IPPROTO_UDP, AF_INET6)) {
5702 if (uh.uh_dport == 0 ||
5703 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5704 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5708 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5709 if (action == PF_PASS) {
5711 pfsync_update_state(s);
5716 } else if (s == NULL)
5717 action = pf_test_udp(&r, &s, dir, kif,
5718 m, off, h, &pd, &a, &ruleset);
5722 case IPPROTO_ICMPV6: {
5723 struct icmp6_hdr ih;
5726 if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
5727 &action, &reason, AF_INET6)) {
5728 log = action != PF_PASS;
5731 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5732 ntohs(h->ip6_plen), IPPROTO_ICMPV6, AF_INET6)) {
5736 action = pf_test_state_icmp(&s, dir, kif,
5738 if (action == PF_PASS) {
5740 pfsync_update_state(s);
5745 } else if (s == NULL)
5746 action = pf_test_icmp(&r, &s, dir, kif,
5747 m, off, h, &pd, &a, &ruleset);
5752 action = pf_test_state_other(&s, dir, kif, &pd);
5753 if (action == PF_PASS) {
5757 } else if (s == NULL)
5758 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5764 /* XXX handle IPv6 options, if not allowed. not implemented. */
5767 if (action == PF_PASS && r->qid) {
5768 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5769 if (pd.tos == IPTOS_LOWDELAY)
5770 m->m_pkthdr.altq_qid = r->pqid;
5772 m->m_pkthdr.altq_qid = r->qid;
5773 m->m_pkthdr.ecn_af = AF_INET6;
5774 m->m_pkthdr.header = h;
5778 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5779 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5780 (s->nat_rule.ptr->action == PF_RDR ||
5781 s->nat_rule.ptr->action == PF_BINAT) &&
5782 IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) {
5784 REASON_SET(&reason, PFRES_MEMORY);
5787 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5790 PFLOG_PACKET(kif, h, m, AF_INET6, dir, reason, r, a, ruleset);
5792 kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5793 kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++;
5795 if (action == PF_PASS || r->action == PF_DROP) {
5797 r->bytes += pd.tot_len;
5800 a->bytes += pd.tot_len;
5803 dirndx = (dir == s->direction) ? 0 : 1;
5804 s->packets[dirndx]++;
5805 s->bytes[dirndx] += pd.tot_len;
5806 if (s->nat_rule.ptr != NULL) {
5807 s->nat_rule.ptr->packets++;
5808 s->nat_rule.ptr->bytes += pd.tot_len;
5810 if (s->src_node != NULL) {
5811 s->src_node->packets++;
5812 s->src_node->bytes += pd.tot_len;
5814 if (s->nat_src_node != NULL) {
5815 s->nat_src_node->packets++;
5816 s->nat_src_node->bytes += pd.tot_len;
5820 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5824 * XXX: we need to make sure that the addresses
5825 * passed to pfr_update_stats() are the same than
5826 * the addresses used during matching (pfr_match)
5828 if (r == &pf_default_rule) {
5830 x = (s == NULL || s->direction == dir) ?
5831 &pd.baddr : &pd.naddr;
5833 x = (s == NULL || s->direction == dir) ?
5834 &pd.naddr : &pd.baddr;
5836 if (x == &pd.baddr || s == NULL) {
5843 if (tr->src.addr.type == PF_ADDR_TABLE)
5844 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5845 s->direction == dir) ? pd.src : pd.dst, pd.af,
5846 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5848 if (tr->dst.addr.type == PF_ADDR_TABLE)
5849 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5850 s->direction == dir) ? pd.dst : pd.src, pd.af,
5851 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5856 if (action == PF_SYNPROXY_DROP) {
5861 /* pf_route6 can free the mbuf causing *m0 to become NULL */
5862 pf_route6(m0, r, dir, ifp, s);