1 /* $NetBSD: pf.c,v 1.59 2009/12/30 16:49:02 elad Exp $ */
2 /* $OpenBSD: pf.c,v 1.552.2.1 2007/11/27 16:37:57 henning Exp $ */
5 * Copyright (c) 2001 Daniel Hartmeier
6 * Copyright (c) 2002,2003 Henning Brauer
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Effort sponsored in part by the Defense Advanced Research Projects
34 * Agency (DARPA) and Air Force Research Laboratory, Air Force
35 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: pf.c,v 1.59 2009/12/30 16:49:02 elad Exp $");
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/filio.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/kernel.h>
57 #include <sys/rwlock.h>
59 #include <sys/kthread.h>
60 #include <sys/kauth.h>
61 #endif /* __NetBSD__ */
64 #include <net/if_types.h>
66 #include <net/route.h>
68 #include <net/radix_mpath.h>
69 #endif /* !__NetBSD__ */
71 #include <netinet/in.h>
73 #include <netinet/in_offload.h>
74 #endif /* __NetBSD__ */
75 #include <netinet/in_var.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_seq.h>
81 #include <netinet/udp.h>
82 #include <netinet/ip_icmp.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/tcp_timer.h>
85 #include <netinet/tcp_var.h>
86 #include <netinet/udp_var.h>
87 #include <netinet/icmp_var.h>
89 #include <netinet/if_ether.h>
91 #include <net/if_ether.h>
92 #endif /* __NetBSD__ */
95 #include <dev/rndvar.h>
98 #endif /* __NetBSD__ */
100 #include <net/pfvar.h>
101 #include <net/if_pflog.h>
104 #include <net/if_pfsync.h>
105 #endif /* NPFSYNC > 0 */
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
111 #include <netinet6/in6_pcb.h>
112 #endif /* __NetBSD__ */
113 #include <netinet/icmp6.h>
114 #include <netinet6/nd6.h>
118 #include <netinet/tcp_rndiss.h>
119 #endif /* __NetBSD__ */
122 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
129 struct pf_state_tree_lan_ext pf_statetbl_lan_ext
;
130 struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy
;
132 struct pf_altqqueue pf_altqs
[2];
133 struct pf_palist pf_pabuf
;
134 struct pf_altqqueue
*pf_altqs_active
;
135 struct pf_altqqueue
*pf_altqs_inactive
;
136 struct pf_status pf_status
;
138 u_int32_t ticket_altqs_active
;
139 u_int32_t ticket_altqs_inactive
;
140 int altqs_inactive_open
;
141 u_int32_t ticket_pabuf
;
143 struct pf_anchor_stackframe
{
144 struct pf_ruleset
*rs
;
146 struct pf_anchor_node
*parent
;
147 struct pf_anchor
*child
;
148 } pf_anchor_stack
[64];
150 struct pool pf_src_tree_pl
, pf_rule_pl
, pf_pooladdr_pl
;
151 struct pool pf_state_pl
, pf_state_key_pl
;
152 struct pool pf_altq_pl
;
154 void pf_print_host(struct pf_addr
*, u_int16_t
, u_int8_t
);
156 void pf_init_threshold(struct pf_threshold
*, u_int32_t
,
158 void pf_add_threshold(struct pf_threshold
*);
159 int pf_check_threshold(struct pf_threshold
*);
161 void pf_change_ap(struct pf_addr
*, u_int16_t
*,
162 u_int16_t
*, u_int16_t
*, struct pf_addr
*,
163 u_int16_t
, u_int8_t
, sa_family_t
);
164 int pf_modulate_sack(struct mbuf
*, int, struct pf_pdesc
*,
165 struct tcphdr
*, struct pf_state_peer
*);
167 void pf_change_a6(struct pf_addr
*, u_int16_t
*,
168 struct pf_addr
*, u_int8_t
);
170 void pf_change_icmp(struct pf_addr
*, u_int16_t
*,
171 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
172 u_int16_t
*, u_int16_t
*, u_int16_t
*,
173 u_int16_t
*, u_int8_t
, sa_family_t
);
174 void pf_send_tcp(const struct pf_rule
*, sa_family_t
,
175 const struct pf_addr
*, const struct pf_addr
*,
176 u_int16_t
, u_int16_t
, u_int32_t
, u_int32_t
,
177 u_int8_t
, u_int16_t
, u_int16_t
, u_int8_t
, int,
178 u_int16_t
, struct ether_header
*, struct ifnet
*);
179 void pf_send_icmp(struct mbuf
*, u_int8_t
, u_int8_t
,
180 sa_family_t
, struct pf_rule
*);
181 struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
182 int, int, struct pfi_kif
*,
183 struct pf_addr
*, u_int16_t
, struct pf_addr
*,
185 struct pf_rule
*pf_get_translation(struct pf_pdesc
*, struct mbuf
*,
186 int, int, struct pfi_kif
*, struct pf_src_node
**,
187 struct pf_addr
*, u_int16_t
,
188 struct pf_addr
*, u_int16_t
,
189 struct pf_addr
*, u_int16_t
*);
190 void pf_attach_state(struct pf_state_key
*,
191 struct pf_state
*, int);
192 void pf_detach_state(struct pf_state
*, int);
193 int pf_test_rule(struct pf_rule
**, struct pf_state
**,
194 int, struct pfi_kif
*, struct mbuf
*, int,
195 void *, struct pf_pdesc
*, struct pf_rule
**,
196 struct pf_ruleset
**, struct ifqueue
*);
197 int pf_test_fragment(struct pf_rule
**, int,
198 struct pfi_kif
*, struct mbuf
*, void *,
199 struct pf_pdesc
*, struct pf_rule
**,
200 struct pf_ruleset
**);
201 int pf_test_state_tcp(struct pf_state
**, int,
202 struct pfi_kif
*, struct mbuf
*, int,
203 void *, struct pf_pdesc
*, u_short
*);
204 int pf_test_state_udp(struct pf_state
**, int,
205 struct pfi_kif
*, struct mbuf
*, int,
206 void *, struct pf_pdesc
*);
207 int pf_test_state_icmp(struct pf_state
**, int,
208 struct pfi_kif
*, struct mbuf
*, int,
209 void *, struct pf_pdesc
*, u_short
*);
210 int pf_test_state_other(struct pf_state
**, int,
211 struct pfi_kif
*, struct pf_pdesc
*);
212 int pf_match_tag(struct mbuf
*, struct pf_rule
*, int *);
213 void pf_step_into_anchor(int *, struct pf_ruleset
**, int,
214 struct pf_rule
**, struct pf_rule
**, int *);
215 int pf_step_out_of_anchor(int *, struct pf_ruleset
**,
216 int, struct pf_rule
**, struct pf_rule
**,
218 void pf_hash(const struct pf_addr
*, struct pf_addr
*,
219 struct pf_poolhashkey
*, sa_family_t
);
220 int pf_map_addr(u_int8_t
, struct pf_rule
*,
221 const struct pf_addr
*, struct pf_addr
*,
222 struct pf_addr
*, struct pf_src_node
**);
223 int pf_get_sport(sa_family_t
, u_int8_t
, struct pf_rule
*,
224 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
225 struct pf_addr
*, u_int16_t
*, u_int16_t
, u_int16_t
,
226 struct pf_src_node
**);
227 void pf_route(struct mbuf
**, struct pf_rule
*, int,
228 struct ifnet
*, struct pf_state
*,
230 void pf_route6(struct mbuf
**, struct pf_rule
*, int,
231 struct ifnet
*, struct pf_state
*,
233 int pf_socket_lookup(int, struct pf_pdesc
*);
234 u_int8_t
pf_get_wscale(struct mbuf
*, int, u_int16_t
,
236 u_int16_t
pf_get_mss(struct mbuf
*, int, u_int16_t
,
238 u_int16_t
pf_calc_mss(struct pf_addr
*, sa_family_t
,
240 void pf_set_rt_ifp(struct pf_state
*,
243 int pf_check_proto_cksum(struct mbuf
*, int, int, int,
244 u_int8_t
, sa_family_t
);
246 int pf_check_proto_cksum(struct mbuf
*, int, int,
247 u_int8_t
, sa_family_t
);
248 #endif /* !__NetBSD__ */
249 int pf_addr_wrap_neq(struct pf_addr_wrap
*,
250 struct pf_addr_wrap
*);
251 struct pf_state
*pf_find_state(struct pfi_kif
*,
252 struct pf_state_key_cmp
*, u_int8_t
);
253 int pf_src_connlimit(struct pf_state
**);
254 void pf_stateins_err(const char *, struct pf_state
*,
256 int pf_check_congestion(struct ifqueue
*);
258 extern struct pool pfr_ktable_pl
;
259 extern struct pool pfr_kentry_pl
;
261 struct pf_pool_limit pf_pool_limits
[PF_LIMIT_MAX
] = {
262 { &pf_state_pl
, PFSTATE_HIWAT
},
263 { &pf_src_tree_pl
, PFSNODE_HIWAT
},
264 { &pf_frent_pl
, PFFRAG_FRENT_HIWAT
},
265 { &pfr_ktable_pl
, PFR_KTABLE_HIWAT
},
266 { &pfr_kentry_pl
, PFR_KENTRY_HIWAT
}
269 #define STATE_LOOKUP() \
271 if (direction == PF_IN) \
272 *state = pf_find_state(kif, &key, PF_EXT_GWY); \
274 *state = pf_find_state(kif, &key, PF_LAN_EXT); \
275 if (*state == NULL || (*state)->timeout == PFTM_PURGE) \
277 if (direction == PF_OUT && \
278 (((*state)->rule.ptr->rt == PF_ROUTETO && \
279 (*state)->rule.ptr->direction == PF_OUT) || \
280 ((*state)->rule.ptr->rt == PF_REPLYTO && \
281 (*state)->rule.ptr->direction == PF_IN)) && \
282 (*state)->rt_kif != NULL && \
283 (*state)->rt_kif != kif) \
287 #define STATE_TRANSLATE(sk) \
288 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
289 ((sk)->af == AF_INET6 && \
290 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
291 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
292 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3])) || \
293 (sk)->lan.port != (sk)->gwy.port
295 #define BOUND_IFACE(r, k) \
296 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
298 #define STATE_INC_COUNTERS(s) \
300 s->rule.ptr->states++; \
301 if (s->anchor.ptr != NULL) \
302 s->anchor.ptr->states++; \
303 if (s->nat_rule.ptr != NULL) \
304 s->nat_rule.ptr->states++; \
307 #define STATE_DEC_COUNTERS(s) \
309 if (s->nat_rule.ptr != NULL) \
310 s->nat_rule.ptr->states--; \
311 if (s->anchor.ptr != NULL) \
312 s->anchor.ptr->states--; \
313 s->rule.ptr->states--; \
316 static __inline
int pf_src_compare(struct pf_src_node
*, struct pf_src_node
*);
317 static __inline
int pf_state_compare_lan_ext(struct pf_state_key
*,
318 struct pf_state_key
*);
319 static __inline
int pf_state_compare_ext_gwy(struct pf_state_key
*,
320 struct pf_state_key
*);
321 static __inline
int pf_state_compare_id(struct pf_state
*,
324 struct pf_src_tree tree_src_tracking
;
326 struct pf_state_tree_id tree_id
;
327 struct pf_state_queue state_list
;
329 RB_GENERATE(pf_src_tree
, pf_src_node
, entry
, pf_src_compare
);
330 RB_GENERATE(pf_state_tree_lan_ext
, pf_state_key
,
331 entry_lan_ext
, pf_state_compare_lan_ext
);
332 RB_GENERATE(pf_state_tree_ext_gwy
, pf_state_key
,
333 entry_ext_gwy
, pf_state_compare_ext_gwy
);
334 RB_GENERATE(pf_state_tree_id
, pf_state
,
335 entry_id
, pf_state_compare_id
);
337 #define PF_DT_SKIP_LANEXT 0x01
338 #define PF_DT_SKIP_EXTGWY 0x02
341 static __inline
struct pfi_kif
*
342 bound_iface(const struct pf_rule
*r
, const struct pf_rule
*nr
,
347 rule_flag
= r
->rule_flag
;
349 rule_flag
|= nr
->rule_flag
;
351 return ((rule_flag
& PFRULE_IFBOUND
) != 0) ? k
: pfi_all
;
353 #endif /* __NetBSD__ */
356 pf_src_compare(struct pf_src_node
*a
, struct pf_src_node
*b
)
360 if (a
->rule
.ptr
> b
->rule
.ptr
)
362 if (a
->rule
.ptr
< b
->rule
.ptr
)
364 if ((diff
= a
->af
- b
->af
) != 0)
369 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
371 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
377 if (a
->addr
.addr32
[3] > b
->addr
.addr32
[3])
379 if (a
->addr
.addr32
[3] < b
->addr
.addr32
[3])
381 if (a
->addr
.addr32
[2] > b
->addr
.addr32
[2])
383 if (a
->addr
.addr32
[2] < b
->addr
.addr32
[2])
385 if (a
->addr
.addr32
[1] > b
->addr
.addr32
[1])
387 if (a
->addr
.addr32
[1] < b
->addr
.addr32
[1])
389 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
391 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
400 pf_state_compare_lan_ext(struct pf_state_key
*a
, struct pf_state_key
*b
)
404 if ((diff
= a
->proto
- b
->proto
) != 0)
406 if ((diff
= a
->af
- b
->af
) != 0)
411 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
413 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
415 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
417 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
423 if (a
->lan
.addr
.addr32
[3] > b
->lan
.addr
.addr32
[3])
425 if (a
->lan
.addr
.addr32
[3] < b
->lan
.addr
.addr32
[3])
427 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
429 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
431 if (a
->lan
.addr
.addr32
[2] > b
->lan
.addr
.addr32
[2])
433 if (a
->lan
.addr
.addr32
[2] < b
->lan
.addr
.addr32
[2])
435 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
437 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
439 if (a
->lan
.addr
.addr32
[1] > b
->lan
.addr
.addr32
[1])
441 if (a
->lan
.addr
.addr32
[1] < b
->lan
.addr
.addr32
[1])
443 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
445 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
447 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
449 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
451 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
453 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
459 if ((diff
= a
->lan
.port
- b
->lan
.port
) != 0)
461 if ((diff
= a
->ext
.port
- b
->ext
.port
) != 0)
468 pf_state_compare_ext_gwy(struct pf_state_key
*a
, struct pf_state_key
*b
)
472 if ((diff
= a
->proto
- b
->proto
) != 0)
474 if ((diff
= a
->af
- b
->af
) != 0)
479 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
481 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
483 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
485 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
491 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
493 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
495 if (a
->gwy
.addr
.addr32
[3] > b
->gwy
.addr
.addr32
[3])
497 if (a
->gwy
.addr
.addr32
[3] < b
->gwy
.addr
.addr32
[3])
499 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
501 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
503 if (a
->gwy
.addr
.addr32
[2] > b
->gwy
.addr
.addr32
[2])
505 if (a
->gwy
.addr
.addr32
[2] < b
->gwy
.addr
.addr32
[2])
507 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
509 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
511 if (a
->gwy
.addr
.addr32
[1] > b
->gwy
.addr
.addr32
[1])
513 if (a
->gwy
.addr
.addr32
[1] < b
->gwy
.addr
.addr32
[1])
515 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
517 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
519 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
521 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
527 if ((diff
= a
->ext
.port
- b
->ext
.port
) != 0)
529 if ((diff
= a
->gwy
.port
- b
->gwy
.port
) != 0)
536 pf_state_compare_id(struct pf_state
*a
, struct pf_state
*b
)
542 if (a
->creatorid
> b
->creatorid
)
544 if (a
->creatorid
< b
->creatorid
)
552 pf_addrcpy(struct pf_addr
*dst
, const struct pf_addr
*src
, sa_family_t af
)
557 dst
->addr32
[0] = src
->addr32
[0];
561 dst
->addr32
[0] = src
->addr32
[0];
562 dst
->addr32
[1] = src
->addr32
[1];
563 dst
->addr32
[2] = src
->addr32
[2];
564 dst
->addr32
[3] = src
->addr32
[3];
571 pf_find_state_byid(struct pf_state_cmp
*key
)
573 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
575 return (RB_FIND(pf_state_tree_id
, &tree_id
, (struct pf_state
*)key
));
579 pf_find_state(struct pfi_kif
*kif
, struct pf_state_key_cmp
*key
, u_int8_t tree
)
581 struct pf_state_key
*sk
;
584 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
588 sk
= RB_FIND(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
589 (struct pf_state_key
*)key
);
592 sk
= RB_FIND(pf_state_tree_ext_gwy
, &pf_statetbl_ext_gwy
,
593 (struct pf_state_key
*)key
);
596 panic("pf_find_state");
599 /* list is sorted, if-bound states before floating ones */
601 TAILQ_FOREACH(s
, &sk
->states
, next
)
602 if (s
->kif
== pfi_all
|| s
->kif
== kif
)
609 pf_find_state_all(struct pf_state_key_cmp
*key
, u_int8_t tree
, int *more
)
611 struct pf_state_key
*sk
;
612 struct pf_state
*s
, *ret
= NULL
;
614 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
618 sk
= RB_FIND(pf_state_tree_lan_ext
,
619 &pf_statetbl_lan_ext
, (struct pf_state_key
*)key
);
622 sk
= RB_FIND(pf_state_tree_ext_gwy
,
623 &pf_statetbl_ext_gwy
, (struct pf_state_key
*)key
);
626 panic("pf_find_state_all");
630 ret
= TAILQ_FIRST(&sk
->states
);
634 TAILQ_FOREACH(s
, &sk
->states
, next
)
642 pf_init_threshold(struct pf_threshold
*threshold
,
643 u_int32_t limit
, u_int32_t seconds
)
645 threshold
->limit
= limit
* PF_THRESHOLD_MULT
;
646 threshold
->seconds
= seconds
;
647 threshold
->count
= 0;
648 threshold
->last
= time_second
;
652 pf_add_threshold(struct pf_threshold
*threshold
)
654 u_int32_t t
= time_second
, diff
= t
- threshold
->last
;
656 if (diff
>= threshold
->seconds
)
657 threshold
->count
= 0;
659 threshold
->count
-= threshold
->count
* diff
/
661 threshold
->count
+= PF_THRESHOLD_MULT
;
666 pf_check_threshold(struct pf_threshold
*threshold
)
668 return (threshold
->count
> threshold
->limit
);
672 pf_src_connlimit(struct pf_state
**state
)
676 (*state
)->src_node
->conn
++;
677 (*state
)->src
.tcp_est
= 1;
678 pf_add_threshold(&(*state
)->src_node
->conn_rate
);
680 if ((*state
)->rule
.ptr
->max_src_conn
&&
681 (*state
)->rule
.ptr
->max_src_conn
<
682 (*state
)->src_node
->conn
) {
683 pf_status
.lcounters
[LCNT_SRCCONN
]++;
687 if ((*state
)->rule
.ptr
->max_src_conn_rate
.limit
&&
688 pf_check_threshold(&(*state
)->src_node
->conn_rate
)) {
689 pf_status
.lcounters
[LCNT_SRCCONNRATE
]++;
696 if ((*state
)->rule
.ptr
->overload_tbl
) {
698 u_int32_t killed
= 0;
700 pf_status
.lcounters
[LCNT_OVERLOAD_TABLE
]++;
701 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
702 printf("pf_src_connlimit: blocking address ");
703 pf_print_host(&(*state
)->src_node
->addr
, 0,
704 (*state
)->state_key
->af
);
707 bzero(&p
, sizeof(p
));
708 p
.pfra_af
= (*state
)->state_key
->af
;
709 switch ((*state
)->state_key
->af
) {
713 p
.pfra_ip4addr
= (*state
)->src_node
->addr
.v4
;
719 p
.pfra_ip6addr
= (*state
)->src_node
->addr
.v6
;
724 pfr_insert_kentry((*state
)->rule
.ptr
->overload_tbl
,
727 /* kill existing states if that's required. */
728 if ((*state
)->rule
.ptr
->flush
) {
729 struct pf_state_key
*sk
;
732 pf_status
.lcounters
[LCNT_OVERLOAD_FLUSH
]++;
733 RB_FOREACH(st
, pf_state_tree_id
, &tree_id
) {
736 * Kill states from this source. (Only those
737 * from the same rule if PF_FLUSH_GLOBAL is not
741 (*state
)->state_key
->af
&&
742 (((*state
)->state_key
->direction
==
744 PF_AEQ(&(*state
)->src_node
->addr
,
745 &sk
->lan
.addr
, sk
->af
)) ||
746 ((*state
)->state_key
->direction
== PF_IN
&&
747 PF_AEQ(&(*state
)->src_node
->addr
,
748 &sk
->ext
.addr
, sk
->af
))) &&
749 ((*state
)->rule
.ptr
->flush
&
751 (*state
)->rule
.ptr
== st
->rule
.ptr
)) {
752 st
->timeout
= PFTM_PURGE
;
753 st
->src
.state
= st
->dst
.state
=
758 if (pf_status
.debug
>= PF_DEBUG_MISC
)
759 printf(", %u states killed", killed
);
761 if (pf_status
.debug
>= PF_DEBUG_MISC
)
765 /* kill this state */
766 (*state
)->timeout
= PFTM_PURGE
;
767 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
772 pf_insert_src_node(struct pf_src_node
**sn
, struct pf_rule
*rule
,
773 struct pf_addr
*src
, sa_family_t af
)
775 struct pf_src_node k
;
779 PF_ACPY(&k
.addr
, src
, af
);
780 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
781 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
785 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
786 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
789 if (!rule
->max_src_nodes
||
790 rule
->src_nodes
< rule
->max_src_nodes
)
791 (*sn
) = pool_get(&pf_src_tree_pl
, PR_NOWAIT
);
793 pf_status
.lcounters
[LCNT_SRCNODES
]++;
796 bzero(*sn
, sizeof(struct pf_src_node
));
798 pf_init_threshold(&(*sn
)->conn_rate
,
799 rule
->max_src_conn_rate
.limit
,
800 rule
->max_src_conn_rate
.seconds
);
803 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
804 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
805 (*sn
)->rule
.ptr
= rule
;
807 (*sn
)->rule
.ptr
= NULL
;
808 PF_ACPY(&(*sn
)->addr
, src
, af
);
809 if (RB_INSERT(pf_src_tree
,
810 &tree_src_tracking
, *sn
) != NULL
) {
811 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
812 printf("pf: src_tree insert failed: ");
813 pf_print_host(&(*sn
)->addr
, 0, af
);
816 pool_put(&pf_src_tree_pl
, *sn
);
819 (*sn
)->creation
= time_second
;
820 (*sn
)->ruletype
= rule
->action
;
821 if ((*sn
)->rule
.ptr
!= NULL
)
822 (*sn
)->rule
.ptr
->src_nodes
++;
823 pf_status
.scounters
[SCNT_SRC_NODE_INSERT
]++;
824 pf_status
.src_nodes
++;
826 if (rule
->max_src_states
&&
827 (*sn
)->states
>= rule
->max_src_states
) {
828 pf_status
.lcounters
[LCNT_SRCSTATES
]++;
836 pf_stateins_err(const char *tree
, struct pf_state
*s
, struct pfi_kif
*kif
)
838 struct pf_state_key
*sk
= s
->state_key
;
840 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
841 printf("pf: state insert failed: %s %s", tree
, kif
->pfik_name
);
843 pf_print_host(&sk
->lan
.addr
, sk
->lan
.port
,
846 pf_print_host(&sk
->gwy
.addr
, sk
->gwy
.port
,
849 pf_print_host(&sk
->ext
.addr
, sk
->ext
.port
,
851 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
852 printf(" (from sync)");
858 pf_insert_state(struct pfi_kif
*kif
, struct pf_state
*s
)
860 struct pf_state_key
*cur
;
863 KASSERT(s
->state_key
!= NULL
);
866 if ((cur
= RB_INSERT(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
867 s
->state_key
)) != NULL
) {
868 /* key exists. check for same kif, if none, add to key */
869 TAILQ_FOREACH(sp
, &cur
->states
, next
)
870 if (sp
->kif
== kif
) { /* collision! */
871 pf_stateins_err("tree_lan_ext", s
, kif
);
873 PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
876 pf_detach_state(s
, PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
877 pf_attach_state(cur
, s
, kif
== pfi_all
? 1 : 0);
880 /* if cur != NULL, we already found a state key and attached to it */
881 if (cur
== NULL
&& (cur
= RB_INSERT(pf_state_tree_ext_gwy
,
882 &pf_statetbl_ext_gwy
, s
->state_key
)) != NULL
) {
883 /* must not happen. we must have found the sk above! */
884 pf_stateins_err("tree_ext_gwy", s
, kif
);
885 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
889 if (s
->id
== 0 && s
->creatorid
== 0) {
890 s
->id
= htobe64(pf_status
.stateid
++);
891 s
->creatorid
= pf_status
.hostid
;
893 if (RB_INSERT(pf_state_tree_id
, &tree_id
, s
) != NULL
) {
894 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
896 printf("pf: state insert failed: "
897 "id: %016" PRIx64
" creatorid: %08x",
898 be64toh(s
->id
), ntohl(s
->creatorid
));
900 printf("pf: state insert failed: "
901 "id: %016llx creatorid: %08x",
902 betoh64(s
->id
), ntohl(s
->creatorid
));
903 #endif /* !__NetBSD__ */
904 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
905 printf(" (from sync)");
908 pf_detach_state(s
, 0);
911 TAILQ_INSERT_TAIL(&state_list
, s
, entry_list
);
912 pf_status
.fcounters
[FCNT_STATE_INSERT
]++;
914 pfi_kif_ref(kif
, PFI_KIF_REF_STATE
);
916 pfsync_insert_state(s
);
922 pf_purge_thread(void *v
)
927 tsleep(pf_purge_thread
, PWAIT
, "pftm", 1 * hz
);
931 /* process a fraction of the state table every second */
932 pf_purge_expired_states(1 + (pf_status
.states
933 / pf_default_rule
.timeout
[PFTM_INTERVAL
]));
935 /* purge other expired types every PFTM_INTERVAL seconds */
936 if (++nloops
>= pf_default_rule
.timeout
[PFTM_INTERVAL
]) {
937 pf_purge_expired_fragments();
938 pf_purge_expired_src_nodes(0);
947 pf_state_expires(const struct pf_state
*state
)
954 /* handle all PFTM_* > PFTM_MAX here */
955 if (state
->timeout
== PFTM_PURGE
)
956 return (time_second
);
957 if (state
->timeout
== PFTM_UNTIL_PACKET
)
959 KASSERT(state
->timeout
!= PFTM_UNLINKED
);
960 KASSERT(state
->timeout
< PFTM_MAX
);
961 timeout
= state
->rule
.ptr
->timeout
[state
->timeout
];
963 timeout
= pf_default_rule
.timeout
[state
->timeout
];
964 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
966 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
967 states
= state
->rule
.ptr
->states
;
969 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
970 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
971 states
= pf_status
.states
;
973 if (end
&& states
> start
&& start
< end
) {
975 return (state
->expire
+ timeout
* (end
- states
) /
978 return (time_second
);
980 return (state
->expire
+ timeout
);
984 pf_purge_expired_src_nodes(int waslocked
)
986 struct pf_src_node
*cur
, *next
;
987 int locked
= waslocked
;
989 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
990 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
992 if (cur
->states
<= 0 && cur
->expire
<= time_second
) {
994 rw_enter_write(&pf_consistency_lock
);
995 next
= RB_NEXT(pf_src_tree
,
996 &tree_src_tracking
, cur
);
999 if (cur
->rule
.ptr
!= NULL
) {
1000 cur
->rule
.ptr
->src_nodes
--;
1001 if (cur
->rule
.ptr
->states
<= 0 &&
1002 cur
->rule
.ptr
->max_src_nodes
<= 0)
1003 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1005 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1006 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1007 pf_status
.src_nodes
--;
1008 pool_put(&pf_src_tree_pl
, cur
);
1012 if (locked
&& !waslocked
)
1013 rw_exit_write(&pf_consistency_lock
);
1017 pf_src_tree_remove_state(struct pf_state
*s
)
1021 if (s
->src_node
!= NULL
) {
1023 --s
->src_node
->conn
;
1024 if (--s
->src_node
->states
<= 0) {
1025 timeout
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1028 pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1029 s
->src_node
->expire
= time_second
+ timeout
;
1032 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1033 if (--s
->nat_src_node
->states
<= 0) {
1034 timeout
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1037 pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1038 s
->nat_src_node
->expire
= time_second
+ timeout
;
1041 s
->src_node
= s
->nat_src_node
= NULL
;
1044 /* callers should be at splsoftnet */
1046 pf_unlink_state(struct pf_state
*cur
)
1048 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1049 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1050 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1051 cur
->state_key
->ext
.port
, cur
->state_key
->lan
.port
,
1052 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1053 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1055 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1057 if (cur
->creatorid
== pf_status
.hostid
)
1058 pfsync_delete_state(cur
);
1060 cur
->timeout
= PFTM_UNLINKED
;
1061 pf_src_tree_remove_state(cur
);
1062 pf_detach_state(cur
, 0);
1065 /* callers should be at splsoftnet and hold the
1066 * write_lock on pf_consistency_lock */
1068 pf_free_state(struct pf_state
*cur
)
1071 if (pfsyncif
!= NULL
&&
1072 (pfsyncif
->sc_bulk_send_next
== cur
||
1073 pfsyncif
->sc_bulk_terminator
== cur
))
1076 KASSERT(cur
->timeout
== PFTM_UNLINKED
);
1077 if (--cur
->rule
.ptr
->states
<= 0 &&
1078 cur
->rule
.ptr
->src_nodes
<= 0)
1079 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1080 if (cur
->nat_rule
.ptr
!= NULL
)
1081 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1082 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1083 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1084 if (cur
->anchor
.ptr
!= NULL
)
1085 if (--cur
->anchor
.ptr
->states
<= 0)
1086 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1087 pf_normalize_tcp_cleanup(cur
);
1088 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1089 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1091 pf_tag_unref(cur
->tag
);
1092 pool_put(&pf_state_pl
, cur
);
1093 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1098 pf_purge_expired_states(u_int32_t maxcheck
)
1100 static struct pf_state
*cur
= NULL
;
1101 struct pf_state
*next
;
1104 while (maxcheck
--) {
1105 /* wrap to start of list when we hit the end */
1107 cur
= TAILQ_FIRST(&state_list
);
1109 break; /* list empty */
1112 /* get next state, as cur may get deleted */
1113 next
= TAILQ_NEXT(cur
, entry_list
);
1115 if (cur
->timeout
== PFTM_UNLINKED
) {
1116 /* free unlinked state */
1118 rw_enter_write(&pf_consistency_lock
);
1122 } else if (pf_state_expires(cur
) <= time_second
) {
1123 /* unlink and free expired state */
1124 pf_unlink_state(cur
);
1126 rw_enter_write(&pf_consistency_lock
);
1135 rw_exit_write(&pf_consistency_lock
);
1139 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1141 if (aw
->type
!= PF_ADDR_TABLE
)
1143 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1149 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1151 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1153 pfr_detach_table(aw
->p
.tbl
);
1158 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1160 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1162 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1164 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1165 kt
= kt
->pfrkt_root
;
1167 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1172 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1177 u_int32_t a
= ntohl(addr
->addr32
[0]);
1178 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1190 u_int8_t i
, curstart
= 255, curend
= 0,
1191 maxstart
= 0, maxend
= 0;
1192 for (i
= 0; i
< 8; i
++) {
1193 if (!addr
->addr16
[i
]) {
1194 if (curstart
== 255)
1200 if ((curend
- curstart
) >
1201 (maxend
- maxstart
)) {
1202 maxstart
= curstart
;
1209 for (i
= 0; i
< 8; i
++) {
1210 if (i
>= maxstart
&& i
<= maxend
) {
1219 b
= ntohs(addr
->addr16
[i
]);
1236 pf_print_state(struct pf_state
*s
)
1238 struct pf_state_key
*sk
= s
->state_key
;
1239 switch (sk
->proto
) {
1249 case IPPROTO_ICMPV6
:
1253 printf("%u ", sk
->proto
);
1256 pf_print_host(&sk
->lan
.addr
, sk
->lan
.port
, sk
->af
);
1258 pf_print_host(&sk
->gwy
.addr
, sk
->gwy
.port
, sk
->af
);
1260 pf_print_host(&sk
->ext
.addr
, sk
->ext
.port
, sk
->af
);
1261 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
1262 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
1263 if (s
->src
.wscale
&& s
->dst
.wscale
)
1264 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
1266 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
1267 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
1268 if (s
->src
.wscale
&& s
->dst
.wscale
)
1269 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
1271 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
1275 pf_print_flags(u_int8_t f
)
1297 #define PF_SET_SKIP_STEPS(i) \
1299 while (head[i] != cur) { \
1300 head[i]->skip[i].ptr = cur; \
1301 head[i] = TAILQ_NEXT(head[i], entries); \
1306 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
1308 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
1311 cur
= TAILQ_FIRST(rules
);
1313 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1315 while (cur
!= NULL
) {
1317 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
1318 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
1319 if (cur
->direction
!= prev
->direction
)
1320 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
1321 if (cur
->af
!= prev
->af
)
1322 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
1323 if (cur
->proto
!= prev
->proto
)
1324 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
1325 if (cur
->src
.neg
!= prev
->src
.neg
||
1326 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
1327 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
1328 if (cur
->src
.port
[0] != prev
->src
.port
[0] ||
1329 cur
->src
.port
[1] != prev
->src
.port
[1] ||
1330 cur
->src
.port_op
!= prev
->src
.port_op
)
1331 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1332 if (cur
->dst
.neg
!= prev
->dst
.neg
||
1333 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
1334 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
1335 if (cur
->dst
.port
[0] != prev
->dst
.port
[0] ||
1336 cur
->dst
.port
[1] != prev
->dst
.port
[1] ||
1337 cur
->dst
.port_op
!= prev
->dst
.port_op
)
1338 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1341 cur
= TAILQ_NEXT(cur
, entries
);
1343 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1344 PF_SET_SKIP_STEPS(i
);
1348 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
1350 if (aw1
->type
!= aw2
->type
)
1352 switch (aw1
->type
) {
1353 case PF_ADDR_ADDRMASK
:
1354 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
1356 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
1359 case PF_ADDR_DYNIFTL
:
1360 return (aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
1361 case PF_ADDR_NOROUTE
:
1362 case PF_ADDR_URPFFAILED
:
1365 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
1366 case PF_ADDR_RTLABEL
:
1367 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
1369 printf("invalid address type: %d\n", aw1
->type
);
1375 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
1381 l
= cksum
+ old
- new;
1382 l
= (l
>> 16) + (l
& 65535);
1390 pf_change_ap(struct pf_addr
*a
, u_int16_t
*p
, u_int16_t
*ic
, u_int16_t
*pc
,
1391 struct pf_addr
*an
, u_int16_t pn
, u_int8_t u
, sa_family_t af
)
1396 PF_ACPY(&ao
, a
, af
);
1404 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
1405 ao
.addr16
[0], an
->addr16
[0], 0),
1406 ao
.addr16
[1], an
->addr16
[1], 0);
1408 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
1409 ao
.addr16
[0], an
->addr16
[0], u
),
1410 ao
.addr16
[1], an
->addr16
[1], u
),
1416 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1417 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1418 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
1419 ao
.addr16
[0], an
->addr16
[0], u
),
1420 ao
.addr16
[1], an
->addr16
[1], u
),
1421 ao
.addr16
[2], an
->addr16
[2], u
),
1422 ao
.addr16
[3], an
->addr16
[3], u
),
1423 ao
.addr16
[4], an
->addr16
[4], u
),
1424 ao
.addr16
[5], an
->addr16
[5], u
),
1425 ao
.addr16
[6], an
->addr16
[6], u
),
1426 ao
.addr16
[7], an
->addr16
[7], u
),
1434 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
1436 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
1440 memcpy(&ao
, a
, sizeof(ao
));
1441 memcpy(a
, &an
, sizeof(u_int32_t
));
1442 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
1443 ao
% 65536, an
% 65536, u
);
1448 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
1452 PF_ACPY(&ao
, a
, AF_INET6
);
1453 PF_ACPY(a
, an
, AF_INET6
);
1455 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1456 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1457 pf_cksum_fixup(pf_cksum_fixup(*c
,
1458 ao
.addr16
[0], an
->addr16
[0], u
),
1459 ao
.addr16
[1], an
->addr16
[1], u
),
1460 ao
.addr16
[2], an
->addr16
[2], u
),
1461 ao
.addr16
[3], an
->addr16
[3], u
),
1462 ao
.addr16
[4], an
->addr16
[4], u
),
1463 ao
.addr16
[5], an
->addr16
[5], u
),
1464 ao
.addr16
[6], an
->addr16
[6], u
),
1465 ao
.addr16
[7], an
->addr16
[7], u
);
1470 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
1471 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
1472 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
1474 struct pf_addr oia
, ooa
;
1476 PF_ACPY(&oia
, ia
, af
);
1477 PF_ACPY(&ooa
, oa
, af
);
1479 /* Change inner protocol port, fix inner protocol checksum. */
1481 u_int16_t oip
= *ip
;
1488 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
1489 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
1491 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
1493 /* Change inner ip address, fix inner ip and icmp checksums. */
1494 PF_ACPY(ia
, na
, af
);
1498 u_int32_t oh2c
= *h2c
;
1500 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
1501 oia
.addr16
[0], ia
->addr16
[0], 0),
1502 oia
.addr16
[1], ia
->addr16
[1], 0);
1503 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
1504 oia
.addr16
[0], ia
->addr16
[0], 0),
1505 oia
.addr16
[1], ia
->addr16
[1], 0);
1506 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
1512 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1513 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1514 pf_cksum_fixup(pf_cksum_fixup(*ic
,
1515 oia
.addr16
[0], ia
->addr16
[0], u
),
1516 oia
.addr16
[1], ia
->addr16
[1], u
),
1517 oia
.addr16
[2], ia
->addr16
[2], u
),
1518 oia
.addr16
[3], ia
->addr16
[3], u
),
1519 oia
.addr16
[4], ia
->addr16
[4], u
),
1520 oia
.addr16
[5], ia
->addr16
[5], u
),
1521 oia
.addr16
[6], ia
->addr16
[6], u
),
1522 oia
.addr16
[7], ia
->addr16
[7], u
);
1526 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
1527 PF_ACPY(oa
, na
, af
);
1531 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
1532 ooa
.addr16
[0], oa
->addr16
[0], 0),
1533 ooa
.addr16
[1], oa
->addr16
[1], 0);
1538 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1539 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
1540 pf_cksum_fixup(pf_cksum_fixup(*ic
,
1541 ooa
.addr16
[0], oa
->addr16
[0], u
),
1542 ooa
.addr16
[1], oa
->addr16
[1], u
),
1543 ooa
.addr16
[2], oa
->addr16
[2], u
),
1544 ooa
.addr16
[3], oa
->addr16
[3], u
),
1545 ooa
.addr16
[4], oa
->addr16
[4], u
),
1546 ooa
.addr16
[5], oa
->addr16
[5], u
),
1547 ooa
.addr16
[6], oa
->addr16
[6], u
),
1548 ooa
.addr16
[7], oa
->addr16
[7], u
);
1556 * Need to modulate the sequence numbers in the TCP SACK option
1557 * (credits to Krzysztof Pfaff for report and patch)
1560 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
1561 struct tcphdr
*th
, struct pf_state_peer
*dst
)
1563 int hlen
= (th
->th_off
<< 2) - sizeof(*th
), thoptlen
= hlen
;
1564 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
1565 int copyback
= 0, i
, olen
;
1566 struct sackblk sack
;
1569 #define TCPOLEN_SACK (2 * sizeof(uint32_t))
1572 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
1573 if (hlen
< TCPOLEN_SACKLEN
||
1574 !pf_pull_hdr(m
, off
+ sizeof(*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
1577 while (hlen
>= TCPOLEN_SACKLEN
) {
1580 case TCPOPT_EOL
: /* FALLTHROUGH */
1588 if (olen
>= TCPOLEN_SACKLEN
) {
1589 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
1590 i
+= TCPOLEN_SACK
) {
1591 memcpy(&sack
, &opt
[i
], sizeof(sack
));
1593 #define SACK_START sack.left
1594 #define SACK_END sack.right
1596 #define SACK_START sack.start
1597 #define SACK_END sack.end
1599 pf_change_a(&SACK_START
, &th
->th_sum
,
1600 htonl(ntohl(SACK_START
) -
1602 pf_change_a(&SACK_END
, &th
->th_sum
,
1603 htonl(ntohl(SACK_END
) -
1607 memcpy(&opt
[i
], &sack
, sizeof(sack
));
1621 m_copyback(m
, off
+ sizeof(*th
), thoptlen
, opts
);
1626 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
1627 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
1628 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
1629 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
1630 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
1635 struct ip
*h
= NULL
;
1638 struct ip6_hdr
*h6
= NULL
;
1643 struct pf_mtag
*pf_mtag
;
1644 #endif /* __NetBSD__ */
1646 /* maximum segment size tcp option */
1647 tlen
= sizeof(struct tcphdr
);
1654 len
= sizeof(struct ip
) + tlen
;
1659 len
= sizeof(struct ip6_hdr
) + tlen
;
1666 /* create outgoing mbuf */
1667 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
1671 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
1676 pf_mtag
->flags
|= PF_TAG_GENERATED
;
1677 pf_mtag
->tag
= rtag
;
1679 if (r
!= NULL
&& r
->rtableid
>= 0)
1680 pf_mtag
->rtableid
= r
->rtableid
;
1683 m
->m_pkthdr
.pf
.flags
|= PF_TAG_GENERATED
;
1684 m
->m_pkthdr
.pf
.tag
= rtag
;
1686 if (r
!= NULL
&& r
->rtableid
>= 0)
1687 m
->m_pkthdr
.pf
.rtableid
= m
->m_pkthdr
.pf
.rtableid
;
1688 #endif /* !__NetBSD__ */
1691 if (r
!= NULL
&& r
->qid
) {
1694 struct altq_tag
*atag
;
1696 mtag
= m_tag_get(PACKET_TAG_ALTQ_QID
, sizeof(*atag
), M_NOWAIT
);
1698 atag
= (struct altq_tag
*)(mtag
+ 1);
1700 /* add hints for ecn */
1702 atag
->hdr
= mtod(m
, struct ip
*);
1703 m_tag_prepend(m
, mtag
);
1706 m
->m_pkthdr
.pf
.qid
= r
->qid
;
1707 /* add hints for ecn */
1708 m
->m_pkthdr
.pf
.hdr
= mtod(m
, struct ip
*);
1709 #endif /* !__NetBSD__ */
1712 m
->m_data
+= max_linkhdr
;
1713 m
->m_pkthdr
.len
= m
->m_len
= len
;
1714 m
->m_pkthdr
.rcvif
= NULL
;
1715 bzero(m
->m_data
, len
);
1719 h
= mtod(m
, struct ip
*);
1721 /* IP header fields included in the TCP checksum */
1722 h
->ip_p
= IPPROTO_TCP
;
1723 h
->ip_len
= htons(tlen
);
1724 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
1725 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
1727 th
= (struct tcphdr
*)((char *)h
+ sizeof(struct ip
));
1732 h6
= mtod(m
, struct ip6_hdr
*);
1734 /* IP header fields included in the TCP checksum */
1735 h6
->ip6_nxt
= IPPROTO_TCP
;
1736 h6
->ip6_plen
= htons(tlen
);
1737 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof(struct in6_addr
));
1738 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof(struct in6_addr
));
1740 th
= (struct tcphdr
*)((char *)h6
+ sizeof(struct ip6_hdr
));
1749 th
->th_sport
= sport
;
1750 th
->th_dport
= dport
;
1751 th
->th_seq
= htonl(seq
);
1752 th
->th_ack
= htonl(ack
);
1753 th
->th_off
= tlen
>> 2;
1754 th
->th_flags
= flags
;
1755 th
->th_win
= htons(win
);
1758 opt
= (char *)(th
+ 1);
1759 opt
[0] = TCPOPT_MAXSEG
;
1762 bcopy((void *)&mss
, (void *)(opt
+ 2), 2);
1769 th
->th_sum
= in_cksum(m
, len
);
1771 /* Finish the IP header */
1773 h
->ip_hl
= sizeof(*h
) >> 2;
1774 h
->ip_tos
= IPTOS_LOWDELAY
;
1775 h
->ip_len
= htons(len
);
1776 h
->ip_off
= htons(ip_mtudisc
? IP_DF
: 0);
1777 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
1780 ip_output(m
, (void *)NULL
, (void *)NULL
, 0,
1781 (void *)NULL
, (void *)NULL
);
1785 * On netbsd, pf_test and pf_test6 are always called
1788 panic("pf_send_tcp: eh != NULL");
1792 struct ether_header
*e
= (void *)ro
.ro_dst
.sa_data
;
1800 ro
.ro_dst
.sa_len
= sizeof(ro
.ro_dst
);
1801 ro
.ro_dst
.sa_family
= pseudo_AF_HDRCMPLT
;
1802 bcopy(eh
->ether_dhost
, e
->ether_shost
, ETHER_ADDR_LEN
);
1803 bcopy(eh
->ether_shost
, e
->ether_dhost
, ETHER_ADDR_LEN
);
1804 e
->ether_type
= eh
->ether_type
;
1805 ip_output(m
, (void *)NULL
, &ro
, IP_ROUTETOETHER
,
1806 (void *)NULL
, (void *)NULL
);
1807 #endif /* !__NetBSD__ */
1814 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
1815 sizeof(struct ip6_hdr
), tlen
);
1817 h6
->ip6_vfc
|= IPV6_VERSION
;
1818 h6
->ip6_hlim
= IPV6_DEFHLIM
;
1820 ip6_output(m
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
1827 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
1832 struct pf_mtag
*pf_mtag
;
1833 #endif /* __NetBSD__ */
1835 m0
= m_copy(m
, 0, M_COPYALL
);
1838 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
1840 pf_mtag
->flags
|= PF_TAG_GENERATED
;
1842 if (r
->rtableid
>= 0)
1843 pf_mtag
->rtableid
= r
->rtableid
;
1845 m0
->m_pkthdr
.pf
.flags
|= PF_TAG_GENERATED
;
1847 if (r
->rtableid
>= 0)
1848 m0
->m_pkthdr
.pf
.rtableid
= r
->rtableid
;
1849 #endif /* !__NetBSD__ */
1855 struct altq_tag
*atag
;
1857 mtag
= m_tag_get(PACKET_TAG_ALTQ_QID
, sizeof(*atag
), M_NOWAIT
);
1859 atag
= (struct altq_tag
*)(mtag
+ 1);
1861 /* add hints for ecn */
1863 atag
->hdr
= mtod(m0
, struct ip
*);
1864 m_tag_prepend(m0
, mtag
);
1867 m0
->m_pkthdr
.pf
.qid
= r
->qid
;
1868 /* add hints for ecn */
1869 m0
->m_pkthdr
.pf
.hdr
= mtod(m0
, struct ip
*);
1870 #endif /* !__NetBSD__ */
1877 icmp_error(m0
, type
, code
, 0, 0);
1882 icmp6_error(m0
, type
, code
, 0);
1889 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
1890 * If n is 0, they match if they are equal. If n is != 0, they match if they
1894 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
1895 struct pf_addr
*b
, sa_family_t af
)
1902 if ((a
->addr32
[0] & m
->addr32
[0]) ==
1903 (b
->addr32
[0] & m
->addr32
[0]))
1909 if (((a
->addr32
[0] & m
->addr32
[0]) ==
1910 (b
->addr32
[0] & m
->addr32
[0])) &&
1911 ((a
->addr32
[1] & m
->addr32
[1]) ==
1912 (b
->addr32
[1] & m
->addr32
[1])) &&
1913 ((a
->addr32
[2] & m
->addr32
[2]) ==
1914 (b
->addr32
[2] & m
->addr32
[2])) &&
1915 ((a
->addr32
[3] & m
->addr32
[3]) ==
1916 (b
->addr32
[3] & m
->addr32
[3])))
1935 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
1939 return ((p
> a1
) && (p
< a2
));
1941 return ((p
< a1
) || (p
> a2
));
1943 return ((p
>= a1
) && (p
<= a2
));
1957 return (0); /* never reached */
1961 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
1966 return (pf_match(op
, a1
, a2
, p
));
1970 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
1972 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
1974 return (pf_match(op
, a1
, a2
, u
));
1978 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
1980 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
1982 return (pf_match(op
, a1
, a2
, g
));
1986 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, int *tag
)
1990 struct pf_mtag
*pf_mtag
= pf_get_mtag(m
);
1991 if (pf_mtag
== NULL
)
1994 *tag
= pf_mtag
->tag
;
1998 *tag
= m
->m_pkthdr
.pf
.tag
;
1999 #endif /* !__NetBSD__ */
2001 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2002 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2006 pf_tag_packet(struct mbuf
*m
, int tag
, int rtableid
)
2008 if (tag
<= 0 && rtableid
< 0)
2012 if (tag
> 0 || rtableid
> 0) {
2013 struct pf_mtag
*pf_mtag
= pf_get_mtag(m
);
2014 if (pf_mtag
== NULL
)
2020 pf_mtag
->rtableid
= rtableid
;
2024 m
->m_pkthdr
.pf
.tag
= tag
;
2026 m
->m_pkthdr
.pf
.rtableid
= rtableid
;
2027 #endif /* !__NetBSD__ */
2033 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2034 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2036 struct pf_anchor_stackframe
*f
;
2038 (*r
)->anchor
->match
= 0;
2041 if (*depth
>= sizeof(pf_anchor_stack
) /
2042 sizeof(pf_anchor_stack
[0])) {
2043 printf("pf_step_into_anchor: stack overflow\n");
2044 *r
= TAILQ_NEXT(*r
, entries
);
2046 } else if (*depth
== 0 && a
!= NULL
)
2048 f
= pf_anchor_stack
+ (*depth
)++;
2051 if ((*r
)->anchor_wildcard
) {
2052 f
->parent
= &(*r
)->anchor
->children
;
2053 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2058 *rs
= &f
->child
->ruleset
;
2062 *rs
= &(*r
)->anchor
->ruleset
;
2064 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2068 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2069 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2071 struct pf_anchor_stackframe
*f
;
2077 f
= pf_anchor_stack
+ *depth
- 1;
2078 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2079 if (f
->child
->match
||
2080 (match
!= NULL
&& *match
)) {
2081 f
->r
->anchor
->match
= 1;
2084 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
2085 if (f
->child
!= NULL
) {
2086 *rs
= &f
->child
->ruleset
;
2087 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2095 if (*depth
== 0 && a
!= NULL
)
2098 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
2099 quick
= f
->r
->quick
;
2100 *r
= TAILQ_NEXT(f
->r
, entries
);
2101 } while (*r
== NULL
);
2108 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
2109 struct pf_addr
*rmask
, const struct pf_addr
*saddr
, sa_family_t af
)
2114 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2115 ((rmask
->addr32
[0] ^ 0xffffffff ) & saddr
->addr32
[0]);
2119 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2120 ((rmask
->addr32
[0] ^ 0xffffffff ) & saddr
->addr32
[0]);
2121 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
2122 ((rmask
->addr32
[1] ^ 0xffffffff ) & saddr
->addr32
[1]);
2123 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
2124 ((rmask
->addr32
[2] ^ 0xffffffff ) & saddr
->addr32
[2]);
2125 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
2126 ((rmask
->addr32
[3] ^ 0xffffffff ) & saddr
->addr32
[3]);
2132 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
2137 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
2141 if (addr
->addr32
[3] == 0xffffffff) {
2142 addr
->addr32
[3] = 0;
2143 if (addr
->addr32
[2] == 0xffffffff) {
2144 addr
->addr32
[2] = 0;
2145 if (addr
->addr32
[1] == 0xffffffff) {
2146 addr
->addr32
[1] = 0;
2148 htonl(ntohl(addr
->addr32
[0]) + 1);
2151 htonl(ntohl(addr
->addr32
[1]) + 1);
2154 htonl(ntohl(addr
->addr32
[2]) + 1);
2157 htonl(ntohl(addr
->addr32
[3]) + 1);
2163 #define mix(a,b,c) \
2165 a -= b; a -= c; a ^= (c >> 13); \
2166 b -= c; b -= a; b ^= (a << 8); \
2167 c -= a; c -= b; c ^= (b >> 13); \
2168 a -= b; a -= c; a ^= (c >> 12); \
2169 b -= c; b -= a; b ^= (a << 16); \
2170 c -= a; c -= b; c ^= (b >> 5); \
2171 a -= b; a -= c; a ^= (c >> 3); \
2172 b -= c; b -= a; b ^= (a << 10); \
2173 c -= a; c -= b; c ^= (b >> 15); \
2177 * hash function based on bridge_hash in if_bridge.c
2180 pf_hash(const struct pf_addr
*inaddr
, struct pf_addr
*hash
,
2181 struct pf_poolhashkey
*key
, sa_family_t af
)
2183 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
2188 a
+= inaddr
->addr32
[0];
2191 hash
->addr32
[0] = c
+ key
->key32
[2];
2196 a
+= inaddr
->addr32
[0];
2197 b
+= inaddr
->addr32
[2];
2199 hash
->addr32
[0] = c
;
2200 a
+= inaddr
->addr32
[1];
2201 b
+= inaddr
->addr32
[3];
2204 hash
->addr32
[1] = c
;
2205 a
+= inaddr
->addr32
[2];
2206 b
+= inaddr
->addr32
[1];
2209 hash
->addr32
[2] = c
;
2210 a
+= inaddr
->addr32
[3];
2211 b
+= inaddr
->addr32
[0];
2214 hash
->addr32
[3] = c
;
2221 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, const struct pf_addr
*saddr
,
2222 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
2224 unsigned char hash
[16];
2225 struct pf_pool
*rpool
= &r
->rpool
;
2226 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
2227 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
2228 struct pf_pooladdr
*acur
= rpool
->cur
;
2229 struct pf_src_node k
;
2231 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
2232 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
2234 PF_ACPY(&k
.addr
, saddr
, af
);
2235 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
2236 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
2240 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
2241 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
2242 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, af
)) {
2243 PF_ACPY(naddr
, &(*sn
)->raddr
, af
);
2244 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
2245 printf("pf_map_addr: src tracking maps ");
2246 pf_print_host(&k
.addr
, 0, af
);
2248 pf_print_host(naddr
, 0, af
);
2255 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
2257 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
2261 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
2262 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
2265 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
2266 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
2271 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
2272 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
2275 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
2276 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
2280 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
2281 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
2282 return (1); /* unsupported */
2284 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
2285 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
2288 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
2290 PF_ACPY(naddr
, raddr
, af
);
2292 case PF_POOL_BITMASK
:
2293 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
2295 case PF_POOL_RANDOM
:
2296 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
)) {
2300 rpool
->counter
.addr32
[0] = htonl(arc4random());
2305 if (rmask
->addr32
[3] != 0xffffffff)
2306 rpool
->counter
.addr32
[3] =
2307 htonl(arc4random());
2310 if (rmask
->addr32
[2] != 0xffffffff)
2311 rpool
->counter
.addr32
[2] =
2312 htonl(arc4random());
2315 if (rmask
->addr32
[1] != 0xffffffff)
2316 rpool
->counter
.addr32
[1] =
2317 htonl(arc4random());
2320 if (rmask
->addr32
[0] != 0xffffffff)
2321 rpool
->counter
.addr32
[0] =
2322 htonl(arc4random());
2326 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
2327 PF_ACPY(init_addr
, naddr
, af
);
2330 PF_AINC(&rpool
->counter
, af
);
2331 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
2334 case PF_POOL_SRCHASH
:
2335 pf_hash(saddr
, (struct pf_addr
*)&hash
, &rpool
->key
, af
);
2336 PF_POOLMASK(naddr
, raddr
, rmask
, (struct pf_addr
*)&hash
, af
);
2338 case PF_POOL_ROUNDROBIN
:
2339 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
2340 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
2341 &rpool
->tblidx
, &rpool
->counter
,
2342 &raddr
, &rmask
, af
))
2344 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
2345 if (!pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
2346 &rpool
->tblidx
, &rpool
->counter
,
2347 &raddr
, &rmask
, af
))
2349 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
, af
))
2353 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
2354 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
2355 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
2357 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
2358 &rpool
->tblidx
, &rpool
->counter
,
2359 &raddr
, &rmask
, af
)) {
2360 /* table contains no address of type 'af' */
2361 if (rpool
->cur
!= acur
)
2365 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
2367 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
2368 &rpool
->tblidx
, &rpool
->counter
,
2369 &raddr
, &rmask
, af
)) {
2370 /* table contains no address of type 'af' */
2371 if (rpool
->cur
!= acur
)
2376 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
2377 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
2378 PF_ACPY(&rpool
->counter
, raddr
, af
);
2382 PF_ACPY(naddr
, &rpool
->counter
, af
);
2383 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
))
2384 PF_ACPY(init_addr
, naddr
, af
);
2385 PF_AINC(&rpool
->counter
, af
);
2389 PF_ACPY(&(*sn
)->raddr
, naddr
, af
);
2391 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
2392 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
2393 printf("pf_map_addr: selected address ");
2394 pf_print_host(naddr
, 0, af
);
2402 pf_get_sport(sa_family_t af
, u_int8_t proto
, struct pf_rule
*r
,
2403 struct pf_addr
*saddr
, struct pf_addr
*daddr
, u_int16_t dport
,
2404 struct pf_addr
*naddr
, u_int16_t
*nport
, u_int16_t low
, u_int16_t high
,
2405 struct pf_src_node
**sn
)
2407 struct pf_state_key_cmp key
;
2408 struct pf_addr init_addr
;
2411 bzero(&init_addr
, sizeof(init_addr
));
2412 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
2415 if (proto
== IPPROTO_ICMP
) {
2423 PF_ACPY(&key
.ext
.addr
, daddr
, key
.af
);
2424 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af
);
2425 key
.ext
.port
= dport
;
2428 * port search; start random, step;
2429 * similar 2 portloop in in_pcbbind
2431 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
2432 proto
== IPPROTO_ICMP
)) {
2433 key
.gwy
.port
= dport
;
2434 if (pf_find_state_all(&key
, PF_EXT_GWY
, NULL
) == NULL
)
2436 } else if (low
== 0 && high
== 0) {
2437 key
.gwy
.port
= *nport
;
2438 if (pf_find_state_all(&key
, PF_EXT_GWY
, NULL
) == NULL
)
2440 } else if (low
== high
) {
2441 key
.gwy
.port
= htons(low
);
2442 if (pf_find_state_all(&key
, PF_EXT_GWY
, NULL
) == NULL
) {
2443 *nport
= htons(low
);
2455 cut
= htonl(arc4random()) % (1 + high
- low
) + low
;
2456 /* low <= cut <= high */
2457 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
2458 key
.gwy
.port
= htons(tmp
);
2459 if (pf_find_state_all(&key
, PF_EXT_GWY
, NULL
) ==
2461 *nport
= htons(tmp
);
2465 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
2466 key
.gwy
.port
= htons(tmp
);
2467 if (pf_find_state_all(&key
, PF_EXT_GWY
, NULL
) ==
2469 *nport
= htons(tmp
);
2475 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
2476 case PF_POOL_RANDOM
:
2477 case PF_POOL_ROUNDROBIN
:
2478 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
2482 case PF_POOL_SRCHASH
:
2483 case PF_POOL_BITMASK
:
2487 } while (! PF_AEQ(&init_addr
, naddr
, af
) );
2489 return (1); /* none available */
2493 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
2494 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
, u_int16_t sport
,
2495 struct pf_addr
*daddr
, u_int16_t dport
, int rs_num
)
2497 struct pf_rule
*r
, *rm
= NULL
;
2498 struct pf_ruleset
*ruleset
= NULL
;
2503 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
2504 while (r
&& rm
== NULL
) {
2505 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
2506 struct pf_addr_wrap
*xdst
= NULL
;
2508 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
2510 if (r
->rpool
.cur
!= NULL
)
2511 xdst
= &r
->rpool
.cur
->addr
;
2518 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
2519 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
2520 else if (r
->direction
&& r
->direction
!= direction
)
2521 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
2522 else if (r
->af
&& r
->af
!= pd
->af
)
2523 r
= r
->skip
[PF_SKIP_AF
].ptr
;
2524 else if (r
->proto
&& r
->proto
!= pd
->proto
)
2525 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
2526 else if (PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
2528 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_ADDR
:
2529 PF_SKIP_DST_ADDR
].ptr
;
2530 else if (src
->port_op
&& !pf_match_port(src
->port_op
,
2531 src
->port
[0], src
->port
[1], sport
))
2532 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
2533 PF_SKIP_DST_PORT
].ptr
;
2534 else if (dst
!= NULL
&&
2535 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
2536 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
2537 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
2539 r
= TAILQ_NEXT(r
, entries
);
2540 else if (dst
!= NULL
&& dst
->port_op
&&
2541 !pf_match_port(dst
->port_op
, dst
->port
[0],
2542 dst
->port
[1], dport
))
2543 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
2544 else if (r
->match_tag
&& !pf_match_tag(m
, r
, &tag
))
2545 r
= TAILQ_NEXT(r
, entries
);
2546 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
2547 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
2548 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
2549 r
= TAILQ_NEXT(r
, entries
);
2553 if (r
->rtableid
>= 0)
2554 rtableid
= r
->rtableid
;
2555 if (r
->anchor
== NULL
) {
2558 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
2562 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
2565 if (pf_tag_packet(m
, tag
, rtableid
))
2567 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
2568 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
))
2574 pf_get_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
, int direction
,
2575 struct pfi_kif
*kif
, struct pf_src_node
**sn
,
2576 struct pf_addr
*saddr
, u_int16_t sport
,
2577 struct pf_addr
*daddr
, u_int16_t dport
,
2578 struct pf_addr
*naddr
, u_int16_t
*nport
)
2580 struct pf_rule
*r
= NULL
;
2582 if (direction
== PF_OUT
) {
2583 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
2584 sport
, daddr
, dport
, PF_RULESET_BINAT
);
2586 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
2587 saddr
, sport
, daddr
, dport
, PF_RULESET_NAT
);
2589 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
2590 sport
, daddr
, dport
, PF_RULESET_RDR
);
2592 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
2593 saddr
, sport
, daddr
, dport
, PF_RULESET_BINAT
);
2597 switch (r
->action
) {
2603 if (pf_get_sport(pd
->af
, pd
->proto
, r
, saddr
,
2604 daddr
, dport
, naddr
, nport
, r
->rpool
.proxy_port
[0],
2605 r
->rpool
.proxy_port
[1], sn
)) {
2606 DPFPRINTF(PF_DEBUG_MISC
,
2607 ("pf: NAT proxy port allocation "
2609 r
->rpool
.proxy_port
[0],
2610 r
->rpool
.proxy_port
[1]));
2615 switch (direction
) {
2617 if (r
->rpool
.cur
->addr
.type
== PF_ADDR_DYNIFTL
){
2621 if (r
->rpool
.cur
->addr
.p
.dyn
->
2625 &r
->rpool
.cur
->addr
.p
.dyn
->
2627 &r
->rpool
.cur
->addr
.p
.dyn
->
2634 if (r
->rpool
.cur
->addr
.p
.dyn
->
2638 &r
->rpool
.cur
->addr
.p
.dyn
->
2640 &r
->rpool
.cur
->addr
.p
.dyn
->
2648 &r
->rpool
.cur
->addr
.v
.a
.addr
,
2649 &r
->rpool
.cur
->addr
.v
.a
.mask
,
2653 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
2657 if (r
->src
.addr
.p
.dyn
->
2661 &r
->src
.addr
.p
.dyn
->
2663 &r
->src
.addr
.p
.dyn
->
2670 if (r
->src
.addr
.p
.dyn
->
2674 &r
->src
.addr
.p
.dyn
->
2676 &r
->src
.addr
.p
.dyn
->
2684 &r
->src
.addr
.v
.a
.addr
,
2685 &r
->src
.addr
.v
.a
.mask
, daddr
,
2691 if (pf_map_addr(pd
->af
, r
, saddr
, naddr
, NULL
, sn
))
2693 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
2695 PF_POOLMASK(naddr
, naddr
,
2696 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
2699 if (r
->rpool
.proxy_port
[1]) {
2700 u_int32_t tmp_nport
;
2702 tmp_nport
= ((ntohs(dport
) -
2703 ntohs(r
->dst
.port
[0])) %
2704 (r
->rpool
.proxy_port
[1] -
2705 r
->rpool
.proxy_port
[0] + 1)) +
2706 r
->rpool
.proxy_port
[0];
2708 /* wrap around if necessary */
2709 if (tmp_nport
> 65535)
2711 *nport
= htons((u_int16_t
)tmp_nport
);
2712 } else if (r
->rpool
.proxy_port
[0])
2713 *nport
= htons(r
->rpool
.proxy_port
[0]);
2725 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
2727 struct pf_addr
*saddr
, *daddr
;
2728 u_int16_t sport
, dport
;
2729 struct inpcbtable
*tb
;
2730 struct inpcb
*inp
= NULL
;
2731 struct socket
*so
= NULL
;
2732 #if defined(__NetBSD__) && defined(INET6)
2733 struct in6pcb
*in6p
= NULL
;
2736 #endif /* __NetBSD__ && INET6 */
2740 pd
->lookup
.uid
= UID_MAX
;
2741 pd
->lookup
.gid
= GID_MAX
;
2742 pd
->lookup
.pid
= NO_PID
;
2743 switch (pd
->proto
) {
2745 if (pd
->hdr
.tcp
== NULL
)
2747 sport
= pd
->hdr
.tcp
->th_sport
;
2748 dport
= pd
->hdr
.tcp
->th_dport
;
2752 if (pd
->hdr
.udp
== NULL
)
2754 sport
= pd
->hdr
.udp
->uh_sport
;
2755 dport
= pd
->hdr
.udp
->uh_dport
;
2761 if (direction
== PF_IN
) {
2776 #define in_pcbhashlookup(tbl, saddr, sport, daddr, dport) \
2777 in_pcblookup_connect(tbl, saddr, sport, daddr, dport)
2778 #define in6_pcbhashlookup(tbl, saddr, sport, daddr, dport) \
2779 in6_pcblookup_connect(tbl, saddr, sport, daddr, dport, 0)
2780 #define in_pcblookup_listen(tbl, addr, port, zero) \
2781 in_pcblookup_bind(tbl, addr, port)
2782 #define in6_pcblookup_listen(tbl, addr, port, zero) \
2783 in6_pcblookup_bind(tbl, addr, port, zero)
2788 inp
= in_pcbhashlookup(tb
, saddr
->v4
, sport
, daddr
->v4
, dport
);
2790 inp
= in_pcblookup_listen(tb
, daddr
->v4
, dport
, 0);
2798 /*###2817 [cc] warning: assignment from incompatible pointer type%%%*/
2799 in6p
= in6_pcbhashlookup(tb
, &saddr
->v6
, sport
, &daddr
->v6
,
2802 in6p
= in6_pcblookup_listen(tb
, &daddr
->v6
, dport
, 0);
2817 so
= inp
->inp_socket
;
2822 /*###2840 [cc] error: 'struct inpcb' has no member named 'in6p_head'%%%*/
2823 so
= in6p
->in6p_socket
;
2827 pd
->lookup
.uid
= kauth_cred_geteuid(so
->so_cred
);
2828 pd
->lookup
.gid
= kauth_cred_getegid(so
->so_cred
);
2830 so
= inp
->inp_socket
;
2831 pd
->lookup
.uid
= so
->so_euid
;
2832 pd
->lookup
.gid
= so
->so_egid
;
2833 #endif /* !__NetBSD__ */
2834 pd
->lookup
.pid
= so
->so_cpid
;
2839 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
2843 u_int8_t
*opt
, optlen
;
2844 u_int8_t wscale
= 0;
2846 hlen
= th_off
<< 2; /* hlen <= sizeof(hdr) */
2847 if (hlen
<= sizeof(struct tcphdr
))
2849 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
2851 opt
= hdr
+ sizeof(struct tcphdr
);
2852 hlen
-= sizeof(struct tcphdr
);
2862 if (wscale
> TCP_MAX_WINSHIFT
)
2863 wscale
= TCP_MAX_WINSHIFT
;
2864 wscale
|= PF_WSCALE_FLAG
;
2879 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
2883 u_int8_t
*opt
, optlen
;
2884 u_int16_t mss
= tcp_mssdflt
;
2886 hlen
= th_off
<< 2; /* hlen <= sizeof(hdr) */
2887 if (hlen
<= sizeof(struct tcphdr
))
2889 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
2891 opt
= hdr
+ sizeof(struct tcphdr
);
2892 hlen
-= sizeof(struct tcphdr
);
2893 while (hlen
>= TCPOLEN_MAXSEG
) {
2901 bcopy((void *)(opt
+ 2), (void *)&mss
, 2);
2917 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
2920 struct sockaddr dst
;
2921 struct sockaddr_in dst4
;
2922 struct sockaddr_in6 dst6
;
2925 struct route
*rop
= &ro
;
2928 u_int16_t mss
= tcp_mssdflt
;
2930 hlen
= 0; /* XXXGCC -Wunitialized m68k */
2932 memset(&ro
, 0, sizeof(ro
));
2936 hlen
= sizeof(struct ip
);
2937 sockaddr_in_init(&u
.dst4
, &addr
->v4
, 0);
2938 rtcache_setdst(rop
, &u
.dst
);
2943 hlen
= sizeof(struct ip6_hdr
);
2944 sockaddr_in6_init(&u
.dst6
, &addr
->v6
, 0, 0, 0);
2945 rtcache_setdst(rop
, &u
.dst
);
2951 rtalloc_noclone(rop
, NO_CLONING
);
2952 if ((rt
= ro
->ro_rt
) != NULL
) {
2953 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof(struct tcphdr
);
2954 mss
= max(tcp_mssdflt
, mss
);
2957 if ((rt
= rtcache_init_noclone(rop
)) != NULL
) {
2958 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof(struct tcphdr
);
2959 mss
= max(tcp_mssdflt
, mss
);
2963 mss
= min(mss
, offer
);
2964 mss
= max(mss
, 64); /* sanity - at least max opt space */
2969 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
)
2971 struct pf_rule
*r
= s
->rule
.ptr
;
2974 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
2976 switch (s
->state_key
->af
) {
2979 pf_map_addr(AF_INET
, r
, saddr
, &s
->rt_addr
, NULL
,
2981 s
->rt_kif
= r
->rpool
.cur
->kif
;
2986 pf_map_addr(AF_INET6
, r
, saddr
, &s
->rt_addr
, NULL
,
2988 s
->rt_kif
= r
->rpool
.cur
->kif
;
2995 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
3000 /* list is sorted, if-bound states before floating */
3002 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
3004 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
3008 pf_detach_state(struct pf_state
*s
, int flags
)
3010 struct pf_state_key
*sk
= s
->state_key
;
3015 s
->state_key
= NULL
;
3016 TAILQ_REMOVE(&sk
->states
, s
, next
);
3017 if (--sk
->refcnt
== 0) {
3018 if (!(flags
& PF_DT_SKIP_EXTGWY
))
3019 RB_REMOVE(pf_state_tree_ext_gwy
,
3020 &pf_statetbl_ext_gwy
, sk
);
3021 if (!(flags
& PF_DT_SKIP_LANEXT
))
3022 RB_REMOVE(pf_state_tree_lan_ext
,
3023 &pf_statetbl_lan_ext
, sk
);
3024 pool_put(&pf_state_key_pl
, sk
);
3028 struct pf_state_key
*
3029 pf_alloc_state_key(struct pf_state
*s
)
3031 struct pf_state_key
*sk
;
3033 if ((sk
= pool_get(&pf_state_key_pl
, PR_NOWAIT
)) == NULL
)
3035 bzero(sk
, sizeof(*sk
));
3036 TAILQ_INIT(&sk
->states
);
3037 pf_attach_state(sk
, s
, 0);
3043 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
3044 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
3045 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
3046 struct ifqueue
*ifq
)
3048 struct pf_rule
*nr
= NULL
;
3049 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
3050 u_int16_t bport
, nport
= 0;
3051 sa_family_t af
= pd
->af
;
3052 struct pf_rule
*r
, *a
= NULL
;
3053 struct pf_ruleset
*ruleset
= NULL
;
3054 struct pf_src_node
*nsn
= NULL
;
3055 struct tcphdr
*th
= pd
->hdr
.tcp
;
3057 int rewrite
= 0, hdrlen
= 0;
3058 int tag
= -1, rtableid
= -1;
3062 u_int16_t mss
= tcp_mssdflt
;
3063 u_int16_t sport
, dport
;
3064 u_int8_t icmptype
= 0, icmpcode
= 0;
3066 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
3067 REASON_SET(&reason
, PFRES_CONGEST
);
3071 sport
= dport
= hdrlen
= 0;
3073 switch (pd
->proto
) {
3075 sport
= th
->th_sport
;
3076 dport
= th
->th_dport
;
3077 hdrlen
= sizeof(*th
);
3080 sport
= pd
->hdr
.udp
->uh_sport
;
3081 dport
= pd
->hdr
.udp
->uh_dport
;
3082 hdrlen
= sizeof(*pd
->hdr
.udp
);
3086 if (pd
->af
!= AF_INET
)
3088 sport
= dport
= pd
->hdr
.icmp
->icmp_id
;
3089 icmptype
= pd
->hdr
.icmp
->icmp_type
;
3090 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
3092 if (icmptype
== ICMP_UNREACH
||
3093 icmptype
== ICMP_SOURCEQUENCH
||
3094 icmptype
== ICMP_REDIRECT
||
3095 icmptype
== ICMP_TIMXCEED
||
3096 icmptype
== ICMP_PARAMPROB
)
3102 case IPPROTO_ICMPV6
:
3103 if (pd
->af
!= AF_INET6
)
3105 sport
= dport
= pd
->hdr
.icmp6
->icmp6_id
;
3106 hdrlen
= sizeof(*pd
->hdr
.icmp6
);
3107 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
3108 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
3110 if (icmptype
== ICMP6_DST_UNREACH
||
3111 icmptype
== ICMP6_PACKET_TOO_BIG
||
3112 icmptype
== ICMP6_TIME_EXCEEDED
||
3113 icmptype
== ICMP6_PARAM_PROB
)
3119 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
3121 if (direction
== PF_OUT
) {
3122 bport
= nport
= sport
;
3123 /* check outgoing packet for BINAT/NAT */
3124 if ((nr
= pf_get_translation(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
3125 saddr
, sport
, daddr
, dport
, &pd
->naddr
, &nport
)) != NULL
) {
3126 PF_ACPY(&pd
->baddr
, saddr
, af
);
3127 switch (pd
->proto
) {
3129 pf_change_ap(saddr
, &th
->th_sport
, pd
->ip_sum
,
3130 &th
->th_sum
, &pd
->naddr
, nport
, 0, af
);
3131 sport
= th
->th_sport
;
3135 pf_change_ap(saddr
, &pd
->hdr
.udp
->uh_sport
,
3136 pd
->ip_sum
, &pd
->hdr
.udp
->uh_sum
,
3137 &pd
->naddr
, nport
, 1, af
);
3138 sport
= pd
->hdr
.udp
->uh_sport
;
3143 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
3144 pd
->naddr
.v4
.s_addr
, 0);
3145 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
3146 pd
->hdr
.icmp
->icmp_cksum
, sport
, nport
, 0);
3147 pd
->hdr
.icmp
->icmp_id
= nport
;
3148 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
3152 case IPPROTO_ICMPV6
:
3153 pf_change_a6(saddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
3162 pf_change_a(&saddr
->v4
.s_addr
,
3163 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
3168 PF_ACPY(saddr
, &pd
->naddr
, af
);
3180 bport
= nport
= dport
;
3181 /* check incoming packet for BINAT/RDR */
3182 if ((nr
= pf_get_translation(pd
, m
, off
, PF_IN
, kif
, &nsn
,
3183 saddr
, sport
, daddr
, dport
, &pd
->naddr
, &nport
)) != NULL
) {
3184 PF_ACPY(&pd
->baddr
, daddr
, af
);
3185 switch (pd
->proto
) {
3187 pf_change_ap(daddr
, &th
->th_dport
, pd
->ip_sum
,
3188 &th
->th_sum
, &pd
->naddr
, nport
, 0, af
);
3189 dport
= th
->th_dport
;
3193 pf_change_ap(daddr
, &pd
->hdr
.udp
->uh_dport
,
3194 pd
->ip_sum
, &pd
->hdr
.udp
->uh_sum
,
3195 &pd
->naddr
, nport
, 1, af
);
3196 dport
= pd
->hdr
.udp
->uh_dport
;
3201 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
3202 pd
->naddr
.v4
.s_addr
, 0);
3206 case IPPROTO_ICMPV6
:
3207 pf_change_a6(daddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
3216 pf_change_a(&daddr
->v4
.s_addr
,
3217 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
3222 PF_ACPY(daddr
, &pd
->naddr
, af
);
3237 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3238 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3239 else if (r
->direction
&& r
->direction
!= direction
)
3240 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3241 else if (r
->af
&& r
->af
!= af
)
3242 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3243 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3244 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3245 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
3247 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
3248 /* tcp/udp only. port_op always 0 in other cases */
3249 else if (r
->src
.port_op
&& !pf_match_port(r
->src
.port_op
,
3250 r
->src
.port
[0], r
->src
.port
[1], sport
))
3251 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
3252 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
3254 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3255 /* tcp/udp only. port_op always 0 in other cases */
3256 else if (r
->dst
.port_op
&& !pf_match_port(r
->dst
.port_op
,
3257 r
->dst
.port
[0], r
->dst
.port
[1], dport
))
3258 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3259 /* icmp only. type always 0 in other cases */
3260 else if (r
->type
&& r
->type
!= icmptype
+ 1)
3261 r
= TAILQ_NEXT(r
, entries
);
3262 /* icmp only. type always 0 in other cases */
3263 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
3264 r
= TAILQ_NEXT(r
, entries
);
3265 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
3266 r
= TAILQ_NEXT(r
, entries
);
3267 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
3268 r
= TAILQ_NEXT(r
, entries
);
3269 else if (pd
->proto
== IPPROTO_TCP
&&
3270 (r
->flagset
& th
->th_flags
) != r
->flags
)
3271 r
= TAILQ_NEXT(r
, entries
);
3272 /* tcp/udp only. uid.op always 0 in other cases */
3273 else if (r
->uid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
3274 pf_socket_lookup(direction
, pd
), 1)) &&
3275 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
3277 r
= TAILQ_NEXT(r
, entries
);
3278 /* tcp/udp only. gid.op always 0 in other cases */
3279 else if (r
->gid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
3280 pf_socket_lookup(direction
, pd
), 1)) &&
3281 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
3283 r
= TAILQ_NEXT(r
, entries
);
3284 else if (r
->prob
&& r
->prob
<= arc4random())
3285 r
= TAILQ_NEXT(r
, entries
);
3286 else if (r
->match_tag
&& !pf_match_tag(m
, r
, &tag
))
3287 r
= TAILQ_NEXT(r
, entries
);
3288 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
3289 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
3290 pf_osfp_fingerprint(pd
, m
, off
, th
),
3291 r
->os_fingerprint
)))
3292 r
= TAILQ_NEXT(r
, entries
);
3296 if (r
->rtableid
>= 0)
3297 rtableid
= r
->rtableid
;
3298 if (r
->anchor
== NULL
) {
3305 r
= TAILQ_NEXT(r
, entries
);
3307 pf_step_into_anchor(&asd
, &ruleset
,
3308 PF_RULESET_FILTER
, &r
, &a
, &match
);
3310 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
3311 PF_RULESET_FILTER
, &r
, &a
, &match
))
3318 REASON_SET(&reason
, PFRES_MATCH
);
3320 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
3322 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
3323 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
->log
? r
: nr
,
3327 if ((r
->action
== PF_DROP
) &&
3328 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
3329 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
3330 (r
->rule_flag
& PFRULE_RETURN
))) {
3331 /* undo NAT changes, if they have taken place */
3333 if (direction
== PF_OUT
) {
3334 switch (pd
->proto
) {
3336 pf_change_ap(saddr
, &th
->th_sport
,
3337 pd
->ip_sum
, &th
->th_sum
,
3338 &pd
->baddr
, bport
, 0, af
);
3339 sport
= th
->th_sport
;
3344 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
3345 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
3347 sport
= pd
->hdr
.udp
->uh_sport
;
3352 case IPPROTO_ICMPV6
:
3359 pf_change_a(&saddr
->v4
.s_addr
,
3361 pd
->baddr
.v4
.s_addr
, 0);
3364 PF_ACPY(saddr
, &pd
->baddr
, af
);
3369 switch (pd
->proto
) {
3371 pf_change_ap(daddr
, &th
->th_dport
,
3372 pd
->ip_sum
, &th
->th_sum
,
3373 &pd
->baddr
, bport
, 0, af
);
3374 dport
= th
->th_dport
;
3379 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
3380 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
3382 dport
= pd
->hdr
.udp
->uh_dport
;
3387 case IPPROTO_ICMPV6
:
3394 pf_change_a(&daddr
->v4
.s_addr
,
3396 pd
->baddr
.v4
.s_addr
, 0);
3399 PF_ACPY(daddr
, &pd
->baddr
, af
);
3405 if (pd
->proto
== IPPROTO_TCP
&&
3406 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
3407 (r
->rule_flag
& PFRULE_RETURN
)) &&
3408 !(th
->th_flags
& TH_RST
)) {
3409 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
3410 struct ip
*h
= mtod(m
, struct ip
*);
3413 if (pf_check_proto_cksum(m
, direction
, off
,
3414 ntohs(h
->ip_len
) - off
, IPPROTO_TCP
, AF_INET
))
3416 if (pf_check_proto_cksum(m
, off
,
3417 ntohs(h
->ip_len
) - off
, IPPROTO_TCP
, AF_INET
))
3418 #endif /* !__NetBSD__ */
3419 REASON_SET(&reason
, PFRES_PROTCKSUM
);
3421 if (th
->th_flags
& TH_SYN
)
3423 if (th
->th_flags
& TH_FIN
)
3425 pf_send_tcp(r
, af
, pd
->dst
,
3426 pd
->src
, th
->th_dport
, th
->th_sport
,
3427 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
3428 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
3430 } else if ((af
== AF_INET
) && r
->return_icmp
)
3431 pf_send_icmp(m
, r
->return_icmp
>> 8,
3432 r
->return_icmp
& 255, af
, r
);
3433 else if ((af
== AF_INET6
) && r
->return_icmp6
)
3434 pf_send_icmp(m
, r
->return_icmp6
>> 8,
3435 r
->return_icmp6
& 255, af
, r
);
3438 if (r
->action
== PF_DROP
)
3441 if (pf_tag_packet(m
, tag
, rtableid
)) {
3442 REASON_SET(&reason
, PFRES_MEMORY
);
3446 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
3447 (pd
->flags
& PFDESC_TCP_NORM
))) {
3448 /* create new state */
3450 struct pf_state
*s
= NULL
;
3451 struct pf_state_key
*sk
= NULL
;
3452 struct pf_src_node
*sn
= NULL
;
3454 /* check maximums */
3455 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
3456 pf_status
.lcounters
[LCNT_STATES
]++;
3457 REASON_SET(&reason
, PFRES_MAXSTATES
);
3460 /* src node for filter rule */
3461 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
3462 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
3463 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
3464 REASON_SET(&reason
, PFRES_SRCLIMIT
);
3467 /* src node for translation rule */
3468 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
3469 ((direction
== PF_OUT
&&
3470 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
3471 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
3472 REASON_SET(&reason
, PFRES_SRCLIMIT
);
3475 s
= pool_get(&pf_state_pl
, PR_NOWAIT
);
3477 REASON_SET(&reason
, PFRES_MEMORY
);
3479 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
3480 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
3481 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
3482 pf_status
.src_nodes
--;
3483 pool_put(&pf_src_tree_pl
, sn
);
3485 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
3487 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
3488 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
3489 pf_status
.src_nodes
--;
3490 pool_put(&pf_src_tree_pl
, nsn
);
3493 pool_put(&pf_state_key_pl
, sk
);
3497 bzero(s
, sizeof(*s
));
3499 s
->nat_rule
.ptr
= nr
;
3501 STATE_INC_COUNTERS(s
);
3502 s
->allow_opts
= r
->allow_opts
;
3503 s
->log
= r
->log
& PF_LOG_ALL
;
3505 s
->log
|= nr
->log
& PF_LOG_ALL
;
3506 switch (pd
->proto
) {
3508 len
= pd
->tot_len
- off
- (th
->th_off
<< 2);
3509 s
->src
.seqlo
= ntohl(th
->th_seq
);
3510 s
->src
.seqhi
= s
->src
.seqlo
+ len
+ 1;
3511 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
3512 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
3513 /* Generate sequence number modulator */
3514 while ((s
->src
.seqdiff
=
3515 tcp_rndiss_next() - s
->src
.seqlo
) == 0)
3517 pf_change_a(&th
->th_seq
, &th
->th_sum
,
3518 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
3522 if (th
->th_flags
& TH_SYN
) {
3524 s
->src
.wscale
= pf_get_wscale(m
, off
,
3527 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
3528 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
3529 /* Remove scale factor from initial window */
3530 int win
= s
->src
.max_win
;
3531 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
3532 s
->src
.max_win
= (win
- 1) >>
3533 (s
->src
.wscale
& PF_WSCALE_MASK
);
3535 if (th
->th_flags
& TH_FIN
)
3539 s
->src
.state
= TCPS_SYN_SENT
;
3540 s
->dst
.state
= TCPS_CLOSED
;
3541 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
3544 s
->src
.state
= PFUDPS_SINGLE
;
3545 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
3546 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
3550 case IPPROTO_ICMPV6
:
3552 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
3555 s
->src
.state
= PFOTHERS_SINGLE
;
3556 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
3557 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
3560 s
->creation
= time_second
;
3561 s
->expire
= time_second
;
3565 s
->src_node
->states
++;
3568 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
3569 s
->nat_src_node
= nsn
;
3570 s
->nat_src_node
->states
++;
3572 if (pd
->proto
== IPPROTO_TCP
) {
3573 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
3574 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
3576 REASON_SET(&reason
, PFRES_MEMORY
);
3577 pf_src_tree_remove_state(s
);
3578 STATE_DEC_COUNTERS(s
);
3579 pool_put(&pf_state_pl
, s
);
3582 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
3583 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
3584 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
3585 /* This really shouldn't happen!!! */
3586 DPFPRINTF(PF_DEBUG_URGENT
,
3587 ("pf_normalize_tcp_stateful failed on "
3589 pf_normalize_tcp_cleanup(s
);
3590 pf_src_tree_remove_state(s
);
3591 STATE_DEC_COUNTERS(s
);
3592 pool_put(&pf_state_pl
, s
);
3597 if ((sk
= pf_alloc_state_key(s
)) == NULL
) {
3598 REASON_SET(&reason
, PFRES_MEMORY
);
3602 sk
->proto
= pd
->proto
;
3603 sk
->direction
= direction
;
3605 if (direction
== PF_OUT
) {
3606 PF_ACPY(&sk
->gwy
.addr
, saddr
, af
);
3607 PF_ACPY(&sk
->ext
.addr
, daddr
, af
);
3608 switch (pd
->proto
) {
3611 case IPPROTO_ICMPV6
:
3613 sk
->gwy
.port
= nport
;
3617 sk
->gwy
.port
= sport
;
3618 sk
->ext
.port
= dport
;
3621 PF_ACPY(&sk
->lan
.addr
, &pd
->baddr
, af
);
3622 sk
->lan
.port
= bport
;
3624 PF_ACPY(&sk
->lan
.addr
, &sk
->gwy
.addr
, af
);
3625 sk
->lan
.port
= sk
->gwy
.port
;
3628 PF_ACPY(&sk
->lan
.addr
, daddr
, af
);
3629 PF_ACPY(&sk
->ext
.addr
, saddr
, af
);
3630 switch (pd
->proto
) {
3633 case IPPROTO_ICMPV6
:
3635 sk
->lan
.port
= nport
;
3639 sk
->lan
.port
= dport
;
3640 sk
->ext
.port
= sport
;
3643 PF_ACPY(&sk
->gwy
.addr
, &pd
->baddr
, af
);
3644 sk
->gwy
.port
= bport
;
3646 PF_ACPY(&sk
->gwy
.addr
, &sk
->lan
.addr
, af
);
3647 sk
->gwy
.port
= sk
->lan
.port
;
3651 pf_set_rt_ifp(s
, saddr
); /* needs s->state_key set */
3653 if (pf_insert_state(bound_iface(r
, nr
, kif
), s
)) {
3654 if (pd
->proto
== IPPROTO_TCP
)
3655 pf_normalize_tcp_cleanup(s
);
3656 REASON_SET(&reason
, PFRES_STATEINS
);
3657 pf_src_tree_remove_state(s
);
3658 STATE_DEC_COUNTERS(s
);
3659 pool_put(&pf_state_pl
, s
);
3667 if (pd
->proto
== IPPROTO_TCP
&&
3668 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
3669 r
->keep_state
== PF_STATE_SYNPROXY
) {
3670 s
->src
.state
= PF_TCPS_PROXY_SRC
;
3672 if (direction
== PF_OUT
) {
3673 pf_change_ap(saddr
, &th
->th_sport
,
3674 pd
->ip_sum
, &th
->th_sum
, &pd
->baddr
,
3676 sport
= th
->th_sport
;
3678 pf_change_ap(daddr
, &th
->th_dport
,
3679 pd
->ip_sum
, &th
->th_sum
, &pd
->baddr
,
3681 sport
= th
->th_dport
;
3684 s
->src
.seqhi
= htonl(arc4random());
3685 /* Find mss option */
3686 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
3687 mss
= pf_calc_mss(saddr
, af
, mss
);
3688 mss
= pf_calc_mss(daddr
, af
, mss
);
3690 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
3691 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
3692 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
3693 REASON_SET(&reason
, PFRES_SYNPROXY
);
3694 return (PF_SYNPROXY_DROP
);
3698 /* copy back packet headers if we performed NAT operations */
3700 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
3706 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
3707 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
3708 struct pf_ruleset
**rsm
)
3710 struct pf_rule
*r
, *a
= NULL
;
3711 struct pf_ruleset
*ruleset
= NULL
;
3712 sa_family_t af
= pd
->af
;
3718 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
3721 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3722 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3723 else if (r
->direction
&& r
->direction
!= direction
)
3724 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3725 else if (r
->af
&& r
->af
!= af
)
3726 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3727 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3728 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3729 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
3731 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
3732 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
3734 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3735 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
3736 r
= TAILQ_NEXT(r
, entries
);
3737 else if (r
->src
.port_op
|| r
->dst
.port_op
||
3738 r
->flagset
|| r
->type
|| r
->code
||
3739 r
->os_fingerprint
!= PF_OSFP_ANY
)
3740 r
= TAILQ_NEXT(r
, entries
);
3741 else if (r
->prob
&& r
->prob
<= arc4random())
3742 r
= TAILQ_NEXT(r
, entries
);
3743 else if (r
->match_tag
&& !pf_match_tag(m
, r
, &tag
))
3744 r
= TAILQ_NEXT(r
, entries
);
3746 if (r
->anchor
== NULL
) {
3753 r
= TAILQ_NEXT(r
, entries
);
3755 pf_step_into_anchor(&asd
, &ruleset
,
3756 PF_RULESET_FILTER
, &r
, &a
, &match
);
3758 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
3759 PF_RULESET_FILTER
, &r
, &a
, &match
))
3766 REASON_SET(&reason
, PFRES_MATCH
);
3769 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
3772 if (r
->action
!= PF_PASS
)
3775 if (pf_tag_packet(m
, tag
, -1)) {
3776 REASON_SET(&reason
, PFRES_MEMORY
);
3784 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
3785 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
3788 struct pf_state_key_cmp key
;
3789 struct tcphdr
*th
= pd
->hdr
.tcp
;
3790 u_int16_t win
= ntohs(th
->th_win
);
3791 u_int32_t ack
, end
, seq
, orig_seq
;
3795 struct pf_state_peer
*src
, *dst
;
3798 key
.proto
= IPPROTO_TCP
;
3799 if (direction
== PF_IN
) {
3800 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
3801 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
3802 key
.ext
.port
= th
->th_sport
;
3803 key
.gwy
.port
= th
->th_dport
;
3805 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
3806 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
3807 key
.lan
.port
= th
->th_sport
;
3808 key
.ext
.port
= th
->th_dport
;
3813 if (direction
== (*state
)->state_key
->direction
) {
3814 src
= &(*state
)->src
;
3815 dst
= &(*state
)->dst
;
3817 src
= &(*state
)->dst
;
3818 dst
= &(*state
)->src
;
3821 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
3822 if (direction
!= (*state
)->state_key
->direction
) {
3823 REASON_SET(reason
, PFRES_SYNPROXY
);
3824 return (PF_SYNPROXY_DROP
);
3826 if (th
->th_flags
& TH_SYN
) {
3827 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
3828 REASON_SET(reason
, PFRES_SYNPROXY
);
3831 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
3832 pd
->src
, th
->th_dport
, th
->th_sport
,
3833 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
3834 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
3836 REASON_SET(reason
, PFRES_SYNPROXY
);
3837 return (PF_SYNPROXY_DROP
);
3838 } else if (!(th
->th_flags
& TH_ACK
) ||
3839 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
3840 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
3841 REASON_SET(reason
, PFRES_SYNPROXY
);
3843 } else if ((*state
)->src_node
!= NULL
&&
3844 pf_src_connlimit(state
)) {
3845 REASON_SET(reason
, PFRES_SRCLIMIT
);
3848 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
3850 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
3851 struct pf_state_host
*src
, *dst
;
3853 if (direction
== PF_OUT
) {
3854 src
= &(*state
)->state_key
->gwy
;
3855 dst
= &(*state
)->state_key
->ext
;
3857 src
= &(*state
)->state_key
->ext
;
3858 dst
= &(*state
)->state_key
->lan
;
3860 if (direction
== (*state
)->state_key
->direction
) {
3861 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
3862 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
3863 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
3864 REASON_SET(reason
, PFRES_SYNPROXY
);
3867 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
3868 if ((*state
)->dst
.seqhi
== 1)
3869 (*state
)->dst
.seqhi
= htonl(arc4random());
3870 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &src
->addr
,
3871 &dst
->addr
, src
->port
, dst
->port
,
3872 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
3873 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
3874 REASON_SET(reason
, PFRES_SYNPROXY
);
3875 return (PF_SYNPROXY_DROP
);
3876 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
3878 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
3879 REASON_SET(reason
, PFRES_SYNPROXY
);
3882 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
3883 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
3884 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
3885 pd
->src
, th
->th_dport
, th
->th_sport
,
3886 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
3887 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
3888 (*state
)->tag
, NULL
, NULL
);
3889 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &src
->addr
,
3890 &dst
->addr
, src
->port
, dst
->port
,
3891 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
3892 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
3894 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
3895 (*state
)->src
.seqlo
;
3896 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
3897 (*state
)->dst
.seqlo
;
3898 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
3899 (*state
)->dst
.max_win
;
3900 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
3901 (*state
)->src
.max_win
;
3902 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
3903 (*state
)->src
.state
= (*state
)->dst
.state
=
3905 REASON_SET(reason
, PFRES_SYNPROXY
);
3906 return (PF_SYNPROXY_DROP
);
3910 if (src
->wscale
&& dst
->wscale
&& !(th
->th_flags
& TH_SYN
)) {
3911 sws
= src
->wscale
& PF_WSCALE_MASK
;
3912 dws
= dst
->wscale
& PF_WSCALE_MASK
;
3917 * Sequence tracking algorithm from Guido van Rooij's paper:
3918 * http://www.madison-gurkha.com/publications/tcp_filtering/
3922 orig_seq
= seq
= ntohl(th
->th_seq
);
3923 if (src
->seqlo
== 0) {
3924 /* First packet from this end. Set its state */
3926 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
3927 src
->scrub
== NULL
) {
3928 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
3929 REASON_SET(reason
, PFRES_MEMORY
);
3934 /* Deferred generation of sequence number modulator */
3935 if (dst
->seqdiff
&& !src
->seqdiff
) {
3936 while ((src
->seqdiff
= tcp_rndiss_next() - seq
) == 0)
3938 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
3939 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
3941 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
3944 ack
= ntohl(th
->th_ack
);
3947 end
= seq
+ pd
->p_len
;
3948 if (th
->th_flags
& TH_SYN
) {
3950 if (dst
->wscale
& PF_WSCALE_FLAG
) {
3951 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
3953 if (src
->wscale
& PF_WSCALE_FLAG
) {
3954 /* Remove scale factor from initial
3956 sws
= src
->wscale
& PF_WSCALE_MASK
;
3957 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
3959 dws
= dst
->wscale
& PF_WSCALE_MASK
;
3961 /* fixup other window */
3962 dst
->max_win
<<= dst
->wscale
&
3964 /* in case of a retrans SYN|ACK */
3969 if (th
->th_flags
& TH_FIN
)
3973 if (src
->state
< TCPS_SYN_SENT
)
3974 src
->state
= TCPS_SYN_SENT
;
3977 * May need to slide the window (seqhi may have been set by
3978 * the crappy stack check or if we picked up the connection
3979 * after establishment)
3981 if (src
->seqhi
== 1 ||
3982 SEQ_GEQ(end
+ MAX(1, dst
->max_win
<< dws
), src
->seqhi
))
3983 src
->seqhi
= end
+ MAX(1, dst
->max_win
<< dws
);
3984 if (win
> src
->max_win
)
3988 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
3990 /* Modulate sequence numbers */
3991 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
3993 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
3996 end
= seq
+ pd
->p_len
;
3997 if (th
->th_flags
& TH_SYN
)
3999 if (th
->th_flags
& TH_FIN
)
4003 if ((th
->th_flags
& TH_ACK
) == 0) {
4004 /* Let it pass through the ack skew check */
4006 } else if ((ack
== 0 &&
4007 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
4008 /* broken tcp stacks do not set ack */
4009 (dst
->state
< TCPS_SYN_SENT
)) {
4011 * Many stacks (ours included) will set the ACK number in an
4012 * FIN|ACK if the SYN times out -- no sequence to ACK.
4018 /* Ease sequencing restrictions on no data packets */
4023 ackskew
= dst
->seqlo
- ack
;
4027 * Need to demodulate the sequence numbers in any TCP SACK options
4028 * (Selective ACK). We could optionally validate the SACK values
4029 * against the current ACK window, either forwards or backwards, but
4030 * I'm not confident that SACK has been implemented properly
4031 * everywhere. It wouldn't surprise me if several stacks accidently
4032 * SACK too far backwards of previously ACKed data. There really aren't
4033 * any security implications of bad SACKing unless the target stack
4034 * doesn't validate the option length correctly. Someone trying to
4035 * spoof into a TCP connection won't bother blindly sending SACK
4038 if (dst
->seqdiff
&& (th
->th_off
<< 2) > sizeof(struct tcphdr
)) {
4039 if (pf_modulate_sack(m
, off
, pd
, th
, dst
))
4044 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
4045 if (SEQ_GEQ(src
->seqhi
, end
) &&
4046 /* Last octet inside other's window space */
4047 SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
)) &&
4048 /* Retrans: not more than one window back */
4049 (ackskew
>= -MAXACKWINDOW
) &&
4050 /* Acking not more than one reassembled fragment backwards */
4051 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
4052 /* Acking not more than one window forward */
4053 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
4054 (orig_seq
== src
->seqlo
+ 1) || (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
4055 /* Require an exact/+1 sequence match on resets when possible */
4057 if (dst
->scrub
|| src
->scrub
) {
4058 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
4059 *state
, src
, dst
, ©back
))
4063 /* update max window */
4064 if (src
->max_win
< win
)
4066 /* synchronize sequencing */
4067 if (SEQ_GT(end
, src
->seqlo
))
4069 /* slide the window of what the other end can send */
4070 if (SEQ_GEQ(ack
+ (win
<< sws
), dst
->seqhi
))
4071 dst
->seqhi
= ack
+ MAX((win
<< sws
), 1);
4075 if (th
->th_flags
& TH_SYN
)
4076 if (src
->state
< TCPS_SYN_SENT
)
4077 src
->state
= TCPS_SYN_SENT
;
4078 if (th
->th_flags
& TH_FIN
)
4079 if (src
->state
< TCPS_CLOSING
)
4080 src
->state
= TCPS_CLOSING
;
4081 if (th
->th_flags
& TH_ACK
) {
4082 if (dst
->state
== TCPS_SYN_SENT
) {
4083 dst
->state
= TCPS_ESTABLISHED
;
4084 if (src
->state
== TCPS_ESTABLISHED
&&
4085 (*state
)->src_node
!= NULL
&&
4086 pf_src_connlimit(state
)) {
4087 REASON_SET(reason
, PFRES_SRCLIMIT
);
4090 } else if (dst
->state
== TCPS_CLOSING
)
4091 dst
->state
= TCPS_FIN_WAIT_2
;
4093 if (th
->th_flags
& TH_RST
)
4094 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
4096 /* update expire time */
4097 (*state
)->expire
= time_second
;
4098 if (src
->state
>= TCPS_FIN_WAIT_2
&&
4099 dst
->state
>= TCPS_FIN_WAIT_2
)
4100 (*state
)->timeout
= PFTM_TCP_CLOSED
;
4101 else if (src
->state
>= TCPS_CLOSING
&&
4102 dst
->state
>= TCPS_CLOSING
)
4103 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
4104 else if (src
->state
< TCPS_ESTABLISHED
||
4105 dst
->state
< TCPS_ESTABLISHED
)
4106 (*state
)->timeout
= PFTM_TCP_OPENING
;
4107 else if (src
->state
>= TCPS_CLOSING
||
4108 dst
->state
>= TCPS_CLOSING
)
4109 (*state
)->timeout
= PFTM_TCP_CLOSING
;
4111 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
4113 /* Fall through to PASS packet */
4115 } else if ((dst
->state
< TCPS_SYN_SENT
||
4116 dst
->state
>= TCPS_FIN_WAIT_2
||
4117 src
->state
>= TCPS_FIN_WAIT_2
) &&
4118 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
4119 /* Within a window forward of the originating packet */
4120 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
4121 /* Within a window backward of the originating packet */
4124 * This currently handles three situations:
4125 * 1) Stupid stacks will shotgun SYNs before their peer
4127 * 2) When PF catches an already established stream (the
4128 * firewall rebooted, the state table was flushed, routes
4130 * 3) Packets get funky immediately after the connection
4131 * closes (this should catch Solaris spurious ACK|FINs
4132 * that web servers like to spew after a close)
4134 * This must be a little more careful than the above code
4135 * since packet floods will also be caught here. We don't
4136 * update the TTL here to mitigate the damage of a packet
4137 * flood and so the same code can handle awkward establishment
4138 * and a loosened connection close.
4139 * In the establishment case, a correct peer response will
4140 * validate the connection, go through the normal state code
4141 * and keep updating the state TTL.
4144 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
4145 printf("pf: loose state match: ");
4146 pf_print_state(*state
);
4147 pf_print_flags(th
->th_flags
);
4148 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4149 "pkts=%llu:%llu\n", seq
, orig_seq
, ack
, pd
->p_len
,
4151 (unsigned long long int)(*state
)->packets
[0],
4152 (unsigned long long int)(*state
)->packets
[1]);
4155 if (dst
->scrub
|| src
->scrub
) {
4156 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
4157 *state
, src
, dst
, ©back
))
4161 /* update max window */
4162 if (src
->max_win
< win
)
4164 /* synchronize sequencing */
4165 if (SEQ_GT(end
, src
->seqlo
))
4167 /* slide the window of what the other end can send */
4168 if (SEQ_GEQ(ack
+ (win
<< sws
), dst
->seqhi
))
4169 dst
->seqhi
= ack
+ MAX((win
<< sws
), 1);
4172 * Cannot set dst->seqhi here since this could be a shotgunned
4173 * SYN and not an already established connection.
4176 if (th
->th_flags
& TH_FIN
)
4177 if (src
->state
< TCPS_CLOSING
)
4178 src
->state
= TCPS_CLOSING
;
4179 if (th
->th_flags
& TH_RST
)
4180 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
4182 /* Fall through to PASS packet */
4185 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
4186 (*state
)->src
.state
== TCPS_SYN_SENT
) {
4187 /* Send RST for state mismatches during handshake */
4188 if (!(th
->th_flags
& TH_RST
))
4189 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
4190 pd
->dst
, pd
->src
, th
->th_dport
,
4191 th
->th_sport
, ntohl(th
->th_ack
), 0,
4193 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
4194 pd
->eh
, kif
->pfik_ifp
);
4198 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
4199 printf("pf: BAD state: ");
4200 pf_print_state(*state
);
4201 pf_print_flags(th
->th_flags
);
4202 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
4203 "pkts=%llu:%llu dir=%s,%s\n",
4204 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
4205 (unsigned long long int)(*state
)->packets
[0],
4206 (unsigned long long int)(*state
)->packets
[1],
4207 direction
== PF_IN
? "in" : "out",
4208 direction
== (*state
)->state_key
->direction
?
4210 printf("pf: State failure on: %c %c %c %c | %c %c\n",
4211 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
4212 SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
)) ?
4214 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
4215 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
4216 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
4217 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
4219 REASON_SET(reason
, PFRES_BADSTATE
);
4223 /* Any packets which have gotten here are to be passed */
4225 /* translate source/destination address, if necessary */
4226 if (STATE_TRANSLATE((*state
)->state_key
)) {
4227 if (direction
== PF_OUT
)
4228 pf_change_ap(pd
->src
, &th
->th_sport
, pd
->ip_sum
,
4229 &th
->th_sum
, &(*state
)->state_key
->gwy
.addr
,
4230 (*state
)->state_key
->gwy
.port
, 0, pd
->af
);
4232 pf_change_ap(pd
->dst
, &th
->th_dport
, pd
->ip_sum
,
4233 &th
->th_sum
, &(*state
)->state_key
->lan
.addr
,
4234 (*state
)->state_key
->lan
.port
, 0, pd
->af
);
4235 m_copyback(m
, off
, sizeof(*th
), th
);
4236 } else if (copyback
) {
4237 /* Copyback sequence modulation or stateful scrub changes */
4238 m_copyback(m
, off
, sizeof(*th
), th
);
4245 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
4246 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
)
4248 struct pf_state_peer
*src
, *dst
;
4249 struct pf_state_key_cmp key
;
4250 struct udphdr
*uh
= pd
->hdr
.udp
;
4253 key
.proto
= IPPROTO_UDP
;
4254 if (direction
== PF_IN
) {
4255 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
4256 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
4257 key
.ext
.port
= uh
->uh_sport
;
4258 key
.gwy
.port
= uh
->uh_dport
;
4260 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
4261 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
4262 key
.lan
.port
= uh
->uh_sport
;
4263 key
.ext
.port
= uh
->uh_dport
;
4268 if (direction
== (*state
)->state_key
->direction
) {
4269 src
= &(*state
)->src
;
4270 dst
= &(*state
)->dst
;
4272 src
= &(*state
)->dst
;
4273 dst
= &(*state
)->src
;
4277 if (src
->state
< PFUDPS_SINGLE
)
4278 src
->state
= PFUDPS_SINGLE
;
4279 if (dst
->state
== PFUDPS_SINGLE
)
4280 dst
->state
= PFUDPS_MULTIPLE
;
4282 /* update expire time */
4283 (*state
)->expire
= time_second
;
4284 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
4285 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
4287 (*state
)->timeout
= PFTM_UDP_SINGLE
;
4289 /* translate source/destination address, if necessary */
4290 if (STATE_TRANSLATE((*state
)->state_key
)) {
4291 if (direction
== PF_OUT
)
4292 pf_change_ap(pd
->src
, &uh
->uh_sport
, pd
->ip_sum
,
4293 &uh
->uh_sum
, &(*state
)->state_key
->gwy
.addr
,
4294 (*state
)->state_key
->gwy
.port
, 1, pd
->af
);
4296 pf_change_ap(pd
->dst
, &uh
->uh_dport
, pd
->ip_sum
,
4297 &uh
->uh_sum
, &(*state
)->state_key
->lan
.addr
,
4298 (*state
)->state_key
->lan
.port
, 1, pd
->af
);
4299 m_copyback(m
, off
, sizeof(*uh
), uh
);
4306 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
4307 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
4310 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4311 u_int16_t icmpid
= 0, *icmpsum
;
4314 struct pf_state_key_cmp key
;
4316 icmpsum
= NULL
; /* XXXGCC -Wunitialized m68k */
4317 icmptype
= 0; /* XXXGCC -Wunitialized m68k */
4319 switch (pd
->proto
) {
4322 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4323 icmpid
= pd
->hdr
.icmp
->icmp_id
;
4324 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
4326 if (icmptype
== ICMP_UNREACH
||
4327 icmptype
== ICMP_SOURCEQUENCH
||
4328 icmptype
== ICMP_REDIRECT
||
4329 icmptype
== ICMP_TIMXCEED
||
4330 icmptype
== ICMP_PARAMPROB
)
4335 case IPPROTO_ICMPV6
:
4336 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4337 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
4338 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
4340 if (icmptype
== ICMP6_DST_UNREACH
||
4341 icmptype
== ICMP6_PACKET_TOO_BIG
||
4342 icmptype
== ICMP6_TIME_EXCEEDED
||
4343 icmptype
== ICMP6_PARAM_PROB
)
4352 * ICMP query/reply message not related to a TCP/UDP packet.
4353 * Search for an ICMP state.
4356 key
.proto
= pd
->proto
;
4357 if (direction
== PF_IN
) {
4358 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
4359 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
4361 key
.gwy
.port
= icmpid
;
4363 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
4364 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
4365 key
.lan
.port
= icmpid
;
4371 (*state
)->expire
= time_second
;
4372 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
4374 /* translate source/destination address, if necessary */
4375 if (STATE_TRANSLATE((*state
)->state_key
)) {
4376 if (direction
== PF_OUT
) {
4380 pf_change_a(&saddr
->v4
.s_addr
,
4382 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
4383 pd
->hdr
.icmp
->icmp_cksum
=
4385 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
4386 (*state
)->state_key
->gwy
.port
, 0);
4387 pd
->hdr
.icmp
->icmp_id
=
4388 (*state
)->state_key
->gwy
.port
;
4389 m_copyback(m
, off
, ICMP_MINLEN
,
4396 &pd
->hdr
.icmp6
->icmp6_cksum
,
4397 &(*state
)->state_key
->gwy
.addr
, 0);
4399 sizeof(struct icmp6_hdr
),
4408 pf_change_a(&daddr
->v4
.s_addr
,
4410 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
4411 pd
->hdr
.icmp
->icmp_cksum
=
4413 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
4414 (*state
)->state_key
->lan
.port
, 0);
4415 pd
->hdr
.icmp
->icmp_id
=
4416 (*state
)->state_key
->lan
.port
;
4417 m_copyback(m
, off
, ICMP_MINLEN
,
4424 &pd
->hdr
.icmp6
->icmp6_cksum
,
4425 &(*state
)->state_key
->lan
.addr
, 0);
4427 sizeof(struct icmp6_hdr
),
4439 * ICMP error message in response to a TCP/UDP packet.
4440 * Extract the inner TCP/UDP header and search for that state.
4443 struct pf_pdesc pd2
;
4448 struct ip6_hdr h2_6
;
4454 memset(&pd2
, 0, sizeof pd2
); /* XXX gcc */
4460 /* offset of h2 in mbuf chain */
4461 ipoff2
= off
+ ICMP_MINLEN
;
4463 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof(h2
),
4464 NULL
, reason
, pd2
.af
)) {
4465 DPFPRINTF(PF_DEBUG_MISC
,
4466 ("pf: ICMP error message too short "
4471 * ICMP error messages don't refer to non-first
4474 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
4475 REASON_SET(reason
, PFRES_FRAG
);
4479 /* offset of protocol header that follows h2 */
4480 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
4482 pd2
.proto
= h2
.ip_p
;
4483 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
4484 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
4485 pd2
.ip_sum
= &h2
.ip_sum
;
4490 ipoff2
= off
+ sizeof(struct icmp6_hdr
);
4492 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof(h2_6
),
4493 NULL
, reason
, pd2
.af
)) {
4494 DPFPRINTF(PF_DEBUG_MISC
,
4495 ("pf: ICMP error message too short "
4499 pd2
.proto
= h2_6
.ip6_nxt
;
4500 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
4501 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
4503 off2
= ipoff2
+ sizeof(h2_6
);
4505 switch (pd2
.proto
) {
4506 case IPPROTO_FRAGMENT
:
4508 * ICMPv6 error messages for
4509 * non-first fragments
4511 REASON_SET(reason
, PFRES_FRAG
);
4514 case IPPROTO_HOPOPTS
:
4515 case IPPROTO_ROUTING
:
4516 case IPPROTO_DSTOPTS
: {
4517 /* get next header and header length */
4518 struct ip6_ext opt6
;
4520 if (!pf_pull_hdr(m
, off2
, &opt6
,
4521 sizeof(opt6
), NULL
, reason
,
4523 DPFPRINTF(PF_DEBUG_MISC
,
4524 ("pf: ICMPv6 short opt\n"));
4527 if (pd2
.proto
== IPPROTO_AH
)
4528 off2
+= (opt6
.ip6e_len
+ 2) * 4;
4530 off2
+= (opt6
.ip6e_len
+ 1) * 8;
4531 pd2
.proto
= opt6
.ip6e_nxt
;
4532 /* goto the next header */
4539 } while (!terminal
);
4544 switch (pd2
.proto
) {
4548 struct pf_state_peer
*src
, *dst
;
4553 * Only the first 8 bytes of the TCP header can be
4554 * expected. Don't access any TCP header fields after
4555 * th_seq, an ackskew test is not possible.
4557 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
4559 DPFPRINTF(PF_DEBUG_MISC
,
4560 ("pf: ICMP error message too short "
4566 key
.proto
= IPPROTO_TCP
;
4567 if (direction
== PF_IN
) {
4568 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
4569 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
4570 key
.ext
.port
= th
.th_dport
;
4571 key
.gwy
.port
= th
.th_sport
;
4573 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
4574 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
4575 key
.lan
.port
= th
.th_dport
;
4576 key
.ext
.port
= th
.th_sport
;
4581 if (direction
== (*state
)->state_key
->direction
) {
4582 src
= &(*state
)->dst
;
4583 dst
= &(*state
)->src
;
4585 src
= &(*state
)->src
;
4586 dst
= &(*state
)->dst
;
4589 if (src
->wscale
&& dst
->wscale
)
4590 dws
= dst
->wscale
& PF_WSCALE_MASK
;
4594 /* Demodulate sequence number */
4595 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
4597 pf_change_a(&th
.th_seq
, icmpsum
,
4602 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
4603 !SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
))) {
4604 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
4605 printf("pf: BAD ICMP %d:%d ",
4606 icmptype
, pd
->hdr
.icmp
->icmp_code
);
4607 pf_print_host(pd
->src
, 0, pd
->af
);
4609 pf_print_host(pd
->dst
, 0, pd
->af
);
4611 pf_print_state(*state
);
4612 printf(" seq=%u\n", seq
);
4614 REASON_SET(reason
, PFRES_BADSTATE
);
4618 if (STATE_TRANSLATE((*state
)->state_key
)) {
4619 if (direction
== PF_IN
) {
4620 pf_change_icmp(pd2
.src
, &th
.th_sport
,
4621 daddr
, &(*state
)->state_key
->lan
.addr
,
4622 (*state
)->state_key
->lan
.port
, NULL
,
4623 pd2
.ip_sum
, icmpsum
,
4624 pd
->ip_sum
, 0, pd2
.af
);
4626 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
4627 saddr
, &(*state
)->state_key
->gwy
.addr
,
4628 (*state
)->state_key
->gwy
.port
, NULL
,
4629 pd2
.ip_sum
, icmpsum
,
4630 pd
->ip_sum
, 0, pd2
.af
);
4639 m_copyback(m
, off
, ICMP_MINLEN
,
4641 m_copyback(m
, ipoff2
, sizeof(h2
),
4648 sizeof(struct icmp6_hdr
),
4650 m_copyback(m
, ipoff2
, sizeof(h2_6
),
4655 m_copyback(m
, off2
, 8, &th
);
4664 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof(uh
),
4665 NULL
, reason
, pd2
.af
)) {
4666 DPFPRINTF(PF_DEBUG_MISC
,
4667 ("pf: ICMP error message too short "
4673 key
.proto
= IPPROTO_UDP
;
4674 if (direction
== PF_IN
) {
4675 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
4676 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
4677 key
.ext
.port
= uh
.uh_dport
;
4678 key
.gwy
.port
= uh
.uh_sport
;
4680 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
4681 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
4682 key
.lan
.port
= uh
.uh_dport
;
4683 key
.ext
.port
= uh
.uh_sport
;
4688 if (STATE_TRANSLATE((*state
)->state_key
)) {
4689 if (direction
== PF_IN
) {
4690 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
4692 &(*state
)->state_key
->lan
.addr
,
4693 (*state
)->state_key
->lan
.port
,
4695 pd2
.ip_sum
, icmpsum
,
4696 pd
->ip_sum
, 1, pd2
.af
);
4698 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
4700 &(*state
)->state_key
->gwy
.addr
,
4701 (*state
)->state_key
->gwy
.port
, &uh
.uh_sum
,
4702 pd2
.ip_sum
, icmpsum
,
4703 pd
->ip_sum
, 1, pd2
.af
);
4708 m_copyback(m
, off
, ICMP_MINLEN
,
4710 m_copyback(m
, ipoff2
, sizeof(h2
), &h2
);
4716 sizeof(struct icmp6_hdr
),
4718 m_copyback(m
, ipoff2
, sizeof(h2_6
),
4723 m_copyback(m
, off2
, sizeof(uh
), &uh
);
4730 case IPPROTO_ICMP
: {
4733 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
4734 NULL
, reason
, pd2
.af
)) {
4735 DPFPRINTF(PF_DEBUG_MISC
,
4736 ("pf: ICMP error message too short i"
4742 key
.proto
= IPPROTO_ICMP
;
4743 if (direction
== PF_IN
) {
4744 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
4745 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
4747 key
.gwy
.port
= iih
.icmp_id
;
4749 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
4750 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
4751 key
.lan
.port
= iih
.icmp_id
;
4757 if (STATE_TRANSLATE((*state
)->state_key
)) {
4758 if (direction
== PF_IN
) {
4759 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
4761 &(*state
)->state_key
->lan
.addr
,
4762 (*state
)->state_key
->lan
.port
, NULL
,
4763 pd2
.ip_sum
, icmpsum
,
4764 pd
->ip_sum
, 0, AF_INET
);
4766 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
4768 &(*state
)->state_key
->gwy
.addr
,
4769 (*state
)->state_key
->gwy
.port
, NULL
,
4770 pd2
.ip_sum
, icmpsum
,
4771 pd
->ip_sum
, 0, AF_INET
);
4773 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
4774 m_copyback(m
, ipoff2
, sizeof(h2
), &h2
);
4775 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
4783 case IPPROTO_ICMPV6
: {
4784 struct icmp6_hdr iih
;
4786 if (!pf_pull_hdr(m
, off2
, &iih
,
4787 sizeof(struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
4788 DPFPRINTF(PF_DEBUG_MISC
,
4789 ("pf: ICMP error message too short "
4795 key
.proto
= IPPROTO_ICMPV6
;
4796 if (direction
== PF_IN
) {
4797 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
4798 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
4800 key
.gwy
.port
= iih
.icmp6_id
;
4802 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
4803 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
4804 key
.lan
.port
= iih
.icmp6_id
;
4810 if (STATE_TRANSLATE((*state
)->state_key
)) {
4811 if (direction
== PF_IN
) {
4812 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
4814 &(*state
)->state_key
->lan
.addr
,
4815 (*state
)->state_key
->lan
.port
, NULL
,
4816 pd2
.ip_sum
, icmpsum
,
4817 pd
->ip_sum
, 0, AF_INET6
);
4819 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
4820 saddr
, &(*state
)->state_key
->gwy
.addr
,
4821 (*state
)->state_key
->gwy
.port
, NULL
,
4822 pd2
.ip_sum
, icmpsum
,
4823 pd
->ip_sum
, 0, AF_INET6
);
4825 m_copyback(m
, off
, sizeof(struct icmp6_hdr
),
4827 m_copyback(m
, ipoff2
, sizeof(h2_6
), &h2_6
);
4828 m_copyback(m
, off2
, sizeof(struct icmp6_hdr
),
4838 key
.proto
= pd2
.proto
;
4839 if (direction
== PF_IN
) {
4840 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
4841 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
4845 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
4846 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
4853 if (STATE_TRANSLATE((*state
)->state_key
)) {
4854 if (direction
== PF_IN
) {
4855 pf_change_icmp(pd2
.src
, NULL
,
4857 &(*state
)->state_key
->lan
.addr
,
4859 pd2
.ip_sum
, icmpsum
,
4860 pd
->ip_sum
, 0, pd2
.af
);
4862 pf_change_icmp(pd2
.dst
, NULL
,
4864 &(*state
)->state_key
->gwy
.addr
,
4866 pd2
.ip_sum
, icmpsum
,
4867 pd
->ip_sum
, 0, pd2
.af
);
4872 m_copyback(m
, off
, ICMP_MINLEN
,
4874 m_copyback(m
, ipoff2
, sizeof(h2
), &h2
);
4880 sizeof(struct icmp6_hdr
),
4882 m_copyback(m
, ipoff2
, sizeof(h2_6
),
4897 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
4898 struct pf_pdesc
*pd
)
4900 struct pf_state_peer
*src
, *dst
;
4901 struct pf_state_key_cmp key
;
4904 key
.proto
= pd
->proto
;
4905 if (direction
== PF_IN
) {
4906 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
4907 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
4911 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
4912 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
4919 if (direction
== (*state
)->state_key
->direction
) {
4920 src
= &(*state
)->src
;
4921 dst
= &(*state
)->dst
;
4923 src
= &(*state
)->dst
;
4924 dst
= &(*state
)->src
;
4928 if (src
->state
< PFOTHERS_SINGLE
)
4929 src
->state
= PFOTHERS_SINGLE
;
4930 if (dst
->state
== PFOTHERS_SINGLE
)
4931 dst
->state
= PFOTHERS_MULTIPLE
;
4933 /* update expire time */
4934 (*state
)->expire
= time_second
;
4935 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
4936 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
4938 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
4940 /* translate source/destination address, if necessary */
4941 if (STATE_TRANSLATE((*state
)->state_key
)) {
4942 if (direction
== PF_OUT
)
4946 pf_change_a(&pd
->src
->v4
.s_addr
,
4948 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
4955 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
4963 pf_change_a(&pd
->dst
->v4
.s_addr
,
4965 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
4972 &(*state
)->state_key
->lan
.addr
, pd
->af
);
4982 * ipoff and off are measured from the start of the mbuf chain.
4983 * h must be at "ipoff" on the mbuf chain.
4986 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
4987 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
4992 struct ip
*h
= mtod(m
, struct ip
*);
4993 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
4997 ACTION_SET(actionp
, PF_PASS
);
4999 ACTION_SET(actionp
, PF_DROP
);
5000 REASON_SET(reasonp
, PFRES_FRAG
);
5004 if (m
->m_pkthdr
.len
< off
+ len
||
5005 ntohs(h
->ip_len
) < off
+ len
) {
5006 ACTION_SET(actionp
, PF_DROP
);
5007 REASON_SET(reasonp
, PFRES_SHORT
);
5015 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
5017 if (m
->m_pkthdr
.len
< off
+ len
||
5018 (ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
)) <
5019 (unsigned)(off
+ len
)) {
5020 ACTION_SET(actionp
, PF_DROP
);
5021 REASON_SET(reasonp
, PFRES_SHORT
);
5028 m_copydata(m
, off
, len
, p
);
5033 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
5037 struct sockaddr dst
;
5038 struct sockaddr_in dst4
;
5039 struct sockaddr_in6 dst6
;
5044 bzero(&ro
, sizeof(ro
));
5047 sockaddr_in_init(&u
.dst4
, &addr
->v4
, 0);
5051 sockaddr_in6_init(&u
.dst6
, &addr
->v6
, 0, 0, 0);
5057 rtcache_setdst(&ro
, &u
.dst
);
5059 ret
= rtcache_init(&ro
) != NULL
? 1 : 0;
5063 #else /* !__NetBSD__ */
5064 struct sockaddr_in
*dst
;
5067 extern int ipmultipath
;
5069 extern int ip6_multipath
;
5070 struct sockaddr_in6
*dst6
;
5071 struct route_in6 ro
;
5075 struct radix_node
*rn
;
5080 bzero(&ro
, sizeof(ro
));
5083 dst
= satosin(&ro
.ro_dst
);
5084 dst
->sin_family
= AF_INET
;
5085 dst
->sin_len
= sizeof(*dst
);
5086 dst
->sin_addr
= addr
->v4
;
5092 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
5093 dst6
->sin6_family
= AF_INET6
;
5094 dst6
->sin6_len
= sizeof(*dst6
);
5095 dst6
->sin6_addr
= addr
->v6
;
5104 /* Skip checks for ipsec interfaces */
5105 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
5108 rtalloc_noclone((struct route
*)&ro
, NO_CLONING
);
5110 if (ro
.ro_rt
!= NULL
) {
5111 /* No interface given, this is a no-route check */
5115 if (kif
->pfik_ifp
== NULL
) {
5120 /* Perform uRPF check if passed input interface */
5122 rn
= (struct radix_node
*)ro
.ro_rt
;
5124 rt
= (struct rtentry
*)rn
;
5125 if (rt
->rt_ifp
->if_type
== IFT_CARP
)
5126 ifp
= rt
->rt_ifp
->if_carpdev
;
5130 if (kif
->pfik_ifp
== ifp
)
5132 rn
= rn_mpath_next(rn
);
5133 } while (check_mpath
== 1 && rn
!= NULL
&& ret
== 0);
5137 if (ro
.ro_rt
!= NULL
)
5140 #endif /* !__NetBSD__ */
5144 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
5147 /* NetBSD doesn't have route labels. */
5151 struct sockaddr_in
*dst
;
5153 struct sockaddr_in6
*dst6
;
5154 struct route_in6 ro
;
5160 bzero(&ro
, sizeof(ro
));
5163 dst
= satosin(&ro
.ro_dst
);
5164 dst
->sin_family
= AF_INET
;
5165 dst
->sin_len
= sizeof(*dst
);
5166 dst
->sin_addr
= addr
->v4
;
5170 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
5171 dst6
->sin6_family
= AF_INET6
;
5172 dst6
->sin6_len
= sizeof(*dst6
);
5173 dst6
->sin6_addr
= addr
->v6
;
5180 rtalloc_noclone((struct route
*)&ro
, NO_CLONING
);
5182 if (ro
.ro_rt
!= NULL
) {
5183 if (ro
.ro_rt
->rt_labelid
== aw
->v
.rtlabel
)
5189 #endif /* !__NetBSD__ */
5194 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
5195 struct pf_state
*s
, struct pf_pdesc
*pd
)
5197 struct mbuf
*m0
, *m1
;
5198 struct route iproute
;
5199 struct route
*ro
= NULL
;
5200 const struct sockaddr
*dst
;
5202 struct sockaddr dst
;
5203 struct sockaddr_in dst4
;
5206 struct ifnet
*ifp
= NULL
;
5207 struct pf_addr naddr
;
5208 struct pf_src_node
*sn
= NULL
;
5214 struct pf_mtag
*pf_mtag
;
5215 #endif /* __NetBSD__ */
5217 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
5218 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
5219 panic("pf_route: invalid parameters");
5222 if ((pf_mtag
= pf_get_mtag(*m
)) == NULL
) {
5227 if (pf_mtag
->routed
++ > 3) {
5233 if ((*m
)->m_pkthdr
.pf
.routed
++ > 3) {
5238 #endif /* !__NetBSD__ */
5240 if (r
->rt
== PF_DUPTO
) {
5241 if ((m0
= m_dup(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
5244 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
5249 if (m0
->m_len
< sizeof(struct ip
)) {
5250 DPFPRINTF(PF_DEBUG_URGENT
,
5251 ("pf_route: m0->m_len < sizeof(struct ip)\n"));
5255 ip
= mtod(m0
, struct ip
*);
5258 memset(ro
, 0, sizeof(*ro
));
5259 sockaddr_in_init(&u
.dst4
, &ip
->ip_dst
, 0);
5261 rtcache_setdst(ro
, dst
);
5263 if (r
->rt
== PF_FASTROUTE
) {
5266 rt
= rtcache_init(ro
);
5269 ip_statinc(IP_STAT_NOROUTE
);
5276 if (rt
->rt_flags
& RTF_GATEWAY
)
5277 dst
= rt
->rt_gateway
;
5279 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
5280 DPFPRINTF(PF_DEBUG_URGENT
,
5281 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
5285 pf_map_addr(AF_INET
, r
,
5286 (const struct pf_addr
*)&ip
->ip_src
,
5288 if (!PF_AZERO(&naddr
, AF_INET
))
5289 u
.dst4
.sin_addr
.s_addr
= naddr
.v4
.s_addr
;
5290 ifp
= r
->rpool
.cur
->kif
?
5291 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
5293 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
5294 u
.dst4
.sin_addr
.s_addr
= s
->rt_addr
.v4
.s_addr
;
5295 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
5302 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
) != PF_PASS
)
5304 else if (m0
== NULL
)
5306 if (m0
->m_len
< sizeof(struct ip
)) {
5307 DPFPRINTF(PF_DEBUG_URGENT
,
5308 ("pf_route: m0->m_len < sizeof(struct ip)\n"));
5311 ip
= mtod(m0
, struct ip
*);
5314 /* Copied from ip_output. */
5317 * If deferred crypto processing is needed, check that the
5318 * interface supports it.
5320 if ((mtag
= m_tag_find(m0
, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED
, NULL
))
5321 != NULL
&& (ifp
->if_capabilities
& IFCAP_IPSEC
) == 0) {
5322 /* Notify IPsec to do its own crypto. */
5323 ipsp_skipcrypto_unmark((struct tdb_ident
*)(mtag
+ 1));
5328 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
5330 if (m0
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
)) {
5331 in_delayed_cksum(m0
);
5332 m0
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
);
5335 if (m0
->m_pkthdr
.csum_flags
& M_TCPV4_CSUM_OUT
) {
5336 if (!(ifp
->if_capabilities
& IFCAP_CSUM_TCPv4
) ||
5337 ifp
->if_bridge
!= NULL
) {
5338 in_delayed_cksum(m0
);
5339 m0
->m_pkthdr
.csum_flags
&= ~M_TCPV4_CSUM_OUT
; /* Clear */
5341 } else if (m0
->m_pkthdr
.csum_flags
& M_UDPV4_CSUM_OUT
) {
5342 if (!(ifp
->if_capabilities
& IFCAP_CSUM_UDPv4
) ||
5343 ifp
->if_bridge
!= NULL
) {
5344 in_delayed_cksum(m0
);
5345 m0
->m_pkthdr
.csum_flags
&= ~M_UDPV4_CSUM_OUT
; /* Clear */
5348 #endif /* !__NetBSD__ */
5350 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
) {
5353 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
5355 m0
->m_pkthdr
.csum_flags
&= ~M_CSUM_IPv4
;
5357 if ((ifp
->if_capabilities
& IFCAP_CSUM_IPv4
) &&
5358 ifp
->if_bridge
== NULL
) {
5359 m0
->m_pkthdr
.csum_flags
|= M_IPV4_CSUM_OUT
;
5360 ipstat
.ips_outhwcsum
++;
5363 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
5365 /* Update relevant hardware checksum stats for TCP/UDP */
5366 if (m0
->m_pkthdr
.csum_flags
& M_TCPV4_CSUM_OUT
)
5367 tcpstat
.tcps_outhwcsum
++;
5368 else if (m0
->m_pkthdr
.csum_flags
& M_UDPV4_CSUM_OUT
)
5369 udpstat
.udps_outhwcsum
++;
5370 #endif /* !__NetBSD__ */
5371 error
= (*ifp
->if_output
)(ifp
, m0
, dst
, NULL
);
5376 * Too large for interface; fragment if possible.
5377 * Must be able to put at least 8 bytes per fragment.
5379 if (ip
->ip_off
& htons(IP_DF
)) {
5380 ip_statinc(IP_STAT_CANTFRAG
);
5381 if (r
->rt
!= PF_DUPTO
) {
5382 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
5390 /* Make ip_fragment re-compute checksums. */
5391 if (IN_NEED_CHECKSUM(ifp
, M_CSUM_IPv4
)) {
5392 m0
->m_pkthdr
.csum_flags
|= M_CSUM_IPv4
;
5394 #endif /* __NetBSD__ */
5396 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
);
5402 for (m0
= m1
; m0
; m0
= m1
) {
5406 error
= (*ifp
->if_output
)(ifp
, m0
, dst
, NULL
);
5412 ip_statinc(IP_STAT_FRAGMENTED
);
5415 if (r
->rt
!= PF_DUPTO
)
5429 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
5430 struct pf_state
*s
, struct pf_pdesc
*pd
)
5433 struct sockaddr_in6 dst
;
5434 struct ip6_hdr
*ip6
;
5435 struct ifnet
*ifp
= NULL
;
5436 struct pf_addr naddr
;
5437 struct pf_src_node
*sn
= NULL
;
5440 struct pf_mtag
*pf_mtag
;
5441 #endif /* __NetBSD__ */
5443 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
5444 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
5445 panic("pf_route6: invalid parameters");
5448 if ((pf_mtag
= pf_get_mtag(*m
)) == NULL
) {
5453 if (pf_mtag
->routed
++ > 3) {
5459 if ((*m
)->m_pkthdr
.pf
.routed
++ > 3) {
5464 #endif /* !__NetBSD__ */
5466 if (r
->rt
== PF_DUPTO
) {
5467 if ((m0
= m_dup(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
5470 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
5475 if (m0
->m_len
< sizeof(struct ip6_hdr
)) {
5476 DPFPRINTF(PF_DEBUG_URGENT
,
5477 ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
5480 ip6
= mtod(m0
, struct ip6_hdr
*);
5482 dst
.sin6_family
= AF_INET6
;
5483 dst
.sin6_len
= sizeof(dst
);
5484 dst
.sin6_addr
= ip6
->ip6_dst
;
5486 /* Cheat. XXX why only in the v6 case??? */
5487 if (r
->rt
== PF_FASTROUTE
) {
5489 pf_mtag
->flags
|= PF_TAG_GENERATED
;
5491 m0
->m_pkthdr
.pf
.flags
|= PF_TAG_GENERATED
;
5492 #endif /* !__NetBSD__ */
5493 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
5497 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
5498 DPFPRINTF(PF_DEBUG_URGENT
,
5499 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
5503 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
5505 if (!PF_AZERO(&naddr
, AF_INET6
))
5506 PF_ACPY((struct pf_addr
*)&dst
.sin6_addr
,
5508 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
5510 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
5511 PF_ACPY((struct pf_addr
*)&dst
.sin6_addr
,
5512 &s
->rt_addr
, AF_INET6
);
5513 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
5519 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
) != PF_PASS
)
5521 else if (m0
== NULL
)
5523 if (m0
->m_len
< sizeof(struct ip6_hdr
)) {
5524 DPFPRINTF(PF_DEBUG_URGENT
,
5525 ("pf_route6: m0->m_len < sizeof(struct ip6_hdr)\n"));
5528 ip6
= mtod(m0
, struct ip6_hdr
*);
5532 * If the packet is too large for the outgoing interface,
5533 * send back an icmp6 error.
5535 if (IN6_IS_SCOPE_EMBEDDABLE(&dst
.sin6_addr
))
5536 dst
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
5537 if ((u_long
)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
5538 error
= nd6_output(ifp
, ifp
, m0
, &dst
, NULL
);
5540 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
5541 if (r
->rt
!= PF_DUPTO
)
5542 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
5548 if (r
->rt
!= PF_DUPTO
)
5560 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
5561 * off is the offset where the protocol header starts
5562 * len is the total length of protocol header plus payload
5563 * returns 0 when the checksum is valid, otherwise returns 1.
5567 pf_check_proto_cksum(struct mbuf
*m
, int direction
, int off
, int len
,
5568 u_int8_t p
, sa_family_t af
)
5571 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
5573 #endif /* !__NetBSD__ */
5576 u_int16_t flag_ok
, flag_bad
;
5577 #endif /* !__NetBSD__ */
5583 flag_ok
= M_TCP_CSUM_IN_OK
;
5584 flag_bad
= M_TCP_CSUM_IN_BAD
;
5587 flag_ok
= M_UDP_CSUM_IN_OK
;
5588 flag_bad
= M_UDP_CSUM_IN_BAD
;
5592 case IPPROTO_ICMPV6
:
5594 flag_ok
= flag_bad
= 0;
5599 if (m
->m_pkthdr
.csum_flags
& flag_ok
)
5601 if (m
->m_pkthdr
.csum_flags
& flag_bad
)
5603 #endif /* !__NetBSD__ */
5604 if (off
< sizeof(struct ip
) || len
< sizeof(struct udphdr
))
5606 if (m
->m_pkthdr
.len
< off
+ len
)
5609 if (direction
== PF_IN
) {
5612 struct tcphdr th
; /* XXX */
5615 m_copydata(m
, off
, sizeof(th
), &th
); /* XXX */
5616 thlen
= th
.th_off
<< 2;
5617 return tcp_input_checksum(af
, m
, &th
, off
,
5618 thlen
, len
- thlen
) != 0;
5622 struct udphdr uh
; /* XXX */
5624 m_copydata(m
, off
, sizeof(uh
), &uh
); /* XXX */
5625 return udp_input_checksum(af
, m
, &uh
, off
, len
) != 0;
5629 #endif /* __NetBSD__ */
5633 if (p
== IPPROTO_ICMP
) {
5638 sum
= in_cksum(m
, len
);
5642 if (m
->m_len
< sizeof(struct ip
))
5644 sum
= in4_cksum(m
, p
, off
, len
);
5650 if (m
->m_len
< sizeof(struct ip6_hdr
))
5652 sum
= in6_cksum(m
, p
, off
, len
);
5660 m
->m_pkthdr
.csum_flags
|= flag_bad
;
5661 #endif /* !__NetBSD__ */
5664 tcp_statinc(TCP_STAT_RCVBADSUM
);
5667 udp_statinc(UDP_STAT_BADSUM
);
5670 icmp_statinc(ICMP_STAT_CHECKSUM
);
5673 case IPPROTO_ICMPV6
:
5674 icmp6_statinc(ICMP6_STAT_CHECKSUM
);
5681 m
->m_pkthdr
.csum_flags
|= flag_ok
;
5682 #endif /* !__NetBSD__ */
5688 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
5689 struct ether_header
*eh
)
5691 struct pfi_kif
*kif
;
5692 u_short action
, reason
= 0, log
= 0;
5693 struct mbuf
*m
= *m0
;
5694 struct ip
*h
= NULL
;
5695 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
5696 struct pf_state
*s
= NULL
;
5697 struct pf_state_key
*sk
= NULL
;
5698 struct pf_ruleset
*ruleset
= NULL
;
5700 int off
, dirndx
, pqid
= 0;
5702 struct pf_mtag
*pf_mtag
= NULL
; /* XXX gcc */
5703 #endif /* __NetBSD__ */
5705 if (!pf_status
.running
)
5708 memset(&pd
, 0, sizeof(pd
));
5709 if (ifp
->if_type
== IFT_CARP
&& ifp
->if_carpdev
)
5710 kif
= (struct pfi_kif
*)ifp
->if_carpdev
->if_pf_kif
;
5712 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
5715 DPFPRINTF(PF_DEBUG_URGENT
,
5716 ("pf_test: kif == NULL, if_xname %s\n", ifp
->if_xname
));
5719 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
5723 if ((m
->m_flags
& M_PKTHDR
) == 0)
5724 panic("non-M_PKTHDR is passed to pf_test");
5725 #endif /* DIAGNOSTIC */
5727 if (m
->m_pkthdr
.len
< (int)sizeof(*h
)) {
5729 REASON_SET(&reason
, PFRES_SHORT
);
5735 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
5736 DPFPRINTF(PF_DEBUG_URGENT
,
5737 ("pf_test: pf_get_mtag returned NULL\n"));
5740 if (pf_mtag
->flags
& PF_TAG_GENERATED
)
5743 if (m
->m_pkthdr
.pf
.flags
& PF_TAG_GENERATED
)
5745 #endif /* !__NetBSD__ */
5747 /* We do IP header normalization and packet reassembly here */
5748 if (pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
) != PF_PASS
) {
5752 m
= *m0
; /* pf_normalize messes with m0 */
5753 h
= mtod(m
, struct ip
*);
5755 off
= h
->ip_hl
<< 2;
5756 if (off
< (int)sizeof(*h
)) {
5758 REASON_SET(&reason
, PFRES_SHORT
);
5763 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
5764 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
5765 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
5766 pd
.ip_sum
= &h
->ip_sum
;
5770 pd
.tot_len
= ntohs(h
->ip_len
);
5773 /* handle fragments that didn't get reassembled by normalization */
5774 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
5775 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
5786 if (!pf_pull_hdr(m
, off
, &th
, sizeof(th
),
5787 &action
, &reason
, AF_INET
)) {
5788 log
= action
!= PF_PASS
;
5791 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
5792 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
5794 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
5795 if (action
== PF_DROP
)
5797 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
5799 if (action
== PF_PASS
) {
5801 pfsync_update_state(s
);
5802 #endif /* NPFSYNC */
5806 } else if (s
== NULL
)
5807 action
= pf_test_rule(&r
, &s
, dir
, kif
,
5808 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
5816 if (!pf_pull_hdr(m
, off
, &uh
, sizeof(uh
),
5817 &action
, &reason
, AF_INET
)) {
5818 log
= action
!= PF_PASS
;
5821 if (uh
.uh_dport
== 0 ||
5822 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
5823 ntohs(uh
.uh_ulen
) < sizeof(struct udphdr
)) {
5825 REASON_SET(&reason
, PFRES_SHORT
);
5828 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
);
5829 if (action
== PF_PASS
) {
5831 pfsync_update_state(s
);
5832 #endif /* NPFSYNC */
5836 } else if (s
== NULL
)
5837 action
= pf_test_rule(&r
, &s
, dir
, kif
,
5838 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
5842 case IPPROTO_ICMP
: {
5846 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
5847 &action
, &reason
, AF_INET
)) {
5848 log
= action
!= PF_PASS
;
5851 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
5853 if (action
== PF_PASS
) {
5855 pfsync_update_state(s
);
5856 #endif /* NPFSYNC */
5860 } else if (s
== NULL
)
5861 action
= pf_test_rule(&r
, &s
, dir
, kif
,
5862 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
5867 case IPPROTO_ICMPV6
: {
5869 DPFPRINTF(PF_DEBUG_MISC
,
5870 ("pf: dropping IPv4 packet with ICMPv6 payload\n"));
5876 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
5877 if (action
== PF_PASS
) {
5879 pfsync_update_state(s
);
5880 #endif /* NPFSYNC */
5884 } else if (s
== NULL
)
5885 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
5886 &pd
, &a
, &ruleset
, &ipintrq
);
5891 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
5892 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
5894 REASON_SET(&reason
, PFRES_IPOPTIONS
);
5896 DPFPRINTF(PF_DEBUG_MISC
,
5897 ("pf: dropping packet with ip options\n"));
5900 if ((s
&& s
->tag
) || r
->rtableid
)
5901 pf_tag_packet(m
, s
? s
->tag
: 0, r
->rtableid
);
5904 if (action
== PF_PASS
&& r
->qid
) {
5907 struct altq_tag
*atag
;
5909 mtag
= m_tag_get(PACKET_TAG_ALTQ_QID
, sizeof(*atag
), M_NOWAIT
);
5911 atag
= (struct altq_tag
*)(mtag
+ 1);
5912 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
5913 atag
->qid
= r
->pqid
;
5916 /* add hints for ecn */
5919 m_tag_prepend(m
, mtag
);
5922 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
5923 m
->m_pkthdr
.pf
.qid
= r
->pqid
;
5925 m
->m_pkthdr
.pf
.qid
= r
->qid
;
5926 /* add hints for ecn */
5927 m
->m_pkthdr
.pf
.hdr
= h
;
5928 #endif /* !__NetBSD__ */
5933 * connections redirected to loopback should not match sockets
5934 * bound specifically to loopback due to security implications,
5935 * see tcp_input() and in_pcblookup_listen().
5937 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
5938 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
5939 (s
->nat_rule
.ptr
->action
== PF_RDR
||
5940 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
5941 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
5943 pf_mtag
->flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
5945 m
->m_pkthdr
.pf
.flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
5946 #endif /* !__NetBSD__ */
5951 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
5952 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
5953 lr
= s
->nat_rule
.ptr
;
5956 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
5960 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
5961 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
5963 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
5964 dirndx
= (dir
== PF_OUT
);
5965 r
->packets
[dirndx
]++;
5966 r
->bytes
[dirndx
] += pd
.tot_len
;
5968 a
->packets
[dirndx
]++;
5969 a
->bytes
[dirndx
] += pd
.tot_len
;
5973 if (s
->nat_rule
.ptr
!= NULL
) {
5974 s
->nat_rule
.ptr
->packets
[dirndx
]++;
5975 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
5977 if (s
->src_node
!= NULL
) {
5978 s
->src_node
->packets
[dirndx
]++;
5979 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
5981 if (s
->nat_src_node
!= NULL
) {
5982 s
->nat_src_node
->packets
[dirndx
]++;
5983 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
5985 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
5986 s
->packets
[dirndx
]++;
5987 s
->bytes
[dirndx
] += pd
.tot_len
;
5990 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
5994 * XXX: we need to make sure that the addresses
5995 * passed to pfr_update_stats() are the same than
5996 * the addresses used during matching (pfr_match)
5998 if (r
== &pf_default_rule
) {
6000 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
6001 &pd
.baddr
: &pd
.naddr
;
6003 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
6004 &pd
.naddr
: &pd
.baddr
;
6005 if (x
== &pd
.baddr
|| s
== NULL
) {
6006 /* we need to change the address */
6013 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
6014 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
6015 sk
->direction
== dir
) ?
6016 pd
.src
: pd
.dst
, pd
.af
,
6017 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
6019 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
6020 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
6021 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
6022 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
6027 if (action
== PF_SYNPROXY_DROP
) {
6032 /* pf_route can free the mbuf causing *m0 to become NULL */
6033 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
6041 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
6042 struct ether_header
*eh
)
6044 struct pfi_kif
*kif
;
6045 u_short action
, reason
= 0, log
= 0;
6046 struct mbuf
*m
= *m0
, *n
= NULL
;
6047 struct ip6_hdr
*h
= NULL
; /* XXX gcc */
6048 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
6049 struct pf_state
*s
= NULL
;
6050 struct pf_state_key
*sk
= NULL
;
6051 struct pf_ruleset
*ruleset
= NULL
;
6053 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
6055 struct pf_mtag
*pf_mtag
= NULL
; /* XXX gcc */
6056 #endif /* __NetBSD__ */
6058 if (!pf_status
.running
)
6061 memset(&pd
, 0, sizeof(pd
));
6062 if (ifp
->if_type
== IFT_CARP
&& ifp
->if_carpdev
)
6063 kif
= (struct pfi_kif
*)ifp
->if_carpdev
->if_pf_kif
;
6065 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
6068 DPFPRINTF(PF_DEBUG_URGENT
,
6069 ("pf_test6: kif == NULL, if_xname %s\n", ifp
->if_xname
));
6072 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
6076 if ((m
->m_flags
& M_PKTHDR
) == 0)
6077 panic("non-M_PKTHDR is passed to pf_test6");
6078 #endif /* DIAGNOSTIC */
6080 if (m
->m_pkthdr
.len
< (int)sizeof(*h
)) {
6082 REASON_SET(&reason
, PFRES_SHORT
);
6088 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
6089 DPFPRINTF(PF_DEBUG_URGENT
,
6090 ("pf_test6: pf_get_mtag returned NULL\n"));
6093 if (pf_mtag
->flags
& PF_TAG_GENERATED
)
6096 if (m
->m_pkthdr
.pf
.flags
& PF_TAG_GENERATED
)
6098 #endif /* !__NetBSD__ */
6100 /* We do IP header normalization and packet reassembly here */
6101 if (pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
) != PF_PASS
) {
6105 m
= *m0
; /* pf_normalize messes with m0 */
6106 h
= mtod(m
, struct ip6_hdr
*);
6110 * we do not support jumbogram yet. if we keep going, zero ip6_plen
6111 * will do something bad, so drop the packet for now.
6113 if (htons(h
->ip6_plen
) == 0) {
6115 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
6120 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
6121 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
6122 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
6126 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
6129 off
= ((char *)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
6130 pd
.proto
= h
->ip6_nxt
;
6133 case IPPROTO_FRAGMENT
:
6134 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
6136 if (action
== PF_DROP
)
6137 REASON_SET(&reason
, PFRES_FRAG
);
6139 case IPPROTO_ROUTING
: {
6140 struct ip6_rthdr rthdr
;
6143 DPFPRINTF(PF_DEBUG_MISC
,
6144 ("pf: IPv6 more than one rthdr\n"));
6146 REASON_SET(&reason
, PFRES_IPOPTIONS
);
6150 if (!pf_pull_hdr(m
, off
, &rthdr
, sizeof(rthdr
), NULL
,
6152 DPFPRINTF(PF_DEBUG_MISC
,
6153 ("pf: IPv6 short rthdr\n"));
6155 REASON_SET(&reason
, PFRES_SHORT
);
6159 if (rthdr
.ip6r_type
== IPV6_RTHDR_TYPE_0
) {
6160 DPFPRINTF(PF_DEBUG_MISC
,
6161 ("pf: IPv6 rthdr0\n"));
6163 REASON_SET(&reason
, PFRES_IPOPTIONS
);
6170 case IPPROTO_HOPOPTS
:
6171 case IPPROTO_DSTOPTS
: {
6172 /* get next header and header length */
6173 struct ip6_ext opt6
;
6175 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
6176 NULL
, &reason
, pd
.af
)) {
6177 DPFPRINTF(PF_DEBUG_MISC
,
6178 ("pf: IPv6 short opt\n"));
6183 if (pd
.proto
== IPPROTO_AH
)
6184 off
+= (opt6
.ip6e_len
+ 2) * 4;
6186 off
+= (opt6
.ip6e_len
+ 1) * 8;
6187 pd
.proto
= opt6
.ip6e_nxt
;
6188 /* goto the next header */
6195 } while (!terminal
);
6197 /* if there's no routing header, use unmodified mbuf for checksumming */
6207 if (!pf_pull_hdr(m
, off
, &th
, sizeof(th
),
6208 &action
, &reason
, AF_INET6
)) {
6209 log
= action
!= PF_PASS
;
6212 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
6213 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
6214 if (action
== PF_DROP
)
6216 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
6218 if (action
== PF_PASS
) {
6220 pfsync_update_state(s
);
6221 #endif /* NPFSYNC */
6225 } else if (s
== NULL
)
6226 action
= pf_test_rule(&r
, &s
, dir
, kif
,
6227 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
6235 if (!pf_pull_hdr(m
, off
, &uh
, sizeof(uh
),
6236 &action
, &reason
, AF_INET6
)) {
6237 log
= action
!= PF_PASS
;
6240 if (uh
.uh_dport
== 0 ||
6241 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
6242 ntohs(uh
.uh_ulen
) < sizeof(struct udphdr
)) {
6244 REASON_SET(&reason
, PFRES_SHORT
);
6247 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
);
6248 if (action
== PF_PASS
) {
6250 pfsync_update_state(s
);
6251 #endif /* NPFSYNC */
6255 } else if (s
== NULL
)
6256 action
= pf_test_rule(&r
, &s
, dir
, kif
,
6257 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
6262 case IPPROTO_ICMP
: {
6264 DPFPRINTF(PF_DEBUG_MISC
,
6265 ("pf: dropping IPv6 packet with ICMPv4 payload\n"));
6270 case IPPROTO_ICMPV6
: {
6271 struct icmp6_hdr ih
;
6274 if (!pf_pull_hdr(m
, off
, &ih
, sizeof(ih
),
6275 &action
, &reason
, AF_INET6
)) {
6276 log
= action
!= PF_PASS
;
6279 action
= pf_test_state_icmp(&s
, dir
, kif
,
6280 m
, off
, h
, &pd
, &reason
);
6281 if (action
== PF_PASS
) {
6283 pfsync_update_state(s
);
6284 #endif /* NPFSYNC */
6288 } else if (s
== NULL
)
6289 action
= pf_test_rule(&r
, &s
, dir
, kif
,
6290 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
6295 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
6296 if (action
== PF_PASS
) {
6298 pfsync_update_state(s
);
6299 #endif /* NPFSYNC */
6303 } else if (s
== NULL
)
6304 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
6305 &pd
, &a
, &ruleset
, &ip6intrq
);
6315 /* handle dangerous IPv6 extension headers. */
6316 if (action
== PF_PASS
&& rh_cnt
&&
6317 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
6319 REASON_SET(&reason
, PFRES_IPOPTIONS
);
6321 DPFPRINTF(PF_DEBUG_MISC
,
6322 ("pf: dropping packet with dangerous v6 headers\n"));
6325 if ((s
&& s
->tag
) || r
->rtableid
)
6326 pf_tag_packet(m
, s
? s
->tag
: 0, r
->rtableid
);
6329 if (action
== PF_PASS
&& r
->qid
) {
6332 struct altq_tag
*atag
;
6334 mtag
= m_tag_get(PACKET_TAG_ALTQ_QID
, sizeof(*atag
), M_NOWAIT
);
6336 atag
= (struct altq_tag
*)(mtag
+ 1);
6337 if (pd
.tos
& IPTOS_LOWDELAY
)
6338 atag
->qid
= r
->pqid
;
6341 /* add hints for ecn */
6342 atag
->af
= AF_INET6
;
6344 m_tag_prepend(m
, mtag
);
6347 if (pd
.tos
& IPTOS_LOWDELAY
)
6348 m
->m_pkthdr
.pf
.qid
= r
->pqid
;
6350 m
->m_pkthdr
.pf
.qid
= r
->qid
;
6351 /* add hints for ecn */
6352 m
->m_pkthdr
.pf
.hdr
= h
;
6353 #endif /* !__NetBSD__ */
6357 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
6358 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
6359 (s
->nat_rule
.ptr
->action
== PF_RDR
||
6360 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
6361 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
6363 pf_mtag
->flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
6365 m
->m_pkthdr
.pf
.flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
6366 #endif /* !__NetBSD__ */
6371 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
6372 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
6373 lr
= s
->nat_rule
.ptr
;
6376 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
6380 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
6381 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
6383 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
6384 dirndx
= (dir
== PF_OUT
);
6385 r
->packets
[dirndx
]++;
6386 r
->bytes
[dirndx
] += pd
.tot_len
;
6388 a
->packets
[dirndx
]++;
6389 a
->bytes
[dirndx
] += pd
.tot_len
;
6393 if (s
->nat_rule
.ptr
!= NULL
) {
6394 s
->nat_rule
.ptr
->packets
[dirndx
]++;
6395 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
6397 if (s
->src_node
!= NULL
) {
6398 s
->src_node
->packets
[dirndx
]++;
6399 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
6401 if (s
->nat_src_node
!= NULL
) {
6402 s
->nat_src_node
->packets
[dirndx
]++;
6403 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
6405 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
6406 s
->packets
[dirndx
]++;
6407 s
->bytes
[dirndx
] += pd
.tot_len
;
6410 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
6414 * XXX: we need to make sure that the addresses
6415 * passed to pfr_update_stats() are the same than
6416 * the addresses used during matching (pfr_match)
6418 if (r
== &pf_default_rule
) {
6420 x
= (s
== NULL
|| sk
->direction
== dir
) ?
6421 &pd
.baddr
: &pd
.naddr
;
6423 x
= (s
== NULL
|| sk
->direction
== dir
) ?
6424 &pd
.naddr
: &pd
.baddr
;
6426 if (x
== &pd
.baddr
|| s
== NULL
) {
6433 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
6434 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
6435 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
6436 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
6438 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
6439 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
6440 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
6441 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
6446 if (action
== PF_SYNPROXY_DROP
) {
6451 /* pf_route6 can free the mbuf causing *m0 to become NULL */
6452 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
6459 pf_check_congestion(struct ifqueue
*ifq
)
6464 if (ifq
->ifq_congestion
)
6468 #endif /* !__NetBSD__ */