2 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $apfw: git commit 6602420f2f101b74305cd78f7cd9e0c8fdedae97 $ */
30 /* $OpenBSD: pf.c,v 1.567 2008/02/20 23:40:13 henning Exp $ */
33 * Copyright (c) 2001 Daniel Hartmeier
34 * Copyright (c) 2002,2003 Henning Brauer
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * - Redistributions in binary form must reproduce the above
44 * copyright notice, this list of conditions and the following
45 * disclaimer in the documentation and/or other materials provided
46 * with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
61 * Effort sponsored in part by the Defense Advanced Research Projects
62 * Agency (DARPA) and Air Force Research Laboratory, Air Force
63 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
67 #include <machine/endian.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/filio.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/kernel.h>
77 #include <sys/random.h>
78 #include <sys/mcache.h>
80 #include <libkern/crypto/md5.h>
81 #include <libkern/libkern.h>
83 #include <mach/thread_act.h>
86 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/udp.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcp_var.h>
102 #include <netinet/tcp_fsm.h>
103 #include <netinet/udp_var.h>
104 #include <netinet/icmp_var.h>
105 #include <net/if_ether.h>
106 #include <net/ethernet.h>
107 #include <net/flowhash.h>
108 #include <net/pfvar.h>
109 #include <net/if_pflog.h>
112 #include <net/if_pfsync.h>
116 #include <netinet/ip6.h>
117 #include <netinet6/in6_pcb.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet/icmp6.h>
120 #include <netinet6/nd6.h>
124 #include <netinet/ip_dummynet.h>
125 #endif /* DUMMYNET */
127 #define DPFPRINTF(n, x) (pf_status.debug >= (n) ? printf x : ((void)0))
130 * On Mac OS X, the rtableid value is treated as the interface scope
131 * value that is equivalent to the interface index used for scoped
132 * routing. A valid scope value is anything but IFSCOPE_NONE (0),
133 * as per definition of ifindex which is a positive, non-zero number.
134 * The other BSDs treat a negative rtableid value as invalid, hence
135 * the test against INT_MAX to handle userland apps which initialize
136 * the field with a negative number.
138 #define PF_RTABLEID_IS_VALID(r) \
139 ((r) > IFSCOPE_NONE && (r) <= INT_MAX)
144 decl_lck_mtx_data(,pf_lock_data
);
145 decl_lck_rw_data(,pf_perim_lock_data
);
146 lck_mtx_t
*pf_lock
= &pf_lock_data
;
147 lck_rw_t
*pf_perim_lock
= &pf_perim_lock_data
;
150 struct pf_state_tree_lan_ext pf_statetbl_lan_ext
;
151 struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy
;
153 struct pf_palist pf_pabuf
;
154 struct pf_status pf_status
;
157 struct pf_altqqueue pf_altqs
[2];
158 struct pf_altqqueue
*pf_altqs_active
;
159 struct pf_altqqueue
*pf_altqs_inactive
;
160 u_int32_t ticket_altqs_active
;
161 u_int32_t ticket_altqs_inactive
;
162 int altqs_inactive_open
;
164 u_int32_t ticket_pabuf
;
166 static MD5_CTX pf_tcp_secret_ctx
;
167 static u_char pf_tcp_secret
[16];
168 static int pf_tcp_secret_init
;
169 static int pf_tcp_iss_off
;
171 static struct pf_anchor_stackframe
{
172 struct pf_ruleset
*rs
;
174 struct pf_anchor_node
*parent
;
175 struct pf_anchor
*child
;
176 } pf_anchor_stack
[64];
178 struct pool pf_src_tree_pl
, pf_rule_pl
, pf_pooladdr_pl
;
179 struct pool pf_state_pl
, pf_state_key_pl
;
181 struct pool pf_altq_pl
;
184 typedef void (*hook_fn_t
)(void *);
187 TAILQ_ENTRY(hook_desc
) hd_list
;
192 #define HOOK_REMOVE 0x01
193 #define HOOK_FREE 0x02
194 #define HOOK_ABORT 0x04
196 static void *hook_establish(struct hook_desc_head
*, int,
198 static void hook_runloop(struct hook_desc_head
*, int flags
);
200 struct pool pf_app_state_pl
;
201 static void pf_print_addr(struct pf_addr
*addr
, sa_family_t af
);
202 static void pf_print_sk_host(struct pf_state_host
*, u_int8_t
, int,
205 static void pf_print_host(struct pf_addr
*, u_int16_t
, u_int8_t
);
207 static void pf_init_threshold(struct pf_threshold
*, u_int32_t
,
209 static void pf_add_threshold(struct pf_threshold
*);
210 static int pf_check_threshold(struct pf_threshold
*);
212 static void pf_change_ap(int, struct mbuf
*, struct pf_addr
*,
213 u_int16_t
*, u_int16_t
*, u_int16_t
*,
214 struct pf_addr
*, u_int16_t
, u_int8_t
, sa_family_t
);
215 static int pf_modulate_sack(struct mbuf
*, int, struct pf_pdesc
*,
216 struct tcphdr
*, struct pf_state_peer
*);
218 static void pf_change_a6(struct pf_addr
*, u_int16_t
*,
219 struct pf_addr
*, u_int8_t
);
221 static void pf_change_icmp(struct pf_addr
*, u_int16_t
*,
222 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
223 u_int16_t
*, u_int16_t
*, u_int16_t
*,
224 u_int16_t
*, u_int8_t
, sa_family_t
);
225 static void pf_send_tcp(const struct pf_rule
*, sa_family_t
,
226 const struct pf_addr
*, const struct pf_addr
*,
227 u_int16_t
, u_int16_t
, u_int32_t
, u_int32_t
,
228 u_int8_t
, u_int16_t
, u_int16_t
, u_int8_t
, int,
229 u_int16_t
, struct ether_header
*, struct ifnet
*);
230 static void pf_send_icmp(struct mbuf
*, u_int8_t
, u_int8_t
,
231 sa_family_t
, struct pf_rule
*);
232 static struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
233 int, int, struct pfi_kif
*, struct pf_addr
*,
234 union pf_state_xport
*, struct pf_addr
*,
235 union pf_state_xport
*, int);
236 static struct pf_rule
*pf_get_translation_aux(struct pf_pdesc
*,
237 struct mbuf
*, int, int, struct pfi_kif
*,
238 struct pf_src_node
**, struct pf_addr
*,
239 union pf_state_xport
*, struct pf_addr
*,
240 union pf_state_xport
*, struct pf_addr
*,
241 union pf_state_xport
*);
242 static void pf_attach_state(struct pf_state_key
*,
243 struct pf_state
*, int);
244 static void pf_detach_state(struct pf_state
*, int);
245 static u_int32_t
pf_tcp_iss(struct pf_pdesc
*);
246 static int pf_test_rule(struct pf_rule
**, struct pf_state
**,
247 int, struct pfi_kif
*, struct mbuf
*, int,
248 void *, struct pf_pdesc
*, struct pf_rule
**,
249 struct pf_ruleset
**, struct ifqueue
*);
251 static int pf_test_dummynet(struct pf_rule
**, int,
252 struct pfi_kif
*, struct mbuf
**,
253 struct pf_pdesc
*, struct ip_fw_args
*);
254 #endif /* DUMMYNET */
255 static int pf_test_fragment(struct pf_rule
**, int,
256 struct pfi_kif
*, struct mbuf
*, void *,
257 struct pf_pdesc
*, struct pf_rule
**,
258 struct pf_ruleset
**);
259 static int pf_test_state_tcp(struct pf_state
**, int,
260 struct pfi_kif
*, struct mbuf
*, int,
261 void *, struct pf_pdesc
*, u_short
*);
262 static int pf_test_state_udp(struct pf_state
**, int,
263 struct pfi_kif
*, struct mbuf
*, int,
264 void *, struct pf_pdesc
*, u_short
*);
265 static int pf_test_state_icmp(struct pf_state
**, int,
266 struct pfi_kif
*, struct mbuf
*, int,
267 void *, struct pf_pdesc
*, u_short
*);
268 static int pf_test_state_other(struct pf_state
**, int,
269 struct pfi_kif
*, struct pf_pdesc
*);
270 static int pf_match_tag(struct mbuf
*, struct pf_rule
*,
271 struct pf_mtag
*, int *);
272 static void pf_hash(struct pf_addr
*, struct pf_addr
*,
273 struct pf_poolhashkey
*, sa_family_t
);
274 static int pf_map_addr(u_int8_t
, struct pf_rule
*,
275 struct pf_addr
*, struct pf_addr
*,
276 struct pf_addr
*, struct pf_src_node
**);
277 static int pf_get_sport(struct pf_pdesc
*, struct pfi_kif
*,
278 struct pf_rule
*, struct pf_addr
*,
279 union pf_state_xport
*, struct pf_addr
*,
280 union pf_state_xport
*, struct pf_addr
*,
281 union pf_state_xport
*, struct pf_src_node
**);
282 static void pf_route(struct mbuf
**, struct pf_rule
*, int,
283 struct ifnet
*, struct pf_state
*,
286 static void pf_route6(struct mbuf
**, struct pf_rule
*, int,
287 struct ifnet
*, struct pf_state
*,
290 static u_int8_t
pf_get_wscale(struct mbuf
*, int, u_int16_t
,
292 static u_int16_t
pf_get_mss(struct mbuf
*, int, u_int16_t
,
294 static u_int16_t
pf_calc_mss(struct pf_addr
*, sa_family_t
,
296 static void pf_set_rt_ifp(struct pf_state
*,
298 static int pf_check_proto_cksum(struct mbuf
*, int, int,
299 u_int8_t
, sa_family_t
);
300 static int pf_addr_wrap_neq(struct pf_addr_wrap
*,
301 struct pf_addr_wrap
*);
302 static struct pf_state
*pf_find_state(struct pfi_kif
*,
303 struct pf_state_key_cmp
*, u_int
);
304 static int pf_src_connlimit(struct pf_state
**);
305 static void pf_stateins_err(const char *, struct pf_state
*,
307 static int pf_check_congestion(struct ifqueue
*);
310 static const char *pf_pptp_ctrl_type_name(u_int16_t code
);
312 static void pf_pptp_handler(struct pf_state
*, int, int,
313 struct pf_pdesc
*, struct pfi_kif
*);
314 static void pf_pptp_unlink(struct pf_state
*);
315 static void pf_grev1_unlink(struct pf_state
*);
316 static int pf_test_state_grev1(struct pf_state
**, int,
317 struct pfi_kif
*, int, struct pf_pdesc
*);
318 static int pf_ike_compare(struct pf_app_state
*,
319 struct pf_app_state
*);
320 static int pf_test_state_esp(struct pf_state
**, int,
321 struct pfi_kif
*, int, struct pf_pdesc
*);
323 extern struct pool pfr_ktable_pl
;
324 extern struct pool pfr_kentry_pl
;
325 extern int path_mtu_discovery
;
327 struct pf_pool_limit pf_pool_limits
[PF_LIMIT_MAX
] = {
328 { &pf_state_pl
, PFSTATE_HIWAT
},
329 { &pf_app_state_pl
, PFAPPSTATE_HIWAT
},
330 { &pf_src_tree_pl
, PFSNODE_HIWAT
},
331 { &pf_frent_pl
, PFFRAG_FRENT_HIWAT
},
332 { &pfr_ktable_pl
, PFR_KTABLE_HIWAT
},
333 { &pfr_kentry_pl
, PFR_KENTRY_HIWAT
},
337 pf_lazy_makewritable(struct pf_pdesc
*pd
, struct mbuf
*m
, int len
)
345 if (m_makewritable(&m
, 0, len
, M_DONTWAIT
))
348 if (len
>= 0 && m
!= pd
->mp
) {
350 pd
->pf_mtag
= pf_find_mtag(m
);
354 struct ip
*h
= mtod(m
, struct ip
*);
355 pd
->src
= (struct pf_addr
*)&h
->ip_src
;
356 pd
->dst
= (struct pf_addr
*)&h
->ip_dst
;
357 pd
->ip_sum
= &h
->ip_sum
;
362 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
363 pd
->src
= (struct pf_addr
*)&h
->ip6_src
;
364 pd
->dst
= (struct pf_addr
*)&h
->ip6_dst
;
372 return (len
< 0 ? 0 : m
);
376 pf_state_lookup_aux(struct pf_state
**state
, struct pfi_kif
*kif
,
377 int direction
, int *action
)
379 if (*state
== NULL
|| (*state
)->timeout
== PFTM_PURGE
) {
384 if (direction
== PF_OUT
&&
385 (((*state
)->rule
.ptr
->rt
== PF_ROUTETO
&&
386 (*state
)->rule
.ptr
->direction
== PF_OUT
) ||
387 ((*state
)->rule
.ptr
->rt
== PF_REPLYTO
&&
388 (*state
)->rule
.ptr
->direction
== PF_IN
)) &&
389 (*state
)->rt_kif
!= NULL
&& (*state
)->rt_kif
!= kif
) {
397 #define STATE_LOOKUP() \
400 *state = pf_find_state(kif, &key, direction); \
401 if (*state != NULL && pd != NULL && \
402 pd->flowhash == 0) { \
403 pd->flowhash = (*state)->state_key->flowhash; \
405 if (pf_state_lookup_aux(state, kif, direction, &action)) \
409 #define STATE_ADDR_TRANSLATE(sk) \
410 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
411 ((sk)->af == AF_INET6 && \
412 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
413 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
414 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3]))
416 #define STATE_TRANSLATE(sk) \
417 (STATE_ADDR_TRANSLATE(sk) || \
418 (sk)->lan.xport.port != (sk)->gwy.xport.port)
420 #define STATE_GRE_TRANSLATE(sk) \
421 (STATE_ADDR_TRANSLATE(sk) || \
422 (sk)->lan.xport.call_id != (sk)->gwy.xport.call_id)
424 #define BOUND_IFACE(r, k) \
425 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
427 #define STATE_INC_COUNTERS(s) \
429 s->rule.ptr->states++; \
430 VERIFY(s->rule.ptr->states != 0); \
431 if (s->anchor.ptr != NULL) { \
432 s->anchor.ptr->states++; \
433 VERIFY(s->anchor.ptr->states != 0); \
435 if (s->nat_rule.ptr != NULL) { \
436 s->nat_rule.ptr->states++; \
437 VERIFY(s->nat_rule.ptr->states != 0); \
441 #define STATE_DEC_COUNTERS(s) \
443 if (s->nat_rule.ptr != NULL) { \
444 VERIFY(s->nat_rule.ptr->states > 0); \
445 s->nat_rule.ptr->states--; \
447 if (s->anchor.ptr != NULL) { \
448 VERIFY(s->anchor.ptr->states > 0); \
449 s->anchor.ptr->states--; \
451 VERIFY(s->rule.ptr->states > 0); \
452 s->rule.ptr->states--; \
455 static __inline
int pf_src_compare(struct pf_src_node
*, struct pf_src_node
*);
456 static __inline
int pf_state_compare_lan_ext(struct pf_state_key
*,
457 struct pf_state_key
*);
458 static __inline
int pf_state_compare_ext_gwy(struct pf_state_key
*,
459 struct pf_state_key
*);
460 static __inline
int pf_state_compare_id(struct pf_state
*,
463 struct pf_src_tree tree_src_tracking
;
465 struct pf_state_tree_id tree_id
;
466 struct pf_state_queue state_list
;
468 RB_GENERATE(pf_src_tree
, pf_src_node
, entry
, pf_src_compare
);
469 RB_GENERATE(pf_state_tree_lan_ext
, pf_state_key
,
470 entry_lan_ext
, pf_state_compare_lan_ext
);
471 RB_GENERATE(pf_state_tree_ext_gwy
, pf_state_key
,
472 entry_ext_gwy
, pf_state_compare_ext_gwy
);
473 RB_GENERATE(pf_state_tree_id
, pf_state
,
474 entry_id
, pf_state_compare_id
);
476 #define PF_DT_SKIP_LANEXT 0x01
477 #define PF_DT_SKIP_EXTGWY 0x02
479 static const u_int16_t PF_PPTP_PORT
= 1723;
480 static const u_int32_t PF_PPTP_MAGIC_NUMBER
= 0x1A2B3C4D;
488 struct pf_pptp_ctrl_hdr
{
490 u_int16_t reserved_0
;
493 struct pf_pptp_ctrl_generic
{
497 #define PF_PPTP_CTRL_TYPE_START_REQ 1
498 struct pf_pptp_ctrl_start_req
{
499 u_int16_t protocol_version
;
500 u_int16_t reserved_1
;
501 u_int32_t framing_capabilities
;
502 u_int32_t bearer_capabilities
;
503 u_int16_t maximum_channels
;
504 u_int16_t firmware_revision
;
505 u_int8_t host_name
[64];
506 u_int8_t vendor_string
[64];
509 #define PF_PPTP_CTRL_TYPE_START_RPY 2
510 struct pf_pptp_ctrl_start_rpy
{
511 u_int16_t protocol_version
;
512 u_int8_t result_code
;
514 u_int32_t framing_capabilities
;
515 u_int32_t bearer_capabilities
;
516 u_int16_t maximum_channels
;
517 u_int16_t firmware_revision
;
518 u_int8_t host_name
[64];
519 u_int8_t vendor_string
[64];
522 #define PF_PPTP_CTRL_TYPE_STOP_REQ 3
523 struct pf_pptp_ctrl_stop_req
{
526 u_int16_t reserved_2
;
529 #define PF_PPTP_CTRL_TYPE_STOP_RPY 4
530 struct pf_pptp_ctrl_stop_rpy
{
533 u_int16_t reserved_1
;
536 #define PF_PPTP_CTRL_TYPE_ECHO_REQ 5
537 struct pf_pptp_ctrl_echo_req
{
538 u_int32_t identifier
;
541 #define PF_PPTP_CTRL_TYPE_ECHO_RPY 6
542 struct pf_pptp_ctrl_echo_rpy
{
543 u_int32_t identifier
;
544 u_int8_t result_code
;
546 u_int16_t reserved_1
;
549 #define PF_PPTP_CTRL_TYPE_CALL_OUT_REQ 7
550 struct pf_pptp_ctrl_call_out_req
{
552 u_int16_t call_sernum
;
554 u_int32_t bearer_type
;
555 u_int32_t framing_type
;
556 u_int16_t rxwindow_size
;
557 u_int16_t proc_delay
;
558 u_int8_t phone_num
[64];
559 u_int8_t sub_addr
[64];
562 #define PF_PPTP_CTRL_TYPE_CALL_OUT_RPY 8
563 struct pf_pptp_ctrl_call_out_rpy
{
565 u_int16_t peer_call_id
;
566 u_int8_t result_code
;
568 u_int16_t cause_code
;
569 u_int32_t connect_speed
;
570 u_int16_t rxwindow_size
;
571 u_int16_t proc_delay
;
572 u_int32_t phy_channel_id
;
575 #define PF_PPTP_CTRL_TYPE_CALL_IN_1ST 9
576 struct pf_pptp_ctrl_call_in_1st
{
578 u_int16_t call_sernum
;
579 u_int32_t bearer_type
;
580 u_int32_t phy_channel_id
;
581 u_int16_t dialed_number_len
;
582 u_int16_t dialing_number_len
;
583 u_int8_t dialed_num
[64];
584 u_int8_t dialing_num
[64];
585 u_int8_t sub_addr
[64];
588 #define PF_PPTP_CTRL_TYPE_CALL_IN_2ND 10
589 struct pf_pptp_ctrl_call_in_2nd
{
591 u_int16_t peer_call_id
;
592 u_int8_t result_code
;
594 u_int16_t rxwindow_size
;
596 u_int16_t reserved_1
;
599 #define PF_PPTP_CTRL_TYPE_CALL_IN_3RD 11
600 struct pf_pptp_ctrl_call_in_3rd
{
602 u_int16_t reserved_1
;
603 u_int32_t connect_speed
;
604 u_int16_t rxwindow_size
;
606 u_int32_t framing_type
;
609 #define PF_PPTP_CTRL_TYPE_CALL_CLR 12
610 struct pf_pptp_ctrl_call_clr
{
612 u_int16_t reserved_1
;
615 #define PF_PPTP_CTRL_TYPE_CALL_DISC 13
616 struct pf_pptp_ctrl_call_disc
{
618 u_int8_t result_code
;
620 u_int16_t cause_code
;
621 u_int16_t reserved_1
;
622 u_int8_t statistics
[128];
625 #define PF_PPTP_CTRL_TYPE_ERROR 14
626 struct pf_pptp_ctrl_error
{
627 u_int16_t peer_call_id
;
628 u_int16_t reserved_1
;
629 u_int32_t crc_errors
;
632 u_int32_t buf_errors
;
633 u_int32_t tim_errors
;
634 u_int32_t align_errors
;
637 #define PF_PPTP_CTRL_TYPE_SET_LINKINFO 15
638 struct pf_pptp_ctrl_set_linkinfo
{
639 u_int16_t peer_call_id
;
640 u_int16_t reserved_1
;
646 static const char *pf_pptp_ctrl_type_name(u_int16_t code
)
650 if (code
< PF_PPTP_CTRL_TYPE_START_REQ
||
651 code
> PF_PPTP_CTRL_TYPE_SET_LINKINFO
) {
652 static char reserved
[] = "reserved-00";
654 sprintf(&reserved
[9], "%02x", code
);
657 static const char *name
[] = {
658 "start_req", "start_rpy", "stop_req", "stop_rpy",
659 "echo_req", "echo_rpy", "call_out_req", "call_out_rpy",
660 "call_in_1st", "call_in_2nd", "call_in_3rd",
661 "call_clr", "call_disc", "error", "set_linkinfo"
664 return (name
[code
- 1]);
669 static const size_t PF_PPTP_CTRL_MSG_MINSIZE
=
670 sizeof (struct pf_pptp_hdr
) +
671 sizeof (struct pf_pptp_ctrl_hdr
) +
672 MIN(sizeof (struct pf_pptp_ctrl_start_req
),
673 MIN(sizeof (struct pf_pptp_ctrl_start_rpy
),
674 MIN(sizeof (struct pf_pptp_ctrl_stop_req
),
675 MIN(sizeof (struct pf_pptp_ctrl_stop_rpy
),
676 MIN(sizeof (struct pf_pptp_ctrl_echo_req
),
677 MIN(sizeof (struct pf_pptp_ctrl_echo_rpy
),
678 MIN(sizeof (struct pf_pptp_ctrl_call_out_req
),
679 MIN(sizeof (struct pf_pptp_ctrl_call_out_rpy
),
680 MIN(sizeof (struct pf_pptp_ctrl_call_in_1st
),
681 MIN(sizeof (struct pf_pptp_ctrl_call_in_2nd
),
682 MIN(sizeof (struct pf_pptp_ctrl_call_in_3rd
),
683 MIN(sizeof (struct pf_pptp_ctrl_call_clr
),
684 MIN(sizeof (struct pf_pptp_ctrl_call_disc
),
685 MIN(sizeof (struct pf_pptp_ctrl_error
),
686 sizeof (struct pf_pptp_ctrl_set_linkinfo
)
689 union pf_pptp_ctrl_msg_union
{
690 struct pf_pptp_ctrl_start_req start_req
;
691 struct pf_pptp_ctrl_start_rpy start_rpy
;
692 struct pf_pptp_ctrl_stop_req stop_req
;
693 struct pf_pptp_ctrl_stop_rpy stop_rpy
;
694 struct pf_pptp_ctrl_echo_req echo_req
;
695 struct pf_pptp_ctrl_echo_rpy echo_rpy
;
696 struct pf_pptp_ctrl_call_out_req call_out_req
;
697 struct pf_pptp_ctrl_call_out_rpy call_out_rpy
;
698 struct pf_pptp_ctrl_call_in_1st call_in_1st
;
699 struct pf_pptp_ctrl_call_in_2nd call_in_2nd
;
700 struct pf_pptp_ctrl_call_in_3rd call_in_3rd
;
701 struct pf_pptp_ctrl_call_clr call_clr
;
702 struct pf_pptp_ctrl_call_disc call_disc
;
703 struct pf_pptp_ctrl_error error
;
704 struct pf_pptp_ctrl_set_linkinfo set_linkinfo
;
708 struct pf_pptp_ctrl_msg
{
709 struct pf_pptp_hdr hdr
;
710 struct pf_pptp_ctrl_hdr ctrl
;
711 union pf_pptp_ctrl_msg_union msg
;
714 #define PF_GRE_FLAG_CHECKSUM_PRESENT 0x8000
715 #define PF_GRE_FLAG_VERSION_MASK 0x0007
716 #define PF_GRE_PPP_ETHERTYPE 0x880B
718 struct pf_grev1_hdr
{
720 u_int16_t protocol_type
;
721 u_int16_t payload_length
;
729 static const u_int16_t PF_IKE_PORT
= 500;
732 u_int64_t initiator_cookie
, responder_cookie
;
733 u_int8_t next_payload
, version
, exchange_type
, flags
;
734 u_int32_t message_id
, length
;
737 #define PF_IKE_PACKET_MINSIZE (sizeof (struct pf_ike_hdr))
739 #define PF_IKEv1_EXCHTYPE_BASE 1
740 #define PF_IKEv1_EXCHTYPE_ID_PROTECT 2
741 #define PF_IKEv1_EXCHTYPE_AUTH_ONLY 3
742 #define PF_IKEv1_EXCHTYPE_AGGRESSIVE 4
743 #define PF_IKEv1_EXCHTYPE_INFORMATIONAL 5
744 #define PF_IKEv2_EXCHTYPE_SA_INIT 34
745 #define PF_IKEv2_EXCHTYPE_AUTH 35
746 #define PF_IKEv2_EXCHTYPE_CREATE_CHILD_SA 36
747 #define PF_IKEv2_EXCHTYPE_INFORMATIONAL 37
749 #define PF_IKEv1_FLAG_E 0x01
750 #define PF_IKEv1_FLAG_C 0x02
751 #define PF_IKEv1_FLAG_A 0x04
752 #define PF_IKEv2_FLAG_I 0x08
753 #define PF_IKEv2_FLAG_V 0x10
754 #define PF_IKEv2_FLAG_R 0x20
763 pf_src_compare(struct pf_src_node
*a
, struct pf_src_node
*b
)
767 if (a
->rule
.ptr
> b
->rule
.ptr
)
769 if (a
->rule
.ptr
< b
->rule
.ptr
)
771 if ((diff
= a
->af
- b
->af
) != 0)
776 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
778 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
784 if (a
->addr
.addr32
[3] > b
->addr
.addr32
[3])
786 if (a
->addr
.addr32
[3] < b
->addr
.addr32
[3])
788 if (a
->addr
.addr32
[2] > b
->addr
.addr32
[2])
790 if (a
->addr
.addr32
[2] < b
->addr
.addr32
[2])
792 if (a
->addr
.addr32
[1] > b
->addr
.addr32
[1])
794 if (a
->addr
.addr32
[1] < b
->addr
.addr32
[1])
796 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
798 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
807 pf_state_compare_lan_ext(struct pf_state_key
*a
, struct pf_state_key
*b
)
812 if ((diff
= a
->proto
- b
->proto
) != 0)
814 if ((diff
= a
->af
- b
->af
) != 0)
817 extfilter
= PF_EXTFILTER_APD
;
822 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
827 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
829 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
834 if ((diff
= a
->proto_variant
- b
->proto_variant
))
836 extfilter
= a
->proto_variant
;
837 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
839 if ((extfilter
< PF_EXTFILTER_AD
) &&
840 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
845 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
846 a
->proto_variant
== b
->proto_variant
) {
847 if (!!(diff
= a
->ext
.xport
.call_id
-
848 b
->ext
.xport
.call_id
))
854 if (!!(diff
= a
->ext
.xport
.spi
- b
->ext
.xport
.spi
))
865 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
867 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
869 if (extfilter
< PF_EXTFILTER_EI
) {
870 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
872 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
879 if (a
->lan
.addr
.addr32
[3] > b
->lan
.addr
.addr32
[3])
881 if (a
->lan
.addr
.addr32
[3] < b
->lan
.addr
.addr32
[3])
883 if (a
->lan
.addr
.addr32
[2] > b
->lan
.addr
.addr32
[2])
885 if (a
->lan
.addr
.addr32
[2] < b
->lan
.addr
.addr32
[2])
887 if (a
->lan
.addr
.addr32
[1] > b
->lan
.addr
.addr32
[1])
889 if (a
->lan
.addr
.addr32
[1] < b
->lan
.addr
.addr32
[1])
891 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
893 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
895 if (extfilter
< PF_EXTFILTER_EI
||
896 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
897 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
899 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
901 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
903 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
905 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
907 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
909 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
911 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
918 if (a
->app_state
&& b
->app_state
) {
919 if (a
->app_state
->compare_lan_ext
&&
920 b
->app_state
->compare_lan_ext
) {
921 diff
= (const char *)b
->app_state
->compare_lan_ext
-
922 (const char *)a
->app_state
->compare_lan_ext
;
925 diff
= a
->app_state
->compare_lan_ext(a
->app_state
,
936 pf_state_compare_ext_gwy(struct pf_state_key
*a
, struct pf_state_key
*b
)
941 if ((diff
= a
->proto
- b
->proto
) != 0)
944 if ((diff
= a
->af
- b
->af
) != 0)
947 extfilter
= PF_EXTFILTER_APD
;
952 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
957 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
959 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
964 if ((diff
= a
->proto_variant
- b
->proto_variant
))
966 extfilter
= a
->proto_variant
;
967 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
969 if ((extfilter
< PF_EXTFILTER_AD
) &&
970 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
975 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
976 a
->proto_variant
== b
->proto_variant
) {
977 if (!!(diff
= a
->gwy
.xport
.call_id
-
978 b
->gwy
.xport
.call_id
))
984 if (!!(diff
= a
->gwy
.xport
.spi
- b
->gwy
.xport
.spi
))
995 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
997 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
999 if (extfilter
< PF_EXTFILTER_EI
) {
1000 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1002 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1009 if (a
->gwy
.addr
.addr32
[3] > b
->gwy
.addr
.addr32
[3])
1011 if (a
->gwy
.addr
.addr32
[3] < b
->gwy
.addr
.addr32
[3])
1013 if (a
->gwy
.addr
.addr32
[2] > b
->gwy
.addr
.addr32
[2])
1015 if (a
->gwy
.addr
.addr32
[2] < b
->gwy
.addr
.addr32
[2])
1017 if (a
->gwy
.addr
.addr32
[1] > b
->gwy
.addr
.addr32
[1])
1019 if (a
->gwy
.addr
.addr32
[1] < b
->gwy
.addr
.addr32
[1])
1021 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1023 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1025 if (extfilter
< PF_EXTFILTER_EI
||
1026 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
1027 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
1029 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
1031 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
1033 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
1035 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
1037 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
1039 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1041 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1048 if (a
->app_state
&& b
->app_state
) {
1049 if (a
->app_state
->compare_ext_gwy
&&
1050 b
->app_state
->compare_ext_gwy
) {
1051 diff
= (const char *)b
->app_state
->compare_ext_gwy
-
1052 (const char *)a
->app_state
->compare_ext_gwy
;
1055 diff
= a
->app_state
->compare_ext_gwy(a
->app_state
,
1066 pf_state_compare_id(struct pf_state
*a
, struct pf_state
*b
)
1072 if (a
->creatorid
> b
->creatorid
)
1074 if (a
->creatorid
< b
->creatorid
)
1082 pf_addrcpy(struct pf_addr
*dst
, struct pf_addr
*src
, sa_family_t af
)
1087 dst
->addr32
[0] = src
->addr32
[0];
1091 dst
->addr32
[0] = src
->addr32
[0];
1092 dst
->addr32
[1] = src
->addr32
[1];
1093 dst
->addr32
[2] = src
->addr32
[2];
1094 dst
->addr32
[3] = src
->addr32
[3];
1101 pf_find_state_byid(struct pf_state_cmp
*key
)
1103 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1105 return (RB_FIND(pf_state_tree_id
, &tree_id
,
1106 (struct pf_state
*)(void *)key
));
1109 static struct pf_state
*
1110 pf_find_state(struct pfi_kif
*kif
, struct pf_state_key_cmp
*key
, u_int dir
)
1112 struct pf_state_key
*sk
= NULL
;
1115 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1119 sk
= RB_FIND(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1120 (struct pf_state_key
*)key
);
1123 sk
= RB_FIND(pf_state_tree_ext_gwy
, &pf_statetbl_ext_gwy
,
1124 (struct pf_state_key
*)key
);
1127 panic("pf_find_state");
1130 /* list is sorted, if-bound states before floating ones */
1132 TAILQ_FOREACH(s
, &sk
->states
, next
)
1133 if (s
->kif
== pfi_all
|| s
->kif
== kif
)
1140 pf_find_state_all(struct pf_state_key_cmp
*key
, u_int dir
, int *more
)
1142 struct pf_state_key
*sk
= NULL
;
1143 struct pf_state
*s
, *ret
= NULL
;
1145 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1149 sk
= RB_FIND(pf_state_tree_lan_ext
,
1150 &pf_statetbl_lan_ext
, (struct pf_state_key
*)key
);
1153 sk
= RB_FIND(pf_state_tree_ext_gwy
,
1154 &pf_statetbl_ext_gwy
, (struct pf_state_key
*)key
);
1157 panic("pf_find_state_all");
1161 ret
= TAILQ_FIRST(&sk
->states
);
1165 TAILQ_FOREACH(s
, &sk
->states
, next
)
1173 pf_init_threshold(struct pf_threshold
*threshold
,
1174 u_int32_t limit
, u_int32_t seconds
)
1176 threshold
->limit
= limit
* PF_THRESHOLD_MULT
;
1177 threshold
->seconds
= seconds
;
1178 threshold
->count
= 0;
1179 threshold
->last
= pf_time_second();
1183 pf_add_threshold(struct pf_threshold
*threshold
)
1185 u_int32_t t
= pf_time_second(), diff
= t
- threshold
->last
;
1187 if (diff
>= threshold
->seconds
)
1188 threshold
->count
= 0;
1190 threshold
->count
-= threshold
->count
* diff
/
1192 threshold
->count
+= PF_THRESHOLD_MULT
;
1193 threshold
->last
= t
;
1197 pf_check_threshold(struct pf_threshold
*threshold
)
1199 return (threshold
->count
> threshold
->limit
);
1203 pf_src_connlimit(struct pf_state
**state
)
1207 (*state
)->src_node
->conn
++;
1208 VERIFY((*state
)->src_node
->conn
!= 0);
1209 (*state
)->src
.tcp_est
= 1;
1210 pf_add_threshold(&(*state
)->src_node
->conn_rate
);
1212 if ((*state
)->rule
.ptr
->max_src_conn
&&
1213 (*state
)->rule
.ptr
->max_src_conn
<
1214 (*state
)->src_node
->conn
) {
1215 pf_status
.lcounters
[LCNT_SRCCONN
]++;
1219 if ((*state
)->rule
.ptr
->max_src_conn_rate
.limit
&&
1220 pf_check_threshold(&(*state
)->src_node
->conn_rate
)) {
1221 pf_status
.lcounters
[LCNT_SRCCONNRATE
]++;
1228 if ((*state
)->rule
.ptr
->overload_tbl
) {
1230 u_int32_t killed
= 0;
1232 pf_status
.lcounters
[LCNT_OVERLOAD_TABLE
]++;
1233 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1234 printf("pf_src_connlimit: blocking address ");
1235 pf_print_host(&(*state
)->src_node
->addr
, 0,
1236 (*state
)->state_key
->af
);
1239 bzero(&p
, sizeof (p
));
1240 p
.pfra_af
= (*state
)->state_key
->af
;
1241 switch ((*state
)->state_key
->af
) {
1245 p
.pfra_ip4addr
= (*state
)->src_node
->addr
.v4
;
1251 p
.pfra_ip6addr
= (*state
)->src_node
->addr
.v6
;
1256 pfr_insert_kentry((*state
)->rule
.ptr
->overload_tbl
,
1257 &p
, pf_calendar_time_second());
1259 /* kill existing states if that's required. */
1260 if ((*state
)->rule
.ptr
->flush
) {
1261 struct pf_state_key
*sk
;
1262 struct pf_state
*st
;
1264 pf_status
.lcounters
[LCNT_OVERLOAD_FLUSH
]++;
1265 RB_FOREACH(st
, pf_state_tree_id
, &tree_id
) {
1268 * Kill states from this source. (Only those
1269 * from the same rule if PF_FLUSH_GLOBAL is not
1273 (*state
)->state_key
->af
&&
1274 (((*state
)->state_key
->direction
==
1276 PF_AEQ(&(*state
)->src_node
->addr
,
1277 &sk
->lan
.addr
, sk
->af
)) ||
1278 ((*state
)->state_key
->direction
== PF_IN
&&
1279 PF_AEQ(&(*state
)->src_node
->addr
,
1280 &sk
->ext
.addr
, sk
->af
))) &&
1281 ((*state
)->rule
.ptr
->flush
&
1283 (*state
)->rule
.ptr
== st
->rule
.ptr
)) {
1284 st
->timeout
= PFTM_PURGE
;
1285 st
->src
.state
= st
->dst
.state
=
1290 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1291 printf(", %u states killed", killed
);
1293 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1297 /* kill this state */
1298 (*state
)->timeout
= PFTM_PURGE
;
1299 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
1304 pf_insert_src_node(struct pf_src_node
**sn
, struct pf_rule
*rule
,
1305 struct pf_addr
*src
, sa_family_t af
)
1307 struct pf_src_node k
;
1311 PF_ACPY(&k
.addr
, src
, af
);
1312 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1313 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1317 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
1318 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
1321 if (!rule
->max_src_nodes
||
1322 rule
->src_nodes
< rule
->max_src_nodes
)
1323 (*sn
) = pool_get(&pf_src_tree_pl
, PR_WAITOK
);
1325 pf_status
.lcounters
[LCNT_SRCNODES
]++;
1328 bzero(*sn
, sizeof (struct pf_src_node
));
1330 pf_init_threshold(&(*sn
)->conn_rate
,
1331 rule
->max_src_conn_rate
.limit
,
1332 rule
->max_src_conn_rate
.seconds
);
1335 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1336 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1337 (*sn
)->rule
.ptr
= rule
;
1339 (*sn
)->rule
.ptr
= NULL
;
1340 PF_ACPY(&(*sn
)->addr
, src
, af
);
1341 if (RB_INSERT(pf_src_tree
,
1342 &tree_src_tracking
, *sn
) != NULL
) {
1343 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1344 printf("pf: src_tree insert failed: ");
1345 pf_print_host(&(*sn
)->addr
, 0, af
);
1348 pool_put(&pf_src_tree_pl
, *sn
);
1351 (*sn
)->creation
= pf_time_second();
1352 (*sn
)->ruletype
= rule
->action
;
1353 if ((*sn
)->rule
.ptr
!= NULL
)
1354 (*sn
)->rule
.ptr
->src_nodes
++;
1355 pf_status
.scounters
[SCNT_SRC_NODE_INSERT
]++;
1356 pf_status
.src_nodes
++;
1358 if (rule
->max_src_states
&&
1359 (*sn
)->states
>= rule
->max_src_states
) {
1360 pf_status
.lcounters
[LCNT_SRCSTATES
]++;
1368 pf_stateins_err(const char *tree
, struct pf_state
*s
, struct pfi_kif
*kif
)
1370 struct pf_state_key
*sk
= s
->state_key
;
1372 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1373 printf("pf: state insert failed: %s %s ", tree
, kif
->pfik_name
);
1374 switch (sk
->proto
) {
1384 case IPPROTO_ICMPV6
:
1388 printf("PROTO=%u", sk
->proto
);
1392 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
,
1395 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
,
1398 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
,
1400 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1401 printf(" (from sync)");
1407 pf_insert_state(struct pfi_kif
*kif
, struct pf_state
*s
)
1409 struct pf_state_key
*cur
;
1410 struct pf_state
*sp
;
1412 VERIFY(s
->state_key
!= NULL
);
1415 if ((cur
= RB_INSERT(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1416 s
->state_key
)) != NULL
) {
1417 /* key exists. check for same kif, if none, add to key */
1418 TAILQ_FOREACH(sp
, &cur
->states
, next
)
1419 if (sp
->kif
== kif
) { /* collision! */
1420 pf_stateins_err("tree_lan_ext", s
, kif
);
1422 PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1425 pf_detach_state(s
, PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1426 pf_attach_state(cur
, s
, kif
== pfi_all
? 1 : 0);
1429 /* if cur != NULL, we already found a state key and attached to it */
1430 if (cur
== NULL
&& (cur
= RB_INSERT(pf_state_tree_ext_gwy
,
1431 &pf_statetbl_ext_gwy
, s
->state_key
)) != NULL
) {
1432 /* must not happen. we must have found the sk above! */
1433 pf_stateins_err("tree_ext_gwy", s
, kif
);
1434 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
1438 if (s
->id
== 0 && s
->creatorid
== 0) {
1439 s
->id
= htobe64(pf_status
.stateid
++);
1440 s
->creatorid
= pf_status
.hostid
;
1442 if (RB_INSERT(pf_state_tree_id
, &tree_id
, s
) != NULL
) {
1443 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1444 printf("pf: state insert failed: "
1445 "id: %016llx creatorid: %08x",
1446 be64toh(s
->id
), ntohl(s
->creatorid
));
1447 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1448 printf(" (from sync)");
1451 pf_detach_state(s
, 0);
1454 TAILQ_INSERT_TAIL(&state_list
, s
, entry_list
);
1455 pf_status
.fcounters
[FCNT_STATE_INSERT
]++;
1457 VERIFY(pf_status
.states
!= 0);
1458 pfi_kif_ref(kif
, PFI_KIF_REF_STATE
);
1460 pfsync_insert_state(s
);
1466 pf_purge_thread_cont(int err
)
1469 static u_int32_t nloops
= 0;
1470 int t
= 1; /* 1 second */
1472 lck_rw_lock_shared(pf_perim_lock
);
1473 lck_mtx_lock(pf_lock
);
1475 /* purge everything if not running */
1476 if (!pf_status
.running
) {
1477 pf_purge_expired_states(pf_status
.states
);
1478 pf_purge_expired_fragments();
1479 pf_purge_expired_src_nodes();
1481 /* terminate thread (we don't currently do this) */
1482 if (pf_purge_thread
== NULL
) {
1483 lck_mtx_unlock(pf_lock
);
1484 lck_rw_done(pf_perim_lock
);
1486 thread_deallocate(current_thread());
1487 thread_terminate(current_thread());
1491 /* if there's nothing left, sleep w/o timeout */
1492 if (pf_status
.states
== 0 &&
1493 pf_normalize_isempty() &&
1494 RB_EMPTY(&tree_src_tracking
)) {
1502 /* process a fraction of the state table every second */
1503 pf_purge_expired_states(1 + (pf_status
.states
1504 / pf_default_rule
.timeout
[PFTM_INTERVAL
]));
1506 /* purge other expired types every PFTM_INTERVAL seconds */
1507 if (++nloops
>= pf_default_rule
.timeout
[PFTM_INTERVAL
]) {
1508 pf_purge_expired_fragments();
1509 pf_purge_expired_src_nodes();
1513 lck_mtx_unlock(pf_lock
);
1514 lck_rw_done(pf_perim_lock
);
1516 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge_cont",
1517 t
* hz
, pf_purge_thread_cont
);
1525 pf_purge_thread_fn(void *v
, wait_result_t w
)
1527 #pragma unused(v, w)
1528 (void) tsleep0(pf_purge_thread_fn
, PWAIT
, "pf_purge", 0,
1529 pf_purge_thread_cont
);
1531 * tsleep0() shouldn't have returned as PCATCH was not set;
1532 * therefore assert in this case.
1538 pf_state_expires(const struct pf_state
*state
)
1545 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1547 /* handle all PFTM_* > PFTM_MAX here */
1548 if (state
->timeout
== PFTM_PURGE
)
1549 return (pf_time_second());
1550 if (state
->timeout
== PFTM_UNTIL_PACKET
)
1552 VERIFY(state
->timeout
!= PFTM_UNLINKED
);
1553 VERIFY(state
->timeout
< PFTM_MAX
);
1554 t
= state
->rule
.ptr
->timeout
[state
->timeout
];
1556 t
= pf_default_rule
.timeout
[state
->timeout
];
1557 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
1559 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
1560 states
= state
->rule
.ptr
->states
;
1562 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
1563 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
1564 states
= pf_status
.states
;
1566 if (end
&& states
> start
&& start
< end
) {
1568 return (state
->expire
+ t
* (end
- states
) /
1571 return (pf_time_second());
1573 return (state
->expire
+ t
);
1577 pf_purge_expired_src_nodes(void)
1579 struct pf_src_node
*cur
, *next
;
1581 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1583 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
1584 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
1586 if (cur
->states
<= 0 && cur
->expire
<= pf_time_second()) {
1587 if (cur
->rule
.ptr
!= NULL
) {
1588 cur
->rule
.ptr
->src_nodes
--;
1589 if (cur
->rule
.ptr
->states
<= 0 &&
1590 cur
->rule
.ptr
->max_src_nodes
<= 0)
1591 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1593 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1594 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1595 pf_status
.src_nodes
--;
1596 pool_put(&pf_src_tree_pl
, cur
);
1602 pf_src_tree_remove_state(struct pf_state
*s
)
1606 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1608 if (s
->src_node
!= NULL
) {
1609 if (s
->src
.tcp_est
) {
1610 VERIFY(s
->src_node
->conn
> 0);
1611 --s
->src_node
->conn
;
1613 VERIFY(s
->src_node
->states
> 0);
1614 if (--s
->src_node
->states
<= 0) {
1615 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1617 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1618 s
->src_node
->expire
= pf_time_second() + t
;
1621 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1622 VERIFY(s
->nat_src_node
->states
> 0);
1623 if (--s
->nat_src_node
->states
<= 0) {
1624 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1626 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1627 s
->nat_src_node
->expire
= pf_time_second() + t
;
1630 s
->src_node
= s
->nat_src_node
= NULL
;
1634 pf_unlink_state(struct pf_state
*cur
)
1636 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1638 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1639 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1640 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1641 cur
->state_key
->ext
.xport
.port
,
1642 cur
->state_key
->lan
.xport
.port
,
1643 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1644 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1647 hook_runloop(&cur
->unlink_hooks
, HOOK_REMOVE
|HOOK_FREE
);
1648 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1650 if (cur
->creatorid
== pf_status
.hostid
)
1651 pfsync_delete_state(cur
);
1653 cur
->timeout
= PFTM_UNLINKED
;
1654 pf_src_tree_remove_state(cur
);
1655 pf_detach_state(cur
, 0);
1658 /* callers should be at splpf and hold the
1659 * write_lock on pf_consistency_lock */
1661 pf_free_state(struct pf_state
*cur
)
1663 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1665 if (pfsyncif
!= NULL
&&
1666 (pfsyncif
->sc_bulk_send_next
== cur
||
1667 pfsyncif
->sc_bulk_terminator
== cur
))
1670 VERIFY(cur
->timeout
== PFTM_UNLINKED
);
1671 VERIFY(cur
->rule
.ptr
->states
> 0);
1672 if (--cur
->rule
.ptr
->states
<= 0 &&
1673 cur
->rule
.ptr
->src_nodes
<= 0)
1674 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1675 if (cur
->nat_rule
.ptr
!= NULL
) {
1676 VERIFY(cur
->nat_rule
.ptr
->states
> 0);
1677 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1678 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1679 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1681 if (cur
->anchor
.ptr
!= NULL
) {
1682 VERIFY(cur
->anchor
.ptr
->states
> 0);
1683 if (--cur
->anchor
.ptr
->states
<= 0)
1684 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1686 pf_normalize_tcp_cleanup(cur
);
1687 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1688 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1690 pf_tag_unref(cur
->tag
);
1691 pool_put(&pf_state_pl
, cur
);
1692 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1693 VERIFY(pf_status
.states
> 0);
1698 pf_purge_expired_states(u_int32_t maxcheck
)
1700 static struct pf_state
*cur
= NULL
;
1701 struct pf_state
*next
;
1703 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1705 while (maxcheck
--) {
1706 /* wrap to start of list when we hit the end */
1708 cur
= TAILQ_FIRST(&state_list
);
1710 break; /* list empty */
1713 /* get next state, as cur may get deleted */
1714 next
= TAILQ_NEXT(cur
, entry_list
);
1716 if (cur
->timeout
== PFTM_UNLINKED
) {
1718 } else if (pf_state_expires(cur
) <= pf_time_second()) {
1719 /* unlink and free expired state */
1720 pf_unlink_state(cur
);
1728 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1730 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1732 if (aw
->type
!= PF_ADDR_TABLE
)
1734 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1740 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1742 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1744 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1746 pfr_detach_table(aw
->p
.tbl
);
1751 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1753 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1755 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1757 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1759 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1760 kt
= kt
->pfrkt_root
;
1762 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1767 pf_print_addr(struct pf_addr
*addr
, sa_family_t af
)
1772 u_int32_t a
= ntohl(addr
->addr32
[0]);
1773 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1781 u_int8_t i
, curstart
= 255, curend
= 0,
1782 maxstart
= 0, maxend
= 0;
1783 for (i
= 0; i
< 8; i
++) {
1784 if (!addr
->addr16
[i
]) {
1785 if (curstart
== 255)
1791 if ((curend
- curstart
) >
1792 (maxend
- maxstart
)) {
1793 maxstart
= curstart
;
1800 for (i
= 0; i
< 8; i
++) {
1801 if (i
>= maxstart
&& i
<= maxend
) {
1810 b
= ntohs(addr
->addr16
[i
]);
1823 pf_print_sk_host(struct pf_state_host
*sh
, sa_family_t af
, int proto
,
1824 u_int8_t proto_variant
)
1826 pf_print_addr(&sh
->addr
, af
);
1831 printf("[%08x]", ntohl(sh
->xport
.spi
));
1835 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
1836 printf("[%u]", ntohs(sh
->xport
.call_id
));
1841 printf("[%u]", ntohs(sh
->xport
.port
));
1850 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1852 pf_print_addr(addr
, af
);
1854 printf("[%u]", ntohs(p
));
1858 pf_print_state(struct pf_state
*s
)
1860 struct pf_state_key
*sk
= s
->state_key
;
1861 switch (sk
->proto
) {
1866 printf("GRE%u ", sk
->proto_variant
);
1877 case IPPROTO_ICMPV6
:
1881 printf("%u ", sk
->proto
);
1884 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1886 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1888 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
, sk
->proto_variant
);
1889 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
1890 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
1891 if (s
->src
.wscale
&& s
->dst
.wscale
)
1892 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
1894 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
1895 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
1896 if (s
->src
.wscale
&& s
->dst
.wscale
)
1897 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
1899 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
1903 pf_print_flags(u_int8_t f
)
1925 #define PF_SET_SKIP_STEPS(i) \
1927 while (head[i] != cur) { \
1928 head[i]->skip[i].ptr = cur; \
1929 head[i] = TAILQ_NEXT(head[i], entries); \
1934 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
1936 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
1939 cur
= TAILQ_FIRST(rules
);
1941 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
1943 while (cur
!= NULL
) {
1945 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
1946 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
1947 if (cur
->direction
!= prev
->direction
)
1948 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
1949 if (cur
->af
!= prev
->af
)
1950 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
1951 if (cur
->proto
!= prev
->proto
)
1952 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
1953 if (cur
->src
.neg
!= prev
->src
.neg
||
1954 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
1955 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
1957 union pf_rule_xport
*cx
= &cur
->src
.xport
;
1958 union pf_rule_xport
*px
= &prev
->src
.xport
;
1960 switch (cur
->proto
) {
1963 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1966 if (prev
->proto
== IPPROTO_GRE
||
1967 prev
->proto
== IPPROTO_ESP
||
1968 cx
->range
.op
!= px
->range
.op
||
1969 cx
->range
.port
[0] != px
->range
.port
[0] ||
1970 cx
->range
.port
[1] != px
->range
.port
[1])
1971 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
1975 if (cur
->dst
.neg
!= prev
->dst
.neg
||
1976 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
1977 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
1979 union pf_rule_xport
*cx
= &cur
->dst
.xport
;
1980 union pf_rule_xport
*px
= &prev
->dst
.xport
;
1982 switch (cur
->proto
) {
1984 if (cur
->proto
!= prev
->proto
||
1985 cx
->call_id
!= px
->call_id
)
1986 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1989 if (cur
->proto
!= prev
->proto
||
1991 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
1994 if (prev
->proto
== IPPROTO_GRE
||
1995 prev
->proto
== IPPROTO_ESP
||
1996 cx
->range
.op
!= px
->range
.op
||
1997 cx
->range
.port
[0] != px
->range
.port
[0] ||
1998 cx
->range
.port
[1] != px
->range
.port
[1])
1999 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2005 cur
= TAILQ_NEXT(cur
, entries
);
2007 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2008 PF_SET_SKIP_STEPS(i
);
2012 pf_calc_state_key_flowhash(struct pf_state_key
*sk
)
2014 struct pf_flowhash_key fh
__attribute__((aligned(8)));
2016 bzero(&fh
, sizeof (fh
));
2017 if (PF_ALEQ(&sk
->lan
.addr
, &sk
->ext
.addr
, sk
->af
)) {
2018 bcopy(&sk
->lan
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2019 bcopy(&sk
->ext
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2021 bcopy(&sk
->ext
.addr
, &fh
.ap1
.addr
, sizeof (fh
.ap1
.addr
));
2022 bcopy(&sk
->lan
.addr
, &fh
.ap2
.addr
, sizeof (fh
.ap2
.addr
));
2024 if (sk
->lan
.xport
.spi
<= sk
->ext
.xport
.spi
) {
2025 fh
.ap1
.xport
.spi
= sk
->lan
.xport
.spi
;
2026 fh
.ap2
.xport
.spi
= sk
->ext
.xport
.spi
;
2028 fh
.ap1
.xport
.spi
= sk
->ext
.xport
.spi
;
2029 fh
.ap2
.xport
.spi
= sk
->lan
.xport
.spi
;
2032 fh
.proto
= sk
->proto
;
2034 return (net_flowhash(&fh
, sizeof (fh
), pf_hash_seed
));
2038 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
2040 if (aw1
->type
!= aw2
->type
)
2042 switch (aw1
->type
) {
2043 case PF_ADDR_ADDRMASK
:
2045 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
2047 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
2050 case PF_ADDR_DYNIFTL
:
2051 return (aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
2052 case PF_ADDR_NOROUTE
:
2053 case PF_ADDR_URPFFAILED
:
2056 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
2057 case PF_ADDR_RTLABEL
:
2058 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
2060 printf("invalid address type: %d\n", aw1
->type
);
2066 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
2072 l
= cksum
+ old
- new;
2073 l
= (l
>> 16) + (l
& 0xffff);
2081 pf_change_ap(int dir
, struct mbuf
*m
, struct pf_addr
*a
, u_int16_t
*p
,
2082 u_int16_t
*ic
, u_int16_t
*pc
, struct pf_addr
*an
, u_int16_t pn
,
2083 u_int8_t u
, sa_family_t af
)
2088 PF_ACPY(&ao
, a
, af
);
2096 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2097 ao
.addr16
[0], an
->addr16
[0], 0),
2098 ao
.addr16
[1], an
->addr16
[1], 0);
2101 * If the packet is originated from an ALG on the NAT gateway
2102 * (source address is loopback or local), in which case the
2103 * TCP/UDP checksum field contains the pseudo header checksum
2104 * that's not yet complemented.
2106 if (dir
== PF_OUT
&& m
!= NULL
&&
2107 (m
->m_flags
& M_PKTHDR
) &&
2108 (m
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))) {
2109 /* Pseudo-header checksum does not include ports */
2110 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2111 ao
.addr16
[0], an
->addr16
[0], u
),
2112 ao
.addr16
[1], an
->addr16
[1], u
);
2114 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2115 ao
.addr16
[0], an
->addr16
[0], u
),
2116 ao
.addr16
[1], an
->addr16
[1], u
),
2124 * If the packet is originated from an ALG on the NAT gateway
2125 * (source address is loopback or local), in which case the
2126 * TCP/UDP checksum field contains the pseudo header checksum
2127 * that's not yet complemented.
2129 if (dir
== PF_OUT
&& m
!= NULL
&&
2130 (m
->m_flags
& M_PKTHDR
) &&
2131 (m
->m_pkthdr
.csum_flags
& (CSUM_TCPIPV6
| CSUM_UDPIPV6
))) {
2132 /* Pseudo-header checksum does not include ports */
2133 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2134 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2135 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2136 ao
.addr16
[0], an
->addr16
[0], u
),
2137 ao
.addr16
[1], an
->addr16
[1], u
),
2138 ao
.addr16
[2], an
->addr16
[2], u
),
2139 ao
.addr16
[3], an
->addr16
[3], u
),
2140 ao
.addr16
[4], an
->addr16
[4], u
),
2141 ao
.addr16
[5], an
->addr16
[5], u
),
2142 ao
.addr16
[6], an
->addr16
[6], u
),
2143 ao
.addr16
[7], an
->addr16
[7], u
),
2146 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2147 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2148 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2149 ao
.addr16
[0], an
->addr16
[0], u
),
2150 ao
.addr16
[1], an
->addr16
[1], u
),
2151 ao
.addr16
[2], an
->addr16
[2], u
),
2152 ao
.addr16
[3], an
->addr16
[3], u
),
2153 ao
.addr16
[4], an
->addr16
[4], u
),
2154 ao
.addr16
[5], an
->addr16
[5], u
),
2155 ao
.addr16
[6], an
->addr16
[6], u
),
2156 ao
.addr16
[7], an
->addr16
[7], u
),
2165 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
2167 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
2171 memcpy(&ao
, a
, sizeof (ao
));
2172 memcpy(a
, &an
, sizeof (u_int32_t
));
2173 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
2174 ao
% 65536, an
% 65536, u
);
2179 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
2183 PF_ACPY(&ao
, a
, AF_INET6
);
2184 PF_ACPY(a
, an
, AF_INET6
);
2186 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2187 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2188 pf_cksum_fixup(pf_cksum_fixup(*c
,
2189 ao
.addr16
[0], an
->addr16
[0], u
),
2190 ao
.addr16
[1], an
->addr16
[1], u
),
2191 ao
.addr16
[2], an
->addr16
[2], u
),
2192 ao
.addr16
[3], an
->addr16
[3], u
),
2193 ao
.addr16
[4], an
->addr16
[4], u
),
2194 ao
.addr16
[5], an
->addr16
[5], u
),
2195 ao
.addr16
[6], an
->addr16
[6], u
),
2196 ao
.addr16
[7], an
->addr16
[7], u
);
2201 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
2202 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
2203 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
2205 struct pf_addr oia
, ooa
;
2207 PF_ACPY(&oia
, ia
, af
);
2208 PF_ACPY(&ooa
, oa
, af
);
2210 /* Change inner protocol port, fix inner protocol checksum. */
2212 u_int16_t oip
= *ip
;
2219 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
2220 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
2222 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
2224 /* Change inner ip address, fix inner ip and icmp checksums. */
2225 PF_ACPY(ia
, na
, af
);
2229 u_int32_t oh2c
= *h2c
;
2231 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
2232 oia
.addr16
[0], ia
->addr16
[0], 0),
2233 oia
.addr16
[1], ia
->addr16
[1], 0);
2234 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2235 oia
.addr16
[0], ia
->addr16
[0], 0),
2236 oia
.addr16
[1], ia
->addr16
[1], 0);
2237 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
2243 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2244 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2245 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2246 oia
.addr16
[0], ia
->addr16
[0], u
),
2247 oia
.addr16
[1], ia
->addr16
[1], u
),
2248 oia
.addr16
[2], ia
->addr16
[2], u
),
2249 oia
.addr16
[3], ia
->addr16
[3], u
),
2250 oia
.addr16
[4], ia
->addr16
[4], u
),
2251 oia
.addr16
[5], ia
->addr16
[5], u
),
2252 oia
.addr16
[6], ia
->addr16
[6], u
),
2253 oia
.addr16
[7], ia
->addr16
[7], u
);
2257 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
2258 PF_ACPY(oa
, na
, af
);
2262 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
2263 ooa
.addr16
[0], oa
->addr16
[0], 0),
2264 ooa
.addr16
[1], oa
->addr16
[1], 0);
2269 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2270 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2271 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2272 ooa
.addr16
[0], oa
->addr16
[0], u
),
2273 ooa
.addr16
[1], oa
->addr16
[1], u
),
2274 ooa
.addr16
[2], oa
->addr16
[2], u
),
2275 ooa
.addr16
[3], oa
->addr16
[3], u
),
2276 ooa
.addr16
[4], oa
->addr16
[4], u
),
2277 ooa
.addr16
[5], oa
->addr16
[5], u
),
2278 ooa
.addr16
[6], oa
->addr16
[6], u
),
2279 ooa
.addr16
[7], oa
->addr16
[7], u
);
2287 * Need to modulate the sequence numbers in the TCP SACK option
2288 * (credits to Krzysztof Pfaff for report and patch)
2291 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
2292 struct tcphdr
*th
, struct pf_state_peer
*dst
)
2294 int hlen
= (th
->th_off
<< 2) - sizeof (*th
), thoptlen
= hlen
;
2295 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
2296 int copyback
= 0, i
, olen
;
2297 struct sackblk sack
;
2299 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2300 if (hlen
< TCPOLEN_SACKLEN
||
2301 !pf_pull_hdr(m
, off
+ sizeof (*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
2304 while (hlen
>= TCPOLEN_SACKLEN
) {
2307 case TCPOPT_EOL
: /* FALLTHROUGH */
2315 if (olen
>= TCPOLEN_SACKLEN
) {
2316 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
2317 i
+= TCPOLEN_SACK
) {
2318 memcpy(&sack
, &opt
[i
], sizeof (sack
));
2319 pf_change_a(&sack
.start
, &th
->th_sum
,
2320 htonl(ntohl(sack
.start
) -
2322 pf_change_a(&sack
.end
, &th
->th_sum
,
2323 htonl(ntohl(sack
.end
) -
2325 memcpy(&opt
[i
], &sack
, sizeof (sack
));
2327 copyback
= off
+ sizeof (*th
) + thoptlen
;
2339 m
= pf_lazy_makewritable(pd
, m
, copyback
);
2342 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2348 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
2349 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
2350 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
2351 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
2352 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
2354 #pragma unused(eh, ifp)
2358 struct ip
*h
= NULL
;
2361 struct ip6_hdr
*h6
= NULL
;
2363 struct tcphdr
*th
= NULL
;
2365 struct pf_mtag
*pf_mtag
;
2367 /* maximum segment size tcp option */
2368 tlen
= sizeof (struct tcphdr
);
2375 len
= sizeof (struct ip
) + tlen
;
2380 len
= sizeof (struct ip6_hdr
) + tlen
;
2384 panic("pf_send_tcp: not AF_INET or AF_INET6!");
2388 /* create outgoing mbuf */
2389 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
2393 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
2399 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2400 pf_mtag
->pftag_tag
= rtag
;
2402 if (r
!= NULL
&& PF_RTABLEID_IS_VALID(r
->rtableid
))
2403 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2406 if (altq_allowed
&& r
!= NULL
&& r
->qid
)
2407 pf_mtag
->pftag_qid
= r
->qid
;
2408 #endif /* PF_ALTQ */
2410 /* add hints for ecn */
2411 pf_mtag
->pftag_hdr
= mtod(m
, struct ip
*);
2412 /* record address family */
2413 pf_mtag
->pftag_flags
&= ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
);
2417 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2422 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2426 /* indicate this is TCP */
2427 pf_mtag
->pftag_flags
|= PF_TAG_TCP
;
2429 /* Make sure headers are 32-bit aligned */
2430 m
->m_data
+= max_linkhdr
;
2431 m
->m_pkthdr
.len
= m
->m_len
= len
;
2432 m
->m_pkthdr
.rcvif
= NULL
;
2433 bzero(m
->m_data
, len
);
2437 h
= mtod(m
, struct ip
*);
2439 /* IP header fields included in the TCP checksum */
2440 h
->ip_p
= IPPROTO_TCP
;
2441 h
->ip_len
= htons(tlen
);
2442 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
2443 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
2445 th
= (struct tcphdr
*)(void *)((caddr_t
)h
+ sizeof (struct ip
));
2450 h6
= mtod(m
, struct ip6_hdr
*);
2452 /* IP header fields included in the TCP checksum */
2453 h6
->ip6_nxt
= IPPROTO_TCP
;
2454 h6
->ip6_plen
= htons(tlen
);
2455 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof (struct in6_addr
));
2456 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof (struct in6_addr
));
2458 th
= (struct tcphdr
*)(void *)
2459 ((caddr_t
)h6
+ sizeof (struct ip6_hdr
));
2465 th
->th_sport
= sport
;
2466 th
->th_dport
= dport
;
2467 th
->th_seq
= htonl(seq
);
2468 th
->th_ack
= htonl(ack
);
2469 th
->th_off
= tlen
>> 2;
2470 th
->th_flags
= flags
;
2471 th
->th_win
= htons(win
);
2474 opt
= (char *)(th
+ 1);
2475 opt
[0] = TCPOPT_MAXSEG
;
2477 #if BYTE_ORDER != BIG_ENDIAN
2480 bcopy((caddr_t
)&mss
, (caddr_t
)(opt
+ 2), 2);
2489 th
->th_sum
= in_cksum(m
, len
);
2491 /* Finish the IP header */
2493 h
->ip_hl
= sizeof (*h
) >> 2;
2494 h
->ip_tos
= IPTOS_LOWDELAY
;
2496 * ip_output() expects ip_len and ip_off to be in host order.
2499 h
->ip_off
= (path_mtu_discovery
? IP_DF
: 0);
2500 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
2503 bzero(&ro
, sizeof (ro
));
2504 ip_output(m
, NULL
, &ro
, 0, NULL
, NULL
);
2505 if (ro
.ro_rt
!= NULL
)
2512 struct route_in6 ro6
;
2515 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
2516 sizeof (struct ip6_hdr
), tlen
);
2518 h6
->ip6_vfc
|= IPV6_VERSION
;
2519 h6
->ip6_hlim
= IPV6_DEFHLIM
;
2521 bzero(&ro6
, sizeof (ro6
));
2522 ip6_output(m
, NULL
, &ro6
, 0, NULL
, NULL
, NULL
);
2523 if (ro6
.ro_rt
!= NULL
)
2532 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
2536 struct pf_mtag
*pf_mtag
;
2538 m0
= m_copy(m
, 0, M_COPYALL
);
2542 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
2545 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
2547 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
2548 pf_mtag
->pftag_rtableid
= r
->rtableid
;
2551 if (altq_allowed
&& r
->qid
)
2552 pf_mtag
->pftag_qid
= r
->qid
;
2553 #endif /* PF_ALTQ */
2555 /* add hints for ecn */
2556 pf_mtag
->pftag_hdr
= mtod(m0
, struct ip
*);
2557 /* record address family */
2558 pf_mtag
->pftag_flags
&=
2559 ~(PF_TAG_HDR_INET
| PF_TAG_HDR_INET6
| PF_TAG_TCP
);
2563 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
2568 pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
2576 icmp_error(m0
, type
, code
, 0, 0);
2581 icmp6_error(m0
, type
, code
, 0);
2588 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2589 * If n is 0, they match if they are equal. If n is != 0, they match if they
2593 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
2594 struct pf_addr
*b
, sa_family_t af
)
2601 if ((a
->addr32
[0] & m
->addr32
[0]) ==
2602 (b
->addr32
[0] & m
->addr32
[0]))
2608 if (((a
->addr32
[0] & m
->addr32
[0]) ==
2609 (b
->addr32
[0] & m
->addr32
[0])) &&
2610 ((a
->addr32
[1] & m
->addr32
[1]) ==
2611 (b
->addr32
[1] & m
->addr32
[1])) &&
2612 ((a
->addr32
[2] & m
->addr32
[2]) ==
2613 (b
->addr32
[2] & m
->addr32
[2])) &&
2614 ((a
->addr32
[3] & m
->addr32
[3]) ==
2615 (b
->addr32
[3] & m
->addr32
[3])))
2634 * Return 1 if b <= a <= e, otherwise return 0.
2637 pf_match_addr_range(struct pf_addr
*b
, struct pf_addr
*e
,
2638 struct pf_addr
*a
, sa_family_t af
)
2643 if ((a
->addr32
[0] < b
->addr32
[0]) ||
2644 (a
->addr32
[0] > e
->addr32
[0]))
2653 for (i
= 0; i
< 4; ++i
)
2654 if (a
->addr32
[i
] > b
->addr32
[i
])
2656 else if (a
->addr32
[i
] < b
->addr32
[i
])
2659 for (i
= 0; i
< 4; ++i
)
2660 if (a
->addr32
[i
] < e
->addr32
[i
])
2662 else if (a
->addr32
[i
] > e
->addr32
[i
])
2672 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
2676 return ((p
> a1
) && (p
< a2
));
2678 return ((p
< a1
) || (p
> a2
));
2680 return ((p
>= a1
) && (p
<= a2
));
2694 return (0); /* never reached */
2698 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
2700 #if BYTE_ORDER != BIG_ENDIAN
2705 return (pf_match(op
, a1
, a2
, p
));
2709 pf_match_xport(u_int8_t proto
, u_int8_t proto_variant
, union pf_rule_xport
*rx
,
2710 union pf_state_xport
*sx
)
2717 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
2718 d
= (rx
->call_id
== sx
->call_id
);
2722 d
= (rx
->spi
== sx
->spi
);
2728 case IPPROTO_ICMPV6
:
2730 d
= pf_match_port(rx
->range
.op
,
2731 rx
->range
.port
[0], rx
->range
.port
[1],
2744 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
2746 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2748 return (pf_match(op
, a1
, a2
, u
));
2752 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
2754 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2756 return (pf_match(op
, a1
, a2
, g
));
2760 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, struct pf_mtag
*pf_mtag
,
2765 *tag
= pf_mtag
->pftag_tag
;
2767 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2768 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2772 pf_tag_packet(struct mbuf
*m
, struct pf_mtag
*pf_mtag
, int tag
,
2773 unsigned int rtableid
, struct pf_pdesc
*pd
)
2775 if (tag
<= 0 && !PF_RTABLEID_IS_VALID(rtableid
) &&
2776 (pd
== NULL
|| pd
->flowhash
== 0))
2779 if (pf_mtag
== NULL
&& (pf_mtag
= pf_get_mtag(m
)) == NULL
)
2783 pf_mtag
->pftag_tag
= tag
;
2784 if (PF_RTABLEID_IS_VALID(rtableid
))
2785 pf_mtag
->pftag_rtableid
= rtableid
;
2786 if (pd
!= NULL
&& pd
->flowhash
!= 0) {
2787 pf_mtag
->pftag_flags
|= PF_TAG_FLOWHASH
;
2788 pf_mtag
->pftag_flowhash
= pd
->flowhash
;
2789 pf_mtag
->pftag_flags
|= (pd
->flags
& PFDESC_FLOW_ADV
) ?
2797 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2798 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2800 struct pf_anchor_stackframe
*f
;
2802 (*r
)->anchor
->match
= 0;
2805 if (*depth
>= (int)sizeof (pf_anchor_stack
) /
2806 (int)sizeof (pf_anchor_stack
[0])) {
2807 printf("pf_step_into_anchor: stack overflow\n");
2808 *r
= TAILQ_NEXT(*r
, entries
);
2810 } else if (*depth
== 0 && a
!= NULL
)
2812 f
= pf_anchor_stack
+ (*depth
)++;
2815 if ((*r
)->anchor_wildcard
) {
2816 f
->parent
= &(*r
)->anchor
->children
;
2817 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2822 *rs
= &f
->child
->ruleset
;
2826 *rs
= &(*r
)->anchor
->ruleset
;
2828 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2832 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2833 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2835 struct pf_anchor_stackframe
*f
;
2841 f
= pf_anchor_stack
+ *depth
- 1;
2842 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2843 if (f
->child
->match
||
2844 (match
!= NULL
&& *match
)) {
2845 f
->r
->anchor
->match
= 1;
2848 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
2849 if (f
->child
!= NULL
) {
2850 *rs
= &f
->child
->ruleset
;
2851 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2859 if (*depth
== 0 && a
!= NULL
)
2862 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
2863 quick
= f
->r
->quick
;
2864 *r
= TAILQ_NEXT(f
->r
, entries
);
2865 } while (*r
== NULL
);
2872 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
2873 struct pf_addr
*rmask
, struct pf_addr
*saddr
, sa_family_t af
)
2878 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2879 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2883 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
2884 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
2885 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
2886 ((rmask
->addr32
[1] ^ 0xffffffff) & saddr
->addr32
[1]);
2887 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
2888 ((rmask
->addr32
[2] ^ 0xffffffff) & saddr
->addr32
[2]);
2889 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
2890 ((rmask
->addr32
[3] ^ 0xffffffff) & saddr
->addr32
[3]);
2896 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
2901 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
2905 if (addr
->addr32
[3] == 0xffffffff) {
2906 addr
->addr32
[3] = 0;
2907 if (addr
->addr32
[2] == 0xffffffff) {
2908 addr
->addr32
[2] = 0;
2909 if (addr
->addr32
[1] == 0xffffffff) {
2910 addr
->addr32
[1] = 0;
2912 htonl(ntohl(addr
->addr32
[0]) + 1);
2915 htonl(ntohl(addr
->addr32
[1]) + 1);
2918 htonl(ntohl(addr
->addr32
[2]) + 1);
2921 htonl(ntohl(addr
->addr32
[3]) + 1);
2927 #define mix(a, b, c) \
2929 a -= b; a -= c; a ^= (c >> 13); \
2930 b -= c; b -= a; b ^= (a << 8); \
2931 c -= a; c -= b; c ^= (b >> 13); \
2932 a -= b; a -= c; a ^= (c >> 12); \
2933 b -= c; b -= a; b ^= (a << 16); \
2934 c -= a; c -= b; c ^= (b >> 5); \
2935 a -= b; a -= c; a ^= (c >> 3); \
2936 b -= c; b -= a; b ^= (a << 10); \
2937 c -= a; c -= b; c ^= (b >> 15); \
2941 * hash function based on bridge_hash in if_bridge.c
2944 pf_hash(struct pf_addr
*inaddr
, struct pf_addr
*hash
,
2945 struct pf_poolhashkey
*key
, sa_family_t af
)
2947 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
2952 a
+= inaddr
->addr32
[0];
2955 hash
->addr32
[0] = c
+ key
->key32
[2];
2960 a
+= inaddr
->addr32
[0];
2961 b
+= inaddr
->addr32
[2];
2963 hash
->addr32
[0] = c
;
2964 a
+= inaddr
->addr32
[1];
2965 b
+= inaddr
->addr32
[3];
2968 hash
->addr32
[1] = c
;
2969 a
+= inaddr
->addr32
[2];
2970 b
+= inaddr
->addr32
[1];
2973 hash
->addr32
[2] = c
;
2974 a
+= inaddr
->addr32
[3];
2975 b
+= inaddr
->addr32
[0];
2978 hash
->addr32
[3] = c
;
2985 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, struct pf_addr
*saddr
,
2986 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
2988 unsigned char hash
[16];
2989 struct pf_pool
*rpool
= &r
->rpool
;
2990 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
2991 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
2992 struct pf_pooladdr
*acur
= rpool
->cur
;
2993 struct pf_src_node k
;
2995 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
2996 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
2998 PF_ACPY(&k
.addr
, saddr
, af
);
2999 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
3000 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
3004 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
3005 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
3006 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, af
)) {
3007 PF_ACPY(naddr
, &(*sn
)->raddr
, af
);
3008 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
3009 printf("pf_map_addr: src tracking maps ");
3010 pf_print_host(&k
.addr
, 0, af
);
3012 pf_print_host(naddr
, 0, af
);
3019 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
3021 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3025 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
3026 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3029 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
3030 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
3035 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
3036 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3039 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
3040 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
3044 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3045 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
3046 return (1); /* unsupported */
3048 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3049 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3052 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
3054 PF_ACPY(naddr
, raddr
, af
);
3056 case PF_POOL_BITMASK
:
3057 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3059 case PF_POOL_RANDOM
:
3060 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
)) {
3064 rpool
->counter
.addr32
[0] = htonl(random());
3069 if (rmask
->addr32
[3] != 0xffffffff)
3070 rpool
->counter
.addr32
[3] =
3074 if (rmask
->addr32
[2] != 0xffffffff)
3075 rpool
->counter
.addr32
[2] =
3079 if (rmask
->addr32
[1] != 0xffffffff)
3080 rpool
->counter
.addr32
[1] =
3084 if (rmask
->addr32
[0] != 0xffffffff)
3085 rpool
->counter
.addr32
[0] =
3090 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3091 PF_ACPY(init_addr
, naddr
, af
);
3094 PF_AINC(&rpool
->counter
, af
);
3095 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3098 case PF_POOL_SRCHASH
:
3099 pf_hash(saddr
, (struct pf_addr
*)(void *)&hash
,
3101 PF_POOLMASK(naddr
, raddr
, rmask
,
3102 (struct pf_addr
*)(void *)&hash
, af
);
3104 case PF_POOL_ROUNDROBIN
:
3105 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3106 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3107 &rpool
->tblidx
, &rpool
->counter
,
3108 &raddr
, &rmask
, af
))
3110 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3111 if (!pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3112 &rpool
->tblidx
, &rpool
->counter
,
3113 &raddr
, &rmask
, af
))
3115 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
, af
))
3119 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
3120 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
3121 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3123 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3124 &rpool
->tblidx
, &rpool
->counter
,
3125 &raddr
, &rmask
, af
)) {
3126 /* table contains no address of type 'af' */
3127 if (rpool
->cur
!= acur
)
3131 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3133 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3134 &rpool
->tblidx
, &rpool
->counter
,
3135 &raddr
, &rmask
, af
)) {
3136 /* table contains no address of type 'af' */
3137 if (rpool
->cur
!= acur
)
3142 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3143 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3144 PF_ACPY(&rpool
->counter
, raddr
, af
);
3148 PF_ACPY(naddr
, &rpool
->counter
, af
);
3149 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
))
3150 PF_ACPY(init_addr
, naddr
, af
);
3151 PF_AINC(&rpool
->counter
, af
);
3155 PF_ACPY(&(*sn
)->raddr
, naddr
, af
);
3157 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
3158 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3159 printf("pf_map_addr: selected address ");
3160 pf_print_host(naddr
, 0, af
);
3168 pf_get_sport(struct pf_pdesc
*pd
, struct pfi_kif
*kif
, struct pf_rule
*r
,
3169 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3170 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3171 union pf_state_xport
*nxport
, struct pf_src_node
**sn
)
3174 struct pf_state_key_cmp key
;
3175 struct pf_addr init_addr
;
3177 sa_family_t af
= pd
->af
;
3178 u_int8_t proto
= pd
->proto
;
3179 unsigned int low
= r
->rpool
.proxy_port
[0];
3180 unsigned int high
= r
->rpool
.proxy_port
[1];
3182 bzero(&init_addr
, sizeof (init_addr
));
3183 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3186 if (proto
== IPPROTO_ICMP
) {
3192 return (0); /* No output necessary. */
3194 /*--- Special mapping rules for UDP ---*/
3195 if (proto
== IPPROTO_UDP
) {
3197 /*--- Never float IKE source port ---*/
3198 if (ntohs(sxport
->port
) == PF_IKE_PORT
) {
3199 nxport
->port
= sxport
->port
;
3203 /*--- Apply exterior mapping options ---*/
3204 if (r
->extmap
> PF_EXTMAP_APD
) {
3207 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3208 struct pf_state_key
*sk
= s
->state_key
;
3211 if (s
->nat_rule
.ptr
!= r
)
3213 if (sk
->proto
!= IPPROTO_UDP
|| sk
->af
!= af
)
3215 if (sk
->lan
.xport
.port
!= sxport
->port
)
3217 if (PF_ANEQ(&sk
->lan
.addr
, saddr
, af
))
3219 if (r
->extmap
< PF_EXTMAP_EI
&&
3220 PF_ANEQ(&sk
->ext
.addr
, daddr
, af
))
3223 nxport
->port
= sk
->gwy
.xport
.port
;
3227 } else if (proto
== IPPROTO_TCP
) {
3230 * APPLE MODIFICATION: <rdar://problem/6546358>
3231 * Fix allows....NAT to use a single binding for TCP session
3232 * with same source IP and source port
3234 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3235 struct pf_state_key
* sk
= s
->state_key
;
3238 if (s
->nat_rule
.ptr
!= r
)
3240 if (sk
->proto
!= IPPROTO_TCP
|| sk
->af
!= af
)
3242 if (sk
->lan
.xport
.port
!= sxport
->port
)
3244 if (!(PF_AEQ(&sk
->lan
.addr
, saddr
, af
)))
3246 nxport
->port
= sk
->gwy
.xport
.port
;
3253 PF_ACPY(&key
.ext
.addr
, daddr
, key
.af
);
3254 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af
);
3257 key
.proto_variant
= r
->extfilter
;
3260 key
.proto_variant
= 0;
3264 key
.ext
.xport
= *dxport
;
3266 memset(&key
.ext
.xport
, 0, sizeof (key
.ext
.xport
));
3268 * port search; start random, step;
3269 * similar 2 portloop in in_pcbbind
3271 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
3272 proto
== IPPROTO_ICMP
)) {
3274 key
.gwy
.xport
= *dxport
;
3276 memset(&key
.gwy
.xport
, 0,
3277 sizeof (key
.ext
.xport
));
3278 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3280 } else if (low
== 0 && high
== 0) {
3281 key
.gwy
.xport
= *nxport
;
3282 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3284 } else if (low
== high
) {
3285 key
.gwy
.xport
.port
= htons(low
);
3286 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3287 nxport
->port
= htons(low
);
3298 cut
= htonl(random()) % (1 + high
- low
) + low
;
3299 /* low <= cut <= high */
3300 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
3301 key
.gwy
.xport
.port
= htons(tmp
);
3302 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3304 nxport
->port
= htons(tmp
);
3308 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
3309 key
.gwy
.xport
.port
= htons(tmp
);
3310 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3312 nxport
->port
= htons(tmp
);
3318 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
3319 case PF_POOL_RANDOM
:
3320 case PF_POOL_ROUNDROBIN
:
3321 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3325 case PF_POOL_SRCHASH
:
3326 case PF_POOL_BITMASK
:
3330 } while (!PF_AEQ(&init_addr
, naddr
, af
));
3332 return (1); /* none available */
3335 static struct pf_rule
*
3336 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3337 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
,
3338 union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3339 union pf_state_xport
*dxport
, int rs_num
)
3341 struct pf_rule
*r
, *rm
= NULL
;
3342 struct pf_ruleset
*ruleset
= NULL
;
3344 unsigned int rtableid
= IFSCOPE_NONE
;
3347 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
3348 while (r
&& rm
== NULL
) {
3349 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
3350 struct pf_addr_wrap
*xdst
= NULL
;
3351 struct pf_addr_wrap
*xsrc
= NULL
;
3352 union pf_rule_xport rdrxport
;
3354 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
3356 if (r
->rpool
.cur
!= NULL
)
3357 xdst
= &r
->rpool
.cur
->addr
;
3358 } else if (r
->action
== PF_RDR
&& direction
== PF_OUT
) {
3361 if (r
->rpool
.cur
!= NULL
) {
3362 rdrxport
.range
.op
= PF_OP_EQ
;
3363 rdrxport
.range
.port
[0] =
3364 htons(r
->rpool
.proxy_port
[0]);
3365 xsrc
= &r
->rpool
.cur
->addr
;
3373 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3374 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3375 else if (r
->direction
&& r
->direction
!= direction
)
3376 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3377 else if (r
->af
&& r
->af
!= pd
->af
)
3378 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3379 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3380 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3381 else if (xsrc
&& PF_MISMATCHAW(xsrc
, saddr
, pd
->af
, 0, NULL
))
3382 r
= TAILQ_NEXT(r
, entries
);
3383 else if (!xsrc
&& PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3385 r
= TAILQ_NEXT(r
, entries
);
3386 else if (xsrc
&& (!rdrxport
.range
.port
[0] ||
3387 !pf_match_xport(r
->proto
, r
->proto_variant
, &rdrxport
,
3389 r
= TAILQ_NEXT(r
, entries
);
3390 else if (!xsrc
&& !pf_match_xport(r
->proto
,
3391 r
->proto_variant
, &src
->xport
, sxport
))
3392 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
3393 PF_SKIP_DST_PORT
].ptr
;
3394 else if (dst
!= NULL
&&
3395 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
3396 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3397 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
3399 r
= TAILQ_NEXT(r
, entries
);
3400 else if (dst
&& !pf_match_xport(r
->proto
, r
->proto_variant
,
3401 &dst
->xport
, dxport
))
3402 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3403 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
3404 r
= TAILQ_NEXT(r
, entries
);
3405 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
3406 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
3407 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
3408 r
= TAILQ_NEXT(r
, entries
);
3412 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
3413 rtableid
= r
->rtableid
;
3414 if (r
->anchor
== NULL
) {
3417 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
3421 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
3424 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, NULL
))
3426 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
3427 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
))
3432 static struct pf_rule
*
3433 pf_get_translation_aux(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3434 int direction
, struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3435 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3436 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3437 union pf_state_xport
*nxport
)
3439 struct pf_rule
*r
= NULL
;
3441 if (direction
== PF_OUT
) {
3442 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3443 sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3445 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3446 saddr
, sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3448 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3449 saddr
, sxport
, daddr
, dxport
, PF_RULESET_NAT
);
3451 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3452 sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3454 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3455 saddr
, sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3459 switch (r
->action
) {
3465 if (pf_get_sport(pd
, kif
, r
, saddr
, sxport
, daddr
,
3466 dxport
, naddr
, nxport
, sn
)) {
3467 DPFPRINTF(PF_DEBUG_MISC
,
3468 ("pf: NAT proxy port allocation "
3470 r
->rpool
.proxy_port
[0],
3471 r
->rpool
.proxy_port
[1]));
3476 switch (direction
) {
3478 if (r
->rpool
.cur
->addr
.type
==
3483 if (r
->rpool
.cur
->addr
.p
.dyn
->
3487 &r
->rpool
.cur
->addr
.p
.dyn
->
3489 &r
->rpool
.cur
->addr
.p
.dyn
->
3496 if (r
->rpool
.cur
->addr
.p
.dyn
->
3500 &r
->rpool
.cur
->addr
.p
.dyn
->
3502 &r
->rpool
.cur
->addr
.p
.dyn
->
3510 &r
->rpool
.cur
->addr
.v
.a
.addr
,
3511 &r
->rpool
.cur
->addr
.v
.a
.mask
,
3516 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
3520 if (r
->src
.addr
.p
.dyn
->
3524 &r
->src
.addr
.p
.dyn
->
3526 &r
->src
.addr
.p
.dyn
->
3533 if (r
->src
.addr
.p
.dyn
->
3537 &r
->src
.addr
.p
.dyn
->
3539 &r
->src
.addr
.p
.dyn
->
3547 &r
->src
.addr
.v
.a
.addr
,
3548 &r
->src
.addr
.v
.a
.mask
, daddr
,
3554 switch (direction
) {
3556 if (r
->dst
.addr
.type
== PF_ADDR_DYNIFTL
) {
3560 if (r
->dst
.addr
.p
.dyn
->
3564 &r
->dst
.addr
.p
.dyn
->
3566 &r
->dst
.addr
.p
.dyn
->
3573 if (r
->dst
.addr
.p
.dyn
->
3577 &r
->dst
.addr
.p
.dyn
->
3579 &r
->dst
.addr
.p
.dyn
->
3587 &r
->dst
.addr
.v
.a
.addr
,
3588 &r
->dst
.addr
.v
.a
.mask
,
3591 if (nxport
&& r
->dst
.xport
.range
.port
[0])
3593 r
->dst
.xport
.range
.port
[0];
3596 if (pf_map_addr(pd
->af
, r
, saddr
,
3599 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3601 PF_POOLMASK(naddr
, naddr
,
3602 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3605 if (nxport
&& dxport
) {
3606 if (r
->rpool
.proxy_port
[1]) {
3607 u_int32_t tmp_nport
;
3610 ((ntohs(dxport
->port
) -
3611 ntohs(r
->dst
.xport
.range
.
3613 (r
->rpool
.proxy_port
[1] -
3614 r
->rpool
.proxy_port
[0] +
3615 1)) + r
->rpool
.proxy_port
[0];
3617 /* wrap around if necessary */
3618 if (tmp_nport
> 65535)
3621 htons((u_int16_t
)tmp_nport
);
3622 } else if (r
->rpool
.proxy_port
[0]) {
3623 nxport
->port
= htons(r
->rpool
.
3640 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
3642 struct pf_addr
*saddr
, *daddr
;
3643 u_int16_t sport
, dport
;
3644 struct inpcbinfo
*pi
;
3649 pd
->lookup
.uid
= UID_MAX
;
3650 pd
->lookup
.gid
= GID_MAX
;
3651 pd
->lookup
.pid
= NO_PID
;
3653 switch (pd
->proto
) {
3655 if (pd
->hdr
.tcp
== NULL
)
3657 sport
= pd
->hdr
.tcp
->th_sport
;
3658 dport
= pd
->hdr
.tcp
->th_dport
;
3662 if (pd
->hdr
.udp
== NULL
)
3664 sport
= pd
->hdr
.udp
->uh_sport
;
3665 dport
= pd
->hdr
.udp
->uh_dport
;
3671 if (direction
== PF_IN
) {
3686 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
, daddr
->v4
, dport
,
3687 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3690 struct in6_addr s6
, d6
;
3692 memset(&s6
, 0, sizeof (s6
));
3693 s6
.s6_addr16
[5] = htons(0xffff);
3694 memcpy(&s6
.s6_addr32
[3], &saddr
->v4
,
3695 sizeof (saddr
->v4
));
3697 memset(&d6
, 0, sizeof (d6
));
3698 d6
.s6_addr16
[5] = htons(0xffff);
3699 memcpy(&d6
.s6_addr32
[3], &daddr
->v4
,
3700 sizeof (daddr
->v4
));
3702 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3703 &d6
, dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3705 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3706 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3708 inp
= in6_pcblookup_hash_exists(pi
, &s6
, sport
,
3709 &d6
, dport
, INPLOOKUP_WILDCARD
,
3710 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3718 inp
= in_pcblookup_hash_exists(pi
, saddr
->v4
, sport
,
3719 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
,
3720 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3729 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
, &daddr
->v6
,
3730 dport
, 0, &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3732 inp
= in6_pcblookup_hash_exists(pi
, &saddr
->v6
, sport
,
3733 &daddr
->v6
, dport
, INPLOOKUP_WILDCARD
,
3734 &pd
->lookup
.uid
, &pd
->lookup
.gid
, NULL
);
3749 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3753 u_int8_t
*opt
, optlen
;
3754 u_int8_t wscale
= 0;
3756 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3757 if (hlen
<= (int)sizeof (struct tcphdr
))
3759 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3761 opt
= hdr
+ sizeof (struct tcphdr
);
3762 hlen
-= sizeof (struct tcphdr
);
3772 if (wscale
> TCP_MAX_WINSHIFT
)
3773 wscale
= TCP_MAX_WINSHIFT
;
3774 wscale
|= PF_WSCALE_FLAG
;
3789 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3793 u_int8_t
*opt
, optlen
;
3794 u_int16_t mss
= tcp_mssdflt
;
3796 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3797 if (hlen
<= (int)sizeof (struct tcphdr
))
3799 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3801 opt
= hdr
+ sizeof (struct tcphdr
);
3802 hlen
-= sizeof (struct tcphdr
);
3803 while (hlen
>= TCPOLEN_MAXSEG
) {
3811 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, 2);
3812 #if BYTE_ORDER != BIG_ENDIAN
3829 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
3832 struct sockaddr_in
*dst
;
3836 struct sockaddr_in6
*dst6
;
3837 struct route_in6 ro6
;
3839 struct rtentry
*rt
= NULL
;
3841 u_int16_t mss
= tcp_mssdflt
;
3846 hlen
= sizeof (struct ip
);
3847 bzero(&ro
, sizeof (ro
));
3848 dst
= (struct sockaddr_in
*)(void *)&ro
.ro_dst
;
3849 dst
->sin_family
= AF_INET
;
3850 dst
->sin_len
= sizeof (*dst
);
3851 dst
->sin_addr
= addr
->v4
;
3858 hlen
= sizeof (struct ip6_hdr
);
3859 bzero(&ro6
, sizeof (ro6
));
3860 dst6
= (struct sockaddr_in6
*)(void *)&ro6
.ro_dst
;
3861 dst6
->sin6_family
= AF_INET6
;
3862 dst6
->sin6_len
= sizeof (*dst6
);
3863 dst6
->sin6_addr
= addr
->v6
;
3864 rtalloc((struct route
*)&ro
);
3869 panic("pf_calc_mss: not AF_INET or AF_INET6!");
3873 if (rt
&& rt
->rt_ifp
) {
3874 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof (struct tcphdr
);
3875 mss
= max(tcp_mssdflt
, mss
);
3878 mss
= min(mss
, offer
);
3879 mss
= max(mss
, 64); /* sanity - at least max opt space */
3884 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
)
3886 struct pf_rule
*r
= s
->rule
.ptr
;
3889 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
3891 switch (s
->state_key
->af
) {
3894 pf_map_addr(AF_INET
, r
, saddr
, &s
->rt_addr
, NULL
,
3896 s
->rt_kif
= r
->rpool
.cur
->kif
;
3901 pf_map_addr(AF_INET6
, r
, saddr
, &s
->rt_addr
, NULL
,
3903 s
->rt_kif
= r
->rpool
.cur
->kif
;
3910 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
3915 /* list is sorted, if-bound states before floating */
3917 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
3919 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
3923 pf_detach_state(struct pf_state
*s
, int flags
)
3925 struct pf_state_key
*sk
= s
->state_key
;
3930 s
->state_key
= NULL
;
3931 TAILQ_REMOVE(&sk
->states
, s
, next
);
3932 if (--sk
->refcnt
== 0) {
3933 if (!(flags
& PF_DT_SKIP_EXTGWY
))
3934 RB_REMOVE(pf_state_tree_ext_gwy
,
3935 &pf_statetbl_ext_gwy
, sk
);
3936 if (!(flags
& PF_DT_SKIP_LANEXT
))
3937 RB_REMOVE(pf_state_tree_lan_ext
,
3938 &pf_statetbl_lan_ext
, sk
);
3940 pool_put(&pf_app_state_pl
, sk
->app_state
);
3941 pool_put(&pf_state_key_pl
, sk
);
3945 struct pf_state_key
*
3946 pf_alloc_state_key(struct pf_state
*s
, struct pf_state_key
*psk
)
3948 struct pf_state_key
*sk
;
3950 if ((sk
= pool_get(&pf_state_key_pl
, PR_WAITOK
)) == NULL
)
3952 bzero(sk
, sizeof (*sk
));
3953 TAILQ_INIT(&sk
->states
);
3954 pf_attach_state(sk
, s
, 0);
3956 /* initialize state key from psk, if provided */
3958 bcopy(&psk
->lan
, &sk
->lan
, sizeof (sk
->lan
));
3959 bcopy(&psk
->gwy
, &sk
->gwy
, sizeof (sk
->gwy
));
3960 bcopy(&psk
->ext
, &sk
->ext
, sizeof (sk
->ext
));
3962 sk
->proto
= psk
->proto
;
3963 sk
->direction
= psk
->direction
;
3964 sk
->proto_variant
= psk
->proto_variant
;
3965 VERIFY(psk
->app_state
== NULL
);
3966 sk
->flowhash
= psk
->flowhash
;
3967 /* don't touch tree entries, states and refcnt on sk */
3974 pf_tcp_iss(struct pf_pdesc
*pd
)
3977 u_int32_t digest
[4];
3979 if (pf_tcp_secret_init
== 0) {
3980 read_random(pf_tcp_secret
, sizeof (pf_tcp_secret
));
3981 MD5Init(&pf_tcp_secret_ctx
);
3982 MD5Update(&pf_tcp_secret_ctx
, pf_tcp_secret
,
3983 sizeof (pf_tcp_secret
));
3984 pf_tcp_secret_init
= 1;
3986 ctx
= pf_tcp_secret_ctx
;
3988 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_sport
, sizeof (u_short
));
3989 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_dport
, sizeof (u_short
));
3990 if (pd
->af
== AF_INET6
) {
3991 MD5Update(&ctx
, (char *)&pd
->src
->v6
, sizeof (struct in6_addr
));
3992 MD5Update(&ctx
, (char *)&pd
->dst
->v6
, sizeof (struct in6_addr
));
3994 MD5Update(&ctx
, (char *)&pd
->src
->v4
, sizeof (struct in_addr
));
3995 MD5Update(&ctx
, (char *)&pd
->dst
->v4
, sizeof (struct in_addr
));
3997 MD5Final((u_char
*)digest
, &ctx
);
3998 pf_tcp_iss_off
+= 4096;
3999 return (digest
[0] + random() + pf_tcp_iss_off
);
4003 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
4004 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
4005 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
4006 struct ifqueue
*ifq
)
4009 struct pf_rule
*nr
= NULL
;
4010 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4011 sa_family_t af
= pd
->af
;
4012 struct pf_rule
*r
, *a
= NULL
;
4013 struct pf_ruleset
*ruleset
= NULL
;
4014 struct pf_src_node
*nsn
= NULL
;
4015 struct tcphdr
*th
= pd
->hdr
.tcp
;
4017 int rewrite
= 0, hdrlen
= 0;
4019 unsigned int rtableid
= IFSCOPE_NONE
;
4023 u_int16_t mss
= tcp_mssdflt
;
4024 u_int8_t icmptype
= 0, icmpcode
= 0;
4026 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
4027 union pf_state_xport bxport
, nxport
, sxport
, dxport
;
4028 struct pf_state_key psk
;
4030 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
4032 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
4033 REASON_SET(&reason
, PFRES_CONGEST
);
4042 switch (pd
->proto
) {
4044 sxport
.port
= th
->th_sport
;
4045 dxport
.port
= th
->th_dport
;
4046 hdrlen
= sizeof (*th
);
4049 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4050 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4051 hdrlen
= sizeof (*pd
->hdr
.udp
);
4055 if (pd
->af
!= AF_INET
)
4057 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
4058 hdrlen
= ICMP_MINLEN
;
4059 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4060 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
4062 if (icmptype
== ICMP_UNREACH
||
4063 icmptype
== ICMP_SOURCEQUENCH
||
4064 icmptype
== ICMP_REDIRECT
||
4065 icmptype
== ICMP_TIMXCEED
||
4066 icmptype
== ICMP_PARAMPROB
)
4071 case IPPROTO_ICMPV6
:
4072 if (pd
->af
!= AF_INET6
)
4074 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
4075 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
4076 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4077 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
4079 if (icmptype
== ICMP6_DST_UNREACH
||
4080 icmptype
== ICMP6_PACKET_TOO_BIG
||
4081 icmptype
== ICMP6_TIME_EXCEEDED
||
4082 icmptype
== ICMP6_PARAM_PROB
)
4087 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
4088 sxport
.call_id
= dxport
.call_id
=
4089 pd
->hdr
.grev1
->call_id
;
4090 hdrlen
= sizeof (*pd
->hdr
.grev1
);
4095 dxport
.spi
= pd
->hdr
.esp
->spi
;
4096 hdrlen
= sizeof (*pd
->hdr
.esp
);
4100 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
4102 if (direction
== PF_OUT
) {
4103 bxport
= nxport
= sxport
;
4104 /* check outgoing packet for BINAT/NAT */
4105 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
4106 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4108 PF_ACPY(&pd
->baddr
, saddr
, af
);
4109 switch (pd
->proto
) {
4111 pf_change_ap(direction
, pd
->mp
, saddr
,
4112 &th
->th_sport
, pd
->ip_sum
, &th
->th_sum
,
4113 &pd
->naddr
, nxport
.port
, 0, af
);
4114 sxport
.port
= th
->th_sport
;
4118 pf_change_ap(direction
, pd
->mp
, saddr
,
4119 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4120 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4121 nxport
.port
, 1, af
);
4122 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4127 if (pd
->af
== AF_INET
) {
4128 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
4129 pd
->naddr
.v4
.s_addr
, 0);
4130 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4131 pd
->hdr
.icmp
->icmp_cksum
, sxport
.port
,
4133 pd
->hdr
.icmp
->icmp_id
= nxport
.port
;
4139 case IPPROTO_ICMPV6
:
4140 if (pd
->af
== AF_INET6
) {
4141 pf_change_a6(saddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4151 pf_change_a(&saddr
->v4
.s_addr
,
4152 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4157 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4168 pf_change_a(&saddr
->v4
.s_addr
,
4169 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4174 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4183 pf_change_a(&saddr
->v4
.s_addr
,
4184 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4189 PF_ACPY(saddr
, &pd
->naddr
, af
);
4201 bxport
.port
= nxport
.port
= dxport
.port
;
4202 /* check incoming packet for BINAT/RDR */
4203 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_IN
, kif
, &nsn
,
4204 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4206 PF_ACPY(&pd
->baddr
, daddr
, af
);
4207 switch (pd
->proto
) {
4209 pf_change_ap(direction
, pd
->mp
, daddr
,
4210 &th
->th_dport
, pd
->ip_sum
, &th
->th_sum
,
4211 &pd
->naddr
, nxport
.port
, 0, af
);
4212 dxport
.port
= th
->th_dport
;
4216 pf_change_ap(direction
, pd
->mp
, daddr
,
4217 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4218 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4219 nxport
.port
, 1, af
);
4220 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4225 if (pd
->af
== AF_INET
) {
4226 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
4227 pd
->naddr
.v4
.s_addr
, 0);
4232 case IPPROTO_ICMPV6
:
4233 if (pd
->af
== AF_INET6
) {
4234 pf_change_a6(daddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4241 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
4242 grev1
->call_id
= nxport
.call_id
;
4247 pf_change_a(&daddr
->v4
.s_addr
,
4248 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4253 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4263 pf_change_a(&daddr
->v4
.s_addr
,
4264 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4269 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4278 pf_change_a(&daddr
->v4
.s_addr
,
4279 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4284 PF_ACPY(daddr
, &pd
->naddr
, af
);
4297 if (nr
&& nr
->tag
> 0)
4302 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
4303 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
4304 else if (r
->direction
&& r
->direction
!= direction
)
4305 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
4306 else if (r
->af
&& r
->af
!= af
)
4307 r
= r
->skip
[PF_SKIP_AF
].ptr
;
4308 else if (r
->proto
&& r
->proto
!= pd
->proto
)
4309 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
4310 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
4312 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
4313 /* tcp/udp only. port_op always 0 in other cases */
4314 else if (r
->proto
== pd
->proto
&&
4315 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4316 r
->src
.xport
.range
.op
&&
4317 !pf_match_port(r
->src
.xport
.range
.op
,
4318 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
4320 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
4321 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
4323 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
4324 /* tcp/udp only. port_op always 0 in other cases */
4325 else if (r
->proto
== pd
->proto
&&
4326 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4327 r
->dst
.xport
.range
.op
&&
4328 !pf_match_port(r
->dst
.xport
.range
.op
,
4329 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
4331 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
4332 /* icmp only. type always 0 in other cases */
4333 else if (r
->type
&& r
->type
!= icmptype
+ 1)
4334 r
= TAILQ_NEXT(r
, entries
);
4335 /* icmp only. type always 0 in other cases */
4336 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
4337 r
= TAILQ_NEXT(r
, entries
);
4338 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
4339 !(r
->tos
& pd
->tos
))
4340 r
= TAILQ_NEXT(r
, entries
);
4341 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
4342 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
4343 r
= TAILQ_NEXT(r
, entries
);
4344 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
4345 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
4346 r
= TAILQ_NEXT(r
, entries
);
4347 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
4348 r
= TAILQ_NEXT(r
, entries
);
4349 else if (pd
->proto
== IPPROTO_TCP
&&
4350 (r
->flagset
& th
->th_flags
) != r
->flags
)
4351 r
= TAILQ_NEXT(r
, entries
);
4352 /* tcp/udp only. uid.op always 0 in other cases */
4353 else if (r
->uid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4354 pf_socket_lookup(direction
, pd
), 1)) &&
4355 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
4357 r
= TAILQ_NEXT(r
, entries
);
4358 /* tcp/udp only. gid.op always 0 in other cases */
4359 else if (r
->gid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4360 pf_socket_lookup(direction
, pd
), 1)) &&
4361 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
4363 r
= TAILQ_NEXT(r
, entries
);
4364 else if (r
->prob
&& r
->prob
<= (random() % (UINT_MAX
- 1) + 1))
4365 r
= TAILQ_NEXT(r
, entries
);
4366 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
4367 r
= TAILQ_NEXT(r
, entries
);
4368 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
4369 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
4370 pf_osfp_fingerprint(pd
, m
, off
, th
),
4371 r
->os_fingerprint
)))
4372 r
= TAILQ_NEXT(r
, entries
);
4376 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
4377 rtableid
= r
->rtableid
;
4378 if (r
->anchor
== NULL
) {
4385 r
= TAILQ_NEXT(r
, entries
);
4387 pf_step_into_anchor(&asd
, &ruleset
,
4388 PF_RULESET_FILTER
, &r
, &a
, &match
);
4390 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
4391 PF_RULESET_FILTER
, &r
, &a
, &match
))
4398 REASON_SET(&reason
, PFRES_MATCH
);
4400 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
4402 if (rewrite
< off
+ hdrlen
)
4403 rewrite
= off
+ hdrlen
;
4405 m
= pf_lazy_makewritable(pd
, m
, rewrite
);
4407 REASON_SET(&reason
, PFRES_MEMORY
);
4411 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
4413 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
->log
? r
: nr
,
4417 if ((r
->action
== PF_DROP
) &&
4418 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4419 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
4420 (r
->rule_flag
& PFRULE_RETURN
))) {
4421 /* undo NAT changes, if they have taken place */
4423 if (direction
== PF_OUT
) {
4424 switch (pd
->proto
) {
4426 pf_change_ap(direction
, pd
->mp
, saddr
,
4427 &th
->th_sport
, pd
->ip_sum
,
4428 &th
->th_sum
, &pd
->baddr
,
4429 bxport
.port
, 0, af
);
4430 sxport
.port
= th
->th_sport
;
4434 pf_change_ap(direction
, pd
->mp
, saddr
,
4435 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4436 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4437 bxport
.port
, 1, af
);
4438 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4443 case IPPROTO_ICMPV6
:
4448 PF_ACPY(&pd
->baddr
, saddr
, af
);
4453 pf_change_a(&saddr
->v4
.s_addr
,
4455 pd
->baddr
.v4
.s_addr
, 0);
4460 PF_ACPY(saddr
, &pd
->baddr
,
4467 PF_ACPY(&pd
->baddr
, saddr
, af
);
4471 pf_change_a(&saddr
->v4
.s_addr
,
4473 pd
->baddr
.v4
.s_addr
, 0);
4478 PF_ACPY(saddr
, &pd
->baddr
,
4487 pf_change_a(&saddr
->v4
.s_addr
,
4489 pd
->baddr
.v4
.s_addr
, 0);
4492 PF_ACPY(saddr
, &pd
->baddr
, af
);
4497 switch (pd
->proto
) {
4499 pf_change_ap(direction
, pd
->mp
, daddr
,
4500 &th
->th_dport
, pd
->ip_sum
,
4501 &th
->th_sum
, &pd
->baddr
,
4502 bxport
.port
, 0, af
);
4503 dxport
.port
= th
->th_dport
;
4507 pf_change_ap(direction
, pd
->mp
, daddr
,
4508 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4509 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4510 bxport
.port
, 1, af
);
4511 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4516 case IPPROTO_ICMPV6
:
4521 if (pd
->proto_variant
==
4522 PF_GRE_PPTP_VARIANT
)
4523 grev1
->call_id
= bxport
.call_id
;
4528 pf_change_a(&daddr
->v4
.s_addr
,
4530 pd
->baddr
.v4
.s_addr
, 0);
4535 PF_ACPY(daddr
, &pd
->baddr
,
4545 pf_change_a(&daddr
->v4
.s_addr
,
4547 pd
->baddr
.v4
.s_addr
, 0);
4552 PF_ACPY(daddr
, &pd
->baddr
,
4561 pf_change_a(&daddr
->v4
.s_addr
,
4563 pd
->baddr
.v4
.s_addr
, 0);
4567 PF_ACPY(daddr
, &pd
->baddr
, af
);
4574 if (pd
->proto
== IPPROTO_TCP
&&
4575 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4576 (r
->rule_flag
& PFRULE_RETURN
)) &&
4577 !(th
->th_flags
& TH_RST
)) {
4578 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
4587 h4
= mtod(m
, struct ip
*);
4588 len
= ntohs(h4
->ip_len
) - off
;
4592 h6
= mtod(m
, struct ip6_hdr
*);
4593 len
= ntohs(h6
->ip6_plen
) -
4594 (off
- sizeof (*h6
));
4599 if (pf_check_proto_cksum(m
, off
, len
, IPPROTO_TCP
, af
))
4600 REASON_SET(&reason
, PFRES_PROTCKSUM
);
4602 if (th
->th_flags
& TH_SYN
)
4604 if (th
->th_flags
& TH_FIN
)
4606 pf_send_tcp(r
, af
, pd
->dst
,
4607 pd
->src
, th
->th_dport
, th
->th_sport
,
4608 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
4609 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
4611 } else if (pd
->proto
!= IPPROTO_ICMP
&& af
== AF_INET
&&
4612 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4614 pf_send_icmp(m
, r
->return_icmp
>> 8,
4615 r
->return_icmp
& 255, af
, r
);
4616 else if (pd
->proto
!= IPPROTO_ICMPV6
&& af
== AF_INET6
&&
4617 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4619 pf_send_icmp(m
, r
->return_icmp6
>> 8,
4620 r
->return_icmp6
& 255, af
, r
);
4623 if (r
->action
== PF_DROP
)
4626 /* prepare state key, for flowhash and/or the state (if created) */
4627 bzero(&psk
, sizeof (psk
));
4628 psk
.proto
= pd
->proto
;
4629 psk
.direction
= direction
;
4631 if (pd
->proto
== IPPROTO_UDP
) {
4632 if (ntohs(pd
->hdr
.udp
->uh_sport
) == PF_IKE_PORT
&&
4633 ntohs(pd
->hdr
.udp
->uh_dport
) == PF_IKE_PORT
) {
4634 psk
.proto_variant
= PF_EXTFILTER_APD
;
4636 psk
.proto_variant
= nr
? nr
->extfilter
: r
->extfilter
;
4637 if (psk
.proto_variant
< PF_EXTFILTER_APD
)
4638 psk
.proto_variant
= PF_EXTFILTER_APD
;
4640 } else if (pd
->proto
== IPPROTO_GRE
) {
4641 psk
.proto_variant
= pd
->proto_variant
;
4643 if (direction
== PF_OUT
) {
4644 PF_ACPY(&psk
.gwy
.addr
, saddr
, af
);
4645 PF_ACPY(&psk
.ext
.addr
, daddr
, af
);
4646 switch (pd
->proto
) {
4648 psk
.gwy
.xport
= sxport
;
4649 psk
.ext
.xport
= dxport
;
4652 psk
.gwy
.xport
.spi
= 0;
4653 psk
.ext
.xport
.spi
= pd
->hdr
.esp
->spi
;
4657 case IPPROTO_ICMPV6
:
4659 psk
.gwy
.xport
.port
= nxport
.port
;
4660 psk
.ext
.xport
.spi
= 0;
4663 psk
.gwy
.xport
= sxport
;
4664 psk
.ext
.xport
= dxport
;
4668 PF_ACPY(&psk
.lan
.addr
, &pd
->baddr
, af
);
4669 psk
.lan
.xport
= bxport
;
4671 PF_ACPY(&psk
.lan
.addr
, &psk
.gwy
.addr
, af
);
4672 psk
.lan
.xport
= psk
.gwy
.xport
;
4675 PF_ACPY(&psk
.lan
.addr
, daddr
, af
);
4676 PF_ACPY(&psk
.ext
.addr
, saddr
, af
);
4677 switch (pd
->proto
) {
4680 case IPPROTO_ICMPV6
:
4682 psk
.lan
.xport
= nxport
;
4683 psk
.ext
.xport
.spi
= 0;
4686 psk
.ext
.xport
.spi
= 0;
4687 psk
.lan
.xport
.spi
= pd
->hdr
.esp
->spi
;
4690 psk
.lan
.xport
= dxport
;
4691 psk
.ext
.xport
= sxport
;
4695 PF_ACPY(&psk
.gwy
.addr
, &pd
->baddr
, af
);
4696 psk
.gwy
.xport
= bxport
;
4698 PF_ACPY(&psk
.gwy
.addr
, &psk
.lan
.addr
, af
);
4699 psk
.gwy
.xport
= psk
.lan
.xport
;
4702 if (pd
->flowhash
!= 0) {
4703 /* flowhash was already computed by upper layers */
4704 psk
.flowhash
= pd
->flowhash
;
4706 psk
.flowhash
= pf_calc_state_key_flowhash(&psk
);
4707 pd
->flowhash
= psk
.flowhash
;
4710 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
4711 REASON_SET(&reason
, PFRES_MEMORY
);
4715 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
4716 (pd
->flags
& PFDESC_TCP_NORM
))) {
4717 /* create new state */
4718 struct pf_state
*s
= NULL
;
4719 struct pf_state_key
*sk
= NULL
;
4720 struct pf_src_node
*sn
= NULL
;
4721 struct pf_ike_hdr ike
;
4723 if (pd
->proto
== IPPROTO_UDP
) {
4724 struct udphdr
*uh
= pd
->hdr
.udp
;
4725 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
4727 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
4728 ntohs(uh
->uh_dport
) == PF_IKE_PORT
&&
4729 plen
>= PF_IKE_PACKET_MINSIZE
) {
4730 if (plen
> PF_IKE_PACKET_MINSIZE
)
4731 plen
= PF_IKE_PACKET_MINSIZE
;
4732 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
4736 if (nr
!= NULL
&& pd
->proto
== IPPROTO_ESP
&&
4737 direction
== PF_OUT
) {
4738 struct pf_state_key_cmp sk0
;
4739 struct pf_state
*s0
;
4743 * This squelches state creation if the external
4744 * address matches an existing incomplete state with a
4745 * different internal address. Only one 'blocking'
4746 * partial state is allowed for each external address.
4748 memset(&sk0
, 0, sizeof (sk0
));
4750 sk0
.proto
= IPPROTO_ESP
;
4751 PF_ACPY(&sk0
.gwy
.addr
, saddr
, sk0
.af
);
4752 PF_ACPY(&sk0
.ext
.addr
, daddr
, sk0
.af
);
4753 s0
= pf_find_state(kif
, &sk0
, PF_IN
);
4755 if (s0
&& PF_ANEQ(&s0
->state_key
->lan
.addr
,
4762 /* check maximums */
4763 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
4764 pf_status
.lcounters
[LCNT_STATES
]++;
4765 REASON_SET(&reason
, PFRES_MAXSTATES
);
4768 /* src node for filter rule */
4769 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
4770 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4771 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
4772 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4775 /* src node for translation rule */
4776 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
4777 ((direction
== PF_OUT
&&
4778 nr
->action
!= PF_RDR
&&
4779 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
4780 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
4781 REASON_SET(&reason
, PFRES_SRCLIMIT
);
4784 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
4786 REASON_SET(&reason
, PFRES_MEMORY
);
4788 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
4789 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
4790 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4791 pf_status
.src_nodes
--;
4792 pool_put(&pf_src_tree_pl
, sn
);
4794 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
4796 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
4797 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
4798 pf_status
.src_nodes
--;
4799 pool_put(&pf_src_tree_pl
, nsn
);
4803 pool_put(&pf_app_state_pl
,
4805 pool_put(&pf_state_key_pl
, sk
);
4809 bzero(s
, sizeof (*s
));
4810 TAILQ_INIT(&s
->unlink_hooks
);
4812 s
->nat_rule
.ptr
= nr
;
4814 STATE_INC_COUNTERS(s
);
4815 s
->allow_opts
= r
->allow_opts
;
4816 s
->log
= r
->log
& PF_LOG_ALL
;
4818 s
->log
|= nr
->log
& PF_LOG_ALL
;
4819 switch (pd
->proto
) {
4821 s
->src
.seqlo
= ntohl(th
->th_seq
);
4822 s
->src
.seqhi
= s
->src
.seqlo
+ pd
->p_len
+ 1;
4823 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
4824 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
4825 /* Generate sequence number modulator */
4826 if ((s
->src
.seqdiff
= pf_tcp_iss(pd
) -
4829 pf_change_a(&th
->th_seq
, &th
->th_sum
,
4830 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
4831 rewrite
= off
+ sizeof (*th
);
4834 if (th
->th_flags
& TH_SYN
) {
4836 s
->src
.wscale
= pf_get_wscale(m
, off
,
4839 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
4840 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
4841 /* Remove scale factor from initial window */
4842 int win
= s
->src
.max_win
;
4843 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
4844 s
->src
.max_win
= (win
- 1) >>
4845 (s
->src
.wscale
& PF_WSCALE_MASK
);
4847 if (th
->th_flags
& TH_FIN
)
4851 s
->src
.state
= TCPS_SYN_SENT
;
4852 s
->dst
.state
= TCPS_CLOSED
;
4853 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
4856 s
->src
.state
= PFUDPS_SINGLE
;
4857 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
4858 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
4862 case IPPROTO_ICMPV6
:
4864 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
4867 s
->src
.state
= PFGRE1S_INITIATING
;
4868 s
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
4869 s
->timeout
= PFTM_GREv1_INITIATING
;
4872 s
->src
.state
= PFESPS_INITIATING
;
4873 s
->dst
.state
= PFESPS_NO_TRAFFIC
;
4874 s
->timeout
= PFTM_ESP_FIRST_PACKET
;
4877 s
->src
.state
= PFOTHERS_SINGLE
;
4878 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
4879 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
4882 s
->creation
= pf_time_second();
4883 s
->expire
= pf_time_second();
4887 s
->src_node
->states
++;
4888 VERIFY(s
->src_node
->states
!= 0);
4891 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
4892 s
->nat_src_node
= nsn
;
4893 s
->nat_src_node
->states
++;
4894 VERIFY(s
->nat_src_node
->states
!= 0);
4896 if (pd
->proto
== IPPROTO_TCP
) {
4897 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
4898 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
4900 REASON_SET(&reason
, PFRES_MEMORY
);
4901 pf_src_tree_remove_state(s
);
4902 STATE_DEC_COUNTERS(s
);
4903 pool_put(&pf_state_pl
, s
);
4906 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
4907 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
4908 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
4909 /* This really shouldn't happen!!! */
4910 DPFPRINTF(PF_DEBUG_URGENT
,
4911 ("pf_normalize_tcp_stateful failed on "
4913 pf_normalize_tcp_cleanup(s
);
4914 pf_src_tree_remove_state(s
);
4915 STATE_DEC_COUNTERS(s
);
4916 pool_put(&pf_state_pl
, s
);
4921 /* allocate state key and import values from psk */
4922 if ((sk
= pf_alloc_state_key(s
, &psk
)) == NULL
) {
4923 REASON_SET(&reason
, PFRES_MEMORY
);
4927 pf_set_rt_ifp(s
, saddr
); /* needs s->state_key set */
4931 if (sk
->app_state
== 0) {
4932 switch (pd
->proto
) {
4934 u_int16_t dport
= (direction
== PF_OUT
) ?
4935 sk
->ext
.xport
.port
: sk
->gwy
.xport
.port
;
4938 ntohs(dport
) == PF_PPTP_PORT
) {
4939 struct pf_app_state
*as
;
4941 as
= pool_get(&pf_app_state_pl
,
4949 bzero(as
, sizeof (*as
));
4950 as
->handler
= pf_pptp_handler
;
4951 as
->compare_lan_ext
= 0;
4952 as
->compare_ext_gwy
= 0;
4953 as
->u
.pptp
.grev1_state
= 0;
4955 (void) hook_establish(&s
->unlink_hooks
,
4956 0, (hook_fn_t
) pf_pptp_unlink
, s
);
4962 struct udphdr
*uh
= pd
->hdr
.udp
;
4965 ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
4966 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
4967 struct pf_app_state
*as
;
4969 as
= pool_get(&pf_app_state_pl
,
4977 bzero(as
, sizeof (*as
));
4978 as
->compare_lan_ext
= pf_ike_compare
;
4979 as
->compare_ext_gwy
= pf_ike_compare
;
4980 as
->u
.ike
.cookie
= ike
.initiator_cookie
;
4991 if (pf_insert_state(BOUND_IFACE(r
, kif
), s
)) {
4992 if (pd
->proto
== IPPROTO_TCP
)
4993 pf_normalize_tcp_cleanup(s
);
4994 REASON_SET(&reason
, PFRES_STATEINS
);
4995 pf_src_tree_remove_state(s
);
4996 STATE_DEC_COUNTERS(s
);
4997 pool_put(&pf_state_pl
, s
);
5005 if (pd
->proto
== IPPROTO_TCP
&&
5006 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
5007 r
->keep_state
== PF_STATE_SYNPROXY
) {
5008 s
->src
.state
= PF_TCPS_PROXY_SRC
;
5010 if (direction
== PF_OUT
) {
5011 pf_change_ap(direction
, pd
->mp
, saddr
,
5012 &th
->th_sport
, pd
->ip_sum
,
5013 &th
->th_sum
, &pd
->baddr
,
5014 bxport
.port
, 0, af
);
5015 sxport
.port
= th
->th_sport
;
5017 pf_change_ap(direction
, pd
->mp
, daddr
,
5018 &th
->th_dport
, pd
->ip_sum
,
5019 &th
->th_sum
, &pd
->baddr
,
5020 bxport
.port
, 0, af
);
5021 sxport
.port
= th
->th_dport
;
5024 s
->src
.seqhi
= htonl(random());
5025 /* Find mss option */
5026 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
5027 mss
= pf_calc_mss(saddr
, af
, mss
);
5028 mss
= pf_calc_mss(daddr
, af
, mss
);
5030 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
5031 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5032 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
5033 REASON_SET(&reason
, PFRES_SYNPROXY
);
5034 return (PF_SYNPROXY_DROP
);
5037 if (sk
->app_state
&& sk
->app_state
->handler
) {
5040 switch (pd
->proto
) {
5042 offx
+= th
->th_off
<< 2;
5045 offx
+= pd
->hdr
.udp
->uh_ulen
<< 2;
5048 /* ALG handlers only apply to TCP and UDP rules */
5053 sk
->app_state
->handler(s
, direction
, offx
,
5056 REASON_SET(&reason
, PFRES_MEMORY
);
5064 /* copy back packet headers if we performed NAT operations */
5066 if (rewrite
< off
+ hdrlen
)
5067 rewrite
= off
+ hdrlen
;
5069 m
= pf_lazy_makewritable(pd
, pd
->mp
, rewrite
);
5071 REASON_SET(&reason
, PFRES_MEMORY
);
5075 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5083 * When pf_test_dummynet() returns PF_PASS, the rule matching parameter "rm"
5084 * remains unchanged, meaning the packet did not match a dummynet rule.
5085 * when the packet does match a dummynet rule, pf_test_dummynet() returns
5086 * PF_PASS and zero out the mbuf rule as the packet is effectively siphoned
5090 pf_test_dummynet(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5091 struct mbuf
**m0
, struct pf_pdesc
*pd
, struct ip_fw_args
*fwa
)
5093 struct mbuf
*m
= *m0
;
5094 struct pf_rule
*am
= NULL
;
5095 struct pf_ruleset
*rsm
= NULL
;
5096 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
5097 sa_family_t af
= pd
->af
;
5098 struct pf_rule
*r
, *a
= NULL
;
5099 struct pf_ruleset
*ruleset
= NULL
;
5100 struct tcphdr
*th
= pd
->hdr
.tcp
;
5104 unsigned int rtableid
= IFSCOPE_NONE
;
5107 u_int8_t icmptype
= 0, icmpcode
= 0;
5108 struct ip_fw_args dnflow
;
5109 struct pf_rule
*prev_matching_rule
= fwa
? fwa
->fwa_pf_rule
: NULL
;
5110 int found_prev_rule
= (prev_matching_rule
) ? 0 : 1;
5112 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
5114 if (!DUMMYNET_LOADED
)
5117 if (TAILQ_EMPTY(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
))
5120 bzero(&dnflow
, sizeof(dnflow
));
5124 /* Fragments don't gave protocol headers */
5125 if (!(pd
->flags
& PFDESC_IP_FRAG
))
5126 switch (pd
->proto
) {
5128 dnflow
.fwa_id
.flags
= pd
->hdr
.tcp
->th_flags
;
5129 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.tcp
->th_dport
);
5130 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.tcp
->th_sport
);
5131 hdrlen
= sizeof (*th
);
5134 dnflow
.fwa_id
.dst_port
= ntohs(pd
->hdr
.udp
->uh_dport
);
5135 dnflow
.fwa_id
.src_port
= ntohs(pd
->hdr
.udp
->uh_sport
);
5136 hdrlen
= sizeof (*pd
->hdr
.udp
);
5142 hdrlen
= ICMP_MINLEN
;
5143 icmptype
= pd
->hdr
.icmp
->icmp_type
;
5144 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
5148 case IPPROTO_ICMPV6
:
5151 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
5152 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
5153 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
5157 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
5158 hdrlen
= sizeof (*pd
->hdr
.grev1
);
5161 hdrlen
= sizeof (*pd
->hdr
.esp
);
5165 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
);
5169 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5170 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5171 else if (r
->direction
&& r
->direction
!= direction
)
5172 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5173 else if (r
->af
&& r
->af
!= af
)
5174 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5175 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5176 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5177 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
5179 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5180 /* tcp/udp only. port_op always 0 in other cases */
5181 else if (r
->proto
== pd
->proto
&&
5182 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5183 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5184 ((r
->src
.xport
.range
.op
&&
5185 !pf_match_port(r
->src
.xport
.range
.op
,
5186 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
5188 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
5189 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
5191 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5192 /* tcp/udp only. port_op always 0 in other cases */
5193 else if (r
->proto
== pd
->proto
&&
5194 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5195 r
->dst
.xport
.range
.op
&&
5196 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5197 !pf_match_port(r
->dst
.xport
.range
.op
,
5198 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
5200 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
5201 /* icmp only. type always 0 in other cases */
5203 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5204 r
->type
!= icmptype
+ 1))
5205 r
= TAILQ_NEXT(r
, entries
);
5206 /* icmp only. type always 0 in other cases */
5208 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5209 r
->code
!= icmpcode
+ 1))
5210 r
= TAILQ_NEXT(r
, entries
);
5211 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5212 r
= TAILQ_NEXT(r
, entries
);
5213 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
5214 r
= TAILQ_NEXT(r
, entries
);
5215 else if (pd
->proto
== IPPROTO_TCP
&&
5216 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5217 (r
->flagset
& th
->th_flags
) != r
->flags
))
5218 r
= TAILQ_NEXT(r
, entries
);
5219 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5220 r
= TAILQ_NEXT(r
, entries
);
5223 * Need to go past the previous dummynet matching rule
5225 if (r
->anchor
== NULL
) {
5226 if (found_prev_rule
) {
5229 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
5230 rtableid
= r
->rtableid
;
5237 } else if (r
== prev_matching_rule
) {
5238 found_prev_rule
= 1;
5240 r
= TAILQ_NEXT(r
, entries
);
5242 pf_step_into_anchor(&asd
, &ruleset
,
5243 PF_RULESET_DUMMYNET
, &r
, &a
, &match
);
5246 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5247 PF_RULESET_DUMMYNET
, &r
, &a
, &match
))
5257 REASON_SET(&reason
, PFRES_DUMMYNET
);
5260 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
,
5264 if (r
->action
== PF_NODUMMYNET
) {
5265 int dirndx
= (direction
== PF_OUT
);
5267 r
->packets
[dirndx
]++;
5268 r
->bytes
[dirndx
] += pd
->tot_len
;
5272 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5273 REASON_SET(&reason
, PFRES_MEMORY
);
5278 if (r
->dnpipe
&& ip_dn_io_ptr
!= NULL
) {
5279 int dirndx
= (direction
== PF_OUT
);
5281 r
->packets
[dirndx
]++;
5282 r
->bytes
[dirndx
] += pd
->tot_len
;
5284 dnflow
.fwa_cookie
= r
->dnpipe
;
5285 dnflow
.fwa_pf_rule
= r
;
5286 dnflow
.fwa_id
.proto
= pd
->proto
;
5287 dnflow
.fwa_flags
= r
->dntype
;
5290 dnflow
.fwa_id
.addr_type
= 4;
5291 dnflow
.fwa_id
.src_ip
= ntohl(saddr
->v4
.s_addr
);
5292 dnflow
.fwa_id
.dst_ip
= ntohl(daddr
->v4
.s_addr
);
5295 dnflow
.fwa_id
.addr_type
= 6;
5296 dnflow
.fwa_id
.src_ip6
= saddr
->v6
;
5297 dnflow
.fwa_id
.dst_ip6
= saddr
->v6
;
5302 dnflow
.fwa_oif
= fwa
->fwa_oif
;
5303 dnflow
.fwa_oflags
= fwa
->fwa_oflags
;
5305 * Note that fwa_ro, fwa_dst and fwa_ipoa are
5306 * actually in a union so the following does work
5307 * for both IPv4 and IPv6
5309 dnflow
.fwa_ro
= fwa
->fwa_ro
;
5310 dnflow
.fwa_dst
= fwa
->fwa_dst
;
5311 dnflow
.fwa_ipoa
= fwa
->fwa_ipoa
;
5312 dnflow
.fwa_ro6_pmtu
= fwa
->fwa_ro6_pmtu
;
5313 dnflow
.fwa_origifp
= fwa
->fwa_origifp
;
5314 dnflow
.fwa_mtu
= fwa
->fwa_mtu
;
5315 dnflow
.fwa_alwaysfrag
= fwa
->fwa_alwaysfrag
;
5316 dnflow
.fwa_unfragpartlen
= fwa
->fwa_unfragpartlen
;
5317 dnflow
.fwa_exthdrs
= fwa
->fwa_exthdrs
;
5320 if (af
== AF_INET
) {
5321 struct ip
*iphdr
= mtod(m
, struct ip
*);
5322 NTOHS(iphdr
->ip_len
);
5323 NTOHS(iphdr
->ip_off
);
5326 * Don't need to unlock pf_lock as NET_THREAD_HELD_PF
5327 * allows for recursive behavior
5332 direction
== PF_IN
? DN_TO_IP_IN
: DN_TO_IP_OUT
:
5333 direction
== PF_IN
? DN_TO_IP6_IN
: DN_TO_IP6_OUT
,
5334 &dnflow
, DN_CLIENT_PF
);
5337 * The packet is siphoned out by dummynet so return a NULL
5338 * mbuf so the caller can still return success.
5347 #endif /* DUMMYNET */
5350 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5351 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
5352 struct pf_ruleset
**rsm
)
5355 struct pf_rule
*r
, *a
= NULL
;
5356 struct pf_ruleset
*ruleset
= NULL
;
5357 sa_family_t af
= pd
->af
;
5363 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
5366 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5367 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5368 else if (r
->direction
&& r
->direction
!= direction
)
5369 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5370 else if (r
->af
&& r
->af
!= af
)
5371 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5372 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5373 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5374 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
5376 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5377 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
5379 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5380 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
5381 !(r
->tos
& pd
->tos
))
5382 r
= TAILQ_NEXT(r
, entries
);
5383 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
5384 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
5385 r
= TAILQ_NEXT(r
, entries
);
5386 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
5387 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
5388 r
= TAILQ_NEXT(r
, entries
);
5389 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
5390 r
= TAILQ_NEXT(r
, entries
);
5391 else if (pd
->proto
== IPPROTO_UDP
&&
5392 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
5393 r
= TAILQ_NEXT(r
, entries
);
5394 else if (pd
->proto
== IPPROTO_TCP
&&
5395 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
5397 r
= TAILQ_NEXT(r
, entries
);
5398 else if ((pd
->proto
== IPPROTO_ICMP
||
5399 pd
->proto
== IPPROTO_ICMPV6
) &&
5400 (r
->type
|| r
->code
))
5401 r
= TAILQ_NEXT(r
, entries
);
5402 else if (r
->prob
&& r
->prob
<= (random() % (UINT_MAX
- 1) + 1))
5403 r
= TAILQ_NEXT(r
, entries
);
5404 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5405 r
= TAILQ_NEXT(r
, entries
);
5407 if (r
->anchor
== NULL
) {
5414 r
= TAILQ_NEXT(r
, entries
);
5416 pf_step_into_anchor(&asd
, &ruleset
,
5417 PF_RULESET_FILTER
, &r
, &a
, &match
);
5419 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5420 PF_RULESET_FILTER
, &r
, &a
, &match
))
5427 REASON_SET(&reason
, PFRES_MATCH
);
5430 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
5433 if (r
->action
!= PF_PASS
)
5436 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1, NULL
)) {
5437 REASON_SET(&reason
, PFRES_MEMORY
);
5445 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
5446 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
5448 #pragma unused(direction)
5450 struct pf_pptp_state
*pptps
;
5451 struct pf_pptp_ctrl_msg cm
;
5453 struct pf_state
*gs
;
5455 u_int16_t
*pac_call_id
;
5456 u_int16_t
*pns_call_id
;
5457 u_int16_t
*spoof_call_id
;
5458 u_int8_t
*pac_state
;
5459 u_int8_t
*pns_state
;
5460 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
5462 struct pf_state_key
*sk
;
5463 struct pf_state_key
*gsk
;
5464 struct pf_app_state
*gas
;
5467 pptps
= &sk
->app_state
->u
.pptp
;
5468 gs
= pptps
->grev1_state
;
5471 gs
->expire
= pf_time_second();
5474 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
5475 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
5478 m_copydata(m
, off
, plen
, &cm
);
5480 if (ntohl(cm
.hdr
.magic
) != PF_PPTP_MAGIC_NUMBER
)
5482 if (ntohs(cm
.hdr
.type
) != 1)
5486 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
5490 memcpy(gs
, s
, sizeof (*gs
));
5492 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
5493 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
5495 TAILQ_INIT(&gs
->unlink_hooks
);
5498 gs
->pfsync_time
= 0;
5499 gs
->packets
[0] = gs
->packets
[1] = 0;
5500 gs
->bytes
[0] = gs
->bytes
[1] = 0;
5501 gs
->timeout
= PFTM_UNLINKED
;
5502 gs
->id
= gs
->creatorid
= 0;
5503 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5504 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
5506 gas
= pool_get(&pf_app_state_pl
, PR_NOWAIT
);
5508 pool_put(&pf_state_pl
, gs
);
5512 gsk
= pf_alloc_state_key(gs
, NULL
);
5514 pool_put(&pf_app_state_pl
, gas
);
5515 pool_put(&pf_state_pl
, gs
);
5519 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
5520 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
5521 memcpy(&gsk
->ext
, &sk
->ext
, sizeof (gsk
->ext
));
5523 gsk
->proto
= IPPROTO_GRE
;
5524 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
5525 gsk
->app_state
= gas
;
5526 gsk
->lan
.xport
.call_id
= 0;
5527 gsk
->gwy
.xport
.call_id
= 0;
5528 gsk
->ext
.xport
.call_id
= 0;
5529 gsk
->flowhash
= pf_calc_state_key_flowhash(gsk
);
5530 memset(gas
, 0, sizeof (*gas
));
5531 gas
->u
.grev1
.pptp_state
= s
;
5532 STATE_INC_COUNTERS(gs
);
5533 pptps
->grev1_state
= gs
;
5534 (void) hook_establish(&gs
->unlink_hooks
, 0,
5535 (hook_fn_t
) pf_grev1_unlink
, gs
);
5537 gsk
= gs
->state_key
;
5540 switch (sk
->direction
) {
5542 pns_call_id
= &gsk
->ext
.xport
.call_id
;
5543 pns_state
= &gs
->dst
.state
;
5544 pac_call_id
= &gsk
->lan
.xport
.call_id
;
5545 pac_state
= &gs
->src
.state
;
5549 pns_call_id
= &gsk
->lan
.xport
.call_id
;
5550 pns_state
= &gs
->src
.state
;
5551 pac_call_id
= &gsk
->ext
.xport
.call_id
;
5552 pac_state
= &gs
->dst
.state
;
5556 DPFPRINTF(PF_DEBUG_URGENT
,
5557 ("pf_pptp_handler: bad directional!\n"));
5564 ct
= ntohs(cm
.ctrl
.type
);
5567 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
5568 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
5569 *pns_state
= PFGRE1S_INITIATING
;
5570 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5571 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
5574 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
5575 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
5576 if (s
->nat_rule
.ptr
)
5578 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5579 &cm
.msg
.call_out_rpy
.call_id
:
5580 &cm
.msg
.call_out_rpy
.peer_call_id
;
5581 if (gs
->timeout
== PFTM_UNLINKED
) {
5582 *pac_state
= PFGRE1S_INITIATING
;
5583 op
= PF_PPTP_INSERT_GRE
;
5587 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
5588 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
5589 *pns_state
= PFGRE1S_INITIATING
;
5590 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5591 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
5594 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
5595 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
5596 *pac_state
= PFGRE1S_INITIATING
;
5597 if (s
->nat_rule
.ptr
)
5599 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5600 &cm
.msg
.call_in_2nd
.call_id
:
5601 &cm
.msg
.call_in_2nd
.peer_call_id
;
5604 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
5605 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5606 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
5607 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
5610 if (gs
->timeout
== PFTM_UNLINKED
)
5611 op
= PF_PPTP_INSERT_GRE
;
5614 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
5615 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
5616 op
= PF_PPTP_REMOVE_GRE
;
5619 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
5620 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
5621 op
= PF_PPTP_REMOVE_GRE
;
5624 case PF_PPTP_CTRL_TYPE_ERROR
:
5625 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5626 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
5629 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
5630 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
5631 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
5639 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
5640 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
5641 if (spoof_call_id
) {
5642 u_int16_t call_id
= 0;
5644 struct pf_state_key_cmp key
;
5647 key
.proto
= IPPROTO_GRE
;
5648 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
5649 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af
);
5650 PF_ACPY(&key
.ext
.addr
, &gsk
->ext
.addr
, key
.af
);
5651 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
5652 key
.ext
.xport
.call_id
= gsk
->ext
.xport
.call_id
;
5654 call_id
= htonl(random());
5657 while (pf_find_state_all(&key
, PF_IN
, 0)) {
5658 call_id
= ntohs(call_id
);
5660 if (--call_id
== 0) call_id
= 0xffff;
5661 call_id
= htons(call_id
);
5663 key
.gwy
.xport
.call_id
= call_id
;
5666 DPFPRINTF(PF_DEBUG_URGENT
,
5667 ("pf_pptp_handler: failed to spoof "
5669 key
.gwy
.xport
.call_id
= 0;
5674 gsk
->gwy
.xport
.call_id
= call_id
;
5680 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
5681 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
5682 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
5683 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5684 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
5686 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
5687 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5688 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
5691 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
5693 pptps
->grev1_state
= NULL
;
5694 STATE_DEC_COUNTERS(gs
);
5695 pool_put(&pf_state_pl
, gs
);
5698 m_copyback(m
, off
, plen
, &cm
);
5702 case PF_PPTP_REMOVE_GRE
:
5703 gs
->timeout
= PFTM_PURGE
;
5704 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5705 gsk
->lan
.xport
.call_id
= 0;
5706 gsk
->gwy
.xport
.call_id
= 0;
5707 gsk
->ext
.xport
.call_id
= 0;
5708 gs
->id
= gs
->creatorid
= 0;
5711 case PF_PPTP_INSERT_GRE
:
5712 gs
->creation
= pf_time_second();
5713 gs
->expire
= pf_time_second();
5714 gs
->timeout
= PFTM_TCP_ESTABLISHED
;
5715 if (gs
->src_node
!= NULL
) {
5716 ++gs
->src_node
->states
;
5717 VERIFY(gs
->src_node
->states
!= 0);
5719 if (gs
->nat_src_node
!= NULL
) {
5720 ++gs
->nat_src_node
->states
;
5721 VERIFY(gs
->nat_src_node
->states
!= 0);
5723 pf_set_rt_ifp(gs
, &sk
->lan
.addr
);
5724 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
5728 * FIX ME: insertion can fail when multiple PNS
5729 * behind the same NAT open calls to the same PAC
5730 * simultaneously because spoofed call ID numbers
5731 * are chosen before states are inserted. This is
5732 * hard to fix and happens infrequently enough that
5733 * users will normally try again and this ALG will
5734 * succeed. Failures are expected to be rare enough
5735 * that fixing this is a low priority.
5737 pptps
->grev1_state
= NULL
;
5738 pd
->lmw
= -1; /* Force PF_DROP on PFRES_MEMORY */
5739 pf_src_tree_remove_state(gs
);
5740 STATE_DEC_COUNTERS(gs
);
5741 pool_put(&pf_state_pl
, gs
);
5742 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
5743 "inserting GREv1 state.\n"));
5753 pf_pptp_unlink(struct pf_state
*s
)
5755 struct pf_app_state
*as
= s
->state_key
->app_state
;
5756 struct pf_state
*grev1s
= as
->u
.pptp
.grev1_state
;
5759 struct pf_app_state
*gas
= grev1s
->state_key
->app_state
;
5761 if (grev1s
->timeout
< PFTM_MAX
)
5762 grev1s
->timeout
= PFTM_PURGE
;
5763 gas
->u
.grev1
.pptp_state
= NULL
;
5764 as
->u
.pptp
.grev1_state
= NULL
;
5769 pf_grev1_unlink(struct pf_state
*s
)
5771 struct pf_app_state
*as
= s
->state_key
->app_state
;
5772 struct pf_state
*pptps
= as
->u
.grev1
.pptp_state
;
5775 struct pf_app_state
*pas
= pptps
->state_key
->app_state
;
5777 pas
->u
.pptp
.grev1_state
= NULL
;
5778 as
->u
.grev1
.pptp_state
= NULL
;
5783 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
5785 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
5786 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
5790 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
5791 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
5795 struct pf_state_key_cmp key
;
5796 struct tcphdr
*th
= pd
->hdr
.tcp
;
5797 u_int16_t win
= ntohs(th
->th_win
);
5798 u_int32_t ack
, end
, seq
, orig_seq
;
5802 struct pf_state_peer
*src
, *dst
;
5806 key
.proto
= IPPROTO_TCP
;
5807 if (direction
== PF_IN
) {
5808 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
5809 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
5810 key
.ext
.xport
.port
= th
->th_sport
;
5811 key
.gwy
.xport
.port
= th
->th_dport
;
5813 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
5814 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
5815 key
.lan
.xport
.port
= th
->th_sport
;
5816 key
.ext
.xport
.port
= th
->th_dport
;
5821 if (direction
== (*state
)->state_key
->direction
) {
5822 src
= &(*state
)->src
;
5823 dst
= &(*state
)->dst
;
5825 src
= &(*state
)->dst
;
5826 dst
= &(*state
)->src
;
5829 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
5830 if (direction
!= (*state
)->state_key
->direction
) {
5831 REASON_SET(reason
, PFRES_SYNPROXY
);
5832 return (PF_SYNPROXY_DROP
);
5834 if (th
->th_flags
& TH_SYN
) {
5835 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
5836 REASON_SET(reason
, PFRES_SYNPROXY
);
5839 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5840 pd
->src
, th
->th_dport
, th
->th_sport
,
5841 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5842 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
5844 REASON_SET(reason
, PFRES_SYNPROXY
);
5845 return (PF_SYNPROXY_DROP
);
5846 } else if (!(th
->th_flags
& TH_ACK
) ||
5847 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5848 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5849 REASON_SET(reason
, PFRES_SYNPROXY
);
5851 } else if ((*state
)->src_node
!= NULL
&&
5852 pf_src_connlimit(state
)) {
5853 REASON_SET(reason
, PFRES_SRCLIMIT
);
5856 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
5858 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
5859 struct pf_state_host
*psrc
, *pdst
;
5861 if (direction
== PF_OUT
) {
5862 psrc
= &(*state
)->state_key
->gwy
;
5863 pdst
= &(*state
)->state_key
->ext
;
5865 psrc
= &(*state
)->state_key
->ext
;
5866 pdst
= &(*state
)->state_key
->lan
;
5868 if (direction
== (*state
)->state_key
->direction
) {
5869 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
5870 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5871 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5872 REASON_SET(reason
, PFRES_SYNPROXY
);
5875 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5876 if ((*state
)->dst
.seqhi
== 1)
5877 (*state
)->dst
.seqhi
= htonl(random());
5878 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5879 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5880 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
5881 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
5882 REASON_SET(reason
, PFRES_SYNPROXY
);
5883 return (PF_SYNPROXY_DROP
);
5884 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
5886 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
5887 REASON_SET(reason
, PFRES_SYNPROXY
);
5890 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
5891 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
5892 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5893 pd
->src
, th
->th_dport
, th
->th_sport
,
5894 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
5895 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
5896 (*state
)->tag
, NULL
, NULL
);
5897 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5898 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5899 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
5900 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
5902 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
5903 (*state
)->src
.seqlo
;
5904 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
5905 (*state
)->dst
.seqlo
;
5906 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
5907 (*state
)->dst
.max_win
;
5908 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
5909 (*state
)->src
.max_win
;
5910 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
5911 (*state
)->src
.state
= (*state
)->dst
.state
=
5913 REASON_SET(reason
, PFRES_SYNPROXY
);
5914 return (PF_SYNPROXY_DROP
);
5918 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
5919 dst
->state
>= TCPS_FIN_WAIT_2
&&
5920 src
->state
>= TCPS_FIN_WAIT_2
) {
5921 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
5922 printf("pf: state reuse ");
5923 pf_print_state(*state
);
5924 pf_print_flags(th
->th_flags
);
5927 /* XXX make sure it's the same direction ?? */
5928 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
5929 pf_unlink_state(*state
);
5934 if (src
->wscale
&& dst
->wscale
&& !(th
->th_flags
& TH_SYN
)) {
5935 sws
= src
->wscale
& PF_WSCALE_MASK
;
5936 dws
= dst
->wscale
& PF_WSCALE_MASK
;
5941 * Sequence tracking algorithm from Guido van Rooij's paper:
5942 * http://www.madison-gurkha.com/publications/tcp_filtering/
5946 orig_seq
= seq
= ntohl(th
->th_seq
);
5947 if (src
->seqlo
== 0) {
5948 /* First packet from this end. Set its state */
5950 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
5951 src
->scrub
== NULL
) {
5952 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
5953 REASON_SET(reason
, PFRES_MEMORY
);
5958 /* Deferred generation of sequence number modulator */
5959 if (dst
->seqdiff
&& !src
->seqdiff
) {
5960 /* use random iss for the TCP server */
5961 while ((src
->seqdiff
= random() - seq
) == 0)
5963 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
5964 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
5966 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
5967 copyback
= off
+ sizeof (*th
);
5969 ack
= ntohl(th
->th_ack
);
5972 end
= seq
+ pd
->p_len
;
5973 if (th
->th_flags
& TH_SYN
) {
5975 if (dst
->wscale
& PF_WSCALE_FLAG
) {
5976 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
5978 if (src
->wscale
& PF_WSCALE_FLAG
) {
5980 * Remove scale factor from initial
5983 sws
= src
->wscale
& PF_WSCALE_MASK
;
5984 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
5986 dws
= dst
->wscale
& PF_WSCALE_MASK
;
5989 * Window scale negotiation has failed,
5990 * therefore we must restore the window
5991 * scale in the state record that we
5992 * optimistically removed in
5993 * pf_test_rule(). Care is required to
5994 * prevent arithmetic overflow from
5995 * zeroing the window when it's
5996 * truncated down to 16-bits.
5998 u_int32_t max_win
= dst
->max_win
;
6000 dst
->wscale
& PF_WSCALE_MASK
;
6001 dst
->max_win
= MIN(0xffff, max_win
);
6002 /* in case of a retrans SYN|ACK */
6007 if (th
->th_flags
& TH_FIN
)
6011 if (src
->state
< TCPS_SYN_SENT
)
6012 src
->state
= TCPS_SYN_SENT
;
6015 * May need to slide the window (seqhi may have been set by
6016 * the crappy stack check or if we picked up the connection
6017 * after establishment)
6019 if (src
->seqhi
== 1 ||
6020 SEQ_GEQ(end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
),
6022 src
->seqhi
= end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
);
6023 if (win
> src
->max_win
)
6027 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6029 /* Modulate sequence numbers */
6030 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6032 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6033 copyback
= off
+ sizeof (*th
);
6035 end
= seq
+ pd
->p_len
;
6036 if (th
->th_flags
& TH_SYN
)
6038 if (th
->th_flags
& TH_FIN
)
6042 if ((th
->th_flags
& TH_ACK
) == 0) {
6043 /* Let it pass through the ack skew check */
6045 } else if ((ack
== 0 &&
6046 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6047 /* broken tcp stacks do not set ack */
6048 (dst
->state
< TCPS_SYN_SENT
)) {
6050 * Many stacks (ours included) will set the ACK number in an
6051 * FIN|ACK if the SYN times out -- no sequence to ACK.
6057 /* Ease sequencing restrictions on no data packets */
6062 ackskew
= dst
->seqlo
- ack
;
6066 * Need to demodulate the sequence numbers in any TCP SACK options
6067 * (Selective ACK). We could optionally validate the SACK values
6068 * against the current ACK window, either forwards or backwards, but
6069 * I'm not confident that SACK has been implemented properly
6070 * everywhere. It wouldn't surprise me if several stacks accidently
6071 * SACK too far backwards of previously ACKed data. There really aren't
6072 * any security implications of bad SACKing unless the target stack
6073 * doesn't validate the option length correctly. Someone trying to
6074 * spoof into a TCP connection won't bother blindly sending SACK
6077 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6078 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6079 if (copyback
== -1) {
6080 REASON_SET(reason
, PFRES_MEMORY
);
6088 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6089 if (SEQ_GEQ(src
->seqhi
, end
) &&
6090 /* Last octet inside other's window space */
6091 SEQ_GEQ(seq
, src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) &&
6092 /* Retrans: not more than one window back */
6093 (ackskew
>= -MAXACKWINDOW
) &&
6094 /* Acking not more than one reassembled fragment backwards */
6095 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6096 /* Acking not more than one window forward */
6097 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6098 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6099 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6100 /* Require an exact/+1 sequence match on resets when possible */
6102 if (dst
->scrub
|| src
->scrub
) {
6103 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6104 *state
, src
, dst
, ©back
))
6110 /* update max window */
6111 if (src
->max_win
< win
)
6113 /* synchronize sequencing */
6114 if (SEQ_GT(end
, src
->seqlo
))
6116 /* slide the window of what the other end can send */
6117 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6118 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6121 if (th
->th_flags
& TH_SYN
)
6122 if (src
->state
< TCPS_SYN_SENT
)
6123 src
->state
= TCPS_SYN_SENT
;
6124 if (th
->th_flags
& TH_FIN
)
6125 if (src
->state
< TCPS_CLOSING
)
6126 src
->state
= TCPS_CLOSING
;
6127 if (th
->th_flags
& TH_ACK
) {
6128 if (dst
->state
== TCPS_SYN_SENT
) {
6129 dst
->state
= TCPS_ESTABLISHED
;
6130 if (src
->state
== TCPS_ESTABLISHED
&&
6131 (*state
)->src_node
!= NULL
&&
6132 pf_src_connlimit(state
)) {
6133 REASON_SET(reason
, PFRES_SRCLIMIT
);
6136 } else if (dst
->state
== TCPS_CLOSING
)
6137 dst
->state
= TCPS_FIN_WAIT_2
;
6139 if (th
->th_flags
& TH_RST
)
6140 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6142 /* update expire time */
6143 (*state
)->expire
= pf_time_second();
6144 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6145 dst
->state
>= TCPS_FIN_WAIT_2
)
6146 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6147 else if (src
->state
>= TCPS_CLOSING
&&
6148 dst
->state
>= TCPS_CLOSING
)
6149 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6150 else if (src
->state
< TCPS_ESTABLISHED
||
6151 dst
->state
< TCPS_ESTABLISHED
)
6152 (*state
)->timeout
= PFTM_TCP_OPENING
;
6153 else if (src
->state
>= TCPS_CLOSING
||
6154 dst
->state
>= TCPS_CLOSING
)
6155 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6157 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6159 /* Fall through to PASS packet */
6161 } else if ((dst
->state
< TCPS_SYN_SENT
||
6162 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6163 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6164 /* Within a window forward of the originating packet */
6165 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6166 /* Within a window backward of the originating packet */
6169 * This currently handles three situations:
6170 * 1) Stupid stacks will shotgun SYNs before their peer
6172 * 2) When PF catches an already established stream (the
6173 * firewall rebooted, the state table was flushed, routes
6175 * 3) Packets get funky immediately after the connection
6176 * closes (this should catch Solaris spurious ACK|FINs
6177 * that web servers like to spew after a close)
6179 * This must be a little more careful than the above code
6180 * since packet floods will also be caught here. We don't
6181 * update the TTL here to mitigate the damage of a packet
6182 * flood and so the same code can handle awkward establishment
6183 * and a loosened connection close.
6184 * In the establishment case, a correct peer response will
6185 * validate the connection, go through the normal state code
6186 * and keep updating the state TTL.
6189 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6190 printf("pf: loose state match: ");
6191 pf_print_state(*state
);
6192 pf_print_flags(th
->th_flags
);
6193 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6194 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6195 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6196 (*state
)->packets
[1],
6197 direction
== PF_IN
? "in" : "out",
6198 direction
== (*state
)->state_key
->direction
?
6202 if (dst
->scrub
|| src
->scrub
) {
6203 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6204 *state
, src
, dst
, ©back
))
6209 /* update max window */
6210 if (src
->max_win
< win
)
6212 /* synchronize sequencing */
6213 if (SEQ_GT(end
, src
->seqlo
))
6215 /* slide the window of what the other end can send */
6216 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6217 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6220 * Cannot set dst->seqhi here since this could be a shotgunned
6221 * SYN and not an already established connection.
6224 if (th
->th_flags
& TH_FIN
)
6225 if (src
->state
< TCPS_CLOSING
)
6226 src
->state
= TCPS_CLOSING
;
6227 if (th
->th_flags
& TH_RST
)
6228 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6230 /* Fall through to PASS packet */
6233 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
6234 (*state
)->src
.state
== TCPS_SYN_SENT
) {
6235 /* Send RST for state mismatches during handshake */
6236 if (!(th
->th_flags
& TH_RST
))
6237 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6238 pd
->dst
, pd
->src
, th
->th_dport
,
6239 th
->th_sport
, ntohl(th
->th_ack
), 0,
6241 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
6242 pd
->eh
, kif
->pfik_ifp
);
6246 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6247 printf("pf: BAD state: ");
6248 pf_print_state(*state
);
6249 pf_print_flags(th
->th_flags
);
6250 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6251 "pkts=%llu:%llu dir=%s,%s\n",
6252 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
6253 (*state
)->packets
[0], (*state
)->packets
[1],
6254 direction
== PF_IN
? "in" : "out",
6255 direction
== (*state
)->state_key
->direction
?
6257 printf("pf: State failure on: %c %c %c %c | %c %c\n",
6258 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
6260 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) ?
6262 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
6263 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
6264 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
6265 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
6267 REASON_SET(reason
, PFRES_BADSTATE
);
6271 /* Any packets which have gotten here are to be passed */
6273 if ((*state
)->state_key
->app_state
&&
6274 (*state
)->state_key
->app_state
->handler
) {
6275 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6276 off
+ (th
->th_off
<< 2), pd
, kif
);
6278 REASON_SET(reason
, PFRES_MEMORY
);
6284 /* translate source/destination address, if necessary */
6285 if (STATE_TRANSLATE((*state
)->state_key
)) {
6286 if (direction
== PF_OUT
)
6287 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
6288 pd
->ip_sum
, &th
->th_sum
,
6289 &(*state
)->state_key
->gwy
.addr
,
6290 (*state
)->state_key
->gwy
.xport
.port
, 0, pd
->af
);
6292 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &th
->th_dport
,
6293 pd
->ip_sum
, &th
->th_sum
,
6294 &(*state
)->state_key
->lan
.addr
,
6295 (*state
)->state_key
->lan
.xport
.port
, 0, pd
->af
);
6296 copyback
= off
+ sizeof (*th
);
6300 m
= pf_lazy_makewritable(pd
, m
, copyback
);
6302 REASON_SET(reason
, PFRES_MEMORY
);
6306 /* Copyback sequence modulation or stateful scrub changes */
6307 m_copyback(m
, off
, sizeof (*th
), th
);
6314 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6315 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6318 struct pf_state_peer
*src
, *dst
;
6319 struct pf_state_key_cmp key
;
6320 struct udphdr
*uh
= pd
->hdr
.udp
;
6321 struct pf_app_state as
;
6322 int dx
, action
, extfilter
;
6324 key
.proto_variant
= PF_EXTFILTER_APD
;
6327 key
.proto
= IPPROTO_UDP
;
6328 if (direction
== PF_IN
) {
6329 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6330 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6331 key
.ext
.xport
.port
= uh
->uh_sport
;
6332 key
.gwy
.xport
.port
= uh
->uh_dport
;
6335 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6336 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6337 key
.lan
.xport
.port
= uh
->uh_sport
;
6338 key
.ext
.xport
.port
= uh
->uh_dport
;
6342 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
6343 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
6344 struct pf_ike_hdr ike
;
6345 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
6346 if (plen
< PF_IKE_PACKET_MINSIZE
) {
6347 DPFPRINTF(PF_DEBUG_MISC
,
6348 ("pf: IKE message too small.\n"));
6352 if (plen
> sizeof (ike
))
6353 plen
= sizeof (ike
);
6354 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
6356 if (ike
.initiator_cookie
) {
6357 key
.app_state
= &as
;
6358 as
.compare_lan_ext
= pf_ike_compare
;
6359 as
.compare_ext_gwy
= pf_ike_compare
;
6360 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6363 * <http://tools.ietf.org/html/\
6364 * draft-ietf-ipsec-nat-t-ike-01>
6365 * Support non-standard NAT-T implementations that
6366 * push the ESP packet over the top of the IKE packet.
6367 * Do not drop packet.
6369 DPFPRINTF(PF_DEBUG_MISC
,
6370 ("pf: IKE initiator cookie = 0.\n"));
6374 *state
= pf_find_state(kif
, &key
, dx
);
6376 if (!key
.app_state
&& *state
== 0) {
6377 key
.proto_variant
= PF_EXTFILTER_AD
;
6378 *state
= pf_find_state(kif
, &key
, dx
);
6381 if (!key
.app_state
&& *state
== 0) {
6382 key
.proto_variant
= PF_EXTFILTER_EI
;
6383 *state
= pf_find_state(kif
, &key
, dx
);
6386 if ((*state
) != NULL
&& pd
!= NULL
&&
6388 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6390 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6393 if (direction
== (*state
)->state_key
->direction
) {
6394 src
= &(*state
)->src
;
6395 dst
= &(*state
)->dst
;
6397 src
= &(*state
)->dst
;
6398 dst
= &(*state
)->src
;
6402 if (src
->state
< PFUDPS_SINGLE
)
6403 src
->state
= PFUDPS_SINGLE
;
6404 if (dst
->state
== PFUDPS_SINGLE
)
6405 dst
->state
= PFUDPS_MULTIPLE
;
6407 /* update expire time */
6408 (*state
)->expire
= pf_time_second();
6409 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
6410 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
6412 (*state
)->timeout
= PFTM_UDP_SINGLE
;
6414 extfilter
= (*state
)->state_key
->proto_variant
;
6415 if (extfilter
> PF_EXTFILTER_APD
) {
6416 (*state
)->state_key
->ext
.xport
.port
= key
.ext
.xport
.port
;
6417 if (extfilter
> PF_EXTFILTER_AD
)
6418 PF_ACPY(&(*state
)->state_key
->ext
.addr
,
6419 &key
.ext
.addr
, key
.af
);
6422 if ((*state
)->state_key
->app_state
&&
6423 (*state
)->state_key
->app_state
->handler
) {
6424 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6425 off
+ uh
->uh_ulen
, pd
, kif
);
6427 REASON_SET(reason
, PFRES_MEMORY
);
6433 /* translate source/destination address, if necessary */
6434 if (STATE_TRANSLATE((*state
)->state_key
)) {
6435 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
6437 REASON_SET(reason
, PFRES_MEMORY
);
6441 if (direction
== PF_OUT
)
6442 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
6443 pd
->ip_sum
, &uh
->uh_sum
,
6444 &(*state
)->state_key
->gwy
.addr
,
6445 (*state
)->state_key
->gwy
.xport
.port
, 1, pd
->af
);
6447 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &uh
->uh_dport
,
6448 pd
->ip_sum
, &uh
->uh_sum
,
6449 &(*state
)->state_key
->lan
.addr
,
6450 (*state
)->state_key
->lan
.xport
.port
, 1, pd
->af
);
6451 m_copyback(m
, off
, sizeof (*uh
), uh
);
6458 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6459 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6462 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
6463 u_int16_t icmpid
= 0, *icmpsum
;
6466 struct pf_state_key_cmp key
;
6468 struct pf_app_state as
;
6471 switch (pd
->proto
) {
6474 icmptype
= pd
->hdr
.icmp
->icmp_type
;
6475 icmpid
= pd
->hdr
.icmp
->icmp_id
;
6476 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
6478 if (icmptype
== ICMP_UNREACH
||
6479 icmptype
== ICMP_SOURCEQUENCH
||
6480 icmptype
== ICMP_REDIRECT
||
6481 icmptype
== ICMP_TIMXCEED
||
6482 icmptype
== ICMP_PARAMPROB
)
6487 case IPPROTO_ICMPV6
:
6488 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
6489 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
6490 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
6492 if (icmptype
== ICMP6_DST_UNREACH
||
6493 icmptype
== ICMP6_PACKET_TOO_BIG
||
6494 icmptype
== ICMP6_TIME_EXCEEDED
||
6495 icmptype
== ICMP6_PARAM_PROB
)
6504 * ICMP query/reply message not related to a TCP/UDP packet.
6505 * Search for an ICMP state.
6508 key
.proto
= pd
->proto
;
6509 if (direction
== PF_IN
) {
6510 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6511 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6512 key
.ext
.xport
.port
= 0;
6513 key
.gwy
.xport
.port
= icmpid
;
6515 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6516 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6517 key
.lan
.xport
.port
= icmpid
;
6518 key
.ext
.xport
.port
= 0;
6523 (*state
)->expire
= pf_time_second();
6524 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
6526 /* translate source/destination address, if necessary */
6527 if (STATE_TRANSLATE((*state
)->state_key
)) {
6528 if (direction
== PF_OUT
) {
6532 pf_change_a(&saddr
->v4
.s_addr
,
6534 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
6535 pd
->hdr
.icmp
->icmp_cksum
=
6537 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6538 (*state
)->state_key
->gwy
.xport
.port
, 0);
6539 pd
->hdr
.icmp
->icmp_id
=
6540 (*state
)->state_key
->gwy
.xport
.port
;
6541 m
= pf_lazy_makewritable(pd
, m
,
6545 m_copyback(m
, off
, ICMP_MINLEN
,
6552 &pd
->hdr
.icmp6
->icmp6_cksum
,
6553 &(*state
)->state_key
->gwy
.addr
, 0);
6554 m
= pf_lazy_makewritable(pd
, m
,
6555 off
+ sizeof (struct icmp6_hdr
));
6559 sizeof (struct icmp6_hdr
),
6568 pf_change_a(&daddr
->v4
.s_addr
,
6570 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
6571 pd
->hdr
.icmp
->icmp_cksum
=
6573 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6574 (*state
)->state_key
->lan
.xport
.port
, 0);
6575 pd
->hdr
.icmp
->icmp_id
=
6576 (*state
)->state_key
->lan
.xport
.port
;
6577 m
= pf_lazy_makewritable(pd
, m
,
6581 m_copyback(m
, off
, ICMP_MINLEN
,
6588 &pd
->hdr
.icmp6
->icmp6_cksum
,
6589 &(*state
)->state_key
->lan
.addr
, 0);
6590 m
= pf_lazy_makewritable(pd
, m
,
6591 off
+ sizeof (struct icmp6_hdr
));
6595 sizeof (struct icmp6_hdr
),
6607 * ICMP error message in response to a TCP/UDP packet.
6608 * Extract the inner TCP/UDP header and search for that state.
6611 struct pf_pdesc pd2
;
6616 struct ip6_hdr h2_6
;
6622 memset(&pd2
, 0, sizeof (pd2
));
6628 /* offset of h2 in mbuf chain */
6629 ipoff2
= off
+ ICMP_MINLEN
;
6631 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
6632 NULL
, reason
, pd2
.af
)) {
6633 DPFPRINTF(PF_DEBUG_MISC
,
6634 ("pf: ICMP error message too short "
6639 * ICMP error messages don't refer to non-first
6642 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
6643 REASON_SET(reason
, PFRES_FRAG
);
6647 /* offset of protocol header that follows h2 */
6648 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
6650 pd2
.proto
= h2
.ip_p
;
6651 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
6652 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
6653 pd2
.ip_sum
= &h2
.ip_sum
;
6658 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
6660 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
6661 NULL
, reason
, pd2
.af
)) {
6662 DPFPRINTF(PF_DEBUG_MISC
,
6663 ("pf: ICMP error message too short "
6667 pd2
.proto
= h2_6
.ip6_nxt
;
6668 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
6669 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
6671 off2
= ipoff2
+ sizeof (h2_6
);
6673 switch (pd2
.proto
) {
6674 case IPPROTO_FRAGMENT
:
6676 * ICMPv6 error messages for
6677 * non-first fragments
6679 REASON_SET(reason
, PFRES_FRAG
);
6682 case IPPROTO_HOPOPTS
:
6683 case IPPROTO_ROUTING
:
6684 case IPPROTO_DSTOPTS
: {
6685 /* get next header and header length */
6686 struct ip6_ext opt6
;
6688 if (!pf_pull_hdr(m
, off2
, &opt6
,
6689 sizeof (opt6
), NULL
, reason
,
6691 DPFPRINTF(PF_DEBUG_MISC
,
6692 ("pf: ICMPv6 short opt\n"));
6695 if (pd2
.proto
== IPPROTO_AH
)
6696 off2
+= (opt6
.ip6e_len
+ 2) * 4;
6698 off2
+= (opt6
.ip6e_len
+ 1) * 8;
6699 pd2
.proto
= opt6
.ip6e_nxt
;
6700 /* goto the next header */
6707 } while (!terminal
);
6712 switch (pd2
.proto
) {
6716 struct pf_state_peer
*src
, *dst
;
6721 * Only the first 8 bytes of the TCP header can be
6722 * expected. Don't access any TCP header fields after
6723 * th_seq, an ackskew test is not possible.
6725 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
6727 DPFPRINTF(PF_DEBUG_MISC
,
6728 ("pf: ICMP error message too short "
6734 key
.proto
= IPPROTO_TCP
;
6735 if (direction
== PF_IN
) {
6736 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6737 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6738 key
.ext
.xport
.port
= th
.th_dport
;
6739 key
.gwy
.xport
.port
= th
.th_sport
;
6741 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6742 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6743 key
.lan
.xport
.port
= th
.th_dport
;
6744 key
.ext
.xport
.port
= th
.th_sport
;
6749 if (direction
== (*state
)->state_key
->direction
) {
6750 src
= &(*state
)->dst
;
6751 dst
= &(*state
)->src
;
6753 src
= &(*state
)->src
;
6754 dst
= &(*state
)->dst
;
6757 if (src
->wscale
&& dst
->wscale
)
6758 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6762 /* Demodulate sequence number */
6763 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
6765 pf_change_a(&th
.th_seq
, icmpsum
,
6770 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
6772 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
))) {
6773 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6774 printf("pf: BAD ICMP %d:%d ",
6775 icmptype
, pd
->hdr
.icmp
->icmp_code
);
6776 pf_print_host(pd
->src
, 0, pd
->af
);
6778 pf_print_host(pd
->dst
, 0, pd
->af
);
6780 pf_print_state(*state
);
6781 printf(" seq=%u\n", seq
);
6783 REASON_SET(reason
, PFRES_BADSTATE
);
6787 if (STATE_TRANSLATE((*state
)->state_key
)) {
6788 if (direction
== PF_IN
) {
6789 pf_change_icmp(pd2
.src
, &th
.th_sport
,
6790 daddr
, &(*state
)->state_key
->lan
.addr
,
6791 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6792 pd2
.ip_sum
, icmpsum
,
6793 pd
->ip_sum
, 0, pd2
.af
);
6795 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
6796 saddr
, &(*state
)->state_key
->gwy
.addr
,
6797 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6798 pd2
.ip_sum
, icmpsum
,
6799 pd
->ip_sum
, 0, pd2
.af
);
6805 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
6811 m_copyback(m
, off
, ICMP_MINLEN
,
6813 m_copyback(m
, ipoff2
, sizeof (h2
),
6820 sizeof (struct icmp6_hdr
),
6822 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6827 m_copyback(m
, off2
, 8, &th
);
6836 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
6837 NULL
, reason
, pd2
.af
)) {
6838 DPFPRINTF(PF_DEBUG_MISC
,
6839 ("pf: ICMP error message too short "
6845 key
.proto
= IPPROTO_UDP
;
6846 if (direction
== PF_IN
) {
6847 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6848 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6849 key
.ext
.xport
.port
= uh
.uh_dport
;
6850 key
.gwy
.xport
.port
= uh
.uh_sport
;
6853 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6854 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6855 key
.lan
.xport
.port
= uh
.uh_dport
;
6856 key
.ext
.xport
.port
= uh
.uh_sport
;
6860 key
.proto_variant
= PF_EXTFILTER_APD
;
6862 if (ntohs(uh
.uh_sport
) == PF_IKE_PORT
&&
6863 ntohs(uh
.uh_dport
) == PF_IKE_PORT
) {
6864 struct pf_ike_hdr ike
;
6866 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
6867 if (direction
== PF_IN
&&
6868 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
6869 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
6870 "ICMP error, embedded IKE message "
6875 if (plen
> sizeof (ike
))
6876 plen
= sizeof (ike
);
6877 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
6879 key
.app_state
= &as
;
6880 as
.compare_lan_ext
= pf_ike_compare
;
6881 as
.compare_ext_gwy
= pf_ike_compare
;
6882 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6885 *state
= pf_find_state(kif
, &key
, dx
);
6887 if (key
.app_state
&& *state
== 0) {
6889 *state
= pf_find_state(kif
, &key
, dx
);
6893 key
.proto_variant
= PF_EXTFILTER_AD
;
6894 *state
= pf_find_state(kif
, &key
, dx
);
6898 key
.proto_variant
= PF_EXTFILTER_EI
;
6899 *state
= pf_find_state(kif
, &key
, dx
);
6902 if (*state
!= NULL
&& pd
!= NULL
&&
6904 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6906 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6909 if (STATE_TRANSLATE((*state
)->state_key
)) {
6910 if (direction
== PF_IN
) {
6911 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
6912 daddr
, &(*state
)->state_key
->lan
.addr
,
6913 (*state
)->state_key
->lan
.xport
.port
, &uh
.uh_sum
,
6914 pd2
.ip_sum
, icmpsum
,
6915 pd
->ip_sum
, 1, pd2
.af
);
6917 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
6918 saddr
, &(*state
)->state_key
->gwy
.addr
,
6919 (*state
)->state_key
->gwy
.xport
.port
, &uh
.uh_sum
,
6920 pd2
.ip_sum
, icmpsum
,
6921 pd
->ip_sum
, 1, pd2
.af
);
6923 m
= pf_lazy_makewritable(pd
, m
,
6924 off2
+ sizeof (uh
));
6930 m_copyback(m
, off
, ICMP_MINLEN
,
6932 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
6938 sizeof (struct icmp6_hdr
),
6940 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6945 m_copyback(m
, off2
, sizeof (uh
), &uh
);
6952 case IPPROTO_ICMP
: {
6955 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
6956 NULL
, reason
, pd2
.af
)) {
6957 DPFPRINTF(PF_DEBUG_MISC
,
6958 ("pf: ICMP error message too short i"
6964 key
.proto
= IPPROTO_ICMP
;
6965 if (direction
== PF_IN
) {
6966 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6967 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6968 key
.ext
.xport
.port
= 0;
6969 key
.gwy
.xport
.port
= iih
.icmp_id
;
6971 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6972 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6973 key
.lan
.xport
.port
= iih
.icmp_id
;
6974 key
.ext
.xport
.port
= 0;
6979 if (STATE_TRANSLATE((*state
)->state_key
)) {
6980 if (direction
== PF_IN
) {
6981 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
6982 daddr
, &(*state
)->state_key
->lan
.addr
,
6983 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6984 pd2
.ip_sum
, icmpsum
,
6985 pd
->ip_sum
, 0, AF_INET
);
6987 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
6988 saddr
, &(*state
)->state_key
->gwy
.addr
,
6989 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6990 pd2
.ip_sum
, icmpsum
,
6991 pd
->ip_sum
, 0, AF_INET
);
6993 m
= pf_lazy_makewritable(pd
, m
, off2
+ ICMP_MINLEN
);
6996 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
6997 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
6998 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
7006 case IPPROTO_ICMPV6
: {
7007 struct icmp6_hdr iih
;
7009 if (!pf_pull_hdr(m
, off2
, &iih
,
7010 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
7011 DPFPRINTF(PF_DEBUG_MISC
,
7012 ("pf: ICMP error message too short "
7018 key
.proto
= IPPROTO_ICMPV6
;
7019 if (direction
== PF_IN
) {
7020 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7021 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7022 key
.ext
.xport
.port
= 0;
7023 key
.gwy
.xport
.port
= iih
.icmp6_id
;
7025 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7026 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7027 key
.lan
.xport
.port
= iih
.icmp6_id
;
7028 key
.ext
.xport
.port
= 0;
7033 if (STATE_TRANSLATE((*state
)->state_key
)) {
7034 if (direction
== PF_IN
) {
7035 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
7036 daddr
, &(*state
)->state_key
->lan
.addr
,
7037 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7038 pd2
.ip_sum
, icmpsum
,
7039 pd
->ip_sum
, 0, AF_INET6
);
7041 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
7042 saddr
, &(*state
)->state_key
->gwy
.addr
,
7043 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7044 pd2
.ip_sum
, icmpsum
,
7045 pd
->ip_sum
, 0, AF_INET6
);
7047 m
= pf_lazy_makewritable(pd
, m
, off2
+
7048 sizeof (struct icmp6_hdr
));
7051 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
7053 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
7054 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
7064 key
.proto
= pd2
.proto
;
7065 if (direction
== PF_IN
) {
7066 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7067 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7068 key
.ext
.xport
.port
= 0;
7069 key
.gwy
.xport
.port
= 0;
7071 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7072 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7073 key
.lan
.xport
.port
= 0;
7074 key
.ext
.xport
.port
= 0;
7079 if (STATE_TRANSLATE((*state
)->state_key
)) {
7080 if (direction
== PF_IN
) {
7081 pf_change_icmp(pd2
.src
, NULL
,
7082 daddr
, &(*state
)->state_key
->lan
.addr
,
7084 pd2
.ip_sum
, icmpsum
,
7085 pd
->ip_sum
, 0, pd2
.af
);
7087 pf_change_icmp(pd2
.dst
, NULL
,
7088 saddr
, &(*state
)->state_key
->gwy
.addr
,
7090 pd2
.ip_sum
, icmpsum
,
7091 pd
->ip_sum
, 0, pd2
.af
);
7096 m
= pf_lazy_makewritable(pd
, m
,
7097 ipoff2
+ sizeof (h2
));
7103 m
= pf_lazy_makewritable(pd
, m
,
7104 ipoff2
+ sizeof (h2_6
));
7108 sizeof (struct icmp6_hdr
),
7110 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7125 pf_test_state_grev1(struct pf_state
**state
, int direction
,
7126 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
7128 struct pf_state_peer
*src
;
7129 struct pf_state_peer
*dst
;
7130 struct pf_state_key_cmp key
;
7131 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
7136 key
.proto
= IPPROTO_GRE
;
7137 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
7138 if (direction
== PF_IN
) {
7139 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7140 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7141 key
.gwy
.xport
.call_id
= grev1
->call_id
;
7143 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7144 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7145 key
.ext
.xport
.call_id
= grev1
->call_id
;
7150 if (direction
== (*state
)->state_key
->direction
) {
7151 src
= &(*state
)->src
;
7152 dst
= &(*state
)->dst
;
7154 src
= &(*state
)->dst
;
7155 dst
= &(*state
)->src
;
7159 if (src
->state
< PFGRE1S_INITIATING
)
7160 src
->state
= PFGRE1S_INITIATING
;
7162 /* update expire time */
7163 (*state
)->expire
= pf_time_second();
7164 if (src
->state
>= PFGRE1S_INITIATING
&&
7165 dst
->state
>= PFGRE1S_INITIATING
) {
7166 if ((*state
)->timeout
!= PFTM_TCP_ESTABLISHED
)
7167 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
7168 src
->state
= PFGRE1S_ESTABLISHED
;
7169 dst
->state
= PFGRE1S_ESTABLISHED
;
7171 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
7174 if ((*state
)->state_key
->app_state
)
7175 (*state
)->state_key
->app_state
->u
.grev1
.pptp_state
->expire
=
7178 /* translate source/destination address, if necessary */
7179 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
7180 if (direction
== PF_OUT
) {
7184 pf_change_a(&pd
->src
->v4
.s_addr
,
7186 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7191 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7197 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
7202 pf_change_a(&pd
->dst
->v4
.s_addr
,
7204 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7209 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7216 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
7219 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
7226 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7227 int off
, struct pf_pdesc
*pd
)
7230 struct pf_state_peer
*src
;
7231 struct pf_state_peer
*dst
;
7232 struct pf_state_key_cmp key
;
7233 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
7236 memset(&key
, 0, sizeof (key
));
7238 key
.proto
= IPPROTO_ESP
;
7239 if (direction
== PF_IN
) {
7240 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7241 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7242 key
.gwy
.xport
.spi
= esp
->spi
;
7244 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7245 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7246 key
.ext
.xport
.spi
= esp
->spi
;
7249 *state
= pf_find_state(kif
, &key
, direction
);
7256 * No matching state. Look for a blocking state. If we find
7257 * one, then use that state and move it so that it's keyed to
7258 * the SPI in the current packet.
7260 if (direction
== PF_IN
) {
7261 key
.gwy
.xport
.spi
= 0;
7263 s
= pf_find_state(kif
, &key
, direction
);
7265 struct pf_state_key
*sk
= s
->state_key
;
7267 RB_REMOVE(pf_state_tree_ext_gwy
,
7268 &pf_statetbl_ext_gwy
, sk
);
7269 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
7272 if (RB_INSERT(pf_state_tree_ext_gwy
,
7273 &pf_statetbl_ext_gwy
, sk
))
7274 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
7279 key
.ext
.xport
.spi
= 0;
7281 s
= pf_find_state(kif
, &key
, direction
);
7283 struct pf_state_key
*sk
= s
->state_key
;
7285 RB_REMOVE(pf_state_tree_lan_ext
,
7286 &pf_statetbl_lan_ext
, sk
);
7287 sk
->ext
.xport
.spi
= esp
->spi
;
7289 if (RB_INSERT(pf_state_tree_lan_ext
,
7290 &pf_statetbl_lan_ext
, sk
))
7291 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
7300 if (s
->creatorid
== pf_status
.hostid
)
7301 pfsync_delete_state(s
);
7303 s
->timeout
= PFTM_UNLINKED
;
7304 hook_runloop(&s
->unlink_hooks
,
7305 HOOK_REMOVE
|HOOK_FREE
);
7306 pf_src_tree_remove_state(s
);
7313 if (*state
!= NULL
&& pd
!= NULL
&&
7314 pd
->flowhash
== 0) {
7315 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7318 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7321 if (direction
== (*state
)->state_key
->direction
) {
7322 src
= &(*state
)->src
;
7323 dst
= &(*state
)->dst
;
7325 src
= &(*state
)->dst
;
7326 dst
= &(*state
)->src
;
7330 if (src
->state
< PFESPS_INITIATING
)
7331 src
->state
= PFESPS_INITIATING
;
7333 /* update expire time */
7334 (*state
)->expire
= pf_time_second();
7335 if (src
->state
>= PFESPS_INITIATING
&&
7336 dst
->state
>= PFESPS_INITIATING
) {
7337 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
7338 src
->state
= PFESPS_ESTABLISHED
;
7339 dst
->state
= PFESPS_ESTABLISHED
;
7341 (*state
)->timeout
= PFTM_ESP_INITIATING
;
7343 /* translate source/destination address, if necessary */
7344 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7345 if (direction
== PF_OUT
) {
7349 pf_change_a(&pd
->src
->v4
.s_addr
,
7351 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7356 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7365 pf_change_a(&pd
->dst
->v4
.s_addr
,
7367 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7372 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7384 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7385 struct pf_pdesc
*pd
)
7387 struct pf_state_peer
*src
, *dst
;
7388 struct pf_state_key_cmp key
;
7392 key
.proto
= pd
->proto
;
7393 if (direction
== PF_IN
) {
7394 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7395 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7396 key
.ext
.xport
.port
= 0;
7397 key
.gwy
.xport
.port
= 0;
7399 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7400 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7401 key
.lan
.xport
.port
= 0;
7402 key
.ext
.xport
.port
= 0;
7407 if (direction
== (*state
)->state_key
->direction
) {
7408 src
= &(*state
)->src
;
7409 dst
= &(*state
)->dst
;
7411 src
= &(*state
)->dst
;
7412 dst
= &(*state
)->src
;
7416 if (src
->state
< PFOTHERS_SINGLE
)
7417 src
->state
= PFOTHERS_SINGLE
;
7418 if (dst
->state
== PFOTHERS_SINGLE
)
7419 dst
->state
= PFOTHERS_MULTIPLE
;
7421 /* update expire time */
7422 (*state
)->expire
= pf_time_second();
7423 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
7424 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
7426 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
7428 /* translate source/destination address, if necessary */
7429 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7430 if (direction
== PF_OUT
) {
7434 pf_change_a(&pd
->src
->v4
.s_addr
,
7436 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
7443 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
7451 pf_change_a(&pd
->dst
->v4
.s_addr
,
7453 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
7460 &(*state
)->state_key
->lan
.addr
, pd
->af
);
7471 * ipoff and off are measured from the start of the mbuf chain.
7472 * h must be at "ipoff" on the mbuf chain.
7475 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
7476 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
7481 struct ip
*h
= mtod(m
, struct ip
*);
7482 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
7485 if (fragoff
>= len
) {
7486 ACTION_SET(actionp
, PF_PASS
);
7488 ACTION_SET(actionp
, PF_DROP
);
7489 REASON_SET(reasonp
, PFRES_FRAG
);
7493 if (m
->m_pkthdr
.len
< off
+ len
||
7494 ntohs(h
->ip_len
) < off
+ len
) {
7495 ACTION_SET(actionp
, PF_DROP
);
7496 REASON_SET(reasonp
, PFRES_SHORT
);
7504 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
7506 if (m
->m_pkthdr
.len
< off
+ len
||
7507 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
7508 (unsigned)(off
+ len
)) {
7509 ACTION_SET(actionp
, PF_DROP
);
7510 REASON_SET(reasonp
, PFRES_SHORT
);
7517 m_copydata(m
, off
, len
, p
);
7522 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
7525 struct sockaddr_in
*dst
;
7528 struct sockaddr_in6
*dst6
;
7529 struct route_in6 ro
;
7534 bzero(&ro
, sizeof (ro
));
7537 dst
= satosin(&ro
.ro_dst
);
7538 dst
->sin_family
= AF_INET
;
7539 dst
->sin_len
= sizeof (*dst
);
7540 dst
->sin_addr
= addr
->v4
;
7544 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7545 dst6
->sin6_family
= AF_INET6
;
7546 dst6
->sin6_len
= sizeof (*dst6
);
7547 dst6
->sin6_addr
= addr
->v6
;
7554 /* XXX: IFT_ENC is not currently used by anything*/
7555 /* Skip checks for ipsec interfaces */
7556 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
7559 rtalloc((struct route
*)&ro
);
7562 if (ro
.ro_rt
!= NULL
)
7568 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
7571 struct sockaddr_in
*dst
;
7573 struct sockaddr_in6
*dst6
;
7574 struct route_in6 ro
;
7580 bzero(&ro
, sizeof (ro
));
7583 dst
= satosin(&ro
.ro_dst
);
7584 dst
->sin_family
= AF_INET
;
7585 dst
->sin_len
= sizeof (*dst
);
7586 dst
->sin_addr
= addr
->v4
;
7590 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7591 dst6
->sin6_family
= AF_INET6
;
7592 dst6
->sin6_len
= sizeof (*dst6
);
7593 dst6
->sin6_addr
= addr
->v6
;
7600 rtalloc((struct route
*)&ro
);
7602 if (ro
.ro_rt
!= NULL
) {
7611 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7612 struct pf_state
*s
, struct pf_pdesc
*pd
)
7615 struct mbuf
*m0
, *m1
;
7616 struct route iproute
;
7617 struct route
*ro
= NULL
;
7618 struct sockaddr_in
*dst
;
7620 struct ifnet
*ifp
= NULL
;
7621 struct pf_addr naddr
;
7622 struct pf_src_node
*sn
= NULL
;
7626 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7627 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7628 panic("pf_route: invalid parameters");
7630 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7636 if (r
->rt
== PF_DUPTO
) {
7637 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7640 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7645 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7646 DPFPRINTF(PF_DEBUG_URGENT
,
7647 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7651 ip
= mtod(m0
, struct ip
*);
7654 bzero((caddr_t
)ro
, sizeof (*ro
));
7655 dst
= satosin((void *)&ro
->ro_dst
);
7656 dst
->sin_family
= AF_INET
;
7657 dst
->sin_len
= sizeof (*dst
);
7658 dst
->sin_addr
= ip
->ip_dst
;
7660 if (r
->rt
== PF_FASTROUTE
) {
7662 if (ro
->ro_rt
== 0) {
7663 ipstat
.ips_noroute
++;
7667 ifp
= ro
->ro_rt
->rt_ifp
;
7669 ro
->ro_rt
->rt_use
++;
7671 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
7672 dst
= satosin((void *)ro
->ro_rt
->rt_gateway
);
7673 RT_UNLOCK(ro
->ro_rt
);
7675 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7676 DPFPRINTF(PF_DEBUG_URGENT
,
7677 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
7681 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
7683 if (!PF_AZERO(&naddr
, AF_INET
))
7684 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
7685 ifp
= r
->rpool
.cur
->kif
?
7686 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7688 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
7689 dst
->sin_addr
.s_addr
=
7690 s
->rt_addr
.v4
.s_addr
;
7691 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7698 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7700 else if (m0
== NULL
)
7702 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7703 DPFPRINTF(PF_DEBUG_URGENT
,
7704 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7707 ip
= mtod(m0
, struct ip
*);
7710 /* Copied from ip_output. */
7712 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
7713 m0
->m_pkthdr
.csum_flags
|= CSUM_IP
;
7714 sw_csum
= m0
->m_pkthdr
.csum_flags
&
7715 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
7717 if (ifp
->if_hwassist
& CSUM_TCP_SUM16
) {
7719 * Special case code for GMACE
7720 * frames that can be checksumed by GMACE SUM16 HW:
7721 * frame >64, no fragments, no UDP
7723 if (apple_hwcksum_tx
&& (m0
->m_pkthdr
.csum_flags
& CSUM_TCP
) &&
7724 (ntohs(ip
->ip_len
) > 50) &&
7725 (ntohs(ip
->ip_len
) <= ifp
->if_mtu
)) {
7727 * Apple GMAC HW, expects:
7728 * STUFF_OFFSET << 16 | START_OFFSET
7730 /* IP+Enet header length */
7731 u_short offset
= ((ip
->ip_hl
) << 2) + 14;
7732 u_short csumprev
= m0
->m_pkthdr
.csum_data
& 0xffff;
7733 m0
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
|
7734 CSUM_TCP_SUM16
; /* for GMAC */
7735 m0
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
7736 m0
->m_pkthdr
.csum_data
+= offset
;
7737 /* do IP hdr chksum in software */
7738 sw_csum
= CSUM_DELAY_IP
;
7740 /* let the software handle any UDP or TCP checksums */
7741 sw_csum
|= (CSUM_DELAY_DATA
& m0
->m_pkthdr
.csum_flags
);
7743 } else if (apple_hwcksum_tx
== 0) {
7744 sw_csum
|= (CSUM_DELAY_DATA
| CSUM_DELAY_IP
) &
7745 m0
->m_pkthdr
.csum_flags
;
7748 if (sw_csum
& CSUM_DELAY_DATA
) {
7749 in_delayed_cksum(m0
);
7750 sw_csum
&= ~CSUM_DELAY_DATA
;
7751 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
7754 if (apple_hwcksum_tx
!= 0) {
7755 m0
->m_pkthdr
.csum_flags
&=
7756 IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
7758 m0
->m_pkthdr
.csum_flags
= 0;
7761 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
||
7762 (ifp
->if_hwassist
& CSUM_FRAGMENT
)) {
7764 if (sw_csum
& CSUM_DELAY_IP
)
7765 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
7766 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
, sintosa(dst
));
7771 * Too large for interface; fragment if possible.
7772 * Must be able to put at least 8 bytes per fragment.
7774 if (ip
->ip_off
& htons(IP_DF
)) {
7775 ipstat
.ips_cantfrag
++;
7776 if (r
->rt
!= PF_DUPTO
) {
7777 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
7786 /* PR-8933605: send ip_len,ip_off to ip_fragment in host byte order */
7787 #if BYTE_ORDER != BIG_ENDIAN
7791 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
7798 for (m0
= m1
; m0
; m0
= m1
) {
7802 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
,
7809 ipstat
.ips_fragmented
++;
7812 if (r
->rt
!= PF_DUPTO
)
7814 if (ro
== &iproute
&& ro
->ro_rt
)
7826 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7827 struct pf_state
*s
, struct pf_pdesc
*pd
)
7831 struct route_in6 ip6route
;
7832 struct route_in6
*ro
;
7833 struct sockaddr_in6
*dst
;
7834 struct ip6_hdr
*ip6
;
7835 struct ifnet
*ifp
= NULL
;
7836 struct pf_addr naddr
;
7837 struct pf_src_node
*sn
= NULL
;
7840 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7841 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7842 panic("pf_route6: invalid parameters");
7844 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7850 if (r
->rt
== PF_DUPTO
) {
7851 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7854 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7859 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7860 DPFPRINTF(PF_DEBUG_URGENT
,
7861 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
7864 ip6
= mtod(m0
, struct ip6_hdr
*);
7867 bzero((caddr_t
)ro
, sizeof (*ro
));
7868 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
7869 dst
->sin6_family
= AF_INET6
;
7870 dst
->sin6_len
= sizeof (*dst
);
7871 dst
->sin6_addr
= ip6
->ip6_dst
;
7873 /* Cheat. XXX why only in the v6 case??? */
7874 if (r
->rt
== PF_FASTROUTE
) {
7875 struct pf_mtag
*pf_mtag
;
7877 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
7879 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
7880 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
7884 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7885 DPFPRINTF(PF_DEBUG_URGENT
,
7886 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
7890 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
7892 if (!PF_AZERO(&naddr
, AF_INET6
))
7893 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7895 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7897 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
7898 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7899 &s
->rt_addr
, AF_INET6
);
7900 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7906 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7908 else if (m0
== NULL
)
7910 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7911 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
7912 "< sizeof (struct ip6_hdr)\n"));
7915 ip6
= mtod(m0
, struct ip6_hdr
*);
7919 * If the packet is too large for the outgoing interface,
7920 * send back an icmp6 error.
7922 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
7923 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
7924 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
7925 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, NULL
);
7927 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
7928 if (r
->rt
!= PF_DUPTO
)
7929 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
7935 if (r
->rt
!= PF_DUPTO
)
7947 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
7948 * off is the offset where the protocol header starts
7949 * len is the total length of protocol header plus payload
7950 * returns 0 when the checksum is valid, otherwise returns 1.
7953 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
7962 * Optimize for the common case; if the hardware calculated
7963 * value doesn't include pseudo-header checksum, or if it
7964 * is partially-computed (only 16-bit summation), do it in
7967 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
&
7968 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) &&
7969 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
7975 case IPPROTO_ICMPV6
:
7981 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
7983 if (m
->m_pkthdr
.len
< off
+ len
)
7988 if (p
== IPPROTO_ICMP
) {
7993 sum
= in_cksum(m
, len
);
7997 if (m
->m_len
< (int)sizeof (struct ip
))
7999 sum
= inet_cksum(m
, p
, off
, len
);
8005 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
8007 sum
= inet6_cksum(m
, p
, off
, len
);
8016 tcpstat
.tcps_rcvbadsum
++;
8019 udpstat
.udps_badsum
++;
8022 icmpstat
.icps_checksum
++;
8025 case IPPROTO_ICMPV6
:
8026 icmp6stat
.icp6s_checksum
++;
8036 #define PF_APPLE_UPDATE_PDESC_IPv4() \
8038 if (m && pd.mp && m != pd.mp) { \
8040 h = mtod(m, struct ip *); \
8041 pd.pf_mtag = pf_get_mtag(m); \
8046 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8047 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8052 struct pfi_kif
*kif
;
8053 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8054 struct mbuf
*m
= *m0
;
8056 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8057 struct pf_state
*s
= NULL
;
8058 struct pf_state_key
*sk
= NULL
;
8059 struct pf_ruleset
*ruleset
= NULL
;
8061 int off
, dirndx
, pqid
= 0;
8063 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8065 if (!pf_status
.running
)
8068 memset(&pd
, 0, sizeof (pd
));
8070 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8071 DPFPRINTF(PF_DEBUG_URGENT
,
8072 ("pf_test: pf_get_mtag returned NULL\n"));
8076 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8079 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8082 DPFPRINTF(PF_DEBUG_URGENT
,
8083 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
8086 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8090 if ((m
->m_flags
& M_PKTHDR
) == 0)
8091 panic("non-M_PKTHDR is passed to pf_test");
8092 #endif /* DIAGNOSTIC */
8094 /* initialize enough of pd for the done label */
8095 h
= mtod(m
, struct ip
*);
8098 pd
.pf_mtag
= pf_get_mtag(m
);
8099 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8100 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8101 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8102 pd
.ip_sum
= &h
->ip_sum
;
8104 pd
.proto_variant
= 0;
8107 pd
.tot_len
= ntohs(h
->ip_len
);
8110 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8112 REASON_SET(&reason
, PFRES_SHORT
);
8118 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8120 #endif /* DUMMYNET */
8122 /* We do IP header normalization and packet reassembly here */
8123 action
= pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
);
8125 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8132 #endif /* DUMMYNET */
8133 m
= *m0
; /* pf_normalize messes with m0 */
8134 h
= mtod(m
, struct ip
*);
8136 off
= h
->ip_hl
<< 2;
8137 if (off
< (int)sizeof (*h
)) {
8139 REASON_SET(&reason
, PFRES_SHORT
);
8144 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8145 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8146 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8147 pd
.ip_sum
= &h
->ip_sum
;
8149 pd
.proto_variant
= 0;
8152 pd
.pf_mtag
= pf_get_mtag(m
);
8155 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8156 pd
.tot_len
= ntohs(h
->ip_len
);
8158 if (pd
.pf_mtag
!= NULL
&& pd
.pf_mtag
->pftag_flowhash
!= 0) {
8159 pd
.flowhash
= pd
.pf_mtag
->pftag_flowhash
;
8160 pd
.flags
|= (m
->m_pkthdr
.m_fhflags
& PF_TAG_FLOWADV
) ?
8161 PFDESC_FLOW_ADV
: 0;
8164 /* handle fragments that didn't get reassembled by normalization */
8165 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
8166 pd
.flags
|= PFDESC_IP_FRAG
;
8168 /* Traffic goes through dummynet first */
8169 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8170 if (action
== PF_DROP
|| m
== NULL
) {
8174 #endif /* DUMMYNET */
8175 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8185 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8186 &action
, &reason
, AF_INET
)) {
8187 log
= action
!= PF_PASS
;
8190 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8191 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
8194 /* Traffic goes through dummynet first */
8195 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8196 if (action
== PF_DROP
|| m
== NULL
) {
8200 #endif /* DUMMYNET */
8201 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8204 PF_APPLE_UPDATE_PDESC_IPv4();
8205 if (action
== PF_DROP
)
8207 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8211 PF_APPLE_UPDATE_PDESC_IPv4();
8212 if (action
== PF_PASS
) {
8214 pfsync_update_state(s
);
8215 #endif /* NPFSYNC */
8219 } else if (s
== NULL
)
8220 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8221 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8229 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8230 &action
, &reason
, AF_INET
)) {
8231 log
= action
!= PF_PASS
;
8234 if (uh
.uh_dport
== 0 ||
8235 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8236 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8238 REASON_SET(&reason
, PFRES_SHORT
);
8242 /* Traffic goes through dummynet first */
8243 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8244 if (action
== PF_DROP
|| m
== NULL
) {
8248 #endif /* DUMMYNET */
8249 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8253 PF_APPLE_UPDATE_PDESC_IPv4();
8254 if (action
== PF_PASS
) {
8256 pfsync_update_state(s
);
8257 #endif /* NPFSYNC */
8261 } else if (s
== NULL
)
8262 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8263 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8267 case IPPROTO_ICMP
: {
8271 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
8272 &action
, &reason
, AF_INET
)) {
8273 log
= action
!= PF_PASS
;
8277 /* Traffic goes through dummynet first */
8278 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8279 if (action
== PF_DROP
|| m
== NULL
) {
8283 #endif /* DUMMYNET */
8284 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8288 PF_APPLE_UPDATE_PDESC_IPv4();
8289 if (action
== PF_PASS
) {
8291 pfsync_update_state(s
);
8292 #endif /* NPFSYNC */
8296 } else if (s
== NULL
)
8297 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8298 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8303 struct pf_esp_hdr esp
;
8306 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8308 log
= action
!= PF_PASS
;
8312 /* Traffic goes through dummynet first */
8313 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8314 if (action
== PF_DROP
|| m
== NULL
) {
8318 #endif /* DUMMYNET */
8319 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8322 PF_APPLE_UPDATE_PDESC_IPv4();
8323 if (action
== PF_PASS
) {
8325 pfsync_update_state(s
);
8326 #endif /* NPFSYNC */
8330 } else if (s
== NULL
)
8331 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8332 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8337 struct pf_grev1_hdr grev1
;
8338 pd
.hdr
.grev1
= &grev1
;
8339 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8340 &reason
, AF_INET
)) {
8341 log
= (action
!= PF_PASS
);
8345 /* Traffic goes through dummynet first */
8346 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8347 if (action
== PF_DROP
|| m
== NULL
) {
8351 #endif /* DUMMYNET */
8352 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8353 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8354 if (ntohs(grev1
.payload_length
) >
8355 m
->m_pkthdr
.len
- off
) {
8357 REASON_SET(&reason
, PFRES_SHORT
);
8360 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8361 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8362 if (pd
.lmw
< 0) goto done
;
8363 PF_APPLE_UPDATE_PDESC_IPv4();
8364 if (action
== PF_PASS
) {
8366 pfsync_update_state(s
);
8367 #endif /* NPFSYNC */
8372 } else if (s
== NULL
) {
8373 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8374 h
, &pd
, &a
, &ruleset
, &ipintrq
);
8375 if (action
== PF_PASS
)
8380 /* not GREv1/PPTP, so treat as ordinary GRE... */
8385 /* Traffic goes through dummynet first */
8386 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8387 if (action
== PF_DROP
|| m
== NULL
) {
8391 #endif /* DUMMYNET */
8392 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8395 PF_APPLE_UPDATE_PDESC_IPv4();
8396 if (action
== PF_PASS
) {
8398 pfsync_update_state(s
);
8399 #endif /* NPFSYNC */
8403 } else if (s
== NULL
)
8404 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
8405 &pd
, &a
, &ruleset
, &ipintrq
);
8411 PF_APPLE_UPDATE_PDESC_IPv4();
8413 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
8414 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
8416 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8418 DPFPRINTF(PF_DEBUG_MISC
,
8419 ("pf: dropping packet with ip options [hlen=%u]\n",
8420 (unsigned int) h
->ip_hl
));
8423 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
8425 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
8428 if (action
== PF_PASS
) {
8430 if (altq_allowed
&& r
->qid
) {
8431 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
8432 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
8434 pd
.pf_mtag
->pftag_qid
= r
->qid
;
8436 #endif /* PF_ALTQ */
8437 /* add hints for ecn */
8438 pd
.pf_mtag
->pftag_hdr
= h
;
8439 /* record address family */
8440 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET6
;
8441 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
8442 /* record TCP vs. non-TCP */
8443 if (pd
.proto
== IPPROTO_TCP
)
8444 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TCP
;
8446 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_TCP
;
8450 * connections redirected to loopback should not match sockets
8451 * bound specifically to loopback due to security implications,
8452 * see tcp_input() and in_pcblookup_listen().
8454 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
8455 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8456 (s
->nat_rule
.ptr
->action
== PF_RDR
||
8457 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
8458 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
8459 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
8464 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8465 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
8466 lr
= s
->nat_rule
.ptr
;
8469 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
8473 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
8474 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
8476 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
8477 dirndx
= (dir
== PF_OUT
);
8478 r
->packets
[dirndx
]++;
8479 r
->bytes
[dirndx
] += pd
.tot_len
;
8481 a
->packets
[dirndx
]++;
8482 a
->bytes
[dirndx
] += pd
.tot_len
;
8486 if (s
->nat_rule
.ptr
!= NULL
) {
8487 s
->nat_rule
.ptr
->packets
[dirndx
]++;
8488 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
8490 if (s
->src_node
!= NULL
) {
8491 s
->src_node
->packets
[dirndx
]++;
8492 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
8494 if (s
->nat_src_node
!= NULL
) {
8495 s
->nat_src_node
->packets
[dirndx
]++;
8496 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
8498 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
8499 s
->packets
[dirndx
]++;
8500 s
->bytes
[dirndx
] += pd
.tot_len
;
8503 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
8507 * XXX: we need to make sure that the addresses
8508 * passed to pfr_update_stats() are the same than
8509 * the addresses used during matching (pfr_match)
8511 if (r
== &pf_default_rule
) {
8513 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8514 &pd
.baddr
: &pd
.naddr
;
8516 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8517 &pd
.naddr
: &pd
.baddr
;
8518 if (x
== &pd
.baddr
|| s
== NULL
) {
8519 /* we need to change the address */
8526 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
8527 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
8528 sk
->direction
== dir
) ?
8529 pd
.src
: pd
.dst
, pd
.af
,
8530 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8532 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
8533 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
8534 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
8535 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8539 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
8543 REASON_SET(&reason
, PFRES_MEMORY
);
8547 if (action
== PF_DROP
) {
8556 if (action
== PF_SYNPROXY_DROP
) {
8561 /* pf_route can free the mbuf causing *m0 to become NULL */
8562 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
8569 #define PF_APPLE_UPDATE_PDESC_IPv6() \
8571 if (m && pd.mp && m != pd.mp) { \
8575 h = mtod(m, struct ip6_hdr *); \
8580 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8581 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8586 struct pfi_kif
*kif
;
8587 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8588 struct mbuf
*m
= *m0
, *n
= NULL
;
8590 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8591 struct pf_state
*s
= NULL
;
8592 struct pf_state_key
*sk
= NULL
;
8593 struct pf_ruleset
*ruleset
= NULL
;
8595 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
8598 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8600 if (!pf_status
.running
)
8603 memset(&pd
, 0, sizeof (pd
));
8605 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8606 DPFPRINTF(PF_DEBUG_URGENT
,
8607 ("pf_test6: pf_get_mtag returned NULL\n"));
8611 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8614 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8617 DPFPRINTF(PF_DEBUG_URGENT
,
8618 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
8621 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8625 if ((m
->m_flags
& M_PKTHDR
) == 0)
8626 panic("non-M_PKTHDR is passed to pf_test6");
8627 #endif /* DIAGNOSTIC */
8629 h
= mtod(m
, struct ip6_hdr
*);
8632 off
= ((caddr_t
)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
8635 pd
.pf_mtag
= pf_get_mtag(m
);
8636 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8637 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8638 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8642 pd
.proto_variant
= 0;
8644 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8645 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
8648 if (pd
.pf_mtag
->pftag_flowhash
!= 0) {
8649 pd
.flowhash
= pd
.pf_mtag
->pftag_flowhash
;
8650 pd
.flags
|= (m
->m_pkthdr
.m_fhflags
& PF_TAG_FLOWADV
) ?
8651 PFDESC_FLOW_ADV
: 0;
8654 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8656 REASON_SET(&reason
, PFRES_SHORT
);
8662 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8664 #endif /* DUMMYNET */
8666 /* We do IP header normalization and packet reassembly here */
8667 action
= pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
);
8669 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8676 #endif /* DUMMYNET */
8677 h
= mtod(m
, struct ip6_hdr
*);
8681 * we do not support jumbogram yet. if we keep going, zero ip6_plen
8682 * will do something bad, so drop the packet for now.
8684 if (htons(h
->ip6_plen
) == 0) {
8686 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
8691 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8692 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8693 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8697 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
8700 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
8701 pd
.proto
= h
->ip6_nxt
;
8702 pd
.proto_variant
= 0;
8705 pd
.pf_mtag
= pf_get_mtag(m
);
8709 case IPPROTO_FRAGMENT
: {
8710 struct ip6_frag ip6f
;
8712 pd
.flags
|= PFDESC_IP_FRAG
;
8713 if (!pf_pull_hdr(m
, off
, &ip6f
, sizeof ip6f
, NULL
,
8715 DPFPRINTF(PF_DEBUG_MISC
,
8716 ("pf: IPv6 short fragment header\n"));
8718 REASON_SET(&reason
, PFRES_SHORT
);
8722 pd
.proto
= nxt
= ip6f
.ip6f_nxt
;
8724 /* Traffic goes through dummynet first */
8725 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8726 if (action
== PF_DROP
|| m
== NULL
) {
8730 #endif /* DUMMYNET */
8731 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
, &pd
, &a
,
8733 if (action
== PF_DROP
) {
8734 REASON_SET(&reason
, PFRES_FRAG
);
8739 case IPPROTO_ROUTING
:
8744 case IPPROTO_HOPOPTS
:
8745 case IPPROTO_DSTOPTS
: {
8746 /* get next header and header length */
8747 struct ip6_ext opt6
;
8749 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
8750 NULL
, &reason
, pd
.af
)) {
8751 DPFPRINTF(PF_DEBUG_MISC
,
8752 ("pf: IPv6 short opt\n"));
8757 if (pd
.proto
== IPPROTO_AH
)
8758 off
+= (opt6
.ip6e_len
+ 2) * 4;
8760 off
+= (opt6
.ip6e_len
+ 1) * 8;
8761 nxt
= opt6
.ip6e_nxt
;
8762 /* goto the next header */
8769 } while (!terminal
);
8771 /* if there's no routing header, use unmodified mbuf for checksumming */
8781 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8782 &action
, &reason
, AF_INET6
)) {
8783 log
= action
!= PF_PASS
;
8786 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8788 /* Traffic goes through dummynet first */
8789 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8790 if (action
== PF_DROP
|| m
== NULL
) {
8794 #endif /* DUMMYNET */
8795 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8798 PF_APPLE_UPDATE_PDESC_IPv6();
8799 if (action
== PF_DROP
)
8801 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8805 PF_APPLE_UPDATE_PDESC_IPv6();
8806 if (action
== PF_PASS
) {
8808 pfsync_update_state(s
);
8809 #endif /* NPFSYNC */
8813 } else if (s
== NULL
)
8814 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8815 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8823 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8824 &action
, &reason
, AF_INET6
)) {
8825 log
= action
!= PF_PASS
;
8828 if (uh
.uh_dport
== 0 ||
8829 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8830 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8832 REASON_SET(&reason
, PFRES_SHORT
);
8836 /* Traffic goes through dummynet first */
8837 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8838 if (action
== PF_DROP
|| m
== NULL
) {
8842 #endif /* DUMMYNET */
8843 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8847 PF_APPLE_UPDATE_PDESC_IPv6();
8848 if (action
== PF_PASS
) {
8850 pfsync_update_state(s
);
8851 #endif /* NPFSYNC */
8855 } else if (s
== NULL
)
8856 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8857 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8861 case IPPROTO_ICMPV6
: {
8862 struct icmp6_hdr ih
;
8865 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
8866 &action
, &reason
, AF_INET6
)) {
8867 log
= action
!= PF_PASS
;
8871 /* Traffic goes through dummynet first */
8872 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8873 if (action
== PF_DROP
|| m
== NULL
) {
8877 #endif /* DUMMYNET */
8878 action
= pf_test_state_icmp(&s
, dir
, kif
,
8879 m
, off
, h
, &pd
, &reason
);
8882 PF_APPLE_UPDATE_PDESC_IPv6();
8883 if (action
== PF_PASS
) {
8885 pfsync_update_state(s
);
8886 #endif /* NPFSYNC */
8890 } else if (s
== NULL
)
8891 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8892 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8897 struct pf_esp_hdr esp
;
8900 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8902 log
= action
!= PF_PASS
;
8906 /* Traffic goes through dummynet first */
8907 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8908 if (action
== PF_DROP
|| m
== NULL
) {
8912 #endif /* DUMMYNET */
8913 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8916 PF_APPLE_UPDATE_PDESC_IPv6();
8917 if (action
== PF_PASS
) {
8919 pfsync_update_state(s
);
8920 #endif /* NPFSYNC */
8924 } else if (s
== NULL
)
8925 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8926 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8931 struct pf_grev1_hdr grev1
;
8933 pd
.hdr
.grev1
= &grev1
;
8934 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8935 &reason
, AF_INET6
)) {
8936 log
= (action
!= PF_PASS
);
8940 /* Traffic goes through dummynet first */
8941 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8942 if (action
== PF_DROP
|| m
== NULL
) {
8946 #endif /* DUMMYNET */
8947 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8948 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8949 if (ntohs(grev1
.payload_length
) >
8950 m
->m_pkthdr
.len
- off
) {
8952 REASON_SET(&reason
, PFRES_SHORT
);
8955 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8958 PF_APPLE_UPDATE_PDESC_IPv6();
8959 if (action
== PF_PASS
) {
8961 pfsync_update_state(s
);
8962 #endif /* NPFSYNC */
8967 } else if (s
== NULL
) {
8968 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8969 h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8970 if (action
== PF_PASS
)
8975 /* not GREv1/PPTP, so treat as ordinary GRE... */
8980 /* Traffic goes through dummynet first */
8981 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8982 if (action
== PF_DROP
|| m
== NULL
) {
8986 #endif /* DUMMYNET */
8987 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8990 PF_APPLE_UPDATE_PDESC_IPv6();
8991 if (action
== PF_PASS
) {
8993 pfsync_update_state(s
);
8994 #endif /* NPFSYNC */
8998 } else if (s
== NULL
)
8999 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9000 &pd
, &a
, &ruleset
, &ip6intrq
);
9006 PF_APPLE_UPDATE_PDESC_IPv6();
9013 /* handle dangerous IPv6 extension headers. */
9014 if (action
== PF_PASS
&& rh_cnt
&&
9015 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9017 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9019 DPFPRINTF(PF_DEBUG_MISC
,
9020 ("pf: dropping packet with dangerous v6 headers\n"));
9023 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) || pd
.flowhash
!= 0)
9024 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9027 if (action
== PF_PASS
) {
9029 if (altq_allowed
&& r
->qid
) {
9030 if (pd
.tos
& IPTOS_LOWDELAY
)
9031 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
9033 pd
.pf_mtag
->pftag_qid
= r
->qid
;
9035 #endif /* PF_ALTQ */
9036 /* add hints for ecn */
9037 pd
.pf_mtag
->pftag_hdr
= h
;
9038 /* record address family */
9039 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET
;
9040 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
9041 /* record TCP vs. non-TCP */
9042 if (pd
.proto
== IPPROTO_TCP
)
9043 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TCP
;
9045 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_TCP
;
9048 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
9049 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9050 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9051 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9052 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
9053 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9058 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9059 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9060 lr
= s
->nat_rule
.ptr
;
9063 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
9067 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9068 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
9070 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9071 dirndx
= (dir
== PF_OUT
);
9072 r
->packets
[dirndx
]++;
9073 r
->bytes
[dirndx
] += pd
.tot_len
;
9075 a
->packets
[dirndx
]++;
9076 a
->bytes
[dirndx
] += pd
.tot_len
;
9080 if (s
->nat_rule
.ptr
!= NULL
) {
9081 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9082 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9084 if (s
->src_node
!= NULL
) {
9085 s
->src_node
->packets
[dirndx
]++;
9086 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9088 if (s
->nat_src_node
!= NULL
) {
9089 s
->nat_src_node
->packets
[dirndx
]++;
9090 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9092 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9093 s
->packets
[dirndx
]++;
9094 s
->bytes
[dirndx
] += pd
.tot_len
;
9097 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9101 * XXX: we need to make sure that the addresses
9102 * passed to pfr_update_stats() are the same than
9103 * the addresses used during matching (pfr_match)
9105 if (r
== &pf_default_rule
) {
9107 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9108 &pd
.baddr
: &pd
.naddr
;
9110 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9111 &pd
.naddr
: &pd
.baddr
;
9113 if (x
== &pd
.baddr
|| s
== NULL
) {
9120 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9121 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9122 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
9123 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9125 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9126 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9127 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9128 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9133 if (action
== PF_SYNPROXY_DROP
) {
9138 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9139 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9141 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
9145 REASON_SET(&reason
, PFRES_MEMORY
);
9149 if (action
== PF_DROP
) {
9158 if (action
== PF_SYNPROXY_DROP
) {
9163 if (action
== PF_PASS
) {
9165 h
= mtod(m
, struct ip6_hdr
*);
9168 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9169 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9178 pf_check_congestion(struct ifqueue
*ifq
)
9185 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
9186 int flags
, const char *wchan
, void *palloc
)
9188 #pragma unused(align, ioff, flags, palloc)
9189 bzero(pp
, sizeof (*pp
));
9190 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
9191 if (pp
->pool_zone
!= NULL
) {
9192 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
9193 zone_change(pp
->pool_zone
, Z_CALLERACCT
, FALSE
);
9194 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
9195 pp
->pool_name
= wchan
;
9199 /* Zones cannot be currently destroyed */
9201 pool_destroy(struct pool
*pp
)
9207 pool_sethiwat(struct pool
*pp
, int n
)
9209 pp
->pool_hiwat
= n
; /* Currently unused */
9213 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
9215 #pragma unused(warnmess, ratecap)
9220 pool_get(struct pool
*pp
, int flags
)
9224 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9226 if (pp
->pool_count
> pp
->pool_limit
) {
9227 DPFPRINTF(PF_DEBUG_NOISY
,
9228 ("pf: pool %s hard limit reached (%d)\n",
9229 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
9235 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
9238 VERIFY(pp
->pool_count
!= 0);
9244 pool_put(struct pool
*pp
, void *v
)
9246 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9248 zfree(pp
->pool_zone
, v
);
9249 VERIFY(pp
->pool_count
!= 0);
9254 pf_find_mtag(struct mbuf
*m
)
9256 if (!(m
->m_flags
& M_PKTHDR
))
9259 return (m_pftag(m
));
9263 pf_get_mtag(struct mbuf
*m
)
9265 return (pf_find_mtag(m
));
9269 pf_time_second(void)
9278 pf_calendar_time_second(void)
9287 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
9289 struct hook_desc
*hd
;
9291 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
9298 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
9300 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
9306 hook_runloop(struct hook_desc_head
*head
, int flags
)
9308 struct hook_desc
*hd
;
9310 if (!(flags
& HOOK_REMOVE
)) {
9311 if (!(flags
& HOOK_ABORT
))
9312 TAILQ_FOREACH(hd
, head
, hd_list
)
9313 hd
->hd_fn(hd
->hd_arg
);
9315 while (!!(hd
= TAILQ_FIRST(head
))) {
9316 TAILQ_REMOVE(head
, hd
, hd_list
);
9317 if (!(flags
& HOOK_ABORT
))
9318 hd
->hd_fn(hd
->hd_arg
);
9319 if (flags
& HOOK_FREE
)
9320 _FREE(hd
, M_DEVBUF
);