2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002 - 2008 Henning Brauer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * Effort sponsored in part by the Defense Advanced Research Projects
33 * Agency (DARPA) and Air Force Research Laboratory, Air Force
34 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/filio.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/endian.h>
53 #include <sys/kthread.h>
54 #include <sys/spinlock.h>
56 #include <machine/inttypes.h>
61 #include <net/if_types.h>
63 #include <net/netisr2.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/tcp.h>
72 #include <netinet/tcp_seq.h>
73 #include <netinet/udp.h>
74 #include <netinet/ip_icmp.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/tcp_timer.h>
77 #include <netinet/tcp_var.h>
78 #include <netinet/udp_var.h>
79 #include <netinet/icmp_var.h>
80 #include <netinet/if_ether.h>
82 #include <net/pf/pfvar.h>
83 #include <net/pf/if_pflog.h>
85 #include <net/pf/if_pfsync.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
95 #include <sys/in_cksum.h>
96 #include <sys/ucred.h>
97 #include <machine/limits.h>
98 #include <sys/msgport2.h>
99 #include <sys/spinlock2.h>
100 #include <net/netmsg2.h>
101 #include <net/toeplitz2.h>
103 extern int ip_optcopy(struct ip *, struct ip *);
104 extern int debug_pfugidhack;
107 * pf_token - shared lock for cpu-localized operations,
108 * exclusive lock otherwise.
110 * pf_gtoken- exclusive lock used for initialization.
112 * pf_spin - only used to atomically fetch and increment stateid
115 struct lwkt_token pf_token = LWKT_TOKEN_INITIALIZER(pf_token);
116 struct lwkt_token pf_gtoken = LWKT_TOKEN_INITIALIZER(pf_gtoken);
117 #if __SIZEOF_LONG__ != 8
118 struct spinlock pf_spin = SPINLOCK_INITIALIZER(pf_spin, "pf_spin");
121 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
123 #define FAIL(code) { error = (code); goto done; }
129 /* mask radix tree */
130 struct radix_node_head *pf_maskhead;
133 struct pf_state_tree pf_statetbl[MAXCPU+1]; /* incls one global table */
135 struct pf_altqqueue pf_altqs[2];
136 struct pf_palist pf_pabuf;
137 struct pf_altqqueue *pf_altqs_active;
138 struct pf_altqqueue *pf_altqs_inactive;
139 struct pf_status pf_status;
141 u_int32_t ticket_altqs_active;
142 u_int32_t ticket_altqs_inactive;
143 int altqs_inactive_open;
144 u_int32_t ticket_pabuf;
146 MD5_CTX pf_tcp_secret_ctx;
147 u_char pf_tcp_secret[16];
148 int pf_tcp_secret_init;
151 struct pf_anchor_stackframe {
152 struct pf_ruleset *rs;
154 struct pf_anchor_node *parent;
155 struct pf_anchor *child;
156 } pf_anchor_stack[64];
158 struct malloc_type *pf_src_tree_pl, *pf_rule_pl, *pf_pooladdr_pl;
159 struct malloc_type *pf_state_pl, *pf_state_key_pl, *pf_state_item_pl;
160 struct malloc_type *pf_altq_pl;
162 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
164 void pf_init_threshold(struct pf_threshold *, u_int32_t,
166 void pf_add_threshold(struct pf_threshold *);
167 int pf_check_threshold(struct pf_threshold *);
169 void pf_change_ap(struct pf_addr *, u_int16_t *,
170 u_int16_t *, u_int16_t *, struct pf_addr *,
171 u_int16_t, u_int8_t, sa_family_t);
172 int pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *,
173 struct tcphdr *, struct pf_state_peer *);
175 void pf_change_a6(struct pf_addr *, u_int16_t *,
176 struct pf_addr *, u_int8_t);
178 void pf_change_icmp(struct pf_addr *, u_int16_t *,
179 struct pf_addr *, struct pf_addr *, u_int16_t,
180 u_int16_t *, u_int16_t *, u_int16_t *,
181 u_int16_t *, u_int8_t, sa_family_t);
182 void pf_send_tcp(const struct pf_rule *, sa_family_t,
183 const struct pf_addr *, const struct pf_addr *,
184 u_int16_t, u_int16_t, u_int32_t, u_int32_t,
185 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
186 u_int16_t, struct ether_header *, struct ifnet *);
187 void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t,
188 sa_family_t, struct pf_rule *);
189 struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *,
190 int, int, struct pfi_kif *,
191 struct pf_addr *, u_int16_t, struct pf_addr *,
193 struct pf_rule *pf_get_translation(struct pf_pdesc *, struct mbuf *,
194 int, int, struct pfi_kif *, struct pf_src_node **,
195 struct pf_state_key **, struct pf_state_key **,
196 struct pf_state_key **, struct pf_state_key **,
197 struct pf_addr *, struct pf_addr *,
198 u_int16_t, u_int16_t);
199 void pf_detach_state(struct pf_state *);
200 int pf_state_key_setup(struct pf_pdesc *, struct pf_rule *,
201 struct pf_state_key **, struct pf_state_key **,
202 struct pf_state_key **, struct pf_state_key **,
203 struct pf_addr *, struct pf_addr *,
204 u_int16_t, u_int16_t);
205 void pf_state_key_detach(struct pf_state *, int);
206 u_int32_t pf_tcp_iss(struct pf_pdesc *);
207 int pf_test_rule(struct pf_rule **, struct pf_state **,
208 int, struct pfi_kif *, struct mbuf *, int,
209 void *, struct pf_pdesc *, struct pf_rule **,
210 struct pf_ruleset **, struct ifqueue *, struct inpcb *);
211 static __inline int pf_create_state(struct pf_rule *, struct pf_rule *,
212 struct pf_rule *, struct pf_pdesc *,
213 struct pf_src_node *, struct pf_state_key *,
214 struct pf_state_key *, struct pf_state_key *,
215 struct pf_state_key *, struct mbuf *, int,
216 u_int16_t, u_int16_t, int *, struct pfi_kif *,
217 struct pf_state **, int, u_int16_t, u_int16_t,
219 int pf_test_fragment(struct pf_rule **, int,
220 struct pfi_kif *, struct mbuf *, void *,
221 struct pf_pdesc *, struct pf_rule **,
222 struct pf_ruleset **);
223 int pf_tcp_track_full(struct pf_state_peer *,
224 struct pf_state_peer *, struct pf_state **,
225 struct pfi_kif *, struct mbuf *, int,
226 struct pf_pdesc *, u_short *, int *);
227 int pf_tcp_track_sloppy(struct pf_state_peer *,
228 struct pf_state_peer *, struct pf_state **,
229 struct pf_pdesc *, u_short *);
230 int pf_test_state_tcp(struct pf_state **, int,
231 struct pfi_kif *, struct mbuf *, int,
232 void *, struct pf_pdesc *, u_short *);
233 int pf_test_state_udp(struct pf_state **, int,
234 struct pfi_kif *, struct mbuf *, int,
235 void *, struct pf_pdesc *);
236 int pf_test_state_icmp(struct pf_state **, int,
237 struct pfi_kif *, struct mbuf *, int,
238 void *, struct pf_pdesc *, u_short *);
239 int pf_test_state_other(struct pf_state **, int,
240 struct pfi_kif *, struct mbuf *, struct pf_pdesc *);
241 void pf_step_into_anchor(int *, struct pf_ruleset **, int,
242 struct pf_rule **, struct pf_rule **, int *);
243 int pf_step_out_of_anchor(int *, struct pf_ruleset **,
244 int, struct pf_rule **, struct pf_rule **,
246 void pf_hash(struct pf_addr *, struct pf_addr *,
247 struct pf_poolhashkey *, sa_family_t);
248 int pf_map_addr(u_int8_t, struct pf_rule *,
249 struct pf_addr *, struct pf_addr *,
250 struct pf_addr *, struct pf_src_node **);
251 int pf_get_sport(struct pf_pdesc *,
252 sa_family_t, u_int8_t, struct pf_rule *,
253 struct pf_addr *, struct pf_addr *,
254 u_int16_t, u_int16_t,
255 struct pf_addr *, u_int16_t *,
256 u_int16_t, u_int16_t,
257 struct pf_src_node **);
258 void pf_route(struct mbuf **, struct pf_rule *, int,
259 struct ifnet *, struct pf_state *,
261 void pf_route6(struct mbuf **, struct pf_rule *, int,
262 struct ifnet *, struct pf_state *,
264 u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t,
266 u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t,
268 u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t,
270 void pf_set_rt_ifp(struct pf_state *,
272 int pf_check_proto_cksum(struct mbuf *, int, int,
273 u_int8_t, sa_family_t);
274 struct pf_divert *pf_get_divert(struct mbuf *);
275 void pf_print_state_parts(struct pf_state *,
276 struct pf_state_key *, struct pf_state_key *);
277 int pf_addr_wrap_neq(struct pf_addr_wrap *,
278 struct pf_addr_wrap *);
279 struct pf_state *pf_find_state(struct pfi_kif *,
280 struct pf_state_key_cmp *, u_int, struct mbuf *);
281 int pf_src_connlimit(struct pf_state *);
282 int pf_check_congestion(struct ifqueue *);
284 extern int pf_end_threads;
286 struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX] = {
287 { &pf_state_pl, PFSTATE_HIWAT },
288 { &pf_src_tree_pl, PFSNODE_HIWAT },
289 { &pf_frent_pl, PFFRAG_FRENT_HIWAT },
290 { &pfr_ktable_pl, PFR_KTABLE_HIWAT },
291 { &pfr_kentry_pl, PFR_KENTRY_HIWAT }
295 * If route-to and direction is out we match with no further processing
296 * (rt_kif must be assigned and not equal to the out interface)
297 * If reply-to and direction is in we match with no further processing
298 * (rt_kif must be assigned and not equal to the in interface)
300 #define STATE_LOOKUP(i, k, d, s, m) \
302 s = pf_find_state(i, k, d, m); \
303 if (s == NULL || (s)->timeout == PFTM_PURGE) \
306 (((s)->rule.ptr->rt == PF_ROUTETO && \
307 (s)->rule.ptr->direction == PF_OUT) || \
308 ((s)->rule.ptr->rt == PF_REPLYTO && \
309 (s)->rule.ptr->direction == PF_IN)) && \
310 (s)->rt_kif != NULL && \
315 #define BOUND_IFACE(r, k) \
316 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
318 #define STATE_INC_COUNTERS(s) \
320 atomic_add_int(&s->rule.ptr->states_cur, 1); \
321 s->rule.ptr->states_tot++; \
322 if (s->anchor.ptr != NULL) { \
323 atomic_add_int(&s->anchor.ptr->states_cur, 1); \
324 s->anchor.ptr->states_tot++; \
326 if (s->nat_rule.ptr != NULL) { \
327 atomic_add_int(&s->nat_rule.ptr->states_cur, 1); \
328 s->nat_rule.ptr->states_tot++; \
332 #define STATE_DEC_COUNTERS(s) \
334 if (s->nat_rule.ptr != NULL) \
335 atomic_add_int(&s->nat_rule.ptr->states_cur, -1); \
336 if (s->anchor.ptr != NULL) \
337 atomic_add_int(&s->anchor.ptr->states_cur, -1); \
338 atomic_add_int(&s->rule.ptr->states_cur, -1); \
341 static MALLOC_DEFINE(M_PFSTATEPL, "pfstatepl", "pf state pool list");
342 static MALLOC_DEFINE(M_PFSRCTREEPL, "pfsrctpl", "pf source tree pool list");
343 static MALLOC_DEFINE(M_PFSTATEKEYPL, "pfstatekeypl", "pf state key pool list");
344 static MALLOC_DEFINE(M_PFSTATEITEMPL, "pfstateitempl", "pf state item pool list");
346 static __inline int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
347 static __inline int pf_state_compare_key(struct pf_state_key *,
348 struct pf_state_key *);
349 static __inline int pf_state_compare_rkey(struct pf_state_key *,
350 struct pf_state_key *);
351 static __inline int pf_state_compare_id(struct pf_state *,
354 struct pf_src_tree tree_src_tracking[MAXCPU];
355 struct pf_state_tree_id tree_id[MAXCPU];
356 struct pf_state_queue state_list[MAXCPU];
358 RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
359 RB_GENERATE(pf_state_tree, pf_state_key, entry, pf_state_compare_key);
360 RB_GENERATE(pf_state_rtree, pf_state_key, entry, pf_state_compare_rkey);
361 RB_GENERATE(pf_state_tree_id, pf_state, entry_id, pf_state_compare_id);
364 pf_src_compare(struct pf_src_node *a, struct pf_src_node *b)
368 if (a->rule.ptr > b->rule.ptr)
370 if (a->rule.ptr < b->rule.ptr)
372 if ((diff = a->af - b->af) != 0)
377 if (a->addr.addr32[0] > b->addr.addr32[0])
379 if (a->addr.addr32[0] < b->addr.addr32[0])
385 if (a->addr.addr32[3] > b->addr.addr32[3])
387 if (a->addr.addr32[3] < b->addr.addr32[3])
389 if (a->addr.addr32[2] > b->addr.addr32[2])
391 if (a->addr.addr32[2] < b->addr.addr32[2])
393 if (a->addr.addr32[1] > b->addr.addr32[1])
395 if (a->addr.addr32[1] < b->addr.addr32[1])
397 if (a->addr.addr32[0] > b->addr.addr32[0])
399 if (a->addr.addr32[0] < b->addr.addr32[0])
408 pf_state_hash(struct pf_state_key *sk)
410 u_int32_t hv = (u_int32_t)(((intptr_t)sk >> 6) ^ ((intptr_t)sk >> 15));
411 if (hv == 0) /* disallow 0 */
418 pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af)
423 dst->addr32[0] = src->addr32[0];
427 dst->addr32[0] = src->addr32[0];
428 dst->addr32[1] = src->addr32[1];
429 dst->addr32[2] = src->addr32[2];
430 dst->addr32[3] = src->addr32[3];
437 pf_init_threshold(struct pf_threshold *threshold,
438 u_int32_t limit, u_int32_t seconds)
440 threshold->limit = limit * PF_THRESHOLD_MULT;
441 threshold->seconds = seconds;
442 threshold->count = 0;
443 threshold->last = time_second;
447 pf_add_threshold(struct pf_threshold *threshold)
449 u_int32_t t = time_second, diff = t - threshold->last;
451 if (diff >= threshold->seconds)
452 threshold->count = 0;
454 threshold->count -= threshold->count * diff /
456 threshold->count += PF_THRESHOLD_MULT;
461 pf_check_threshold(struct pf_threshold *threshold)
463 return (threshold->count > threshold->limit);
467 pf_src_connlimit(struct pf_state *state)
470 int cpu = mycpu->gd_cpuid;
472 state->src_node->conn++;
473 state->src.tcp_est = 1;
474 pf_add_threshold(&state->src_node->conn_rate);
476 if (state->rule.ptr->max_src_conn &&
477 state->rule.ptr->max_src_conn <
478 state->src_node->conn) {
479 pf_status.lcounters[LCNT_SRCCONN]++;
483 if (state->rule.ptr->max_src_conn_rate.limit &&
484 pf_check_threshold(&state->src_node->conn_rate)) {
485 pf_status.lcounters[LCNT_SRCCONNRATE]++;
492 if (state->rule.ptr->overload_tbl) {
494 u_int32_t killed = 0;
496 pf_status.lcounters[LCNT_OVERLOAD_TABLE]++;
497 if (pf_status.debug >= PF_DEBUG_MISC) {
498 kprintf("pf_src_connlimit: blocking address ");
499 pf_print_host(&state->src_node->addr, 0,
500 state->key[PF_SK_WIRE]->af);
503 bzero(&p, sizeof(p));
504 p.pfra_af = state->key[PF_SK_WIRE]->af;
505 switch (state->key[PF_SK_WIRE]->af) {
509 p.pfra_ip4addr = state->src_node->addr.v4;
515 p.pfra_ip6addr = state->src_node->addr.v6;
520 pfr_insert_kentry(state->rule.ptr->overload_tbl,
523 /* kill existing states if that's required. */
524 if (state->rule.ptr->flush) {
525 struct pf_state_key *sk;
528 pf_status.lcounters[LCNT_OVERLOAD_FLUSH]++;
529 RB_FOREACH(st, pf_state_tree_id, &tree_id[cpu]) {
530 sk = st->key[PF_SK_WIRE];
532 * Kill states from this source. (Only those
533 * from the same rule if PF_FLUSH_GLOBAL is not
534 * set). (Only on current cpu).
537 state->key[PF_SK_WIRE]->af &&
538 ((state->direction == PF_OUT &&
539 PF_AEQ(&state->src_node->addr,
540 &sk->addr[0], sk->af)) ||
541 (state->direction == PF_IN &&
542 PF_AEQ(&state->src_node->addr,
543 &sk->addr[1], sk->af))) &&
544 (state->rule.ptr->flush &
546 state->rule.ptr == st->rule.ptr)) {
547 st->timeout = PFTM_PURGE;
548 st->src.state = st->dst.state =
553 if (pf_status.debug >= PF_DEBUG_MISC)
554 kprintf(", %u states killed", killed);
556 if (pf_status.debug >= PF_DEBUG_MISC)
560 /* kill this state */
561 state->timeout = PFTM_PURGE;
562 state->src.state = state->dst.state = TCPS_CLOSED;
568 pf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule,
569 struct pf_addr *src, sa_family_t af)
571 struct pf_src_node k;
572 int cpu = mycpu->gd_cpuid;
576 PF_ACPY(&k.addr, src, af);
577 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
578 rule->rpool.opts & PF_POOL_STICKYADDR)
582 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
583 *sn = RB_FIND(pf_src_tree, &tree_src_tracking[cpu], &k);
586 if (!rule->max_src_nodes ||
587 rule->src_nodes < rule->max_src_nodes)
588 (*sn) = kmalloc(sizeof(struct pf_src_node),
589 M_PFSRCTREEPL, M_NOWAIT|M_ZERO);
591 pf_status.lcounters[LCNT_SRCNODES]++;
595 pf_init_threshold(&(*sn)->conn_rate,
596 rule->max_src_conn_rate.limit,
597 rule->max_src_conn_rate.seconds);
600 if (rule->rule_flag & PFRULE_RULESRCTRACK ||
601 rule->rpool.opts & PF_POOL_STICKYADDR)
602 (*sn)->rule.ptr = rule;
604 (*sn)->rule.ptr = NULL;
605 PF_ACPY(&(*sn)->addr, src, af);
606 if (RB_INSERT(pf_src_tree,
607 &tree_src_tracking[cpu], *sn) != NULL) {
608 if (pf_status.debug >= PF_DEBUG_MISC) {
609 kprintf("pf: src_tree insert failed: ");
610 pf_print_host(&(*sn)->addr, 0, af);
613 kfree(*sn, M_PFSRCTREEPL);
618 * Atomic op required to increment src_nodes in the rule
619 * because we hold a shared token here (decrements will use
620 * an exclusive token).
622 (*sn)->creation = time_second;
623 (*sn)->ruletype = rule->action;
624 if ((*sn)->rule.ptr != NULL)
625 atomic_add_int(&(*sn)->rule.ptr->src_nodes, 1);
626 pf_status.scounters[SCNT_SRC_NODE_INSERT]++;
627 atomic_add_int(&pf_status.src_nodes, 1);
629 if (rule->max_src_states &&
630 (*sn)->states >= rule->max_src_states) {
631 pf_status.lcounters[LCNT_SRCSTATES]++;
639 * state table (indexed by the pf_state_key structure), normal RBTREE
643 pf_state_compare_key(struct pf_state_key *a, struct pf_state_key *b)
647 if ((diff = a->proto - b->proto) != 0)
649 if ((diff = a->af - b->af) != 0)
654 if (a->addr[0].addr32[0] > b->addr[0].addr32[0])
656 if (a->addr[0].addr32[0] < b->addr[0].addr32[0])
658 if (a->addr[1].addr32[0] > b->addr[1].addr32[0])
660 if (a->addr[1].addr32[0] < b->addr[1].addr32[0])
666 if (a->addr[0].addr32[3] > b->addr[0].addr32[3])
668 if (a->addr[0].addr32[3] < b->addr[0].addr32[3])
670 if (a->addr[1].addr32[3] > b->addr[1].addr32[3])
672 if (a->addr[1].addr32[3] < b->addr[1].addr32[3])
674 if (a->addr[0].addr32[2] > b->addr[0].addr32[2])
676 if (a->addr[0].addr32[2] < b->addr[0].addr32[2])
678 if (a->addr[1].addr32[2] > b->addr[1].addr32[2])
680 if (a->addr[1].addr32[2] < b->addr[1].addr32[2])
682 if (a->addr[0].addr32[1] > b->addr[0].addr32[1])
684 if (a->addr[0].addr32[1] < b->addr[0].addr32[1])
686 if (a->addr[1].addr32[1] > b->addr[1].addr32[1])
688 if (a->addr[1].addr32[1] < b->addr[1].addr32[1])
690 if (a->addr[0].addr32[0] > b->addr[0].addr32[0])
692 if (a->addr[0].addr32[0] < b->addr[0].addr32[0])
694 if (a->addr[1].addr32[0] > b->addr[1].addr32[0])
696 if (a->addr[1].addr32[0] < b->addr[1].addr32[0])
702 if ((diff = a->port[0] - b->port[0]) != 0)
704 if ((diff = a->port[1] - b->port[1]) != 0)
711 * Used for RB_FIND only, compare in the reverse direction. The
712 * element to be reversed is always (a), since we obviously can't
713 * reverse the state tree depicted by (b).
716 pf_state_compare_rkey(struct pf_state_key *a, struct pf_state_key *b)
720 if ((diff = a->proto - b->proto) != 0)
722 if ((diff = a->af - b->af) != 0)
727 if (a->addr[1].addr32[0] > b->addr[0].addr32[0])
729 if (a->addr[1].addr32[0] < b->addr[0].addr32[0])
731 if (a->addr[0].addr32[0] > b->addr[1].addr32[0])
733 if (a->addr[0].addr32[0] < b->addr[1].addr32[0])
739 if (a->addr[1].addr32[3] > b->addr[0].addr32[3])
741 if (a->addr[1].addr32[3] < b->addr[0].addr32[3])
743 if (a->addr[0].addr32[3] > b->addr[1].addr32[3])
745 if (a->addr[0].addr32[3] < b->addr[1].addr32[3])
747 if (a->addr[1].addr32[2] > b->addr[0].addr32[2])
749 if (a->addr[1].addr32[2] < b->addr[0].addr32[2])
751 if (a->addr[0].addr32[2] > b->addr[1].addr32[2])
753 if (a->addr[0].addr32[2] < b->addr[1].addr32[2])
755 if (a->addr[1].addr32[1] > b->addr[0].addr32[1])
757 if (a->addr[1].addr32[1] < b->addr[0].addr32[1])
759 if (a->addr[0].addr32[1] > b->addr[1].addr32[1])
761 if (a->addr[0].addr32[1] < b->addr[1].addr32[1])
763 if (a->addr[1].addr32[0] > b->addr[0].addr32[0])
765 if (a->addr[1].addr32[0] < b->addr[0].addr32[0])
767 if (a->addr[0].addr32[0] > b->addr[1].addr32[0])
769 if (a->addr[0].addr32[0] < b->addr[1].addr32[0])
775 if ((diff = a->port[1] - b->port[0]) != 0)
777 if ((diff = a->port[0] - b->port[1]) != 0)
784 pf_state_compare_id(struct pf_state *a, struct pf_state *b)
790 if (a->creatorid > b->creatorid)
792 if (a->creatorid < b->creatorid)
799 pf_state_key_attach(struct pf_state_key *sk, struct pf_state *s, int idx)
801 struct pf_state_item *si;
802 struct pf_state_key *cur;
807 * PFSTATE_STACK_GLOBAL is set when the state might not hash to the
808 * current cpu. The keys are managed on the global statetbl tree
809 * for this case. Only translations (RDR, NAT) can cause this.
811 * When this flag is not set we must still check the global statetbl
812 * for a collision, and if we find one we set the HALF_DUPLEX flag
815 if (s->state_flags & PFSTATE_STACK_GLOBAL) {
817 lockmgr(&pf_global_statetbl_lock, LK_EXCLUSIVE);
819 cpu = mycpu->gd_cpuid;
820 lockmgr(&pf_global_statetbl_lock, LK_SHARED);
822 KKASSERT(s->key[idx] == NULL); /* XXX handle this? */
824 if (pf_status.debug >= PF_DEBUG_MISC) {
825 kprintf("state_key attach cpu %d (%08x:%d) %s (%08x:%d)\n",
827 ntohl(sk->addr[0].addr32[0]), ntohs(sk->port[0]),
828 (idx == PF_SK_WIRE ? "->" : "<-"),
829 ntohl(sk->addr[1].addr32[0]), ntohs(sk->port[1]));
833 * Check whether (e.g.) a PASS rule being put on a per-cpu tree
834 * collides with a translation rule on the global tree. This is
835 * NOT an error. We *WANT* to establish state for this case so the
836 * packet path is short-cutted and doesn't need to scan the ruleset
837 * on every packet. But the established state will only see one
838 * side of a two-way packet conversation. To prevent this from
839 * causing problems (e.g. generating a RST), we force PFSTATE_SLOPPY
840 * to be set on the established state.
842 * A collision against RDR state can only occur with a PASS IN in the
843 * opposite direction or a PASS OUT in the forwards direction. This
844 * is because RDRs are processed on the input side.
846 * A collision against NAT state can only occur with a PASS IN in the
847 * forwards direction or a PASS OUT in the opposite direction. This
848 * is because NATs are processed on the output side.
850 * In both situations we need to do a reverse addr/port test because
851 * the PASS IN or PASS OUT only establishes if it doesn't match the
852 * established RDR state in the forwards direction. The direction
853 * flag has to be ignored (it will be one way for a PASS IN and the
854 * other way for a PASS OUT).
856 * pf_global_statetbl_lock will be locked shared when testing and
857 * not entering into the global state table.
860 (cur = RB_FIND(pf_state_rtree,
861 (struct pf_state_rtree *)&pf_statetbl[MAXCPU],
863 TAILQ_FOREACH(si, &cur->states, entry) {
865 * NOTE: We must ignore direction mismatches.
867 if (si->s->kif == s->kif) {
868 s->state_flags |= PFSTATE_HALF_DUPLEX |
870 if (pf_status.debug >= PF_DEBUG_MISC) {
872 "pf: %s key attach collision "
874 (idx == PF_SK_WIRE) ?
877 pf_print_state_parts(s,
878 (idx == PF_SK_WIRE) ? sk : NULL,
879 (idx == PF_SK_STACK) ? sk : NULL);
888 * Enter into either the per-cpu or the global state table.
890 * pf_global_statetbl_lock will be locked exclusively when entering
891 * into the global state table.
893 if ((cur = RB_INSERT(pf_state_tree, &pf_statetbl[cpu], sk)) != NULL) {
894 /* key exists. check for same kif, if none, add to key */
895 TAILQ_FOREACH(si, &cur->states, entry) {
896 if (si->s->kif == s->kif &&
897 si->s->direction == s->direction) {
898 if (pf_status.debug >= PF_DEBUG_MISC) {
900 "pf: %s key attach failed on %s: ",
901 (idx == PF_SK_WIRE) ?
904 pf_print_state_parts(s,
905 (idx == PF_SK_WIRE) ? sk : NULL,
906 (idx == PF_SK_STACK) ? sk : NULL);
909 kfree(sk, M_PFSTATEKEYPL);
911 goto failed; /* collision! */
914 kfree(sk, M_PFSTATEKEYPL);
921 if ((si = kmalloc(sizeof(struct pf_state_item),
922 M_PFSTATEITEMPL, M_NOWAIT)) == NULL) {
923 pf_state_key_detach(s, idx);
925 goto failed; /* collision! */
929 /* list is sorted, if-bound states before floating */
930 if (s->kif == pfi_all)
931 TAILQ_INSERT_TAIL(&s->key[idx]->states, si, entry);
933 TAILQ_INSERT_HEAD(&s->key[idx]->states, si, entry);
937 lockmgr(&pf_global_statetbl_lock, LK_RELEASE);
942 * NOTE: Can only be called indirectly via the purge thread with pf_token
943 * exclusively locked.
946 pf_detach_state(struct pf_state *s)
948 if (s->key[PF_SK_WIRE] == s->key[PF_SK_STACK])
949 s->key[PF_SK_WIRE] = NULL;
951 if (s->key[PF_SK_STACK] != NULL)
952 pf_state_key_detach(s, PF_SK_STACK);
954 if (s->key[PF_SK_WIRE] != NULL)
955 pf_state_key_detach(s, PF_SK_WIRE);
959 * NOTE: Can only be called indirectly via the purge thread with pf_token
960 * exclusively locked.
963 pf_state_key_detach(struct pf_state *s, int idx)
965 struct pf_state_item *si;
969 * PFSTATE_STACK_GLOBAL is set for translations when the translated
970 * address/port is not localized to the same cpu that the untranslated
971 * address/port is on. The wire pf_state_key is managed on the global
972 * statetbl tree for this case.
974 if (s->state_flags & PFSTATE_STACK_GLOBAL) {
976 lockmgr(&pf_global_statetbl_lock, LK_EXCLUSIVE);
978 cpu = mycpu->gd_cpuid;
981 si = TAILQ_FIRST(&s->key[idx]->states);
982 while (si && si->s != s)
983 si = TAILQ_NEXT(si, entry);
986 TAILQ_REMOVE(&s->key[idx]->states, si, entry);
987 kfree(si, M_PFSTATEITEMPL);
990 if (TAILQ_EMPTY(&s->key[idx]->states)) {
991 RB_REMOVE(pf_state_tree, &pf_statetbl[cpu], s->key[idx]);
992 if (s->key[idx]->reverse)
993 s->key[idx]->reverse->reverse = NULL;
994 if (s->key[idx]->inp)
995 s->key[idx]->inp->inp_pf_sk = NULL;
996 kfree(s->key[idx], M_PFSTATEKEYPL);
1000 if (s->state_flags & PFSTATE_STACK_GLOBAL)
1001 lockmgr(&pf_global_statetbl_lock, LK_RELEASE);
1004 struct pf_state_key *
1005 pf_alloc_state_key(int pool_flags)
1007 struct pf_state_key *sk;
1009 sk = kmalloc(sizeof(struct pf_state_key), M_PFSTATEKEYPL, pool_flags);
1011 TAILQ_INIT(&sk->states);
1017 pf_state_key_setup(struct pf_pdesc *pd, struct pf_rule *nr,
1018 struct pf_state_key **skw, struct pf_state_key **sks,
1019 struct pf_state_key **skp, struct pf_state_key **nkp,
1020 struct pf_addr *saddr, struct pf_addr *daddr,
1021 u_int16_t sport, u_int16_t dport)
1023 KKASSERT((*skp == NULL && *nkp == NULL));
1025 if ((*skp = pf_alloc_state_key(M_NOWAIT | M_ZERO)) == NULL)
1028 PF_ACPY(&(*skp)->addr[pd->sidx], saddr, pd->af);
1029 PF_ACPY(&(*skp)->addr[pd->didx], daddr, pd->af);
1030 (*skp)->port[pd->sidx] = sport;
1031 (*skp)->port[pd->didx] = dport;
1032 (*skp)->proto = pd->proto;
1033 (*skp)->af = pd->af;
1036 if ((*nkp = pf_alloc_state_key(M_NOWAIT | M_ZERO)) == NULL)
1037 return (ENOMEM); /* caller must handle cleanup */
1039 /* XXX maybe just bcopy and TAILQ_INIT(&(*nkp)->states) */
1040 PF_ACPY(&(*nkp)->addr[0], &(*skp)->addr[0], pd->af);
1041 PF_ACPY(&(*nkp)->addr[1], &(*skp)->addr[1], pd->af);
1042 (*nkp)->port[0] = (*skp)->port[0];
1043 (*nkp)->port[1] = (*skp)->port[1];
1044 (*nkp)->proto = pd->proto;
1045 (*nkp)->af = pd->af;
1050 if (pd->dir == PF_IN) {
1061 * Insert pf_state with one or two state keys (allowing a reverse path lookup
1062 * which is used by NAT). In the NAT case skw is the initiator (?) and
1063 * sks is the target.
1066 pf_state_insert(struct pfi_kif *kif, struct pf_state_key *skw,
1067 struct pf_state_key *sks, struct pf_state *s)
1069 int cpu = mycpu->gd_cpuid;
1075 if (pf_state_key_attach(skw, s, PF_SK_WIRE))
1077 s->key[PF_SK_STACK] = s->key[PF_SK_WIRE];
1083 if (pf_state_key_attach(skw, s, PF_SK_WIRE)) {
1084 kfree(sks, M_PFSTATEKEYPL);
1087 if (pf_state_key_attach(sks, s, PF_SK_STACK)) {
1088 pf_state_key_detach(s, PF_SK_WIRE);
1093 if (s->id == 0 && s->creatorid == 0) {
1096 #if __SIZEOF_LONG__ == 8
1097 sid = atomic_fetchadd_long(&pf_status.stateid, 1);
1099 spin_lock(&pf_spin);
1100 sid = pf_status.stateid++;
1101 spin_unlock(&pf_spin);
1103 s->id = htobe64(sid);
1104 s->creatorid = pf_status.hostid;
1108 * Calculate hash code for altq
1110 s->hash = crc32(s->key[PF_SK_WIRE], PF_STATE_KEY_HASH_LENGTH);
1112 if (RB_INSERT(pf_state_tree_id, &tree_id[cpu], s) != NULL) {
1113 if (pf_status.debug >= PF_DEBUG_MISC) {
1114 kprintf("pf: state insert failed: "
1115 "id: %016jx creatorid: %08x",
1116 (uintmax_t)be64toh(s->id), ntohl(s->creatorid));
1117 if (s->sync_flags & PFSTATE_FROMSYNC)
1118 kprintf(" (from sync)");
1124 TAILQ_INSERT_TAIL(&state_list[cpu], s, entry_list);
1125 pf_status.fcounters[FCNT_STATE_INSERT]++;
1126 atomic_add_int(&pf_status.states, 1);
1127 pfi_kif_ref(kif, PFI_KIF_REF_STATE);
1128 pfsync_insert_state(s);
1133 pf_find_state_byid(struct pf_state_cmp *key)
1135 int cpu = mycpu->gd_cpuid;
1137 pf_status.fcounters[FCNT_STATE_SEARCH]++;
1139 return (RB_FIND(pf_state_tree_id, &tree_id[cpu],
1140 (struct pf_state *)key));
1144 * WARNING! May return a state structure that was localized to another cpu,
1145 * destruction is typically protected by the callers pf_token.
1146 * The element can only be destroyed
1149 pf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int dir,
1152 struct pf_state_key *skey = (void *)key;
1153 struct pf_state_key *sk;
1154 struct pf_state_item *si;
1156 int cpu = mycpu->gd_cpuid;
1159 pf_status.fcounters[FCNT_STATE_SEARCH]++;
1161 if (dir == PF_OUT && m->m_pkthdr.pf.statekey &&
1162 ((struct pf_state_key *)m->m_pkthdr.pf.statekey)->reverse) {
1163 sk = ((struct pf_state_key *)m->m_pkthdr.pf.statekey)->reverse;
1165 sk = RB_FIND(pf_state_tree, &pf_statetbl[cpu], skey);
1167 lockmgr(&pf_global_statetbl_lock, LK_SHARED);
1168 sk = RB_FIND(pf_state_tree, &pf_statetbl[MAXCPU], skey);
1170 lockmgr(&pf_global_statetbl_lock, LK_RELEASE);
1175 if (dir == PF_OUT && m->m_pkthdr.pf.statekey) {
1176 ((struct pf_state_key *)
1177 m->m_pkthdr.pf.statekey)->reverse = sk;
1178 sk->reverse = m->m_pkthdr.pf.statekey;
1182 m->m_pkthdr.pf.statekey = NULL;
1184 /* list is sorted, if-bound states before floating ones */
1185 TAILQ_FOREACH(si, &sk->states, entry) {
1186 if ((si->s->kif == pfi_all || si->s->kif == kif) &&
1187 sk == (dir == PF_IN ? si->s->key[PF_SK_WIRE] :
1188 si->s->key[PF_SK_STACK])) {
1194 * Extract state before potentially releasing the global statetbl
1195 * lock. Ignore the state if the create is still in-progress as
1196 * it can be deleted out from under us by the owning localized cpu.
1197 * However, if CREATEINPROG is not set, state can only be deleted
1198 * by the purge thread which we are protected from via our shared
1203 if (s && (s->state_flags & PFSTATE_CREATEINPROG))
1209 lockmgr(&pf_global_statetbl_lock, LK_RELEASE);
1214 * WARNING! May return a state structure that was localized to another cpu,
1215 * destruction is typically protected by the callers pf_token.
1218 pf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more)
1220 struct pf_state_key *skey = (void *)key;
1221 struct pf_state_key *sk;
1222 struct pf_state_item *si, *ret = NULL;
1224 int cpu = mycpu->gd_cpuid;
1227 pf_status.fcounters[FCNT_STATE_SEARCH]++;
1229 sk = RB_FIND(pf_state_tree, &pf_statetbl[cpu], skey);
1231 lockmgr(&pf_global_statetbl_lock, LK_SHARED);
1232 sk = RB_FIND(pf_state_tree, &pf_statetbl[MAXCPU], skey);
1236 TAILQ_FOREACH(si, &sk->states, entry)
1237 if (dir == PF_INOUT ||
1238 (sk == (dir == PF_IN ? si->s->key[PF_SK_WIRE] :
1239 si->s->key[PF_SK_STACK]))) {
1252 * Extract state before potentially releasing the global statetbl
1253 * lock. Ignore the state if the create is still in-progress as
1254 * it can be deleted out from under us by the owning localized cpu.
1255 * However, if CREATEINPROG is not set, state can only be deleted
1256 * by the purge thread which we are protected from via our shared
1261 if (s && (s->state_flags & PFSTATE_CREATEINPROG))
1267 lockmgr(&pf_global_statetbl_lock, LK_RELEASE);
1271 /* END state table stuff */
1274 pf_purge_thread(void *v)
1276 globaldata_t save_gd = mycpu;
1283 tsleep(pf_purge_thread, PWAIT, "pftm", 1 * hz);
1285 endingit = pf_end_threads;
1287 for (nn = 0; nn < ncpus; ++nn) {
1288 lwkt_setcpu_self(globaldata_find(nn));
1290 lwkt_gettoken(&pf_token);
1291 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
1295 * process a fraction of the state table every second
1297 if(!pf_purge_expired_states(
1298 1 + (pf_status.states /
1299 pf_default_rule.timeout[
1300 PFTM_INTERVAL]), 0)) {
1301 pf_purge_expired_states(
1302 1 + (pf_status.states /
1303 pf_default_rule.timeout[
1304 PFTM_INTERVAL]), 1);
1308 * purge other expired types every PFTM_INTERVAL
1312 pf_default_rule.timeout[PFTM_INTERVAL]) {
1313 pf_purge_expired_fragments();
1314 if (!pf_purge_expired_src_nodes(locked)) {
1315 pf_purge_expired_src_nodes(1);
1321 * If terminating the thread, clean everything out
1325 pf_purge_expired_states(pf_status.states, 0);
1326 pf_purge_expired_fragments();
1327 pf_purge_expired_src_nodes(1);
1331 lockmgr(&pf_consistency_lock, LK_RELEASE);
1332 lwkt_reltoken(&pf_token);
1334 lwkt_setcpu_self(save_gd);
1340 * Thread termination
1343 wakeup(pf_purge_thread);
1348 pf_state_expires(const struct pf_state *state)
1355 /* handle all PFTM_* > PFTM_MAX here */
1356 if (state->timeout == PFTM_PURGE)
1357 return (time_second);
1358 if (state->timeout == PFTM_UNTIL_PACKET)
1360 KKASSERT(state->timeout != PFTM_UNLINKED);
1361 KKASSERT(state->timeout < PFTM_MAX);
1362 timeout = state->rule.ptr->timeout[state->timeout];
1364 timeout = pf_default_rule.timeout[state->timeout];
1365 start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START];
1367 end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END];
1368 states = state->rule.ptr->states_cur;
1370 start = pf_default_rule.timeout[PFTM_ADAPTIVE_START];
1371 end = pf_default_rule.timeout[PFTM_ADAPTIVE_END];
1372 states = pf_status.states;
1374 if (end && states > start && start < end) {
1376 return (state->expire + timeout * (end - states) /
1379 return (time_second);
1381 return (state->expire + timeout);
1385 * (called with exclusive pf_token)
1388 pf_purge_expired_src_nodes(int waslocked)
1390 struct pf_src_node *cur, *next;
1391 int locked = waslocked;
1392 int cpu = mycpu->gd_cpuid;
1394 for (cur = RB_MIN(pf_src_tree, &tree_src_tracking[cpu]);
1397 next = RB_NEXT(pf_src_tree, &tree_src_tracking[cpu], cur);
1399 if (cur->states <= 0 && cur->expire <= time_second) {
1401 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
1402 next = RB_NEXT(pf_src_tree,
1403 &tree_src_tracking[cpu], cur);
1406 if (cur->rule.ptr != NULL) {
1408 * decrements in rule should be ok, token is
1409 * held exclusively in this code path.
1411 cur->rule.ptr->src_nodes--;
1412 if (cur->rule.ptr->states_cur <= 0 &&
1413 cur->rule.ptr->max_src_nodes <= 0)
1414 pf_rm_rule(NULL, cur->rule.ptr);
1416 RB_REMOVE(pf_src_tree, &tree_src_tracking[cpu], cur);
1417 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
1418 atomic_add_int(&pf_status.src_nodes, -1);
1419 kfree(cur, M_PFSRCTREEPL);
1422 if (locked && !waslocked)
1423 lockmgr(&pf_consistency_lock, LK_RELEASE);
1428 pf_src_tree_remove_state(struct pf_state *s)
1432 if (s->src_node != NULL) {
1434 --s->src_node->conn;
1435 if (--s->src_node->states <= 0) {
1436 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1439 pf_default_rule.timeout[PFTM_SRC_NODE];
1441 s->src_node->expire = time_second + timeout;
1444 if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) {
1445 if (--s->nat_src_node->states <= 0) {
1446 timeout = s->rule.ptr->timeout[PFTM_SRC_NODE];
1449 pf_default_rule.timeout[PFTM_SRC_NODE];
1450 s->nat_src_node->expire = time_second + timeout;
1453 s->src_node = s->nat_src_node = NULL;
1456 /* callers should be at crit_enter() */
1458 pf_unlink_state(struct pf_state *cur)
1460 int cpu = mycpu->gd_cpuid;
1462 if (cur->src.state == PF_TCPS_PROXY_DST) {
1463 /* XXX wire key the right one? */
1464 pf_send_tcp(cur->rule.ptr, cur->key[PF_SK_WIRE]->af,
1465 &cur->key[PF_SK_WIRE]->addr[1],
1466 &cur->key[PF_SK_WIRE]->addr[0],
1467 cur->key[PF_SK_WIRE]->port[1],
1468 cur->key[PF_SK_WIRE]->port[0],
1469 cur->src.seqhi, cur->src.seqlo + 1,
1470 TH_RST|TH_ACK, 0, 0, 0, 1, cur->tag, NULL, NULL);
1472 RB_REMOVE(pf_state_tree_id, &tree_id[cpu], cur);
1473 if (cur->creatorid == pf_status.hostid)
1474 pfsync_delete_state(cur);
1475 cur->timeout = PFTM_UNLINKED;
1476 pf_src_tree_remove_state(cur);
1477 pf_detach_state(cur);
1480 static struct pf_state *purge_cur[MAXCPU];
1483 * callers should be at crit_enter() and hold pf_consistency_lock exclusively.
1484 * pf_token must also be held exclusively.
1487 pf_free_state(struct pf_state *cur)
1489 int cpu = mycpu->gd_cpuid;
1491 KKASSERT(cur->cpuid == cpu);
1493 if (pfsyncif != NULL &&
1494 (pfsyncif->sc_bulk_send_next == cur ||
1495 pfsyncif->sc_bulk_terminator == cur))
1497 KKASSERT(cur->timeout == PFTM_UNLINKED);
1499 * decrements in rule should be ok, token is
1500 * held exclusively in this code path.
1502 if (--cur->rule.ptr->states_cur <= 0 &&
1503 cur->rule.ptr->src_nodes <= 0)
1504 pf_rm_rule(NULL, cur->rule.ptr);
1505 if (cur->nat_rule.ptr != NULL) {
1506 if (--cur->nat_rule.ptr->states_cur <= 0 &&
1507 cur->nat_rule.ptr->src_nodes <= 0) {
1508 pf_rm_rule(NULL, cur->nat_rule.ptr);
1511 if (cur->anchor.ptr != NULL) {
1512 if (--cur->anchor.ptr->states_cur <= 0)
1513 pf_rm_rule(NULL, cur->anchor.ptr);
1515 pf_normalize_tcp_cleanup(cur);
1516 pfi_kif_unref(cur->kif, PFI_KIF_REF_STATE);
1519 * We may be freeing pf_purge_expired_states()'s saved scan entry,
1520 * adjust it if necessary.
1522 if (purge_cur[cpu] == cur) {
1523 kprintf("PURGE CONFLICT\n");
1524 purge_cur[cpu] = TAILQ_NEXT(purge_cur[cpu], entry_list);
1526 TAILQ_REMOVE(&state_list[cpu], cur, entry_list);
1528 pf_tag_unref(cur->tag);
1529 kfree(cur, M_PFSTATEPL);
1530 pf_status.fcounters[FCNT_STATE_REMOVALS]++;
1531 atomic_add_int(&pf_status.states, -1);
1535 pf_purge_expired_states(u_int32_t maxcheck, int waslocked)
1537 struct pf_state *cur;
1538 int locked = waslocked;
1539 int cpu = mycpu->gd_cpuid;
1541 while (maxcheck--) {
1543 * Wrap to start of list when we hit the end
1545 cur = purge_cur[cpu];
1547 cur = TAILQ_FIRST(&state_list[cpu]);
1549 break; /* list empty */
1553 * Setup next (purge_cur) while we process this one. If
1554 * we block and something else deletes purge_cur,
1555 * pf_free_state() will adjust it further ahead.
1557 purge_cur[cpu] = TAILQ_NEXT(cur, entry_list);
1559 if (cur->timeout == PFTM_UNLINKED) {
1560 /* free unlinked state */
1562 lockmgr(&pf_consistency_lock, LK_EXCLUSIVE);
1566 } else if (pf_state_expires(cur) <= time_second) {
1567 /* unlink and free expired state */
1568 pf_unlink_state(cur);
1570 if (!lockmgr(&pf_consistency_lock, LK_EXCLUSIVE))
1579 lockmgr(&pf_consistency_lock, LK_RELEASE);
1584 pf_tbladdr_setup(struct pf_ruleset *rs, struct pf_addr_wrap *aw)
1586 if (aw->type != PF_ADDR_TABLE)
1588 if ((aw->p.tbl = pfr_attach_table(rs, aw->v.tblname)) == NULL)
1594 pf_tbladdr_remove(struct pf_addr_wrap *aw)
1596 if (aw->type != PF_ADDR_TABLE || aw->p.tbl == NULL)
1598 pfr_detach_table(aw->p.tbl);
1603 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
1605 struct pfr_ktable *kt = aw->p.tbl;
1607 if (aw->type != PF_ADDR_TABLE || kt == NULL)
1609 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
1610 kt = kt->pfrkt_root;
1612 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
1617 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af)
1622 u_int32_t a = ntohl(addr->addr32[0]);
1623 kprintf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255,
1635 u_int8_t i, curstart = 255, curend = 0,
1636 maxstart = 0, maxend = 0;
1637 for (i = 0; i < 8; i++) {
1638 if (!addr->addr16[i]) {
1639 if (curstart == 255)
1645 if ((curend - curstart) >
1646 (maxend - maxstart)) {
1647 maxstart = curstart;
1654 for (i = 0; i < 8; i++) {
1655 if (i >= maxstart && i <= maxend) {
1664 b = ntohs(addr->addr16[i]);
1681 pf_print_state(struct pf_state *s)
1683 pf_print_state_parts(s, NULL, NULL);
1687 pf_print_state_parts(struct pf_state *s,
1688 struct pf_state_key *skwp, struct pf_state_key *sksp)
1690 struct pf_state_key *skw, *sks;
1691 u_int8_t proto, dir;
1693 /* Do our best to fill these, but they're skipped if NULL */
1694 skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL);
1695 sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL);
1696 proto = skw ? skw->proto : (sks ? sks->proto : 0);
1697 dir = s ? s->direction : 0;
1709 case IPPROTO_ICMPV6:
1713 kprintf("%u ", skw->proto);
1726 pf_print_host(&skw->addr[0], skw->port[0], skw->af);
1728 pf_print_host(&skw->addr[1], skw->port[1], skw->af);
1731 kprintf(" stack: ");
1733 pf_print_host(&sks->addr[0], sks->port[0], sks->af);
1735 pf_print_host(&sks->addr[1], sks->port[1], sks->af);
1740 if (proto == IPPROTO_TCP) {
1741 kprintf(" [lo=%u high=%u win=%u modulator=%u",
1742 s->src.seqlo, s->src.seqhi,
1743 s->src.max_win, s->src.seqdiff);
1744 if (s->src.wscale && s->dst.wscale)
1745 kprintf(" wscale=%u",
1746 s->src.wscale & PF_WSCALE_MASK);
1748 kprintf(" [lo=%u high=%u win=%u modulator=%u",
1749 s->dst.seqlo, s->dst.seqhi,
1750 s->dst.max_win, s->dst.seqdiff);
1751 if (s->src.wscale && s->dst.wscale)
1752 kprintf(" wscale=%u",
1753 s->dst.wscale & PF_WSCALE_MASK);
1756 kprintf(" %u:%u", s->src.state, s->dst.state);
1761 pf_print_flags(u_int8_t f)
1783 #define PF_SET_SKIP_STEPS(i) \
1785 while (head[i] != cur) { \
1786 head[i]->skip[i].ptr = cur; \
1787 head[i] = TAILQ_NEXT(head[i], entries); \
1792 pf_calc_skip_steps(struct pf_rulequeue *rules)
1794 struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT];
1797 cur = TAILQ_FIRST(rules);
1799 for (i = 0; i < PF_SKIP_COUNT; ++i)
1801 while (cur != NULL) {
1803 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot)
1804 PF_SET_SKIP_STEPS(PF_SKIP_IFP);
1805 if (cur->direction != prev->direction)
1806 PF_SET_SKIP_STEPS(PF_SKIP_DIR);
1807 if (cur->af != prev->af)
1808 PF_SET_SKIP_STEPS(PF_SKIP_AF);
1809 if (cur->proto != prev->proto)
1810 PF_SET_SKIP_STEPS(PF_SKIP_PROTO);
1811 if (cur->src.neg != prev->src.neg ||
1812 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr))
1813 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR);
1814 if (cur->src.port[0] != prev->src.port[0] ||
1815 cur->src.port[1] != prev->src.port[1] ||
1816 cur->src.port_op != prev->src.port_op)
1817 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT);
1818 if (cur->dst.neg != prev->dst.neg ||
1819 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr))
1820 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR);
1821 if (cur->dst.port[0] != prev->dst.port[0] ||
1822 cur->dst.port[1] != prev->dst.port[1] ||
1823 cur->dst.port_op != prev->dst.port_op)
1824 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT);
1827 cur = TAILQ_NEXT(cur, entries);
1829 for (i = 0; i < PF_SKIP_COUNT; ++i)
1830 PF_SET_SKIP_STEPS(i);
1834 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2)
1836 if (aw1->type != aw2->type)
1838 switch (aw1->type) {
1839 case PF_ADDR_ADDRMASK:
1841 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0))
1843 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0))
1846 case PF_ADDR_DYNIFTL:
1847 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt);
1848 case PF_ADDR_NOROUTE:
1849 case PF_ADDR_URPFFAILED:
1852 return (aw1->p.tbl != aw2->p.tbl);
1853 case PF_ADDR_RTLABEL:
1854 return (aw1->v.rtlabel != aw2->v.rtlabel);
1856 kprintf("invalid address type: %d\n", aw1->type);
1862 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp)
1868 l = cksum + old - new;
1869 l = (l >> 16) + (l & 65535);
1877 pf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc,
1878 struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af)
1883 PF_ACPY(&ao, a, af);
1891 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1892 ao.addr16[0], an->addr16[0], 0),
1893 ao.addr16[1], an->addr16[1], 0);
1895 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1896 ao.addr16[0], an->addr16[0], u),
1897 ao.addr16[1], an->addr16[1], u),
1903 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1904 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1905 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc,
1906 ao.addr16[0], an->addr16[0], u),
1907 ao.addr16[1], an->addr16[1], u),
1908 ao.addr16[2], an->addr16[2], u),
1909 ao.addr16[3], an->addr16[3], u),
1910 ao.addr16[4], an->addr16[4], u),
1911 ao.addr16[5], an->addr16[5], u),
1912 ao.addr16[6], an->addr16[6], u),
1913 ao.addr16[7], an->addr16[7], u),
1921 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1923 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u)
1927 memcpy(&ao, a, sizeof(ao));
1928 memcpy(a, &an, sizeof(u_int32_t));
1929 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u),
1930 ao % 65536, an % 65536, u);
1935 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u)
1939 PF_ACPY(&ao, a, AF_INET6);
1940 PF_ACPY(a, an, AF_INET6);
1942 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1943 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1944 pf_cksum_fixup(pf_cksum_fixup(*c,
1945 ao.addr16[0], an->addr16[0], u),
1946 ao.addr16[1], an->addr16[1], u),
1947 ao.addr16[2], an->addr16[2], u),
1948 ao.addr16[3], an->addr16[3], u),
1949 ao.addr16[4], an->addr16[4], u),
1950 ao.addr16[5], an->addr16[5], u),
1951 ao.addr16[6], an->addr16[6], u),
1952 ao.addr16[7], an->addr16[7], u);
1957 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa,
1958 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c,
1959 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af)
1961 struct pf_addr oia, ooa;
1963 PF_ACPY(&oia, ia, af);
1965 PF_ACPY(&ooa, oa, af);
1967 /* Change inner protocol port, fix inner protocol checksum. */
1969 u_int16_t oip = *ip;
1976 *pc = pf_cksum_fixup(*pc, oip, *ip, u);
1977 *ic = pf_cksum_fixup(*ic, oip, *ip, 0);
1979 *ic = pf_cksum_fixup(*ic, opc, *pc, 0);
1981 /* Change inner ip address, fix inner ip and icmp checksums. */
1982 PF_ACPY(ia, na, af);
1986 u_int32_t oh2c = *h2c;
1988 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c,
1989 oia.addr16[0], ia->addr16[0], 0),
1990 oia.addr16[1], ia->addr16[1], 0);
1991 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic,
1992 oia.addr16[0], ia->addr16[0], 0),
1993 oia.addr16[1], ia->addr16[1], 0);
1994 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0);
2000 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2001 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2002 pf_cksum_fixup(pf_cksum_fixup(*ic,
2003 oia.addr16[0], ia->addr16[0], u),
2004 oia.addr16[1], ia->addr16[1], u),
2005 oia.addr16[2], ia->addr16[2], u),
2006 oia.addr16[3], ia->addr16[3], u),
2007 oia.addr16[4], ia->addr16[4], u),
2008 oia.addr16[5], ia->addr16[5], u),
2009 oia.addr16[6], ia->addr16[6], u),
2010 oia.addr16[7], ia->addr16[7], u);
2014 /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */
2016 PF_ACPY(oa, na, af);
2020 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc,
2021 ooa.addr16[0], oa->addr16[0], 0),
2022 ooa.addr16[1], oa->addr16[1], 0);
2027 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2028 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2029 pf_cksum_fixup(pf_cksum_fixup(*ic,
2030 ooa.addr16[0], oa->addr16[0], u),
2031 ooa.addr16[1], oa->addr16[1], u),
2032 ooa.addr16[2], oa->addr16[2], u),
2033 ooa.addr16[3], oa->addr16[3], u),
2034 ooa.addr16[4], oa->addr16[4], u),
2035 ooa.addr16[5], oa->addr16[5], u),
2036 ooa.addr16[6], oa->addr16[6], u),
2037 ooa.addr16[7], oa->addr16[7], u);
2046 * Need to modulate the sequence numbers in the TCP SACK option
2047 * (credits to Krzysztof Pfaff for report and patch)
2050 pf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd,
2051 struct tcphdr *th, struct pf_state_peer *dst)
2053 int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen;
2054 u_int8_t opts[TCP_MAXOLEN], *opt = opts;
2055 int copyback = 0, i, olen;
2056 struct raw_sackblock sack;
2058 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2059 if (hlen < TCPOLEN_SACKLEN ||
2060 !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af))
2063 while (hlen >= TCPOLEN_SACKLEN) {
2066 case TCPOPT_EOL: /* FALLTHROUGH */
2074 if (olen >= TCPOLEN_SACKLEN) {
2075 for (i = 2; i + TCPOLEN_SACK <= olen;
2076 i += TCPOLEN_SACK) {
2077 memcpy(&sack, &opt[i], sizeof(sack));
2078 pf_change_a(&sack.rblk_start, &th->th_sum,
2079 htonl(ntohl(sack.rblk_start) -
2081 pf_change_a(&sack.rblk_end, &th->th_sum,
2082 htonl(ntohl(sack.rblk_end) -
2084 memcpy(&opt[i], &sack, sizeof(sack));
2098 m_copyback(m, off + sizeof(*th), thoptlen, opts);
2103 pf_send_tcp(const struct pf_rule *r, sa_family_t af,
2104 const struct pf_addr *saddr, const struct pf_addr *daddr,
2105 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack,
2106 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag,
2107 u_int16_t rtag, struct ether_header *eh, struct ifnet *ifp)
2112 struct ip *h = NULL;
2115 struct ip6_hdr *h6 = NULL;
2117 struct tcphdr *th = NULL;
2120 ASSERT_LWKT_TOKEN_HELD(&pf_token);
2122 /* maximum segment size tcp option */
2123 tlen = sizeof(struct tcphdr);
2130 len = sizeof(struct ip) + tlen;
2135 len = sizeof(struct ip6_hdr) + tlen;
2141 * Create outgoing mbuf.
2143 * DragonFly doesn't zero the auxillary pkghdr fields, only fw_flags,
2144 * so make sure pf.flags is clear.
2146 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
2151 m->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
2152 m->m_pkthdr.pf.flags = 0;
2153 m->m_pkthdr.pf.tag = rtag;
2154 /* XXX Recheck when upgrading to > 4.4 */
2155 m->m_pkthdr.pf.statekey = NULL;
2156 if (r != NULL && r->rtableid >= 0)
2157 m->m_pkthdr.pf.rtableid = r->rtableid;
2160 if (r != NULL && r->qid) {
2161 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
2162 m->m_pkthdr.pf.qid = r->qid;
2163 m->m_pkthdr.pf.ecn_af = af;
2164 m->m_pkthdr.pf.hdr = mtod(m, struct ip *);
2167 m->m_data += max_linkhdr;
2168 m->m_pkthdr.len = m->m_len = len;
2169 m->m_pkthdr.rcvif = NULL;
2170 bzero(m->m_data, len);
2174 h = mtod(m, struct ip *);
2176 /* IP header fields included in the TCP checksum */
2177 h->ip_p = IPPROTO_TCP;
2179 h->ip_src.s_addr = saddr->v4.s_addr;
2180 h->ip_dst.s_addr = daddr->v4.s_addr;
2182 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip));
2187 h6 = mtod(m, struct ip6_hdr *);
2189 /* IP header fields included in the TCP checksum */
2190 h6->ip6_nxt = IPPROTO_TCP;
2191 h6->ip6_plen = htons(tlen);
2192 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr));
2193 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr));
2195 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr));
2201 th->th_sport = sport;
2202 th->th_dport = dport;
2203 th->th_seq = htonl(seq);
2204 th->th_ack = htonl(ack);
2205 th->th_off = tlen >> 2;
2206 th->th_flags = flags;
2207 th->th_win = htons(win);
2210 opt = (char *)(th + 1);
2211 opt[0] = TCPOPT_MAXSEG;
2214 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2);
2221 th->th_sum = in_cksum(m, len);
2223 /* Finish the IP header */
2225 h->ip_hl = sizeof(*h) >> 2;
2226 h->ip_tos = IPTOS_LOWDELAY;
2228 h->ip_off = path_mtu_discovery ? IP_DF : 0;
2229 h->ip_ttl = ttl ? ttl : ip_defttl;
2232 lwkt_reltoken(&pf_token);
2233 ip_output(m, NULL, NULL, 0, NULL, NULL);
2234 lwkt_gettoken(&pf_token);
2238 struct ether_header *e = (void *)ro.ro_dst.sa_data;
2246 ro.ro_dst.sa_len = sizeof(ro.ro_dst);
2247 ro.ro_dst.sa_family = pseudo_AF_HDRCMPLT;
2248 bcopy(eh->ether_dhost, e->ether_shost, ETHER_ADDR_LEN);
2249 bcopy(eh->ether_shost, e->ether_dhost, ETHER_ADDR_LEN);
2250 e->ether_type = eh->ether_type;
2251 /* XXX_IMPORT: later */
2252 lwkt_reltoken(&pf_token);
2253 ip_output(m, NULL, &ro, 0, NULL, NULL);
2254 lwkt_gettoken(&pf_token);
2261 th->th_sum = in6_cksum(m, IPPROTO_TCP,
2262 sizeof(struct ip6_hdr), tlen);
2264 h6->ip6_vfc |= IPV6_VERSION;
2265 h6->ip6_hlim = IPV6_DEFHLIM;
2267 lwkt_reltoken(&pf_token);
2268 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
2269 lwkt_gettoken(&pf_token);
2276 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af,
2282 * DragonFly doesn't zero the auxillary pkghdr fields, only fw_flags,
2283 * so make sure pf.flags is clear.
2285 if ((m0 = m_copy(m, 0, M_COPYALL)) == NULL)
2288 m0->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
2289 m0->m_pkthdr.pf.flags = 0;
2290 /* XXX Re-Check when Upgrading to > 4.4 */
2291 m0->m_pkthdr.pf.statekey = NULL;
2293 if (r->rtableid >= 0)
2294 m0->m_pkthdr.pf.rtableid = r->rtableid;
2298 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
2299 m0->m_pkthdr.pf.qid = r->qid;
2300 m0->m_pkthdr.pf.ecn_af = af;
2301 m0->m_pkthdr.pf.hdr = mtod(m0, struct ip *);
2308 icmp_error(m0, type, code, 0, 0);
2313 icmp6_error(m0, type, code, 0);
2320 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2321 * If n is 0, they match if they are equal. If n is != 0, they match if they
2325 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m,
2326 struct pf_addr *b, sa_family_t af)
2333 if ((a->addr32[0] & m->addr32[0]) ==
2334 (b->addr32[0] & m->addr32[0]))
2340 if (((a->addr32[0] & m->addr32[0]) ==
2341 (b->addr32[0] & m->addr32[0])) &&
2342 ((a->addr32[1] & m->addr32[1]) ==
2343 (b->addr32[1] & m->addr32[1])) &&
2344 ((a->addr32[2] & m->addr32[2]) ==
2345 (b->addr32[2] & m->addr32[2])) &&
2346 ((a->addr32[3] & m->addr32[3]) ==
2347 (b->addr32[3] & m->addr32[3])))
2366 * Return 1 if b <= a <= e, otherwise return 0.
2369 pf_match_addr_range(struct pf_addr *b, struct pf_addr *e,
2370 struct pf_addr *a, sa_family_t af)
2375 if ((a->addr32[0] < b->addr32[0]) ||
2376 (a->addr32[0] > e->addr32[0]))
2385 for (i = 0; i < 4; ++i)
2386 if (a->addr32[i] > b->addr32[i])
2388 else if (a->addr32[i] < b->addr32[i])
2391 for (i = 0; i < 4; ++i)
2392 if (a->addr32[i] < e->addr32[i])
2394 else if (a->addr32[i] > e->addr32[i])
2404 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p)
2408 return ((p > a1) && (p < a2));
2410 return ((p < a1) || (p > a2));
2412 return ((p >= a1) && (p <= a2));
2426 return (0); /* never reached */
2430 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p)
2435 return (pf_match(op, a1, a2, p));
2439 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u)
2441 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2443 return (pf_match(op, a1, a2, u));
2447 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g)
2449 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE)
2451 return (pf_match(op, a1, a2, g));
2455 pf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag)
2458 *tag = m->m_pkthdr.pf.tag;
2460 return ((!r->match_tag_not && r->match_tag == *tag) ||
2461 (r->match_tag_not && r->match_tag != *tag));
2465 pf_tag_packet(struct mbuf *m, int tag, int rtableid)
2467 if (tag <= 0 && rtableid < 0)
2471 m->m_pkthdr.pf.tag = tag;
2473 m->m_pkthdr.pf.rtableid = rtableid;
2479 pf_step_into_anchor(int *depth, struct pf_ruleset **rs, int n,
2480 struct pf_rule **r, struct pf_rule **a, int *match)
2482 struct pf_anchor_stackframe *f;
2484 (*r)->anchor->match = 0;
2487 if (*depth >= NELEM(pf_anchor_stack)) {
2488 kprintf("pf_step_into_anchor: stack overflow\n");
2489 *r = TAILQ_NEXT(*r, entries);
2491 } else if (*depth == 0 && a != NULL)
2493 f = pf_anchor_stack + (*depth)++;
2496 if ((*r)->anchor_wildcard) {
2497 f->parent = &(*r)->anchor->children;
2498 if ((f->child = RB_MIN(pf_anchor_node, f->parent)) ==
2503 *rs = &f->child->ruleset;
2507 *rs = &(*r)->anchor->ruleset;
2509 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
2513 pf_step_out_of_anchor(int *depth, struct pf_ruleset **rs, int n,
2514 struct pf_rule **r, struct pf_rule **a, int *match)
2516 struct pf_anchor_stackframe *f;
2522 f = pf_anchor_stack + *depth - 1;
2523 if (f->parent != NULL && f->child != NULL) {
2524 if (f->child->match ||
2525 (match != NULL && *match)) {
2526 f->r->anchor->match = 1;
2529 f->child = RB_NEXT(pf_anchor_node, f->parent, f->child);
2530 if (f->child != NULL) {
2531 *rs = &f->child->ruleset;
2532 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr);
2540 if (*depth == 0 && a != NULL)
2543 if (f->r->anchor->match || (match != NULL && *match))
2544 quick = f->r->quick;
2545 *r = TAILQ_NEXT(f->r, entries);
2546 } while (*r == NULL);
2553 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr,
2554 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af)
2559 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2560 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2564 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) |
2565 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]);
2566 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) |
2567 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]);
2568 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) |
2569 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]);
2570 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) |
2571 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]);
2577 pf_addr_inc(struct pf_addr *addr, sa_family_t af)
2582 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1);
2586 if (addr->addr32[3] == 0xffffffff) {
2587 addr->addr32[3] = 0;
2588 if (addr->addr32[2] == 0xffffffff) {
2589 addr->addr32[2] = 0;
2590 if (addr->addr32[1] == 0xffffffff) {
2591 addr->addr32[1] = 0;
2593 htonl(ntohl(addr->addr32[0]) + 1);
2596 htonl(ntohl(addr->addr32[1]) + 1);
2599 htonl(ntohl(addr->addr32[2]) + 1);
2602 htonl(ntohl(addr->addr32[3]) + 1);
2608 #define mix(a,b,c) \
2610 a -= b; a -= c; a ^= (c >> 13); \
2611 b -= c; b -= a; b ^= (a << 8); \
2612 c -= a; c -= b; c ^= (b >> 13); \
2613 a -= b; a -= c; a ^= (c >> 12); \
2614 b -= c; b -= a; b ^= (a << 16); \
2615 c -= a; c -= b; c ^= (b >> 5); \
2616 a -= b; a -= c; a ^= (c >> 3); \
2617 b -= c; b -= a; b ^= (a << 10); \
2618 c -= a; c -= b; c ^= (b >> 15); \
2622 * hash function based on bridge_hash in if_bridge.c
2625 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
2626 struct pf_poolhashkey *key, sa_family_t af)
2628 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
2633 a += inaddr->addr32[0];
2636 hash->addr32[0] = c + key->key32[2];
2641 a += inaddr->addr32[0];
2642 b += inaddr->addr32[2];
2644 hash->addr32[0] = c;
2645 a += inaddr->addr32[1];
2646 b += inaddr->addr32[3];
2649 hash->addr32[1] = c;
2650 a += inaddr->addr32[2];
2651 b += inaddr->addr32[1];
2654 hash->addr32[2] = c;
2655 a += inaddr->addr32[3];
2656 b += inaddr->addr32[0];
2659 hash->addr32[3] = c;
2666 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
2667 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
2669 unsigned char hash[16];
2670 struct pf_pool *rpool = &r->rpool;
2671 struct pf_pooladdr *acur = rpool->cur;
2672 struct pf_pooladdr *cur;
2673 struct pf_addr *raddr;
2674 struct pf_addr *rmask;
2675 struct pf_addr counter;
2676 struct pf_src_node k;
2677 int cpu = mycpu->gd_cpuid;
2681 * NOTE! rpool->cur and rpool->tblidx can be iterators and thus
2682 * may represent a SMP race due to the shared nature of the
2683 * rpool structure. We allow the race and ensure that updates
2684 * do not create a fatal condition.
2688 raddr = &cur->addr.v.a.addr;
2689 rmask = &cur->addr.v.a.mask;
2691 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
2692 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
2694 PF_ACPY(&k.addr, saddr, af);
2695 if (r->rule_flag & PFRULE_RULESRCTRACK ||
2696 r->rpool.opts & PF_POOL_STICKYADDR)
2700 pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
2701 *sn = RB_FIND(pf_src_tree, &tree_src_tracking[cpu], &k);
2702 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
2703 PF_ACPY(naddr, &(*sn)->raddr, af);
2704 if (pf_status.debug >= PF_DEBUG_MISC) {
2705 kprintf("pf_map_addr: src tracking maps ");
2706 pf_print_host(&k.addr, 0, af);
2708 pf_print_host(naddr, 0, af);
2715 if (cur->addr.type == PF_ADDR_NOROUTE)
2717 if (cur->addr.type == PF_ADDR_DYNIFTL) {
2721 if (cur->addr.p.dyn->pfid_acnt4 < 1 &&
2722 (rpool->opts & PF_POOL_TYPEMASK) !=
2725 raddr = &cur->addr.p.dyn->pfid_addr4;
2726 rmask = &cur->addr.p.dyn->pfid_mask4;
2731 if (cur->addr.p.dyn->pfid_acnt6 < 1 &&
2732 (rpool->opts & PF_POOL_TYPEMASK) !=
2735 raddr = &cur->addr.p.dyn->pfid_addr6;
2736 rmask = &cur->addr.p.dyn->pfid_mask6;
2740 } else if (cur->addr.type == PF_ADDR_TABLE) {
2741 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
2742 return (1); /* unsupported */
2744 raddr = &cur->addr.v.a.addr;
2745 rmask = &cur->addr.v.a.mask;
2748 switch (rpool->opts & PF_POOL_TYPEMASK) {
2750 PF_ACPY(naddr, raddr, af);
2752 case PF_POOL_BITMASK:
2753 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
2755 case PF_POOL_RANDOM:
2756 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
2760 counter.addr32[0] = htonl(karc4random());
2765 if (rmask->addr32[3] != 0xffffffff)
2767 htonl(karc4random());
2770 if (rmask->addr32[2] != 0xffffffff)
2772 htonl(karc4random());
2775 if (rmask->addr32[1] != 0xffffffff)
2777 htonl(karc4random());
2780 if (rmask->addr32[0] != 0xffffffff)
2782 htonl(karc4random());
2786 PF_POOLMASK(naddr, raddr, rmask, &counter, af);
2787 PF_ACPY(init_addr, naddr, af);
2790 counter = rpool->counter;
2792 PF_AINC(&counter, af);
2793 PF_POOLMASK(naddr, raddr, rmask, &counter, af);
2794 rpool->counter = counter;
2797 case PF_POOL_SRCHASH:
2798 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
2799 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
2801 case PF_POOL_ROUNDROBIN:
2802 tblidx = rpool->tblidx;
2803 counter = rpool->counter;
2804 if (cur->addr.type == PF_ADDR_TABLE) {
2805 if (!pfr_pool_get(cur->addr.p.tbl,
2807 &raddr, &rmask, af)) {
2810 } else if (cur->addr.type == PF_ADDR_DYNIFTL) {
2811 if (!pfr_pool_get(cur->addr.p.dyn->pfid_kt,
2813 &raddr, &rmask, af)) {
2816 } else if (pf_match_addr(0, raddr, rmask,
2822 if ((cur = TAILQ_NEXT(cur, entries)) == NULL)
2823 cur = TAILQ_FIRST(&rpool->list);
2824 if (cur->addr.type == PF_ADDR_TABLE) {
2826 if (pfr_pool_get(cur->addr.p.tbl,
2828 &raddr, &rmask, af)) {
2829 /* table contains no address of type 'af' */
2834 } else if (cur->addr.type == PF_ADDR_DYNIFTL) {
2836 if (pfr_pool_get(cur->addr.p.dyn->pfid_kt,
2838 &raddr, &rmask, af)) {
2839 /* table contains no address of type 'af' */
2845 raddr = &cur->addr.v.a.addr;
2846 rmask = &cur->addr.v.a.mask;
2847 PF_ACPY(&counter, raddr, af);
2852 rpool->tblidx = tblidx;
2853 PF_ACPY(naddr, &counter, af);
2854 if (init_addr != NULL && PF_AZERO(init_addr, af))
2855 PF_ACPY(init_addr, naddr, af);
2856 PF_AINC(&counter, af);
2857 rpool->counter = counter;
2861 PF_ACPY(&(*sn)->raddr, naddr, af);
2863 if (pf_status.debug >= PF_DEBUG_MISC &&
2864 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
2865 kprintf("pf_map_addr: selected address ");
2866 pf_print_host(naddr, 0, af);
2874 pf_get_sport(struct pf_pdesc *pd, sa_family_t af,
2875 u_int8_t proto, struct pf_rule *r,
2876 struct pf_addr *saddr, struct pf_addr *daddr,
2877 u_int16_t sport, u_int16_t dport,
2878 struct pf_addr *naddr, u_int16_t *nport,
2879 u_int16_t low, u_int16_t high, struct pf_src_node **sn)
2881 struct pf_state_key_cmp key;
2882 struct pf_addr init_addr;
2884 u_int32_t toeplitz_sport;
2886 bzero(&init_addr, sizeof(init_addr));
2887 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2890 if (proto == IPPROTO_ICMP) {
2895 bzero(&key, sizeof(key));
2898 key.port[0] = dport;
2899 PF_ACPY(&key.addr[0], daddr, key.af);
2902 PF_ACPY(&key.addr[1], naddr, key.af);
2905 * We want to select a port that calculates to a toeplitz hash
2906 * that masks to the same cpu, otherwise the response may
2907 * not see the new state.
2909 * We can still do this even if the kernel is disregarding
2910 * the hash and vectoring the packets to a specific cpu,
2911 * but it will reduce the number of ports we can use.
2916 toeplitz_piecemeal_port(sport) ^
2917 toeplitz_piecemeal_addr(saddr->v4.s_addr) ^
2918 toeplitz_piecemeal_addr(naddr->v4.s_addr);
2929 * port search; start random, step;
2930 * similar 2 portloop in in_pcbbind
2932 * WARNING! We try to match such that the kernel will
2933 * dispatch the translated host/port to the same
2934 * cpu, but this might not be possible.
2936 * In the case where the port is fixed, or for the
2937 * UDP case (whos toeplitz does not incorporate the
2938 * port), we set not_cpu_localized which ultimately
2939 * causes the pf_state_tree element
2941 * XXX fixed ports present a problem for cpu localization.
2943 if (!(proto == IPPROTO_TCP ||
2944 proto == IPPROTO_UDP ||
2945 proto == IPPROTO_ICMP)) {
2947 * non-specific protocol, leave port intact.
2949 key.port[1] = sport;
2950 if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
2952 pd->not_cpu_localized = 1;
2955 } else if (low == 0 && high == 0) {
2957 * static-port same as originator.
2959 key.port[1] = sport;
2960 if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
2962 pd->not_cpu_localized = 1;
2965 } else if (low == high) {
2967 * specific port as specified.
2969 key.port[1] = htons(low);
2970 if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
2971 *nport = htons(low);
2972 pd->not_cpu_localized = 1;
2977 * normal dynamic port
2987 cut = htonl(karc4random()) % (1 + high - low) + low;
2988 /* low <= cut <= high */
2989 for (tmp = cut; tmp <= high; ++(tmp)) {
2990 key.port[1] = htons(tmp);
2991 if ((toeplitz_piecemeal_port(key.port[1]) ^
2992 toeplitz_sport) & ncpus2_mask) {
2995 if (pf_find_state_all(&key, PF_IN, NULL) ==
2996 NULL && !in_baddynamic(tmp, proto)) {
2997 if (proto == IPPROTO_UDP)
2998 pd->not_cpu_localized = 1;
2999 *nport = htons(tmp);
3003 for (tmp = cut - 1; tmp >= low; --(tmp)) {
3004 key.port[1] = htons(tmp);
3005 if ((toeplitz_piecemeal_port(key.port[1]) ^
3006 toeplitz_sport) & ncpus2_mask) {
3009 if (pf_find_state_all(&key, PF_IN, NULL) ==
3010 NULL && !in_baddynamic(tmp, proto)) {
3011 if (proto == IPPROTO_UDP)
3012 pd->not_cpu_localized = 1;
3013 *nport = htons(tmp);
3022 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
3023 case PF_POOL_RANDOM:
3024 case PF_POOL_ROUNDROBIN:
3025 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
3029 case PF_POOL_SRCHASH:
3030 case PF_POOL_BITMASK:
3034 } while (! PF_AEQ(&init_addr, naddr, af) );
3035 return (1); /* none available */
3039 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
3040 int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
3041 struct pf_addr *daddr, u_int16_t dport, int rs_num)
3043 struct pf_rule *r, *rm = NULL;
3044 struct pf_ruleset *ruleset = NULL;
3049 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
3050 while (r && rm == NULL) {
3051 struct pf_rule_addr *src = NULL, *dst = NULL;
3052 struct pf_addr_wrap *xdst = NULL;
3053 struct pf_pooladdr *cur;
3055 if (r->action == PF_BINAT && direction == PF_IN) {
3057 cur = r->rpool.cur; /* SMP race possible */
3067 if (pfi_kif_match(r->kif, kif) == r->ifnot)
3068 r = r->skip[PF_SKIP_IFP].ptr;
3069 else if (r->direction && r->direction != direction)
3070 r = r->skip[PF_SKIP_DIR].ptr;
3071 else if (r->af && r->af != pd->af)
3072 r = r->skip[PF_SKIP_AF].ptr;
3073 else if (r->proto && r->proto != pd->proto)
3074 r = r->skip[PF_SKIP_PROTO].ptr;
3075 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
3077 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
3078 PF_SKIP_DST_ADDR].ptr;
3079 else if (src->port_op && !pf_match_port(src->port_op,
3080 src->port[0], src->port[1], sport))
3081 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
3082 PF_SKIP_DST_PORT].ptr;
3083 else if (dst != NULL &&
3084 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL))
3085 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3086 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
3088 r = TAILQ_NEXT(r, entries);
3089 else if (dst != NULL && dst->port_op &&
3090 !pf_match_port(dst->port_op, dst->port[0],
3091 dst->port[1], dport))
3092 r = r->skip[PF_SKIP_DST_PORT].ptr;
3093 else if (r->match_tag && !pf_match_tag(m, r, &tag))
3094 r = TAILQ_NEXT(r, entries);
3095 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
3096 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
3097 off, pd->hdr.tcp), r->os_fingerprint)))
3098 r = TAILQ_NEXT(r, entries);
3102 if (r->rtableid >= 0)
3103 rtableid = r->rtableid;
3104 if (r->anchor == NULL) {
3107 pf_step_into_anchor(&asd, &ruleset, rs_num,
3111 pf_step_out_of_anchor(&asd, &ruleset, rs_num, &r,
3114 if (pf_tag_packet(m, tag, rtableid))
3116 if (rm != NULL && (rm->action == PF_NONAT ||
3117 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
3123 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
3124 struct pfi_kif *kif, struct pf_src_node **sn,
3125 struct pf_state_key **skw, struct pf_state_key **sks,
3126 struct pf_state_key **skp, struct pf_state_key **nkp,
3127 struct pf_addr *saddr, struct pf_addr *daddr,
3128 u_int16_t sport, u_int16_t dport)
3130 struct pf_rule *r = NULL;
3132 if (direction == PF_OUT) {
3133 r = pf_match_translation(pd, m, off, direction, kif, saddr,
3134 sport, daddr, dport, PF_RULESET_BINAT);
3136 r = pf_match_translation(pd, m, off, direction, kif,
3137 saddr, sport, daddr, dport, PF_RULESET_NAT);
3139 r = pf_match_translation(pd, m, off, direction, kif, saddr,
3140 sport, daddr, dport, PF_RULESET_RDR);
3142 r = pf_match_translation(pd, m, off, direction, kif,
3143 saddr, sport, daddr, dport, PF_RULESET_BINAT);
3147 struct pf_addr *naddr;
3150 if (pf_state_key_setup(pd, r, skw, sks, skp, nkp,
3151 saddr, daddr, sport, dport))
3154 /* XXX We only modify one side for now. */
3155 naddr = &(*nkp)->addr[1];
3156 nport = &(*nkp)->port[1];
3159 * NOTE: Currently all translations will clear
3160 * BRIDGE_MBUF_TAGGED, telling the bridge to
3161 * ignore the original input encapsulation.
3163 switch (r->action) {
3169 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
3170 if (pf_get_sport(pd, pd->af, pd->proto, r,
3171 saddr, daddr, sport, dport,
3172 naddr, nport, r->rpool.proxy_port[0],
3173 r->rpool.proxy_port[1], sn)) {
3174 DPFPRINTF(PF_DEBUG_MISC,
3175 ("pf: NAT proxy port allocation "
3177 r->rpool.proxy_port[0],
3178 r->rpool.proxy_port[1]));
3183 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
3184 switch (direction) {
3186 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
3190 if (r->rpool.cur->addr.p.dyn->
3194 &r->rpool.cur->addr.p.dyn->
3196 &r->rpool.cur->addr.p.dyn->
3203 if (r->rpool.cur->addr.p.dyn->
3207 &r->rpool.cur->addr.p.dyn->
3209 &r->rpool.cur->addr.p.dyn->
3217 &r->rpool.cur->addr.v.a.addr,
3218 &r->rpool.cur->addr.v.a.mask,
3222 if (r->src.addr.type == PF_ADDR_DYNIFTL) {
3226 if (r->src.addr.p.dyn->
3230 &r->src.addr.p.dyn->
3232 &r->src.addr.p.dyn->
3239 if (r->src.addr.p.dyn->
3243 &r->src.addr.p.dyn->
3245 &r->src.addr.p.dyn->
3253 &r->src.addr.v.a.addr,
3254 &r->src.addr.v.a.mask, daddr,
3260 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
3261 if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
3263 if ((r->rpool.opts & PF_POOL_TYPEMASK) ==
3265 PF_POOLMASK(naddr, naddr,
3266 &r->rpool.cur->addr.v.a.mask, daddr,
3269 if (r->rpool.proxy_port[1]) {
3270 u_int32_t tmp_nport;
3272 tmp_nport = ((ntohs(dport) -
3273 ntohs(r->dst.port[0])) %
3274 (r->rpool.proxy_port[1] -
3275 r->rpool.proxy_port[0] + 1)) +
3276 r->rpool.proxy_port[0];
3278 /* wrap around if necessary */
3279 if (tmp_nport > 65535)
3281 *nport = htons((u_int16_t)tmp_nport);
3282 } else if (r->rpool.proxy_port[0]) {
3283 *nport = htons(r->rpool.proxy_port[0]);
3285 pd->not_cpu_localized = 1;
3296 struct netmsg_hashlookup {
3297 struct netmsg_base base;
3298 struct inpcb **nm_pinp;
3299 struct inpcbinfo *nm_pcbinfo;
3300 struct pf_addr *nm_saddr;
3301 struct pf_addr *nm_daddr;
3307 #ifdef PF_SOCKET_LOOKUP_DOMSG
3309 in_pcblookup_hash_handler(netmsg_t msg)
3311 struct netmsg_hashlookup *rmsg = (struct netmsg_hashlookup *)msg;
3313 if (rmsg->nm_af == AF_INET)
3314 *rmsg->nm_pinp = in_pcblookup_hash(rmsg->nm_pcbinfo,
3315 rmsg->nm_saddr->v4, rmsg->nm_sport, rmsg->nm_daddr->v4,
3316 rmsg->nm_dport, INPLOOKUP_WILDCARD, NULL);
3319 *rmsg->nm_pinp = in6_pcblookup_hash(rmsg->nm_pcbinfo,
3320 &rmsg->nm_saddr->v6, rmsg->nm_sport, &rmsg->nm_daddr->v6,
3321 rmsg->nm_dport, INPLOOKUP_WILDCARD, NULL);
3323 lwkt_replymsg(&rmsg->base.lmsg, 0);
3325 #endif /* PF_SOCKET_LOOKUP_DOMSG */
3328 pf_socket_lookup(int direction, struct pf_pdesc *pd)
3330 struct pf_addr *saddr, *daddr;
3331 u_int16_t sport, dport;
3332 struct inpcbinfo *pi;
3334 struct netmsg_hashlookup *msg = NULL;
3335 #ifdef PF_SOCKET_LOOKUP_DOMSG
3336 struct netmsg_hashlookup msg0;
3342 pd->lookup.uid = UID_MAX;
3343 pd->lookup.gid = GID_MAX;
3344 pd->lookup.pid = NO_PID;
3345 if (direction == PF_IN) {
3352 switch (pd->proto) {
3354 if (pd->hdr.tcp == NULL)
3356 sport = pd->hdr.tcp->th_sport;
3357 dport = pd->hdr.tcp->th_dport;
3359 pi_cpu = tcp_addrcpu(saddr->v4.s_addr, sport, daddr->v4.s_addr, dport);
3360 pi = &tcbinfo[pi_cpu];
3362 * Our netstack runs lockless on MP systems
3363 * (only for TCP connections at the moment).
3365 * As we are not allowed to read another CPU's tcbinfo,
3366 * we have to ask that CPU via remote call to search the
3369 * Prepare a msg iff data belongs to another CPU.
3371 if (pi_cpu != mycpu->gd_cpuid) {
3372 #ifdef PF_SOCKET_LOOKUP_DOMSG
3376 * Following lwkt_domsg() is dangerous and could
3377 * lockup the network system, e.g.
3380 * netisr0 domsg to netisr1 (due to lookup)
3381 * netisr1 domsg to netisr0 (due to lookup)
3383 * We simply return -1 here, since we are probably
3384 * called before NAT, so the TCP packet should
3385 * already be on the correct CPU.
3388 netmsg_init(&msg->base, NULL, &curthread->td_msgport,
3389 0, in_pcblookup_hash_handler);
3390 msg->nm_pinp = &inp;
3391 msg->nm_pcbinfo = pi;
3392 msg->nm_saddr = saddr;
3393 msg->nm_sport = sport;
3394 msg->nm_daddr = daddr;
3395 msg->nm_dport = dport;
3396 msg->nm_af = pd->af;
3397 #else /* !PF_SOCKET_LOOKUP_DOMSG */
3398 kprintf("pf_socket_lookup: tcp packet not on the "
3399 "correct cpu %d, cur cpu %d\n",
3401 print_backtrace(-1);
3403 #endif /* PF_SOCKET_LOOKUP_DOMSG */
3407 if (pd->hdr.udp == NULL)
3409 sport = pd->hdr.udp->uh_sport;
3410 dport = pd->hdr.udp->uh_dport;
3411 pi = &udbinfo[mycpuid];
3416 if (direction != PF_IN) {
3427 * Query other CPU, second part
3429 * msg only gets initialized when:
3431 * 2) the info belongs to another CPU
3433 * Use some switch/case magic to avoid code duplication.
3436 inp = in6_pcblookup_hash(pi, &saddr->v6, sport,
3437 &daddr->v6, dport, INPLOOKUP_WILDCARD, NULL);
3443 /* FALLTHROUGH if SMP and on other CPU */
3447 lwkt_domsg(netisr_cpuport(pi_cpu),
3448 &msg->base.lmsg, 0);
3451 inp = in_pcblookup_hash(pi, saddr->v4, sport, daddr->v4,
3452 dport, INPLOOKUP_WILDCARD, NULL);
3461 pd->lookup.uid = inp->inp_socket->so_cred->cr_uid;
3462 pd->lookup.gid = inp->inp_socket->so_cred->cr_groups[0];
3467 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3471 u_int8_t *opt, optlen;
3472 u_int8_t wscale = 0;
3474 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
3475 if (hlen <= sizeof(struct tcphdr))
3477 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3479 opt = hdr + sizeof(struct tcphdr);
3480 hlen -= sizeof(struct tcphdr);
3490 if (wscale > TCP_MAX_WINSHIFT)
3491 wscale = TCP_MAX_WINSHIFT;
3492 wscale |= PF_WSCALE_FLAG;
3507 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af)
3511 u_int8_t *opt, optlen;
3512 u_int16_t mss = tcp_mssdflt;
3514 hlen = th_off << 2; /* hlen <= sizeof(hdr) */
3515 if (hlen <= sizeof(struct tcphdr))
3517 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af))
3519 opt = hdr + sizeof(struct tcphdr);
3520 hlen -= sizeof(struct tcphdr);
3521 while (hlen >= TCPOLEN_MAXSEG) {
3529 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2);
3544 pf_calc_mss(struct pf_addr *addr, sa_family_t af, u_int16_t offer)
3547 struct sockaddr_in *dst;
3551 struct sockaddr_in6 *dst6;
3552 struct route_in6 ro6;
3554 struct rtentry *rt = NULL;
3556 u_int16_t mss = tcp_mssdflt;
3561 hlen = sizeof(struct ip);
3562 bzero(&ro, sizeof(ro));
3563 dst = (struct sockaddr_in *)&ro.ro_dst;
3564 dst->sin_family = AF_INET;
3565 dst->sin_len = sizeof(*dst);
3566 dst->sin_addr = addr->v4;
3567 rtalloc_ign(&ro, (RTF_CLONING | RTF_PRCLONING));
3573 hlen = sizeof(struct ip6_hdr);
3574 bzero(&ro6, sizeof(ro6));
3575 dst6 = (struct sockaddr_in6 *)&ro6.ro_dst;
3576 dst6->sin6_family = AF_INET6;
3577 dst6->sin6_len = sizeof(*dst6);
3578 dst6->sin6_addr = addr->v6;
3579 rtalloc_ign((struct route *)&ro6, (RTF_CLONING | RTF_PRCLONING));
3585 if (rt && rt->rt_ifp) {
3586 mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr);
3587 mss = max(tcp_mssdflt, mss);
3590 mss = min(mss, offer);
3591 mss = max(mss, 64); /* sanity - at least max opt space */
3596 pf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr)
3598 struct pf_rule *r = s->rule.ptr;
3601 if (!r->rt || r->rt == PF_FASTROUTE)
3603 switch (s->key[PF_SK_WIRE]->af) {
3606 pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL,
3608 s->rt_kif = r->rpool.cur->kif;
3613 pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL,
3615 s->rt_kif = r->rpool.cur->kif;
3622 pf_tcp_iss(struct pf_pdesc *pd)
3625 u_int32_t digest[4];
3627 if (pf_tcp_secret_init == 0) {
3628 lwkt_gettoken(&pf_gtoken);
3629 if (pf_tcp_secret_init == 0) {
3630 karc4rand(pf_tcp_secret, sizeof(pf_tcp_secret));
3631 MD5Init(&pf_tcp_secret_ctx);
3632 MD5Update(&pf_tcp_secret_ctx, pf_tcp_secret,
3633 sizeof(pf_tcp_secret));
3634 pf_tcp_secret_init = 1;
3636 lwkt_reltoken(&pf_gtoken);
3638 ctx = pf_tcp_secret_ctx;
3640 MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short));
3641 MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short));
3642 if (pd->af == AF_INET6) {
3643 MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr));
3644 MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr));
3646 MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr));
3647 MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr));
3649 MD5Final((u_char *)digest, &ctx);
3650 pf_tcp_iss_off += 4096;
3652 return (digest[0] + pd->hdr.tcp->th_seq + pf_tcp_iss_off);
3656 pf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction,
3657 struct pfi_kif *kif, struct mbuf *m, int off, void *h,
3658 struct pf_pdesc *pd, struct pf_rule **am, struct pf_ruleset **rsm,
3659 struct ifqueue *ifq, struct inpcb *inp)
3661 struct pf_rule *nr = NULL;
3662 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
3663 sa_family_t af = pd->af;
3664 struct pf_rule *r, *a = NULL;
3665 struct pf_ruleset *ruleset = NULL;
3666 struct pf_src_node *nsn = NULL;
3667 struct tcphdr *th = pd->hdr.tcp;
3668 struct pf_state_key *skw = NULL, *sks = NULL;
3669 struct pf_state_key *sk = NULL, *nk = NULL;
3671 int rewrite = 0, hdrlen = 0;
3672 int tag = -1, rtableid = -1;
3676 u_int16_t sport = 0, dport = 0;
3677 u_int16_t bproto_sum = 0, bip_sum = 0;
3678 u_int8_t icmptype = 0, icmpcode = 0;
3681 if (direction == PF_IN && pf_check_congestion(ifq)) {
3682 REASON_SET(&reason, PFRES_CONGEST);
3687 pd->lookup.done = pf_socket_lookup(direction, pd);
3688 else if (debug_pfugidhack) {
3689 DPFPRINTF(PF_DEBUG_MISC, ("pf: unlocked lookup\n"));
3690 pd->lookup.done = pf_socket_lookup(direction, pd);
3693 switch (pd->proto) {
3695 sport = th->th_sport;
3696 dport = th->th_dport;
3697 hdrlen = sizeof(*th);
3700 sport = pd->hdr.udp->uh_sport;
3701 dport = pd->hdr.udp->uh_dport;
3702 hdrlen = sizeof(*pd->hdr.udp);
3706 if (pd->af != AF_INET)
3708 sport = dport = pd->hdr.icmp->icmp_id;
3709 hdrlen = sizeof(*pd->hdr.icmp);
3710 icmptype = pd->hdr.icmp->icmp_type;
3711 icmpcode = pd->hdr.icmp->icmp_code;
3713 if (icmptype == ICMP_UNREACH ||
3714 icmptype == ICMP_SOURCEQUENCH ||
3715 icmptype == ICMP_REDIRECT ||
3716 icmptype == ICMP_TIMXCEED ||
3717 icmptype == ICMP_PARAMPROB)
3722 case IPPROTO_ICMPV6:
3725 sport = dport = pd->hdr.icmp6->icmp6_id;
3726 hdrlen = sizeof(*pd->hdr.icmp6);
3727 icmptype = pd->hdr.icmp6->icmp6_type;
3728 icmpcode = pd->hdr.icmp6->icmp6_code;
3730 if (icmptype == ICMP6_DST_UNREACH ||
3731 icmptype == ICMP6_PACKET_TOO_BIG ||
3732 icmptype == ICMP6_TIME_EXCEEDED ||
3733 icmptype == ICMP6_PARAM_PROB)
3738 sport = dport = hdrlen = 0;
3742 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
3744 /* check packet for BINAT/NAT/RDR */
3745 if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn,
3746 &skw, &sks, &sk, &nk, saddr, daddr, sport, dport)) != NULL) {
3747 if (nk == NULL || sk == NULL) {
3748 REASON_SET(&reason, PFRES_MEMORY);
3753 bip_sum = *pd->ip_sum;
3755 m->m_flags &= ~M_HASH;
3756 switch (pd->proto) {
3758 bproto_sum = th->th_sum;
3759 pd->proto_sum = &th->th_sum;
3761 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) ||
3762 nk->port[pd->sidx] != sport) {
3763 pf_change_ap(saddr, &th->th_sport, pd->ip_sum,
3764 &th->th_sum, &nk->addr[pd->sidx],
3765 nk->port[pd->sidx], 0, af);
3766 pd->sport = &th->th_sport;
3767 sport = th->th_sport;
3770 if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) ||
3771 nk->port[pd->didx] != dport) {
3772 pf_change_ap(daddr, &th->th_dport, pd->ip_sum,
3773 &th->th_sum, &nk->addr[pd->didx],
3774 nk->port[pd->didx], 0, af);
3775 dport = th->th_dport;
3776 pd->dport = &th->th_dport;
3781 bproto_sum = pd->hdr.udp->uh_sum;
3782 pd->proto_sum = &pd->hdr.udp->uh_sum;
3784 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) ||
3785 nk->port[pd->sidx] != sport) {
3786 pf_change_ap(saddr, &pd->hdr.udp->uh_sport,
3787 pd->ip_sum, &pd->hdr.udp->uh_sum,
3788 &nk->addr[pd->sidx],
3789 nk->port[pd->sidx], 1, af);
3790 sport = pd->hdr.udp->uh_sport;
3791 pd->sport = &pd->hdr.udp->uh_sport;
3794 if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) ||
3795 nk->port[pd->didx] != dport) {
3796 pf_change_ap(daddr, &pd->hdr.udp->uh_dport,
3797 pd->ip_sum, &pd->hdr.udp->uh_sum,
3798 &nk->addr[pd->didx],
3799 nk->port[pd->didx], 1, af);
3800 dport = pd->hdr.udp->uh_dport;
3801 pd->dport = &pd->hdr.udp->uh_dport;
3807 nk->port[0] = nk->port[1];
3808 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET))
3809 pf_change_a(&saddr->v4.s_addr, pd->ip_sum,
3810 nk->addr[pd->sidx].v4.s_addr, 0);
3812 if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET))
3813 pf_change_a(&daddr->v4.s_addr, pd->ip_sum,
3814 nk->addr[pd->didx].v4.s_addr, 0);
3816 if (nk->port[1] != pd->hdr.icmp->icmp_id) {
3817 pd->hdr.icmp->icmp_cksum = pf_cksum_fixup(
3818 pd->hdr.icmp->icmp_cksum, sport,
3820 pd->hdr.icmp->icmp_id = nk->port[1];
3821 pd->sport = &pd->hdr.icmp->icmp_id;
3823 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
3827 case IPPROTO_ICMPV6:
3828 nk->port[0] = nk->port[1];
3829 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6))
3830 pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum,
3831 &nk->addr[pd->sidx], 0);
3833 if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6))
3834 pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum,
3835 &nk->addr[pd->didx], 0);
3844 &nk->addr[pd->sidx], AF_INET))
3845 pf_change_a(&saddr->v4.s_addr,
3847 nk->addr[pd->sidx].v4.s_addr, 0);
3850 &nk->addr[pd->didx], AF_INET))
3851 pf_change_a(&daddr->v4.s_addr,
3853 nk->addr[pd->didx].v4.s_addr, 0);
3859 &nk->addr[pd->sidx], AF_INET6))
3860 PF_ACPY(saddr, &nk->addr[pd->sidx], af);
3863 &nk->addr[pd->didx], AF_INET6))
3864 PF_ACPY(saddr, &nk->addr[pd->didx], af);
3877 if (pfi_kif_match(r->kif, kif) == r->ifnot)
3878 r = r->skip[PF_SKIP_IFP].ptr;
3879 else if (r->direction && r->direction != direction)
3880 r = r->skip[PF_SKIP_DIR].ptr;
3881 else if (r->af && r->af != af)
3882 r = r->skip[PF_SKIP_AF].ptr;
3883 else if (r->proto && r->proto != pd->proto)
3884 r = r->skip[PF_SKIP_PROTO].ptr;
3885 else if (PF_MISMATCHAW(&r->src.addr, saddr, af,
3887 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
3888 /* tcp/udp only. port_op always 0 in other cases */
3889 else if (r->src.port_op && !pf_match_port(r->src.port_op,
3890 r->src.port[0], r->src.port[1], sport))
3891 r = r->skip[PF_SKIP_SRC_PORT].ptr;
3892 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af,
3894 r = r->skip[PF_SKIP_DST_ADDR].ptr;
3895 /* tcp/udp only. port_op always 0 in other cases */
3896 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
3897 r->dst.port[0], r->dst.port[1], dport))
3898 r = r->skip[PF_SKIP_DST_PORT].ptr;
3899 /* icmp only. type always 0 in other cases */
3900 else if (r->type && r->type != icmptype + 1)
3901 r = TAILQ_NEXT(r, entries);
3902 /* icmp only. type always 0 in other cases */
3903 else if (r->code && r->code != icmpcode + 1)
3904 r = TAILQ_NEXT(r, entries);
3905 else if (r->tos && !(r->tos == pd->tos))
3906 r = TAILQ_NEXT(r, entries);
3907 else if (r->rule_flag & PFRULE_FRAGMENT)
3908 r = TAILQ_NEXT(r, entries);
3909 else if (pd->proto == IPPROTO_TCP &&
3910 (r->flagset & th->th_flags) != r->flags)
3911 r = TAILQ_NEXT(r, entries);
3912 /* tcp/udp only. uid.op always 0 in other cases */
3913 else if (r->uid.op && (pd->lookup.done || (pd->lookup.done =
3914 pf_socket_lookup(direction, pd), 1)) &&
3915 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1],
3917 r = TAILQ_NEXT(r, entries);
3918 /* tcp/udp only. gid.op always 0 in other cases */
3919 else if (r->gid.op && (pd->lookup.done || (pd->lookup.done =
3920 pf_socket_lookup(direction, pd), 1)) &&
3921 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1],
3923 r = TAILQ_NEXT(r, entries);
3925 r->prob <= karc4random())
3926 r = TAILQ_NEXT(r, entries);
3927 else if (r->match_tag && !pf_match_tag(m, r, &tag))
3928 r = TAILQ_NEXT(r, entries);
3929 else if (r->os_fingerprint != PF_OSFP_ANY &&
3930 (pd->proto != IPPROTO_TCP || !pf_osfp_match(
3931 pf_osfp_fingerprint(pd, m, off, th),
3932 r->os_fingerprint)))
3933 r = TAILQ_NEXT(r, entries);
3937 if (r->rtableid >= 0)
3938 rtableid = r->rtableid;
3939 if (r->anchor == NULL) {
3946 r = TAILQ_NEXT(r, entries);
3948 pf_step_into_anchor(&asd, &ruleset,
3949 PF_RULESET_FILTER, &r, &a, &match);
3951 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
3952 PF_RULESET_FILTER, &r, &a, &match))
3959 REASON_SET(&reason, PFRES_MATCH);
3961 if (r->log || (nr != NULL && nr->log)) {
3963 m_copyback(m, off, hdrlen, pd->hdr.any);
3964 PFLOG_PACKET(kif, h, m, af, direction, reason, r->log ? r : nr,
3968 if ((r->action == PF_DROP) &&
3969 ((r->rule_flag & PFRULE_RETURNRST) ||
3970 (r->rule_flag & PFRULE_RETURNICMP) ||
3971 (r->rule_flag & PFRULE_RETURN))) {
3972 /* undo NAT changes, if they have taken place */
3974 PF_ACPY(saddr, &sk->addr[pd->sidx], af);
3975 PF_ACPY(daddr, &sk->addr[pd->didx], af);
3977 *pd->sport = sk->port[pd->sidx];
3979 *pd->dport = sk->port[pd->didx];
3981 *pd->proto_sum = bproto_sum;
3983 *pd->ip_sum = bip_sum;
3984 m_copyback(m, off, hdrlen, pd->hdr.any);
3986 if (pd->proto == IPPROTO_TCP &&
3987 ((r->rule_flag & PFRULE_RETURNRST) ||
3988 (r->rule_flag & PFRULE_RETURN)) &&
3989 !(th->th_flags & TH_RST)) {
3990 u_int32_t ack = ntohl(th->th_seq) + pd->p_len;
3998 h4 = mtod(m, struct ip *);
3999 len = h4->ip_len - off;
4003 h6 = mtod(m, struct ip6_hdr *);
4004 len = h6->ip6_plen - (off - sizeof(*h6));
4009 if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af))
4010 REASON_SET(&reason, PFRES_PROTCKSUM);
4012 if (th->th_flags & TH_SYN)
4014 if (th->th_flags & TH_FIN)
4016 pf_send_tcp(r, af, pd->dst,
4017 pd->src, th->th_dport, th->th_sport,
4018 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0,
4019 r->return_ttl, 1, 0, pd->eh, kif->pfik_ifp);
4021 } else if (pd->proto != IPPROTO_ICMP && af == AF_INET &&
4023 pf_send_icmp(m, r->return_icmp >> 8,
4024 r->return_icmp & 255, af, r);
4025 else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 &&
4027 pf_send_icmp(m, r->return_icmp6 >> 8,
4028 r->return_icmp6 & 255, af, r);
4031 if (r->action == PF_DROP)
4034 if (pf_tag_packet(m, tag, rtableid)) {
4035 REASON_SET(&reason, PFRES_MEMORY);
4039 if (!state_icmp && (r->keep_state || nr != NULL ||
4040 (pd->flags & PFDESC_TCP_NORM))) {
4042 action = pf_create_state(r, nr, a, pd, nsn, skw, sks, nk, sk, m,
4043 off, sport, dport, &rewrite, kif, sm, tag, bproto_sum,
4045 if (action != PF_PASS)
4049 /* copy back packet headers if we performed NAT operations */
4051 m_copyback(m, off, hdrlen, pd->hdr.any);
4057 kfree(sk, M_PFSTATEKEYPL);
4059 kfree(nk, M_PFSTATEKEYPL);
4064 pf_create_state(struct pf_rule *r, struct pf_rule *nr, struct pf_rule *a,
4065 struct pf_pdesc *pd, struct pf_src_node *nsn, struct pf_state_key *skw,
4066 struct pf_state_key *sks, struct pf_state_key *nk, struct pf_state_key *sk,
4067 struct mbuf *m, int off, u_int16_t sport, u_int16_t dport, int *rewrite,
4068 struct pfi_kif *kif, struct pf_state **sm, int tag, u_int16_t bproto_sum,
4069 u_int16_t bip_sum, int hdrlen)
4071 struct pf_state *s = NULL;
4072 struct pf_src_node *sn = NULL;
4073 struct tcphdr *th = pd->hdr.tcp;
4074 u_int16_t mss = tcp_mssdflt;
4076 int cpu = mycpu->gd_cpuid;
4078 /* check maximums */
4079 if (r->max_states && (r->states_cur >= r->max_states)) {
4080 pf_status.lcounters[LCNT_STATES]++;
4081 REASON_SET(&reason, PFRES_MAXSTATES);
4084 /* src node for filter rule */
4085 if ((r->rule_flag & PFRULE_SRCTRACK ||
4086 r->rpool.opts & PF_POOL_STICKYADDR) &&
4087 pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) {
4088 REASON_SET(&reason, PFRES_SRCLIMIT);
4091 /* src node for translation rule */
4092 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) &&
4093 pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) {
4094 REASON_SET(&reason, PFRES_SRCLIMIT);
4097 s = kmalloc(sizeof(struct pf_state), M_PFSTATEPL, M_NOWAIT|M_ZERO);
4099 REASON_SET(&reason, PFRES_MEMORY);
4102 lockinit(&s->lk, "pfstlk", 0, 0);
4103 s->id = 0; /* XXX Do we really need that? not in OpenBSD */
4106 s->nat_rule.ptr = nr;
4108 s->state_flags = PFSTATE_CREATEINPROG;
4109 STATE_INC_COUNTERS(s);
4111 s->state_flags |= PFSTATE_ALLOWOPTS;
4112 if (r->rule_flag & PFRULE_STATESLOPPY)
4113 s->state_flags |= PFSTATE_SLOPPY;
4114 if (pd->not_cpu_localized)
4115 s->state_flags |= PFSTATE_STACK_GLOBAL;
4117 s->log = r->log & PF_LOG_ALL;
4119 s->log |= nr->log & PF_LOG_ALL;
4120 switch (pd->proto) {
4122 s->src.seqlo = ntohl(th->th_seq);
4123 s->src.seqhi = s->src.seqlo + pd->p_len + 1;
4124 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN &&
4125 r->keep_state == PF_STATE_MODULATE) {
4126 /* Generate sequence number modulator */
4127 if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) ==
4130 pf_change_a(&th->th_seq, &th->th_sum,
4131 htonl(s->src.seqlo + s->src.seqdiff), 0);
4135 if (th->th_flags & TH_SYN) {
4137 s->src.wscale = pf_get_wscale(m, off,
4138 th->th_off, pd->af);
4140 s->src.max_win = MAX(ntohs(th->th_win), 1);
4141 if (s->src.wscale & PF_WSCALE_MASK) {
4142 /* Remove scale factor from initial window */
4143 int win = s->src.max_win;
4144 win += 1 << (s->src.wscale & PF_WSCALE_MASK);
4145 s->src.max_win = (win - 1) >>
4146 (s->src.wscale & PF_WSCALE_MASK);
4148 if (th->th_flags & TH_FIN)
4152 s->src.state = TCPS_SYN_SENT;
4153 s->dst.state = TCPS_CLOSED;
4154 s->timeout = PFTM_TCP_FIRST_PACKET;
4157 s->src.state = PFUDPS_SINGLE;
4158 s->dst.state = PFUDPS_NO_TRAFFIC;
4159 s->timeout = PFTM_UDP_FIRST_PACKET;
4163 case IPPROTO_ICMPV6:
4165 s->timeout = PFTM_ICMP_FIRST_PACKET;
4168 s->src.state = PFOTHERS_SINGLE;
4169 s->dst.state = PFOTHERS_NO_TRAFFIC;
4170 s->timeout = PFTM_OTHER_FIRST_PACKET;
4173 s->creation = time_second;
4174 s->expire = time_second;
4178 s->src_node->states++;
4181 /* XXX We only modify one side for now. */
4182 PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af);
4183 s->nat_src_node = nsn;
4184 s->nat_src_node->states++;
4186 if (pd->proto == IPPROTO_TCP) {
4187 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m,
4188 off, pd, th, &s->src, &s->dst)) {
4189 REASON_SET(&reason, PFRES_MEMORY);
4190 pf_src_tree_remove_state(s);
4191 STATE_DEC_COUNTERS(s);
4192 kfree(s, M_PFSTATEPL);
4195 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub &&
4196 pf_normalize_tcp_stateful(m, off, pd, &reason, th, s,
4197 &s->src, &s->dst, rewrite)) {
4198 /* This really shouldn't happen!!! */
4199 DPFPRINTF(PF_DEBUG_URGENT,
4200 ("pf_normalize_tcp_stateful failed on first pkt"));
4201 pf_normalize_tcp_cleanup(s);
4202 pf_src_tree_remove_state(s);
4203 STATE_DEC_COUNTERS(s);
4204 kfree(s, M_PFSTATEPL);
4208 s->direction = pd->dir;
4210 if (sk == NULL && pf_state_key_setup(pd, nr, &skw, &sks, &sk, &nk,
4211 pd->src, pd->dst, sport, dport)) {
4212 REASON_SET(&reason, PFRES_MEMORY);
4216 if (pf_state_insert(BOUND_IFACE(r, kif), skw, sks, s)) {
4217 if (pd->proto == IPPROTO_TCP)
4218 pf_normalize_tcp_cleanup(s);
4219 REASON_SET(&reason, PFRES_STATEINS);
4220 pf_src_tree_remove_state(s);
4221 STATE_DEC_COUNTERS(s);
4222 kfree(s, M_PFSTATEPL);
4227 pf_set_rt_ifp(s, pd->src); /* needs s->state_key set */
4232 if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) ==
4233 TH_SYN && r->keep_state == PF_STATE_SYNPROXY) {
4234 s->src.state = PF_TCPS_PROXY_SRC;
4235 /* undo NAT changes, if they have taken place */
4237 struct pf_state_key *skt = s->key[PF_SK_WIRE];
4238 if (pd->dir == PF_OUT)
4239 skt = s->key[PF_SK_STACK];
4240 PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af);
4241 PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af);
4243 *pd->sport = skt->port[pd->sidx];
4245 *pd->dport = skt->port[pd->didx];
4247 *pd->proto_sum = bproto_sum;
4249 *pd->ip_sum = bip_sum;
4250 m->m_flags &= ~M_HASH;
4251 m_copyback(m, off, hdrlen, pd->hdr.any);
4253 s->src.seqhi = htonl(karc4random());
4254 /* Find mss option */
4255 mss = pf_get_mss(m, off, th->th_off, pd->af);
4256 mss = pf_calc_mss(pd->src, pd->af, mss);
4257 mss = pf_calc_mss(pd->dst, pd->af, mss);
4259 s->state_flags &= ~PFSTATE_CREATEINPROG;
4260 pf_send_tcp(r, pd->af, pd->dst, pd->src, th->th_dport,
4261 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1,
4262 TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL, NULL);
4263 REASON_SET(&reason, PFRES_SYNPROXY);
4264 return (PF_SYNPROXY_DROP);
4267 s->state_flags &= ~PFSTATE_CREATEINPROG;
4272 kfree(sk, M_PFSTATEKEYPL);
4274 kfree(nk, M_PFSTATEKEYPL);
4276 if (sn != NULL && sn->states == 0 && sn->expire == 0) {
4277 RB_REMOVE(pf_src_tree, &tree_src_tracking[cpu], sn);
4278 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4279 atomic_add_int(&pf_status.src_nodes, -1);
4280 kfree(sn, M_PFSRCTREEPL);
4282 if (nsn != sn && nsn != NULL && nsn->states == 0 && nsn->expire == 0) {
4283 RB_REMOVE(pf_src_tree, &tree_src_tracking[cpu], nsn);
4284 pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++;
4285 atomic_add_int(&pf_status.src_nodes, -1);
4286 kfree(nsn, M_PFSRCTREEPL);
4289 pf_src_tree_remove_state(s);
4290 STATE_DEC_COUNTERS(s);
4291 kfree(s, M_PFSTATEPL);
4298 pf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif,
4299 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am,
4300 struct pf_ruleset **rsm)
4302 struct pf_rule *r, *a = NULL;
4303 struct pf_ruleset *ruleset = NULL;
4304 sa_family_t af = pd->af;
4310 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr);
4313 if (pfi_kif_match(r->kif, kif) == r->ifnot)
4314 r = r->skip[PF_SKIP_IFP].ptr;
4315 else if (r->direction && r->direction != direction)
4316 r = r->skip[PF_SKIP_DIR].ptr;
4317 else if (r->af && r->af != af)
4318 r = r->skip[PF_SKIP_AF].ptr;
4319 else if (r->proto && r->proto != pd->proto)
4320 r = r->skip[PF_SKIP_PROTO].ptr;
4321 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af,
4323 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
4324 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af,
4326 r = r->skip[PF_SKIP_DST_ADDR].ptr;
4327 else if (r->tos && !(r->tos == pd->tos))
4328 r = TAILQ_NEXT(r, entries);
4329 else if (r->os_fingerprint != PF_OSFP_ANY)
4330 r = TAILQ_NEXT(r, entries);
4331 else if (pd->proto == IPPROTO_UDP &&
4332 (r->src.port_op || r->dst.port_op))
4333 r = TAILQ_NEXT(r, entries);
4334 else if (pd->proto == IPPROTO_TCP &&
4335 (r->src.port_op || r->dst.port_op || r->flagset))
4336 r = TAILQ_NEXT(r, entries);
4337 else if ((pd->proto == IPPROTO_ICMP ||
4338 pd->proto == IPPROTO_ICMPV6) &&
4339 (r->type || r->code))
4340 r = TAILQ_NEXT(r, entries);
4341 else if (r->prob && r->prob <= karc4random())
4342 r = TAILQ_NEXT(r, entries);
4343 else if (r->match_tag && !pf_match_tag(m, r, &tag))
4344 r = TAILQ_NEXT(r, entries);
4346 if (r->anchor == NULL) {
4353 r = TAILQ_NEXT(r, entries);
4355 pf_step_into_anchor(&asd, &ruleset,
4356 PF_RULESET_FILTER, &r, &a, &match);
4358 if (r == NULL && pf_step_out_of_anchor(&asd, &ruleset,
4359 PF_RULESET_FILTER, &r, &a, &match))
4366 REASON_SET(&reason, PFRES_MATCH);
4369 PFLOG_PACKET(kif, h, m, af, direction, reason, r, a, ruleset,
4372 if (r->action != PF_PASS)
4375 if (pf_tag_packet(m, tag, -1)) {
4376 REASON_SET(&reason, PFRES_MEMORY);
4384 * Called with state locked
4387 pf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst,
4388 struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off,
4389 struct pf_pdesc *pd, u_short *reason, int *copyback)
4391 struct tcphdr *th = pd->hdr.tcp;
4392 u_int16_t win = ntohs(th->th_win);
4393 u_int32_t ack, end, seq, orig_seq;
4397 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) {
4398 sws = src->wscale & PF_WSCALE_MASK;
4399 dws = dst->wscale & PF_WSCALE_MASK;
4405 * Sequence tracking algorithm from Guido van Rooij's paper:
4406 * http://www.madison-gurkha.com/publications/tcp_filtering/
4410 orig_seq = seq = ntohl(th->th_seq);
4411 if (src->seqlo == 0) {
4412 /* First packet from this end. Set its state */
4414 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) &&
4415 src->scrub == NULL) {
4416 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) {
4417 REASON_SET(reason, PFRES_MEMORY);
4422 /* Deferred generation of sequence number modulator */
4423 if (dst->seqdiff && !src->seqdiff) {
4424 /* use random iss for the TCP server */
4425 while ((src->seqdiff = karc4random() - seq) == 0)
4427 ack = ntohl(th->th_ack) - dst->seqdiff;
4428 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
4430 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
4433 ack = ntohl(th->th_ack);
4436 end = seq + pd->p_len;
4437 if (th->th_flags & TH_SYN) {
4439 (*state)->sync_flags |= PFSTATE_GOT_SYN2;
4440 if (dst->wscale & PF_WSCALE_FLAG) {
4441 src->wscale = pf_get_wscale(m, off, th->th_off,
4443 if (src->wscale & PF_WSCALE_FLAG) {
4444 /* Remove scale factor from initial
4446 sws = src->wscale & PF_WSCALE_MASK;
4447 win = ((u_int32_t)win + (1 << sws) - 1)
4449 dws = dst->wscale & PF_WSCALE_MASK;
4451 /* fixup other window */
4452 dst->max_win <<= dst->wscale &
4454 /* in case of a retrans SYN|ACK */
4459 if (th->th_flags & TH_FIN)
4463 if (src->state < TCPS_SYN_SENT)
4464 src->state = TCPS_SYN_SENT;
4467 * May need to slide the window (seqhi may have been set by
4468 * the crappy stack check or if we picked up the connection
4469 * after establishment)
4471 if (src->seqhi == 1 ||
4472 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi))
4473 src->seqhi = end + MAX(1, dst->max_win << dws);
4474 if (win > src->max_win)
4478 ack = ntohl(th->th_ack) - dst->seqdiff;
4480 /* Modulate sequence numbers */
4481 pf_change_a(&th->th_seq, &th->th_sum, htonl(seq +
4483 pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0);
4486 end = seq + pd->p_len;
4487 if (th->th_flags & TH_SYN)
4489 if (th->th_flags & TH_FIN)
4493 if ((th->th_flags & TH_ACK) == 0) {
4494 /* Let it pass through the ack skew check */
4496 } else if ((ack == 0 &&
4497 (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) ||
4498 /* broken tcp stacks do not set ack */
4499 (dst->state < TCPS_SYN_SENT)) {
4501 * Many stacks (ours included) will set the ACK number in an
4502 * FIN|ACK if the SYN times out -- no sequence to ACK.
4508 /* Ease sequencing restrictions on no data packets */
4513 ackskew = dst->seqlo - ack;
4517 * Need to demodulate the sequence numbers in any TCP SACK options
4518 * (Selective ACK). We could optionally validate the SACK values
4519 * against the current ACK window, either forwards or backwards, but
4520 * I'm not confident that SACK has been implemented properly
4521 * everywhere. It wouldn't surprise me if several stacks accidently
4522 * SACK too far backwards of previously ACKed data. There really aren't
4523 * any security implications of bad SACKing unless the target stack
4524 * doesn't validate the option length correctly. Someone trying to
4525 * spoof into a TCP connection won't bother blindly sending SACK
4528 if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) {
4529 if (pf_modulate_sack(m, off, pd, th, dst))
4534 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
4535 if (SEQ_GEQ(src->seqhi, end) &&
4536 /* Last octet inside other's window space */
4537 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) &&
4538 /* Retrans: not more than one window back */
4539 (ackskew >= -MAXACKWINDOW) &&
4540 /* Acking not more than one reassembled fragment backwards */
4541 (ackskew <= (MAXACKWINDOW << sws)) &&
4542 /* Acking not more than one window forward */
4543 ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo ||
4544 (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) ||
4545 (pd->flags & PFDESC_IP_REAS) == 0)) {
4546 /* Require an exact/+1 sequence match on resets when possible */
4548 if (dst->scrub || src->scrub) {
4549 if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4550 *state, src, dst, copyback))
4554 /* update max window */
4555 if (src->max_win < win)
4557 /* synchronize sequencing */
4558 if (SEQ_GT(end, src->seqlo))
4560 /* slide the window of what the other end can send */
4561 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4562 dst->seqhi = ack + MAX((win << sws), 1);
4566 if (th->th_flags & TH_SYN)
4567 if (src->state < TCPS_SYN_SENT)
4568 src->state = TCPS_SYN_SENT;
4569 if (th->th_flags & TH_FIN)
4570 if (src->state < TCPS_CLOSING)
4571 src->state = TCPS_CLOSING;
4572 if (th->th_flags & TH_ACK) {
4573 if (dst->state == TCPS_SYN_SENT) {
4574 dst->state = TCPS_ESTABLISHED;
4575 if (src->state == TCPS_ESTABLISHED &&
4576 (*state)->src_node != NULL &&
4577 pf_src_connlimit(*state)) {
4578 REASON_SET(reason, PFRES_SRCLIMIT);
4581 } else if (dst->state == TCPS_CLOSING)
4582 dst->state = TCPS_FIN_WAIT_2;
4584 if (th->th_flags & TH_RST)
4585 src->state = dst->state = TCPS_TIME_WAIT;
4587 /* update expire time */
4588 (*state)->expire = time_second;
4589 if (src->state >= TCPS_FIN_WAIT_2 &&
4590 dst->state >= TCPS_FIN_WAIT_2)
4591 (*state)->timeout = PFTM_TCP_CLOSED;
4592 else if (src->state >= TCPS_CLOSING &&
4593 dst->state >= TCPS_CLOSING)
4594 (*state)->timeout = PFTM_TCP_FIN_WAIT;
4595 else if (src->state < TCPS_ESTABLISHED ||
4596 dst->state < TCPS_ESTABLISHED)
4597 (*state)->timeout = PFTM_TCP_OPENING;
4598 else if (src->state >= TCPS_CLOSING ||
4599 dst->state >= TCPS_CLOSING)
4600 (*state)->timeout = PFTM_TCP_CLOSING;
4602 (*state)->timeout = PFTM_TCP_ESTABLISHED;
4604 /* Fall through to PASS packet */
4606 } else if ((dst->state < TCPS_SYN_SENT ||
4607 dst->state >= TCPS_FIN_WAIT_2 ||
4608 src->state >= TCPS_FIN_WAIT_2) &&
4609 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) &&
4610 /* Within a window forward of the originating packet */
4611 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) {
4612 /* Within a window backward of the originating packet */
4615 * This currently handles three situations:
4616 * 1) Stupid stacks will shotgun SYNs before their peer
4618 * 2) When PF catches an already established stream (the
4619 * firewall rebooted, the state table was flushed, routes
4621 * 3) Packets get funky immediately after the connection
4622 * closes (this should catch Solaris spurious ACK|FINs
4623 * that web servers like to spew after a close)
4625 * This must be a little more careful than the above code
4626 * since packet floods will also be caught here. We don't
4627 * update the TTL here to mitigate the damage of a packet
4628 * flood and so the same code can handle awkward establishment
4629 * and a loosened connection close.
4630 * In the establishment case, a correct peer response will
4631 * validate the connection, go through the normal state code
4632 * and keep updating the state TTL.
4635 if (pf_status.debug >= PF_DEBUG_MISC) {
4636 kprintf("pf: loose state match: ");
4637 pf_print_state(*state);
4638 pf_print_flags(th->th_flags);
4639 kprintf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4640 "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, pd->p_len,
4641 ackskew, (unsigned long long)(*state)->packets[0],
4642 (unsigned long long)(*state)->packets[1],
4643 pd->dir == PF_IN ? "in" : "out",
4644 pd->dir == (*state)->direction ? "fwd" : "rev");
4647 if (dst->scrub || src->scrub) {
4648 if (pf_normalize_tcp_stateful(m, off, pd, reason, th,
4649 *state, src, dst, copyback))
4653 /* update max window */
4654 if (src->max_win < win)
4656 /* synchronize sequencing */
4657 if (SEQ_GT(end, src->seqlo))
4659 /* slide the window of what the other end can send */
4660 if (SEQ_GEQ(ack + (win << sws), dst->seqhi))
4661 dst->seqhi = ack + MAX((win << sws), 1);
4664 * Cannot set dst->seqhi here since this could be a shotgunned
4665 * SYN and not an already established connection.
4668 if (th->th_flags & TH_FIN)
4669 if (src->state < TCPS_CLOSING)
4670 src->state = TCPS_CLOSING;
4671 if (th->th_flags & TH_RST)
4672 src->state = dst->state = TCPS_TIME_WAIT;
4674 /* Fall through to PASS packet */
4676 } else if ((*state)->pickup_mode == PF_PICKUPS_HASHONLY ||
4677 ((*state)->pickup_mode == PF_PICKUPS_ENABLED &&
4678 ((*state)->sync_flags & PFSTATE_GOT_SYN_MASK) !=
4679 PFSTATE_GOT_SYN_MASK)) {
4681 * If pickup mode is hash only, do not fail on sequence checks.
4683 * If pickup mode is enabled and we did not see the SYN in
4684 * both direction, do not fail on sequence checks because
4685 * we do not have complete information on window scale.
4687 * Adjust expiration and fall through to PASS packet.
4688 * XXX Add a FIN check to reduce timeout?
4690 (*state)->expire = time_second;
4693 * Failure processing
4695 if ((*state)->dst.state == TCPS_SYN_SENT &&
4696 (*state)->src.state == TCPS_SYN_SENT) {
4697 /* Send RST for state mismatches during handshake */
4698 if (!(th->th_flags & TH_RST))
4699 pf_send_tcp((*state)->rule.ptr, pd->af,
4700 pd->dst, pd->src, th->th_dport,
4701 th->th_sport, ntohl(th->th_ack), 0,
4703 (*state)->rule.ptr->return_ttl, 1, 0,
4704 pd->eh, kif->pfik_ifp);
4708 } else if (pf_status.debug >= PF_DEBUG_MISC) {
4709 kprintf("pf: BAD state: ");
4710 pf_print_state(*state);
4711 pf_print_flags(th->th_flags);
4712 kprintf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4713 "pkts=%llu:%llu dir=%s,%s\n",
4714 seq, orig_seq, ack, pd->p_len, ackskew,
4715 (unsigned long long)(*state)->packets[0],
4716 (unsigned long long)(*state)->packets[1],
4717 pd->dir == PF_IN ? "in" : "out",
4718 pd->dir == (*state)->direction ? "fwd" : "rev");
4719 kprintf("pf: State failure on: %c %c %c %c | %c %c\n",
4720 SEQ_GEQ(src->seqhi, end) ? ' ' : '1',
4721 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ?
4723 (ackskew >= -MAXACKWINDOW) ? ' ' : '3',
4724 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4',
4725 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5',
4726 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6');
4728 REASON_SET(reason, PFRES_BADSTATE);
4736 * Called with state locked
4739 pf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst,
4740 struct pf_state **state, struct pf_pdesc *pd, u_short *reason)
4742 struct tcphdr *th = pd->hdr.tcp;
4744 if (th->th_flags & TH_SYN)
4745 if (src->state < TCPS_SYN_SENT)
4746 src->state = TCPS_SYN_SENT;
4747 if (th->th_flags & TH_FIN)
4748 if (src->state < TCPS_CLOSING)
4749 src->state = TCPS_CLOSING;
4750 if (th->th_flags & TH_ACK) {
4751 if (dst->state == TCPS_SYN_SENT) {
4752 dst->state = TCPS_ESTABLISHED;
4753 if (src->state == TCPS_ESTABLISHED &&
4754 (*state)->src_node != NULL &&
4755 pf_src_connlimit(*state)) {
4756 REASON_SET(reason, PFRES_SRCLIMIT);
4759 } else if (dst->state == TCPS_CLOSING) {
4760 dst->state = TCPS_FIN_WAIT_2;
4761 } else if (src->state == TCPS_SYN_SENT &&
4762 dst->state < TCPS_SYN_SENT) {
4764 * Handle a special sloppy case where we only see one
4765 * half of the connection. If there is a ACK after
4766 * the initial SYN without ever seeing a packet from
4767 * the destination, set the connection to established.
4769 dst->state = src->state = TCPS_ESTABLISHED;
4770 if ((*state)->src_node != NULL &&
4771 pf_src_connlimit(*state)) {
4772 REASON_SET(reason, PFRES_SRCLIMIT);
4775 } else if (src->state == TCPS_CLOSING &&
4776 dst->state == TCPS_ESTABLISHED &&
4779 * Handle the closing of half connections where we
4780 * don't see the full bidirectional FIN/ACK+ACK
4783 dst->state = TCPS_CLOSING;
4786 if (th->th_flags & TH_RST)
4787 src->state = dst->state = TCPS_TIME_WAIT;
4789 /* update expire time */
4790 (*state)->expire = time_second;
4791 if (src->state >= TCPS_FIN_WAIT_2 &&
4792 dst->state >= TCPS_FIN_WAIT_2)
4793 (*state)->timeout = PFTM_TCP_CLOSED;
4794 else if (src->state >= TCPS_CLOSING &&
4795 dst->state >= TCPS_CLOSING)
4796 (*state)->timeout = PFTM_TCP_FIN_WAIT;
4797 else if (src->state < TCPS_ESTABLISHED ||
4798 dst->state < TCPS_ESTABLISHED)
4799 (*state)->timeout = PFTM_TCP_OPENING;
4800 else if (src->state >= TCPS_CLOSING ||
4801 dst->state >= TCPS_CLOSING)
4802 (*state)->timeout = PFTM_TCP_CLOSING;
4804 (*state)->timeout = PFTM_TCP_ESTABLISHED;
4810 * Test TCP connection state. Caller must hold the state locked.
4813 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif,
4814 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
4817 struct pf_state_key_cmp key;
4818 struct tcphdr *th = pd->hdr.tcp;
4821 struct pf_state_peer *src, *dst;
4822 struct pf_state_key *sk;
4824 bzero(&key, sizeof(key));
4826 key.proto = IPPROTO_TCP;
4827 if (direction == PF_IN) { /* wire side, straight */
4828 PF_ACPY(&key.addr[0], pd->src, key.af);
4829 PF_ACPY(&key.addr[1], pd->dst, key.af);
4830 key.port[0] = th->th_sport;
4831 key.port[1] = th->th_dport;
4832 if (pf_status.debug >= PF_DEBUG_MISC) {
4833 kprintf("test-tcp IN (%08x:%d) -> (%08x:%d)\n",
4834 ntohl(key.addr[0].addr32[0]),
4836 ntohl(key.addr[1].addr32[0]),
4837 ntohs(key.port[1]));
4839 } else { /* stack side, reverse */
4840 PF_ACPY(&key.addr[1], pd->src, key.af);
4841 PF_ACPY(&key.addr[0], pd->dst, key.af);
4842 key.port[1] = th->th_sport;
4843 key.port[0] = th->th_dport;
4844 if (pf_status.debug >= PF_DEBUG_MISC) {
4845 kprintf("test-tcp OUT (%08x:%d) <- (%08x:%d)\n",
4846 ntohl(key.addr[0].addr32[0]),
4848 ntohl(key.addr[1].addr32[0]),
4849 ntohs(key.port[1]));
4853 STATE_LOOKUP(kif, &key, direction, *state, m);
4854 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
4856 if (direction == (*state)->direction) {
4857 src = &(*state)->src;
4858 dst = &(*state)->dst;
4860 src = &(*state)->dst;
4861 dst = &(*state)->src;
4864 sk = (*state)->key[pd->didx];
4866 if ((*state)->src.state == PF_TCPS_PROXY_SRC) {
4867 if (direction != (*state)->direction) {
4868 REASON_SET(reason, PFRES_SYNPROXY);
4869 FAIL (PF_SYNPROXY_DROP);
4871 if (th->th_flags & TH_SYN) {
4872 if (ntohl(th->th_seq) != (*state)->src.seqlo) {
4873 REASON_SET(reason, PFRES_SYNPROXY);
4876 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
4877 pd->src, th->th_dport, th->th_sport,
4878 (*state)->src.seqhi, ntohl(th->th_seq) + 1,
4879 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1,
4881 REASON_SET(reason, PFRES_SYNPROXY);
4882 FAIL (PF_SYNPROXY_DROP);
4883 } else if (!(th->th_flags & TH_ACK) ||
4884 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
4885 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
4886 REASON_SET(reason, PFRES_SYNPROXY);
4888 } else if ((*state)->src_node != NULL &&
4889 pf_src_connlimit(*state)) {
4890 REASON_SET(reason, PFRES_SRCLIMIT);
4893 (*state)->src.state = PF_TCPS_PROXY_DST;
4895 if ((*state)->src.state == PF_TCPS_PROXY_DST) {
4896 if (direction == (*state)->direction) {
4897 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) ||
4898 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) ||
4899 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) {
4900 REASON_SET(reason, PFRES_SYNPROXY);
4903 (*state)->src.max_win = MAX(ntohs(th->th_win), 1);
4904 if ((*state)->dst.seqhi == 1)
4905 (*state)->dst.seqhi = htonl(karc4random());
4906 pf_send_tcp((*state)->rule.ptr, pd->af,
4907 &sk->addr[pd->sidx], &sk->addr[pd->didx],
4908 sk->port[pd->sidx], sk->port[pd->didx],
4909 (*state)->dst.seqhi, 0, TH_SYN, 0,
4910 (*state)->src.mss, 0, 0, (*state)->tag, NULL, NULL);
4911 REASON_SET(reason, PFRES_SYNPROXY);
4912 FAIL (PF_SYNPROXY_DROP);
4913 } else if (((th->th_flags & (TH_SYN|TH_ACK)) !=
4915 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) {
4916 REASON_SET(reason, PFRES_SYNPROXY);
4919 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1);
4920 (*state)->dst.seqlo = ntohl(th->th_seq);
4921 pf_send_tcp((*state)->rule.ptr, pd->af, pd->dst,
4922 pd->src, th->th_dport, th->th_sport,
4923 ntohl(th->th_ack), ntohl(th->th_seq) + 1,
4924 TH_ACK, (*state)->src.max_win, 0, 0, 0,
4925 (*state)->tag, NULL, NULL);
4926 pf_send_tcp((*state)->rule.ptr, pd->af,
4927 &sk->addr[pd->sidx], &sk->addr[pd->didx],
4928 sk->port[pd->sidx], sk->port[pd->didx],
4929 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1,
4930 TH_ACK, (*state)->dst.max_win, 0, 0, 1,
4932 (*state)->src.seqdiff = (*state)->dst.seqhi -
4933 (*state)->src.seqlo;
4934 (*state)->dst.seqdiff = (*state)->src.seqhi -
4935 (*state)->dst.seqlo;
4936 (*state)->src.seqhi = (*state)->src.seqlo +
4937 (*state)->dst.max_win;
4938 (*state)->dst.seqhi = (*state)->dst.seqlo +
4939 (*state)->src.max_win;
4940 (*state)->src.wscale = (*state)->dst.wscale = 0;
4941 (*state)->src.state = (*state)->dst.state =
4943 REASON_SET(reason, PFRES_SYNPROXY);
4944 FAIL (PF_SYNPROXY_DROP);
4949 * Check for connection (addr+port pair) reuse. We can't actually
4950 * unlink the state if we don't own it.
4952 if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) &&
4953 dst->state >= TCPS_FIN_WAIT_2 &&
4954 src->state >= TCPS_FIN_WAIT_2) {
4955 if (pf_status.debug >= PF_DEBUG_MISC) {
4956 kprintf("pf: state reuse ");
4957 pf_print_state(*state);
4958 pf_print_flags(th->th_flags);
4961 /* XXX make sure it's the same direction ?? */
4962 (*state)->src.state = (*state)->dst.state = TCPS_CLOSED;
4963 if ((*state)->cpuid == mycpu->gd_cpuid) {
4964 pf_unlink_state(*state);
4967 (*state)->timeout = PFTM_PURGE;
4972 if ((*state)->state_flags & PFSTATE_SLOPPY) {
4973 if (pf_tcp_track_sloppy(src, dst, state, pd,
4974 reason) == PF_DROP) {
4978 if (pf_tcp_track_full(src, dst, state, kif, m, off, pd,
4979 reason, ©back) == PF_DROP) {
4984 /* translate source/destination address, if necessary */
4985 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
4986 struct pf_state_key *nk = (*state)->key[pd->didx];
4988 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) ||
4989 nk->port[pd->sidx] != th->th_sport) {
4991 * The translated source address may be completely
4992 * unrelated to the saved link header, make sure
4993 * a bridge doesn't try to use it.
4995 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
4996 pf_change_ap(pd->src, &th->th_sport, pd->ip_sum,
4997 &th->th_sum, &nk->addr[pd->sidx],
4998 nk->port[pd->sidx], 0, pd->af);
5001 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) ||
5002 nk->port[pd->didx] != th->th_dport) {
5004 * If we don't redispatch the packet will go into
5005 * the protocol stack on the wrong cpu for the
5006 * post-translated address.
5008 pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum,
5009 &th->th_sum, &nk->addr[pd->didx],
5010 nk->port[pd->didx], 0, pd->af);
5015 /* Copyback sequence modulation or stateful scrub changes if needed */
5017 m->m_flags &= ~M_HASH;
5018 m_copyback(m, off, sizeof(*th), (caddr_t)th);
5021 pfsync_update_state(*state);
5025 lockmgr(&(*state)->lk, LK_RELEASE);
5030 * Test UDP connection state. Caller must hold the state locked.
5033 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif,
5034 struct mbuf *m, int off, void *h, struct pf_pdesc *pd)
5036 struct pf_state_peer *src, *dst;
5037 struct pf_state_key_cmp key;
5038 struct udphdr *uh = pd->hdr.udp;
5040 bzero(&key, sizeof(key));
5042 key.proto = IPPROTO_UDP;
5043 if (direction == PF_IN) { /* wire side, straight */
5044 PF_ACPY(&key.addr[0], pd->src, key.af);
5045 PF_ACPY(&key.addr[1], pd->dst, key.af);
5046 key.port[0] = uh->uh_sport;
5047 key.port[1] = uh->uh_dport;
5048 } else { /* stack side, reverse */
5049 PF_ACPY(&key.addr[1], pd->src, key.af);
5050 PF_ACPY(&key.addr[0], pd->dst, key.af);
5051 key.port[1] = uh->uh_sport;
5052 key.port[0] = uh->uh_dport;
5055 STATE_LOOKUP(kif, &key, direction, *state, m);
5056 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5058 if (direction == (*state)->direction) {
5059 src = &(*state)->src;
5060 dst = &(*state)->dst;
5062 src = &(*state)->dst;
5063 dst = &(*state)->src;
5067 if (src->state < PFUDPS_SINGLE)
5068 src->state = PFUDPS_SINGLE;
5069 if (dst->state == PFUDPS_SINGLE)
5070 dst->state = PFUDPS_MULTIPLE;
5072 /* update expire time */
5073 (*state)->expire = time_second;
5074 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE)
5075 (*state)->timeout = PFTM_UDP_MULTIPLE;
5077 (*state)->timeout = PFTM_UDP_SINGLE;
5079 /* translate source/destination address, if necessary */
5080 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
5081 struct pf_state_key *nk = (*state)->key[pd->didx];
5083 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) ||
5084 nk->port[pd->sidx] != uh->uh_sport) {
5086 * The translated source address may be completely
5087 * unrelated to the saved link header, make sure
5088 * a bridge doesn't try to use it.
5090 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
5091 m->m_flags &= ~M_HASH;
5092 pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum,
5093 &uh->uh_sum, &nk->addr[pd->sidx],
5094 nk->port[pd->sidx], 1, pd->af);
5097 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) ||
5098 nk->port[pd->didx] != uh->uh_dport) {
5100 * If we don't redispatch the packet will go into
5101 * the protocol stack on the wrong cpu for the
5102 * post-translated address.
5104 m->m_flags &= ~M_HASH;
5105 pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum,
5106 &uh->uh_sum, &nk->addr[pd->didx],
5107 nk->port[pd->didx], 1, pd->af);
5109 m_copyback(m, off, sizeof(*uh), (caddr_t)uh);
5112 pfsync_update_state(*state);
5113 lockmgr(&(*state)->lk, LK_RELEASE);
5118 * Test ICMP connection state. Caller must hold the state locked.
5121 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif,
5122 struct mbuf *m, int off, void *h, struct pf_pdesc *pd,
5125 struct pf_addr *saddr = pd->src, *daddr = pd->dst;
5126 u_int16_t icmpid = 0, *icmpsum;
5130 struct pf_state_key_cmp key;
5132 bzero(&key, sizeof(key));
5134 switch (pd->proto) {
5137 icmptype = pd->hdr.icmp->icmp_type;
5138 icmpid = pd->hdr.icmp->icmp_id;
5139 icmpsum = &pd->hdr.icmp->icmp_cksum;
5141 if (icmptype == ICMP_UNREACH ||
5142 icmptype == ICMP_SOURCEQUENCH ||
5143 icmptype == ICMP_REDIRECT ||
5144 icmptype == ICMP_TIMXCEED ||
5145 icmptype == ICMP_PARAMPROB)
5150 case IPPROTO_ICMPV6:
5151 icmptype = pd->hdr.icmp6->icmp6_type;
5152 icmpid = pd->hdr.icmp6->icmp6_id;
5153 icmpsum = &pd->hdr.icmp6->icmp6_cksum;
5155 if (icmptype == ICMP6_DST_UNREACH ||
5156 icmptype == ICMP6_PACKET_TOO_BIG ||
5157 icmptype == ICMP6_TIME_EXCEEDED ||
5158 icmptype == ICMP6_PARAM_PROB)
5167 * ICMP query/reply message not related to a TCP/UDP packet.
5168 * Search for an ICMP state.
5171 key.proto = pd->proto;
5172 key.port[0] = key.port[1] = icmpid;
5173 if (direction == PF_IN) { /* wire side, straight */
5174 PF_ACPY(&key.addr[0], pd->src, key.af);
5175 PF_ACPY(&key.addr[1], pd->dst, key.af);
5176 } else { /* stack side, reverse */
5177 PF_ACPY(&key.addr[1], pd->src, key.af);
5178 PF_ACPY(&key.addr[0], pd->dst, key.af);
5181 STATE_LOOKUP(kif, &key, direction, *state, m);
5182 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5184 (*state)->expire = time_second;
5185 (*state)->timeout = PFTM_ICMP_ERROR_REPLY;
5187 /* translate source/destination address, if necessary */
5188 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
5189 struct pf_state_key *nk = (*state)->key[pd->didx];
5194 if (PF_ANEQ(pd->src,
5195 &nk->addr[pd->sidx], AF_INET))
5196 pf_change_a(&saddr->v4.s_addr,
5198 nk->addr[pd->sidx].v4.s_addr, 0);
5200 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx],
5202 pf_change_a(&daddr->v4.s_addr,
5204 nk->addr[pd->didx].v4.s_addr, 0);
5207 pd->hdr.icmp->icmp_id) {
5208 pd->hdr.icmp->icmp_cksum =
5210 pd->hdr.icmp->icmp_cksum, icmpid,
5211 nk->port[pd->sidx], 0);
5212 pd->hdr.icmp->icmp_id =
5216 m->m_flags &= ~M_HASH;
5217 m_copyback(m, off, ICMP_MINLEN,
5218 (caddr_t)pd->hdr.icmp);
5223 if (PF_ANEQ(pd->src,
5224 &nk->addr[pd->sidx], AF_INET6))
5226 &pd->hdr.icmp6->icmp6_cksum,
5227 &nk->addr[pd->sidx], 0);
5229 if (PF_ANEQ(pd->dst,
5230 &nk->addr[pd->didx], AF_INET6))
5232 &pd->hdr.icmp6->icmp6_cksum,
5233 &nk->addr[pd->didx], 0);
5235 m->m_flags &= ~M_HASH;
5237 sizeof(struct icmp6_hdr),
5238 (caddr_t)pd->hdr.icmp6);
5245 * ICMP error message in response to a TCP/UDP packet.
5246 * Extract the inner TCP/UDP header and search for that state.
5249 struct pf_pdesc pd2;
5254 struct ip6_hdr h2_6;
5260 pd2.not_cpu_localized = 1;
5262 /* Payload packet is from the opposite direction. */
5263 pd2.sidx = (direction == PF_IN) ? 1 : 0;
5264 pd2.didx = (direction == PF_IN) ? 0 : 1;
5268 /* offset of h2 in mbuf chain */
5269 ipoff2 = off + ICMP_MINLEN;
5271 if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2),
5272 NULL, reason, pd2.af)) {
5273 DPFPRINTF(PF_DEBUG_MISC,
5274 ("pf: ICMP error message too short "
5279 * ICMP error messages don't refer to non-first
5282 if (h2.ip_off & htons(IP_OFFMASK)) {
5283 REASON_SET(reason, PFRES_FRAG);
5287 /* offset of protocol header that follows h2 */
5288 off2 = ipoff2 + (h2.ip_hl << 2);
5290 pd2.proto = h2.ip_p;
5291 pd2.src = (struct pf_addr *)&h2.ip_src;
5292 pd2.dst = (struct pf_addr *)&h2.ip_dst;
5293 pd2.ip_sum = &h2.ip_sum;
5298 ipoff2 = off + sizeof(struct icmp6_hdr);
5300 if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6),
5301 NULL, reason, pd2.af)) {
5302 DPFPRINTF(PF_DEBUG_MISC,
5303 ("pf: ICMP error message too short "
5307 pd2.proto = h2_6.ip6_nxt;
5308 pd2.src = (struct pf_addr *)&h2_6.ip6_src;
5309 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst;
5311 off2 = ipoff2 + sizeof(h2_6);
5313 switch (pd2.proto) {
5314 case IPPROTO_FRAGMENT:
5316 * ICMPv6 error messages for
5317 * non-first fragments
5319 REASON_SET(reason, PFRES_FRAG);
5322 case IPPROTO_HOPOPTS:
5323 case IPPROTO_ROUTING:
5324 case IPPROTO_DSTOPTS: {
5325 /* get next header and header length */
5326 struct ip6_ext opt6;
5328 if (!pf_pull_hdr(m, off2, &opt6,
5329 sizeof(opt6), NULL, reason,
5331 DPFPRINTF(PF_DEBUG_MISC,
5332 ("pf: ICMPv6 short opt\n"));
5335 if (pd2.proto == IPPROTO_AH)
5336 off2 += (opt6.ip6e_len + 2) * 4;
5338 off2 += (opt6.ip6e_len + 1) * 8;
5339 pd2.proto = opt6.ip6e_nxt;
5340 /* goto the next header */
5347 } while (!terminal);
5351 DPFPRINTF(PF_DEBUG_MISC,
5352 ("pf: ICMP AF %d unknown (ip6)\n", pd->af));
5357 switch (pd2.proto) {
5361 struct pf_state_peer *src, *dst;
5366 * Only the first 8 bytes of the TCP header can be
5367 * expected. Don't access any TCP header fields after
5368 * th_seq, an ackskew test is not possible.
5370 if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason,
5372 DPFPRINTF(PF_DEBUG_MISC,
5373 ("pf: ICMP error message too short "
5379 key.proto = IPPROTO_TCP;
5380 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5381 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5382 key.port[pd2.sidx] = th.th_sport;
5383 key.port[pd2.didx] = th.th_dport;
5385 STATE_LOOKUP(kif, &key, direction, *state, m);
5386 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5388 if (direction == (*state)->direction) {
5389 src = &(*state)->dst;
5390 dst = &(*state)->src;
5392 src = &(*state)->src;
5393 dst = &(*state)->dst;
5396 if (src->wscale && dst->wscale)
5397 dws = dst->wscale & PF_WSCALE_MASK;
5401 /* Demodulate sequence number */
5402 seq = ntohl(th.th_seq) - src->seqdiff;
5404 pf_change_a(&th.th_seq, icmpsum,
5409 if (!((*state)->state_flags & PFSTATE_SLOPPY) &&
5410 (!SEQ_GEQ(src->seqhi, seq) ||
5411 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) {
5412 if (pf_status.debug >= PF_DEBUG_MISC) {
5413 kprintf("pf: BAD ICMP %d:%d ",
5414 icmptype, pd->hdr.icmp->icmp_code);
5415 pf_print_host(pd->src, 0, pd->af);
5417 pf_print_host(pd->dst, 0, pd->af);
5418 kprintf(" state: ");
5419 pf_print_state(*state);
5420 kprintf(" seq=%u\n", seq);
5422 REASON_SET(reason, PFRES_BADSTATE);
5425 if (pf_status.debug >= PF_DEBUG_MISC) {
5426 kprintf("pf: OK ICMP %d:%d ",
5427 icmptype, pd->hdr.icmp->icmp_code);
5428 pf_print_host(pd->src, 0, pd->af);
5430 pf_print_host(pd->dst, 0, pd->af);
5431 kprintf(" state: ");
5432 pf_print_state(*state);
5433 kprintf(" seq=%u\n", seq);
5437 /* translate source/destination address, if necessary */
5438 if ((*state)->key[PF_SK_WIRE] !=
5439 (*state)->key[PF_SK_STACK]) {
5440 struct pf_state_key *nk =
5441 (*state)->key[pd->didx];
5443 if (PF_ANEQ(pd2.src,
5444 &nk->addr[pd2.sidx], pd2.af) ||
5445 nk->port[pd2.sidx] != th.th_sport)
5446 pf_change_icmp(pd2.src, &th.th_sport,
5447 daddr, &nk->addr[pd2.sidx],
5448 nk->port[pd2.sidx], NULL,
5449 pd2.ip_sum, icmpsum,
5450 pd->ip_sum, 0, pd2.af);
5452 if (PF_ANEQ(pd2.dst,
5453 &nk->addr[pd2.didx], pd2.af) ||
5454 nk->port[pd2.didx] != th.th_dport)
5455 pf_change_icmp(pd2.dst, &th.th_dport,
5456 NULL, /* XXX Inbound NAT? */
5457 &nk->addr[pd2.didx],
5458 nk->port[pd2.didx], NULL,
5459 pd2.ip_sum, icmpsum,
5460 pd->ip_sum, 0, pd2.af);
5468 m_copyback(m, off, ICMP_MINLEN,
5469 (caddr_t)pd->hdr.icmp);
5470 m_copyback(m, ipoff2, sizeof(h2),
5477 sizeof(struct icmp6_hdr),
5478 (caddr_t)pd->hdr.icmp6);
5479 m_copyback(m, ipoff2, sizeof(h2_6),
5484 m->m_flags &= ~M_HASH;
5485 m_copyback(m, off2, 8, (caddr_t)&th);
5492 if (!pf_pull_hdr(m, off2, &uh, sizeof(uh),
5493 NULL, reason, pd2.af)) {
5494 DPFPRINTF(PF_DEBUG_MISC,
5495 ("pf: ICMP error message too short "
5501 key.proto = IPPROTO_UDP;
5502 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5503 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5504 key.port[pd2.sidx] = uh.uh_sport;
5505 key.port[pd2.didx] = uh.uh_dport;
5507 STATE_LOOKUP(kif, &key, direction, *state, m);
5508 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5510 /* translate source/destination address, if necessary */
5511 if ((*state)->key[PF_SK_WIRE] !=
5512 (*state)->key[PF_SK_STACK]) {
5513 struct pf_state_key *nk =
5514 (*state)->key[pd->didx];
5516 if (PF_ANEQ(pd2.src,
5517 &nk->addr[pd2.sidx], pd2.af) ||
5518 nk->port[pd2.sidx] != uh.uh_sport)
5519 pf_change_icmp(pd2.src, &uh.uh_sport,
5520 daddr, &nk->addr[pd2.sidx],
5521 nk->port[pd2.sidx], &uh.uh_sum,
5522 pd2.ip_sum, icmpsum,
5523 pd->ip_sum, 1, pd2.af);
5525 if (PF_ANEQ(pd2.dst,
5526 &nk->addr[pd2.didx], pd2.af) ||
5527 nk->port[pd2.didx] != uh.uh_dport)
5528 pf_change_icmp(pd2.dst, &uh.uh_dport,
5529 NULL, /* XXX Inbound NAT? */
5530 &nk->addr[pd2.didx],
5531 nk->port[pd2.didx], &uh.uh_sum,
5532 pd2.ip_sum, icmpsum,
5533 pd->ip_sum, 1, pd2.af);
5538 m_copyback(m, off, ICMP_MINLEN,
5539 (caddr_t)pd->hdr.icmp);
5540 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5546 sizeof(struct icmp6_hdr),
5547 (caddr_t)pd->hdr.icmp6);
5548 m_copyback(m, ipoff2, sizeof(h2_6),
5553 m->m_flags &= ~M_HASH;
5554 m_copyback(m, off2, sizeof(uh), (caddr_t)&uh);
5559 case IPPROTO_ICMP: {
5562 if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN,
5563 NULL, reason, pd2.af)) {
5564 DPFPRINTF(PF_DEBUG_MISC,
5565 ("pf: ICMP error message too short i"
5571 key.proto = IPPROTO_ICMP;
5572 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5573 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5574 key.port[0] = key.port[1] = iih.icmp_id;
5576 STATE_LOOKUP(kif, &key, direction, *state, m);
5577 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5579 /* translate source/destination address, if necessary */
5580 if ((*state)->key[PF_SK_WIRE] !=
5581 (*state)->key[PF_SK_STACK]) {
5582 struct pf_state_key *nk =
5583 (*state)->key[pd->didx];
5585 if (PF_ANEQ(pd2.src,
5586 &nk->addr[pd2.sidx], pd2.af) ||
5587 nk->port[pd2.sidx] != iih.icmp_id)
5588 pf_change_icmp(pd2.src, &iih.icmp_id,
5589 daddr, &nk->addr[pd2.sidx],
5590 nk->port[pd2.sidx], NULL,
5591 pd2.ip_sum, icmpsum,
5592 pd->ip_sum, 0, AF_INET);
5594 if (PF_ANEQ(pd2.dst,
5595 &nk->addr[pd2.didx], pd2.af) ||
5596 nk->port[pd2.didx] != iih.icmp_id)
5597 pf_change_icmp(pd2.dst, &iih.icmp_id,
5598 NULL, /* XXX Inbound NAT? */
5599 &nk->addr[pd2.didx],
5600 nk->port[pd2.didx], NULL,
5601 pd2.ip_sum, icmpsum,
5602 pd->ip_sum, 0, AF_INET);
5604 m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp);
5605 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5606 m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih);
5607 m->m_flags &= ~M_HASH;
5613 case IPPROTO_ICMPV6: {
5614 struct icmp6_hdr iih;
5616 if (!pf_pull_hdr(m, off2, &iih,
5617 sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) {
5618 DPFPRINTF(PF_DEBUG_MISC,
5619 ("pf: ICMP error message too short "
5625 key.proto = IPPROTO_ICMPV6;
5626 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5627 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5628 key.port[0] = key.port[1] = iih.icmp6_id;
5630 STATE_LOOKUP(kif, &key, direction, *state, m);
5631 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5633 /* translate source/destination address, if necessary */
5634 if ((*state)->key[PF_SK_WIRE] !=
5635 (*state)->key[PF_SK_STACK]) {
5636 struct pf_state_key *nk =
5637 (*state)->key[pd->didx];
5639 if (PF_ANEQ(pd2.src,
5640 &nk->addr[pd2.sidx], pd2.af) ||
5641 nk->port[pd2.sidx] != iih.icmp6_id)
5642 pf_change_icmp(pd2.src, &iih.icmp6_id,
5643 daddr, &nk->addr[pd2.sidx],
5644 nk->port[pd2.sidx], NULL,
5645 pd2.ip_sum, icmpsum,
5646 pd->ip_sum, 0, AF_INET6);
5648 if (PF_ANEQ(pd2.dst,
5649 &nk->addr[pd2.didx], pd2.af) ||
5650 nk->port[pd2.didx] != iih.icmp6_id)
5651 pf_change_icmp(pd2.dst, &iih.icmp6_id,
5652 NULL, /* XXX Inbound NAT? */
5653 &nk->addr[pd2.didx],
5654 nk->port[pd2.didx], NULL,
5655 pd2.ip_sum, icmpsum,
5656 pd->ip_sum, 0, AF_INET6);
5658 m_copyback(m, off, sizeof(struct icmp6_hdr),
5659 (caddr_t)pd->hdr.icmp6);
5660 m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6);
5661 m_copyback(m, off2, sizeof(struct icmp6_hdr),
5663 m->m_flags &= ~M_HASH;
5670 key.proto = pd2.proto;
5671 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af);
5672 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af);
5673 key.port[0] = key.port[1] = 0;
5675 STATE_LOOKUP(kif, &key, direction, *state, m);
5676 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5678 /* translate source/destination address, if necessary */
5679 if ((*state)->key[PF_SK_WIRE] !=
5680 (*state)->key[PF_SK_STACK]) {
5681 struct pf_state_key *nk =
5682 (*state)->key[pd->didx];
5684 if (PF_ANEQ(pd2.src,
5685 &nk->addr[pd2.sidx], pd2.af))
5686 pf_change_icmp(pd2.src, NULL, daddr,
5687 &nk->addr[pd2.sidx], 0, NULL,
5688 pd2.ip_sum, icmpsum,
5689 pd->ip_sum, 0, pd2.af);
5691 if (PF_ANEQ(pd2.dst,
5692 &nk->addr[pd2.didx], pd2.af))
5693 pf_change_icmp(pd2.src, NULL,
5694 NULL, /* XXX Inbound NAT? */
5695 &nk->addr[pd2.didx], 0, NULL,
5696 pd2.ip_sum, icmpsum,
5697 pd->ip_sum, 0, pd2.af);
5702 m_copyback(m, off, ICMP_MINLEN,
5703 (caddr_t)pd->hdr.icmp);
5704 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2);
5705 m->m_flags &= ~M_HASH;
5711 sizeof(struct icmp6_hdr),
5712 (caddr_t)pd->hdr.icmp6);
5713 m_copyback(m, ipoff2, sizeof(h2_6),
5715 m->m_flags &= ~M_HASH;
5725 pfsync_update_state(*state);
5729 lockmgr(&(*state)->lk, LK_RELEASE);
5734 * Test other connection state. Caller must hold the state locked.
5737 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif,
5738 struct mbuf *m, struct pf_pdesc *pd)
5740 struct pf_state_peer *src, *dst;
5741 struct pf_state_key_cmp key;
5743 bzero(&key, sizeof(key));
5745 key.proto = pd->proto;
5746 if (direction == PF_IN) {
5747 PF_ACPY(&key.addr[0], pd->src, key.af);
5748 PF_ACPY(&key.addr[1], pd->dst, key.af);
5749 key.port[0] = key.port[1] = 0;
5751 PF_ACPY(&key.addr[1], pd->src, key.af);
5752 PF_ACPY(&key.addr[0], pd->dst, key.af);
5753 key.port[1] = key.port[0] = 0;
5756 STATE_LOOKUP(kif, &key, direction, *state, m);
5757 lockmgr(&(*state)->lk, LK_EXCLUSIVE);
5759 if (direction == (*state)->direction) {
5760 src = &(*state)->src;
5761 dst = &(*state)->dst;
5763 src = &(*state)->dst;
5764 dst = &(*state)->src;
5768 if (src->state < PFOTHERS_SINGLE)
5769 src->state = PFOTHERS_SINGLE;
5770 if (dst->state == PFOTHERS_SINGLE)
5771 dst->state = PFOTHERS_MULTIPLE;
5773 /* update expire time */
5774 (*state)->expire = time_second;
5775 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE)
5776 (*state)->timeout = PFTM_OTHER_MULTIPLE;
5778 (*state)->timeout = PFTM_OTHER_SINGLE;
5780 /* translate source/destination address, if necessary */
5781 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) {
5782 struct pf_state_key *nk = (*state)->key[pd->didx];
5791 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET))
5792 pf_change_a(&pd->src->v4.s_addr,
5794 nk->addr[pd->sidx].v4.s_addr,
5798 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET))
5799 pf_change_a(&pd->dst->v4.s_addr,
5801 nk->addr[pd->didx].v4.s_addr,
5808 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET))
5809 PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af);
5811 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET))
5812 PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af);
5817 pfsync_update_state(*state);
5818 lockmgr(&(*state)->lk, LK_RELEASE);
5823 * ipoff and off are measured from the start of the mbuf chain.
5824 * h must be at "ipoff" on the mbuf chain.
5827 pf_pull_hdr(struct mbuf *m, int off, void *p, int len,
5828 u_short *actionp, u_short *reasonp, sa_family_t af)
5833 struct ip *h = mtod(m, struct ip *);
5834 u_int16_t fragoff = (h->ip_off & IP_OFFMASK) << 3;
5838 ACTION_SET(actionp, PF_PASS);
5840 ACTION_SET(actionp, PF_DROP);
5841 REASON_SET(reasonp, PFRES_FRAG);
5845 if (m->m_pkthdr.len < off + len ||
5846 h->ip_len < off + len) {
5847 ACTION_SET(actionp, PF_DROP);
5848 REASON_SET(reasonp, PFRES_SHORT);
5856 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
5858 if (m->m_pkthdr.len < off + len ||
5859 (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) <
5860 (unsigned)(off + len)) {
5861 ACTION_SET(actionp, PF_DROP);
5862 REASON_SET(reasonp, PFRES_SHORT);
5869 m_copydata(m, off, len, p);
5874 pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif)
5876 struct sockaddr_in *dst;
5880 struct sockaddr_in6 *dst6;
5881 struct route_in6 ro;
5885 struct radix_node *rn;
5890 bzero(&ro, sizeof(ro));
5893 dst = satosin(&ro.ro_dst);
5894 dst->sin_family = AF_INET;
5895 dst->sin_len = sizeof(*dst);
5896 dst->sin_addr = addr->v4;
5900 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
5901 dst6->sin6_family = AF_INET6;
5902 dst6->sin6_len = sizeof(*dst6);
5903 dst6->sin6_addr = addr->v6;
5910 /* Skip checks for ipsec interfaces */
5911 if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC)
5914 rtalloc_ign((struct route *)&ro, 0);
5916 if (ro.ro_rt != NULL) {
5917 /* No interface given, this is a no-route check */
5921 if (kif->pfik_ifp == NULL) {
5926 /* Perform uRPF check if passed input interface */
5928 rn = (struct radix_node *)ro.ro_rt;
5930 rt = (struct rtentry *)rn;
5933 if (kif->pfik_ifp == ifp)
5936 } while (check_mpath == 1 && rn != NULL && ret == 0);
5940 if (ro.ro_rt != NULL)
5946 pf_rtlabel_match(struct pf_addr *addr, sa_family_t af, struct pf_addr_wrap *aw)
5948 struct sockaddr_in *dst;
5950 struct sockaddr_in6 *dst6;
5951 struct route_in6 ro;
5957 ASSERT_LWKT_TOKEN_HELD(&pf_token);
5959 bzero(&ro, sizeof(ro));
5962 dst = satosin(&ro.ro_dst);
5963 dst->sin_family = AF_INET;
5964 dst->sin_len = sizeof(*dst);
5965 dst->sin_addr = addr->v4;
5969 dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
5970 dst6->sin6_family = AF_INET6;
5971 dst6->sin6_len = sizeof(*dst6);
5972 dst6->sin6_addr = addr->v6;
5979 rtalloc_ign((struct route *)&ro, (RTF_CLONING | RTF_PRCLONING));
5981 if (ro.ro_rt != NULL) {
5990 pf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
5991 struct pf_state *s, struct pf_pdesc *pd)
5993 struct mbuf *m0, *m1;
5994 struct route iproute;
5995 struct route *ro = NULL;
5996 struct sockaddr_in *dst;
5998 struct ifnet *ifp = NULL;
5999 struct pf_addr naddr;
6000 struct pf_src_node *sn = NULL;
6007 ASSERT_LWKT_TOKEN_HELD(&pf_token);
6009 if (m == NULL || *m == NULL || r == NULL ||
6010 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
6011 panic("pf_route: invalid parameters");
6013 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
6014 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
6015 (*m)->m_pkthdr.pf.routed = 1;
6017 if ((*m)->m_pkthdr.pf.routed++ > 3) {
6024 if (r->rt == PF_DUPTO) {
6025 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL) {
6029 if ((r->rt == PF_REPLYTO) == (r->direction == dir)) {
6035 if (m0->m_len < sizeof(struct ip)) {
6036 DPFPRINTF(PF_DEBUG_URGENT,
6037 ("pf_route: m0->m_len < sizeof(struct ip)\n"));
6041 ip = mtod(m0, struct ip *);
6044 bzero((caddr_t)ro, sizeof(*ro));
6045 dst = satosin(&ro->ro_dst);
6046 dst->sin_family = AF_INET;
6047 dst->sin_len = sizeof(*dst);
6048 dst->sin_addr = ip->ip_dst;
6050 if (r->rt == PF_FASTROUTE) {
6052 if (ro->ro_rt == 0) {
6053 ipstat.ips_noroute++;
6057 ifp = ro->ro_rt->rt_ifp;
6058 ro->ro_rt->rt_use++;
6060 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
6061 dst = satosin(ro->ro_rt->rt_gateway);
6063 if (TAILQ_EMPTY(&r->rpool.list)) {
6064 DPFPRINTF(PF_DEBUG_URGENT,
6065 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
6069 pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src,
6071 if (!PF_AZERO(&naddr, AF_INET))
6072 dst->sin_addr.s_addr = naddr.v4.s_addr;
6073 ifp = r->rpool.cur->kif ?
6074 r->rpool.cur->kif->pfik_ifp : NULL;
6076 if (!PF_AZERO(&s->rt_addr, AF_INET))
6077 dst->sin_addr.s_addr =
6078 s->rt_addr.v4.s_addr;
6079 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
6086 if (pf_test(PF_OUT, ifp, &m0, NULL, NULL) != PF_PASS) {
6088 } else if (m0 == NULL) {
6091 if (m0->m_len < sizeof(struct ip)) {
6092 DPFPRINTF(PF_DEBUG_URGENT,
6093 ("pf_route: m0->m_len < sizeof(struct ip)\n"));
6096 ip = mtod(m0, struct ip *);
6099 /* Copied from FreeBSD 5.1-CURRENT ip_output. */
6100 m0->m_pkthdr.csum_flags |= CSUM_IP;
6101 sw_csum = m0->m_pkthdr.csum_flags & ~ifp->if_hwassist;
6102 if (sw_csum & CSUM_DELAY_DATA) {
6103 in_delayed_cksum(m0);
6104 sw_csum &= ~CSUM_DELAY_DATA;
6106 m0->m_pkthdr.csum_flags &= ifp->if_hwassist;
6107 m0->m_pkthdr.csum_iphlen = (ip->ip_hl << 2);
6110 * WARNING! We cannot fragment if the packet was modified from an
6111 * original which expected to be using TSO. In this
6112 * situation we pray that the target interface is
6113 * compatible with the originating interface.
6115 if (ip->ip_len <= ifp->if_mtu ||
6116 (m0->m_pkthdr.csum_flags & CSUM_TSO) ||
6117 ((ifp->if_hwassist & CSUM_FRAGMENT) &&
6118 (ip->ip_off & IP_DF) == 0)) {
6119 ip->ip_len = htons(ip->ip_len);
6120 ip->ip_off = htons(ip->ip_off);
6122 if (sw_csum & CSUM_DELAY_IP) {
6124 if (ip->ip_v == IPVERSION &&
6125 (ip->ip_hl << 2) == sizeof(*ip)) {
6126 ip->ip_sum = in_cksum_hdr(ip);
6128 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2);
6131 lwkt_reltoken(&pf_token);
6132 error = ifp->if_output(ifp, m0, sintosa(dst), ro->ro_rt);
6133 lwkt_gettoken(&pf_token);
6138 * Too large for interface; fragment if possible.
6139 * Must be able to put at least 8 bytes per fragment.
6141 if (ip->ip_off & IP_DF) {
6142 ipstat.ips_cantfrag++;
6143 if (r->rt != PF_DUPTO) {
6144 icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0,
6152 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist, sw_csum);
6157 for (m0 = m1; m0; m0 = m1) {
6161 lwkt_reltoken(&pf_token);
6162 error = (*ifp->if_output)(ifp, m0, sintosa(dst),
6164 lwkt_gettoken(&pf_token);
6170 ipstat.ips_fragmented++;
6173 if (r->rt != PF_DUPTO)
6175 if (ro == &iproute && ro->ro_rt)
6187 pf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp,
6188 struct pf_state *s, struct pf_pdesc *pd)
6191 struct route_in6 ip6route;
6192 struct route_in6 *ro;
6193 struct sockaddr_in6 *dst;
6194 struct ip6_hdr *ip6;
6195 struct ifnet *ifp = NULL;
6196 struct pf_addr naddr;
6197 struct pf_src_node *sn = NULL;
6199 if (m == NULL || *m == NULL || r == NULL ||
6200 (dir != PF_IN && dir != PF_OUT) || oifp == NULL)
6201 panic("pf_route6: invalid parameters");
6203 if (((*m)->m_pkthdr.fw_flags & PF_MBUF_ROUTED) == 0) {
6204 (*m)->m_pkthdr.fw_flags |= PF_MBUF_ROUTED;
6205 (*m)->m_pkthdr.pf.routed = 1;
6207 if ((*m)->m_pkthdr.pf.routed++ > 3) {
6214 if (r->rt == PF_DUPTO) {
6215 if ((m0 = m_dup(*m, MB_DONTWAIT)) == NULL)
6218 if ((r->rt == PF_REPLYTO) == (r->direction == dir))
6223 if (m0->m_len < sizeof(struct ip6_hdr)) {
6224 DPFPRINTF(PF_DEBUG_URGENT,
6225 ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
6228 ip6 = mtod(m0, struct ip6_hdr *);
6231 bzero((caddr_t)ro, sizeof(*ro));
6232 dst = (struct sockaddr_in6 *)&ro->ro_dst;
6233 dst->sin6_family = AF_INET6;
6234 dst->sin6_len = sizeof(*dst);
6235 dst->sin6_addr = ip6->ip6_dst;
6238 * DragonFly doesn't zero the auxillary pkghdr fields, only fw_flags,
6239 * so make sure pf.flags is clear.
6241 * Cheat. XXX why only in the v6 case???
6243 if (r->rt == PF_FASTROUTE) {
6244 m0->m_pkthdr.fw_flags |= PF_MBUF_TAGGED;
6245 m0->m_pkthdr.pf.flags = 0;
6246 /* XXX Re-Check when Upgrading to > 4.4 */
6247 m0->m_pkthdr.pf.statekey = NULL;
6248 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
6252 if (TAILQ_EMPTY(&r->rpool.list)) {
6253 DPFPRINTF(PF_DEBUG_URGENT,
6254 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
6258 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src,
6260 if (!PF_AZERO(&naddr, AF_INET6))
6261 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
6263 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL;
6265 if (!PF_AZERO(&s->rt_addr, AF_INET6))
6266 PF_ACPY((struct pf_addr *)&dst->sin6_addr,
6267 &s->rt_addr, AF_INET6);
6268 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL;
6274 if (pf_test6(PF_OUT, ifp, &m0, NULL, NULL) != PF_PASS) {
6276 } else if (m0 == NULL) {
6279 if (m0->m_len < sizeof(struct ip6_hdr)) {
6280 DPFPRINTF(PF_DEBUG_URGENT,
6281 ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
6284 ip6 = mtod(m0, struct ip6_hdr *);
6288 * If the packet is too large for the outgoing interface,
6289 * send back an icmp6 error.
6291 if (IN6_IS_ADDR_LINKLOCAL(&dst->sin6_addr))
6292 dst->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
6293 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
6294 nd6_output(ifp, ifp, m0, dst, NULL);
6296 in6_ifstat_inc(ifp, ifs6_in_toobig);
6297 if (r->rt != PF_DUPTO)
6298 icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
6304 if (r->rt != PF_DUPTO)
6316 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
6317 * off is the offset where the protocol header starts
6318 * len is the total length of protocol header plus payload
6319 * returns 0 when the checksum is valid, otherwise returns 1.
6323 * FreeBSD supports cksum offload for the following drivers.
6324 * em(4), gx(4), lge(4), nge(4), ti(4), xl(4)
6325 * If we can make full use of it we would outperform ipfw/ipfilter in
6326 * very heavy traffic.
6327 * I have not tested 'cause I don't have NICs that supports cksum offload.
6328 * (There might be problems. Typical phenomena would be
6329 * 1. No route message for UDP packet.
6330 * 2. No connection acceptance from external hosts regardless of rule set.)
6333 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p,
6340 if (off < sizeof(struct ip) || len < sizeof(struct udphdr))
6342 if (m->m_pkthdr.len < off + len)
6348 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
6349 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
6350 sum = m->m_pkthdr.csum_data;
6352 ip = mtod(m, struct ip *);
6353 sum = in_pseudo(ip->ip_src.s_addr,
6354 ip->ip_dst.s_addr, htonl((u_short)len +
6355 m->m_pkthdr.csum_data + p));
6363 case IPPROTO_ICMPV6:
6373 if (p == IPPROTO_ICMP) {
6378 sum = in_cksum(m, len);
6382 if (m->m_len < sizeof(struct ip))
6384 sum = in_cksum_range(m, p, off, len);
6386 m->m_pkthdr.csum_flags |=
6389 m->m_pkthdr.csum_data = 0xffff;
6395 if (m->m_len < sizeof(struct ip6_hdr))
6397 sum = in6_cksum(m, p, off, len);
6400 * IPv6 H/W cksum off-load not supported yet!
6403 * m->m_pkthdr.csum_flags |=
6404 * (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
6405 * m->m_pkthdr.csum_data = 0xffff;
6417 tcpstat.tcps_rcvbadsum++;
6420 udp_stat.udps_badsum++;
6423 icmpstat.icps_checksum++;
6426 case IPPROTO_ICMPV6:
6427 icmp6stat.icp6s_checksum++;
6437 pf_find_divert(struct mbuf *m)
6441 if ((mtag = m_tag_find(m, PACKET_TAG_PF_DIVERT, NULL)) == NULL)
6444 return ((struct pf_divert *)(mtag + 1));
6448 pf_get_divert(struct mbuf *m)
6452 if ((mtag = m_tag_find(m, PACKET_TAG_PF_DIVERT, NULL)) == NULL) {
6453 mtag = m_tag_get(PACKET_TAG_PF_DIVERT, sizeof(struct pf_divert),
6457 bzero(mtag + 1, sizeof(struct pf_divert));
6458 m_tag_prepend(m, mtag);
6461 return ((struct pf_divert *)(mtag + 1));
6467 * WARNING: pf_token held shared on entry, THIS IS CPU LOCALIZED CODE
6470 pf_test(int dir, struct ifnet *ifp, struct mbuf **m0,
6471 struct ether_header *eh, struct inpcb *inp)
6473 struct pfi_kif *kif;
6474 u_short action, reason = 0, log = 0;
6475 struct mbuf *m = *m0;
6476 struct ip *h = NULL;
6477 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
6478 struct pf_state *s = NULL;
6479 struct pf_ruleset *ruleset = NULL;
6486 if (!pf_status.running)
6489 memset(&pd, 0, sizeof(pd));
6491 if (ifp->if_type == IFT_CARP && ifp->if_carpdev)
6492 kif = (struct pfi_kif *)ifp->if_carpdev->if_pf_kif;
6495 kif = (struct pfi_kif *)ifp->if_pf_kif;
6498 DPFPRINTF(PF_DEBUG_URGENT,
6499 ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname));
6502 if (kif->pfik_flags & PFI_IFLAG_SKIP)
6506 if ((m->m_flags & M_PKTHDR) == 0)
6507 panic("non-M_PKTHDR is passed to pf_test");
6508 #endif /* DIAGNOSTIC */
6510 if (m->m_pkthdr.len < (int)sizeof(*h)) {
6512 REASON_SET(&reason, PFRES_SHORT);
6518 * DragonFly doesn't zero the auxillary pkghdr fields, only fw_flags,
6519 * so make sure pf.flags is clear.
6521 if (m->m_pkthdr.fw_flags & PF_MBUF_TAGGED)
6523 m->m_pkthdr.pf.flags = 0;
6524 /* Re-Check when updating to > 4.4 */
6525 m->m_pkthdr.pf.statekey = NULL;
6527 /* We do IP header normalization and packet reassembly here */
6528 if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) {
6532 m = *m0; /* pf_normalize messes with m0 */
6533 h = mtod(m, struct ip *);
6535 off = h->ip_hl << 2;
6536 if (off < (int)sizeof(*h)) {
6538 REASON_SET(&reason, PFRES_SHORT);
6543 pd.src = (struct pf_addr *)&h->ip_src;
6544 pd.dst = (struct pf_addr *)&h->ip_dst;
6545 pd.sport = pd.dport = NULL;
6546 pd.ip_sum = &h->ip_sum;
6547 pd.proto_sum = NULL;
6550 pd.sidx = (dir == PF_IN) ? 0 : 1;
6551 pd.didx = (dir == PF_IN) ? 1 : 0;
6554 pd.tot_len = h->ip_len;
6557 /* handle fragments that didn't get reassembled by normalization */
6558 if (h->ip_off & (IP_MF | IP_OFFMASK)) {
6559 action = pf_test_fragment(&r, dir, kif, m, h,
6570 if (!pf_pull_hdr(m, off, &th, sizeof(th),
6571 &action, &reason, AF_INET)) {
6572 log = action != PF_PASS;
6575 pd.p_len = pd.tot_len - off - (th.th_off << 2);
6577 if ((th.th_flags & TH_ACK) && pd.p_len == 0)
6580 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
6581 if (action == PF_DROP)
6583 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
6585 if (action == PF_PASS) {
6589 } else if (s == NULL) {
6590 action = pf_test_rule(&r, &s, dir, kif,
6592 &ruleset, NULL, inp);
6601 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
6602 &action, &reason, AF_INET)) {
6603 log = action != PF_PASS;
6606 if (uh.uh_dport == 0 ||
6607 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
6608 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
6610 REASON_SET(&reason, PFRES_SHORT);
6613 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
6614 if (action == PF_PASS) {
6618 } else if (s == NULL) {
6619 action = pf_test_rule(&r, &s, dir, kif,
6621 &ruleset, NULL, inp);
6626 case IPPROTO_ICMP: {
6630 if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN,
6631 &action, &reason, AF_INET)) {
6632 log = action != PF_PASS;
6635 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd,
6637 if (action == PF_PASS) {
6641 } else if (s == NULL) {
6642 action = pf_test_rule(&r, &s, dir, kif,
6644 &ruleset, NULL, inp);
6650 action = pf_test_state_other(&s, dir, kif, m, &pd);
6651 if (action == PF_PASS) {
6655 } else if (s == NULL) {
6656 action = pf_test_rule(&r, &s, dir, kif, m, off, h,
6657 &pd, &a, &ruleset, NULL, inp);
6663 if (action == PF_PASS && h->ip_hl > 5 &&
6664 !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
6666 REASON_SET(&reason, PFRES_IPOPTIONS);
6668 DPFPRINTF(PF_DEBUG_MISC,
6669 ("pf: dropping packet with ip options\n"));
6672 if ((s && s->tag) || r->rtableid)
6673 pf_tag_packet(m, s ? s->tag : 0, r->rtableid);
6676 if (dir == PF_IN && s && s->key[PF_SK_STACK])
6677 m->m_pkthdr.pf.statekey = s->key[PF_SK_STACK];
6682 * Generate a hash code and qid request for ALTQ. A qid of 0
6683 * is allowed and will cause altq to select the default queue.
6685 if (action == PF_PASS) {
6686 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
6687 if (pqid || (pd.tos & IPTOS_LOWDELAY))
6688 m->m_pkthdr.pf.qid = r->pqid;
6690 m->m_pkthdr.pf.qid = r->qid;
6691 m->m_pkthdr.pf.ecn_af = AF_INET;
6692 m->m_pkthdr.pf.hdr = h;
6693 /* add connection hash for fairq */
6696 m->m_pkthdr.pf.state_hash = s->hash;
6697 m->m_pkthdr.pf.flags |= PF_TAG_STATE_HASHED;
6703 * connections redirected to loopback should not match sockets
6704 * bound specifically to loopback due to security implications,
6705 * see tcp_input() and in_pcblookup_listen().
6707 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
6708 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
6709 (s->nat_rule.ptr->action == PF_RDR ||
6710 s->nat_rule.ptr->action == PF_BINAT) &&
6711 (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
6712 m->m_pkthdr.pf.flags |= PF_TAG_TRANSLATE_LOCALHOST;
6714 if (dir == PF_IN && action == PF_PASS && r->divert.port) {
6715 struct pf_divert *divert;
6717 if ((divert = pf_get_divert(m))) {
6718 m->m_pkthdr.pf.flags |= PF_TAG_DIVERTED;
6719 divert->port = r->divert.port;
6720 divert->addr.ipv4 = r->divert.addr.v4;
6727 if (s != NULL && s->nat_rule.ptr != NULL &&
6728 s->nat_rule.ptr->log & PF_LOG_ALL)
6729 lr = s->nat_rule.ptr;
6732 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, lr, a, ruleset,
6736 kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
6737 kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++;
6739 if (action == PF_PASS || r->action == PF_DROP) {
6740 dirndx = (dir == PF_OUT);
6741 r->packets[dirndx]++;
6742 r->bytes[dirndx] += pd.tot_len;
6744 a->packets[dirndx]++;
6745 a->bytes[dirndx] += pd.tot_len;
6748 if (s->nat_rule.ptr != NULL) {
6749 s->nat_rule.ptr->packets[dirndx]++;
6750 s->nat_rule.ptr->bytes[dirndx] += pd.tot_len;
6752 if (s->src_node != NULL) {
6753 s->src_node->packets[dirndx]++;
6754 s->src_node->bytes[dirndx] += pd.tot_len;
6756 if (s->nat_src_node != NULL) {
6757 s->nat_src_node->packets[dirndx]++;
6758 s->nat_src_node->bytes[dirndx] += pd.tot_len;
6760 dirndx = (dir == s->direction) ? 0 : 1;
6761 s->packets[dirndx]++;
6762 s->bytes[dirndx] += pd.tot_len;
6765 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
6766 if (nr != NULL && r == &pf_default_rule)
6768 if (tr->src.addr.type == PF_ADDR_TABLE)
6769 pfr_update_stats(tr->src.addr.p.tbl,
6770 (s == NULL) ? pd.src :
6771 &s->key[(s->direction == PF_IN)]->
6772 addr[(s->direction == PF_OUT)],
6773 pd.af, pd.tot_len, dir == PF_OUT,
6774 r->action == PF_PASS, tr->src.neg);
6775 if (tr->dst.addr.type == PF_ADDR_TABLE)
6776 pfr_update_stats(tr->dst.addr.p.tbl,
6777 (s == NULL) ? pd.dst :
6778 &s->key[(s->direction == PF_IN)]->
6779 addr[(s->direction == PF_IN)],
6780 pd.af, pd.tot_len, dir == PF_OUT,
6781 r->action == PF_PASS, tr->dst.neg);
6785 if (action == PF_SYNPROXY_DROP) {
6790 /* pf_route can free the mbuf causing *m0 to become NULL */
6791 pf_route(m0, r, dir, kif->pfik_ifp, s, &pd);
6801 * WARNING: pf_token held shared on entry, THIS IS CPU LOCALIZED CODE
6804 pf_test6(int dir, struct ifnet *ifp, struct mbuf **m0,
6805 struct ether_header *eh, struct inpcb *inp)
6807 struct pfi_kif *kif;
6808 u_short action, reason = 0, log = 0;
6809 struct mbuf *m = *m0, *n = NULL;
6810 struct ip6_hdr *h = NULL;
6811 struct pf_rule *a = NULL, *r = &pf_default_rule, *tr, *nr;
6812 struct pf_state *s = NULL;
6813 struct pf_ruleset *ruleset = NULL;
6815 int off, terminal = 0, dirndx, rh_cnt = 0;
6817 if (!pf_status.running)
6820 memset(&pd, 0, sizeof(pd));
6822 if (ifp->if_type == IFT_CARP && ifp->if_carpdev)
6823 kif = (struct pfi_kif *)ifp->if_carpdev->if_pf_kif;
6826 kif = (struct pfi_kif *)ifp->if_pf_kif;
6829 DPFPRINTF(PF_DEBUG_URGENT,
6830 ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname));
6833 if (kif->pfik_flags & PFI_IFLAG_SKIP)
6837 if ((m->m_flags & M_PKTHDR) == 0)
6838 panic("non-M_PKTHDR is passed to pf_test6");
6839 #endif /* DIAGNOSTIC */
6841 if (m->m_pkthdr.len < (int)sizeof(*h)) {
6843 REASON_SET(&reason, PFRES_SHORT);
6849 * DragonFly doesn't zero the auxillary pkghdr fields, only fw_flags,
6850 * so make sure pf.flags is clear.
6852 if (m->m_pkthdr.fw_flags & PF_MBUF_TAGGED)
6854 m->m_pkthdr.pf.flags = 0;
6855 /* Re-Check when updating to > 4.4 */
6856 m->m_pkthdr.pf.statekey = NULL;
6858 /* We do IP header normalization and packet reassembly here */
6859 if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) {
6863 m = *m0; /* pf_normalize messes with m0 */
6864 h = mtod(m, struct ip6_hdr *);
6868 * we do not support jumbogram yet. if we keep going, zero ip6_plen
6869 * will do something bad, so drop the packet for now.
6871 if (htons(h->ip6_plen) == 0) {
6873 REASON_SET(&reason, PFRES_NORM); /*XXX*/
6878 pd.src = (struct pf_addr *)&h->ip6_src;
6879 pd.dst = (struct pf_addr *)&h->ip6_dst;
6880 pd.sport = pd.dport = NULL;
6882 pd.proto_sum = NULL;
6884 pd.sidx = (dir == PF_IN) ? 0 : 1;
6885 pd.didx = (dir == PF_IN) ? 1 : 0;
6888 pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr);
6891 off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
6892 pd.proto = h->ip6_nxt;
6895 case IPPROTO_FRAGMENT:
6896 action = pf_test_fragment(&r, dir, kif, m, h,
6898 if (action == PF_DROP)
6899 REASON_SET(&reason, PFRES_FRAG);
6901 case IPPROTO_ROUTING: {
6902 struct ip6_rthdr rthdr;
6905 DPFPRINTF(PF_DEBUG_MISC,
6906 ("pf: IPv6 more than one rthdr\n"));
6908 REASON_SET(&reason, PFRES_IPOPTIONS);
6912 if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL,
6914 DPFPRINTF(PF_DEBUG_MISC,
6915 ("pf: IPv6 short rthdr\n"));
6917 REASON_SET(&reason, PFRES_SHORT);
6921 if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
6922 DPFPRINTF(PF_DEBUG_MISC,
6923 ("pf: IPv6 rthdr0\n"));
6925 REASON_SET(&reason, PFRES_IPOPTIONS);
6932 case IPPROTO_HOPOPTS:
6933 case IPPROTO_DSTOPTS: {
6934 /* get next header and header length */
6935 struct ip6_ext opt6;
6937 if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6),
6938 NULL, &reason, pd.af)) {
6939 DPFPRINTF(PF_DEBUG_MISC,
6940 ("pf: IPv6 short opt\n"));
6945 if (pd.proto == IPPROTO_AH)
6946 off += (opt6.ip6e_len + 2) * 4;
6948 off += (opt6.ip6e_len + 1) * 8;
6949 pd.proto = opt6.ip6e_nxt;
6950 /* goto the next header */
6957 } while (!terminal);
6959 /* if there's no routing header, use unmodified mbuf for checksumming */
6969 if (!pf_pull_hdr(m, off, &th, sizeof(th),
6970 &action, &reason, AF_INET6)) {
6971 log = action != PF_PASS;
6974 pd.p_len = pd.tot_len - off - (th.th_off << 2);
6975 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd);
6976 if (action == PF_DROP)
6978 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd,
6980 if (action == PF_PASS) {
6984 } else if (s == NULL) {
6985 action = pf_test_rule(&r, &s, dir, kif,
6987 &ruleset, NULL, inp);
6996 if (!pf_pull_hdr(m, off, &uh, sizeof(uh),
6997 &action, &reason, AF_INET6)) {
6998 log = action != PF_PASS;
7001 if (uh.uh_dport == 0 ||
7002 ntohs(uh.uh_ulen) > m->m_pkthdr.len - off ||
7003 ntohs(uh.uh_ulen) < sizeof(struct udphdr)) {
7005 REASON_SET(&reason, PFRES_SHORT);
7008 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd);
7009 if (action == PF_PASS) {
7013 } else if (s == NULL) {
7014 action = pf_test_rule(&r, &s, dir, kif,
7016 &ruleset, NULL, inp);
7021 case IPPROTO_ICMPV6: {
7022 struct icmp6_hdr ih;
7025 if (!pf_pull_hdr(m, off, &ih, sizeof(ih),
7026 &action, &reason, AF_INET6)) {
7027 log = action != PF_PASS;
7030 action = pf_test_state_icmp(&s, dir, kif,
7031 m, off, h, &pd, &reason);
7032 if (action == PF_PASS) {
7036 } else if (s == NULL) {
7037 action = pf_test_rule(&r, &s, dir, kif,
7039 &ruleset, NULL, inp);
7045 action = pf_test_state_other(&s, dir, kif, m, &pd);
7046 if (action == PF_PASS) {
7050 } else if (s == NULL) {
7051 action = pf_test_rule(&r, &s, dir, kif, m, off, h,
7052 &pd, &a, &ruleset, NULL, inp);
7063 /* handle dangerous IPv6 extension headers. */
7064 if (action == PF_PASS && rh_cnt &&
7065 !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) {
7067 REASON_SET(&reason, PFRES_IPOPTIONS);
7069 DPFPRINTF(PF_DEBUG_MISC,
7070 ("pf: dropping packet with dangerous v6 headers\n"));
7073 if ((s && s->tag) || r->rtableid)
7074 pf_tag_packet(m, s ? s->tag : 0, r->rtableid);
7077 if (dir == PF_IN && s && s->key[PF_SK_STACK])
7078 m->m_pkthdr.pf.statekey = s->key[PF_SK_STACK];
7083 * Generate a hash code and qid request for ALTQ. A qid of 0
7084 * is allowed and will cause altq to select the default queue.
7086 if (action == PF_PASS) {
7087 m->m_pkthdr.fw_flags |= PF_MBUF_STRUCTURE;
7088 if (pd.tos & IPTOS_LOWDELAY)
7089 m->m_pkthdr.pf.qid = r->pqid;
7091 m->m_pkthdr.pf.qid = r->qid;
7092 m->m_pkthdr.pf.ecn_af = AF_INET6;
7093 m->m_pkthdr.pf.hdr = h;
7096 m->m_pkthdr.pf.state_hash = s->hash;
7097 m->m_pkthdr.pf.flags |= PF_TAG_STATE_HASHED;
7102 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP ||
7103 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL &&
7104 (s->nat_rule.ptr->action == PF_RDR ||
7105 s->nat_rule.ptr->action == PF_BINAT) &&
7106 IN6_IS_ADDR_LOOPBACK(&pd.dst->v6))
7107 m->m_pkthdr.pf.flags |= PF_TAG_TRANSLATE_LOCALHOST;
7109 if (dir == PF_IN && action == PF_PASS && r->divert.port) {
7110 struct pf_divert *divert;
7112 if ((divert = pf_get_divert(m))) {
7113 m->m_pkthdr.pf.flags |= PF_TAG_DIVERTED;
7114 divert->port = r->divert.port;
7115 divert->addr.ipv6 = r->divert.addr.v6;
7122 if (s != NULL && s->nat_rule.ptr != NULL &&
7123 s->nat_rule.ptr->log & PF_LOG_ALL)
7124 lr = s->nat_rule.ptr;
7127 PFLOG_PACKET(kif, h, m, AF_INET6, dir, reason, lr, a, ruleset,
7131 kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len;
7132 kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++;
7134 if (action == PF_PASS || r->action == PF_DROP) {
7135 dirndx = (dir == PF_OUT);
7136 r->packets[dirndx]++;
7137 r->bytes[dirndx] += pd.tot_len;
7139 a->packets[dirndx]++;
7140 a->bytes[dirndx] += pd.tot_len;
7143 if (s->nat_rule.ptr != NULL) {
7144 s->nat_rule.ptr->packets[dirndx]++;
7145 s->nat_rule.ptr->bytes[dirndx] += pd.tot_len;
7147 if (s->src_node != NULL) {
7148 s->src_node->packets[dirndx]++;
7149 s->src_node->bytes[dirndx] += pd.tot_len;
7151 if (s->nat_src_node != NULL) {
7152 s->nat_src_node->packets[dirndx]++;
7153 s->nat_src_node->bytes[dirndx] += pd.tot_len;
7155 dirndx = (dir == s->direction) ? 0 : 1;
7156 s->packets[dirndx]++;
7157 s->bytes[dirndx] += pd.tot_len;
7160 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule;
7161 if (nr != NULL && r == &pf_default_rule)
7163 if (tr->src.addr.type == PF_ADDR_TABLE)
7164 pfr_update_stats(tr->src.addr.p.tbl,
7165 (s == NULL) ? pd.src :
7166 &s->key[(s->direction == PF_IN)]->addr[0],
7167 pd.af, pd.tot_len, dir == PF_OUT,
7168 r->action == PF_PASS, tr->src.neg);
7169 if (tr->dst.addr.type == PF_ADDR_TABLE)
7170 pfr_update_stats(tr->dst.addr.p.tbl,
7171 (s == NULL) ? pd.dst :
7172 &s->key[(s->direction == PF_IN)]->addr[1],
7173 pd.af, pd.tot_len, dir == PF_OUT,
7174 r->action == PF_PASS, tr->dst.neg);
7178 if (action == PF_SYNPROXY_DROP) {
7183 /* pf_route6 can free the mbuf causing *m0 to become NULL */
7184 pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd);
7191 pf_check_congestion(struct ifqueue *ifq)