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 union pf_state_xport nxport
, sxport
, dxport
;
5109 struct ip_fw_args dnflow
;
5110 struct pf_rule
*prev_matching_rule
= fwa
? fwa
->fwa_pf_rule
: NULL
;
5111 int found_prev_rule
= (prev_matching_rule
) ? 0 : 1;
5113 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
5115 if (!DUMMYNET_LOADED
)
5118 if (TAILQ_EMPTY(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
)) {
5121 bzero(&dnflow
, sizeof(dnflow
));
5128 /* Fragments don't gave protocol headers */
5129 if (!(pd
->flags
& PFDESC_IP_FRAG
))
5130 switch (pd
->proto
) {
5132 dnflow
.fwa_id
.flags
= pd
->hdr
.tcp
->th_flags
;
5133 dnflow
.fwa_id
.dst_port
= pd
->hdr
.tcp
->th_dport
;
5134 dnflow
.fwa_id
.src_port
= pd
->hdr
.tcp
->th_sport
;
5135 sxport
.port
= pd
->hdr
.tcp
->th_sport
;
5136 dxport
.port
= pd
->hdr
.tcp
->th_dport
;
5137 hdrlen
= sizeof (*th
);
5140 dnflow
.fwa_id
.dst_port
= pd
->hdr
.udp
->uh_dport
;
5141 dnflow
.fwa_id
.src_port
= pd
->hdr
.udp
->uh_sport
;
5142 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
5143 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
5144 hdrlen
= sizeof (*pd
->hdr
.udp
);
5148 if (pd
->af
!= AF_INET
)
5150 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
5151 hdrlen
= ICMP_MINLEN
;
5152 icmptype
= pd
->hdr
.icmp
->icmp_type
;
5153 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
5157 case IPPROTO_ICMPV6
:
5158 if (pd
->af
!= AF_INET6
)
5160 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
5161 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
5162 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
5163 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
5167 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
5168 sxport
.call_id
= dxport
.call_id
=
5169 pd
->hdr
.grev1
->call_id
;
5170 hdrlen
= sizeof (*pd
->hdr
.grev1
);
5175 dxport
.spi
= pd
->hdr
.esp
->spi
;
5176 hdrlen
= sizeof (*pd
->hdr
.esp
);
5180 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_DUMMYNET
].active
.ptr
);
5184 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5185 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5186 else if (r
->direction
&& r
->direction
!= direction
)
5187 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5188 else if (r
->af
&& r
->af
!= af
)
5189 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5190 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5191 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5192 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
5194 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5195 /* tcp/udp only. port_op always 0 in other cases */
5196 else if (r
->proto
== pd
->proto
&&
5197 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5198 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5199 ((r
->src
.xport
.range
.op
&&
5200 !pf_match_port(r
->src
.xport
.range
.op
,
5201 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
5203 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
5204 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
5206 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5207 /* tcp/udp only. port_op always 0 in other cases */
5208 else if (r
->proto
== pd
->proto
&&
5209 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
5210 r
->dst
.xport
.range
.op
&&
5211 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5212 !pf_match_port(r
->dst
.xport
.range
.op
,
5213 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
5215 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
5216 /* icmp only. type always 0 in other cases */
5218 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5219 r
->type
!= icmptype
+ 1))
5220 r
= TAILQ_NEXT(r
, entries
);
5221 /* icmp only. type always 0 in other cases */
5223 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5224 r
->code
!= icmpcode
+ 1))
5225 r
= TAILQ_NEXT(r
, entries
);
5226 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5227 r
= TAILQ_NEXT(r
, entries
);
5228 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
5229 r
= TAILQ_NEXT(r
, entries
);
5230 else if (pd
->proto
== IPPROTO_TCP
&&
5231 ((pd
->flags
& PFDESC_IP_FRAG
) ||
5232 (r
->flagset
& th
->th_flags
) != r
->flags
))
5233 r
= TAILQ_NEXT(r
, entries
);
5234 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5235 r
= TAILQ_NEXT(r
, entries
);
5238 * Need to go past the previous dummynet matching rule
5240 if (r
->anchor
== NULL
) {
5241 if (found_prev_rule
) {
5244 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
5245 rtableid
= r
->rtableid
;
5252 } else if (r
== prev_matching_rule
) {
5253 found_prev_rule
= 1;
5255 r
= TAILQ_NEXT(r
, entries
);
5257 pf_step_into_anchor(&asd
, &ruleset
,
5258 PF_RULESET_DUMMYNET
, &r
, &a
, &match
);
5261 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5262 PF_RULESET_DUMMYNET
, &r
, &a
, &match
))
5272 REASON_SET(&reason
, PFRES_DUMMYNET
);
5275 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
,
5279 if (r
->action
== PF_NODUMMYNET
) {
5280 int dirndx
= (direction
== PF_OUT
);
5282 r
->packets
[dirndx
]++;
5283 r
->bytes
[dirndx
] += pd
->tot_len
;
5287 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
, pd
)) {
5288 REASON_SET(&reason
, PFRES_MEMORY
);
5293 if (r
->dnpipe
&& ip_dn_io_ptr
!= NULL
) {
5294 int dirndx
= (direction
== PF_OUT
);
5296 r
->packets
[dirndx
]++;
5297 r
->bytes
[dirndx
] += pd
->tot_len
;
5299 dnflow
.fwa_cookie
= r
->dnpipe
;
5300 dnflow
.fwa_pf_rule
= r
;
5301 dnflow
.fwa_id
.addr_type
= (af
== AF_INET
) ? 4 : 6;
5302 dnflow
.fwa_id
.proto
= pd
->proto
;
5303 dnflow
.fwa_flags
= r
->dntype
;
5306 dnflow
.fwa_oif
= fwa
->fwa_oif
;
5307 dnflow
.fwa_oflags
= fwa
->fwa_oflags
;
5309 * Note that fwa_ro, fwa_dst and fwa_ipoa are
5310 * actually in a union so the following does work
5311 * for both IPv4 and IPv6
5313 dnflow
.fwa_ro
= fwa
->fwa_ro
;
5314 dnflow
.fwa_dst
= fwa
->fwa_dst
;
5315 dnflow
.fwa_ipoa
= fwa
->fwa_ipoa
;
5316 dnflow
.fwa_ro6_pmtu
= fwa
->fwa_ro6_pmtu
;
5317 dnflow
.fwa_origifp
= fwa
->fwa_origifp
;
5318 dnflow
.fwa_mtu
= fwa
->fwa_mtu
;
5319 dnflow
.fwa_alwaysfrag
= fwa
->fwa_alwaysfrag
;
5320 dnflow
.fwa_unfragpartlen
= fwa
->fwa_unfragpartlen
;
5321 dnflow
.fwa_exthdrs
= fwa
->fwa_exthdrs
;
5324 if (af
== AF_INET
) {
5325 struct ip
*iphdr
= mtod(m
, struct ip
*);
5326 NTOHS(iphdr
->ip_len
);
5327 NTOHS(iphdr
->ip_off
);
5330 * Don't need to unlock pf_lock as NET_THREAD_HELD_PF
5331 * allows for recursive behavior
5336 direction
== PF_IN
? DN_TO_IP_IN
: DN_TO_IP_OUT
:
5337 direction
== PF_IN
? DN_TO_IP6_IN
: DN_TO_IP6_OUT
,
5338 &dnflow
, DN_CLIENT_PF
);
5341 * The packet is siphoned out by dummynet so return a NULL
5342 * mbuf so the caller can still return success.
5351 #endif /* DUMMYNET */
5354 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5355 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
5356 struct pf_ruleset
**rsm
)
5359 struct pf_rule
*r
, *a
= NULL
;
5360 struct pf_ruleset
*ruleset
= NULL
;
5361 sa_family_t af
= pd
->af
;
5367 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
5370 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5371 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5372 else if (r
->direction
&& r
->direction
!= direction
)
5373 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5374 else if (r
->af
&& r
->af
!= af
)
5375 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5376 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5377 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5378 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
5380 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5381 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
5383 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5384 else if ((r
->rule_flag
& PFRULE_TOS
) && r
->tos
&&
5385 !(r
->tos
& pd
->tos
))
5386 r
= TAILQ_NEXT(r
, entries
);
5387 else if ((r
->rule_flag
& PFRULE_DSCP
) && r
->tos
&&
5388 !(r
->tos
& (pd
->tos
& DSCP_MASK
)))
5389 r
= TAILQ_NEXT(r
, entries
);
5390 else if ((r
->rule_flag
& PFRULE_SC
) && r
->tos
&&
5391 ((r
->tos
& SCIDX_MASK
) != pd
->sc
))
5392 r
= TAILQ_NEXT(r
, entries
);
5393 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
5394 r
= TAILQ_NEXT(r
, entries
);
5395 else if (pd
->proto
== IPPROTO_UDP
&&
5396 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
5397 r
= TAILQ_NEXT(r
, entries
);
5398 else if (pd
->proto
== IPPROTO_TCP
&&
5399 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
5401 r
= TAILQ_NEXT(r
, entries
);
5402 else if ((pd
->proto
== IPPROTO_ICMP
||
5403 pd
->proto
== IPPROTO_ICMPV6
) &&
5404 (r
->type
|| r
->code
))
5405 r
= TAILQ_NEXT(r
, entries
);
5406 else if (r
->prob
&& r
->prob
<= (random() % (UINT_MAX
- 1) + 1))
5407 r
= TAILQ_NEXT(r
, entries
);
5408 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5409 r
= TAILQ_NEXT(r
, entries
);
5411 if (r
->anchor
== NULL
) {
5418 r
= TAILQ_NEXT(r
, entries
);
5420 pf_step_into_anchor(&asd
, &ruleset
,
5421 PF_RULESET_FILTER
, &r
, &a
, &match
);
5423 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5424 PF_RULESET_FILTER
, &r
, &a
, &match
))
5431 REASON_SET(&reason
, PFRES_MATCH
);
5434 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
5437 if (r
->action
!= PF_PASS
)
5440 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1, NULL
)) {
5441 REASON_SET(&reason
, PFRES_MEMORY
);
5449 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
5450 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
5452 #pragma unused(direction)
5454 struct pf_pptp_state
*pptps
;
5455 struct pf_pptp_ctrl_msg cm
;
5457 struct pf_state
*gs
;
5459 u_int16_t
*pac_call_id
;
5460 u_int16_t
*pns_call_id
;
5461 u_int16_t
*spoof_call_id
;
5462 u_int8_t
*pac_state
;
5463 u_int8_t
*pns_state
;
5464 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
5466 struct pf_state_key
*sk
;
5467 struct pf_state_key
*gsk
;
5468 struct pf_app_state
*gas
;
5471 pptps
= &sk
->app_state
->u
.pptp
;
5472 gs
= pptps
->grev1_state
;
5475 gs
->expire
= pf_time_second();
5478 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
5479 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
5482 m_copydata(m
, off
, plen
, &cm
);
5484 if (ntohl(cm
.hdr
.magic
) != PF_PPTP_MAGIC_NUMBER
)
5486 if (ntohs(cm
.hdr
.type
) != 1)
5490 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
5494 memcpy(gs
, s
, sizeof (*gs
));
5496 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
5497 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
5499 TAILQ_INIT(&gs
->unlink_hooks
);
5502 gs
->pfsync_time
= 0;
5503 gs
->packets
[0] = gs
->packets
[1] = 0;
5504 gs
->bytes
[0] = gs
->bytes
[1] = 0;
5505 gs
->timeout
= PFTM_UNLINKED
;
5506 gs
->id
= gs
->creatorid
= 0;
5507 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5508 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
5510 gas
= pool_get(&pf_app_state_pl
, PR_NOWAIT
);
5512 pool_put(&pf_state_pl
, gs
);
5516 gsk
= pf_alloc_state_key(gs
, NULL
);
5518 pool_put(&pf_app_state_pl
, gas
);
5519 pool_put(&pf_state_pl
, gs
);
5523 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
5524 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
5525 memcpy(&gsk
->ext
, &sk
->ext
, sizeof (gsk
->ext
));
5527 gsk
->proto
= IPPROTO_GRE
;
5528 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
5529 gsk
->app_state
= gas
;
5530 gsk
->lan
.xport
.call_id
= 0;
5531 gsk
->gwy
.xport
.call_id
= 0;
5532 gsk
->ext
.xport
.call_id
= 0;
5533 gsk
->flowhash
= pf_calc_state_key_flowhash(gsk
);
5534 memset(gas
, 0, sizeof (*gas
));
5535 gas
->u
.grev1
.pptp_state
= s
;
5536 STATE_INC_COUNTERS(gs
);
5537 pptps
->grev1_state
= gs
;
5538 (void) hook_establish(&gs
->unlink_hooks
, 0,
5539 (hook_fn_t
) pf_grev1_unlink
, gs
);
5541 gsk
= gs
->state_key
;
5544 switch (sk
->direction
) {
5546 pns_call_id
= &gsk
->ext
.xport
.call_id
;
5547 pns_state
= &gs
->dst
.state
;
5548 pac_call_id
= &gsk
->lan
.xport
.call_id
;
5549 pac_state
= &gs
->src
.state
;
5553 pns_call_id
= &gsk
->lan
.xport
.call_id
;
5554 pns_state
= &gs
->src
.state
;
5555 pac_call_id
= &gsk
->ext
.xport
.call_id
;
5556 pac_state
= &gs
->dst
.state
;
5560 DPFPRINTF(PF_DEBUG_URGENT
,
5561 ("pf_pptp_handler: bad directional!\n"));
5568 ct
= ntohs(cm
.ctrl
.type
);
5571 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
5572 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
5573 *pns_state
= PFGRE1S_INITIATING
;
5574 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5575 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
5578 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
5579 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
5580 if (s
->nat_rule
.ptr
)
5582 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5583 &cm
.msg
.call_out_rpy
.call_id
:
5584 &cm
.msg
.call_out_rpy
.peer_call_id
;
5585 if (gs
->timeout
== PFTM_UNLINKED
) {
5586 *pac_state
= PFGRE1S_INITIATING
;
5587 op
= PF_PPTP_INSERT_GRE
;
5591 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
5592 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
5593 *pns_state
= PFGRE1S_INITIATING
;
5594 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5595 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
5598 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
5599 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
5600 *pac_state
= PFGRE1S_INITIATING
;
5601 if (s
->nat_rule
.ptr
)
5603 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5604 &cm
.msg
.call_in_2nd
.call_id
:
5605 &cm
.msg
.call_in_2nd
.peer_call_id
;
5608 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
5609 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5610 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
5611 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
5614 if (gs
->timeout
== PFTM_UNLINKED
)
5615 op
= PF_PPTP_INSERT_GRE
;
5618 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
5619 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
5620 op
= PF_PPTP_REMOVE_GRE
;
5623 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
5624 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
5625 op
= PF_PPTP_REMOVE_GRE
;
5628 case PF_PPTP_CTRL_TYPE_ERROR
:
5629 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5630 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
5633 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
5634 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
5635 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
5643 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
5644 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
5645 if (spoof_call_id
) {
5646 u_int16_t call_id
= 0;
5648 struct pf_state_key_cmp key
;
5651 key
.proto
= IPPROTO_GRE
;
5652 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
5653 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af
);
5654 PF_ACPY(&key
.ext
.addr
, &gsk
->ext
.addr
, key
.af
);
5655 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
5656 key
.ext
.xport
.call_id
= gsk
->ext
.xport
.call_id
;
5658 call_id
= htonl(random());
5661 while (pf_find_state_all(&key
, PF_IN
, 0)) {
5662 call_id
= ntohs(call_id
);
5664 if (--call_id
== 0) call_id
= 0xffff;
5665 call_id
= htons(call_id
);
5667 key
.gwy
.xport
.call_id
= call_id
;
5670 DPFPRINTF(PF_DEBUG_URGENT
,
5671 ("pf_pptp_handler: failed to spoof "
5673 key
.gwy
.xport
.call_id
= 0;
5678 gsk
->gwy
.xport
.call_id
= call_id
;
5684 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
5685 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
5686 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
5687 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5688 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
5690 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
5691 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5692 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
5695 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
5697 pptps
->grev1_state
= NULL
;
5698 STATE_DEC_COUNTERS(gs
);
5699 pool_put(&pf_state_pl
, gs
);
5702 m_copyback(m
, off
, plen
, &cm
);
5706 case PF_PPTP_REMOVE_GRE
:
5707 gs
->timeout
= PFTM_PURGE
;
5708 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5709 gsk
->lan
.xport
.call_id
= 0;
5710 gsk
->gwy
.xport
.call_id
= 0;
5711 gsk
->ext
.xport
.call_id
= 0;
5712 gs
->id
= gs
->creatorid
= 0;
5715 case PF_PPTP_INSERT_GRE
:
5716 gs
->creation
= pf_time_second();
5717 gs
->expire
= pf_time_second();
5718 gs
->timeout
= PFTM_TCP_ESTABLISHED
;
5719 if (gs
->src_node
!= NULL
) {
5720 ++gs
->src_node
->states
;
5721 VERIFY(gs
->src_node
->states
!= 0);
5723 if (gs
->nat_src_node
!= NULL
) {
5724 ++gs
->nat_src_node
->states
;
5725 VERIFY(gs
->nat_src_node
->states
!= 0);
5727 pf_set_rt_ifp(gs
, &sk
->lan
.addr
);
5728 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
5732 * FIX ME: insertion can fail when multiple PNS
5733 * behind the same NAT open calls to the same PAC
5734 * simultaneously because spoofed call ID numbers
5735 * are chosen before states are inserted. This is
5736 * hard to fix and happens infrequently enough that
5737 * users will normally try again and this ALG will
5738 * succeed. Failures are expected to be rare enough
5739 * that fixing this is a low priority.
5741 pptps
->grev1_state
= NULL
;
5742 pd
->lmw
= -1; /* Force PF_DROP on PFRES_MEMORY */
5743 pf_src_tree_remove_state(gs
);
5744 STATE_DEC_COUNTERS(gs
);
5745 pool_put(&pf_state_pl
, gs
);
5746 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
5747 "inserting GREv1 state.\n"));
5757 pf_pptp_unlink(struct pf_state
*s
)
5759 struct pf_app_state
*as
= s
->state_key
->app_state
;
5760 struct pf_state
*grev1s
= as
->u
.pptp
.grev1_state
;
5763 struct pf_app_state
*gas
= grev1s
->state_key
->app_state
;
5765 if (grev1s
->timeout
< PFTM_MAX
)
5766 grev1s
->timeout
= PFTM_PURGE
;
5767 gas
->u
.grev1
.pptp_state
= NULL
;
5768 as
->u
.pptp
.grev1_state
= NULL
;
5773 pf_grev1_unlink(struct pf_state
*s
)
5775 struct pf_app_state
*as
= s
->state_key
->app_state
;
5776 struct pf_state
*pptps
= as
->u
.grev1
.pptp_state
;
5779 struct pf_app_state
*pas
= pptps
->state_key
->app_state
;
5781 pas
->u
.pptp
.grev1_state
= NULL
;
5782 as
->u
.grev1
.pptp_state
= NULL
;
5787 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
5789 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
5790 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
5794 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
5795 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
5799 struct pf_state_key_cmp key
;
5800 struct tcphdr
*th
= pd
->hdr
.tcp
;
5801 u_int16_t win
= ntohs(th
->th_win
);
5802 u_int32_t ack
, end
, seq
, orig_seq
;
5806 struct pf_state_peer
*src
, *dst
;
5810 key
.proto
= IPPROTO_TCP
;
5811 if (direction
== PF_IN
) {
5812 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
5813 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
5814 key
.ext
.xport
.port
= th
->th_sport
;
5815 key
.gwy
.xport
.port
= th
->th_dport
;
5817 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
5818 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
5819 key
.lan
.xport
.port
= th
->th_sport
;
5820 key
.ext
.xport
.port
= th
->th_dport
;
5825 if (direction
== (*state
)->state_key
->direction
) {
5826 src
= &(*state
)->src
;
5827 dst
= &(*state
)->dst
;
5829 src
= &(*state
)->dst
;
5830 dst
= &(*state
)->src
;
5833 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
5834 if (direction
!= (*state
)->state_key
->direction
) {
5835 REASON_SET(reason
, PFRES_SYNPROXY
);
5836 return (PF_SYNPROXY_DROP
);
5838 if (th
->th_flags
& TH_SYN
) {
5839 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
5840 REASON_SET(reason
, PFRES_SYNPROXY
);
5843 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5844 pd
->src
, th
->th_dport
, th
->th_sport
,
5845 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5846 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
5848 REASON_SET(reason
, PFRES_SYNPROXY
);
5849 return (PF_SYNPROXY_DROP
);
5850 } else if (!(th
->th_flags
& TH_ACK
) ||
5851 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5852 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5853 REASON_SET(reason
, PFRES_SYNPROXY
);
5855 } else if ((*state
)->src_node
!= NULL
&&
5856 pf_src_connlimit(state
)) {
5857 REASON_SET(reason
, PFRES_SRCLIMIT
);
5860 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
5862 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
5863 struct pf_state_host
*psrc
, *pdst
;
5865 if (direction
== PF_OUT
) {
5866 psrc
= &(*state
)->state_key
->gwy
;
5867 pdst
= &(*state
)->state_key
->ext
;
5869 psrc
= &(*state
)->state_key
->ext
;
5870 pdst
= &(*state
)->state_key
->lan
;
5872 if (direction
== (*state
)->state_key
->direction
) {
5873 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
5874 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5875 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5876 REASON_SET(reason
, PFRES_SYNPROXY
);
5879 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5880 if ((*state
)->dst
.seqhi
== 1)
5881 (*state
)->dst
.seqhi
= htonl(random());
5882 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5883 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5884 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
5885 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
5886 REASON_SET(reason
, PFRES_SYNPROXY
);
5887 return (PF_SYNPROXY_DROP
);
5888 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
5890 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
5891 REASON_SET(reason
, PFRES_SYNPROXY
);
5894 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
5895 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
5896 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5897 pd
->src
, th
->th_dport
, th
->th_sport
,
5898 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
5899 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
5900 (*state
)->tag
, NULL
, NULL
);
5901 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5902 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5903 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
5904 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
5906 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
5907 (*state
)->src
.seqlo
;
5908 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
5909 (*state
)->dst
.seqlo
;
5910 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
5911 (*state
)->dst
.max_win
;
5912 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
5913 (*state
)->src
.max_win
;
5914 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
5915 (*state
)->src
.state
= (*state
)->dst
.state
=
5917 REASON_SET(reason
, PFRES_SYNPROXY
);
5918 return (PF_SYNPROXY_DROP
);
5922 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
5923 dst
->state
>= TCPS_FIN_WAIT_2
&&
5924 src
->state
>= TCPS_FIN_WAIT_2
) {
5925 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
5926 printf("pf: state reuse ");
5927 pf_print_state(*state
);
5928 pf_print_flags(th
->th_flags
);
5931 /* XXX make sure it's the same direction ?? */
5932 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
5933 pf_unlink_state(*state
);
5938 if (src
->wscale
&& dst
->wscale
&& !(th
->th_flags
& TH_SYN
)) {
5939 sws
= src
->wscale
& PF_WSCALE_MASK
;
5940 dws
= dst
->wscale
& PF_WSCALE_MASK
;
5945 * Sequence tracking algorithm from Guido van Rooij's paper:
5946 * http://www.madison-gurkha.com/publications/tcp_filtering/
5950 orig_seq
= seq
= ntohl(th
->th_seq
);
5951 if (src
->seqlo
== 0) {
5952 /* First packet from this end. Set its state */
5954 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
5955 src
->scrub
== NULL
) {
5956 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
5957 REASON_SET(reason
, PFRES_MEMORY
);
5962 /* Deferred generation of sequence number modulator */
5963 if (dst
->seqdiff
&& !src
->seqdiff
) {
5964 /* use random iss for the TCP server */
5965 while ((src
->seqdiff
= random() - seq
) == 0)
5967 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
5968 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
5970 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
5971 copyback
= off
+ sizeof (*th
);
5973 ack
= ntohl(th
->th_ack
);
5976 end
= seq
+ pd
->p_len
;
5977 if (th
->th_flags
& TH_SYN
) {
5979 if (dst
->wscale
& PF_WSCALE_FLAG
) {
5980 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
5982 if (src
->wscale
& PF_WSCALE_FLAG
) {
5984 * Remove scale factor from initial
5987 sws
= src
->wscale
& PF_WSCALE_MASK
;
5988 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
5990 dws
= dst
->wscale
& PF_WSCALE_MASK
;
5993 * Window scale negotiation has failed,
5994 * therefore we must restore the window
5995 * scale in the state record that we
5996 * optimistically removed in
5997 * pf_test_rule(). Care is required to
5998 * prevent arithmetic overflow from
5999 * zeroing the window when it's
6000 * truncated down to 16-bits.
6002 u_int32_t max_win
= dst
->max_win
;
6004 dst
->wscale
& PF_WSCALE_MASK
;
6005 dst
->max_win
= MIN(0xffff, max_win
);
6006 /* in case of a retrans SYN|ACK */
6011 if (th
->th_flags
& TH_FIN
)
6015 if (src
->state
< TCPS_SYN_SENT
)
6016 src
->state
= TCPS_SYN_SENT
;
6019 * May need to slide the window (seqhi may have been set by
6020 * the crappy stack check or if we picked up the connection
6021 * after establishment)
6023 if (src
->seqhi
== 1 ||
6024 SEQ_GEQ(end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
),
6026 src
->seqhi
= end
+ MAX(1, (u_int32_t
)dst
->max_win
<< dws
);
6027 if (win
> src
->max_win
)
6031 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6033 /* Modulate sequence numbers */
6034 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6036 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6037 copyback
= off
+ sizeof (*th
);
6039 end
= seq
+ pd
->p_len
;
6040 if (th
->th_flags
& TH_SYN
)
6042 if (th
->th_flags
& TH_FIN
)
6046 if ((th
->th_flags
& TH_ACK
) == 0) {
6047 /* Let it pass through the ack skew check */
6049 } else if ((ack
== 0 &&
6050 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6051 /* broken tcp stacks do not set ack */
6052 (dst
->state
< TCPS_SYN_SENT
)) {
6054 * Many stacks (ours included) will set the ACK number in an
6055 * FIN|ACK if the SYN times out -- no sequence to ACK.
6061 /* Ease sequencing restrictions on no data packets */
6066 ackskew
= dst
->seqlo
- ack
;
6070 * Need to demodulate the sequence numbers in any TCP SACK options
6071 * (Selective ACK). We could optionally validate the SACK values
6072 * against the current ACK window, either forwards or backwards, but
6073 * I'm not confident that SACK has been implemented properly
6074 * everywhere. It wouldn't surprise me if several stacks accidently
6075 * SACK too far backwards of previously ACKed data. There really aren't
6076 * any security implications of bad SACKing unless the target stack
6077 * doesn't validate the option length correctly. Someone trying to
6078 * spoof into a TCP connection won't bother blindly sending SACK
6081 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6082 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6083 if (copyback
== -1) {
6084 REASON_SET(reason
, PFRES_MEMORY
);
6092 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6093 if (SEQ_GEQ(src
->seqhi
, end
) &&
6094 /* Last octet inside other's window space */
6095 SEQ_GEQ(seq
, src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) &&
6096 /* Retrans: not more than one window back */
6097 (ackskew
>= -MAXACKWINDOW
) &&
6098 /* Acking not more than one reassembled fragment backwards */
6099 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6100 /* Acking not more than one window forward */
6101 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6102 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6103 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6104 /* Require an exact/+1 sequence match on resets when possible */
6106 if (dst
->scrub
|| src
->scrub
) {
6107 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6108 *state
, src
, dst
, ©back
))
6114 /* update max window */
6115 if (src
->max_win
< win
)
6117 /* synchronize sequencing */
6118 if (SEQ_GT(end
, src
->seqlo
))
6120 /* slide the window of what the other end can send */
6121 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6122 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6125 if (th
->th_flags
& TH_SYN
)
6126 if (src
->state
< TCPS_SYN_SENT
)
6127 src
->state
= TCPS_SYN_SENT
;
6128 if (th
->th_flags
& TH_FIN
)
6129 if (src
->state
< TCPS_CLOSING
)
6130 src
->state
= TCPS_CLOSING
;
6131 if (th
->th_flags
& TH_ACK
) {
6132 if (dst
->state
== TCPS_SYN_SENT
) {
6133 dst
->state
= TCPS_ESTABLISHED
;
6134 if (src
->state
== TCPS_ESTABLISHED
&&
6135 (*state
)->src_node
!= NULL
&&
6136 pf_src_connlimit(state
)) {
6137 REASON_SET(reason
, PFRES_SRCLIMIT
);
6140 } else if (dst
->state
== TCPS_CLOSING
)
6141 dst
->state
= TCPS_FIN_WAIT_2
;
6143 if (th
->th_flags
& TH_RST
)
6144 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6146 /* update expire time */
6147 (*state
)->expire
= pf_time_second();
6148 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6149 dst
->state
>= TCPS_FIN_WAIT_2
)
6150 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6151 else if (src
->state
>= TCPS_CLOSING
&&
6152 dst
->state
>= TCPS_CLOSING
)
6153 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6154 else if (src
->state
< TCPS_ESTABLISHED
||
6155 dst
->state
< TCPS_ESTABLISHED
)
6156 (*state
)->timeout
= PFTM_TCP_OPENING
;
6157 else if (src
->state
>= TCPS_CLOSING
||
6158 dst
->state
>= TCPS_CLOSING
)
6159 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6161 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6163 /* Fall through to PASS packet */
6165 } else if ((dst
->state
< TCPS_SYN_SENT
||
6166 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6167 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6168 /* Within a window forward of the originating packet */
6169 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6170 /* Within a window backward of the originating packet */
6173 * This currently handles three situations:
6174 * 1) Stupid stacks will shotgun SYNs before their peer
6176 * 2) When PF catches an already established stream (the
6177 * firewall rebooted, the state table was flushed, routes
6179 * 3) Packets get funky immediately after the connection
6180 * closes (this should catch Solaris spurious ACK|FINs
6181 * that web servers like to spew after a close)
6183 * This must be a little more careful than the above code
6184 * since packet floods will also be caught here. We don't
6185 * update the TTL here to mitigate the damage of a packet
6186 * flood and so the same code can handle awkward establishment
6187 * and a loosened connection close.
6188 * In the establishment case, a correct peer response will
6189 * validate the connection, go through the normal state code
6190 * and keep updating the state TTL.
6193 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6194 printf("pf: loose state match: ");
6195 pf_print_state(*state
);
6196 pf_print_flags(th
->th_flags
);
6197 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6198 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6199 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6200 (*state
)->packets
[1],
6201 direction
== PF_IN
? "in" : "out",
6202 direction
== (*state
)->state_key
->direction
?
6206 if (dst
->scrub
|| src
->scrub
) {
6207 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6208 *state
, src
, dst
, ©back
))
6213 /* update max window */
6214 if (src
->max_win
< win
)
6216 /* synchronize sequencing */
6217 if (SEQ_GT(end
, src
->seqlo
))
6219 /* slide the window of what the other end can send */
6220 if (SEQ_GEQ(ack
+ ((u_int32_t
)win
<< sws
), dst
->seqhi
))
6221 dst
->seqhi
= ack
+ MAX(((u_int32_t
)win
<< sws
), 1);
6224 * Cannot set dst->seqhi here since this could be a shotgunned
6225 * SYN and not an already established connection.
6228 if (th
->th_flags
& TH_FIN
)
6229 if (src
->state
< TCPS_CLOSING
)
6230 src
->state
= TCPS_CLOSING
;
6231 if (th
->th_flags
& TH_RST
)
6232 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6234 /* Fall through to PASS packet */
6237 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
6238 (*state
)->src
.state
== TCPS_SYN_SENT
) {
6239 /* Send RST for state mismatches during handshake */
6240 if (!(th
->th_flags
& TH_RST
))
6241 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6242 pd
->dst
, pd
->src
, th
->th_dport
,
6243 th
->th_sport
, ntohl(th
->th_ack
), 0,
6245 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
6246 pd
->eh
, kif
->pfik_ifp
);
6250 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6251 printf("pf: BAD state: ");
6252 pf_print_state(*state
);
6253 pf_print_flags(th
->th_flags
);
6254 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6255 "pkts=%llu:%llu dir=%s,%s\n",
6256 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
6257 (*state
)->packets
[0], (*state
)->packets
[1],
6258 direction
== PF_IN
? "in" : "out",
6259 direction
== (*state
)->state_key
->direction
?
6261 printf("pf: State failure on: %c %c %c %c | %c %c\n",
6262 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
6264 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
)) ?
6266 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
6267 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
6268 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
6269 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
6271 REASON_SET(reason
, PFRES_BADSTATE
);
6275 /* Any packets which have gotten here are to be passed */
6277 if ((*state
)->state_key
->app_state
&&
6278 (*state
)->state_key
->app_state
->handler
) {
6279 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6280 off
+ (th
->th_off
<< 2), pd
, kif
);
6282 REASON_SET(reason
, PFRES_MEMORY
);
6288 /* translate source/destination address, if necessary */
6289 if (STATE_TRANSLATE((*state
)->state_key
)) {
6290 if (direction
== PF_OUT
)
6291 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
6292 pd
->ip_sum
, &th
->th_sum
,
6293 &(*state
)->state_key
->gwy
.addr
,
6294 (*state
)->state_key
->gwy
.xport
.port
, 0, pd
->af
);
6296 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &th
->th_dport
,
6297 pd
->ip_sum
, &th
->th_sum
,
6298 &(*state
)->state_key
->lan
.addr
,
6299 (*state
)->state_key
->lan
.xport
.port
, 0, pd
->af
);
6300 copyback
= off
+ sizeof (*th
);
6304 m
= pf_lazy_makewritable(pd
, m
, copyback
);
6306 REASON_SET(reason
, PFRES_MEMORY
);
6310 /* Copyback sequence modulation or stateful scrub changes */
6311 m_copyback(m
, off
, sizeof (*th
), th
);
6318 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6319 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6322 struct pf_state_peer
*src
, *dst
;
6323 struct pf_state_key_cmp key
;
6324 struct udphdr
*uh
= pd
->hdr
.udp
;
6325 struct pf_app_state as
;
6326 int dx
, action
, extfilter
;
6328 key
.proto_variant
= PF_EXTFILTER_APD
;
6331 key
.proto
= IPPROTO_UDP
;
6332 if (direction
== PF_IN
) {
6333 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6334 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6335 key
.ext
.xport
.port
= uh
->uh_sport
;
6336 key
.gwy
.xport
.port
= uh
->uh_dport
;
6339 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6340 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6341 key
.lan
.xport
.port
= uh
->uh_sport
;
6342 key
.ext
.xport
.port
= uh
->uh_dport
;
6346 if (ntohs(uh
->uh_sport
) == PF_IKE_PORT
&&
6347 ntohs(uh
->uh_dport
) == PF_IKE_PORT
) {
6348 struct pf_ike_hdr ike
;
6349 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
6350 if (plen
< PF_IKE_PACKET_MINSIZE
) {
6351 DPFPRINTF(PF_DEBUG_MISC
,
6352 ("pf: IKE message too small.\n"));
6356 if (plen
> sizeof (ike
))
6357 plen
= sizeof (ike
);
6358 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
6360 if (ike
.initiator_cookie
) {
6361 key
.app_state
= &as
;
6362 as
.compare_lan_ext
= pf_ike_compare
;
6363 as
.compare_ext_gwy
= pf_ike_compare
;
6364 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6367 * <http://tools.ietf.org/html/\
6368 * draft-ietf-ipsec-nat-t-ike-01>
6369 * Support non-standard NAT-T implementations that
6370 * push the ESP packet over the top of the IKE packet.
6371 * Do not drop packet.
6373 DPFPRINTF(PF_DEBUG_MISC
,
6374 ("pf: IKE initiator cookie = 0.\n"));
6378 *state
= pf_find_state(kif
, &key
, dx
);
6380 if (!key
.app_state
&& *state
== 0) {
6381 key
.proto_variant
= PF_EXTFILTER_AD
;
6382 *state
= pf_find_state(kif
, &key
, dx
);
6385 if (!key
.app_state
&& *state
== 0) {
6386 key
.proto_variant
= PF_EXTFILTER_EI
;
6387 *state
= pf_find_state(kif
, &key
, dx
);
6390 if ((*state
) != NULL
&& pd
!= NULL
&&
6392 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6394 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6397 if (direction
== (*state
)->state_key
->direction
) {
6398 src
= &(*state
)->src
;
6399 dst
= &(*state
)->dst
;
6401 src
= &(*state
)->dst
;
6402 dst
= &(*state
)->src
;
6406 if (src
->state
< PFUDPS_SINGLE
)
6407 src
->state
= PFUDPS_SINGLE
;
6408 if (dst
->state
== PFUDPS_SINGLE
)
6409 dst
->state
= PFUDPS_MULTIPLE
;
6411 /* update expire time */
6412 (*state
)->expire
= pf_time_second();
6413 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
6414 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
6416 (*state
)->timeout
= PFTM_UDP_SINGLE
;
6418 extfilter
= (*state
)->state_key
->proto_variant
;
6419 if (extfilter
> PF_EXTFILTER_APD
) {
6420 (*state
)->state_key
->ext
.xport
.port
= key
.ext
.xport
.port
;
6421 if (extfilter
> PF_EXTFILTER_AD
)
6422 PF_ACPY(&(*state
)->state_key
->ext
.addr
,
6423 &key
.ext
.addr
, key
.af
);
6426 if ((*state
)->state_key
->app_state
&&
6427 (*state
)->state_key
->app_state
->handler
) {
6428 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6429 off
+ uh
->uh_ulen
, pd
, kif
);
6431 REASON_SET(reason
, PFRES_MEMORY
);
6437 /* translate source/destination address, if necessary */
6438 if (STATE_TRANSLATE((*state
)->state_key
)) {
6439 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
6441 REASON_SET(reason
, PFRES_MEMORY
);
6445 if (direction
== PF_OUT
)
6446 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
6447 pd
->ip_sum
, &uh
->uh_sum
,
6448 &(*state
)->state_key
->gwy
.addr
,
6449 (*state
)->state_key
->gwy
.xport
.port
, 1, pd
->af
);
6451 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &uh
->uh_dport
,
6452 pd
->ip_sum
, &uh
->uh_sum
,
6453 &(*state
)->state_key
->lan
.addr
,
6454 (*state
)->state_key
->lan
.xport
.port
, 1, pd
->af
);
6455 m_copyback(m
, off
, sizeof (*uh
), uh
);
6462 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6463 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6466 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
6467 u_int16_t icmpid
= 0, *icmpsum
;
6470 struct pf_state_key_cmp key
;
6472 struct pf_app_state as
;
6475 switch (pd
->proto
) {
6478 icmptype
= pd
->hdr
.icmp
->icmp_type
;
6479 icmpid
= pd
->hdr
.icmp
->icmp_id
;
6480 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
6482 if (icmptype
== ICMP_UNREACH
||
6483 icmptype
== ICMP_SOURCEQUENCH
||
6484 icmptype
== ICMP_REDIRECT
||
6485 icmptype
== ICMP_TIMXCEED
||
6486 icmptype
== ICMP_PARAMPROB
)
6491 case IPPROTO_ICMPV6
:
6492 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
6493 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
6494 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
6496 if (icmptype
== ICMP6_DST_UNREACH
||
6497 icmptype
== ICMP6_PACKET_TOO_BIG
||
6498 icmptype
== ICMP6_TIME_EXCEEDED
||
6499 icmptype
== ICMP6_PARAM_PROB
)
6508 * ICMP query/reply message not related to a TCP/UDP packet.
6509 * Search for an ICMP state.
6512 key
.proto
= pd
->proto
;
6513 if (direction
== PF_IN
) {
6514 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6515 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6516 key
.ext
.xport
.port
= 0;
6517 key
.gwy
.xport
.port
= icmpid
;
6519 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6520 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6521 key
.lan
.xport
.port
= icmpid
;
6522 key
.ext
.xport
.port
= 0;
6527 (*state
)->expire
= pf_time_second();
6528 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
6530 /* translate source/destination address, if necessary */
6531 if (STATE_TRANSLATE((*state
)->state_key
)) {
6532 if (direction
== PF_OUT
) {
6536 pf_change_a(&saddr
->v4
.s_addr
,
6538 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
6539 pd
->hdr
.icmp
->icmp_cksum
=
6541 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6542 (*state
)->state_key
->gwy
.xport
.port
, 0);
6543 pd
->hdr
.icmp
->icmp_id
=
6544 (*state
)->state_key
->gwy
.xport
.port
;
6545 m
= pf_lazy_makewritable(pd
, m
,
6549 m_copyback(m
, off
, ICMP_MINLEN
,
6556 &pd
->hdr
.icmp6
->icmp6_cksum
,
6557 &(*state
)->state_key
->gwy
.addr
, 0);
6558 m
= pf_lazy_makewritable(pd
, m
,
6559 off
+ sizeof (struct icmp6_hdr
));
6563 sizeof (struct icmp6_hdr
),
6572 pf_change_a(&daddr
->v4
.s_addr
,
6574 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
6575 pd
->hdr
.icmp
->icmp_cksum
=
6577 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6578 (*state
)->state_key
->lan
.xport
.port
, 0);
6579 pd
->hdr
.icmp
->icmp_id
=
6580 (*state
)->state_key
->lan
.xport
.port
;
6581 m
= pf_lazy_makewritable(pd
, m
,
6585 m_copyback(m
, off
, ICMP_MINLEN
,
6592 &pd
->hdr
.icmp6
->icmp6_cksum
,
6593 &(*state
)->state_key
->lan
.addr
, 0);
6594 m
= pf_lazy_makewritable(pd
, m
,
6595 off
+ sizeof (struct icmp6_hdr
));
6599 sizeof (struct icmp6_hdr
),
6611 * ICMP error message in response to a TCP/UDP packet.
6612 * Extract the inner TCP/UDP header and search for that state.
6615 struct pf_pdesc pd2
;
6620 struct ip6_hdr h2_6
;
6626 memset(&pd2
, 0, sizeof (pd2
));
6632 /* offset of h2 in mbuf chain */
6633 ipoff2
= off
+ ICMP_MINLEN
;
6635 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
6636 NULL
, reason
, pd2
.af
)) {
6637 DPFPRINTF(PF_DEBUG_MISC
,
6638 ("pf: ICMP error message too short "
6643 * ICMP error messages don't refer to non-first
6646 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
6647 REASON_SET(reason
, PFRES_FRAG
);
6651 /* offset of protocol header that follows h2 */
6652 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
6654 pd2
.proto
= h2
.ip_p
;
6655 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
6656 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
6657 pd2
.ip_sum
= &h2
.ip_sum
;
6662 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
6664 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
6665 NULL
, reason
, pd2
.af
)) {
6666 DPFPRINTF(PF_DEBUG_MISC
,
6667 ("pf: ICMP error message too short "
6671 pd2
.proto
= h2_6
.ip6_nxt
;
6672 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
6673 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
6675 off2
= ipoff2
+ sizeof (h2_6
);
6677 switch (pd2
.proto
) {
6678 case IPPROTO_FRAGMENT
:
6680 * ICMPv6 error messages for
6681 * non-first fragments
6683 REASON_SET(reason
, PFRES_FRAG
);
6686 case IPPROTO_HOPOPTS
:
6687 case IPPROTO_ROUTING
:
6688 case IPPROTO_DSTOPTS
: {
6689 /* get next header and header length */
6690 struct ip6_ext opt6
;
6692 if (!pf_pull_hdr(m
, off2
, &opt6
,
6693 sizeof (opt6
), NULL
, reason
,
6695 DPFPRINTF(PF_DEBUG_MISC
,
6696 ("pf: ICMPv6 short opt\n"));
6699 if (pd2
.proto
== IPPROTO_AH
)
6700 off2
+= (opt6
.ip6e_len
+ 2) * 4;
6702 off2
+= (opt6
.ip6e_len
+ 1) * 8;
6703 pd2
.proto
= opt6
.ip6e_nxt
;
6704 /* goto the next header */
6711 } while (!terminal
);
6716 switch (pd2
.proto
) {
6720 struct pf_state_peer
*src
, *dst
;
6725 * Only the first 8 bytes of the TCP header can be
6726 * expected. Don't access any TCP header fields after
6727 * th_seq, an ackskew test is not possible.
6729 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
6731 DPFPRINTF(PF_DEBUG_MISC
,
6732 ("pf: ICMP error message too short "
6738 key
.proto
= IPPROTO_TCP
;
6739 if (direction
== PF_IN
) {
6740 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6741 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6742 key
.ext
.xport
.port
= th
.th_dport
;
6743 key
.gwy
.xport
.port
= th
.th_sport
;
6745 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6746 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6747 key
.lan
.xport
.port
= th
.th_dport
;
6748 key
.ext
.xport
.port
= th
.th_sport
;
6753 if (direction
== (*state
)->state_key
->direction
) {
6754 src
= &(*state
)->dst
;
6755 dst
= &(*state
)->src
;
6757 src
= &(*state
)->src
;
6758 dst
= &(*state
)->dst
;
6761 if (src
->wscale
&& dst
->wscale
)
6762 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6766 /* Demodulate sequence number */
6767 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
6769 pf_change_a(&th
.th_seq
, icmpsum
,
6774 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
6776 src
->seqlo
- ((u_int32_t
)dst
->max_win
<< dws
))) {
6777 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6778 printf("pf: BAD ICMP %d:%d ",
6779 icmptype
, pd
->hdr
.icmp
->icmp_code
);
6780 pf_print_host(pd
->src
, 0, pd
->af
);
6782 pf_print_host(pd
->dst
, 0, pd
->af
);
6784 pf_print_state(*state
);
6785 printf(" seq=%u\n", seq
);
6787 REASON_SET(reason
, PFRES_BADSTATE
);
6791 if (STATE_TRANSLATE((*state
)->state_key
)) {
6792 if (direction
== PF_IN
) {
6793 pf_change_icmp(pd2
.src
, &th
.th_sport
,
6794 daddr
, &(*state
)->state_key
->lan
.addr
,
6795 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6796 pd2
.ip_sum
, icmpsum
,
6797 pd
->ip_sum
, 0, pd2
.af
);
6799 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
6800 saddr
, &(*state
)->state_key
->gwy
.addr
,
6801 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6802 pd2
.ip_sum
, icmpsum
,
6803 pd
->ip_sum
, 0, pd2
.af
);
6809 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
6815 m_copyback(m
, off
, ICMP_MINLEN
,
6817 m_copyback(m
, ipoff2
, sizeof (h2
),
6824 sizeof (struct icmp6_hdr
),
6826 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6831 m_copyback(m
, off2
, 8, &th
);
6840 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
6841 NULL
, reason
, pd2
.af
)) {
6842 DPFPRINTF(PF_DEBUG_MISC
,
6843 ("pf: ICMP error message too short "
6849 key
.proto
= IPPROTO_UDP
;
6850 if (direction
== PF_IN
) {
6851 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6852 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6853 key
.ext
.xport
.port
= uh
.uh_dport
;
6854 key
.gwy
.xport
.port
= uh
.uh_sport
;
6857 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6858 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6859 key
.lan
.xport
.port
= uh
.uh_dport
;
6860 key
.ext
.xport
.port
= uh
.uh_sport
;
6864 key
.proto_variant
= PF_EXTFILTER_APD
;
6866 if (ntohs(uh
.uh_sport
) == PF_IKE_PORT
&&
6867 ntohs(uh
.uh_dport
) == PF_IKE_PORT
) {
6868 struct pf_ike_hdr ike
;
6870 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
6871 if (direction
== PF_IN
&&
6872 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
6873 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
6874 "ICMP error, embedded IKE message "
6879 if (plen
> sizeof (ike
))
6880 plen
= sizeof (ike
);
6881 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
6883 key
.app_state
= &as
;
6884 as
.compare_lan_ext
= pf_ike_compare
;
6885 as
.compare_ext_gwy
= pf_ike_compare
;
6886 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6889 *state
= pf_find_state(kif
, &key
, dx
);
6891 if (key
.app_state
&& *state
== 0) {
6893 *state
= pf_find_state(kif
, &key
, dx
);
6897 key
.proto_variant
= PF_EXTFILTER_AD
;
6898 *state
= pf_find_state(kif
, &key
, dx
);
6902 key
.proto_variant
= PF_EXTFILTER_EI
;
6903 *state
= pf_find_state(kif
, &key
, dx
);
6906 if (*state
!= NULL
&& pd
!= NULL
&&
6908 pd
->flowhash
= (*state
)->state_key
->flowhash
;
6910 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6913 if (STATE_TRANSLATE((*state
)->state_key
)) {
6914 if (direction
== PF_IN
) {
6915 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
6916 daddr
, &(*state
)->state_key
->lan
.addr
,
6917 (*state
)->state_key
->lan
.xport
.port
, &uh
.uh_sum
,
6918 pd2
.ip_sum
, icmpsum
,
6919 pd
->ip_sum
, 1, pd2
.af
);
6921 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
6922 saddr
, &(*state
)->state_key
->gwy
.addr
,
6923 (*state
)->state_key
->gwy
.xport
.port
, &uh
.uh_sum
,
6924 pd2
.ip_sum
, icmpsum
,
6925 pd
->ip_sum
, 1, pd2
.af
);
6927 m
= pf_lazy_makewritable(pd
, m
,
6928 off2
+ sizeof (uh
));
6934 m_copyback(m
, off
, ICMP_MINLEN
,
6936 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
6942 sizeof (struct icmp6_hdr
),
6944 m_copyback(m
, ipoff2
, sizeof (h2_6
),
6949 m_copyback(m
, off2
, sizeof (uh
), &uh
);
6956 case IPPROTO_ICMP
: {
6959 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
6960 NULL
, reason
, pd2
.af
)) {
6961 DPFPRINTF(PF_DEBUG_MISC
,
6962 ("pf: ICMP error message too short i"
6968 key
.proto
= IPPROTO_ICMP
;
6969 if (direction
== PF_IN
) {
6970 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6971 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6972 key
.ext
.xport
.port
= 0;
6973 key
.gwy
.xport
.port
= iih
.icmp_id
;
6975 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6976 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6977 key
.lan
.xport
.port
= iih
.icmp_id
;
6978 key
.ext
.xport
.port
= 0;
6983 if (STATE_TRANSLATE((*state
)->state_key
)) {
6984 if (direction
== PF_IN
) {
6985 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
6986 daddr
, &(*state
)->state_key
->lan
.addr
,
6987 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6988 pd2
.ip_sum
, icmpsum
,
6989 pd
->ip_sum
, 0, AF_INET
);
6991 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
6992 saddr
, &(*state
)->state_key
->gwy
.addr
,
6993 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
6994 pd2
.ip_sum
, icmpsum
,
6995 pd
->ip_sum
, 0, AF_INET
);
6997 m
= pf_lazy_makewritable(pd
, m
, off2
+ ICMP_MINLEN
);
7000 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
7001 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7002 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
7010 case IPPROTO_ICMPV6
: {
7011 struct icmp6_hdr iih
;
7013 if (!pf_pull_hdr(m
, off2
, &iih
,
7014 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
7015 DPFPRINTF(PF_DEBUG_MISC
,
7016 ("pf: ICMP error message too short "
7022 key
.proto
= IPPROTO_ICMPV6
;
7023 if (direction
== PF_IN
) {
7024 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7025 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7026 key
.ext
.xport
.port
= 0;
7027 key
.gwy
.xport
.port
= iih
.icmp6_id
;
7029 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7030 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7031 key
.lan
.xport
.port
= iih
.icmp6_id
;
7032 key
.ext
.xport
.port
= 0;
7037 if (STATE_TRANSLATE((*state
)->state_key
)) {
7038 if (direction
== PF_IN
) {
7039 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
7040 daddr
, &(*state
)->state_key
->lan
.addr
,
7041 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7042 pd2
.ip_sum
, icmpsum
,
7043 pd
->ip_sum
, 0, AF_INET6
);
7045 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
7046 saddr
, &(*state
)->state_key
->gwy
.addr
,
7047 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7048 pd2
.ip_sum
, icmpsum
,
7049 pd
->ip_sum
, 0, AF_INET6
);
7051 m
= pf_lazy_makewritable(pd
, m
, off2
+
7052 sizeof (struct icmp6_hdr
));
7055 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
7057 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
7058 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
7068 key
.proto
= pd2
.proto
;
7069 if (direction
== PF_IN
) {
7070 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7071 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7072 key
.ext
.xport
.port
= 0;
7073 key
.gwy
.xport
.port
= 0;
7075 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7076 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7077 key
.lan
.xport
.port
= 0;
7078 key
.ext
.xport
.port
= 0;
7083 if (STATE_TRANSLATE((*state
)->state_key
)) {
7084 if (direction
== PF_IN
) {
7085 pf_change_icmp(pd2
.src
, NULL
,
7086 daddr
, &(*state
)->state_key
->lan
.addr
,
7088 pd2
.ip_sum
, icmpsum
,
7089 pd
->ip_sum
, 0, pd2
.af
);
7091 pf_change_icmp(pd2
.dst
, NULL
,
7092 saddr
, &(*state
)->state_key
->gwy
.addr
,
7094 pd2
.ip_sum
, icmpsum
,
7095 pd
->ip_sum
, 0, pd2
.af
);
7100 m
= pf_lazy_makewritable(pd
, m
,
7101 ipoff2
+ sizeof (h2
));
7107 m
= pf_lazy_makewritable(pd
, m
,
7108 ipoff2
+ sizeof (h2_6
));
7112 sizeof (struct icmp6_hdr
),
7114 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7129 pf_test_state_grev1(struct pf_state
**state
, int direction
,
7130 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
7132 struct pf_state_peer
*src
;
7133 struct pf_state_peer
*dst
;
7134 struct pf_state_key_cmp key
;
7135 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
7140 key
.proto
= IPPROTO_GRE
;
7141 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
7142 if (direction
== PF_IN
) {
7143 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7144 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7145 key
.gwy
.xport
.call_id
= grev1
->call_id
;
7147 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7148 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7149 key
.ext
.xport
.call_id
= grev1
->call_id
;
7154 if (direction
== (*state
)->state_key
->direction
) {
7155 src
= &(*state
)->src
;
7156 dst
= &(*state
)->dst
;
7158 src
= &(*state
)->dst
;
7159 dst
= &(*state
)->src
;
7163 if (src
->state
< PFGRE1S_INITIATING
)
7164 src
->state
= PFGRE1S_INITIATING
;
7166 /* update expire time */
7167 (*state
)->expire
= pf_time_second();
7168 if (src
->state
>= PFGRE1S_INITIATING
&&
7169 dst
->state
>= PFGRE1S_INITIATING
) {
7170 if ((*state
)->timeout
!= PFTM_TCP_ESTABLISHED
)
7171 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
7172 src
->state
= PFGRE1S_ESTABLISHED
;
7173 dst
->state
= PFGRE1S_ESTABLISHED
;
7175 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
7178 if ((*state
)->state_key
->app_state
)
7179 (*state
)->state_key
->app_state
->u
.grev1
.pptp_state
->expire
=
7182 /* translate source/destination address, if necessary */
7183 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
7184 if (direction
== PF_OUT
) {
7188 pf_change_a(&pd
->src
->v4
.s_addr
,
7190 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7195 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7201 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
7206 pf_change_a(&pd
->dst
->v4
.s_addr
,
7208 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7213 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7220 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
7223 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
7230 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7231 int off
, struct pf_pdesc
*pd
)
7234 struct pf_state_peer
*src
;
7235 struct pf_state_peer
*dst
;
7236 struct pf_state_key_cmp key
;
7237 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
7240 memset(&key
, 0, sizeof (key
));
7242 key
.proto
= IPPROTO_ESP
;
7243 if (direction
== PF_IN
) {
7244 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7245 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7246 key
.gwy
.xport
.spi
= esp
->spi
;
7248 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7249 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7250 key
.ext
.xport
.spi
= esp
->spi
;
7253 *state
= pf_find_state(kif
, &key
, direction
);
7260 * No matching state. Look for a blocking state. If we find
7261 * one, then use that state and move it so that it's keyed to
7262 * the SPI in the current packet.
7264 if (direction
== PF_IN
) {
7265 key
.gwy
.xport
.spi
= 0;
7267 s
= pf_find_state(kif
, &key
, direction
);
7269 struct pf_state_key
*sk
= s
->state_key
;
7271 RB_REMOVE(pf_state_tree_ext_gwy
,
7272 &pf_statetbl_ext_gwy
, sk
);
7273 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
7276 if (RB_INSERT(pf_state_tree_ext_gwy
,
7277 &pf_statetbl_ext_gwy
, sk
))
7278 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
7283 key
.ext
.xport
.spi
= 0;
7285 s
= pf_find_state(kif
, &key
, direction
);
7287 struct pf_state_key
*sk
= s
->state_key
;
7289 RB_REMOVE(pf_state_tree_lan_ext
,
7290 &pf_statetbl_lan_ext
, sk
);
7291 sk
->ext
.xport
.spi
= esp
->spi
;
7293 if (RB_INSERT(pf_state_tree_lan_ext
,
7294 &pf_statetbl_lan_ext
, sk
))
7295 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
7304 if (s
->creatorid
== pf_status
.hostid
)
7305 pfsync_delete_state(s
);
7307 s
->timeout
= PFTM_UNLINKED
;
7308 hook_runloop(&s
->unlink_hooks
,
7309 HOOK_REMOVE
|HOOK_FREE
);
7310 pf_src_tree_remove_state(s
);
7317 if (*state
!= NULL
&& pd
!= NULL
&&
7318 pd
->flowhash
== 0) {
7319 pd
->flowhash
= (*state
)->state_key
->flowhash
;
7322 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7325 if (direction
== (*state
)->state_key
->direction
) {
7326 src
= &(*state
)->src
;
7327 dst
= &(*state
)->dst
;
7329 src
= &(*state
)->dst
;
7330 dst
= &(*state
)->src
;
7334 if (src
->state
< PFESPS_INITIATING
)
7335 src
->state
= PFESPS_INITIATING
;
7337 /* update expire time */
7338 (*state
)->expire
= pf_time_second();
7339 if (src
->state
>= PFESPS_INITIATING
&&
7340 dst
->state
>= PFESPS_INITIATING
) {
7341 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
7342 src
->state
= PFESPS_ESTABLISHED
;
7343 dst
->state
= PFESPS_ESTABLISHED
;
7345 (*state
)->timeout
= PFTM_ESP_INITIATING
;
7347 /* translate source/destination address, if necessary */
7348 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7349 if (direction
== PF_OUT
) {
7353 pf_change_a(&pd
->src
->v4
.s_addr
,
7355 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7360 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7369 pf_change_a(&pd
->dst
->v4
.s_addr
,
7371 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7376 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7388 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7389 struct pf_pdesc
*pd
)
7391 struct pf_state_peer
*src
, *dst
;
7392 struct pf_state_key_cmp key
;
7396 key
.proto
= pd
->proto
;
7397 if (direction
== PF_IN
) {
7398 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7399 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7400 key
.ext
.xport
.port
= 0;
7401 key
.gwy
.xport
.port
= 0;
7403 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7404 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7405 key
.lan
.xport
.port
= 0;
7406 key
.ext
.xport
.port
= 0;
7411 if (direction
== (*state
)->state_key
->direction
) {
7412 src
= &(*state
)->src
;
7413 dst
= &(*state
)->dst
;
7415 src
= &(*state
)->dst
;
7416 dst
= &(*state
)->src
;
7420 if (src
->state
< PFOTHERS_SINGLE
)
7421 src
->state
= PFOTHERS_SINGLE
;
7422 if (dst
->state
== PFOTHERS_SINGLE
)
7423 dst
->state
= PFOTHERS_MULTIPLE
;
7425 /* update expire time */
7426 (*state
)->expire
= pf_time_second();
7427 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
7428 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
7430 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
7432 /* translate source/destination address, if necessary */
7433 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7434 if (direction
== PF_OUT
) {
7438 pf_change_a(&pd
->src
->v4
.s_addr
,
7440 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
7447 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
7455 pf_change_a(&pd
->dst
->v4
.s_addr
,
7457 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
7464 &(*state
)->state_key
->lan
.addr
, pd
->af
);
7475 * ipoff and off are measured from the start of the mbuf chain.
7476 * h must be at "ipoff" on the mbuf chain.
7479 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
7480 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
7485 struct ip
*h
= mtod(m
, struct ip
*);
7486 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
7489 if (fragoff
>= len
) {
7490 ACTION_SET(actionp
, PF_PASS
);
7492 ACTION_SET(actionp
, PF_DROP
);
7493 REASON_SET(reasonp
, PFRES_FRAG
);
7497 if (m
->m_pkthdr
.len
< off
+ len
||
7498 ntohs(h
->ip_len
) < off
+ len
) {
7499 ACTION_SET(actionp
, PF_DROP
);
7500 REASON_SET(reasonp
, PFRES_SHORT
);
7508 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
7510 if (m
->m_pkthdr
.len
< off
+ len
||
7511 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
7512 (unsigned)(off
+ len
)) {
7513 ACTION_SET(actionp
, PF_DROP
);
7514 REASON_SET(reasonp
, PFRES_SHORT
);
7521 m_copydata(m
, off
, len
, p
);
7526 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
7529 struct sockaddr_in
*dst
;
7532 struct sockaddr_in6
*dst6
;
7533 struct route_in6 ro
;
7538 bzero(&ro
, sizeof (ro
));
7541 dst
= satosin(&ro
.ro_dst
);
7542 dst
->sin_family
= AF_INET
;
7543 dst
->sin_len
= sizeof (*dst
);
7544 dst
->sin_addr
= addr
->v4
;
7548 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7549 dst6
->sin6_family
= AF_INET6
;
7550 dst6
->sin6_len
= sizeof (*dst6
);
7551 dst6
->sin6_addr
= addr
->v6
;
7558 /* XXX: IFT_ENC is not currently used by anything*/
7559 /* Skip checks for ipsec interfaces */
7560 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
7563 rtalloc((struct route
*)&ro
);
7566 if (ro
.ro_rt
!= NULL
)
7572 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
7575 struct sockaddr_in
*dst
;
7577 struct sockaddr_in6
*dst6
;
7578 struct route_in6 ro
;
7584 bzero(&ro
, sizeof (ro
));
7587 dst
= satosin(&ro
.ro_dst
);
7588 dst
->sin_family
= AF_INET
;
7589 dst
->sin_len
= sizeof (*dst
);
7590 dst
->sin_addr
= addr
->v4
;
7594 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7595 dst6
->sin6_family
= AF_INET6
;
7596 dst6
->sin6_len
= sizeof (*dst6
);
7597 dst6
->sin6_addr
= addr
->v6
;
7604 rtalloc((struct route
*)&ro
);
7606 if (ro
.ro_rt
!= NULL
) {
7615 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7616 struct pf_state
*s
, struct pf_pdesc
*pd
)
7619 struct mbuf
*m0
, *m1
;
7620 struct route iproute
;
7621 struct route
*ro
= NULL
;
7622 struct sockaddr_in
*dst
;
7624 struct ifnet
*ifp
= NULL
;
7625 struct pf_addr naddr
;
7626 struct pf_src_node
*sn
= NULL
;
7630 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7631 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7632 panic("pf_route: invalid parameters");
7634 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7640 if (r
->rt
== PF_DUPTO
) {
7641 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7644 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7649 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7650 DPFPRINTF(PF_DEBUG_URGENT
,
7651 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7655 ip
= mtod(m0
, struct ip
*);
7658 bzero((caddr_t
)ro
, sizeof (*ro
));
7659 dst
= satosin((void *)&ro
->ro_dst
);
7660 dst
->sin_family
= AF_INET
;
7661 dst
->sin_len
= sizeof (*dst
);
7662 dst
->sin_addr
= ip
->ip_dst
;
7664 if (r
->rt
== PF_FASTROUTE
) {
7666 if (ro
->ro_rt
== 0) {
7667 ipstat
.ips_noroute
++;
7671 ifp
= ro
->ro_rt
->rt_ifp
;
7673 ro
->ro_rt
->rt_use
++;
7675 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
7676 dst
= satosin((void *)ro
->ro_rt
->rt_gateway
);
7677 RT_UNLOCK(ro
->ro_rt
);
7679 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7680 DPFPRINTF(PF_DEBUG_URGENT
,
7681 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
7685 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
7687 if (!PF_AZERO(&naddr
, AF_INET
))
7688 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
7689 ifp
= r
->rpool
.cur
->kif
?
7690 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7692 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
7693 dst
->sin_addr
.s_addr
=
7694 s
->rt_addr
.v4
.s_addr
;
7695 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7702 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7704 else if (m0
== NULL
)
7706 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7707 DPFPRINTF(PF_DEBUG_URGENT
,
7708 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7711 ip
= mtod(m0
, struct ip
*);
7714 /* Copied from ip_output. */
7716 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
7717 m0
->m_pkthdr
.csum_flags
|= CSUM_IP
;
7718 sw_csum
= m0
->m_pkthdr
.csum_flags
&
7719 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
7721 if (ifp
->if_hwassist
& CSUM_TCP_SUM16
) {
7723 * Special case code for GMACE
7724 * frames that can be checksumed by GMACE SUM16 HW:
7725 * frame >64, no fragments, no UDP
7727 if (apple_hwcksum_tx
&& (m0
->m_pkthdr
.csum_flags
& CSUM_TCP
) &&
7728 (ntohs(ip
->ip_len
) > 50) &&
7729 (ntohs(ip
->ip_len
) <= ifp
->if_mtu
)) {
7731 * Apple GMAC HW, expects:
7732 * STUFF_OFFSET << 16 | START_OFFSET
7734 /* IP+Enet header length */
7735 u_short offset
= ((ip
->ip_hl
) << 2) + 14;
7736 u_short csumprev
= m0
->m_pkthdr
.csum_data
& 0xffff;
7737 m0
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
|
7738 CSUM_TCP_SUM16
; /* for GMAC */
7739 m0
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
7740 m0
->m_pkthdr
.csum_data
+= offset
;
7741 /* do IP hdr chksum in software */
7742 sw_csum
= CSUM_DELAY_IP
;
7744 /* let the software handle any UDP or TCP checksums */
7745 sw_csum
|= (CSUM_DELAY_DATA
& m0
->m_pkthdr
.csum_flags
);
7747 } else if (apple_hwcksum_tx
== 0) {
7748 sw_csum
|= (CSUM_DELAY_DATA
| CSUM_DELAY_IP
) &
7749 m0
->m_pkthdr
.csum_flags
;
7752 if (sw_csum
& CSUM_DELAY_DATA
) {
7753 in_delayed_cksum(m0
);
7754 sw_csum
&= ~CSUM_DELAY_DATA
;
7755 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
7758 if (apple_hwcksum_tx
!= 0) {
7759 m0
->m_pkthdr
.csum_flags
&=
7760 IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
7762 m0
->m_pkthdr
.csum_flags
= 0;
7765 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
||
7766 (ifp
->if_hwassist
& CSUM_FRAGMENT
)) {
7768 if (sw_csum
& CSUM_DELAY_IP
)
7769 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
7770 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
, sintosa(dst
));
7775 * Too large for interface; fragment if possible.
7776 * Must be able to put at least 8 bytes per fragment.
7778 if (ip
->ip_off
& htons(IP_DF
)) {
7779 ipstat
.ips_cantfrag
++;
7780 if (r
->rt
!= PF_DUPTO
) {
7781 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
7790 /* PR-8933605: send ip_len,ip_off to ip_fragment in host byte order */
7791 #if BYTE_ORDER != BIG_ENDIAN
7795 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
7802 for (m0
= m1
; m0
; m0
= m1
) {
7806 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
->ro_rt
,
7813 ipstat
.ips_fragmented
++;
7816 if (r
->rt
!= PF_DUPTO
)
7818 if (ro
== &iproute
&& ro
->ro_rt
)
7830 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7831 struct pf_state
*s
, struct pf_pdesc
*pd
)
7835 struct route_in6 ip6route
;
7836 struct route_in6
*ro
;
7837 struct sockaddr_in6
*dst
;
7838 struct ip6_hdr
*ip6
;
7839 struct ifnet
*ifp
= NULL
;
7840 struct pf_addr naddr
;
7841 struct pf_src_node
*sn
= NULL
;
7844 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7845 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7846 panic("pf_route6: invalid parameters");
7848 if (pd
->pf_mtag
->pftag_routed
++ > 3) {
7854 if (r
->rt
== PF_DUPTO
) {
7855 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7858 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7863 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7864 DPFPRINTF(PF_DEBUG_URGENT
,
7865 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
7868 ip6
= mtod(m0
, struct ip6_hdr
*);
7871 bzero((caddr_t
)ro
, sizeof (*ro
));
7872 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
7873 dst
->sin6_family
= AF_INET6
;
7874 dst
->sin6_len
= sizeof (*dst
);
7875 dst
->sin6_addr
= ip6
->ip6_dst
;
7877 /* Cheat. XXX why only in the v6 case??? */
7878 if (r
->rt
== PF_FASTROUTE
) {
7879 struct pf_mtag
*pf_mtag
;
7881 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
7883 pf_mtag
->pftag_flags
|= PF_TAG_GENERATED
;
7884 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
7888 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7889 DPFPRINTF(PF_DEBUG_URGENT
,
7890 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
7894 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
7896 if (!PF_AZERO(&naddr
, AF_INET6
))
7897 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7899 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7901 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
7902 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
7903 &s
->rt_addr
, AF_INET6
);
7904 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7910 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
, NULL
) != PF_PASS
)
7912 else if (m0
== NULL
)
7914 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
7915 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
7916 "< sizeof (struct ip6_hdr)\n"));
7919 ip6
= mtod(m0
, struct ip6_hdr
*);
7923 * If the packet is too large for the outgoing interface,
7924 * send back an icmp6 error.
7926 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
7927 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
7928 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
7929 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, NULL
);
7931 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
7932 if (r
->rt
!= PF_DUPTO
)
7933 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
7939 if (r
->rt
!= PF_DUPTO
)
7951 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
7952 * off is the offset where the protocol header starts
7953 * len is the total length of protocol header plus payload
7954 * returns 0 when the checksum is valid, otherwise returns 1.
7957 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
7966 * Optimize for the common case; if the hardware calculated
7967 * value doesn't include pseudo-header checksum, or if it
7968 * is partially-computed (only 16-bit summation), do it in
7971 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
&
7972 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) &&
7973 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
7979 case IPPROTO_ICMPV6
:
7985 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
7987 if (m
->m_pkthdr
.len
< off
+ len
)
7992 if (p
== IPPROTO_ICMP
) {
7997 sum
= in_cksum(m
, len
);
8001 if (m
->m_len
< (int)sizeof (struct ip
))
8003 sum
= inet_cksum(m
, p
, off
, len
);
8009 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
8011 sum
= inet6_cksum(m
, p
, off
, len
);
8020 tcpstat
.tcps_rcvbadsum
++;
8023 udpstat
.udps_badsum
++;
8026 icmpstat
.icps_checksum
++;
8029 case IPPROTO_ICMPV6
:
8030 icmp6stat
.icp6s_checksum
++;
8040 #define PF_APPLE_UPDATE_PDESC_IPv4() \
8042 if (m && pd.mp && m != pd.mp) { \
8044 h = mtod(m, struct ip *); \
8045 pd.pf_mtag = pf_get_mtag(m); \
8050 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8051 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8056 struct pfi_kif
*kif
;
8057 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8058 struct mbuf
*m
= *m0
;
8060 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8061 struct pf_state
*s
= NULL
;
8062 struct pf_state_key
*sk
= NULL
;
8063 struct pf_ruleset
*ruleset
= NULL
;
8065 int off
, dirndx
, pqid
= 0;
8067 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8069 if (!pf_status
.running
)
8072 memset(&pd
, 0, sizeof (pd
));
8074 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8075 DPFPRINTF(PF_DEBUG_URGENT
,
8076 ("pf_test: pf_get_mtag returned NULL\n"));
8080 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8083 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8086 DPFPRINTF(PF_DEBUG_URGENT
,
8087 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
8090 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8094 if ((m
->m_flags
& M_PKTHDR
) == 0)
8095 panic("non-M_PKTHDR is passed to pf_test");
8096 #endif /* DIAGNOSTIC */
8098 /* initialize enough of pd for the done label */
8099 h
= mtod(m
, struct ip
*);
8102 pd
.pf_mtag
= pf_get_mtag(m
);
8103 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8104 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8105 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8106 pd
.ip_sum
= &h
->ip_sum
;
8108 pd
.proto_variant
= 0;
8111 pd
.tot_len
= ntohs(h
->ip_len
);
8114 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8116 REASON_SET(&reason
, PFRES_SHORT
);
8122 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8124 #endif /* DUMMYNET */
8126 /* We do IP header normalization and packet reassembly here */
8127 action
= pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
);
8129 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8136 #endif /* DUMMYNET */
8137 m
= *m0
; /* pf_normalize messes with m0 */
8138 h
= mtod(m
, struct ip
*);
8140 off
= h
->ip_hl
<< 2;
8141 if (off
< (int)sizeof (*h
)) {
8143 REASON_SET(&reason
, PFRES_SHORT
);
8148 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8149 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8150 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8151 pd
.ip_sum
= &h
->ip_sum
;
8153 pd
.proto_variant
= 0;
8156 pd
.pf_mtag
= pf_get_mtag(m
);
8159 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8160 pd
.tot_len
= ntohs(h
->ip_len
);
8162 if (pd
.pf_mtag
!= NULL
&& pd
.pf_mtag
->pftag_flowhash
!= 0) {
8163 pd
.flowhash
= pd
.pf_mtag
->pftag_flowhash
;
8164 pd
.flags
|= (m
->m_pkthdr
.m_fhflags
& PF_TAG_FLOWADV
) ?
8165 PFDESC_FLOW_ADV
: 0;
8168 /* handle fragments that didn't get reassembled by normalization */
8169 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
8170 pd
.flags
|= PFDESC_IP_FRAG
;
8172 /* Traffic goes through dummynet first */
8173 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8174 if (action
== PF_DROP
|| m
== NULL
) {
8178 #endif /* DUMMYNET */
8179 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8189 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8190 &action
, &reason
, AF_INET
)) {
8191 log
= action
!= PF_PASS
;
8194 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8195 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
8198 /* Traffic goes through dummynet first */
8199 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8200 if (action
== PF_DROP
|| m
== NULL
) {
8204 #endif /* DUMMYNET */
8205 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8208 PF_APPLE_UPDATE_PDESC_IPv4();
8209 if (action
== PF_DROP
)
8211 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8215 PF_APPLE_UPDATE_PDESC_IPv4();
8216 if (action
== PF_PASS
) {
8218 pfsync_update_state(s
);
8219 #endif /* NPFSYNC */
8223 } else if (s
== NULL
)
8224 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8225 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8233 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8234 &action
, &reason
, AF_INET
)) {
8235 log
= action
!= PF_PASS
;
8238 if (uh
.uh_dport
== 0 ||
8239 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8240 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8242 REASON_SET(&reason
, PFRES_SHORT
);
8246 /* Traffic goes through dummynet first */
8247 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8248 if (action
== PF_DROP
|| m
== NULL
) {
8252 #endif /* DUMMYNET */
8253 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8257 PF_APPLE_UPDATE_PDESC_IPv4();
8258 if (action
== PF_PASS
) {
8260 pfsync_update_state(s
);
8261 #endif /* NPFSYNC */
8265 } else if (s
== NULL
)
8266 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8267 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8271 case IPPROTO_ICMP
: {
8275 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
8276 &action
, &reason
, AF_INET
)) {
8277 log
= action
!= PF_PASS
;
8281 /* Traffic goes through dummynet first */
8282 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8283 if (action
== PF_DROP
|| m
== NULL
) {
8287 #endif /* DUMMYNET */
8288 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8292 PF_APPLE_UPDATE_PDESC_IPv4();
8293 if (action
== PF_PASS
) {
8295 pfsync_update_state(s
);
8296 #endif /* NPFSYNC */
8300 } else if (s
== NULL
)
8301 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8302 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8307 struct pf_esp_hdr esp
;
8310 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8312 log
= action
!= PF_PASS
;
8316 /* Traffic goes through dummynet first */
8317 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8318 if (action
== PF_DROP
|| m
== NULL
) {
8322 #endif /* DUMMYNET */
8323 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8326 PF_APPLE_UPDATE_PDESC_IPv4();
8327 if (action
== PF_PASS
) {
8329 pfsync_update_state(s
);
8330 #endif /* NPFSYNC */
8334 } else if (s
== NULL
)
8335 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8336 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8341 struct pf_grev1_hdr grev1
;
8342 pd
.hdr
.grev1
= &grev1
;
8343 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8344 &reason
, AF_INET
)) {
8345 log
= (action
!= PF_PASS
);
8349 /* Traffic goes through dummynet first */
8350 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8351 if (action
== PF_DROP
|| m
== NULL
) {
8355 #endif /* DUMMYNET */
8356 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8357 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8358 if (ntohs(grev1
.payload_length
) >
8359 m
->m_pkthdr
.len
- off
) {
8361 REASON_SET(&reason
, PFRES_SHORT
);
8364 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8365 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8366 if (pd
.lmw
< 0) goto done
;
8367 PF_APPLE_UPDATE_PDESC_IPv4();
8368 if (action
== PF_PASS
) {
8370 pfsync_update_state(s
);
8371 #endif /* NPFSYNC */
8376 } else if (s
== NULL
) {
8377 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8378 h
, &pd
, &a
, &ruleset
, &ipintrq
);
8379 if (action
== PF_PASS
)
8384 /* not GREv1/PPTP, so treat as ordinary GRE... */
8389 /* Traffic goes through dummynet first */
8390 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8391 if (action
== PF_DROP
|| m
== NULL
) {
8395 #endif /* DUMMYNET */
8396 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8399 PF_APPLE_UPDATE_PDESC_IPv4();
8400 if (action
== PF_PASS
) {
8402 pfsync_update_state(s
);
8403 #endif /* NPFSYNC */
8407 } else if (s
== NULL
)
8408 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
8409 &pd
, &a
, &ruleset
, &ipintrq
);
8415 PF_APPLE_UPDATE_PDESC_IPv4();
8417 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
8418 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
8420 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8422 DPFPRINTF(PF_DEBUG_MISC
,
8423 ("pf: dropping packet with ip options [hlen=%u]\n",
8424 (unsigned int) h
->ip_hl
));
8427 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) ||
8429 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
8432 if (action
== PF_PASS
) {
8434 if (altq_allowed
&& r
->qid
) {
8435 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
8436 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
8438 pd
.pf_mtag
->pftag_qid
= r
->qid
;
8440 #endif /* PF_ALTQ */
8441 /* add hints for ecn */
8442 pd
.pf_mtag
->pftag_hdr
= h
;
8443 /* record address family */
8444 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET6
;
8445 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET
;
8446 /* record TCP vs. non-TCP */
8447 if (pd
.proto
== IPPROTO_TCP
)
8448 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TCP
;
8450 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_TCP
;
8454 * connections redirected to loopback should not match sockets
8455 * bound specifically to loopback due to security implications,
8456 * see tcp_input() and in_pcblookup_listen().
8458 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
8459 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8460 (s
->nat_rule
.ptr
->action
== PF_RDR
||
8461 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
8462 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
8463 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
8468 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8469 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
8470 lr
= s
->nat_rule
.ptr
;
8473 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
8477 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
8478 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
8480 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
8481 dirndx
= (dir
== PF_OUT
);
8482 r
->packets
[dirndx
]++;
8483 r
->bytes
[dirndx
] += pd
.tot_len
;
8485 a
->packets
[dirndx
]++;
8486 a
->bytes
[dirndx
] += pd
.tot_len
;
8490 if (s
->nat_rule
.ptr
!= NULL
) {
8491 s
->nat_rule
.ptr
->packets
[dirndx
]++;
8492 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
8494 if (s
->src_node
!= NULL
) {
8495 s
->src_node
->packets
[dirndx
]++;
8496 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
8498 if (s
->nat_src_node
!= NULL
) {
8499 s
->nat_src_node
->packets
[dirndx
]++;
8500 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
8502 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
8503 s
->packets
[dirndx
]++;
8504 s
->bytes
[dirndx
] += pd
.tot_len
;
8507 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
8511 * XXX: we need to make sure that the addresses
8512 * passed to pfr_update_stats() are the same than
8513 * the addresses used during matching (pfr_match)
8515 if (r
== &pf_default_rule
) {
8517 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8518 &pd
.baddr
: &pd
.naddr
;
8520 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8521 &pd
.naddr
: &pd
.baddr
;
8522 if (x
== &pd
.baddr
|| s
== NULL
) {
8523 /* we need to change the address */
8530 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
8531 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
8532 sk
->direction
== dir
) ?
8533 pd
.src
: pd
.dst
, pd
.af
,
8534 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8536 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
8537 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
8538 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
8539 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8543 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
8547 REASON_SET(&reason
, PFRES_MEMORY
);
8551 if (action
== PF_DROP
) {
8560 if (action
== PF_SYNPROXY_DROP
) {
8565 /* pf_route can free the mbuf causing *m0 to become NULL */
8566 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
8573 #define PF_APPLE_UPDATE_PDESC_IPv6() \
8575 if (m && pd.mp && m != pd.mp) { \
8579 h = mtod(m, struct ip6_hdr *); \
8584 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8585 struct ether_header
*eh
, struct ip_fw_args
*fwa
)
8590 struct pfi_kif
*kif
;
8591 u_short action
= PF_PASS
, reason
= 0, log
= 0;
8592 struct mbuf
*m
= *m0
, *n
= NULL
;
8594 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8595 struct pf_state
*s
= NULL
;
8596 struct pf_state_key
*sk
= NULL
;
8597 struct pf_ruleset
*ruleset
= NULL
;
8599 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
8602 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8604 if (!pf_status
.running
)
8607 memset(&pd
, 0, sizeof (pd
));
8609 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8610 DPFPRINTF(PF_DEBUG_URGENT
,
8611 ("pf_test6: pf_get_mtag returned NULL\n"));
8615 if (pd
.pf_mtag
->pftag_flags
& PF_TAG_GENERATED
)
8618 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8621 DPFPRINTF(PF_DEBUG_URGENT
,
8622 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
8625 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8629 if ((m
->m_flags
& M_PKTHDR
) == 0)
8630 panic("non-M_PKTHDR is passed to pf_test6");
8631 #endif /* DIAGNOSTIC */
8633 h
= mtod(m
, struct ip6_hdr
*);
8636 off
= ((caddr_t
)h
- m
->m_data
) + sizeof(struct ip6_hdr
);
8639 pd
.pf_mtag
= pf_get_mtag(m
);
8640 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8641 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8642 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8646 pd
.proto_variant
= 0;
8648 pd
.sc
= MBUF_SCIDX(mbuf_get_service_class(m
));
8649 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof(struct ip6_hdr
);
8652 if (pd
.pf_mtag
->pftag_flowhash
!= 0) {
8653 pd
.flowhash
= pd
.pf_mtag
->pftag_flowhash
;
8654 pd
.flags
|= (m
->m_pkthdr
.m_fhflags
& PF_TAG_FLOWADV
) ?
8655 PFDESC_FLOW_ADV
: 0;
8658 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8660 REASON_SET(&reason
, PFRES_SHORT
);
8666 if (fwa
!= NULL
&& fwa
->fwa_pf_rule
!= NULL
)
8668 #endif /* DUMMYNET */
8670 /* We do IP header normalization and packet reassembly here */
8671 action
= pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
);
8673 if (action
!= PF_PASS
|| pd
.lmw
< 0) {
8680 #endif /* DUMMYNET */
8681 h
= mtod(m
, struct ip6_hdr
*);
8685 * we do not support jumbogram yet. if we keep going, zero ip6_plen
8686 * will do something bad, so drop the packet for now.
8688 if (htons(h
->ip6_plen
) == 0) {
8690 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
8695 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8696 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8697 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8701 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
8704 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
8705 pd
.proto
= h
->ip6_nxt
;
8706 pd
.proto_variant
= 0;
8709 pd
.pf_mtag
= pf_get_mtag(m
);
8713 case IPPROTO_FRAGMENT
: {
8714 struct ip6_frag ip6f
;
8716 pd
.flags
|= PFDESC_IP_FRAG
;
8717 if (!pf_pull_hdr(m
, off
, &ip6f
, sizeof ip6f
, NULL
,
8719 DPFPRINTF(PF_DEBUG_MISC
,
8720 ("pf: IPv6 short fragment header\n"));
8722 REASON_SET(&reason
, PFRES_SHORT
);
8726 pd
.proto
= nxt
= ip6f
.ip6f_nxt
;
8728 /* Traffic goes through dummynet first */
8729 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8730 if (action
== PF_DROP
|| m
== NULL
) {
8734 #endif /* DUMMYNET */
8735 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
, &pd
, &a
,
8737 if (action
== PF_DROP
) {
8738 REASON_SET(&reason
, PFRES_FRAG
);
8743 case IPPROTO_ROUTING
:
8748 case IPPROTO_HOPOPTS
:
8749 case IPPROTO_DSTOPTS
: {
8750 /* get next header and header length */
8751 struct ip6_ext opt6
;
8753 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof(opt6
),
8754 NULL
, &reason
, pd
.af
)) {
8755 DPFPRINTF(PF_DEBUG_MISC
,
8756 ("pf: IPv6 short opt\n"));
8761 if (pd
.proto
== IPPROTO_AH
)
8762 off
+= (opt6
.ip6e_len
+ 2) * 4;
8764 off
+= (opt6
.ip6e_len
+ 1) * 8;
8765 nxt
= opt6
.ip6e_nxt
;
8766 /* goto the next header */
8773 } while (!terminal
);
8775 /* if there's no routing header, use unmodified mbuf for checksumming */
8785 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8786 &action
, &reason
, AF_INET6
)) {
8787 log
= action
!= PF_PASS
;
8790 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8792 /* Traffic goes through dummynet first */
8793 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8794 if (action
== PF_DROP
|| m
== NULL
) {
8798 #endif /* DUMMYNET */
8799 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8802 PF_APPLE_UPDATE_PDESC_IPv6();
8803 if (action
== PF_DROP
)
8805 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8809 PF_APPLE_UPDATE_PDESC_IPv6();
8810 if (action
== PF_PASS
) {
8812 pfsync_update_state(s
);
8813 #endif /* NPFSYNC */
8817 } else if (s
== NULL
)
8818 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8819 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8827 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8828 &action
, &reason
, AF_INET6
)) {
8829 log
= action
!= PF_PASS
;
8832 if (uh
.uh_dport
== 0 ||
8833 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8834 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8836 REASON_SET(&reason
, PFRES_SHORT
);
8840 /* Traffic goes through dummynet first */
8841 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8842 if (action
== PF_DROP
|| m
== NULL
) {
8846 #endif /* DUMMYNET */
8847 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8851 PF_APPLE_UPDATE_PDESC_IPv6();
8852 if (action
== PF_PASS
) {
8854 pfsync_update_state(s
);
8855 #endif /* NPFSYNC */
8859 } else if (s
== NULL
)
8860 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8861 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8865 case IPPROTO_ICMPV6
: {
8866 struct icmp6_hdr ih
;
8869 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
8870 &action
, &reason
, AF_INET6
)) {
8871 log
= action
!= PF_PASS
;
8875 /* Traffic goes through dummynet first */
8876 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8877 if (action
== PF_DROP
|| m
== NULL
) {
8881 #endif /* DUMMYNET */
8882 action
= pf_test_state_icmp(&s
, dir
, kif
,
8883 m
, off
, h
, &pd
, &reason
);
8886 PF_APPLE_UPDATE_PDESC_IPv6();
8887 if (action
== PF_PASS
) {
8889 pfsync_update_state(s
);
8890 #endif /* NPFSYNC */
8894 } else if (s
== NULL
)
8895 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8896 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8901 struct pf_esp_hdr esp
;
8904 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8906 log
= action
!= PF_PASS
;
8910 /* Traffic goes through dummynet first */
8911 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8912 if (action
== PF_DROP
|| m
== NULL
) {
8916 #endif /* DUMMYNET */
8917 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8920 PF_APPLE_UPDATE_PDESC_IPv6();
8921 if (action
== PF_PASS
) {
8923 pfsync_update_state(s
);
8924 #endif /* NPFSYNC */
8928 } else if (s
== NULL
)
8929 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8930 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8935 struct pf_grev1_hdr grev1
;
8937 pd
.hdr
.grev1
= &grev1
;
8938 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8939 &reason
, AF_INET6
)) {
8940 log
= (action
!= PF_PASS
);
8944 /* Traffic goes through dummynet first */
8945 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8946 if (action
== PF_DROP
|| m
== NULL
) {
8950 #endif /* DUMMYNET */
8951 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8952 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8953 if (ntohs(grev1
.payload_length
) >
8954 m
->m_pkthdr
.len
- off
) {
8956 REASON_SET(&reason
, PFRES_SHORT
);
8959 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8962 PF_APPLE_UPDATE_PDESC_IPv6();
8963 if (action
== PF_PASS
) {
8965 pfsync_update_state(s
);
8966 #endif /* NPFSYNC */
8971 } else if (s
== NULL
) {
8972 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8973 h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8974 if (action
== PF_PASS
)
8979 /* not GREv1/PPTP, so treat as ordinary GRE... */
8984 /* Traffic goes through dummynet first */
8985 action
= pf_test_dummynet(&r
, dir
, kif
, &m
, &pd
, fwa
);
8986 if (action
== PF_DROP
|| m
== NULL
) {
8990 #endif /* DUMMYNET */
8991 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8994 PF_APPLE_UPDATE_PDESC_IPv6();
8995 if (action
== PF_PASS
) {
8997 pfsync_update_state(s
);
8998 #endif /* NPFSYNC */
9002 } else if (s
== NULL
)
9003 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9004 &pd
, &a
, &ruleset
, &ip6intrq
);
9010 PF_APPLE_UPDATE_PDESC_IPv6();
9017 /* handle dangerous IPv6 extension headers. */
9018 if (action
== PF_PASS
&& rh_cnt
&&
9019 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9021 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9023 DPFPRINTF(PF_DEBUG_MISC
,
9024 ("pf: dropping packet with dangerous v6 headers\n"));
9027 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
) || pd
.flowhash
!= 0)
9028 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9031 if (action
== PF_PASS
) {
9033 if (altq_allowed
&& r
->qid
) {
9034 if (pd
.tos
& IPTOS_LOWDELAY
)
9035 pd
.pf_mtag
->pftag_qid
= r
->pqid
;
9037 pd
.pf_mtag
->pftag_qid
= r
->qid
;
9039 #endif /* PF_ALTQ */
9040 /* add hints for ecn */
9041 pd
.pf_mtag
->pftag_hdr
= h
;
9042 /* record address family */
9043 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_HDR_INET
;
9044 pd
.pf_mtag
->pftag_flags
|= PF_TAG_HDR_INET6
;
9045 /* record TCP vs. non-TCP */
9046 if (pd
.proto
== IPPROTO_TCP
)
9047 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TCP
;
9049 pd
.pf_mtag
->pftag_flags
&= ~PF_TAG_TCP
;
9052 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
9053 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9054 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9055 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9056 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
9057 pd
.pf_mtag
->pftag_flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9062 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9063 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9064 lr
= s
->nat_rule
.ptr
;
9067 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
9071 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9072 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
9074 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9075 dirndx
= (dir
== PF_OUT
);
9076 r
->packets
[dirndx
]++;
9077 r
->bytes
[dirndx
] += pd
.tot_len
;
9079 a
->packets
[dirndx
]++;
9080 a
->bytes
[dirndx
] += pd
.tot_len
;
9084 if (s
->nat_rule
.ptr
!= NULL
) {
9085 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9086 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9088 if (s
->src_node
!= NULL
) {
9089 s
->src_node
->packets
[dirndx
]++;
9090 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9092 if (s
->nat_src_node
!= NULL
) {
9093 s
->nat_src_node
->packets
[dirndx
]++;
9094 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9096 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9097 s
->packets
[dirndx
]++;
9098 s
->bytes
[dirndx
] += pd
.tot_len
;
9101 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9105 * XXX: we need to make sure that the addresses
9106 * passed to pfr_update_stats() are the same than
9107 * the addresses used during matching (pfr_match)
9109 if (r
== &pf_default_rule
) {
9111 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9112 &pd
.baddr
: &pd
.naddr
;
9114 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9115 &pd
.naddr
: &pd
.baddr
;
9117 if (x
== &pd
.baddr
|| s
== NULL
) {
9124 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9125 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9126 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
9127 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9129 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9130 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9131 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9132 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9137 if (action
== PF_SYNPROXY_DROP
) {
9142 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9143 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9145 VERIFY(m
== NULL
|| pd
.mp
== NULL
|| pd
.mp
== m
);
9149 REASON_SET(&reason
, PFRES_MEMORY
);
9153 if (action
== PF_DROP
) {
9162 if (action
== PF_SYNPROXY_DROP
) {
9167 if (action
== PF_PASS
) {
9169 h
= mtod(m
, struct ip6_hdr
*);
9172 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9173 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9182 pf_check_congestion(struct ifqueue
*ifq
)
9189 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
9190 int flags
, const char *wchan
, void *palloc
)
9192 #pragma unused(align, ioff, flags, palloc)
9193 bzero(pp
, sizeof (*pp
));
9194 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
9195 if (pp
->pool_zone
!= NULL
) {
9196 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
9197 zone_change(pp
->pool_zone
, Z_CALLERACCT
, FALSE
);
9198 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
9199 pp
->pool_name
= wchan
;
9203 /* Zones cannot be currently destroyed */
9205 pool_destroy(struct pool
*pp
)
9211 pool_sethiwat(struct pool
*pp
, int n
)
9213 pp
->pool_hiwat
= n
; /* Currently unused */
9217 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
9219 #pragma unused(warnmess, ratecap)
9224 pool_get(struct pool
*pp
, int flags
)
9228 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9230 if (pp
->pool_count
> pp
->pool_limit
) {
9231 DPFPRINTF(PF_DEBUG_NOISY
,
9232 ("pf: pool %s hard limit reached (%d)\n",
9233 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
9239 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
9242 VERIFY(pp
->pool_count
!= 0);
9248 pool_put(struct pool
*pp
, void *v
)
9250 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9252 zfree(pp
->pool_zone
, v
);
9253 VERIFY(pp
->pool_count
!= 0);
9258 pf_find_mtag(struct mbuf
*m
)
9260 if (!(m
->m_flags
& M_PKTHDR
))
9263 return (m_pftag(m
));
9267 pf_get_mtag(struct mbuf
*m
)
9269 return (pf_find_mtag(m
));
9273 pf_time_second(void)
9282 pf_calendar_time_second(void)
9291 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
9293 struct hook_desc
*hd
;
9295 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
9302 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
9304 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
9310 hook_runloop(struct hook_desc_head
*head
, int flags
)
9312 struct hook_desc
*hd
;
9314 if (!(flags
& HOOK_REMOVE
)) {
9315 if (!(flags
& HOOK_ABORT
))
9316 TAILQ_FOREACH(hd
, head
, hd_list
)
9317 hd
->hd_fn(hd
->hd_arg
);
9319 while (!!(hd
= TAILQ_FIRST(head
))) {
9320 TAILQ_REMOVE(head
, hd
, hd_list
);
9321 if (!(flags
& HOOK_ABORT
))
9322 hd
->hd_fn(hd
->hd_arg
);
9323 if (flags
& HOOK_FREE
)
9324 _FREE(hd
, M_DEVBUF
);