2 * Copyright (c) 2007-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $apfw: git commit 6602420f2f101b74305cd78f7cd9e0c8fdedae97 $ */
30 /* $OpenBSD: pf.c,v 1.567 2008/02/20 23:40:13 henning Exp $ */
33 * Copyright (c) 2001 Daniel Hartmeier
34 * Copyright (c) 2002,2003 Henning Brauer
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * - Redistributions in binary form must reproduce the above
44 * copyright notice, this list of conditions and the following
45 * disclaimer in the documentation and/or other materials provided
46 * with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
61 * Effort sponsored in part by the Defense Advanced Research Projects
62 * Agency (DARPA) and Air Force Research Laboratory, Air Force
63 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
67 #include <machine/endian.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/filio.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/kernel.h>
77 #include <sys/random.h>
78 #include <sys/mcache.h>
79 #include <sys/protosw.h>
81 #include <libkern/crypto/md5.h>
82 #include <libkern/libkern.h>
84 #include <mach/thread_act.h>
87 #include <net/if_types.h>
89 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_seq.h>
99 #include <netinet/udp.h>
100 #include <netinet/ip_icmp.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/tcp_timer.h>
103 #include <netinet/tcp_var.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/udp_var.h>
106 #include <netinet/icmp_var.h>
107 #include <net/if_ether.h>
108 #include <net/ethernet.h>
109 #include <net/flowhash.h>
110 #include <net/pfvar.h>
111 #include <net/if_pflog.h>
114 #include <net/if_pfsync.h>
118 #include <netinet/ip6.h>
119 #include <netinet6/in6_pcb.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet/icmp6.h>
122 #include <netinet6/nd6.h>
126 #include <netinet/ip_dummynet.h>
127 #endif /* DUMMYNET */
130 * For RandomULong(), to get a 32 bits random value
131 * Note that random() returns a 31 bits value, see rdar://11159750
133 #include <dev/random/randomdev.h>
135 #define DPFPRINTF(n, x) (pf_status.debug >= (n) ? printf x : ((void)0))
138 * On Mac OS X, the rtableid value is treated as the interface scope
139 * value that is equivalent to the interface index used for scoped
140 * routing. A valid scope value is anything but IFSCOPE_NONE (0),
141 * as per definition of ifindex which is a positive, non-zero number.
142 * The other BSDs treat a negative rtableid value as invalid, hence
143 * the test against INT_MAX to handle userland apps which initialize
144 * the field with a negative number.
146 #define PF_RTABLEID_IS_VALID(r) \
147 ((r) > IFSCOPE_NONE && (r) <= INT_MAX)
152 decl_lck_mtx_data(,pf_lock_data
);
153 decl_lck_rw_data(,pf_perim_lock_data
);
154 lck_mtx_t
*pf_lock
= &pf_lock_data
;
155 lck_rw_t
*pf_perim_lock
= &pf_perim_lock_data
;
158 struct pf_state_tree_lan_ext pf_statetbl_lan_ext
;
159 struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy
;
161 struct pf_palist pf_pabuf
;
162 struct pf_status pf_status
;
165 struct pf_altqqueue pf_altqs
[2];
166 struct pf_altqqueue
*pf_altqs_active
;
167 struct pf_altqqueue
*pf_altqs_inactive
;
168 u_int32_t ticket_altqs_active
;
169 u_int32_t ticket_altqs_inactive
;
170 int altqs_inactive_open
;
172 u_int32_t ticket_pabuf
;
174 static MD5_CTX pf_tcp_secret_ctx
;
175 static u_char pf_tcp_secret
[16];
176 static int pf_tcp_secret_init
;
177 static int pf_tcp_iss_off
;
179 static struct pf_anchor_stackframe
{
180 struct pf_ruleset
*rs
;
182 struct pf_anchor_node
*parent
;
183 struct pf_anchor
*child
;
184 } pf_anchor_stack
[64];
186 struct pool pf_src_tree_pl
, pf_rule_pl
, pf_pooladdr_pl
;
187 struct pool pf_state_pl
, pf_state_key_pl
;
189 struct pool pf_altq_pl
;
192 typedef void (*hook_fn_t
)(void *);
195 TAILQ_ENTRY(hook_desc
) hd_list
;
200 #define HOOK_REMOVE 0x01
201 #define HOOK_FREE 0x02
202 #define HOOK_ABORT 0x04
204 static void *hook_establish(struct hook_desc_head
*, int,
206 static void hook_runloop(struct hook_desc_head
*, int flags
);
208 struct pool pf_app_state_pl
;
209 static void pf_print_addr(struct pf_addr
*addr
, sa_family_t af
);
210 static void pf_print_sk_host(struct pf_state_host
*, u_int8_t
, int,
213 static void pf_print_host(struct pf_addr
*, u_int16_t
, u_int8_t
);
215 static void pf_init_threshold(struct pf_threshold
*, u_int32_t
,
217 static void pf_add_threshold(struct pf_threshold
*);
218 static int pf_check_threshold(struct pf_threshold
*);
220 static void pf_change_ap(int, struct mbuf
*, struct pf_addr
*,
221 u_int16_t
*, u_int16_t
*, u_int16_t
*,
222 struct pf_addr
*, u_int16_t
, u_int8_t
, sa_family_t
);
223 static int pf_modulate_sack(struct mbuf
*, int, struct pf_pdesc
*,
224 struct tcphdr
*, struct pf_state_peer
*);
226 static void pf_change_a6(struct pf_addr
*, u_int16_t
*,
227 struct pf_addr
*, u_int8_t
);
229 static void pf_change_icmp(struct pf_addr
*, u_int16_t
*,
230 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
231 u_int16_t
*, u_int16_t
*, u_int16_t
*,
232 u_int16_t
*, u_int8_t
, sa_family_t
);
233 static void pf_send_tcp(const struct pf_rule
*, sa_family_t
,
234 const struct pf_addr
*, const struct pf_addr
*,
235 u_int16_t
, u_int16_t
, u_int32_t
, u_int32_t
,
236 u_int8_t
, u_int16_t
, u_int16_t
, u_int8_t
, int,
237 u_int16_t
, struct ether_header
*, struct ifnet
*);
238 static void pf_send_icmp(struct mbuf
*, u_int8_t
, u_int8_t
,
239 sa_family_t
, struct pf_rule
*);
240 static struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
241 int, int, struct pfi_kif
*, struct pf_addr
*,
242 union pf_state_xport
*, struct pf_addr
*,
243 union pf_state_xport
*, int);
244 static struct pf_rule
*pf_get_translation_aux(struct pf_pdesc
*,
245 struct mbuf
*, int, int, struct pfi_kif
*,
246 struct pf_src_node
**, struct pf_addr
*,
247 union pf_state_xport
*, struct pf_addr
*,
248 union pf_state_xport
*, struct pf_addr
*,
249 union pf_state_xport
*);
250 static void pf_attach_state(struct pf_state_key
*,
251 struct pf_state
*, int);
252 static void pf_detach_state(struct pf_state
*, int);
253 static u_int32_t
pf_tcp_iss(struct pf_pdesc
*);
254 static int pf_test_rule(struct pf_rule
**, struct pf_state
**,
255 int, struct pfi_kif
*, struct mbuf
*, int,
256 void *, struct pf_pdesc
*, struct pf_rule
**,
257 struct pf_ruleset
**, struct ifqueue
*);
259 static int pf_test_dummynet(struct pf_rule
**, int,
260 struct pfi_kif
*, struct mbuf
**,
261 struct pf_pdesc
*, struct ip_fw_args
*);
262 #endif /* DUMMYNET */
263 static int pf_test_fragment(struct pf_rule
**, int,
264 struct pfi_kif
*, struct mbuf
*, void *,
265 struct pf_pdesc
*, struct pf_rule
**,
266 struct pf_ruleset
**);
267 static int pf_test_state_tcp(struct pf_state
**, int,
268 struct pfi_kif
*, struct mbuf
*, int,
269 void *, struct pf_pdesc
*, u_short
*);
270 static int pf_test_state_udp(struct pf_state
**, int,
271 struct pfi_kif
*, struct mbuf
*, int,
272 void *, struct pf_pdesc
*, u_short
*);
273 static int pf_test_state_icmp(struct pf_state
**, int,
274 struct pfi_kif
*, struct mbuf
*, int,
275 void *, struct pf_pdesc
*, u_short
*);
276 static int pf_test_state_other(struct pf_state
**, int,
277 struct pfi_kif
*, struct pf_pdesc
*);
278 static int pf_match_tag(struct mbuf
*, struct pf_rule
*,
279 struct pf_mtag
*, int *);
280 static void pf_hash(struct pf_addr
*, struct pf_addr
*,
281 struct pf_poolhashkey
*, sa_family_t
);
282 static int pf_map_addr(u_int8_t
, struct pf_rule
*,
283 struct pf_addr
*, struct pf_addr
*,
284 struct pf_addr
*, struct pf_src_node
**);
285 static int pf_get_sport(struct pf_pdesc
*, struct pfi_kif
*,
286 struct pf_rule
*, struct pf_addr
*,
287 union pf_state_xport
*, struct pf_addr
*,
288 union pf_state_xport
*, struct pf_addr
*,
289 union pf_state_xport
*, struct pf_src_node
**);
290 static void pf_route(struct mbuf
**, struct pf_rule
*, int,
291 struct ifnet
*, struct pf_state
*,
294 static void pf_route6(struct mbuf
**, struct pf_rule
*, int,
295 struct ifnet
*, struct pf_state
*,
298 static u_int8_t
pf_get_wscale(struct mbuf
*, int, u_int16_t
,
300 static u_int16_t
pf_get_mss(struct mbuf
*, int, u_int16_t
,
302 static u_int16_t
pf_calc_mss(struct pf_addr
*, sa_family_t
,
304 static void pf_set_rt_ifp(struct pf_state
*,
306 static int pf_check_proto_cksum(struct mbuf
*, int, int,
307 u_int8_t
, sa_family_t
);
308 static int pf_addr_wrap_neq(struct pf_addr_wrap
*,
309 struct pf_addr_wrap
*);
310 static struct pf_state
*pf_find_state(struct pfi_kif
*,
311 struct pf_state_key_cmp
*, u_int
);
312 static int pf_src_connlimit(struct pf_state
**);
313 static void pf_stateins_err(const char *, struct pf_state
*,
315 static int pf_check_congestion(struct ifqueue
*);
318 static const char *pf_pptp_ctrl_type_name(u_int16_t code
);
320 static void pf_pptp_handler(struct pf_state
*, int, int,
321 struct pf_pdesc
*, struct pfi_kif
*);
322 static void pf_pptp_unlink(struct pf_state
*);
323 static void pf_grev1_unlink(struct pf_state
*);
324 static int pf_test_state_grev1(struct pf_state
**, int,
325 struct pfi_kif
*, int, struct pf_pdesc
*);
326 static int pf_ike_compare(struct pf_app_state
*,
327 struct pf_app_state
*);
328 static int pf_test_state_esp(struct pf_state
**, int,
329 struct pfi_kif
*, int, struct pf_pdesc
*);
331 extern struct pool pfr_ktable_pl
;
332 extern struct pool pfr_kentry_pl
;
333 extern int path_mtu_discovery
;
335 struct pf_pool_limit pf_pool_limits
[PF_LIMIT_MAX
] = {
336 { &pf_state_pl
, PFSTATE_HIWAT
},
337 { &pf_app_state_pl
, PFAPPSTATE_HIWAT
},
338 { &pf_src_tree_pl
, PFSNODE_HIWAT
},
339 { &pf_frent_pl
, PFFRAG_FRENT_HIWAT
},
340 { &pfr_ktable_pl
, PFR_KTABLE_HIWAT
},
341 { &pfr_kentry_pl
, PFR_KENTRY_HIWAT
},
345 pf_lazy_makewritable(struct pf_pdesc
*pd
, struct mbuf
*m
, int len
)
353 if (m_makewritable(&m
, 0, len
, M_DONTWAIT
))
356 if (len
>= 0 && m
!= pd
->mp
) {
358 pd
->pf_mtag
= pf_find_mtag(m
);
362 struct ip
*h
= mtod(m
, struct ip
*);
363 pd
->src
= (struct pf_addr
*)&h
->ip_src
;
364 pd
->dst
= (struct pf_addr
*)&h
->ip_dst
;
365 pd
->ip_sum
= &h
->ip_sum
;
370 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
371 pd
->src
= (struct pf_addr
*)&h
->ip6_src
;
372 pd
->dst
= (struct pf_addr
*)&h
->ip6_dst
;
380 return (len
< 0 ? 0 : m
);
384 pf_state_lookup_aux(struct pf_state
**state
, struct pfi_kif
*kif
,
385 int direction
, int *action
)
387 if (*state
== NULL
|| (*state
)->timeout
== PFTM_PURGE
) {
392 if (direction
== PF_OUT
&&
393 (((*state
)->rule
.ptr
->rt
== PF_ROUTETO
&&
394 (*state
)->rule
.ptr
->direction
== PF_OUT
) ||
395 ((*state
)->rule
.ptr
->rt
== PF_REPLYTO
&&
396 (*state
)->rule
.ptr
->direction
== PF_IN
)) &&
397 (*state
)->rt_kif
!= NULL
&& (*state
)->rt_kif
!= kif
) {
405 #define STATE_LOOKUP() \
408 *state = pf_find_state(kif, &key, direction); \
409 if (*state != NULL && pd != NULL && \
410 !(pd->pktflags & PKTF_FLOW_ID)) { \
411 pd->flowsrc = (*state)->state_key->flowsrc; \
412 pd->flowhash = (*state)->state_key->flowhash; \
413 if (pd->flowhash != 0) { \
414 pd->pktflags |= PKTF_FLOW_ID; \
415 pd->pktflags &= ~PKTF_FLOW_ADV; \
418 if (pf_state_lookup_aux(state, kif, direction, &action)) \
422 #define STATE_ADDR_TRANSLATE(sk) \
423 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
424 ((sk)->af == AF_INET6 && \
425 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
426 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
427 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3]))
429 #define STATE_TRANSLATE(sk) \
430 (STATE_ADDR_TRANSLATE(sk) || \
431 (sk)->lan.xport.port != (sk)->gwy.xport.port)
433 #define STATE_GRE_TRANSLATE(sk) \
434 (STATE_ADDR_TRANSLATE(sk) || \
435 (sk)->lan.xport.call_id != (sk)->gwy.xport.call_id)
437 #define BOUND_IFACE(r, k) \
438 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
440 #define STATE_INC_COUNTERS(s) \
442 s->rule.ptr->states++; \
443 VERIFY(s->rule.ptr->states != 0); \
444 if (s->anchor.ptr != NULL) { \
445 s->anchor.ptr->states++; \
446 VERIFY(s->anchor.ptr->states != 0); \
448 if (s->nat_rule.ptr != NULL) { \
449 s->nat_rule.ptr->states++; \
450 VERIFY(s->nat_rule.ptr->states != 0); \
454 #define STATE_DEC_COUNTERS(s) \
456 if (s->nat_rule.ptr != NULL) { \
457 VERIFY(s->nat_rule.ptr->states > 0); \
458 s->nat_rule.ptr->states--; \
460 if (s->anchor.ptr != NULL) { \
461 VERIFY(s->anchor.ptr->states > 0); \
462 s->anchor.ptr->states--; \
464 VERIFY(s->rule.ptr->states > 0); \
465 s->rule.ptr->states--; \
468 static __inline
int pf_src_compare(struct pf_src_node
*, struct pf_src_node
*);
469 static __inline
int pf_state_compare_lan_ext(struct pf_state_key
*,
470 struct pf_state_key
*);
471 static __inline
int pf_state_compare_ext_gwy(struct pf_state_key
*,
472 struct pf_state_key
*);
473 static __inline
int pf_state_compare_id(struct pf_state
*,
476 struct pf_src_tree tree_src_tracking
;
478 struct pf_state_tree_id tree_id
;
479 struct pf_state_queue state_list
;
481 RB_GENERATE(pf_src_tree
, pf_src_node
, entry
, pf_src_compare
);
482 RB_GENERATE(pf_state_tree_lan_ext
, pf_state_key
,
483 entry_lan_ext
, pf_state_compare_lan_ext
);
484 RB_GENERATE(pf_state_tree_ext_gwy
, pf_state_key
,
485 entry_ext_gwy
, pf_state_compare_ext_gwy
);
486 RB_GENERATE(pf_state_tree_id
, pf_state
,
487 entry_id
, pf_state_compare_id
);
489 #define PF_DT_SKIP_LANEXT 0x01
490 #define PF_DT_SKIP_EXTGWY 0x02
492 static const u_int16_t PF_PPTP_PORT
= 1723;
493 static const u_int32_t PF_PPTP_MAGIC_NUMBER
= 0x1A2B3C4D;
501 struct pf_pptp_ctrl_hdr
{
503 u_int16_t reserved_0
;
506 struct pf_pptp_ctrl_generic
{
510 #define PF_PPTP_CTRL_TYPE_START_REQ 1
511 struct pf_pptp_ctrl_start_req
{
512 u_int16_t protocol_version
;
513 u_int16_t reserved_1
;
514 u_int32_t framing_capabilities
;
515 u_int32_t bearer_capabilities
;
516 u_int16_t maximum_channels
;
517 u_int16_t firmware_revision
;
518 u_int8_t host_name
[64];
519 u_int8_t vendor_string
[64];
522 #define PF_PPTP_CTRL_TYPE_START_RPY 2
523 struct pf_pptp_ctrl_start_rpy
{
524 u_int16_t protocol_version
;
525 u_int8_t result_code
;
527 u_int32_t framing_capabilities
;
528 u_int32_t bearer_capabilities
;
529 u_int16_t maximum_channels
;
530 u_int16_t firmware_revision
;
531 u_int8_t host_name
[64];
532 u_int8_t vendor_string
[64];
535 #define PF_PPTP_CTRL_TYPE_STOP_REQ 3
536 struct pf_pptp_ctrl_stop_req
{
539 u_int16_t reserved_2
;
542 #define PF_PPTP_CTRL_TYPE_STOP_RPY 4
543 struct pf_pptp_ctrl_stop_rpy
{
546 u_int16_t reserved_1
;
549 #define PF_PPTP_CTRL_TYPE_ECHO_REQ 5
550 struct pf_pptp_ctrl_echo_req
{
551 u_int32_t identifier
;
554 #define PF_PPTP_CTRL_TYPE_ECHO_RPY 6
555 struct pf_pptp_ctrl_echo_rpy
{
556 u_int32_t identifier
;
557 u_int8_t result_code
;
559 u_int16_t reserved_1
;
562 #define PF_PPTP_CTRL_TYPE_CALL_OUT_REQ 7
563 struct pf_pptp_ctrl_call_out_req
{
565 u_int16_t call_sernum
;
567 u_int32_t bearer_type
;
568 u_int32_t framing_type
;
569 u_int16_t rxwindow_size
;
570 u_int16_t proc_delay
;
571 u_int8_t phone_num
[64];
572 u_int8_t sub_addr
[64];
575 #define PF_PPTP_CTRL_TYPE_CALL_OUT_RPY 8
576 struct pf_pptp_ctrl_call_out_rpy
{
578 u_int16_t peer_call_id
;
579 u_int8_t result_code
;
581 u_int16_t cause_code
;
582 u_int32_t connect_speed
;
583 u_int16_t rxwindow_size
;
584 u_int16_t proc_delay
;
585 u_int32_t phy_channel_id
;
588 #define PF_PPTP_CTRL_TYPE_CALL_IN_1ST 9
589 struct pf_pptp_ctrl_call_in_1st
{
591 u_int16_t call_sernum
;
592 u_int32_t bearer_type
;
593 u_int32_t phy_channel_id
;
594 u_int16_t dialed_number_len
;
595 u_int16_t dialing_number_len
;
596 u_int8_t dialed_num
[64];
597 u_int8_t dialing_num
[64];
598 u_int8_t sub_addr
[64];
601 #define PF_PPTP_CTRL_TYPE_CALL_IN_2ND 10
602 struct pf_pptp_ctrl_call_in_2nd
{
604 u_int16_t peer_call_id
;
605 u_int8_t result_code
;
607 u_int16_t rxwindow_size
;
609 u_int16_t reserved_1
;
612 #define PF_PPTP_CTRL_TYPE_CALL_IN_3RD 11
613 struct pf_pptp_ctrl_call_in_3rd
{
615 u_int16_t reserved_1
;
616 u_int32_t connect_speed
;
617 u_int16_t rxwindow_size
;
619 u_int32_t framing_type
;
622 #define PF_PPTP_CTRL_TYPE_CALL_CLR 12
623 struct pf_pptp_ctrl_call_clr
{
625 u_int16_t reserved_1
;
628 #define PF_PPTP_CTRL_TYPE_CALL_DISC 13
629 struct pf_pptp_ctrl_call_disc
{
631 u_int8_t result_code
;
633 u_int16_t cause_code
;
634 u_int16_t reserved_1
;
635 u_int8_t statistics
[128];
638 #define PF_PPTP_CTRL_TYPE_ERROR 14
639 struct pf_pptp_ctrl_error
{
640 u_int16_t peer_call_id
;
641 u_int16_t reserved_1
;
642 u_int32_t crc_errors
;
645 u_int32_t buf_errors
;
646 u_int32_t tim_errors
;
647 u_int32_t align_errors
;
650 #define PF_PPTP_CTRL_TYPE_SET_LINKINFO 15
651 struct pf_pptp_ctrl_set_linkinfo
{
652 u_int16_t peer_call_id
;
653 u_int16_t reserved_1
;
659 static const char *pf_pptp_ctrl_type_name(u_int16_t code
)
663 if (code
< PF_PPTP_CTRL_TYPE_START_REQ
||
664 code
> PF_PPTP_CTRL_TYPE_SET_LINKINFO
) {
665 static char reserved
[] = "reserved-00";
667 sprintf(&reserved
[9], "%02x", code
);
670 static const char *name
[] = {
671 "start_req", "start_rpy", "stop_req", "stop_rpy",
672 "echo_req", "echo_rpy", "call_out_req", "call_out_rpy",
673 "call_in_1st", "call_in_2nd", "call_in_3rd",
674 "call_clr", "call_disc", "error", "set_linkinfo"
677 return (name
[code
- 1]);
682 static const size_t PF_PPTP_CTRL_MSG_MINSIZE
=
683 sizeof (struct pf_pptp_hdr
) +
684 sizeof (struct pf_pptp_ctrl_hdr
) +
685 MIN(sizeof (struct pf_pptp_ctrl_start_req
),
686 MIN(sizeof (struct pf_pptp_ctrl_start_rpy
),
687 MIN(sizeof (struct pf_pptp_ctrl_stop_req
),
688 MIN(sizeof (struct pf_pptp_ctrl_stop_rpy
),
689 MIN(sizeof (struct pf_pptp_ctrl_echo_req
),
690 MIN(sizeof (struct pf_pptp_ctrl_echo_rpy
),
691 MIN(sizeof (struct pf_pptp_ctrl_call_out_req
),
692 MIN(sizeof (struct pf_pptp_ctrl_call_out_rpy
),
693 MIN(sizeof (struct pf_pptp_ctrl_call_in_1st
),
694 MIN(sizeof (struct pf_pptp_ctrl_call_in_2nd
),
695 MIN(sizeof (struct pf_pptp_ctrl_call_in_3rd
),
696 MIN(sizeof (struct pf_pptp_ctrl_call_clr
),
697 MIN(sizeof (struct pf_pptp_ctrl_call_disc
),
698 MIN(sizeof (struct pf_pptp_ctrl_error
),
699 sizeof (struct pf_pptp_ctrl_set_linkinfo
)
702 union pf_pptp_ctrl_msg_union
{
703 struct pf_pptp_ctrl_start_req start_req
;
704 struct pf_pptp_ctrl_start_rpy start_rpy
;
705 struct pf_pptp_ctrl_stop_req stop_req
;
706 struct pf_pptp_ctrl_stop_rpy stop_rpy
;
707 struct pf_pptp_ctrl_echo_req echo_req
;
708 struct pf_pptp_ctrl_echo_rpy echo_rpy
;
709 struct pf_pptp_ctrl_call_out_req call_out_req
;
710 struct pf_pptp_ctrl_call_out_rpy call_out_rpy
;
711 struct pf_pptp_ctrl_call_in_1st call_in_1st
;
712 struct pf_pptp_ctrl_call_in_2nd call_in_2nd
;
713 struct pf_pptp_ctrl_call_in_3rd call_in_3rd
;
714 struct pf_pptp_ctrl_call_clr call_clr
;
715 struct pf_pptp_ctrl_call_disc call_disc
;
716 struct pf_pptp_ctrl_error error
;
717 struct pf_pptp_ctrl_set_linkinfo set_linkinfo
;
721 struct pf_pptp_ctrl_msg
{
722 struct pf_pptp_hdr hdr
;
723 struct pf_pptp_ctrl_hdr ctrl
;
724 union pf_pptp_ctrl_msg_union msg
;
727 #define PF_GRE_FLAG_CHECKSUM_PRESENT 0x8000
728 #define PF_GRE_FLAG_VERSION_MASK 0x0007
729 #define PF_GRE_PPP_ETHERTYPE 0x880B
731 struct pf_grev1_hdr
{
733 u_int16_t protocol_type
;
734 u_int16_t payload_length
;
742 static const u_int16_t PF_IKE_PORT
= 500;
745 u_int64_t initiator_cookie
, responder_cookie
;
746 u_int8_t next_payload
, version
, exchange_type
, flags
;
747 u_int32_t message_id
, length
;
750 #define PF_IKE_PACKET_MINSIZE (sizeof (struct pf_ike_hdr))
752 #define PF_IKEv1_EXCHTYPE_BASE 1
753 #define PF_IKEv1_EXCHTYPE_ID_PROTECT 2
754 #define PF_IKEv1_EXCHTYPE_AUTH_ONLY 3
755 #define PF_IKEv1_EXCHTYPE_AGGRESSIVE 4
756 #define PF_IKEv1_EXCHTYPE_INFORMATIONAL 5
757 #define PF_IKEv2_EXCHTYPE_SA_INIT 34
758 #define PF_IKEv2_EXCHTYPE_AUTH 35
759 #define PF_IKEv2_EXCHTYPE_CREATE_CHILD_SA 36
760 #define PF_IKEv2_EXCHTYPE_INFORMATIONAL 37
762 #define PF_IKEv1_FLAG_E 0x01
763 #define PF_IKEv1_FLAG_C 0x02
764 #define PF_IKEv1_FLAG_A 0x04
765 #define PF_IKEv2_FLAG_I 0x08
766 #define PF_IKEv2_FLAG_V 0x10
767 #define PF_IKEv2_FLAG_R 0x20
776 pf_src_compare(struct pf_src_node
*a
, struct pf_src_node
*b
)
780 if (a
->rule
.ptr
> b
->rule
.ptr
)
782 if (a
->rule
.ptr
< b
->rule
.ptr
)
784 if ((diff
= a
->af
- b
->af
) != 0)
789 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
791 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
797 if (a
->addr
.addr32
[3] > b
->addr
.addr32
[3])
799 if (a
->addr
.addr32
[3] < b
->addr
.addr32
[3])
801 if (a
->addr
.addr32
[2] > b
->addr
.addr32
[2])
803 if (a
->addr
.addr32
[2] < b
->addr
.addr32
[2])
805 if (a
->addr
.addr32
[1] > b
->addr
.addr32
[1])
807 if (a
->addr
.addr32
[1] < b
->addr
.addr32
[1])
809 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
811 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
820 pf_state_compare_lan_ext(struct pf_state_key
*a
, struct pf_state_key
*b
)
825 if ((diff
= a
->proto
- b
->proto
) != 0)
827 if ((diff
= a
->af
- b
->af
) != 0)
830 extfilter
= PF_EXTFILTER_APD
;
835 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
840 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
842 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
847 if ((diff
= a
->proto_variant
- b
->proto_variant
))
849 extfilter
= a
->proto_variant
;
850 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
852 if ((extfilter
< PF_EXTFILTER_AD
) &&
853 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
858 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
859 a
->proto_variant
== b
->proto_variant
) {
860 if (!!(diff
= a
->ext
.xport
.call_id
-
861 b
->ext
.xport
.call_id
))
867 if (!!(diff
= a
->ext
.xport
.spi
- b
->ext
.xport
.spi
))
878 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
880 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
882 if (extfilter
< PF_EXTFILTER_EI
) {
883 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
885 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
892 if (a
->lan
.addr
.addr32
[3] > b
->lan
.addr
.addr32
[3])
894 if (a
->lan
.addr
.addr32
[3] < b
->lan
.addr
.addr32
[3])
896 if (a
->lan
.addr
.addr32
[2] > b
->lan
.addr
.addr32
[2])
898 if (a
->lan
.addr
.addr32
[2] < b
->lan
.addr
.addr32
[2])
900 if (a
->lan
.addr
.addr32
[1] > b
->lan
.addr
.addr32
[1])
902 if (a
->lan
.addr
.addr32
[1] < b
->lan
.addr
.addr32
[1])
904 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
906 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
908 if (extfilter
< PF_EXTFILTER_EI
||
909 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
910 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
912 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
914 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
916 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
918 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
920 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
922 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
924 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
931 if (a
->app_state
&& b
->app_state
) {
932 if (a
->app_state
->compare_lan_ext
&&
933 b
->app_state
->compare_lan_ext
) {
934 diff
= (const char *)b
->app_state
->compare_lan_ext
-
935 (const char *)a
->app_state
->compare_lan_ext
;
938 diff
= a
->app_state
->compare_lan_ext(a
->app_state
,
949 pf_state_compare_ext_gwy(struct pf_state_key
*a
, struct pf_state_key
*b
)
954 if ((diff
= a
->proto
- b
->proto
) != 0)
957 if ((diff
= a
->af
- b
->af
) != 0)
960 extfilter
= PF_EXTFILTER_APD
;
965 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
970 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
972 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
977 if ((diff
= a
->proto_variant
- b
->proto_variant
))
979 extfilter
= a
->proto_variant
;
980 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
982 if ((extfilter
< PF_EXTFILTER_AD
) &&
983 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
988 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
989 a
->proto_variant
== b
->proto_variant
) {
990 if (!!(diff
= a
->gwy
.xport
.call_id
-
991 b
->gwy
.xport
.call_id
))
997 if (!!(diff
= a
->gwy
.xport
.spi
- b
->gwy
.xport
.spi
))
1008 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1010 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1012 if (extfilter
< PF_EXTFILTER_EI
) {
1013 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1015 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1022 if (a
->gwy
.addr
.addr32
[3] > b
->gwy
.addr
.addr32
[3])
1024 if (a
->gwy
.addr
.addr32
[3] < b
->gwy
.addr
.addr32
[3])
1026 if (a
->gwy
.addr
.addr32
[2] > b
->gwy
.addr
.addr32
[2])
1028 if (a
->gwy
.addr
.addr32
[2] < b
->gwy
.addr
.addr32
[2])
1030 if (a
->gwy
.addr
.addr32
[1] > b
->gwy
.addr
.addr32
[1])
1032 if (a
->gwy
.addr
.addr32
[1] < b
->gwy
.addr
.addr32
[1])
1034 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1036 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1038 if (extfilter
< PF_EXTFILTER_EI
||
1039 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
1040 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
1042 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
1044 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
1046 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
1048 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
1050 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
1052 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1054 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1061 if (a
->app_state
&& b
->app_state
) {
1062 if (a
->app_state
->compare_ext_gwy
&&
1063 b
->app_state
->compare_ext_gwy
) {
1064 diff
= (const char *)b
->app_state
->compare_ext_gwy
-
1065 (const char *)a
->app_state
->compare_ext_gwy
;
1068 diff
= a
->app_state
->compare_ext_gwy(a
->app_state
,
1079 pf_state_compare_id(struct pf_state
*a
, struct pf_state
*b
)
1085 if (a
->creatorid
> b
->creatorid
)
1087 if (a
->creatorid
< b
->creatorid
)
1095 pf_addrcpy(struct pf_addr
*dst
, struct pf_addr
*src
, sa_family_t af
)
1100 dst
->addr32
[0] = src
->addr32
[0];
1104 dst
->addr32
[0] = src
->addr32
[0];
1105 dst
->addr32
[1] = src
->addr32
[1];
1106 dst
->addr32
[2] = src
->addr32
[2];
1107 dst
->addr32
[3] = src
->addr32
[3];
1114 pf_find_state_byid(struct pf_state_cmp
*key
)
1116 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1118 return (RB_FIND(pf_state_tree_id
, &tree_id
,
1119 (struct pf_state
*)(void *)key
));
1122 static struct pf_state
*
1123 pf_find_state(struct pfi_kif
*kif
, struct pf_state_key_cmp
*key
, u_int dir
)
1125 struct pf_state_key
*sk
= NULL
;
1128 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1132 sk
= RB_FIND(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1133 (struct pf_state_key
*)key
);
1136 sk
= RB_FIND(pf_state_tree_ext_gwy
, &pf_statetbl_ext_gwy
,
1137 (struct pf_state_key
*)key
);
1140 panic("pf_find_state");
1143 /* list is sorted, if-bound states before floating ones */
1145 TAILQ_FOREACH(s
, &sk
->states
, next
)
1146 if (s
->kif
== pfi_all
|| s
->kif
== kif
)
1153 pf_find_state_all(struct pf_state_key_cmp
*key
, u_int dir
, int *more
)
1155 struct pf_state_key
*sk
= NULL
;
1156 struct pf_state
*s
, *ret
= NULL
;
1158 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1162 sk
= RB_FIND(pf_state_tree_lan_ext
,
1163 &pf_statetbl_lan_ext
, (struct pf_state_key
*)key
);
1166 sk
= RB_FIND(pf_state_tree_ext_gwy
,
1167 &pf_statetbl_ext_gwy
, (struct pf_state_key
*)key
);
1170 panic("pf_find_state_all");
1174 ret
= TAILQ_FIRST(&sk
->states
);
1178 TAILQ_FOREACH(s
, &sk
->states
, next
)
1186 pf_init_threshold(struct pf_threshold
*threshold
,
1187 u_int32_t limit
, u_int32_t seconds
)
1189 threshold
->limit
= limit
* PF_THRESHOLD_MULT
;
1190 threshold
->seconds
= seconds
;
1191 threshold
->count
= 0;
1192 threshold
->last
= pf_time_second();
1196 pf_add_threshold(struct pf_threshold
*threshold
)
1198 u_int32_t t
= pf_time_second(), diff
= t
- threshold
->last
;
1200 if (diff
>= threshold
->seconds
)
1201 threshold
->count
= 0;
1203 threshold
->count
-= threshold
->count
* diff
/
1205 threshold
->count
+= PF_THRESHOLD_MULT
;
1206 threshold
->last
= t
;
1210 pf_check_threshold(struct pf_threshold
*threshold
)
1212 return (threshold
->count
> threshold
->limit
);
1216 pf_src_connlimit(struct pf_state
**state
)
1220 (*state
)->src_node
->conn
++;
1221 VERIFY((*state
)->src_node
->conn
!= 0);
1222 (*state
)->src
.tcp_est
= 1;
1223 pf_add_threshold(&(*state
)->src_node
->conn_rate
);
1225 if ((*state
)->rule
.ptr
->max_src_conn
&&
1226 (*state
)->rule
.ptr
->max_src_conn
<
1227 (*state
)->src_node
->conn
) {
1228 pf_status
.lcounters
[LCNT_SRCCONN
]++;
1232 if ((*state
)->rule
.ptr
->max_src_conn_rate
.limit
&&
1233 pf_check_threshold(&(*state
)->src_node
->conn_rate
)) {
1234 pf_status
.lcounters
[LCNT_SRCCONNRATE
]++;
1241 if ((*state
)->rule
.ptr
->overload_tbl
) {
1243 u_int32_t killed
= 0;
1245 pf_status
.lcounters
[LCNT_OVERLOAD_TABLE
]++;
1246 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1247 printf("pf_src_connlimit: blocking address ");
1248 pf_print_host(&(*state
)->src_node
->addr
, 0,
1249 (*state
)->state_key
->af
);
1252 bzero(&p
, sizeof (p
));
1253 p
.pfra_af
= (*state
)->state_key
->af
;
1254 switch ((*state
)->state_key
->af
) {
1258 p
.pfra_ip4addr
= (*state
)->src_node
->addr
.v4
;
1264 p
.pfra_ip6addr
= (*state
)->src_node
->addr
.v6
;
1269 pfr_insert_kentry((*state
)->rule
.ptr
->overload_tbl
,
1270 &p
, pf_calendar_time_second());
1272 /* kill existing states if that's required. */
1273 if ((*state
)->rule
.ptr
->flush
) {
1274 struct pf_state_key
*sk
;
1275 struct pf_state
*st
;
1277 pf_status
.lcounters
[LCNT_OVERLOAD_FLUSH
]++;
1278 RB_FOREACH(st
, pf_state_tree_id
, &tree_id
) {
1281 * Kill states from this source. (Only those
1282 * from the same rule if PF_FLUSH_GLOBAL is not
1286 (*state
)->state_key
->af
&&
1287 (((*state
)->state_key
->direction
==
1289 PF_AEQ(&(*state
)->src_node
->addr
,
1290 &sk
->lan
.addr
, sk
->af
)) ||
1291 ((*state
)->state_key
->direction
== PF_IN
&&
1292 PF_AEQ(&(*state
)->src_node
->addr
,
1293 &sk
->ext
.addr
, sk
->af
))) &&
1294 ((*state
)->rule
.ptr
->flush
&
1296 (*state
)->rule
.ptr
== st
->rule
.ptr
)) {
1297 st
->timeout
= PFTM_PURGE
;
1298 st
->src
.state
= st
->dst
.state
=
1303 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1304 printf(", %u states killed", killed
);
1306 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1310 /* kill this state */
1311 (*state
)->timeout
= PFTM_PURGE
;
1312 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
1317 pf_insert_src_node(struct pf_src_node
**sn
, struct pf_rule
*rule
,
1318 struct pf_addr
*src
, sa_family_t af
)
1320 struct pf_src_node k
;
1324 PF_ACPY(&k
.addr
, src
, af
);
1325 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1326 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1330 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
1331 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
1334 if (!rule
->max_src_nodes
||
1335 rule
->src_nodes
< rule
->max_src_nodes
)
1336 (*sn
) = pool_get(&pf_src_tree_pl
, PR_WAITOK
);
1338 pf_status
.lcounters
[LCNT_SRCNODES
]++;
1341 bzero(*sn
, sizeof (struct pf_src_node
));
1343 pf_init_threshold(&(*sn
)->conn_rate
,
1344 rule
->max_src_conn_rate
.limit
,
1345 rule
->max_src_conn_rate
.seconds
);
1348 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1349 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1350 (*sn
)->rule
.ptr
= rule
;
1352 (*sn
)->rule
.ptr
= NULL
;
1353 PF_ACPY(&(*sn
)->addr
, src
, af
);
1354 if (RB_INSERT(pf_src_tree
,
1355 &tree_src_tracking
, *sn
) != NULL
) {
1356 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1357 printf("pf: src_tree insert failed: ");
1358 pf_print_host(&(*sn
)->addr
, 0, af
);
1361 pool_put(&pf_src_tree_pl
, *sn
);
1364 (*sn
)->creation
= pf_time_second();
1365 (*sn
)->ruletype
= rule
->action
;
1366 if ((*sn
)->rule
.ptr
!= NULL
)
1367 (*sn
)->rule
.ptr
->src_nodes
++;
1368 pf_status
.scounters
[SCNT_SRC_NODE_INSERT
]++;
1369 pf_status
.src_nodes
++;
1371 if (rule
->max_src_states
&&
1372 (*sn
)->states
>= rule
->max_src_states
) {
1373 pf_status
.lcounters
[LCNT_SRCSTATES
]++;
1381 pf_stateins_err(const char *tree
, struct pf_state
*s
, struct pfi_kif
*kif
)
1383 struct pf_state_key
*sk
= s
->state_key
;
1385 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1386 printf("pf: state insert failed: %s %s ", tree
, kif
->pfik_name
);
1387 switch (sk
->proto
) {
1397 case IPPROTO_ICMPV6
:
1401 printf("PROTO=%u", sk
->proto
);
1405 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
,
1408 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
,
1411 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
,
1413 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1414 printf(" (from sync)");
1420 pf_insert_state(struct pfi_kif
*kif
, struct pf_state
*s
)
1422 struct pf_state_key
*cur
;
1423 struct pf_state
*sp
;
1425 VERIFY(s
->state_key
!= NULL
);
1428 if ((cur
= RB_INSERT(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1429 s
->state_key
)) != NULL
) {
1430 /* key exists. check for same kif, if none, add to key */
1431 TAILQ_FOREACH(sp
, &cur
->states
, next
)
1432 if (sp
->kif
== kif
) { /* collision! */
1433 pf_stateins_err("tree_lan_ext", s
, kif
);
1435 PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1438 pf_detach_state(s
, PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1439 pf_attach_state(cur
, s
, kif
== pfi_all
? 1 : 0);
1442 /* if cur != NULL, we already found a state key and attached to it */
1443 if (cur
== NULL
&& (cur
= RB_INSERT(pf_state_tree_ext_gwy
,
1444 &pf_statetbl_ext_gwy
, s
->state_key
)) != NULL
) {
1445 /* must not happen. we must have found the sk above! */
1446 pf_stateins_err("tree_ext_gwy", s
, kif
);
1447 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
1451 if (s
->id
== 0 && s
->creatorid
== 0) {
1452 s
->id
= htobe64(pf_status
.stateid
++);
1453 s
->creatorid
= pf_status
.hostid
;
1455 if (RB_INSERT(pf_state_tree_id
, &tree_id
, s
) != NULL
) {
1456 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1457 printf("pf: state insert failed: "
1458 "id: %016llx creatorid: %08x",
1459 be64toh(s
->id
), ntohl(s
->creatorid
));
1460 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1461 printf(" (from sync)");
1464 pf_detach_state(s
, 0);
1467 TAILQ_INSERT_TAIL(&state_list
, s
, entry_list
);
1468 pf_status
.fcounters
[FCNT_STATE_INSERT
]++;
1470 VERIFY(pf_status
.states
!= 0);
1471 pfi_kif_ref(kif
, PFI_KIF_REF_STATE
);
1473 pfsync_insert_state(s
);
1479 pf_purge_thread_cont(int err
)
1482 static u_int32_t nloops
= 0;
1483 int t
= 1; /* 1 second */
1486 * Update coarse-grained networking timestamp (in sec.); the idea
1487 * is to piggy-back on the periodic timeout callout to update
1488 * the counter returnable via net_uptime().
1490 net_update_uptime();
1492 lck_rw_lock_shared(pf_perim_lock
);
1493 lck_mtx_lock(pf_lock
);
1495 /* purge everything if not running */
1496 if (!pf_status
.running
) {
1497 pf_purge_expired_states(pf_status
.states
);
1498 pf_purge_expired_fragments();
1499 pf_purge_expired_src_nodes();
1501 /* terminate thread (we don't currently do this) */
1502 if (pf_purge_thread
== NULL
) {
1503 lck_mtx_unlock(pf_lock
);
1504 lck_rw_done(pf_perim_lock
);
1506 thread_deallocate(current_thread());
1507 thread_terminate(current_thread());
1511 /* if there's nothing left, sleep w/o timeout */
1512 if (pf_status
.states
== 0 &&
1513 pf_normalize_isempty() &&
1514 RB_EMPTY(&tree_src_tracking
)) {
1522 /* process a fraction of the state table every second */
1523 pf_purge_expired_states(1 + (pf_status
.states
1524 / pf_default_rule
.timeout
[PFTM_INTERVAL
]));
1526 /* purge other expired types every PFTM_INTERVAL seconds */
1527 if (++nloops
>= pf_default_rule
.timeout
[PFTM_INTERVAL
]) {
1528 pf_purge_expired_fragments();
1529 pf_purge_expired_src_nodes();
1533 lck_mtx_unlock(pf_lock
);
1534 lck_rw_done(pf_perim_lock
);
1536 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge_cont",
1537 t
* hz
, pf_purge_thread_cont
);
1545 pf_purge_thread_fn(void *v
, wait_result_t w
)
1547 #pragma unused(v, w)
1548 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge", 0,
1549 pf_purge_thread_cont
);
1551 * tsleep0() shouldn't have returned as PCATCH was not set;
1552 * therefore assert in this case.
1558 pf_state_expires(const struct pf_state
*state
)
1565 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1567 /* handle all PFTM_* > PFTM_MAX here */
1568 if (state
->timeout
== PFTM_PURGE
)
1569 return (pf_time_second());
1570 if (state
->timeout
== PFTM_UNTIL_PACKET
)
1572 VERIFY(state
->timeout
!= PFTM_UNLINKED
);
1573 VERIFY(state
->timeout
< PFTM_MAX
);
1574 t
= state
->rule
.ptr
->timeout
[state
->timeout
];
1576 t
= pf_default_rule
.timeout
[state
->timeout
];
1577 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
1579 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
1580 states
= state
->rule
.ptr
->states
;
1582 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
1583 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
1584 states
= pf_status
.states
;
1586 if (end
&& states
> start
&& start
< end
) {
1588 return (state
->expire
+ t
* (end
- states
) /
1591 return (pf_time_second());
1593 return (state
->expire
+ t
);
1597 pf_purge_expired_src_nodes(void)
1599 struct pf_src_node
*cur
, *next
;
1601 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1603 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
1604 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
1606 if (cur
->states
<= 0 && cur
->expire
<= pf_time_second()) {
1607 if (cur
->rule
.ptr
!= NULL
) {
1608 cur
->rule
.ptr
->src_nodes
--;
1609 if (cur
->rule
.ptr
->states
<= 0 &&
1610 cur
->rule
.ptr
->max_src_nodes
<= 0)
1611 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1613 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1614 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1615 pf_status
.src_nodes
--;
1616 pool_put(&pf_src_tree_pl
, cur
);
1622 pf_src_tree_remove_state(struct pf_state
*s
)
1626 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1628 if (s
->src_node
!= NULL
) {
1629 if (s
->src
.tcp_est
) {
1630 VERIFY(s
->src_node
->conn
> 0);
1631 --s
->src_node
->conn
;
1633 VERIFY(s
->src_node
->states
> 0);
1634 if (--s
->src_node
->states
<= 0) {
1635 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1637 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1638 s
->src_node
->expire
= pf_time_second() + t
;
1641 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1642 VERIFY(s
->nat_src_node
->states
> 0);
1643 if (--s
->nat_src_node
->states
<= 0) {
1644 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1646 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1647 s
->nat_src_node
->expire
= pf_time_second() + t
;
1650 s
->src_node
= s
->nat_src_node
= NULL
;
1654 pf_unlink_state(struct pf_state
*cur
)
1656 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1658 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1659 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1660 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1661 cur
->state_key
->ext
.xport
.port
,
1662 cur
->state_key
->lan
.xport
.port
,
1663 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1664 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1667 hook_runloop(&cur
->unlink_hooks
, HOOK_REMOVE
|HOOK_FREE
);
1668 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1670 if (cur
->creatorid
== pf_status
.hostid
)
1671 pfsync_delete_state(cur
);
1673 cur
->timeout
= PFTM_UNLINKED
;
1674 pf_src_tree_remove_state(cur
);
1675 pf_detach_state(cur
, 0);
1678 /* callers should be at splpf and hold the
1679 * write_lock on pf_consistency_lock */
1681 pf_free_state(struct pf_state
*cur
)
1683 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1685 if (pfsyncif
!= NULL
&&
1686 (pfsyncif
->sc_bulk_send_next
== cur
||
1687 pfsyncif
->sc_bulk_terminator
== cur
))
1690 VERIFY(cur
->timeout
== PFTM_UNLINKED
);
1691 VERIFY(cur
->rule
.ptr
->states
> 0);
1692 if (--cur
->rule
.ptr
->states
<= 0 &&
1693 cur
->rule
.ptr
->src_nodes
<= 0)
1694 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1695 if (cur
->nat_rule
.ptr
!= NULL
) {
1696 VERIFY(cur
->nat_rule
.ptr
->states
> 0);
1697 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1698 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1699 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1701 if (cur
->anchor
.ptr
!= NULL
) {
1702 VERIFY(cur
->anchor
.ptr
->states
> 0);
1703 if (--cur
->anchor
.ptr
->states
<= 0)
1704 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1706 pf_normalize_tcp_cleanup(cur
);
1707 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1708 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1710 pf_tag_unref(cur
->tag
);
1711 pool_put(&pf_state_pl
, cur
);
1712 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1713 VERIFY(pf_status
.states
> 0);
1718 pf_purge_expired_states(u_int32_t maxcheck
)
1720 static struct pf_state
*cur
= NULL
;
1721 struct pf_state
*next
;
1723 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1725 while (maxcheck
--) {
1726 /* wrap to start of list when we hit the end */
1728 cur
= TAILQ_FIRST(&state_list
);
1730 break; /* list empty */
1733 /* get next state, as cur may get deleted */
1734 next
= TAILQ_NEXT(cur
, entry_list
);
1736 if (cur
->timeout
== PFTM_UNLINKED
) {
1738 } else if (pf_state_expires(cur
) <= pf_time_second()) {
1739 /* unlink and free expired state */
1740 pf_unlink_state(cur
);
1748 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1750 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1752 if (aw
->type
!= PF_ADDR_TABLE
)
1754 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1760 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1762 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1764 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1766 pfr_detach_table(aw
->p
.tbl
);
1771 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1773 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1775 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1777 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1779 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1780 kt
= kt
->pfrkt_root
;
1782 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1787 pf_print_addr(struct pf_addr
*addr
, sa_family_t af
)
1792 u_int32_t a
= ntohl(addr
->addr32
[0]);
1793 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1801 u_int8_t i
, curstart
= 255, curend
= 0,
1802 maxstart
= 0, maxend
= 0;
1803 for (i
= 0; i
< 8; i
++) {
1804 if (!addr
->addr16
[i
]) {
1805 if (curstart
== 255)
1811 if ((curend
- curstart
) >
1812 (maxend
- maxstart
)) {
1813 maxstart
= curstart
;
1820 for (i
= 0; i
< 8; i
++) {
1821 if (i
>= maxstart
&& i
<= maxend
) {
1830 b
= ntohs(addr
->addr16
[i
]);
1843 pf_print_sk_host(struct pf_state_host
*sh
, sa_family_t af
, int proto
,
1844 u_int8_t proto_variant
)
1846 pf_print_addr(&sh
->addr
, af
);
1851 printf("[%08x]", ntohl(sh
->xport
.spi
));
1855 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
1856 printf("[%u]", ntohs(sh
->xport
.call_id
));
1861 printf("[%u]", ntohs(sh
->xport
.port
));
1870 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1872 pf_print_addr(addr
, af
);
1874 printf("[%u]", ntohs(p
));
1878 pf_print_state(struct pf_state
*s
)
1880 struct pf_state_key
*sk
= s
->state_key
;
1881 switch (sk
->proto
) {
1886 printf("GRE%u ", sk
->proto_variant
);
1897 case IPPROTO_ICMPV6
:
1901 printf("%u ", sk
->proto
);
1904 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1906 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1908 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1909 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
1910 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
1911 if (s
->src
.wscale
&& s
->dst
.wscale
)
1912 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
1914 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
1915 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
1916 if (s
->src
.wscale
&& s
->dst
.wscale
)
1917 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
1919 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
1923 pf_print_flags(u_int8_t f
)
1945 #define PF_SET_SKIP_STEPS(i) \
1947 while (head[i] != cur) { \
1948 head[i]->skip[i].ptr = cur; \
1949 head[i] = TAILQ_NEXT(head[i], entries); \
1954 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
1956 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
1959 cur
= TAILQ_FIRST(rules
);
1961 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1963 while (cur
!= NULL
) {
1965 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
1966 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
1967 if (cur
->direction
!= prev
->direction
)
1968 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
1969 if (cur
->af
!= prev
->af
)
1970 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
1971 if (cur
->proto
!= prev
->proto
)
1972 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
1973 if (cur
->src
.neg
!= prev
->src
.neg
||
1974 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
1975 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
1977 union pf_rule_xport
*cx
= &cur
->src
.xport
;
1978 union pf_rule_xport
*px
= &prev
->src
.xport
;
1980 switch (cur
->proto
) {
1983 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1986 if (prev
->proto
== IPPROTO_GRE
||
1987 prev
->proto
== IPPROTO_ESP
||
1988 cx
->range
.op
!= px
->range
.op
||
1989 cx
->range
.port
[0] != px
->range
.port
[0] ||
1990 cx
->range
.port
[1] != px
->range
.port
[1])
1991 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1995 if (cur
->dst
.neg
!= prev
->dst
.neg
||
1996 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
1997 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
1999 union pf_rule_xport
*cx
= &cur
->dst
.xport
;
2000 union pf_rule_xport
*px
= &prev
->dst
.xport
;
2002 switch (cur
->proto
) {
2004 if (cur
->proto
!= prev
->proto
||
2005 cx
->call_id
!= px
->call_id
)
2006 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2009 if (cur
->proto
!= prev
->proto
||
2011 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2014 if (prev
->proto
== IPPROTO_GRE
||
2015 prev
->proto
== IPPROTO_ESP
||
2016 cx
->range
.op
!= px
->range
.op
||
2017 cx
->range
.port
[0] != px
->range
.port
[0] ||
2018 cx
->range
.port
[1] != px
->range
.port
[1])
2019 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2025 cur
= TAILQ_NEXT(cur
, entries
);
2027 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2028 PF_SET_SKIP_STEPS(i
);
2032 pf_calc_state_key_flowhash(struct pf_state_key
*sk
)
2034 struct pf_flowhash_key fh
__attribute__((aligned(8)));
2035 uint32_t flowhash
= 0;
2037 bzero(&fh
, sizeof (fh
));
2038 if (PF_ALEQ(&sk
->lan
.addr
, &sk
->ext
.addr
, sk
->af
)) {
2039 bcopy(&sk
->lan
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2040 bcopy(&sk
->ext
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2042 bcopy(&sk
->ext
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2043 bcopy(&sk
->lan
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2045 if (sk
->lan
.xport
.spi
<= sk
->ext
.xport
.spi
) {
2046 fh
.ap1
.xport
.spi
= sk
->lan
.xport
.spi
;
2047 fh
.ap2
.xport
.spi
= sk
->ext
.xport
.spi
;
2049 fh
.ap1
.xport
.spi
= sk
->ext
.xport
.spi
;
2050 fh
.ap2
.xport
.spi
= sk
->lan
.xport
.spi
;
2053 fh
.proto
= sk
->proto
;
2056 flowhash
= net_flowhash(&fh
, sizeof (fh
), pf_hash_seed
);
2057 if (flowhash
== 0) {
2058 /* try to get a non-zero flowhash */
2059 pf_hash_seed
= RandomULong();
2067 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
2069 if (aw1
->type
!= aw2
->type
)
2071 switch (aw1
->type
) {
2072 case PF_ADDR_ADDRMASK
:
2074 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
2076 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
2079 case PF_ADDR_DYNIFTL
:
2080 return (aw1
->p
.dyn
== NULL
|| aw2
->p
.dyn
== NULL
||
2081 aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
2082 case PF_ADDR_NOROUTE
:
2083 case PF_ADDR_URPFFAILED
:
2086 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
2087 case PF_ADDR_RTLABEL
:
2088 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
2090 printf("invalid address type: %d\n", aw1
->type
);
2096 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
2102 l
= cksum
+ old
- new;
2103 l
= (l
>> 16) + (l
& 0xffff);
2111 pf_change_ap(int dir
, struct mbuf
*m
, struct pf_addr
*a
, u_int16_t
*p
,
2112 u_int16_t
*ic
, u_int16_t
*pc
, struct pf_addr
*an
, u_int16_t pn
,
2113 u_int8_t u
, sa_family_t af
)
2118 PF_ACPY(&ao
, a
, af
);
2126 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2127 ao
.addr16
[0], an
->addr16
[0], 0),
2128 ao
.addr16
[1], an
->addr16
[1], 0);
2131 * If the packet is originated from an ALG on the NAT gateway
2132 * (source address is loopback or local), in which case the
2133 * TCP/UDP checksum field contains the pseudo header checksum
2134 * that's not yet complemented.
2136 if (dir
== PF_OUT
&& m
!= NULL
&&
2137 (m
->m_flags
& M_PKTHDR
) &&
2138 (m
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))) {
2139 /* Pseudo-header checksum does not include ports */
2140 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2141 ao
.addr16
[0], an
->addr16
[0], u
),
2142 ao
.addr16
[1], an
->addr16
[1], u
);
2144 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2145 ao
.addr16
[0], an
->addr16
[0], u
),
2146 ao
.addr16
[1], an
->addr16
[1], u
),
2154 * If the packet is originated from an ALG on the NAT gateway
2155 * (source address is loopback or local), in which case the
2156 * TCP/UDP checksum field contains the pseudo header checksum
2157 * that's not yet complemented.
2159 if (dir
== PF_OUT
&& m
!= NULL
&&
2160 (m
->m_flags
& M_PKTHDR
) &&
2161 (m
->m_pkthdr
.csum_flags
& (CSUM_TCPIPV6
| CSUM_UDPIPV6
))) {
2162 /* Pseudo-header checksum does not include ports */
2163 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2164 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2165 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2166 ao
.addr16
[0], an
->addr16
[0], u
),
2167 ao
.addr16
[1], an
->addr16
[1], u
),
2168 ao
.addr16
[2], an
->addr16
[2], u
),
2169 ao
.addr16
[3], an
->addr16
[3], u
),
2170 ao
.addr16
[4], an
->addr16
[4], u
),
2171 ao
.addr16
[5], an
->addr16
[5], u
),
2172 ao
.addr16
[6], an
->addr16
[6], u
),
2173 ao
.addr16
[7], an
->addr16
[7], u
),
2176 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2177 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2178 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2179 ao
.addr16
[0], an
->addr16
[0], u
),
2180 ao
.addr16
[1], an
->addr16
[1], u
),
2181 ao
.addr16
[2], an
->addr16
[2], u
),
2182 ao
.addr16
[3], an
->addr16
[3], u
),
2183 ao
.addr16
[4], an
->addr16
[4], u
),
2184 ao
.addr16
[5], an
->addr16
[5], u
),
2185 ao
.addr16
[6], an
->addr16
[6], u
),
2186 ao
.addr16
[7], an
->addr16
[7], u
),
2195 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
2197 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
2201 memcpy(&ao
, a
, sizeof (ao
));
2202 memcpy(a
, &an
, sizeof (u_int32_t
));
2203 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
2204 ao
% 65536, an
% 65536, u
);
2209 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
2213 PF_ACPY(&ao
, a
, AF_INET6
);
2214 PF_ACPY(a
, an
, AF_INET6
);
2216 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2217 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2218 pf_cksum_fixup(pf_cksum_fixup(*c
,
2219 ao
.addr16
[0], an
->addr16
[0], u
),
2220 ao
.addr16
[1], an
->addr16
[1], u
),
2221 ao
.addr16
[2], an
->addr16
[2], u
),
2222 ao
.addr16
[3], an
->addr16
[3], u
),
2223 ao
.addr16
[4], an
->addr16
[4], u
),
2224 ao
.addr16
[5], an
->addr16
[5], u
),
2225 ao
.addr16
[6], an
->addr16
[6], u
),
2226 ao
.addr16
[7], an
->addr16
[7], u
);
2231 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
2232 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
2233 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
2235 struct pf_addr oia
, ooa
;
2237 PF_ACPY(&oia
, ia
, af
);
2238 PF_ACPY(&ooa
, oa
, af
);
2240 /* Change inner protocol port, fix inner protocol checksum. */
2242 u_int16_t oip
= *ip
;
2249 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
2250 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
2252 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
2254 /* Change inner ip address, fix inner ip and icmp checksums. */
2255 PF_ACPY(ia
, na
, af
);
2259 u_int32_t oh2c
= *h2c
;
2261 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
2262 oia
.addr16
[0], ia
->addr16
[0], 0),
2263 oia
.addr16
[1], ia
->addr16
[1], 0);
2264 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2265 oia
.addr16
[0], ia
->addr16
[0], 0),
2266 oia
.addr16
[1], ia
->addr16
[1], 0);
2267 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
2273 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2274 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2275 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2276 oia
.addr16
[0], ia
->addr16
[0], u
),
2277 oia
.addr16
[1], ia
->addr16
[1], u
),
2278 oia
.addr16
[2], ia
->addr16
[2], u
),
2279 oia
.addr16
[3], ia
->addr16
[3], u
),
2280 oia
.addr16
[4], ia
->addr16
[4], u
),
2281 oia
.addr16
[5], ia
->addr16
[5], u
),
2282 oia
.addr16
[6], ia
->addr16
[6], u
),
2283 oia
.addr16
[7], ia
->addr16
[7], u
);
2287 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
2288 PF_ACPY(oa
, na
, af
);
2292 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
2293 ooa
.addr16
[0], oa
->addr16
[0], 0),
2294 ooa
.addr16
[1], oa
->addr16
[1], 0);
2299 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2300 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2301 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2302 ooa
.addr16
[0], oa
->addr16
[0], u
),
2303 ooa
.addr16
[1], oa
->addr16
[1], u
),
2304 ooa
.addr16
[2], oa
->addr16
[2], u
),
2305 ooa
.addr16
[3], oa
->addr16
[3], u
),
2306 ooa
.addr16
[4], oa
->addr16
[4], u
),
2307 ooa
.addr16
[5], oa
->addr16
[5], u
),
2308 ooa
.addr16
[6], oa
->addr16
[6], u
),
2309 ooa
.addr16
[7], oa
->addr16
[7], u
);
2317 * Need to modulate the sequence numbers in the TCP SACK option
2318 * (credits to Krzysztof Pfaff for report and patch)
2321 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
2322 struct tcphdr
*th
, struct pf_state_peer
*dst
)
2324 int hlen
= (th
->th_off
<< 2) - sizeof (*th
), thoptlen
= hlen
;
2325 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
2326 int copyback
= 0, i
, olen
;
2327 struct sackblk sack
;
2329 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2330 if (hlen
< TCPOLEN_SACKLEN
||
2331 !pf_pull_hdr(m
, off
+ sizeof (*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
2334 while (hlen
>= TCPOLEN_SACKLEN
) {
2337 case TCPOPT_EOL
: /* FALLTHROUGH */
2345 if (olen
>= TCPOLEN_SACKLEN
) {
2346 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
2347 i
+= TCPOLEN_SACK
) {
2348 memcpy(&sack
, &opt
[i
], sizeof (sack
));
2349 pf_change_a(&sack
.start
, &th
->th_sum
,
2350 htonl(ntohl(sack
.start
) -
2352 pf_change_a(&sack
.end
, &th
->th_sum
,
2353 htonl(ntohl(sack
.end
) -
2355 memcpy(&opt
[i
], &sack
, sizeof (sack
));
2357 copyback
= off
+ sizeof (*th
) + thoptlen
;
2369 m
= pf_lazy_makewritable(pd
, m
, copyback
);
2372 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2378 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
2379 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
2380 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
2381 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
2382 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
2384 #pragma unused(eh, ifp)
2388 struct ip
*h
= NULL
;
2391 struct ip6_hdr
*h6
= NULL
;
2393 struct tcphdr
*th
= NULL
;
2395 struct pf_mtag
*pf_mtag
;
2397 /* maximum segment size tcp option */
2398 tlen
= sizeof (struct tcphdr
);
2405 len
= sizeof (struct ip
) + tlen
;
2410 len
= sizeof (struct ip6_hdr
) + tlen
;
2414 panic("pf_send_tcp: not AF_INET or AF_INET6!");
2418 /* create outgoing mbuf */
2419 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
2423 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
2429 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2430 pf_mtag
->pftag_tag
= rtag
;
2432 if (r
!= NULL
&& PF_RTABLEID_IS_VALID(r
->rtableid
))
2433 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2436 if (altq_allowed
&& r
!= NULL
&& r
->qid
)
2437 pf_mtag
->pftag_qid
= r
->qid
;
2438 #endif /* PF_ALTQ */
2441 /* add hints for ecn */
2442 pf_mtag
->pftag_hdr
= mtod(m
, struct ip
*);
2443 /* record address family */
2444 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2448 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2453 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2459 /* indicate this is TCP */
2460 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
2462 /* Make sure headers are 32-bit aligned */
2463 m
->m_data
+= max_linkhdr
;
2464 m
->m_pkthdr
.len
= m
->m_len
= len
;
2465 m
->m_pkthdr
.rcvif
= NULL
;
2466 bzero(m
->m_data
, len
);
2470 h
= mtod(m
, struct ip
*);
2472 /* IP header fields included in the TCP checksum */
2473 h
->ip_p
= IPPROTO_TCP
;
2474 h
->ip_len
= htons(tlen
);
2475 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
2476 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
2478 th
= (struct tcphdr
*)(void *)((caddr_t
)h
+ sizeof (struct ip
));
2483 h6
= mtod(m
, struct ip6_hdr
*);
2485 /* IP header fields included in the TCP checksum */
2486 h6
->ip6_nxt
= IPPROTO_TCP
;
2487 h6
->ip6_plen
= htons(tlen
);
2488 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof (struct in6_addr
));
2489 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof (struct in6_addr
));
2491 th
= (struct tcphdr
*)(void *)
2492 ((caddr_t
)h6
+ sizeof (struct ip6_hdr
));
2498 th
->th_sport
= sport
;
2499 th
->th_dport
= dport
;
2500 th
->th_seq
= htonl(seq
);
2501 th
->th_ack
= htonl(ack
);
2502 th
->th_off
= tlen
>> 2;
2503 th
->th_flags
= flags
;
2504 th
->th_win
= htons(win
);
2507 opt
= (char *)(th
+ 1);
2508 opt
[0] = TCPOPT_MAXSEG
;
2510 #if BYTE_ORDER != BIG_ENDIAN
2513 bcopy((caddr_t
)&mss
, (caddr_t
)(opt
+ 2), 2);
2522 th
->th_sum
= in_cksum(m
, len
);
2524 /* Finish the IP header */
2526 h
->ip_hl
= sizeof (*h
) >> 2;
2527 h
->ip_tos
= IPTOS_LOWDELAY
;
2529 * ip_output() expects ip_len and ip_off to be in host order.
2532 h
->ip_off
= (path_mtu_discovery
? IP_DF
: 0);
2533 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
2536 bzero(&ro
, sizeof (ro
));
2537 ip_output(m
, NULL
, &ro
, 0, NULL
, NULL
);
2544 struct route_in6 ro6
;
2547 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
2548 sizeof (struct ip6_hdr
), tlen
);
2550 h6
->ip6_vfc
|= IPV6_VERSION
;
2551 h6
->ip6_hlim
= IPV6_DEFHLIM
;
2553 bzero(&ro6
, sizeof (ro6
));
2554 ip6_output(m
, NULL
, &ro6
, 0, NULL
, NULL
, NULL
);
2555 ROUTE_RELEASE(&ro6
);
2563 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
2567 struct pf_mtag
*pf_mtag
;
2569 m0
= m_copy(m
, 0, M_COPYALL
);
2573 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
2576 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2578 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
2579 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2582 if (altq_allowed
&& r
->qid
)
2583 pf_mtag
->pftag_qid
= r
->qid
;
2584 #endif /* PF_ALTQ */
2587 /* add hints for ecn */
2588 pf_mtag
->pftag_hdr
= mtod(m0
, struct ip
*);
2589 /* record address family */
2590 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2594 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2595 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMP
;
2600 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2601 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMPV6
;
2610 icmp_error(m0
, type
, code
, 0, 0);
2615 icmp6_error(m0
, type
, code
, 0);
2622 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2623 * If n is 0, they match if they are equal. If n is != 0, they match if they
2627 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
2628 struct pf_addr
*b
, sa_family_t af
)
2635 if ((a
->addr32
[0] & m
->addr32
[0]) ==
2636 (b
->addr32
[0] & m
->addr32
[0]))
2642 if (((a
->addr32
[0] & m
->addr32
[0]) ==
2643 (b
->addr32
[0] & m
->addr32
[0])) &&
2644 ((a
->addr32
[1] & m
->addr32
[1]) ==
2645 (b
->addr32
[1] & m
->addr32
[1])) &&
2646 ((a
->addr32
[2] & m
->addr32
[2]) ==
2647 (b
->addr32
[2] & m
->addr32
[2])) &&
2648 ((a
->addr32
[3] & m
->addr32
[3]) ==
2649 (b
->addr32
[3] & m
->addr32
[3])))
2668 * Return 1 if b <= a <= e, otherwise return 0.
2671 pf_match_addr_range(struct pf_addr
*b
, struct pf_addr
*e
,
2672 struct pf_addr
*a
, sa_family_t af
)
2677 if ((a
->addr32
[0] < b
->addr32
[0]) ||
2678 (a
->addr32
[0] > e
->addr32
[0]))
2687 for (i
= 0; i
< 4; ++i
)
2688 if (a
->addr32
[i
] > b
->addr32
[i
])
2690 else if (a
->addr32
[i
] < b
->addr32
[i
])
2693 for (i
= 0; i
< 4; ++i
)
2694 if (a
->addr32
[i
] < e
->addr32
[i
])
2696 else if (a
->addr32
[i
] > e
->addr32
[i
])
2706 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
2710 return ((p
> a1
) && (p
< a2
));
2712 return ((p
< a1
) || (p
> a2
));
2714 return ((p
>= a1
) && (p
<= a2
));
2728 return (0); /* never reached */
2732 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
2734 #if BYTE_ORDER != BIG_ENDIAN
2739 return (pf_match(op
, a1
, a2
, p
));
2743 pf_match_xport(u_int8_t proto
, u_int8_t proto_variant
, union pf_rule_xport
*rx
,
2744 union pf_state_xport
*sx
)
2751 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
2752 d
= (rx
->call_id
== sx
->call_id
);
2756 d
= (rx
->spi
== sx
->spi
);
2762 case IPPROTO_ICMPV6
:
2764 d
= pf_match_port(rx
->range
.op
,
2765 rx
->range
.port
[0], rx
->range
.port
[1],
2778 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
2780 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2782 return (pf_match(op
, a1
, a2
, u
));
2786 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
2788 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2790 return (pf_match(op
, a1
, a2
, g
));
2794 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, struct pf_mtag
*pf_mtag
,
2799 *tag
= pf_mtag
->pftag_tag
;
2801 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2802 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2806 pf_tag_packet(struct mbuf
*m
, struct pf_mtag
*pf_mtag
, int tag
,
2807 unsigned int rtableid
, struct pf_pdesc
*pd
)
2809 if (tag
<= 0 && !PF_RTABLEID_IS_VALID(rtableid
) &&
2810 (pd
== NULL
|| !(pd
->pktflags
& PKTF_FLOW_ID
)))
2813 if (pf_mtag
== NULL
&& (pf_mtag
= pf_get_mtag(m
)) == NULL
)
2817 pf_mtag
->pftag_tag
= tag
;
2818 if (PF_RTABLEID_IS_VALID(rtableid
))
2819 pf_mtag
->pftag_rtableid
= rtableid
;
2820 if (pd
!= NULL
&& (pd
->pktflags
& PKTF_FLOW_ID
)) {
2821 m
->m_pkthdr
.pkt_flowsrc
= pd
->flowsrc
;
2822 m
->m_pkthdr
.pkt_flowid
= pd
->flowhash
;
2823 m
->m_pkthdr
.pkt_flags
|= pd
->pktflags
;
2824 m
->m_pkthdr
.pkt_proto
= pd
->proto
;
2831 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2832 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2834 struct pf_anchor_stackframe
*f
;
2836 (*r
)->anchor
->match
= 0;
2839 if (*depth
>= (int)sizeof (pf_anchor_stack
) /
2840 (int)sizeof (pf_anchor_stack
[0])) {
2841 printf("pf_step_into_anchor: stack overflow\n");
2842 *r
= TAILQ_NEXT(*r
, entries
);
2844 } else if (*depth
== 0 && a
!= NULL
)
2846 f
= pf_anchor_stack
+ (*depth
)++;
2849 if ((*r
)->anchor_wildcard
) {
2850 f
->parent
= &(*r
)->anchor
->children
;
2851 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2856 *rs
= &f
->child
->ruleset
;
2860 *rs
= &(*r
)->anchor
->ruleset
;
2862 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2866 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2867 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2869 struct pf_anchor_stackframe
*f
;
2875 f
= pf_anchor_stack
+ *depth
- 1;
2876 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2877 if (f
->child
->match
||
2878 (match
!= NULL
&& *match
)) {
2879 f
->r
->anchor
->match
= 1;
2882 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
2883 if (f
->child
!= NULL
) {
2884 *rs
= &f
->child
->ruleset
;
2885 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2893 if (*depth
== 0 && a
!= NULL
)
2896 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
2897 quick
= f
->r
->quick
;
2898 *r
= TAILQ_NEXT(f
->r
, entries
);
2899 } while (*r
== NULL
);
2906 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
2907 struct pf_addr
*rmask
, struct pf_addr
*saddr
, sa_family_t af
)
2912 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2913 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2917 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2918 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2919 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
2920 ((rmask
->addr32
[1] ^ 0xffffffff) & saddr
->addr32
[1]);
2921 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
2922 ((rmask
->addr32
[2] ^ 0xffffffff) & saddr
->addr32
[2]);
2923 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
2924 ((rmask
->addr32
[3] ^ 0xffffffff) & saddr
->addr32
[3]);
2930 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
2935 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
2939 if (addr
->addr32
[3] == 0xffffffff) {
2940 addr
->addr32
[3] = 0;
2941 if (addr
->addr32
[2] == 0xffffffff) {
2942 addr
->addr32
[2] = 0;
2943 if (addr
->addr32
[1] == 0xffffffff) {
2944 addr
->addr32
[1] = 0;
2946 htonl(ntohl(addr
->addr32
[0]) + 1);
2949 htonl(ntohl(addr
->addr32
[1]) + 1);
2952 htonl(ntohl(addr
->addr32
[2]) + 1);
2955 htonl(ntohl(addr
->addr32
[3]) + 1);
2961 #define mix(a, b, c) \
2963 a -= b; a -= c; a ^= (c >> 13); \
2964 b -= c; b -= a; b ^= (a << 8); \
2965 c -= a; c -= b; c ^= (b >> 13); \
2966 a -= b; a -= c; a ^= (c >> 12); \
2967 b -= c; b -= a; b ^= (a << 16); \
2968 c -= a; c -= b; c ^= (b >> 5); \
2969 a -= b; a -= c; a ^= (c >> 3); \
2970 b -= c; b -= a; b ^= (a << 10); \
2971 c -= a; c -= b; c ^= (b >> 15); \
2975 * hash function based on bridge_hash in if_bridge.c
2978 pf_hash(struct pf_addr
*inaddr
, struct pf_addr
*hash
,
2979 struct pf_poolhashkey
*key
, sa_family_t af
)
2981 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
2986 a
+= inaddr
->addr32
[0];
2989 hash
->addr32
[0] = c
+ key
->key32
[2];
2994 a
+= inaddr
->addr32
[0];
2995 b
+= inaddr
->addr32
[2];
2997 hash
->addr32
[0] = c
;
2998 a
+= inaddr
->addr32
[1];
2999 b
+= inaddr
->addr32
[3];
3002 hash
->addr32
[1] = c
;
3003 a
+= inaddr
->addr32
[2];
3004 b
+= inaddr
->addr32
[1];
3007 hash
->addr32
[2] = c
;
3008 a
+= inaddr
->addr32
[3];
3009 b
+= inaddr
->addr32
[0];
3012 hash
->addr32
[3] = c
;
3019 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, struct pf_addr
*saddr
,
3020 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
3022 unsigned char hash
[16];
3023 struct pf_pool
*rpool
= &r
->rpool
;
3024 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3025 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3026 struct pf_pooladdr
*acur
= rpool
->cur
;
3027 struct pf_src_node k
;
3029 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
3030 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3032 PF_ACPY(&k
.addr
, saddr
, af
);
3033 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
3034 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
3038 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
3039 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
3040 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, af
)) {
3041 PF_ACPY(naddr
, &(*sn
)->raddr
, af
);
3042 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
3043 printf("pf_map_addr: src tracking maps ");
3044 pf_print_host(&k
.addr
, 0, af
);
3046 pf_print_host(naddr
, 0, af
);
3053 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
3055 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3056 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3061 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
3062 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3065 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
3066 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
3071 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
3072 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3075 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
3076 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
3080 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3081 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
3082 return (1); /* unsupported */
3084 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3085 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3088 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
3090 PF_ACPY(naddr
, raddr
, af
);
3092 case PF_POOL_BITMASK
:
3093 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3095 case PF_POOL_RANDOM
:
3096 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
)) {
3100 rpool
->counter
.addr32
[0] = htonl(random());
3105 if (rmask
->addr32
[3] != 0xffffffff)
3106 rpool
->counter
.addr32
[3] =
3110 if (rmask
->addr32
[2] != 0xffffffff)
3111 rpool
->counter
.addr32
[2] =
3115 if (rmask
->addr32
[1] != 0xffffffff)
3116 rpool
->counter
.addr32
[1] =
3120 if (rmask
->addr32
[0] != 0xffffffff)
3121 rpool
->counter
.addr32
[0] =
3126 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3127 PF_ACPY(init_addr
, naddr
, af
);
3130 PF_AINC(&rpool
->counter
, af
);
3131 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3134 case PF_POOL_SRCHASH
:
3135 pf_hash(saddr
, (struct pf_addr
*)(void *)&hash
,
3137 PF_POOLMASK(naddr
, raddr
, rmask
,
3138 (struct pf_addr
*)(void *)&hash
, af
);
3140 case PF_POOL_ROUNDROBIN
:
3141 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3142 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3143 &rpool
->tblidx
, &rpool
->counter
,
3144 &raddr
, &rmask
, af
))
3146 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3147 if (rpool
->cur
->addr
.p
.dyn
!= NULL
&&
3148 !pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3149 &rpool
->tblidx
, &rpool
->counter
,
3150 &raddr
, &rmask
, af
))
3152 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
, af
))
3156 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
3157 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
3158 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3160 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3161 &rpool
->tblidx
, &rpool
->counter
,
3162 &raddr
, &rmask
, af
)) {
3163 /* table contains no address of type 'af' */
3164 if (rpool
->cur
!= acur
)
3168 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3170 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3172 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3173 &rpool
->tblidx
, &rpool
->counter
,
3174 &raddr
, &rmask
, af
)) {
3175 /* table contains no address of type 'af' */
3176 if (rpool
->cur
!= acur
)
3181 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3182 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3183 PF_ACPY(&rpool
->counter
, raddr
, af
);
3187 PF_ACPY(naddr
, &rpool
->counter
, af
);
3188 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
))
3189 PF_ACPY(init_addr
, naddr
, af
);
3190 PF_AINC(&rpool
->counter
, af
);
3194 PF_ACPY(&(*sn
)->raddr
, naddr
, af
);
3196 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
3197 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3198 printf("pf_map_addr: selected address ");
3199 pf_print_host(naddr
, 0, af
);
3207 pf_get_sport(struct pf_pdesc
*pd
, struct pfi_kif
*kif
, struct pf_rule
*r
,
3208 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3209 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3210 union pf_state_xport
*nxport
, struct pf_src_node
**sn
)
3213 struct pf_state_key_cmp key
;
3214 struct pf_addr init_addr
;
3216 sa_family_t af
= pd
->af
;
3217 u_int8_t proto
= pd
->proto
;
3218 unsigned int low
= r
->rpool
.proxy_port
[0];
3219 unsigned int high
= r
->rpool
.proxy_port
[1];
3221 bzero(&init_addr
, sizeof (init_addr
));
3222 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3225 if (proto
== IPPROTO_ICMP
) {
3231 return (0); /* No output necessary. */
3233 /*--- Special mapping rules for UDP ---*/
3234 if (proto
== IPPROTO_UDP
) {
3236 /*--- Never float IKE source port ---*/
3237 if (ntohs(sxport
->port
) == PF_IKE_PORT
) {
3238 nxport
->port
= sxport
->port
;
3242 /*--- Apply exterior mapping options ---*/
3243 if (r
->extmap
> PF_EXTMAP_APD
) {
3246 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3247 struct pf_state_key
*sk
= s
->state_key
;
3250 if (s
->nat_rule
.ptr
!= r
)
3252 if (sk
->proto
!= IPPROTO_UDP
|| sk
->af
!= af
)
3254 if (sk
->lan
.xport
.port
!= sxport
->port
)
3256 if (PF_ANEQ(&sk
->lan
.addr
, saddr
, af
))
3258 if (r
->extmap
< PF_EXTMAP_EI
&&
3259 PF_ANEQ(&sk
->ext
.addr
, daddr
, af
))
3262 nxport
->port
= sk
->gwy
.xport
.port
;
3266 } else if (proto
== IPPROTO_TCP
) {
3269 * APPLE MODIFICATION: <rdar://problem/6546358>
3270 * Fix allows....NAT to use a single binding for TCP session
3271 * with same source IP and source port
3273 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3274 struct pf_state_key
* sk
= s
->state_key
;
3277 if (s
->nat_rule
.ptr
!= r
)
3279 if (sk
->proto
!= IPPROTO_TCP
|| sk
->af
!= af
)
3281 if (sk
->lan
.xport
.port
!= sxport
->port
)
3283 if (!(PF_AEQ(&sk
->lan
.addr
, saddr
, af
)))
3285 nxport
->port
= sk
->gwy
.xport
.port
;
3292 PF_ACPY(&key
.ext
.addr
, daddr
, key
.af
);
3293 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af
);
3296 key
.proto_variant
= r
->extfilter
;
3299 key
.proto_variant
= 0;
3303 key
.ext
.xport
= *dxport
;
3305 memset(&key
.ext
.xport
, 0, sizeof (key
.ext
.xport
));
3307 * port search; start random, step;
3308 * similar 2 portloop in in_pcbbind
3310 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
3311 proto
== IPPROTO_ICMP
)) {
3313 key
.gwy
.xport
= *dxport
;
3315 memset(&key
.gwy
.xport
, 0,
3316 sizeof (key
.ext
.xport
));
3317 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3319 } else if (low
== 0 && high
== 0) {
3320 key
.gwy
.xport
= *nxport
;
3321 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3323 } else if (low
== high
) {
3324 key
.gwy
.xport
.port
= htons(low
);
3325 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3326 nxport
->port
= htons(low
);
3337 cut
= htonl(random()) % (1 + high
- low
) + low
;
3338 /* low <= cut <= high */
3339 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
3340 key
.gwy
.xport
.port
= htons(tmp
);
3341 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3343 nxport
->port
= htons(tmp
);
3347 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
3348 key
.gwy
.xport
.port
= htons(tmp
);
3349 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3351 nxport
->port
= htons(tmp
);
3357 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
3358 case PF_POOL_RANDOM
:
3359 case PF_POOL_ROUNDROBIN
:
3360 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3364 case PF_POOL_SRCHASH
:
3365 case PF_POOL_BITMASK
:
3369 } while (!PF_AEQ(&init_addr
, naddr
, af
));
3371 return (1); /* none available */
3374 static struct pf_rule
*
3375 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3376 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
,
3377 union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3378 union pf_state_xport
*dxport
, int rs_num
)
3380 struct pf_rule
*r
, *rm
= NULL
;
3381 struct pf_ruleset
*ruleset
= NULL
;
3383 unsigned int rtableid
= IFSCOPE_NONE
;
3386 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
3387 while (r
&& rm
== NULL
) {
3388 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
3389 struct pf_addr_wrap
*xdst
= NULL
;
3390 struct pf_addr_wrap
*xsrc
= NULL
;
3391 union pf_rule_xport rdrxport
;
3393 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
3395 if (r
->rpool
.cur
!= NULL
)
3396 xdst
= &r
->rpool
.cur
->addr
;
3397 } else if (r
->action
== PF_RDR
&& direction
== PF_OUT
) {
3400 if (r
->rpool
.cur
!= NULL
) {
3401 rdrxport
.range
.op
= PF_OP_EQ
;
3402 rdrxport
.range
.port
[0] =
3403 htons(r
->rpool
.proxy_port
[0]);
3404 xsrc
= &r
->rpool
.cur
->addr
;
3412 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3413 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3414 else if (r
->direction
&& r
->direction
!= direction
)
3415 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3416 else if (r
->af
&& r
->af
!= pd
->af
)
3417 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3418 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3419 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3420 else if (xsrc
&& PF_MISMATCHAW(xsrc
, saddr
, pd
->af
, 0, NULL
))
3421 r
= TAILQ_NEXT(r
, entries
);
3422 else if (!xsrc
&& PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3424 r
= TAILQ_NEXT(r
, entries
);
3425 else if (xsrc
&& (!rdrxport
.range
.port
[0] ||
3426 !pf_match_xport(r
->proto
, r
->proto_variant
, &rdrxport
,
3428 r
= TAILQ_NEXT(r
, entries
);
3429 else if (!xsrc
&& !pf_match_xport(r
->proto
,
3430 r
->proto_variant
, &src
->xport
, sxport
))
3431 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
3432 PF_SKIP_DST_PORT
].ptr
;
3433 else if (dst
!= NULL
&&
3434 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
3435 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3436 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
3438 r
= TAILQ_NEXT(r
, entries
);
3439 else if (dst
&& !pf_match_xport(r
->proto
, r
->proto_variant
,
3440 &dst
->xport
, dxport
))
3441 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3442 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
3443 r
= TAILQ_NEXT(r
, entries
);
3444 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
3445 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
3446 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
3447 r
= TAILQ_NEXT(r
, entries
);
3451 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
3452 rtableid
= r
->rtableid
;
3453 if (r
->anchor
== NULL
) {
3456 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
3460 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
3463 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, NULL
))
3465 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
3466 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
))
3471 static struct pf_rule
*
3472 pf_get_translation_aux(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3473 int direction
, struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3474 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3475 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3476 union pf_state_xport
*nxport
)
3478 struct pf_rule
*r
= NULL
;
3480 if (direction
== PF_OUT
) {
3481 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3482 sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3484 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3485 saddr
, sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3487 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3488 saddr
, sxport
, daddr
, dxport
, PF_RULESET_NAT
);
3490 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3491 sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3493 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3494 saddr
, sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3498 switch (r
->action
) {
3504 if (pf_get_sport(pd
, kif
, r
, saddr
, sxport
, daddr
,
3505 dxport
, naddr
, nxport
, sn
)) {
3506 DPFPRINTF(PF_DEBUG_MISC
,
3507 ("pf: NAT proxy port allocation "
3509 r
->rpool
.proxy_port
[0],
3510 r
->rpool
.proxy_port
[1]));
3515 switch (direction
) {
3517 if (r
->rpool
.cur
->addr
.type
==
3519 if (r
->rpool
.cur
->addr
.p
.dyn
== NULL
)
3524 if (r
->rpool
.cur
->addr
.p
.dyn
->
3528 &r
->rpool
.cur
->addr
.p
.dyn
->
3530 &r
->rpool
.cur
->addr
.p
.dyn
->
3537 if (r
->rpool
.cur
->addr
.p
.dyn
->
3541 &r
->rpool
.cur
->addr
.p
.dyn
->
3543 &r
->rpool
.cur
->addr
.p
.dyn
->
3551 &r
->rpool
.cur
->addr
.v
.a
.addr
,
3552 &r
->rpool
.cur
->addr
.v
.a
.mask
,
3557 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
3558 if (r
->src
.addr
.p
.dyn
== NULL
)
3563 if (r
->src
.addr
.p
.dyn
->
3567 &r
->src
.addr
.p
.dyn
->
3569 &r
->src
.addr
.p
.dyn
->
3576 if (r
->src
.addr
.p
.dyn
->
3580 &r
->src
.addr
.p
.dyn
->
3582 &r
->src
.addr
.p
.dyn
->
3590 &r
->src
.addr
.v
.a
.addr
,
3591 &r
->src
.addr
.v
.a
.mask
, daddr
,
3597 switch (direction
) {
3599 if (r
->dst
.addr
.type
== PF_ADDR_DYNIFTL
) {
3600 if (r
->dst
.addr
.p
.dyn
== NULL
)
3605 if (r
->dst
.addr
.p
.dyn
->
3609 &r
->dst
.addr
.p
.dyn
->
3611 &r
->dst
.addr
.p
.dyn
->
3618 if (r
->dst
.addr
.p
.dyn
->
3622 &r
->dst
.addr
.p
.dyn
->
3624 &r
->dst
.addr
.p
.dyn
->
3632 &r
->dst
.addr
.v
.a
.addr
,
3633 &r
->dst
.addr
.v
.a
.mask
,
3636 if (nxport
&& r
->dst
.xport
.range
.port
[0])
3638 r
->dst
.xport
.range
.port
[0];
3641 if (pf_map_addr(pd
->af
, r
, saddr
,
3644 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3646 PF_POOLMASK(naddr
, naddr
,
3647 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3650 if (nxport
&& dxport
) {
3651 if (r
->rpool
.proxy_port
[1]) {
3652 u_int32_t tmp_nport
;
3655 ((ntohs(dxport
->port
) -
3656 ntohs(r
->dst
.xport
.range
.
3658 (r
->rpool
.proxy_port
[1] -
3659 r
->rpool
.proxy_port
[0] +
3660 1)) + r
->rpool
.proxy_port
[0];
3662 /* wrap around if necessary */
3663 if (tmp_nport
> 65535)
3666 htons((u_int16_t
)tmp_nport
);
3667 } else if (r
->rpool
.proxy_port
[0]) {
3668 nxport
->port
= htons(r
->rpool
.
3685 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
3687 struct pf_addr
*saddr
, *daddr
;
3688 u_int16_t sport
, dport
;
3689 struct inpcbinfo
*pi
;
3694 pd
->lookup
.uid
= UID_MAX
;
3695 pd
->lookup
.gid
= GID_MAX
;
3696 pd
->lookup
.pid
= NO_PID
;
3698 switch (pd
->proto
) {
3700 if (pd
->hdr
.tcp
== NULL
)
3702 sport
= pd
->hdr
.tcp
->th_sport
;
3703 dport
= pd
->hdr
.tcp
->th_dport
;
3707 if (pd
->hdr
.udp
== NULL
)
3709 sport
= pd
->hdr
.udp
->uh_sport
;
3710 dport
= pd
->hdr
.udp
->uh_dport
;
3716 if (direction
== PF_IN
) {
3731 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
, daddr
->v4
, dport
,
3732 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3735 struct in6_addr s6
, d6
;
3737 memset(&s6
, 0, sizeof (s6
));
3738 s6
.s6_addr16
[5] = htons(0xffff);
3739 memcpy(&s6
.s6_addr32
[3], &saddr
->v4
,
3740 sizeof (saddr
->v4
));
3742 memset(&d6
, 0, sizeof (d6
));
3743 d6
.s6_addr16
[5] = htons(0xffff);
3744 memcpy(&d6
.s6_addr32
[3], &daddr
->v4
,
3745 sizeof (daddr
->v4
));
3747 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3748 &d6
, dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3750 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3751 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3753 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3754 &d6
, dport
, INPLOOKUP_WILDCARD
,
3755 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3763 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3764 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
,
3765 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3774 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
, &daddr
->v6
,
3775 dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3777 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
,
3778 &daddr
->v6
, dport
, INPLOOKUP_WILDCARD
,
3779 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3794 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3798 u_int8_t
*opt
, optlen
;
3799 u_int8_t wscale
= 0;
3801 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3802 if (hlen
<= (int)sizeof (struct tcphdr
))
3804 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3806 opt
= hdr
+ sizeof (struct tcphdr
);
3807 hlen
-= sizeof (struct tcphdr
);
3817 if (wscale
> TCP_MAX_WINSHIFT
)
3818 wscale
= TCP_MAX_WINSHIFT
;
3819 wscale
|= PF_WSCALE_FLAG
;
3834 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3838 u_int8_t
*opt
, optlen
;
3839 u_int16_t mss
= tcp_mssdflt
;
3841 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3842 if (hlen
<= (int)sizeof (struct tcphdr
))
3844 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3846 opt
= hdr
+ sizeof (struct tcphdr
);
3847 hlen
-= sizeof (struct tcphdr
);
3848 while (hlen
>= TCPOLEN_MAXSEG
) {
3856 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, 2);
3857 #if BYTE_ORDER != BIG_ENDIAN
3874 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
3877 struct sockaddr_in
*dst
;
3881 struct sockaddr_in6
*dst6
;
3882 struct route_in6 ro6
;
3884 struct rtentry
*rt
= NULL
;
3886 u_int16_t mss
= tcp_mssdflt
;
3891 hlen
= sizeof (struct ip
);
3892 bzero(&ro
, sizeof (ro
));
3893 dst
= (struct sockaddr_in
*)(void *)&ro
.ro_dst
;
3894 dst
->sin_family
= AF_INET
;
3895 dst
->sin_len
= sizeof (*dst
);
3896 dst
->sin_addr
= addr
->v4
;
3903 hlen
= sizeof (struct ip6_hdr
);
3904 bzero(&ro6
, sizeof (ro6
));
3905 dst6
= (struct sockaddr_in6
*)(void *)&ro6
.ro_dst
;
3906 dst6
->sin6_family
= AF_INET6
;
3907 dst6
->sin6_len
= sizeof (*dst6
);
3908 dst6
->sin6_addr
= addr
->v6
;
3909 rtalloc((struct route
*)&ro
);
3914 panic("pf_calc_mss: not AF_INET or AF_INET6!");
3918 if (rt
&& rt
->rt_ifp
) {
3919 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof (struct tcphdr
);
3920 mss
= max(tcp_mssdflt
, mss
);
3923 mss
= min(mss
, offer
);
3924 mss
= max(mss
, 64); /* sanity - at least max opt space */
3929 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
)
3931 struct pf_rule
*r
= s
->rule
.ptr
;
3934 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
3936 switch (s
->state_key
->af
) {
3939 pf_map_addr(AF_INET
, r
, saddr
, &s
->rt_addr
, NULL
,
3941 s
->rt_kif
= r
->rpool
.cur
->kif
;
3946 pf_map_addr(AF_INET6
, r
, saddr
, &s
->rt_addr
, NULL
,
3948 s
->rt_kif
= r
->rpool
.cur
->kif
;
3955 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
3960 /* list is sorted, if-bound states before floating */
3962 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
3964 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
3968 pf_detach_state(struct pf_state
*s
, int flags
)
3970 struct pf_state_key
*sk
= s
->state_key
;
3975 s
->state_key
= NULL
;
3976 TAILQ_REMOVE(&sk
->states
, s
, next
);
3977 if (--sk
->refcnt
== 0) {
3978 if (!(flags
& PF_DT_SKIP_EXTGWY
))
3979 RB_REMOVE(pf_state_tree_ext_gwy
,
3980 &pf_statetbl_ext_gwy
, sk
);
3981 if (!(flags
& PF_DT_SKIP_LANEXT
))
3982 RB_REMOVE(pf_state_tree_lan_ext
,
3983 &pf_statetbl_lan_ext
, sk
);
3985 pool_put(&pf_app_state_pl
, sk
->app_state
);
3986 pool_put(&pf_state_key_pl
, sk
);
3990 struct pf_state_key
*
3991 pf_alloc_state_key(struct pf_state
*s
, struct pf_state_key
*psk
)
3993 struct pf_state_key
*sk
;
3995 if ((sk
= pool_get(&pf_state_key_pl
, PR_WAITOK
)) == NULL
)
3997 bzero(sk
, sizeof (*sk
));
3998 TAILQ_INIT(&sk
->states
);
3999 pf_attach_state(sk
, s
, 0);
4001 /* initialize state key from psk, if provided */
4003 bcopy(&psk
->lan
, &sk
->lan
, sizeof (sk
->lan
));
4004 bcopy(&psk
->gwy
, &sk
->gwy
, sizeof (sk
->gwy
));
4005 bcopy(&psk
->ext
, &sk
->ext
, sizeof (sk
->ext
));
4007 sk
->proto
= psk
->proto
;
4008 sk
->direction
= psk
->direction
;
4009 sk
->proto_variant
= psk
->proto_variant
;
4010 VERIFY(psk
->app_state
== NULL
);
4011 sk
->flowsrc
= psk
->flowsrc
;
4012 sk
->flowhash
= psk
->flowhash
;
4013 /* don't touch tree entries, states and refcnt on sk */
4020 pf_tcp_iss(struct pf_pdesc
*pd
)
4023 u_int32_t digest
[4];
4025 if (pf_tcp_secret_init
== 0) {
4026 read_random(pf_tcp_secret
, sizeof (pf_tcp_secret
));
4027 MD5Init(&pf_tcp_secret_ctx
);
4028 MD5Update(&pf_tcp_secret_ctx
, pf_tcp_secret
,
4029 sizeof (pf_tcp_secret
));
4030 pf_tcp_secret_init
= 1;
4032 ctx
= pf_tcp_secret_ctx
;
4034 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_sport
, sizeof (u_short
));
4035 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_dport
, sizeof (u_short
));
4036 if (pd
->af
== AF_INET6
) {
4037 MD5Update(&ctx
, (char *)&pd
->src
->v6
, sizeof (struct in6_addr
));
4038 MD5Update(&ctx
, (char *)&pd
->dst
->v6
, sizeof (struct in6_addr
));
4040 MD5Update(&ctx
, (char *)&pd
->src
->v4
, sizeof (struct in_addr
));
4041 MD5Update(&ctx
, (char *)&pd
->dst
->v4
, sizeof (struct in_addr
));
4043 MD5Final((u_char
*)digest
, &ctx
);
4044 pf_tcp_iss_off
+= 4096;
4045 return (digest
[0] + random() + pf_tcp_iss_off
);
4049 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
4050 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
4051 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
4052 struct ifqueue
*ifq
)
4055 struct pf_rule
*nr
= NULL
;
4056 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4057 sa_family_t af
= pd
->af
;
4058 struct pf_rule
*r
, *a
= NULL
;
4059 struct pf_ruleset
*ruleset
= NULL
;
4060 struct pf_src_node
*nsn
= NULL
;
4061 struct tcphdr
*th
= pd
->hdr
.tcp
;
4063 int rewrite
= 0, hdrlen
= 0;
4065 unsigned int rtableid
= IFSCOPE_NONE
;
4069 u_int16_t mss
= tcp_mssdflt
;
4070 u_int8_t icmptype
= 0, icmpcode
= 0;
4072 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
4073 union pf_state_xport bxport
, nxport
, sxport
, dxport
;
4074 struct pf_state_key psk
;
4076 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
4078 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
4079 REASON_SET(&reason
, PFRES_CONGEST
);
4088 switch (pd
->proto
) {
4090 sxport
.port
= th
->th_sport
;
4091 dxport
.port
= th
->th_dport
;
4092 hdrlen
= sizeof (*th
);
4095 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4096 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4097 hdrlen
= sizeof (*pd
->hdr
.udp
);
4101 if (pd
->af
!= AF_INET
)
4103 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
4104 hdrlen
= ICMP_MINLEN
;
4105 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4106 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
4108 if (icmptype
== ICMP_UNREACH
||
4109 icmptype
== ICMP_SOURCEQUENCH
||
4110 icmptype
== ICMP_REDIRECT
||
4111 icmptype
== ICMP_TIMXCEED
||
4112 icmptype
== ICMP_PARAMPROB
)
4117 case IPPROTO_ICMPV6
:
4118 if (pd
->af
!= AF_INET6
)
4120 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
4121 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
4122 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4123 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
4125 if (icmptype
== ICMP6_DST_UNREACH
||
4126 icmptype
== ICMP6_PACKET_TOO_BIG
||
4127 icmptype
== ICMP6_TIME_EXCEEDED
||
4128 icmptype
== ICMP6_PARAM_PROB
)
4133 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
4134 sxport
.call_id
= dxport
.call_id
=
4135 pd
->hdr
.grev1
->call_id
;
4136 hdrlen
= sizeof (*pd
->hdr
.grev1
);
4141 dxport
.spi
= pd
->hdr
.esp
->spi
;
4142 hdrlen
= sizeof (*pd
->hdr
.esp
);
4146 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
4148 if (direction
== PF_OUT
) {
4149 bxport
= nxport
= sxport
;
4150 /* check outgoing packet for BINAT/NAT */
4151 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
4152 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4154 PF_ACPY(&pd
->baddr
, saddr
, af
);
4155 switch (pd
->proto
) {
4157 pf_change_ap(direction
, pd
->mp
, saddr
,
4158 &th
->th_sport
, pd
->ip_sum
, &th
->th_sum
,
4159 &pd
->naddr
, nxport
.port
, 0, af
);
4160 sxport
.port
= th
->th_sport
;
4164 pf_change_ap(direction
, pd
->mp
, saddr
,
4165 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4166 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4167 nxport
.port
, 1, af
);
4168 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4173 if (pd
->af
== AF_INET
) {
4174 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
4175 pd
->naddr
.v4
.s_addr
, 0);
4176 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4177 pd
->hdr
.icmp
->icmp_cksum
, sxport
.port
,
4179 pd
->hdr
.icmp
->icmp_id
= nxport
.port
;
4185 case IPPROTO_ICMPV6
:
4186 if (pd
->af
== AF_INET6
) {
4187 pf_change_a6(saddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4197 pf_change_a(&saddr
->v4
.s_addr
,
4198 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4203 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4214 pf_change_a(&saddr
->v4
.s_addr
,
4215 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4220 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4229 pf_change_a(&saddr
->v4
.s_addr
,
4230 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4235 PF_ACPY(saddr
, &pd
->naddr
, af
);
4247 bxport
.port
= nxport
.port
= dxport
.port
;
4248 /* check incoming packet for BINAT/RDR */
4249 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_IN
, kif
, &nsn
,
4250 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4252 PF_ACPY(&pd
->baddr
, daddr
, af
);
4253 switch (pd
->proto
) {
4255 pf_change_ap(direction
, pd
->mp
, daddr
,
4256 &th
->th_dport
, pd
->ip_sum
, &th
->th_sum
,
4257 &pd
->naddr
, nxport
.port
, 0, af
);
4258 dxport
.port
= th
->th_dport
;
4262 pf_change_ap(direction
, pd
->mp
, daddr
,
4263 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4264 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4265 nxport
.port
, 1, af
);
4266 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4271 if (pd
->af
== AF_INET
) {
4272 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
4273 pd
->naddr
.v4
.s_addr
, 0);
4278 case IPPROTO_ICMPV6
:
4279 if (pd
->af
== AF_INET6
) {
4280 pf_change_a6(daddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4287 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
4288 grev1
->call_id
= nxport
.call_id
;
4293 pf_change_a(&daddr
->v4
.s_addr
,
4294 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4299 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4309 pf_change_a(&daddr
->v4
.s_addr
,
4310 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4315 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4324 pf_change_a(&daddr
->v4
.s_addr
,
4325 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4330 PF_ACPY(daddr
, &pd
->naddr
, af
);
4343 if (nr
&& nr
->tag
> 0)
4348 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
4349 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
4350 else if (r
->direction
&& r
->direction
!= direction
)
4351 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
4352 else if (r
->af
&& r
->af
!= af
)
4353 r
= r
->skip
[PF_SKIP_AF
].ptr
;
4354 else if (r
->proto
&& r
->proto
!= pd
->proto
)
4355 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
4356 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
4358 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
4359 /* tcp/udp only. port_op always 0 in other cases */
4360 else if (r
->proto
== pd
->proto
&&
4361 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4362 r
->src
.xport
.range
.op
&&
4363 !pf_match_port(r
->src
.xport
.range
.op
,
4364 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
4366 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
4367 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
4369 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
4370 /* tcp/udp only. port_op always 0 in other cases */
4371 else if (r
->proto
== pd
->proto
&&
4372 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4373 r
->dst
.xport
.range
.op
&&
4374 !pf_match_port(r
->dst
.xport
.range
.op
,
4375 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
4377 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
4378 /* icmp only. type always 0 in other cases */
4379 else if (r
->type
&& r
->type
!= icmptype
+ 1)
4380 r
= TAILQ_NEXT(r
, entries
);
4381 /* icmp only. type always 0 in other cases */
4382 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
4383 r
= TAILQ_NEXT(r
, entries
);
4384 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
4385 !(r
->tos
& pd
->tos
))
4386 r
= TAILQ_NEXT(r
, entries
);
4387 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
4388 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
4389 r
= TAILQ_NEXT(r
, entries
);
4390 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
4391 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
4392 r
= TAILQ_NEXT(r
, entries
);
4393 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
4394 r
= TAILQ_NEXT(r
, entries
);
4395 else if (pd
->proto
== IPPROTO_TCP
&&
4396 (r
->flagset
& th
->th_flags
) != r
->flags
)
4397 r
= TAILQ_NEXT(r
, entries
);
4398 /* tcp/udp only. uid.op always 0 in other cases */
4399 else if (r
->uid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4400 pf_socket_lookup(direction
, pd
), 1)) &&
4401 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
4403 r
= TAILQ_NEXT(r
, entries
);
4404 /* tcp/udp only. gid.op always 0 in other cases */
4405 else if (r
->gid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4406 pf_socket_lookup(direction
, pd
), 1)) &&
4407 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
4409 r
= TAILQ_NEXT(r
, entries
);
4410 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
4411 r
= TAILQ_NEXT(r
, entries
);
4412 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
4413 r
= TAILQ_NEXT(r
, entries
);
4414 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
4415 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
4416 pf_osfp_fingerprint(pd
, m
, off
, th
),
4417 r
->os_fingerprint
)))
4418 r
= TAILQ_NEXT(r
, entries
);
4422 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
4423 rtableid
= r
->rtableid
;
4424 if (r
->anchor
== NULL
) {
4431 r
= TAILQ_NEXT(r
, entries
);
4433 pf_step_into_anchor(&asd
, &ruleset
,
4434 PF_RULESET_FILTER
, &r
, &a
, &match
);
4436 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
4437 PF_RULESET_FILTER
, &r
, &a
, &match
))
4444 REASON_SET(&reason
, PFRES_MATCH
);
4446 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
4448 if (rewrite
< off
+ hdrlen
)
4449 rewrite
= off
+ hdrlen
;
4451 m
= pf_lazy_makewritable(pd
, m
, rewrite
);
4453 REASON_SET(&reason
, PFRES_MEMORY
);
4457 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
4459 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
->log
? r
: nr
,
4463 if ((r
->action
== PF_DROP
) &&
4464 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4465 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
4466 (r
->rule_flag
& PFRULE_RETURN
))) {
4467 /* undo NAT changes, if they have taken place */
4469 if (direction
== PF_OUT
) {
4470 switch (pd
->proto
) {
4472 pf_change_ap(direction
, pd
->mp
, saddr
,
4473 &th
->th_sport
, pd
->ip_sum
,
4474 &th
->th_sum
, &pd
->baddr
,
4475 bxport
.port
, 0, af
);
4476 sxport
.port
= th
->th_sport
;
4480 pf_change_ap(direction
, pd
->mp
, saddr
,
4481 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4482 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4483 bxport
.port
, 1, af
);
4484 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4489 case IPPROTO_ICMPV6
:
4494 PF_ACPY(&pd
->baddr
, saddr
, af
);
4499 pf_change_a(&saddr
->v4
.s_addr
,
4501 pd
->baddr
.v4
.s_addr
, 0);
4506 PF_ACPY(saddr
, &pd
->baddr
,
4513 PF_ACPY(&pd
->baddr
, saddr
, af
);
4517 pf_change_a(&saddr
->v4
.s_addr
,
4519 pd
->baddr
.v4
.s_addr
, 0);
4524 PF_ACPY(saddr
, &pd
->baddr
,
4533 pf_change_a(&saddr
->v4
.s_addr
,
4535 pd
->baddr
.v4
.s_addr
, 0);
4538 PF_ACPY(saddr
, &pd
->baddr
, af
);
4543 switch (pd
->proto
) {
4545 pf_change_ap(direction
, pd
->mp
, daddr
,
4546 &th
->th_dport
, pd
->ip_sum
,
4547 &th
->th_sum
, &pd
->baddr
,
4548 bxport
.port
, 0, af
);
4549 dxport
.port
= th
->th_dport
;
4553 pf_change_ap(direction
, pd
->mp
, daddr
,
4554 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4555 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4556 bxport
.port
, 1, af
);
4557 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4562 case IPPROTO_ICMPV6
:
4567 if (pd
->proto_variant
==
4568 PF_GRE_PPTP_VARIANT
)
4569 grev1
->call_id
= bxport
.call_id
;
4574 pf_change_a(&daddr
->v4
.s_addr
,
4576 pd
->baddr
.v4
.s_addr
, 0);
4581 PF_ACPY(daddr
, &pd
->baddr
,
4591 pf_change_a(&daddr
->v4
.s_addr
,
4593 pd
->baddr
.v4
.s_addr
, 0);
4598 PF_ACPY(daddr
, &pd
->baddr
,
4607 pf_change_a(&daddr
->v4
.s_addr
,
4609 pd
->baddr
.v4
.s_addr
, 0);
4613 PF_ACPY(daddr
, &pd
->baddr
, af
);
4620 if (pd
->proto
== IPPROTO_TCP
&&
4621 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4622 (r
->rule_flag
& PFRULE_RETURN
)) &&
4623 !(th
->th_flags
& TH_RST
)) {
4624 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
4633 h4
= mtod(m
, struct ip
*);
4634 len
= ntohs(h4
->ip_len
) - off
;
4638 h6
= mtod(m
, struct ip6_hdr
*);
4639 len
= ntohs(h6
->ip6_plen
) -
4640 (off
- sizeof (*h6
));
4645 if (pf_check_proto_cksum(m
, off
, len
, IPPROTO_TCP
, af
))
4646 REASON_SET(&reason
, PFRES_PROTCKSUM
);
4648 if (th
->th_flags
& TH_SYN
)
4650 if (th
->th_flags
& TH_FIN
)
4652 pf_send_tcp(r
, af
, pd
->dst
,
4653 pd
->src
, th
->th_dport
, th
->th_sport
,
4654 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
4655 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
4657 } else if (pd
->proto
!= IPPROTO_ICMP
&& af
== AF_INET
&&
4658 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4660 pf_send_icmp(m
, r
->return_icmp
>> 8,
4661 r
->return_icmp
& 255, af
, r
);
4662 else if (pd
->proto
!= IPPROTO_ICMPV6
&& af
== AF_INET6
&&
4663 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4665 pf_send_icmp(m
, r
->return_icmp6
>> 8,
4666 r
->return_icmp6
& 255, af
, r
);
4669 if (r
->action
== PF_DROP
)
4672 /* prepare state key, for flowhash and/or the state (if created) */
4673 bzero(&psk
, sizeof (psk
));
4674 psk
.proto
= pd
->proto
;
4675 psk
.direction
= direction
;
4677 if (pd
->proto
== IPPROTO_UDP
) {
4678 if (ntohs(pd
->hdr
.udp
->uh_sport
) == PF_IKE_PORT
&&
4679 ntohs(pd
->hdr
.udp
->uh_dport
) == PF_IKE_PORT
) {
4680 psk
.proto_variant
= PF_EXTFILTER_APD
;
4682 psk
.proto_variant
= nr
? nr
->extfilter
: r
->extfilter
;
4683 if (psk
.proto_variant
< PF_EXTFILTER_APD
)
4684 psk
.proto_variant
= PF_EXTFILTER_APD
;
4686 } else if (pd
->proto
== IPPROTO_GRE
) {
4687 psk
.proto_variant
= pd
->proto_variant
;
4689 if (direction
== PF_OUT
) {
4690 PF_ACPY(&psk
.gwy
.addr
, saddr
, af
);
4691 PF_ACPY(&psk
.ext
.addr
, daddr
, af
);
4692 switch (pd
->proto
) {
4694 psk
.gwy
.xport
= sxport
;
4695 psk
.ext
.xport
= dxport
;
4698 psk
.gwy
.xport
.spi
= 0;
4699 psk
.ext
.xport
.spi
= pd
->hdr
.esp
->spi
;
4703 case IPPROTO_ICMPV6
:
4705 psk
.gwy
.xport
.port
= nxport
.port
;
4706 psk
.ext
.xport
.spi
= 0;
4709 psk
.gwy
.xport
= sxport
;
4710 psk
.ext
.xport
= dxport
;
4714 PF_ACPY(&psk
.lan
.addr
, &pd
->baddr
, af
);
4715 psk
.lan
.xport
= bxport
;
4717 PF_ACPY(&psk
.lan
.addr
, &psk
.gwy
.addr
, af
);
4718 psk
.lan
.xport
= psk
.gwy
.xport
;
4721 PF_ACPY(&psk
.lan
.addr
, daddr
, af
);
4722 PF_ACPY(&psk
.ext
.addr
, saddr
, af
);
4723 switch (pd
->proto
) {
4726 case IPPROTO_ICMPV6
:
4728 psk
.lan
.xport
= nxport
;
4729 psk
.ext
.xport
.spi
= 0;
4732 psk
.ext
.xport
.spi
= 0;
4733 psk
.lan
.xport
.spi
= pd
->hdr
.esp
->spi
;
4736 psk
.lan
.xport
= dxport
;
4737 psk
.ext
.xport
= sxport
;
4741 PF_ACPY(&psk
.gwy
.addr
, &pd
->baddr
, af
);
4742 psk
.gwy
.xport
= bxport
;
4744 PF_ACPY(&psk
.gwy
.addr
, &psk
.lan
.addr
, af
);
4745 psk
.gwy
.xport
= psk
.lan
.xport
;
4748 if (pd
->pktflags
& PKTF_FLOW_ID
) {
4749 /* flow hash was already computed outside of PF */
4750 psk
.flowsrc
= pd
->flowsrc
;
4751 psk
.flowhash
= pd
->flowhash
;
4753 /* compute flow hash and store it in state key */
4754 psk
.flowsrc
= FLOWSRC_PF
;
4755 psk
.flowhash
= pf_calc_state_key_flowhash(&psk
);
4756 pd
->flowsrc
= psk
.flowsrc
;
4757 pd
->flowhash
= psk
.flowhash
;
4758 pd
->pktflags
|= PKTF_FLOW_ID
;
4759 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
4762 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
4763 REASON_SET(&reason
, PFRES_MEMORY
);
4767 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
4768 (pd
->flags
& PFDESC_TCP_NORM
))) {
4769 /* create new state */
4770 struct pf_state
*s
= NULL
;
4771 struct pf_state_key
*sk
= NULL
;
4772 struct pf_src_node
*sn
= NULL
;
4773 struct pf_ike_hdr ike
;
4775 if (pd
->proto
== IPPROTO_UDP
) {
4776 struct udphdr
*uh
= pd
->hdr
.udp
;
4777 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
4779 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
4780 ntohs(uh
->uh_dport
) == PF_IKE_PORT
&&
4781 plen
>= PF_IKE_PACKET_MINSIZE
) {
4782 if (plen
> PF_IKE_PACKET_MINSIZE
)
4783 plen
= PF_IKE_PACKET_MINSIZE
;
4784 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
4788 if (nr
!= NULL
&& pd
->proto
== IPPROTO_ESP
&&
4789 direction
== PF_OUT
) {
4790 struct pf_state_key_cmp sk0
;
4791 struct pf_state
*s0
;
4795 * This squelches state creation if the external
4796 * address matches an existing incomplete state with a
4797 * different internal address. Only one 'blocking'
4798 * partial state is allowed for each external address.
4800 memset(&sk0
, 0, sizeof (sk0
));
4802 sk0
.proto
= IPPROTO_ESP
;
4803 PF_ACPY(&sk0
.gwy
.addr
, saddr
, sk0
.af
);
4804 PF_ACPY(&sk0
.ext
.addr
, daddr
, sk0
.af
);
4805 s0
= pf_find_state(kif
, &sk0
, PF_IN
);
4807 if (s0
&& PF_ANEQ(&s0
->state_key
->lan
.addr
,
4814 /* check maximums */
4815 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
4816 pf_status
.lcounters
[LCNT_STATES
]++;
4817 REASON_SET(&reason
, PFRES_MAXSTATES
);
4820 /* src node for filter rule */
4821 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
4822 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4823 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
4824 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4827 /* src node for translation rule */
4828 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4829 ((direction
== PF_OUT
&&
4830 nr
->action
!= PF_RDR
&&
4831 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
4832 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
4833 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4836 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
4838 REASON_SET(&reason
, PFRES_MEMORY
);
4840 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
4841 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
4842 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4843 pf_status
.src_nodes
--;
4844 pool_put(&pf_src_tree_pl
, sn
);
4846 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
4848 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
4849 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4850 pf_status
.src_nodes
--;
4851 pool_put(&pf_src_tree_pl
, nsn
);
4855 pool_put(&pf_app_state_pl
,
4857 pool_put(&pf_state_key_pl
, sk
);
4861 bzero(s
, sizeof (*s
));
4862 TAILQ_INIT(&s
->unlink_hooks
);
4864 s
->nat_rule
.ptr
= nr
;
4866 STATE_INC_COUNTERS(s
);
4867 s
->allow_opts
= r
->allow_opts
;
4868 s
->log
= r
->log
& PF_LOG_ALL
;
4870 s
->log
|= nr
->log
& PF_LOG_ALL
;
4871 switch (pd
->proto
) {
4873 s
->src
.seqlo
= ntohl(th
->th_seq
);
4874 s
->src
.seqhi
= s
->src
.seqlo
+ pd
->p_len
+ 1;
4875 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
4876 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
4877 /* Generate sequence number modulator */
4878 if ((s
->src
.seqdiff
= pf_tcp_iss(pd
) -
4881 pf_change_a(&th
->th_seq
, &th
->th_sum
,
4882 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
4883 rewrite
= off
+ sizeof (*th
);
4886 if (th
->th_flags
& TH_SYN
) {
4888 s
->src
.wscale
= pf_get_wscale(m
, off
,
4891 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
4892 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
4893 /* Remove scale factor from initial window */
4894 int win
= s
->src
.max_win
;
4895 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
4896 s
->src
.max_win
= (win
- 1) >>
4897 (s
->src
.wscale
& PF_WSCALE_MASK
);
4899 if (th
->th_flags
& TH_FIN
)
4903 s
->src
.state
= TCPS_SYN_SENT
;
4904 s
->dst
.state
= TCPS_CLOSED
;
4905 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
4908 s
->src
.state
= PFUDPS_SINGLE
;
4909 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
4910 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
4914 case IPPROTO_ICMPV6
:
4916 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
4919 s
->src
.state
= PFGRE1S_INITIATING
;
4920 s
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
4921 s
->timeout
= PFTM_GREv1_INITIATING
;
4924 s
->src
.state
= PFESPS_INITIATING
;
4925 s
->dst
.state
= PFESPS_NO_TRAFFIC
;
4926 s
->timeout
= PFTM_ESP_FIRST_PACKET
;
4929 s
->src
.state
= PFOTHERS_SINGLE
;
4930 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
4931 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
4934 s
->creation
= pf_time_second();
4935 s
->expire
= pf_time_second();
4939 s
->src_node
->states
++;
4940 VERIFY(s
->src_node
->states
!= 0);
4943 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
4944 s
->nat_src_node
= nsn
;
4945 s
->nat_src_node
->states
++;
4946 VERIFY(s
->nat_src_node
->states
!= 0);
4948 if (pd
->proto
== IPPROTO_TCP
) {
4949 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
4950 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
4952 REASON_SET(&reason
, PFRES_MEMORY
);
4953 pf_src_tree_remove_state(s
);
4954 STATE_DEC_COUNTERS(s
);
4955 pool_put(&pf_state_pl
, s
);
4958 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
4959 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
4960 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
4961 /* This really shouldn't happen!!! */
4962 DPFPRINTF(PF_DEBUG_URGENT
,
4963 ("pf_normalize_tcp_stateful failed on "
4965 pf_normalize_tcp_cleanup(s
);
4966 pf_src_tree_remove_state(s
);
4967 STATE_DEC_COUNTERS(s
);
4968 pool_put(&pf_state_pl
, s
);
4973 /* allocate state key and import values from psk */
4974 if ((sk
= pf_alloc_state_key(s
, &psk
)) == NULL
) {
4975 REASON_SET(&reason
, PFRES_MEMORY
);
4979 pf_set_rt_ifp(s
, saddr
); /* needs s->state_key set */
4983 if (sk
->app_state
== 0) {
4984 switch (pd
->proto
) {
4986 u_int16_t dport
= (direction
== PF_OUT
) ?
4987 sk
->ext
.xport
.port
: sk
->gwy
.xport
.port
;
4990 ntohs(dport
) == PF_PPTP_PORT
) {
4991 struct pf_app_state
*as
;
4993 as
= pool_get(&pf_app_state_pl
,
5001 bzero(as
, sizeof (*as
));
5002 as
->handler
= pf_pptp_handler
;
5003 as
->compare_lan_ext
= 0;
5004 as
->compare_ext_gwy
= 0;
5005 as
->u
.pptp
.grev1_state
= 0;
5007 (void) hook_establish(&s
->unlink_hooks
,
5008 0, (hook_fn_t
) pf_pptp_unlink
, s
);
5014 struct udphdr
*uh
= pd
->hdr
.udp
;
5017 ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
5018 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
5019 struct pf_app_state
*as
;
5021 as
= pool_get(&pf_app_state_pl
,
5029 bzero(as
, sizeof (*as
));
5030 as
->compare_lan_ext
= pf_ike_compare
;
5031 as
->compare_ext_gwy
= pf_ike_compare
;
5032 as
->u
.ike
.cookie
= ike
.initiator_cookie
;
5043 if (pf_insert_state(BOUND_IFACE(r
, kif
), s
)) {
5044 if (pd
->proto
== IPPROTO_TCP
)
5045 pf_normalize_tcp_cleanup(s
);
5046 REASON_SET(&reason
, PFRES_STATEINS
);
5047 pf_src_tree_remove_state(s
);
5048 STATE_DEC_COUNTERS(s
);
5049 pool_put(&pf_state_pl
, s
);
5057 if (pd
->proto
== IPPROTO_TCP
&&
5058 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
5059 r
->keep_state
== PF_STATE_SYNPROXY
) {
5060 s
->src
.state
= PF_TCPS_PROXY_SRC
;
5062 if (direction
== PF_OUT
) {
5063 pf_change_ap(direction
, pd
->mp
, saddr
,
5064 &th
->th_sport
, pd
->ip_sum
,
5065 &th
->th_sum
, &pd
->baddr
,
5066 bxport
.port
, 0, af
);
5067 sxport
.port
= th
->th_sport
;
5069 pf_change_ap(direction
, pd
->mp
, daddr
,
5070 &th
->th_dport
, pd
->ip_sum
,
5071 &th
->th_sum
, &pd
->baddr
,
5072 bxport
.port
, 0, af
);
5073 sxport
.port
= th
->th_dport
;
5076 s
->src
.seqhi
= htonl(random());
5077 /* Find mss option */
5078 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
5079 mss
= pf_calc_mss(saddr
, af
, mss
);
5080 mss
= pf_calc_mss(daddr
, af
, mss
);
5082 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
5083 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5084 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
5085 REASON_SET(&reason
, PFRES_SYNPROXY
);
5086 return (PF_SYNPROXY_DROP
);
5089 if (sk
->app_state
&& sk
->app_state
->handler
) {
5092 switch (pd
->proto
) {
5094 offx
+= th
->th_off
<< 2;
5097 offx
+= pd
->hdr
.udp
->uh_ulen
<< 2;
5100 /* ALG handlers only apply to TCP and UDP rules */
5105 sk
->app_state
->handler(s
, direction
, offx
,
5108 REASON_SET(&reason
, PFRES_MEMORY
);
5116 /* copy back packet headers if we performed NAT operations */
5118 if (rewrite
< off
+ hdrlen
)
5119 rewrite
= off
+ hdrlen
;
5121 m
= pf_lazy_makewritable(pd
, pd
->mp
, rewrite
);
5123 REASON_SET(&reason
, PFRES_MEMORY
);
5127 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5135 * When pf_test_dummynet() returns PF_PASS, the rule matching parameter "rm"
5136 * remains unchanged, meaning the packet did not match a dummynet rule.
5137 * when the packet does match a dummynet rule, pf_test_dummynet() returns
5138 * PF_PASS and zero out the mbuf rule as the packet is effectively siphoned
5142 pf_test_dummynet(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5143 struct mbuf
**m0
, struct pf_pdesc
*pd
, struct ip_fw_args
*fwa
)
5145 struct mbuf
*m
= *m0
;
5146 struct pf_rule
*am
= NULL
;
5147 struct pf_ruleset
*rsm
= NULL
;
5148 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
5149 sa_family_t af
= pd
->af
;
5150 struct pf_rule
*r
, *a
= NULL
;
5151 struct pf_ruleset
*ruleset
= NULL
;
5152 struct tcphdr
*th
= pd
->hdr
.tcp
;
5156 unsigned int rtableid
= IFSCOPE_NONE
;
5159 u_int8_t icmptype
= 0, icmpcode
= 0;
5160 struct ip_fw_args dnflow
;
5161 struct pf_rule
*prev_matching_rule
= fwa
? fwa
->fwa_pf_rule
: NULL
;
5162 int found_prev_rule
= (prev_matching_rule
) ? 0 : 1;
5164 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
5166 if (!DUMMYNET_LOADED
)
5169 if (TAILQ_EMPTY(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
))
5172 bzero(&dnflow
, sizeof(dnflow
));
5176 /* Fragments don't gave protocol headers */
5177 if (!(pd
->flags
& PFDESC_IP_FRAG
))
5178 switch (pd
->proto
) {
5180 dnflow
.fwa_id
.flags
= pd
->hdr
.tcp
->th_flags
;
5181 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.tcp
->th_dport
);
5182 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.tcp
->th_sport
);
5183 hdrlen
= sizeof (*th
);
5186 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.udp
->uh_dport
);
5187 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.udp
->uh_sport
);
5188 hdrlen
= sizeof (*pd
->hdr
.udp
);
5194 hdrlen
= ICMP_MINLEN
;
5195 icmptype
= pd
->hdr
.icmp
->icmp_type
;
5196 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
5200 case IPPROTO_ICMPV6
:
5203 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
5204 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
5205 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
5209 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
5210 hdrlen
= sizeof (*pd
->hdr
.grev1
);
5213 hdrlen
= sizeof (*pd
->hdr
.esp
);
5217 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
);
5221 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5222 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5223 else if (r
->direction
&& r
->direction
!= direction
)
5224 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5225 else if (r
->af
&& r
->af
!= af
)
5226 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5227 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5228 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5229 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
5231 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5232 /* tcp/udp only. port_op always 0 in other cases */
5233 else if (r
->proto
== pd
->proto
&&
5234 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5235 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5236 ((r
->src
.xport
.range
.op
&&
5237 !pf_match_port(r
->src
.xport
.range
.op
,
5238 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
5240 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
5241 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
5243 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5244 /* tcp/udp only. port_op always 0 in other cases */
5245 else if (r
->proto
== pd
->proto
&&
5246 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5247 r
->dst
.xport
.range
.op
&&
5248 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5249 !pf_match_port(r
->dst
.xport
.range
.op
,
5250 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
5252 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
5253 /* icmp only. type always 0 in other cases */
5255 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5256 r
->type
!= icmptype
+ 1))
5257 r
= TAILQ_NEXT(r
, entries
);
5258 /* icmp only. type always 0 in other cases */
5260 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5261 r
->code
!= icmpcode
+ 1))
5262 r
= TAILQ_NEXT(r
, entries
);
5263 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5264 r
= TAILQ_NEXT(r
, entries
);
5265 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
5266 r
= TAILQ_NEXT(r
, entries
);
5267 else if (pd
->proto
== IPPROTO_TCP
&&
5268 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5269 (r
->flagset
& th
->th_flags
) != r
->flags
))
5270 r
= TAILQ_NEXT(r
, entries
);
5271 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
5272 r
= TAILQ_NEXT(r
, entries
);
5273 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5274 r
= TAILQ_NEXT(r
, entries
);
5277 * Need to go past the previous dummynet matching rule
5279 if (r
->anchor
== NULL
) {
5280 if (found_prev_rule
) {
5283 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
5284 rtableid
= r
->rtableid
;
5291 } else if (r
== prev_matching_rule
) {
5292 found_prev_rule
= 1;
5294 r
= TAILQ_NEXT(r
, entries
);
5296 pf_step_into_anchor(&asd
, &ruleset
,
5297 PF_RULESET_DUMMYNET
, &r
, &a
, &match
);
5300 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5301 PF_RULESET_DUMMYNET
, &r
, &a
, &match
))
5311 REASON_SET(&reason
, PFRES_DUMMYNET
);
5314 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
,
5318 if (r
->action
== PF_NODUMMYNET
) {
5319 int dirndx
= (direction
== PF_OUT
);
5321 r
->packets
[dirndx
]++;
5322 r
->bytes
[dirndx
] += pd
->tot_len
;
5326 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5327 REASON_SET(&reason
, PFRES_MEMORY
);
5332 if (r
->dnpipe
&& ip_dn_io_ptr
!= NULL
) {
5333 int dirndx
= (direction
== PF_OUT
);
5335 r
->packets
[dirndx
]++;
5336 r
->bytes
[dirndx
] += pd
->tot_len
;
5338 dnflow
.fwa_cookie
= r
->dnpipe
;
5339 dnflow
.fwa_pf_rule
= r
;
5340 dnflow
.fwa_id
.proto
= pd
->proto
;
5341 dnflow
.fwa_flags
= r
->dntype
;
5344 dnflow
.fwa_id
.addr_type
= 4;
5345 dnflow
.fwa_id
.src_ip
= ntohl(saddr
->v4
.s_addr
);
5346 dnflow
.fwa_id
.dst_ip
= ntohl(daddr
->v4
.s_addr
);
5349 dnflow
.fwa_id
.addr_type
= 6;
5350 dnflow
.fwa_id
.src_ip6
= saddr
->v6
;
5351 dnflow
.fwa_id
.dst_ip6
= saddr
->v6
;
5356 dnflow
.fwa_oif
= fwa
->fwa_oif
;
5357 dnflow
.fwa_oflags
= fwa
->fwa_oflags
;
5359 * Note that fwa_ro, fwa_dst and fwa_ipoa are
5360 * actually in a union so the following does work
5361 * for both IPv4 and IPv6
5363 dnflow
.fwa_ro
= fwa
->fwa_ro
;
5364 dnflow
.fwa_dst
= fwa
->fwa_dst
;
5365 dnflow
.fwa_ipoa
= fwa
->fwa_ipoa
;
5366 dnflow
.fwa_ro6_pmtu
= fwa
->fwa_ro6_pmtu
;
5367 dnflow
.fwa_origifp
= fwa
->fwa_origifp
;
5368 dnflow
.fwa_mtu
= fwa
->fwa_mtu
;
5369 dnflow
.fwa_alwaysfrag
= fwa
->fwa_alwaysfrag
;
5370 dnflow
.fwa_unfragpartlen
= fwa
->fwa_unfragpartlen
;
5371 dnflow
.fwa_exthdrs
= fwa
->fwa_exthdrs
;
5374 if (af
== AF_INET
) {
5375 struct ip
*iphdr
= mtod(m
, struct ip
*);
5376 NTOHS(iphdr
->ip_len
);
5377 NTOHS(iphdr
->ip_off
);
5380 * Don't need to unlock pf_lock as NET_THREAD_HELD_PF
5381 * allows for recursive behavior
5386 direction
== PF_IN
? DN_TO_IP_IN
: DN_TO_IP_OUT
:
5387 direction
== PF_IN
? DN_TO_IP6_IN
: DN_TO_IP6_OUT
,
5388 &dnflow
, DN_CLIENT_PF
);
5391 * The packet is siphoned out by dummynet so return a NULL
5392 * mbuf so the caller can still return success.
5401 #endif /* DUMMYNET */
5404 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5405 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
5406 struct pf_ruleset
**rsm
)
5409 struct pf_rule
*r
, *a
= NULL
;
5410 struct pf_ruleset
*ruleset
= NULL
;
5411 sa_family_t af
= pd
->af
;
5417 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
5420 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5421 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5422 else if (r
->direction
&& r
->direction
!= direction
)
5423 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5424 else if (r
->af
&& r
->af
!= af
)
5425 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5426 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5427 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5428 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
5430 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5431 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
5433 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5434 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
5435 !(r
->tos
& pd
->tos
))
5436 r
= TAILQ_NEXT(r
, entries
);
5437 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
5438 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
5439 r
= TAILQ_NEXT(r
, entries
);
5440 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
5441 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
5442 r
= TAILQ_NEXT(r
, entries
);
5443 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
5444 r
= TAILQ_NEXT(r
, entries
);
5445 else if (pd
->proto
== IPPROTO_UDP
&&
5446 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
5447 r
= TAILQ_NEXT(r
, entries
);
5448 else if (pd
->proto
== IPPROTO_TCP
&&
5449 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
5451 r
= TAILQ_NEXT(r
, entries
);
5452 else if ((pd
->proto
== IPPROTO_ICMP
||
5453 pd
->proto
== IPPROTO_ICMPV6
) &&
5454 (r
->type
|| r
->code
))
5455 r
= TAILQ_NEXT(r
, entries
);
5456 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
5457 r
= TAILQ_NEXT(r
, entries
);
5458 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5459 r
= TAILQ_NEXT(r
, entries
);
5461 if (r
->anchor
== NULL
) {
5468 r
= TAILQ_NEXT(r
, entries
);
5470 pf_step_into_anchor(&asd
, &ruleset
,
5471 PF_RULESET_FILTER
, &r
, &a
, &match
);
5473 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5474 PF_RULESET_FILTER
, &r
, &a
, &match
))
5481 REASON_SET(&reason
, PFRES_MATCH
);
5484 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
5487 if (r
->action
!= PF_PASS
)
5490 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1, NULL
)) {
5491 REASON_SET(&reason
, PFRES_MEMORY
);
5499 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
5500 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
5502 #pragma unused(direction)
5504 struct pf_pptp_state
*pptps
;
5505 struct pf_pptp_ctrl_msg cm
;
5507 struct pf_state
*gs
;
5509 u_int16_t
*pac_call_id
;
5510 u_int16_t
*pns_call_id
;
5511 u_int16_t
*spoof_call_id
;
5512 u_int8_t
*pac_state
;
5513 u_int8_t
*pns_state
;
5514 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
5516 struct pf_state_key
*sk
;
5517 struct pf_state_key
*gsk
;
5518 struct pf_app_state
*gas
;
5521 pptps
= &sk
->app_state
->u
.pptp
;
5522 gs
= pptps
->grev1_state
;
5525 gs
->expire
= pf_time_second();
5528 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
5529 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
5532 m_copydata(m
, off
, plen
, &cm
);
5534 if (ntohl(cm
.hdr
.magic
) != PF_PPTP_MAGIC_NUMBER
)
5536 if (ntohs(cm
.hdr
.type
) != 1)
5540 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
5544 memcpy(gs
, s
, sizeof (*gs
));
5546 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
5547 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
5549 TAILQ_INIT(&gs
->unlink_hooks
);
5552 gs
->pfsync_time
= 0;
5553 gs
->packets
[0] = gs
->packets
[1] = 0;
5554 gs
->bytes
[0] = gs
->bytes
[1] = 0;
5555 gs
->timeout
= PFTM_UNLINKED
;
5556 gs
->id
= gs
->creatorid
= 0;
5557 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5558 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
5560 gas
= pool_get(&pf_app_state_pl
, PR_NOWAIT
);
5562 pool_put(&pf_state_pl
, gs
);
5566 gsk
= pf_alloc_state_key(gs
, NULL
);
5568 pool_put(&pf_app_state_pl
, gas
);
5569 pool_put(&pf_state_pl
, gs
);
5573 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
5574 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
5575 memcpy(&gsk
->ext
, &sk
->ext
, sizeof (gsk
->ext
));
5577 gsk
->proto
= IPPROTO_GRE
;
5578 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
5579 gsk
->app_state
= gas
;
5580 gsk
->lan
.xport
.call_id
= 0;
5581 gsk
->gwy
.xport
.call_id
= 0;
5582 gsk
->ext
.xport
.call_id
= 0;
5583 gsk
->flowsrc
= FLOWSRC_PF
;
5584 gsk
->flowhash
= pf_calc_state_key_flowhash(gsk
);
5585 memset(gas
, 0, sizeof (*gas
));
5586 gas
->u
.grev1
.pptp_state
= s
;
5587 STATE_INC_COUNTERS(gs
);
5588 pptps
->grev1_state
= gs
;
5589 (void) hook_establish(&gs
->unlink_hooks
, 0,
5590 (hook_fn_t
) pf_grev1_unlink
, gs
);
5592 gsk
= gs
->state_key
;
5595 switch (sk
->direction
) {
5597 pns_call_id
= &gsk
->ext
.xport
.call_id
;
5598 pns_state
= &gs
->dst
.state
;
5599 pac_call_id
= &gsk
->lan
.xport
.call_id
;
5600 pac_state
= &gs
->src
.state
;
5604 pns_call_id
= &gsk
->lan
.xport
.call_id
;
5605 pns_state
= &gs
->src
.state
;
5606 pac_call_id
= &gsk
->ext
.xport
.call_id
;
5607 pac_state
= &gs
->dst
.state
;
5611 DPFPRINTF(PF_DEBUG_URGENT
,
5612 ("pf_pptp_handler: bad directional!\n"));
5619 ct
= ntohs(cm
.ctrl
.type
);
5622 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
5623 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
5624 *pns_state
= PFGRE1S_INITIATING
;
5625 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5626 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
5629 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
5630 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
5631 if (s
->nat_rule
.ptr
)
5633 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5634 &cm
.msg
.call_out_rpy
.call_id
:
5635 &cm
.msg
.call_out_rpy
.peer_call_id
;
5636 if (gs
->timeout
== PFTM_UNLINKED
) {
5637 *pac_state
= PFGRE1S_INITIATING
;
5638 op
= PF_PPTP_INSERT_GRE
;
5642 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
5643 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
5644 *pns_state
= PFGRE1S_INITIATING
;
5645 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5646 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
5649 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
5650 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
5651 *pac_state
= PFGRE1S_INITIATING
;
5652 if (s
->nat_rule
.ptr
)
5654 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5655 &cm
.msg
.call_in_2nd
.call_id
:
5656 &cm
.msg
.call_in_2nd
.peer_call_id
;
5659 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
5660 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5661 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
5662 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
5665 if (gs
->timeout
== PFTM_UNLINKED
)
5666 op
= PF_PPTP_INSERT_GRE
;
5669 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
5670 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
5671 op
= PF_PPTP_REMOVE_GRE
;
5674 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
5675 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
5676 op
= PF_PPTP_REMOVE_GRE
;
5679 case PF_PPTP_CTRL_TYPE_ERROR
:
5680 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5681 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
5684 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
5685 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
5686 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
5694 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
5695 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
5696 if (spoof_call_id
) {
5697 u_int16_t call_id
= 0;
5699 struct pf_state_key_cmp key
;
5702 key
.proto
= IPPROTO_GRE
;
5703 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
5704 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af
);
5705 PF_ACPY(&key
.ext
.addr
, &gsk
->ext
.addr
, key
.af
);
5706 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
5707 key
.ext
.xport
.call_id
= gsk
->ext
.xport
.call_id
;
5709 call_id
= htonl(random());
5712 while (pf_find_state_all(&key
, PF_IN
, 0)) {
5713 call_id
= ntohs(call_id
);
5715 if (--call_id
== 0) call_id
= 0xffff;
5716 call_id
= htons(call_id
);
5718 key
.gwy
.xport
.call_id
= call_id
;
5721 DPFPRINTF(PF_DEBUG_URGENT
,
5722 ("pf_pptp_handler: failed to spoof "
5724 key
.gwy
.xport
.call_id
= 0;
5729 gsk
->gwy
.xport
.call_id
= call_id
;
5735 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
5736 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
5737 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
5738 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5739 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
5741 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
5742 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5743 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
5746 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
5748 pptps
->grev1_state
= NULL
;
5749 STATE_DEC_COUNTERS(gs
);
5750 pool_put(&pf_state_pl
, gs
);
5753 m_copyback(m
, off
, plen
, &cm
);
5757 case PF_PPTP_REMOVE_GRE
:
5758 gs
->timeout
= PFTM_PURGE
;
5759 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5760 gsk
->lan
.xport
.call_id
= 0;
5761 gsk
->gwy
.xport
.call_id
= 0;
5762 gsk
->ext
.xport
.call_id
= 0;
5763 gs
->id
= gs
->creatorid
= 0;
5766 case PF_PPTP_INSERT_GRE
:
5767 gs
->creation
= pf_time_second();
5768 gs
->expire
= pf_time_second();
5769 gs
->timeout
= PFTM_TCP_ESTABLISHED
;
5770 if (gs
->src_node
!= NULL
) {
5771 ++gs
->src_node
->states
;
5772 VERIFY(gs
->src_node
->states
!= 0);
5774 if (gs
->nat_src_node
!= NULL
) {
5775 ++gs
->nat_src_node
->states
;
5776 VERIFY(gs
->nat_src_node
->states
!= 0);
5778 pf_set_rt_ifp(gs
, &sk
->lan
.addr
);
5779 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
5783 * FIX ME: insertion can fail when multiple PNS
5784 * behind the same NAT open calls to the same PAC
5785 * simultaneously because spoofed call ID numbers
5786 * are chosen before states are inserted. This is
5787 * hard to fix and happens infrequently enough that
5788 * users will normally try again and this ALG will
5789 * succeed. Failures are expected to be rare enough
5790 * that fixing this is a low priority.
5792 pptps
->grev1_state
= NULL
;
5793 pd
->lmw
= -1; /* Force PF_DROP on PFRES_MEMORY */
5794 pf_src_tree_remove_state(gs
);
5795 STATE_DEC_COUNTERS(gs
);
5796 pool_put(&pf_state_pl
, gs
);
5797 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
5798 "inserting GREv1 state.\n"));
5808 pf_pptp_unlink(struct pf_state
*s
)
5810 struct pf_app_state
*as
= s
->state_key
->app_state
;
5811 struct pf_state
*grev1s
= as
->u
.pptp
.grev1_state
;
5814 struct pf_app_state
*gas
= grev1s
->state_key
->app_state
;
5816 if (grev1s
->timeout
< PFTM_MAX
)
5817 grev1s
->timeout
= PFTM_PURGE
;
5818 gas
->u
.grev1
.pptp_state
= NULL
;
5819 as
->u
.pptp
.grev1_state
= NULL
;
5824 pf_grev1_unlink(struct pf_state
*s
)
5826 struct pf_app_state
*as
= s
->state_key
->app_state
;
5827 struct pf_state
*pptps
= as
->u
.grev1
.pptp_state
;
5830 struct pf_app_state
*pas
= pptps
->state_key
->app_state
;
5832 pas
->u
.pptp
.grev1_state
= NULL
;
5833 as
->u
.grev1
.pptp_state
= NULL
;
5838 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
5840 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
5841 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
5845 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
5846 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
5850 struct pf_state_key_cmp key
;
5851 struct tcphdr
*th
= pd
->hdr
.tcp
;
5852 u_int16_t win
= ntohs(th
->th_win
);
5853 u_int32_t ack
, end
, seq
, orig_seq
;
5857 struct pf_state_peer
*src
, *dst
;
5861 key
.proto
= IPPROTO_TCP
;
5862 if (direction
== PF_IN
) {
5863 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
5864 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
5865 key
.ext
.xport
.port
= th
->th_sport
;
5866 key
.gwy
.xport
.port
= th
->th_dport
;
5868 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
5869 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
5870 key
.lan
.xport
.port
= th
->th_sport
;
5871 key
.ext
.xport
.port
= th
->th_dport
;
5876 if (direction
== (*state
)->state_key
->direction
) {
5877 src
= &(*state
)->src
;
5878 dst
= &(*state
)->dst
;
5880 src
= &(*state
)->dst
;
5881 dst
= &(*state
)->src
;
5884 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
5885 if (direction
!= (*state
)->state_key
->direction
) {
5886 REASON_SET(reason
, PFRES_SYNPROXY
);
5887 return (PF_SYNPROXY_DROP
);
5889 if (th
->th_flags
& TH_SYN
) {
5890 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
5891 REASON_SET(reason
, PFRES_SYNPROXY
);
5894 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5895 pd
->src
, th
->th_dport
, th
->th_sport
,
5896 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5897 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
5899 REASON_SET(reason
, PFRES_SYNPROXY
);
5900 return (PF_SYNPROXY_DROP
);
5901 } else if (!(th
->th_flags
& TH_ACK
) ||
5902 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5903 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5904 REASON_SET(reason
, PFRES_SYNPROXY
);
5906 } else if ((*state
)->src_node
!= NULL
&&
5907 pf_src_connlimit(state
)) {
5908 REASON_SET(reason
, PFRES_SRCLIMIT
);
5911 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
5913 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
5914 struct pf_state_host
*psrc
, *pdst
;
5916 if (direction
== PF_OUT
) {
5917 psrc
= &(*state
)->state_key
->gwy
;
5918 pdst
= &(*state
)->state_key
->ext
;
5920 psrc
= &(*state
)->state_key
->ext
;
5921 pdst
= &(*state
)->state_key
->lan
;
5923 if (direction
== (*state
)->state_key
->direction
) {
5924 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
5925 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5926 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5927 REASON_SET(reason
, PFRES_SYNPROXY
);
5930 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5931 if ((*state
)->dst
.seqhi
== 1)
5932 (*state
)->dst
.seqhi
= htonl(random());
5933 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5934 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5935 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
5936 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
5937 REASON_SET(reason
, PFRES_SYNPROXY
);
5938 return (PF_SYNPROXY_DROP
);
5939 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
5941 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
5942 REASON_SET(reason
, PFRES_SYNPROXY
);
5945 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
5946 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
5947 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5948 pd
->src
, th
->th_dport
, th
->th_sport
,
5949 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
5950 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
5951 (*state
)->tag
, NULL
, NULL
);
5952 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5953 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5954 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
5955 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
5957 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
5958 (*state
)->src
.seqlo
;
5959 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
5960 (*state
)->dst
.seqlo
;
5961 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
5962 (*state
)->dst
.max_win
;
5963 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
5964 (*state
)->src
.max_win
;
5965 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
5966 (*state
)->src
.state
= (*state
)->dst
.state
=
5968 REASON_SET(reason
, PFRES_SYNPROXY
);
5969 return (PF_SYNPROXY_DROP
);
5973 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
5974 dst
->state
>= TCPS_FIN_WAIT_2
&&
5975 src
->state
>= TCPS_FIN_WAIT_2
) {
5976 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
5977 printf("pf: state reuse ");
5978 pf_print_state(*state
);
5979 pf_print_flags(th
->th_flags
);
5982 /* XXX make sure it's the same direction ?? */
5983 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
5984 pf_unlink_state(*state
);
5989 if ((th
->th_flags
& TH_SYN
) == 0) {
5990 sws
= (src
->wscale
& PF_WSCALE_FLAG
) ?
5991 (src
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
5992 dws
= (dst
->wscale
& PF_WSCALE_FLAG
) ?
5993 (dst
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
5999 * Sequence tracking algorithm from Guido van Rooij's paper:
6000 * http://www.madison-gurkha.com/publications/tcp_filtering/
6004 orig_seq
= seq
= ntohl(th
->th_seq
);
6005 if (src
->seqlo
== 0) {
6006 /* First packet from this end. Set its state */
6008 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
6009 src
->scrub
== NULL
) {
6010 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
6011 REASON_SET(reason
, PFRES_MEMORY
);
6016 /* Deferred generation of sequence number modulator */
6017 if (dst
->seqdiff
&& !src
->seqdiff
) {
6018 /* use random iss for the TCP server */
6019 while ((src
->seqdiff
= random() - seq
) == 0)
6021 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6022 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6024 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6025 copyback
= off
+ sizeof (*th
);
6027 ack
= ntohl(th
->th_ack
);
6030 end
= seq
+ pd
->p_len
;
6031 if (th
->th_flags
& TH_SYN
) {
6033 if (dst
->wscale
& PF_WSCALE_FLAG
) {
6034 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
6036 if (src
->wscale
& PF_WSCALE_FLAG
) {
6038 * Remove scale factor from initial
6041 sws
= src
->wscale
& PF_WSCALE_MASK
;
6042 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
6044 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6047 * Window scale negotiation has failed,
6048 * therefore we must restore the window
6049 * scale in the state record that we
6050 * optimistically removed in
6051 * pf_test_rule(). Care is required to
6052 * prevent arithmetic overflow from
6053 * zeroing the window when it's
6054 * truncated down to 16-bits.
6056 u_int32_t max_win
= dst
->max_win
;
6058 dst
->wscale
& PF_WSCALE_MASK
;
6059 dst
->max_win
= MIN(0xffff, max_win
);
6060 /* in case of a retrans SYN|ACK */
6065 if (th
->th_flags
& TH_FIN
)
6069 if (src
->state
< TCPS_SYN_SENT
)
6070 src
->state
= TCPS_SYN_SENT
;
6073 * May need to slide the window (seqhi may have been set by
6074 * the crappy stack check or if we picked up the connection
6075 * after establishment)
6077 if (src
->seqhi
== 1 ||
6078 SEQ_GEQ(end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
),
6080 src
->seqhi
= end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
);
6081 if (win
> src
->max_win
)
6085 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6087 /* Modulate sequence numbers */
6088 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6090 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6091 copyback
= off
+ sizeof (*th
);
6093 end
= seq
+ pd
->p_len
;
6094 if (th
->th_flags
& TH_SYN
)
6096 if (th
->th_flags
& TH_FIN
)
6100 if ((th
->th_flags
& TH_ACK
) == 0) {
6101 /* Let it pass through the ack skew check */
6103 } else if ((ack
== 0 &&
6104 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6105 /* broken tcp stacks do not set ack */
6106 (dst
->state
< TCPS_SYN_SENT
)) {
6108 * Many stacks (ours included) will set the ACK number in an
6109 * FIN|ACK if the SYN times out -- no sequence to ACK.
6115 /* Ease sequencing restrictions on no data packets */
6120 ackskew
= dst
->seqlo
- ack
;
6124 * Need to demodulate the sequence numbers in any TCP SACK options
6125 * (Selective ACK). We could optionally validate the SACK values
6126 * against the current ACK window, either forwards or backwards, but
6127 * I'm not confident that SACK has been implemented properly
6128 * everywhere. It wouldn't surprise me if several stacks accidently
6129 * SACK too far backwards of previously ACKed data. There really aren't
6130 * any security implications of bad SACKing unless the target stack
6131 * doesn't validate the option length correctly. Someone trying to
6132 * spoof into a TCP connection won't bother blindly sending SACK
6135 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6136 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6137 if (copyback
== -1) {
6138 REASON_SET(reason
, PFRES_MEMORY
);
6146 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6147 if (SEQ_GEQ(src
->seqhi
, end
) &&
6148 /* Last octet inside other's window space */
6149 SEQ_GEQ(seq
, src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) &&
6150 /* Retrans: not more than one window back */
6151 (ackskew
>= -MAXACKWINDOW
) &&
6152 /* Acking not more than one reassembled fragment backwards */
6153 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6154 /* Acking not more than one window forward */
6155 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6156 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6157 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6158 /* Require an exact/+1 sequence match on resets when possible */
6160 if (dst
->scrub
|| src
->scrub
) {
6161 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6162 *state
, src
, dst
, ©back
))
6168 /* update max window */
6169 if (src
->max_win
< win
)
6171 /* synchronize sequencing */
6172 if (SEQ_GT(end
, src
->seqlo
))
6174 /* slide the window of what the other end can send */
6175 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6176 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6179 if (th
->th_flags
& TH_SYN
)
6180 if (src
->state
< TCPS_SYN_SENT
)
6181 src
->state
= TCPS_SYN_SENT
;
6182 if (th
->th_flags
& TH_FIN
)
6183 if (src
->state
< TCPS_CLOSING
)
6184 src
->state
= TCPS_CLOSING
;
6185 if (th
->th_flags
& TH_ACK
) {
6186 if (dst
->state
== TCPS_SYN_SENT
) {
6187 dst
->state
= TCPS_ESTABLISHED
;
6188 if (src
->state
== TCPS_ESTABLISHED
&&
6189 (*state
)->src_node
!= NULL
&&
6190 pf_src_connlimit(state
)) {
6191 REASON_SET(reason
, PFRES_SRCLIMIT
);
6194 } else if (dst
->state
== TCPS_CLOSING
)
6195 dst
->state
= TCPS_FIN_WAIT_2
;
6197 if (th
->th_flags
& TH_RST
)
6198 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6200 /* update expire time */
6201 (*state
)->expire
= pf_time_second();
6202 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6203 dst
->state
>= TCPS_FIN_WAIT_2
)
6204 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6205 else if (src
->state
>= TCPS_CLOSING
&&
6206 dst
->state
>= TCPS_CLOSING
)
6207 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6208 else if (src
->state
< TCPS_ESTABLISHED
||
6209 dst
->state
< TCPS_ESTABLISHED
)
6210 (*state
)->timeout
= PFTM_TCP_OPENING
;
6211 else if (src
->state
>= TCPS_CLOSING
||
6212 dst
->state
>= TCPS_CLOSING
)
6213 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6215 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6217 /* Fall through to PASS packet */
6219 } else if ((dst
->state
< TCPS_SYN_SENT
||
6220 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6221 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6222 /* Within a window forward of the originating packet */
6223 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6224 /* Within a window backward of the originating packet */
6227 * This currently handles three situations:
6228 * 1) Stupid stacks will shotgun SYNs before their peer
6230 * 2) When PF catches an already established stream (the
6231 * firewall rebooted, the state table was flushed, routes
6233 * 3) Packets get funky immediately after the connection
6234 * closes (this should catch Solaris spurious ACK|FINs
6235 * that web servers like to spew after a close)
6237 * This must be a little more careful than the above code
6238 * since packet floods will also be caught here. We don't
6239 * update the TTL here to mitigate the damage of a packet
6240 * flood and so the same code can handle awkward establishment
6241 * and a loosened connection close.
6242 * In the establishment case, a correct peer response will
6243 * validate the connection, go through the normal state code
6244 * and keep updating the state TTL.
6247 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6248 printf("pf: loose state match: ");
6249 pf_print_state(*state
);
6250 pf_print_flags(th
->th_flags
);
6251 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6252 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6253 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6254 (*state
)->packets
[1],
6255 direction
== PF_IN
? "in" : "out",
6256 direction
== (*state
)->state_key
->direction
?
6260 if (dst
->scrub
|| src
->scrub
) {
6261 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6262 *state
, src
, dst
, ©back
))
6267 /* update max window */
6268 if (src
->max_win
< win
)
6270 /* synchronize sequencing */
6271 if (SEQ_GT(end
, src
->seqlo
))
6273 /* slide the window of what the other end can send */
6274 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6275 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6278 * Cannot set dst->seqhi here since this could be a shotgunned
6279 * SYN and not an already established connection.
6282 if (th
->th_flags
& TH_FIN
)
6283 if (src
->state
< TCPS_CLOSING
)
6284 src
->state
= TCPS_CLOSING
;
6285 if (th
->th_flags
& TH_RST
)
6286 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6288 /* Fall through to PASS packet */
6291 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
6292 (*state
)->src
.state
== TCPS_SYN_SENT
) {
6293 /* Send RST for state mismatches during handshake */
6294 if (!(th
->th_flags
& TH_RST
))
6295 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6296 pd
->dst
, pd
->src
, th
->th_dport
,
6297 th
->th_sport
, ntohl(th
->th_ack
), 0,
6299 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
6300 pd
->eh
, kif
->pfik_ifp
);
6304 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6305 printf("pf: BAD state: ");
6306 pf_print_state(*state
);
6307 pf_print_flags(th
->th_flags
);
6308 printf("\n seq=%u (%u) ack=%u len=%u ackskew=%d "
6309 "sws=%u dws=%u pkts=%llu:%llu dir=%s,%s\n",
6310 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
6311 (unsigned int)sws
, (unsigned int)dws
,
6312 (*state
)->packets
[0], (*state
)->packets
[1],
6313 direction
== PF_IN
? "in" : "out",
6314 direction
== (*state
)->state_key
->direction
?
6316 printf("pf: State failure on: %c %c %c %c | %c %c\n",
6317 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
6319 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) ?
6321 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
6322 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
6323 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
6324 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
6326 REASON_SET(reason
, PFRES_BADSTATE
);
6330 /* Any packets which have gotten here are to be passed */
6332 if ((*state
)->state_key
->app_state
&&
6333 (*state
)->state_key
->app_state
->handler
) {
6334 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6335 off
+ (th
->th_off
<< 2), pd
, kif
);
6337 REASON_SET(reason
, PFRES_MEMORY
);
6343 /* translate source/destination address, if necessary */
6344 if (STATE_TRANSLATE((*state
)->state_key
)) {
6345 if (direction
== PF_OUT
)
6346 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
6347 pd
->ip_sum
, &th
->th_sum
,
6348 &(*state
)->state_key
->gwy
.addr
,
6349 (*state
)->state_key
->gwy
.xport
.port
, 0, pd
->af
);
6351 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &th
->th_dport
,
6352 pd
->ip_sum
, &th
->th_sum
,
6353 &(*state
)->state_key
->lan
.addr
,
6354 (*state
)->state_key
->lan
.xport
.port
, 0, pd
->af
);
6355 copyback
= off
+ sizeof (*th
);
6359 m
= pf_lazy_makewritable(pd
, m
, copyback
);
6361 REASON_SET(reason
, PFRES_MEMORY
);
6365 /* Copyback sequence modulation or stateful scrub changes */
6366 m_copyback(m
, off
, sizeof (*th
), th
);
6373 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6374 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6377 struct pf_state_peer
*src
, *dst
;
6378 struct pf_state_key_cmp key
;
6379 struct udphdr
*uh
= pd
->hdr
.udp
;
6380 struct pf_app_state as
;
6381 int dx
, action
, extfilter
;
6383 key
.proto_variant
= PF_EXTFILTER_APD
;
6386 key
.proto
= IPPROTO_UDP
;
6387 if (direction
== PF_IN
) {
6388 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6389 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6390 key
.ext
.xport
.port
= uh
->uh_sport
;
6391 key
.gwy
.xport
.port
= uh
->uh_dport
;
6394 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6395 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6396 key
.lan
.xport
.port
= uh
->uh_sport
;
6397 key
.ext
.xport
.port
= uh
->uh_dport
;
6401 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
6402 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
6403 struct pf_ike_hdr ike
;
6404 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
6405 if (plen
< PF_IKE_PACKET_MINSIZE
) {
6406 DPFPRINTF(PF_DEBUG_MISC
,
6407 ("pf: IKE message too small.\n"));
6411 if (plen
> sizeof (ike
))
6412 plen
= sizeof (ike
);
6413 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
6415 if (ike
.initiator_cookie
) {
6416 key
.app_state
= &as
;
6417 as
.compare_lan_ext
= pf_ike_compare
;
6418 as
.compare_ext_gwy
= pf_ike_compare
;
6419 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6422 * <http://tools.ietf.org/html/\
6423 * draft-ietf-ipsec-nat-t-ike-01>
6424 * Support non-standard NAT-T implementations that
6425 * push the ESP packet over the top of the IKE packet.
6426 * Do not drop packet.
6428 DPFPRINTF(PF_DEBUG_MISC
,
6429 ("pf: IKE initiator cookie = 0.\n"));
6433 *state
= pf_find_state(kif
, &key
, dx
);
6435 if (!key
.app_state
&& *state
== 0) {
6436 key
.proto_variant
= PF_EXTFILTER_AD
;
6437 *state
= pf_find_state(kif
, &key
, dx
);
6440 if (!key
.app_state
&& *state
== 0) {
6441 key
.proto_variant
= PF_EXTFILTER_EI
;
6442 *state
= pf_find_state(kif
, &key
, dx
);
6445 /* similar to STATE_LOOKUP() */
6446 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
6447 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
6448 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6449 if (pd
->flowhash
!= 0) {
6450 pd
->pktflags
|= PKTF_FLOW_ID
;
6451 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
6455 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6458 if (direction
== (*state
)->state_key
->direction
) {
6459 src
= &(*state
)->src
;
6460 dst
= &(*state
)->dst
;
6462 src
= &(*state
)->dst
;
6463 dst
= &(*state
)->src
;
6467 if (src
->state
< PFUDPS_SINGLE
)
6468 src
->state
= PFUDPS_SINGLE
;
6469 if (dst
->state
== PFUDPS_SINGLE
)
6470 dst
->state
= PFUDPS_MULTIPLE
;
6472 /* update expire time */
6473 (*state
)->expire
= pf_time_second();
6474 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
6475 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
6477 (*state
)->timeout
= PFTM_UDP_SINGLE
;
6479 extfilter
= (*state
)->state_key
->proto_variant
;
6480 if (extfilter
> PF_EXTFILTER_APD
) {
6481 (*state
)->state_key
->ext
.xport
.port
= key
.ext
.xport
.port
;
6482 if (extfilter
> PF_EXTFILTER_AD
)
6483 PF_ACPY(&(*state
)->state_key
->ext
.addr
,
6484 &key
.ext
.addr
, key
.af
);
6487 if ((*state
)->state_key
->app_state
&&
6488 (*state
)->state_key
->app_state
->handler
) {
6489 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6490 off
+ uh
->uh_ulen
, pd
, kif
);
6492 REASON_SET(reason
, PFRES_MEMORY
);
6498 /* translate source/destination address, if necessary */
6499 if (STATE_TRANSLATE((*state
)->state_key
)) {
6500 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
6502 REASON_SET(reason
, PFRES_MEMORY
);
6506 if (direction
== PF_OUT
)
6507 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
6508 pd
->ip_sum
, &uh
->uh_sum
,
6509 &(*state
)->state_key
->gwy
.addr
,
6510 (*state
)->state_key
->gwy
.xport
.port
, 1, pd
->af
);
6512 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &uh
->uh_dport
,
6513 pd
->ip_sum
, &uh
->uh_sum
,
6514 &(*state
)->state_key
->lan
.addr
,
6515 (*state
)->state_key
->lan
.xport
.port
, 1, pd
->af
);
6516 m_copyback(m
, off
, sizeof (*uh
), uh
);
6523 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6524 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6527 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
6528 u_int16_t icmpid
= 0, *icmpsum
;
6531 struct pf_state_key_cmp key
;
6533 struct pf_app_state as
;
6536 switch (pd
->proto
) {
6539 icmptype
= pd
->hdr
.icmp
->icmp_type
;
6540 icmpid
= pd
->hdr
.icmp
->icmp_id
;
6541 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
6543 if (icmptype
== ICMP_UNREACH
||
6544 icmptype
== ICMP_SOURCEQUENCH
||
6545 icmptype
== ICMP_REDIRECT
||
6546 icmptype
== ICMP_TIMXCEED
||
6547 icmptype
== ICMP_PARAMPROB
)
6552 case IPPROTO_ICMPV6
:
6553 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
6554 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
6555 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
6557 if (icmptype
== ICMP6_DST_UNREACH
||
6558 icmptype
== ICMP6_PACKET_TOO_BIG
||
6559 icmptype
== ICMP6_TIME_EXCEEDED
||
6560 icmptype
== ICMP6_PARAM_PROB
)
6569 * ICMP query/reply message not related to a TCP/UDP packet.
6570 * Search for an ICMP state.
6573 key
.proto
= pd
->proto
;
6574 if (direction
== PF_IN
) {
6575 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6576 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6577 key
.ext
.xport
.port
= 0;
6578 key
.gwy
.xport
.port
= icmpid
;
6580 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6581 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6582 key
.lan
.xport
.port
= icmpid
;
6583 key
.ext
.xport
.port
= 0;
6588 (*state
)->expire
= pf_time_second();
6589 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
6591 /* translate source/destination address, if necessary */
6592 if (STATE_TRANSLATE((*state
)->state_key
)) {
6593 if (direction
== PF_OUT
) {
6597 pf_change_a(&saddr
->v4
.s_addr
,
6599 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
6600 pd
->hdr
.icmp
->icmp_cksum
=
6602 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6603 (*state
)->state_key
->gwy
.xport
.port
, 0);
6604 pd
->hdr
.icmp
->icmp_id
=
6605 (*state
)->state_key
->gwy
.xport
.port
;
6606 m
= pf_lazy_makewritable(pd
, m
,
6610 m_copyback(m
, off
, ICMP_MINLEN
,
6617 &pd
->hdr
.icmp6
->icmp6_cksum
,
6618 &(*state
)->state_key
->gwy
.addr
, 0);
6619 m
= pf_lazy_makewritable(pd
, m
,
6620 off
+ sizeof (struct icmp6_hdr
));
6624 sizeof (struct icmp6_hdr
),
6633 pf_change_a(&daddr
->v4
.s_addr
,
6635 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
6636 pd
->hdr
.icmp
->icmp_cksum
=
6638 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6639 (*state
)->state_key
->lan
.xport
.port
, 0);
6640 pd
->hdr
.icmp
->icmp_id
=
6641 (*state
)->state_key
->lan
.xport
.port
;
6642 m
= pf_lazy_makewritable(pd
, m
,
6646 m_copyback(m
, off
, ICMP_MINLEN
,
6653 &pd
->hdr
.icmp6
->icmp6_cksum
,
6654 &(*state
)->state_key
->lan
.addr
, 0);
6655 m
= pf_lazy_makewritable(pd
, m
,
6656 off
+ sizeof (struct icmp6_hdr
));
6660 sizeof (struct icmp6_hdr
),
6672 * ICMP error message in response to a TCP/UDP packet.
6673 * Extract the inner TCP/UDP header and search for that state.
6676 struct pf_pdesc pd2
;
6681 struct ip6_hdr h2_6
;
6687 memset(&pd2
, 0, sizeof (pd2
));
6693 /* offset of h2 in mbuf chain */
6694 ipoff2
= off
+ ICMP_MINLEN
;
6696 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
6697 NULL
, reason
, pd2
.af
)) {
6698 DPFPRINTF(PF_DEBUG_MISC
,
6699 ("pf: ICMP error message too short "
6704 * ICMP error messages don't refer to non-first
6707 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
6708 REASON_SET(reason
, PFRES_FRAG
);
6712 /* offset of protocol header that follows h2 */
6713 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
6715 pd2
.proto
= h2
.ip_p
;
6716 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
6717 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
6718 pd2
.ip_sum
= &h2
.ip_sum
;
6723 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
6725 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
6726 NULL
, reason
, pd2
.af
)) {
6727 DPFPRINTF(PF_DEBUG_MISC
,
6728 ("pf: ICMP error message too short "
6732 pd2
.proto
= h2_6
.ip6_nxt
;
6733 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
6734 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
6736 off2
= ipoff2
+ sizeof (h2_6
);
6738 switch (pd2
.proto
) {
6739 case IPPROTO_FRAGMENT
:
6741 * ICMPv6 error messages for
6742 * non-first fragments
6744 REASON_SET(reason
, PFRES_FRAG
);
6747 case IPPROTO_HOPOPTS
:
6748 case IPPROTO_ROUTING
:
6749 case IPPROTO_DSTOPTS
: {
6750 /* get next header and header length */
6751 struct ip6_ext opt6
;
6753 if (!pf_pull_hdr(m
, off2
, &opt6
,
6754 sizeof (opt6
), NULL
, reason
,
6756 DPFPRINTF(PF_DEBUG_MISC
,
6757 ("pf: ICMPv6 short opt\n"));
6760 if (pd2
.proto
== IPPROTO_AH
)
6761 off2
+= (opt6
.ip6e_len
+ 2) * 4;
6763 off2
+= (opt6
.ip6e_len
+ 1) * 8;
6764 pd2
.proto
= opt6
.ip6e_nxt
;
6765 /* goto the next header */
6772 } while (!terminal
);
6777 switch (pd2
.proto
) {
6781 struct pf_state_peer
*src
, *dst
;
6786 * Only the first 8 bytes of the TCP header can be
6787 * expected. Don't access any TCP header fields after
6788 * th_seq, an ackskew test is not possible.
6790 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
6792 DPFPRINTF(PF_DEBUG_MISC
,
6793 ("pf: ICMP error message too short "
6799 key
.proto
= IPPROTO_TCP
;
6800 if (direction
== PF_IN
) {
6801 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6802 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6803 key
.ext
.xport
.port
= th
.th_dport
;
6804 key
.gwy
.xport
.port
= th
.th_sport
;
6806 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6807 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6808 key
.lan
.xport
.port
= th
.th_dport
;
6809 key
.ext
.xport
.port
= th
.th_sport
;
6814 if (direction
== (*state
)->state_key
->direction
) {
6815 src
= &(*state
)->dst
;
6816 dst
= &(*state
)->src
;
6818 src
= &(*state
)->src
;
6819 dst
= &(*state
)->dst
;
6822 if (src
->wscale
&& (dst
->wscale
& PF_WSCALE_FLAG
))
6823 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6825 dws
= TCP_MAX_WINSHIFT
;
6827 /* Demodulate sequence number */
6828 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
6830 pf_change_a(&th
.th_seq
, icmpsum
,
6835 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
6837 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
))) {
6838 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6839 printf("pf: BAD ICMP %d:%d ",
6840 icmptype
, pd
->hdr
.icmp
->icmp_code
);
6841 pf_print_host(pd
->src
, 0, pd
->af
);
6843 pf_print_host(pd
->dst
, 0, pd
->af
);
6845 pf_print_state(*state
);
6846 printf(" seq=%u\n", seq
);
6848 REASON_SET(reason
, PFRES_BADSTATE
);
6852 if (STATE_TRANSLATE((*state
)->state_key
)) {
6853 if (direction
== PF_IN
) {
6854 pf_change_icmp(pd2
.src
, &th
.th_sport
,
6855 daddr
, &(*state
)->state_key
->lan
.addr
,
6856 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6857 pd2
.ip_sum
, icmpsum
,
6858 pd
->ip_sum
, 0, pd2
.af
);
6860 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
6861 saddr
, &(*state
)->state_key
->gwy
.addr
,
6862 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6863 pd2
.ip_sum
, icmpsum
,
6864 pd
->ip_sum
, 0, pd2
.af
);
6870 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
6876 m_copyback(m
, off
, ICMP_MINLEN
,
6878 m_copyback(m
, ipoff2
, sizeof (h2
),
6885 sizeof (struct icmp6_hdr
),
6887 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6892 m_copyback(m
, off2
, 8, &th
);
6901 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
6902 NULL
, reason
, pd2
.af
)) {
6903 DPFPRINTF(PF_DEBUG_MISC
,
6904 ("pf: ICMP error message too short "
6910 key
.proto
= IPPROTO_UDP
;
6911 if (direction
== PF_IN
) {
6912 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6913 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6914 key
.ext
.xport
.port
= uh
.uh_dport
;
6915 key
.gwy
.xport
.port
= uh
.uh_sport
;
6918 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6919 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6920 key
.lan
.xport
.port
= uh
.uh_dport
;
6921 key
.ext
.xport
.port
= uh
.uh_sport
;
6925 key
.proto_variant
= PF_EXTFILTER_APD
;
6927 if (ntohs(uh
.uh_sport
) == PF_IKE_PORT
&&
6928 ntohs(uh
.uh_dport
) == PF_IKE_PORT
) {
6929 struct pf_ike_hdr ike
;
6931 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
6932 if (direction
== PF_IN
&&
6933 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
6934 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
6935 "ICMP error, embedded IKE message "
6940 if (plen
> sizeof (ike
))
6941 plen
= sizeof (ike
);
6942 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
6944 key
.app_state
= &as
;
6945 as
.compare_lan_ext
= pf_ike_compare
;
6946 as
.compare_ext_gwy
= pf_ike_compare
;
6947 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6950 *state
= pf_find_state(kif
, &key
, dx
);
6952 if (key
.app_state
&& *state
== 0) {
6954 *state
= pf_find_state(kif
, &key
, dx
);
6958 key
.proto_variant
= PF_EXTFILTER_AD
;
6959 *state
= pf_find_state(kif
, &key
, dx
);
6963 key
.proto_variant
= PF_EXTFILTER_EI
;
6964 *state
= pf_find_state(kif
, &key
, dx
);
6967 /* similar to STATE_LOOKUP() */
6968 if (*state
!= NULL
&& pd
!= NULL
&&
6969 !(pd
->pktflags
& PKTF_FLOW_ID
)) {
6970 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
6971 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6972 if (pd
->flowhash
!= 0) {
6973 pd
->pktflags
|= PKTF_FLOW_ID
;
6974 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
6978 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6981 if (STATE_TRANSLATE((*state
)->state_key
)) {
6982 if (direction
== PF_IN
) {
6983 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
6984 daddr
, &(*state
)->state_key
->lan
.addr
,
6985 (*state
)->state_key
->lan
.xport
.port
, &uh
.uh_sum
,
6986 pd2
.ip_sum
, icmpsum
,
6987 pd
->ip_sum
, 1, pd2
.af
);
6989 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
6990 saddr
, &(*state
)->state_key
->gwy
.addr
,
6991 (*state
)->state_key
->gwy
.xport
.port
, &uh
.uh_sum
,
6992 pd2
.ip_sum
, icmpsum
,
6993 pd
->ip_sum
, 1, pd2
.af
);
6995 m
= pf_lazy_makewritable(pd
, m
,
6996 off2
+ sizeof (uh
));
7002 m_copyback(m
, off
, ICMP_MINLEN
,
7004 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7010 sizeof (struct icmp6_hdr
),
7012 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7017 m_copyback(m
, off2
, sizeof (uh
), &uh
);
7024 case IPPROTO_ICMP
: {
7027 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
7028 NULL
, reason
, pd2
.af
)) {
7029 DPFPRINTF(PF_DEBUG_MISC
,
7030 ("pf: ICMP error message too short i"
7036 key
.proto
= IPPROTO_ICMP
;
7037 if (direction
== PF_IN
) {
7038 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7039 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7040 key
.ext
.xport
.port
= 0;
7041 key
.gwy
.xport
.port
= iih
.icmp_id
;
7043 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7044 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7045 key
.lan
.xport
.port
= iih
.icmp_id
;
7046 key
.ext
.xport
.port
= 0;
7051 if (STATE_TRANSLATE((*state
)->state_key
)) {
7052 if (direction
== PF_IN
) {
7053 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
7054 daddr
, &(*state
)->state_key
->lan
.addr
,
7055 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7056 pd2
.ip_sum
, icmpsum
,
7057 pd
->ip_sum
, 0, AF_INET
);
7059 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
7060 saddr
, &(*state
)->state_key
->gwy
.addr
,
7061 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7062 pd2
.ip_sum
, icmpsum
,
7063 pd
->ip_sum
, 0, AF_INET
);
7065 m
= pf_lazy_makewritable(pd
, m
, off2
+ ICMP_MINLEN
);
7068 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
7069 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7070 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
7078 case IPPROTO_ICMPV6
: {
7079 struct icmp6_hdr iih
;
7081 if (!pf_pull_hdr(m
, off2
, &iih
,
7082 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
7083 DPFPRINTF(PF_DEBUG_MISC
,
7084 ("pf: ICMP error message too short "
7090 key
.proto
= IPPROTO_ICMPV6
;
7091 if (direction
== PF_IN
) {
7092 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7093 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7094 key
.ext
.xport
.port
= 0;
7095 key
.gwy
.xport
.port
= iih
.icmp6_id
;
7097 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7098 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7099 key
.lan
.xport
.port
= iih
.icmp6_id
;
7100 key
.ext
.xport
.port
= 0;
7105 if (STATE_TRANSLATE((*state
)->state_key
)) {
7106 if (direction
== PF_IN
) {
7107 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
7108 daddr
, &(*state
)->state_key
->lan
.addr
,
7109 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7110 pd2
.ip_sum
, icmpsum
,
7111 pd
->ip_sum
, 0, AF_INET6
);
7113 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
7114 saddr
, &(*state
)->state_key
->gwy
.addr
,
7115 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7116 pd2
.ip_sum
, icmpsum
,
7117 pd
->ip_sum
, 0, AF_INET6
);
7119 m
= pf_lazy_makewritable(pd
, m
, off2
+
7120 sizeof (struct icmp6_hdr
));
7123 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
7125 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
7126 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
7136 key
.proto
= pd2
.proto
;
7137 if (direction
== PF_IN
) {
7138 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7139 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7140 key
.ext
.xport
.port
= 0;
7141 key
.gwy
.xport
.port
= 0;
7143 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7144 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7145 key
.lan
.xport
.port
= 0;
7146 key
.ext
.xport
.port
= 0;
7151 if (STATE_TRANSLATE((*state
)->state_key
)) {
7152 if (direction
== PF_IN
) {
7153 pf_change_icmp(pd2
.src
, NULL
,
7154 daddr
, &(*state
)->state_key
->lan
.addr
,
7156 pd2
.ip_sum
, icmpsum
,
7157 pd
->ip_sum
, 0, pd2
.af
);
7159 pf_change_icmp(pd2
.dst
, NULL
,
7160 saddr
, &(*state
)->state_key
->gwy
.addr
,
7162 pd2
.ip_sum
, icmpsum
,
7163 pd
->ip_sum
, 0, pd2
.af
);
7168 m
= pf_lazy_makewritable(pd
, m
,
7169 ipoff2
+ sizeof (h2
));
7175 m
= pf_lazy_makewritable(pd
, m
,
7176 ipoff2
+ sizeof (h2_6
));
7180 sizeof (struct icmp6_hdr
),
7182 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7197 pf_test_state_grev1(struct pf_state
**state
, int direction
,
7198 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
7200 struct pf_state_peer
*src
;
7201 struct pf_state_peer
*dst
;
7202 struct pf_state_key_cmp key
;
7203 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
7208 key
.proto
= IPPROTO_GRE
;
7209 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
7210 if (direction
== PF_IN
) {
7211 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7212 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7213 key
.gwy
.xport
.call_id
= grev1
->call_id
;
7215 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7216 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7217 key
.ext
.xport
.call_id
= grev1
->call_id
;
7222 if (direction
== (*state
)->state_key
->direction
) {
7223 src
= &(*state
)->src
;
7224 dst
= &(*state
)->dst
;
7226 src
= &(*state
)->dst
;
7227 dst
= &(*state
)->src
;
7231 if (src
->state
< PFGRE1S_INITIATING
)
7232 src
->state
= PFGRE1S_INITIATING
;
7234 /* update expire time */
7235 (*state
)->expire
= pf_time_second();
7236 if (src
->state
>= PFGRE1S_INITIATING
&&
7237 dst
->state
>= PFGRE1S_INITIATING
) {
7238 if ((*state
)->timeout
!= PFTM_TCP_ESTABLISHED
)
7239 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
7240 src
->state
= PFGRE1S_ESTABLISHED
;
7241 dst
->state
= PFGRE1S_ESTABLISHED
;
7243 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
7246 if ((*state
)->state_key
->app_state
)
7247 (*state
)->state_key
->app_state
->u
.grev1
.pptp_state
->expire
=
7250 /* translate source/destination address, if necessary */
7251 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
7252 if (direction
== PF_OUT
) {
7256 pf_change_a(&pd
->src
->v4
.s_addr
,
7258 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7263 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7269 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
7274 pf_change_a(&pd
->dst
->v4
.s_addr
,
7276 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7281 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7288 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
7291 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
7298 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7299 int off
, struct pf_pdesc
*pd
)
7302 struct pf_state_peer
*src
;
7303 struct pf_state_peer
*dst
;
7304 struct pf_state_key_cmp key
;
7305 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
7308 memset(&key
, 0, sizeof (key
));
7310 key
.proto
= IPPROTO_ESP
;
7311 if (direction
== PF_IN
) {
7312 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7313 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7314 key
.gwy
.xport
.spi
= esp
->spi
;
7316 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7317 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7318 key
.ext
.xport
.spi
= esp
->spi
;
7321 *state
= pf_find_state(kif
, &key
, direction
);
7328 * No matching state. Look for a blocking state. If we find
7329 * one, then use that state and move it so that it's keyed to
7330 * the SPI in the current packet.
7332 if (direction
== PF_IN
) {
7333 key
.gwy
.xport
.spi
= 0;
7335 s
= pf_find_state(kif
, &key
, direction
);
7337 struct pf_state_key
*sk
= s
->state_key
;
7339 RB_REMOVE(pf_state_tree_ext_gwy
,
7340 &pf_statetbl_ext_gwy
, sk
);
7341 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
7344 if (RB_INSERT(pf_state_tree_ext_gwy
,
7345 &pf_statetbl_ext_gwy
, sk
))
7346 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
7351 key
.ext
.xport
.spi
= 0;
7353 s
= pf_find_state(kif
, &key
, direction
);
7355 struct pf_state_key
*sk
= s
->state_key
;
7357 RB_REMOVE(pf_state_tree_lan_ext
,
7358 &pf_statetbl_lan_ext
, sk
);
7359 sk
->ext
.xport
.spi
= esp
->spi
;
7361 if (RB_INSERT(pf_state_tree_lan_ext
,
7362 &pf_statetbl_lan_ext
, sk
))
7363 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
7372 if (s
->creatorid
== pf_status
.hostid
)
7373 pfsync_delete_state(s
);
7375 s
->timeout
= PFTM_UNLINKED
;
7376 hook_runloop(&s
->unlink_hooks
,
7377 HOOK_REMOVE
|HOOK_FREE
);
7378 pf_src_tree_remove_state(s
);
7385 /* similar to STATE_LOOKUP() */
7386 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
7387 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
7388 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7389 if (pd
->flowhash
!= 0) {
7390 pd
->pktflags
|= PKTF_FLOW_ID
;
7391 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
7395 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7398 if (direction
== (*state
)->state_key
->direction
) {
7399 src
= &(*state
)->src
;
7400 dst
= &(*state
)->dst
;
7402 src
= &(*state
)->dst
;
7403 dst
= &(*state
)->src
;
7407 if (src
->state
< PFESPS_INITIATING
)
7408 src
->state
= PFESPS_INITIATING
;
7410 /* update expire time */
7411 (*state
)->expire
= pf_time_second();
7412 if (src
->state
>= PFESPS_INITIATING
&&
7413 dst
->state
>= PFESPS_INITIATING
) {
7414 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
7415 src
->state
= PFESPS_ESTABLISHED
;
7416 dst
->state
= PFESPS_ESTABLISHED
;
7418 (*state
)->timeout
= PFTM_ESP_INITIATING
;
7420 /* translate source/destination address, if necessary */
7421 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7422 if (direction
== PF_OUT
) {
7426 pf_change_a(&pd
->src
->v4
.s_addr
,
7428 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7433 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7442 pf_change_a(&pd
->dst
->v4
.s_addr
,
7444 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7449 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7461 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7462 struct pf_pdesc
*pd
)
7464 struct pf_state_peer
*src
, *dst
;
7465 struct pf_state_key_cmp key
;
7469 key
.proto
= pd
->proto
;
7470 if (direction
== PF_IN
) {
7471 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7472 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7473 key
.ext
.xport
.port
= 0;
7474 key
.gwy
.xport
.port
= 0;
7476 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7477 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7478 key
.lan
.xport
.port
= 0;
7479 key
.ext
.xport
.port
= 0;
7484 if (direction
== (*state
)->state_key
->direction
) {
7485 src
= &(*state
)->src
;
7486 dst
= &(*state
)->dst
;
7488 src
= &(*state
)->dst
;
7489 dst
= &(*state
)->src
;
7493 if (src
->state
< PFOTHERS_SINGLE
)
7494 src
->state
= PFOTHERS_SINGLE
;
7495 if (dst
->state
== PFOTHERS_SINGLE
)
7496 dst
->state
= PFOTHERS_MULTIPLE
;
7498 /* update expire time */
7499 (*state
)->expire
= pf_time_second();
7500 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
7501 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
7503 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
7505 /* translate source/destination address, if necessary */
7506 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7507 if (direction
== PF_OUT
) {
7511 pf_change_a(&pd
->src
->v4
.s_addr
,
7513 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
7520 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
7528 pf_change_a(&pd
->dst
->v4
.s_addr
,
7530 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
7537 &(*state
)->state_key
->lan
.addr
, pd
->af
);
7548 * ipoff and off are measured from the start of the mbuf chain.
7549 * h must be at "ipoff" on the mbuf chain.
7552 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
7553 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
7558 struct ip
*h
= mtod(m
, struct ip
*);
7559 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
7562 if (fragoff
>= len
) {
7563 ACTION_SET(actionp
, PF_PASS
);
7565 ACTION_SET(actionp
, PF_DROP
);
7566 REASON_SET(reasonp
, PFRES_FRAG
);
7570 if (m
->m_pkthdr
.len
< off
+ len
||
7571 ntohs(h
->ip_len
) < off
+ len
) {
7572 ACTION_SET(actionp
, PF_DROP
);
7573 REASON_SET(reasonp
, PFRES_SHORT
);
7581 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
7583 if (m
->m_pkthdr
.len
< off
+ len
||
7584 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
7585 (unsigned)(off
+ len
)) {
7586 ACTION_SET(actionp
, PF_DROP
);
7587 REASON_SET(reasonp
, PFRES_SHORT
);
7594 m_copydata(m
, off
, len
, p
);
7599 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
7602 struct sockaddr_in
*dst
;
7605 struct sockaddr_in6
*dst6
;
7606 struct route_in6 ro
;
7611 bzero(&ro
, sizeof (ro
));
7614 dst
= satosin(&ro
.ro_dst
);
7615 dst
->sin_family
= AF_INET
;
7616 dst
->sin_len
= sizeof (*dst
);
7617 dst
->sin_addr
= addr
->v4
;
7621 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7622 dst6
->sin6_family
= AF_INET6
;
7623 dst6
->sin6_len
= sizeof (*dst6
);
7624 dst6
->sin6_addr
= addr
->v6
;
7631 /* XXX: IFT_ENC is not currently used by anything*/
7632 /* Skip checks for ipsec interfaces */
7633 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
7636 /* XXX: what is the point of this? */
7637 rtalloc((struct route
*)&ro
);
7645 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
7648 struct sockaddr_in
*dst
;
7650 struct sockaddr_in6
*dst6
;
7651 struct route_in6 ro
;
7657 bzero(&ro
, sizeof (ro
));
7660 dst
= satosin(&ro
.ro_dst
);
7661 dst
->sin_family
= AF_INET
;
7662 dst
->sin_len
= sizeof (*dst
);
7663 dst
->sin_addr
= addr
->v4
;
7667 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7668 dst6
->sin6_family
= AF_INET6
;
7669 dst6
->sin6_len
= sizeof (*dst6
);
7670 dst6
->sin6_addr
= addr
->v6
;
7677 /* XXX: what is the point of this? */
7678 rtalloc((struct route
*)&ro
);
7687 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7688 struct pf_state
*s
, struct pf_pdesc
*pd
)
7691 struct mbuf
*m0
, *m1
;
7692 struct route iproute
;
7693 struct route
*ro
= &iproute
;
7694 struct sockaddr_in
*dst
;
7696 struct ifnet
*ifp
= NULL
;
7697 struct pf_addr naddr
;
7698 struct pf_src_node
*sn
= NULL
;
7702 bzero(&iproute
, sizeof (iproute
));
7704 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7705 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7706 panic("pf_route: invalid parameters");
7708 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7714 if (r
->rt
== PF_DUPTO
) {
7715 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7718 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7723 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7724 DPFPRINTF(PF_DEBUG_URGENT
,
7725 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7729 ip
= mtod(m0
, struct ip
*);
7731 dst
= satosin((void *)&ro
->ro_dst
);
7732 dst
->sin_family
= AF_INET
;
7733 dst
->sin_len
= sizeof (*dst
);
7734 dst
->sin_addr
= ip
->ip_dst
;
7736 if (r
->rt
== PF_FASTROUTE
) {
7738 if (ro
->ro_rt
== NULL
) {
7739 ipstat
.ips_noroute
++;
7743 ifp
= ro
->ro_rt
->rt_ifp
;
7745 ro
->ro_rt
->rt_use
++;
7747 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
7748 dst
= satosin((void *)ro
->ro_rt
->rt_gateway
);
7749 RT_UNLOCK(ro
->ro_rt
);
7751 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7752 DPFPRINTF(PF_DEBUG_URGENT
,
7753 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
7757 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
7759 if (!PF_AZERO(&naddr
, AF_INET
))
7760 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
7761 ifp
= r
->rpool
.cur
->kif
?
7762 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7764 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
7765 dst
->sin_addr
.s_addr
=
7766 s
->rt_addr
.v4
.s_addr
;
7767 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7774 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7776 else if (m0
== NULL
)
7778 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7779 DPFPRINTF(PF_DEBUG_URGENT
,
7780 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7783 ip
= mtod(m0
, struct ip
*);
7786 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
7787 ip_output_checksum(ifp
, m0
, ((ip
->ip_hl
) << 2), ntohs(ip
->ip_len
),
7790 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
|| TSO_IPV4_OK(ifp
, m0
) ||
7791 (!(ip
->ip_off
& htons(IP_DF
)) &&
7792 (ifp
->if_hwassist
& CSUM_FRAGMENT
))) {
7794 if (sw_csum
& CSUM_DELAY_IP
) {
7795 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
7796 sw_csum
&= ~CSUM_DELAY_IP
;
7797 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IP
;
7799 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
, sintosa(dst
));
7804 * Too large for interface; fragment if possible.
7805 * Must be able to put at least 8 bytes per fragment.
7806 * Balk when DF bit is set or the interface didn't support TSO.
7808 if ((ip
->ip_off
& htons(IP_DF
)) ||
7809 (m0
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
)) {
7810 ipstat
.ips_cantfrag
++;
7811 if (r
->rt
!= PF_DUPTO
) {
7812 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
7821 /* PR-8933605: send ip_len,ip_off to ip_fragment in host byte order */
7822 #if BYTE_ORDER != BIG_ENDIAN
7826 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
7833 for (m0
= m1
; m0
; m0
= m1
) {
7837 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
,
7844 ipstat
.ips_fragmented
++;
7847 if (r
->rt
!= PF_DUPTO
)
7850 ROUTE_RELEASE(&iproute
);
7861 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7862 struct pf_state
*s
, struct pf_pdesc
*pd
)
7866 struct route_in6 ip6route
;
7867 struct route_in6
*ro
;
7868 struct sockaddr_in6
*dst
;
7869 struct ip6_hdr
*ip6
;
7870 struct ifnet
*ifp
= NULL
;
7871 struct pf_addr naddr
;
7872 struct pf_src_node
*sn
= NULL
;
7875 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7876 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7877 panic("pf_route6: invalid parameters");
7879 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7885 if (r
->rt
== PF_DUPTO
) {
7886 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7889 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7894 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7895 DPFPRINTF(PF_DEBUG_URGENT
,
7896 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
7899 ip6
= mtod(m0
, struct ip6_hdr
*);
7902 bzero((caddr_t
)ro
, sizeof (*ro
));
7903 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
7904 dst
->sin6_family
= AF_INET6
;
7905 dst
->sin6_len
= sizeof (*dst
);
7906 dst
->sin6_addr
= ip6
->ip6_dst
;
7908 /* Cheat. XXX why only in the v6 case??? */
7909 if (r
->rt
== PF_FASTROUTE
) {
7910 struct pf_mtag
*pf_mtag
;
7912 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
7914 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
7915 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
7919 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7920 DPFPRINTF(PF_DEBUG_URGENT
,
7921 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
7925 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
7927 if (!PF_AZERO(&naddr
, AF_INET6
))
7928 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7930 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7932 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
7933 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7934 &s
->rt_addr
, AF_INET6
);
7935 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7941 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7943 else if (m0
== NULL
)
7945 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7946 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
7947 "< sizeof (struct ip6_hdr)\n"));
7950 ip6
= mtod(m0
, struct ip6_hdr
*);
7954 * If the packet is too large for the outgoing interface,
7955 * send back an icmp6 error.
7957 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
7958 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
7959 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
7960 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, NULL
);
7962 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
7963 if (r
->rt
!= PF_DUPTO
)
7964 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
7970 if (r
->rt
!= PF_DUPTO
)
7982 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
7983 * off is the offset where the protocol header starts
7984 * len is the total length of protocol header plus payload
7985 * returns 0 when the checksum is valid, otherwise returns 1.
7988 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
7997 * Optimize for the common case; if the hardware calculated
7998 * value doesn't include pseudo-header checksum, or if it
7999 * is partially-computed (only 16-bit summation), do it in
8002 if ((m
->m_pkthdr
.csum_flags
&
8003 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) ==
8004 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
) &&
8005 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
8011 case IPPROTO_ICMPV6
:
8017 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
8019 if (m
->m_pkthdr
.len
< off
+ len
)
8024 if (p
== IPPROTO_ICMP
) {
8029 sum
= in_cksum(m
, len
);
8033 if (m
->m_len
< (int)sizeof (struct ip
))
8035 sum
= inet_cksum(m
, p
, off
, len
);
8041 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
8043 sum
= inet6_cksum(m
, p
, off
, len
);
8052 tcpstat
.tcps_rcvbadsum
++;
8055 udpstat
.udps_badsum
++;
8058 icmpstat
.icps_checksum
++;
8061 case IPPROTO_ICMPV6
:
8062 icmp6stat
.icp6s_checksum
++;
8072 #define PF_APPLE_UPDATE_PDESC_IPv4() \
8074 if (m && pd.mp && m != pd.mp) { \
8076 h = mtod(m, struct ip *); \
8077 pd.pf_mtag = pf_get_mtag(m); \
8082 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8083 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8088 struct pfi_kif
*kif
;
8089 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8090 struct mbuf
*m
= *m0
;
8092 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8093 struct pf_state
*s
= NULL
;
8094 struct pf_state_key
*sk
= NULL
;
8095 struct pf_ruleset
*ruleset
= NULL
;
8097 int off
, dirndx
, pqid
= 0;
8099 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8101 if (!pf_status
.running
)
8104 memset(&pd
, 0, sizeof (pd
));
8106 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8107 DPFPRINTF(PF_DEBUG_URGENT
,
8108 ("pf_test: pf_get_mtag returned NULL\n"));
8112 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8115 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8118 DPFPRINTF(PF_DEBUG_URGENT
,
8119 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
8122 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8125 VERIFY(m
->m_flags
& M_PKTHDR
);
8127 /* initialize enough of pd for the done label */
8128 h
= mtod(m
, struct ip
*);
8131 pd
.pf_mtag
= pf_get_mtag(m
);
8132 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8133 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8134 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8135 pd
.ip_sum
= &h
->ip_sum
;
8137 pd
.proto_variant
= 0;
8140 pd
.tot_len
= ntohs(h
->ip_len
);
8143 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8145 REASON_SET(&reason
, PFRES_SHORT
);
8151 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8153 #endif /* DUMMYNET */
8155 /* We do IP header normalization and packet reassembly here */
8156 action
= pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
);
8158 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8165 #endif /* DUMMYNET */
8166 m
= *m0
; /* pf_normalize messes with m0 */
8167 h
= mtod(m
, struct ip
*);
8169 off
= h
->ip_hl
<< 2;
8170 if (off
< (int)sizeof (*h
)) {
8172 REASON_SET(&reason
, PFRES_SHORT
);
8177 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8178 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8179 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8180 pd
.ip_sum
= &h
->ip_sum
;
8182 pd
.proto_variant
= 0;
8185 pd
.pf_mtag
= pf_get_mtag(m
);
8188 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8189 pd
.tot_len
= ntohs(h
->ip_len
);
8192 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
8193 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
8194 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
8195 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
8198 /* handle fragments that didn't get reassembled by normalization */
8199 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
8200 pd
.flags
|= PFDESC_IP_FRAG
;
8202 /* Traffic goes through dummynet first */
8203 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8204 if (action
== PF_DROP
|| m
== NULL
) {
8208 #endif /* DUMMYNET */
8209 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8219 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8220 &action
, &reason
, AF_INET
)) {
8221 log
= action
!= PF_PASS
;
8224 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8225 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
8228 /* Traffic goes through dummynet first */
8229 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8230 if (action
== PF_DROP
|| m
== NULL
) {
8234 #endif /* DUMMYNET */
8235 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8238 PF_APPLE_UPDATE_PDESC_IPv4();
8239 if (action
== PF_DROP
)
8241 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8245 PF_APPLE_UPDATE_PDESC_IPv4();
8246 if (action
== PF_PASS
) {
8248 pfsync_update_state(s
);
8249 #endif /* NPFSYNC */
8253 } else if (s
== NULL
)
8254 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8255 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8263 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8264 &action
, &reason
, AF_INET
)) {
8265 log
= action
!= PF_PASS
;
8268 if (uh
.uh_dport
== 0 ||
8269 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8270 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8272 REASON_SET(&reason
, PFRES_SHORT
);
8276 /* Traffic goes through dummynet first */
8277 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8278 if (action
== PF_DROP
|| m
== NULL
) {
8282 #endif /* DUMMYNET */
8283 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8287 PF_APPLE_UPDATE_PDESC_IPv4();
8288 if (action
== PF_PASS
) {
8290 pfsync_update_state(s
);
8291 #endif /* NPFSYNC */
8295 } else if (s
== NULL
)
8296 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8297 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8301 case IPPROTO_ICMP
: {
8305 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
8306 &action
, &reason
, AF_INET
)) {
8307 log
= action
!= PF_PASS
;
8311 /* Traffic goes through dummynet first */
8312 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8313 if (action
== PF_DROP
|| m
== NULL
) {
8317 #endif /* DUMMYNET */
8318 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8322 PF_APPLE_UPDATE_PDESC_IPv4();
8323 if (action
== PF_PASS
) {
8325 pfsync_update_state(s
);
8326 #endif /* NPFSYNC */
8330 } else if (s
== NULL
)
8331 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8332 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8337 struct pf_esp_hdr esp
;
8340 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8342 log
= action
!= PF_PASS
;
8346 /* Traffic goes through dummynet first */
8347 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8348 if (action
== PF_DROP
|| m
== NULL
) {
8352 #endif /* DUMMYNET */
8353 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8356 PF_APPLE_UPDATE_PDESC_IPv4();
8357 if (action
== PF_PASS
) {
8359 pfsync_update_state(s
);
8360 #endif /* NPFSYNC */
8364 } else if (s
== NULL
)
8365 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8366 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8371 struct pf_grev1_hdr grev1
;
8372 pd
.hdr
.grev1
= &grev1
;
8373 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8374 &reason
, AF_INET
)) {
8375 log
= (action
!= PF_PASS
);
8379 /* Traffic goes through dummynet first */
8380 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8381 if (action
== PF_DROP
|| m
== NULL
) {
8385 #endif /* DUMMYNET */
8386 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8387 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8388 if (ntohs(grev1
.payload_length
) >
8389 m
->m_pkthdr
.len
- off
) {
8391 REASON_SET(&reason
, PFRES_SHORT
);
8394 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8395 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8396 if (pd
.lmw
< 0) goto done
;
8397 PF_APPLE_UPDATE_PDESC_IPv4();
8398 if (action
== PF_PASS
) {
8400 pfsync_update_state(s
);
8401 #endif /* NPFSYNC */
8406 } else if (s
== NULL
) {
8407 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8408 h
, &pd
, &a
, &ruleset
, NULL
);
8409 if (action
== PF_PASS
)
8414 /* not GREv1/PPTP, so treat as ordinary GRE... */
8419 /* Traffic goes through dummynet first */
8420 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8421 if (action
== PF_DROP
|| m
== NULL
) {
8425 #endif /* DUMMYNET */
8426 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8429 PF_APPLE_UPDATE_PDESC_IPv4();
8430 if (action
== PF_PASS
) {
8432 pfsync_update_state(s
);
8433 #endif /* NPFSYNC */
8437 } else if (s
== NULL
)
8438 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
8439 &pd
, &a
, &ruleset
, NULL
);
8445 PF_APPLE_UPDATE_PDESC_IPv4();
8447 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
8448 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
8450 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8452 DPFPRINTF(PF_DEBUG_MISC
,
8453 ("pf: dropping packet with ip options [hlen=%u]\n",
8454 (unsigned int) h
->ip_hl
));
8457 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
8458 (pd
.pktflags
& PKTF_FLOW_ID
))
8459 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
8462 if (action
== PF_PASS
) {
8464 if (altq_allowed
&& r
->qid
) {
8465 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
8466 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
8468 pd
.pf_mtag
->pftag_qid
= r
->qid
;
8470 #endif /* PF_ALTQ */
8472 /* add hints for ecn */
8473 pd
.pf_mtag
->pftag_hdr
= h
;
8474 /* record address family */
8475 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET6
;
8476 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
8478 /* record protocol */
8479 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
8483 * connections redirected to loopback should not match sockets
8484 * bound specifically to loopback due to security implications,
8485 * see tcp_input() and in_pcblookup_listen().
8487 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
8488 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8489 (s
->nat_rule
.ptr
->action
== PF_RDR
||
8490 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
8491 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
8492 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
8497 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8498 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
8499 lr
= s
->nat_rule
.ptr
;
8502 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
8506 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
8507 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
8509 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
8510 dirndx
= (dir
== PF_OUT
);
8511 r
->packets
[dirndx
]++;
8512 r
->bytes
[dirndx
] += pd
.tot_len
;
8514 a
->packets
[dirndx
]++;
8515 a
->bytes
[dirndx
] += pd
.tot_len
;
8519 if (s
->nat_rule
.ptr
!= NULL
) {
8520 s
->nat_rule
.ptr
->packets
[dirndx
]++;
8521 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
8523 if (s
->src_node
!= NULL
) {
8524 s
->src_node
->packets
[dirndx
]++;
8525 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
8527 if (s
->nat_src_node
!= NULL
) {
8528 s
->nat_src_node
->packets
[dirndx
]++;
8529 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
8531 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
8532 s
->packets
[dirndx
]++;
8533 s
->bytes
[dirndx
] += pd
.tot_len
;
8536 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
8540 * XXX: we need to make sure that the addresses
8541 * passed to pfr_update_stats() are the same than
8542 * the addresses used during matching (pfr_match)
8544 if (r
== &pf_default_rule
) {
8546 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8547 &pd
.baddr
: &pd
.naddr
;
8549 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8550 &pd
.naddr
: &pd
.baddr
;
8551 if (x
== &pd
.baddr
|| s
== NULL
) {
8552 /* we need to change the address */
8559 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
8560 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
8561 sk
->direction
== dir
) ?
8562 pd
.src
: pd
.dst
, pd
.af
,
8563 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8565 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
8566 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
8567 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
8568 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8572 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
8576 REASON_SET(&reason
, PFRES_MEMORY
);
8580 if (action
== PF_DROP
) {
8589 if (action
== PF_SYNPROXY_DROP
) {
8594 /* pf_route can free the mbuf causing *m0 to become NULL */
8595 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
8602 #define PF_APPLE_UPDATE_PDESC_IPv6() \
8604 if (m && pd.mp && m != pd.mp) { \
8608 h = mtod(m, struct ip6_hdr *); \
8613 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8614 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8619 struct pfi_kif
*kif
;
8620 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8621 struct mbuf
*m
= *m0
, *n
= NULL
;
8623 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8624 struct pf_state
*s
= NULL
;
8625 struct pf_state_key
*sk
= NULL
;
8626 struct pf_ruleset
*ruleset
= NULL
;
8628 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
8631 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8633 if (!pf_status
.running
)
8636 memset(&pd
, 0, sizeof (pd
));
8638 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8639 DPFPRINTF(PF_DEBUG_URGENT
,
8640 ("pf_test6: pf_get_mtag returned NULL\n"));
8644 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8647 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8650 DPFPRINTF(PF_DEBUG_URGENT
,
8651 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
8654 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8657 VERIFY(m
->m_flags
& M_PKTHDR
);
8659 h
= mtod(m
, struct ip6_hdr
*);
8662 off
= ((caddr_t
)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
8665 pd
.pf_mtag
= pf_get_mtag(m
);
8666 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8667 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8668 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8672 pd
.proto_variant
= 0;
8674 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8675 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
8678 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
8679 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
8680 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
8681 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
8684 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8686 REASON_SET(&reason
, PFRES_SHORT
);
8692 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8694 #endif /* DUMMYNET */
8696 /* We do IP header normalization and packet reassembly here */
8697 action
= pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
);
8699 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8706 #endif /* DUMMYNET */
8707 h
= mtod(m
, struct ip6_hdr
*);
8711 * we do not support jumbogram yet. if we keep going, zero ip6_plen
8712 * will do something bad, so drop the packet for now.
8714 if (htons(h
->ip6_plen
) == 0) {
8716 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
8721 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8722 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8723 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8727 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
8730 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
8731 pd
.proto
= h
->ip6_nxt
;
8732 pd
.proto_variant
= 0;
8735 pd
.pf_mtag
= pf_get_mtag(m
);
8739 case IPPROTO_FRAGMENT
: {
8740 struct ip6_frag ip6f
;
8742 pd
.flags
|= PFDESC_IP_FRAG
;
8743 if (!pf_pull_hdr(m
, off
, &ip6f
, sizeof ip6f
, NULL
,
8745 DPFPRINTF(PF_DEBUG_MISC
,
8746 ("pf: IPv6 short fragment header\n"));
8748 REASON_SET(&reason
, PFRES_SHORT
);
8752 pd
.proto
= nxt
= ip6f
.ip6f_nxt
;
8754 /* Traffic goes through dummynet first */
8755 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8756 if (action
== PF_DROP
|| m
== NULL
) {
8760 #endif /* DUMMYNET */
8761 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
, &pd
, &a
,
8763 if (action
== PF_DROP
) {
8764 REASON_SET(&reason
, PFRES_FRAG
);
8769 case IPPROTO_ROUTING
:
8774 case IPPROTO_HOPOPTS
:
8775 case IPPROTO_DSTOPTS
: {
8776 /* get next header and header length */
8777 struct ip6_ext opt6
;
8779 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
8780 NULL
, &reason
, pd
.af
)) {
8781 DPFPRINTF(PF_DEBUG_MISC
,
8782 ("pf: IPv6 short opt\n"));
8787 if (pd
.proto
== IPPROTO_AH
)
8788 off
+= (opt6
.ip6e_len
+ 2) * 4;
8790 off
+= (opt6
.ip6e_len
+ 1) * 8;
8791 nxt
= opt6
.ip6e_nxt
;
8792 /* goto the next header */
8799 } while (!terminal
);
8801 /* if there's no routing header, use unmodified mbuf for checksumming */
8811 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8812 &action
, &reason
, AF_INET6
)) {
8813 log
= action
!= PF_PASS
;
8816 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8818 /* Traffic goes through dummynet first */
8819 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8820 if (action
== PF_DROP
|| m
== NULL
) {
8824 #endif /* DUMMYNET */
8825 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8828 PF_APPLE_UPDATE_PDESC_IPv6();
8829 if (action
== PF_DROP
)
8831 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8835 PF_APPLE_UPDATE_PDESC_IPv6();
8836 if (action
== PF_PASS
) {
8838 pfsync_update_state(s
);
8839 #endif /* NPFSYNC */
8843 } else if (s
== NULL
)
8844 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8845 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8853 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8854 &action
, &reason
, AF_INET6
)) {
8855 log
= action
!= PF_PASS
;
8858 if (uh
.uh_dport
== 0 ||
8859 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8860 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8862 REASON_SET(&reason
, PFRES_SHORT
);
8866 /* Traffic goes through dummynet first */
8867 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8868 if (action
== PF_DROP
|| m
== NULL
) {
8872 #endif /* DUMMYNET */
8873 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8877 PF_APPLE_UPDATE_PDESC_IPv6();
8878 if (action
== PF_PASS
) {
8880 pfsync_update_state(s
);
8881 #endif /* NPFSYNC */
8885 } else if (s
== NULL
)
8886 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8887 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8891 case IPPROTO_ICMPV6
: {
8892 struct icmp6_hdr ih
;
8895 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
8896 &action
, &reason
, AF_INET6
)) {
8897 log
= action
!= PF_PASS
;
8901 /* Traffic goes through dummynet first */
8902 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8903 if (action
== PF_DROP
|| m
== NULL
) {
8907 #endif /* DUMMYNET */
8908 action
= pf_test_state_icmp(&s
, dir
, kif
,
8909 m
, off
, h
, &pd
, &reason
);
8912 PF_APPLE_UPDATE_PDESC_IPv6();
8913 if (action
== PF_PASS
) {
8915 pfsync_update_state(s
);
8916 #endif /* NPFSYNC */
8920 } else if (s
== NULL
)
8921 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8922 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8927 struct pf_esp_hdr esp
;
8930 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8932 log
= action
!= PF_PASS
;
8936 /* Traffic goes through dummynet first */
8937 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8938 if (action
== PF_DROP
|| m
== NULL
) {
8942 #endif /* DUMMYNET */
8943 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8946 PF_APPLE_UPDATE_PDESC_IPv6();
8947 if (action
== PF_PASS
) {
8949 pfsync_update_state(s
);
8950 #endif /* NPFSYNC */
8954 } else if (s
== NULL
)
8955 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8956 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8961 struct pf_grev1_hdr grev1
;
8963 pd
.hdr
.grev1
= &grev1
;
8964 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8965 &reason
, AF_INET6
)) {
8966 log
= (action
!= PF_PASS
);
8970 /* Traffic goes through dummynet first */
8971 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8972 if (action
== PF_DROP
|| m
== NULL
) {
8976 #endif /* DUMMYNET */
8977 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8978 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8979 if (ntohs(grev1
.payload_length
) >
8980 m
->m_pkthdr
.len
- off
) {
8982 REASON_SET(&reason
, PFRES_SHORT
);
8985 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8988 PF_APPLE_UPDATE_PDESC_IPv6();
8989 if (action
== PF_PASS
) {
8991 pfsync_update_state(s
);
8992 #endif /* NPFSYNC */
8997 } else if (s
== NULL
) {
8998 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8999 h
, &pd
, &a
, &ruleset
, NULL
);
9000 if (action
== PF_PASS
)
9005 /* not GREv1/PPTP, so treat as ordinary GRE... */
9010 /* Traffic goes through dummynet first */
9011 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9012 if (action
== PF_DROP
|| m
== NULL
) {
9016 #endif /* DUMMYNET */
9017 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
9020 PF_APPLE_UPDATE_PDESC_IPv6();
9021 if (action
== PF_PASS
) {
9023 pfsync_update_state(s
);
9024 #endif /* NPFSYNC */
9028 } else if (s
== NULL
)
9029 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9030 &pd
, &a
, &ruleset
, NULL
);
9036 PF_APPLE_UPDATE_PDESC_IPv6();
9043 /* handle dangerous IPv6 extension headers. */
9044 if (action
== PF_PASS
&& rh_cnt
&&
9045 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9047 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9049 DPFPRINTF(PF_DEBUG_MISC
,
9050 ("pf: dropping packet with dangerous v6 headers\n"));
9053 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
9054 (pd
.pktflags
& PKTF_FLOW_ID
))
9055 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9058 if (action
== PF_PASS
) {
9060 if (altq_allowed
&& r
->qid
) {
9061 if (pd
.tos
& IPTOS_LOWDELAY
)
9062 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
9064 pd
.pf_mtag
->pftag_qid
= r
->qid
;
9066 #endif /* PF_ALTQ */
9068 /* add hints for ecn */
9069 pd
.pf_mtag
->pftag_hdr
= h
;
9070 /* record address family */
9071 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET
;
9072 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
9074 /* record protocol */
9075 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
9078 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
9079 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9080 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9081 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9082 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
9083 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9088 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9089 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9090 lr
= s
->nat_rule
.ptr
;
9093 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
9097 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9098 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
9100 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9101 dirndx
= (dir
== PF_OUT
);
9102 r
->packets
[dirndx
]++;
9103 r
->bytes
[dirndx
] += pd
.tot_len
;
9105 a
->packets
[dirndx
]++;
9106 a
->bytes
[dirndx
] += pd
.tot_len
;
9110 if (s
->nat_rule
.ptr
!= NULL
) {
9111 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9112 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9114 if (s
->src_node
!= NULL
) {
9115 s
->src_node
->packets
[dirndx
]++;
9116 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9118 if (s
->nat_src_node
!= NULL
) {
9119 s
->nat_src_node
->packets
[dirndx
]++;
9120 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9122 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9123 s
->packets
[dirndx
]++;
9124 s
->bytes
[dirndx
] += pd
.tot_len
;
9127 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9131 * XXX: we need to make sure that the addresses
9132 * passed to pfr_update_stats() are the same than
9133 * the addresses used during matching (pfr_match)
9135 if (r
== &pf_default_rule
) {
9137 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9138 &pd
.baddr
: &pd
.naddr
;
9140 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9141 &pd
.naddr
: &pd
.baddr
;
9143 if (x
== &pd
.baddr
|| s
== NULL
) {
9150 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9151 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9152 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
9153 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9155 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9156 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9157 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9158 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9163 if (action
== PF_SYNPROXY_DROP
) {
9168 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9169 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9171 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
9175 REASON_SET(&reason
, PFRES_MEMORY
);
9179 if (action
== PF_DROP
) {
9188 if (action
== PF_SYNPROXY_DROP
) {
9193 if (action
== PF_PASS
) {
9195 h
= mtod(m
, struct ip6_hdr
*);
9198 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9199 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9208 pf_check_congestion(struct ifqueue
*ifq
)
9215 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
9216 int flags
, const char *wchan
, void *palloc
)
9218 #pragma unused(align, ioff, flags, palloc)
9219 bzero(pp
, sizeof (*pp
));
9220 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
9221 if (pp
->pool_zone
!= NULL
) {
9222 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
9223 zone_change(pp
->pool_zone
, Z_CALLERACCT
, FALSE
);
9224 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
9225 pp
->pool_name
= wchan
;
9229 /* Zones cannot be currently destroyed */
9231 pool_destroy(struct pool
*pp
)
9237 pool_sethiwat(struct pool
*pp
, int n
)
9239 pp
->pool_hiwat
= n
; /* Currently unused */
9243 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
9245 #pragma unused(warnmess, ratecap)
9250 pool_get(struct pool
*pp
, int flags
)
9254 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9256 if (pp
->pool_count
> pp
->pool_limit
) {
9257 DPFPRINTF(PF_DEBUG_NOISY
,
9258 ("pf: pool %s hard limit reached (%d)\n",
9259 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
9265 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
9268 VERIFY(pp
->pool_count
!= 0);
9274 pool_put(struct pool
*pp
, void *v
)
9276 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9278 zfree(pp
->pool_zone
, v
);
9279 VERIFY(pp
->pool_count
!= 0);
9284 pf_find_mtag(struct mbuf
*m
)
9286 if (!(m
->m_flags
& M_PKTHDR
))
9289 return (m_pftag(m
));
9293 pf_get_mtag(struct mbuf
*m
)
9295 return (pf_find_mtag(m
));
9299 pf_time_second(void)
9308 pf_calendar_time_second(void)
9317 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
9319 struct hook_desc
*hd
;
9321 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
9328 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
9330 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
9336 hook_runloop(struct hook_desc_head
*head
, int flags
)
9338 struct hook_desc
*hd
;
9340 if (!(flags
& HOOK_REMOVE
)) {
9341 if (!(flags
& HOOK_ABORT
))
9342 TAILQ_FOREACH(hd
, head
, hd_list
)
9343 hd
->hd_fn(hd
->hd_arg
);
9345 while (!!(hd
= TAILQ_FIRST(head
))) {
9346 TAILQ_REMOVE(head
, hd
, hd_list
);
9347 if (!(flags
& HOOK_ABORT
))
9348 hd
->hd_fn(hd
->hd_arg
);
9349 if (flags
& HOOK_FREE
)
9350 _FREE(hd
, M_DEVBUF
);