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());
1571 VERIFY(state
->timeout
!= PFTM_UNLINKED
);
1572 VERIFY(state
->timeout
< PFTM_MAX
);
1573 t
= state
->rule
.ptr
->timeout
[state
->timeout
];
1575 t
= pf_default_rule
.timeout
[state
->timeout
];
1576 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
1578 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
1579 states
= state
->rule
.ptr
->states
;
1581 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
1582 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
1583 states
= pf_status
.states
;
1585 if (end
&& states
> start
&& start
< end
) {
1587 return (state
->expire
+ t
* (end
- states
) /
1590 return (pf_time_second());
1592 return (state
->expire
+ t
);
1596 pf_purge_expired_src_nodes(void)
1598 struct pf_src_node
*cur
, *next
;
1600 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1602 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
1603 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
1605 if (cur
->states
<= 0 && cur
->expire
<= pf_time_second()) {
1606 if (cur
->rule
.ptr
!= NULL
) {
1607 cur
->rule
.ptr
->src_nodes
--;
1608 if (cur
->rule
.ptr
->states
<= 0 &&
1609 cur
->rule
.ptr
->max_src_nodes
<= 0)
1610 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1612 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1613 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1614 pf_status
.src_nodes
--;
1615 pool_put(&pf_src_tree_pl
, cur
);
1621 pf_src_tree_remove_state(struct pf_state
*s
)
1625 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1627 if (s
->src_node
!= NULL
) {
1628 if (s
->src
.tcp_est
) {
1629 VERIFY(s
->src_node
->conn
> 0);
1630 --s
->src_node
->conn
;
1632 VERIFY(s
->src_node
->states
> 0);
1633 if (--s
->src_node
->states
<= 0) {
1634 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1636 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1637 s
->src_node
->expire
= pf_time_second() + t
;
1640 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1641 VERIFY(s
->nat_src_node
->states
> 0);
1642 if (--s
->nat_src_node
->states
<= 0) {
1643 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1645 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1646 s
->nat_src_node
->expire
= pf_time_second() + t
;
1649 s
->src_node
= s
->nat_src_node
= NULL
;
1653 pf_unlink_state(struct pf_state
*cur
)
1655 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1657 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1658 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1659 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1660 cur
->state_key
->ext
.xport
.port
,
1661 cur
->state_key
->lan
.xport
.port
,
1662 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1663 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1666 hook_runloop(&cur
->unlink_hooks
, HOOK_REMOVE
|HOOK_FREE
);
1667 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1669 if (cur
->creatorid
== pf_status
.hostid
)
1670 pfsync_delete_state(cur
);
1672 cur
->timeout
= PFTM_UNLINKED
;
1673 pf_src_tree_remove_state(cur
);
1674 pf_detach_state(cur
, 0);
1677 /* callers should be at splpf and hold the
1678 * write_lock on pf_consistency_lock */
1680 pf_free_state(struct pf_state
*cur
)
1682 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1684 if (pfsyncif
!= NULL
&&
1685 (pfsyncif
->sc_bulk_send_next
== cur
||
1686 pfsyncif
->sc_bulk_terminator
== cur
))
1689 VERIFY(cur
->timeout
== PFTM_UNLINKED
);
1690 VERIFY(cur
->rule
.ptr
->states
> 0);
1691 if (--cur
->rule
.ptr
->states
<= 0 &&
1692 cur
->rule
.ptr
->src_nodes
<= 0)
1693 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1694 if (cur
->nat_rule
.ptr
!= NULL
) {
1695 VERIFY(cur
->nat_rule
.ptr
->states
> 0);
1696 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1697 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1698 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1700 if (cur
->anchor
.ptr
!= NULL
) {
1701 VERIFY(cur
->anchor
.ptr
->states
> 0);
1702 if (--cur
->anchor
.ptr
->states
<= 0)
1703 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1705 pf_normalize_tcp_cleanup(cur
);
1706 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1707 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1709 pf_tag_unref(cur
->tag
);
1710 pool_put(&pf_state_pl
, cur
);
1711 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1712 VERIFY(pf_status
.states
> 0);
1717 pf_purge_expired_states(u_int32_t maxcheck
)
1719 static struct pf_state
*cur
= NULL
;
1720 struct pf_state
*next
;
1722 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1724 while (maxcheck
--) {
1725 /* wrap to start of list when we hit the end */
1727 cur
= TAILQ_FIRST(&state_list
);
1729 break; /* list empty */
1732 /* get next state, as cur may get deleted */
1733 next
= TAILQ_NEXT(cur
, entry_list
);
1735 if (cur
->timeout
== PFTM_UNLINKED
) {
1737 } else if (pf_state_expires(cur
) <= pf_time_second()) {
1738 /* unlink and free expired state */
1739 pf_unlink_state(cur
);
1747 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1749 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1751 if (aw
->type
!= PF_ADDR_TABLE
)
1753 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1759 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1761 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1763 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1765 pfr_detach_table(aw
->p
.tbl
);
1770 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1772 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1774 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1776 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1778 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1779 kt
= kt
->pfrkt_root
;
1781 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1786 pf_print_addr(struct pf_addr
*addr
, sa_family_t af
)
1791 u_int32_t a
= ntohl(addr
->addr32
[0]);
1792 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1800 u_int8_t i
, curstart
= 255, curend
= 0,
1801 maxstart
= 0, maxend
= 0;
1802 for (i
= 0; i
< 8; i
++) {
1803 if (!addr
->addr16
[i
]) {
1804 if (curstart
== 255)
1810 if ((curend
- curstart
) >
1811 (maxend
- maxstart
)) {
1812 maxstart
= curstart
;
1819 for (i
= 0; i
< 8; i
++) {
1820 if (i
>= maxstart
&& i
<= maxend
) {
1829 b
= ntohs(addr
->addr16
[i
]);
1842 pf_print_sk_host(struct pf_state_host
*sh
, sa_family_t af
, int proto
,
1843 u_int8_t proto_variant
)
1845 pf_print_addr(&sh
->addr
, af
);
1850 printf("[%08x]", ntohl(sh
->xport
.spi
));
1854 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
1855 printf("[%u]", ntohs(sh
->xport
.call_id
));
1860 printf("[%u]", ntohs(sh
->xport
.port
));
1869 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1871 pf_print_addr(addr
, af
);
1873 printf("[%u]", ntohs(p
));
1877 pf_print_state(struct pf_state
*s
)
1879 struct pf_state_key
*sk
= s
->state_key
;
1880 switch (sk
->proto
) {
1885 printf("GRE%u ", sk
->proto_variant
);
1896 case IPPROTO_ICMPV6
:
1900 printf("%u ", sk
->proto
);
1903 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1905 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1907 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1908 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
1909 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
1910 if (s
->src
.wscale
&& s
->dst
.wscale
)
1911 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
1913 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
1914 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
1915 if (s
->src
.wscale
&& s
->dst
.wscale
)
1916 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
1918 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
1922 pf_print_flags(u_int8_t f
)
1944 #define PF_SET_SKIP_STEPS(i) \
1946 while (head[i] != cur) { \
1947 head[i]->skip[i].ptr = cur; \
1948 head[i] = TAILQ_NEXT(head[i], entries); \
1953 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
1955 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
1958 cur
= TAILQ_FIRST(rules
);
1960 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1962 while (cur
!= NULL
) {
1964 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
1965 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
1966 if (cur
->direction
!= prev
->direction
)
1967 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
1968 if (cur
->af
!= prev
->af
)
1969 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
1970 if (cur
->proto
!= prev
->proto
)
1971 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
1972 if (cur
->src
.neg
!= prev
->src
.neg
||
1973 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
1974 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
1976 union pf_rule_xport
*cx
= &cur
->src
.xport
;
1977 union pf_rule_xport
*px
= &prev
->src
.xport
;
1979 switch (cur
->proto
) {
1982 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1985 if (prev
->proto
== IPPROTO_GRE
||
1986 prev
->proto
== IPPROTO_ESP
||
1987 cx
->range
.op
!= px
->range
.op
||
1988 cx
->range
.port
[0] != px
->range
.port
[0] ||
1989 cx
->range
.port
[1] != px
->range
.port
[1])
1990 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1994 if (cur
->dst
.neg
!= prev
->dst
.neg
||
1995 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
1996 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
1998 union pf_rule_xport
*cx
= &cur
->dst
.xport
;
1999 union pf_rule_xport
*px
= &prev
->dst
.xport
;
2001 switch (cur
->proto
) {
2003 if (cur
->proto
!= prev
->proto
||
2004 cx
->call_id
!= px
->call_id
)
2005 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2008 if (cur
->proto
!= prev
->proto
||
2010 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2013 if (prev
->proto
== IPPROTO_GRE
||
2014 prev
->proto
== IPPROTO_ESP
||
2015 cx
->range
.op
!= px
->range
.op
||
2016 cx
->range
.port
[0] != px
->range
.port
[0] ||
2017 cx
->range
.port
[1] != px
->range
.port
[1])
2018 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2024 cur
= TAILQ_NEXT(cur
, entries
);
2026 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2027 PF_SET_SKIP_STEPS(i
);
2031 pf_calc_state_key_flowhash(struct pf_state_key
*sk
)
2033 struct pf_flowhash_key fh
__attribute__((aligned(8)));
2034 uint32_t flowhash
= 0;
2036 bzero(&fh
, sizeof (fh
));
2037 if (PF_ALEQ(&sk
->lan
.addr
, &sk
->ext
.addr
, sk
->af
)) {
2038 bcopy(&sk
->lan
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2039 bcopy(&sk
->ext
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2041 bcopy(&sk
->ext
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2042 bcopy(&sk
->lan
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2044 if (sk
->lan
.xport
.spi
<= sk
->ext
.xport
.spi
) {
2045 fh
.ap1
.xport
.spi
= sk
->lan
.xport
.spi
;
2046 fh
.ap2
.xport
.spi
= sk
->ext
.xport
.spi
;
2048 fh
.ap1
.xport
.spi
= sk
->ext
.xport
.spi
;
2049 fh
.ap2
.xport
.spi
= sk
->lan
.xport
.spi
;
2052 fh
.proto
= sk
->proto
;
2055 flowhash
= net_flowhash(&fh
, sizeof (fh
), pf_hash_seed
);
2056 if (flowhash
== 0) {
2057 /* try to get a non-zero flowhash */
2058 pf_hash_seed
= RandomULong();
2066 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
2068 if (aw1
->type
!= aw2
->type
)
2070 switch (aw1
->type
) {
2071 case PF_ADDR_ADDRMASK
:
2073 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
2075 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
2078 case PF_ADDR_DYNIFTL
:
2079 return (aw1
->p
.dyn
== NULL
|| aw2
->p
.dyn
== NULL
||
2080 aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
2081 case PF_ADDR_NOROUTE
:
2082 case PF_ADDR_URPFFAILED
:
2085 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
2086 case PF_ADDR_RTLABEL
:
2087 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
2089 printf("invalid address type: %d\n", aw1
->type
);
2095 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
2101 l
= cksum
+ old
- new;
2102 l
= (l
>> 16) + (l
& 0xffff);
2110 pf_change_ap(int dir
, struct mbuf
*m
, struct pf_addr
*a
, u_int16_t
*p
,
2111 u_int16_t
*ic
, u_int16_t
*pc
, struct pf_addr
*an
, u_int16_t pn
,
2112 u_int8_t u
, sa_family_t af
)
2117 PF_ACPY(&ao
, a
, af
);
2125 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2126 ao
.addr16
[0], an
->addr16
[0], 0),
2127 ao
.addr16
[1], an
->addr16
[1], 0);
2130 * If the packet is originated from an ALG on the NAT gateway
2131 * (source address is loopback or local), in which case the
2132 * TCP/UDP checksum field contains the pseudo header checksum
2133 * that's not yet complemented.
2135 if (dir
== PF_OUT
&& m
!= NULL
&&
2136 (m
->m_flags
& M_PKTHDR
) &&
2137 (m
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))) {
2138 /* Pseudo-header checksum does not include ports */
2139 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2140 ao
.addr16
[0], an
->addr16
[0], u
),
2141 ao
.addr16
[1], an
->addr16
[1], u
);
2143 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2144 ao
.addr16
[0], an
->addr16
[0], u
),
2145 ao
.addr16
[1], an
->addr16
[1], u
),
2153 * If the packet is originated from an ALG on the NAT gateway
2154 * (source address is loopback or local), in which case the
2155 * TCP/UDP checksum field contains the pseudo header checksum
2156 * that's not yet complemented.
2158 if (dir
== PF_OUT
&& m
!= NULL
&&
2159 (m
->m_flags
& M_PKTHDR
) &&
2160 (m
->m_pkthdr
.csum_flags
& (CSUM_TCPIPV6
| CSUM_UDPIPV6
))) {
2161 /* Pseudo-header checksum does not include ports */
2162 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2163 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2164 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2165 ao
.addr16
[0], an
->addr16
[0], u
),
2166 ao
.addr16
[1], an
->addr16
[1], u
),
2167 ao
.addr16
[2], an
->addr16
[2], u
),
2168 ao
.addr16
[3], an
->addr16
[3], u
),
2169 ao
.addr16
[4], an
->addr16
[4], u
),
2170 ao
.addr16
[5], an
->addr16
[5], u
),
2171 ao
.addr16
[6], an
->addr16
[6], u
),
2172 ao
.addr16
[7], an
->addr16
[7], u
),
2175 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2176 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2177 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2178 ao
.addr16
[0], an
->addr16
[0], u
),
2179 ao
.addr16
[1], an
->addr16
[1], u
),
2180 ao
.addr16
[2], an
->addr16
[2], u
),
2181 ao
.addr16
[3], an
->addr16
[3], u
),
2182 ao
.addr16
[4], an
->addr16
[4], u
),
2183 ao
.addr16
[5], an
->addr16
[5], u
),
2184 ao
.addr16
[6], an
->addr16
[6], u
),
2185 ao
.addr16
[7], an
->addr16
[7], u
),
2194 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
2196 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
2200 memcpy(&ao
, a
, sizeof (ao
));
2201 memcpy(a
, &an
, sizeof (u_int32_t
));
2202 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
2203 ao
% 65536, an
% 65536, u
);
2208 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
2212 PF_ACPY(&ao
, a
, AF_INET6
);
2213 PF_ACPY(a
, an
, AF_INET6
);
2215 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2216 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2217 pf_cksum_fixup(pf_cksum_fixup(*c
,
2218 ao
.addr16
[0], an
->addr16
[0], u
),
2219 ao
.addr16
[1], an
->addr16
[1], u
),
2220 ao
.addr16
[2], an
->addr16
[2], u
),
2221 ao
.addr16
[3], an
->addr16
[3], u
),
2222 ao
.addr16
[4], an
->addr16
[4], u
),
2223 ao
.addr16
[5], an
->addr16
[5], u
),
2224 ao
.addr16
[6], an
->addr16
[6], u
),
2225 ao
.addr16
[7], an
->addr16
[7], u
);
2230 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
2231 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
2232 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
2234 struct pf_addr oia
, ooa
;
2236 PF_ACPY(&oia
, ia
, af
);
2237 PF_ACPY(&ooa
, oa
, af
);
2239 /* Change inner protocol port, fix inner protocol checksum. */
2241 u_int16_t oip
= *ip
;
2248 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
2249 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
2251 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
2253 /* Change inner ip address, fix inner ip and icmp checksums. */
2254 PF_ACPY(ia
, na
, af
);
2258 u_int32_t oh2c
= *h2c
;
2260 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
2261 oia
.addr16
[0], ia
->addr16
[0], 0),
2262 oia
.addr16
[1], ia
->addr16
[1], 0);
2263 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2264 oia
.addr16
[0], ia
->addr16
[0], 0),
2265 oia
.addr16
[1], ia
->addr16
[1], 0);
2266 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
2272 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2273 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2274 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2275 oia
.addr16
[0], ia
->addr16
[0], u
),
2276 oia
.addr16
[1], ia
->addr16
[1], u
),
2277 oia
.addr16
[2], ia
->addr16
[2], u
),
2278 oia
.addr16
[3], ia
->addr16
[3], u
),
2279 oia
.addr16
[4], ia
->addr16
[4], u
),
2280 oia
.addr16
[5], ia
->addr16
[5], u
),
2281 oia
.addr16
[6], ia
->addr16
[6], u
),
2282 oia
.addr16
[7], ia
->addr16
[7], u
);
2286 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
2287 PF_ACPY(oa
, na
, af
);
2291 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
2292 ooa
.addr16
[0], oa
->addr16
[0], 0),
2293 ooa
.addr16
[1], oa
->addr16
[1], 0);
2298 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2299 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2300 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2301 ooa
.addr16
[0], oa
->addr16
[0], u
),
2302 ooa
.addr16
[1], oa
->addr16
[1], u
),
2303 ooa
.addr16
[2], oa
->addr16
[2], u
),
2304 ooa
.addr16
[3], oa
->addr16
[3], u
),
2305 ooa
.addr16
[4], oa
->addr16
[4], u
),
2306 ooa
.addr16
[5], oa
->addr16
[5], u
),
2307 ooa
.addr16
[6], oa
->addr16
[6], u
),
2308 ooa
.addr16
[7], oa
->addr16
[7], u
);
2316 * Need to modulate the sequence numbers in the TCP SACK option
2317 * (credits to Krzysztof Pfaff for report and patch)
2320 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
2321 struct tcphdr
*th
, struct pf_state_peer
*dst
)
2323 int hlen
= (th
->th_off
<< 2) - sizeof (*th
), thoptlen
= hlen
;
2324 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
2325 int copyback
= 0, i
, olen
;
2326 struct sackblk sack
;
2328 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2329 if (hlen
< TCPOLEN_SACKLEN
||
2330 !pf_pull_hdr(m
, off
+ sizeof (*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
2333 while (hlen
>= TCPOLEN_SACKLEN
) {
2336 case TCPOPT_EOL
: /* FALLTHROUGH */
2344 if (olen
>= TCPOLEN_SACKLEN
) {
2345 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
2346 i
+= TCPOLEN_SACK
) {
2347 memcpy(&sack
, &opt
[i
], sizeof (sack
));
2348 pf_change_a(&sack
.start
, &th
->th_sum
,
2349 htonl(ntohl(sack
.start
) -
2351 pf_change_a(&sack
.end
, &th
->th_sum
,
2352 htonl(ntohl(sack
.end
) -
2354 memcpy(&opt
[i
], &sack
, sizeof (sack
));
2356 copyback
= off
+ sizeof (*th
) + thoptlen
;
2368 m
= pf_lazy_makewritable(pd
, m
, copyback
);
2371 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2377 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
2378 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
2379 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
2380 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
2381 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
2383 #pragma unused(eh, ifp)
2387 struct ip
*h
= NULL
;
2390 struct ip6_hdr
*h6
= NULL
;
2392 struct tcphdr
*th
= NULL
;
2394 struct pf_mtag
*pf_mtag
;
2396 /* maximum segment size tcp option */
2397 tlen
= sizeof (struct tcphdr
);
2404 len
= sizeof (struct ip
) + tlen
;
2409 len
= sizeof (struct ip6_hdr
) + tlen
;
2413 panic("pf_send_tcp: not AF_INET or AF_INET6!");
2417 /* create outgoing mbuf */
2418 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
2422 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
2428 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2429 pf_mtag
->pftag_tag
= rtag
;
2431 if (r
!= NULL
&& PF_RTABLEID_IS_VALID(r
->rtableid
))
2432 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2435 if (altq_allowed
&& r
!= NULL
&& r
->qid
)
2436 pf_mtag
->pftag_qid
= r
->qid
;
2437 #endif /* PF_ALTQ */
2440 /* add hints for ecn */
2441 pf_mtag
->pftag_hdr
= mtod(m
, struct ip
*);
2442 /* record address family */
2443 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2447 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2452 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2458 /* indicate this is TCP */
2459 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
2461 /* Make sure headers are 32-bit aligned */
2462 m
->m_data
+= max_linkhdr
;
2463 m
->m_pkthdr
.len
= m
->m_len
= len
;
2464 m
->m_pkthdr
.rcvif
= NULL
;
2465 bzero(m
->m_data
, len
);
2469 h
= mtod(m
, struct ip
*);
2471 /* IP header fields included in the TCP checksum */
2472 h
->ip_p
= IPPROTO_TCP
;
2473 h
->ip_len
= htons(tlen
);
2474 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
2475 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
2477 th
= (struct tcphdr
*)(void *)((caddr_t
)h
+ sizeof (struct ip
));
2482 h6
= mtod(m
, struct ip6_hdr
*);
2484 /* IP header fields included in the TCP checksum */
2485 h6
->ip6_nxt
= IPPROTO_TCP
;
2486 h6
->ip6_plen
= htons(tlen
);
2487 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof (struct in6_addr
));
2488 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof (struct in6_addr
));
2490 th
= (struct tcphdr
*)(void *)
2491 ((caddr_t
)h6
+ sizeof (struct ip6_hdr
));
2497 th
->th_sport
= sport
;
2498 th
->th_dport
= dport
;
2499 th
->th_seq
= htonl(seq
);
2500 th
->th_ack
= htonl(ack
);
2501 th
->th_off
= tlen
>> 2;
2502 th
->th_flags
= flags
;
2503 th
->th_win
= htons(win
);
2506 opt
= (char *)(th
+ 1);
2507 opt
[0] = TCPOPT_MAXSEG
;
2509 #if BYTE_ORDER != BIG_ENDIAN
2512 bcopy((caddr_t
)&mss
, (caddr_t
)(opt
+ 2), 2);
2521 th
->th_sum
= in_cksum(m
, len
);
2523 /* Finish the IP header */
2525 h
->ip_hl
= sizeof (*h
) >> 2;
2526 h
->ip_tos
= IPTOS_LOWDELAY
;
2528 * ip_output() expects ip_len and ip_off to be in host order.
2531 h
->ip_off
= (path_mtu_discovery
? IP_DF
: 0);
2532 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
2535 bzero(&ro
, sizeof (ro
));
2536 ip_output(m
, NULL
, &ro
, 0, NULL
, NULL
);
2543 struct route_in6 ro6
;
2546 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
2547 sizeof (struct ip6_hdr
), tlen
);
2549 h6
->ip6_vfc
|= IPV6_VERSION
;
2550 h6
->ip6_hlim
= IPV6_DEFHLIM
;
2552 bzero(&ro6
, sizeof (ro6
));
2553 ip6_output(m
, NULL
, &ro6
, 0, NULL
, NULL
, NULL
);
2554 ROUTE_RELEASE(&ro6
);
2562 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
2566 struct pf_mtag
*pf_mtag
;
2568 m0
= m_copy(m
, 0, M_COPYALL
);
2572 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
2575 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2577 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
2578 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2581 if (altq_allowed
&& r
->qid
)
2582 pf_mtag
->pftag_qid
= r
->qid
;
2583 #endif /* PF_ALTQ */
2586 /* add hints for ecn */
2587 pf_mtag
->pftag_hdr
= mtod(m0
, struct ip
*);
2588 /* record address family */
2589 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2593 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2594 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMP
;
2599 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2600 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMPV6
;
2609 icmp_error(m0
, type
, code
, 0, 0);
2614 icmp6_error(m0
, type
, code
, 0);
2621 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2622 * If n is 0, they match if they are equal. If n is != 0, they match if they
2626 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
2627 struct pf_addr
*b
, sa_family_t af
)
2634 if ((a
->addr32
[0] & m
->addr32
[0]) ==
2635 (b
->addr32
[0] & m
->addr32
[0]))
2641 if (((a
->addr32
[0] & m
->addr32
[0]) ==
2642 (b
->addr32
[0] & m
->addr32
[0])) &&
2643 ((a
->addr32
[1] & m
->addr32
[1]) ==
2644 (b
->addr32
[1] & m
->addr32
[1])) &&
2645 ((a
->addr32
[2] & m
->addr32
[2]) ==
2646 (b
->addr32
[2] & m
->addr32
[2])) &&
2647 ((a
->addr32
[3] & m
->addr32
[3]) ==
2648 (b
->addr32
[3] & m
->addr32
[3])))
2667 * Return 1 if b <= a <= e, otherwise return 0.
2670 pf_match_addr_range(struct pf_addr
*b
, struct pf_addr
*e
,
2671 struct pf_addr
*a
, sa_family_t af
)
2676 if ((a
->addr32
[0] < b
->addr32
[0]) ||
2677 (a
->addr32
[0] > e
->addr32
[0]))
2686 for (i
= 0; i
< 4; ++i
)
2687 if (a
->addr32
[i
] > b
->addr32
[i
])
2689 else if (a
->addr32
[i
] < b
->addr32
[i
])
2692 for (i
= 0; i
< 4; ++i
)
2693 if (a
->addr32
[i
] < e
->addr32
[i
])
2695 else if (a
->addr32
[i
] > e
->addr32
[i
])
2705 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
2709 return ((p
> a1
) && (p
< a2
));
2711 return ((p
< a1
) || (p
> a2
));
2713 return ((p
>= a1
) && (p
<= a2
));
2727 return (0); /* never reached */
2731 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
2733 #if BYTE_ORDER != BIG_ENDIAN
2738 return (pf_match(op
, a1
, a2
, p
));
2742 pf_match_xport(u_int8_t proto
, u_int8_t proto_variant
, union pf_rule_xport
*rx
,
2743 union pf_state_xport
*sx
)
2750 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
2751 d
= (rx
->call_id
== sx
->call_id
);
2755 d
= (rx
->spi
== sx
->spi
);
2761 case IPPROTO_ICMPV6
:
2763 d
= pf_match_port(rx
->range
.op
,
2764 rx
->range
.port
[0], rx
->range
.port
[1],
2777 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
2779 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2781 return (pf_match(op
, a1
, a2
, u
));
2785 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
2787 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2789 return (pf_match(op
, a1
, a2
, g
));
2793 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, struct pf_mtag
*pf_mtag
,
2798 *tag
= pf_mtag
->pftag_tag
;
2800 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2801 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2805 pf_tag_packet(struct mbuf
*m
, struct pf_mtag
*pf_mtag
, int tag
,
2806 unsigned int rtableid
, struct pf_pdesc
*pd
)
2808 if (tag
<= 0 && !PF_RTABLEID_IS_VALID(rtableid
) &&
2809 (pd
== NULL
|| !(pd
->pktflags
& PKTF_FLOW_ID
)))
2812 if (pf_mtag
== NULL
&& (pf_mtag
= pf_get_mtag(m
)) == NULL
)
2816 pf_mtag
->pftag_tag
= tag
;
2817 if (PF_RTABLEID_IS_VALID(rtableid
))
2818 pf_mtag
->pftag_rtableid
= rtableid
;
2819 if (pd
!= NULL
&& (pd
->pktflags
& PKTF_FLOW_ID
)) {
2820 m
->m_pkthdr
.pkt_flowsrc
= pd
->flowsrc
;
2821 m
->m_pkthdr
.pkt_flowid
= pd
->flowhash
;
2822 m
->m_pkthdr
.pkt_flags
|= pd
->pktflags
;
2823 m
->m_pkthdr
.pkt_proto
= pd
->proto
;
2830 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2831 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2833 struct pf_anchor_stackframe
*f
;
2835 (*r
)->anchor
->match
= 0;
2838 if (*depth
>= (int)sizeof (pf_anchor_stack
) /
2839 (int)sizeof (pf_anchor_stack
[0])) {
2840 printf("pf_step_into_anchor: stack overflow\n");
2841 *r
= TAILQ_NEXT(*r
, entries
);
2843 } else if (*depth
== 0 && a
!= NULL
)
2845 f
= pf_anchor_stack
+ (*depth
)++;
2848 if ((*r
)->anchor_wildcard
) {
2849 f
->parent
= &(*r
)->anchor
->children
;
2850 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2855 *rs
= &f
->child
->ruleset
;
2859 *rs
= &(*r
)->anchor
->ruleset
;
2861 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2865 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2866 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2868 struct pf_anchor_stackframe
*f
;
2874 f
= pf_anchor_stack
+ *depth
- 1;
2875 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2876 if (f
->child
->match
||
2877 (match
!= NULL
&& *match
)) {
2878 f
->r
->anchor
->match
= 1;
2881 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
2882 if (f
->child
!= NULL
) {
2883 *rs
= &f
->child
->ruleset
;
2884 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2892 if (*depth
== 0 && a
!= NULL
)
2895 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
2896 quick
= f
->r
->quick
;
2897 *r
= TAILQ_NEXT(f
->r
, entries
);
2898 } while (*r
== NULL
);
2905 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
2906 struct pf_addr
*rmask
, struct pf_addr
*saddr
, sa_family_t af
)
2911 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2912 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2916 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2917 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2918 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
2919 ((rmask
->addr32
[1] ^ 0xffffffff) & saddr
->addr32
[1]);
2920 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
2921 ((rmask
->addr32
[2] ^ 0xffffffff) & saddr
->addr32
[2]);
2922 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
2923 ((rmask
->addr32
[3] ^ 0xffffffff) & saddr
->addr32
[3]);
2929 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
2934 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
2938 if (addr
->addr32
[3] == 0xffffffff) {
2939 addr
->addr32
[3] = 0;
2940 if (addr
->addr32
[2] == 0xffffffff) {
2941 addr
->addr32
[2] = 0;
2942 if (addr
->addr32
[1] == 0xffffffff) {
2943 addr
->addr32
[1] = 0;
2945 htonl(ntohl(addr
->addr32
[0]) + 1);
2948 htonl(ntohl(addr
->addr32
[1]) + 1);
2951 htonl(ntohl(addr
->addr32
[2]) + 1);
2954 htonl(ntohl(addr
->addr32
[3]) + 1);
2960 #define mix(a, b, c) \
2962 a -= b; a -= c; a ^= (c >> 13); \
2963 b -= c; b -= a; b ^= (a << 8); \
2964 c -= a; c -= b; c ^= (b >> 13); \
2965 a -= b; a -= c; a ^= (c >> 12); \
2966 b -= c; b -= a; b ^= (a << 16); \
2967 c -= a; c -= b; c ^= (b >> 5); \
2968 a -= b; a -= c; a ^= (c >> 3); \
2969 b -= c; b -= a; b ^= (a << 10); \
2970 c -= a; c -= b; c ^= (b >> 15); \
2974 * hash function based on bridge_hash in if_bridge.c
2977 pf_hash(struct pf_addr
*inaddr
, struct pf_addr
*hash
,
2978 struct pf_poolhashkey
*key
, sa_family_t af
)
2980 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
2985 a
+= inaddr
->addr32
[0];
2988 hash
->addr32
[0] = c
+ key
->key32
[2];
2993 a
+= inaddr
->addr32
[0];
2994 b
+= inaddr
->addr32
[2];
2996 hash
->addr32
[0] = c
;
2997 a
+= inaddr
->addr32
[1];
2998 b
+= inaddr
->addr32
[3];
3001 hash
->addr32
[1] = c
;
3002 a
+= inaddr
->addr32
[2];
3003 b
+= inaddr
->addr32
[1];
3006 hash
->addr32
[2] = c
;
3007 a
+= inaddr
->addr32
[3];
3008 b
+= inaddr
->addr32
[0];
3011 hash
->addr32
[3] = c
;
3018 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, struct pf_addr
*saddr
,
3019 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
3021 unsigned char hash
[16];
3022 struct pf_pool
*rpool
= &r
->rpool
;
3023 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3024 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3025 struct pf_pooladdr
*acur
= rpool
->cur
;
3026 struct pf_src_node k
;
3028 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
3029 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3031 PF_ACPY(&k
.addr
, saddr
, af
);
3032 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
3033 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
3037 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
3038 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
3039 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, af
)) {
3040 PF_ACPY(naddr
, &(*sn
)->raddr
, af
);
3041 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
3042 printf("pf_map_addr: src tracking maps ");
3043 pf_print_host(&k
.addr
, 0, af
);
3045 pf_print_host(naddr
, 0, af
);
3052 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
3054 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3055 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3060 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
3061 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3064 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
3065 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
3070 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
3071 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3074 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
3075 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
3079 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3080 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
3081 return (1); /* unsupported */
3083 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3084 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3087 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
3089 PF_ACPY(naddr
, raddr
, af
);
3091 case PF_POOL_BITMASK
:
3092 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3094 case PF_POOL_RANDOM
:
3095 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
)) {
3099 rpool
->counter
.addr32
[0] = htonl(random());
3104 if (rmask
->addr32
[3] != 0xffffffff)
3105 rpool
->counter
.addr32
[3] =
3109 if (rmask
->addr32
[2] != 0xffffffff)
3110 rpool
->counter
.addr32
[2] =
3114 if (rmask
->addr32
[1] != 0xffffffff)
3115 rpool
->counter
.addr32
[1] =
3119 if (rmask
->addr32
[0] != 0xffffffff)
3120 rpool
->counter
.addr32
[0] =
3125 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3126 PF_ACPY(init_addr
, naddr
, af
);
3129 PF_AINC(&rpool
->counter
, af
);
3130 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3133 case PF_POOL_SRCHASH
:
3134 pf_hash(saddr
, (struct pf_addr
*)(void *)&hash
,
3136 PF_POOLMASK(naddr
, raddr
, rmask
,
3137 (struct pf_addr
*)(void *)&hash
, af
);
3139 case PF_POOL_ROUNDROBIN
:
3140 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3141 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3142 &rpool
->tblidx
, &rpool
->counter
,
3143 &raddr
, &rmask
, af
))
3145 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3146 if (rpool
->cur
->addr
.p
.dyn
!= NULL
&&
3147 !pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3148 &rpool
->tblidx
, &rpool
->counter
,
3149 &raddr
, &rmask
, af
))
3151 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
, af
))
3155 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
3156 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
3157 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3159 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3160 &rpool
->tblidx
, &rpool
->counter
,
3161 &raddr
, &rmask
, af
)) {
3162 /* table contains no address of type 'af' */
3163 if (rpool
->cur
!= acur
)
3167 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3169 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3171 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3172 &rpool
->tblidx
, &rpool
->counter
,
3173 &raddr
, &rmask
, af
)) {
3174 /* table contains no address of type 'af' */
3175 if (rpool
->cur
!= acur
)
3180 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3181 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3182 PF_ACPY(&rpool
->counter
, raddr
, af
);
3186 PF_ACPY(naddr
, &rpool
->counter
, af
);
3187 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
))
3188 PF_ACPY(init_addr
, naddr
, af
);
3189 PF_AINC(&rpool
->counter
, af
);
3193 PF_ACPY(&(*sn
)->raddr
, naddr
, af
);
3195 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
3196 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3197 printf("pf_map_addr: selected address ");
3198 pf_print_host(naddr
, 0, af
);
3206 pf_get_sport(struct pf_pdesc
*pd
, struct pfi_kif
*kif
, struct pf_rule
*r
,
3207 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3208 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3209 union pf_state_xport
*nxport
, struct pf_src_node
**sn
)
3212 struct pf_state_key_cmp key
;
3213 struct pf_addr init_addr
;
3215 sa_family_t af
= pd
->af
;
3216 u_int8_t proto
= pd
->proto
;
3217 unsigned int low
= r
->rpool
.proxy_port
[0];
3218 unsigned int high
= r
->rpool
.proxy_port
[1];
3220 bzero(&init_addr
, sizeof (init_addr
));
3221 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3224 if (proto
== IPPROTO_ICMP
) {
3230 return (0); /* No output necessary. */
3232 /*--- Special mapping rules for UDP ---*/
3233 if (proto
== IPPROTO_UDP
) {
3235 /*--- Never float IKE source port ---*/
3236 if (ntohs(sxport
->port
) == PF_IKE_PORT
) {
3237 nxport
->port
= sxport
->port
;
3241 /*--- Apply exterior mapping options ---*/
3242 if (r
->extmap
> PF_EXTMAP_APD
) {
3245 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3246 struct pf_state_key
*sk
= s
->state_key
;
3249 if (s
->nat_rule
.ptr
!= r
)
3251 if (sk
->proto
!= IPPROTO_UDP
|| sk
->af
!= af
)
3253 if (sk
->lan
.xport
.port
!= sxport
->port
)
3255 if (PF_ANEQ(&sk
->lan
.addr
, saddr
, af
))
3257 if (r
->extmap
< PF_EXTMAP_EI
&&
3258 PF_ANEQ(&sk
->ext
.addr
, daddr
, af
))
3261 nxport
->port
= sk
->gwy
.xport
.port
;
3265 } else if (proto
== IPPROTO_TCP
) {
3268 * APPLE MODIFICATION: <rdar://problem/6546358>
3269 * Fix allows....NAT to use a single binding for TCP session
3270 * with same source IP and source port
3272 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3273 struct pf_state_key
* sk
= s
->state_key
;
3276 if (s
->nat_rule
.ptr
!= r
)
3278 if (sk
->proto
!= IPPROTO_TCP
|| sk
->af
!= af
)
3280 if (sk
->lan
.xport
.port
!= sxport
->port
)
3282 if (!(PF_AEQ(&sk
->lan
.addr
, saddr
, af
)))
3284 nxport
->port
= sk
->gwy
.xport
.port
;
3291 PF_ACPY(&key
.ext
.addr
, daddr
, key
.af
);
3292 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af
);
3295 key
.proto_variant
= r
->extfilter
;
3298 key
.proto_variant
= 0;
3302 key
.ext
.xport
= *dxport
;
3304 memset(&key
.ext
.xport
, 0, sizeof (key
.ext
.xport
));
3306 * port search; start random, step;
3307 * similar 2 portloop in in_pcbbind
3309 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
3310 proto
== IPPROTO_ICMP
)) {
3312 key
.gwy
.xport
= *dxport
;
3314 memset(&key
.gwy
.xport
, 0,
3315 sizeof (key
.ext
.xport
));
3316 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3318 } else if (low
== 0 && high
== 0) {
3319 key
.gwy
.xport
= *nxport
;
3320 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3322 } else if (low
== high
) {
3323 key
.gwy
.xport
.port
= htons(low
);
3324 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3325 nxport
->port
= htons(low
);
3336 cut
= htonl(random()) % (1 + high
- low
) + low
;
3337 /* low <= cut <= high */
3338 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
3339 key
.gwy
.xport
.port
= htons(tmp
);
3340 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3342 nxport
->port
= htons(tmp
);
3346 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
3347 key
.gwy
.xport
.port
= htons(tmp
);
3348 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3350 nxport
->port
= htons(tmp
);
3356 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
3357 case PF_POOL_RANDOM
:
3358 case PF_POOL_ROUNDROBIN
:
3359 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3363 case PF_POOL_SRCHASH
:
3364 case PF_POOL_BITMASK
:
3368 } while (!PF_AEQ(&init_addr
, naddr
, af
));
3370 return (1); /* none available */
3373 static struct pf_rule
*
3374 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3375 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
,
3376 union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3377 union pf_state_xport
*dxport
, int rs_num
)
3379 struct pf_rule
*r
, *rm
= NULL
;
3380 struct pf_ruleset
*ruleset
= NULL
;
3382 unsigned int rtableid
= IFSCOPE_NONE
;
3385 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
3386 while (r
&& rm
== NULL
) {
3387 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
3388 struct pf_addr_wrap
*xdst
= NULL
;
3389 struct pf_addr_wrap
*xsrc
= NULL
;
3390 union pf_rule_xport rdrxport
;
3392 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
3394 if (r
->rpool
.cur
!= NULL
)
3395 xdst
= &r
->rpool
.cur
->addr
;
3396 } else if (r
->action
== PF_RDR
&& direction
== PF_OUT
) {
3399 if (r
->rpool
.cur
!= NULL
) {
3400 rdrxport
.range
.op
= PF_OP_EQ
;
3401 rdrxport
.range
.port
[0] =
3402 htons(r
->rpool
.proxy_port
[0]);
3403 xsrc
= &r
->rpool
.cur
->addr
;
3411 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3412 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3413 else if (r
->direction
&& r
->direction
!= direction
)
3414 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3415 else if (r
->af
&& r
->af
!= pd
->af
)
3416 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3417 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3418 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3419 else if (xsrc
&& PF_MISMATCHAW(xsrc
, saddr
, pd
->af
, 0, NULL
))
3420 r
= TAILQ_NEXT(r
, entries
);
3421 else if (!xsrc
&& PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3423 r
= TAILQ_NEXT(r
, entries
);
3424 else if (xsrc
&& (!rdrxport
.range
.port
[0] ||
3425 !pf_match_xport(r
->proto
, r
->proto_variant
, &rdrxport
,
3427 r
= TAILQ_NEXT(r
, entries
);
3428 else if (!xsrc
&& !pf_match_xport(r
->proto
,
3429 r
->proto_variant
, &src
->xport
, sxport
))
3430 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
3431 PF_SKIP_DST_PORT
].ptr
;
3432 else if (dst
!= NULL
&&
3433 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
3434 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3435 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
3437 r
= TAILQ_NEXT(r
, entries
);
3438 else if (dst
&& !pf_match_xport(r
->proto
, r
->proto_variant
,
3439 &dst
->xport
, dxport
))
3440 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3441 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
3442 r
= TAILQ_NEXT(r
, entries
);
3443 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
3444 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
3445 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
3446 r
= TAILQ_NEXT(r
, entries
);
3450 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
3451 rtableid
= r
->rtableid
;
3452 if (r
->anchor
== NULL
) {
3455 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
3459 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
3462 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, NULL
))
3464 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
3465 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
))
3470 static struct pf_rule
*
3471 pf_get_translation_aux(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3472 int direction
, struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3473 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3474 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3475 union pf_state_xport
*nxport
)
3477 struct pf_rule
*r
= NULL
;
3479 if (direction
== PF_OUT
) {
3480 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3481 sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3483 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3484 saddr
, sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3486 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3487 saddr
, sxport
, daddr
, dxport
, PF_RULESET_NAT
);
3489 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3490 sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3492 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3493 saddr
, sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3497 switch (r
->action
) {
3503 if (pf_get_sport(pd
, kif
, r
, saddr
, sxport
, daddr
,
3504 dxport
, naddr
, nxport
, sn
)) {
3505 DPFPRINTF(PF_DEBUG_MISC
,
3506 ("pf: NAT proxy port allocation "
3508 r
->rpool
.proxy_port
[0],
3509 r
->rpool
.proxy_port
[1]));
3514 switch (direction
) {
3516 if (r
->rpool
.cur
->addr
.type
==
3518 if (r
->rpool
.cur
->addr
.p
.dyn
== NULL
)
3523 if (r
->rpool
.cur
->addr
.p
.dyn
->
3527 &r
->rpool
.cur
->addr
.p
.dyn
->
3529 &r
->rpool
.cur
->addr
.p
.dyn
->
3536 if (r
->rpool
.cur
->addr
.p
.dyn
->
3540 &r
->rpool
.cur
->addr
.p
.dyn
->
3542 &r
->rpool
.cur
->addr
.p
.dyn
->
3550 &r
->rpool
.cur
->addr
.v
.a
.addr
,
3551 &r
->rpool
.cur
->addr
.v
.a
.mask
,
3556 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
3557 if (r
->src
.addr
.p
.dyn
== NULL
)
3562 if (r
->src
.addr
.p
.dyn
->
3566 &r
->src
.addr
.p
.dyn
->
3568 &r
->src
.addr
.p
.dyn
->
3575 if (r
->src
.addr
.p
.dyn
->
3579 &r
->src
.addr
.p
.dyn
->
3581 &r
->src
.addr
.p
.dyn
->
3589 &r
->src
.addr
.v
.a
.addr
,
3590 &r
->src
.addr
.v
.a
.mask
, daddr
,
3596 switch (direction
) {
3598 if (r
->dst
.addr
.type
== PF_ADDR_DYNIFTL
) {
3599 if (r
->dst
.addr
.p
.dyn
== NULL
)
3604 if (r
->dst
.addr
.p
.dyn
->
3608 &r
->dst
.addr
.p
.dyn
->
3610 &r
->dst
.addr
.p
.dyn
->
3617 if (r
->dst
.addr
.p
.dyn
->
3621 &r
->dst
.addr
.p
.dyn
->
3623 &r
->dst
.addr
.p
.dyn
->
3631 &r
->dst
.addr
.v
.a
.addr
,
3632 &r
->dst
.addr
.v
.a
.mask
,
3635 if (nxport
&& r
->dst
.xport
.range
.port
[0])
3637 r
->dst
.xport
.range
.port
[0];
3640 if (pf_map_addr(pd
->af
, r
, saddr
,
3643 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3645 PF_POOLMASK(naddr
, naddr
,
3646 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3649 if (nxport
&& dxport
) {
3650 if (r
->rpool
.proxy_port
[1]) {
3651 u_int32_t tmp_nport
;
3654 ((ntohs(dxport
->port
) -
3655 ntohs(r
->dst
.xport
.range
.
3657 (r
->rpool
.proxy_port
[1] -
3658 r
->rpool
.proxy_port
[0] +
3659 1)) + r
->rpool
.proxy_port
[0];
3661 /* wrap around if necessary */
3662 if (tmp_nport
> 65535)
3665 htons((u_int16_t
)tmp_nport
);
3666 } else if (r
->rpool
.proxy_port
[0]) {
3667 nxport
->port
= htons(r
->rpool
.
3684 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
3686 struct pf_addr
*saddr
, *daddr
;
3687 u_int16_t sport
, dport
;
3688 struct inpcbinfo
*pi
;
3693 pd
->lookup
.uid
= UID_MAX
;
3694 pd
->lookup
.gid
= GID_MAX
;
3695 pd
->lookup
.pid
= NO_PID
;
3697 switch (pd
->proto
) {
3699 if (pd
->hdr
.tcp
== NULL
)
3701 sport
= pd
->hdr
.tcp
->th_sport
;
3702 dport
= pd
->hdr
.tcp
->th_dport
;
3706 if (pd
->hdr
.udp
== NULL
)
3708 sport
= pd
->hdr
.udp
->uh_sport
;
3709 dport
= pd
->hdr
.udp
->uh_dport
;
3715 if (direction
== PF_IN
) {
3730 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
, daddr
->v4
, dport
,
3731 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3734 struct in6_addr s6
, d6
;
3736 memset(&s6
, 0, sizeof (s6
));
3737 s6
.s6_addr16
[5] = htons(0xffff);
3738 memcpy(&s6
.s6_addr32
[3], &saddr
->v4
,
3739 sizeof (saddr
->v4
));
3741 memset(&d6
, 0, sizeof (d6
));
3742 d6
.s6_addr16
[5] = htons(0xffff);
3743 memcpy(&d6
.s6_addr32
[3], &daddr
->v4
,
3744 sizeof (daddr
->v4
));
3746 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3747 &d6
, dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3749 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3750 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3752 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3753 &d6
, dport
, INPLOOKUP_WILDCARD
,
3754 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3762 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3763 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
,
3764 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3773 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
, &daddr
->v6
,
3774 dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3776 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
,
3777 &daddr
->v6
, dport
, INPLOOKUP_WILDCARD
,
3778 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3793 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3797 u_int8_t
*opt
, optlen
;
3798 u_int8_t wscale
= 0;
3800 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3801 if (hlen
<= (int)sizeof (struct tcphdr
))
3803 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3805 opt
= hdr
+ sizeof (struct tcphdr
);
3806 hlen
-= sizeof (struct tcphdr
);
3816 if (wscale
> TCP_MAX_WINSHIFT
)
3817 wscale
= TCP_MAX_WINSHIFT
;
3818 wscale
|= PF_WSCALE_FLAG
;
3833 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3837 u_int8_t
*opt
, optlen
;
3838 u_int16_t mss
= tcp_mssdflt
;
3840 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3841 if (hlen
<= (int)sizeof (struct tcphdr
))
3843 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3845 opt
= hdr
+ sizeof (struct tcphdr
);
3846 hlen
-= sizeof (struct tcphdr
);
3847 while (hlen
>= TCPOLEN_MAXSEG
) {
3855 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, 2);
3856 #if BYTE_ORDER != BIG_ENDIAN
3873 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
3876 struct sockaddr_in
*dst
;
3880 struct sockaddr_in6
*dst6
;
3881 struct route_in6 ro6
;
3883 struct rtentry
*rt
= NULL
;
3885 u_int16_t mss
= tcp_mssdflt
;
3890 hlen
= sizeof (struct ip
);
3891 bzero(&ro
, sizeof (ro
));
3892 dst
= (struct sockaddr_in
*)(void *)&ro
.ro_dst
;
3893 dst
->sin_family
= AF_INET
;
3894 dst
->sin_len
= sizeof (*dst
);
3895 dst
->sin_addr
= addr
->v4
;
3902 hlen
= sizeof (struct ip6_hdr
);
3903 bzero(&ro6
, sizeof (ro6
));
3904 dst6
= (struct sockaddr_in6
*)(void *)&ro6
.ro_dst
;
3905 dst6
->sin6_family
= AF_INET6
;
3906 dst6
->sin6_len
= sizeof (*dst6
);
3907 dst6
->sin6_addr
= addr
->v6
;
3908 rtalloc((struct route
*)&ro
);
3913 panic("pf_calc_mss: not AF_INET or AF_INET6!");
3917 if (rt
&& rt
->rt_ifp
) {
3918 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof (struct tcphdr
);
3919 mss
= max(tcp_mssdflt
, mss
);
3922 mss
= min(mss
, offer
);
3923 mss
= max(mss
, 64); /* sanity - at least max opt space */
3928 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
)
3930 struct pf_rule
*r
= s
->rule
.ptr
;
3933 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
3935 switch (s
->state_key
->af
) {
3938 pf_map_addr(AF_INET
, r
, saddr
, &s
->rt_addr
, NULL
,
3940 s
->rt_kif
= r
->rpool
.cur
->kif
;
3945 pf_map_addr(AF_INET6
, r
, saddr
, &s
->rt_addr
, NULL
,
3947 s
->rt_kif
= r
->rpool
.cur
->kif
;
3954 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
3959 /* list is sorted, if-bound states before floating */
3961 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
3963 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
3967 pf_detach_state(struct pf_state
*s
, int flags
)
3969 struct pf_state_key
*sk
= s
->state_key
;
3974 s
->state_key
= NULL
;
3975 TAILQ_REMOVE(&sk
->states
, s
, next
);
3976 if (--sk
->refcnt
== 0) {
3977 if (!(flags
& PF_DT_SKIP_EXTGWY
))
3978 RB_REMOVE(pf_state_tree_ext_gwy
,
3979 &pf_statetbl_ext_gwy
, sk
);
3980 if (!(flags
& PF_DT_SKIP_LANEXT
))
3981 RB_REMOVE(pf_state_tree_lan_ext
,
3982 &pf_statetbl_lan_ext
, sk
);
3984 pool_put(&pf_app_state_pl
, sk
->app_state
);
3985 pool_put(&pf_state_key_pl
, sk
);
3989 struct pf_state_key
*
3990 pf_alloc_state_key(struct pf_state
*s
, struct pf_state_key
*psk
)
3992 struct pf_state_key
*sk
;
3994 if ((sk
= pool_get(&pf_state_key_pl
, PR_WAITOK
)) == NULL
)
3996 bzero(sk
, sizeof (*sk
));
3997 TAILQ_INIT(&sk
->states
);
3998 pf_attach_state(sk
, s
, 0);
4000 /* initialize state key from psk, if provided */
4002 bcopy(&psk
->lan
, &sk
->lan
, sizeof (sk
->lan
));
4003 bcopy(&psk
->gwy
, &sk
->gwy
, sizeof (sk
->gwy
));
4004 bcopy(&psk
->ext
, &sk
->ext
, sizeof (sk
->ext
));
4006 sk
->proto
= psk
->proto
;
4007 sk
->direction
= psk
->direction
;
4008 sk
->proto_variant
= psk
->proto_variant
;
4009 VERIFY(psk
->app_state
== NULL
);
4010 sk
->flowsrc
= psk
->flowsrc
;
4011 sk
->flowhash
= psk
->flowhash
;
4012 /* don't touch tree entries, states and refcnt on sk */
4019 pf_tcp_iss(struct pf_pdesc
*pd
)
4022 u_int32_t digest
[4];
4024 if (pf_tcp_secret_init
== 0) {
4025 read_random(pf_tcp_secret
, sizeof (pf_tcp_secret
));
4026 MD5Init(&pf_tcp_secret_ctx
);
4027 MD5Update(&pf_tcp_secret_ctx
, pf_tcp_secret
,
4028 sizeof (pf_tcp_secret
));
4029 pf_tcp_secret_init
= 1;
4031 ctx
= pf_tcp_secret_ctx
;
4033 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_sport
, sizeof (u_short
));
4034 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_dport
, sizeof (u_short
));
4035 if (pd
->af
== AF_INET6
) {
4036 MD5Update(&ctx
, (char *)&pd
->src
->v6
, sizeof (struct in6_addr
));
4037 MD5Update(&ctx
, (char *)&pd
->dst
->v6
, sizeof (struct in6_addr
));
4039 MD5Update(&ctx
, (char *)&pd
->src
->v4
, sizeof (struct in_addr
));
4040 MD5Update(&ctx
, (char *)&pd
->dst
->v4
, sizeof (struct in_addr
));
4042 MD5Final((u_char
*)digest
, &ctx
);
4043 pf_tcp_iss_off
+= 4096;
4044 return (digest
[0] + random() + pf_tcp_iss_off
);
4048 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
4049 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
4050 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
4051 struct ifqueue
*ifq
)
4054 struct pf_rule
*nr
= NULL
;
4055 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4056 sa_family_t af
= pd
->af
;
4057 struct pf_rule
*r
, *a
= NULL
;
4058 struct pf_ruleset
*ruleset
= NULL
;
4059 struct pf_src_node
*nsn
= NULL
;
4060 struct tcphdr
*th
= pd
->hdr
.tcp
;
4062 int rewrite
= 0, hdrlen
= 0;
4064 unsigned int rtableid
= IFSCOPE_NONE
;
4068 u_int16_t mss
= tcp_mssdflt
;
4069 u_int8_t icmptype
= 0, icmpcode
= 0;
4071 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
4072 union pf_state_xport bxport
, nxport
, sxport
, dxport
;
4073 struct pf_state_key psk
;
4075 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
4077 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
4078 REASON_SET(&reason
, PFRES_CONGEST
);
4087 switch (pd
->proto
) {
4089 sxport
.port
= th
->th_sport
;
4090 dxport
.port
= th
->th_dport
;
4091 hdrlen
= sizeof (*th
);
4094 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4095 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4096 hdrlen
= sizeof (*pd
->hdr
.udp
);
4100 if (pd
->af
!= AF_INET
)
4102 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
4103 hdrlen
= ICMP_MINLEN
;
4104 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4105 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
4107 if (icmptype
== ICMP_UNREACH
||
4108 icmptype
== ICMP_SOURCEQUENCH
||
4109 icmptype
== ICMP_REDIRECT
||
4110 icmptype
== ICMP_TIMXCEED
||
4111 icmptype
== ICMP_PARAMPROB
)
4116 case IPPROTO_ICMPV6
:
4117 if (pd
->af
!= AF_INET6
)
4119 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
4120 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
4121 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4122 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
4124 if (icmptype
== ICMP6_DST_UNREACH
||
4125 icmptype
== ICMP6_PACKET_TOO_BIG
||
4126 icmptype
== ICMP6_TIME_EXCEEDED
||
4127 icmptype
== ICMP6_PARAM_PROB
)
4132 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
4133 sxport
.call_id
= dxport
.call_id
=
4134 pd
->hdr
.grev1
->call_id
;
4135 hdrlen
= sizeof (*pd
->hdr
.grev1
);
4140 dxport
.spi
= pd
->hdr
.esp
->spi
;
4141 hdrlen
= sizeof (*pd
->hdr
.esp
);
4145 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
4147 if (direction
== PF_OUT
) {
4148 bxport
= nxport
= sxport
;
4149 /* check outgoing packet for BINAT/NAT */
4150 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
4151 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4153 PF_ACPY(&pd
->baddr
, saddr
, af
);
4154 switch (pd
->proto
) {
4156 pf_change_ap(direction
, pd
->mp
, saddr
,
4157 &th
->th_sport
, pd
->ip_sum
, &th
->th_sum
,
4158 &pd
->naddr
, nxport
.port
, 0, af
);
4159 sxport
.port
= th
->th_sport
;
4163 pf_change_ap(direction
, pd
->mp
, saddr
,
4164 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4165 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4166 nxport
.port
, 1, af
);
4167 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4172 if (pd
->af
== AF_INET
) {
4173 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
4174 pd
->naddr
.v4
.s_addr
, 0);
4175 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4176 pd
->hdr
.icmp
->icmp_cksum
, sxport
.port
,
4178 pd
->hdr
.icmp
->icmp_id
= nxport
.port
;
4184 case IPPROTO_ICMPV6
:
4185 if (pd
->af
== AF_INET6
) {
4186 pf_change_a6(saddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4196 pf_change_a(&saddr
->v4
.s_addr
,
4197 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4202 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4213 pf_change_a(&saddr
->v4
.s_addr
,
4214 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4219 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4228 pf_change_a(&saddr
->v4
.s_addr
,
4229 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4234 PF_ACPY(saddr
, &pd
->naddr
, af
);
4246 bxport
.port
= nxport
.port
= dxport
.port
;
4247 /* check incoming packet for BINAT/RDR */
4248 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_IN
, kif
, &nsn
,
4249 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4251 PF_ACPY(&pd
->baddr
, daddr
, af
);
4252 switch (pd
->proto
) {
4254 pf_change_ap(direction
, pd
->mp
, daddr
,
4255 &th
->th_dport
, pd
->ip_sum
, &th
->th_sum
,
4256 &pd
->naddr
, nxport
.port
, 0, af
);
4257 dxport
.port
= th
->th_dport
;
4261 pf_change_ap(direction
, pd
->mp
, daddr
,
4262 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4263 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4264 nxport
.port
, 1, af
);
4265 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4270 if (pd
->af
== AF_INET
) {
4271 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
4272 pd
->naddr
.v4
.s_addr
, 0);
4277 case IPPROTO_ICMPV6
:
4278 if (pd
->af
== AF_INET6
) {
4279 pf_change_a6(daddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4286 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
4287 grev1
->call_id
= nxport
.call_id
;
4292 pf_change_a(&daddr
->v4
.s_addr
,
4293 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4298 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4308 pf_change_a(&daddr
->v4
.s_addr
,
4309 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4314 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4323 pf_change_a(&daddr
->v4
.s_addr
,
4324 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4329 PF_ACPY(daddr
, &pd
->naddr
, af
);
4342 if (nr
&& nr
->tag
> 0)
4347 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
4348 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
4349 else if (r
->direction
&& r
->direction
!= direction
)
4350 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
4351 else if (r
->af
&& r
->af
!= af
)
4352 r
= r
->skip
[PF_SKIP_AF
].ptr
;
4353 else if (r
->proto
&& r
->proto
!= pd
->proto
)
4354 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
4355 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
4357 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
4358 /* tcp/udp only. port_op always 0 in other cases */
4359 else if (r
->proto
== pd
->proto
&&
4360 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4361 r
->src
.xport
.range
.op
&&
4362 !pf_match_port(r
->src
.xport
.range
.op
,
4363 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
4365 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
4366 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
4368 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
4369 /* tcp/udp only. port_op always 0 in other cases */
4370 else if (r
->proto
== pd
->proto
&&
4371 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4372 r
->dst
.xport
.range
.op
&&
4373 !pf_match_port(r
->dst
.xport
.range
.op
,
4374 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
4376 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
4377 /* icmp only. type always 0 in other cases */
4378 else if (r
->type
&& r
->type
!= icmptype
+ 1)
4379 r
= TAILQ_NEXT(r
, entries
);
4380 /* icmp only. type always 0 in other cases */
4381 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
4382 r
= TAILQ_NEXT(r
, entries
);
4383 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
4384 !(r
->tos
& pd
->tos
))
4385 r
= TAILQ_NEXT(r
, entries
);
4386 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
4387 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
4388 r
= TAILQ_NEXT(r
, entries
);
4389 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
4390 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
4391 r
= TAILQ_NEXT(r
, entries
);
4392 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
4393 r
= TAILQ_NEXT(r
, entries
);
4394 else if (pd
->proto
== IPPROTO_TCP
&&
4395 (r
->flagset
& th
->th_flags
) != r
->flags
)
4396 r
= TAILQ_NEXT(r
, entries
);
4397 /* tcp/udp only. uid.op always 0 in other cases */
4398 else if (r
->uid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4399 pf_socket_lookup(direction
, pd
), 1)) &&
4400 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
4402 r
= TAILQ_NEXT(r
, entries
);
4403 /* tcp/udp only. gid.op always 0 in other cases */
4404 else if (r
->gid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4405 pf_socket_lookup(direction
, pd
), 1)) &&
4406 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
4408 r
= TAILQ_NEXT(r
, entries
);
4409 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
4410 r
= TAILQ_NEXT(r
, entries
);
4411 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
4412 r
= TAILQ_NEXT(r
, entries
);
4413 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
4414 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
4415 pf_osfp_fingerprint(pd
, m
, off
, th
),
4416 r
->os_fingerprint
)))
4417 r
= TAILQ_NEXT(r
, entries
);
4421 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
4422 rtableid
= r
->rtableid
;
4423 if (r
->anchor
== NULL
) {
4430 r
= TAILQ_NEXT(r
, entries
);
4432 pf_step_into_anchor(&asd
, &ruleset
,
4433 PF_RULESET_FILTER
, &r
, &a
, &match
);
4435 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
4436 PF_RULESET_FILTER
, &r
, &a
, &match
))
4443 REASON_SET(&reason
, PFRES_MATCH
);
4445 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
4447 if (rewrite
< off
+ hdrlen
)
4448 rewrite
= off
+ hdrlen
;
4450 m
= pf_lazy_makewritable(pd
, m
, rewrite
);
4452 REASON_SET(&reason
, PFRES_MEMORY
);
4456 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
4458 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
->log
? r
: nr
,
4462 if ((r
->action
== PF_DROP
) &&
4463 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4464 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
4465 (r
->rule_flag
& PFRULE_RETURN
))) {
4466 /* undo NAT changes, if they have taken place */
4468 if (direction
== PF_OUT
) {
4469 switch (pd
->proto
) {
4471 pf_change_ap(direction
, pd
->mp
, saddr
,
4472 &th
->th_sport
, pd
->ip_sum
,
4473 &th
->th_sum
, &pd
->baddr
,
4474 bxport
.port
, 0, af
);
4475 sxport
.port
= th
->th_sport
;
4479 pf_change_ap(direction
, pd
->mp
, saddr
,
4480 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4481 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4482 bxport
.port
, 1, af
);
4483 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4488 case IPPROTO_ICMPV6
:
4493 PF_ACPY(&pd
->baddr
, saddr
, af
);
4498 pf_change_a(&saddr
->v4
.s_addr
,
4500 pd
->baddr
.v4
.s_addr
, 0);
4505 PF_ACPY(saddr
, &pd
->baddr
,
4512 PF_ACPY(&pd
->baddr
, saddr
, af
);
4516 pf_change_a(&saddr
->v4
.s_addr
,
4518 pd
->baddr
.v4
.s_addr
, 0);
4523 PF_ACPY(saddr
, &pd
->baddr
,
4532 pf_change_a(&saddr
->v4
.s_addr
,
4534 pd
->baddr
.v4
.s_addr
, 0);
4537 PF_ACPY(saddr
, &pd
->baddr
, af
);
4542 switch (pd
->proto
) {
4544 pf_change_ap(direction
, pd
->mp
, daddr
,
4545 &th
->th_dport
, pd
->ip_sum
,
4546 &th
->th_sum
, &pd
->baddr
,
4547 bxport
.port
, 0, af
);
4548 dxport
.port
= th
->th_dport
;
4552 pf_change_ap(direction
, pd
->mp
, daddr
,
4553 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4554 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4555 bxport
.port
, 1, af
);
4556 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4561 case IPPROTO_ICMPV6
:
4566 if (pd
->proto_variant
==
4567 PF_GRE_PPTP_VARIANT
)
4568 grev1
->call_id
= bxport
.call_id
;
4573 pf_change_a(&daddr
->v4
.s_addr
,
4575 pd
->baddr
.v4
.s_addr
, 0);
4580 PF_ACPY(daddr
, &pd
->baddr
,
4590 pf_change_a(&daddr
->v4
.s_addr
,
4592 pd
->baddr
.v4
.s_addr
, 0);
4597 PF_ACPY(daddr
, &pd
->baddr
,
4606 pf_change_a(&daddr
->v4
.s_addr
,
4608 pd
->baddr
.v4
.s_addr
, 0);
4612 PF_ACPY(daddr
, &pd
->baddr
, af
);
4619 if (pd
->proto
== IPPROTO_TCP
&&
4620 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4621 (r
->rule_flag
& PFRULE_RETURN
)) &&
4622 !(th
->th_flags
& TH_RST
)) {
4623 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
4632 h4
= mtod(m
, struct ip
*);
4633 len
= ntohs(h4
->ip_len
) - off
;
4637 h6
= mtod(m
, struct ip6_hdr
*);
4638 len
= ntohs(h6
->ip6_plen
) -
4639 (off
- sizeof (*h6
));
4644 if (pf_check_proto_cksum(m
, off
, len
, IPPROTO_TCP
, af
))
4645 REASON_SET(&reason
, PFRES_PROTCKSUM
);
4647 if (th
->th_flags
& TH_SYN
)
4649 if (th
->th_flags
& TH_FIN
)
4651 pf_send_tcp(r
, af
, pd
->dst
,
4652 pd
->src
, th
->th_dport
, th
->th_sport
,
4653 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
4654 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
4656 } else if (pd
->proto
!= IPPROTO_ICMP
&& af
== AF_INET
&&
4657 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4659 pf_send_icmp(m
, r
->return_icmp
>> 8,
4660 r
->return_icmp
& 255, af
, r
);
4661 else if (pd
->proto
!= IPPROTO_ICMPV6
&& af
== AF_INET6
&&
4662 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4664 pf_send_icmp(m
, r
->return_icmp6
>> 8,
4665 r
->return_icmp6
& 255, af
, r
);
4668 if (r
->action
== PF_DROP
)
4671 /* prepare state key, for flowhash and/or the state (if created) */
4672 bzero(&psk
, sizeof (psk
));
4673 psk
.proto
= pd
->proto
;
4674 psk
.direction
= direction
;
4676 if (pd
->proto
== IPPROTO_UDP
) {
4677 if (ntohs(pd
->hdr
.udp
->uh_sport
) == PF_IKE_PORT
&&
4678 ntohs(pd
->hdr
.udp
->uh_dport
) == PF_IKE_PORT
) {
4679 psk
.proto_variant
= PF_EXTFILTER_APD
;
4681 psk
.proto_variant
= nr
? nr
->extfilter
: r
->extfilter
;
4682 if (psk
.proto_variant
< PF_EXTFILTER_APD
)
4683 psk
.proto_variant
= PF_EXTFILTER_APD
;
4685 } else if (pd
->proto
== IPPROTO_GRE
) {
4686 psk
.proto_variant
= pd
->proto_variant
;
4688 if (direction
== PF_OUT
) {
4689 PF_ACPY(&psk
.gwy
.addr
, saddr
, af
);
4690 PF_ACPY(&psk
.ext
.addr
, daddr
, af
);
4691 switch (pd
->proto
) {
4693 psk
.gwy
.xport
= sxport
;
4694 psk
.ext
.xport
= dxport
;
4697 psk
.gwy
.xport
.spi
= 0;
4698 psk
.ext
.xport
.spi
= pd
->hdr
.esp
->spi
;
4702 case IPPROTO_ICMPV6
:
4704 psk
.gwy
.xport
.port
= nxport
.port
;
4705 psk
.ext
.xport
.spi
= 0;
4708 psk
.gwy
.xport
= sxport
;
4709 psk
.ext
.xport
= dxport
;
4713 PF_ACPY(&psk
.lan
.addr
, &pd
->baddr
, af
);
4714 psk
.lan
.xport
= bxport
;
4716 PF_ACPY(&psk
.lan
.addr
, &psk
.gwy
.addr
, af
);
4717 psk
.lan
.xport
= psk
.gwy
.xport
;
4720 PF_ACPY(&psk
.lan
.addr
, daddr
, af
);
4721 PF_ACPY(&psk
.ext
.addr
, saddr
, af
);
4722 switch (pd
->proto
) {
4725 case IPPROTO_ICMPV6
:
4727 psk
.lan
.xport
= nxport
;
4728 psk
.ext
.xport
.spi
= 0;
4731 psk
.ext
.xport
.spi
= 0;
4732 psk
.lan
.xport
.spi
= pd
->hdr
.esp
->spi
;
4735 psk
.lan
.xport
= dxport
;
4736 psk
.ext
.xport
= sxport
;
4740 PF_ACPY(&psk
.gwy
.addr
, &pd
->baddr
, af
);
4741 psk
.gwy
.xport
= bxport
;
4743 PF_ACPY(&psk
.gwy
.addr
, &psk
.lan
.addr
, af
);
4744 psk
.gwy
.xport
= psk
.lan
.xport
;
4747 if (pd
->pktflags
& PKTF_FLOW_ID
) {
4748 /* flow hash was already computed outside of PF */
4749 psk
.flowsrc
= pd
->flowsrc
;
4750 psk
.flowhash
= pd
->flowhash
;
4752 /* compute flow hash and store it in state key */
4753 psk
.flowsrc
= FLOWSRC_PF
;
4754 psk
.flowhash
= pf_calc_state_key_flowhash(&psk
);
4755 pd
->flowsrc
= psk
.flowsrc
;
4756 pd
->flowhash
= psk
.flowhash
;
4757 pd
->pktflags
|= PKTF_FLOW_ID
;
4758 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
4761 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
4762 REASON_SET(&reason
, PFRES_MEMORY
);
4766 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
4767 (pd
->flags
& PFDESC_TCP_NORM
))) {
4768 /* create new state */
4769 struct pf_state
*s
= NULL
;
4770 struct pf_state_key
*sk
= NULL
;
4771 struct pf_src_node
*sn
= NULL
;
4772 struct pf_ike_hdr ike
;
4774 if (pd
->proto
== IPPROTO_UDP
) {
4775 struct udphdr
*uh
= pd
->hdr
.udp
;
4776 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
4778 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
4779 ntohs(uh
->uh_dport
) == PF_IKE_PORT
&&
4780 plen
>= PF_IKE_PACKET_MINSIZE
) {
4781 if (plen
> PF_IKE_PACKET_MINSIZE
)
4782 plen
= PF_IKE_PACKET_MINSIZE
;
4783 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
4787 if (nr
!= NULL
&& pd
->proto
== IPPROTO_ESP
&&
4788 direction
== PF_OUT
) {
4789 struct pf_state_key_cmp sk0
;
4790 struct pf_state
*s0
;
4794 * This squelches state creation if the external
4795 * address matches an existing incomplete state with a
4796 * different internal address. Only one 'blocking'
4797 * partial state is allowed for each external address.
4799 memset(&sk0
, 0, sizeof (sk0
));
4801 sk0
.proto
= IPPROTO_ESP
;
4802 PF_ACPY(&sk0
.gwy
.addr
, saddr
, sk0
.af
);
4803 PF_ACPY(&sk0
.ext
.addr
, daddr
, sk0
.af
);
4804 s0
= pf_find_state(kif
, &sk0
, PF_IN
);
4806 if (s0
&& PF_ANEQ(&s0
->state_key
->lan
.addr
,
4813 /* check maximums */
4814 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
4815 pf_status
.lcounters
[LCNT_STATES
]++;
4816 REASON_SET(&reason
, PFRES_MAXSTATES
);
4819 /* src node for filter rule */
4820 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
4821 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4822 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
4823 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4826 /* src node for translation rule */
4827 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4828 ((direction
== PF_OUT
&&
4829 nr
->action
!= PF_RDR
&&
4830 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
4831 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
4832 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4835 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
4837 REASON_SET(&reason
, PFRES_MEMORY
);
4839 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
4840 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
4841 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4842 pf_status
.src_nodes
--;
4843 pool_put(&pf_src_tree_pl
, sn
);
4845 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
4847 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
4848 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4849 pf_status
.src_nodes
--;
4850 pool_put(&pf_src_tree_pl
, nsn
);
4854 pool_put(&pf_app_state_pl
,
4856 pool_put(&pf_state_key_pl
, sk
);
4860 bzero(s
, sizeof (*s
));
4861 TAILQ_INIT(&s
->unlink_hooks
);
4863 s
->nat_rule
.ptr
= nr
;
4865 STATE_INC_COUNTERS(s
);
4866 s
->allow_opts
= r
->allow_opts
;
4867 s
->log
= r
->log
& PF_LOG_ALL
;
4869 s
->log
|= nr
->log
& PF_LOG_ALL
;
4870 switch (pd
->proto
) {
4872 s
->src
.seqlo
= ntohl(th
->th_seq
);
4873 s
->src
.seqhi
= s
->src
.seqlo
+ pd
->p_len
+ 1;
4874 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
4875 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
4876 /* Generate sequence number modulator */
4877 if ((s
->src
.seqdiff
= pf_tcp_iss(pd
) -
4880 pf_change_a(&th
->th_seq
, &th
->th_sum
,
4881 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
4882 rewrite
= off
+ sizeof (*th
);
4885 if (th
->th_flags
& TH_SYN
) {
4887 s
->src
.wscale
= pf_get_wscale(m
, off
,
4890 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
4891 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
4892 /* Remove scale factor from initial window */
4893 int win
= s
->src
.max_win
;
4894 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
4895 s
->src
.max_win
= (win
- 1) >>
4896 (s
->src
.wscale
& PF_WSCALE_MASK
);
4898 if (th
->th_flags
& TH_FIN
)
4902 s
->src
.state
= TCPS_SYN_SENT
;
4903 s
->dst
.state
= TCPS_CLOSED
;
4904 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
4907 s
->src
.state
= PFUDPS_SINGLE
;
4908 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
4909 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
4913 case IPPROTO_ICMPV6
:
4915 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
4918 s
->src
.state
= PFGRE1S_INITIATING
;
4919 s
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
4920 s
->timeout
= PFTM_GREv1_INITIATING
;
4923 s
->src
.state
= PFESPS_INITIATING
;
4924 s
->dst
.state
= PFESPS_NO_TRAFFIC
;
4925 s
->timeout
= PFTM_ESP_FIRST_PACKET
;
4928 s
->src
.state
= PFOTHERS_SINGLE
;
4929 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
4930 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
4933 s
->creation
= pf_time_second();
4934 s
->expire
= pf_time_second();
4938 s
->src_node
->states
++;
4939 VERIFY(s
->src_node
->states
!= 0);
4942 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
4943 s
->nat_src_node
= nsn
;
4944 s
->nat_src_node
->states
++;
4945 VERIFY(s
->nat_src_node
->states
!= 0);
4947 if (pd
->proto
== IPPROTO_TCP
) {
4948 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
4949 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
4951 REASON_SET(&reason
, PFRES_MEMORY
);
4952 pf_src_tree_remove_state(s
);
4953 STATE_DEC_COUNTERS(s
);
4954 pool_put(&pf_state_pl
, s
);
4957 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
4958 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
4959 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
4960 /* This really shouldn't happen!!! */
4961 DPFPRINTF(PF_DEBUG_URGENT
,
4962 ("pf_normalize_tcp_stateful failed on "
4964 pf_normalize_tcp_cleanup(s
);
4965 pf_src_tree_remove_state(s
);
4966 STATE_DEC_COUNTERS(s
);
4967 pool_put(&pf_state_pl
, s
);
4972 /* allocate state key and import values from psk */
4973 if ((sk
= pf_alloc_state_key(s
, &psk
)) == NULL
) {
4974 REASON_SET(&reason
, PFRES_MEMORY
);
4978 pf_set_rt_ifp(s
, saddr
); /* needs s->state_key set */
4982 if (sk
->app_state
== 0) {
4983 switch (pd
->proto
) {
4985 u_int16_t dport
= (direction
== PF_OUT
) ?
4986 sk
->ext
.xport
.port
: sk
->gwy
.xport
.port
;
4989 ntohs(dport
) == PF_PPTP_PORT
) {
4990 struct pf_app_state
*as
;
4992 as
= pool_get(&pf_app_state_pl
,
5000 bzero(as
, sizeof (*as
));
5001 as
->handler
= pf_pptp_handler
;
5002 as
->compare_lan_ext
= 0;
5003 as
->compare_ext_gwy
= 0;
5004 as
->u
.pptp
.grev1_state
= 0;
5006 (void) hook_establish(&s
->unlink_hooks
,
5007 0, (hook_fn_t
) pf_pptp_unlink
, s
);
5013 struct udphdr
*uh
= pd
->hdr
.udp
;
5016 ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
5017 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
5018 struct pf_app_state
*as
;
5020 as
= pool_get(&pf_app_state_pl
,
5028 bzero(as
, sizeof (*as
));
5029 as
->compare_lan_ext
= pf_ike_compare
;
5030 as
->compare_ext_gwy
= pf_ike_compare
;
5031 as
->u
.ike
.cookie
= ike
.initiator_cookie
;
5042 if (pf_insert_state(BOUND_IFACE(r
, kif
), s
)) {
5043 if (pd
->proto
== IPPROTO_TCP
)
5044 pf_normalize_tcp_cleanup(s
);
5045 REASON_SET(&reason
, PFRES_STATEINS
);
5046 pf_src_tree_remove_state(s
);
5047 STATE_DEC_COUNTERS(s
);
5048 pool_put(&pf_state_pl
, s
);
5056 if (pd
->proto
== IPPROTO_TCP
&&
5057 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
5058 r
->keep_state
== PF_STATE_SYNPROXY
) {
5059 s
->src
.state
= PF_TCPS_PROXY_SRC
;
5061 if (direction
== PF_OUT
) {
5062 pf_change_ap(direction
, pd
->mp
, saddr
,
5063 &th
->th_sport
, pd
->ip_sum
,
5064 &th
->th_sum
, &pd
->baddr
,
5065 bxport
.port
, 0, af
);
5066 sxport
.port
= th
->th_sport
;
5068 pf_change_ap(direction
, pd
->mp
, daddr
,
5069 &th
->th_dport
, pd
->ip_sum
,
5070 &th
->th_sum
, &pd
->baddr
,
5071 bxport
.port
, 0, af
);
5072 sxport
.port
= th
->th_dport
;
5075 s
->src
.seqhi
= htonl(random());
5076 /* Find mss option */
5077 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
5078 mss
= pf_calc_mss(saddr
, af
, mss
);
5079 mss
= pf_calc_mss(daddr
, af
, mss
);
5081 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
5082 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5083 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
5084 REASON_SET(&reason
, PFRES_SYNPROXY
);
5085 return (PF_SYNPROXY_DROP
);
5088 if (sk
->app_state
&& sk
->app_state
->handler
) {
5091 switch (pd
->proto
) {
5093 offx
+= th
->th_off
<< 2;
5096 offx
+= pd
->hdr
.udp
->uh_ulen
<< 2;
5099 /* ALG handlers only apply to TCP and UDP rules */
5104 sk
->app_state
->handler(s
, direction
, offx
,
5107 REASON_SET(&reason
, PFRES_MEMORY
);
5115 /* copy back packet headers if we performed NAT operations */
5117 if (rewrite
< off
+ hdrlen
)
5118 rewrite
= off
+ hdrlen
;
5120 m
= pf_lazy_makewritable(pd
, pd
->mp
, rewrite
);
5122 REASON_SET(&reason
, PFRES_MEMORY
);
5126 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5134 * When pf_test_dummynet() returns PF_PASS, the rule matching parameter "rm"
5135 * remains unchanged, meaning the packet did not match a dummynet rule.
5136 * when the packet does match a dummynet rule, pf_test_dummynet() returns
5137 * PF_PASS and zero out the mbuf rule as the packet is effectively siphoned
5141 pf_test_dummynet(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5142 struct mbuf
**m0
, struct pf_pdesc
*pd
, struct ip_fw_args
*fwa
)
5144 struct mbuf
*m
= *m0
;
5145 struct pf_rule
*am
= NULL
;
5146 struct pf_ruleset
*rsm
= NULL
;
5147 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
5148 sa_family_t af
= pd
->af
;
5149 struct pf_rule
*r
, *a
= NULL
;
5150 struct pf_ruleset
*ruleset
= NULL
;
5151 struct tcphdr
*th
= pd
->hdr
.tcp
;
5155 unsigned int rtableid
= IFSCOPE_NONE
;
5158 u_int8_t icmptype
= 0, icmpcode
= 0;
5159 struct ip_fw_args dnflow
;
5160 struct pf_rule
*prev_matching_rule
= fwa
? fwa
->fwa_pf_rule
: NULL
;
5161 int found_prev_rule
= (prev_matching_rule
) ? 0 : 1;
5163 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
5165 if (!DUMMYNET_LOADED
)
5168 if (TAILQ_EMPTY(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
))
5171 bzero(&dnflow
, sizeof(dnflow
));
5175 /* Fragments don't gave protocol headers */
5176 if (!(pd
->flags
& PFDESC_IP_FRAG
))
5177 switch (pd
->proto
) {
5179 dnflow
.fwa_id
.flags
= pd
->hdr
.tcp
->th_flags
;
5180 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.tcp
->th_dport
);
5181 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.tcp
->th_sport
);
5182 hdrlen
= sizeof (*th
);
5185 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.udp
->uh_dport
);
5186 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.udp
->uh_sport
);
5187 hdrlen
= sizeof (*pd
->hdr
.udp
);
5193 hdrlen
= ICMP_MINLEN
;
5194 icmptype
= pd
->hdr
.icmp
->icmp_type
;
5195 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
5199 case IPPROTO_ICMPV6
:
5202 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
5203 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
5204 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
5208 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
5209 hdrlen
= sizeof (*pd
->hdr
.grev1
);
5212 hdrlen
= sizeof (*pd
->hdr
.esp
);
5216 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
);
5220 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5221 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5222 else if (r
->direction
&& r
->direction
!= direction
)
5223 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5224 else if (r
->af
&& r
->af
!= af
)
5225 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5226 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5227 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5228 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
5230 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5231 /* tcp/udp only. port_op always 0 in other cases */
5232 else if (r
->proto
== pd
->proto
&&
5233 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5234 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5235 ((r
->src
.xport
.range
.op
&&
5236 !pf_match_port(r
->src
.xport
.range
.op
,
5237 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
5239 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
5240 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
5242 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5243 /* tcp/udp only. port_op always 0 in other cases */
5244 else if (r
->proto
== pd
->proto
&&
5245 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5246 r
->dst
.xport
.range
.op
&&
5247 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5248 !pf_match_port(r
->dst
.xport
.range
.op
,
5249 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
5251 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
5252 /* icmp only. type always 0 in other cases */
5254 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5255 r
->type
!= icmptype
+ 1))
5256 r
= TAILQ_NEXT(r
, entries
);
5257 /* icmp only. type always 0 in other cases */
5259 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5260 r
->code
!= icmpcode
+ 1))
5261 r
= TAILQ_NEXT(r
, entries
);
5262 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5263 r
= TAILQ_NEXT(r
, entries
);
5264 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
5265 r
= TAILQ_NEXT(r
, entries
);
5266 else if (pd
->proto
== IPPROTO_TCP
&&
5267 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5268 (r
->flagset
& th
->th_flags
) != r
->flags
))
5269 r
= TAILQ_NEXT(r
, entries
);
5270 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
5271 r
= TAILQ_NEXT(r
, entries
);
5272 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5273 r
= TAILQ_NEXT(r
, entries
);
5276 * Need to go past the previous dummynet matching rule
5278 if (r
->anchor
== NULL
) {
5279 if (found_prev_rule
) {
5282 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
5283 rtableid
= r
->rtableid
;
5290 } else if (r
== prev_matching_rule
) {
5291 found_prev_rule
= 1;
5293 r
= TAILQ_NEXT(r
, entries
);
5295 pf_step_into_anchor(&asd
, &ruleset
,
5296 PF_RULESET_DUMMYNET
, &r
, &a
, &match
);
5299 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5300 PF_RULESET_DUMMYNET
, &r
, &a
, &match
))
5310 REASON_SET(&reason
, PFRES_DUMMYNET
);
5313 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
,
5317 if (r
->action
== PF_NODUMMYNET
) {
5318 int dirndx
= (direction
== PF_OUT
);
5320 r
->packets
[dirndx
]++;
5321 r
->bytes
[dirndx
] += pd
->tot_len
;
5325 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5326 REASON_SET(&reason
, PFRES_MEMORY
);
5331 if (r
->dnpipe
&& ip_dn_io_ptr
!= NULL
) {
5332 int dirndx
= (direction
== PF_OUT
);
5334 r
->packets
[dirndx
]++;
5335 r
->bytes
[dirndx
] += pd
->tot_len
;
5337 dnflow
.fwa_cookie
= r
->dnpipe
;
5338 dnflow
.fwa_pf_rule
= r
;
5339 dnflow
.fwa_id
.proto
= pd
->proto
;
5340 dnflow
.fwa_flags
= r
->dntype
;
5343 dnflow
.fwa_id
.addr_type
= 4;
5344 dnflow
.fwa_id
.src_ip
= ntohl(saddr
->v4
.s_addr
);
5345 dnflow
.fwa_id
.dst_ip
= ntohl(daddr
->v4
.s_addr
);
5348 dnflow
.fwa_id
.addr_type
= 6;
5349 dnflow
.fwa_id
.src_ip6
= saddr
->v6
;
5350 dnflow
.fwa_id
.dst_ip6
= saddr
->v6
;
5355 dnflow
.fwa_oif
= fwa
->fwa_oif
;
5356 dnflow
.fwa_oflags
= fwa
->fwa_oflags
;
5358 * Note that fwa_ro, fwa_dst and fwa_ipoa are
5359 * actually in a union so the following does work
5360 * for both IPv4 and IPv6
5362 dnflow
.fwa_ro
= fwa
->fwa_ro
;
5363 dnflow
.fwa_dst
= fwa
->fwa_dst
;
5364 dnflow
.fwa_ipoa
= fwa
->fwa_ipoa
;
5365 dnflow
.fwa_ro6_pmtu
= fwa
->fwa_ro6_pmtu
;
5366 dnflow
.fwa_origifp
= fwa
->fwa_origifp
;
5367 dnflow
.fwa_mtu
= fwa
->fwa_mtu
;
5368 dnflow
.fwa_alwaysfrag
= fwa
->fwa_alwaysfrag
;
5369 dnflow
.fwa_unfragpartlen
= fwa
->fwa_unfragpartlen
;
5370 dnflow
.fwa_exthdrs
= fwa
->fwa_exthdrs
;
5373 if (af
== AF_INET
) {
5374 struct ip
*iphdr
= mtod(m
, struct ip
*);
5375 NTOHS(iphdr
->ip_len
);
5376 NTOHS(iphdr
->ip_off
);
5379 * Don't need to unlock pf_lock as NET_THREAD_HELD_PF
5380 * allows for recursive behavior
5385 direction
== PF_IN
? DN_TO_IP_IN
: DN_TO_IP_OUT
:
5386 direction
== PF_IN
? DN_TO_IP6_IN
: DN_TO_IP6_OUT
,
5387 &dnflow
, DN_CLIENT_PF
);
5390 * The packet is siphoned out by dummynet so return a NULL
5391 * mbuf so the caller can still return success.
5400 #endif /* DUMMYNET */
5403 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5404 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
5405 struct pf_ruleset
**rsm
)
5408 struct pf_rule
*r
, *a
= NULL
;
5409 struct pf_ruleset
*ruleset
= NULL
;
5410 sa_family_t af
= pd
->af
;
5416 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
5419 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5420 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5421 else if (r
->direction
&& r
->direction
!= direction
)
5422 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5423 else if (r
->af
&& r
->af
!= af
)
5424 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5425 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5426 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5427 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
5429 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5430 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
5432 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5433 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
5434 !(r
->tos
& pd
->tos
))
5435 r
= TAILQ_NEXT(r
, entries
);
5436 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
5437 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
5438 r
= TAILQ_NEXT(r
, entries
);
5439 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
5440 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
5441 r
= TAILQ_NEXT(r
, entries
);
5442 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
5443 r
= TAILQ_NEXT(r
, entries
);
5444 else if (pd
->proto
== IPPROTO_UDP
&&
5445 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
5446 r
= TAILQ_NEXT(r
, entries
);
5447 else if (pd
->proto
== IPPROTO_TCP
&&
5448 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
5450 r
= TAILQ_NEXT(r
, entries
);
5451 else if ((pd
->proto
== IPPROTO_ICMP
||
5452 pd
->proto
== IPPROTO_ICMPV6
) &&
5453 (r
->type
|| r
->code
))
5454 r
= TAILQ_NEXT(r
, entries
);
5455 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
5456 r
= TAILQ_NEXT(r
, entries
);
5457 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5458 r
= TAILQ_NEXT(r
, entries
);
5460 if (r
->anchor
== NULL
) {
5467 r
= TAILQ_NEXT(r
, entries
);
5469 pf_step_into_anchor(&asd
, &ruleset
,
5470 PF_RULESET_FILTER
, &r
, &a
, &match
);
5472 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5473 PF_RULESET_FILTER
, &r
, &a
, &match
))
5480 REASON_SET(&reason
, PFRES_MATCH
);
5483 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
5486 if (r
->action
!= PF_PASS
)
5489 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1, NULL
)) {
5490 REASON_SET(&reason
, PFRES_MEMORY
);
5498 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
5499 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
5501 #pragma unused(direction)
5503 struct pf_pptp_state
*pptps
;
5504 struct pf_pptp_ctrl_msg cm
;
5506 struct pf_state
*gs
;
5508 u_int16_t
*pac_call_id
;
5509 u_int16_t
*pns_call_id
;
5510 u_int16_t
*spoof_call_id
;
5511 u_int8_t
*pac_state
;
5512 u_int8_t
*pns_state
;
5513 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
5515 struct pf_state_key
*sk
;
5516 struct pf_state_key
*gsk
;
5517 struct pf_app_state
*gas
;
5520 pptps
= &sk
->app_state
->u
.pptp
;
5521 gs
= pptps
->grev1_state
;
5524 gs
->expire
= pf_time_second();
5527 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
5528 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
5531 m_copydata(m
, off
, plen
, &cm
);
5533 if (ntohl(cm
.hdr
.magic
) != PF_PPTP_MAGIC_NUMBER
)
5535 if (ntohs(cm
.hdr
.type
) != 1)
5539 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
5543 memcpy(gs
, s
, sizeof (*gs
));
5545 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
5546 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
5548 TAILQ_INIT(&gs
->unlink_hooks
);
5551 gs
->pfsync_time
= 0;
5552 gs
->packets
[0] = gs
->packets
[1] = 0;
5553 gs
->bytes
[0] = gs
->bytes
[1] = 0;
5554 gs
->timeout
= PFTM_UNLINKED
;
5555 gs
->id
= gs
->creatorid
= 0;
5556 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5557 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
5559 gas
= pool_get(&pf_app_state_pl
, PR_NOWAIT
);
5561 pool_put(&pf_state_pl
, gs
);
5565 gsk
= pf_alloc_state_key(gs
, NULL
);
5567 pool_put(&pf_app_state_pl
, gas
);
5568 pool_put(&pf_state_pl
, gs
);
5572 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
5573 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
5574 memcpy(&gsk
->ext
, &sk
->ext
, sizeof (gsk
->ext
));
5576 gsk
->proto
= IPPROTO_GRE
;
5577 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
5578 gsk
->app_state
= gas
;
5579 gsk
->lan
.xport
.call_id
= 0;
5580 gsk
->gwy
.xport
.call_id
= 0;
5581 gsk
->ext
.xport
.call_id
= 0;
5582 gsk
->flowsrc
= FLOWSRC_PF
;
5583 gsk
->flowhash
= pf_calc_state_key_flowhash(gsk
);
5584 memset(gas
, 0, sizeof (*gas
));
5585 gas
->u
.grev1
.pptp_state
= s
;
5586 STATE_INC_COUNTERS(gs
);
5587 pptps
->grev1_state
= gs
;
5588 (void) hook_establish(&gs
->unlink_hooks
, 0,
5589 (hook_fn_t
) pf_grev1_unlink
, gs
);
5591 gsk
= gs
->state_key
;
5594 switch (sk
->direction
) {
5596 pns_call_id
= &gsk
->ext
.xport
.call_id
;
5597 pns_state
= &gs
->dst
.state
;
5598 pac_call_id
= &gsk
->lan
.xport
.call_id
;
5599 pac_state
= &gs
->src
.state
;
5603 pns_call_id
= &gsk
->lan
.xport
.call_id
;
5604 pns_state
= &gs
->src
.state
;
5605 pac_call_id
= &gsk
->ext
.xport
.call_id
;
5606 pac_state
= &gs
->dst
.state
;
5610 DPFPRINTF(PF_DEBUG_URGENT
,
5611 ("pf_pptp_handler: bad directional!\n"));
5618 ct
= ntohs(cm
.ctrl
.type
);
5621 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
5622 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
5623 *pns_state
= PFGRE1S_INITIATING
;
5624 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5625 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
5628 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
5629 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
5630 if (s
->nat_rule
.ptr
)
5632 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5633 &cm
.msg
.call_out_rpy
.call_id
:
5634 &cm
.msg
.call_out_rpy
.peer_call_id
;
5635 if (gs
->timeout
== PFTM_UNLINKED
) {
5636 *pac_state
= PFGRE1S_INITIATING
;
5637 op
= PF_PPTP_INSERT_GRE
;
5641 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
5642 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
5643 *pns_state
= PFGRE1S_INITIATING
;
5644 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5645 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
5648 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
5649 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
5650 *pac_state
= PFGRE1S_INITIATING
;
5651 if (s
->nat_rule
.ptr
)
5653 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5654 &cm
.msg
.call_in_2nd
.call_id
:
5655 &cm
.msg
.call_in_2nd
.peer_call_id
;
5658 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
5659 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5660 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
5661 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
5664 if (gs
->timeout
== PFTM_UNLINKED
)
5665 op
= PF_PPTP_INSERT_GRE
;
5668 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
5669 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
5670 op
= PF_PPTP_REMOVE_GRE
;
5673 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
5674 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
5675 op
= PF_PPTP_REMOVE_GRE
;
5678 case PF_PPTP_CTRL_TYPE_ERROR
:
5679 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5680 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
5683 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
5684 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
5685 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
5693 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
5694 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
5695 if (spoof_call_id
) {
5696 u_int16_t call_id
= 0;
5698 struct pf_state_key_cmp key
;
5701 key
.proto
= IPPROTO_GRE
;
5702 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
5703 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af
);
5704 PF_ACPY(&key
.ext
.addr
, &gsk
->ext
.addr
, key
.af
);
5705 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
5706 key
.ext
.xport
.call_id
= gsk
->ext
.xport
.call_id
;
5708 call_id
= htonl(random());
5711 while (pf_find_state_all(&key
, PF_IN
, 0)) {
5712 call_id
= ntohs(call_id
);
5714 if (--call_id
== 0) call_id
= 0xffff;
5715 call_id
= htons(call_id
);
5717 key
.gwy
.xport
.call_id
= call_id
;
5720 DPFPRINTF(PF_DEBUG_URGENT
,
5721 ("pf_pptp_handler: failed to spoof "
5723 key
.gwy
.xport
.call_id
= 0;
5728 gsk
->gwy
.xport
.call_id
= call_id
;
5734 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
5735 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
5736 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
5737 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5738 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
5740 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
5741 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5742 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
5745 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
5747 pptps
->grev1_state
= NULL
;
5748 STATE_DEC_COUNTERS(gs
);
5749 pool_put(&pf_state_pl
, gs
);
5752 m_copyback(m
, off
, plen
, &cm
);
5756 case PF_PPTP_REMOVE_GRE
:
5757 gs
->timeout
= PFTM_PURGE
;
5758 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5759 gsk
->lan
.xport
.call_id
= 0;
5760 gsk
->gwy
.xport
.call_id
= 0;
5761 gsk
->ext
.xport
.call_id
= 0;
5762 gs
->id
= gs
->creatorid
= 0;
5765 case PF_PPTP_INSERT_GRE
:
5766 gs
->creation
= pf_time_second();
5767 gs
->expire
= pf_time_second();
5768 gs
->timeout
= PFTM_TCP_ESTABLISHED
;
5769 if (gs
->src_node
!= NULL
) {
5770 ++gs
->src_node
->states
;
5771 VERIFY(gs
->src_node
->states
!= 0);
5773 if (gs
->nat_src_node
!= NULL
) {
5774 ++gs
->nat_src_node
->states
;
5775 VERIFY(gs
->nat_src_node
->states
!= 0);
5777 pf_set_rt_ifp(gs
, &sk
->lan
.addr
);
5778 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
5782 * FIX ME: insertion can fail when multiple PNS
5783 * behind the same NAT open calls to the same PAC
5784 * simultaneously because spoofed call ID numbers
5785 * are chosen before states are inserted. This is
5786 * hard to fix and happens infrequently enough that
5787 * users will normally try again and this ALG will
5788 * succeed. Failures are expected to be rare enough
5789 * that fixing this is a low priority.
5791 pptps
->grev1_state
= NULL
;
5792 pd
->lmw
= -1; /* Force PF_DROP on PFRES_MEMORY */
5793 pf_src_tree_remove_state(gs
);
5794 STATE_DEC_COUNTERS(gs
);
5795 pool_put(&pf_state_pl
, gs
);
5796 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
5797 "inserting GREv1 state.\n"));
5807 pf_pptp_unlink(struct pf_state
*s
)
5809 struct pf_app_state
*as
= s
->state_key
->app_state
;
5810 struct pf_state
*grev1s
= as
->u
.pptp
.grev1_state
;
5813 struct pf_app_state
*gas
= grev1s
->state_key
->app_state
;
5815 if (grev1s
->timeout
< PFTM_MAX
)
5816 grev1s
->timeout
= PFTM_PURGE
;
5817 gas
->u
.grev1
.pptp_state
= NULL
;
5818 as
->u
.pptp
.grev1_state
= NULL
;
5823 pf_grev1_unlink(struct pf_state
*s
)
5825 struct pf_app_state
*as
= s
->state_key
->app_state
;
5826 struct pf_state
*pptps
= as
->u
.grev1
.pptp_state
;
5829 struct pf_app_state
*pas
= pptps
->state_key
->app_state
;
5831 pas
->u
.pptp
.grev1_state
= NULL
;
5832 as
->u
.grev1
.pptp_state
= NULL
;
5837 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
5839 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
5840 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
5844 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
5845 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
5849 struct pf_state_key_cmp key
;
5850 struct tcphdr
*th
= pd
->hdr
.tcp
;
5851 u_int16_t win
= ntohs(th
->th_win
);
5852 u_int32_t ack
, end
, seq
, orig_seq
;
5856 struct pf_state_peer
*src
, *dst
;
5860 key
.proto
= IPPROTO_TCP
;
5861 if (direction
== PF_IN
) {
5862 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
5863 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
5864 key
.ext
.xport
.port
= th
->th_sport
;
5865 key
.gwy
.xport
.port
= th
->th_dport
;
5867 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
5868 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
5869 key
.lan
.xport
.port
= th
->th_sport
;
5870 key
.ext
.xport
.port
= th
->th_dport
;
5875 if (direction
== (*state
)->state_key
->direction
) {
5876 src
= &(*state
)->src
;
5877 dst
= &(*state
)->dst
;
5879 src
= &(*state
)->dst
;
5880 dst
= &(*state
)->src
;
5883 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
5884 if (direction
!= (*state
)->state_key
->direction
) {
5885 REASON_SET(reason
, PFRES_SYNPROXY
);
5886 return (PF_SYNPROXY_DROP
);
5888 if (th
->th_flags
& TH_SYN
) {
5889 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
5890 REASON_SET(reason
, PFRES_SYNPROXY
);
5893 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5894 pd
->src
, th
->th_dport
, th
->th_sport
,
5895 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5896 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
5898 REASON_SET(reason
, PFRES_SYNPROXY
);
5899 return (PF_SYNPROXY_DROP
);
5900 } else if (!(th
->th_flags
& TH_ACK
) ||
5901 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5902 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5903 REASON_SET(reason
, PFRES_SYNPROXY
);
5905 } else if ((*state
)->src_node
!= NULL
&&
5906 pf_src_connlimit(state
)) {
5907 REASON_SET(reason
, PFRES_SRCLIMIT
);
5910 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
5912 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
5913 struct pf_state_host
*psrc
, *pdst
;
5915 if (direction
== PF_OUT
) {
5916 psrc
= &(*state
)->state_key
->gwy
;
5917 pdst
= &(*state
)->state_key
->ext
;
5919 psrc
= &(*state
)->state_key
->ext
;
5920 pdst
= &(*state
)->state_key
->lan
;
5922 if (direction
== (*state
)->state_key
->direction
) {
5923 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
5924 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5925 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5926 REASON_SET(reason
, PFRES_SYNPROXY
);
5929 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5930 if ((*state
)->dst
.seqhi
== 1)
5931 (*state
)->dst
.seqhi
= htonl(random());
5932 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5933 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5934 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
5935 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
5936 REASON_SET(reason
, PFRES_SYNPROXY
);
5937 return (PF_SYNPROXY_DROP
);
5938 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
5940 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
5941 REASON_SET(reason
, PFRES_SYNPROXY
);
5944 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
5945 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
5946 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5947 pd
->src
, th
->th_dport
, th
->th_sport
,
5948 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
5949 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
5950 (*state
)->tag
, NULL
, NULL
);
5951 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5952 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5953 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
5954 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
5956 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
5957 (*state
)->src
.seqlo
;
5958 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
5959 (*state
)->dst
.seqlo
;
5960 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
5961 (*state
)->dst
.max_win
;
5962 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
5963 (*state
)->src
.max_win
;
5964 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
5965 (*state
)->src
.state
= (*state
)->dst
.state
=
5967 REASON_SET(reason
, PFRES_SYNPROXY
);
5968 return (PF_SYNPROXY_DROP
);
5972 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
5973 dst
->state
>= TCPS_FIN_WAIT_2
&&
5974 src
->state
>= TCPS_FIN_WAIT_2
) {
5975 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
5976 printf("pf: state reuse ");
5977 pf_print_state(*state
);
5978 pf_print_flags(th
->th_flags
);
5981 /* XXX make sure it's the same direction ?? */
5982 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
5983 pf_unlink_state(*state
);
5988 if ((th
->th_flags
& TH_SYN
) == 0) {
5989 sws
= (src
->wscale
& PF_WSCALE_FLAG
) ?
5990 (src
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
5991 dws
= (dst
->wscale
& PF_WSCALE_FLAG
) ?
5992 (dst
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
5998 * Sequence tracking algorithm from Guido van Rooij's paper:
5999 * http://www.madison-gurkha.com/publications/tcp_filtering/
6003 orig_seq
= seq
= ntohl(th
->th_seq
);
6004 if (src
->seqlo
== 0) {
6005 /* First packet from this end. Set its state */
6007 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
6008 src
->scrub
== NULL
) {
6009 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
6010 REASON_SET(reason
, PFRES_MEMORY
);
6015 /* Deferred generation of sequence number modulator */
6016 if (dst
->seqdiff
&& !src
->seqdiff
) {
6017 /* use random iss for the TCP server */
6018 while ((src
->seqdiff
= random() - seq
) == 0)
6020 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6021 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6023 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6024 copyback
= off
+ sizeof (*th
);
6026 ack
= ntohl(th
->th_ack
);
6029 end
= seq
+ pd
->p_len
;
6030 if (th
->th_flags
& TH_SYN
) {
6032 if (dst
->wscale
& PF_WSCALE_FLAG
) {
6033 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
6035 if (src
->wscale
& PF_WSCALE_FLAG
) {
6037 * Remove scale factor from initial
6040 sws
= src
->wscale
& PF_WSCALE_MASK
;
6041 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
6043 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6046 * Window scale negotiation has failed,
6047 * therefore we must restore the window
6048 * scale in the state record that we
6049 * optimistically removed in
6050 * pf_test_rule(). Care is required to
6051 * prevent arithmetic overflow from
6052 * zeroing the window when it's
6053 * truncated down to 16-bits.
6055 u_int32_t max_win
= dst
->max_win
;
6057 dst
->wscale
& PF_WSCALE_MASK
;
6058 dst
->max_win
= MIN(0xffff, max_win
);
6059 /* in case of a retrans SYN|ACK */
6064 if (th
->th_flags
& TH_FIN
)
6068 if (src
->state
< TCPS_SYN_SENT
)
6069 src
->state
= TCPS_SYN_SENT
;
6072 * May need to slide the window (seqhi may have been set by
6073 * the crappy stack check or if we picked up the connection
6074 * after establishment)
6076 if (src
->seqhi
== 1 ||
6077 SEQ_GEQ(end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
),
6079 src
->seqhi
= end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
);
6080 if (win
> src
->max_win
)
6084 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6086 /* Modulate sequence numbers */
6087 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6089 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6090 copyback
= off
+ sizeof (*th
);
6092 end
= seq
+ pd
->p_len
;
6093 if (th
->th_flags
& TH_SYN
)
6095 if (th
->th_flags
& TH_FIN
)
6099 if ((th
->th_flags
& TH_ACK
) == 0) {
6100 /* Let it pass through the ack skew check */
6102 } else if ((ack
== 0 &&
6103 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6104 /* broken tcp stacks do not set ack */
6105 (dst
->state
< TCPS_SYN_SENT
)) {
6107 * Many stacks (ours included) will set the ACK number in an
6108 * FIN|ACK if the SYN times out -- no sequence to ACK.
6114 /* Ease sequencing restrictions on no data packets */
6119 ackskew
= dst
->seqlo
- ack
;
6123 * Need to demodulate the sequence numbers in any TCP SACK options
6124 * (Selective ACK). We could optionally validate the SACK values
6125 * against the current ACK window, either forwards or backwards, but
6126 * I'm not confident that SACK has been implemented properly
6127 * everywhere. It wouldn't surprise me if several stacks accidently
6128 * SACK too far backwards of previously ACKed data. There really aren't
6129 * any security implications of bad SACKing unless the target stack
6130 * doesn't validate the option length correctly. Someone trying to
6131 * spoof into a TCP connection won't bother blindly sending SACK
6134 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6135 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6136 if (copyback
== -1) {
6137 REASON_SET(reason
, PFRES_MEMORY
);
6145 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6146 if (SEQ_GEQ(src
->seqhi
, end
) &&
6147 /* Last octet inside other's window space */
6148 SEQ_GEQ(seq
, src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) &&
6149 /* Retrans: not more than one window back */
6150 (ackskew
>= -MAXACKWINDOW
) &&
6151 /* Acking not more than one reassembled fragment backwards */
6152 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6153 /* Acking not more than one window forward */
6154 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6155 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6156 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6157 /* Require an exact/+1 sequence match on resets when possible */
6159 if (dst
->scrub
|| src
->scrub
) {
6160 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6161 *state
, src
, dst
, ©back
))
6167 /* update max window */
6168 if (src
->max_win
< win
)
6170 /* synchronize sequencing */
6171 if (SEQ_GT(end
, src
->seqlo
))
6173 /* slide the window of what the other end can send */
6174 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6175 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6178 if (th
->th_flags
& TH_SYN
)
6179 if (src
->state
< TCPS_SYN_SENT
)
6180 src
->state
= TCPS_SYN_SENT
;
6181 if (th
->th_flags
& TH_FIN
)
6182 if (src
->state
< TCPS_CLOSING
)
6183 src
->state
= TCPS_CLOSING
;
6184 if (th
->th_flags
& TH_ACK
) {
6185 if (dst
->state
== TCPS_SYN_SENT
) {
6186 dst
->state
= TCPS_ESTABLISHED
;
6187 if (src
->state
== TCPS_ESTABLISHED
&&
6188 (*state
)->src_node
!= NULL
&&
6189 pf_src_connlimit(state
)) {
6190 REASON_SET(reason
, PFRES_SRCLIMIT
);
6193 } else if (dst
->state
== TCPS_CLOSING
)
6194 dst
->state
= TCPS_FIN_WAIT_2
;
6196 if (th
->th_flags
& TH_RST
)
6197 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6199 /* update expire time */
6200 (*state
)->expire
= pf_time_second();
6201 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6202 dst
->state
>= TCPS_FIN_WAIT_2
)
6203 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6204 else if (src
->state
>= TCPS_CLOSING
&&
6205 dst
->state
>= TCPS_CLOSING
)
6206 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6207 else if (src
->state
< TCPS_ESTABLISHED
||
6208 dst
->state
< TCPS_ESTABLISHED
)
6209 (*state
)->timeout
= PFTM_TCP_OPENING
;
6210 else if (src
->state
>= TCPS_CLOSING
||
6211 dst
->state
>= TCPS_CLOSING
)
6212 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6214 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6216 /* Fall through to PASS packet */
6218 } else if ((dst
->state
< TCPS_SYN_SENT
||
6219 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6220 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6221 /* Within a window forward of the originating packet */
6222 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6223 /* Within a window backward of the originating packet */
6226 * This currently handles three situations:
6227 * 1) Stupid stacks will shotgun SYNs before their peer
6229 * 2) When PF catches an already established stream (the
6230 * firewall rebooted, the state table was flushed, routes
6232 * 3) Packets get funky immediately after the connection
6233 * closes (this should catch Solaris spurious ACK|FINs
6234 * that web servers like to spew after a close)
6236 * This must be a little more careful than the above code
6237 * since packet floods will also be caught here. We don't
6238 * update the TTL here to mitigate the damage of a packet
6239 * flood and so the same code can handle awkward establishment
6240 * and a loosened connection close.
6241 * In the establishment case, a correct peer response will
6242 * validate the connection, go through the normal state code
6243 * and keep updating the state TTL.
6246 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6247 printf("pf: loose state match: ");
6248 pf_print_state(*state
);
6249 pf_print_flags(th
->th_flags
);
6250 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6251 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6252 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6253 (*state
)->packets
[1],
6254 direction
== PF_IN
? "in" : "out",
6255 direction
== (*state
)->state_key
->direction
?
6259 if (dst
->scrub
|| src
->scrub
) {
6260 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6261 *state
, src
, dst
, ©back
))
6266 /* update max window */
6267 if (src
->max_win
< win
)
6269 /* synchronize sequencing */
6270 if (SEQ_GT(end
, src
->seqlo
))
6272 /* slide the window of what the other end can send */
6273 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6274 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6277 * Cannot set dst->seqhi here since this could be a shotgunned
6278 * SYN and not an already established connection.
6281 if (th
->th_flags
& TH_FIN
)
6282 if (src
->state
< TCPS_CLOSING
)
6283 src
->state
= TCPS_CLOSING
;
6284 if (th
->th_flags
& TH_RST
)
6285 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6287 /* Fall through to PASS packet */
6290 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
6291 (*state
)->src
.state
== TCPS_SYN_SENT
) {
6292 /* Send RST for state mismatches during handshake */
6293 if (!(th
->th_flags
& TH_RST
))
6294 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6295 pd
->dst
, pd
->src
, th
->th_dport
,
6296 th
->th_sport
, ntohl(th
->th_ack
), 0,
6298 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
6299 pd
->eh
, kif
->pfik_ifp
);
6303 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6304 printf("pf: BAD state: ");
6305 pf_print_state(*state
);
6306 pf_print_flags(th
->th_flags
);
6307 printf("\n seq=%u (%u) ack=%u len=%u ackskew=%d "
6308 "sws=%u dws=%u pkts=%llu:%llu dir=%s,%s\n",
6309 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
6310 (unsigned int)sws
, (unsigned int)dws
,
6311 (*state
)->packets
[0], (*state
)->packets
[1],
6312 direction
== PF_IN
? "in" : "out",
6313 direction
== (*state
)->state_key
->direction
?
6315 printf("pf: State failure on: %c %c %c %c | %c %c\n",
6316 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
6318 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) ?
6320 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
6321 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
6322 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
6323 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
6325 REASON_SET(reason
, PFRES_BADSTATE
);
6329 /* Any packets which have gotten here are to be passed */
6331 if ((*state
)->state_key
->app_state
&&
6332 (*state
)->state_key
->app_state
->handler
) {
6333 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6334 off
+ (th
->th_off
<< 2), pd
, kif
);
6336 REASON_SET(reason
, PFRES_MEMORY
);
6342 /* translate source/destination address, if necessary */
6343 if (STATE_TRANSLATE((*state
)->state_key
)) {
6344 if (direction
== PF_OUT
)
6345 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
6346 pd
->ip_sum
, &th
->th_sum
,
6347 &(*state
)->state_key
->gwy
.addr
,
6348 (*state
)->state_key
->gwy
.xport
.port
, 0, pd
->af
);
6350 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &th
->th_dport
,
6351 pd
->ip_sum
, &th
->th_sum
,
6352 &(*state
)->state_key
->lan
.addr
,
6353 (*state
)->state_key
->lan
.xport
.port
, 0, pd
->af
);
6354 copyback
= off
+ sizeof (*th
);
6358 m
= pf_lazy_makewritable(pd
, m
, copyback
);
6360 REASON_SET(reason
, PFRES_MEMORY
);
6364 /* Copyback sequence modulation or stateful scrub changes */
6365 m_copyback(m
, off
, sizeof (*th
), th
);
6372 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6373 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6376 struct pf_state_peer
*src
, *dst
;
6377 struct pf_state_key_cmp key
;
6378 struct udphdr
*uh
= pd
->hdr
.udp
;
6379 struct pf_app_state as
;
6380 int dx
, action
, extfilter
;
6382 key
.proto_variant
= PF_EXTFILTER_APD
;
6385 key
.proto
= IPPROTO_UDP
;
6386 if (direction
== PF_IN
) {
6387 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6388 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6389 key
.ext
.xport
.port
= uh
->uh_sport
;
6390 key
.gwy
.xport
.port
= uh
->uh_dport
;
6393 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6394 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6395 key
.lan
.xport
.port
= uh
->uh_sport
;
6396 key
.ext
.xport
.port
= uh
->uh_dport
;
6400 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
6401 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
6402 struct pf_ike_hdr ike
;
6403 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
6404 if (plen
< PF_IKE_PACKET_MINSIZE
) {
6405 DPFPRINTF(PF_DEBUG_MISC
,
6406 ("pf: IKE message too small.\n"));
6410 if (plen
> sizeof (ike
))
6411 plen
= sizeof (ike
);
6412 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
6414 if (ike
.initiator_cookie
) {
6415 key
.app_state
= &as
;
6416 as
.compare_lan_ext
= pf_ike_compare
;
6417 as
.compare_ext_gwy
= pf_ike_compare
;
6418 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6421 * <http://tools.ietf.org/html/\
6422 * draft-ietf-ipsec-nat-t-ike-01>
6423 * Support non-standard NAT-T implementations that
6424 * push the ESP packet over the top of the IKE packet.
6425 * Do not drop packet.
6427 DPFPRINTF(PF_DEBUG_MISC
,
6428 ("pf: IKE initiator cookie = 0.\n"));
6432 *state
= pf_find_state(kif
, &key
, dx
);
6434 if (!key
.app_state
&& *state
== 0) {
6435 key
.proto_variant
= PF_EXTFILTER_AD
;
6436 *state
= pf_find_state(kif
, &key
, dx
);
6439 if (!key
.app_state
&& *state
== 0) {
6440 key
.proto_variant
= PF_EXTFILTER_EI
;
6441 *state
= pf_find_state(kif
, &key
, dx
);
6444 /* similar to STATE_LOOKUP() */
6445 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
6446 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
6447 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6448 if (pd
->flowhash
!= 0) {
6449 pd
->pktflags
|= PKTF_FLOW_ID
;
6450 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
6454 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6457 if (direction
== (*state
)->state_key
->direction
) {
6458 src
= &(*state
)->src
;
6459 dst
= &(*state
)->dst
;
6461 src
= &(*state
)->dst
;
6462 dst
= &(*state
)->src
;
6466 if (src
->state
< PFUDPS_SINGLE
)
6467 src
->state
= PFUDPS_SINGLE
;
6468 if (dst
->state
== PFUDPS_SINGLE
)
6469 dst
->state
= PFUDPS_MULTIPLE
;
6471 /* update expire time */
6472 (*state
)->expire
= pf_time_second();
6473 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
6474 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
6476 (*state
)->timeout
= PFTM_UDP_SINGLE
;
6478 extfilter
= (*state
)->state_key
->proto_variant
;
6479 if (extfilter
> PF_EXTFILTER_APD
) {
6480 (*state
)->state_key
->ext
.xport
.port
= key
.ext
.xport
.port
;
6481 if (extfilter
> PF_EXTFILTER_AD
)
6482 PF_ACPY(&(*state
)->state_key
->ext
.addr
,
6483 &key
.ext
.addr
, key
.af
);
6486 if ((*state
)->state_key
->app_state
&&
6487 (*state
)->state_key
->app_state
->handler
) {
6488 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6489 off
+ uh
->uh_ulen
, pd
, kif
);
6491 REASON_SET(reason
, PFRES_MEMORY
);
6497 /* translate source/destination address, if necessary */
6498 if (STATE_TRANSLATE((*state
)->state_key
)) {
6499 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
6501 REASON_SET(reason
, PFRES_MEMORY
);
6505 if (direction
== PF_OUT
)
6506 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
6507 pd
->ip_sum
, &uh
->uh_sum
,
6508 &(*state
)->state_key
->gwy
.addr
,
6509 (*state
)->state_key
->gwy
.xport
.port
, 1, pd
->af
);
6511 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &uh
->uh_dport
,
6512 pd
->ip_sum
, &uh
->uh_sum
,
6513 &(*state
)->state_key
->lan
.addr
,
6514 (*state
)->state_key
->lan
.xport
.port
, 1, pd
->af
);
6515 m_copyback(m
, off
, sizeof (*uh
), uh
);
6522 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6523 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6526 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
6527 u_int16_t icmpid
= 0, *icmpsum
;
6530 struct pf_state_key_cmp key
;
6532 struct pf_app_state as
;
6535 switch (pd
->proto
) {
6538 icmptype
= pd
->hdr
.icmp
->icmp_type
;
6539 icmpid
= pd
->hdr
.icmp
->icmp_id
;
6540 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
6542 if (icmptype
== ICMP_UNREACH
||
6543 icmptype
== ICMP_SOURCEQUENCH
||
6544 icmptype
== ICMP_REDIRECT
||
6545 icmptype
== ICMP_TIMXCEED
||
6546 icmptype
== ICMP_PARAMPROB
)
6551 case IPPROTO_ICMPV6
:
6552 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
6553 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
6554 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
6556 if (icmptype
== ICMP6_DST_UNREACH
||
6557 icmptype
== ICMP6_PACKET_TOO_BIG
||
6558 icmptype
== ICMP6_TIME_EXCEEDED
||
6559 icmptype
== ICMP6_PARAM_PROB
)
6568 * ICMP query/reply message not related to a TCP/UDP packet.
6569 * Search for an ICMP state.
6572 key
.proto
= pd
->proto
;
6573 if (direction
== PF_IN
) {
6574 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6575 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6576 key
.ext
.xport
.port
= 0;
6577 key
.gwy
.xport
.port
= icmpid
;
6579 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6580 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6581 key
.lan
.xport
.port
= icmpid
;
6582 key
.ext
.xport
.port
= 0;
6587 (*state
)->expire
= pf_time_second();
6588 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
6590 /* translate source/destination address, if necessary */
6591 if (STATE_TRANSLATE((*state
)->state_key
)) {
6592 if (direction
== PF_OUT
) {
6596 pf_change_a(&saddr
->v4
.s_addr
,
6598 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
6599 pd
->hdr
.icmp
->icmp_cksum
=
6601 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6602 (*state
)->state_key
->gwy
.xport
.port
, 0);
6603 pd
->hdr
.icmp
->icmp_id
=
6604 (*state
)->state_key
->gwy
.xport
.port
;
6605 m
= pf_lazy_makewritable(pd
, m
,
6609 m_copyback(m
, off
, ICMP_MINLEN
,
6616 &pd
->hdr
.icmp6
->icmp6_cksum
,
6617 &(*state
)->state_key
->gwy
.addr
, 0);
6618 m
= pf_lazy_makewritable(pd
, m
,
6619 off
+ sizeof (struct icmp6_hdr
));
6623 sizeof (struct icmp6_hdr
),
6632 pf_change_a(&daddr
->v4
.s_addr
,
6634 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
6635 pd
->hdr
.icmp
->icmp_cksum
=
6637 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6638 (*state
)->state_key
->lan
.xport
.port
, 0);
6639 pd
->hdr
.icmp
->icmp_id
=
6640 (*state
)->state_key
->lan
.xport
.port
;
6641 m
= pf_lazy_makewritable(pd
, m
,
6645 m_copyback(m
, off
, ICMP_MINLEN
,
6652 &pd
->hdr
.icmp6
->icmp6_cksum
,
6653 &(*state
)->state_key
->lan
.addr
, 0);
6654 m
= pf_lazy_makewritable(pd
, m
,
6655 off
+ sizeof (struct icmp6_hdr
));
6659 sizeof (struct icmp6_hdr
),
6671 * ICMP error message in response to a TCP/UDP packet.
6672 * Extract the inner TCP/UDP header and search for that state.
6675 struct pf_pdesc pd2
;
6680 struct ip6_hdr h2_6
;
6686 memset(&pd2
, 0, sizeof (pd2
));
6692 /* offset of h2 in mbuf chain */
6693 ipoff2
= off
+ ICMP_MINLEN
;
6695 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
6696 NULL
, reason
, pd2
.af
)) {
6697 DPFPRINTF(PF_DEBUG_MISC
,
6698 ("pf: ICMP error message too short "
6703 * ICMP error messages don't refer to non-first
6706 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
6707 REASON_SET(reason
, PFRES_FRAG
);
6711 /* offset of protocol header that follows h2 */
6712 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
6714 pd2
.proto
= h2
.ip_p
;
6715 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
6716 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
6717 pd2
.ip_sum
= &h2
.ip_sum
;
6722 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
6724 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
6725 NULL
, reason
, pd2
.af
)) {
6726 DPFPRINTF(PF_DEBUG_MISC
,
6727 ("pf: ICMP error message too short "
6731 pd2
.proto
= h2_6
.ip6_nxt
;
6732 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
6733 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
6735 off2
= ipoff2
+ sizeof (h2_6
);
6737 switch (pd2
.proto
) {
6738 case IPPROTO_FRAGMENT
:
6740 * ICMPv6 error messages for
6741 * non-first fragments
6743 REASON_SET(reason
, PFRES_FRAG
);
6746 case IPPROTO_HOPOPTS
:
6747 case IPPROTO_ROUTING
:
6748 case IPPROTO_DSTOPTS
: {
6749 /* get next header and header length */
6750 struct ip6_ext opt6
;
6752 if (!pf_pull_hdr(m
, off2
, &opt6
,
6753 sizeof (opt6
), NULL
, reason
,
6755 DPFPRINTF(PF_DEBUG_MISC
,
6756 ("pf: ICMPv6 short opt\n"));
6759 if (pd2
.proto
== IPPROTO_AH
)
6760 off2
+= (opt6
.ip6e_len
+ 2) * 4;
6762 off2
+= (opt6
.ip6e_len
+ 1) * 8;
6763 pd2
.proto
= opt6
.ip6e_nxt
;
6764 /* goto the next header */
6771 } while (!terminal
);
6776 switch (pd2
.proto
) {
6780 struct pf_state_peer
*src
, *dst
;
6785 * Only the first 8 bytes of the TCP header can be
6786 * expected. Don't access any TCP header fields after
6787 * th_seq, an ackskew test is not possible.
6789 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
6791 DPFPRINTF(PF_DEBUG_MISC
,
6792 ("pf: ICMP error message too short "
6798 key
.proto
= IPPROTO_TCP
;
6799 if (direction
== PF_IN
) {
6800 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6801 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6802 key
.ext
.xport
.port
= th
.th_dport
;
6803 key
.gwy
.xport
.port
= th
.th_sport
;
6805 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6806 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6807 key
.lan
.xport
.port
= th
.th_dport
;
6808 key
.ext
.xport
.port
= th
.th_sport
;
6813 if (direction
== (*state
)->state_key
->direction
) {
6814 src
= &(*state
)->dst
;
6815 dst
= &(*state
)->src
;
6817 src
= &(*state
)->src
;
6818 dst
= &(*state
)->dst
;
6821 if (src
->wscale
&& (dst
->wscale
& PF_WSCALE_FLAG
))
6822 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6824 dws
= TCP_MAX_WINSHIFT
;
6826 /* Demodulate sequence number */
6827 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
6829 pf_change_a(&th
.th_seq
, icmpsum
,
6834 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
6836 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
))) {
6837 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6838 printf("pf: BAD ICMP %d:%d ",
6839 icmptype
, pd
->hdr
.icmp
->icmp_code
);
6840 pf_print_host(pd
->src
, 0, pd
->af
);
6842 pf_print_host(pd
->dst
, 0, pd
->af
);
6844 pf_print_state(*state
);
6845 printf(" seq=%u\n", seq
);
6847 REASON_SET(reason
, PFRES_BADSTATE
);
6851 if (STATE_TRANSLATE((*state
)->state_key
)) {
6852 if (direction
== PF_IN
) {
6853 pf_change_icmp(pd2
.src
, &th
.th_sport
,
6854 daddr
, &(*state
)->state_key
->lan
.addr
,
6855 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6856 pd2
.ip_sum
, icmpsum
,
6857 pd
->ip_sum
, 0, pd2
.af
);
6859 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
6860 saddr
, &(*state
)->state_key
->gwy
.addr
,
6861 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6862 pd2
.ip_sum
, icmpsum
,
6863 pd
->ip_sum
, 0, pd2
.af
);
6869 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
6875 m_copyback(m
, off
, ICMP_MINLEN
,
6877 m_copyback(m
, ipoff2
, sizeof (h2
),
6884 sizeof (struct icmp6_hdr
),
6886 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6891 m_copyback(m
, off2
, 8, &th
);
6900 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
6901 NULL
, reason
, pd2
.af
)) {
6902 DPFPRINTF(PF_DEBUG_MISC
,
6903 ("pf: ICMP error message too short "
6909 key
.proto
= IPPROTO_UDP
;
6910 if (direction
== PF_IN
) {
6911 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6912 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6913 key
.ext
.xport
.port
= uh
.uh_dport
;
6914 key
.gwy
.xport
.port
= uh
.uh_sport
;
6917 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6918 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6919 key
.lan
.xport
.port
= uh
.uh_dport
;
6920 key
.ext
.xport
.port
= uh
.uh_sport
;
6924 key
.proto_variant
= PF_EXTFILTER_APD
;
6926 if (ntohs(uh
.uh_sport
) == PF_IKE_PORT
&&
6927 ntohs(uh
.uh_dport
) == PF_IKE_PORT
) {
6928 struct pf_ike_hdr ike
;
6930 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
6931 if (direction
== PF_IN
&&
6932 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
6933 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
6934 "ICMP error, embedded IKE message "
6939 if (plen
> sizeof (ike
))
6940 plen
= sizeof (ike
);
6941 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
6943 key
.app_state
= &as
;
6944 as
.compare_lan_ext
= pf_ike_compare
;
6945 as
.compare_ext_gwy
= pf_ike_compare
;
6946 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6949 *state
= pf_find_state(kif
, &key
, dx
);
6951 if (key
.app_state
&& *state
== 0) {
6953 *state
= pf_find_state(kif
, &key
, dx
);
6957 key
.proto_variant
= PF_EXTFILTER_AD
;
6958 *state
= pf_find_state(kif
, &key
, dx
);
6962 key
.proto_variant
= PF_EXTFILTER_EI
;
6963 *state
= pf_find_state(kif
, &key
, dx
);
6966 /* similar to STATE_LOOKUP() */
6967 if (*state
!= NULL
&& pd
!= NULL
&&
6968 !(pd
->pktflags
& PKTF_FLOW_ID
)) {
6969 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
6970 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6971 if (pd
->flowhash
!= 0) {
6972 pd
->pktflags
|= PKTF_FLOW_ID
;
6973 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
6977 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6980 if (STATE_TRANSLATE((*state
)->state_key
)) {
6981 if (direction
== PF_IN
) {
6982 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
6983 daddr
, &(*state
)->state_key
->lan
.addr
,
6984 (*state
)->state_key
->lan
.xport
.port
, &uh
.uh_sum
,
6985 pd2
.ip_sum
, icmpsum
,
6986 pd
->ip_sum
, 1, pd2
.af
);
6988 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
6989 saddr
, &(*state
)->state_key
->gwy
.addr
,
6990 (*state
)->state_key
->gwy
.xport
.port
, &uh
.uh_sum
,
6991 pd2
.ip_sum
, icmpsum
,
6992 pd
->ip_sum
, 1, pd2
.af
);
6994 m
= pf_lazy_makewritable(pd
, m
,
6995 off2
+ sizeof (uh
));
7001 m_copyback(m
, off
, ICMP_MINLEN
,
7003 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7009 sizeof (struct icmp6_hdr
),
7011 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7016 m_copyback(m
, off2
, sizeof (uh
), &uh
);
7023 case IPPROTO_ICMP
: {
7026 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
7027 NULL
, reason
, pd2
.af
)) {
7028 DPFPRINTF(PF_DEBUG_MISC
,
7029 ("pf: ICMP error message too short i"
7035 key
.proto
= IPPROTO_ICMP
;
7036 if (direction
== PF_IN
) {
7037 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7038 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7039 key
.ext
.xport
.port
= 0;
7040 key
.gwy
.xport
.port
= iih
.icmp_id
;
7042 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7043 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7044 key
.lan
.xport
.port
= iih
.icmp_id
;
7045 key
.ext
.xport
.port
= 0;
7050 if (STATE_TRANSLATE((*state
)->state_key
)) {
7051 if (direction
== PF_IN
) {
7052 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
7053 daddr
, &(*state
)->state_key
->lan
.addr
,
7054 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7055 pd2
.ip_sum
, icmpsum
,
7056 pd
->ip_sum
, 0, AF_INET
);
7058 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
7059 saddr
, &(*state
)->state_key
->gwy
.addr
,
7060 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7061 pd2
.ip_sum
, icmpsum
,
7062 pd
->ip_sum
, 0, AF_INET
);
7064 m
= pf_lazy_makewritable(pd
, m
, off2
+ ICMP_MINLEN
);
7067 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
7068 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7069 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
7077 case IPPROTO_ICMPV6
: {
7078 struct icmp6_hdr iih
;
7080 if (!pf_pull_hdr(m
, off2
, &iih
,
7081 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
7082 DPFPRINTF(PF_DEBUG_MISC
,
7083 ("pf: ICMP error message too short "
7089 key
.proto
= IPPROTO_ICMPV6
;
7090 if (direction
== PF_IN
) {
7091 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7092 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7093 key
.ext
.xport
.port
= 0;
7094 key
.gwy
.xport
.port
= iih
.icmp6_id
;
7096 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7097 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7098 key
.lan
.xport
.port
= iih
.icmp6_id
;
7099 key
.ext
.xport
.port
= 0;
7104 if (STATE_TRANSLATE((*state
)->state_key
)) {
7105 if (direction
== PF_IN
) {
7106 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
7107 daddr
, &(*state
)->state_key
->lan
.addr
,
7108 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7109 pd2
.ip_sum
, icmpsum
,
7110 pd
->ip_sum
, 0, AF_INET6
);
7112 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
7113 saddr
, &(*state
)->state_key
->gwy
.addr
,
7114 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7115 pd2
.ip_sum
, icmpsum
,
7116 pd
->ip_sum
, 0, AF_INET6
);
7118 m
= pf_lazy_makewritable(pd
, m
, off2
+
7119 sizeof (struct icmp6_hdr
));
7122 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
7124 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
7125 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
7135 key
.proto
= pd2
.proto
;
7136 if (direction
== PF_IN
) {
7137 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7138 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7139 key
.ext
.xport
.port
= 0;
7140 key
.gwy
.xport
.port
= 0;
7142 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7143 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7144 key
.lan
.xport
.port
= 0;
7145 key
.ext
.xport
.port
= 0;
7150 if (STATE_TRANSLATE((*state
)->state_key
)) {
7151 if (direction
== PF_IN
) {
7152 pf_change_icmp(pd2
.src
, NULL
,
7153 daddr
, &(*state
)->state_key
->lan
.addr
,
7155 pd2
.ip_sum
, icmpsum
,
7156 pd
->ip_sum
, 0, pd2
.af
);
7158 pf_change_icmp(pd2
.dst
, NULL
,
7159 saddr
, &(*state
)->state_key
->gwy
.addr
,
7161 pd2
.ip_sum
, icmpsum
,
7162 pd
->ip_sum
, 0, pd2
.af
);
7167 m
= pf_lazy_makewritable(pd
, m
,
7168 ipoff2
+ sizeof (h2
));
7174 m
= pf_lazy_makewritable(pd
, m
,
7175 ipoff2
+ sizeof (h2_6
));
7179 sizeof (struct icmp6_hdr
),
7181 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7196 pf_test_state_grev1(struct pf_state
**state
, int direction
,
7197 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
7199 struct pf_state_peer
*src
;
7200 struct pf_state_peer
*dst
;
7201 struct pf_state_key_cmp key
;
7202 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
7207 key
.proto
= IPPROTO_GRE
;
7208 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
7209 if (direction
== PF_IN
) {
7210 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7211 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7212 key
.gwy
.xport
.call_id
= grev1
->call_id
;
7214 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7215 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7216 key
.ext
.xport
.call_id
= grev1
->call_id
;
7221 if (direction
== (*state
)->state_key
->direction
) {
7222 src
= &(*state
)->src
;
7223 dst
= &(*state
)->dst
;
7225 src
= &(*state
)->dst
;
7226 dst
= &(*state
)->src
;
7230 if (src
->state
< PFGRE1S_INITIATING
)
7231 src
->state
= PFGRE1S_INITIATING
;
7233 /* update expire time */
7234 (*state
)->expire
= pf_time_second();
7235 if (src
->state
>= PFGRE1S_INITIATING
&&
7236 dst
->state
>= PFGRE1S_INITIATING
) {
7237 if ((*state
)->timeout
!= PFTM_TCP_ESTABLISHED
)
7238 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
7239 src
->state
= PFGRE1S_ESTABLISHED
;
7240 dst
->state
= PFGRE1S_ESTABLISHED
;
7242 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
7245 if ((*state
)->state_key
->app_state
)
7246 (*state
)->state_key
->app_state
->u
.grev1
.pptp_state
->expire
=
7249 /* translate source/destination address, if necessary */
7250 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
7251 if (direction
== PF_OUT
) {
7255 pf_change_a(&pd
->src
->v4
.s_addr
,
7257 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7262 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7268 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
7273 pf_change_a(&pd
->dst
->v4
.s_addr
,
7275 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7280 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7287 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
7290 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
7297 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7298 int off
, struct pf_pdesc
*pd
)
7301 struct pf_state_peer
*src
;
7302 struct pf_state_peer
*dst
;
7303 struct pf_state_key_cmp key
;
7304 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
7307 memset(&key
, 0, sizeof (key
));
7309 key
.proto
= IPPROTO_ESP
;
7310 if (direction
== PF_IN
) {
7311 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7312 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7313 key
.gwy
.xport
.spi
= esp
->spi
;
7315 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7316 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7317 key
.ext
.xport
.spi
= esp
->spi
;
7320 *state
= pf_find_state(kif
, &key
, direction
);
7327 * No matching state. Look for a blocking state. If we find
7328 * one, then use that state and move it so that it's keyed to
7329 * the SPI in the current packet.
7331 if (direction
== PF_IN
) {
7332 key
.gwy
.xport
.spi
= 0;
7334 s
= pf_find_state(kif
, &key
, direction
);
7336 struct pf_state_key
*sk
= s
->state_key
;
7338 RB_REMOVE(pf_state_tree_ext_gwy
,
7339 &pf_statetbl_ext_gwy
, sk
);
7340 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
7343 if (RB_INSERT(pf_state_tree_ext_gwy
,
7344 &pf_statetbl_ext_gwy
, sk
))
7345 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
7350 key
.ext
.xport
.spi
= 0;
7352 s
= pf_find_state(kif
, &key
, direction
);
7354 struct pf_state_key
*sk
= s
->state_key
;
7356 RB_REMOVE(pf_state_tree_lan_ext
,
7357 &pf_statetbl_lan_ext
, sk
);
7358 sk
->ext
.xport
.spi
= esp
->spi
;
7360 if (RB_INSERT(pf_state_tree_lan_ext
,
7361 &pf_statetbl_lan_ext
, sk
))
7362 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
7371 if (s
->creatorid
== pf_status
.hostid
)
7372 pfsync_delete_state(s
);
7374 s
->timeout
= PFTM_UNLINKED
;
7375 hook_runloop(&s
->unlink_hooks
,
7376 HOOK_REMOVE
|HOOK_FREE
);
7377 pf_src_tree_remove_state(s
);
7384 /* similar to STATE_LOOKUP() */
7385 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
7386 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
7387 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7388 if (pd
->flowhash
!= 0) {
7389 pd
->pktflags
|= PKTF_FLOW_ID
;
7390 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
7394 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7397 if (direction
== (*state
)->state_key
->direction
) {
7398 src
= &(*state
)->src
;
7399 dst
= &(*state
)->dst
;
7401 src
= &(*state
)->dst
;
7402 dst
= &(*state
)->src
;
7406 if (src
->state
< PFESPS_INITIATING
)
7407 src
->state
= PFESPS_INITIATING
;
7409 /* update expire time */
7410 (*state
)->expire
= pf_time_second();
7411 if (src
->state
>= PFESPS_INITIATING
&&
7412 dst
->state
>= PFESPS_INITIATING
) {
7413 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
7414 src
->state
= PFESPS_ESTABLISHED
;
7415 dst
->state
= PFESPS_ESTABLISHED
;
7417 (*state
)->timeout
= PFTM_ESP_INITIATING
;
7419 /* translate source/destination address, if necessary */
7420 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7421 if (direction
== PF_OUT
) {
7425 pf_change_a(&pd
->src
->v4
.s_addr
,
7427 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7432 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7441 pf_change_a(&pd
->dst
->v4
.s_addr
,
7443 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7448 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7460 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7461 struct pf_pdesc
*pd
)
7463 struct pf_state_peer
*src
, *dst
;
7464 struct pf_state_key_cmp key
;
7468 key
.proto
= pd
->proto
;
7469 if (direction
== PF_IN
) {
7470 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7471 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7472 key
.ext
.xport
.port
= 0;
7473 key
.gwy
.xport
.port
= 0;
7475 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7476 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7477 key
.lan
.xport
.port
= 0;
7478 key
.ext
.xport
.port
= 0;
7483 if (direction
== (*state
)->state_key
->direction
) {
7484 src
= &(*state
)->src
;
7485 dst
= &(*state
)->dst
;
7487 src
= &(*state
)->dst
;
7488 dst
= &(*state
)->src
;
7492 if (src
->state
< PFOTHERS_SINGLE
)
7493 src
->state
= PFOTHERS_SINGLE
;
7494 if (dst
->state
== PFOTHERS_SINGLE
)
7495 dst
->state
= PFOTHERS_MULTIPLE
;
7497 /* update expire time */
7498 (*state
)->expire
= pf_time_second();
7499 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
7500 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
7502 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
7504 /* translate source/destination address, if necessary */
7505 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7506 if (direction
== PF_OUT
) {
7510 pf_change_a(&pd
->src
->v4
.s_addr
,
7512 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
7519 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
7527 pf_change_a(&pd
->dst
->v4
.s_addr
,
7529 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
7536 &(*state
)->state_key
->lan
.addr
, pd
->af
);
7547 * ipoff and off are measured from the start of the mbuf chain.
7548 * h must be at "ipoff" on the mbuf chain.
7551 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
7552 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
7557 struct ip
*h
= mtod(m
, struct ip
*);
7558 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
7561 if (fragoff
>= len
) {
7562 ACTION_SET(actionp
, PF_PASS
);
7564 ACTION_SET(actionp
, PF_DROP
);
7565 REASON_SET(reasonp
, PFRES_FRAG
);
7569 if (m
->m_pkthdr
.len
< off
+ len
||
7570 ntohs(h
->ip_len
) < off
+ len
) {
7571 ACTION_SET(actionp
, PF_DROP
);
7572 REASON_SET(reasonp
, PFRES_SHORT
);
7580 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
7582 if (m
->m_pkthdr
.len
< off
+ len
||
7583 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
7584 (unsigned)(off
+ len
)) {
7585 ACTION_SET(actionp
, PF_DROP
);
7586 REASON_SET(reasonp
, PFRES_SHORT
);
7593 m_copydata(m
, off
, len
, p
);
7598 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
7601 struct sockaddr_in
*dst
;
7604 struct sockaddr_in6
*dst6
;
7605 struct route_in6 ro
;
7610 bzero(&ro
, sizeof (ro
));
7613 dst
= satosin(&ro
.ro_dst
);
7614 dst
->sin_family
= AF_INET
;
7615 dst
->sin_len
= sizeof (*dst
);
7616 dst
->sin_addr
= addr
->v4
;
7620 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7621 dst6
->sin6_family
= AF_INET6
;
7622 dst6
->sin6_len
= sizeof (*dst6
);
7623 dst6
->sin6_addr
= addr
->v6
;
7630 /* XXX: IFT_ENC is not currently used by anything*/
7631 /* Skip checks for ipsec interfaces */
7632 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
7635 /* XXX: what is the point of this? */
7636 rtalloc((struct route
*)&ro
);
7644 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
7647 struct sockaddr_in
*dst
;
7649 struct sockaddr_in6
*dst6
;
7650 struct route_in6 ro
;
7656 bzero(&ro
, sizeof (ro
));
7659 dst
= satosin(&ro
.ro_dst
);
7660 dst
->sin_family
= AF_INET
;
7661 dst
->sin_len
= sizeof (*dst
);
7662 dst
->sin_addr
= addr
->v4
;
7666 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7667 dst6
->sin6_family
= AF_INET6
;
7668 dst6
->sin6_len
= sizeof (*dst6
);
7669 dst6
->sin6_addr
= addr
->v6
;
7676 /* XXX: what is the point of this? */
7677 rtalloc((struct route
*)&ro
);
7686 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7687 struct pf_state
*s
, struct pf_pdesc
*pd
)
7690 struct mbuf
*m0
, *m1
;
7691 struct route iproute
;
7692 struct route
*ro
= &iproute
;
7693 struct sockaddr_in
*dst
;
7695 struct ifnet
*ifp
= NULL
;
7696 struct pf_addr naddr
;
7697 struct pf_src_node
*sn
= NULL
;
7701 bzero(&iproute
, sizeof (iproute
));
7703 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7704 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7705 panic("pf_route: invalid parameters");
7707 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7713 if (r
->rt
== PF_DUPTO
) {
7714 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7717 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7722 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7723 DPFPRINTF(PF_DEBUG_URGENT
,
7724 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7728 ip
= mtod(m0
, struct ip
*);
7730 dst
= satosin((void *)&ro
->ro_dst
);
7731 dst
->sin_family
= AF_INET
;
7732 dst
->sin_len
= sizeof (*dst
);
7733 dst
->sin_addr
= ip
->ip_dst
;
7735 if (r
->rt
== PF_FASTROUTE
) {
7737 if (ro
->ro_rt
== NULL
) {
7738 ipstat
.ips_noroute
++;
7742 ifp
= ro
->ro_rt
->rt_ifp
;
7744 ro
->ro_rt
->rt_use
++;
7746 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
7747 dst
= satosin((void *)ro
->ro_rt
->rt_gateway
);
7748 RT_UNLOCK(ro
->ro_rt
);
7750 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7751 DPFPRINTF(PF_DEBUG_URGENT
,
7752 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
7756 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
7758 if (!PF_AZERO(&naddr
, AF_INET
))
7759 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
7760 ifp
= r
->rpool
.cur
->kif
?
7761 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7763 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
7764 dst
->sin_addr
.s_addr
=
7765 s
->rt_addr
.v4
.s_addr
;
7766 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7773 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7775 else if (m0
== NULL
)
7777 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7778 DPFPRINTF(PF_DEBUG_URGENT
,
7779 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7782 ip
= mtod(m0
, struct ip
*);
7785 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
7786 ip_output_checksum(ifp
, m0
, ((ip
->ip_hl
) << 2), ntohs(ip
->ip_len
),
7789 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
|| TSO_IPV4_OK(ifp
, m0
) ||
7790 (!(ip
->ip_off
& htons(IP_DF
)) &&
7791 (ifp
->if_hwassist
& CSUM_FRAGMENT
))) {
7793 if (sw_csum
& CSUM_DELAY_IP
) {
7794 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
7795 sw_csum
&= ~CSUM_DELAY_IP
;
7796 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IP
;
7798 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
, sintosa(dst
));
7803 * Too large for interface; fragment if possible.
7804 * Must be able to put at least 8 bytes per fragment.
7805 * Balk when DF bit is set or the interface didn't support TSO.
7807 if ((ip
->ip_off
& htons(IP_DF
)) ||
7808 (m0
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
)) {
7809 ipstat
.ips_cantfrag
++;
7810 if (r
->rt
!= PF_DUPTO
) {
7811 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
7820 /* PR-8933605: send ip_len,ip_off to ip_fragment in host byte order */
7821 #if BYTE_ORDER != BIG_ENDIAN
7825 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
7832 for (m0
= m1
; m0
; m0
= m1
) {
7836 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
,
7843 ipstat
.ips_fragmented
++;
7846 if (r
->rt
!= PF_DUPTO
)
7849 ROUTE_RELEASE(&iproute
);
7860 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7861 struct pf_state
*s
, struct pf_pdesc
*pd
)
7865 struct route_in6 ip6route
;
7866 struct route_in6
*ro
;
7867 struct sockaddr_in6
*dst
;
7868 struct ip6_hdr
*ip6
;
7869 struct ifnet
*ifp
= NULL
;
7870 struct pf_addr naddr
;
7871 struct pf_src_node
*sn
= NULL
;
7874 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7875 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7876 panic("pf_route6: invalid parameters");
7878 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7884 if (r
->rt
== PF_DUPTO
) {
7885 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7888 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7893 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7894 DPFPRINTF(PF_DEBUG_URGENT
,
7895 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
7898 ip6
= mtod(m0
, struct ip6_hdr
*);
7901 bzero((caddr_t
)ro
, sizeof (*ro
));
7902 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
7903 dst
->sin6_family
= AF_INET6
;
7904 dst
->sin6_len
= sizeof (*dst
);
7905 dst
->sin6_addr
= ip6
->ip6_dst
;
7907 /* Cheat. XXX why only in the v6 case??? */
7908 if (r
->rt
== PF_FASTROUTE
) {
7909 struct pf_mtag
*pf_mtag
;
7911 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
7913 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
7914 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
7918 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7919 DPFPRINTF(PF_DEBUG_URGENT
,
7920 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
7924 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
7926 if (!PF_AZERO(&naddr
, AF_INET6
))
7927 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7929 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7931 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
7932 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7933 &s
->rt_addr
, AF_INET6
);
7934 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7940 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7942 else if (m0
== NULL
)
7944 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7945 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
7946 "< sizeof (struct ip6_hdr)\n"));
7949 ip6
= mtod(m0
, struct ip6_hdr
*);
7953 * If the packet is too large for the outgoing interface,
7954 * send back an icmp6 error.
7956 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
7957 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
7958 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
7959 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, NULL
);
7961 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
7962 if (r
->rt
!= PF_DUPTO
)
7963 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
7969 if (r
->rt
!= PF_DUPTO
)
7981 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
7982 * off is the offset where the protocol header starts
7983 * len is the total length of protocol header plus payload
7984 * returns 0 when the checksum is valid, otherwise returns 1.
7987 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
7996 * Optimize for the common case; if the hardware calculated
7997 * value doesn't include pseudo-header checksum, or if it
7998 * is partially-computed (only 16-bit summation), do it in
8001 if ((m
->m_pkthdr
.csum_flags
&
8002 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) ==
8003 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
) &&
8004 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
8010 case IPPROTO_ICMPV6
:
8016 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
8018 if (m
->m_pkthdr
.len
< off
+ len
)
8023 if (p
== IPPROTO_ICMP
) {
8028 sum
= in_cksum(m
, len
);
8032 if (m
->m_len
< (int)sizeof (struct ip
))
8034 sum
= inet_cksum(m
, p
, off
, len
);
8040 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
8042 sum
= inet6_cksum(m
, p
, off
, len
);
8051 tcpstat
.tcps_rcvbadsum
++;
8054 udpstat
.udps_badsum
++;
8057 icmpstat
.icps_checksum
++;
8060 case IPPROTO_ICMPV6
:
8061 icmp6stat
.icp6s_checksum
++;
8071 #define PF_APPLE_UPDATE_PDESC_IPv4() \
8073 if (m && pd.mp && m != pd.mp) { \
8075 h = mtod(m, struct ip *); \
8076 pd.pf_mtag = pf_get_mtag(m); \
8081 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8082 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8087 struct pfi_kif
*kif
;
8088 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8089 struct mbuf
*m
= *m0
;
8091 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8092 struct pf_state
*s
= NULL
;
8093 struct pf_state_key
*sk
= NULL
;
8094 struct pf_ruleset
*ruleset
= NULL
;
8096 int off
, dirndx
, pqid
= 0;
8098 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8100 if (!pf_status
.running
)
8103 memset(&pd
, 0, sizeof (pd
));
8105 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8106 DPFPRINTF(PF_DEBUG_URGENT
,
8107 ("pf_test: pf_get_mtag returned NULL\n"));
8111 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8114 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8117 DPFPRINTF(PF_DEBUG_URGENT
,
8118 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
8121 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8124 VERIFY(m
->m_flags
& M_PKTHDR
);
8126 /* initialize enough of pd for the done label */
8127 h
= mtod(m
, struct ip
*);
8130 pd
.pf_mtag
= pf_get_mtag(m
);
8131 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8132 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8133 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8134 pd
.ip_sum
= &h
->ip_sum
;
8136 pd
.proto_variant
= 0;
8139 pd
.tot_len
= ntohs(h
->ip_len
);
8142 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8144 REASON_SET(&reason
, PFRES_SHORT
);
8150 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8152 #endif /* DUMMYNET */
8154 /* We do IP header normalization and packet reassembly here */
8155 action
= pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
);
8157 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8164 #endif /* DUMMYNET */
8165 m
= *m0
; /* pf_normalize messes with m0 */
8166 h
= mtod(m
, struct ip
*);
8168 off
= h
->ip_hl
<< 2;
8169 if (off
< (int)sizeof (*h
)) {
8171 REASON_SET(&reason
, PFRES_SHORT
);
8176 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8177 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8178 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8179 pd
.ip_sum
= &h
->ip_sum
;
8181 pd
.proto_variant
= 0;
8184 pd
.pf_mtag
= pf_get_mtag(m
);
8187 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8188 pd
.tot_len
= ntohs(h
->ip_len
);
8191 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
8192 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
8193 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
8194 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
8197 /* handle fragments that didn't get reassembled by normalization */
8198 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
8199 pd
.flags
|= PFDESC_IP_FRAG
;
8201 /* Traffic goes through dummynet first */
8202 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8203 if (action
== PF_DROP
|| m
== NULL
) {
8207 #endif /* DUMMYNET */
8208 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8218 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8219 &action
, &reason
, AF_INET
)) {
8220 log
= action
!= PF_PASS
;
8223 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8224 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
8227 /* Traffic goes through dummynet first */
8228 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8229 if (action
== PF_DROP
|| m
== NULL
) {
8233 #endif /* DUMMYNET */
8234 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8237 PF_APPLE_UPDATE_PDESC_IPv4();
8238 if (action
== PF_DROP
)
8240 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8244 PF_APPLE_UPDATE_PDESC_IPv4();
8245 if (action
== PF_PASS
) {
8247 pfsync_update_state(s
);
8248 #endif /* NPFSYNC */
8252 } else if (s
== NULL
)
8253 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8254 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8262 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8263 &action
, &reason
, AF_INET
)) {
8264 log
= action
!= PF_PASS
;
8267 if (uh
.uh_dport
== 0 ||
8268 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8269 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8271 REASON_SET(&reason
, PFRES_SHORT
);
8275 /* Traffic goes through dummynet first */
8276 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8277 if (action
== PF_DROP
|| m
== NULL
) {
8281 #endif /* DUMMYNET */
8282 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8286 PF_APPLE_UPDATE_PDESC_IPv4();
8287 if (action
== PF_PASS
) {
8289 pfsync_update_state(s
);
8290 #endif /* NPFSYNC */
8294 } else if (s
== NULL
)
8295 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8296 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8300 case IPPROTO_ICMP
: {
8304 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
8305 &action
, &reason
, AF_INET
)) {
8306 log
= action
!= PF_PASS
;
8310 /* Traffic goes through dummynet first */
8311 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8312 if (action
== PF_DROP
|| m
== NULL
) {
8316 #endif /* DUMMYNET */
8317 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8321 PF_APPLE_UPDATE_PDESC_IPv4();
8322 if (action
== PF_PASS
) {
8324 pfsync_update_state(s
);
8325 #endif /* NPFSYNC */
8329 } else if (s
== NULL
)
8330 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8331 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8336 struct pf_esp_hdr esp
;
8339 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8341 log
= action
!= PF_PASS
;
8345 /* Traffic goes through dummynet first */
8346 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8347 if (action
== PF_DROP
|| m
== NULL
) {
8351 #endif /* DUMMYNET */
8352 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8355 PF_APPLE_UPDATE_PDESC_IPv4();
8356 if (action
== PF_PASS
) {
8358 pfsync_update_state(s
);
8359 #endif /* NPFSYNC */
8363 } else if (s
== NULL
)
8364 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8365 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8370 struct pf_grev1_hdr grev1
;
8371 pd
.hdr
.grev1
= &grev1
;
8372 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8373 &reason
, AF_INET
)) {
8374 log
= (action
!= PF_PASS
);
8378 /* Traffic goes through dummynet first */
8379 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8380 if (action
== PF_DROP
|| m
== NULL
) {
8384 #endif /* DUMMYNET */
8385 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8386 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8387 if (ntohs(grev1
.payload_length
) >
8388 m
->m_pkthdr
.len
- off
) {
8390 REASON_SET(&reason
, PFRES_SHORT
);
8393 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8394 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8395 if (pd
.lmw
< 0) goto done
;
8396 PF_APPLE_UPDATE_PDESC_IPv4();
8397 if (action
== PF_PASS
) {
8399 pfsync_update_state(s
);
8400 #endif /* NPFSYNC */
8405 } else if (s
== NULL
) {
8406 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8407 h
, &pd
, &a
, &ruleset
, NULL
);
8408 if (action
== PF_PASS
)
8413 /* not GREv1/PPTP, so treat as ordinary GRE... */
8418 /* Traffic goes through dummynet first */
8419 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8420 if (action
== PF_DROP
|| m
== NULL
) {
8424 #endif /* DUMMYNET */
8425 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8428 PF_APPLE_UPDATE_PDESC_IPv4();
8429 if (action
== PF_PASS
) {
8431 pfsync_update_state(s
);
8432 #endif /* NPFSYNC */
8436 } else if (s
== NULL
)
8437 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
8438 &pd
, &a
, &ruleset
, NULL
);
8444 PF_APPLE_UPDATE_PDESC_IPv4();
8446 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
8447 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
8449 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8451 DPFPRINTF(PF_DEBUG_MISC
,
8452 ("pf: dropping packet with ip options [hlen=%u]\n",
8453 (unsigned int) h
->ip_hl
));
8456 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
8457 (pd
.pktflags
& PKTF_FLOW_ID
))
8458 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
8461 if (action
== PF_PASS
) {
8463 if (altq_allowed
&& r
->qid
) {
8464 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
8465 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
8467 pd
.pf_mtag
->pftag_qid
= r
->qid
;
8469 #endif /* PF_ALTQ */
8471 /* add hints for ecn */
8472 pd
.pf_mtag
->pftag_hdr
= h
;
8473 /* record address family */
8474 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET6
;
8475 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
8477 /* record protocol */
8478 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
8482 * connections redirected to loopback should not match sockets
8483 * bound specifically to loopback due to security implications,
8484 * see tcp_input() and in_pcblookup_listen().
8486 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
8487 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8488 (s
->nat_rule
.ptr
->action
== PF_RDR
||
8489 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
8490 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
8491 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
8496 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8497 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
8498 lr
= s
->nat_rule
.ptr
;
8501 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
8505 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
8506 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
8508 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
8509 dirndx
= (dir
== PF_OUT
);
8510 r
->packets
[dirndx
]++;
8511 r
->bytes
[dirndx
] += pd
.tot_len
;
8513 a
->packets
[dirndx
]++;
8514 a
->bytes
[dirndx
] += pd
.tot_len
;
8518 if (s
->nat_rule
.ptr
!= NULL
) {
8519 s
->nat_rule
.ptr
->packets
[dirndx
]++;
8520 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
8522 if (s
->src_node
!= NULL
) {
8523 s
->src_node
->packets
[dirndx
]++;
8524 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
8526 if (s
->nat_src_node
!= NULL
) {
8527 s
->nat_src_node
->packets
[dirndx
]++;
8528 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
8530 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
8531 s
->packets
[dirndx
]++;
8532 s
->bytes
[dirndx
] += pd
.tot_len
;
8535 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
8539 * XXX: we need to make sure that the addresses
8540 * passed to pfr_update_stats() are the same than
8541 * the addresses used during matching (pfr_match)
8543 if (r
== &pf_default_rule
) {
8545 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8546 &pd
.baddr
: &pd
.naddr
;
8548 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8549 &pd
.naddr
: &pd
.baddr
;
8550 if (x
== &pd
.baddr
|| s
== NULL
) {
8551 /* we need to change the address */
8558 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
8559 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
8560 sk
->direction
== dir
) ?
8561 pd
.src
: pd
.dst
, pd
.af
,
8562 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8564 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
8565 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
8566 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
8567 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8571 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
8575 REASON_SET(&reason
, PFRES_MEMORY
);
8579 if (action
== PF_DROP
) {
8588 if (action
== PF_SYNPROXY_DROP
) {
8593 /* pf_route can free the mbuf causing *m0 to become NULL */
8594 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
8601 #define PF_APPLE_UPDATE_PDESC_IPv6() \
8603 if (m && pd.mp && m != pd.mp) { \
8607 h = mtod(m, struct ip6_hdr *); \
8612 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8613 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8618 struct pfi_kif
*kif
;
8619 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8620 struct mbuf
*m
= *m0
, *n
= NULL
;
8622 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8623 struct pf_state
*s
= NULL
;
8624 struct pf_state_key
*sk
= NULL
;
8625 struct pf_ruleset
*ruleset
= NULL
;
8627 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
8630 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8632 if (!pf_status
.running
)
8635 memset(&pd
, 0, sizeof (pd
));
8637 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8638 DPFPRINTF(PF_DEBUG_URGENT
,
8639 ("pf_test6: pf_get_mtag returned NULL\n"));
8643 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8646 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8649 DPFPRINTF(PF_DEBUG_URGENT
,
8650 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
8653 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8656 VERIFY(m
->m_flags
& M_PKTHDR
);
8658 h
= mtod(m
, struct ip6_hdr
*);
8661 off
= ((caddr_t
)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
8664 pd
.pf_mtag
= pf_get_mtag(m
);
8665 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8666 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8667 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8671 pd
.proto_variant
= 0;
8673 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8674 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
8677 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
8678 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
8679 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
8680 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
8683 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8685 REASON_SET(&reason
, PFRES_SHORT
);
8691 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8693 #endif /* DUMMYNET */
8695 /* We do IP header normalization and packet reassembly here */
8696 action
= pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
);
8698 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8705 #endif /* DUMMYNET */
8706 h
= mtod(m
, struct ip6_hdr
*);
8710 * we do not support jumbogram yet. if we keep going, zero ip6_plen
8711 * will do something bad, so drop the packet for now.
8713 if (htons(h
->ip6_plen
) == 0) {
8715 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
8720 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8721 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8722 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8726 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
8729 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
8730 pd
.proto
= h
->ip6_nxt
;
8731 pd
.proto_variant
= 0;
8734 pd
.pf_mtag
= pf_get_mtag(m
);
8738 case IPPROTO_FRAGMENT
: {
8739 struct ip6_frag ip6f
;
8741 pd
.flags
|= PFDESC_IP_FRAG
;
8742 if (!pf_pull_hdr(m
, off
, &ip6f
, sizeof ip6f
, NULL
,
8744 DPFPRINTF(PF_DEBUG_MISC
,
8745 ("pf: IPv6 short fragment header\n"));
8747 REASON_SET(&reason
, PFRES_SHORT
);
8751 pd
.proto
= nxt
= ip6f
.ip6f_nxt
;
8753 /* Traffic goes through dummynet first */
8754 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8755 if (action
== PF_DROP
|| m
== NULL
) {
8759 #endif /* DUMMYNET */
8760 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
, &pd
, &a
,
8762 if (action
== PF_DROP
) {
8763 REASON_SET(&reason
, PFRES_FRAG
);
8768 case IPPROTO_ROUTING
:
8773 case IPPROTO_HOPOPTS
:
8774 case IPPROTO_DSTOPTS
: {
8775 /* get next header and header length */
8776 struct ip6_ext opt6
;
8778 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
8779 NULL
, &reason
, pd
.af
)) {
8780 DPFPRINTF(PF_DEBUG_MISC
,
8781 ("pf: IPv6 short opt\n"));
8786 if (pd
.proto
== IPPROTO_AH
)
8787 off
+= (opt6
.ip6e_len
+ 2) * 4;
8789 off
+= (opt6
.ip6e_len
+ 1) * 8;
8790 nxt
= opt6
.ip6e_nxt
;
8791 /* goto the next header */
8798 } while (!terminal
);
8800 /* if there's no routing header, use unmodified mbuf for checksumming */
8810 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8811 &action
, &reason
, AF_INET6
)) {
8812 log
= action
!= PF_PASS
;
8815 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8817 /* Traffic goes through dummynet first */
8818 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8819 if (action
== PF_DROP
|| m
== NULL
) {
8823 #endif /* DUMMYNET */
8824 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8827 PF_APPLE_UPDATE_PDESC_IPv6();
8828 if (action
== PF_DROP
)
8830 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8834 PF_APPLE_UPDATE_PDESC_IPv6();
8835 if (action
== PF_PASS
) {
8837 pfsync_update_state(s
);
8838 #endif /* NPFSYNC */
8842 } else if (s
== NULL
)
8843 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8844 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8852 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8853 &action
, &reason
, AF_INET6
)) {
8854 log
= action
!= PF_PASS
;
8857 if (uh
.uh_dport
== 0 ||
8858 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8859 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8861 REASON_SET(&reason
, PFRES_SHORT
);
8865 /* Traffic goes through dummynet first */
8866 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8867 if (action
== PF_DROP
|| m
== NULL
) {
8871 #endif /* DUMMYNET */
8872 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8876 PF_APPLE_UPDATE_PDESC_IPv6();
8877 if (action
== PF_PASS
) {
8879 pfsync_update_state(s
);
8880 #endif /* NPFSYNC */
8884 } else if (s
== NULL
)
8885 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8886 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8890 case IPPROTO_ICMPV6
: {
8891 struct icmp6_hdr ih
;
8894 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
8895 &action
, &reason
, AF_INET6
)) {
8896 log
= action
!= PF_PASS
;
8900 /* Traffic goes through dummynet first */
8901 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8902 if (action
== PF_DROP
|| m
== NULL
) {
8906 #endif /* DUMMYNET */
8907 action
= pf_test_state_icmp(&s
, dir
, kif
,
8908 m
, off
, h
, &pd
, &reason
);
8911 PF_APPLE_UPDATE_PDESC_IPv6();
8912 if (action
== PF_PASS
) {
8914 pfsync_update_state(s
);
8915 #endif /* NPFSYNC */
8919 } else if (s
== NULL
)
8920 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8921 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8926 struct pf_esp_hdr esp
;
8929 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8931 log
= action
!= PF_PASS
;
8935 /* Traffic goes through dummynet first */
8936 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8937 if (action
== PF_DROP
|| m
== NULL
) {
8941 #endif /* DUMMYNET */
8942 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8945 PF_APPLE_UPDATE_PDESC_IPv6();
8946 if (action
== PF_PASS
) {
8948 pfsync_update_state(s
);
8949 #endif /* NPFSYNC */
8953 } else if (s
== NULL
)
8954 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8955 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
8960 struct pf_grev1_hdr grev1
;
8962 pd
.hdr
.grev1
= &grev1
;
8963 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8964 &reason
, AF_INET6
)) {
8965 log
= (action
!= PF_PASS
);
8969 /* Traffic goes through dummynet first */
8970 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8971 if (action
== PF_DROP
|| m
== NULL
) {
8975 #endif /* DUMMYNET */
8976 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8977 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8978 if (ntohs(grev1
.payload_length
) >
8979 m
->m_pkthdr
.len
- off
) {
8981 REASON_SET(&reason
, PFRES_SHORT
);
8984 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8987 PF_APPLE_UPDATE_PDESC_IPv6();
8988 if (action
== PF_PASS
) {
8990 pfsync_update_state(s
);
8991 #endif /* NPFSYNC */
8996 } else if (s
== NULL
) {
8997 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8998 h
, &pd
, &a
, &ruleset
, NULL
);
8999 if (action
== PF_PASS
)
9004 /* not GREv1/PPTP, so treat as ordinary GRE... */
9009 /* Traffic goes through dummynet first */
9010 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9011 if (action
== PF_DROP
|| m
== NULL
) {
9015 #endif /* DUMMYNET */
9016 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
9019 PF_APPLE_UPDATE_PDESC_IPv6();
9020 if (action
== PF_PASS
) {
9022 pfsync_update_state(s
);
9023 #endif /* NPFSYNC */
9027 } else if (s
== NULL
)
9028 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9029 &pd
, &a
, &ruleset
, NULL
);
9035 PF_APPLE_UPDATE_PDESC_IPv6();
9042 /* handle dangerous IPv6 extension headers. */
9043 if (action
== PF_PASS
&& rh_cnt
&&
9044 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9046 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9048 DPFPRINTF(PF_DEBUG_MISC
,
9049 ("pf: dropping packet with dangerous v6 headers\n"));
9052 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
9053 (pd
.pktflags
& PKTF_FLOW_ID
))
9054 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9057 if (action
== PF_PASS
) {
9059 if (altq_allowed
&& r
->qid
) {
9060 if (pd
.tos
& IPTOS_LOWDELAY
)
9061 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
9063 pd
.pf_mtag
->pftag_qid
= r
->qid
;
9065 #endif /* PF_ALTQ */
9067 /* add hints for ecn */
9068 pd
.pf_mtag
->pftag_hdr
= h
;
9069 /* record address family */
9070 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET
;
9071 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
9073 /* record protocol */
9074 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
9077 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
9078 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9079 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9080 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9081 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
9082 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9087 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9088 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9089 lr
= s
->nat_rule
.ptr
;
9092 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
9096 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9097 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
9099 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9100 dirndx
= (dir
== PF_OUT
);
9101 r
->packets
[dirndx
]++;
9102 r
->bytes
[dirndx
] += pd
.tot_len
;
9104 a
->packets
[dirndx
]++;
9105 a
->bytes
[dirndx
] += pd
.tot_len
;
9109 if (s
->nat_rule
.ptr
!= NULL
) {
9110 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9111 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9113 if (s
->src_node
!= NULL
) {
9114 s
->src_node
->packets
[dirndx
]++;
9115 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9117 if (s
->nat_src_node
!= NULL
) {
9118 s
->nat_src_node
->packets
[dirndx
]++;
9119 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9121 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9122 s
->packets
[dirndx
]++;
9123 s
->bytes
[dirndx
] += pd
.tot_len
;
9126 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9130 * XXX: we need to make sure that the addresses
9131 * passed to pfr_update_stats() are the same than
9132 * the addresses used during matching (pfr_match)
9134 if (r
== &pf_default_rule
) {
9136 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9137 &pd
.baddr
: &pd
.naddr
;
9139 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9140 &pd
.naddr
: &pd
.baddr
;
9142 if (x
== &pd
.baddr
|| s
== NULL
) {
9149 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9150 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9151 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
9152 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9154 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9155 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9156 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9157 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9162 if (action
== PF_SYNPROXY_DROP
) {
9167 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9168 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9170 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
9174 REASON_SET(&reason
, PFRES_MEMORY
);
9178 if (action
== PF_DROP
) {
9187 if (action
== PF_SYNPROXY_DROP
) {
9192 if (action
== PF_PASS
) {
9194 h
= mtod(m
, struct ip6_hdr
*);
9197 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9198 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9207 pf_check_congestion(struct ifqueue
*ifq
)
9214 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
9215 int flags
, const char *wchan
, void *palloc
)
9217 #pragma unused(align, ioff, flags, palloc)
9218 bzero(pp
, sizeof (*pp
));
9219 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
9220 if (pp
->pool_zone
!= NULL
) {
9221 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
9222 zone_change(pp
->pool_zone
, Z_CALLERACCT
, FALSE
);
9223 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
9224 pp
->pool_name
= wchan
;
9228 /* Zones cannot be currently destroyed */
9230 pool_destroy(struct pool
*pp
)
9236 pool_sethiwat(struct pool
*pp
, int n
)
9238 pp
->pool_hiwat
= n
; /* Currently unused */
9242 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
9244 #pragma unused(warnmess, ratecap)
9249 pool_get(struct pool
*pp
, int flags
)
9253 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9255 if (pp
->pool_count
> pp
->pool_limit
) {
9256 DPFPRINTF(PF_DEBUG_NOISY
,
9257 ("pf: pool %s hard limit reached (%d)\n",
9258 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
9264 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
9267 VERIFY(pp
->pool_count
!= 0);
9273 pool_put(struct pool
*pp
, void *v
)
9275 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9277 zfree(pp
->pool_zone
, v
);
9278 VERIFY(pp
->pool_count
!= 0);
9283 pf_find_mtag(struct mbuf
*m
)
9285 if (!(m
->m_flags
& M_PKTHDR
))
9288 return (m_pftag(m
));
9292 pf_get_mtag(struct mbuf
*m
)
9294 return (pf_find_mtag(m
));
9298 pf_time_second(void)
9307 pf_calendar_time_second(void)
9316 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
9318 struct hook_desc
*hd
;
9320 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
9327 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
9329 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
9335 hook_runloop(struct hook_desc_head
*head
, int flags
)
9337 struct hook_desc
*hd
;
9339 if (!(flags
& HOOK_REMOVE
)) {
9340 if (!(flags
& HOOK_ABORT
))
9341 TAILQ_FOREACH(hd
, head
, hd_list
)
9342 hd
->hd_fn(hd
->hd_arg
);
9344 while (!!(hd
= TAILQ_FIRST(head
))) {
9345 TAILQ_REMOVE(head
, hd
, hd_list
);
9346 if (!(flags
& HOOK_ABORT
))
9347 hd
->hd_fn(hd
->hd_arg
);
9348 if (flags
& HOOK_FREE
)
9349 _FREE(hd
, M_DEVBUF
);