2 * Copyright (c) 2007-2016 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 - 2013 Henning Brauer
35 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * - Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * - Redistributions in binary form must reproduce the above
45 * copyright notice, this list of conditions and the following
46 * disclaimer in the documentation and/or other materials provided
47 * with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
53 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
62 * Effort sponsored in part by the Defense Advanced Research Projects
63 * Agency (DARPA) and Air Force Research Laboratory, Air Force
64 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
68 #include <machine/endian.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/filio.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/kernel.h>
78 #include <sys/random.h>
79 #include <sys/mcache.h>
80 #include <sys/protosw.h>
82 #include <libkern/crypto/md5.h>
83 #include <libkern/libkern.h>
85 #include <mach/thread_act.h>
88 #include <net/if_types.h>
90 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/tcp.h>
99 #include <netinet/tcp_seq.h>
100 #include <netinet/udp.h>
101 #include <netinet/ip_icmp.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/tcp_timer.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet/tcp_fsm.h>
106 #include <netinet/udp_var.h>
107 #include <netinet/icmp_var.h>
108 #include <net/if_ether.h>
109 #include <net/ethernet.h>
110 #include <net/flowhash.h>
111 #include <net/pfvar.h>
112 #include <net/if_pflog.h>
115 #include <net/if_pfsync.h>
119 #include <netinet/ip6.h>
120 #include <netinet6/in6_pcb.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet/icmp6.h>
123 #include <netinet6/nd6.h>
127 #include <netinet/ip_dummynet.h>
128 #endif /* DUMMYNET */
131 * For RandomULong(), to get a 32 bits random value
132 * Note that random() returns a 31 bits value, see rdar://11159750
134 #include <dev/random/randomdev.h>
136 #define DPFPRINTF(n, x) (pf_status.debug >= (n) ? printf x : ((void)0))
139 * On Mac OS X, the rtableid value is treated as the interface scope
140 * value that is equivalent to the interface index used for scoped
141 * routing. A valid scope value is anything but IFSCOPE_NONE (0),
142 * as per definition of ifindex which is a positive, non-zero number.
143 * The other BSDs treat a negative rtableid value as invalid, hence
144 * the test against INT_MAX to handle userland apps which initialize
145 * the field with a negative number.
147 #define PF_RTABLEID_IS_VALID(r) \
148 ((r) > IFSCOPE_NONE && (r) <= INT_MAX)
153 decl_lck_mtx_data(,pf_lock_data
);
154 decl_lck_rw_data(,pf_perim_lock_data
);
155 lck_mtx_t
*pf_lock
= &pf_lock_data
;
156 lck_rw_t
*pf_perim_lock
= &pf_perim_lock_data
;
159 struct pf_state_tree_lan_ext pf_statetbl_lan_ext
;
160 struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy
;
162 struct pf_palist pf_pabuf
;
163 struct pf_status pf_status
;
166 struct pf_altqqueue pf_altqs
[2];
167 struct pf_altqqueue
*pf_altqs_active
;
168 struct pf_altqqueue
*pf_altqs_inactive
;
169 u_int32_t ticket_altqs_active
;
170 u_int32_t ticket_altqs_inactive
;
171 int altqs_inactive_open
;
173 u_int32_t ticket_pabuf
;
175 static MD5_CTX pf_tcp_secret_ctx
;
176 static u_char pf_tcp_secret
[16];
177 static int pf_tcp_secret_init
;
178 static int pf_tcp_iss_off
;
180 static struct pf_anchor_stackframe
{
181 struct pf_ruleset
*rs
;
183 struct pf_anchor_node
*parent
;
184 struct pf_anchor
*child
;
185 } pf_anchor_stack
[64];
187 struct pool pf_src_tree_pl
, pf_rule_pl
, pf_pooladdr_pl
;
188 struct pool pf_state_pl
, pf_state_key_pl
;
190 struct pool pf_altq_pl
;
193 typedef void (*hook_fn_t
)(void *);
196 TAILQ_ENTRY(hook_desc
) hd_list
;
201 #define HOOK_REMOVE 0x01
202 #define HOOK_FREE 0x02
203 #define HOOK_ABORT 0x04
205 static void *hook_establish(struct hook_desc_head
*, int,
207 static void hook_runloop(struct hook_desc_head
*, int flags
);
209 struct pool pf_app_state_pl
;
210 static void pf_print_addr(struct pf_addr
*addr
, sa_family_t af
);
211 static void pf_print_sk_host(struct pf_state_host
*, u_int8_t
, int,
214 static void pf_print_host(struct pf_addr
*, u_int16_t
, u_int8_t
);
216 static void pf_init_threshold(struct pf_threshold
*, u_int32_t
,
218 static void pf_add_threshold(struct pf_threshold
*);
219 static int pf_check_threshold(struct pf_threshold
*);
221 static void pf_change_ap(int, struct mbuf
*, struct pf_addr
*,
222 u_int16_t
*, u_int16_t
*, u_int16_t
*,
223 struct pf_addr
*, u_int16_t
, u_int8_t
, sa_family_t
,
225 static int pf_modulate_sack(struct mbuf
*, int, struct pf_pdesc
*,
226 struct tcphdr
*, struct pf_state_peer
*);
228 static void pf_change_a6(struct pf_addr
*, u_int16_t
*,
229 struct pf_addr
*, u_int8_t
);
230 void pf_change_addr(struct pf_addr
*a
, u_int16_t
*c
,
231 struct pf_addr
*an
, u_int8_t u
,
232 sa_family_t af
, sa_family_t afn
);
234 static void pf_change_icmp(struct pf_addr
*, u_int16_t
*,
235 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
236 u_int16_t
*, u_int16_t
*, u_int16_t
*,
237 u_int16_t
*, u_int8_t
, sa_family_t
);
238 static void pf_send_tcp(const struct pf_rule
*, sa_family_t
,
239 const struct pf_addr
*, const struct pf_addr
*,
240 u_int16_t
, u_int16_t
, u_int32_t
, u_int32_t
,
241 u_int8_t
, u_int16_t
, u_int16_t
, u_int8_t
, int,
242 u_int16_t
, struct ether_header
*, struct ifnet
*);
243 static void pf_send_icmp(struct mbuf
*, u_int8_t
, u_int8_t
,
244 sa_family_t
, struct pf_rule
*);
245 static struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
246 int, int, struct pfi_kif
*, struct pf_addr
*,
247 union pf_state_xport
*, struct pf_addr
*,
248 union pf_state_xport
*, int);
249 static struct pf_rule
*pf_get_translation_aux(struct pf_pdesc
*,
250 struct mbuf
*, int, int, struct pfi_kif
*,
251 struct pf_src_node
**, struct pf_addr
*,
252 union pf_state_xport
*, struct pf_addr
*,
253 union pf_state_xport
*, union pf_state_xport
*);
254 static void pf_attach_state(struct pf_state_key
*,
255 struct pf_state
*, int);
256 static void pf_detach_state(struct pf_state
*, int);
257 static u_int32_t
pf_tcp_iss(struct pf_pdesc
*);
258 static int pf_test_rule(struct pf_rule
**, struct pf_state
**,
259 int, struct pfi_kif
*, struct mbuf
*, int,
260 void *, struct pf_pdesc
*, struct pf_rule
**,
261 struct pf_ruleset
**, struct ifqueue
*);
263 static int pf_test_dummynet(struct pf_rule
**, int,
264 struct pfi_kif
*, struct mbuf
**,
265 struct pf_pdesc
*, struct ip_fw_args
*);
266 #endif /* DUMMYNET */
267 static int pf_test_fragment(struct pf_rule
**, int,
268 struct pfi_kif
*, struct mbuf
*, void *,
269 struct pf_pdesc
*, struct pf_rule
**,
270 struct pf_ruleset
**);
271 static int pf_test_state_tcp(struct pf_state
**, int,
272 struct pfi_kif
*, struct mbuf
*, int,
273 void *, struct pf_pdesc
*, u_short
*);
274 static int pf_test_state_udp(struct pf_state
**, int,
275 struct pfi_kif
*, struct mbuf
*, int,
276 void *, struct pf_pdesc
*, u_short
*);
277 static int pf_test_state_icmp(struct pf_state
**, int,
278 struct pfi_kif
*, struct mbuf
*, int,
279 void *, struct pf_pdesc
*, u_short
*);
280 static int pf_test_state_other(struct pf_state
**, int,
281 struct pfi_kif
*, struct pf_pdesc
*);
282 static int pf_match_tag(struct mbuf
*, struct pf_rule
*,
283 struct pf_mtag
*, int *);
284 static void pf_hash(struct pf_addr
*, struct pf_addr
*,
285 struct pf_poolhashkey
*, sa_family_t
);
286 static int pf_map_addr(u_int8_t
, struct pf_rule
*,
287 struct pf_addr
*, struct pf_addr
*,
288 struct pf_addr
*, struct pf_src_node
**);
289 static int pf_get_sport(struct pf_pdesc
*, struct pfi_kif
*,
290 struct pf_rule
*, struct pf_addr
*,
291 union pf_state_xport
*, struct pf_addr
*,
292 union pf_state_xport
*, struct pf_addr
*,
293 union pf_state_xport
*, struct pf_src_node
**);
294 static void pf_route(struct mbuf
**, struct pf_rule
*, int,
295 struct ifnet
*, struct pf_state
*,
298 static void pf_route6(struct mbuf
**, struct pf_rule
*, int,
299 struct ifnet
*, struct pf_state
*,
302 static u_int8_t
pf_get_wscale(struct mbuf
*, int, u_int16_t
,
304 static u_int16_t
pf_get_mss(struct mbuf
*, int, u_int16_t
,
306 static u_int16_t
pf_calc_mss(struct pf_addr
*, sa_family_t
,
308 static void pf_set_rt_ifp(struct pf_state
*,
309 struct pf_addr
*, sa_family_t af
);
310 static int pf_check_proto_cksum(struct mbuf
*, int, int,
311 u_int8_t
, sa_family_t
);
312 static int pf_addr_wrap_neq(struct pf_addr_wrap
*,
313 struct pf_addr_wrap
*);
314 static struct pf_state
*pf_find_state(struct pfi_kif
*,
315 struct pf_state_key_cmp
*, u_int
);
316 static int pf_src_connlimit(struct pf_state
**);
317 static void pf_stateins_err(const char *, struct pf_state
*,
319 static int pf_check_congestion(struct ifqueue
*);
322 static const char *pf_pptp_ctrl_type_name(u_int16_t code
);
324 static void pf_pptp_handler(struct pf_state
*, int, int,
325 struct pf_pdesc
*, struct pfi_kif
*);
326 static void pf_pptp_unlink(struct pf_state
*);
327 static void pf_grev1_unlink(struct pf_state
*);
328 static int pf_test_state_grev1(struct pf_state
**, int,
329 struct pfi_kif
*, int, struct pf_pdesc
*);
330 static int pf_ike_compare(struct pf_app_state
*,
331 struct pf_app_state
*);
332 static int pf_test_state_esp(struct pf_state
**, int,
333 struct pfi_kif
*, int, struct pf_pdesc
*);
335 extern struct pool pfr_ktable_pl
;
336 extern struct pool pfr_kentry_pl
;
337 extern int path_mtu_discovery
;
339 struct pf_pool_limit pf_pool_limits
[PF_LIMIT_MAX
] = {
340 { &pf_state_pl
, PFSTATE_HIWAT
},
341 { &pf_app_state_pl
, PFAPPSTATE_HIWAT
},
342 { &pf_src_tree_pl
, PFSNODE_HIWAT
},
343 { &pf_frent_pl
, PFFRAG_FRENT_HIWAT
},
344 { &pfr_ktable_pl
, PFR_KTABLE_HIWAT
},
345 { &pfr_kentry_pl
, PFR_KENTRY_HIWAT
},
349 pf_lazy_makewritable(struct pf_pdesc
*pd
, struct mbuf
*m
, int len
)
357 if (m_makewritable(&m
, 0, len
, M_DONTWAIT
))
360 if (len
>= 0 && m
!= pd
->mp
) {
362 pd
->pf_mtag
= pf_find_mtag(m
);
366 struct ip
*h
= mtod(m
, struct ip
*);
367 pd
->src
= (struct pf_addr
*)&h
->ip_src
;
368 pd
->dst
= (struct pf_addr
*)&h
->ip_dst
;
369 pd
->ip_sum
= &h
->ip_sum
;
374 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
375 pd
->src
= (struct pf_addr
*)&h
->ip6_src
;
376 pd
->dst
= (struct pf_addr
*)&h
->ip6_dst
;
384 return (len
< 0 ? 0 : m
);
388 pf_state_lookup_aux(struct pf_state
**state
, struct pfi_kif
*kif
,
389 int direction
, int *action
)
391 if (*state
== NULL
|| (*state
)->timeout
== PFTM_PURGE
) {
396 if (direction
== PF_OUT
&&
397 (((*state
)->rule
.ptr
->rt
== PF_ROUTETO
&&
398 (*state
)->rule
.ptr
->direction
== PF_OUT
) ||
399 ((*state
)->rule
.ptr
->rt
== PF_REPLYTO
&&
400 (*state
)->rule
.ptr
->direction
== PF_IN
)) &&
401 (*state
)->rt_kif
!= NULL
&& (*state
)->rt_kif
!= kif
) {
409 #define STATE_LOOKUP() \
412 *state = pf_find_state(kif, &key, direction); \
413 if (*state != NULL && pd != NULL && \
414 !(pd->pktflags & PKTF_FLOW_ID)) { \
415 pd->flowsrc = (*state)->state_key->flowsrc; \
416 pd->flowhash = (*state)->state_key->flowhash; \
417 if (pd->flowhash != 0) { \
418 pd->pktflags |= PKTF_FLOW_ID; \
419 pd->pktflags &= ~PKTF_FLOW_ADV; \
422 if (pf_state_lookup_aux(state, kif, direction, &action)) \
426 #define STATE_ADDR_TRANSLATE(sk) \
427 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
428 ((sk)->af_lan == AF_INET6 && \
429 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
430 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
431 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3]))
433 #define STATE_TRANSLATE(sk) \
434 ((sk)->af_lan != (sk)->af_gwy || \
435 STATE_ADDR_TRANSLATE(sk) || \
436 (sk)->lan.xport.port != (sk)->gwy.xport.port)
438 #define STATE_GRE_TRANSLATE(sk) \
439 (STATE_ADDR_TRANSLATE(sk) || \
440 (sk)->lan.xport.call_id != (sk)->gwy.xport.call_id)
442 #define BOUND_IFACE(r, k) \
443 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
445 #define STATE_INC_COUNTERS(s) \
447 s->rule.ptr->states++; \
448 VERIFY(s->rule.ptr->states != 0); \
449 if (s->anchor.ptr != NULL) { \
450 s->anchor.ptr->states++; \
451 VERIFY(s->anchor.ptr->states != 0); \
453 if (s->nat_rule.ptr != NULL) { \
454 s->nat_rule.ptr->states++; \
455 VERIFY(s->nat_rule.ptr->states != 0); \
459 #define STATE_DEC_COUNTERS(s) \
461 if (s->nat_rule.ptr != NULL) { \
462 VERIFY(s->nat_rule.ptr->states > 0); \
463 s->nat_rule.ptr->states--; \
465 if (s->anchor.ptr != NULL) { \
466 VERIFY(s->anchor.ptr->states > 0); \
467 s->anchor.ptr->states--; \
469 VERIFY(s->rule.ptr->states > 0); \
470 s->rule.ptr->states--; \
473 static __inline
int pf_src_compare(struct pf_src_node
*, struct pf_src_node
*);
474 static __inline
int pf_state_compare_lan_ext(struct pf_state_key
*,
475 struct pf_state_key
*);
476 static __inline
int pf_state_compare_ext_gwy(struct pf_state_key
*,
477 struct pf_state_key
*);
478 static __inline
int pf_state_compare_id(struct pf_state
*,
481 struct pf_src_tree tree_src_tracking
;
483 struct pf_state_tree_id tree_id
;
484 struct pf_state_queue state_list
;
486 RB_GENERATE(pf_src_tree
, pf_src_node
, entry
, pf_src_compare
);
487 RB_GENERATE(pf_state_tree_lan_ext
, pf_state_key
,
488 entry_lan_ext
, pf_state_compare_lan_ext
);
489 RB_GENERATE(pf_state_tree_ext_gwy
, pf_state_key
,
490 entry_ext_gwy
, pf_state_compare_ext_gwy
);
491 RB_GENERATE(pf_state_tree_id
, pf_state
,
492 entry_id
, pf_state_compare_id
);
494 #define PF_DT_SKIP_LANEXT 0x01
495 #define PF_DT_SKIP_EXTGWY 0x02
497 static const u_int16_t PF_PPTP_PORT
= 1723;
498 static const u_int32_t PF_PPTP_MAGIC_NUMBER
= 0x1A2B3C4D;
506 struct pf_pptp_ctrl_hdr
{
508 u_int16_t reserved_0
;
511 struct pf_pptp_ctrl_generic
{
515 #define PF_PPTP_CTRL_TYPE_START_REQ 1
516 struct pf_pptp_ctrl_start_req
{
517 u_int16_t protocol_version
;
518 u_int16_t reserved_1
;
519 u_int32_t framing_capabilities
;
520 u_int32_t bearer_capabilities
;
521 u_int16_t maximum_channels
;
522 u_int16_t firmware_revision
;
523 u_int8_t host_name
[64];
524 u_int8_t vendor_string
[64];
527 #define PF_PPTP_CTRL_TYPE_START_RPY 2
528 struct pf_pptp_ctrl_start_rpy
{
529 u_int16_t protocol_version
;
530 u_int8_t result_code
;
532 u_int32_t framing_capabilities
;
533 u_int32_t bearer_capabilities
;
534 u_int16_t maximum_channels
;
535 u_int16_t firmware_revision
;
536 u_int8_t host_name
[64];
537 u_int8_t vendor_string
[64];
540 #define PF_PPTP_CTRL_TYPE_STOP_REQ 3
541 struct pf_pptp_ctrl_stop_req
{
544 u_int16_t reserved_2
;
547 #define PF_PPTP_CTRL_TYPE_STOP_RPY 4
548 struct pf_pptp_ctrl_stop_rpy
{
551 u_int16_t reserved_1
;
554 #define PF_PPTP_CTRL_TYPE_ECHO_REQ 5
555 struct pf_pptp_ctrl_echo_req
{
556 u_int32_t identifier
;
559 #define PF_PPTP_CTRL_TYPE_ECHO_RPY 6
560 struct pf_pptp_ctrl_echo_rpy
{
561 u_int32_t identifier
;
562 u_int8_t result_code
;
564 u_int16_t reserved_1
;
567 #define PF_PPTP_CTRL_TYPE_CALL_OUT_REQ 7
568 struct pf_pptp_ctrl_call_out_req
{
570 u_int16_t call_sernum
;
572 u_int32_t bearer_type
;
573 u_int32_t framing_type
;
574 u_int16_t rxwindow_size
;
575 u_int16_t proc_delay
;
576 u_int8_t phone_num
[64];
577 u_int8_t sub_addr
[64];
580 #define PF_PPTP_CTRL_TYPE_CALL_OUT_RPY 8
581 struct pf_pptp_ctrl_call_out_rpy
{
583 u_int16_t peer_call_id
;
584 u_int8_t result_code
;
586 u_int16_t cause_code
;
587 u_int32_t connect_speed
;
588 u_int16_t rxwindow_size
;
589 u_int16_t proc_delay
;
590 u_int32_t phy_channel_id
;
593 #define PF_PPTP_CTRL_TYPE_CALL_IN_1ST 9
594 struct pf_pptp_ctrl_call_in_1st
{
596 u_int16_t call_sernum
;
597 u_int32_t bearer_type
;
598 u_int32_t phy_channel_id
;
599 u_int16_t dialed_number_len
;
600 u_int16_t dialing_number_len
;
601 u_int8_t dialed_num
[64];
602 u_int8_t dialing_num
[64];
603 u_int8_t sub_addr
[64];
606 #define PF_PPTP_CTRL_TYPE_CALL_IN_2ND 10
607 struct pf_pptp_ctrl_call_in_2nd
{
609 u_int16_t peer_call_id
;
610 u_int8_t result_code
;
612 u_int16_t rxwindow_size
;
614 u_int16_t reserved_1
;
617 #define PF_PPTP_CTRL_TYPE_CALL_IN_3RD 11
618 struct pf_pptp_ctrl_call_in_3rd
{
620 u_int16_t reserved_1
;
621 u_int32_t connect_speed
;
622 u_int16_t rxwindow_size
;
624 u_int32_t framing_type
;
627 #define PF_PPTP_CTRL_TYPE_CALL_CLR 12
628 struct pf_pptp_ctrl_call_clr
{
630 u_int16_t reserved_1
;
633 #define PF_PPTP_CTRL_TYPE_CALL_DISC 13
634 struct pf_pptp_ctrl_call_disc
{
636 u_int8_t result_code
;
638 u_int16_t cause_code
;
639 u_int16_t reserved_1
;
640 u_int8_t statistics
[128];
643 #define PF_PPTP_CTRL_TYPE_ERROR 14
644 struct pf_pptp_ctrl_error
{
645 u_int16_t peer_call_id
;
646 u_int16_t reserved_1
;
647 u_int32_t crc_errors
;
650 u_int32_t buf_errors
;
651 u_int32_t tim_errors
;
652 u_int32_t align_errors
;
655 #define PF_PPTP_CTRL_TYPE_SET_LINKINFO 15
656 struct pf_pptp_ctrl_set_linkinfo
{
657 u_int16_t peer_call_id
;
658 u_int16_t reserved_1
;
664 static const char *pf_pptp_ctrl_type_name(u_int16_t code
)
668 if (code
< PF_PPTP_CTRL_TYPE_START_REQ
||
669 code
> PF_PPTP_CTRL_TYPE_SET_LINKINFO
) {
670 static char reserved
[] = "reserved-00";
672 sprintf(&reserved
[9], "%02x", code
);
675 static const char *name
[] = {
676 "start_req", "start_rpy", "stop_req", "stop_rpy",
677 "echo_req", "echo_rpy", "call_out_req", "call_out_rpy",
678 "call_in_1st", "call_in_2nd", "call_in_3rd",
679 "call_clr", "call_disc", "error", "set_linkinfo"
682 return (name
[code
- 1]);
687 static const size_t PF_PPTP_CTRL_MSG_MINSIZE
=
688 sizeof (struct pf_pptp_hdr
) + sizeof (struct pf_pptp_ctrl_hdr
);
690 union pf_pptp_ctrl_msg_union
{
691 struct pf_pptp_ctrl_start_req start_req
;
692 struct pf_pptp_ctrl_start_rpy start_rpy
;
693 struct pf_pptp_ctrl_stop_req stop_req
;
694 struct pf_pptp_ctrl_stop_rpy stop_rpy
;
695 struct pf_pptp_ctrl_echo_req echo_req
;
696 struct pf_pptp_ctrl_echo_rpy echo_rpy
;
697 struct pf_pptp_ctrl_call_out_req call_out_req
;
698 struct pf_pptp_ctrl_call_out_rpy call_out_rpy
;
699 struct pf_pptp_ctrl_call_in_1st call_in_1st
;
700 struct pf_pptp_ctrl_call_in_2nd call_in_2nd
;
701 struct pf_pptp_ctrl_call_in_3rd call_in_3rd
;
702 struct pf_pptp_ctrl_call_clr call_clr
;
703 struct pf_pptp_ctrl_call_disc call_disc
;
704 struct pf_pptp_ctrl_error error
;
705 struct pf_pptp_ctrl_set_linkinfo set_linkinfo
;
709 struct pf_pptp_ctrl_msg
{
710 struct pf_pptp_hdr hdr
;
711 struct pf_pptp_ctrl_hdr ctrl
;
712 union pf_pptp_ctrl_msg_union msg
;
715 #define PF_GRE_FLAG_CHECKSUM_PRESENT 0x8000
716 #define PF_GRE_FLAG_VERSION_MASK 0x0007
717 #define PF_GRE_PPP_ETHERTYPE 0x880B
719 struct pf_grev1_hdr
{
721 u_int16_t protocol_type
;
722 u_int16_t payload_length
;
730 static const u_int16_t PF_IKE_PORT
= 500;
733 u_int64_t initiator_cookie
, responder_cookie
;
734 u_int8_t next_payload
, version
, exchange_type
, flags
;
735 u_int32_t message_id
, length
;
738 #define PF_IKE_PACKET_MINSIZE (sizeof (struct pf_ike_hdr))
740 #define PF_IKEv1_EXCHTYPE_BASE 1
741 #define PF_IKEv1_EXCHTYPE_ID_PROTECT 2
742 #define PF_IKEv1_EXCHTYPE_AUTH_ONLY 3
743 #define PF_IKEv1_EXCHTYPE_AGGRESSIVE 4
744 #define PF_IKEv1_EXCHTYPE_INFORMATIONAL 5
745 #define PF_IKEv2_EXCHTYPE_SA_INIT 34
746 #define PF_IKEv2_EXCHTYPE_AUTH 35
747 #define PF_IKEv2_EXCHTYPE_CREATE_CHILD_SA 36
748 #define PF_IKEv2_EXCHTYPE_INFORMATIONAL 37
750 #define PF_IKEv1_FLAG_E 0x01
751 #define PF_IKEv1_FLAG_C 0x02
752 #define PF_IKEv1_FLAG_A 0x04
753 #define PF_IKEv2_FLAG_I 0x08
754 #define PF_IKEv2_FLAG_V 0x10
755 #define PF_IKEv2_FLAG_R 0x20
764 pf_addr_compare(struct pf_addr
*a
, struct pf_addr
*b
, sa_family_t af
)
769 if (a
->addr32
[0] > b
->addr32
[0])
771 if (a
->addr32
[0] < b
->addr32
[0])
777 if (a
->addr32
[3] > b
->addr32
[3])
779 if (a
->addr32
[3] < b
->addr32
[3])
781 if (a
->addr32
[2] > b
->addr32
[2])
783 if (a
->addr32
[2] < b
->addr32
[2])
785 if (a
->addr32
[1] > b
->addr32
[1])
787 if (a
->addr32
[1] < b
->addr32
[1])
789 if (a
->addr32
[0] > b
->addr32
[0])
791 if (a
->addr32
[0] < b
->addr32
[0])
800 pf_src_compare(struct pf_src_node
*a
, struct pf_src_node
*b
)
804 if (a
->rule
.ptr
> b
->rule
.ptr
)
806 if (a
->rule
.ptr
< b
->rule
.ptr
)
808 if ((diff
= a
->af
- b
->af
) != 0)
810 if ((diff
= pf_addr_compare(&a
->addr
, &b
->addr
, a
->af
)) != 0)
816 pf_state_compare_lan_ext(struct pf_state_key
*a
, struct pf_state_key
*b
)
821 if ((diff
= a
->proto
- b
->proto
) != 0)
823 if ((diff
= a
->af_lan
- b
->af_lan
) != 0)
826 extfilter
= PF_EXTFILTER_APD
;
831 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
836 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
838 if ((diff
= a
->ext_lan
.xport
.port
- b
->ext_lan
.xport
.port
) != 0)
843 if ((diff
= a
->proto_variant
- b
->proto_variant
))
845 extfilter
= a
->proto_variant
;
846 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
848 if ((extfilter
< PF_EXTFILTER_AD
) &&
849 (diff
= a
->ext_lan
.xport
.port
- b
->ext_lan
.xport
.port
) != 0)
854 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
855 a
->proto_variant
== b
->proto_variant
) {
856 if (!!(diff
= a
->ext_lan
.xport
.call_id
-
857 b
->ext_lan
.xport
.call_id
))
863 if (!!(diff
= a
->ext_lan
.xport
.spi
- b
->ext_lan
.xport
.spi
))
874 if ((diff
= pf_addr_compare(&a
->lan
.addr
, &b
->lan
.addr
,
878 if (extfilter
< PF_EXTFILTER_EI
) {
879 if ((diff
= pf_addr_compare(&a
->ext_lan
.addr
,
888 if ((diff
= pf_addr_compare(&a
->lan
.addr
, &b
->lan
.addr
,
892 if (extfilter
< PF_EXTFILTER_EI
||
893 !PF_AZERO(&b
->ext_lan
.addr
, AF_INET6
)) {
894 if ((diff
= pf_addr_compare(&a
->ext_lan
.addr
,
903 if (a
->app_state
&& b
->app_state
) {
904 if (a
->app_state
->compare_lan_ext
&&
905 b
->app_state
->compare_lan_ext
) {
906 diff
= (const char *)b
->app_state
->compare_lan_ext
-
907 (const char *)a
->app_state
->compare_lan_ext
;
910 diff
= a
->app_state
->compare_lan_ext(a
->app_state
,
921 pf_state_compare_ext_gwy(struct pf_state_key
*a
, struct pf_state_key
*b
)
926 if ((diff
= a
->proto
- b
->proto
) != 0)
929 if ((diff
= a
->af_gwy
- b
->af_gwy
) != 0)
932 extfilter
= PF_EXTFILTER_APD
;
937 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
942 if ((diff
= a
->ext_gwy
.xport
.port
- b
->ext_gwy
.xport
.port
) != 0)
944 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
949 if ((diff
= a
->proto_variant
- b
->proto_variant
))
951 extfilter
= a
->proto_variant
;
952 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
954 if ((extfilter
< PF_EXTFILTER_AD
) &&
955 (diff
= a
->ext_gwy
.xport
.port
- b
->ext_gwy
.xport
.port
) != 0)
960 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
961 a
->proto_variant
== b
->proto_variant
) {
962 if (!!(diff
= a
->gwy
.xport
.call_id
-
963 b
->gwy
.xport
.call_id
))
969 if (!!(diff
= a
->gwy
.xport
.spi
- b
->gwy
.xport
.spi
))
980 if ((diff
= pf_addr_compare(&a
->gwy
.addr
, &b
->gwy
.addr
,
984 if (extfilter
< PF_EXTFILTER_EI
) {
985 if ((diff
= pf_addr_compare(&a
->ext_gwy
.addr
, &b
->ext_gwy
.addr
,
993 if ((diff
= pf_addr_compare(&a
->gwy
.addr
, &b
->gwy
.addr
,
997 if (extfilter
< PF_EXTFILTER_EI
||
998 !PF_AZERO(&b
->ext_gwy
.addr
, AF_INET6
)) {
999 if ((diff
= pf_addr_compare(&a
->ext_gwy
.addr
, &b
->ext_gwy
.addr
,
1007 if (a
->app_state
&& b
->app_state
) {
1008 if (a
->app_state
->compare_ext_gwy
&&
1009 b
->app_state
->compare_ext_gwy
) {
1010 diff
= (const char *)b
->app_state
->compare_ext_gwy
-
1011 (const char *)a
->app_state
->compare_ext_gwy
;
1014 diff
= a
->app_state
->compare_ext_gwy(a
->app_state
,
1025 pf_state_compare_id(struct pf_state
*a
, struct pf_state
*b
)
1031 if (a
->creatorid
> b
->creatorid
)
1033 if (a
->creatorid
< b
->creatorid
)
1041 pf_addrcpy(struct pf_addr
*dst
, struct pf_addr
*src
, sa_family_t af
)
1046 dst
->addr32
[0] = src
->addr32
[0];
1050 dst
->addr32
[0] = src
->addr32
[0];
1051 dst
->addr32
[1] = src
->addr32
[1];
1052 dst
->addr32
[2] = src
->addr32
[2];
1053 dst
->addr32
[3] = src
->addr32
[3];
1060 pf_find_state_byid(struct pf_state_cmp
*key
)
1062 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1064 return (RB_FIND(pf_state_tree_id
, &tree_id
,
1065 (struct pf_state
*)(void *)key
));
1068 static struct pf_state
*
1069 pf_find_state(struct pfi_kif
*kif
, struct pf_state_key_cmp
*key
, u_int dir
)
1071 struct pf_state_key
*sk
= NULL
;
1074 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1078 sk
= RB_FIND(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1079 (struct pf_state_key
*)key
);
1082 sk
= RB_FIND(pf_state_tree_ext_gwy
, &pf_statetbl_ext_gwy
,
1083 (struct pf_state_key
*)key
);
1085 * NAT64 is done only on input, for packets coming in from
1086 * from the LAN side, need to lookup the lan_ext tree.
1089 sk
= RB_FIND(pf_state_tree_lan_ext
,
1090 &pf_statetbl_lan_ext
,
1091 (struct pf_state_key
*)key
);
1092 if (sk
&& sk
->af_lan
== sk
->af_gwy
)
1097 panic("pf_find_state");
1100 /* list is sorted, if-bound states before floating ones */
1102 TAILQ_FOREACH(s
, &sk
->states
, next
)
1103 if (s
->kif
== pfi_all
|| s
->kif
== kif
)
1110 pf_find_state_all(struct pf_state_key_cmp
*key
, u_int dir
, int *more
)
1112 struct pf_state_key
*sk
= NULL
;
1113 struct pf_state
*s
, *ret
= NULL
;
1115 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1119 sk
= RB_FIND(pf_state_tree_lan_ext
,
1120 &pf_statetbl_lan_ext
, (struct pf_state_key
*)key
);
1123 sk
= RB_FIND(pf_state_tree_ext_gwy
,
1124 &pf_statetbl_ext_gwy
, (struct pf_state_key
*)key
);
1126 * NAT64 is done only on input, for packets coming in from
1127 * from the LAN side, need to lookup the lan_ext tree.
1129 if ((sk
== NULL
) && pf_nat64_configured
) {
1130 sk
= RB_FIND(pf_state_tree_lan_ext
,
1131 &pf_statetbl_lan_ext
,
1132 (struct pf_state_key
*)key
);
1133 if (sk
&& sk
->af_lan
== sk
->af_gwy
)
1138 panic("pf_find_state_all");
1142 ret
= TAILQ_FIRST(&sk
->states
);
1146 TAILQ_FOREACH(s
, &sk
->states
, next
)
1154 pf_init_threshold(struct pf_threshold
*threshold
,
1155 u_int32_t limit
, u_int32_t seconds
)
1157 threshold
->limit
= limit
* PF_THRESHOLD_MULT
;
1158 threshold
->seconds
= seconds
;
1159 threshold
->count
= 0;
1160 threshold
->last
= pf_time_second();
1164 pf_add_threshold(struct pf_threshold
*threshold
)
1166 u_int32_t t
= pf_time_second(), diff
= t
- threshold
->last
;
1168 if (diff
>= threshold
->seconds
)
1169 threshold
->count
= 0;
1171 threshold
->count
-= threshold
->count
* diff
/
1173 threshold
->count
+= PF_THRESHOLD_MULT
;
1174 threshold
->last
= t
;
1178 pf_check_threshold(struct pf_threshold
*threshold
)
1180 return (threshold
->count
> threshold
->limit
);
1184 pf_src_connlimit(struct pf_state
**state
)
1187 (*state
)->src_node
->conn
++;
1188 VERIFY((*state
)->src_node
->conn
!= 0);
1189 (*state
)->src
.tcp_est
= 1;
1190 pf_add_threshold(&(*state
)->src_node
->conn_rate
);
1192 if ((*state
)->rule
.ptr
->max_src_conn
&&
1193 (*state
)->rule
.ptr
->max_src_conn
<
1194 (*state
)->src_node
->conn
) {
1195 pf_status
.lcounters
[LCNT_SRCCONN
]++;
1199 if ((*state
)->rule
.ptr
->max_src_conn_rate
.limit
&&
1200 pf_check_threshold(&(*state
)->src_node
->conn_rate
)) {
1201 pf_status
.lcounters
[LCNT_SRCCONNRATE
]++;
1208 if ((*state
)->rule
.ptr
->overload_tbl
) {
1210 u_int32_t killed
= 0;
1212 pf_status
.lcounters
[LCNT_OVERLOAD_TABLE
]++;
1213 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1214 printf("pf_src_connlimit: blocking address ");
1215 pf_print_host(&(*state
)->src_node
->addr
, 0,
1216 (*state
)->state_key
->af_lan
);
1219 bzero(&p
, sizeof (p
));
1220 p
.pfra_af
= (*state
)->state_key
->af_lan
;
1221 switch ((*state
)->state_key
->af_lan
) {
1225 p
.pfra_ip4addr
= (*state
)->src_node
->addr
.v4
;
1231 p
.pfra_ip6addr
= (*state
)->src_node
->addr
.v6
;
1236 pfr_insert_kentry((*state
)->rule
.ptr
->overload_tbl
,
1237 &p
, pf_calendar_time_second());
1239 /* kill existing states if that's required. */
1240 if ((*state
)->rule
.ptr
->flush
) {
1241 struct pf_state_key
*sk
;
1242 struct pf_state
*st
;
1244 pf_status
.lcounters
[LCNT_OVERLOAD_FLUSH
]++;
1245 RB_FOREACH(st
, pf_state_tree_id
, &tree_id
) {
1248 * Kill states from this source. (Only those
1249 * from the same rule if PF_FLUSH_GLOBAL is not
1253 (*state
)->state_key
->af_lan
&&
1254 (((*state
)->state_key
->direction
==
1256 PF_AEQ(&(*state
)->src_node
->addr
,
1257 &sk
->lan
.addr
, sk
->af_lan
)) ||
1258 ((*state
)->state_key
->direction
== PF_IN
&&
1259 PF_AEQ(&(*state
)->src_node
->addr
,
1260 &sk
->ext_lan
.addr
, sk
->af_lan
))) &&
1261 ((*state
)->rule
.ptr
->flush
&
1263 (*state
)->rule
.ptr
== st
->rule
.ptr
)) {
1264 st
->timeout
= PFTM_PURGE
;
1265 st
->src
.state
= st
->dst
.state
=
1270 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1271 printf(", %u states killed", killed
);
1273 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1277 /* kill this state */
1278 (*state
)->timeout
= PFTM_PURGE
;
1279 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
1284 pf_insert_src_node(struct pf_src_node
**sn
, struct pf_rule
*rule
,
1285 struct pf_addr
*src
, sa_family_t af
)
1287 struct pf_src_node k
;
1291 PF_ACPY(&k
.addr
, src
, af
);
1292 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1293 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1297 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
1298 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
1301 if (!rule
->max_src_nodes
||
1302 rule
->src_nodes
< rule
->max_src_nodes
)
1303 (*sn
) = pool_get(&pf_src_tree_pl
, PR_WAITOK
);
1305 pf_status
.lcounters
[LCNT_SRCNODES
]++;
1308 bzero(*sn
, sizeof (struct pf_src_node
));
1310 pf_init_threshold(&(*sn
)->conn_rate
,
1311 rule
->max_src_conn_rate
.limit
,
1312 rule
->max_src_conn_rate
.seconds
);
1315 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1316 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1317 (*sn
)->rule
.ptr
= rule
;
1319 (*sn
)->rule
.ptr
= NULL
;
1320 PF_ACPY(&(*sn
)->addr
, src
, af
);
1321 if (RB_INSERT(pf_src_tree
,
1322 &tree_src_tracking
, *sn
) != NULL
) {
1323 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1324 printf("pf: src_tree insert failed: ");
1325 pf_print_host(&(*sn
)->addr
, 0, af
);
1328 pool_put(&pf_src_tree_pl
, *sn
);
1331 (*sn
)->creation
= pf_time_second();
1332 (*sn
)->ruletype
= rule
->action
;
1333 if ((*sn
)->rule
.ptr
!= NULL
)
1334 (*sn
)->rule
.ptr
->src_nodes
++;
1335 pf_status
.scounters
[SCNT_SRC_NODE_INSERT
]++;
1336 pf_status
.src_nodes
++;
1338 if (rule
->max_src_states
&&
1339 (*sn
)->states
>= rule
->max_src_states
) {
1340 pf_status
.lcounters
[LCNT_SRCSTATES
]++;
1348 pf_stateins_err(const char *tree
, struct pf_state
*s
, struct pfi_kif
*kif
)
1350 struct pf_state_key
*sk
= s
->state_key
;
1352 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1353 printf("pf: state insert failed: %s %s ", tree
, kif
->pfik_name
);
1354 switch (sk
->proto
) {
1364 case IPPROTO_ICMPV6
:
1368 printf("PROTO=%u", sk
->proto
);
1372 pf_print_sk_host(&sk
->lan
, sk
->af_lan
, sk
->proto
,
1375 pf_print_sk_host(&sk
->gwy
, sk
->af_gwy
, sk
->proto
,
1377 printf(" ext_lan: ");
1378 pf_print_sk_host(&sk
->ext_lan
, sk
->af_lan
, sk
->proto
,
1380 printf(" ext_gwy: ");
1381 pf_print_sk_host(&sk
->ext_gwy
, sk
->af_gwy
, sk
->proto
,
1383 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1384 printf(" (from sync)");
1390 pf_insert_state(struct pfi_kif
*kif
, struct pf_state
*s
)
1392 struct pf_state_key
*cur
;
1393 struct pf_state
*sp
;
1395 VERIFY(s
->state_key
!= NULL
);
1398 if ((cur
= RB_INSERT(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1399 s
->state_key
)) != NULL
) {
1400 /* key exists. check for same kif, if none, add to key */
1401 TAILQ_FOREACH(sp
, &cur
->states
, next
)
1402 if (sp
->kif
== kif
) { /* collision! */
1403 pf_stateins_err("tree_lan_ext", s
, kif
);
1405 PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1408 pf_detach_state(s
, PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1409 pf_attach_state(cur
, s
, kif
== pfi_all
? 1 : 0);
1412 /* if cur != NULL, we already found a state key and attached to it */
1413 if (cur
== NULL
&& (cur
= RB_INSERT(pf_state_tree_ext_gwy
,
1414 &pf_statetbl_ext_gwy
, s
->state_key
)) != NULL
) {
1415 /* must not happen. we must have found the sk above! */
1416 pf_stateins_err("tree_ext_gwy", s
, kif
);
1417 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
1421 if (s
->id
== 0 && s
->creatorid
== 0) {
1422 s
->id
= htobe64(pf_status
.stateid
++);
1423 s
->creatorid
= pf_status
.hostid
;
1425 if (RB_INSERT(pf_state_tree_id
, &tree_id
, s
) != NULL
) {
1426 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1427 printf("pf: state insert failed: "
1428 "id: %016llx creatorid: %08x",
1429 be64toh(s
->id
), ntohl(s
->creatorid
));
1430 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1431 printf(" (from sync)");
1434 pf_detach_state(s
, 0);
1437 TAILQ_INSERT_TAIL(&state_list
, s
, entry_list
);
1438 pf_status
.fcounters
[FCNT_STATE_INSERT
]++;
1440 VERIFY(pf_status
.states
!= 0);
1441 pfi_kif_ref(kif
, PFI_KIF_REF_STATE
);
1443 pfsync_insert_state(s
);
1449 pf_purge_thread_cont(int err
)
1452 static u_int32_t nloops
= 0;
1453 int t
= 1; /* 1 second */
1456 * Update coarse-grained networking timestamp (in sec.); the idea
1457 * is to piggy-back on the periodic timeout callout to update
1458 * the counter returnable via net_uptime().
1460 net_update_uptime();
1462 lck_rw_lock_shared(pf_perim_lock
);
1463 lck_mtx_lock(pf_lock
);
1465 /* purge everything if not running */
1466 if (!pf_status
.running
) {
1467 pf_purge_expired_states(pf_status
.states
);
1468 pf_purge_expired_fragments();
1469 pf_purge_expired_src_nodes();
1471 /* terminate thread (we don't currently do this) */
1472 if (pf_purge_thread
== NULL
) {
1473 lck_mtx_unlock(pf_lock
);
1474 lck_rw_done(pf_perim_lock
);
1476 thread_deallocate(current_thread());
1477 thread_terminate(current_thread());
1481 /* if there's nothing left, sleep w/o timeout */
1482 if (pf_status
.states
== 0 &&
1483 pf_normalize_isempty() &&
1484 RB_EMPTY(&tree_src_tracking
)) {
1492 /* process a fraction of the state table every second */
1493 pf_purge_expired_states(1 + (pf_status
.states
1494 / pf_default_rule
.timeout
[PFTM_INTERVAL
]));
1496 /* purge other expired types every PFTM_INTERVAL seconds */
1497 if (++nloops
>= pf_default_rule
.timeout
[PFTM_INTERVAL
]) {
1498 pf_purge_expired_fragments();
1499 pf_purge_expired_src_nodes();
1503 lck_mtx_unlock(pf_lock
);
1504 lck_rw_done(pf_perim_lock
);
1506 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge_cont",
1507 t
* hz
, pf_purge_thread_cont
);
1515 pf_purge_thread_fn(void *v
, wait_result_t w
)
1517 #pragma unused(v, w)
1518 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge", 0,
1519 pf_purge_thread_cont
);
1521 * tsleep0() shouldn't have returned as PCATCH was not set;
1522 * therefore assert in this case.
1528 pf_state_expires(const struct pf_state
*state
)
1535 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1537 /* handle all PFTM_* > PFTM_MAX here */
1538 if (state
->timeout
== PFTM_PURGE
)
1539 return (pf_time_second());
1541 VERIFY(state
->timeout
!= PFTM_UNLINKED
);
1542 VERIFY(state
->timeout
< PFTM_MAX
);
1543 t
= state
->rule
.ptr
->timeout
[state
->timeout
];
1545 t
= pf_default_rule
.timeout
[state
->timeout
];
1546 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
1548 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
1549 states
= state
->rule
.ptr
->states
;
1551 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
1552 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
1553 states
= pf_status
.states
;
1555 if (end
&& states
> start
&& start
< end
) {
1557 return (state
->expire
+ t
* (end
- states
) /
1560 return (pf_time_second());
1562 return (state
->expire
+ t
);
1566 pf_purge_expired_src_nodes(void)
1568 struct pf_src_node
*cur
, *next
;
1570 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1572 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
1573 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
1575 if (cur
->states
<= 0 && cur
->expire
<= pf_time_second()) {
1576 if (cur
->rule
.ptr
!= NULL
) {
1577 cur
->rule
.ptr
->src_nodes
--;
1578 if (cur
->rule
.ptr
->states
<= 0 &&
1579 cur
->rule
.ptr
->max_src_nodes
<= 0)
1580 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1582 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1583 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1584 pf_status
.src_nodes
--;
1585 pool_put(&pf_src_tree_pl
, cur
);
1591 pf_src_tree_remove_state(struct pf_state
*s
)
1595 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1597 if (s
->src_node
!= NULL
) {
1598 if (s
->src
.tcp_est
) {
1599 VERIFY(s
->src_node
->conn
> 0);
1600 --s
->src_node
->conn
;
1602 VERIFY(s
->src_node
->states
> 0);
1603 if (--s
->src_node
->states
<= 0) {
1604 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1606 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1607 s
->src_node
->expire
= pf_time_second() + t
;
1610 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1611 VERIFY(s
->nat_src_node
->states
> 0);
1612 if (--s
->nat_src_node
->states
<= 0) {
1613 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1615 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1616 s
->nat_src_node
->expire
= pf_time_second() + t
;
1619 s
->src_node
= s
->nat_src_node
= NULL
;
1623 pf_unlink_state(struct pf_state
*cur
)
1625 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1627 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1628 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af_lan
,
1629 &cur
->state_key
->ext_lan
.addr
, &cur
->state_key
->lan
.addr
,
1630 cur
->state_key
->ext_lan
.xport
.port
,
1631 cur
->state_key
->lan
.xport
.port
,
1632 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1633 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1636 hook_runloop(&cur
->unlink_hooks
, HOOK_REMOVE
|HOOK_FREE
);
1637 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1639 if (cur
->creatorid
== pf_status
.hostid
)
1640 pfsync_delete_state(cur
);
1642 cur
->timeout
= PFTM_UNLINKED
;
1643 pf_src_tree_remove_state(cur
);
1644 pf_detach_state(cur
, 0);
1647 /* callers should be at splpf and hold the
1648 * write_lock on pf_consistency_lock */
1650 pf_free_state(struct pf_state
*cur
)
1652 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1654 if (pfsyncif
!= NULL
&&
1655 (pfsyncif
->sc_bulk_send_next
== cur
||
1656 pfsyncif
->sc_bulk_terminator
== cur
))
1659 VERIFY(cur
->timeout
== PFTM_UNLINKED
);
1660 VERIFY(cur
->rule
.ptr
->states
> 0);
1661 if (--cur
->rule
.ptr
->states
<= 0 &&
1662 cur
->rule
.ptr
->src_nodes
<= 0)
1663 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1664 if (cur
->nat_rule
.ptr
!= NULL
) {
1665 VERIFY(cur
->nat_rule
.ptr
->states
> 0);
1666 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1667 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1668 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1670 if (cur
->anchor
.ptr
!= NULL
) {
1671 VERIFY(cur
->anchor
.ptr
->states
> 0);
1672 if (--cur
->anchor
.ptr
->states
<= 0)
1673 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1675 pf_normalize_tcp_cleanup(cur
);
1676 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1677 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1679 pf_tag_unref(cur
->tag
);
1680 pool_put(&pf_state_pl
, cur
);
1681 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1682 VERIFY(pf_status
.states
> 0);
1687 pf_purge_expired_states(u_int32_t maxcheck
)
1689 static struct pf_state
*cur
= NULL
;
1690 struct pf_state
*next
;
1692 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1694 while (maxcheck
--) {
1695 /* wrap to start of list when we hit the end */
1697 cur
= TAILQ_FIRST(&state_list
);
1699 break; /* list empty */
1702 /* get next state, as cur may get deleted */
1703 next
= TAILQ_NEXT(cur
, entry_list
);
1705 if (cur
->timeout
== PFTM_UNLINKED
) {
1707 } else if (pf_state_expires(cur
) <= pf_time_second()) {
1708 /* unlink and free expired state */
1709 pf_unlink_state(cur
);
1717 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1719 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1721 if (aw
->type
!= PF_ADDR_TABLE
)
1723 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1729 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1731 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1733 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1735 pfr_detach_table(aw
->p
.tbl
);
1740 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1742 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1744 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1746 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1748 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1749 kt
= kt
->pfrkt_root
;
1751 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1756 pf_print_addr(struct pf_addr
*addr
, sa_family_t af
)
1761 u_int32_t a
= ntohl(addr
->addr32
[0]);
1762 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1770 u_int8_t i
, curstart
= 255, curend
= 0,
1771 maxstart
= 0, maxend
= 0;
1772 for (i
= 0; i
< 8; i
++) {
1773 if (!addr
->addr16
[i
]) {
1774 if (curstart
== 255)
1780 if ((curend
- curstart
) >
1781 (maxend
- maxstart
)) {
1782 maxstart
= curstart
;
1789 for (i
= 0; i
< 8; i
++) {
1790 if (i
>= maxstart
&& i
<= maxend
) {
1799 b
= ntohs(addr
->addr16
[i
]);
1812 pf_print_sk_host(struct pf_state_host
*sh
, sa_family_t af
, int proto
,
1813 u_int8_t proto_variant
)
1815 pf_print_addr(&sh
->addr
, af
);
1820 printf("[%08x]", ntohl(sh
->xport
.spi
));
1824 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
1825 printf("[%u]", ntohs(sh
->xport
.call_id
));
1830 printf("[%u]", ntohs(sh
->xport
.port
));
1839 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1841 pf_print_addr(addr
, af
);
1843 printf("[%u]", ntohs(p
));
1847 pf_print_state(struct pf_state
*s
)
1849 struct pf_state_key
*sk
= s
->state_key
;
1850 switch (sk
->proto
) {
1855 printf("GRE%u ", sk
->proto_variant
);
1866 case IPPROTO_ICMPV6
:
1870 printf("%u ", sk
->proto
);
1873 pf_print_sk_host(&sk
->lan
, sk
->af_lan
, sk
->proto
, sk
->proto_variant
);
1875 pf_print_sk_host(&sk
->gwy
, sk
->af_gwy
, sk
->proto
, sk
->proto_variant
);
1877 pf_print_sk_host(&sk
->ext_lan
, sk
->af_lan
, sk
->proto
,
1880 pf_print_sk_host(&sk
->ext_gwy
, sk
->af_gwy
, sk
->proto
,
1882 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
1883 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
1884 if (s
->src
.wscale
&& s
->dst
.wscale
)
1885 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
1887 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
1888 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
1889 if (s
->src
.wscale
&& s
->dst
.wscale
)
1890 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
1892 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
1896 pf_print_flags(u_int8_t f
)
1918 #define PF_SET_SKIP_STEPS(i) \
1920 while (head[i] != cur) { \
1921 head[i]->skip[i].ptr = cur; \
1922 head[i] = TAILQ_NEXT(head[i], entries); \
1927 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
1929 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
1932 cur
= TAILQ_FIRST(rules
);
1934 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1936 while (cur
!= NULL
) {
1938 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
1939 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
1940 if (cur
->direction
!= prev
->direction
)
1941 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
1942 if (cur
->af
!= prev
->af
)
1943 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
1944 if (cur
->proto
!= prev
->proto
)
1945 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
1946 if (cur
->src
.neg
!= prev
->src
.neg
||
1947 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
1948 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
1950 union pf_rule_xport
*cx
= &cur
->src
.xport
;
1951 union pf_rule_xport
*px
= &prev
->src
.xport
;
1953 switch (cur
->proto
) {
1956 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1959 if (prev
->proto
== IPPROTO_GRE
||
1960 prev
->proto
== IPPROTO_ESP
||
1961 cx
->range
.op
!= px
->range
.op
||
1962 cx
->range
.port
[0] != px
->range
.port
[0] ||
1963 cx
->range
.port
[1] != px
->range
.port
[1])
1964 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1968 if (cur
->dst
.neg
!= prev
->dst
.neg
||
1969 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
1970 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
1972 union pf_rule_xport
*cx
= &cur
->dst
.xport
;
1973 union pf_rule_xport
*px
= &prev
->dst
.xport
;
1975 switch (cur
->proto
) {
1977 if (cur
->proto
!= prev
->proto
||
1978 cx
->call_id
!= px
->call_id
)
1979 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1982 if (cur
->proto
!= prev
->proto
||
1984 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1987 if (prev
->proto
== IPPROTO_GRE
||
1988 prev
->proto
== IPPROTO_ESP
||
1989 cx
->range
.op
!= px
->range
.op
||
1990 cx
->range
.port
[0] != px
->range
.port
[0] ||
1991 cx
->range
.port
[1] != px
->range
.port
[1])
1992 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1998 cur
= TAILQ_NEXT(cur
, entries
);
2000 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2001 PF_SET_SKIP_STEPS(i
);
2005 pf_calc_state_key_flowhash(struct pf_state_key
*sk
)
2007 struct pf_flowhash_key fh
__attribute__((aligned(8)));
2008 uint32_t flowhash
= 0;
2010 bzero(&fh
, sizeof (fh
));
2011 if (PF_ALEQ(&sk
->lan
.addr
, &sk
->ext_lan
.addr
, sk
->af_lan
)) {
2012 bcopy(&sk
->lan
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2013 bcopy(&sk
->ext_lan
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2015 bcopy(&sk
->ext_lan
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2016 bcopy(&sk
->lan
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2018 if (sk
->lan
.xport
.spi
<= sk
->ext_lan
.xport
.spi
) {
2019 fh
.ap1
.xport
.spi
= sk
->lan
.xport
.spi
;
2020 fh
.ap2
.xport
.spi
= sk
->ext_lan
.xport
.spi
;
2022 fh
.ap1
.xport
.spi
= sk
->ext_lan
.xport
.spi
;
2023 fh
.ap2
.xport
.spi
= sk
->lan
.xport
.spi
;
2026 fh
.proto
= sk
->proto
;
2029 flowhash
= net_flowhash(&fh
, sizeof (fh
), pf_hash_seed
);
2030 if (flowhash
== 0) {
2031 /* try to get a non-zero flowhash */
2032 pf_hash_seed
= RandomULong();
2040 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
2042 if (aw1
->type
!= aw2
->type
)
2044 switch (aw1
->type
) {
2045 case PF_ADDR_ADDRMASK
:
2047 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
2049 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
2052 case PF_ADDR_DYNIFTL
:
2053 return (aw1
->p
.dyn
== NULL
|| aw2
->p
.dyn
== NULL
||
2054 aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
2055 case PF_ADDR_NOROUTE
:
2056 case PF_ADDR_URPFFAILED
:
2059 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
2060 case PF_ADDR_RTLABEL
:
2061 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
2063 printf("invalid address type: %d\n", aw1
->type
);
2069 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
2075 l
= cksum
+ old
- new;
2076 l
= (l
>> 16) + (l
& 0xffff);
2084 * change ip address & port
2085 * dir : packet direction
2086 * a : address to be changed
2087 * p : port to be changed
2088 * ic : ip header checksum
2089 * pc : protocol checksum
2090 * an : new ip address
2092 * u : should be 1 if UDP packet else 0
2093 * af : address family of the packet
2094 * afn : address family of the new address
2095 * ua : should be 1 if ip address needs to be updated in the packet else
2096 * only the checksum is recalculated & updated.
2099 pf_change_ap(int dir
, struct mbuf
*m
, struct pf_addr
*a
, u_int16_t
*p
,
2100 u_int16_t
*ic
, u_int16_t
*pc
, struct pf_addr
*an
, u_int16_t pn
,
2101 u_int8_t u
, sa_family_t af
, sa_family_t afn
, int ua
)
2106 PF_ACPY(&ao
, a
, af
);
2108 PF_ACPY(a
, an
, afn
);
2117 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2118 ao
.addr16
[0], an
->addr16
[0], 0),
2119 ao
.addr16
[1], an
->addr16
[1], 0);
2122 * If the packet is originated from an ALG on the NAT gateway
2123 * (source address is loopback or local), in which case the
2124 * TCP/UDP checksum field contains the pseudo header checksum
2125 * that's not yet complemented. A packet generated locally
2126 * will have UDP/TCP CSUM flag set (gets set in protocol
2129 if (dir
== PF_OUT
&& m
!= NULL
&&
2130 (m
->m_flags
& M_PKTHDR
) &&
2131 (m
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))) {
2132 /* Pseudo-header checksum does not include ports */
2133 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2134 ao
.addr16
[0], an
->addr16
[0], u
),
2135 ao
.addr16
[1], an
->addr16
[1], u
);
2138 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2139 *pc
, ao
.addr16
[0], an
->addr16
[0], u
),
2140 ao
.addr16
[1], an
->addr16
[1], u
),
2147 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2148 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2150 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2151 ao
.addr16
[0], an
->addr16
[0], u
),
2152 ao
.addr16
[1], an
->addr16
[1], u
),
2153 0, an
->addr16
[2], u
),
2154 0, an
->addr16
[3], u
),
2155 0, an
->addr16
[4], u
),
2156 0, an
->addr16
[5], u
),
2157 0, an
->addr16
[6], u
),
2158 0, an
->addr16
[7], u
),
2170 * If the packet is originated from an ALG on the NAT gateway
2171 * (source address is loopback or local), in which case the
2172 * TCP/UDP checksum field contains the pseudo header checksum
2173 * that's not yet complemented.
2174 * A packet generated locally
2175 * will have UDP/TCP CSUM flag set (gets set in protocol
2178 if (dir
== PF_OUT
&& m
!= NULL
&&
2179 (m
->m_flags
& M_PKTHDR
) &&
2180 (m
->m_pkthdr
.csum_flags
& (CSUM_TCPIPV6
|
2182 /* Pseudo-header checksum does not include ports */
2184 ~pf_cksum_fixup(pf_cksum_fixup(
2185 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2186 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2188 ao
.addr16
[0], an
->addr16
[0], u
),
2189 ao
.addr16
[1], an
->addr16
[1], u
),
2190 ao
.addr16
[2], an
->addr16
[2], u
),
2191 ao
.addr16
[3], an
->addr16
[3], u
),
2192 ao
.addr16
[4], an
->addr16
[4], u
),
2193 ao
.addr16
[5], an
->addr16
[5], u
),
2194 ao
.addr16
[6], an
->addr16
[6], u
),
2195 ao
.addr16
[7], an
->addr16
[7], u
);
2198 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2199 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2200 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2202 ao
.addr16
[0], an
->addr16
[0], u
),
2203 ao
.addr16
[1], an
->addr16
[1], u
),
2204 ao
.addr16
[2], an
->addr16
[2], u
),
2205 ao
.addr16
[3], an
->addr16
[3], u
),
2206 ao
.addr16
[4], an
->addr16
[4], u
),
2207 ao
.addr16
[5], an
->addr16
[5], u
),
2208 ao
.addr16
[6], an
->addr16
[6], u
),
2209 ao
.addr16
[7], an
->addr16
[7], u
),
2215 *pc
= 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(pf_cksum_fixup(*pc
,
2218 ao
.addr16
[0], an
->addr16
[0], u
),
2219 ao
.addr16
[1], an
->addr16
[1], u
),
2220 ao
.addr16
[2], 0, u
),
2221 ao
.addr16
[3], 0, u
),
2222 ao
.addr16
[4], 0, u
),
2223 ao
.addr16
[5], 0, u
),
2224 ao
.addr16
[6], 0, u
),
2225 ao
.addr16
[7], 0, u
),
2236 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
2238 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
2242 memcpy(&ao
, a
, sizeof (ao
));
2243 memcpy(a
, &an
, sizeof (u_int32_t
));
2244 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
2245 ao
% 65536, an
% 65536, u
);
2250 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
2254 PF_ACPY(&ao
, a
, AF_INET6
);
2255 PF_ACPY(a
, an
, AF_INET6
);
2257 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2258 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2259 pf_cksum_fixup(pf_cksum_fixup(*c
,
2260 ao
.addr16
[0], an
->addr16
[0], u
),
2261 ao
.addr16
[1], an
->addr16
[1], u
),
2262 ao
.addr16
[2], an
->addr16
[2], u
),
2263 ao
.addr16
[3], an
->addr16
[3], u
),
2264 ao
.addr16
[4], an
->addr16
[4], u
),
2265 ao
.addr16
[5], an
->addr16
[5], u
),
2266 ao
.addr16
[6], an
->addr16
[6], u
),
2267 ao
.addr16
[7], an
->addr16
[7], u
);
2271 pf_change_addr(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
,
2272 sa_family_t af
, sa_family_t afn
)
2276 PF_ACPY(&ao
, a
, af
);
2277 PF_ACPY(a
, an
, afn
);
2283 pf_change_a(a
, c
, an
->v4
.s_addr
, u
);
2286 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2287 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2288 pf_cksum_fixup(pf_cksum_fixup(*c
,
2289 ao
.addr16
[0], an
->addr16
[0], u
),
2290 ao
.addr16
[1], an
->addr16
[1], u
),
2291 0, an
->addr16
[2], u
),
2292 0, an
->addr16
[3], u
),
2293 0, an
->addr16
[4], u
),
2294 0, an
->addr16
[5], u
),
2295 0, an
->addr16
[6], u
),
2296 0, an
->addr16
[7], u
);
2303 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2304 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2305 pf_cksum_fixup(pf_cksum_fixup(*c
,
2306 ao
.addr16
[0], an
->addr16
[0], u
),
2307 ao
.addr16
[1], an
->addr16
[1], u
),
2308 ao
.addr16
[2], 0, u
),
2309 ao
.addr16
[3], 0, u
),
2310 ao
.addr16
[4], 0, u
),
2311 ao
.addr16
[5], 0, u
),
2312 ao
.addr16
[6], 0, u
),
2313 ao
.addr16
[7], 0, u
);
2316 pf_change_a6(a
, c
, an
, u
);
2326 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
2327 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
2328 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
2330 struct pf_addr oia
, ooa
;
2332 PF_ACPY(&oia
, ia
, af
);
2333 PF_ACPY(&ooa
, oa
, af
);
2335 /* Change inner protocol port, fix inner protocol checksum. */
2337 u_int16_t oip
= *ip
;
2344 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
2345 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
2347 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
2349 /* Change inner ip address, fix inner ip and icmp checksums. */
2350 PF_ACPY(ia
, na
, af
);
2354 u_int32_t oh2c
= *h2c
;
2356 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
2357 oia
.addr16
[0], ia
->addr16
[0], 0),
2358 oia
.addr16
[1], ia
->addr16
[1], 0);
2359 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2360 oia
.addr16
[0], ia
->addr16
[0], 0),
2361 oia
.addr16
[1], ia
->addr16
[1], 0);
2362 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
2368 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2369 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2370 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2371 oia
.addr16
[0], ia
->addr16
[0], u
),
2372 oia
.addr16
[1], ia
->addr16
[1], u
),
2373 oia
.addr16
[2], ia
->addr16
[2], u
),
2374 oia
.addr16
[3], ia
->addr16
[3], u
),
2375 oia
.addr16
[4], ia
->addr16
[4], u
),
2376 oia
.addr16
[5], ia
->addr16
[5], u
),
2377 oia
.addr16
[6], ia
->addr16
[6], u
),
2378 oia
.addr16
[7], ia
->addr16
[7], u
);
2382 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
2383 PF_ACPY(oa
, na
, af
);
2387 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
2388 ooa
.addr16
[0], oa
->addr16
[0], 0),
2389 ooa
.addr16
[1], oa
->addr16
[1], 0);
2394 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2395 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2396 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2397 ooa
.addr16
[0], oa
->addr16
[0], u
),
2398 ooa
.addr16
[1], oa
->addr16
[1], u
),
2399 ooa
.addr16
[2], oa
->addr16
[2], u
),
2400 ooa
.addr16
[3], oa
->addr16
[3], u
),
2401 ooa
.addr16
[4], oa
->addr16
[4], u
),
2402 ooa
.addr16
[5], oa
->addr16
[5], u
),
2403 ooa
.addr16
[6], oa
->addr16
[6], u
),
2404 ooa
.addr16
[7], oa
->addr16
[7], u
);
2412 * Need to modulate the sequence numbers in the TCP SACK option
2413 * (credits to Krzysztof Pfaff for report and patch)
2416 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
2417 struct tcphdr
*th
, struct pf_state_peer
*dst
)
2419 int hlen
= (th
->th_off
<< 2) - sizeof (*th
), thoptlen
= hlen
;
2420 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
2421 int copyback
= 0, i
, olen
;
2422 struct sackblk sack
;
2424 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2425 if (hlen
< TCPOLEN_SACKLEN
||
2426 !pf_pull_hdr(m
, off
+ sizeof (*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
2429 while (hlen
>= TCPOLEN_SACKLEN
) {
2432 case TCPOPT_EOL
: /* FALLTHROUGH */
2440 if (olen
>= TCPOLEN_SACKLEN
) {
2441 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
2442 i
+= TCPOLEN_SACK
) {
2443 memcpy(&sack
, &opt
[i
], sizeof (sack
));
2444 pf_change_a(&sack
.start
, &th
->th_sum
,
2445 htonl(ntohl(sack
.start
) -
2447 pf_change_a(&sack
.end
, &th
->th_sum
,
2448 htonl(ntohl(sack
.end
) -
2450 memcpy(&opt
[i
], &sack
, sizeof (sack
));
2452 copyback
= off
+ sizeof (*th
) + thoptlen
;
2464 m
= pf_lazy_makewritable(pd
, m
, copyback
);
2467 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2473 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
2474 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
2475 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
2476 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
2477 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
2479 #pragma unused(eh, ifp)
2483 struct ip
*h
= NULL
;
2486 struct ip6_hdr
*h6
= NULL
;
2488 struct tcphdr
*th
= NULL
;
2490 struct pf_mtag
*pf_mtag
;
2492 /* maximum segment size tcp option */
2493 tlen
= sizeof (struct tcphdr
);
2500 len
= sizeof (struct ip
) + tlen
;
2505 len
= sizeof (struct ip6_hdr
) + tlen
;
2509 panic("pf_send_tcp: not AF_INET or AF_INET6!");
2513 /* create outgoing mbuf */
2514 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
2518 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
2524 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2525 pf_mtag
->pftag_tag
= rtag
;
2527 if (r
!= NULL
&& PF_RTABLEID_IS_VALID(r
->rtableid
))
2528 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2531 if (altq_allowed
&& r
!= NULL
&& r
->qid
)
2532 pf_mtag
->pftag_qid
= r
->qid
;
2533 #endif /* PF_ALTQ */
2536 /* add hints for ecn */
2537 pf_mtag
->pftag_hdr
= mtod(m
, struct ip
*);
2538 /* record address family */
2539 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2543 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2548 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2554 /* indicate this is TCP */
2555 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
2557 /* Make sure headers are 32-bit aligned */
2558 m
->m_data
+= max_linkhdr
;
2559 m
->m_pkthdr
.len
= m
->m_len
= len
;
2560 m
->m_pkthdr
.rcvif
= NULL
;
2561 bzero(m
->m_data
, len
);
2565 h
= mtod(m
, struct ip
*);
2567 /* IP header fields included in the TCP checksum */
2568 h
->ip_p
= IPPROTO_TCP
;
2569 h
->ip_len
= htons(tlen
);
2570 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
2571 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
2573 th
= (struct tcphdr
*)(void *)((caddr_t
)h
+ sizeof (struct ip
));
2578 h6
= mtod(m
, struct ip6_hdr
*);
2580 /* IP header fields included in the TCP checksum */
2581 h6
->ip6_nxt
= IPPROTO_TCP
;
2582 h6
->ip6_plen
= htons(tlen
);
2583 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof (struct in6_addr
));
2584 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof (struct in6_addr
));
2586 th
= (struct tcphdr
*)(void *)
2587 ((caddr_t
)h6
+ sizeof (struct ip6_hdr
));
2593 th
->th_sport
= sport
;
2594 th
->th_dport
= dport
;
2595 th
->th_seq
= htonl(seq
);
2596 th
->th_ack
= htonl(ack
);
2597 th
->th_off
= tlen
>> 2;
2598 th
->th_flags
= flags
;
2599 th
->th_win
= htons(win
);
2602 opt
= (char *)(th
+ 1);
2603 opt
[0] = TCPOPT_MAXSEG
;
2605 #if BYTE_ORDER != BIG_ENDIAN
2608 bcopy((caddr_t
)&mss
, (caddr_t
)(opt
+ 2), 2);
2617 th
->th_sum
= in_cksum(m
, len
);
2619 /* Finish the IP header */
2621 h
->ip_hl
= sizeof (*h
) >> 2;
2622 h
->ip_tos
= IPTOS_LOWDELAY
;
2624 * ip_output() expects ip_len and ip_off to be in host order.
2627 h
->ip_off
= (path_mtu_discovery
? IP_DF
: 0);
2628 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
2631 bzero(&ro
, sizeof (ro
));
2632 ip_output(m
, NULL
, &ro
, 0, NULL
, NULL
);
2639 struct route_in6 ro6
;
2642 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
2643 sizeof (struct ip6_hdr
), tlen
);
2645 h6
->ip6_vfc
|= IPV6_VERSION
;
2646 h6
->ip6_hlim
= IPV6_DEFHLIM
;
2648 bzero(&ro6
, sizeof (ro6
));
2649 ip6_output(m
, NULL
, &ro6
, 0, NULL
, NULL
, NULL
);
2650 ROUTE_RELEASE(&ro6
);
2658 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
2662 struct pf_mtag
*pf_mtag
;
2664 m0
= m_copy(m
, 0, M_COPYALL
);
2668 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
2671 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2673 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
2674 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2677 if (altq_allowed
&& r
->qid
)
2678 pf_mtag
->pftag_qid
= r
->qid
;
2679 #endif /* PF_ALTQ */
2682 /* add hints for ecn */
2683 pf_mtag
->pftag_hdr
= mtod(m0
, struct ip
*);
2684 /* record address family */
2685 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2689 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2690 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMP
;
2695 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2696 m0
->m_pkthdr
.pkt_proto
= IPPROTO_ICMPV6
;
2705 icmp_error(m0
, type
, code
, 0, 0);
2710 icmp6_error(m0
, type
, code
, 0);
2717 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2718 * If n is 0, they match if they are equal. If n is != 0, they match if they
2722 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
2723 struct pf_addr
*b
, sa_family_t af
)
2730 if ((a
->addr32
[0] & m
->addr32
[0]) ==
2731 (b
->addr32
[0] & m
->addr32
[0]))
2737 if (((a
->addr32
[0] & m
->addr32
[0]) ==
2738 (b
->addr32
[0] & m
->addr32
[0])) &&
2739 ((a
->addr32
[1] & m
->addr32
[1]) ==
2740 (b
->addr32
[1] & m
->addr32
[1])) &&
2741 ((a
->addr32
[2] & m
->addr32
[2]) ==
2742 (b
->addr32
[2] & m
->addr32
[2])) &&
2743 ((a
->addr32
[3] & m
->addr32
[3]) ==
2744 (b
->addr32
[3] & m
->addr32
[3])))
2763 * Return 1 if b <= a <= e, otherwise return 0.
2766 pf_match_addr_range(struct pf_addr
*b
, struct pf_addr
*e
,
2767 struct pf_addr
*a
, sa_family_t af
)
2772 if ((a
->addr32
[0] < b
->addr32
[0]) ||
2773 (a
->addr32
[0] > e
->addr32
[0]))
2782 for (i
= 0; i
< 4; ++i
)
2783 if (a
->addr32
[i
] > b
->addr32
[i
])
2785 else if (a
->addr32
[i
] < b
->addr32
[i
])
2788 for (i
= 0; i
< 4; ++i
)
2789 if (a
->addr32
[i
] < e
->addr32
[i
])
2791 else if (a
->addr32
[i
] > e
->addr32
[i
])
2801 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
2805 return ((p
> a1
) && (p
< a2
));
2807 return ((p
< a1
) || (p
> a2
));
2809 return ((p
>= a1
) && (p
<= a2
));
2823 return (0); /* never reached */
2827 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
2829 #if BYTE_ORDER != BIG_ENDIAN
2834 return (pf_match(op
, a1
, a2
, p
));
2838 pf_match_xport(u_int8_t proto
, u_int8_t proto_variant
, union pf_rule_xport
*rx
,
2839 union pf_state_xport
*sx
)
2846 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
2847 d
= (rx
->call_id
== sx
->call_id
);
2851 d
= (rx
->spi
== sx
->spi
);
2857 case IPPROTO_ICMPV6
:
2859 d
= pf_match_port(rx
->range
.op
,
2860 rx
->range
.port
[0], rx
->range
.port
[1],
2873 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
2875 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2877 return (pf_match(op
, a1
, a2
, u
));
2881 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
2883 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2885 return (pf_match(op
, a1
, a2
, g
));
2889 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, struct pf_mtag
*pf_mtag
,
2894 *tag
= pf_mtag
->pftag_tag
;
2896 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2897 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2901 pf_tag_packet(struct mbuf
*m
, struct pf_mtag
*pf_mtag
, int tag
,
2902 unsigned int rtableid
, struct pf_pdesc
*pd
)
2904 if (tag
<= 0 && !PF_RTABLEID_IS_VALID(rtableid
) &&
2905 (pd
== NULL
|| !(pd
->pktflags
& PKTF_FLOW_ID
)))
2908 if (pf_mtag
== NULL
&& (pf_mtag
= pf_get_mtag(m
)) == NULL
)
2912 pf_mtag
->pftag_tag
= tag
;
2913 if (PF_RTABLEID_IS_VALID(rtableid
))
2914 pf_mtag
->pftag_rtableid
= rtableid
;
2915 if (pd
!= NULL
&& (pd
->pktflags
& PKTF_FLOW_ID
)) {
2916 m
->m_pkthdr
.pkt_flowsrc
= pd
->flowsrc
;
2917 m
->m_pkthdr
.pkt_flowid
= pd
->flowhash
;
2918 m
->m_pkthdr
.pkt_flags
|= pd
->pktflags
;
2919 m
->m_pkthdr
.pkt_proto
= pd
->proto
;
2926 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2927 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2929 struct pf_anchor_stackframe
*f
;
2931 (*r
)->anchor
->match
= 0;
2934 if (*depth
>= (int)sizeof (pf_anchor_stack
) /
2935 (int)sizeof (pf_anchor_stack
[0])) {
2936 printf("pf_step_into_anchor: stack overflow\n");
2937 *r
= TAILQ_NEXT(*r
, entries
);
2939 } else if (*depth
== 0 && a
!= NULL
)
2941 f
= pf_anchor_stack
+ (*depth
)++;
2944 if ((*r
)->anchor_wildcard
) {
2945 f
->parent
= &(*r
)->anchor
->children
;
2946 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2951 *rs
= &f
->child
->ruleset
;
2955 *rs
= &(*r
)->anchor
->ruleset
;
2957 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2961 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2962 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2964 struct pf_anchor_stackframe
*f
;
2970 f
= pf_anchor_stack
+ *depth
- 1;
2971 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2972 if (f
->child
->match
||
2973 (match
!= NULL
&& *match
)) {
2974 f
->r
->anchor
->match
= 1;
2977 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
2978 if (f
->child
!= NULL
) {
2979 *rs
= &f
->child
->ruleset
;
2980 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2988 if (*depth
== 0 && a
!= NULL
)
2991 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
2992 quick
= f
->r
->quick
;
2993 *r
= TAILQ_NEXT(f
->r
, entries
);
2994 } while (*r
== NULL
);
3001 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
3002 struct pf_addr
*rmask
, struct pf_addr
*saddr
, sa_family_t af
)
3007 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
3008 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
3012 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
3013 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
3014 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
3015 ((rmask
->addr32
[1] ^ 0xffffffff) & saddr
->addr32
[1]);
3016 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
3017 ((rmask
->addr32
[2] ^ 0xffffffff) & saddr
->addr32
[2]);
3018 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
3019 ((rmask
->addr32
[3] ^ 0xffffffff) & saddr
->addr32
[3]);
3025 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
3030 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
3034 if (addr
->addr32
[3] == 0xffffffff) {
3035 addr
->addr32
[3] = 0;
3036 if (addr
->addr32
[2] == 0xffffffff) {
3037 addr
->addr32
[2] = 0;
3038 if (addr
->addr32
[1] == 0xffffffff) {
3039 addr
->addr32
[1] = 0;
3041 htonl(ntohl(addr
->addr32
[0]) + 1);
3044 htonl(ntohl(addr
->addr32
[1]) + 1);
3047 htonl(ntohl(addr
->addr32
[2]) + 1);
3050 htonl(ntohl(addr
->addr32
[3]) + 1);
3056 #define mix(a, b, c) \
3058 a -= b; a -= c; a ^= (c >> 13); \
3059 b -= c; b -= a; b ^= (a << 8); \
3060 c -= a; c -= b; c ^= (b >> 13); \
3061 a -= b; a -= c; a ^= (c >> 12); \
3062 b -= c; b -= a; b ^= (a << 16); \
3063 c -= a; c -= b; c ^= (b >> 5); \
3064 a -= b; a -= c; a ^= (c >> 3); \
3065 b -= c; b -= a; b ^= (a << 10); \
3066 c -= a; c -= b; c ^= (b >> 15); \
3070 * hash function based on bridge_hash in if_bridge.c
3073 pf_hash(struct pf_addr
*inaddr
, struct pf_addr
*hash
,
3074 struct pf_poolhashkey
*key
, sa_family_t af
)
3076 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
3081 a
+= inaddr
->addr32
[0];
3084 hash
->addr32
[0] = c
+ key
->key32
[2];
3089 a
+= inaddr
->addr32
[0];
3090 b
+= inaddr
->addr32
[2];
3092 hash
->addr32
[0] = c
;
3093 a
+= inaddr
->addr32
[1];
3094 b
+= inaddr
->addr32
[3];
3097 hash
->addr32
[1] = c
;
3098 a
+= inaddr
->addr32
[2];
3099 b
+= inaddr
->addr32
[1];
3102 hash
->addr32
[2] = c
;
3103 a
+= inaddr
->addr32
[3];
3104 b
+= inaddr
->addr32
[0];
3107 hash
->addr32
[3] = c
;
3114 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, struct pf_addr
*saddr
,
3115 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
3117 unsigned char hash
[16];
3118 struct pf_pool
*rpool
= &r
->rpool
;
3119 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3120 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3121 struct pf_pooladdr
*acur
= rpool
->cur
;
3122 struct pf_src_node k
;
3124 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
3125 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3127 PF_ACPY(&k
.addr
, saddr
, af
);
3128 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
3129 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
3133 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
3134 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
3135 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, rpool
->af
)) {
3136 PF_ACPY(naddr
, &(*sn
)->raddr
, rpool
->af
);
3137 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
3138 printf("pf_map_addr: src tracking maps ");
3139 pf_print_host(&k
.addr
, 0, af
);
3141 pf_print_host(naddr
, 0, rpool
->af
);
3148 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
3150 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3151 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3153 switch (rpool
->af
) {
3156 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
3157 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3160 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
3161 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
3166 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
3167 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3170 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
3171 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
3175 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3176 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
3177 return (1); /* unsupported */
3179 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3180 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3183 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
3185 PF_ACPY(naddr
, raddr
, rpool
->af
);
3187 case PF_POOL_BITMASK
:
3188 ASSERT(af
== rpool
->af
);
3189 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3191 case PF_POOL_RANDOM
:
3192 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, rpool
->af
)) {
3196 rpool
->counter
.addr32
[0] = htonl(random());
3201 if (rmask
->addr32
[3] != 0xffffffff)
3202 rpool
->counter
.addr32
[3] =
3206 if (rmask
->addr32
[2] != 0xffffffff)
3207 rpool
->counter
.addr32
[2] =
3211 if (rmask
->addr32
[1] != 0xffffffff)
3212 rpool
->counter
.addr32
[1] =
3216 if (rmask
->addr32
[0] != 0xffffffff)
3217 rpool
->counter
.addr32
[0] =
3222 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
,
3224 PF_ACPY(init_addr
, naddr
, rpool
->af
);
3227 PF_AINC(&rpool
->counter
, rpool
->af
);
3228 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
,
3232 case PF_POOL_SRCHASH
:
3233 ASSERT(af
== rpool
->af
);
3234 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3235 pf_hash(saddr
, (struct pf_addr
*)(void *)&hash
,
3237 PF_POOLMASK(naddr
, raddr
, rmask
,
3238 (struct pf_addr
*)(void *)&hash
, af
);
3240 case PF_POOL_ROUNDROBIN
:
3241 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3242 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3243 &rpool
->tblidx
, &rpool
->counter
,
3244 &raddr
, &rmask
, rpool
->af
))
3246 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3247 if (rpool
->cur
->addr
.p
.dyn
!= NULL
&&
3248 !pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3249 &rpool
->tblidx
, &rpool
->counter
,
3250 &raddr
, &rmask
, af
))
3252 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
,
3257 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
3258 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
3259 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3261 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3262 &rpool
->tblidx
, &rpool
->counter
,
3263 &raddr
, &rmask
, rpool
->af
)) {
3264 /* table contains no address of type
3266 if (rpool
->cur
!= acur
)
3270 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3272 if (rpool
->cur
->addr
.p
.dyn
== NULL
)
3274 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3275 &rpool
->tblidx
, &rpool
->counter
,
3276 &raddr
, &rmask
, rpool
->af
)) {
3277 /* table contains no address of type
3279 if (rpool
->cur
!= acur
)
3284 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3285 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3286 PF_ACPY(&rpool
->counter
, raddr
, rpool
->af
);
3290 PF_ACPY(naddr
, &rpool
->counter
, rpool
->af
);
3291 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, rpool
->af
))
3292 PF_ACPY(init_addr
, naddr
, rpool
->af
);
3293 PF_AINC(&rpool
->counter
, rpool
->af
);
3297 PF_ACPY(&(*sn
)->raddr
, naddr
, rpool
->af
);
3299 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
3300 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3301 printf("pf_map_addr: selected address ");
3302 pf_print_host(naddr
, 0, rpool
->af
);
3310 pf_get_sport(struct pf_pdesc
*pd
, struct pfi_kif
*kif
, struct pf_rule
*r
,
3311 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3312 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3313 union pf_state_xport
*nxport
, struct pf_src_node
**sn
)
3316 struct pf_state_key_cmp key
;
3317 struct pf_addr init_addr
;
3319 sa_family_t af
= pd
->af
;
3320 u_int8_t proto
= pd
->proto
;
3321 unsigned int low
= r
->rpool
.proxy_port
[0];
3322 unsigned int high
= r
->rpool
.proxy_port
[1];
3324 bzero(&init_addr
, sizeof (init_addr
));
3325 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3328 if (proto
== IPPROTO_ICMP
) {
3334 return (0); /* No output necessary. */
3336 /*--- Special mapping rules for UDP ---*/
3337 if (proto
== IPPROTO_UDP
) {
3339 /*--- Never float IKE source port ---*/
3340 if (ntohs(sxport
->port
) == PF_IKE_PORT
) {
3341 nxport
->port
= sxport
->port
;
3345 /*--- Apply exterior mapping options ---*/
3346 if (r
->extmap
> PF_EXTMAP_APD
) {
3349 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3350 struct pf_state_key
*sk
= s
->state_key
;
3353 if (s
->nat_rule
.ptr
!= r
)
3355 if (sk
->proto
!= IPPROTO_UDP
||
3358 if (sk
->lan
.xport
.port
!= sxport
->port
)
3360 if (PF_ANEQ(&sk
->lan
.addr
, saddr
, af
))
3362 if (r
->extmap
< PF_EXTMAP_EI
&&
3363 PF_ANEQ(&sk
->ext_lan
.addr
, daddr
, af
))
3366 nxport
->port
= sk
->gwy
.xport
.port
;
3370 } else if (proto
== IPPROTO_TCP
) {
3373 * APPLE MODIFICATION: <rdar://problem/6546358>
3374 * Fix allows....NAT to use a single binding for TCP session
3375 * with same source IP and source port
3377 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3378 struct pf_state_key
* sk
= s
->state_key
;
3381 if (s
->nat_rule
.ptr
!= r
)
3383 if (sk
->proto
!= IPPROTO_TCP
|| sk
->af_lan
!= af
)
3385 if (sk
->lan
.xport
.port
!= sxport
->port
)
3387 if (!(PF_AEQ(&sk
->lan
.addr
, saddr
, af
)))
3389 nxport
->port
= sk
->gwy
.xport
.port
;
3396 PF_ACPY(&key
.ext_gwy
.addr
, daddr
, key
.af_gwy
);
3397 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af_gwy
);
3400 key
.proto_variant
= r
->extfilter
;
3403 key
.proto_variant
= 0;
3407 key
.ext_gwy
.xport
= *dxport
;
3409 memset(&key
.ext_gwy
.xport
, 0,
3410 sizeof (key
.ext_gwy
.xport
));
3412 * port search; start random, step;
3413 * similar 2 portloop in in_pcbbind
3415 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
3416 proto
== IPPROTO_ICMP
)) {
3418 key
.gwy
.xport
= *dxport
;
3420 memset(&key
.gwy
.xport
, 0,
3421 sizeof (key
.gwy
.xport
));
3422 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3424 } else if (low
== 0 && high
== 0) {
3425 key
.gwy
.xport
= *nxport
;
3426 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3428 } else if (low
== high
) {
3429 key
.gwy
.xport
.port
= htons(low
);
3430 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3431 nxport
->port
= htons(low
);
3442 cut
= htonl(random()) % (1 + high
- low
) + low
;
3443 /* low <= cut <= high */
3444 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
3445 key
.gwy
.xport
.port
= htons(tmp
);
3446 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3448 nxport
->port
= htons(tmp
);
3452 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
3453 key
.gwy
.xport
.port
= htons(tmp
);
3454 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3456 nxport
->port
= htons(tmp
);
3462 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
3463 case PF_POOL_RANDOM
:
3464 case PF_POOL_ROUNDROBIN
:
3465 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3469 case PF_POOL_SRCHASH
:
3470 case PF_POOL_BITMASK
:
3474 } while (!PF_AEQ(&init_addr
, naddr
, af
));
3476 return (1); /* none available */
3479 static struct pf_rule
*
3480 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3481 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
,
3482 union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3483 union pf_state_xport
*dxport
, int rs_num
)
3485 struct pf_rule
*r
, *rm
= NULL
;
3486 struct pf_ruleset
*ruleset
= NULL
;
3488 unsigned int rtableid
= IFSCOPE_NONE
;
3491 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
3492 while (r
&& rm
== NULL
) {
3493 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
3494 struct pf_addr_wrap
*xdst
= NULL
;
3495 struct pf_addr_wrap
*xsrc
= NULL
;
3496 union pf_rule_xport rdrxport
;
3498 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
3500 if (r
->rpool
.cur
!= NULL
)
3501 xdst
= &r
->rpool
.cur
->addr
;
3502 } else if (r
->action
== PF_RDR
&& direction
== PF_OUT
) {
3505 if (r
->rpool
.cur
!= NULL
) {
3506 rdrxport
.range
.op
= PF_OP_EQ
;
3507 rdrxport
.range
.port
[0] =
3508 htons(r
->rpool
.proxy_port
[0]);
3509 xsrc
= &r
->rpool
.cur
->addr
;
3517 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3518 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3519 else if (r
->direction
&& r
->direction
!= direction
)
3520 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3521 else if (r
->af
&& r
->af
!= pd
->af
)
3522 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3523 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3524 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3525 else if (xsrc
&& PF_MISMATCHAW(xsrc
, saddr
, pd
->af
, 0, NULL
))
3526 r
= TAILQ_NEXT(r
, entries
);
3527 else if (!xsrc
&& PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3529 r
= TAILQ_NEXT(r
, entries
);
3530 else if (xsrc
&& (!rdrxport
.range
.port
[0] ||
3531 !pf_match_xport(r
->proto
, r
->proto_variant
, &rdrxport
,
3533 r
= TAILQ_NEXT(r
, entries
);
3534 else if (!xsrc
&& !pf_match_xport(r
->proto
,
3535 r
->proto_variant
, &src
->xport
, sxport
))
3536 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
3537 PF_SKIP_DST_PORT
].ptr
;
3538 else if (dst
!= NULL
&&
3539 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
3540 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3541 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
3543 r
= TAILQ_NEXT(r
, entries
);
3544 else if (dst
&& !pf_match_xport(r
->proto
, r
->proto_variant
,
3545 &dst
->xport
, dxport
))
3546 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3547 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
3548 r
= TAILQ_NEXT(r
, entries
);
3549 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
3550 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
3551 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
3552 r
= TAILQ_NEXT(r
, entries
);
3556 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
3557 rtableid
= r
->rtableid
;
3558 if (r
->anchor
== NULL
) {
3561 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
3565 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
3568 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, NULL
))
3570 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
3571 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
||
3572 rm
->action
== PF_NONAT64
))
3578 * Get address translation information for NAT/BINAT/RDR
3579 * pd : pf packet descriptor
3580 * m : mbuf holding the packet
3581 * off : offset to protocol header
3582 * direction : direction of packet
3583 * kif : pf interface info obtained from the packet's recv interface
3584 * sn : source node pointer (output)
3585 * saddr : packet source address
3586 * sxport : packet source port
3587 * daddr : packet destination address
3588 * dxport : packet destination port
3589 * nsxport : translated source port (output)
3591 * Translated source & destination address are updated in pd->nsaddr &
3594 static struct pf_rule
*
3595 pf_get_translation_aux(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3596 int direction
, struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3597 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3598 union pf_state_xport
*dxport
, union pf_state_xport
*nsxport
)
3600 struct pf_rule
*r
= NULL
;
3603 if (direction
== PF_OUT
) {
3604 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3605 sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3607 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3608 saddr
, sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3610 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3611 saddr
, sxport
, daddr
, dxport
, PF_RULESET_NAT
);
3613 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3614 sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3616 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3617 saddr
, sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3621 struct pf_addr
*nsaddr
= &pd
->naddr
;
3622 struct pf_addr
*ndaddr
= &pd
->ndaddr
;
3627 switch (r
->action
) {
3636 * we do NAT64 on incoming path and we call ip_input
3637 * which asserts receive interface to be not NULL.
3638 * The below check is to prevent NAT64 action on any
3639 * packet generated by local entity using synthesized
3642 if ((r
->action
== PF_NAT64
) && (direction
== PF_OUT
))
3645 if (pf_get_sport(pd
, kif
, r
, saddr
, sxport
, daddr
,
3646 dxport
, nsaddr
, nsxport
, sn
)) {
3647 DPFPRINTF(PF_DEBUG_MISC
,
3648 ("pf: NAT proxy port allocation "
3650 r
->rpool
.proxy_port
[0],
3651 r
->rpool
.proxy_port
[1]));
3655 * For NAT64 the destination IPv4 address is derived
3656 * from the last 32 bits of synthesized IPv6 address
3658 if (r
->action
== PF_NAT64
) {
3659 ndaddr
->v4
.s_addr
= daddr
->addr32
[3];
3664 switch (direction
) {
3666 if (r
->rpool
.cur
->addr
.type
==
3668 if (r
->rpool
.cur
->addr
.p
.dyn
== NULL
)
3673 if (r
->rpool
.cur
->addr
.p
.dyn
->
3677 &r
->rpool
.cur
->addr
.p
.dyn
->
3679 &r
->rpool
.cur
->addr
.p
.dyn
->
3686 if (r
->rpool
.cur
->addr
.p
.dyn
->
3690 &r
->rpool
.cur
->addr
.p
.dyn
->
3692 &r
->rpool
.cur
->addr
.p
.dyn
->
3700 &r
->rpool
.cur
->addr
.v
.a
.addr
,
3701 &r
->rpool
.cur
->addr
.v
.a
.mask
,
3706 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
3707 if (r
->src
.addr
.p
.dyn
== NULL
)
3712 if (r
->src
.addr
.p
.dyn
->
3716 &r
->src
.addr
.p
.dyn
->
3718 &r
->src
.addr
.p
.dyn
->
3725 if (r
->src
.addr
.p
.dyn
->
3729 &r
->src
.addr
.p
.dyn
->
3731 &r
->src
.addr
.p
.dyn
->
3739 &r
->src
.addr
.v
.a
.addr
,
3740 &r
->src
.addr
.v
.a
.mask
, daddr
,
3746 switch (direction
) {
3748 if (r
->dst
.addr
.type
== PF_ADDR_DYNIFTL
) {
3749 if (r
->dst
.addr
.p
.dyn
== NULL
)
3754 if (r
->dst
.addr
.p
.dyn
->
3758 &r
->dst
.addr
.p
.dyn
->
3760 &r
->dst
.addr
.p
.dyn
->
3767 if (r
->dst
.addr
.p
.dyn
->
3771 &r
->dst
.addr
.p
.dyn
->
3773 &r
->dst
.addr
.p
.dyn
->
3781 &r
->dst
.addr
.v
.a
.addr
,
3782 &r
->dst
.addr
.v
.a
.mask
,
3785 if (nsxport
&& r
->dst
.xport
.range
.port
[0])
3787 r
->dst
.xport
.range
.port
[0];
3790 if (pf_map_addr(pd
->af
, r
, saddr
,
3793 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3795 PF_POOLMASK(ndaddr
, ndaddr
,
3796 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3799 if (nsxport
&& dxport
) {
3800 if (r
->rpool
.proxy_port
[1]) {
3801 u_int32_t tmp_nport
;
3804 ((ntohs(dxport
->port
) -
3805 ntohs(r
->dst
.xport
.range
.
3807 (r
->rpool
.proxy_port
[1] -
3808 r
->rpool
.proxy_port
[0] +
3809 1)) + r
->rpool
.proxy_port
[0];
3811 /* wrap around if necessary */
3812 if (tmp_nport
> 65535)
3815 htons((u_int16_t
)tmp_nport
);
3816 } else if (r
->rpool
.proxy_port
[0]) {
3817 nsxport
->port
= htons(r
->rpool
.
3834 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
3836 struct pf_addr
*saddr
, *daddr
;
3837 u_int16_t sport
, dport
;
3838 struct inpcbinfo
*pi
;
3843 pd
->lookup
.uid
= UID_MAX
;
3844 pd
->lookup
.gid
= GID_MAX
;
3845 pd
->lookup
.pid
= NO_PID
;
3847 switch (pd
->proto
) {
3849 if (pd
->hdr
.tcp
== NULL
)
3851 sport
= pd
->hdr
.tcp
->th_sport
;
3852 dport
= pd
->hdr
.tcp
->th_dport
;
3856 if (pd
->hdr
.udp
== NULL
)
3858 sport
= pd
->hdr
.udp
->uh_sport
;
3859 dport
= pd
->hdr
.udp
->uh_dport
;
3865 if (direction
== PF_IN
) {
3880 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
, daddr
->v4
, dport
,
3881 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3884 struct in6_addr s6
, d6
;
3886 memset(&s6
, 0, sizeof (s6
));
3887 s6
.s6_addr16
[5] = htons(0xffff);
3888 memcpy(&s6
.s6_addr32
[3], &saddr
->v4
,
3889 sizeof (saddr
->v4
));
3891 memset(&d6
, 0, sizeof (d6
));
3892 d6
.s6_addr16
[5] = htons(0xffff);
3893 memcpy(&d6
.s6_addr32
[3], &daddr
->v4
,
3894 sizeof (daddr
->v4
));
3896 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3897 &d6
, dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3899 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3900 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3902 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3903 &d6
, dport
, INPLOOKUP_WILDCARD
,
3904 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3912 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3913 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
,
3914 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3923 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
, &daddr
->v6
,
3924 dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3926 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
,
3927 &daddr
->v6
, dport
, INPLOOKUP_WILDCARD
,
3928 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3943 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3947 u_int8_t
*opt
, optlen
;
3948 u_int8_t wscale
= 0;
3950 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3951 if (hlen
<= (int)sizeof (struct tcphdr
))
3953 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3955 opt
= hdr
+ sizeof (struct tcphdr
);
3956 hlen
-= sizeof (struct tcphdr
);
3966 if (wscale
> TCP_MAX_WINSHIFT
)
3967 wscale
= TCP_MAX_WINSHIFT
;
3968 wscale
|= PF_WSCALE_FLAG
;
3983 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3987 u_int8_t
*opt
, optlen
;
3988 u_int16_t mss
= tcp_mssdflt
;
3990 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3991 if (hlen
<= (int)sizeof (struct tcphdr
))
3993 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3995 opt
= hdr
+ sizeof (struct tcphdr
);
3996 hlen
-= sizeof (struct tcphdr
);
3997 while (hlen
>= TCPOLEN_MAXSEG
) {
4005 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, 2);
4006 #if BYTE_ORDER != BIG_ENDIAN
4023 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
4026 struct sockaddr_in
*dst
;
4030 struct sockaddr_in6
*dst6
;
4031 struct route_in6 ro6
;
4033 struct rtentry
*rt
= NULL
;
4035 u_int16_t mss
= tcp_mssdflt
;
4040 hlen
= sizeof (struct ip
);
4041 bzero(&ro
, sizeof (ro
));
4042 dst
= (struct sockaddr_in
*)(void *)&ro
.ro_dst
;
4043 dst
->sin_family
= AF_INET
;
4044 dst
->sin_len
= sizeof (*dst
);
4045 dst
->sin_addr
= addr
->v4
;
4052 hlen
= sizeof (struct ip6_hdr
);
4053 bzero(&ro6
, sizeof (ro6
));
4054 dst6
= (struct sockaddr_in6
*)(void *)&ro6
.ro_dst
;
4055 dst6
->sin6_family
= AF_INET6
;
4056 dst6
->sin6_len
= sizeof (*dst6
);
4057 dst6
->sin6_addr
= addr
->v6
;
4058 rtalloc((struct route
*)&ro
);
4063 panic("pf_calc_mss: not AF_INET or AF_INET6!");
4067 if (rt
&& rt
->rt_ifp
) {
4068 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof (struct tcphdr
);
4069 mss
= max(tcp_mssdflt
, mss
);
4072 mss
= min(mss
, offer
);
4073 mss
= max(mss
, 64); /* sanity - at least max opt space */
4078 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
, sa_family_t af
)
4080 struct pf_rule
*r
= s
->rule
.ptr
;
4084 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
4086 if ((af
== AF_INET
) || (af
== AF_INET6
)) {
4087 pf_map_addr(af
, r
, saddr
, &s
->rt_addr
, NULL
,
4089 s
->rt_kif
= r
->rpool
.cur
->kif
;
4096 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
4101 /* list is sorted, if-bound states before floating */
4103 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
4105 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
4109 pf_detach_state(struct pf_state
*s
, int flags
)
4111 struct pf_state_key
*sk
= s
->state_key
;
4116 s
->state_key
= NULL
;
4117 TAILQ_REMOVE(&sk
->states
, s
, next
);
4118 if (--sk
->refcnt
== 0) {
4119 if (!(flags
& PF_DT_SKIP_EXTGWY
))
4120 RB_REMOVE(pf_state_tree_ext_gwy
,
4121 &pf_statetbl_ext_gwy
, sk
);
4122 if (!(flags
& PF_DT_SKIP_LANEXT
))
4123 RB_REMOVE(pf_state_tree_lan_ext
,
4124 &pf_statetbl_lan_ext
, sk
);
4126 pool_put(&pf_app_state_pl
, sk
->app_state
);
4127 pool_put(&pf_state_key_pl
, sk
);
4131 struct pf_state_key
*
4132 pf_alloc_state_key(struct pf_state
*s
, struct pf_state_key
*psk
)
4134 struct pf_state_key
*sk
;
4136 if ((sk
= pool_get(&pf_state_key_pl
, PR_WAITOK
)) == NULL
)
4138 bzero(sk
, sizeof (*sk
));
4139 TAILQ_INIT(&sk
->states
);
4140 pf_attach_state(sk
, s
, 0);
4142 /* initialize state key from psk, if provided */
4144 bcopy(&psk
->lan
, &sk
->lan
, sizeof (sk
->lan
));
4145 bcopy(&psk
->gwy
, &sk
->gwy
, sizeof (sk
->gwy
));
4146 bcopy(&psk
->ext_lan
, &sk
->ext_lan
, sizeof (sk
->ext_lan
));
4147 bcopy(&psk
->ext_gwy
, &sk
->ext_gwy
, sizeof (sk
->ext_gwy
));
4148 sk
->af_lan
= psk
->af_lan
;
4149 sk
->af_gwy
= psk
->af_gwy
;
4150 sk
->proto
= psk
->proto
;
4151 sk
->direction
= psk
->direction
;
4152 sk
->proto_variant
= psk
->proto_variant
;
4153 VERIFY(psk
->app_state
== NULL
);
4154 sk
->flowsrc
= psk
->flowsrc
;
4155 sk
->flowhash
= psk
->flowhash
;
4156 /* don't touch tree entries, states and refcnt on sk */
4163 pf_tcp_iss(struct pf_pdesc
*pd
)
4166 u_int32_t digest
[4];
4168 if (pf_tcp_secret_init
== 0) {
4169 read_random(pf_tcp_secret
, sizeof (pf_tcp_secret
));
4170 MD5Init(&pf_tcp_secret_ctx
);
4171 MD5Update(&pf_tcp_secret_ctx
, pf_tcp_secret
,
4172 sizeof (pf_tcp_secret
));
4173 pf_tcp_secret_init
= 1;
4175 ctx
= pf_tcp_secret_ctx
;
4177 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_sport
, sizeof (u_short
));
4178 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_dport
, sizeof (u_short
));
4179 if (pd
->af
== AF_INET6
) {
4180 MD5Update(&ctx
, (char *)&pd
->src
->v6
, sizeof (struct in6_addr
));
4181 MD5Update(&ctx
, (char *)&pd
->dst
->v6
, sizeof (struct in6_addr
));
4183 MD5Update(&ctx
, (char *)&pd
->src
->v4
, sizeof (struct in_addr
));
4184 MD5Update(&ctx
, (char *)&pd
->dst
->v4
, sizeof (struct in_addr
));
4186 MD5Final((u_char
*)digest
, &ctx
);
4187 pf_tcp_iss_off
+= 4096;
4188 return (digest
[0] + random() + pf_tcp_iss_off
);
4192 * This routine is called to perform address family translation on the
4193 * inner IP header (that may come as payload) of an ICMP(v4/6) error
4197 pf_change_icmp_af(struct mbuf
*m
, int off
,
4198 struct pf_pdesc
*pd
, struct pf_pdesc
*pd2
, struct pf_addr
*src
,
4199 struct pf_addr
*dst
, sa_family_t af
, sa_family_t naf
)
4201 struct mbuf
*n
= NULL
;
4202 struct ip
*ip4
= NULL
;
4203 struct ip6_hdr
*ip6
= NULL
;
4204 int hlen
, olen
, mlen
;
4206 if (af
== naf
|| (af
!= AF_INET
&& af
!= AF_INET6
) ||
4207 (naf
!= AF_INET
&& naf
!= AF_INET6
))
4210 /* split the mbuf chain on the inner ip/ip6 header boundary */
4211 if ((n
= m_split(m
, off
, M_DONTWAIT
)) == NULL
)
4215 olen
= pd2
->off
- off
;
4217 hlen
= naf
== AF_INET
? sizeof(*ip4
) : sizeof(*ip6
);
4219 /* trim old header */
4222 /* prepend a new one */
4223 if (M_PREPEND(n
, hlen
, M_DONTWAIT
, 0) == NULL
)
4226 /* translate inner ip/ip6 header */
4229 ip4
= mtod(n
, struct ip
*);
4230 bzero(ip4
, sizeof(*ip4
));
4231 ip4
->ip_v
= IPVERSION
;
4232 ip4
->ip_hl
= sizeof(*ip4
) >> 2;
4233 ip4
->ip_len
= htons(sizeof(*ip4
) + pd2
->tot_len
- olen
);
4234 ip4
->ip_id
= htons(ip_randomid());
4235 ip4
->ip_off
= htons(IP_DF
);
4236 ip4
->ip_ttl
= pd2
->ttl
;
4237 if (pd2
->proto
== IPPROTO_ICMPV6
)
4238 ip4
->ip_p
= IPPROTO_ICMP
;
4240 ip4
->ip_p
= pd2
->proto
;
4241 ip4
->ip_src
= src
->v4
;
4242 ip4
->ip_dst
= dst
->v4
;
4243 ip4
->ip_sum
= in_cksum(n
, ip4
->ip_hl
<< 2);
4246 ip6
= mtod(n
, struct ip6_hdr
*);
4247 bzero(ip6
, sizeof(*ip6
));
4248 ip6
->ip6_vfc
= IPV6_VERSION
;
4249 ip6
->ip6_plen
= htons(pd2
->tot_len
- olen
);
4250 if (pd2
->proto
== IPPROTO_ICMP
)
4251 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
4253 ip6
->ip6_nxt
= pd2
->proto
;
4254 if (!pd2
->ttl
|| pd2
->ttl
> IPV6_DEFHLIM
)
4255 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
4257 ip6
->ip6_hlim
= pd2
->ttl
;
4258 ip6
->ip6_src
= src
->v6
;
4259 ip6
->ip6_dst
= dst
->v6
;
4263 /* adjust payload offset and total packet length */
4264 pd2
->off
+= hlen
- olen
;
4265 pd
->tot_len
+= hlen
- olen
;
4267 /* merge modified inner packet with the original header */
4268 mlen
= n
->m_pkthdr
.len
;
4270 m
->m_pkthdr
.len
+= mlen
;
4275 #define PTR_IP(field) ((int32_t)offsetof(struct ip, field))
4276 #define PTR_IP6(field) ((int32_t)offsetof(struct ip6_hdr, field))
4279 pf_translate_icmp_af(int af
, void *arg
)
4282 struct icmp6_hdr
*icmp6
;
4291 type
= icmp6
->icmp6_type
;
4292 code
= icmp6
->icmp6_code
;
4293 mtu
= ntohl(icmp6
->icmp6_mtu
);
4296 case ICMP6_ECHO_REQUEST
:
4299 case ICMP6_ECHO_REPLY
:
4300 type
= ICMP_ECHOREPLY
;
4302 case ICMP6_DST_UNREACH
:
4303 type
= ICMP_UNREACH
;
4305 case ICMP6_DST_UNREACH_NOROUTE
:
4306 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
4307 case ICMP6_DST_UNREACH_ADDR
:
4308 code
= ICMP_UNREACH_HOST
;
4310 case ICMP6_DST_UNREACH_ADMIN
:
4311 code
= ICMP_UNREACH_HOST_PROHIB
;
4313 case ICMP6_DST_UNREACH_NOPORT
:
4314 code
= ICMP_UNREACH_PORT
;
4320 case ICMP6_PACKET_TOO_BIG
:
4321 type
= ICMP_UNREACH
;
4322 code
= ICMP_UNREACH_NEEDFRAG
;
4325 case ICMP6_TIME_EXCEEDED
:
4326 type
= ICMP_TIMXCEED
;
4328 case ICMP6_PARAM_PROB
:
4330 case ICMP6_PARAMPROB_HEADER
:
4331 type
= ICMP_PARAMPROB
;
4332 code
= ICMP_PARAMPROB_ERRATPTR
;
4333 ptr
= ntohl(icmp6
->icmp6_pptr
);
4335 if (ptr
== PTR_IP6(ip6_vfc
))
4337 else if (ptr
== PTR_IP6(ip6_vfc
) + 1)
4338 ptr
= PTR_IP(ip_tos
);
4339 else if (ptr
== PTR_IP6(ip6_plen
) ||
4340 ptr
== PTR_IP6(ip6_plen
) + 1)
4341 ptr
= PTR_IP(ip_len
);
4342 else if (ptr
== PTR_IP6(ip6_nxt
))
4344 else if (ptr
== PTR_IP6(ip6_hlim
))
4345 ptr
= PTR_IP(ip_ttl
);
4346 else if (ptr
>= PTR_IP6(ip6_src
) &&
4347 ptr
< PTR_IP6(ip6_dst
))
4348 ptr
= PTR_IP(ip_src
);
4349 else if (ptr
>= PTR_IP6(ip6_dst
) &&
4350 ptr
< (int32_t)sizeof(struct ip6_hdr
))
4351 ptr
= PTR_IP(ip_dst
);
4356 case ICMP6_PARAMPROB_NEXTHEADER
:
4357 type
= ICMP_UNREACH
;
4358 code
= ICMP_UNREACH_PROTOCOL
;
4367 icmp6
->icmp6_type
= type
;
4368 icmp6
->icmp6_code
= code
;
4369 /* aligns well with a icmpv4 nextmtu */
4370 icmp6
->icmp6_mtu
= htonl(mtu
);
4371 /* icmpv4 pptr is a one most significant byte */
4373 icmp6
->icmp6_pptr
= htonl(ptr
<< 24);
4378 type
= icmp4
->icmp_type
;
4379 code
= icmp4
->icmp_code
;
4380 mtu
= ntohs(icmp4
->icmp_nextmtu
);
4384 type
= ICMP6_ECHO_REQUEST
;
4386 case ICMP_ECHOREPLY
:
4387 type
= ICMP6_ECHO_REPLY
;
4390 type
= ICMP6_DST_UNREACH
;
4392 case ICMP_UNREACH_NET
:
4393 case ICMP_UNREACH_HOST
:
4394 case ICMP_UNREACH_NET_UNKNOWN
:
4395 case ICMP_UNREACH_HOST_UNKNOWN
:
4396 case ICMP_UNREACH_ISOLATED
:
4397 case ICMP_UNREACH_TOSNET
:
4398 case ICMP_UNREACH_TOSHOST
:
4399 code
= ICMP6_DST_UNREACH_NOROUTE
;
4401 case ICMP_UNREACH_PORT
:
4402 code
= ICMP6_DST_UNREACH_NOPORT
;
4404 case ICMP_UNREACH_NET_PROHIB
:
4405 case ICMP_UNREACH_HOST_PROHIB
:
4406 case ICMP_UNREACH_FILTER_PROHIB
:
4407 case ICMP_UNREACH_PRECEDENCE_CUTOFF
:
4408 code
= ICMP6_DST_UNREACH_ADMIN
;
4410 case ICMP_UNREACH_PROTOCOL
:
4411 type
= ICMP6_PARAM_PROB
;
4412 code
= ICMP6_PARAMPROB_NEXTHEADER
;
4413 ptr
= offsetof(struct ip6_hdr
, ip6_nxt
);
4415 case ICMP_UNREACH_NEEDFRAG
:
4416 type
= ICMP6_PACKET_TOO_BIG
;
4425 type
= ICMP6_TIME_EXCEEDED
;
4427 case ICMP_PARAMPROB
:
4428 type
= ICMP6_PARAM_PROB
;
4430 case ICMP_PARAMPROB_ERRATPTR
:
4431 code
= ICMP6_PARAMPROB_HEADER
;
4433 case ICMP_PARAMPROB_LENGTH
:
4434 code
= ICMP6_PARAMPROB_HEADER
;
4440 ptr
= icmp4
->icmp_pptr
;
4441 if (ptr
== 0 || ptr
== PTR_IP(ip_tos
))
4443 else if (ptr
== PTR_IP(ip_len
) ||
4444 ptr
== PTR_IP(ip_len
) + 1)
4445 ptr
= PTR_IP6(ip6_plen
);
4446 else if (ptr
== PTR_IP(ip_ttl
))
4447 ptr
= PTR_IP6(ip6_hlim
);
4448 else if (ptr
== PTR_IP(ip_p
))
4449 ptr
= PTR_IP6(ip6_nxt
);
4450 else if (ptr
>= PTR_IP(ip_src
) &&
4451 ptr
< PTR_IP(ip_dst
))
4452 ptr
= PTR_IP6(ip6_src
);
4453 else if (ptr
>= PTR_IP(ip_dst
) &&
4454 ptr
< (int32_t)sizeof(struct ip
))
4455 ptr
= PTR_IP6(ip6_dst
);
4463 icmp4
->icmp_type
= type
;
4464 icmp4
->icmp_code
= code
;
4465 icmp4
->icmp_nextmtu
= htons(mtu
);
4467 icmp4
->icmp_void
= htonl(ptr
);
4475 pf_nat64_ipv6(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
)
4480 * ip_input asserts for rcvif to be not NULL
4481 * That may not be true for two corner cases
4482 * 1. If for some reason a local app sends DNS
4483 * AAAA query to local host
4484 * 2. If IPv6 stack in kernel internally generates a
4485 * message destined for a synthesized IPv6 end-point.
4487 if (m
->m_pkthdr
.rcvif
== NULL
)
4490 /* trim the old header */
4493 /* prepend the new one */
4494 if (M_PREPEND(m
, sizeof(*ip4
), M_DONTWAIT
, 0) == NULL
)
4497 ip4
= mtod(m
, struct ip
*);
4500 ip4
->ip_tos
= pd
->tos
& htonl(0x0ff00000);
4501 ip4
->ip_len
= htons(sizeof(*ip4
) + (pd
->tot_len
- off
));
4503 ip4
->ip_off
= htons(IP_DF
);
4504 ip4
->ip_ttl
= pd
->ttl
;
4505 ip4
->ip_p
= pd
->proto
;
4507 ip4
->ip_src
= pd
->naddr
.v4
;
4508 ip4
->ip_dst
= pd
->ndaddr
.v4
;
4509 ip4
->ip_sum
= in_cksum(m
, ip4
->ip_hl
<< 2);
4511 /* recalculate icmp checksums */
4512 if (pd
->proto
== IPPROTO_ICMP
) {
4515 int moff
, hlen
= sizeof(*ip4
);
4517 if ((mp
= m_pulldown(m
, hlen
, ICMP_MINLEN
, &moff
)) == NULL
)
4520 icmp
= (struct icmp
*)(void *)(mtod(mp
, char *) + moff
);
4521 icmp
->icmp_cksum
= 0;
4522 icmp
->icmp_cksum
= inet_cksum(m
, 0, hlen
,
4523 ntohs(ip4
->ip_len
) - hlen
);
4531 pf_nat64_ipv4(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
)
4533 struct ip6_hdr
*ip6
;
4535 if (m
->m_pkthdr
.rcvif
== NULL
)
4539 if (M_PREPEND(m
, sizeof(*ip6
), M_DONTWAIT
, 0) == NULL
)
4542 ip6
= mtod(m
, struct ip6_hdr
*);
4543 ip6
->ip6_vfc
= htonl((6 << 28) | (pd
->tos
<< 20));
4544 ip6
->ip6_plen
= htons(pd
->tot_len
- off
);
4545 ip6
->ip6_nxt
= pd
->proto
;
4546 ip6
->ip6_hlim
= pd
->ttl
;
4547 ip6
->ip6_src
= pd
->naddr
.v6
;
4548 ip6
->ip6_dst
= pd
->ndaddr
.v6
;
4550 /* recalculate icmp6 checksums */
4551 if (pd
->proto
== IPPROTO_ICMPV6
) {
4553 struct icmp6_hdr
*icmp6
;
4554 int moff
, hlen
= sizeof(*ip6
);
4556 if ((mp
= m_pulldown(m
, hlen
, sizeof(*icmp6
), &moff
)) == NULL
)
4559 icmp6
= (struct icmp6_hdr
*)(void *)(mtod(mp
, char *) + moff
);
4560 icmp6
->icmp6_cksum
= 0;
4561 icmp6
->icmp6_cksum
= inet6_cksum(m
, IPPROTO_ICMPV6
, hlen
,
4562 ntohs(ip6
->ip6_plen
));
4563 } else if (pd
->proto
== IPPROTO_UDP
) {
4566 int moff
, hlen
= sizeof(*ip6
);
4567 if ((mp
= m_pulldown(m
, hlen
, sizeof(*uh
), &moff
)) == NULL
)
4569 uh
= (struct udphdr
*)(void *)(mtod(mp
, char *) + moff
);
4570 if (uh
->uh_sum
== 0)
4571 uh
->uh_sum
= inet6_cksum(m
, IPPROTO_UDP
, hlen
,
4572 ntohs(ip6
->ip6_plen
));
4580 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
4581 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
4582 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
4583 struct ifqueue
*ifq
)
4586 struct pf_rule
*nr
= NULL
;
4587 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4588 sa_family_t af
= pd
->af
;
4589 struct pf_rule
*r
, *a
= NULL
;
4590 struct pf_ruleset
*ruleset
= NULL
;
4591 struct pf_src_node
*nsn
= NULL
;
4592 struct tcphdr
*th
= pd
->hdr
.tcp
;
4593 struct udphdr
*uh
= pd
->hdr
.udp
;
4595 int rewrite
= 0, hdrlen
= 0;
4597 unsigned int rtableid
= IFSCOPE_NONE
;
4601 u_int16_t mss
= tcp_mssdflt
;
4602 u_int8_t icmptype
= 0, icmpcode
= 0;
4604 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
4605 union pf_state_xport bxport
, bdxport
, nxport
, sxport
, dxport
;
4606 struct pf_state_key psk
;
4608 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
4610 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
4611 REASON_SET(&reason
, PFRES_CONGEST
);
4620 switch (pd
->proto
) {
4622 sxport
.port
= th
->th_sport
;
4623 dxport
.port
= th
->th_dport
;
4624 hdrlen
= sizeof (*th
);
4627 sxport
.port
= uh
->uh_sport
;
4628 dxport
.port
= uh
->uh_dport
;
4629 hdrlen
= sizeof (*uh
);
4633 if (pd
->af
!= AF_INET
)
4635 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
4636 hdrlen
= ICMP_MINLEN
;
4637 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4638 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
4640 if (icmptype
== ICMP_UNREACH
||
4641 icmptype
== ICMP_SOURCEQUENCH
||
4642 icmptype
== ICMP_REDIRECT
||
4643 icmptype
== ICMP_TIMXCEED
||
4644 icmptype
== ICMP_PARAMPROB
)
4649 case IPPROTO_ICMPV6
:
4650 if (pd
->af
!= AF_INET6
)
4652 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
4653 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
4654 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4655 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
4657 if (icmptype
== ICMP6_DST_UNREACH
||
4658 icmptype
== ICMP6_PACKET_TOO_BIG
||
4659 icmptype
== ICMP6_TIME_EXCEEDED
||
4660 icmptype
== ICMP6_PARAM_PROB
)
4665 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
4666 sxport
.call_id
= dxport
.call_id
=
4667 pd
->hdr
.grev1
->call_id
;
4668 hdrlen
= sizeof (*pd
->hdr
.grev1
);
4673 dxport
.spi
= pd
->hdr
.esp
->spi
;
4674 hdrlen
= sizeof (*pd
->hdr
.esp
);
4678 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
4683 if (direction
== PF_OUT
)
4688 /* check packet for BINAT/NAT/RDR */
4689 if ((nr
= pf_get_translation_aux(pd
, m
, off
, direction
, kif
, &nsn
,
4690 saddr
, &sxport
, daddr
, &dxport
, &nxport
)) !=
4695 if (pd
->af
!= pd
->naf
)
4700 PF_ACPY(&pd
->baddr
, saddr
, af
);
4701 PF_ACPY(&pd
->bdaddr
, daddr
, af
);
4703 switch (pd
->proto
) {
4705 if (pd
->af
!= pd
->naf
||
4706 PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4707 pf_change_ap(direction
, pd
->mp
, saddr
,
4708 &th
->th_sport
, pd
->ip_sum
, &th
->th_sum
,
4709 &pd
->naddr
, nxport
.port
, 0, af
,
4711 sxport
.port
= th
->th_sport
;
4714 if (pd
->af
!= pd
->naf
||
4715 PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
) ||
4716 (nr
&& (nr
->action
== PF_RDR
) &&
4717 (th
->th_dport
!= nxport
.port
))) {
4718 if (nr
&& nr
->action
== PF_RDR
)
4719 dport
= nxport
.port
;
4721 dport
= th
->th_dport
;
4722 pf_change_ap(direction
, pd
->mp
, daddr
,
4723 &th
->th_dport
, pd
->ip_sum
,
4724 &th
->th_sum
, &pd
->ndaddr
,
4725 dport
, 0, af
, pd
->naf
, ua
);
4726 dxport
.port
= th
->th_dport
;
4732 if (pd
->af
!= pd
->naf
||
4733 PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4734 pf_change_ap(direction
, pd
->mp
, saddr
,
4735 &uh
->uh_sport
, pd
->ip_sum
,
4736 &uh
->uh_sum
, &pd
->naddr
,
4737 nxport
.port
, 1, af
, pd
->naf
, ua
);
4738 sxport
.port
= uh
->uh_sport
;
4741 if (pd
->af
!= pd
->naf
||
4742 PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
) ||
4743 (nr
&& (nr
->action
== PF_RDR
) &&
4744 (uh
->uh_dport
!= nxport
.port
))) {
4745 if (nr
&& nr
->action
== PF_RDR
)
4746 dport
= nxport
.port
;
4748 dport
= uh
->uh_dport
;
4749 pf_change_ap(direction
, pd
->mp
, daddr
,
4750 &uh
->uh_dport
, pd
->ip_sum
,
4751 &uh
->uh_sum
, &pd
->ndaddr
,
4752 dport
, 0, af
, pd
->naf
, ua
);
4753 dxport
.port
= uh
->uh_dport
;
4759 if (pd
->af
!= AF_INET
)
4763 * pd->af != pd->naf not handled yet here and would be
4764 * needed for NAT46 needed to support XLAT.
4765 * Will cross the bridge when it comes.
4767 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4768 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
4769 pd
->naddr
.v4
.s_addr
, 0);
4770 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4771 pd
->hdr
.icmp
->icmp_cksum
, sxport
.port
,
4773 pd
->hdr
.icmp
->icmp_id
= nxport
.port
;
4776 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
)) {
4777 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
4778 pd
->ndaddr
.v4
.s_addr
, 0);
4784 case IPPROTO_ICMPV6
:
4785 if (pd
->af
!= AF_INET6
)
4788 if (pd
->af
!= pd
->naf
||
4789 PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4790 pf_change_addr(saddr
,
4791 &pd
->hdr
.icmp6
->icmp6_cksum
,
4792 &pd
->naddr
, 0, pd
->af
, pd
->naf
);
4795 if (pd
->af
!= pd
->naf
||
4796 PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
)) {
4797 pf_change_addr(daddr
,
4798 &pd
->hdr
.icmp6
->icmp6_cksum
,
4799 &pd
->ndaddr
, 0, pd
->af
, pd
->naf
);
4802 if (pd
->af
!= pd
->naf
) {
4803 if (pf_translate_icmp_af(AF_INET
,
4806 pd
->proto
= IPPROTO_ICMP
;
4812 if ((direction
== PF_IN
) &&
4813 (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
))
4814 grev1
->call_id
= nxport
.call_id
;
4819 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4820 pf_change_a(&saddr
->v4
.s_addr
,
4822 pd
->naddr
.v4
.s_addr
, 0);
4824 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
)) {
4825 pf_change_a(&daddr
->v4
.s_addr
,
4827 pd
->ndaddr
.v4
.s_addr
, 0);
4833 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
))
4834 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4835 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
))
4836 PF_ACPY(daddr
, &pd
->ndaddr
, AF_INET6
);
4843 if (direction
== PF_OUT
)
4849 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
)) {
4850 pf_change_a(&saddr
->v4
.s_addr
,
4851 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4853 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
)) {
4854 pf_change_a(&daddr
->v4
.s_addr
,
4856 pd
->ndaddr
.v4
.s_addr
, 0);
4862 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
))
4863 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4864 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
))
4865 PF_ACPY(daddr
, &pd
->ndaddr
, AF_INET6
);
4874 if ((pd
->naf
!= AF_INET
) ||
4875 (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
))) {
4876 pf_change_addr(saddr
, pd
->ip_sum
,
4877 &pd
->naddr
, 0, af
, pd
->naf
);
4880 if ((pd
->naf
!= AF_INET
) ||
4881 (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
))) {
4882 pf_change_addr(daddr
, pd
->ip_sum
,
4883 &pd
->ndaddr
, 0, af
, pd
->naf
);
4889 if (PF_ANEQ(saddr
, &pd
->naddr
, pd
->af
))
4890 PF_ACPY(saddr
, &pd
->naddr
, af
);
4891 if (PF_ANEQ(daddr
, &pd
->ndaddr
, pd
->af
))
4892 PF_ACPY(daddr
, &pd
->ndaddr
, af
);
4905 if (nr
&& nr
->tag
> 0)
4910 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
4911 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
4912 else if (r
->direction
&& r
->direction
!= direction
)
4913 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
4914 else if (r
->af
&& r
->af
!= pd
->af
)
4915 r
= r
->skip
[PF_SKIP_AF
].ptr
;
4916 else if (r
->proto
&& r
->proto
!= pd
->proto
)
4917 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
4918 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, pd
->af
,
4920 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
4921 /* tcp/udp only. port_op always 0 in other cases */
4922 else if (r
->proto
== pd
->proto
&&
4923 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4924 r
->src
.xport
.range
.op
&&
4925 !pf_match_port(r
->src
.xport
.range
.op
,
4926 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
4928 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
4929 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, pd
->af
,
4931 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
4932 /* tcp/udp only. port_op always 0 in other cases */
4933 else if (r
->proto
== pd
->proto
&&
4934 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4935 r
->dst
.xport
.range
.op
&&
4936 !pf_match_port(r
->dst
.xport
.range
.op
,
4937 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
4939 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
4940 /* icmp only. type always 0 in other cases */
4941 else if (r
->type
&& r
->type
!= icmptype
+ 1)
4942 r
= TAILQ_NEXT(r
, entries
);
4943 /* icmp only. type always 0 in other cases */
4944 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
4945 r
= TAILQ_NEXT(r
, entries
);
4946 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
4947 !(r
->tos
& pd
->tos
))
4948 r
= TAILQ_NEXT(r
, entries
);
4949 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
4950 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
4951 r
= TAILQ_NEXT(r
, entries
);
4952 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
4953 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
4954 r
= TAILQ_NEXT(r
, entries
);
4955 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
4956 r
= TAILQ_NEXT(r
, entries
);
4957 else if (pd
->proto
== IPPROTO_TCP
&&
4958 (r
->flagset
& th
->th_flags
) != r
->flags
)
4959 r
= TAILQ_NEXT(r
, entries
);
4960 /* tcp/udp only. uid.op always 0 in other cases */
4961 else if (r
->uid
.op
&& (pd
->lookup
.done
|| ((void)(pd
->lookup
.done
=
4962 pf_socket_lookup(direction
, pd
)), 1)) &&
4963 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
4965 r
= TAILQ_NEXT(r
, entries
);
4966 /* tcp/udp only. gid.op always 0 in other cases */
4967 else if (r
->gid
.op
&& (pd
->lookup
.done
|| ((void)(pd
->lookup
.done
=
4968 pf_socket_lookup(direction
, pd
)), 1)) &&
4969 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
4971 r
= TAILQ_NEXT(r
, entries
);
4972 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
4973 r
= TAILQ_NEXT(r
, entries
);
4974 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
4975 r
= TAILQ_NEXT(r
, entries
);
4976 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
4977 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
4978 pf_osfp_fingerprint(pd
, m
, off
, th
),
4979 r
->os_fingerprint
)))
4980 r
= TAILQ_NEXT(r
, entries
);
4984 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
4985 rtableid
= r
->rtableid
;
4986 if (r
->anchor
== NULL
) {
4993 r
= TAILQ_NEXT(r
, entries
);
4995 pf_step_into_anchor(&asd
, &ruleset
,
4996 PF_RULESET_FILTER
, &r
, &a
, &match
);
4998 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
4999 PF_RULESET_FILTER
, &r
, &a
, &match
))
5006 REASON_SET(&reason
, PFRES_MATCH
);
5008 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
5010 if (rewrite
< off
+ hdrlen
)
5011 rewrite
= off
+ hdrlen
;
5013 m
= pf_lazy_makewritable(pd
, m
, rewrite
);
5015 REASON_SET(&reason
, PFRES_MEMORY
);
5019 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5021 PFLOG_PACKET(kif
, h
, m
, pd
->af
, direction
, reason
,
5022 r
->log
? r
: nr
, a
, ruleset
, pd
);
5025 if ((r
->action
== PF_DROP
) &&
5026 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
5027 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
5028 (r
->rule_flag
& PFRULE_RETURN
))) {
5029 /* undo NAT changes, if they have taken place */
5030 /* XXX For NAT64 we are not reverting the changes */
5031 if (nr
!= NULL
&& nr
->action
!= PF_NAT64
) {
5032 if (direction
== PF_OUT
) {
5034 switch (pd
->proto
) {
5036 pf_change_ap(direction
, pd
->mp
, saddr
,
5037 &th
->th_sport
, pd
->ip_sum
,
5038 &th
->th_sum
, &pd
->baddr
,
5039 bxport
.port
, 0, af
, pd
->af
, 1);
5040 sxport
.port
= th
->th_sport
;
5044 pf_change_ap(direction
, pd
->mp
, saddr
,
5045 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
5046 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
5047 bxport
.port
, 1, af
, pd
->af
, 1);
5048 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
5053 case IPPROTO_ICMPV6
:
5058 PF_ACPY(&pd
->baddr
, saddr
, af
);
5063 pf_change_a(&saddr
->v4
.s_addr
,
5065 pd
->baddr
.v4
.s_addr
, 0);
5070 PF_ACPY(saddr
, &pd
->baddr
,
5077 PF_ACPY(&pd
->baddr
, saddr
, af
);
5081 pf_change_a(&saddr
->v4
.s_addr
,
5083 pd
->baddr
.v4
.s_addr
, 0);
5088 PF_ACPY(saddr
, &pd
->baddr
,
5097 pf_change_a(&saddr
->v4
.s_addr
,
5099 pd
->baddr
.v4
.s_addr
, 0);
5102 PF_ACPY(saddr
, &pd
->baddr
, af
);
5107 switch (pd
->proto
) {
5109 pf_change_ap(direction
, pd
->mp
, daddr
,
5110 &th
->th_dport
, pd
->ip_sum
,
5111 &th
->th_sum
, &pd
->bdaddr
,
5112 bdxport
.port
, 0, af
, pd
->af
, 1);
5113 dxport
.port
= th
->th_dport
;
5117 pf_change_ap(direction
, pd
->mp
, daddr
,
5118 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
5119 &pd
->hdr
.udp
->uh_sum
, &pd
->bdaddr
,
5120 bdxport
.port
, 1, af
, pd
->af
, 1);
5121 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
5126 case IPPROTO_ICMPV6
:
5131 if (pd
->proto_variant
==
5132 PF_GRE_PPTP_VARIANT
)
5139 pf_change_a(&daddr
->v4
.s_addr
,
5141 pd
->bdaddr
.v4
.s_addr
, 0);
5146 PF_ACPY(daddr
, &pd
->bdaddr
,
5156 pf_change_a(&daddr
->v4
.s_addr
,
5158 pd
->bdaddr
.v4
.s_addr
, 0);
5163 PF_ACPY(daddr
, &pd
->bdaddr
,
5172 pf_change_a(&daddr
->v4
.s_addr
,
5174 pd
->bdaddr
.v4
.s_addr
, 0);
5178 PF_ACPY(daddr
, &pd
->bdaddr
, af
);
5185 if (pd
->proto
== IPPROTO_TCP
&&
5186 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
5187 (r
->rule_flag
& PFRULE_RETURN
)) &&
5188 !(th
->th_flags
& TH_RST
)) {
5189 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
5198 h4
= mtod(m
, struct ip
*);
5199 len
= ntohs(h4
->ip_len
) - off
;
5203 h6
= mtod(m
, struct ip6_hdr
*);
5204 len
= ntohs(h6
->ip6_plen
) -
5205 (off
- sizeof (*h6
));
5210 if (pf_check_proto_cksum(m
, off
, len
, IPPROTO_TCP
,
5212 REASON_SET(&reason
, PFRES_PROTCKSUM
);
5214 if (th
->th_flags
& TH_SYN
)
5216 if (th
->th_flags
& TH_FIN
)
5218 pf_send_tcp(r
, pd
->af
, pd
->dst
,
5219 pd
->src
, th
->th_dport
, th
->th_sport
,
5220 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
5221 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
5223 } else if (pd
->proto
!= IPPROTO_ICMP
&& pd
->af
== AF_INET
&&
5224 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
5226 pf_send_icmp(m
, r
->return_icmp
>> 8,
5227 r
->return_icmp
& 255, pd
->af
, r
);
5228 else if (pd
->proto
!= IPPROTO_ICMPV6
&& af
== AF_INET6
&&
5229 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
5231 pf_send_icmp(m
, r
->return_icmp6
>> 8,
5232 r
->return_icmp6
& 255, pd
->af
, r
);
5235 if (r
->action
== PF_DROP
)
5238 /* prepare state key, for flowhash and/or the state (if created) */
5239 bzero(&psk
, sizeof (psk
));
5240 psk
.proto
= pd
->proto
;
5241 psk
.direction
= direction
;
5242 if (pd
->proto
== IPPROTO_UDP
) {
5243 if (ntohs(pd
->hdr
.udp
->uh_sport
) == PF_IKE_PORT
&&
5244 ntohs(pd
->hdr
.udp
->uh_dport
) == PF_IKE_PORT
) {
5245 psk
.proto_variant
= PF_EXTFILTER_APD
;
5247 psk
.proto_variant
= nr
? nr
->extfilter
: r
->extfilter
;
5248 if (psk
.proto_variant
< PF_EXTFILTER_APD
)
5249 psk
.proto_variant
= PF_EXTFILTER_APD
;
5251 } else if (pd
->proto
== IPPROTO_GRE
) {
5252 psk
.proto_variant
= pd
->proto_variant
;
5254 if (direction
== PF_OUT
) {
5256 PF_ACPY(&psk
.gwy
.addr
, saddr
, af
);
5257 PF_ACPY(&psk
.ext_gwy
.addr
, daddr
, af
);
5258 switch (pd
->proto
) {
5260 psk
.gwy
.xport
.spi
= 0;
5261 psk
.ext_gwy
.xport
.spi
= pd
->hdr
.esp
->spi
;
5265 case IPPROTO_ICMPV6
:
5268 * NAT64 requires protocol translation between ICMPv4
5269 * and ICMPv6. TCP and UDP do not require protocol
5270 * translation. To avoid adding complexity just to
5271 * handle ICMP(v4/v6), we always lookup for
5272 * proto = IPPROTO_ICMP on both LAN and WAN side
5274 psk
.proto
= IPPROTO_ICMP
;
5275 psk
.gwy
.xport
.port
= nxport
.port
;
5276 psk
.ext_gwy
.xport
.spi
= 0;
5279 psk
.gwy
.xport
= sxport
;
5280 psk
.ext_gwy
.xport
= dxport
;
5285 PF_ACPY(&psk
.lan
.addr
, &pd
->baddr
, af
);
5286 psk
.lan
.xport
= bxport
;
5287 PF_ACPY(&psk
.ext_lan
.addr
, &pd
->bdaddr
, af
);
5288 psk
.ext_lan
.xport
= bdxport
;
5290 PF_ACPY(&psk
.lan
.addr
, &psk
.gwy
.addr
, af
);
5291 psk
.lan
.xport
= psk
.gwy
.xport
;
5292 PF_ACPY(&psk
.ext_lan
.addr
, &psk
.ext_gwy
.addr
, af
);
5293 psk
.ext_lan
.xport
= psk
.ext_gwy
.xport
;
5297 if (nr
&& nr
->action
== PF_NAT64
) {
5298 PF_ACPY(&psk
.lan
.addr
, &pd
->baddr
, af
);
5299 PF_ACPY(&psk
.ext_lan
.addr
, &pd
->bdaddr
, af
);
5301 PF_ACPY(&psk
.lan
.addr
, daddr
, af
);
5302 PF_ACPY(&psk
.ext_lan
.addr
, saddr
, af
);
5304 switch (pd
->proto
) {
5307 case IPPROTO_ICMPV6
:
5310 * NAT64 requires protocol translation between ICMPv4
5311 * and ICMPv6. TCP and UDP do not require protocol
5312 * translation. To avoid adding complexity just to
5313 * handle ICMP(v4/v6), we always lookup for
5314 * proto = IPPROTO_ICMP on both LAN and WAN side
5316 psk
.proto
= IPPROTO_ICMP
;
5317 if (nr
&& nr
->action
== PF_NAT64
) {
5318 psk
.lan
.xport
= bxport
;
5319 psk
.ext_lan
.xport
= bxport
;
5321 psk
.lan
.xport
= nxport
;
5322 psk
.ext_lan
.xport
.spi
= 0;
5326 psk
.ext_lan
.xport
.spi
= 0;
5327 psk
.lan
.xport
.spi
= pd
->hdr
.esp
->spi
;
5331 if (nr
->action
== PF_NAT64
) {
5332 psk
.lan
.xport
= bxport
;
5333 psk
.ext_lan
.xport
= bdxport
;
5335 psk
.lan
.xport
= dxport
;
5336 psk
.ext_lan
.xport
= sxport
;
5339 psk
.lan
.xport
= dxport
;
5340 psk
.ext_lan
.xport
= sxport
;
5344 psk
.af_gwy
= pd
->naf
;
5346 if (nr
->action
== PF_NAT64
) {
5347 PF_ACPY(&psk
.gwy
.addr
, &pd
->naddr
, pd
->naf
);
5348 PF_ACPY(&psk
.ext_gwy
.addr
, &pd
->ndaddr
,
5350 if ((pd
->proto
== IPPROTO_ICMPV6
) ||
5351 (pd
->proto
== IPPROTO_ICMP
)) {
5352 psk
.gwy
.xport
= nxport
;
5353 psk
.ext_gwy
.xport
= nxport
;
5355 psk
.gwy
.xport
= sxport
;
5356 psk
.ext_gwy
.xport
= dxport
;
5359 PF_ACPY(&psk
.gwy
.addr
, &pd
->bdaddr
, af
);
5360 psk
.gwy
.xport
= bdxport
;
5361 PF_ACPY(&psk
.ext_gwy
.addr
, saddr
, af
);
5362 psk
.ext_gwy
.xport
= sxport
;
5365 PF_ACPY(&psk
.gwy
.addr
, &psk
.lan
.addr
, af
);
5366 psk
.gwy
.xport
= psk
.lan
.xport
;
5367 PF_ACPY(&psk
.ext_gwy
.addr
, &psk
.ext_lan
.addr
, af
);
5368 psk
.ext_gwy
.xport
= psk
.ext_lan
.xport
;
5371 if (pd
->pktflags
& PKTF_FLOW_ID
) {
5372 /* flow hash was already computed outside of PF */
5373 psk
.flowsrc
= pd
->flowsrc
;
5374 psk
.flowhash
= pd
->flowhash
;
5376 /* compute flow hash and store it in state key */
5377 psk
.flowsrc
= FLOWSRC_PF
;
5378 psk
.flowhash
= pf_calc_state_key_flowhash(&psk
);
5379 pd
->flowsrc
= psk
.flowsrc
;
5380 pd
->flowhash
= psk
.flowhash
;
5381 pd
->pktflags
|= PKTF_FLOW_ID
;
5382 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
5385 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5386 REASON_SET(&reason
, PFRES_MEMORY
);
5390 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
5391 (pd
->flags
& PFDESC_TCP_NORM
))) {
5392 /* create new state */
5393 struct pf_state
*s
= NULL
;
5394 struct pf_state_key
*sk
= NULL
;
5395 struct pf_src_node
*sn
= NULL
;
5396 struct pf_ike_hdr ike
;
5398 if (pd
->proto
== IPPROTO_UDP
) {
5399 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
5401 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
5402 ntohs(uh
->uh_dport
) == PF_IKE_PORT
&&
5403 plen
>= PF_IKE_PACKET_MINSIZE
) {
5404 if (plen
> PF_IKE_PACKET_MINSIZE
)
5405 plen
= PF_IKE_PACKET_MINSIZE
;
5406 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
5410 if (nr
!= NULL
&& pd
->proto
== IPPROTO_ESP
&&
5411 direction
== PF_OUT
) {
5412 struct pf_state_key_cmp sk0
;
5413 struct pf_state
*s0
;
5417 * This squelches state creation if the external
5418 * address matches an existing incomplete state with a
5419 * different internal address. Only one 'blocking'
5420 * partial state is allowed for each external address.
5422 memset(&sk0
, 0, sizeof (sk0
));
5423 sk0
.af_gwy
= pd
->af
;
5424 sk0
.proto
= IPPROTO_ESP
;
5425 PF_ACPY(&sk0
.gwy
.addr
, saddr
, sk0
.af_gwy
);
5426 PF_ACPY(&sk0
.ext_gwy
.addr
, daddr
, sk0
.af_gwy
);
5427 s0
= pf_find_state(kif
, &sk0
, PF_IN
);
5429 if (s0
&& PF_ANEQ(&s0
->state_key
->lan
.addr
,
5436 /* check maximums */
5437 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
5438 pf_status
.lcounters
[LCNT_STATES
]++;
5439 REASON_SET(&reason
, PFRES_MAXSTATES
);
5442 /* src node for filter rule */
5443 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
5444 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
5445 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
5446 REASON_SET(&reason
, PFRES_SRCLIMIT
);
5449 /* src node for translation rule */
5450 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
5451 ((direction
== PF_OUT
&&
5452 nr
->action
!= PF_RDR
&&
5453 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
5454 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
5455 REASON_SET(&reason
, PFRES_SRCLIMIT
);
5458 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
5460 REASON_SET(&reason
, PFRES_MEMORY
);
5462 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
5463 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
5464 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
5465 pf_status
.src_nodes
--;
5466 pool_put(&pf_src_tree_pl
, sn
);
5468 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
5470 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
5471 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
5472 pf_status
.src_nodes
--;
5473 pool_put(&pf_src_tree_pl
, nsn
);
5477 pool_put(&pf_app_state_pl
,
5479 pool_put(&pf_state_key_pl
, sk
);
5483 bzero(s
, sizeof (*s
));
5484 TAILQ_INIT(&s
->unlink_hooks
);
5486 s
->nat_rule
.ptr
= nr
;
5488 STATE_INC_COUNTERS(s
);
5489 s
->allow_opts
= r
->allow_opts
;
5490 s
->log
= r
->log
& PF_LOG_ALL
;
5492 s
->log
|= nr
->log
& PF_LOG_ALL
;
5493 switch (pd
->proto
) {
5495 s
->src
.seqlo
= ntohl(th
->th_seq
);
5496 s
->src
.seqhi
= s
->src
.seqlo
+ pd
->p_len
+ 1;
5497 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
5498 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
5499 /* Generate sequence number modulator */
5500 if ((s
->src
.seqdiff
= pf_tcp_iss(pd
) -
5503 pf_change_a(&th
->th_seq
, &th
->th_sum
,
5504 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
5505 rewrite
= off
+ sizeof (*th
);
5508 if (th
->th_flags
& TH_SYN
) {
5510 s
->src
.wscale
= pf_get_wscale(m
, off
,
5513 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5514 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
5515 /* Remove scale factor from initial window */
5516 int win
= s
->src
.max_win
;
5517 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
5518 s
->src
.max_win
= (win
- 1) >>
5519 (s
->src
.wscale
& PF_WSCALE_MASK
);
5521 if (th
->th_flags
& TH_FIN
)
5525 s
->src
.state
= TCPS_SYN_SENT
;
5526 s
->dst
.state
= TCPS_CLOSED
;
5527 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
5530 s
->src
.state
= PFUDPS_SINGLE
;
5531 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
5532 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
5536 case IPPROTO_ICMPV6
:
5538 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
5541 s
->src
.state
= PFGRE1S_INITIATING
;
5542 s
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5543 s
->timeout
= PFTM_GREv1_INITIATING
;
5546 s
->src
.state
= PFESPS_INITIATING
;
5547 s
->dst
.state
= PFESPS_NO_TRAFFIC
;
5548 s
->timeout
= PFTM_ESP_FIRST_PACKET
;
5551 s
->src
.state
= PFOTHERS_SINGLE
;
5552 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
5553 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
5556 s
->creation
= pf_time_second();
5557 s
->expire
= pf_time_second();
5561 s
->src_node
->states
++;
5562 VERIFY(s
->src_node
->states
!= 0);
5565 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
5566 s
->nat_src_node
= nsn
;
5567 s
->nat_src_node
->states
++;
5568 VERIFY(s
->nat_src_node
->states
!= 0);
5570 if (pd
->proto
== IPPROTO_TCP
) {
5571 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
5572 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
5574 REASON_SET(&reason
, PFRES_MEMORY
);
5575 pf_src_tree_remove_state(s
);
5576 STATE_DEC_COUNTERS(s
);
5577 pool_put(&pf_state_pl
, s
);
5580 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
5581 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
5582 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
5583 /* This really shouldn't happen!!! */
5584 DPFPRINTF(PF_DEBUG_URGENT
,
5585 ("pf_normalize_tcp_stateful failed on "
5587 pf_normalize_tcp_cleanup(s
);
5588 pf_src_tree_remove_state(s
);
5589 STATE_DEC_COUNTERS(s
);
5590 pool_put(&pf_state_pl
, s
);
5595 /* allocate state key and import values from psk */
5596 if ((sk
= pf_alloc_state_key(s
, &psk
)) == NULL
) {
5597 REASON_SET(&reason
, PFRES_MEMORY
);
5601 pf_set_rt_ifp(s
, saddr
, af
); /* needs s->state_key set */
5605 if (sk
->app_state
== 0) {
5606 switch (pd
->proto
) {
5608 u_int16_t dport
= (direction
== PF_OUT
) ?
5609 sk
->ext_gwy
.xport
.port
: sk
->gwy
.xport
.port
;
5612 ntohs(dport
) == PF_PPTP_PORT
) {
5613 struct pf_app_state
*as
;
5615 as
= pool_get(&pf_app_state_pl
,
5623 bzero(as
, sizeof (*as
));
5624 as
->handler
= pf_pptp_handler
;
5625 as
->compare_lan_ext
= 0;
5626 as
->compare_ext_gwy
= 0;
5627 as
->u
.pptp
.grev1_state
= 0;
5629 (void) hook_establish(&s
->unlink_hooks
,
5630 0, (hook_fn_t
) pf_pptp_unlink
, s
);
5637 ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
5638 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
5639 struct pf_app_state
*as
;
5641 as
= pool_get(&pf_app_state_pl
,
5649 bzero(as
, sizeof (*as
));
5650 as
->compare_lan_ext
= pf_ike_compare
;
5651 as
->compare_ext_gwy
= pf_ike_compare
;
5652 as
->u
.ike
.cookie
= ike
.initiator_cookie
;
5663 if (pf_insert_state(BOUND_IFACE(r
, kif
), s
)) {
5664 if (pd
->proto
== IPPROTO_TCP
)
5665 pf_normalize_tcp_cleanup(s
);
5666 REASON_SET(&reason
, PFRES_STATEINS
);
5667 pf_src_tree_remove_state(s
);
5668 STATE_DEC_COUNTERS(s
);
5669 pool_put(&pf_state_pl
, s
);
5677 if (pd
->proto
== IPPROTO_TCP
&&
5678 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
5679 r
->keep_state
== PF_STATE_SYNPROXY
) {
5680 int ua
= (sk
->af_lan
== sk
->af_gwy
) ? 1 : 0;
5681 s
->src
.state
= PF_TCPS_PROXY_SRC
;
5683 if (direction
== PF_OUT
) {
5684 pf_change_ap(direction
, pd
->mp
, saddr
,
5685 &th
->th_sport
, pd
->ip_sum
,
5686 &th
->th_sum
, &pd
->baddr
,
5687 bxport
.port
, 0, af
, pd
->af
, ua
);
5688 sxport
.port
= th
->th_sport
;
5690 pf_change_ap(direction
, pd
->mp
, daddr
,
5691 &th
->th_dport
, pd
->ip_sum
,
5692 &th
->th_sum
, &pd
->baddr
,
5693 bxport
.port
, 0, af
, pd
->af
, ua
);
5694 sxport
.port
= th
->th_dport
;
5697 s
->src
.seqhi
= htonl(random());
5698 /* Find mss option */
5699 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
5700 mss
= pf_calc_mss(saddr
, af
, mss
);
5701 mss
= pf_calc_mss(daddr
, af
, mss
);
5703 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
5704 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5705 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
5706 REASON_SET(&reason
, PFRES_SYNPROXY
);
5707 return (PF_SYNPROXY_DROP
);
5710 if (sk
->app_state
&& sk
->app_state
->handler
) {
5713 switch (pd
->proto
) {
5715 offx
+= th
->th_off
<< 2;
5718 offx
+= pd
->hdr
.udp
->uh_ulen
<< 2;
5721 /* ALG handlers only apply to TCP and UDP rules */
5726 sk
->app_state
->handler(s
, direction
, offx
,
5729 REASON_SET(&reason
, PFRES_MEMORY
);
5737 /* copy back packet headers if we performed NAT operations */
5739 if (rewrite
< off
+ hdrlen
)
5740 rewrite
= off
+ hdrlen
;
5742 m
= pf_lazy_makewritable(pd
, pd
->mp
, rewrite
);
5744 REASON_SET(&reason
, PFRES_MEMORY
);
5748 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5749 if (af
== AF_INET6
&& pd
->naf
== AF_INET
)
5750 return pf_nat64_ipv6(m
, off
, pd
);
5751 else if (af
== AF_INET
&& pd
->naf
== AF_INET6
)
5752 return pf_nat64_ipv4(m
, off
, pd
);
5761 * When pf_test_dummynet() returns PF_PASS, the rule matching parameter "rm"
5762 * remains unchanged, meaning the packet did not match a dummynet rule.
5763 * when the packet does match a dummynet rule, pf_test_dummynet() returns
5764 * PF_PASS and zero out the mbuf rule as the packet is effectively siphoned
5768 pf_test_dummynet(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5769 struct mbuf
**m0
, struct pf_pdesc
*pd
, struct ip_fw_args
*fwa
)
5771 struct mbuf
*m
= *m0
;
5772 struct pf_rule
*am
= NULL
;
5773 struct pf_ruleset
*rsm
= NULL
;
5774 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
5775 sa_family_t af
= pd
->af
;
5776 struct pf_rule
*r
, *a
= NULL
;
5777 struct pf_ruleset
*ruleset
= NULL
;
5778 struct tcphdr
*th
= pd
->hdr
.tcp
;
5782 unsigned int rtableid
= IFSCOPE_NONE
;
5785 u_int8_t icmptype
= 0, icmpcode
= 0;
5786 struct ip_fw_args dnflow
;
5787 struct pf_rule
*prev_matching_rule
= fwa
? fwa
->fwa_pf_rule
: NULL
;
5788 int found_prev_rule
= (prev_matching_rule
) ? 0 : 1;
5790 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
5792 if (!DUMMYNET_LOADED
)
5795 if (TAILQ_EMPTY(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
))
5798 bzero(&dnflow
, sizeof(dnflow
));
5802 /* Fragments don't gave protocol headers */
5803 if (!(pd
->flags
& PFDESC_IP_FRAG
))
5804 switch (pd
->proto
) {
5806 dnflow
.fwa_id
.flags
= pd
->hdr
.tcp
->th_flags
;
5807 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.tcp
->th_dport
);
5808 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.tcp
->th_sport
);
5809 hdrlen
= sizeof (*th
);
5812 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.udp
->uh_dport
);
5813 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.udp
->uh_sport
);
5814 hdrlen
= sizeof (*pd
->hdr
.udp
);
5820 hdrlen
= ICMP_MINLEN
;
5821 icmptype
= pd
->hdr
.icmp
->icmp_type
;
5822 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
5826 case IPPROTO_ICMPV6
:
5829 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
5830 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
5831 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
5835 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
5836 hdrlen
= sizeof (*pd
->hdr
.grev1
);
5839 hdrlen
= sizeof (*pd
->hdr
.esp
);
5843 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
);
5847 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5848 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5849 else if (r
->direction
&& r
->direction
!= direction
)
5850 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5851 else if (r
->af
&& r
->af
!= af
)
5852 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5853 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5854 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5855 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
5857 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5858 /* tcp/udp only. port_op always 0 in other cases */
5859 else if (r
->proto
== pd
->proto
&&
5860 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5861 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5862 ((r
->src
.xport
.range
.op
&&
5863 !pf_match_port(r
->src
.xport
.range
.op
,
5864 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
5866 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
5867 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
5869 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5870 /* tcp/udp only. port_op always 0 in other cases */
5871 else if (r
->proto
== pd
->proto
&&
5872 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5873 r
->dst
.xport
.range
.op
&&
5874 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5875 !pf_match_port(r
->dst
.xport
.range
.op
,
5876 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
5878 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
5879 /* icmp only. type always 0 in other cases */
5881 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5882 r
->type
!= icmptype
+ 1))
5883 r
= TAILQ_NEXT(r
, entries
);
5884 /* icmp only. type always 0 in other cases */
5886 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5887 r
->code
!= icmpcode
+ 1))
5888 r
= TAILQ_NEXT(r
, entries
);
5889 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5890 r
= TAILQ_NEXT(r
, entries
);
5891 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
5892 r
= TAILQ_NEXT(r
, entries
);
5893 else if (pd
->proto
== IPPROTO_TCP
&&
5894 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5895 (r
->flagset
& th
->th_flags
) != r
->flags
))
5896 r
= TAILQ_NEXT(r
, entries
);
5897 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
5898 r
= TAILQ_NEXT(r
, entries
);
5899 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5900 r
= TAILQ_NEXT(r
, entries
);
5903 * Need to go past the previous dummynet matching rule
5905 if (r
->anchor
== NULL
) {
5906 if (found_prev_rule
) {
5909 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
5910 rtableid
= r
->rtableid
;
5917 } else if (r
== prev_matching_rule
) {
5918 found_prev_rule
= 1;
5920 r
= TAILQ_NEXT(r
, entries
);
5922 pf_step_into_anchor(&asd
, &ruleset
,
5923 PF_RULESET_DUMMYNET
, &r
, &a
, &match
);
5926 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5927 PF_RULESET_DUMMYNET
, &r
, &a
, &match
))
5937 REASON_SET(&reason
, PFRES_DUMMYNET
);
5940 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
,
5944 if (r
->action
== PF_NODUMMYNET
) {
5945 int dirndx
= (direction
== PF_OUT
);
5947 r
->packets
[dirndx
]++;
5948 r
->bytes
[dirndx
] += pd
->tot_len
;
5952 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5953 REASON_SET(&reason
, PFRES_MEMORY
);
5958 if (r
->dnpipe
&& ip_dn_io_ptr
!= NULL
) {
5959 int dirndx
= (direction
== PF_OUT
);
5961 r
->packets
[dirndx
]++;
5962 r
->bytes
[dirndx
] += pd
->tot_len
;
5964 dnflow
.fwa_cookie
= r
->dnpipe
;
5965 dnflow
.fwa_pf_rule
= r
;
5966 dnflow
.fwa_id
.proto
= pd
->proto
;
5967 dnflow
.fwa_flags
= r
->dntype
;
5970 dnflow
.fwa_id
.addr_type
= 4;
5971 dnflow
.fwa_id
.src_ip
= ntohl(saddr
->v4
.s_addr
);
5972 dnflow
.fwa_id
.dst_ip
= ntohl(daddr
->v4
.s_addr
);
5975 dnflow
.fwa_id
.addr_type
= 6;
5976 dnflow
.fwa_id
.src_ip6
= saddr
->v6
;
5977 dnflow
.fwa_id
.dst_ip6
= saddr
->v6
;
5982 dnflow
.fwa_oif
= fwa
->fwa_oif
;
5983 dnflow
.fwa_oflags
= fwa
->fwa_oflags
;
5985 * Note that fwa_ro, fwa_dst and fwa_ipoa are
5986 * actually in a union so the following does work
5987 * for both IPv4 and IPv6
5989 dnflow
.fwa_ro
= fwa
->fwa_ro
;
5990 dnflow
.fwa_dst
= fwa
->fwa_dst
;
5991 dnflow
.fwa_ipoa
= fwa
->fwa_ipoa
;
5992 dnflow
.fwa_ro6_pmtu
= fwa
->fwa_ro6_pmtu
;
5993 dnflow
.fwa_origifp
= fwa
->fwa_origifp
;
5994 dnflow
.fwa_mtu
= fwa
->fwa_mtu
;
5995 dnflow
.fwa_alwaysfrag
= fwa
->fwa_alwaysfrag
;
5996 dnflow
.fwa_unfragpartlen
= fwa
->fwa_unfragpartlen
;
5997 dnflow
.fwa_exthdrs
= fwa
->fwa_exthdrs
;
6000 if (af
== AF_INET
) {
6001 struct ip
*iphdr
= mtod(m
, struct ip
*);
6002 NTOHS(iphdr
->ip_len
);
6003 NTOHS(iphdr
->ip_off
);
6006 * Don't need to unlock pf_lock as NET_THREAD_HELD_PF
6007 * allows for recursive behavior
6012 direction
== PF_IN
? DN_TO_IP_IN
: DN_TO_IP_OUT
:
6013 direction
== PF_IN
? DN_TO_IP6_IN
: DN_TO_IP6_OUT
,
6014 &dnflow
, DN_CLIENT_PF
);
6017 * The packet is siphoned out by dummynet so return a NULL
6018 * mbuf so the caller can still return success.
6027 #endif /* DUMMYNET */
6030 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
6031 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
6032 struct pf_ruleset
**rsm
)
6035 struct pf_rule
*r
, *a
= NULL
;
6036 struct pf_ruleset
*ruleset
= NULL
;
6037 sa_family_t af
= pd
->af
;
6043 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
6046 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
6047 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
6048 else if (r
->direction
&& r
->direction
!= direction
)
6049 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
6050 else if (r
->af
&& r
->af
!= af
)
6051 r
= r
->skip
[PF_SKIP_AF
].ptr
;
6052 else if (r
->proto
&& r
->proto
!= pd
->proto
)
6053 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
6054 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
6056 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
6057 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
6059 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
6060 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
6061 !(r
->tos
& pd
->tos
))
6062 r
= TAILQ_NEXT(r
, entries
);
6063 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
6064 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
6065 r
= TAILQ_NEXT(r
, entries
);
6066 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
6067 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
6068 r
= TAILQ_NEXT(r
, entries
);
6069 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
6070 r
= TAILQ_NEXT(r
, entries
);
6071 else if (pd
->proto
== IPPROTO_UDP
&&
6072 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
6073 r
= TAILQ_NEXT(r
, entries
);
6074 else if (pd
->proto
== IPPROTO_TCP
&&
6075 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
6077 r
= TAILQ_NEXT(r
, entries
);
6078 else if ((pd
->proto
== IPPROTO_ICMP
||
6079 pd
->proto
== IPPROTO_ICMPV6
) &&
6080 (r
->type
|| r
->code
))
6081 r
= TAILQ_NEXT(r
, entries
);
6082 else if (r
->prob
&& r
->prob
<= (RandomULong() % (UINT_MAX
- 1) + 1))
6083 r
= TAILQ_NEXT(r
, entries
);
6084 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
6085 r
= TAILQ_NEXT(r
, entries
);
6087 if (r
->anchor
== NULL
) {
6094 r
= TAILQ_NEXT(r
, entries
);
6096 pf_step_into_anchor(&asd
, &ruleset
,
6097 PF_RULESET_FILTER
, &r
, &a
, &match
);
6099 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
6100 PF_RULESET_FILTER
, &r
, &a
, &match
))
6107 REASON_SET(&reason
, PFRES_MATCH
);
6110 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
6113 if (r
->action
!= PF_PASS
)
6116 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1, NULL
)) {
6117 REASON_SET(&reason
, PFRES_MEMORY
);
6125 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
6126 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
6128 #pragma unused(direction)
6130 struct pf_pptp_state
*pptps
;
6131 struct pf_pptp_ctrl_msg cm
;
6133 struct pf_state
*gs
;
6135 u_int16_t
*pac_call_id
;
6136 u_int16_t
*pns_call_id
;
6137 u_int16_t
*spoof_call_id
;
6138 u_int8_t
*pac_state
;
6139 u_int8_t
*pns_state
;
6140 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
6142 struct pf_state_key
*sk
;
6143 struct pf_state_key
*gsk
;
6144 struct pf_app_state
*gas
;
6147 pptps
= &sk
->app_state
->u
.pptp
;
6148 gs
= pptps
->grev1_state
;
6151 gs
->expire
= pf_time_second();
6154 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
6155 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
6157 tlen
= plen
- PF_PPTP_CTRL_MSG_MINSIZE
;
6158 m_copydata(m
, off
, plen
, &cm
);
6160 if (ntohl(cm
.hdr
.magic
) != PF_PPTP_MAGIC_NUMBER
)
6162 if (ntohs(cm
.hdr
.type
) != 1)
6165 #define TYPE_LEN_CHECK(_type, _name) \
6166 case PF_PPTP_CTRL_TYPE_##_type: \
6167 if (tlen < sizeof(struct pf_pptp_ctrl_##_name)) \
6171 switch (cm
.ctrl
.type
) {
6172 TYPE_LEN_CHECK(START_REQ
, start_req
);
6173 TYPE_LEN_CHECK(START_RPY
, start_rpy
);
6174 TYPE_LEN_CHECK(STOP_REQ
, stop_req
);
6175 TYPE_LEN_CHECK(STOP_RPY
, stop_rpy
);
6176 TYPE_LEN_CHECK(ECHO_REQ
, echo_req
);
6177 TYPE_LEN_CHECK(ECHO_RPY
, echo_rpy
);
6178 TYPE_LEN_CHECK(CALL_OUT_REQ
, call_out_req
);
6179 TYPE_LEN_CHECK(CALL_OUT_RPY
, call_out_rpy
);
6180 TYPE_LEN_CHECK(CALL_IN_1ST
, call_in_1st
);
6181 TYPE_LEN_CHECK(CALL_IN_2ND
, call_in_2nd
);
6182 TYPE_LEN_CHECK(CALL_IN_3RD
, call_in_3rd
);
6183 TYPE_LEN_CHECK(CALL_CLR
, call_clr
);
6184 TYPE_LEN_CHECK(CALL_DISC
, call_disc
);
6185 TYPE_LEN_CHECK(ERROR
, error
);
6186 TYPE_LEN_CHECK(SET_LINKINFO
, set_linkinfo
);
6190 #undef TYPE_LEN_CHECK
6193 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
6197 memcpy(gs
, s
, sizeof (*gs
));
6199 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
6200 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
6202 TAILQ_INIT(&gs
->unlink_hooks
);
6205 gs
->pfsync_time
= 0;
6206 gs
->packets
[0] = gs
->packets
[1] = 0;
6207 gs
->bytes
[0] = gs
->bytes
[1] = 0;
6208 gs
->timeout
= PFTM_UNLINKED
;
6209 gs
->id
= gs
->creatorid
= 0;
6210 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
6211 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
6213 gas
= pool_get(&pf_app_state_pl
, PR_NOWAIT
);
6215 pool_put(&pf_state_pl
, gs
);
6219 gsk
= pf_alloc_state_key(gs
, NULL
);
6221 pool_put(&pf_app_state_pl
, gas
);
6222 pool_put(&pf_state_pl
, gs
);
6226 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
6227 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
6228 memcpy(&gsk
->ext_lan
, &sk
->ext_lan
, sizeof (gsk
->ext_lan
));
6229 memcpy(&gsk
->ext_gwy
, &sk
->ext_gwy
, sizeof (gsk
->ext_gwy
));
6230 gsk
->af_lan
= sk
->af_lan
;
6231 gsk
->af_gwy
= sk
->af_gwy
;
6232 gsk
->proto
= IPPROTO_GRE
;
6233 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
6234 gsk
->app_state
= gas
;
6235 gsk
->lan
.xport
.call_id
= 0;
6236 gsk
->gwy
.xport
.call_id
= 0;
6237 gsk
->ext_lan
.xport
.call_id
= 0;
6238 gsk
->ext_gwy
.xport
.call_id
= 0;
6239 gsk
->flowsrc
= FLOWSRC_PF
;
6240 gsk
->flowhash
= pf_calc_state_key_flowhash(gsk
);
6241 memset(gas
, 0, sizeof (*gas
));
6242 gas
->u
.grev1
.pptp_state
= s
;
6243 STATE_INC_COUNTERS(gs
);
6244 pptps
->grev1_state
= gs
;
6245 (void) hook_establish(&gs
->unlink_hooks
, 0,
6246 (hook_fn_t
) pf_grev1_unlink
, gs
);
6248 gsk
= gs
->state_key
;
6251 switch (sk
->direction
) {
6253 pns_call_id
= &gsk
->ext_lan
.xport
.call_id
;
6254 pns_state
= &gs
->dst
.state
;
6255 pac_call_id
= &gsk
->lan
.xport
.call_id
;
6256 pac_state
= &gs
->src
.state
;
6260 pns_call_id
= &gsk
->lan
.xport
.call_id
;
6261 pns_state
= &gs
->src
.state
;
6262 pac_call_id
= &gsk
->ext_lan
.xport
.call_id
;
6263 pac_state
= &gs
->dst
.state
;
6267 DPFPRINTF(PF_DEBUG_URGENT
,
6268 ("pf_pptp_handler: bad directional!\n"));
6275 ct
= ntohs(cm
.ctrl
.type
);
6278 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
6279 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
6280 *pns_state
= PFGRE1S_INITIATING
;
6281 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
6282 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
6285 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
6286 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
6287 if (s
->nat_rule
.ptr
)
6289 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
6290 &cm
.msg
.call_out_rpy
.call_id
:
6291 &cm
.msg
.call_out_rpy
.peer_call_id
;
6292 if (gs
->timeout
== PFTM_UNLINKED
) {
6293 *pac_state
= PFGRE1S_INITIATING
;
6294 op
= PF_PPTP_INSERT_GRE
;
6298 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
6299 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
6300 *pns_state
= PFGRE1S_INITIATING
;
6301 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
6302 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
6305 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
6306 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
6307 *pac_state
= PFGRE1S_INITIATING
;
6308 if (s
->nat_rule
.ptr
)
6310 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
6311 &cm
.msg
.call_in_2nd
.call_id
:
6312 &cm
.msg
.call_in_2nd
.peer_call_id
;
6315 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
6316 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
6317 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
6318 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
6321 if (gs
->timeout
== PFTM_UNLINKED
)
6322 op
= PF_PPTP_INSERT_GRE
;
6325 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
6326 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
6327 op
= PF_PPTP_REMOVE_GRE
;
6330 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
6331 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
6332 op
= PF_PPTP_REMOVE_GRE
;
6335 case PF_PPTP_CTRL_TYPE_ERROR
:
6336 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
6337 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
6340 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
6341 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
6342 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
6350 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
6351 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
6352 if (spoof_call_id
) {
6353 u_int16_t call_id
= 0;
6355 struct pf_state_key_cmp key
;
6357 key
.af_gwy
= gsk
->af_gwy
;
6358 key
.proto
= IPPROTO_GRE
;
6359 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
6360 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af_gwy
);
6361 PF_ACPY(&key
.ext_gwy
.addr
, &gsk
->ext_gwy
.addr
, key
.af_gwy
);
6362 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
6363 key
.ext_gwy
.xport
.call_id
= gsk
->ext_gwy
.xport
.call_id
;
6365 call_id
= htonl(random());
6368 while (pf_find_state_all(&key
, PF_IN
, 0)) {
6369 call_id
= ntohs(call_id
);
6371 if (--call_id
== 0) call_id
= 0xffff;
6372 call_id
= htons(call_id
);
6374 key
.gwy
.xport
.call_id
= call_id
;
6377 DPFPRINTF(PF_DEBUG_URGENT
,
6378 ("pf_pptp_handler: failed to spoof "
6380 key
.gwy
.xport
.call_id
= 0;
6385 gsk
->gwy
.xport
.call_id
= call_id
;
6391 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
6392 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
6393 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
6394 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
6395 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
6397 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
6398 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
6399 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
6402 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
6404 pptps
->grev1_state
= NULL
;
6405 STATE_DEC_COUNTERS(gs
);
6406 pool_put(&pf_state_pl
, gs
);
6409 m_copyback(m
, off
, plen
, &cm
);
6413 case PF_PPTP_REMOVE_GRE
:
6414 gs
->timeout
= PFTM_PURGE
;
6415 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
6416 gsk
->lan
.xport
.call_id
= 0;
6417 gsk
->gwy
.xport
.call_id
= 0;
6418 gsk
->ext_lan
.xport
.call_id
= 0;
6419 gsk
->ext_gwy
.xport
.call_id
= 0;
6420 gs
->id
= gs
->creatorid
= 0;
6423 case PF_PPTP_INSERT_GRE
:
6424 gs
->creation
= pf_time_second();
6425 gs
->expire
= pf_time_second();
6426 gs
->timeout
= PFTM_TCP_ESTABLISHED
;
6427 if (gs
->src_node
!= NULL
) {
6428 ++gs
->src_node
->states
;
6429 VERIFY(gs
->src_node
->states
!= 0);
6431 if (gs
->nat_src_node
!= NULL
) {
6432 ++gs
->nat_src_node
->states
;
6433 VERIFY(gs
->nat_src_node
->states
!= 0);
6435 pf_set_rt_ifp(gs
, &sk
->lan
.addr
, sk
->af_lan
);
6436 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
6440 * FIX ME: insertion can fail when multiple PNS
6441 * behind the same NAT open calls to the same PAC
6442 * simultaneously because spoofed call ID numbers
6443 * are chosen before states are inserted. This is
6444 * hard to fix and happens infrequently enough that
6445 * users will normally try again and this ALG will
6446 * succeed. Failures are expected to be rare enough
6447 * that fixing this is a low priority.
6449 pptps
->grev1_state
= NULL
;
6450 pd
->lmw
= -1; /* Force PF_DROP on PFRES_MEMORY */
6451 pf_src_tree_remove_state(gs
);
6452 STATE_DEC_COUNTERS(gs
);
6453 pool_put(&pf_state_pl
, gs
);
6454 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
6455 "inserting GREv1 state.\n"));
6465 pf_pptp_unlink(struct pf_state
*s
)
6467 struct pf_app_state
*as
= s
->state_key
->app_state
;
6468 struct pf_state
*grev1s
= as
->u
.pptp
.grev1_state
;
6471 struct pf_app_state
*gas
= grev1s
->state_key
->app_state
;
6473 if (grev1s
->timeout
< PFTM_MAX
)
6474 grev1s
->timeout
= PFTM_PURGE
;
6475 gas
->u
.grev1
.pptp_state
= NULL
;
6476 as
->u
.pptp
.grev1_state
= NULL
;
6481 pf_grev1_unlink(struct pf_state
*s
)
6483 struct pf_app_state
*as
= s
->state_key
->app_state
;
6484 struct pf_state
*pptps
= as
->u
.grev1
.pptp_state
;
6487 struct pf_app_state
*pas
= pptps
->state_key
->app_state
;
6489 pas
->u
.pptp
.grev1_state
= NULL
;
6490 as
->u
.grev1
.pptp_state
= NULL
;
6495 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
6497 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
6498 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
6502 pf_do_nat64(struct pf_state_key
*sk
, struct pf_pdesc
*pd
, struct mbuf
*m
,
6505 if (pd
->af
== AF_INET
) {
6506 if (pd
->af
!= sk
->af_lan
) {
6507 pd
->ndaddr
= sk
->lan
.addr
;
6508 pd
->naddr
= sk
->ext_lan
.addr
;
6510 pd
->naddr
= sk
->gwy
.addr
;
6511 pd
->ndaddr
= sk
->ext_gwy
.addr
;
6513 return (pf_nat64_ipv4(m
, off
, pd
));
6515 else if (pd
->af
== AF_INET6
) {
6516 if (pd
->af
!= sk
->af_lan
) {
6517 pd
->ndaddr
= sk
->lan
.addr
;
6518 pd
->naddr
= sk
->ext_lan
.addr
;
6520 pd
->naddr
= sk
->gwy
.addr
;
6521 pd
->ndaddr
= sk
->ext_gwy
.addr
;
6523 return (pf_nat64_ipv6(m
, off
, pd
));
6529 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6530 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
6534 struct pf_state_key_cmp key
;
6535 struct tcphdr
*th
= pd
->hdr
.tcp
;
6536 u_int16_t win
= ntohs(th
->th_win
);
6537 u_int32_t ack
, end
, seq
, orig_seq
;
6541 struct pf_state_peer
*src
, *dst
;
6542 struct pf_state_key
*sk
;
6545 key
.proto
= IPPROTO_TCP
;
6546 key
.af_lan
= key
.af_gwy
= pd
->af
;
6549 * For NAT64 the first time rule search and state creation
6550 * is done on the incoming side only.
6551 * Once the state gets created, NAT64's LAN side (ipv6) will
6552 * not be able to find the state in ext-gwy tree as that normally
6553 * is intended to be looked up for incoming traffic from the
6555 * Therefore to handle NAT64 case we init keys here for both
6556 * lan-ext as well as ext-gwy trees.
6557 * In the state lookup we attempt a lookup on both trees if
6558 * first one does not return any result and return a match if
6559 * the match state's was created by NAT64 rule.
6561 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
6562 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
6563 key
.ext_gwy
.xport
.port
= th
->th_sport
;
6564 key
.gwy
.xport
.port
= th
->th_dport
;
6566 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
6567 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
6568 key
.lan
.xport
.port
= th
->th_sport
;
6569 key
.ext_lan
.xport
.port
= th
->th_dport
;
6573 sk
= (*state
)->state_key
;
6575 * In case of NAT64 the translation is first applied on the LAN
6576 * side. Therefore for stack's address family comparison
6577 * we use sk->af_lan.
6579 if ((direction
== sk
->direction
) && (pd
->af
== sk
->af_lan
)) {
6580 src
= &(*state
)->src
;
6581 dst
= &(*state
)->dst
;
6583 src
= &(*state
)->dst
;
6584 dst
= &(*state
)->src
;
6587 if (src
->state
== PF_TCPS_PROXY_SRC
) {
6588 if (direction
!= sk
->direction
) {
6589 REASON_SET(reason
, PFRES_SYNPROXY
);
6590 return (PF_SYNPROXY_DROP
);
6592 if (th
->th_flags
& TH_SYN
) {
6593 if (ntohl(th
->th_seq
) != src
->seqlo
) {
6594 REASON_SET(reason
, PFRES_SYNPROXY
);
6597 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
6598 pd
->src
, th
->th_dport
, th
->th_sport
,
6599 src
->seqhi
, ntohl(th
->th_seq
) + 1,
6600 TH_SYN
|TH_ACK
, 0, src
->mss
, 0, 1,
6602 REASON_SET(reason
, PFRES_SYNPROXY
);
6603 return (PF_SYNPROXY_DROP
);
6604 } else if (!(th
->th_flags
& TH_ACK
) ||
6605 (ntohl(th
->th_ack
) != src
->seqhi
+ 1) ||
6606 (ntohl(th
->th_seq
) != src
->seqlo
+ 1)) {
6607 REASON_SET(reason
, PFRES_SYNPROXY
);
6609 } else if ((*state
)->src_node
!= NULL
&&
6610 pf_src_connlimit(state
)) {
6611 REASON_SET(reason
, PFRES_SRCLIMIT
);
6614 src
->state
= PF_TCPS_PROXY_DST
;
6616 if (src
->state
== PF_TCPS_PROXY_DST
) {
6617 struct pf_state_host
*psrc
, *pdst
;
6619 if (direction
== PF_OUT
) {
6621 pdst
= &sk
->ext_gwy
;
6623 psrc
= &sk
->ext_lan
;
6626 if (direction
== sk
->direction
) {
6627 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
6628 (ntohl(th
->th_ack
) != src
->seqhi
+ 1) ||
6629 (ntohl(th
->th_seq
) != src
->seqlo
+ 1)) {
6630 REASON_SET(reason
, PFRES_SYNPROXY
);
6633 src
->max_win
= MAX(ntohs(th
->th_win
), 1);
6634 if (dst
->seqhi
== 1)
6635 dst
->seqhi
= htonl(random());
6636 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
6637 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
6638 dst
->seqhi
, 0, TH_SYN
, 0,
6639 src
->mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
6640 REASON_SET(reason
, PFRES_SYNPROXY
);
6641 return (PF_SYNPROXY_DROP
);
6642 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
6644 (ntohl(th
->th_ack
) != dst
->seqhi
+ 1)) {
6645 REASON_SET(reason
, PFRES_SYNPROXY
);
6648 dst
->max_win
= MAX(ntohs(th
->th_win
), 1);
6649 dst
->seqlo
= ntohl(th
->th_seq
);
6650 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
6651 pd
->src
, th
->th_dport
, th
->th_sport
,
6652 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
6653 TH_ACK
, src
->max_win
, 0, 0, 0,
6654 (*state
)->tag
, NULL
, NULL
);
6655 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
6656 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
6657 src
->seqhi
+ 1, src
->seqlo
+ 1,
6658 TH_ACK
, dst
->max_win
, 0, 0, 1,
6660 src
->seqdiff
= dst
->seqhi
-
6662 dst
->seqdiff
= src
->seqhi
-
6664 src
->seqhi
= src
->seqlo
+
6666 dst
->seqhi
= dst
->seqlo
+
6668 src
->wscale
= dst
->wscale
= 0;
6669 src
->state
= dst
->state
=
6671 REASON_SET(reason
, PFRES_SYNPROXY
);
6672 return (PF_SYNPROXY_DROP
);
6676 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
6677 dst
->state
>= TCPS_FIN_WAIT_2
&&
6678 src
->state
>= TCPS_FIN_WAIT_2
) {
6679 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6680 printf("pf: state reuse ");
6681 pf_print_state(*state
);
6682 pf_print_flags(th
->th_flags
);
6685 /* XXX make sure it's the same direction ?? */
6686 src
->state
= dst
->state
= TCPS_CLOSED
;
6687 pf_unlink_state(*state
);
6692 if ((th
->th_flags
& TH_SYN
) == 0) {
6693 sws
= (src
->wscale
& PF_WSCALE_FLAG
) ?
6694 (src
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
6695 dws
= (dst
->wscale
& PF_WSCALE_FLAG
) ?
6696 (dst
->wscale
& PF_WSCALE_MASK
) : TCP_MAX_WINSHIFT
;
6702 * Sequence tracking algorithm from Guido van Rooij's paper:
6703 * http://www.madison-gurkha.com/publications/tcp_filtering/
6707 orig_seq
= seq
= ntohl(th
->th_seq
);
6708 if (src
->seqlo
== 0) {
6709 /* First packet from this end. Set its state */
6711 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
6712 src
->scrub
== NULL
) {
6713 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
6714 REASON_SET(reason
, PFRES_MEMORY
);
6719 /* Deferred generation of sequence number modulator */
6720 if (dst
->seqdiff
&& !src
->seqdiff
) {
6721 /* use random iss for the TCP server */
6722 while ((src
->seqdiff
= random() - seq
) == 0)
6724 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6725 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6727 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6728 copyback
= off
+ sizeof (*th
);
6730 ack
= ntohl(th
->th_ack
);
6733 end
= seq
+ pd
->p_len
;
6734 if (th
->th_flags
& TH_SYN
) {
6736 if (dst
->wscale
& PF_WSCALE_FLAG
) {
6737 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
6739 if (src
->wscale
& PF_WSCALE_FLAG
) {
6741 * Remove scale factor from initial
6744 sws
= src
->wscale
& PF_WSCALE_MASK
;
6745 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
6747 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6750 * Window scale negotiation has failed,
6751 * therefore we must restore the window
6752 * scale in the state record that we
6753 * optimistically removed in
6754 * pf_test_rule(). Care is required to
6755 * prevent arithmetic overflow from
6756 * zeroing the window when it's
6757 * truncated down to 16-bits.
6759 u_int32_t max_win
= dst
->max_win
;
6761 dst
->wscale
& PF_WSCALE_MASK
;
6762 dst
->max_win
= MIN(0xffff, max_win
);
6763 /* in case of a retrans SYN|ACK */
6768 if (th
->th_flags
& TH_FIN
)
6772 if (src
->state
< TCPS_SYN_SENT
)
6773 src
->state
= TCPS_SYN_SENT
;
6776 * May need to slide the window (seqhi may have been set by
6777 * the crappy stack check or if we picked up the connection
6778 * after establishment)
6780 if (src
->seqhi
== 1 ||
6781 SEQ_GEQ(end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
),
6783 src
->seqhi
= end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
);
6784 if (win
> src
->max_win
)
6788 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6790 /* Modulate sequence numbers */
6791 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6793 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6794 copyback
= off
+ sizeof (*th
);
6796 end
= seq
+ pd
->p_len
;
6797 if (th
->th_flags
& TH_SYN
)
6799 if (th
->th_flags
& TH_FIN
)
6803 if ((th
->th_flags
& TH_ACK
) == 0) {
6804 /* Let it pass through the ack skew check */
6806 } else if ((ack
== 0 &&
6807 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6808 /* broken tcp stacks do not set ack */
6809 (dst
->state
< TCPS_SYN_SENT
)) {
6811 * Many stacks (ours included) will set the ACK number in an
6812 * FIN|ACK if the SYN times out -- no sequence to ACK.
6818 /* Ease sequencing restrictions on no data packets */
6823 ackskew
= dst
->seqlo
- ack
;
6827 * Need to demodulate the sequence numbers in any TCP SACK options
6828 * (Selective ACK). We could optionally validate the SACK values
6829 * against the current ACK window, either forwards or backwards, but
6830 * I'm not confident that SACK has been implemented properly
6831 * everywhere. It wouldn't surprise me if several stacks accidently
6832 * SACK too far backwards of previously ACKed data. There really aren't
6833 * any security implications of bad SACKing unless the target stack
6834 * doesn't validate the option length correctly. Someone trying to
6835 * spoof into a TCP connection won't bother blindly sending SACK
6838 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6839 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6840 if (copyback
== -1) {
6841 REASON_SET(reason
, PFRES_MEMORY
);
6849 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6850 if (SEQ_GEQ(src
->seqhi
, end
) &&
6851 /* Last octet inside other's window space */
6852 SEQ_GEQ(seq
, src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) &&
6853 /* Retrans: not more than one window back */
6854 (ackskew
>= -MAXACKWINDOW
) &&
6855 /* Acking not more than one reassembled fragment backwards */
6856 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6857 /* Acking not more than one window forward */
6858 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6859 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6860 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6861 /* Require an exact/+1 sequence match on resets when possible */
6863 if (dst
->scrub
|| src
->scrub
) {
6864 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6865 *state
, src
, dst
, ©back
))
6871 /* update max window */
6872 if (src
->max_win
< win
)
6874 /* synchronize sequencing */
6875 if (SEQ_GT(end
, src
->seqlo
))
6877 /* slide the window of what the other end can send */
6878 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6879 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6882 if (th
->th_flags
& TH_SYN
)
6883 if (src
->state
< TCPS_SYN_SENT
)
6884 src
->state
= TCPS_SYN_SENT
;
6885 if (th
->th_flags
& TH_FIN
)
6886 if (src
->state
< TCPS_CLOSING
)
6887 src
->state
= TCPS_CLOSING
;
6888 if (th
->th_flags
& TH_ACK
) {
6889 if (dst
->state
== TCPS_SYN_SENT
) {
6890 dst
->state
= TCPS_ESTABLISHED
;
6891 if (src
->state
== TCPS_ESTABLISHED
&&
6892 (*state
)->src_node
!= NULL
&&
6893 pf_src_connlimit(state
)) {
6894 REASON_SET(reason
, PFRES_SRCLIMIT
);
6897 } else if (dst
->state
== TCPS_CLOSING
)
6898 dst
->state
= TCPS_FIN_WAIT_2
;
6900 if (th
->th_flags
& TH_RST
)
6901 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6903 /* update expire time */
6904 (*state
)->expire
= pf_time_second();
6905 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6906 dst
->state
>= TCPS_FIN_WAIT_2
)
6907 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6908 else if (src
->state
>= TCPS_CLOSING
&&
6909 dst
->state
>= TCPS_CLOSING
)
6910 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6911 else if (src
->state
< TCPS_ESTABLISHED
||
6912 dst
->state
< TCPS_ESTABLISHED
)
6913 (*state
)->timeout
= PFTM_TCP_OPENING
;
6914 else if (src
->state
>= TCPS_CLOSING
||
6915 dst
->state
>= TCPS_CLOSING
)
6916 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6918 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6920 /* Fall through to PASS packet */
6922 } else if ((dst
->state
< TCPS_SYN_SENT
||
6923 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6924 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6925 /* Within a window forward of the originating packet */
6926 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6927 /* Within a window backward of the originating packet */
6930 * This currently handles three situations:
6931 * 1) Stupid stacks will shotgun SYNs before their peer
6933 * 2) When PF catches an already established stream (the
6934 * firewall rebooted, the state table was flushed, routes
6936 * 3) Packets get funky immediately after the connection
6937 * closes (this should catch Solaris spurious ACK|FINs
6938 * that web servers like to spew after a close)
6940 * This must be a little more careful than the above code
6941 * since packet floods will also be caught here. We don't
6942 * update the TTL here to mitigate the damage of a packet
6943 * flood and so the same code can handle awkward establishment
6944 * and a loosened connection close.
6945 * In the establishment case, a correct peer response will
6946 * validate the connection, go through the normal state code
6947 * and keep updating the state TTL.
6950 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6951 printf("pf: loose state match: ");
6952 pf_print_state(*state
);
6953 pf_print_flags(th
->th_flags
);
6954 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6955 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6956 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6957 (*state
)->packets
[1],
6958 direction
== PF_IN
? "in" : "out",
6959 direction
== sk
->direction
?
6963 if (dst
->scrub
|| src
->scrub
) {
6964 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6965 *state
, src
, dst
, ©back
))
6970 /* update max window */
6971 if (src
->max_win
< win
)
6973 /* synchronize sequencing */
6974 if (SEQ_GT(end
, src
->seqlo
))
6976 /* slide the window of what the other end can send */
6977 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6978 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6981 * Cannot set dst->seqhi here since this could be a shotgunned
6982 * SYN and not an already established connection.
6985 if (th
->th_flags
& TH_FIN
)
6986 if (src
->state
< TCPS_CLOSING
)
6987 src
->state
= TCPS_CLOSING
;
6988 if (th
->th_flags
& TH_RST
)
6989 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6991 /* Fall through to PASS packet */
6994 if (dst
->state
== TCPS_SYN_SENT
&&
6995 src
->state
== TCPS_SYN_SENT
) {
6996 /* Send RST for state mismatches during handshake */
6997 if (!(th
->th_flags
& TH_RST
))
6998 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6999 pd
->dst
, pd
->src
, th
->th_dport
,
7000 th
->th_sport
, ntohl(th
->th_ack
), 0,
7002 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
7003 pd
->eh
, kif
->pfik_ifp
);
7007 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
7008 printf("pf: BAD state: ");
7009 pf_print_state(*state
);
7010 pf_print_flags(th
->th_flags
);
7011 printf("\n seq=%u (%u) ack=%u len=%u ackskew=%d "
7012 "sws=%u dws=%u pkts=%llu:%llu dir=%s,%s\n",
7013 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
7014 (unsigned int)sws
, (unsigned int)dws
,
7015 (*state
)->packets
[0], (*state
)->packets
[1],
7016 direction
== PF_IN
? "in" : "out",
7017 direction
== sk
->direction
?
7019 printf("pf: State failure on: %c %c %c %c | %c %c\n",
7020 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
7022 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) ?
7024 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
7025 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
7026 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
7027 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
7029 REASON_SET(reason
, PFRES_BADSTATE
);
7033 /* Any packets which have gotten here are to be passed */
7035 if (sk
->app_state
&&
7036 sk
->app_state
->handler
) {
7037 sk
->app_state
->handler(*state
, direction
,
7038 off
+ (th
->th_off
<< 2), pd
, kif
);
7040 REASON_SET(reason
, PFRES_MEMORY
);
7046 /* translate source/destination address, if necessary */
7047 if (STATE_TRANSLATE(sk
)) {
7048 pd
->naf
= (pd
->af
== sk
->af_lan
) ? sk
->af_gwy
: sk
->af_lan
;
7050 if (direction
== PF_OUT
) {
7051 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
7052 pd
->ip_sum
, &th
->th_sum
, &sk
->gwy
.addr
,
7053 sk
->gwy
.xport
.port
, 0, pd
->af
, pd
->naf
, 1);
7055 if (pd
->af
!= pd
->naf
) {
7056 if (pd
->af
== sk
->af_gwy
) {
7057 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7058 &th
->th_dport
, pd
->ip_sum
,
7059 &th
->th_sum
, &sk
->lan
.addr
,
7060 sk
->lan
.xport
.port
, 0,
7061 pd
->af
, pd
->naf
, 0);
7063 pf_change_ap(direction
, pd
->mp
, pd
->src
,
7064 &th
->th_sport
, pd
->ip_sum
,
7065 &th
->th_sum
, &sk
->ext_lan
.addr
,
7066 th
->th_sport
, 0, pd
->af
,
7070 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7071 &th
->th_dport
, pd
->ip_sum
,
7072 &th
->th_sum
, &sk
->ext_gwy
.addr
,
7073 th
->th_dport
, 0, pd
->af
,
7076 pf_change_ap(direction
, pd
->mp
, pd
->src
,
7077 &th
->th_sport
, pd
->ip_sum
,
7078 &th
->th_sum
, &sk
->gwy
.addr
,
7079 sk
->gwy
.xport
.port
, 0, pd
->af
,
7083 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7084 &th
->th_dport
, pd
->ip_sum
,
7085 &th
->th_sum
, &sk
->lan
.addr
,
7086 sk
->lan
.xport
.port
, 0, pd
->af
,
7091 copyback
= off
+ sizeof (*th
);
7095 m
= pf_lazy_makewritable(pd
, m
, copyback
);
7097 REASON_SET(reason
, PFRES_MEMORY
);
7101 /* Copyback sequence modulation or stateful scrub changes */
7102 m_copyback(m
, off
, sizeof (*th
), th
);
7104 if (sk
->af_lan
!= sk
->af_gwy
)
7105 return (pf_do_nat64(sk
, pd
, m
, off
));
7111 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7112 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
7115 struct pf_state_peer
*src
, *dst
;
7116 struct pf_state_key_cmp key
;
7117 struct pf_state_key
*sk
;
7118 struct udphdr
*uh
= pd
->hdr
.udp
;
7119 struct pf_app_state as
;
7120 int action
, extfilter
;
7122 key
.proto_variant
= PF_EXTFILTER_APD
;
7124 key
.proto
= IPPROTO_UDP
;
7125 key
.af_lan
= key
.af_gwy
= pd
->af
;
7128 * For NAT64 the first time rule search and state creation
7129 * is done on the incoming side only.
7130 * Once the state gets created, NAT64's LAN side (ipv6) will
7131 * not be able to find the state in ext-gwy tree as that normally
7132 * is intended to be looked up for incoming traffic from the
7134 * Therefore to handle NAT64 case we init keys here for both
7135 * lan-ext as well as ext-gwy trees.
7136 * In the state lookup we attempt a lookup on both trees if
7137 * first one does not return any result and return a match if
7138 * the match state's was created by NAT64 rule.
7140 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
7141 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
7142 key
.ext_gwy
.xport
.port
= uh
->uh_sport
;
7143 key
.gwy
.xport
.port
= uh
->uh_dport
;
7145 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
7146 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
7147 key
.lan
.xport
.port
= uh
->uh_sport
;
7148 key
.ext_lan
.xport
.port
= uh
->uh_dport
;
7150 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
7151 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
7152 struct pf_ike_hdr ike
;
7153 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
7154 if (plen
< PF_IKE_PACKET_MINSIZE
) {
7155 DPFPRINTF(PF_DEBUG_MISC
,
7156 ("pf: IKE message too small.\n"));
7160 if (plen
> sizeof (ike
))
7161 plen
= sizeof (ike
);
7162 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
7164 if (ike
.initiator_cookie
) {
7165 key
.app_state
= &as
;
7166 as
.compare_lan_ext
= pf_ike_compare
;
7167 as
.compare_ext_gwy
= pf_ike_compare
;
7168 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
7171 * <http://tools.ietf.org/html/\
7172 * draft-ietf-ipsec-nat-t-ike-01>
7173 * Support non-standard NAT-T implementations that
7174 * push the ESP packet over the top of the IKE packet.
7175 * Do not drop packet.
7177 DPFPRINTF(PF_DEBUG_MISC
,
7178 ("pf: IKE initiator cookie = 0.\n"));
7182 *state
= pf_find_state(kif
, &key
, direction
);
7184 if (!key
.app_state
&& *state
== 0) {
7185 key
.proto_variant
= PF_EXTFILTER_AD
;
7186 *state
= pf_find_state(kif
, &key
, direction
);
7189 if (!key
.app_state
&& *state
== 0) {
7190 key
.proto_variant
= PF_EXTFILTER_EI
;
7191 *state
= pf_find_state(kif
, &key
, direction
);
7194 /* similar to STATE_LOOKUP() */
7195 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
7196 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
7197 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7198 if (pd
->flowhash
!= 0) {
7199 pd
->pktflags
|= PKTF_FLOW_ID
;
7200 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
7204 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7207 sk
= (*state
)->state_key
;
7210 * In case of NAT64 the translation is first applied on the LAN
7211 * side. Therefore for stack's address family comparison
7212 * we use sk->af_lan.
7214 if ((direction
== sk
->direction
) && (pd
->af
== sk
->af_lan
)) {
7215 src
= &(*state
)->src
;
7216 dst
= &(*state
)->dst
;
7218 src
= &(*state
)->dst
;
7219 dst
= &(*state
)->src
;
7223 if (src
->state
< PFUDPS_SINGLE
)
7224 src
->state
= PFUDPS_SINGLE
;
7225 if (dst
->state
== PFUDPS_SINGLE
)
7226 dst
->state
= PFUDPS_MULTIPLE
;
7228 /* update expire time */
7229 (*state
)->expire
= pf_time_second();
7230 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
7231 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
7233 (*state
)->timeout
= PFTM_UDP_SINGLE
;
7235 extfilter
= sk
->proto_variant
;
7236 if (extfilter
> PF_EXTFILTER_APD
) {
7237 if (direction
== PF_OUT
) {
7238 sk
->ext_lan
.xport
.port
= key
.ext_lan
.xport
.port
;
7239 if (extfilter
> PF_EXTFILTER_AD
)
7240 PF_ACPY(&sk
->ext_lan
.addr
, &key
.ext_lan
.addr
,
7243 sk
->ext_gwy
.xport
.port
= key
.ext_gwy
.xport
.port
;
7244 if (extfilter
> PF_EXTFILTER_AD
)
7245 PF_ACPY(&sk
->ext_gwy
.addr
, &key
.ext_gwy
.addr
,
7250 if (sk
->app_state
&& sk
->app_state
->handler
) {
7251 sk
->app_state
->handler(*state
, direction
, off
+ uh
->uh_ulen
,
7254 REASON_SET(reason
, PFRES_MEMORY
);
7260 /* translate source/destination address, if necessary */
7261 if (STATE_TRANSLATE(sk
)) {
7262 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
7264 REASON_SET(reason
, PFRES_MEMORY
);
7268 pd
->naf
= (pd
->af
== sk
->af_lan
) ? sk
->af_gwy
: sk
->af_lan
;
7270 if (direction
== PF_OUT
) {
7271 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
7272 pd
->ip_sum
, &uh
->uh_sum
, &sk
->gwy
.addr
,
7273 sk
->gwy
.xport
.port
, 1, pd
->af
, pd
->naf
, 1);
7275 if (pd
->af
!= pd
->naf
) {
7277 if (pd
->af
== sk
->af_gwy
) {
7278 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7279 &uh
->uh_dport
, pd
->ip_sum
,
7280 &uh
->uh_sum
, &sk
->lan
.addr
,
7281 sk
->lan
.xport
.port
, 1,
7282 pd
->af
, pd
->naf
, 0);
7284 pf_change_ap(direction
, pd
->mp
, pd
->src
,
7285 &uh
->uh_sport
, pd
->ip_sum
,
7286 &uh
->uh_sum
, &sk
->ext_lan
.addr
,
7287 uh
->uh_sport
, 1, pd
->af
,
7291 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7292 &uh
->uh_dport
, pd
->ip_sum
,
7293 &uh
->uh_sum
, &sk
->ext_gwy
.addr
,
7294 uh
->uh_dport
, 1, pd
->af
,
7297 pf_change_ap(direction
, pd
->mp
, pd
->src
,
7298 &uh
->uh_sport
, pd
->ip_sum
,
7299 &uh
->uh_sum
, &sk
->gwy
.addr
,
7300 sk
->gwy
.xport
.port
, 1, pd
->af
,
7304 pf_change_ap(direction
, pd
->mp
, pd
->dst
,
7305 &uh
->uh_dport
, pd
->ip_sum
,
7306 &uh
->uh_sum
, &sk
->lan
.addr
,
7307 sk
->lan
.xport
.port
, 1,
7308 pd
->af
, pd
->naf
, 1);
7312 m_copyback(m
, off
, sizeof (*uh
), uh
);
7313 if (sk
->af_lan
!= sk
->af_gwy
)
7314 return (pf_do_nat64(sk
, pd
, m
, off
));
7320 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7321 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
7324 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
7325 struct in_addr srcv4_inaddr
= saddr
->v4
;
7326 u_int16_t icmpid
= 0, *icmpsum
;
7329 struct pf_state_key_cmp key
;
7330 struct pf_state_key
*sk
;
7332 struct pf_app_state as
;
7337 switch (pd
->proto
) {
7340 icmptype
= pd
->hdr
.icmp
->icmp_type
;
7341 icmpid
= pd
->hdr
.icmp
->icmp_id
;
7342 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
7344 if (icmptype
== ICMP_UNREACH
||
7345 icmptype
== ICMP_SOURCEQUENCH
||
7346 icmptype
== ICMP_REDIRECT
||
7347 icmptype
== ICMP_TIMXCEED
||
7348 icmptype
== ICMP_PARAMPROB
)
7353 case IPPROTO_ICMPV6
:
7354 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
7355 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
7356 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
7358 if (icmptype
== ICMP6_DST_UNREACH
||
7359 icmptype
== ICMP6_PACKET_TOO_BIG
||
7360 icmptype
== ICMP6_TIME_EXCEEDED
||
7361 icmptype
== ICMP6_PARAM_PROB
)
7370 * ICMP query/reply message not related to a TCP/UDP packet.
7371 * Search for an ICMP state.
7374 * NAT64 requires protocol translation between ICMPv4
7375 * and ICMPv6. TCP and UDP do not require protocol
7376 * translation. To avoid adding complexity just to
7377 * handle ICMP(v4/v6), we always lookup for
7378 * proto = IPPROTO_ICMP on both LAN and WAN side
7380 key
.proto
= IPPROTO_ICMP
;
7381 key
.af_lan
= key
.af_gwy
= pd
->af
;
7383 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
7384 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
7385 key
.ext_gwy
.xport
.port
= 0;
7386 key
.gwy
.xport
.port
= icmpid
;
7388 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
7389 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
7390 key
.lan
.xport
.port
= icmpid
;
7391 key
.ext_lan
.xport
.port
= 0;
7395 sk
= (*state
)->state_key
;
7396 (*state
)->expire
= pf_time_second();
7397 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
7399 /* translate source/destination address, if necessary */
7400 if (STATE_TRANSLATE(sk
)) {
7401 pd
->naf
= (pd
->af
== sk
->af_lan
) ?
7402 sk
->af_gwy
: sk
->af_lan
;
7403 if (direction
== PF_OUT
) {
7407 pf_change_a(&saddr
->v4
.s_addr
,
7409 sk
->gwy
.addr
.v4
.s_addr
, 0);
7410 pd
->hdr
.icmp
->icmp_cksum
=
7412 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
7413 sk
->gwy
.xport
.port
, 0);
7414 pd
->hdr
.icmp
->icmp_id
=
7416 m
= pf_lazy_makewritable(pd
, m
,
7420 m_copyback(m
, off
, ICMP_MINLEN
,
7427 &pd
->hdr
.icmp6
->icmp6_cksum
,
7429 m
= pf_lazy_makewritable(pd
, m
,
7430 off
+ sizeof (struct icmp6_hdr
));
7434 sizeof (struct icmp6_hdr
),
7443 if (pd
->naf
!= AF_INET
) {
7444 if (pf_translate_icmp_af(
7445 AF_INET6
, pd
->hdr
.icmp
))
7448 pd
->proto
= IPPROTO_ICMPV6
;
7452 pf_change_a(&daddr
->v4
.s_addr
,
7454 sk
->lan
.addr
.v4
.s_addr
, 0);
7456 pd
->hdr
.icmp
->icmp_cksum
=
7458 pd
->hdr
.icmp
->icmp_cksum
,
7459 icmpid
, sk
->lan
.xport
.port
, 0);
7461 pd
->hdr
.icmp
->icmp_id
=
7465 m
= pf_lazy_makewritable(pd
, m
,
7469 m_copyback(m
, off
, ICMP_MINLEN
,
7471 if (sk
->af_lan
!= sk
->af_gwy
)
7472 return (pf_do_nat64(sk
, pd
, m
,
7478 if (pd
->naf
!= AF_INET6
) {
7479 if (pf_translate_icmp_af(
7480 AF_INET
, pd
->hdr
.icmp6
))
7483 pd
->proto
= IPPROTO_ICMP
;
7486 &pd
->hdr
.icmp6
->icmp6_cksum
,
7489 m
= pf_lazy_makewritable(pd
, m
,
7490 off
+ sizeof (struct icmp6_hdr
));
7494 sizeof (struct icmp6_hdr
),
7496 if (sk
->af_lan
!= sk
->af_gwy
)
7497 return (pf_do_nat64(sk
, pd
, m
,
7509 * ICMP error message in response to a TCP/UDP packet.
7510 * Extract the inner TCP/UDP header and search for that state.
7512 struct pf_pdesc pd2
; /* For inner (original) header */
7517 struct ip6_hdr h2_6
;
7523 memset(&pd2
, 0, sizeof (pd2
));
7529 /* offset of h2 in mbuf chain */
7530 ipoff2
= off
+ ICMP_MINLEN
;
7532 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
7533 NULL
, reason
, pd2
.af
)) {
7534 DPFPRINTF(PF_DEBUG_MISC
,
7535 ("pf: ICMP error message too short "
7540 * ICMP error messages don't refer to non-first
7543 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
7544 REASON_SET(reason
, PFRES_FRAG
);
7548 /* offset of protocol header that follows h2 */
7549 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
7551 pd2
.off
= ipoff2
+ (h2
.ip_hl
<< 2);
7553 pd2
.proto
= h2
.ip_p
;
7554 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
7555 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
7556 pd2
.ip_sum
= &h2
.ip_sum
;
7561 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
7563 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
7564 NULL
, reason
, pd2
.af
)) {
7565 DPFPRINTF(PF_DEBUG_MISC
,
7566 ("pf: ICMP error message too short "
7570 pd2
.proto
= h2_6
.ip6_nxt
;
7571 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
7572 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
7574 off2
= ipoff2
+ sizeof (h2_6
);
7576 switch (pd2
.proto
) {
7577 case IPPROTO_FRAGMENT
:
7579 * ICMPv6 error messages for
7580 * non-first fragments
7582 REASON_SET(reason
, PFRES_FRAG
);
7585 case IPPROTO_HOPOPTS
:
7586 case IPPROTO_ROUTING
:
7587 case IPPROTO_DSTOPTS
: {
7588 /* get next header and header length */
7589 struct ip6_ext opt6
;
7591 if (!pf_pull_hdr(m
, off2
, &opt6
,
7592 sizeof (opt6
), NULL
, reason
,
7594 DPFPRINTF(PF_DEBUG_MISC
,
7595 ("pf: ICMPv6 short opt\n"));
7598 if (pd2
.proto
== IPPROTO_AH
)
7599 off2
+= (opt6
.ip6e_len
+ 2) * 4;
7601 off2
+= (opt6
.ip6e_len
+ 1) * 8;
7602 pd2
.proto
= opt6
.ip6e_nxt
;
7603 /* goto the next header */
7610 } while (!terminal
);
7617 switch (pd2
.proto
) {
7621 struct pf_state_peer
*src
, *dst
;
7626 * Only the first 8 bytes of the TCP header can be
7627 * expected. Don't access any TCP header fields after
7628 * th_seq, an ackskew test is not possible.
7630 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
7632 DPFPRINTF(PF_DEBUG_MISC
,
7633 ("pf: ICMP error message too short "
7638 key
.proto
= IPPROTO_TCP
;
7639 key
.af_gwy
= pd2
.af
;
7640 PF_ACPY(&key
.ext_gwy
.addr
, pd2
.dst
, key
.af_gwy
);
7641 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af_gwy
);
7642 key
.ext_gwy
.xport
.port
= th
.th_dport
;
7643 key
.gwy
.xport
.port
= th
.th_sport
;
7645 key
.af_lan
= pd2
.af
;
7646 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af_lan
);
7647 PF_ACPY(&key
.ext_lan
.addr
, pd2
.src
, key
.af_lan
);
7648 key
.lan
.xport
.port
= th
.th_dport
;
7649 key
.ext_lan
.xport
.port
= th
.th_sport
;
7653 sk
= (*state
)->state_key
;
7654 if ((direction
== sk
->direction
) &&
7655 ((sk
->af_lan
== sk
->af_gwy
) ||
7656 (pd2
.af
== sk
->af_lan
))) {
7657 src
= &(*state
)->dst
;
7658 dst
= &(*state
)->src
;
7660 src
= &(*state
)->src
;
7661 dst
= &(*state
)->dst
;
7664 if (src
->wscale
&& (dst
->wscale
& PF_WSCALE_FLAG
))
7665 dws
= dst
->wscale
& PF_WSCALE_MASK
;
7667 dws
= TCP_MAX_WINSHIFT
;
7669 /* Demodulate sequence number */
7670 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
7672 pf_change_a(&th
.th_seq
, icmpsum
,
7677 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
7679 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
))) {
7680 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
7681 printf("pf: BAD ICMP %d:%d ",
7682 icmptype
, pd
->hdr
.icmp
->icmp_code
);
7683 pf_print_host(pd
->src
, 0, pd
->af
);
7685 pf_print_host(pd
->dst
, 0, pd
->af
);
7687 pf_print_state(*state
);
7688 printf(" seq=%u\n", seq
);
7690 REASON_SET(reason
, PFRES_BADSTATE
);
7694 pd
->naf
= pd2
.naf
= (pd2
.af
== sk
->af_lan
) ?
7695 sk
->af_gwy
: sk
->af_lan
;
7697 if (STATE_TRANSLATE(sk
)) {
7699 if (sk
->af_lan
!= sk
->af_gwy
) {
7700 struct pf_state_host
*saddr2
, *daddr2
;
7702 if (pd2
.naf
== sk
->af_lan
) {
7704 daddr2
= &sk
->ext_lan
;
7706 saddr2
= &sk
->ext_gwy
;
7710 /* translate ICMP message types and codes */
7711 if (pf_translate_icmp_af(pd
->naf
,
7715 pf_lazy_makewritable(pd
, m
, off2
+ 8);
7719 m_copyback(m
, pd
->off
,
7720 sizeof(struct icmp6_hdr
),
7724 * translate inner ip header within the
7727 if (pf_change_icmp_af(m
, ipoff2
, pd
,
7728 &pd2
, &saddr2
->addr
, &daddr2
->addr
,
7732 if (pd
->naf
== AF_INET
)
7733 pd
->proto
= IPPROTO_ICMP
;
7735 pd
->proto
= IPPROTO_ICMPV6
;
7738 * translate inner tcp header within
7741 pf_change_ap(direction
, NULL
, pd2
.src
,
7742 &th
.th_sport
, pd2
.ip_sum
,
7743 &th
.th_sum
, &daddr2
->addr
,
7744 saddr2
->xport
.port
, 0, pd2
.af
,
7747 pf_change_ap(direction
, NULL
, pd2
.dst
,
7748 &th
.th_dport
, pd2
.ip_sum
,
7749 &th
.th_sum
, &saddr2
->addr
,
7750 daddr2
->xport
.port
, 0, pd2
.af
,
7753 m_copyback(m
, pd2
.off
, 8, &th
);
7755 /* translate outer ip header */
7756 PF_ACPY(&pd
->naddr
, &daddr2
->addr
,
7758 PF_ACPY(&pd
->ndaddr
, &saddr2
->addr
,
7760 if (pd
->af
== AF_INET
) {
7761 memcpy(&pd
->naddr
.addr32
[3],
7763 sizeof(pd
->naddr
.addr32
[3]));
7764 return (pf_nat64_ipv4(m
, off
,
7767 return (pf_nat64_ipv6(m
, off
,
7771 if (direction
== PF_IN
) {
7772 pf_change_icmp(pd2
.src
, &th
.th_sport
,
7773 daddr
, &sk
->lan
.addr
,
7774 sk
->lan
.xport
.port
, NULL
,
7775 pd2
.ip_sum
, icmpsum
,
7776 pd
->ip_sum
, 0, pd2
.af
);
7778 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
7779 saddr
, &sk
->gwy
.addr
,
7780 sk
->gwy
.xport
.port
, NULL
,
7781 pd2
.ip_sum
, icmpsum
,
7782 pd
->ip_sum
, 0, pd2
.af
);
7788 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
7794 m_copyback(m
, off
, ICMP_MINLEN
,
7796 m_copyback(m
, ipoff2
, sizeof (h2
),
7803 sizeof (struct icmp6_hdr
),
7805 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7810 m_copyback(m
, off2
, 8, &th
);
7818 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
7819 NULL
, reason
, pd2
.af
)) {
7820 DPFPRINTF(PF_DEBUG_MISC
,
7821 ("pf: ICMP error message too short "
7826 key
.af_gwy
= pd2
.af
;
7827 PF_ACPY(&key
.ext_gwy
.addr
, pd2
.dst
, key
.af_gwy
);
7828 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af_gwy
);
7829 key
.ext_gwy
.xport
.port
= uh
.uh_dport
;
7830 key
.gwy
.xport
.port
= uh
.uh_sport
;
7832 key
.af_lan
= pd2
.af
;
7833 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af_lan
);
7834 PF_ACPY(&key
.ext_lan
.addr
, pd2
.src
, key
.af_lan
);
7835 key
.lan
.xport
.port
= uh
.uh_dport
;
7836 key
.ext_lan
.xport
.port
= uh
.uh_sport
;
7838 key
.proto
= IPPROTO_UDP
;
7839 key
.proto_variant
= PF_EXTFILTER_APD
;
7842 if (ntohs(uh
.uh_sport
) == PF_IKE_PORT
&&
7843 ntohs(uh
.uh_dport
) == PF_IKE_PORT
) {
7844 struct pf_ike_hdr ike
;
7846 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
7847 if (direction
== PF_IN
&&
7848 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
7849 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
7850 "ICMP error, embedded IKE message "
7855 if (plen
> sizeof (ike
))
7856 plen
= sizeof (ike
);
7857 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
7859 key
.app_state
= &as
;
7860 as
.compare_lan_ext
= pf_ike_compare
;
7861 as
.compare_ext_gwy
= pf_ike_compare
;
7862 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
7865 *state
= pf_find_state(kif
, &key
, dx
);
7867 if (key
.app_state
&& *state
== 0) {
7869 *state
= pf_find_state(kif
, &key
, dx
);
7873 key
.proto_variant
= PF_EXTFILTER_AD
;
7874 *state
= pf_find_state(kif
, &key
, dx
);
7878 key
.proto_variant
= PF_EXTFILTER_EI
;
7879 *state
= pf_find_state(kif
, &key
, dx
);
7882 /* similar to STATE_LOOKUP() */
7883 if (*state
!= NULL
&& pd
!= NULL
&&
7884 !(pd
->pktflags
& PKTF_FLOW_ID
)) {
7885 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
7886 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7887 if (pd
->flowhash
!= 0) {
7888 pd
->pktflags
|= PKTF_FLOW_ID
;
7889 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
7893 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7896 sk
= (*state
)->state_key
;
7897 pd
->naf
= pd2
.naf
= (pd2
.af
== sk
->af_lan
) ?
7898 sk
->af_gwy
: sk
->af_lan
;
7900 if (STATE_TRANSLATE(sk
)) {
7902 if (sk
->af_lan
!= sk
->af_gwy
) {
7903 struct pf_state_host
*saddr2
, *daddr2
;
7905 if (pd2
.naf
== sk
->af_lan
) {
7907 daddr2
= &sk
->ext_lan
;
7909 saddr2
= &sk
->ext_gwy
;
7913 /* translate ICMP message */
7914 if (pf_translate_icmp_af(pd
->naf
,
7918 pf_lazy_makewritable(pd
, m
, off2
+ 8);
7922 m_copyback(m
, pd
->off
,
7923 sizeof(struct icmp6_hdr
),
7927 * translate inner ip header within the
7930 if (pf_change_icmp_af(m
, ipoff2
, pd
,
7931 &pd2
, &saddr2
->addr
, &daddr2
->addr
,
7935 if (pd
->naf
== AF_INET
)
7936 pd
->proto
= IPPROTO_ICMP
;
7938 pd
->proto
= IPPROTO_ICMPV6
;
7941 * translate inner udp header within
7944 pf_change_ap(direction
, NULL
, pd2
.src
,
7945 &uh
.uh_sport
, pd2
.ip_sum
,
7946 &uh
.uh_sum
, &daddr2
->addr
,
7947 saddr2
->xport
.port
, 0, pd2
.af
,
7950 pf_change_ap(direction
, NULL
, pd2
.dst
,
7951 &uh
.uh_dport
, pd2
.ip_sum
,
7952 &uh
.uh_sum
, &saddr2
->addr
,
7953 daddr2
->xport
.port
, 0, pd2
.af
,
7956 m_copyback(m
, pd2
.off
, sizeof(uh
), &uh
);
7958 /* translate outer ip header */
7959 PF_ACPY(&pd
->naddr
, &daddr2
->addr
,
7961 PF_ACPY(&pd
->ndaddr
, &saddr2
->addr
,
7963 if (pd
->af
== AF_INET
) {
7964 memcpy(&pd
->naddr
.addr32
[3],
7966 sizeof(pd
->naddr
.addr32
[3]));
7967 return (pf_nat64_ipv4(m
, off
,
7970 return (pf_nat64_ipv6(m
, off
,
7974 if (direction
== PF_IN
) {
7975 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
7976 daddr
, &sk
->lan
.addr
,
7977 sk
->lan
.xport
.port
, &uh
.uh_sum
,
7978 pd2
.ip_sum
, icmpsum
,
7979 pd
->ip_sum
, 1, pd2
.af
);
7981 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
7982 saddr
, &sk
->gwy
.addr
,
7983 sk
->gwy
.xport
.port
, &uh
.uh_sum
,
7984 pd2
.ip_sum
, icmpsum
,
7985 pd
->ip_sum
, 1, pd2
.af
);
7987 m
= pf_lazy_makewritable(pd
, m
,
7988 off2
+ sizeof (uh
));
7994 m_copyback(m
, off
, ICMP_MINLEN
,
7996 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
8002 sizeof (struct icmp6_hdr
),
8004 m_copyback(m
, ipoff2
, sizeof (h2_6
),
8009 m_copyback(m
, off2
, sizeof (uh
), &uh
);
8015 case IPPROTO_ICMP
: {
8018 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
8019 NULL
, reason
, pd2
.af
)) {
8020 DPFPRINTF(PF_DEBUG_MISC
,
8021 ("pf: ICMP error message too short i"
8026 key
.proto
= IPPROTO_ICMP
;
8027 if (direction
== PF_IN
) {
8028 key
.af_gwy
= pd2
.af
;
8029 PF_ACPY(&key
.ext_gwy
.addr
, pd2
.dst
, key
.af_gwy
);
8030 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af_gwy
);
8031 key
.ext_gwy
.xport
.port
= 0;
8032 key
.gwy
.xport
.port
= iih
.icmp_id
;
8034 key
.af_lan
= pd2
.af
;
8035 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af_lan
);
8036 PF_ACPY(&key
.ext_lan
.addr
, pd2
.src
, key
.af_lan
);
8037 key
.lan
.xport
.port
= iih
.icmp_id
;
8038 key
.ext_lan
.xport
.port
= 0;
8043 sk
= (*state
)->state_key
;
8044 if (STATE_TRANSLATE(sk
)) {
8045 if (direction
== PF_IN
) {
8046 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
8047 daddr
, &sk
->lan
.addr
,
8048 sk
->lan
.xport
.port
, NULL
,
8049 pd2
.ip_sum
, icmpsum
,
8050 pd
->ip_sum
, 0, AF_INET
);
8052 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
8053 saddr
, &sk
->gwy
.addr
,
8054 sk
->gwy
.xport
.port
, NULL
,
8055 pd2
.ip_sum
, icmpsum
,
8056 pd
->ip_sum
, 0, AF_INET
);
8058 m
= pf_lazy_makewritable(pd
, m
,
8059 off2
+ ICMP_MINLEN
);
8062 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
8063 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
8064 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
8071 case IPPROTO_ICMPV6
: {
8072 struct icmp6_hdr iih
;
8074 if (!pf_pull_hdr(m
, off2
, &iih
,
8075 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
8076 DPFPRINTF(PF_DEBUG_MISC
,
8077 ("pf: ICMP error message too short "
8082 key
.proto
= IPPROTO_ICMPV6
;
8083 if (direction
== PF_IN
) {
8084 key
.af_gwy
= pd2
.af
;
8085 PF_ACPY(&key
.ext_gwy
.addr
, pd2
.dst
, key
.af_gwy
);
8086 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af_gwy
);
8087 key
.ext_gwy
.xport
.port
= 0;
8088 key
.gwy
.xport
.port
= iih
.icmp6_id
;
8090 key
.af_lan
= pd2
.af
;
8091 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af_lan
);
8092 PF_ACPY(&key
.ext_lan
.addr
, pd2
.src
, key
.af_lan
);
8093 key
.lan
.xport
.port
= iih
.icmp6_id
;
8094 key
.ext_lan
.xport
.port
= 0;
8099 sk
= (*state
)->state_key
;
8100 if (STATE_TRANSLATE(sk
)) {
8101 if (direction
== PF_IN
) {
8102 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
8103 daddr
, &sk
->lan
.addr
,
8104 sk
->lan
.xport
.port
, NULL
,
8105 pd2
.ip_sum
, icmpsum
,
8106 pd
->ip_sum
, 0, AF_INET6
);
8108 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
8109 saddr
, &sk
->gwy
.addr
,
8110 sk
->gwy
.xport
.port
, NULL
,
8111 pd2
.ip_sum
, icmpsum
,
8112 pd
->ip_sum
, 0, AF_INET6
);
8114 m
= pf_lazy_makewritable(pd
, m
, off2
+
8115 sizeof (struct icmp6_hdr
));
8118 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
8120 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
8121 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
8129 key
.proto
= pd2
.proto
;
8130 if (direction
== PF_IN
) {
8131 key
.af_gwy
= pd2
.af
;
8132 PF_ACPY(&key
.ext_gwy
.addr
, pd2
.dst
, key
.af_gwy
);
8133 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af_gwy
);
8134 key
.ext_gwy
.xport
.port
= 0;
8135 key
.gwy
.xport
.port
= 0;
8137 key
.af_lan
= pd2
.af
;
8138 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af_lan
);
8139 PF_ACPY(&key
.ext_lan
.addr
, pd2
.src
, key
.af_lan
);
8140 key
.lan
.xport
.port
= 0;
8141 key
.ext_lan
.xport
.port
= 0;
8146 sk
= (*state
)->state_key
;
8147 if (STATE_TRANSLATE(sk
)) {
8148 if (direction
== PF_IN
) {
8149 pf_change_icmp(pd2
.src
, NULL
, daddr
,
8150 &sk
->lan
.addr
, 0, NULL
,
8151 pd2
.ip_sum
, icmpsum
,
8152 pd
->ip_sum
, 0, pd2
.af
);
8154 pf_change_icmp(pd2
.dst
, NULL
, saddr
,
8155 &sk
->gwy
.addr
, 0, NULL
,
8156 pd2
.ip_sum
, icmpsum
,
8157 pd
->ip_sum
, 0, pd2
.af
);
8162 m
= pf_lazy_makewritable(pd
, m
,
8163 ipoff2
+ sizeof (h2
));
8169 m
= pf_lazy_makewritable(pd
, m
,
8170 ipoff2
+ sizeof (h2_6
));
8174 sizeof (struct icmp6_hdr
),
8176 m_copyback(m
, ipoff2
, sizeof (h2_6
),
8190 pf_test_state_grev1(struct pf_state
**state
, int direction
,
8191 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
8193 struct pf_state_peer
*src
;
8194 struct pf_state_peer
*dst
;
8195 struct pf_state_key_cmp key
;
8196 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
8200 key
.proto
= IPPROTO_GRE
;
8201 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8202 if (direction
== PF_IN
) {
8203 key
.af_gwy
= pd
->af
;
8204 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
8205 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
8206 key
.gwy
.xport
.call_id
= grev1
->call_id
;
8208 key
.af_lan
= pd
->af
;
8209 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
8210 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
8211 key
.ext_lan
.xport
.call_id
= grev1
->call_id
;
8216 if (direction
== (*state
)->state_key
->direction
) {
8217 src
= &(*state
)->src
;
8218 dst
= &(*state
)->dst
;
8220 src
= &(*state
)->dst
;
8221 dst
= &(*state
)->src
;
8225 if (src
->state
< PFGRE1S_INITIATING
)
8226 src
->state
= PFGRE1S_INITIATING
;
8228 /* update expire time */
8229 (*state
)->expire
= pf_time_second();
8230 if (src
->state
>= PFGRE1S_INITIATING
&&
8231 dst
->state
>= PFGRE1S_INITIATING
) {
8232 if ((*state
)->timeout
!= PFTM_TCP_ESTABLISHED
)
8233 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
8234 src
->state
= PFGRE1S_ESTABLISHED
;
8235 dst
->state
= PFGRE1S_ESTABLISHED
;
8237 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
8240 if ((*state
)->state_key
->app_state
)
8241 (*state
)->state_key
->app_state
->u
.grev1
.pptp_state
->expire
=
8244 /* translate source/destination address, if necessary */
8245 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
8246 if (direction
== PF_OUT
) {
8250 pf_change_a(&pd
->src
->v4
.s_addr
,
8252 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
8257 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
8263 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
8268 pf_change_a(&pd
->dst
->v4
.s_addr
,
8270 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
8275 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
8282 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
8285 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
8292 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
8293 int off
, struct pf_pdesc
*pd
)
8296 struct pf_state_peer
*src
;
8297 struct pf_state_peer
*dst
;
8298 struct pf_state_key_cmp key
;
8299 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
8302 memset(&key
, 0, sizeof (key
));
8303 key
.proto
= IPPROTO_ESP
;
8304 if (direction
== PF_IN
) {
8305 key
.af_gwy
= pd
->af
;
8306 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
8307 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
8308 key
.gwy
.xport
.spi
= esp
->spi
;
8310 key
.af_lan
= pd
->af
;
8311 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
8312 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
8313 key
.ext_lan
.xport
.spi
= esp
->spi
;
8316 *state
= pf_find_state(kif
, &key
, direction
);
8323 * No matching state. Look for a blocking state. If we find
8324 * one, then use that state and move it so that it's keyed to
8325 * the SPI in the current packet.
8327 if (direction
== PF_IN
) {
8328 key
.gwy
.xport
.spi
= 0;
8330 s
= pf_find_state(kif
, &key
, direction
);
8332 struct pf_state_key
*sk
= s
->state_key
;
8334 RB_REMOVE(pf_state_tree_ext_gwy
,
8335 &pf_statetbl_ext_gwy
, sk
);
8336 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
8339 if (RB_INSERT(pf_state_tree_ext_gwy
,
8340 &pf_statetbl_ext_gwy
, sk
))
8341 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
8346 key
.ext_lan
.xport
.spi
= 0;
8348 s
= pf_find_state(kif
, &key
, direction
);
8350 struct pf_state_key
*sk
= s
->state_key
;
8352 RB_REMOVE(pf_state_tree_lan_ext
,
8353 &pf_statetbl_lan_ext
, sk
);
8354 sk
->ext_lan
.xport
.spi
= esp
->spi
;
8356 if (RB_INSERT(pf_state_tree_lan_ext
,
8357 &pf_statetbl_lan_ext
, sk
))
8358 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
8367 if (s
->creatorid
== pf_status
.hostid
)
8368 pfsync_delete_state(s
);
8370 s
->timeout
= PFTM_UNLINKED
;
8371 hook_runloop(&s
->unlink_hooks
,
8372 HOOK_REMOVE
|HOOK_FREE
);
8373 pf_src_tree_remove_state(s
);
8380 /* similar to STATE_LOOKUP() */
8381 if (*state
!= NULL
&& pd
!= NULL
&& !(pd
->pktflags
& PKTF_FLOW_ID
)) {
8382 pd
->flowsrc
= (*state
)->state_key
->flowsrc
;
8383 pd
->flowhash
= (*state
)->state_key
->flowhash
;
8384 if (pd
->flowhash
!= 0) {
8385 pd
->pktflags
|= PKTF_FLOW_ID
;
8386 pd
->pktflags
&= ~PKTF_FLOW_ADV
;
8390 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
8393 if (direction
== (*state
)->state_key
->direction
) {
8394 src
= &(*state
)->src
;
8395 dst
= &(*state
)->dst
;
8397 src
= &(*state
)->dst
;
8398 dst
= &(*state
)->src
;
8402 if (src
->state
< PFESPS_INITIATING
)
8403 src
->state
= PFESPS_INITIATING
;
8405 /* update expire time */
8406 (*state
)->expire
= pf_time_second();
8407 if (src
->state
>= PFESPS_INITIATING
&&
8408 dst
->state
>= PFESPS_INITIATING
) {
8409 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
8410 src
->state
= PFESPS_ESTABLISHED
;
8411 dst
->state
= PFESPS_ESTABLISHED
;
8413 (*state
)->timeout
= PFTM_ESP_INITIATING
;
8415 /* translate source/destination address, if necessary */
8416 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
8417 if (direction
== PF_OUT
) {
8421 pf_change_a(&pd
->src
->v4
.s_addr
,
8423 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
8428 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
8437 pf_change_a(&pd
->dst
->v4
.s_addr
,
8439 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
8444 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
8456 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
8457 struct pf_pdesc
*pd
)
8459 struct pf_state_peer
*src
, *dst
;
8460 struct pf_state_key_cmp key
;
8463 key
.proto
= pd
->proto
;
8464 if (direction
== PF_IN
) {
8465 key
.af_gwy
= pd
->af
;
8466 PF_ACPY(&key
.ext_gwy
.addr
, pd
->src
, key
.af_gwy
);
8467 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af_gwy
);
8468 key
.ext_gwy
.xport
.port
= 0;
8469 key
.gwy
.xport
.port
= 0;
8471 key
.af_lan
= pd
->af
;
8472 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af_lan
);
8473 PF_ACPY(&key
.ext_lan
.addr
, pd
->dst
, key
.af_lan
);
8474 key
.lan
.xport
.port
= 0;
8475 key
.ext_lan
.xport
.port
= 0;
8480 if (direction
== (*state
)->state_key
->direction
) {
8481 src
= &(*state
)->src
;
8482 dst
= &(*state
)->dst
;
8484 src
= &(*state
)->dst
;
8485 dst
= &(*state
)->src
;
8489 if (src
->state
< PFOTHERS_SINGLE
)
8490 src
->state
= PFOTHERS_SINGLE
;
8491 if (dst
->state
== PFOTHERS_SINGLE
)
8492 dst
->state
= PFOTHERS_MULTIPLE
;
8494 /* update expire time */
8495 (*state
)->expire
= pf_time_second();
8496 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
8497 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
8499 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
8501 /* translate source/destination address, if necessary */
8502 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
8503 if (direction
== PF_OUT
) {
8507 pf_change_a(&pd
->src
->v4
.s_addr
,
8509 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
8516 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
8524 pf_change_a(&pd
->dst
->v4
.s_addr
,
8526 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
8533 &(*state
)->state_key
->lan
.addr
, pd
->af
);
8544 * ipoff and off are measured from the start of the mbuf chain.
8545 * h must be at "ipoff" on the mbuf chain.
8548 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
8549 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
8554 struct ip
*h
= mtod(m
, struct ip
*);
8555 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
8558 if (fragoff
>= len
) {
8559 ACTION_SET(actionp
, PF_PASS
);
8561 ACTION_SET(actionp
, PF_DROP
);
8562 REASON_SET(reasonp
, PFRES_FRAG
);
8566 if (m
->m_pkthdr
.len
< off
+ len
||
8567 ntohs(h
->ip_len
) < off
+ len
) {
8568 ACTION_SET(actionp
, PF_DROP
);
8569 REASON_SET(reasonp
, PFRES_SHORT
);
8577 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
8579 if (m
->m_pkthdr
.len
< off
+ len
||
8580 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
8581 (unsigned)(off
+ len
)) {
8582 ACTION_SET(actionp
, PF_DROP
);
8583 REASON_SET(reasonp
, PFRES_SHORT
);
8590 m_copydata(m
, off
, len
, p
);
8595 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
8598 struct sockaddr_in
*dst
;
8601 struct sockaddr_in6
*dst6
;
8602 struct route_in6 ro
;
8607 bzero(&ro
, sizeof (ro
));
8610 dst
= satosin(&ro
.ro_dst
);
8611 dst
->sin_family
= AF_INET
;
8612 dst
->sin_len
= sizeof (*dst
);
8613 dst
->sin_addr
= addr
->v4
;
8617 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
8618 dst6
->sin6_family
= AF_INET6
;
8619 dst6
->sin6_len
= sizeof (*dst6
);
8620 dst6
->sin6_addr
= addr
->v6
;
8627 /* XXX: IFT_ENC is not currently used by anything*/
8628 /* Skip checks for ipsec interfaces */
8629 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
8632 /* XXX: what is the point of this? */
8633 rtalloc((struct route
*)&ro
);
8641 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
8644 struct sockaddr_in
*dst
;
8646 struct sockaddr_in6
*dst6
;
8647 struct route_in6 ro
;
8653 bzero(&ro
, sizeof (ro
));
8656 dst
= satosin(&ro
.ro_dst
);
8657 dst
->sin_family
= AF_INET
;
8658 dst
->sin_len
= sizeof (*dst
);
8659 dst
->sin_addr
= addr
->v4
;
8663 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
8664 dst6
->sin6_family
= AF_INET6
;
8665 dst6
->sin6_len
= sizeof (*dst6
);
8666 dst6
->sin6_addr
= addr
->v6
;
8673 /* XXX: what is the point of this? */
8674 rtalloc((struct route
*)&ro
);
8683 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
8684 struct pf_state
*s
, struct pf_pdesc
*pd
)
8687 struct mbuf
*m0
, *m1
;
8688 struct route iproute
;
8689 struct route
*ro
= &iproute
;
8690 struct sockaddr_in
*dst
;
8692 struct ifnet
*ifp
= NULL
;
8693 struct pf_addr naddr
;
8694 struct pf_src_node
*sn
= NULL
;
8698 bzero(&iproute
, sizeof (iproute
));
8700 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
8701 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
8702 panic("pf_route: invalid parameters");
8704 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
8710 if (r
->rt
== PF_DUPTO
) {
8711 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
8714 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
8719 if (m0
->m_len
< (int)sizeof (struct ip
)) {
8720 DPFPRINTF(PF_DEBUG_URGENT
,
8721 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
8725 ip
= mtod(m0
, struct ip
*);
8727 dst
= satosin((void *)&ro
->ro_dst
);
8728 dst
->sin_family
= AF_INET
;
8729 dst
->sin_len
= sizeof (*dst
);
8730 dst
->sin_addr
= ip
->ip_dst
;
8732 if (r
->rt
== PF_FASTROUTE
) {
8734 if (ro
->ro_rt
== NULL
) {
8735 ipstat
.ips_noroute
++;
8739 ifp
= ro
->ro_rt
->rt_ifp
;
8741 ro
->ro_rt
->rt_use
++;
8743 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
8744 dst
= satosin((void *)ro
->ro_rt
->rt_gateway
);
8745 RT_UNLOCK(ro
->ro_rt
);
8747 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
8748 DPFPRINTF(PF_DEBUG_URGENT
,
8749 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
8753 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
8755 if (!PF_AZERO(&naddr
, AF_INET
))
8756 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
8757 ifp
= r
->rpool
.cur
->kif
?
8758 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
8760 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
8761 dst
->sin_addr
.s_addr
=
8762 s
->rt_addr
.v4
.s_addr
;
8763 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
8770 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
8772 else if (m0
== NULL
)
8774 if (m0
->m_len
< (int)sizeof (struct ip
)) {
8775 DPFPRINTF(PF_DEBUG_URGENT
,
8776 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
8779 ip
= mtod(m0
, struct ip
*);
8782 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
8783 ip_output_checksum(ifp
, m0
, ((ip
->ip_hl
) << 2), ntohs(ip
->ip_len
),
8786 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
|| TSO_IPV4_OK(ifp
, m0
) ||
8787 (!(ip
->ip_off
& htons(IP_DF
)) &&
8788 (ifp
->if_hwassist
& CSUM_FRAGMENT
))) {
8790 if (sw_csum
& CSUM_DELAY_IP
) {
8791 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
8792 sw_csum
&= ~CSUM_DELAY_IP
;
8793 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IP
;
8795 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
, sintosa(dst
));
8800 * Too large for interface; fragment if possible.
8801 * Must be able to put at least 8 bytes per fragment.
8802 * Balk when DF bit is set or the interface didn't support TSO.
8804 if ((ip
->ip_off
& htons(IP_DF
)) ||
8805 (m0
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
)) {
8806 ipstat
.ips_cantfrag
++;
8807 if (r
->rt
!= PF_DUPTO
) {
8808 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
8817 /* PR-8933605: send ip_len,ip_off to ip_fragment in host byte order */
8818 #if BYTE_ORDER != BIG_ENDIAN
8822 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
8829 for (m0
= m1
; m0
; m0
= m1
) {
8833 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
,
8840 ipstat
.ips_fragmented
++;
8843 if (r
->rt
!= PF_DUPTO
)
8846 ROUTE_RELEASE(&iproute
);
8857 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
8858 struct pf_state
*s
, struct pf_pdesc
*pd
)
8862 struct route_in6 ip6route
;
8863 struct route_in6
*ro
;
8864 struct sockaddr_in6
*dst
;
8865 struct ip6_hdr
*ip6
;
8866 struct ifnet
*ifp
= NULL
;
8867 struct pf_addr naddr
;
8868 struct pf_src_node
*sn
= NULL
;
8871 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
8872 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
8873 panic("pf_route6: invalid parameters");
8875 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
8881 if (r
->rt
== PF_DUPTO
) {
8882 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
8885 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
8890 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
8891 DPFPRINTF(PF_DEBUG_URGENT
,
8892 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
8895 ip6
= mtod(m0
, struct ip6_hdr
*);
8898 bzero((caddr_t
)ro
, sizeof (*ro
));
8899 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
8900 dst
->sin6_family
= AF_INET6
;
8901 dst
->sin6_len
= sizeof (*dst
);
8902 dst
->sin6_addr
= ip6
->ip6_dst
;
8904 /* Cheat. XXX why only in the v6 case??? */
8905 if (r
->rt
== PF_FASTROUTE
) {
8906 struct pf_mtag
*pf_mtag
;
8908 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
8910 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
8911 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
8915 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
8916 DPFPRINTF(PF_DEBUG_URGENT
,
8917 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
8921 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
8923 if (!PF_AZERO(&naddr
, AF_INET6
))
8924 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
8926 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
8928 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
8929 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
8930 &s
->rt_addr
, AF_INET6
);
8931 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
8937 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
8939 else if (m0
== NULL
)
8941 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
8942 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
8943 "< sizeof (struct ip6_hdr)\n"));
8946 ip6
= mtod(m0
, struct ip6_hdr
*);
8950 * If the packet is too large for the outgoing interface,
8951 * send back an icmp6 error.
8953 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
8954 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
8955 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
8956 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, NULL
);
8958 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
8959 if (r
->rt
!= PF_DUPTO
)
8960 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
8966 if (r
->rt
!= PF_DUPTO
)
8978 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
8979 * off is the offset where the protocol header starts
8980 * len is the total length of protocol header plus payload
8981 * returns 0 when the checksum is valid, otherwise returns 1.
8984 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
8993 * Optimize for the common case; if the hardware calculated
8994 * value doesn't include pseudo-header checksum, or if it
8995 * is partially-computed (only 16-bit summation), do it in
8998 if ((m
->m_pkthdr
.csum_flags
&
8999 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) ==
9000 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
) &&
9001 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
9007 case IPPROTO_ICMPV6
:
9013 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
9015 if (m
->m_pkthdr
.len
< off
+ len
)
9020 if (p
== IPPROTO_ICMP
) {
9025 sum
= in_cksum(m
, len
);
9029 if (m
->m_len
< (int)sizeof (struct ip
))
9031 sum
= inet_cksum(m
, p
, off
, len
);
9037 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
9039 sum
= inet6_cksum(m
, p
, off
, len
);
9048 tcpstat
.tcps_rcvbadsum
++;
9051 udpstat
.udps_badsum
++;
9054 icmpstat
.icps_checksum
++;
9057 case IPPROTO_ICMPV6
:
9058 icmp6stat
.icp6s_checksum
++;
9068 #define PF_APPLE_UPDATE_PDESC_IPv4() \
9070 if (m && pd.mp && m != pd.mp) { \
9072 h = mtod(m, struct ip *); \
9073 pd.pf_mtag = pf_get_mtag(m); \
9078 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
9079 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
9084 struct pfi_kif
*kif
;
9085 u_short action
= PF_PASS
, reason
= 0, log
= 0;
9086 struct mbuf
*m
= *m0
;
9088 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
9089 struct pf_state
*s
= NULL
;
9090 struct pf_state_key
*sk
= NULL
;
9091 struct pf_ruleset
*ruleset
= NULL
;
9093 int off
, dirndx
, pqid
= 0;
9095 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9097 if (!pf_status
.running
)
9100 memset(&pd
, 0, sizeof (pd
));
9102 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
9103 DPFPRINTF(PF_DEBUG_URGENT
,
9104 ("pf_test: pf_get_mtag returned NULL\n"));
9108 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
9111 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
9114 DPFPRINTF(PF_DEBUG_URGENT
,
9115 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
9118 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
9121 VERIFY(m
->m_flags
& M_PKTHDR
);
9123 /* initialize enough of pd for the done label */
9124 h
= mtod(m
, struct ip
*);
9127 pd
.pf_mtag
= pf_get_mtag(m
);
9128 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
9129 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
9130 PF_ACPY(&pd
.baddr
, pd
.src
, AF_INET
);
9131 PF_ACPY(&pd
.bdaddr
, pd
.dst
, AF_INET
);
9132 pd
.ip_sum
= &h
->ip_sum
;
9134 pd
.proto_variant
= 0;
9138 pd
.tot_len
= ntohs(h
->ip_len
);
9141 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
9143 REASON_SET(&reason
, PFRES_SHORT
);
9149 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
9151 #endif /* DUMMYNET */
9153 /* We do IP header normalization and packet reassembly here */
9154 action
= pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
);
9156 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
9163 #endif /* DUMMYNET */
9164 m
= *m0
; /* pf_normalize messes with m0 */
9165 h
= mtod(m
, struct ip
*);
9167 off
= h
->ip_hl
<< 2;
9168 if (off
< (int)sizeof (*h
)) {
9170 REASON_SET(&reason
, PFRES_SHORT
);
9175 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
9176 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
9177 PF_ACPY(&pd
.baddr
, pd
.src
, AF_INET
);
9178 PF_ACPY(&pd
.bdaddr
, pd
.dst
, AF_INET
);
9179 pd
.ip_sum
= &h
->ip_sum
;
9181 pd
.proto_variant
= 0;
9184 pd
.pf_mtag
= pf_get_mtag(m
);
9188 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
9189 pd
.tot_len
= ntohs(h
->ip_len
);
9192 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
9193 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
9194 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
9195 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
9198 /* handle fragments that didn't get reassembled by normalization */
9199 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
9200 pd
.flags
|= PFDESC_IP_FRAG
;
9202 /* Traffic goes through dummynet first */
9203 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9204 if (action
== PF_DROP
|| m
== NULL
) {
9208 #endif /* DUMMYNET */
9209 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
9219 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
9220 &action
, &reason
, AF_INET
)) {
9221 log
= action
!= PF_PASS
;
9224 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
9225 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
9228 /* Traffic goes through dummynet first */
9229 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9230 if (action
== PF_DROP
|| m
== NULL
) {
9234 #endif /* DUMMYNET */
9235 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
9238 PF_APPLE_UPDATE_PDESC_IPv4();
9239 if (action
== PF_DROP
)
9241 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
9243 if (action
== PF_NAT64
)
9247 PF_APPLE_UPDATE_PDESC_IPv4();
9248 if (action
== PF_PASS
) {
9250 pfsync_update_state(s
);
9251 #endif /* NPFSYNC */
9255 } else if (s
== NULL
)
9256 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9257 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9265 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
9266 &action
, &reason
, AF_INET
)) {
9267 log
= action
!= PF_PASS
;
9270 if (uh
.uh_dport
== 0 ||
9271 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
9272 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
9274 REASON_SET(&reason
, PFRES_SHORT
);
9278 /* Traffic goes through dummynet first */
9279 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9280 if (action
== PF_DROP
|| m
== NULL
) {
9284 #endif /* DUMMYNET */
9285 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
9287 if (action
== PF_NAT64
)
9291 PF_APPLE_UPDATE_PDESC_IPv4();
9292 if (action
== PF_PASS
) {
9294 pfsync_update_state(s
);
9295 #endif /* NPFSYNC */
9299 } else if (s
== NULL
)
9300 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9301 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9305 case IPPROTO_ICMP
: {
9309 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
9310 &action
, &reason
, AF_INET
)) {
9311 log
= action
!= PF_PASS
;
9315 /* Traffic goes through dummynet first */
9316 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9317 if (action
== PF_DROP
|| m
== NULL
) {
9321 #endif /* DUMMYNET */
9322 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
9324 if (action
== PF_NAT64
)
9328 PF_APPLE_UPDATE_PDESC_IPv4();
9329 if (action
== PF_PASS
) {
9331 pfsync_update_state(s
);
9332 #endif /* NPFSYNC */
9336 } else if (s
== NULL
)
9337 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9338 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9343 struct pf_esp_hdr esp
;
9346 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
9348 log
= action
!= PF_PASS
;
9352 /* Traffic goes through dummynet first */
9353 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9354 if (action
== PF_DROP
|| m
== NULL
) {
9358 #endif /* DUMMYNET */
9359 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
9362 PF_APPLE_UPDATE_PDESC_IPv4();
9363 if (action
== PF_PASS
) {
9365 pfsync_update_state(s
);
9366 #endif /* NPFSYNC */
9370 } else if (s
== NULL
)
9371 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9372 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9377 struct pf_grev1_hdr grev1
;
9378 pd
.hdr
.grev1
= &grev1
;
9379 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
9380 &reason
, AF_INET
)) {
9381 log
= (action
!= PF_PASS
);
9385 /* Traffic goes through dummynet first */
9386 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9387 if (action
== PF_DROP
|| m
== NULL
) {
9391 #endif /* DUMMYNET */
9392 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
9393 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
9394 if (ntohs(grev1
.payload_length
) >
9395 m
->m_pkthdr
.len
- off
) {
9397 REASON_SET(&reason
, PFRES_SHORT
);
9400 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
9401 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
9402 if (pd
.lmw
< 0) goto done
;
9403 PF_APPLE_UPDATE_PDESC_IPv4();
9404 if (action
== PF_PASS
) {
9406 pfsync_update_state(s
);
9407 #endif /* NPFSYNC */
9412 } else if (s
== NULL
) {
9413 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
9414 h
, &pd
, &a
, &ruleset
, NULL
);
9415 if (action
== PF_PASS
)
9420 /* not GREv1/PPTP, so treat as ordinary GRE... */
9425 /* Traffic goes through dummynet first */
9426 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9427 if (action
== PF_DROP
|| m
== NULL
) {
9431 #endif /* DUMMYNET */
9432 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
9435 PF_APPLE_UPDATE_PDESC_IPv4();
9436 if (action
== PF_PASS
) {
9438 pfsync_update_state(s
);
9439 #endif /* NPFSYNC */
9443 } else if (s
== NULL
)
9444 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9445 &pd
, &a
, &ruleset
, NULL
);
9450 if (action
== PF_NAT64
) {
9456 PF_APPLE_UPDATE_PDESC_IPv4();
9458 if (action
!= PF_DROP
) {
9459 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
9460 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9462 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9464 DPFPRINTF(PF_DEBUG_MISC
,
9465 ("pf: dropping packet with ip options [hlen=%u]\n",
9466 (unsigned int) h
->ip_hl
));
9469 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
9470 (pd
.pktflags
& PKTF_FLOW_ID
))
9471 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9474 if (action
== PF_PASS
) {
9476 if (altq_allowed
&& r
->qid
) {
9477 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
9478 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
9480 pd
.pf_mtag
->pftag_qid
= r
->qid
;
9482 #endif /* PF_ALTQ */
9484 /* add hints for ecn */
9485 pd
.pf_mtag
->pftag_hdr
= h
;
9486 /* record address family */
9487 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET6
;
9488 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
9490 /* record protocol */
9491 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
9494 * connections redirected to loopback should not match sockets
9495 * bound specifically to loopback due to security implications,
9496 * see tcp_input() and in_pcblookup_listen().
9498 if (dir
== PF_IN
&& (pd
.proto
== IPPROTO_TCP
||
9499 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&&
9500 s
->nat_rule
.ptr
!= NULL
&&
9501 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9502 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9503 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
)
9505 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9512 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9513 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9514 lr
= s
->nat_rule
.ptr
;
9517 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
9521 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9522 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
9524 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9525 dirndx
= (dir
== PF_OUT
);
9526 r
->packets
[dirndx
]++;
9527 r
->bytes
[dirndx
] += pd
.tot_len
;
9529 a
->packets
[dirndx
]++;
9530 a
->bytes
[dirndx
] += pd
.tot_len
;
9534 if (s
->nat_rule
.ptr
!= NULL
) {
9535 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9536 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9538 if (s
->src_node
!= NULL
) {
9539 s
->src_node
->packets
[dirndx
]++;
9540 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9542 if (s
->nat_src_node
!= NULL
) {
9543 s
->nat_src_node
->packets
[dirndx
]++;
9544 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9546 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9547 s
->packets
[dirndx
]++;
9548 s
->bytes
[dirndx
] += pd
.tot_len
;
9551 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9555 * XXX: we need to make sure that the addresses
9556 * passed to pfr_update_stats() are the same than
9557 * the addresses used during matching (pfr_match)
9559 if (r
== &pf_default_rule
) {
9561 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
9562 &pd
.baddr
: &pd
.naddr
;
9564 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
9565 &pd
.naddr
: &pd
.baddr
;
9566 if (x
== &pd
.baddr
|| s
== NULL
) {
9567 /* we need to change the address */
9574 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9575 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9576 sk
->direction
== dir
) ?
9577 pd
.src
: pd
.dst
, pd
.af
,
9578 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9580 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9581 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9582 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9583 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9587 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
9591 REASON_SET(&reason
, PFRES_MEMORY
);
9595 if (action
== PF_DROP
) {
9604 if (action
== PF_SYNPROXY_DROP
) {
9609 /* pf_route can free the mbuf causing *m0 to become NULL */
9610 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9617 #define PF_APPLE_UPDATE_PDESC_IPv6() \
9619 if (m && pd.mp && m != pd.mp) { \
9623 h = mtod(m, struct ip6_hdr *); \
9628 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
9629 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
9634 struct pfi_kif
*kif
;
9635 u_short action
= PF_PASS
, reason
= 0, log
= 0;
9636 struct mbuf
*m
= *m0
, *n
= NULL
;
9638 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
9639 struct pf_state
*s
= NULL
;
9640 struct pf_state_key
*sk
= NULL
;
9641 struct pf_ruleset
*ruleset
= NULL
;
9643 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
9646 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9648 if (!pf_status
.running
)
9651 memset(&pd
, 0, sizeof (pd
));
9653 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
9654 DPFPRINTF(PF_DEBUG_URGENT
,
9655 ("pf_test6: pf_get_mtag returned NULL\n"));
9659 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
9662 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
9665 DPFPRINTF(PF_DEBUG_URGENT
,
9666 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
9669 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
9672 VERIFY(m
->m_flags
& M_PKTHDR
);
9674 h
= mtod(m
, struct ip6_hdr
*);
9677 off
= ((caddr_t
)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
9680 pd
.pf_mtag
= pf_get_mtag(m
);
9681 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
9682 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
9683 PF_ACPY(&pd
.baddr
, pd
.src
, AF_INET6
);
9684 PF_ACPY(&pd
.bdaddr
, pd
.dst
, AF_INET6
);
9688 pd
.proto_variant
= 0;
9690 pd
.ttl
= h
->ip6_hlim
;
9691 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
9692 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
9695 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_ID
) {
9696 pd
.flowsrc
= m
->m_pkthdr
.pkt_flowsrc
;
9697 pd
.flowhash
= m
->m_pkthdr
.pkt_flowid
;
9698 pd
.pktflags
= (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_MASK
);
9701 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
9703 REASON_SET(&reason
, PFRES_SHORT
);
9709 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
9711 #endif /* DUMMYNET */
9713 /* We do IP header normalization and packet reassembly here */
9714 action
= pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
);
9716 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
9723 #endif /* DUMMYNET */
9724 h
= mtod(m
, struct ip6_hdr
*);
9728 * we do not support jumbogram yet. if we keep going, zero ip6_plen
9729 * will do something bad, so drop the packet for now.
9731 if (htons(h
->ip6_plen
) == 0) {
9733 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
9738 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
9739 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
9740 PF_ACPY(&pd
.baddr
, pd
.src
, AF_INET6
);
9741 PF_ACPY(&pd
.bdaddr
, pd
.dst
, AF_INET6
);
9745 pd
.ttl
= h
->ip6_hlim
;
9746 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
9749 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
9750 pd
.proto
= h
->ip6_nxt
;
9751 pd
.proto_variant
= 0;
9754 pd
.pf_mtag
= pf_get_mtag(m
);
9758 case IPPROTO_FRAGMENT
: {
9759 struct ip6_frag ip6f
;
9761 pd
.flags
|= PFDESC_IP_FRAG
;
9762 if (!pf_pull_hdr(m
, off
, &ip6f
, sizeof ip6f
, NULL
,
9764 DPFPRINTF(PF_DEBUG_MISC
,
9765 ("pf: IPv6 short fragment header\n"));
9767 REASON_SET(&reason
, PFRES_SHORT
);
9771 pd
.proto
= nxt
= ip6f
.ip6f_nxt
;
9773 /* Traffic goes through dummynet first */
9774 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9775 if (action
== PF_DROP
|| m
== NULL
) {
9779 #endif /* DUMMYNET */
9780 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
, &pd
, &a
,
9782 if (action
== PF_DROP
) {
9783 REASON_SET(&reason
, PFRES_FRAG
);
9788 case IPPROTO_ROUTING
:
9793 case IPPROTO_HOPOPTS
:
9794 case IPPROTO_DSTOPTS
: {
9795 /* get next header and header length */
9796 struct ip6_ext opt6
;
9798 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
9799 NULL
, &reason
, pd
.af
)) {
9800 DPFPRINTF(PF_DEBUG_MISC
,
9801 ("pf: IPv6 short opt\n"));
9806 if (pd
.proto
== IPPROTO_AH
)
9807 off
+= (opt6
.ip6e_len
+ 2) * 4;
9809 off
+= (opt6
.ip6e_len
+ 1) * 8;
9810 nxt
= opt6
.ip6e_nxt
;
9811 /* goto the next header */
9818 } while (!terminal
);
9820 /* if there's no routing header, use unmodified mbuf for checksumming */
9830 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
9831 &action
, &reason
, AF_INET6
)) {
9832 log
= action
!= PF_PASS
;
9835 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
9837 /* Traffic goes through dummynet first */
9838 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9839 if (action
== PF_DROP
|| m
== NULL
) {
9843 #endif /* DUMMYNET */
9844 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
9847 PF_APPLE_UPDATE_PDESC_IPv6();
9848 if (action
== PF_DROP
)
9850 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
9852 if (action
== PF_NAT64
)
9856 PF_APPLE_UPDATE_PDESC_IPv6();
9857 if (action
== PF_PASS
) {
9859 pfsync_update_state(s
);
9860 #endif /* NPFSYNC */
9864 } else if (s
== NULL
)
9865 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9866 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9874 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
9875 &action
, &reason
, AF_INET6
)) {
9876 log
= action
!= PF_PASS
;
9879 if (uh
.uh_dport
== 0 ||
9880 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
9881 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
9883 REASON_SET(&reason
, PFRES_SHORT
);
9887 /* Traffic goes through dummynet first */
9888 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9889 if (action
== PF_DROP
|| m
== NULL
) {
9893 #endif /* DUMMYNET */
9894 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
9896 if (action
== PF_NAT64
)
9900 PF_APPLE_UPDATE_PDESC_IPv6();
9901 if (action
== PF_PASS
) {
9903 pfsync_update_state(s
);
9904 #endif /* NPFSYNC */
9908 } else if (s
== NULL
)
9909 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9910 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9914 case IPPROTO_ICMPV6
: {
9915 struct icmp6_hdr ih
;
9918 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
9919 &action
, &reason
, AF_INET6
)) {
9920 log
= action
!= PF_PASS
;
9924 /* Traffic goes through dummynet first */
9925 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9926 if (action
== PF_DROP
|| m
== NULL
) {
9930 #endif /* DUMMYNET */
9931 action
= pf_test_state_icmp(&s
, dir
, kif
,
9932 m
, off
, h
, &pd
, &reason
);
9933 if (action
== PF_NAT64
)
9937 PF_APPLE_UPDATE_PDESC_IPv6();
9938 if (action
== PF_PASS
) {
9940 pfsync_update_state(s
);
9941 #endif /* NPFSYNC */
9945 } else if (s
== NULL
)
9946 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9947 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9952 struct pf_esp_hdr esp
;
9955 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
9957 log
= action
!= PF_PASS
;
9961 /* Traffic goes through dummynet first */
9962 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9963 if (action
== PF_DROP
|| m
== NULL
) {
9967 #endif /* DUMMYNET */
9968 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
9971 PF_APPLE_UPDATE_PDESC_IPv6();
9972 if (action
== PF_PASS
) {
9974 pfsync_update_state(s
);
9975 #endif /* NPFSYNC */
9979 } else if (s
== NULL
)
9980 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9981 m
, off
, h
, &pd
, &a
, &ruleset
, NULL
);
9986 struct pf_grev1_hdr grev1
;
9988 pd
.hdr
.grev1
= &grev1
;
9989 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
9990 &reason
, AF_INET6
)) {
9991 log
= (action
!= PF_PASS
);
9995 /* Traffic goes through dummynet first */
9996 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
9997 if (action
== PF_DROP
|| m
== NULL
) {
10001 #endif /* DUMMYNET */
10002 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
10003 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
10004 if (ntohs(grev1
.payload_length
) >
10005 m
->m_pkthdr
.len
- off
) {
10007 REASON_SET(&reason
, PFRES_SHORT
);
10010 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
10013 PF_APPLE_UPDATE_PDESC_IPv6();
10014 if (action
== PF_PASS
) {
10016 pfsync_update_state(s
);
10017 #endif /* NPFSYNC */
10022 } else if (s
== NULL
) {
10023 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
10024 h
, &pd
, &a
, &ruleset
, NULL
);
10025 if (action
== PF_PASS
)
10030 /* not GREv1/PPTP, so treat as ordinary GRE... */
10035 /* Traffic goes through dummynet first */
10036 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
10037 if (action
== PF_DROP
|| m
== NULL
) {
10041 #endif /* DUMMYNET */
10042 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
10045 PF_APPLE_UPDATE_PDESC_IPv6();
10046 if (action
== PF_PASS
) {
10048 pfsync_update_state(s
);
10049 #endif /* NPFSYNC */
10053 } else if (s
== NULL
)
10054 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
10055 &pd
, &a
, &ruleset
, NULL
);
10060 if (action
== PF_NAT64
) {
10066 PF_APPLE_UPDATE_PDESC_IPv6();
10073 /* handle dangerous IPv6 extension headers. */
10074 if (action
!= PF_DROP
) {
10075 if (action
== PF_PASS
&& rh_cnt
&&
10076 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
10078 REASON_SET(&reason
, PFRES_IPOPTIONS
);
10080 DPFPRINTF(PF_DEBUG_MISC
,
10081 ("pf: dropping packet with dangerous v6 headers\n"));
10084 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
10085 (pd
.pktflags
& PKTF_FLOW_ID
))
10086 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
10089 if (action
== PF_PASS
) {
10091 if (altq_allowed
&& r
->qid
) {
10092 if (pd
.tos
& IPTOS_LOWDELAY
)
10093 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
10095 pd
.pf_mtag
->pftag_qid
= r
->qid
;
10097 #endif /* PF_ALTQ */
10099 /* add hints for ecn */
10100 pd
.pf_mtag
->pftag_hdr
= h
;
10101 /* record address family */
10102 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET
;
10103 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
10104 #endif /* PF_ECN */
10105 /* record protocol */
10106 m
->m_pkthdr
.pkt_proto
= pd
.proto
;
10107 if (dir
== PF_IN
&& (pd
.proto
== IPPROTO_TCP
||
10108 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&&
10109 s
->nat_rule
.ptr
!= NULL
&&
10110 (s
->nat_rule
.ptr
->action
== PF_RDR
||
10111 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
10112 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
10113 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
10119 struct pf_rule
*lr
;
10121 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
10122 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
10123 lr
= s
->nat_rule
.ptr
;
10126 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
10130 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
10131 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
10133 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
10134 dirndx
= (dir
== PF_OUT
);
10135 r
->packets
[dirndx
]++;
10136 r
->bytes
[dirndx
] += pd
.tot_len
;
10138 a
->packets
[dirndx
]++;
10139 a
->bytes
[dirndx
] += pd
.tot_len
;
10143 if (s
->nat_rule
.ptr
!= NULL
) {
10144 s
->nat_rule
.ptr
->packets
[dirndx
]++;
10145 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
10147 if (s
->src_node
!= NULL
) {
10148 s
->src_node
->packets
[dirndx
]++;
10149 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
10151 if (s
->nat_src_node
!= NULL
) {
10152 s
->nat_src_node
->packets
[dirndx
]++;
10153 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
10155 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
10156 s
->packets
[dirndx
]++;
10157 s
->bytes
[dirndx
] += pd
.tot_len
;
10160 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
10164 * XXX: we need to make sure that the addresses
10165 * passed to pfr_update_stats() are the same than
10166 * the addresses used during matching (pfr_match)
10168 if (r
== &pf_default_rule
) {
10170 x
= (s
== NULL
|| sk
->direction
== dir
) ?
10171 &pd
.baddr
: &pd
.naddr
;
10173 x
= (s
== NULL
|| sk
->direction
== dir
) ?
10174 &pd
.naddr
: &pd
.baddr
;
10176 if (x
== &pd
.baddr
|| s
== NULL
) {
10183 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
10184 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
10185 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
10186 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
10188 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
10189 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
10190 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
10191 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
10196 if (action
== PF_SYNPROXY_DROP
) {
10201 /* pf_route6 can free the mbuf causing *m0 to become NULL */
10202 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
10204 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
10208 REASON_SET(&reason
, PFRES_MEMORY
);
10212 if (action
== PF_DROP
) {
10221 if (action
== PF_SYNPROXY_DROP
) {
10225 } else if (r
->rt
) {
10226 if (action
== PF_PASS
) {
10228 h
= mtod(m
, struct ip6_hdr
*);
10231 /* pf_route6 can free the mbuf causing *m0 to become NULL */
10232 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
10241 pf_check_congestion(struct ifqueue
*ifq
)
10243 #pragma unused(ifq)
10248 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
10249 int flags
, const char *wchan
, void *palloc
)
10251 #pragma unused(align, ioff, flags, palloc)
10252 bzero(pp
, sizeof (*pp
));
10253 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
10254 if (pp
->pool_zone
!= NULL
) {
10255 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
10256 zone_change(pp
->pool_zone
, Z_CALLERACCT
, FALSE
);
10257 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
10258 pp
->pool_name
= wchan
;
10262 /* Zones cannot be currently destroyed */
10264 pool_destroy(struct pool
*pp
)
10270 pool_sethiwat(struct pool
*pp
, int n
)
10272 pp
->pool_hiwat
= n
; /* Currently unused */
10276 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
10278 #pragma unused(warnmess, ratecap)
10279 pp
->pool_limit
= n
;
10283 pool_get(struct pool
*pp
, int flags
)
10287 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
10289 if (pp
->pool_count
> pp
->pool_limit
) {
10290 DPFPRINTF(PF_DEBUG_NOISY
,
10291 ("pf: pool %s hard limit reached (%d)\n",
10292 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
10298 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
10301 VERIFY(pp
->pool_count
!= 0);
10307 pool_put(struct pool
*pp
, void *v
)
10309 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
10311 zfree(pp
->pool_zone
, v
);
10312 VERIFY(pp
->pool_count
!= 0);
10317 pf_find_mtag(struct mbuf
*m
)
10319 if (!(m
->m_flags
& M_PKTHDR
))
10322 return (m_pftag(m
));
10326 pf_get_mtag(struct mbuf
*m
)
10328 return (pf_find_mtag(m
));
10332 pf_time_second(void)
10341 pf_calendar_time_second(void)
10350 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
10352 struct hook_desc
*hd
;
10354 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
10361 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
10363 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
10369 hook_runloop(struct hook_desc_head
*head
, int flags
)
10371 struct hook_desc
*hd
;
10373 if (!(flags
& HOOK_REMOVE
)) {
10374 if (!(flags
& HOOK_ABORT
))
10375 TAILQ_FOREACH(hd
, head
, hd_list
)
10376 hd
->hd_fn(hd
->hd_arg
);
10378 while (!!(hd
= TAILQ_FIRST(head
))) {
10379 TAILQ_REMOVE(head
, hd
, hd_list
);
10380 if (!(flags
& HOOK_ABORT
))
10381 hd
->hd_fn(hd
->hd_arg
);
10382 if (flags
& HOOK_FREE
)
10383 _FREE(hd
, M_DEVBUF
);