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.17 2008/04/07 00:43:44 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/netisr.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/tcp.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/tcp_timer.h>
79 #include <netinet/tcp_var.h>
80 #include <netinet/udp_var.h>
81 #include <netinet/icmp_var.h>
83 #include <net/pf/pfvar.h>
84 #include <net/pf/if_pflog.h>
87 #include <net/pf/if_pfsync.h>
88 #endif /* NPFSYNC > 0 */
91 #include <netinet/ip6.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/in6_pcb.h>
99 #include <sys/in_cksum.h>
100 #include <sys/ucred.h>
101 #include <machine/limits.h>
102 #include <sys/msgport2.h>
103 #include <net/netmsg2.h>
105 extern int ip_optcopy(struct ip *, struct ip *);
107 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
113 struct pf_anchorqueue pf_anchors;
114 struct pf_ruleset pf_main_ruleset;
115 struct pf_altqqueue pf_altqs[2];
116 struct pf_palist pf_pabuf;
117 struct pf_altqqueue *pf_altqs_active;
118 struct pf_altqqueue *pf_altqs_inactive;
119 struct pf_status pf_status;
121 u_int32_t ticket_altqs_active;
122 u_int32_t ticket_altqs_inactive;
123 int altqs_inactive_open;
124 u_int32_t ticket_pabuf;
126 struct callout pf_expire_to; /* expire timeout */
128 vm_zone_t pf_src_tree_pl, pf_rule_pl;
129 vm_zone_t pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
131 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
132 void pf_print_state(struct pf_state *);
133 void pf_print_flags(u_int8_t);
135 u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
137 void pf_change_ap(struct pf_addr *, u_int16_t *,
138 u_int16_t *, u_int16_t *, struct pf_addr *,
139 u_int16_t, u_int8_t, sa_family_t);
141 void pf_change_a6(struct pf_addr *, u_int16_t *,
142 struct pf_addr *, u_int8_t);
144 void pf_change_icmp(struct pf_addr *, u_int16_t *,
145 struct pf_addr *, struct pf_addr *, u_int16_t,
146 u_int16_t *, u_int16_t *, u_int16_t *,
147 u_int16_t *, u_int8_t, sa_family_t);
148 void pf_send_tcp(const struct pf_rule *, sa_family_t,
149 const struct pf_addr *, const struct pf_addr *,
150 u_int16_t, u_int16_t, u_int32_t, u_int32_t,
151 u_int8_t, u_int16_t, u_int16_t, u_int8_t);
152 void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
153 sa_family_t, struct pf_rule *);
154 struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *,
155 int, int, struct pfi_kif *,
156 struct pf_addr *, u_int16_t, struct pf_addr *,
158 struct pf_rule *pf_get_translation(struct pf_pdesc *, struct mbuf *,
159 int, int, struct pfi_kif *, struct pf_src_node **,
160 struct pf_addr *, u_int16_t,
161 struct pf_addr *, u_int16_t,
162 struct pf_addr *, u_int16_t *);
163 int pf_test_tcp(struct pf_rule **, struct pf_state **,
164 int, struct pfi_kif *, struct mbuf *, int,
165 void *, struct pf_pdesc *, struct pf_rule **,
166 struct pf_ruleset **);
167 int pf_test_udp(struct pf_rule **, struct pf_state **,
168 int, struct pfi_kif *, struct mbuf *, int,
169 void *, struct pf_pdesc *, struct pf_rule **,
170 struct pf_ruleset **);
171 int pf_test_icmp(struct pf_rule **, struct pf_state **,
172 int, struct pfi_kif *, struct mbuf *, int,
173 void *, struct pf_pdesc *, struct pf_rule **,
174 struct pf_ruleset **);
175 int pf_test_other(struct pf_rule **, struct pf_state **,
176 int, struct pfi_kif *, struct mbuf *, int, void *,
177 struct pf_pdesc *, struct pf_rule **,
178 struct pf_ruleset **);
179 int pf_test_fragment(struct pf_rule **, int,
180 struct pfi_kif *, struct mbuf *, void *,
181 struct pf_pdesc *, struct pf_rule **,
182 struct pf_ruleset **);
183 int pf_test_state_tcp(struct pf_state **, int,
184 struct pfi_kif *, struct mbuf *, int,
185 void *, struct pf_pdesc *, u_short *);
186 int pf_test_state_udp(struct pf_state **, int,
187 struct pfi_kif *, struct mbuf *, int,
188 void *, struct pf_pdesc *);
189 int pf_test_state_icmp(struct pf_state **, int,
190 struct pfi_kif *, struct mbuf *, int,
191 void *, struct pf_pdesc *);
192 int pf_test_state_other(struct pf_state **, int,
193 struct pfi_kif *, struct pf_pdesc *);
194 static int pf_match_tag(struct mbuf *, struct pf_rule *,
195 struct pf_rule *, int *);
196 void pf_hash(struct pf_addr *, struct pf_addr *,
197 struct pf_poolhashkey *, sa_family_t);
198 int pf_map_addr(u_int8_t, struct pf_rule *,
199 struct pf_addr *, struct pf_addr *,
200 struct pf_addr *, struct pf_src_node **);
201 int pf_get_sport(sa_family_t, u_int8_t, struct pf_rule *,
202 struct pf_addr *, struct pf_addr *, u_int16_t,
203 struct pf_addr *, u_int16_t*, u_int16_t, u_int16_t,
204 struct pf_src_node **);
205 void pf_route(struct mbuf **, struct pf_rule *, int,
206 struct ifnet *, struct pf_state *);
207 void pf_route6(struct mbuf **, struct pf_rule *, int,
208 struct ifnet *, struct pf_state *);
209 int pf_socket_lookup(uid_t *, gid_t *,
210 int, struct pf_pdesc *);
211 u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t,
213 u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t,
215 u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t,
217 void pf_set_rt_ifp(struct pf_state *,
219 int pf_check_proto_cksum(struct mbuf *, int, int,
220 u_int8_t, sa_family_t);
221 int pf_addr_wrap_neq(struct pf_addr_wrap *,
222 struct pf_addr_wrap *);
223 struct pf_state *pf_find_state_recurse(struct pfi_kif *,
224 struct pf_state *, u_int8_t);
226 struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
228 #define STATE_LOOKUP() \
230 if (direction == PF_IN) \
231 *state = pf_find_state_recurse( \
232 kif, &key, PF_EXT_GWY); \
234 *state = pf_find_state_recurse( \
235 kif, &key, PF_LAN_EXT); \
236 if (*state == NULL) \
238 if (direction == PF_OUT && \
239 (((*state)->rule.ptr->rt == PF_ROUTETO && \
240 (*state)->rule.ptr->direction == PF_OUT) || \
241 ((*state)->rule.ptr->rt == PF_REPLYTO && \
242 (*state)->rule.ptr->direction == PF_IN)) && \
243 (*state)->rt_kif != NULL && \
244 (*state)->rt_kif != kif) \
248 #define STATE_TRANSLATE(s) \
249 (s)->lan.addr.addr32[0] != (s)->gwy.addr.addr32[0] || \
250 ((s)->af == AF_INET6 && \
251 ((s)->lan.addr.addr32[1] != (s)->gwy.addr.addr32[1] || \
252 (s)->lan.addr.addr32[2] != (s)->gwy.addr.addr32[2] || \
253 (s)->lan.addr.addr32[3] != (s)->gwy.addr.addr32[3])) || \
254 (s)->lan.port != (s)->gwy.port
256 #define BOUND_IFACE(r, k) (((r)->rule_flag & PFRULE_IFBOUND) ? (k) : \
257 ((r)->rule_flag & PFRULE_GRBOUND) ? (k)->pfik_parent : \
258 (k)->pfik_parent->pfik_parent)
260 static int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
261 static int pf_state_compare_lan_ext(struct pf_state *,
263 static int pf_state_compare_ext_gwy(struct pf_state *,
265 static int pf_state_compare_id(struct pf_state *,
268 struct pf_src_tree tree_src_tracking;
270 struct pf_state_tree_id tree_id;
271 struct pf_state_queue state_updates;
273 RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
274 RB_GENERATE(pf_state_tree_lan_ext, pf_state,
275 u.s.entry_lan_ext, pf_state_compare_lan_ext);
276 RB_GENERATE(pf_state_tree_ext_gwy, pf_state,
277 u.s.entry_ext_gwy, pf_state_compare_ext_gwy);
278 RB_GENERATE(pf_state_tree_id, pf_state,
279 u.s.entry_id, pf_state_compare_id);
282 pf_src_compare(struct pf_src_node *a, struct pf_src_node *b)
286 if (a->rule.ptr > b->rule.ptr)
288 if (a->rule.ptr < b->rule.ptr)
290 if ((diff = a->af - b->af) != 0)
295 if (a->addr.addr32[0] > b->addr.addr32[0])
297 if (a->addr.addr32[0] < b->addr.addr32[0])
303 if (a->addr.addr32[3] > b->addr.addr32[3])
305 if (a->addr.addr32[3] < b->addr.addr32[3])
307 if (a->addr.addr32[2] > b->addr.addr32[2])
309 if (a->addr.addr32[2] < b->addr.addr32[2])
311 if (a->addr.addr32[1] > b->addr.addr32[1])
313 if (a->addr.addr32[1] < b->addr.addr32[1])
315 if (a->addr.addr32[0] > b->addr.addr32[0])
317 if (a->addr.addr32[0] < b->addr.addr32[0])
327 pf_state_hash(struct pf_state *s)
329 int hv = (intptr_t)s / sizeof(*s);
331 hv ^= crc32(&s->lan, sizeof(s->lan));
332 hv ^= crc32(&s->gwy, sizeof(s->gwy));
333 hv ^= crc32(&s->ext, sizeof(s->ext));
338 pf_state_compare_lan_ext(struct pf_state *a, struct pf_state *b)
342 if ((diff = a->proto - b->proto) != 0)
344 if ((diff = a->af - b->af) != 0)
349 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
351 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
353 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
355 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
361 if (a->lan.addr.addr32[3] > b->lan.addr.addr32[3])
363 if (a->lan.addr.addr32[3] < b->lan.addr.addr32[3])
365 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
367 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
369 if (a->lan.addr.addr32[2] > b->lan.addr.addr32[2])
371 if (a->lan.addr.addr32[2] < b->lan.addr.addr32[2])
373 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
375 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
377 if (a->lan.addr.addr32[1] > b->lan.addr.addr32[1])
379 if (a->lan.addr.addr32[1] < b->lan.addr.addr32[1])
381 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
383 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
385 if (a->lan.addr.addr32[0] > b->lan.addr.addr32[0])
387 if (a->lan.addr.addr32[0] < b->lan.addr.addr32[0])
389 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
391 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
397 if ((diff = a->lan.port - b->lan.port) != 0)
399 if ((diff = a->ext.port - b->ext.port) != 0)
406 pf_state_compare_ext_gwy(struct pf_state *a, struct pf_state *b)
410 if ((diff = a->proto - b->proto) != 0)
412 if ((diff = a->af - b->af) != 0)
417 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
419 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
421 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
423 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
429 if (a->ext.addr.addr32[3] > b->ext.addr.addr32[3])
431 if (a->ext.addr.addr32[3] < b->ext.addr.addr32[3])
433 if (a->gwy.addr.addr32[3] > b->gwy.addr.addr32[3])
435 if (a->gwy.addr.addr32[3] < b->gwy.addr.addr32[3])
437 if (a->ext.addr.addr32[2] > b->ext.addr.addr32[2])
439 if (a->ext.addr.addr32[2] < b->ext.addr.addr32[2])
441 if (a->gwy.addr.addr32[2] > b->gwy.addr.addr32[2])
443 if (a->gwy.addr.addr32[2] < b->gwy.addr.addr32[2])
445 if (a->ext.addr.addr32[1] > b->ext.addr.addr32[1])
447 if (a->ext.addr.addr32[1] < b->ext.addr.addr32[1])
449 if (a->gwy.addr.addr32[1] > b->gwy.addr.addr32[1])
451 if (a->gwy.addr.addr32[1] < b->gwy.addr.addr32[1])
453 if (a->ext.addr.addr32[0] > b->ext.addr.addr32[0])
455 if (a->ext.addr.addr32[0] < b->ext.addr.addr32[0])
457 if (a->gwy.addr.addr32[0] > b->gwy.addr.addr32[0])
459 if (a->gwy.addr.addr32[0] < b->gwy.addr.addr32[0])
465 if ((diff = a->ext.port - b->ext.port) != 0)
467 if ((diff = a->gwy.port - b->gwy.port) != 0)
474 pf_state_compare_id(struct pf_state *a, struct pf_state *b)
480 if (a->creatorid > b->creatorid)
482 if (a->creatorid < b->creatorid)
490 pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
495 dst->addr32[0] = src->addr32[0];
499 dst->addr32[0] = src->addr32[0];
500 dst->addr32[1] = src->addr32[1];
501 dst->addr32[2] = src->addr32[2];
502 dst->addr32[3] = src->addr32[3];
509 pf_find_state_byid(struct pf_state *key)
511 pf_status.fcounters[FCNT_STATE_SEARCH]++;
512 return (RB_FIND(pf_state_tree_id, &tree_id, key));
516 pf_find_state_recurse(struct pfi_kif *kif, struct pf_state *key, u_int8_t tree)
520 pf_status.fcounters[FCNT_STATE_SEARCH]++;
524 for (; kif != NULL; kif = kif->pfik_parent) {
525 s = RB_FIND(pf_state_tree_lan_ext,
526 &kif->pfik_lan_ext, key);
532 for (; kif != NULL; kif = kif->pfik_parent) {
533 s = RB_FIND(pf_state_tree_ext_gwy,
534 &kif->pfik_ext_gwy, key);
540 panic("pf_find_state_recurse");
545 pf_find_state_all(struct pf_state *key, u_int8_t tree, int *more)
547 struct pf_state *s, *ss = NULL;
550 pf_status.fcounters[FCNT_STATE_SEARCH]++;
554 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
555 s = RB_FIND(pf_state_tree_lan_ext,
556 &kif->pfik_lan_ext, key);
566 TAILQ_FOREACH(kif, &pfi_statehead, pfik_w_states) {
567 s = RB_FIND(pf_state_tree_ext_gwy,
568 &kif->pfik_ext_gwy, key);
578 panic("pf_find_state_all");
583 pf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule,
584 struct pf_addr *src, sa_family_t af)
586 struct pf_src_node k;
590 PF_ACPY(&k.addr, src, af);
591 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
592 rule->rpool.opts & PF_POOL_STICKYADDR)
596 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
597 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
600 if (!rule->max_src_nodes ||
601 rule->src_nodes < rule->max_src_nodes)
602 (*sn) = pool_get(&pf_src_tree_pl, PR_NOWAIT);
605 bzero(*sn, sizeof(struct pf_src_node));
607 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
608 rule->rpool.opts & PF_POOL_STICKYADDR)
609 (*sn)->rule.ptr = rule;
611 (*sn)->rule.ptr = NULL;
612 PF_ACPY(&(*sn)->addr, src, af);
613 if (RB_INSERT(pf_src_tree,
614 &tree_src_tracking, *sn) != NULL) {
615 if (pf_status.debug >= PF_DEBUG_MISC) {
616 kprintf("pf: src_tree insert failed: ");
617 pf_print_host(&(*sn)->addr, 0, af);
620 pool_put(&pf_src_tree_pl, *sn);
623 (*sn)->creation = time_second;
624 (*sn)->ruletype = rule->action;
625 if ((*sn)->rule.ptr != NULL)
626 (*sn)->rule.ptr->src_nodes++;
627 pf_status.scounters[SCNT_SRC_NODE_INSERT]++;
628 pf_status.src_nodes++;
630 if (rule->max_src_states &&
631 (*sn)->states >= rule->max_src_states)
638 pf_insert_state(struct pfi_kif *kif, struct pf_state *state)
640 /* Thou MUST NOT insert multiple duplicate keys */
641 state->u.s.kif = kif;
642 if (RB_INSERT(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state)) {
643 if (pf_status.debug >= PF_DEBUG_MISC) {
644 kprintf("pf: state insert failed: tree_lan_ext");
646 pf_print_host(&state->lan.addr, state->lan.port,
649 pf_print_host(&state->gwy.addr, state->gwy.port,
652 pf_print_host(&state->ext.addr, state->ext.port,
654 if (state->sync_flags & PFSTATE_FROMSYNC)
655 kprintf(" (from sync)");
661 if (RB_INSERT(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state)) {
662 if (pf_status.debug >= PF_DEBUG_MISC) {
663 kprintf("pf: state insert failed: tree_ext_gwy");
665 pf_print_host(&state->lan.addr, state->lan.port,
668 pf_print_host(&state->gwy.addr, state->gwy.port,
671 pf_print_host(&state->ext.addr, state->ext.port,
673 if (state->sync_flags & PFSTATE_FROMSYNC)
674 kprintf(" (from sync)");
677 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
681 if (state->id == 0 && state->creatorid == 0) {
682 state->id = htobe64(pf_status.stateid++);
683 state->creatorid = pf_status.hostid;
685 if (RB_INSERT(pf_state_tree_id, &tree_id, state) != NULL) {
686 if (pf_status.debug >= PF_DEBUG_MISC) {
687 kprintf("pf: state insert failed: "
688 "id: %016" PRIx64 " creatorid: %08" PRIx32,
689 be64toh(state->id), ntohl(state->creatorid));
690 if (state->sync_flags & PFSTATE_FROMSYNC)
691 kprintf(" (from sync)");
694 RB_REMOVE(pf_state_tree_lan_ext, &kif->pfik_lan_ext, state);
695 RB_REMOVE(pf_state_tree_ext_gwy, &kif->pfik_ext_gwy, state);
698 TAILQ_INSERT_HEAD(&state_updates, state, u.s.entry_updates);
700 pf_status.fcounters[FCNT_STATE_INSERT]++;
702 pfi_attach_state(kif);
704 pfsync_insert_state(state);
710 pf_purge_timeout(void *arg)
712 struct callout *to = arg;
715 pf_purge_expired_states();
716 pf_purge_expired_fragments();
717 pf_purge_expired_src_nodes();
720 callout_reset(to, pf_default_rule.timeout[PFTM_INTERVAL] * hz,
721 pf_purge_timeout, to);
725 pf_state_expires(const struct pf_state *state)
732 /* handle all PFTM_* > PFTM_MAX here */
733 if (state->timeout == PFTM_PURGE)
734 return (time_second);
735 if (state->timeout == PFTM_UNTIL_PACKET)
737 KASSERT((state->timeout < PFTM_MAX),
738 ("pf_state_expires: timeout > PFTM_MAX"));
739 timeout = state->rule.ptr->timeout[state->timeout];
741 timeout = pf_default_rule.timeout[state->timeout];
742 start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
744 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
745 states = state->rule.ptr->states;
747 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
748 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
749 states = pf_status.states;
751 if (end && states > start && start < end) {
753 return (state->expire + timeout * (end - states) /
756 return (time_second);
758 return (state->expire + timeout);
762 pf_purge_expired_src_nodes(void)
764 struct pf_src_node *cur, *next;
766 for (cur = RB_MIN(pf_src_tree, &tree_src_tracking); cur; cur = next) {
767 next = RB_NEXT(pf_src_tree, &tree_src_tracking, cur);
769 if (cur->states <= 0 && cur->expire <= time_second) {
770 if (cur->rule.ptr != NULL) {
771 cur->rule.ptr->src_nodes--;
772 if (cur->rule.ptr->states <= 0 &&
773 cur->rule.ptr->max_src_nodes <= 0)
774 pf_rm_rule(NULL, cur->rule.ptr);
776 RB_REMOVE(pf_src_tree, &tree_src_tracking, cur);
777 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
778 pf_status.src_nodes--;
779 pool_put(&pf_src_tree_pl, cur);
785 pf_src_tree_remove_state(struct pf_state *s)
789 if (s->src_node != NULL) {
790 if (--s->src_node->states <= 0) {
791 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
794 pf_default_rule.timeout[PFTM_SRC_NODE];
795 s->src_node->expire = time_second + timeout;
798 if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
799 if (--s->nat_src_node->states <= 0) {
800 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
803 pf_default_rule.timeout[PFTM_SRC_NODE];
804 s->nat_src_node->expire = time_second + timeout;
807 s->src_node = s->nat_src_node = NULL;
811 pf_purge_expired_states_callback(struct pf_state *cur, void *data __unused)
813 if (pf_state_expires(cur) <= time_second) {
814 RB_REMOVE(pf_state_tree_ext_gwy,
815 &cur->u.s.kif->pfik_ext_gwy, cur);
816 RB_REMOVE(pf_state_tree_lan_ext,
817 &cur->u.s.kif->pfik_lan_ext, cur);
818 RB_REMOVE(pf_state_tree_id, &tree_id, cur);
819 if (cur->src.state == PF_TCPS_PROXY_DST) {
820 pf_send_tcp(cur->rule.ptr, cur->af,
821 &cur->ext.addr, &cur->lan.addr,
822 cur->ext.port, cur->lan.port,
823 cur->src.seqhi, cur->src.seqlo + 1, 0,
824 TH_RST|TH_ACK, 0, 0);
827 pfsync_delete_state(cur);
829 pf_src_tree_remove_state(cur);
830 if (--cur->rule.ptr->states <= 0 &&
831 cur->rule.ptr->src_nodes <= 0)
832 pf_rm_rule(NULL, cur->rule.ptr);
833 if (cur->nat_rule.ptr != NULL)
834 if (--cur->nat_rule.ptr->states <= 0 &&
835 cur->nat_rule.ptr->src_nodes <= 0)
836 pf_rm_rule(NULL, cur->nat_rule.ptr);
837 if (cur->anchor.ptr != NULL)
838 if (--cur->anchor.ptr->states <= 0)
839 pf_rm_rule(NULL, cur->anchor.ptr);
840 pf_normalize_tcp_cleanup(cur);
841 pfi_detach_state(cur->u.s.kif);
842 TAILQ_REMOVE(&state_updates, cur, u.s.entry_updates);
843 pool_put(&pf_state_pl, cur);
844 pf_status.fcounters[FCNT_STATE_REMOVALS]++;
851 pf_purge_expired_states(void)
853 RB_SCAN(pf_state_tree_id, &tree_id, NULL,
854 pf_purge_expired_states_callback, NULL);
859 pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
861 if (aw->type != PF_ADDR_TABLE)
863 if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
869 pf_tbladdr_remove(struct pf_addr_wrap *aw)
871 if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
873 pfr_detach_table(aw->p.tbl);
878 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
880 struct pfr_ktable *kt = aw->p.tbl;
882 if (aw->type != PF_ADDR_TABLE || kt == NULL)
884 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
887 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
892 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
897 u_int32_t a = ntohl(addr->addr32[0]);
898 kprintf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
910 u_int8_t i, curstart = 255, curend = 0,
911 maxstart = 0, maxend = 0;
912 for (i = 0; i < 8; i++) {
913 if (!addr->addr16[i]) {
920 if ((curend - curstart) >
921 (maxend - maxstart)) {
929 for (i = 0; i < 8; i++) {
930 if (i >= maxstart && i <= maxend) {
939 b = ntohs(addr->addr16[i]);
956 pf_print_state(struct pf_state *s)
972 kprintf("%u ", s->proto);
975 pf_print_host(&s->lan.addr, s->lan.port, s->af);
977 pf_print_host(&s->gwy.addr, s->gwy.port, s->af);
979 pf_print_host(&s->ext.addr, s->ext.port, s->af);
980 kprintf(" [lo=%u high=%u win=%u modulator=%u", s->src.seqlo,
981 s->src.seqhi, s->src.max_win, s->src.seqdiff);
982 if (s->src.wscale && s->dst.wscale)
983 kprintf(" wscale=%u", s->src.wscale & PF_WSCALE_MASK);
985 kprintf(" [lo=%u high=%u win=%u modulator=%u", s->dst.seqlo,
986 s->dst.seqhi, s->dst.max_win, s->dst.seqdiff);
987 if (s->src.wscale && s->dst.wscale)
988 kprintf(" wscale=%u", s->dst.wscale & PF_WSCALE_MASK);
990 kprintf(" %u:%u", s->src.state, s->dst.state);
994 pf_print_flags(u_int8_t f)
1016 #define PF_SET_SKIP_STEPS(i) \
1018 while (head[i] != cur) { \
1019 head[i]->skip[i].ptr = cur; \
1020 head[i] = TAILQ_NEXT(head[i], entries); \
1025 pf_calc_skip_steps(struct pf_rulequeue *rules)
1027 struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1030 cur = TAILQ_FIRST(rules);
1032 for (i = 0; i < PF_SKIP_COUNT; ++i)
1034 while (cur != NULL) {
1036 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1037 PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1038 if (cur->direction != prev->direction)
1039 PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1040 if (cur->af != prev->af)
1041 PF_SET_SKIP_STEPS(PF_SKIP_AF);
1042 if (cur->proto != prev->proto)
1043 PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
1044 if (cur->src.not != prev->src.not ||
1045 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1046 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1047 if (cur->src.port[0] != prev->src.port[0] ||
1048 cur->src.port[1] != prev->src.port[1] ||
1049 cur->src.port_op != prev->src.port_op)
1050 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
1051 if (cur->dst.not != prev->dst.not ||
1052 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1053 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1054 if (cur->dst.port[0] != prev->dst.port[0] ||
1055 cur->dst.port[1] != prev->dst.port[1] ||
1056 cur->dst.port_op != prev->dst.port_op)
1057 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1060 cur = TAILQ_NEXT(cur, entries);
1062 for (i = 0; i < PF_SKIP_COUNT; ++i)
1063 PF_SET_SKIP_STEPS(i);
1067 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1069 if (aw1->type != aw2->type)
1071 switch (aw1->type) {
1072 case PF_ADDR_ADDRMASK:
1073 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1075 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1078 case PF_ADDR_DYNIFTL:
1079 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1080 case PF_ADDR_NOROUTE:
1083 return (aw1->p.tbl != aw2->p.tbl);
1085 kprintf("invalid address type: %d\n", aw1->type);
1091 pf_update_anchor_rules(void)
1093 struct pf_rule *rule;
1096 for (i = 0; i < PF_RULESET_MAX; ++i)
1097 TAILQ_FOREACH(rule, pf_main_ruleset.rules[i].active.ptr,
1099 if (rule->anchorname[0])
1100 rule->anchor = pf_find_anchor(rule->anchorname);
1102 rule->anchor = NULL;
1106 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1112 l = cksum + old - new;
1113 l = (l >> 16) + (l & 65535);
1121 pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1122 struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1127 PF_ACPY(&ao, a, af);
1135 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1136 ao.addr16[0], an->addr16[0], 0),
1137 ao.addr16[1], an->addr16[1], 0);
1139 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1140 ao.addr16[0], an->addr16[0], u),
1141 ao.addr16[1], an->addr16[1], u),
1147 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1148 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1149 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1150 ao.addr16[0], an->addr16[0], u),
1151 ao.addr16[1], an->addr16[1], u),
1152 ao.addr16[2], an->addr16[2], u),
1153 ao.addr16[3], an->addr16[3], u),
1154 ao.addr16[4], an->addr16[4], u),
1155 ao.addr16[5], an->addr16[5], u),
1156 ao.addr16[6], an->addr16[6], u),
1157 ao.addr16[7], an->addr16[7], u),
1165 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1167 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1171 memcpy(&ao, a, sizeof(ao));
1172 memcpy(a, &an, sizeof(u_int32_t));
1173 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1174 ao % 65536, an % 65536, u);
1179 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1183 PF_ACPY(&ao, a, AF_INET6);
1184 PF_ACPY(a, an, AF_INET6);
1186 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1187 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1188 pf_cksum_fixup(pf_cksum_fixup(*c,
1189 ao.addr16[0], an->addr16[0], u),
1190 ao.addr16[1], an->addr16[1], u),
1191 ao.addr16[2], an->addr16[2], u),
1192 ao.addr16[3], an->addr16[3], u),
1193 ao.addr16[4], an->addr16[4], u),
1194 ao.addr16[5], an->addr16[5], u),
1195 ao.addr16[6], an->addr16[6], u),
1196 ao.addr16[7], an->addr16[7], u);
1201 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1202 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1203 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1205 struct pf_addr oia, ooa;
1207 PF_ACPY(&oia, ia, af);
1208 PF_ACPY(&ooa, oa, af);
1210 /* Change inner protocol port, fix inner protocol checksum. */
1212 u_int16_t oip = *ip;
1219 *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1220 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1222 *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1224 /* Change inner ip address, fix inner ip and icmp checksums. */
1225 PF_ACPY(ia, na, af);
1229 u_int32_t oh2c = *h2c;
1231 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1232 oia.addr16[0], ia->addr16[0], 0),
1233 oia.addr16[1], ia->addr16[1], 0);
1234 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1235 oia.addr16[0], ia->addr16[0], 0),
1236 oia.addr16[1], ia->addr16[1], 0);
1237 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
1243 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1244 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1245 pf_cksum_fixup(pf_cksum_fixup(*ic,
1246 oia.addr16[0], ia->addr16[0], u),
1247 oia.addr16[1], ia->addr16[1], u),
1248 oia.addr16[2], ia->addr16[2], u),
1249 oia.addr16[3], ia->addr16[3], u),
1250 oia.addr16[4], ia->addr16[4], u),
1251 oia.addr16[5], ia->addr16[5], u),
1252 oia.addr16[6], ia->addr16[6], u),
1253 oia.addr16[7], ia->addr16[7], u);
1257 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1258 PF_ACPY(oa, na, af);
1262 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
1263 ooa.addr16[0], oa->addr16[0], 0),
1264 ooa.addr16[1], oa->addr16[1], 0);
1269 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1270 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1271 pf_cksum_fixup(pf_cksum_fixup(*ic,
1272 ooa.addr16[0], oa->addr16[0], u),
1273 ooa.addr16[1], oa->addr16[1], u),
1274 ooa.addr16[2], oa->addr16[2], u),
1275 ooa.addr16[3], oa->addr16[3], u),
1276 ooa.addr16[4], oa->addr16[4], u),
1277 ooa.addr16[5], oa->addr16[5], u),
1278 ooa.addr16[6], oa->addr16[6], u),
1279 ooa.addr16[7], oa->addr16[7], u);
1286 pf_send_tcp(const struct pf_rule *r, sa_family_t af,
1287 const struct pf_addr *saddr, const struct pf_addr *daddr,
1288 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
1289 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl)
1294 struct ip *h = NULL;
1297 struct ip6_hdr *h6 = NULL;
1299 struct tcphdr *th = NULL;
1302 /* maximum segment size tcp option */
1303 tlen = sizeof(struct tcphdr);
1310 len = sizeof(struct ip) + tlen;
1315 len = sizeof(struct ip6_hdr) + tlen;
1320 /* create outgoing mbuf */
1321 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1324 m->m_pkthdr.fw_flags = PF_MBUF_GENERATED;
1326 if (r != NULL && r->qid) {
1327 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1328 m->m_pkthdr.altq_qid = r->qid;
1329 m->m_pkthdr.ecn_af = af;
1330 m->m_pkthdr.header = mtod(m, struct ip *);
1333 m->m_data += max_linkhdr;
1334 m->m_pkthdr.len = m->m_len = len;
1335 m->m_pkthdr.rcvif = NULL;
1336 bzero(m->m_data, len);
1340 h = mtod(m, struct ip *);
1342 /* IP header fields included in the TCP checksum */
1343 h->ip_p = IPPROTO_TCP;
1345 h->ip_src.s_addr = saddr->v4.s_addr;
1346 h->ip_dst.s_addr = daddr->v4.s_addr;
1348 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
1353 h6 = mtod(m, struct ip6_hdr *);
1355 /* IP header fields included in the TCP checksum */
1356 h6->ip6_nxt = IPPROTO_TCP;
1357 h6->ip6_plen = htons(tlen);
1358 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
1359 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
1361 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
1367 th->th_sport = sport;
1368 th->th_dport = dport;
1369 th->th_seq = htonl(seq);
1370 th->th_ack = htonl(ack);
1371 th->th_off = tlen >> 2;
1372 th->th_flags = flags;
1373 th->th_win = htons(win);
1376 opt = (char *)(th + 1);
1377 opt[0] = TCPOPT_MAXSEG;
1380 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
1387 th->th_sum = in_cksum(m, len);
1389 /* Finish the IP header */
1391 h->ip_hl = sizeof(*h) >> 2;
1392 h->ip_tos = IPTOS_LOWDELAY;
1394 h->ip_off = path_mtu_discovery ? IP_DF : 0;
1395 h->ip_ttl = ttl ? ttl : ip_defttl;
1397 ip_output(m, (void *)NULL, (void *)NULL, 0, (void *)NULL,
1404 th->th_sum = in6_cksum(m, IPPROTO_TCP,
1405 sizeof(struct ip6_hdr), tlen);
1407 h6->ip6_vfc |= IPV6_VERSION;
1408 h6->ip6_hlim = IPV6_DEFHLIM;
1410 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1417 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
1422 m0 = m_copypacket(m, MB_DONTWAIT);
1425 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
1429 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
1430 m->m_pkthdr.altq_qid = r->qid;
1431 m->m_pkthdr.ecn_af = af;
1432 m->m_pkthdr.header = mtod(m0, struct ip *);
1439 icmp_error(m0, type, code, 0, 0);
1444 icmp6_error(m0, type, code, 0);
1451 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
1452 * If n is 0, they match if they are equal. If n is != 0, they match if they
1456 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
1457 struct pf_addr *b, sa_family_t af)
1464 if ((a->addr32[0] & m->addr32[0]) ==
1465 (b->addr32[0] & m->addr32[0]))
1471 if (((a->addr32[0] & m->addr32[0]) ==
1472 (b->addr32[0] & m->addr32[0])) &&
1473 ((a->addr32[1] & m->addr32[1]) ==
1474 (b->addr32[1] & m->addr32[1])) &&
1475 ((a->addr32[2] & m->addr32[2]) ==
1476 (b->addr32[2] & m->addr32[2])) &&
1477 ((a->addr32[3] & m->addr32[3]) ==
1478 (b->addr32[3] & m->addr32[3])))
1497 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
1501 return ((p > a1) && (p < a2));
1503 return ((p < a1) || (p > a2));
1505 return ((p >= a1) && (p <= a2));
1519 return (0); /* never reached */
1523 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
1528 return (pf_match(op, a1, a2, p));
1532 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
1534 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1536 return (pf_match(op, a1, a2, u));
1540 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
1542 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
1544 return (pf_match(op, a1, a2, g));
1548 pf_match_tag(struct mbuf *m, struct pf_rule *r, struct pf_rule *nat_rule,
1551 if (*tag == -1) { /* find mbuf tag */
1552 if (nat_rule != NULL && nat_rule->tag)
1553 *tag = nat_rule->tag;
1554 else if (m->m_pkthdr.fw_flags & PF_MBUF_TAGGED)
1555 *tag = m->m_pkthdr.pf_tag;
1560 return ((!r->match_tag_not && r->match_tag == *tag) ||
1561 (r->match_tag_not && r->match_tag != *tag));
1565 pf_tag_packet(struct mbuf *m, int tag)
1570 m->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
1571 m->m_pkthdr.pf_tag = tag;
1574 #define PF_STEP_INTO_ANCHOR(r, a, s, n) \
1576 if ((r) == NULL || (r)->anchor == NULL || \
1577 (s) != NULL || (a) != NULL) \
1578 panic("PF_STEP_INTO_ANCHOR"); \
1580 (s) = TAILQ_FIRST(&(r)->anchor->rulesets); \
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); \
1591 #define PF_STEP_OUT_OF_ANCHOR(r, a, s, n) \
1593 if ((r) != NULL || (a) == NULL || (s) == NULL) \
1594 panic("PF_STEP_OUT_OF_ANCHOR"); \
1595 (s) = TAILQ_NEXT((s), entries); \
1596 while ((s) != NULL && ((r) = \
1597 TAILQ_FIRST((s)->rules[n].active.ptr)) == NULL) \
1598 (s) = TAILQ_NEXT((s), entries); \
1599 if ((r) == NULL) { \
1600 (r) = TAILQ_NEXT((a), entries); \
1607 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
1608 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
1613 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1614 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1618 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
1619 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
1620 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
1621 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
1622 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
1623 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
1624 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
1625 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
1631 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
1636 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
1640 if (addr->addr32[3] == 0xffffffff) {
1641 addr->addr32[3] = 0;
1642 if (addr->addr32[2] == 0xffffffff) {
1643 addr->addr32[2] = 0;
1644 if (addr->addr32[1] == 0xffffffff) {
1645 addr->addr32[1] = 0;
1647 htonl(ntohl(addr->addr32[0]) + 1);
1650 htonl(ntohl(addr->addr32[1]) + 1);
1653 htonl(ntohl(addr->addr32[2]) + 1);
1656 htonl(ntohl(addr->addr32[3]) + 1);
1662 #define mix(a,b,c) \
1664 a -= b; a -= c; a ^= (c >> 13); \
1665 b -= c; b -= a; b ^= (a << 8); \
1666 c -= a; c -= b; c ^= (b >> 13); \
1667 a -= b; a -= c; a ^= (c >> 12); \
1668 b -= c; b -= a; b ^= (a << 16); \
1669 c -= a; c -= b; c ^= (b >> 5); \
1670 a -= b; a -= c; a ^= (c >> 3); \
1671 b -= c; b -= a; b ^= (a << 10); \
1672 c -= a; c -= b; c ^= (b >> 15); \
1676 * hash function based on bridge_hash in if_bridge.c
1679 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
1680 struct pf_poolhashkey *key, sa_family_t af)
1682 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
1687 a += inaddr->addr32[0];
1690 hash->addr32[0] = c + key->key32[2];
1695 a += inaddr->addr32[0];
1696 b += inaddr->addr32[2];
1698 hash->addr32[0] = c;
1699 a += inaddr->addr32[1];
1700 b += inaddr->addr32[3];
1703 hash->addr32[1] = c;
1704 a += inaddr->addr32[2];
1705 b += inaddr->addr32[1];
1708 hash->addr32[2] = c;
1709 a += inaddr->addr32[3];
1710 b += inaddr->addr32[0];
1713 hash->addr32[3] = c;
1720 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
1721 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
1723 unsigned char hash[16];
1724 struct pf_pool *rpool = &r->rpool;
1725 struct pf_addr *raddr = &rpool->cur->addr.v.a.addr;
1726 struct pf_addr *rmask = &rpool->cur->addr.v.a.mask;
1727 struct pf_pooladdr *acur = rpool->cur;
1728 struct pf_src_node k;
1730 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
1731 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1733 PF_ACPY(&k.addr, saddr, af);
1734 if (r->rule_flag & PFRULE_RULESRCTRACK ||
1735 r->rpool.opts & PF_POOL_STICKYADDR)
1739 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
1740 *sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
1741 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
1742 PF_ACPY(naddr, &(*sn)->raddr, af);
1743 if (pf_status.debug >= PF_DEBUG_MISC) {
1744 kprintf("pf_map_addr: src tracking maps ");
1745 pf_print_host(&k.addr, 0, af);
1747 pf_print_host(naddr, 0, af);
1754 if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
1756 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1757 if (af == AF_INET) {
1758 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
1759 (rpool->opts & PF_POOL_TYPEMASK) !=
1762 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
1763 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
1765 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
1766 (rpool->opts & PF_POOL_TYPEMASK) !=
1769 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
1770 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
1772 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1773 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
1774 return (1); /* unsupported */
1776 raddr = &rpool->cur->addr.v.a.addr;
1777 rmask = &rpool->cur->addr.v.a.mask;
1780 switch (rpool->opts & PF_POOL_TYPEMASK) {
1782 PF_ACPY(naddr, raddr, af);
1784 case PF_POOL_BITMASK:
1785 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
1787 case PF_POOL_RANDOM:
1788 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
1792 rpool->counter.addr32[0] = karc4random();
1797 if (rmask->addr32[3] != 0xffffffff)
1798 rpool->counter.addr32[3] = karc4random();
1801 if (rmask->addr32[2] != 0xffffffff)
1802 rpool->counter.addr32[2] = karc4random();
1805 if (rmask->addr32[1] != 0xffffffff)
1806 rpool->counter.addr32[1] = karc4random();
1809 if (rmask->addr32[0] != 0xffffffff)
1810 rpool->counter.addr32[0] = karc4random();
1814 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1815 PF_ACPY(init_addr, naddr, af);
1818 PF_AINC(&rpool->counter, af);
1819 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
1822 case PF_POOL_SRCHASH:
1823 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
1824 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
1826 case PF_POOL_ROUNDROBIN:
1827 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1828 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
1829 &rpool->tblidx, &rpool->counter,
1830 &raddr, &rmask, af))
1832 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1833 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1834 &rpool->tblidx, &rpool->counter,
1835 &raddr, &rmask, af))
1837 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
1841 if ((rpool->cur = TAILQ_NEXT(rpool->cur, entries)) == NULL)
1842 rpool->cur = TAILQ_FIRST(&rpool->list);
1843 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
1845 if (pfr_pool_get(rpool->cur->addr.p.tbl,
1846 &rpool->tblidx, &rpool->counter,
1847 &raddr, &rmask, af)) {
1848 /* table contains no address of type 'af' */
1849 if (rpool->cur != acur)
1853 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
1855 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
1856 &rpool->tblidx, &rpool->counter,
1857 &raddr, &rmask, af)) {
1858 /* table contains no address of type 'af' */
1859 if (rpool->cur != acur)
1864 raddr = &rpool->cur->addr.v.a.addr;
1865 rmask = &rpool->cur->addr.v.a.mask;
1866 PF_ACPY(&rpool->counter, raddr, af);
1870 PF_ACPY(naddr, &rpool->counter, af);
1871 PF_AINC(&rpool->counter, af);
1875 PF_ACPY(&(*sn)->raddr, naddr, af);
1877 if (pf_status.debug >= PF_DEBUG_MISC &&
1878 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
1879 kprintf("pf_map_addr: selected address ");
1880 pf_print_host(naddr, 0, af);
1888 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
1889 struct pf_addr *saddr, struct pf_addr *daddr, u_int16_t dport,
1890 struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
1891 struct pf_src_node **sn)
1893 struct pf_state key;
1894 struct pf_addr init_addr;
1897 bzero(&init_addr, sizeof(init_addr));
1898 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1904 PF_ACPY(&key.ext.addr, daddr, key.af);
1905 PF_ACPY(&key.gwy.addr, naddr, key.af);
1906 key.ext.port = dport;
1909 * port search; start random, step;
1910 * similar 2 portloop in in_pcbbind
1912 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP)) {
1914 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1916 } else if (low == 0 && high == 0) {
1917 key.gwy.port = *nport;
1918 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL)
1920 } else if (low == high) {
1921 key.gwy.port = htons(low);
1922 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) == NULL) {
1923 *nport = htons(low);
1935 cut = karc4random() % (1 + high - low) + low;
1936 /* low <= cut <= high */
1937 for (tmp = cut; tmp <= high; ++(tmp)) {
1938 key.gwy.port = htons(tmp);
1939 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1941 *nport = htons(tmp);
1945 for (tmp = cut - 1; tmp >= low; --(tmp)) {
1946 key.gwy.port = htons(tmp);
1947 if (pf_find_state_all(&key, PF_EXT_GWY, NULL) ==
1949 *nport = htons(tmp);
1955 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
1956 case PF_POOL_RANDOM:
1957 case PF_POOL_ROUNDROBIN:
1958 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
1962 case PF_POOL_SRCHASH:
1963 case PF_POOL_BITMASK:
1967 } while (! PF_AEQ(&init_addr, naddr, af) );
1969 return (1); /* none available */
1973 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
1974 int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
1975 struct pf_addr *daddr, u_int16_t dport, int rs_num)
1977 struct pf_rule *r, *rm = NULL, *anchorrule = NULL;
1978 struct pf_ruleset *ruleset = NULL;
1980 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1981 while (r && rm == NULL) {
1982 struct pf_rule_addr *src = NULL, *dst = NULL;
1983 struct pf_addr_wrap *xdst = NULL;
1985 if (r->action == PF_BINAT && direction == PF_IN) {
1987 if (r->rpool.cur != NULL)
1988 xdst = &r->rpool.cur->addr;
1995 if (r->kif != NULL &&
1996 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1997 r = r->skip[PF_SKIP_IFP].ptr;
1998 else if (r->direction && r->direction != direction)
1999 r = r->skip[PF_SKIP_DIR].ptr;
2000 else if (r->af && r->af != pd->af)
2001 r = r->skip[PF_SKIP_AF].ptr;
2002 else if (r->proto && r->proto != pd->proto)
2003 r = r->skip[PF_SKIP_PROTO].ptr;
2004 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af, src->not))
2005 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
2006 PF_SKIP_DST_ADDR].ptr;
2007 else if (src->port_op && !pf_match_port(src->port_op,
2008 src->port[0], src->port[1], sport))
2009 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
2010 PF_SKIP_DST_PORT].ptr;
2011 else if (dst != NULL &&
2012 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->not))
2013 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2014 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af, 0))
2015 r = TAILQ_NEXT(r, entries);
2016 else if (dst != NULL && dst->port_op &&
2017 !pf_match_port(dst->port_op, dst->port[0],
2018 dst->port[1], dport))
2019 r = r->skip[PF_SKIP_DST_PORT].ptr;
2020 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
2021 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
2022 off, pd->hdr.tcp), r->os_fingerprint)))
2023 r = TAILQ_NEXT(r, entries);
2024 else if (r->anchorname[0] && r->anchor == NULL)
2025 r = TAILQ_NEXT(r, entries);
2026 else if (r->anchor == NULL)
2029 PF_STEP_INTO_ANCHOR(r, anchorrule, ruleset, rs_num);
2030 if (r == NULL && anchorrule != NULL)
2031 PF_STEP_OUT_OF_ANCHOR(r, anchorrule, ruleset,
2034 if (rm != NULL && (rm->action == PF_NONAT ||
2035 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2041 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
2042 struct pfi_kif *kif, struct pf_src_node **sn,
2043 struct pf_addr *saddr, u_int16_t sport,
2044 struct pf_addr *daddr, u_int16_t dport,
2045 struct pf_addr *naddr, u_int16_t *nport)
2047 struct pf_rule *r = NULL;
2049 if (direction == PF_OUT) {
2050 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2051 sport, daddr, dport, PF_RULESET_BINAT);
2053 r = pf_match_translation(pd, m, off, direction, kif,
2054 saddr, sport, daddr, dport, PF_RULESET_NAT);
2056 r = pf_match_translation(pd, m, off, direction, kif, saddr,
2057 sport, daddr, dport, PF_RULESET_RDR);
2059 r = pf_match_translation(pd, m, off, direction, kif,
2060 saddr, sport, daddr, dport, PF_RULESET_BINAT);
2064 switch (r->action) {
2070 if (pf_get_sport(pd->af, pd->proto, r, saddr,
2071 daddr, dport, naddr, nport, r->rpool.proxy_port[0],
2072 r->rpool.proxy_port[1], sn)) {
2073 DPFPRINTF(PF_DEBUG_MISC,
2074 ("pf: NAT proxy port allocation "
2076 r->rpool.proxy_port[0],
2077 r->rpool.proxy_port[1]));
2082 switch (direction) {
2084 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
2085 if (pd->af == AF_INET) {
2086 if (r->rpool.cur->addr.p.dyn->
2090 &r->rpool.cur->addr.p.dyn->
2092 &r->rpool.cur->addr.p.dyn->
2096 if (r->rpool.cur->addr.p.dyn->
2100 &r->rpool.cur->addr.p.dyn->
2102 &r->rpool.cur->addr.p.dyn->
2108 &r->rpool.cur->addr.v.a.addr,
2109 &r->rpool.cur->addr.v.a.mask,
2113 if (r->src.addr.type == PF_ADDR_DYNIFTL){
2114 if (pd->af == AF_INET) {
2115 if (r->src.addr.p.dyn->
2119 &r->src.addr.p.dyn->
2121 &r->src.addr.p.dyn->
2125 if (r->src.addr.p.dyn->
2129 &r->src.addr.p.dyn->
2131 &r->src.addr.p.dyn->
2137 &r->src.addr.v.a.addr,
2138 &r->src.addr.v.a.mask, daddr,
2144 if (pf_map_addr(r->af, r, saddr, naddr, NULL, sn))
2147 if (r->rpool.proxy_port[1]) {
2148 u_int32_t tmp_nport;
2150 tmp_nport = ((ntohs(dport) -
2151 ntohs(r->dst.port[0])) %
2152 (r->rpool.proxy_port[1] -
2153 r->rpool.proxy_port[0] + 1)) +
2154 r->rpool.proxy_port[0];
2156 /* wrap around if necessary */
2157 if (tmp_nport > 65535)
2159 *nport = htons((u_int16_t)tmp_nport);
2160 } else if (r->rpool.proxy_port[0])
2161 *nport = htons(r->rpool.proxy_port[0]);
2173 struct netmsg_hashlookup {
2174 struct netmsg nm_netmsg;
2175 struct inpcb **nm_pinp;
2176 struct inpcbinfo *nm_pcbinfo;
2177 struct pf_addr *nm_saddr;
2178 struct pf_addr *nm_daddr;
2185 in_pcblookup_hash_handler(struct netmsg *msg0)
2187 struct netmsg_hashlookup *msg = (struct netmsg_hashlookup *)msg0;
2189 if (msg->nm_af == AF_INET)
2190 *msg->nm_pinp = in_pcblookup_hash(msg->nm_pcbinfo,
2191 msg->nm_saddr->v4, msg->nm_sport, msg->nm_daddr->v4,
2192 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2195 *msg->nm_pinp = in6_pcblookup_hash(msg->nm_pcbinfo,
2196 &msg->nm_saddr->v6, msg->nm_sport, &msg->nm_daddr->v6,
2197 msg->nm_dport, INPLOOKUP_WILDCARD, NULL);
2199 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
2204 pf_socket_lookup(uid_t *uid, gid_t *gid, int direction, struct pf_pdesc *pd)
2206 struct pf_addr *saddr, *daddr;
2207 u_int16_t sport, dport;
2208 struct inpcbinfo *pi;
2211 struct netmsg_hashlookup *msg = NULL;
2217 if (direction == PF_IN) {
2224 switch (pd->proto) {
2226 sport = pd->hdr.tcp->th_sport;
2227 dport = pd->hdr.tcp->th_dport;
2229 pi_cpu = tcp_addrcpu(saddr->v4.s_addr, sport, daddr->v4.s_addr, dport);
2230 pi = &tcbinfo[pi_cpu];
2233 * Our netstack runs lockless on MP systems
2234 * (only for TCP connections at the moment).
2236 * As we are not allowed to read another CPU's tcbinfo,
2237 * we have to ask that CPU via remote call to search the
2240 * Prepare a msg iff data belongs to another CPU.
2242 if (pi_cpu != mycpu->gd_cpuid) {
2243 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
2244 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
2245 in_pcblookup_hash_handler);
2246 msg->nm_pinp = &inp;
2247 msg->nm_pcbinfo = pi;
2248 msg->nm_saddr = saddr;
2249 msg->nm_sport = sport;
2250 msg->nm_daddr = daddr;
2251 msg->nm_dport = dport;
2252 msg->nm_af = pd->af;
2257 sport = pd->hdr.udp->uh_sport;
2258 dport = pd->hdr.udp->uh_dport;
2264 if (direction != PF_IN) {
2276 * Query other CPU, second part
2278 * msg only gets initialized when:
2280 * 2) the info belongs to another CPU
2282 * Use some switch/case magic to avoid code duplication.
2287 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
2288 &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
2294 /* FALLTHROUGH if SMP and on other CPU */
2299 lwkt_sendmsg(tcp_cport(pi_cpu),
2300 &msg->nm_netmsg.nm_lmsg);
2304 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
2305 dport, INPLOOKUP_WILDCARD, NULL);
2314 *uid = inp->inp_socket->so_cred->cr_uid;
2315 *gid = inp->inp_socket->so_cred->cr_groups[0];
2320 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2324 u_int8_t *opt, optlen;
2325 u_int8_t wscale = 0;
2327 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2328 if (hlen <= sizeof(struct tcphdr))
2330 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2332 opt = hdr + sizeof(struct tcphdr);
2333 hlen -= sizeof(struct tcphdr);
2343 if (wscale > TCP_MAX_WINSHIFT)
2344 wscale = TCP_MAX_WINSHIFT;
2345 wscale |= PF_WSCALE_FLAG;
2360 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
2364 u_int8_t *opt, optlen;
2365 u_int16_t mss = tcp_mssdflt;
2367 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
2368 if (hlen <= sizeof(struct tcphdr))
2370 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
2372 opt = hdr + sizeof(struct tcphdr);
2373 hlen -= sizeof(struct tcphdr);
2374 while (hlen >= TCPOLEN_MAXSEG) {
2382 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
2397 pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
2400 struct sockaddr_in *dst;
2404 struct sockaddr_in6 *dst6;
2405 struct route_in6 ro6;
2407 struct rtentry *rt = NULL;
2409 u_int16_t mss = tcp_mssdflt;
2414 hlen = sizeof(struct ip);
2415 bzero(&ro, sizeof(ro));
2416 dst = (struct sockaddr_in *)&ro.ro_dst;
2417 dst->sin_family = AF_INET;
2418 dst->sin_len = sizeof(*dst);
2419 dst->sin_addr = addr->v4;
2420 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
2426 hlen = sizeof(struct ip6_hdr);
2427 bzero(&ro6, sizeof(ro6));
2428 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
2429 dst6->sin6_family = AF_INET6;
2430 dst6->sin6_len = sizeof(*dst6);
2431 dst6->sin6_addr = addr->v6;
2432 rtalloc_ign((struct route *)&ro6, (RTF_CLONING | RTF_PRCLONING));
2438 if (rt && rt->rt_ifp) {
2439 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
2440 mss = max(tcp_mssdflt, mss);
2443 mss = min(mss, offer);
2444 mss = max(mss, 64); /* sanity - at least max opt space */
2449 pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
2451 struct pf_rule *r = s->rule.ptr;
2454 if (!r->rt || r->rt == PF_FASTROUTE)
2459 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
2461 s->rt_kif = r->rpool.cur->kif;
2466 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
2468 s->rt_kif = r->rpool.cur->kif;
2475 pf_test_tcp(struct pf_rule **rm, struct pf_state **sm, int direction,
2476 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2477 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2479 struct pf_rule *nr = NULL;
2480 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2481 struct tcphdr *th = pd->hdr.tcp;
2482 u_int16_t bport, nport = 0;
2483 sa_family_t af = pd->af;
2487 struct pf_rule *r, *a = NULL;
2488 struct pf_ruleset *ruleset = NULL;
2489 struct pf_src_node *nsn = NULL;
2493 u_int16_t mss = tcp_mssdflt;
2495 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2497 if (direction == PF_OUT) {
2498 bport = nport = th->th_sport;
2499 /* check outgoing packet for BINAT/NAT */
2500 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2501 saddr, th->th_sport, daddr, th->th_dport,
2502 &pd->naddr, &nport)) != NULL) {
2503 PF_ACPY(&pd->baddr, saddr, af);
2504 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2505 &th->th_sum, &pd->naddr, nport, 0, af);
2512 bport = nport = th->th_dport;
2513 /* check incoming packet for BINAT/RDR */
2514 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2515 saddr, th->th_sport, daddr, th->th_dport,
2516 &pd->naddr, &nport)) != NULL) {
2517 PF_ACPY(&pd->baddr, daddr, af);
2518 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2519 &th->th_sum, &pd->naddr, nport, 0, af);
2529 if (r->kif != NULL &&
2530 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2531 r = r->skip[PF_SKIP_IFP].ptr;
2532 else if (r->direction && r->direction != direction)
2533 r = r->skip[PF_SKIP_DIR].ptr;
2534 else if (r->af && r->af != af)
2535 r = r->skip[PF_SKIP_AF].ptr;
2536 else if (r->proto && r->proto != IPPROTO_TCP)
2537 r = r->skip[PF_SKIP_PROTO].ptr;
2538 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2539 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2540 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2541 r->src.port[0], r->src.port[1], th->th_sport))
2542 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2543 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2544 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2545 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2546 r->dst.port[0], r->dst.port[1], th->th_dport))
2547 r = r->skip[PF_SKIP_DST_PORT].ptr;
2548 else if (r->tos && !(r->tos & pd->tos))
2549 r = TAILQ_NEXT(r, entries);
2550 else if (r->rule_flag & PFRULE_FRAGMENT)
2551 r = TAILQ_NEXT(r, entries);
2552 else if ((r->flagset & th->th_flags) != r->flags)
2553 r = TAILQ_NEXT(r, entries);
2554 else if (r->uid.op && (lookup != -1 || (lookup =
2555 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2556 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2558 r = TAILQ_NEXT(r, entries);
2559 else if (r->gid.op && (lookup != -1 || (lookup =
2560 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2561 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2563 r = TAILQ_NEXT(r, entries);
2564 else if (r->prob && r->prob <= karc4random())
2565 r = TAILQ_NEXT(r, entries);
2566 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2567 r = TAILQ_NEXT(r, entries);
2568 else if (r->anchorname[0] && r->anchor == NULL)
2569 r = TAILQ_NEXT(r, entries);
2570 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
2571 pf_osfp_fingerprint(pd, m, off, th), r->os_fingerprint))
2572 r = TAILQ_NEXT(r, entries);
2576 if (r->anchor == NULL) {
2582 r = TAILQ_NEXT(r, entries);
2584 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2587 if (r == NULL && a != NULL)
2588 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2595 REASON_SET(&reason, PFRES_MATCH);
2599 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2600 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2603 if ((r->action == PF_DROP) &&
2604 ((r->rule_flag & PFRULE_RETURNRST) ||
2605 (r->rule_flag & PFRULE_RETURNICMP) ||
2606 (r->rule_flag & PFRULE_RETURN))) {
2607 /* undo NAT changes, if they have taken place */
2609 if (direction == PF_OUT) {
2610 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
2611 &th->th_sum, &pd->baddr, bport, 0, af);
2614 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
2615 &th->th_sum, &pd->baddr, bport, 0, af);
2619 if (((r->rule_flag & PFRULE_RETURNRST) ||
2620 (r->rule_flag & PFRULE_RETURN)) &&
2621 !(th->th_flags & TH_RST)) {
2622 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
2624 if (th->th_flags & TH_SYN)
2626 if (th->th_flags & TH_FIN)
2628 pf_send_tcp(r, af, pd->dst,
2629 pd->src, th->th_dport, th->th_sport,
2630 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
2632 } else if ((af == AF_INET) && r->return_icmp)
2633 pf_send_icmp(m, r->return_icmp >> 8,
2634 r->return_icmp & 255, af, r);
2635 else if ((af == AF_INET6) && r->return_icmp6)
2636 pf_send_icmp(m, r->return_icmp6 >> 8,
2637 r->return_icmp6 & 255, af, r);
2640 if (r->action == PF_DROP)
2643 pf_tag_packet(m, tag);
2645 if (r->keep_state || nr != NULL ||
2646 (pd->flags & PFDESC_TCP_NORM)) {
2647 /* create new state */
2649 struct pf_state *s = NULL;
2650 struct pf_src_node *sn = NULL;
2652 len = pd->tot_len - off - (th->th_off << 2);
2654 /* check maximums */
2655 if (r->max_states && (r->states >= r->max_states))
2657 /* src node for flter rule */
2658 if ((r->rule_flag & PFRULE_SRCTRACK ||
2659 r->rpool.opts & PF_POOL_STICKYADDR) &&
2660 pf_insert_src_node(&sn, r, saddr, af) != 0)
2662 /* src node for translation rule */
2663 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2664 ((direction == PF_OUT &&
2665 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2666 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2668 s = pool_get(&pf_state_pl, PR_NOWAIT);
2671 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
2672 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
2673 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2674 pf_status.src_nodes--;
2675 pool_put(&pf_src_tree_pl, sn);
2677 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
2679 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
2680 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
2681 pf_status.src_nodes--;
2682 pool_put(&pf_src_tree_pl, nsn);
2684 REASON_SET(&reason, PFRES_MEMORY);
2687 bzero(s, sizeof(*s));
2692 s->nat_rule.ptr = nr;
2693 if (s->nat_rule.ptr != NULL)
2694 s->nat_rule.ptr->states++;
2696 s->allow_opts = r->allow_opts;
2697 s->log = r->log & 2;
2698 s->proto = IPPROTO_TCP;
2699 s->direction = direction;
2701 if (direction == PF_OUT) {
2702 PF_ACPY(&s->gwy.addr, saddr, af);
2703 s->gwy.port = th->th_sport; /* sport */
2704 PF_ACPY(&s->ext.addr, daddr, af);
2705 s->ext.port = th->th_dport;
2707 PF_ACPY(&s->lan.addr, &pd->baddr, af);
2708 s->lan.port = bport;
2710 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
2711 s->lan.port = s->gwy.port;
2714 PF_ACPY(&s->lan.addr, daddr, af);
2715 s->lan.port = th->th_dport;
2716 PF_ACPY(&s->ext.addr, saddr, af);
2717 s->ext.port = th->th_sport;
2719 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
2720 s->gwy.port = bport;
2722 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
2723 s->gwy.port = s->lan.port;
2727 s->src.seqlo = ntohl(th->th_seq);
2728 s->src.seqhi = s->src.seqlo + len + 1;
2729 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2730 r->keep_state == PF_STATE_MODULATE) {
2731 /* Generate sequence number modulator */
2732 while ((s->src.seqdiff = karc4random()) == 0)
2734 pf_change_a(&th->th_seq, &th->th_sum,
2735 htonl(s->src.seqlo + s->src.seqdiff), 0);
2739 s->src.max_win = MAX(ntohs(th->th_win), 1);
2740 if (th->th_flags & TH_SYN) {
2742 s->src.wscale = pf_get_wscale(m, off, th->th_off, af);
2743 } else if (s->src.wscale & PF_WSCALE_MASK) {
2744 /* Remove scale factor from initial window */
2745 u_int win = s->src.max_win;
2746 win += 1 << (s->src.wscale & PF_WSCALE_MASK);
2747 s->src.max_win = (win - 1) >>
2748 (s->src.wscale & PF_WSCALE_MASK);
2750 if (th->th_flags & TH_FIN)
2754 s->src.state = TCPS_SYN_SENT;
2755 s->dst.state = TCPS_CLOSED;
2756 s->creation = time_second;
2757 s->expire = time_second;
2758 s->timeout = PFTM_TCP_FIRST_PACKET;
2759 pf_set_rt_ifp(s, saddr);
2762 s->src_node->states++;
2765 PF_ACPY(&nsn->raddr, &pd->naddr, af);
2766 s->nat_src_node = nsn;
2767 s->nat_src_node->states++;
2769 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
2770 off, pd, th, &s->src, &s->dst)) {
2771 REASON_SET(&reason, PFRES_MEMORY);
2772 pf_src_tree_remove_state(s);
2773 pool_put(&pf_state_pl, s);
2776 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
2777 pf_normalize_tcp_stateful(m, off, pd, &reason, th, &s->src,
2778 &s->dst, &rewrite)) {
2779 pf_normalize_tcp_cleanup(s);
2780 pf_src_tree_remove_state(s);
2781 pool_put(&pf_state_pl, s);
2784 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
2785 pf_normalize_tcp_cleanup(s);
2786 REASON_SET(&reason, PFRES_MEMORY);
2787 pf_src_tree_remove_state(s);
2788 pool_put(&pf_state_pl, s);
2792 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
2793 r->keep_state == PF_STATE_SYNPROXY) {
2794 s->src.state = PF_TCPS_PROXY_SRC;
2796 if (direction == PF_OUT) {
2797 pf_change_ap(saddr, &th->th_sport,
2798 pd->ip_sum, &th->th_sum, &pd->baddr,
2801 pf_change_ap(daddr, &th->th_dport,
2802 pd->ip_sum, &th->th_sum, &pd->baddr,
2806 s->src.seqhi = karc4random();
2807 /* Find mss option */
2808 mss = pf_get_mss(m, off, th->th_off, af);
2809 mss = pf_calc_mss(saddr, af, mss);
2810 mss = pf_calc_mss(daddr, af, mss);
2812 pf_send_tcp(r, af, daddr, saddr, th->th_dport,
2813 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
2814 TH_SYN|TH_ACK, 0, s->src.mss, 0);
2815 return (PF_SYNPROXY_DROP);
2819 /* copy back packet headers if we performed NAT operations */
2821 m_copyback(m, off, sizeof(*th), (caddr_t)th);
2827 pf_test_udp(struct pf_rule **rm, struct pf_state **sm, int direction,
2828 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
2829 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
2831 struct pf_rule *nr = NULL;
2832 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
2833 struct udphdr *uh = pd->hdr.udp;
2834 u_int16_t bport, nport = 0;
2835 sa_family_t af = pd->af;
2839 struct pf_rule *r, *a = NULL;
2840 struct pf_ruleset *ruleset = NULL;
2841 struct pf_src_node *nsn = NULL;
2846 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
2848 if (direction == PF_OUT) {
2849 bport = nport = uh->uh_sport;
2850 /* check outgoing packet for BINAT/NAT */
2851 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
2852 saddr, uh->uh_sport, daddr, uh->uh_dport,
2853 &pd->naddr, &nport)) != NULL) {
2854 PF_ACPY(&pd->baddr, saddr, af);
2855 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2856 &uh->uh_sum, &pd->naddr, nport, 1, af);
2863 bport = nport = uh->uh_dport;
2864 /* check incoming packet for BINAT/RDR */
2865 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
2866 saddr, uh->uh_sport, daddr, uh->uh_dport, &pd->naddr,
2868 PF_ACPY(&pd->baddr, daddr, af);
2869 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2870 &uh->uh_sum, &pd->naddr, nport, 1, af);
2880 if (r->kif != NULL &&
2881 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
2882 r = r->skip[PF_SKIP_IFP].ptr;
2883 else if (r->direction && r->direction != direction)
2884 r = r->skip[PF_SKIP_DIR].ptr;
2885 else if (r->af && r->af != af)
2886 r = r->skip[PF_SKIP_AF].ptr;
2887 else if (r->proto && r->proto != IPPROTO_UDP)
2888 r = r->skip[PF_SKIP_PROTO].ptr;
2889 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
2890 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
2891 else if (r->src.port_op && !pf_match_port(r->src.port_op,
2892 r->src.port[0], r->src.port[1], uh->uh_sport))
2893 r = r->skip[PF_SKIP_SRC_PORT].ptr;
2894 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
2895 r = r->skip[PF_SKIP_DST_ADDR].ptr;
2896 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
2897 r->dst.port[0], r->dst.port[1], uh->uh_dport))
2898 r = r->skip[PF_SKIP_DST_PORT].ptr;
2899 else if (r->tos && !(r->tos & pd->tos))
2900 r = TAILQ_NEXT(r, entries);
2901 else if (r->rule_flag & PFRULE_FRAGMENT)
2902 r = TAILQ_NEXT(r, entries);
2903 else if (r->uid.op && (lookup != -1 || (lookup =
2904 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2905 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
2907 r = TAILQ_NEXT(r, entries);
2908 else if (r->gid.op && (lookup != -1 || (lookup =
2909 pf_socket_lookup(&uid, &gid, direction, pd), 1)) &&
2910 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
2912 r = TAILQ_NEXT(r, entries);
2913 else if (r->prob && r->prob <= karc4random())
2914 r = TAILQ_NEXT(r, entries);
2915 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
2916 r = TAILQ_NEXT(r, entries);
2917 else if (r->anchorname[0] && r->anchor == NULL)
2918 r = TAILQ_NEXT(r, entries);
2919 else if (r->os_fingerprint != PF_OSFP_ANY)
2920 r = TAILQ_NEXT(r, entries);
2924 if (r->anchor == NULL) {
2930 r = TAILQ_NEXT(r, entries);
2932 PF_STEP_INTO_ANCHOR(r, a, ruleset,
2935 if (r == NULL && a != NULL)
2936 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
2943 REASON_SET(&reason, PFRES_MATCH);
2947 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
2948 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
2951 if ((r->action == PF_DROP) &&
2952 ((r->rule_flag & PFRULE_RETURNICMP) ||
2953 (r->rule_flag & PFRULE_RETURN))) {
2954 /* undo NAT changes, if they have taken place */
2956 if (direction == PF_OUT) {
2957 pf_change_ap(saddr, &uh->uh_sport, pd->ip_sum,
2958 &uh->uh_sum, &pd->baddr, bport, 1, af);
2961 pf_change_ap(daddr, &uh->uh_dport, pd->ip_sum,
2962 &uh->uh_sum, &pd->baddr, bport, 1, af);
2966 if ((af == AF_INET) && r->return_icmp)
2967 pf_send_icmp(m, r->return_icmp >> 8,
2968 r->return_icmp & 255, af, r);
2969 else if ((af == AF_INET6) && r->return_icmp6)
2970 pf_send_icmp(m, r->return_icmp6 >> 8,
2971 r->return_icmp6 & 255, af, r);
2974 if (r->action == PF_DROP)
2977 pf_tag_packet(m, tag);
2979 if (r->keep_state || nr != NULL) {
2980 /* create new state */
2981 struct pf_state *s = NULL;
2982 struct pf_src_node *sn = NULL;
2984 /* check maximums */
2985 if (r->max_states && (r->states >= r->max_states))
2987 /* src node for flter rule */
2988 if ((r->rule_flag & PFRULE_SRCTRACK ||
2989 r->rpool.opts & PF_POOL_STICKYADDR) &&
2990 pf_insert_src_node(&sn, r, saddr, af) != 0)
2992 /* src node for translation rule */
2993 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
2994 ((direction == PF_OUT &&
2995 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
2996 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
2998 s = pool_get(&pf_state_pl, PR_NOWAIT);
3001 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3002 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3003 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3004 pf_status.src_nodes--;
3005 pool_put(&pf_src_tree_pl, sn);
3007 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3009 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3010 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3011 pf_status.src_nodes--;
3012 pool_put(&pf_src_tree_pl, nsn);
3014 REASON_SET(&reason, PFRES_MEMORY);
3017 bzero(s, sizeof(*s));
3022 s->nat_rule.ptr = nr;
3023 if (s->nat_rule.ptr != NULL)
3024 s->nat_rule.ptr->states++;
3026 s->allow_opts = r->allow_opts;
3027 s->log = r->log & 2;
3028 s->proto = IPPROTO_UDP;
3029 s->direction = direction;
3031 if (direction == PF_OUT) {
3032 PF_ACPY(&s->gwy.addr, saddr, af);
3033 s->gwy.port = uh->uh_sport;
3034 PF_ACPY(&s->ext.addr, daddr, af);
3035 s->ext.port = uh->uh_dport;
3037 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3038 s->lan.port = bport;
3040 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3041 s->lan.port = s->gwy.port;
3044 PF_ACPY(&s->lan.addr, daddr, af);
3045 s->lan.port = uh->uh_dport;
3046 PF_ACPY(&s->ext.addr, saddr, af);
3047 s->ext.port = uh->uh_sport;
3049 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3050 s->gwy.port = bport;
3052 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3053 s->gwy.port = s->lan.port;
3056 s->src.state = PFUDPS_SINGLE;
3057 s->dst.state = PFUDPS_NO_TRAFFIC;
3058 s->creation = time_second;
3059 s->expire = time_second;
3060 s->timeout = PFTM_UDP_FIRST_PACKET;
3061 pf_set_rt_ifp(s, saddr);
3064 s->src_node->states++;
3067 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3068 s->nat_src_node = nsn;
3069 s->nat_src_node->states++;
3071 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3072 REASON_SET(&reason, PFRES_MEMORY);
3073 pf_src_tree_remove_state(s);
3074 pool_put(&pf_state_pl, s);
3080 /* copy back packet headers if we performed NAT operations */
3082 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
3088 pf_test_icmp(struct pf_rule **rm, struct pf_state **sm, int direction,
3089 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3090 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm)
3092 struct pf_rule *nr = NULL;
3093 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3094 struct pf_rule *r, *a = NULL;
3095 struct pf_ruleset *ruleset = NULL;
3096 struct pf_src_node *nsn = NULL;
3098 u_int16_t icmpid = 0;
3099 sa_family_t af = pd->af;
3100 u_int8_t icmptype = 0, icmpcode = 0;
3107 switch (pd->proto) {
3110 icmptype = pd->hdr.icmp->icmp_type;
3111 icmpcode = pd->hdr.icmp->icmp_code;
3112 icmpid = pd->hdr.icmp->icmp_id;
3114 if (icmptype == ICMP_UNREACH ||
3115 icmptype == ICMP_SOURCEQUENCH ||
3116 icmptype == ICMP_REDIRECT ||
3117 icmptype == ICMP_TIMXCEED ||
3118 icmptype == ICMP_PARAMPROB)
3123 case IPPROTO_ICMPV6:
3124 icmptype = pd->hdr.icmp6->icmp6_type;
3125 icmpcode = pd->hdr.icmp6->icmp6_code;
3126 icmpid = pd->hdr.icmp6->icmp6_id;
3128 if (icmptype == ICMP6_DST_UNREACH ||
3129 icmptype == ICMP6_PACKET_TOO_BIG ||
3130 icmptype == ICMP6_TIME_EXCEEDED ||
3131 icmptype == ICMP6_PARAM_PROB)
3137 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3139 if (direction == PF_OUT) {
3140 /* check outgoing packet for BINAT/NAT */
3141 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3142 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3143 PF_ACPY(&pd->baddr, saddr, af);
3147 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3148 pd->naddr.v4.s_addr, 0);
3153 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3164 /* check incoming packet for BINAT/RDR */
3165 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3166 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3167 PF_ACPY(&pd->baddr, daddr, af);
3171 pf_change_a(&daddr->v4.s_addr,
3172 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3177 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3191 if (r->kif != NULL &&
3192 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3193 r = r->skip[PF_SKIP_IFP].ptr;
3194 else if (r->direction && r->direction != direction)
3195 r = r->skip[PF_SKIP_DIR].ptr;
3196 else if (r->af && r->af != af)
3197 r = r->skip[PF_SKIP_AF].ptr;
3198 else if (r->proto && r->proto != pd->proto)
3199 r = r->skip[PF_SKIP_PROTO].ptr;
3200 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, r->src.not))
3201 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3202 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, r->dst.not))
3203 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3204 else if (r->type && r->type != icmptype + 1)
3205 r = TAILQ_NEXT(r, entries);
3206 else if (r->code && r->code != icmpcode + 1)
3207 r = TAILQ_NEXT(r, entries);
3208 else if (r->tos && !(r->tos & pd->tos))
3209 r = TAILQ_NEXT(r, entries);
3210 else if (r->rule_flag & PFRULE_FRAGMENT)
3211 r = TAILQ_NEXT(r, entries);
3212 else if (r->prob && r->prob <= karc4random())
3213 r = TAILQ_NEXT(r, entries);
3214 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3215 r = TAILQ_NEXT(r, entries);
3216 else if (r->anchorname[0] && r->anchor == NULL)
3217 r = TAILQ_NEXT(r, entries);
3218 else if (r->os_fingerprint != PF_OSFP_ANY)
3219 r = TAILQ_NEXT(r, entries);
3223 if (r->anchor == NULL) {
3229 r = TAILQ_NEXT(r, entries);
3231 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3234 if (r == NULL && a != NULL)
3235 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3242 REASON_SET(&reason, PFRES_MATCH);
3247 m_copyback(m, off, sizeof(struct icmp6_hdr),
3248 (caddr_t)pd->hdr.icmp6);
3250 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3253 if (r->action != PF_PASS)
3256 pf_tag_packet(m, tag);
3258 if (!state_icmp && (r->keep_state || nr != NULL)) {
3259 /* create new state */
3260 struct pf_state *s = NULL;
3261 struct pf_src_node *sn = NULL;
3263 /* check maximums */
3264 if (r->max_states && (r->states >= r->max_states))
3266 /* src node for flter rule */
3267 if ((r->rule_flag & PFRULE_SRCTRACK ||
3268 r->rpool.opts & PF_POOL_STICKYADDR) &&
3269 pf_insert_src_node(&sn, r, saddr, af) != 0)
3271 /* src node for translation rule */
3272 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3273 ((direction == PF_OUT &&
3274 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3275 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3277 s = pool_get(&pf_state_pl, PR_NOWAIT);
3280 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3281 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3282 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3283 pf_status.src_nodes--;
3284 pool_put(&pf_src_tree_pl, sn);
3286 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3288 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3289 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3290 pf_status.src_nodes--;
3291 pool_put(&pf_src_tree_pl, nsn);
3293 REASON_SET(&reason, PFRES_MEMORY);
3296 bzero(s, sizeof(*s));
3301 s->nat_rule.ptr = nr;
3302 if (s->nat_rule.ptr != NULL)
3303 s->nat_rule.ptr->states++;
3305 s->allow_opts = r->allow_opts;
3306 s->log = r->log & 2;
3307 s->proto = pd->proto;
3308 s->direction = direction;
3310 if (direction == PF_OUT) {
3311 PF_ACPY(&s->gwy.addr, saddr, af);
3312 s->gwy.port = icmpid;
3313 PF_ACPY(&s->ext.addr, daddr, af);
3314 s->ext.port = icmpid;
3316 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3318 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3319 s->lan.port = icmpid;
3321 PF_ACPY(&s->lan.addr, daddr, af);
3322 s->lan.port = icmpid;
3323 PF_ACPY(&s->ext.addr, saddr, af);
3324 s->ext.port = icmpid;
3326 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3328 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3329 s->gwy.port = icmpid;
3331 s->creation = time_second;
3332 s->expire = time_second;
3333 s->timeout = PFTM_ICMP_FIRST_PACKET;
3334 pf_set_rt_ifp(s, saddr);
3337 s->src_node->states++;
3340 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3341 s->nat_src_node = nsn;
3342 s->nat_src_node->states++;
3344 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3345 REASON_SET(&reason, PFRES_MEMORY);
3346 pf_src_tree_remove_state(s);
3347 pool_put(&pf_state_pl, s);
3354 /* copy back packet headers if we performed IPv6 NAT operations */
3356 m_copyback(m, off, sizeof(struct icmp6_hdr),
3357 (caddr_t)pd->hdr.icmp6);
3364 pf_test_other(struct pf_rule **rm, struct pf_state **sm, int direction,
3365 struct pfi_kif *kif, struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3366 struct pf_rule **am, struct pf_ruleset **rsm)
3368 struct pf_rule *nr = NULL;
3369 struct pf_rule *r, *a = NULL;
3370 struct pf_ruleset *ruleset = NULL;
3371 struct pf_src_node *nsn = NULL;
3372 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3373 sa_family_t af = pd->af;
3377 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3379 if (direction == PF_OUT) {
3380 /* check outgoing packet for BINAT/NAT */
3381 if ((nr = pf_get_translation(pd, m, off, PF_OUT, kif, &nsn,
3382 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3383 PF_ACPY(&pd->baddr, saddr, af);
3387 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3388 pd->naddr.v4.s_addr, 0);
3393 PF_ACPY(saddr, &pd->naddr, af);
3402 /* check incoming packet for BINAT/RDR */
3403 if ((nr = pf_get_translation(pd, m, off, PF_IN, kif, &nsn,
3404 saddr, 0, daddr, 0, &pd->naddr, NULL)) != NULL) {
3405 PF_ACPY(&pd->baddr, daddr, af);
3409 pf_change_a(&daddr->v4.s_addr,
3410 pd->ip_sum, pd->naddr.v4.s_addr, 0);
3415 PF_ACPY(daddr, &pd->naddr, af);
3427 if (r->kif != NULL &&
3428 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3429 r = r->skip[PF_SKIP_IFP].ptr;
3430 else if (r->direction && r->direction != direction)
3431 r = r->skip[PF_SKIP_DIR].ptr;
3432 else if (r->af && r->af != af)
3433 r = r->skip[PF_SKIP_AF].ptr;
3434 else if (r->proto && r->proto != pd->proto)
3435 r = r->skip[PF_SKIP_PROTO].ptr;
3436 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3437 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3438 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3439 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3440 else if (r->tos && !(r->tos & pd->tos))
3441 r = TAILQ_NEXT(r, entries);
3442 else if (r->rule_flag & PFRULE_FRAGMENT)
3443 r = TAILQ_NEXT(r, entries);
3444 else if (r->prob && r->prob <= karc4random())
3445 r = TAILQ_NEXT(r, entries);
3446 else if (r->match_tag && !pf_match_tag(m, r, nr, &tag))
3447 r = TAILQ_NEXT(r, entries);
3448 else if (r->anchorname[0] && r->anchor == NULL)
3449 r = TAILQ_NEXT(r, entries);
3450 else if (r->os_fingerprint != PF_OSFP_ANY)
3451 r = TAILQ_NEXT(r, entries);
3455 if (r->anchor == NULL) {
3461 r = TAILQ_NEXT(r, entries);
3463 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3466 if (r == NULL && a != NULL)
3467 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3474 REASON_SET(&reason, PFRES_MATCH);
3477 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3479 if ((r->action == PF_DROP) &&
3480 ((r->rule_flag & PFRULE_RETURNICMP) ||
3481 (r->rule_flag & PFRULE_RETURN))) {
3482 struct pf_addr *a = NULL;
3485 if (direction == PF_OUT)
3494 pf_change_a(&a->v4.s_addr, pd->ip_sum,
3495 pd->baddr.v4.s_addr, 0);
3500 PF_ACPY(a, &pd->baddr, af);
3505 if ((af == AF_INET) && r->return_icmp)
3506 pf_send_icmp(m, r->return_icmp >> 8,
3507 r->return_icmp & 255, af, r);
3508 else if ((af == AF_INET6) && r->return_icmp6)
3509 pf_send_icmp(m, r->return_icmp6 >> 8,
3510 r->return_icmp6 & 255, af, r);
3513 if (r->action != PF_PASS)
3516 pf_tag_packet(m, tag);
3518 if (r->keep_state || nr != NULL) {
3519 /* create new state */
3520 struct pf_state *s = NULL;
3521 struct pf_src_node *sn = NULL;
3523 /* check maximums */
3524 if (r->max_states && (r->states >= r->max_states))
3526 /* src node for flter rule */
3527 if ((r->rule_flag & PFRULE_SRCTRACK ||
3528 r->rpool.opts & PF_POOL_STICKYADDR) &&
3529 pf_insert_src_node(&sn, r, saddr, af) != 0)
3531 /* src node for translation rule */
3532 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
3533 ((direction == PF_OUT &&
3534 pf_insert_src_node(&nsn, nr, &pd->baddr, af) != 0) ||
3535 (pf_insert_src_node(&nsn, nr, saddr, af) != 0)))
3537 s = pool_get(&pf_state_pl, PR_NOWAIT);
3540 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
3541 RB_REMOVE(pf_src_tree, &tree_src_tracking, sn);
3542 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3543 pf_status.src_nodes--;
3544 pool_put(&pf_src_tree_pl, sn);
3546 if (nsn != sn && nsn != NULL && nsn->states == 0 &&
3548 RB_REMOVE(pf_src_tree, &tree_src_tracking, nsn);
3549 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
3550 pf_status.src_nodes--;
3551 pool_put(&pf_src_tree_pl, nsn);
3553 REASON_SET(&reason, PFRES_MEMORY);
3556 bzero(s, sizeof(*s));
3561 s->nat_rule.ptr = nr;
3562 if (s->nat_rule.ptr != NULL)
3563 s->nat_rule.ptr->states++;
3565 s->allow_opts = r->allow_opts;
3566 s->log = r->log & 2;
3567 s->proto = pd->proto;
3568 s->direction = direction;
3570 if (direction == PF_OUT) {
3571 PF_ACPY(&s->gwy.addr, saddr, af);
3572 PF_ACPY(&s->ext.addr, daddr, af);
3574 PF_ACPY(&s->lan.addr, &pd->baddr, af);
3576 PF_ACPY(&s->lan.addr, &s->gwy.addr, af);
3578 PF_ACPY(&s->lan.addr, daddr, af);
3579 PF_ACPY(&s->ext.addr, saddr, af);
3581 PF_ACPY(&s->gwy.addr, &pd->baddr, af);
3583 PF_ACPY(&s->gwy.addr, &s->lan.addr, af);
3585 s->src.state = PFOTHERS_SINGLE;
3586 s->dst.state = PFOTHERS_NO_TRAFFIC;
3587 s->creation = time_second;
3588 s->expire = time_second;
3589 s->timeout = PFTM_OTHER_FIRST_PACKET;
3590 pf_set_rt_ifp(s, saddr);
3593 s->src_node->states++;
3596 PF_ACPY(&nsn->raddr, &pd->naddr, af);
3597 s->nat_src_node = nsn;
3598 s->nat_src_node->states++;
3600 if (pf_insert_state(BOUND_IFACE(r, kif), s)) {
3601 REASON_SET(&reason, PFRES_MEMORY);
3602 pf_src_tree_remove_state(s);
3603 pool_put(&pf_state_pl, s);
3613 pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
3614 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
3615 struct pf_ruleset **rsm)
3617 struct pf_rule *r, *a = NULL;
3618 struct pf_ruleset *ruleset = NULL;
3619 sa_family_t af = pd->af;
3623 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3626 if (r->kif != NULL &&
3627 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
3628 r = r->skip[PF_SKIP_IFP].ptr;
3629 else if (r->direction && r->direction != direction)
3630 r = r->skip[PF_SKIP_DIR].ptr;
3631 else if (r->af && r->af != af)
3632 r = r->skip[PF_SKIP_AF].ptr;
3633 else if (r->proto && r->proto != pd->proto)
3634 r = r->skip[PF_SKIP_PROTO].ptr;
3635 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
3636 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3637 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
3638 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3639 else if (r->tos && !(r->tos & pd->tos))
3640 r = TAILQ_NEXT(r, entries);
3641 else if (r->src.port_op || r->dst.port_op ||
3642 r->flagset || r->type || r->code ||
3643 r->os_fingerprint != PF_OSFP_ANY)
3644 r = TAILQ_NEXT(r, entries);
3645 else if (r->prob && r->prob <= karc4random())
3646 r = TAILQ_NEXT(r, entries);
3647 else if (r->match_tag && !pf_match_tag(m, r, NULL, &tag))
3648 r = TAILQ_NEXT(r, entries);
3649 else if (r->anchorname[0] && r->anchor == NULL)
3650 r = TAILQ_NEXT(r, entries);
3652 if (r->anchor == NULL) {
3658 r = TAILQ_NEXT(r, entries);
3660 PF_STEP_INTO_ANCHOR(r, a, ruleset,
3663 if (r == NULL && a != NULL)
3664 PF_STEP_OUT_OF_ANCHOR(r, a, ruleset,
3671 REASON_SET(&reason, PFRES_MATCH);
3674 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset);
3676 if (r->action != PF_PASS)
3679 pf_tag_packet(m, tag);
3685 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
3686 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
3689 struct pf_state key;
3690 struct tcphdr *th = pd->hdr.tcp;
3691 u_int16_t win = ntohs(th->th_win);
3692 u_int32_t ack, end, seq;
3696 struct pf_state_peer *src, *dst;
3699 key.proto = IPPROTO_TCP;
3700 if (direction == PF_IN) {
3701 PF_ACPY(&key.ext.addr, pd->src, key.af);
3702 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
3703 key.ext.port = th->th_sport;
3704 key.gwy.port = th->th_dport;
3706 PF_ACPY(&key.lan.addr, pd->src, key.af);
3707 PF_ACPY(&key.ext.addr, pd->dst, key.af);
3708 key.lan.port = th->th_sport;
3709 key.ext.port = th->th_dport;
3714 if (direction == (*state)->direction) {
3715 src = &(*state)->src;
3716 dst = &(*state)->dst;
3718 src = &(*state)->dst;
3719 dst = &(*state)->src;
3722 if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
3723 if (direction != (*state)->direction)
3724 return (PF_SYNPROXY_DROP);
3725 if (th->th_flags & TH_SYN) {
3726 if (ntohl(th->th_seq) != (*state)->src.seqlo)
3728 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3729 pd->src, th->th_dport, th->th_sport,
3730 (*state)->src.seqhi, ntohl(th->th_seq) + 1,
3731 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0);
3732 return (PF_SYNPROXY_DROP);
3733 } else if (!(th->th_flags & TH_ACK) ||
3734 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3735 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3738 (*state)->src.state = PF_TCPS_PROXY_DST;
3740 if ((*state)->src.state == PF_TCPS_PROXY_DST) {
3741 struct pf_state_host *src, *dst;
3743 if (direction == PF_OUT) {
3744 src = &(*state)->gwy;
3745 dst = &(*state)->ext;
3747 src = &(*state)->ext;
3748 dst = &(*state)->lan;
3750 if (direction == (*state)->direction) {
3751 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
3752 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
3753 (ntohl(th->th_seq) != (*state)->src.seqlo + 1))
3755 (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
3756 if ((*state)->dst.seqhi == 1)
3757 (*state)->dst.seqhi = karc4random();
3758 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3759 &dst->addr, src->port, dst->port,
3760 (*state)->dst.seqhi, 0, TH_SYN, 0,
3761 (*state)->src.mss, 0);
3762 return (PF_SYNPROXY_DROP);
3763 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
3765 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1))
3768 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
3769 (*state)->dst.seqlo = ntohl(th->th_seq);
3770 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
3771 pd->src, th->th_dport, th->th_sport,
3772 ntohl(th->th_ack), ntohl(th->th_seq) + 1,
3773 TH_ACK, (*state)->src.max_win, 0, 0);
3774 pf_send_tcp((*state)->rule.ptr, pd->af, &src->addr,
3775 &dst->addr, src->port, dst->port,
3776 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
3777 TH_ACK, (*state)->dst.max_win, 0, 0);
3778 (*state)->src.seqdiff = (*state)->dst.seqhi -
3779 (*state)->src.seqlo;
3780 (*state)->dst.seqdiff = (*state)->src.seqhi -
3781 (*state)->dst.seqlo;
3782 (*state)->src.seqhi = (*state)->src.seqlo +
3783 (*state)->dst.max_win;
3784 (*state)->dst.seqhi = (*state)->dst.seqlo +
3785 (*state)->src.max_win;
3786 (*state)->src.wscale = (*state)->dst.wscale = 0;
3787 (*state)->src.state = (*state)->dst.state =
3789 return (PF_SYNPROXY_DROP);
3793 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
3794 sws = src->wscale & PF_WSCALE_MASK;
3795 dws = dst->wscale & PF_WSCALE_MASK;
3800 * Sequence tracking algorithm from Guido van Rooij's paper:
3801 * http://www.madison-gurkha.com/publications/tcp_filtering/
3805 seq = ntohl(th->th_seq);
3806 if (src->seqlo == 0) {
3807 /* First packet from this end. Set its state */
3809 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
3810 src->scrub == NULL) {
3811 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
3812 REASON_SET(reason, PFRES_MEMORY);
3817 /* Deferred generation of sequence number modulator */
3818 if (dst->seqdiff && !src->seqdiff) {
3819 while ((src->seqdiff = karc4random()) == 0)
3821 ack = ntohl(th->th_ack) - dst->seqdiff;
3822 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3824 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3827 ack = ntohl(th->th_ack);
3830 end = seq + pd->p_len;
3831 if (th->th_flags & TH_SYN) {
3833 if (dst->wscale & PF_WSCALE_FLAG) {
3834 src->wscale = pf_get_wscale(m, off, th->th_off,
3836 if (src->wscale & PF_WSCALE_FLAG) {
3837 /* Remove scale factor from initial
3839 sws = src->wscale & PF_WSCALE_MASK;
3840 win = ((u_int32_t)win + (1 << sws) - 1)
3842 dws = dst->wscale & PF_WSCALE_MASK;
3844 /* fixup other window */
3845 dst->max_win <<= dst->wscale &
3847 /* in case of a retrans SYN|ACK */
3852 if (th->th_flags & TH_FIN)
3856 if (src->state < TCPS_SYN_SENT)
3857 src->state = TCPS_SYN_SENT;
3860 * May need to slide the window (seqhi may have been set by
3861 * the crappy stack check or if we picked up the connection
3862 * after establishment)
3864 if (src->seqhi == 1 ||
3865 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
3866 src->seqhi = end + MAX(1, dst->max_win << dws);
3867 if (win > src->max_win)
3871 ack = ntohl(th->th_ack) - dst->seqdiff;
3873 /* Modulate sequence numbers */
3874 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
3876 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
3879 end = seq + pd->p_len;
3880 if (th->th_flags & TH_SYN)
3882 if (th->th_flags & TH_FIN)
3886 if ((th->th_flags & TH_ACK) == 0) {
3887 /* Let it pass through the ack skew check */
3889 } else if ((ack == 0 &&
3890 (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
3891 /* broken tcp stacks do not set ack */
3892 (dst->state < TCPS_SYN_SENT)) {
3894 * Many stacks (ours included) will set the ACK number in an
3895 * FIN|ACK if the SYN times out -- no sequence to ACK.
3901 /* Ease sequencing restrictions on no data packets */
3906 ackskew = dst->seqlo - ack;
3908 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
3909 if (SEQ_GEQ(src->seqhi, end) &&
3910 /* Last octet inside other's window space */
3911 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
3912 /* Retrans: not more than one window back */
3913 (ackskew >= -MAXACKWINDOW) &&
3914 /* Acking not more than one reassembled fragment backwards */
3915 (ackskew <= (MAXACKWINDOW << sws))) {
3916 /* Acking not more than one window forward */
3918 /* update max window */
3919 if (src->max_win < win)
3921 /* synchronize sequencing */
3922 if (SEQ_GT(end, src->seqlo))
3924 /* slide the window of what the other end can send */
3925 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
3926 dst->seqhi = ack + MAX((win << sws), 1);
3930 if (th->th_flags & TH_SYN)
3931 if (src->state < TCPS_SYN_SENT)
3932 src->state = TCPS_SYN_SENT;
3933 if (th->th_flags & TH_FIN)
3934 if (src->state < TCPS_CLOSING)
3935 src->state = TCPS_CLOSING;
3936 if (th->th_flags & TH_ACK) {
3937 if (dst->state == TCPS_SYN_SENT)
3938 dst->state = TCPS_ESTABLISHED;
3939 else if (dst->state == TCPS_CLOSING)
3940 dst->state = TCPS_FIN_WAIT_2;
3942 if (th->th_flags & TH_RST)
3943 src->state = dst->state = TCPS_TIME_WAIT;
3945 /* update expire time */
3946 (*state)->expire = time_second;
3947 if (src->state >= TCPS_FIN_WAIT_2 &&
3948 dst->state >= TCPS_FIN_WAIT_2)
3949 (*state)->timeout = PFTM_TCP_CLOSED;
3950 else if (src->state >= TCPS_FIN_WAIT_2 ||
3951 dst->state >= TCPS_FIN_WAIT_2)
3952 (*state)->timeout = PFTM_TCP_FIN_WAIT;
3953 else if (src->state < TCPS_ESTABLISHED ||
3954 dst->state < TCPS_ESTABLISHED)
3955 (*state)->timeout = PFTM_TCP_OPENING;
3956 else if (src->state >= TCPS_CLOSING ||
3957 dst->state >= TCPS_CLOSING)
3958 (*state)->timeout = PFTM_TCP_CLOSING;
3960 (*state)->timeout = PFTM_TCP_ESTABLISHED;
3962 /* Fall through to PASS packet */
3964 } else if ((dst->state < TCPS_SYN_SENT ||
3965 dst->state >= TCPS_FIN_WAIT_2 ||
3966 src->state >= TCPS_FIN_WAIT_2) &&
3967 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
3968 /* Within a window forward of the originating packet */
3969 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
3970 /* Within a window backward of the originating packet */
3973 * This currently handles three situations:
3974 * 1) Stupid stacks will shotgun SYNs before their peer
3976 * 2) When PF catches an already established stream (the
3977 * firewall rebooted, the state table was flushed, routes
3979 * 3) Packets get funky immediately after the connection
3980 * closes (this should catch Solaris spurious ACK|FINs
3981 * that web servers like to spew after a close)
3983 * This must be a little more careful than the above code
3984 * since packet floods will also be caught here. We don't
3985 * update the TTL here to mitigate the damage of a packet
3986 * flood and so the same code can handle awkward establishment
3987 * and a loosened connection close.
3988 * In the establishment case, a correct peer response will
3989 * validate the connection, go through the normal state code
3990 * and keep updating the state TTL.
3993 if (pf_status.debug >= PF_DEBUG_MISC) {
3994 kprintf("pf: loose state match: ");
3995 pf_print_state(*state);
3996 pf_print_flags(th->th_flags);
3997 kprintf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d\n",
3998 seq, ack, pd->p_len, ackskew,
3999 (*state)->packets[0], (*state)->packets[1]);
4002 /* update max window */
4003 if (src->max_win < win)
4005 /* synchronize sequencing */
4006 if (SEQ_GT(end, src->seqlo))
4008 /* slide the window of what the other end can send */
4009 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4010 dst->seqhi = ack + MAX((win << sws), 1);
4013 * Cannot set dst->seqhi here since this could be a shotgunned
4014 * SYN and not an already established connection.
4017 if (th->th_flags & TH_FIN)
4018 if (src->state < TCPS_CLOSING)
4019 src->state = TCPS_CLOSING;
4020 if (th->th_flags & TH_RST)
4021 src->state = dst->state = TCPS_TIME_WAIT;
4023 /* Fall through to PASS packet */
4026 if ((*state)->dst.state == TCPS_SYN_SENT &&
4027 (*state)->src.state == TCPS_SYN_SENT) {
4028 /* Send RST for state mismatches during handshake */
4029 if (!(th->th_flags & TH_RST)) {
4030 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
4032 if (th->th_flags & TH_SYN)
4034 if (th->th_flags & TH_FIN)
4036 pf_send_tcp((*state)->rule.ptr, pd->af,
4037 pd->dst, pd->src, th->th_dport,
4038 th->th_sport, ntohl(th->th_ack), ack,
4039 TH_RST|TH_ACK, 0, 0,
4040 (*state)->rule.ptr->return_ttl);
4045 } else if (pf_status.debug >= PF_DEBUG_MISC) {
4046 kprintf("pf: BAD state: ");
4047 pf_print_state(*state);
4048 pf_print_flags(th->th_flags);
4049 kprintf(" seq=%u ack=%u len=%u ackskew=%d pkts=%d:%d "
4050 "dir=%s,%s\n", seq, ack, pd->p_len, ackskew,
4051 (*state)->packets[0], (*state)->packets[1],
4052 direction == PF_IN ? "in" : "out",
4053 direction == (*state)->direction ? "fwd" : "rev");
4054 kprintf("pf: State failure on: %c %c %c %c | %c %c\n",
4055 SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
4056 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
4058 (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
4059 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
4060 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
4061 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
4066 if (dst->scrub || src->scrub) {
4067 if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4068 src, dst, ©back))
4072 /* Any packets which have gotten here are to be passed */
4074 /* translate source/destination address, if necessary */
4075 if (STATE_TRANSLATE(*state)) {
4076 if (direction == PF_OUT)
4077 pf_change_ap(pd->src, &th->th_sport, pd->ip_sum,
4078 &th->th_sum, &(*state)->gwy.addr,
4079 (*state)->gwy.port, 0, pd->af);
4081 pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum,
4082 &th->th_sum, &(*state)->lan.addr,
4083 (*state)->lan.port, 0, pd->af);
4084 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4085 } else if (copyback) {
4086 /* Copyback sequence modulation or stateful scrub changes */
4087 m_copyback(m, off, sizeof(*th), (caddr_t)th);
4094 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif,
4095 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4097 struct pf_state_peer *src, *dst;
4098 struct pf_state key;
4099 struct udphdr *uh = pd->hdr.udp;
4102 key.proto = IPPROTO_UDP;
4103 if (direction == PF_IN) {
4104 PF_ACPY(&key.ext.addr, pd->src, key.af);
4105 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4106 key.ext.port = uh->uh_sport;
4107 key.gwy.port = uh->uh_dport;
4109 PF_ACPY(&key.lan.addr, pd->src, key.af);
4110 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4111 key.lan.port = uh->uh_sport;
4112 key.ext.port = uh->uh_dport;
4117 if (direction == (*state)->direction) {
4118 src = &(*state)->src;
4119 dst = &(*state)->dst;
4121 src = &(*state)->dst;
4122 dst = &(*state)->src;
4126 if (src->state < PFUDPS_SINGLE)
4127 src->state = PFUDPS_SINGLE;
4128 if (dst->state == PFUDPS_SINGLE)
4129 dst->state = PFUDPS_MULTIPLE;
4131 /* update expire time */
4132 (*state)->expire = time_second;
4133 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
4134 (*state)->timeout = PFTM_UDP_MULTIPLE;
4136 (*state)->timeout = PFTM_UDP_SINGLE;
4138 /* translate source/destination address, if necessary */
4139 if (STATE_TRANSLATE(*state)) {
4140 if (direction == PF_OUT)
4141 pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum,
4142 &uh->uh_sum, &(*state)->gwy.addr,
4143 (*state)->gwy.port, 1, pd->af);
4145 pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum,
4146 &uh->uh_sum, &(*state)->lan.addr,
4147 (*state)->lan.port, 1, pd->af);
4148 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
4155 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif,
4156 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
4158 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
4159 u_int16_t icmpid = 0;
4160 u_int16_t *icmpsum = NULL;
4161 u_int8_t icmptype = 0;
4164 switch (pd->proto) {
4167 icmptype = pd->hdr.icmp->icmp_type;
4168 icmpid = pd->hdr.icmp->icmp_id;
4169 icmpsum = &pd->hdr.icmp->icmp_cksum;
4171 if (icmptype == ICMP_UNREACH ||
4172 icmptype == ICMP_SOURCEQUENCH ||
4173 icmptype == ICMP_REDIRECT ||
4174 icmptype == ICMP_TIMXCEED ||
4175 icmptype == ICMP_PARAMPROB)
4180 case IPPROTO_ICMPV6:
4181 icmptype = pd->hdr.icmp6->icmp6_type;
4182 icmpid = pd->hdr.icmp6->icmp6_id;
4183 icmpsum = &pd->hdr.icmp6->icmp6_cksum;
4185 if (icmptype == ICMP6_DST_UNREACH ||
4186 icmptype == ICMP6_PACKET_TOO_BIG ||
4187 icmptype == ICMP6_TIME_EXCEEDED ||
4188 icmptype == ICMP6_PARAM_PROB)
4197 * ICMP query/reply message not related to a TCP/UDP packet.
4198 * Search for an ICMP state.
4200 struct pf_state key;
4203 key.proto = pd->proto;
4204 if (direction == PF_IN) {
4205 PF_ACPY(&key.ext.addr, pd->src, key.af);
4206 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4207 key.ext.port = icmpid;
4208 key.gwy.port = icmpid;
4210 PF_ACPY(&key.lan.addr, pd->src, key.af);
4211 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4212 key.lan.port = icmpid;
4213 key.ext.port = icmpid;
4218 (*state)->expire = time_second;
4219 (*state)->timeout = PFTM_ICMP_ERROR_REPLY;
4221 /* translate source/destination address, if necessary */
4222 if (PF_ANEQ(&(*state)->lan.addr, &(*state)->gwy.addr, pd->af)) {
4223 if (direction == PF_OUT) {
4227 pf_change_a(&saddr->v4.s_addr,
4229 (*state)->gwy.addr.v4.s_addr, 0);
4235 &pd->hdr.icmp6->icmp6_cksum,
4236 &(*state)->gwy.addr, 0);
4238 sizeof(struct icmp6_hdr),
4239 (caddr_t)pd->hdr.icmp6);
4247 pf_change_a(&daddr->v4.s_addr,
4249 (*state)->lan.addr.v4.s_addr, 0);
4255 &pd->hdr.icmp6->icmp6_cksum,
4256 &(*state)->lan.addr, 0);
4258 sizeof(struct icmp6_hdr),
4259 (caddr_t)pd->hdr.icmp6);
4270 * ICMP error message in response to a TCP/UDP packet.
4271 * Extract the inner TCP/UDP header and search for that state.
4274 struct pf_pdesc pd2;
4279 struct ip6_hdr h2_6;
4289 /* offset of h2 in mbuf chain */
4290 ipoff2 = off + ICMP_MINLEN;
4292 if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
4293 NULL, NULL, pd2.af)) {
4294 DPFPRINTF(PF_DEBUG_MISC,
4295 ("pf: ICMP error message too short "
4300 * ICMP error messages don't refer to non-first
4304 * Note: We are dealing with an encapsulated
4305 * header. This means ip_off/ip_len are not
4306 * in host byte order!
4308 if (h2.ip_off & htons(IP_OFFMASK))
4311 /* offset of protocol header that follows h2 */
4312 off2 = ipoff2 + (h2.ip_hl << 2);
4314 pd2.proto = h2.ip_p;
4315 pd2.src = (struct pf_addr *)&h2.ip_src;
4316 pd2.dst = (struct pf_addr *)&h2.ip_dst;
4317 pd2.ip_sum = &h2.ip_sum;
4322 ipoff2 = off + sizeof(struct icmp6_hdr);
4324 if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
4325 NULL, NULL, pd2.af)) {
4326 DPFPRINTF(PF_DEBUG_MISC,
4327 ("pf: ICMP error message too short "
4331 pd2.proto = h2_6.ip6_nxt;
4332 pd2.src = (struct pf_addr *)&h2_6.ip6_src;
4333 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
4335 off2 = ipoff2 + sizeof(h2_6);
4337 switch (pd2.proto) {
4338 case IPPROTO_FRAGMENT:
4340 * ICMPv6 error messages for
4341 * non-first fragments
4345 case IPPROTO_HOPOPTS:
4346 case IPPROTO_ROUTING:
4347 case IPPROTO_DSTOPTS: {
4348 /* get next header and header length */
4349 struct ip6_ext opt6;
4351 if (!pf_pull_hdr(m, off2, &opt6,
4352 sizeof(opt6), NULL, NULL, pd2.af)) {
4353 DPFPRINTF(PF_DEBUG_MISC,
4354 ("pf: ICMPv6 short opt\n"));
4357 if (pd2.proto == IPPROTO_AH)
4358 off2 += (opt6.ip6e_len + 2) * 4;
4360 off2 += (opt6.ip6e_len + 1) * 8;
4361 pd2.proto = opt6.ip6e_nxt;
4362 /* goto the next header */
4369 } while (!terminal);
4374 switch (pd2.proto) {
4378 struct pf_state key;
4379 struct pf_state_peer *src, *dst;
4384 * Only the first 8 bytes of the TCP header can be
4385 * expected. Don't access any TCP header fields after
4386 * th_seq, an ackskew test is not possible.
4388 if (!pf_pull_hdr(m, off2, &th, 8, NULL, NULL, pd2.af)) {
4389 DPFPRINTF(PF_DEBUG_MISC,
4390 ("pf: ICMP error message too short "
4396 key.proto = IPPROTO_TCP;
4397 if (direction == PF_IN) {
4398 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4399 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4400 key.ext.port = th.th_dport;
4401 key.gwy.port = th.th_sport;
4403 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4404 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4405 key.lan.port = th.th_dport;
4406 key.ext.port = th.th_sport;
4411 if (direction == (*state)->direction) {
4412 src = &(*state)->dst;
4413 dst = &(*state)->src;
4415 src = &(*state)->src;
4416 dst = &(*state)->dst;
4419 if (src->wscale && dst->wscale &&
4420 !(th.th_flags & TH_SYN))
4421 dws = dst->wscale & PF_WSCALE_MASK;
4425 /* Demodulate sequence number */
4426 seq = ntohl(th.th_seq) - src->seqdiff;
4428 pf_change_a(&th.th_seq, icmpsum,
4433 if (!SEQ_GEQ(src->seqhi, seq) ||
4434 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws))) {
4435 if (pf_status.debug >= PF_DEBUG_MISC) {
4436 kprintf("pf: BAD ICMP %d:%d ",
4437 icmptype, pd->hdr.icmp->icmp_code);
4438 pf_print_host(pd->src, 0, pd->af);
4440 pf_print_host(pd->dst, 0, pd->af);
4441 kprintf(" state: ");
4442 pf_print_state(*state);
4443 kprintf(" seq=%u\n", seq);
4448 if (STATE_TRANSLATE(*state)) {
4449 if (direction == PF_IN) {
4450 pf_change_icmp(pd2.src, &th.th_sport,
4451 daddr, &(*state)->lan.addr,
4452 (*state)->lan.port, NULL,
4453 pd2.ip_sum, icmpsum,
4454 pd->ip_sum, 0, pd2.af);
4456 pf_change_icmp(pd2.dst, &th.th_dport,
4457 saddr, &(*state)->gwy.addr,
4458 (*state)->gwy.port, NULL,
4459 pd2.ip_sum, icmpsum,
4460 pd->ip_sum, 0, pd2.af);
4469 m_copyback(m, off, ICMP_MINLEN,
4470 (caddr_t)pd->hdr.icmp);
4471 m_copyback(m, ipoff2, sizeof(h2),
4478 sizeof(struct icmp6_hdr),
4479 (caddr_t)pd->hdr.icmp6);
4480 m_copyback(m, ipoff2, sizeof(h2_6),
4485 m_copyback(m, off2, 8, (caddr_t)&th);
4493 struct pf_state key;
4495 if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
4496 NULL, NULL, pd2.af)) {
4497 DPFPRINTF(PF_DEBUG_MISC,
4498 ("pf: ICMP error message too short "
4504 key.proto = IPPROTO_UDP;
4505 if (direction == PF_IN) {
4506 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4507 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4508 key.ext.port = uh.uh_dport;
4509 key.gwy.port = uh.uh_sport;
4511 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4512 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4513 key.lan.port = uh.uh_dport;
4514 key.ext.port = uh.uh_sport;
4519 if (STATE_TRANSLATE(*state)) {
4520 if (direction == PF_IN) {
4521 pf_change_icmp(pd2.src, &uh.uh_sport,
4522 daddr, &(*state)->lan.addr,
4523 (*state)->lan.port, &uh.uh_sum,
4524 pd2.ip_sum, icmpsum,
4525 pd->ip_sum, 1, pd2.af);
4527 pf_change_icmp(pd2.dst, &uh.uh_dport,
4528 saddr, &(*state)->gwy.addr,
4529 (*state)->gwy.port, &uh.uh_sum,
4530 pd2.ip_sum, icmpsum,
4531 pd->ip_sum, 1, pd2.af);
4536 m_copyback(m, off, ICMP_MINLEN,
4537 (caddr_t)pd->hdr.icmp);
4538 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4544 sizeof(struct icmp6_hdr),
4545 (caddr_t)pd->hdr.icmp6);
4546 m_copyback(m, ipoff2, sizeof(h2_6),
4551 m_copyback(m, off2, sizeof(uh), (caddr_t)&uh);
4558 case IPPROTO_ICMP: {
4560 struct pf_state key;
4562 if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
4563 NULL, NULL, pd2.af)) {
4564 DPFPRINTF(PF_DEBUG_MISC,
4565 ("pf: ICMP error message too short i"
4571 key.proto = IPPROTO_ICMP;
4572 if (direction == PF_IN) {
4573 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4574 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4575 key.ext.port = iih.icmp_id;
4576 key.gwy.port = iih.icmp_id;
4578 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4579 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4580 key.lan.port = iih.icmp_id;
4581 key.ext.port = iih.icmp_id;
4586 if (STATE_TRANSLATE(*state)) {
4587 if (direction == PF_IN) {
4588 pf_change_icmp(pd2.src, &iih.icmp_id,
4589 daddr, &(*state)->lan.addr,
4590 (*state)->lan.port, NULL,
4591 pd2.ip_sum, icmpsum,
4592 pd->ip_sum, 0, AF_INET);
4594 pf_change_icmp(pd2.dst, &iih.icmp_id,
4595 saddr, &(*state)->gwy.addr,
4596 (*state)->gwy.port, NULL,
4597 pd2.ip_sum, icmpsum,
4598 pd->ip_sum, 0, AF_INET);
4600 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
4601 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4602 m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih);
4610 case IPPROTO_ICMPV6: {
4611 struct icmp6_hdr iih;
4612 struct pf_state key;
4614 if (!pf_pull_hdr(m, off2, &iih,
4615 sizeof(struct icmp6_hdr), NULL, NULL, pd2.af)) {
4616 DPFPRINTF(PF_DEBUG_MISC,
4617 ("pf: ICMP error message too short "
4623 key.proto = IPPROTO_ICMPV6;
4624 if (direction == PF_IN) {
4625 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4626 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4627 key.ext.port = iih.icmp6_id;
4628 key.gwy.port = iih.icmp6_id;
4630 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4631 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4632 key.lan.port = iih.icmp6_id;
4633 key.ext.port = iih.icmp6_id;
4638 if (STATE_TRANSLATE(*state)) {
4639 if (direction == PF_IN) {
4640 pf_change_icmp(pd2.src, &iih.icmp6_id,
4641 daddr, &(*state)->lan.addr,
4642 (*state)->lan.port, NULL,
4643 pd2.ip_sum, icmpsum,
4644 pd->ip_sum, 0, AF_INET6);
4646 pf_change_icmp(pd2.dst, &iih.icmp6_id,
4647 saddr, &(*state)->gwy.addr,
4648 (*state)->gwy.port, NULL,
4649 pd2.ip_sum, icmpsum,
4650 pd->ip_sum, 0, AF_INET6);
4652 m_copyback(m, off, sizeof(struct icmp6_hdr),
4653 (caddr_t)pd->hdr.icmp6);
4654 m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6);
4655 m_copyback(m, off2, sizeof(struct icmp6_hdr),
4664 struct pf_state key;
4667 key.proto = pd2.proto;
4668 if (direction == PF_IN) {
4669 PF_ACPY(&key.ext.addr, pd2.dst, key.af);
4670 PF_ACPY(&key.gwy.addr, pd2.src, key.af);
4674 PF_ACPY(&key.lan.addr, pd2.dst, key.af);
4675 PF_ACPY(&key.ext.addr, pd2.src, key.af);
4682 if (STATE_TRANSLATE(*state)) {
4683 if (direction == PF_IN) {
4684 pf_change_icmp(pd2.src, NULL,
4685 daddr, &(*state)->lan.addr,
4687 pd2.ip_sum, icmpsum,
4688 pd->ip_sum, 0, pd2.af);
4690 pf_change_icmp(pd2.dst, NULL,
4691 saddr, &(*state)->gwy.addr,
4693 pd2.ip_sum, icmpsum,
4694 pd->ip_sum, 0, pd2.af);
4699 m_copyback(m, off, ICMP_MINLEN,
4700 (caddr_t)pd->hdr.icmp);
4701 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
4707 sizeof(struct icmp6_hdr),
4708 (caddr_t)pd->hdr.icmp6);
4709 m_copyback(m, ipoff2, sizeof(h2_6),
4724 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif,
4725 struct pf_pdesc *pd)
4727 struct pf_state_peer *src, *dst;
4728 struct pf_state key;
4731 key.proto = pd->proto;
4732 if (direction == PF_IN) {
4733 PF_ACPY(&key.ext.addr, pd->src, key.af);
4734 PF_ACPY(&key.gwy.addr, pd->dst, key.af);
4738 PF_ACPY(&key.lan.addr, pd->src, key.af);
4739 PF_ACPY(&key.ext.addr, pd->dst, key.af);
4746 if (direction == (*state)->direction) {
4747 src = &(*state)->src;
4748 dst = &(*state)->dst;
4750 src = &(*state)->dst;
4751 dst = &(*state)->src;
4755 if (src->state < PFOTHERS_SINGLE)
4756 src->state = PFOTHERS_SINGLE;
4757 if (dst->state == PFOTHERS_SINGLE)
4758 dst->state = PFOTHERS_MULTIPLE;
4760 /* update expire time */
4761 (*state)->expire = time_second;
4762 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
4763 (*state)->timeout = PFTM_OTHER_MULTIPLE;
4765 (*state)->timeout = PFTM_OTHER_SINGLE;
4767 /* translate source/destination address, if necessary */
4768 if (STATE_TRANSLATE(*state)) {
4769 if (direction == PF_OUT)
4773 pf_change_a(&pd->src->v4.s_addr,
4774 pd->ip_sum, (*state)->gwy.addr.v4.s_addr,
4780 PF_ACPY(pd->src, &(*state)->gwy.addr, pd->af);
4788 pf_change_a(&pd->dst->v4.s_addr,
4789 pd->ip_sum, (*state)->lan.addr.v4.s_addr,
4795 PF_ACPY(pd->dst, &(*state)->lan.addr, pd->af);
4805 * ipoff and off are measured from the start of the mbuf chain.
4806 * h must be at "ipoff" on the mbuf chain.
4809 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
4810 u_short *actionp, u_short *reasonp, sa_family_t af)
4815 struct ip *h = mtod(m, struct ip *);
4816 u_int16_t fragoff = (h->ip_off & IP_OFFMASK) << 3;
4820 ACTION_SET(actionp, PF_PASS);
4822 ACTION_SET(actionp, PF_DROP);
4823 REASON_SET(reasonp, PFRES_FRAG);
4827 if (m->m_pkthdr.len < off + len ||
4828 h->ip_len < off + len) {
4829 ACTION_SET(actionp, PF_DROP);
4830 REASON_SET(reasonp, PFRES_SHORT);
4838 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
4840 if (m->m_pkthdr.len < off + len ||
4841 (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
4842 (unsigned)(off + len)) {
4843 ACTION_SET(actionp, PF_DROP);
4844 REASON_SET(reasonp, PFRES_SHORT);
4851 m_copydata(m, off, len, p);
4856 pf_routable(struct pf_addr *addr, sa_family_t af)
4858 struct sockaddr_in *dst;
4862 bzero(&ro, sizeof(ro));
4863 dst = satosin(&ro.ro_dst);
4864 dst->sin_family = af;
4865 dst->sin_len = sizeof(*dst);
4866 dst->sin_addr = addr->v4;
4867 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
4869 if (ro.ro_rt != NULL) {
4879 pf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
4882 struct mbuf *m0, *m1;
4883 struct route iproute;
4884 struct route *ro = NULL;
4885 struct sockaddr_in *dst;
4887 struct ifnet *ifp = NULL;
4888 struct pf_addr naddr;
4889 struct pf_src_node *sn = NULL;
4893 if (m == NULL || *m == NULL || r == NULL ||
4894 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
4895 panic("pf_route: invalid parameters");
4897 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
4898 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
4899 (*m)->m_pkthdr.pf_routed = 1;
4901 if ((*m)->m_pkthdr.pf_routed > 3) {
4906 (*m)->m_pkthdr.pf_routed++;
4909 if (r->rt == PF_DUPTO) {
4910 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
4913 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
4918 if (m0->m_len < sizeof(struct ip))
4919 panic("pf_route: m0->m_len < sizeof(struct ip)");
4920 ip = mtod(m0, struct ip *);
4923 bzero((caddr_t)ro, sizeof(*ro));
4924 dst = satosin(&ro->ro_dst);
4925 dst->sin_family = AF_INET;
4926 dst->sin_len = sizeof(*dst);
4927 dst->sin_addr = ip->ip_dst;
4929 if (r->rt == PF_FASTROUTE) {
4931 if (ro->ro_rt == 0) {
4932 ipstat.ips_noroute++;
4936 ifp = ro->ro_rt->rt_ifp;
4937 ro->ro_rt->rt_use++;
4939 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
4940 dst = satosin(ro->ro_rt->rt_gateway);
4942 if (TAILQ_EMPTY(&r->rpool.list))
4943 panic("pf_route: TAILQ_EMPTY(&r->rpool.list)");
4945 pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
4947 if (!PF_AZERO(&naddr, AF_INET))
4948 dst->sin_addr.s_addr = naddr.v4.s_addr;
4949 ifp = r->rpool.cur->kif ?
4950 r->rpool.cur->kif->pfik_ifp : NULL;
4952 if (!PF_AZERO(&s->rt_addr, AF_INET))
4953 dst->sin_addr.s_addr =
4954 s->rt_addr.v4.s_addr;
4955 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
4962 if (pf_test(PF_OUT, ifp, &m0) != PF_PASS)
4964 else if (m0 == NULL)
4966 if (m0->m_len < sizeof(struct ip))
4967 panic("pf_route: m0->m_len < sizeof(struct ip)");
4968 ip = mtod(m0, struct ip *);
4971 /* Copied from ip_output. */
4972 m0->m_pkthdr.csum_flags |= CSUM_IP;
4973 sw_csum = m0->m_pkthdr.csum_flags & ~ifp->if_hwassist;
4974 if (sw_csum & CSUM_DELAY_DATA) {
4975 in_delayed_cksum(m0);
4976 sw_csum &= ~CSUM_DELAY_DATA;
4978 m0->m_pkthdr.csum_flags &= ifp->if_hwassist;
4981 * If small enough for interface, or the interface will take
4982 * care of the fragmentation for us, can just send directly.
4984 if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) &&
4985 (ip->ip_off & IP_DF) == 0)) {
4986 ip->ip_len = htons(ip->ip_len);
4987 ip->ip_off = htons(ip->ip_off);
4989 if (sw_csum & CSUM_DELAY_IP) {
4991 if (ip->ip_v == IPVERSION &&
4992 (ip->ip_hl << 2) == sizeof(*ip)) {
4993 ip->ip_sum = in_cksum_hdr(ip);
4995 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
4999 lwkt_serialize_enter(ifp->if_serializer);
5000 error = (*ifp->if_output)(ifp, m0, sintosa(dst), ro->ro_rt);
5001 lwkt_serialize_exit(ifp->if_serializer);
5006 * Too large for interface; fragment if possible.
5007 * Must be able to put at least 8 bytes per fragment.
5009 if (ip->ip_off & IP_DF) {
5010 ipstat.ips_cantfrag++;
5011 if (r->rt != PF_DUPTO) {
5012 icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
5020 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist, sw_csum);
5024 for (m0 = m1; m0; m0 = m1) {
5028 lwkt_serialize_enter(ifp->if_serializer);
5029 error = (*ifp->if_output)(ifp, m0, sintosa(dst),
5031 lwkt_serialize_exit(ifp->if_serializer);
5038 ipstat.ips_fragmented++;
5041 if (r->rt != PF_DUPTO)
5043 if (ro == &iproute && ro->ro_rt)
5055 pf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
5059 struct route_in6 ip6route;
5060 struct route_in6 *ro;
5061 struct sockaddr_in6 *dst;
5062 struct ip6_hdr *ip6;
5063 struct ifnet *ifp = NULL;
5064 struct pf_addr naddr;
5065 struct pf_src_node *sn = NULL;
5068 if (m == NULL || *m == NULL || r == NULL ||
5069 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
5070 panic("pf_route6: invalid parameters");
5072 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
5073 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
5074 (*m)->m_pkthdr.pf_routed = 1;
5076 if ((*m)->m_pkthdr.pf_routed > 3) {
5081 (*m)->m_pkthdr.pf_routed++;
5084 if (r->rt == PF_DUPTO) {
5085 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
5088 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
5093 if (m0->m_len < sizeof(struct ip6_hdr))
5094 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5095 ip6 = mtod(m0, struct ip6_hdr *);
5098 bzero((caddr_t)ro, sizeof(*ro));
5099 dst = (struct sockaddr_in6 *)&ro->ro_dst;
5100 dst->sin6_family = AF_INET6;
5101 dst->sin6_len = sizeof(*dst);
5102 dst->sin6_addr = ip6->ip6_dst;
5105 if (r->rt == PF_FASTROUTE) {
5106 m0->m_pkthdr.fw_flags |= PF_MBUF_GENERATED;
5107 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
5111 if (TAILQ_EMPTY(&r->rpool.list))
5112 panic("pf_route6: TAILQ_EMPTY(&r->rpool.list)");
5114 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
5116 if (!PF_AZERO(&naddr, AF_INET6))
5117 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5119 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
5121 if (!PF_AZERO(&s->rt_addr, AF_INET6))
5122 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
5123 &s->rt_addr, AF_INET6);
5124 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
5130 if (pf_test6(PF_OUT, ifp, &m0) != PF_PASS)
5132 else if (m0 == NULL)
5134 if (m0->m_len < sizeof(struct ip6_hdr))
5135 panic("pf_route6: m0->m_len < sizeof(struct ip6_hdr)");
5136 ip6 = mtod(m0, struct ip6_hdr *);
5140 * If the packet is too large for the outgoing interface,
5141 * send back an icmp6 error.
5143 if (IN6_IS_ADDR_LINKLOCAL(&dst->sin6_addr))
5144 dst->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
5145 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
5146 error = nd6_output(ifp, ifp, m0, dst, NULL);
5148 in6_ifstat_inc(ifp, ifs6_in_toobig);
5149 if (r->rt != PF_DUPTO)
5150 icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
5156 if (r->rt != PF_DUPTO)
5168 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
5169 * off is the offset where the protocol header starts
5170 * len is the total length of protocol header plus payload
5171 * returns 0 when the checksum is valid, otherwise returns 1.
5175 * FreeBSD supports cksum offload for the following drivers.
5176 * em(4), gx(4), lge(4), nge(4), ti(4), xl(4)
5177 * If we can make full use of it we would outperform ipfw/ipfilter in
5178 * very heavy traffic.
5179 * I have not tested 'cause I don't have NICs that supports cksum offload.
5180 * (There might be problems. Typical phenomena would be
5181 * 1. No route message for UDP packet.
5182 * 2. No connection acceptance from external hosts regardless of rule set.)
5185 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p,
5192 if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
5194 if (m->m_pkthdr.len < off + len)
5200 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
5201 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
5202 sum = m->m_pkthdr.csum_data;
5204 ip = mtod(m, struct ip *);
5205 sum = in_pseudo(ip->ip_src.s_addr,
5206 ip->ip_dst.s_addr, htonl((u_short)len +
5207 m->m_pkthdr.csum_data + p));
5215 case IPPROTO_ICMPV6:
5225 if (p == IPPROTO_ICMP) {
5230 sum = in_cksum(m, len);
5234 if (m->m_len < sizeof(struct ip))
5236 sum = in_cksum_range(m, p, off, len);
5238 m->m_pkthdr.csum_flags |=
5241 m->m_pkthdr.csum_data = 0xffff;
5247 if (m->m_len < sizeof(struct ip6_hdr))
5249 sum = in6_cksum(m, p, off, len);
5252 * IPv6 H/W cksum off-load not supported yet!
5255 * m->m_pkthdr.csum_flags |=
5256 * (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
5257 * m->m_pkthdr.csum_data = 0xffff;
5269 tcpstat.tcps_rcvbadsum++;
5272 udpstat.udps_badsum++;
5275 icmpstat.icps_checksum++;
5278 case IPPROTO_ICMPV6:
5279 icmp6stat.icp6s_checksum++;
5290 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0)
5292 struct pfi_kif *kif;
5293 u_short action, reason = 0, log = 0;
5294 struct mbuf *m = *m0;
5295 struct ip *h = NULL;
5296 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5297 struct pf_state *s = NULL;
5298 struct pf_ruleset *ruleset = NULL;
5300 int off, dirndx, pqid = 0;
5302 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5305 kif = pfi_index2kif[ifp->if_index];
5310 if ((m->m_flags & M_PKTHDR) == 0)
5311 panic("non-M_PKTHDR is passed to pf_test");
5314 memset(&pd, 0, sizeof(pd));
5315 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5317 REASON_SET(&reason, PFRES_SHORT);
5322 /* We do IP header normalization and packet reassembly here */
5323 if (pf_normalize_ip(m0, dir, kif, &reason) != PF_PASS) {
5328 h = mtod(m, struct ip *);
5330 off = h->ip_hl << 2;
5331 if (off < (int)sizeof(*h)) {
5333 REASON_SET(&reason, PFRES_SHORT);
5338 pd.src = (struct pf_addr *)&h->ip_src;
5339 pd.dst = (struct pf_addr *)&h->ip_dst;
5340 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET);
5341 pd.ip_sum = &h->ip_sum;
5345 pd.tot_len = h->ip_len;
5347 /* handle fragments that didn't get reassembled by normalization */
5348 if (h->ip_off & (IP_MF | IP_OFFMASK)) {
5349 action = pf_test_fragment(&r, dir, kif, m, h,
5360 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5361 &action, &reason, AF_INET)) {
5362 log = action != PF_PASS;
5365 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5366 h->ip_len - off, IPPROTO_TCP, AF_INET)) {
5370 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5371 if ((th.th_flags & TH_ACK) && pd.p_len == 0)
5373 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5374 if (action == PF_DROP)
5376 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5378 if (action == PF_PASS) {
5380 pfsync_update_state(s);
5385 } else if (s == NULL)
5386 action = pf_test_tcp(&r, &s, dir, kif,
5387 m, off, h, &pd, &a, &ruleset);
5395 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5396 &action, &reason, AF_INET)) {
5397 log = action != PF_PASS;
5400 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5401 off, h->ip_len - off, IPPROTO_UDP, AF_INET)) {
5405 if (uh.uh_dport == 0 ||
5406 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5407 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5411 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5412 if (action == PF_PASS) {
5414 pfsync_update_state(s);
5419 } else if (s == NULL)
5420 action = pf_test_udp(&r, &s, dir, kif,
5421 m, off, h, &pd, &a, &ruleset);
5425 case IPPROTO_ICMP: {
5429 if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
5430 &action, &reason, AF_INET)) {
5431 log = action != PF_PASS;
5434 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5435 h->ip_len - off, IPPROTO_ICMP, AF_INET)) {
5439 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd);
5440 if (action == PF_PASS) {
5442 pfsync_update_state(s);
5447 } else if (s == NULL)
5448 action = pf_test_icmp(&r, &s, dir, kif,
5449 m, off, h, &pd, &a, &ruleset);
5454 action = pf_test_state_other(&s, dir, kif, &pd);
5455 if (action == PF_PASS) {
5457 pfsync_update_state(s);
5462 } else if (s == NULL)
5463 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5469 if (action == PF_PASS && h->ip_hl > 5 &&
5470 !((s && s->allow_opts) || r->allow_opts)) {
5472 REASON_SET(&reason, PFRES_SHORT);
5474 DPFPRINTF(PF_DEBUG_MISC,
5475 ("pf: dropping packet with ip options\n"));
5479 if (action == PF_PASS && r->qid) {
5480 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5481 if (pd.tos == IPTOS_LOWDELAY)
5482 m->m_pkthdr.altq_qid = r->pqid;
5484 m->m_pkthdr.altq_qid = r->qid;
5486 m->m_pkthdr.fw_flags |= ALTQ_MBUF_STATE_HASHED;
5487 m->m_pkthdr.altq_state_hash = pf_state_hash(s);
5489 m->m_pkthdr.ecn_af = AF_INET;
5490 m->m_pkthdr.header = h;
5495 * connections redirected to loopback should not match sockets
5496 * bound specifically to loopback due to security implications,
5497 * see tcp_input() and in_pcblookup_listen().
5499 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5500 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5501 (s->nat_rule.ptr->action == PF_RDR ||
5502 s->nat_rule.ptr->action == PF_BINAT) &&
5503 (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
5505 REASON_SET(&reason, PFRES_MEMORY);
5508 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5511 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, a, ruleset);
5513 kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5514 kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++;
5516 if (action == PF_PASS || r->action == PF_DROP) {
5518 r->bytes += pd.tot_len;
5521 a->bytes += pd.tot_len;
5524 dirndx = (dir == s->direction) ? 0 : 1;
5525 s->packets[dirndx]++;
5526 s->bytes[dirndx] += pd.tot_len;
5527 if (s->nat_rule.ptr != NULL) {
5528 s->nat_rule.ptr->packets++;
5529 s->nat_rule.ptr->bytes += pd.tot_len;
5531 if (s->src_node != NULL) {
5532 s->src_node->packets++;
5533 s->src_node->bytes += pd.tot_len;
5535 if (s->nat_src_node != NULL) {
5536 s->nat_src_node->packets++;
5537 s->nat_src_node->bytes += pd.tot_len;
5541 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5545 * XXX: we need to make sure that the addresses
5546 * passed to pfr_update_stats() are the same than
5547 * the addresses used during matching (pfr_match)
5549 if (r == &pf_default_rule) {
5551 x = (s == NULL || s->direction == dir) ?
5552 &pd.baddr : &pd.naddr;
5554 x = (s == NULL || s->direction == dir) ?
5555 &pd.naddr : &pd.baddr;
5556 if (x == &pd.baddr || s == NULL) {
5557 /* we need to change the address */
5564 if (tr->src.addr.type == PF_ADDR_TABLE)
5565 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5566 s->direction == dir) ? pd.src : pd.dst, pd.af,
5567 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5569 if (tr->dst.addr.type == PF_ADDR_TABLE)
5570 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5571 s->direction == dir) ? pd.dst : pd.src, pd.af,
5572 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5577 if (action == PF_SYNPROXY_DROP) {
5582 /* pf_route can free the mbuf causing *m0 to become NULL */
5583 pf_route(m0, r, dir, ifp, s);
5591 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0)
5593 struct pfi_kif *kif;
5594 u_short action, reason = 0, log = 0;
5595 struct mbuf *m = *m0;
5596 struct ip6_hdr *h = NULL;
5597 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
5598 struct pf_state *s = NULL;
5599 struct pf_ruleset *ruleset = NULL;
5601 int off, terminal = 0, dirndx;
5603 if (!pf_status.running || (m->m_pkthdr.fw_flags & PF_MBUF_GENERATED))
5606 kif = pfi_index2kif[ifp->if_index];
5611 if ((m->m_flags & M_PKTHDR) == 0)
5612 panic("non-M_PKTHDR is passed to pf_test");
5615 memset(&pd, 0, sizeof(pd));
5616 if (m->m_pkthdr.len < (int)sizeof(*h)) {
5618 REASON_SET(&reason, PFRES_SHORT);
5623 /* We do IP header normalization and packet reassembly here */
5624 if (pf_normalize_ip6(m0, dir, kif, &reason) != PF_PASS) {
5629 h = mtod(m, struct ip6_hdr *);
5631 pd.src = (struct pf_addr *)&h->ip6_src;
5632 pd.dst = (struct pf_addr *)&h->ip6_dst;
5633 PF_ACPY(&pd.baddr, dir == PF_OUT ? pd.src : pd.dst, AF_INET6);
5637 pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
5639 off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
5640 pd.proto = h->ip6_nxt;
5643 case IPPROTO_FRAGMENT:
5644 action = pf_test_fragment(&r, dir, kif, m, h,
5646 if (action == PF_DROP)
5647 REASON_SET(&reason, PFRES_FRAG);
5650 case IPPROTO_HOPOPTS:
5651 case IPPROTO_ROUTING:
5652 case IPPROTO_DSTOPTS: {
5653 /* get next header and header length */
5654 struct ip6_ext opt6;
5656 if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
5657 NULL, NULL, pd.af)) {
5658 DPFPRINTF(PF_DEBUG_MISC,
5659 ("pf: IPv6 short opt\n"));
5661 REASON_SET(&reason, PFRES_SHORT);
5665 if (pd.proto == IPPROTO_AH)
5666 off += (opt6.ip6e_len + 2) * 4;
5668 off += (opt6.ip6e_len + 1) * 8;
5669 pd.proto = opt6.ip6e_nxt;
5670 /* goto the next header */
5677 } while (!terminal);
5685 if (!pf_pull_hdr(m, off, &th, sizeof(th),
5686 &action, &reason, AF_INET6)) {
5687 log = action != PF_PASS;
5690 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5691 ntohs(h->ip6_plen), IPPROTO_TCP, AF_INET6)) {
5695 pd.p_len = pd.tot_len - off - (th.th_off << 2);
5696 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
5697 if (action == PF_DROP)
5699 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
5701 if (action == PF_PASS) {
5703 pfsync_update_state(s);
5708 } else if (s == NULL)
5709 action = pf_test_tcp(&r, &s, dir, kif,
5710 m, off, h, &pd, &a, &ruleset);
5718 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
5719 &action, &reason, AF_INET6)) {
5720 log = action != PF_PASS;
5723 if (dir == PF_IN && uh.uh_sum && pf_check_proto_cksum(m,
5724 off, ntohs(h->ip6_plen), IPPROTO_UDP, AF_INET6)) {
5728 if (uh.uh_dport == 0 ||
5729 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
5730 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
5734 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
5735 if (action == PF_PASS) {
5737 pfsync_update_state(s);
5742 } else if (s == NULL)
5743 action = pf_test_udp(&r, &s, dir, kif,
5744 m, off, h, &pd, &a, &ruleset);
5748 case IPPROTO_ICMPV6: {
5749 struct icmp6_hdr ih;
5752 if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
5753 &action, &reason, AF_INET6)) {
5754 log = action != PF_PASS;
5757 if (dir == PF_IN && pf_check_proto_cksum(m, off,
5758 ntohs(h->ip6_plen), IPPROTO_ICMPV6, AF_INET6)) {
5762 action = pf_test_state_icmp(&s, dir, kif,
5764 if (action == PF_PASS) {
5766 pfsync_update_state(s);
5771 } else if (s == NULL)
5772 action = pf_test_icmp(&r, &s, dir, kif,
5773 m, off, h, &pd, &a, &ruleset);
5778 action = pf_test_state_other(&s, dir, kif, &pd);
5779 if (action == PF_PASS) {
5783 } else if (s == NULL)
5784 action = pf_test_other(&r, &s, dir, kif, m, off, h,
5790 /* XXX handle IPv6 options, if not allowed. not implemented. */
5793 if (action == PF_PASS && r->qid) {
5794 m->m_pkthdr.fw_flags |= ALTQ_MBUF_TAGGED;
5795 if (pd.tos == IPTOS_LOWDELAY)
5796 m->m_pkthdr.altq_qid = r->pqid;
5798 m->m_pkthdr.altq_qid = r->qid;
5800 m->m_pkthdr.fw_flags |= ALTQ_MBUF_STATE_HASHED;
5801 m->m_pkthdr.altq_state_hash = pf_state_hash(s);
5803 m->m_pkthdr.ecn_af = AF_INET6;
5804 m->m_pkthdr.header = h;
5808 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
5809 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
5810 (s->nat_rule.ptr->action == PF_RDR ||
5811 s->nat_rule.ptr->action == PF_BINAT) &&
5812 IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) {
5814 REASON_SET(&reason, PFRES_MEMORY);
5817 m->m_pkthdr.fw_flags |= PF_MBUF_TRANSLATE_LOCALHOST;
5820 PFLOG_PACKET(kif, h, m, AF_INET6, dir, reason, r, a, ruleset);
5822 kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
5823 kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++;
5825 if (action == PF_PASS || r->action == PF_DROP) {
5827 r->bytes += pd.tot_len;
5830 a->bytes += pd.tot_len;
5833 dirndx = (dir == s->direction) ? 0 : 1;
5834 s->packets[dirndx]++;
5835 s->bytes[dirndx] += pd.tot_len;
5836 if (s->nat_rule.ptr != NULL) {
5837 s->nat_rule.ptr->packets++;
5838 s->nat_rule.ptr->bytes += pd.tot_len;
5840 if (s->src_node != NULL) {
5841 s->src_node->packets++;
5842 s->src_node->bytes += pd.tot_len;
5844 if (s->nat_src_node != NULL) {
5845 s->nat_src_node->packets++;
5846 s->nat_src_node->bytes += pd.tot_len;
5850 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
5854 * XXX: we need to make sure that the addresses
5855 * passed to pfr_update_stats() are the same than
5856 * the addresses used during matching (pfr_match)
5858 if (r == &pf_default_rule) {
5860 x = (s == NULL || s->direction == dir) ?
5861 &pd.baddr : &pd.naddr;
5863 x = (s == NULL || s->direction == dir) ?
5864 &pd.naddr : &pd.baddr;
5866 if (x == &pd.baddr || s == NULL) {
5873 if (tr->src.addr.type == PF_ADDR_TABLE)
5874 pfr_update_stats(tr->src.addr.p.tbl, (s == NULL ||
5875 s->direction == dir) ? pd.src : pd.dst, pd.af,
5876 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5878 if (tr->dst.addr.type == PF_ADDR_TABLE)
5879 pfr_update_stats(tr->dst.addr.p.tbl, (s == NULL ||
5880 s->direction == dir) ? pd.dst : pd.src, pd.af,
5881 pd.tot_len, dir == PF_OUT, r->action == PF_PASS,
5886 if (action == PF_SYNPROXY_DROP) {
5891 /* pf_route6 can free the mbuf causing *m0 to become NULL */
5892 pf_route6(m0, r, dir, ifp, s);