2 * Copyright (c) 2008 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: pf.c,v 1.37 2008/12/05 23:10:20 jhw Exp $ */
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>
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>
123 #ifndef NO_APPLE_EXTENSIONS
124 #define DPFPRINTF(n, x) (pf_status.debug >= (n) ? printf x : ((void)0))
126 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
129 /* XXX: should be in header somewhere */
130 #define satosin(sa) ((struct sockaddr_in *)(sa))
131 #define sintosa(sin) ((struct sockaddr *)(sin))
134 * On Mac OS X, the rtableid value is treated as the interface scope
135 * value that is equivalent to the interface index used for scoped
136 * routing. A valid scope value is anything but IFSCOPE_NONE (0),
137 * as per definition of ifindex which is a positive, non-zero number.
138 * The other BSDs treat a negative rtableid value as invalid, hence
139 * the test against INT_MAX to handle userland apps which initialize
140 * the field with a negative number.
142 #define PF_RTABLEID_IS_VALID(r) \
143 ((r) > IFSCOPE_NONE && (r) <= INT_MAX)
149 lck_rw_t
*pf_perim_lock
;
152 struct pf_state_tree_lan_ext pf_statetbl_lan_ext
;
153 struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy
;
155 struct pf_palist pf_pabuf
;
156 struct pf_status pf_status
;
159 struct pf_altqqueue pf_altqs
[2];
160 struct pf_altqqueue
*pf_altqs_active
;
161 struct pf_altqqueue
*pf_altqs_inactive
;
162 u_int32_t ticket_altqs_active
;
163 u_int32_t ticket_altqs_inactive
;
164 int altqs_inactive_open
;
166 u_int32_t ticket_pabuf
;
168 static MD5_CTX pf_tcp_secret_ctx
;
169 static u_char pf_tcp_secret
[16];
170 static int pf_tcp_secret_init
;
171 static int pf_tcp_iss_off
;
173 static struct pf_anchor_stackframe
{
174 struct pf_ruleset
*rs
;
176 struct pf_anchor_node
*parent
;
177 struct pf_anchor
*child
;
178 } pf_anchor_stack
[64];
180 struct pool pf_src_tree_pl
, pf_rule_pl
, pf_pooladdr_pl
;
181 struct pool pf_state_pl
, pf_state_key_pl
;
183 struct pool pf_altq_pl
;
186 #ifndef NO_APPLE_EXTENSIONS
187 typedef void (*hook_fn_t
)(void *);
190 TAILQ_ENTRY(hook_desc
) hd_list
;
195 #define HOOK_REMOVE 0x01
196 #define HOOK_FREE 0x02
197 #define HOOK_ABORT 0x04
199 static void *hook_establish(struct hook_desc_head
*, int,
201 static void hook_runloop(struct hook_desc_head
*, int flags
);
203 struct pool pf_app_state_pl
;
204 static void pf_print_addr(struct pf_addr
*addr
, sa_family_t af
);
205 static void pf_print_sk_host(struct pf_state_host
*, u_int8_t
, int,
209 static void pf_print_host(struct pf_addr
*, u_int16_t
, u_int8_t
);
211 static void pf_init_threshold(struct pf_threshold
*, u_int32_t
,
213 static void pf_add_threshold(struct pf_threshold
*);
214 static int pf_check_threshold(struct pf_threshold
*);
216 static void pf_change_ap(int, struct mbuf
*, struct pf_addr
*,
217 u_int16_t
*, u_int16_t
*, u_int16_t
*,
218 struct pf_addr
*, u_int16_t
, u_int8_t
, sa_family_t
);
219 static int pf_modulate_sack(struct mbuf
*, int, struct pf_pdesc
*,
220 struct tcphdr
*, struct pf_state_peer
*);
222 static void pf_change_a6(struct pf_addr
*, u_int16_t
*,
223 struct pf_addr
*, u_int8_t
);
225 static void pf_change_icmp(struct pf_addr
*, u_int16_t
*,
226 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
227 u_int16_t
*, u_int16_t
*, u_int16_t
*,
228 u_int16_t
*, u_int8_t
, sa_family_t
);
229 static void pf_send_tcp(const struct pf_rule
*, sa_family_t
,
230 const struct pf_addr
*, const struct pf_addr
*,
231 u_int16_t
, u_int16_t
, u_int32_t
, u_int32_t
,
232 u_int8_t
, u_int16_t
, u_int16_t
, u_int8_t
, int,
233 u_int16_t
, struct ether_header
*, struct ifnet
*);
234 static void pf_send_icmp(struct mbuf
*, u_int8_t
, u_int8_t
,
235 sa_family_t
, struct pf_rule
*);
236 #ifndef NO_APPLE_EXTENSIONS
237 static struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
238 int, int, struct pfi_kif
*, struct pf_addr
*,
239 union pf_state_xport
*, struct pf_addr
*,
240 union pf_state_xport
*, int);
241 static struct pf_rule
*pf_get_translation_aux(struct pf_pdesc
*,
242 struct mbuf
*, int, int, struct pfi_kif
*,
243 struct pf_src_node
**, struct pf_addr
*,
244 union pf_state_xport
*, struct pf_addr
*,
245 union pf_state_xport
*, struct pf_addr
*,
246 union pf_state_xport
*);
248 struct pf_rule
*pf_match_translation(struct pf_pdesc
*, struct mbuf
*,
249 int, int, struct pfi_kif
*,
250 struct pf_addr
*, u_int16_t
, struct pf_addr
*,
252 struct pf_rule
*pf_get_translation(struct pf_pdesc
*, struct mbuf
*,
253 int, int, struct pfi_kif
*, struct pf_src_node
**,
254 struct pf_addr
*, u_int16_t
,
255 struct pf_addr
*, u_int16_t
,
256 struct pf_addr
*, u_int16_t
*);
258 static void pf_attach_state(struct pf_state_key
*,
259 struct pf_state
*, int);
260 static void pf_detach_state(struct pf_state
*, int);
261 static u_int32_t
pf_tcp_iss(struct pf_pdesc
*);
262 static int pf_test_rule(struct pf_rule
**, struct pf_state
**,
263 int, struct pfi_kif
*, struct mbuf
*, int,
264 void *, struct pf_pdesc
*, struct pf_rule
**,
265 struct pf_ruleset
**, struct ifqueue
*);
266 static int pf_test_fragment(struct pf_rule
**, int,
267 struct pfi_kif
*, struct mbuf
*, void *,
268 struct pf_pdesc
*, struct pf_rule
**,
269 struct pf_ruleset
**);
270 static int pf_test_state_tcp(struct pf_state
**, int,
271 struct pfi_kif
*, struct mbuf
*, int,
272 void *, struct pf_pdesc
*, u_short
*);
273 static int pf_test_state_udp(struct pf_state
**, int,
274 struct pfi_kif
*, struct mbuf
*, int,
275 void *, struct pf_pdesc
*);
276 static int pf_test_state_icmp(struct pf_state
**, int,
277 struct pfi_kif
*, struct mbuf
*, int,
278 void *, struct pf_pdesc
*, u_short
*);
279 static int pf_test_state_other(struct pf_state
**, int,
280 struct pfi_kif
*, struct pf_pdesc
*);
281 static int pf_match_tag(struct mbuf
*, struct pf_rule
*,
282 struct pf_mtag
*, int *);
283 static void pf_step_into_anchor(int *, struct pf_ruleset
**, int,
284 struct pf_rule
**, struct pf_rule
**, int *);
285 static int pf_step_out_of_anchor(int *, struct pf_ruleset
**,
286 int, struct pf_rule
**, struct pf_rule
**,
288 static void pf_hash(struct pf_addr
*, struct pf_addr
*,
289 struct pf_poolhashkey
*, sa_family_t
);
290 static int pf_map_addr(u_int8_t
, struct pf_rule
*,
291 struct pf_addr
*, struct pf_addr
*,
292 struct pf_addr
*, struct pf_src_node
**);
293 #ifndef NO_APPLE_EXTENSIONS
294 static int pf_get_sport(struct pf_pdesc
*, struct pfi_kif
*,
295 struct pf_rule
*, struct pf_addr
*,
296 union pf_state_xport
*, struct pf_addr
*,
297 union pf_state_xport
*, struct pf_addr
*,
298 union pf_state_xport
*, struct pf_src_node
**);
300 int pf_get_sport(sa_family_t
, u_int8_t
, struct pf_rule
*,
301 struct pf_addr
*, struct pf_addr
*, u_int16_t
,
302 struct pf_addr
*, u_int16_t
*, u_int16_t
, u_int16_t
,
303 struct pf_src_node
**);
305 static void pf_route(struct mbuf
**, struct pf_rule
*, int,
306 struct ifnet
*, struct pf_state
*,
309 static void pf_route6(struct mbuf
**, struct pf_rule
*, int,
310 struct ifnet
*, struct pf_state
*,
313 static u_int8_t
pf_get_wscale(struct mbuf
*, int, u_int16_t
,
315 static u_int16_t
pf_get_mss(struct mbuf
*, int, u_int16_t
,
317 static u_int16_t
pf_calc_mss(struct pf_addr
*, sa_family_t
,
319 static void pf_set_rt_ifp(struct pf_state
*,
321 static int pf_check_proto_cksum(struct mbuf
*, int, int,
322 u_int8_t
, sa_family_t
);
323 static int pf_addr_wrap_neq(struct pf_addr_wrap
*,
324 struct pf_addr_wrap
*);
325 static struct pf_state
*pf_find_state(struct pfi_kif
*,
326 struct pf_state_key_cmp
*, u_int
);
327 static int pf_src_connlimit(struct pf_state
**);
328 static void pf_stateins_err(const char *, struct pf_state
*,
330 static int pf_check_congestion(struct ifqueue
*);
332 #ifndef NO_APPLE_EXTENSIONS
334 static const char *pf_pptp_ctrl_type_name(u_int16_t code
);
336 static void pf_pptp_handler(struct pf_state
*, int, int,
337 struct pf_pdesc
*, struct pfi_kif
*);
338 static void pf_pptp_unlink(struct pf_state
*);
339 static int pf_test_state_grev1(struct pf_state
**, int,
340 struct pfi_kif
*, int, struct pf_pdesc
*);
341 static int pf_ike_compare(struct pf_app_state
*,
342 struct pf_app_state
*);
343 static int pf_test_state_esp(struct pf_state
**, int,
344 struct pfi_kif
*, int, struct pf_pdesc
*);
347 extern struct pool pfr_ktable_pl
;
348 extern struct pool pfr_kentry_pl
;
349 extern int path_mtu_discovery
;
351 struct pf_pool_limit pf_pool_limits
[PF_LIMIT_MAX
] = {
352 { &pf_state_pl
, PFSTATE_HIWAT
},
353 { &pf_app_state_pl
, PFAPPSTATE_HIWAT
},
354 { &pf_src_tree_pl
, PFSNODE_HIWAT
},
355 { &pf_frent_pl
, PFFRAG_FRENT_HIWAT
},
356 { &pfr_ktable_pl
, PFR_KTABLE_HIWAT
},
357 { &pfr_kentry_pl
, PFR_KENTRY_HIWAT
}
360 #ifndef NO_APPLE_EXTENSIONS
362 pf_lazy_makewritable(struct pf_pdesc
*pd
, struct mbuf
*m
, int len
)
370 if (m_makewritable(&m
, 0, len
, M_DONTWAIT
))
373 if (len
>= 0 && m
!= pd
->mp
) {
378 struct ip
*h
= mtod(m
, struct ip
*);
379 pd
->src
= (struct pf_addr
*)&h
->ip_src
;
380 pd
->dst
= (struct pf_addr
*)&h
->ip_dst
;
381 pd
->ip_sum
= &h
->ip_sum
;
386 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
387 pd
->src
= (struct pf_addr
*)&h
->ip6_src
;
388 pd
->dst
= (struct pf_addr
*)&h
->ip6_dst
;
396 return (len
< 0 ? 0 : m
);
400 pf_state_lookup_aux(struct pf_state
**state
, struct pfi_kif
*kif
,
401 int direction
, int *action
)
403 if (*state
== NULL
|| (*state
)->timeout
== PFTM_PURGE
) {
408 if (direction
== PF_OUT
&&
409 (((*state
)->rule
.ptr
->rt
== PF_ROUTETO
&&
410 (*state
)->rule
.ptr
->direction
== PF_OUT
) ||
411 ((*state
)->rule
.ptr
->rt
== PF_REPLYTO
&&
412 (*state
)->rule
.ptr
->direction
== PF_IN
)) &&
413 (*state
)->rt_kif
!= NULL
&& (*state
)->rt_kif
!= kif
) {
421 #define STATE_LOOKUP() \
424 *state = pf_find_state(kif, &key, direction); \
425 if (pf_state_lookup_aux(state, kif, direction, &action)) \
429 #define STATE_ADDR_TRANSLATE(sk) \
430 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
431 ((sk)->af == AF_INET6 && \
432 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
433 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
434 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3]))
436 #define STATE_TRANSLATE(sk) \
437 (STATE_ADDR_TRANSLATE(sk) || \
438 (sk)->lan.xport.port != (sk)->gwy.xport.port)
440 #define STATE_GRE_TRANSLATE(sk) \
441 (STATE_ADDR_TRANSLATE(sk) || \
442 (sk)->lan.xport.call_id != (sk)->gwy.xport.call_id)
445 #define STATE_LOOKUP() \
447 *state = pf_find_state(kif, &key, direction); \
448 if (*state == NULL || (*state)->timeout == PFTM_PURGE) \
450 if (direction == PF_OUT && \
451 (((*state)->rule.ptr->rt == PF_ROUTETO && \
452 (*state)->rule.ptr->direction == PF_OUT) || \
453 ((*state)->rule.ptr->rt == PF_REPLYTO && \
454 (*state)->rule.ptr->direction == PF_IN)) && \
455 (*state)->rt_kif != NULL && \
456 (*state)->rt_kif != kif) \
460 #define STATE_TRANSLATE(sk) \
461 (sk)->lan.addr.addr32[0] != (sk)->gwy.addr.addr32[0] || \
462 ((sk)->af == AF_INET6 && \
463 ((sk)->lan.addr.addr32[1] != (sk)->gwy.addr.addr32[1] || \
464 (sk)->lan.addr.addr32[2] != (sk)->gwy.addr.addr32[2] || \
465 (sk)->lan.addr.addr32[3] != (sk)->gwy.addr.addr32[3])) || \
466 (sk)->lan.port != (sk)->gwy.port
469 #define BOUND_IFACE(r, k) \
470 ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : pfi_all
472 #define STATE_INC_COUNTERS(s) \
474 s->rule.ptr->states++; \
475 if (s->anchor.ptr != NULL) \
476 s->anchor.ptr->states++; \
477 if (s->nat_rule.ptr != NULL) \
478 s->nat_rule.ptr->states++; \
481 #define STATE_DEC_COUNTERS(s) \
483 if (s->nat_rule.ptr != NULL) \
484 s->nat_rule.ptr->states--; \
485 if (s->anchor.ptr != NULL) \
486 s->anchor.ptr->states--; \
487 s->rule.ptr->states--; \
490 static __inline
int pf_src_compare(struct pf_src_node
*, struct pf_src_node
*);
491 static __inline
int pf_state_compare_lan_ext(struct pf_state_key
*,
492 struct pf_state_key
*);
493 static __inline
int pf_state_compare_ext_gwy(struct pf_state_key
*,
494 struct pf_state_key
*);
495 static __inline
int pf_state_compare_id(struct pf_state
*,
498 struct pf_src_tree tree_src_tracking
;
500 struct pf_state_tree_id tree_id
;
501 struct pf_state_queue state_list
;
503 RB_GENERATE(pf_src_tree
, pf_src_node
, entry
, pf_src_compare
);
504 RB_GENERATE(pf_state_tree_lan_ext
, pf_state_key
,
505 entry_lan_ext
, pf_state_compare_lan_ext
);
506 RB_GENERATE(pf_state_tree_ext_gwy
, pf_state_key
,
507 entry_ext_gwy
, pf_state_compare_ext_gwy
);
508 RB_GENERATE(pf_state_tree_id
, pf_state
,
509 entry_id
, pf_state_compare_id
);
511 #define PF_DT_SKIP_LANEXT 0x01
512 #define PF_DT_SKIP_EXTGWY 0x02
514 #ifndef NO_APPLE_EXTENSIONS
515 static const u_int16_t PF_PPTP_PORT
= htons(1723);
516 static const u_int32_t PF_PPTP_MAGIC_NUMBER
= htonl(0x1A2B3C4D);
524 struct pf_pptp_ctrl_hdr
{
526 u_int16_t reserved_0
;
529 struct pf_pptp_ctrl_generic
{
533 #define PF_PPTP_CTRL_TYPE_START_REQ 1
534 struct pf_pptp_ctrl_start_req
{
535 u_int16_t protocol_version
;
536 u_int16_t reserved_1
;
537 u_int32_t framing_capabilities
;
538 u_int32_t bearer_capabilities
;
539 u_int16_t maximum_channels
;
540 u_int16_t firmware_revision
;
541 u_int8_t host_name
[64];
542 u_int8_t vendor_string
[64];
545 #define PF_PPTP_CTRL_TYPE_START_RPY 2
546 struct pf_pptp_ctrl_start_rpy
{
547 u_int16_t protocol_version
;
548 u_int8_t result_code
;
550 u_int32_t framing_capabilities
;
551 u_int32_t bearer_capabilities
;
552 u_int16_t maximum_channels
;
553 u_int16_t firmware_revision
;
554 u_int8_t host_name
[64];
555 u_int8_t vendor_string
[64];
558 #define PF_PPTP_CTRL_TYPE_STOP_REQ 3
559 struct pf_pptp_ctrl_stop_req
{
562 u_int16_t reserved_2
;
565 #define PF_PPTP_CTRL_TYPE_STOP_RPY 4
566 struct pf_pptp_ctrl_stop_rpy
{
569 u_int16_t reserved_1
;
572 #define PF_PPTP_CTRL_TYPE_ECHO_REQ 5
573 struct pf_pptp_ctrl_echo_req
{
574 u_int32_t identifier
;
577 #define PF_PPTP_CTRL_TYPE_ECHO_RPY 6
578 struct pf_pptp_ctrl_echo_rpy
{
579 u_int32_t identifier
;
580 u_int8_t result_code
;
582 u_int16_t reserved_1
;
585 #define PF_PPTP_CTRL_TYPE_CALL_OUT_REQ 7
586 struct pf_pptp_ctrl_call_out_req
{
588 u_int16_t call_sernum
;
590 u_int32_t bearer_type
;
591 u_int32_t framing_type
;
592 u_int16_t rxwindow_size
;
593 u_int16_t proc_delay
;
594 u_int8_t phone_num
[64];
595 u_int8_t sub_addr
[64];
598 #define PF_PPTP_CTRL_TYPE_CALL_OUT_RPY 8
599 struct pf_pptp_ctrl_call_out_rpy
{
601 u_int16_t peer_call_id
;
602 u_int8_t result_code
;
604 u_int16_t cause_code
;
605 u_int32_t connect_speed
;
606 u_int16_t rxwindow_size
;
607 u_int16_t proc_delay
;
608 u_int32_t phy_channel_id
;
611 #define PF_PPTP_CTRL_TYPE_CALL_IN_1ST 9
612 struct pf_pptp_ctrl_call_in_1st
{
614 u_int16_t call_sernum
;
615 u_int32_t bearer_type
;
616 u_int32_t phy_channel_id
;
617 u_int16_t dialed_number_len
;
618 u_int16_t dialing_number_len
;
619 u_int8_t dialed_num
[64];
620 u_int8_t dialing_num
[64];
621 u_int8_t sub_addr
[64];
624 #define PF_PPTP_CTRL_TYPE_CALL_IN_2ND 10
625 struct pf_pptp_ctrl_call_in_2nd
{
627 u_int16_t peer_call_id
;
628 u_int8_t result_code
;
630 u_int16_t rxwindow_size
;
632 u_int16_t reserved_1
;
635 #define PF_PPTP_CTRL_TYPE_CALL_IN_3RD 11
636 struct pf_pptp_ctrl_call_in_3rd
{
638 u_int16_t reserved_1
;
639 u_int32_t connect_speed
;
640 u_int16_t rxwindow_size
;
642 u_int32_t framing_type
;
645 #define PF_PPTP_CTRL_TYPE_CALL_CLR 12
646 struct pf_pptp_ctrl_call_clr
{
648 u_int16_t reserved_1
;
651 #define PF_PPTP_CTRL_TYPE_CALL_DISC 13
652 struct pf_pptp_ctrl_call_disc
{
654 u_int8_t result_code
;
656 u_int16_t cause_code
;
657 u_int16_t reserved_1
;
658 u_int8_t statistics
[128];
661 #define PF_PPTP_CTRL_TYPE_ERROR 14
662 struct pf_pptp_ctrl_error
{
663 u_int16_t peer_call_id
;
664 u_int16_t reserved_1
;
665 u_int32_t crc_errors
;
668 u_int32_t buf_errors
;
669 u_int32_t tim_errors
;
670 u_int32_t align_errors
;
673 #define PF_PPTP_CTRL_TYPE_SET_LINKINFO 15
674 struct pf_pptp_ctrl_set_linkinfo
{
675 u_int16_t peer_call_id
;
676 u_int16_t reserved_1
;
682 static const char *pf_pptp_ctrl_type_name(u_int16_t code
)
686 if (code
< PF_PPTP_CTRL_TYPE_START_REQ
||
687 code
> PF_PPTP_CTRL_TYPE_SET_LINKINFO
) {
688 static char reserved
[] = "reserved-00";
690 sprintf(&reserved
[9], "%02x", code
);
693 static const char *name
[] = {
694 "start_req", "start_rpy", "stop_req", "stop_rpy",
695 "echo_req", "echo_rpy", "call_out_req", "call_out_rpy",
696 "call_in_1st", "call_in_2nd", "call_in_3rd",
697 "call_clr", "call_disc", "error", "set_linkinfo"
700 return (name
[code
- 1]);
705 static const size_t PF_PPTP_CTRL_MSG_MINSIZE
=
706 sizeof (struct pf_pptp_hdr
) +
707 sizeof (struct pf_pptp_ctrl_hdr
) +
708 MIN(sizeof (struct pf_pptp_ctrl_start_req
),
709 MIN(sizeof (struct pf_pptp_ctrl_start_rpy
),
710 MIN(sizeof (struct pf_pptp_ctrl_stop_req
),
711 MIN(sizeof (struct pf_pptp_ctrl_stop_rpy
),
712 MIN(sizeof (struct pf_pptp_ctrl_echo_req
),
713 MIN(sizeof (struct pf_pptp_ctrl_echo_rpy
),
714 MIN(sizeof (struct pf_pptp_ctrl_call_out_req
),
715 MIN(sizeof (struct pf_pptp_ctrl_call_out_rpy
),
716 MIN(sizeof (struct pf_pptp_ctrl_call_in_1st
),
717 MIN(sizeof (struct pf_pptp_ctrl_call_in_2nd
),
718 MIN(sizeof (struct pf_pptp_ctrl_call_in_3rd
),
719 MIN(sizeof (struct pf_pptp_ctrl_call_clr
),
720 MIN(sizeof (struct pf_pptp_ctrl_call_disc
),
721 MIN(sizeof (struct pf_pptp_ctrl_error
),
722 sizeof (struct pf_pptp_ctrl_set_linkinfo
)
725 union pf_pptp_ctrl_msg_union
{
726 struct pf_pptp_ctrl_start_req start_req
;
727 struct pf_pptp_ctrl_start_rpy start_rpy
;
728 struct pf_pptp_ctrl_stop_req stop_req
;
729 struct pf_pptp_ctrl_stop_rpy stop_rpy
;
730 struct pf_pptp_ctrl_echo_req echo_req
;
731 struct pf_pptp_ctrl_echo_rpy echo_rpy
;
732 struct pf_pptp_ctrl_call_out_req call_out_req
;
733 struct pf_pptp_ctrl_call_out_rpy call_out_rpy
;
734 struct pf_pptp_ctrl_call_in_1st call_in_1st
;
735 struct pf_pptp_ctrl_call_in_2nd call_in_2nd
;
736 struct pf_pptp_ctrl_call_in_3rd call_in_3rd
;
737 struct pf_pptp_ctrl_call_clr call_clr
;
738 struct pf_pptp_ctrl_call_disc call_disc
;
739 struct pf_pptp_ctrl_error error
;
740 struct pf_pptp_ctrl_set_linkinfo set_linkinfo
;
744 struct pf_pptp_ctrl_msg
{
745 struct pf_pptp_hdr hdr
;
746 struct pf_pptp_ctrl_hdr ctrl
;
747 union pf_pptp_ctrl_msg_union msg
;
750 #define PF_GRE_FLAG_CHECKSUM_PRESENT 0x8000
751 #define PF_GRE_FLAG_VERSION_MASK 0x0007
752 #define PF_GRE_PPP_ETHERTYPE 0x880B
754 struct pf_grev1_hdr
{
756 u_int16_t protocol_type
;
757 u_int16_t payload_length
;
765 static const u_int16_t PF_IKE_PORT
= htons(500);
768 u_int64_t initiator_cookie
, responder_cookie
;
769 u_int8_t next_payload
, version
, exchange_type
, flags
;
770 u_int32_t message_id
, length
;
773 #define PF_IKE_PACKET_MINSIZE (sizeof (struct pf_ike_hdr))
775 #define PF_IKEv1_EXCHTYPE_BASE 1
776 #define PF_IKEv1_EXCHTYPE_ID_PROTECT 2
777 #define PF_IKEv1_EXCHTYPE_AUTH_ONLY 3
778 #define PF_IKEv1_EXCHTYPE_AGGRESSIVE 4
779 #define PF_IKEv1_EXCHTYPE_INFORMATIONAL 5
780 #define PF_IKEv2_EXCHTYPE_SA_INIT 34
781 #define PF_IKEv2_EXCHTYPE_AUTH 35
782 #define PF_IKEv2_EXCHTYPE_CREATE_CHILD_SA 36
783 #define PF_IKEv2_EXCHTYPE_INFORMATIONAL 37
785 #define PF_IKEv1_FLAG_E 0x01
786 #define PF_IKEv1_FLAG_C 0x02
787 #define PF_IKEv1_FLAG_A 0x04
788 #define PF_IKEv2_FLAG_I 0x08
789 #define PF_IKEv2_FLAG_V 0x10
790 #define PF_IKEv2_FLAG_R 0x20
800 pf_src_compare(struct pf_src_node
*a
, struct pf_src_node
*b
)
804 if (a
->rule
.ptr
> b
->rule
.ptr
)
806 if (a
->rule
.ptr
< b
->rule
.ptr
)
808 if ((diff
= a
->af
- b
->af
) != 0)
813 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
815 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
821 if (a
->addr
.addr32
[3] > b
->addr
.addr32
[3])
823 if (a
->addr
.addr32
[3] < b
->addr
.addr32
[3])
825 if (a
->addr
.addr32
[2] > b
->addr
.addr32
[2])
827 if (a
->addr
.addr32
[2] < b
->addr
.addr32
[2])
829 if (a
->addr
.addr32
[1] > b
->addr
.addr32
[1])
831 if (a
->addr
.addr32
[1] < b
->addr
.addr32
[1])
833 if (a
->addr
.addr32
[0] > b
->addr
.addr32
[0])
835 if (a
->addr
.addr32
[0] < b
->addr
.addr32
[0])
844 pf_state_compare_lan_ext(struct pf_state_key
*a
, struct pf_state_key
*b
)
847 #ifndef NO_APPLE_EXTENSIONS
851 if ((diff
= a
->proto
- b
->proto
) != 0)
853 if ((diff
= a
->af
- b
->af
) != 0)
856 #ifndef NO_APPLE_EXTENSIONS
857 extfilter
= PF_EXTFILTER_APD
;
862 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
867 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
869 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
874 if ((diff
= a
->proto_variant
- b
->proto_variant
))
876 extfilter
= a
->proto_variant
;
877 if ((diff
= a
->lan
.xport
.port
- b
->lan
.xport
.port
) != 0)
879 if ((extfilter
< PF_EXTFILTER_AD
) &&
880 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
885 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
886 a
->proto_variant
== b
->proto_variant
) {
887 if (!!(diff
= a
->ext
.xport
.call_id
-
888 b
->ext
.xport
.call_id
))
894 if (!!(diff
= a
->ext
.xport
.spi
- b
->ext
.xport
.spi
))
906 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
908 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
910 #ifndef NO_APPLE_EXTENSIONS
911 if (extfilter
< PF_EXTFILTER_EI
) {
912 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
914 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
918 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
920 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
927 #ifndef NO_APPLE_EXTENSIONS
928 if (a
->lan
.addr
.addr32
[3] > b
->lan
.addr
.addr32
[3])
930 if (a
->lan
.addr
.addr32
[3] < b
->lan
.addr
.addr32
[3])
932 if (a
->lan
.addr
.addr32
[2] > b
->lan
.addr
.addr32
[2])
934 if (a
->lan
.addr
.addr32
[2] < b
->lan
.addr
.addr32
[2])
936 if (a
->lan
.addr
.addr32
[1] > b
->lan
.addr
.addr32
[1])
938 if (a
->lan
.addr
.addr32
[1] < b
->lan
.addr
.addr32
[1])
940 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
942 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
944 if (extfilter
< PF_EXTFILTER_EI
||
945 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
946 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
948 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
950 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
952 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
954 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
956 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
958 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
960 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
964 if (a
->lan
.addr
.addr32
[3] > b
->lan
.addr
.addr32
[3])
966 if (a
->lan
.addr
.addr32
[3] < b
->lan
.addr
.addr32
[3])
968 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
970 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
972 if (a
->lan
.addr
.addr32
[2] > b
->lan
.addr
.addr32
[2])
974 if (a
->lan
.addr
.addr32
[2] < b
->lan
.addr
.addr32
[2])
976 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
978 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
980 if (a
->lan
.addr
.addr32
[1] > b
->lan
.addr
.addr32
[1])
982 if (a
->lan
.addr
.addr32
[1] < b
->lan
.addr
.addr32
[1])
984 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
986 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
988 if (a
->lan
.addr
.addr32
[0] > b
->lan
.addr
.addr32
[0])
990 if (a
->lan
.addr
.addr32
[0] < b
->lan
.addr
.addr32
[0])
992 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
994 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1001 #ifndef NO_APPLE_EXTENSIONS
1002 if (a
->app_state
&& b
->app_state
) {
1003 if (a
->app_state
->compare_lan_ext
&&
1004 b
->app_state
->compare_lan_ext
) {
1005 diff
= (const char *)b
->app_state
->compare_lan_ext
-
1006 (const char *)a
->app_state
->compare_lan_ext
;
1009 diff
= a
->app_state
->compare_lan_ext(a
->app_state
,
1016 if ((diff
= a
->lan
.port
- b
->lan
.port
) != 0)
1018 if ((diff
= a
->ext
.port
- b
->ext
.port
) != 0)
1026 pf_state_compare_ext_gwy(struct pf_state_key
*a
, struct pf_state_key
*b
)
1029 #ifndef NO_APPLE_EXTENSIONS
1033 if ((diff
= a
->proto
- b
->proto
) != 0)
1036 if ((diff
= a
->af
- b
->af
) != 0)
1039 #ifndef NO_APPLE_EXTENSIONS
1040 extfilter
= PF_EXTFILTER_APD
;
1044 case IPPROTO_ICMPV6
:
1045 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
1050 if ((diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
1052 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
1057 if ((diff
= a
->proto_variant
- b
->proto_variant
))
1059 extfilter
= a
->proto_variant
;
1060 if ((diff
= a
->gwy
.xport
.port
- b
->gwy
.xport
.port
) != 0)
1062 if ((extfilter
< PF_EXTFILTER_AD
) &&
1063 (diff
= a
->ext
.xport
.port
- b
->ext
.xport
.port
) != 0)
1068 if (a
->proto_variant
== PF_GRE_PPTP_VARIANT
&&
1069 a
->proto_variant
== b
->proto_variant
) {
1070 if (!!(diff
= a
->gwy
.xport
.call_id
-
1071 b
->gwy
.xport
.call_id
))
1077 if (!!(diff
= a
->gwy
.xport
.spi
- b
->gwy
.xport
.spi
))
1089 #ifndef NO_APPLE_EXTENSIONS
1090 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1092 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1094 if (extfilter
< PF_EXTFILTER_EI
) {
1095 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1097 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1101 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1103 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1105 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1107 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1114 #ifndef NO_APPLE_EXTENSIONS
1115 if (a
->gwy
.addr
.addr32
[3] > b
->gwy
.addr
.addr32
[3])
1117 if (a
->gwy
.addr
.addr32
[3] < b
->gwy
.addr
.addr32
[3])
1119 if (a
->gwy
.addr
.addr32
[2] > b
->gwy
.addr
.addr32
[2])
1121 if (a
->gwy
.addr
.addr32
[2] < b
->gwy
.addr
.addr32
[2])
1123 if (a
->gwy
.addr
.addr32
[1] > b
->gwy
.addr
.addr32
[1])
1125 if (a
->gwy
.addr
.addr32
[1] < b
->gwy
.addr
.addr32
[1])
1127 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1129 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1131 if (extfilter
< PF_EXTFILTER_EI
||
1132 !PF_AZERO(&b
->ext
.addr
, AF_INET6
)) {
1133 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
1135 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
1137 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
1139 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
1141 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
1143 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
1145 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1147 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1151 if (a
->ext
.addr
.addr32
[3] > b
->ext
.addr
.addr32
[3])
1153 if (a
->ext
.addr
.addr32
[3] < b
->ext
.addr
.addr32
[3])
1155 if (a
->gwy
.addr
.addr32
[3] > b
->gwy
.addr
.addr32
[3])
1157 if (a
->gwy
.addr
.addr32
[3] < b
->gwy
.addr
.addr32
[3])
1159 if (a
->ext
.addr
.addr32
[2] > b
->ext
.addr
.addr32
[2])
1161 if (a
->ext
.addr
.addr32
[2] < b
->ext
.addr
.addr32
[2])
1163 if (a
->gwy
.addr
.addr32
[2] > b
->gwy
.addr
.addr32
[2])
1165 if (a
->gwy
.addr
.addr32
[2] < b
->gwy
.addr
.addr32
[2])
1167 if (a
->ext
.addr
.addr32
[1] > b
->ext
.addr
.addr32
[1])
1169 if (a
->ext
.addr
.addr32
[1] < b
->ext
.addr
.addr32
[1])
1171 if (a
->gwy
.addr
.addr32
[1] > b
->gwy
.addr
.addr32
[1])
1173 if (a
->gwy
.addr
.addr32
[1] < b
->gwy
.addr
.addr32
[1])
1175 if (a
->ext
.addr
.addr32
[0] > b
->ext
.addr
.addr32
[0])
1177 if (a
->ext
.addr
.addr32
[0] < b
->ext
.addr
.addr32
[0])
1179 if (a
->gwy
.addr
.addr32
[0] > b
->gwy
.addr
.addr32
[0])
1181 if (a
->gwy
.addr
.addr32
[0] < b
->gwy
.addr
.addr32
[0])
1188 #ifndef NO_APPLE_EXTENSIONS
1189 if (a
->app_state
&& b
->app_state
) {
1190 if (a
->app_state
->compare_ext_gwy
&&
1191 b
->app_state
->compare_ext_gwy
) {
1192 diff
= (const char *)b
->app_state
->compare_ext_gwy
-
1193 (const char *)a
->app_state
->compare_ext_gwy
;
1196 diff
= a
->app_state
->compare_ext_gwy(a
->app_state
,
1203 if ((diff
= a
->ext
.port
- b
->ext
.port
) != 0)
1205 if ((diff
= a
->gwy
.port
- b
->gwy
.port
) != 0)
1213 pf_state_compare_id(struct pf_state
*a
, struct pf_state
*b
)
1219 if (a
->creatorid
> b
->creatorid
)
1221 if (a
->creatorid
< b
->creatorid
)
1229 pf_addrcpy(struct pf_addr
*dst
, struct pf_addr
*src
, sa_family_t af
)
1234 dst
->addr32
[0] = src
->addr32
[0];
1238 dst
->addr32
[0] = src
->addr32
[0];
1239 dst
->addr32
[1] = src
->addr32
[1];
1240 dst
->addr32
[2] = src
->addr32
[2];
1241 dst
->addr32
[3] = src
->addr32
[3];
1248 pf_find_state_byid(struct pf_state_cmp
*key
)
1250 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1252 return (RB_FIND(pf_state_tree_id
, &tree_id
, (struct pf_state
*)key
));
1255 static struct pf_state
*
1256 pf_find_state(struct pfi_kif
*kif
, struct pf_state_key_cmp
*key
, u_int dir
)
1258 struct pf_state_key
*sk
= NULL
;
1261 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1265 sk
= RB_FIND(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1266 (struct pf_state_key
*)key
);
1269 sk
= RB_FIND(pf_state_tree_ext_gwy
, &pf_statetbl_ext_gwy
,
1270 (struct pf_state_key
*)key
);
1273 panic("pf_find_state");
1276 /* list is sorted, if-bound states before floating ones */
1278 TAILQ_FOREACH(s
, &sk
->states
, next
)
1279 if (s
->kif
== pfi_all
|| s
->kif
== kif
)
1286 pf_find_state_all(struct pf_state_key_cmp
*key
, u_int dir
, int *more
)
1288 struct pf_state_key
*sk
= NULL
;
1289 struct pf_state
*s
, *ret
= NULL
;
1291 pf_status
.fcounters
[FCNT_STATE_SEARCH
]++;
1295 sk
= RB_FIND(pf_state_tree_lan_ext
,
1296 &pf_statetbl_lan_ext
, (struct pf_state_key
*)key
);
1299 sk
= RB_FIND(pf_state_tree_ext_gwy
,
1300 &pf_statetbl_ext_gwy
, (struct pf_state_key
*)key
);
1303 panic("pf_find_state_all");
1307 ret
= TAILQ_FIRST(&sk
->states
);
1311 TAILQ_FOREACH(s
, &sk
->states
, next
)
1319 pf_init_threshold(struct pf_threshold
*threshold
,
1320 u_int32_t limit
, u_int32_t seconds
)
1322 threshold
->limit
= limit
* PF_THRESHOLD_MULT
;
1323 threshold
->seconds
= seconds
;
1324 threshold
->count
= 0;
1325 threshold
->last
= pf_time_second();
1329 pf_add_threshold(struct pf_threshold
*threshold
)
1331 u_int32_t t
= pf_time_second(), diff
= t
- threshold
->last
;
1333 if (diff
>= threshold
->seconds
)
1334 threshold
->count
= 0;
1336 threshold
->count
-= threshold
->count
* diff
/
1338 threshold
->count
+= PF_THRESHOLD_MULT
;
1339 threshold
->last
= t
;
1343 pf_check_threshold(struct pf_threshold
*threshold
)
1345 return (threshold
->count
> threshold
->limit
);
1349 pf_src_connlimit(struct pf_state
**state
)
1353 (*state
)->src_node
->conn
++;
1354 (*state
)->src
.tcp_est
= 1;
1355 pf_add_threshold(&(*state
)->src_node
->conn_rate
);
1357 if ((*state
)->rule
.ptr
->max_src_conn
&&
1358 (*state
)->rule
.ptr
->max_src_conn
<
1359 (*state
)->src_node
->conn
) {
1360 pf_status
.lcounters
[LCNT_SRCCONN
]++;
1364 if ((*state
)->rule
.ptr
->max_src_conn_rate
.limit
&&
1365 pf_check_threshold(&(*state
)->src_node
->conn_rate
)) {
1366 pf_status
.lcounters
[LCNT_SRCCONNRATE
]++;
1373 if ((*state
)->rule
.ptr
->overload_tbl
) {
1375 u_int32_t killed
= 0;
1377 pf_status
.lcounters
[LCNT_OVERLOAD_TABLE
]++;
1378 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1379 printf("pf_src_connlimit: blocking address ");
1380 pf_print_host(&(*state
)->src_node
->addr
, 0,
1381 (*state
)->state_key
->af
);
1384 bzero(&p
, sizeof (p
));
1385 p
.pfra_af
= (*state
)->state_key
->af
;
1386 switch ((*state
)->state_key
->af
) {
1390 p
.pfra_ip4addr
= (*state
)->src_node
->addr
.v4
;
1396 p
.pfra_ip6addr
= (*state
)->src_node
->addr
.v6
;
1401 pfr_insert_kentry((*state
)->rule
.ptr
->overload_tbl
,
1402 &p
, pf_time_second());
1404 /* kill existing states if that's required. */
1405 if ((*state
)->rule
.ptr
->flush
) {
1406 struct pf_state_key
*sk
;
1407 struct pf_state
*st
;
1409 pf_status
.lcounters
[LCNT_OVERLOAD_FLUSH
]++;
1410 RB_FOREACH(st
, pf_state_tree_id
, &tree_id
) {
1413 * Kill states from this source. (Only those
1414 * from the same rule if PF_FLUSH_GLOBAL is not
1418 (*state
)->state_key
->af
&&
1419 (((*state
)->state_key
->direction
==
1421 PF_AEQ(&(*state
)->src_node
->addr
,
1422 &sk
->lan
.addr
, sk
->af
)) ||
1423 ((*state
)->state_key
->direction
== PF_IN
&&
1424 PF_AEQ(&(*state
)->src_node
->addr
,
1425 &sk
->ext
.addr
, sk
->af
))) &&
1426 ((*state
)->rule
.ptr
->flush
&
1428 (*state
)->rule
.ptr
== st
->rule
.ptr
)) {
1429 st
->timeout
= PFTM_PURGE
;
1430 st
->src
.state
= st
->dst
.state
=
1435 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1436 printf(", %u states killed", killed
);
1438 if (pf_status
.debug
>= PF_DEBUG_MISC
)
1442 /* kill this state */
1443 (*state
)->timeout
= PFTM_PURGE
;
1444 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
1449 pf_insert_src_node(struct pf_src_node
**sn
, struct pf_rule
*rule
,
1450 struct pf_addr
*src
, sa_family_t af
)
1452 struct pf_src_node k
;
1456 PF_ACPY(&k
.addr
, src
, af
);
1457 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1458 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1462 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
1463 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
1466 if (!rule
->max_src_nodes
||
1467 rule
->src_nodes
< rule
->max_src_nodes
)
1468 (*sn
) = pool_get(&pf_src_tree_pl
, PR_WAITOK
);
1470 pf_status
.lcounters
[LCNT_SRCNODES
]++;
1473 bzero(*sn
, sizeof (struct pf_src_node
));
1475 pf_init_threshold(&(*sn
)->conn_rate
,
1476 rule
->max_src_conn_rate
.limit
,
1477 rule
->max_src_conn_rate
.seconds
);
1480 if (rule
->rule_flag
& PFRULE_RULESRCTRACK
||
1481 rule
->rpool
.opts
& PF_POOL_STICKYADDR
)
1482 (*sn
)->rule
.ptr
= rule
;
1484 (*sn
)->rule
.ptr
= NULL
;
1485 PF_ACPY(&(*sn
)->addr
, src
, af
);
1486 if (RB_INSERT(pf_src_tree
,
1487 &tree_src_tracking
, *sn
) != NULL
) {
1488 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1489 printf("pf: src_tree insert failed: ");
1490 pf_print_host(&(*sn
)->addr
, 0, af
);
1493 pool_put(&pf_src_tree_pl
, *sn
);
1496 (*sn
)->creation
= pf_time_second();
1497 (*sn
)->ruletype
= rule
->action
;
1498 if ((*sn
)->rule
.ptr
!= NULL
)
1499 (*sn
)->rule
.ptr
->src_nodes
++;
1500 pf_status
.scounters
[SCNT_SRC_NODE_INSERT
]++;
1501 pf_status
.src_nodes
++;
1503 if (rule
->max_src_states
&&
1504 (*sn
)->states
>= rule
->max_src_states
) {
1505 pf_status
.lcounters
[LCNT_SRCSTATES
]++;
1513 pf_stateins_err(const char *tree
, struct pf_state
*s
, struct pfi_kif
*kif
)
1515 struct pf_state_key
*sk
= s
->state_key
;
1517 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1518 #ifndef NO_APPLE_EXTENSIONS
1519 printf("pf: state insert failed: %s %s ", tree
, kif
->pfik_name
);
1520 switch (sk
->proto
) {
1530 case IPPROTO_ICMPV6
:
1534 printf("PROTO=%u", sk
->proto
);
1538 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
,
1541 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
,
1544 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
,
1547 printf("pf: state insert failed: %s %s", tree
, kif
->pfik_name
);
1549 pf_print_host(&sk
->lan
.addr
, sk
->lan
.port
,
1552 pf_print_host(&sk
->gwy
.addr
, sk
->gwy
.port
,
1555 pf_print_host(&sk
->ext
.addr
, sk
->ext
.port
,
1558 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1559 printf(" (from sync)");
1565 pf_insert_state(struct pfi_kif
*kif
, struct pf_state
*s
)
1567 struct pf_state_key
*cur
;
1568 struct pf_state
*sp
;
1570 VERIFY(s
->state_key
!= NULL
);
1573 if ((cur
= RB_INSERT(pf_state_tree_lan_ext
, &pf_statetbl_lan_ext
,
1574 s
->state_key
)) != NULL
) {
1575 /* key exists. check for same kif, if none, add to key */
1576 TAILQ_FOREACH(sp
, &cur
->states
, next
)
1577 if (sp
->kif
== kif
) { /* collision! */
1578 pf_stateins_err("tree_lan_ext", s
, kif
);
1580 PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1583 pf_detach_state(s
, PF_DT_SKIP_LANEXT
|PF_DT_SKIP_EXTGWY
);
1584 pf_attach_state(cur
, s
, kif
== pfi_all
? 1 : 0);
1587 /* if cur != NULL, we already found a state key and attached to it */
1588 if (cur
== NULL
&& (cur
= RB_INSERT(pf_state_tree_ext_gwy
,
1589 &pf_statetbl_ext_gwy
, s
->state_key
)) != NULL
) {
1590 /* must not happen. we must have found the sk above! */
1591 pf_stateins_err("tree_ext_gwy", s
, kif
);
1592 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
1596 if (s
->id
== 0 && s
->creatorid
== 0) {
1597 s
->id
= htobe64(pf_status
.stateid
++);
1598 s
->creatorid
= pf_status
.hostid
;
1600 if (RB_INSERT(pf_state_tree_id
, &tree_id
, s
) != NULL
) {
1601 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
1602 printf("pf: state insert failed: "
1603 "id: %016llx creatorid: %08x",
1604 be64toh(s
->id
), ntohl(s
->creatorid
));
1605 if (s
->sync_flags
& PFSTATE_FROMSYNC
)
1606 printf(" (from sync)");
1609 pf_detach_state(s
, 0);
1612 TAILQ_INSERT_TAIL(&state_list
, s
, entry_list
);
1613 pf_status
.fcounters
[FCNT_STATE_INSERT
]++;
1615 pfi_kif_ref(kif
, PFI_KIF_REF_STATE
);
1617 pfsync_insert_state(s
);
1623 pf_purge_thread_fn(void *v
, wait_result_t w
)
1625 #pragma unused(v, w)
1626 u_int32_t nloops
= 0;
1630 (void) tsleep(pf_purge_thread_fn
, PWAIT
, "pftm", t
* hz
);
1632 lck_rw_lock_shared(pf_perim_lock
);
1633 lck_mtx_lock(pf_lock
);
1635 /* purge everything if not running */
1636 if (!pf_status
.running
) {
1637 pf_purge_expired_states(pf_status
.states
);
1638 pf_purge_expired_fragments();
1639 pf_purge_expired_src_nodes();
1641 /* terminate thread (we don't currently do this) */
1642 if (pf_purge_thread
== NULL
) {
1643 lck_mtx_unlock(pf_lock
);
1644 lck_rw_done(pf_perim_lock
);
1646 thread_deallocate(current_thread());
1647 thread_terminate(current_thread());
1651 /* if there's nothing left, sleep w/o timeout */
1652 if (pf_status
.states
== 0 &&
1653 pf_normalize_isempty() &&
1654 RB_EMPTY(&tree_src_tracking
))
1657 lck_mtx_unlock(pf_lock
);
1658 lck_rw_done(pf_perim_lock
);
1661 } else if (t
== 0) {
1662 /* Set timeout to 1 second */
1666 /* process a fraction of the state table every second */
1667 pf_purge_expired_states(1 + (pf_status
.states
1668 / pf_default_rule
.timeout
[PFTM_INTERVAL
]));
1670 /* purge other expired types every PFTM_INTERVAL seconds */
1671 if (++nloops
>= pf_default_rule
.timeout
[PFTM_INTERVAL
]) {
1672 pf_purge_expired_fragments();
1673 pf_purge_expired_src_nodes();
1677 lck_mtx_unlock(pf_lock
);
1678 lck_rw_done(pf_perim_lock
);
1683 pf_state_expires(const struct pf_state
*state
)
1690 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1692 /* handle all PFTM_* > PFTM_MAX here */
1693 if (state
->timeout
== PFTM_PURGE
)
1694 return (pf_time_second());
1695 if (state
->timeout
== PFTM_UNTIL_PACKET
)
1697 VERIFY(state
->timeout
!= PFTM_UNLINKED
);
1698 VERIFY(state
->timeout
< PFTM_MAX
);
1699 t
= state
->rule
.ptr
->timeout
[state
->timeout
];
1701 t
= pf_default_rule
.timeout
[state
->timeout
];
1702 start
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_START
];
1704 end
= state
->rule
.ptr
->timeout
[PFTM_ADAPTIVE_END
];
1705 states
= state
->rule
.ptr
->states
;
1707 start
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_START
];
1708 end
= pf_default_rule
.timeout
[PFTM_ADAPTIVE_END
];
1709 states
= pf_status
.states
;
1711 if (end
&& states
> start
&& start
< end
) {
1713 return (state
->expire
+ t
* (end
- states
) /
1716 return (pf_time_second());
1718 return (state
->expire
+ t
);
1722 pf_purge_expired_src_nodes(void)
1724 struct pf_src_node
*cur
, *next
;
1726 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1728 for (cur
= RB_MIN(pf_src_tree
, &tree_src_tracking
); cur
; cur
= next
) {
1729 next
= RB_NEXT(pf_src_tree
, &tree_src_tracking
, cur
);
1731 if (cur
->states
<= 0 && cur
->expire
<= pf_time_second()) {
1732 if (cur
->rule
.ptr
!= NULL
) {
1733 cur
->rule
.ptr
->src_nodes
--;
1734 if (cur
->rule
.ptr
->states
<= 0 &&
1735 cur
->rule
.ptr
->max_src_nodes
<= 0)
1736 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1738 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, cur
);
1739 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
1740 pf_status
.src_nodes
--;
1741 pool_put(&pf_src_tree_pl
, cur
);
1747 pf_src_tree_remove_state(struct pf_state
*s
)
1751 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1753 if (s
->src_node
!= NULL
) {
1755 --s
->src_node
->conn
;
1756 if (--s
->src_node
->states
<= 0) {
1757 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1759 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1760 s
->src_node
->expire
= pf_time_second() + t
;
1763 if (s
->nat_src_node
!= s
->src_node
&& s
->nat_src_node
!= NULL
) {
1764 if (--s
->nat_src_node
->states
<= 0) {
1765 t
= s
->rule
.ptr
->timeout
[PFTM_SRC_NODE
];
1767 t
= pf_default_rule
.timeout
[PFTM_SRC_NODE
];
1768 s
->nat_src_node
->expire
= pf_time_second() + t
;
1771 s
->src_node
= s
->nat_src_node
= NULL
;
1775 pf_unlink_state(struct pf_state
*cur
)
1777 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1779 #ifndef NO_APPLE_EXTENSIONS
1780 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1781 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1782 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1783 cur
->state_key
->ext
.xport
.port
,
1784 cur
->state_key
->lan
.xport
.port
,
1785 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1786 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1789 hook_runloop(&cur
->unlink_hooks
, HOOK_REMOVE
|HOOK_FREE
);
1791 if (cur
->src
.state
== PF_TCPS_PROXY_DST
) {
1792 pf_send_tcp(cur
->rule
.ptr
, cur
->state_key
->af
,
1793 &cur
->state_key
->ext
.addr
, &cur
->state_key
->lan
.addr
,
1794 cur
->state_key
->ext
.port
, cur
->state_key
->lan
.port
,
1795 cur
->src
.seqhi
, cur
->src
.seqlo
+ 1,
1796 TH_RST
|TH_ACK
, 0, 0, 0, 1, cur
->tag
, NULL
, NULL
);
1799 RB_REMOVE(pf_state_tree_id
, &tree_id
, cur
);
1801 if (cur
->creatorid
== pf_status
.hostid
)
1802 pfsync_delete_state(cur
);
1804 cur
->timeout
= PFTM_UNLINKED
;
1805 pf_src_tree_remove_state(cur
);
1806 pf_detach_state(cur
, 0);
1809 /* callers should be at splpf and hold the
1810 * write_lock on pf_consistency_lock */
1812 pf_free_state(struct pf_state
*cur
)
1814 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1816 if (pfsyncif
!= NULL
&&
1817 (pfsyncif
->sc_bulk_send_next
== cur
||
1818 pfsyncif
->sc_bulk_terminator
== cur
))
1821 VERIFY(cur
->timeout
== PFTM_UNLINKED
);
1822 if (--cur
->rule
.ptr
->states
<= 0 &&
1823 cur
->rule
.ptr
->src_nodes
<= 0)
1824 pf_rm_rule(NULL
, cur
->rule
.ptr
);
1825 if (cur
->nat_rule
.ptr
!= NULL
)
1826 if (--cur
->nat_rule
.ptr
->states
<= 0 &&
1827 cur
->nat_rule
.ptr
->src_nodes
<= 0)
1828 pf_rm_rule(NULL
, cur
->nat_rule
.ptr
);
1829 if (cur
->anchor
.ptr
!= NULL
)
1830 if (--cur
->anchor
.ptr
->states
<= 0)
1831 pf_rm_rule(NULL
, cur
->anchor
.ptr
);
1832 pf_normalize_tcp_cleanup(cur
);
1833 pfi_kif_unref(cur
->kif
, PFI_KIF_REF_STATE
);
1834 TAILQ_REMOVE(&state_list
, cur
, entry_list
);
1836 pf_tag_unref(cur
->tag
);
1837 pool_put(&pf_state_pl
, cur
);
1838 pf_status
.fcounters
[FCNT_STATE_REMOVALS
]++;
1843 pf_purge_expired_states(u_int32_t maxcheck
)
1845 static struct pf_state
*cur
= NULL
;
1846 struct pf_state
*next
;
1848 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1850 while (maxcheck
--) {
1851 /* wrap to start of list when we hit the end */
1853 cur
= TAILQ_FIRST(&state_list
);
1855 break; /* list empty */
1858 /* get next state, as cur may get deleted */
1859 next
= TAILQ_NEXT(cur
, entry_list
);
1861 if (cur
->timeout
== PFTM_UNLINKED
) {
1863 } else if (pf_state_expires(cur
) <= pf_time_second()) {
1864 /* unlink and free expired state */
1865 pf_unlink_state(cur
);
1873 pf_tbladdr_setup(struct pf_ruleset
*rs
, struct pf_addr_wrap
*aw
)
1875 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1877 if (aw
->type
!= PF_ADDR_TABLE
)
1879 if ((aw
->p
.tbl
= pfr_attach_table(rs
, aw
->v
.tblname
)) == NULL
)
1885 pf_tbladdr_remove(struct pf_addr_wrap
*aw
)
1887 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1889 if (aw
->type
!= PF_ADDR_TABLE
|| aw
->p
.tbl
== NULL
)
1891 pfr_detach_table(aw
->p
.tbl
);
1896 pf_tbladdr_copyout(struct pf_addr_wrap
*aw
)
1898 struct pfr_ktable
*kt
= aw
->p
.tbl
;
1900 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1902 if (aw
->type
!= PF_ADDR_TABLE
|| kt
== NULL
)
1904 if (!(kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) && kt
->pfrkt_root
!= NULL
)
1905 kt
= kt
->pfrkt_root
;
1907 aw
->p
.tblcnt
= (kt
->pfrkt_flags
& PFR_TFLAG_ACTIVE
) ?
1911 #ifndef NO_APPLE_EXTENSIONS
1913 pf_print_addr(struct pf_addr
*addr
, sa_family_t af
)
1918 u_int32_t a
= ntohl(addr
->addr32
[0]);
1919 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
1927 u_int8_t i
, curstart
= 255, curend
= 0,
1928 maxstart
= 0, maxend
= 0;
1929 for (i
= 0; i
< 8; i
++) {
1930 if (!addr
->addr16
[i
]) {
1931 if (curstart
== 255)
1937 if ((curend
- curstart
) >
1938 (maxend
- maxstart
)) {
1939 maxstart
= curstart
;
1946 for (i
= 0; i
< 8; i
++) {
1947 if (i
>= maxstart
&& i
<= maxend
) {
1956 b
= ntohs(addr
->addr16
[i
]);
1969 pf_print_sk_host(struct pf_state_host
*sh
, sa_family_t af
, int proto
,
1970 u_int8_t proto_variant
)
1972 pf_print_addr(&sh
->addr
, af
);
1977 printf("[%08x]", ntohl(sh
->xport
.spi
));
1981 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
1982 printf("[%u]", ntohs(sh
->xport
.call_id
));
1987 printf("[%u]", ntohs(sh
->xport
.port
));
1997 pf_print_host(struct pf_addr
*addr
, u_int16_t p
, sa_family_t af
)
1999 #ifndef NO_APPLE_EXTENSIONS
2000 pf_print_addr(addr
, af
);
2002 printf("[%u]", ntohs(p
));
2007 u_int32_t a
= ntohl(addr
->addr32
[0]);
2008 printf("%u.%u.%u.%u", (a
>>24)&255, (a
>>16)&255,
2020 u_int8_t i
, curstart
= 255, curend
= 0,
2021 maxstart
= 0, maxend
= 0;
2022 for (i
= 0; i
< 8; i
++) {
2023 if (!addr
->addr16
[i
]) {
2024 if (curstart
== 255)
2030 if ((curend
- curstart
) >
2031 (maxend
- maxstart
)) {
2032 maxstart
= curstart
;
2039 for (i
= 0; i
< 8; i
++) {
2040 if (i
>= maxstart
&& i
<= maxend
) {
2049 b
= ntohs(addr
->addr16
[i
]);
2067 pf_print_state(struct pf_state
*s
)
2069 struct pf_state_key
*sk
= s
->state_key
;
2070 switch (sk
->proto
) {
2071 #ifndef NO_APPLE_EXTENSIONS
2076 printf("GRE%u ", sk
->proto_variant
);
2088 case IPPROTO_ICMPV6
:
2092 printf("%u ", sk
->proto
);
2095 #ifndef NO_APPLE_EXTENSIONS
2096 pf_print_sk_host(&sk
->lan
, sk
->af
, sk
->proto
, sk
->proto_variant
);
2098 pf_print_sk_host(&sk
->gwy
, sk
->af
, sk
->proto
, sk
->proto_variant
);
2100 pf_print_sk_host(&sk
->ext
, sk
->af
, sk
->proto
, sk
->proto_variant
);
2102 pf_print_host(&sk
->lan
.addr
, sk
->lan
.port
, sk
->af
);
2104 pf_print_host(&sk
->gwy
.addr
, sk
->gwy
.port
, sk
->af
);
2106 pf_print_host(&sk
->ext
.addr
, sk
->ext
.port
, sk
->af
);
2108 printf(" [lo=%u high=%u win=%u modulator=%u", s
->src
.seqlo
,
2109 s
->src
.seqhi
, s
->src
.max_win
, s
->src
.seqdiff
);
2110 if (s
->src
.wscale
&& s
->dst
.wscale
)
2111 printf(" wscale=%u", s
->src
.wscale
& PF_WSCALE_MASK
);
2113 printf(" [lo=%u high=%u win=%u modulator=%u", s
->dst
.seqlo
,
2114 s
->dst
.seqhi
, s
->dst
.max_win
, s
->dst
.seqdiff
);
2115 if (s
->src
.wscale
&& s
->dst
.wscale
)
2116 printf(" wscale=%u", s
->dst
.wscale
& PF_WSCALE_MASK
);
2118 printf(" %u:%u", s
->src
.state
, s
->dst
.state
);
2122 pf_print_flags(u_int8_t f
)
2144 #define PF_SET_SKIP_STEPS(i) \
2146 while (head[i] != cur) { \
2147 head[i]->skip[i].ptr = cur; \
2148 head[i] = TAILQ_NEXT(head[i], entries); \
2153 pf_calc_skip_steps(struct pf_rulequeue
*rules
)
2155 struct pf_rule
*cur
, *prev
, *head
[PF_SKIP_COUNT
];
2158 cur
= TAILQ_FIRST(rules
);
2160 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2162 while (cur
!= NULL
) {
2164 if (cur
->kif
!= prev
->kif
|| cur
->ifnot
!= prev
->ifnot
)
2165 PF_SET_SKIP_STEPS(PF_SKIP_IFP
);
2166 if (cur
->direction
!= prev
->direction
)
2167 PF_SET_SKIP_STEPS(PF_SKIP_DIR
);
2168 if (cur
->af
!= prev
->af
)
2169 PF_SET_SKIP_STEPS(PF_SKIP_AF
);
2170 if (cur
->proto
!= prev
->proto
)
2171 PF_SET_SKIP_STEPS(PF_SKIP_PROTO
);
2172 if (cur
->src
.neg
!= prev
->src
.neg
||
2173 pf_addr_wrap_neq(&cur
->src
.addr
, &prev
->src
.addr
))
2174 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR
);
2175 #ifndef NO_APPLE_EXTENSIONS
2177 union pf_rule_xport
*cx
= &cur
->src
.xport
;
2178 union pf_rule_xport
*px
= &prev
->src
.xport
;
2180 switch (cur
->proto
) {
2183 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
2186 if (prev
->proto
== IPPROTO_GRE
||
2187 prev
->proto
== IPPROTO_ESP
||
2188 cx
->range
.op
!= px
->range
.op
||
2189 cx
->range
.port
[0] != px
->range
.port
[0] ||
2190 cx
->range
.port
[1] != px
->range
.port
[1])
2191 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
2196 if (cur
->src
.port
[0] != prev
->src
.port
[0] ||
2197 cur
->src
.port
[1] != prev
->src
.port
[1] ||
2198 cur
->src
.port_op
!= prev
->src
.port_op
)
2199 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT
);
2201 if (cur
->dst
.neg
!= prev
->dst
.neg
||
2202 pf_addr_wrap_neq(&cur
->dst
.addr
, &prev
->dst
.addr
))
2203 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR
);
2204 #ifndef NO_APPLE_EXTENSIONS
2206 union pf_rule_xport
*cx
= &cur
->dst
.xport
;
2207 union pf_rule_xport
*px
= &prev
->dst
.xport
;
2209 switch (cur
->proto
) {
2211 if (cur
->proto
!= prev
->proto
||
2212 cx
->call_id
!= px
->call_id
)
2213 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2216 if (cur
->proto
!= prev
->proto
||
2218 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2221 if (prev
->proto
== IPPROTO_GRE
||
2222 prev
->proto
== IPPROTO_ESP
||
2223 cx
->range
.op
!= px
->range
.op
||
2224 cx
->range
.port
[0] != px
->range
.port
[0] ||
2225 cx
->range
.port
[1] != px
->range
.port
[1])
2226 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2231 if (cur
->dst
.port
[0] != prev
->dst
.port
[0] ||
2232 cur
->dst
.port
[1] != prev
->dst
.port
[1] ||
2233 cur
->dst
.port_op
!= prev
->dst
.port_op
)
2234 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT
);
2238 cur
= TAILQ_NEXT(cur
, entries
);
2240 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
)
2241 PF_SET_SKIP_STEPS(i
);
2245 pf_addr_wrap_neq(struct pf_addr_wrap
*aw1
, struct pf_addr_wrap
*aw2
)
2247 if (aw1
->type
!= aw2
->type
)
2249 switch (aw1
->type
) {
2250 case PF_ADDR_ADDRMASK
:
2252 if (PF_ANEQ(&aw1
->v
.a
.addr
, &aw2
->v
.a
.addr
, 0))
2254 if (PF_ANEQ(&aw1
->v
.a
.mask
, &aw2
->v
.a
.mask
, 0))
2257 case PF_ADDR_DYNIFTL
:
2258 return (aw1
->p
.dyn
->pfid_kt
!= aw2
->p
.dyn
->pfid_kt
);
2259 case PF_ADDR_NOROUTE
:
2260 case PF_ADDR_URPFFAILED
:
2263 return (aw1
->p
.tbl
!= aw2
->p
.tbl
);
2264 case PF_ADDR_RTLABEL
:
2265 return (aw1
->v
.rtlabel
!= aw2
->v
.rtlabel
);
2267 printf("invalid address type: %d\n", aw1
->type
);
2273 pf_cksum_fixup(u_int16_t cksum
, u_int16_t old
, u_int16_t
new, u_int8_t udp
)
2279 l
= cksum
+ old
- new;
2280 l
= (l
>> 16) + (l
& 0xffff);
2288 pf_change_ap(int dir
, struct mbuf
*m
, struct pf_addr
*a
, u_int16_t
*p
,
2289 u_int16_t
*ic
, u_int16_t
*pc
, struct pf_addr
*an
, u_int16_t pn
,
2290 u_int8_t u
, sa_family_t af
)
2295 PF_ACPY(&ao
, a
, af
);
2303 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2304 ao
.addr16
[0], an
->addr16
[0], 0),
2305 ao
.addr16
[1], an
->addr16
[1], 0);
2308 * If the packet is originated from an ALG on the NAT gateway
2309 * (source address is loopback or local), in which case the
2310 * TCP/UDP checksum field contains the pseudo header checksum
2311 * that's not yet complemented.
2313 if (dir
== PF_OUT
&& m
!= NULL
&&
2314 (m
->m_flags
& M_PKTHDR
) &&
2315 (m
->m_pkthdr
.csum_flags
& (CSUM_TCP
| CSUM_UDP
))) {
2316 /* Pseudo-header checksum does not include ports */
2317 *pc
= ~pf_cksum_fixup(pf_cksum_fixup(~*pc
,
2318 ao
.addr16
[0], an
->addr16
[0], u
),
2319 ao
.addr16
[1], an
->addr16
[1], u
);
2321 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2322 ao
.addr16
[0], an
->addr16
[0], u
),
2323 ao
.addr16
[1], an
->addr16
[1], u
),
2330 *pc
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2331 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2332 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc
,
2333 ao
.addr16
[0], an
->addr16
[0], u
),
2334 ao
.addr16
[1], an
->addr16
[1], u
),
2335 ao
.addr16
[2], an
->addr16
[2], u
),
2336 ao
.addr16
[3], an
->addr16
[3], u
),
2337 ao
.addr16
[4], an
->addr16
[4], u
),
2338 ao
.addr16
[5], an
->addr16
[5], u
),
2339 ao
.addr16
[6], an
->addr16
[6], u
),
2340 ao
.addr16
[7], an
->addr16
[7], u
),
2348 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */
2350 pf_change_a(void *a
, u_int16_t
*c
, u_int32_t an
, u_int8_t u
)
2354 memcpy(&ao
, a
, sizeof (ao
));
2355 memcpy(a
, &an
, sizeof (u_int32_t
));
2356 *c
= pf_cksum_fixup(pf_cksum_fixup(*c
, ao
/ 65536, an
/ 65536, u
),
2357 ao
% 65536, an
% 65536, u
);
2362 pf_change_a6(struct pf_addr
*a
, u_int16_t
*c
, struct pf_addr
*an
, u_int8_t u
)
2366 PF_ACPY(&ao
, a
, AF_INET6
);
2367 PF_ACPY(a
, an
, AF_INET6
);
2369 *c
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2370 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2371 pf_cksum_fixup(pf_cksum_fixup(*c
,
2372 ao
.addr16
[0], an
->addr16
[0], u
),
2373 ao
.addr16
[1], an
->addr16
[1], u
),
2374 ao
.addr16
[2], an
->addr16
[2], u
),
2375 ao
.addr16
[3], an
->addr16
[3], u
),
2376 ao
.addr16
[4], an
->addr16
[4], u
),
2377 ao
.addr16
[5], an
->addr16
[5], u
),
2378 ao
.addr16
[6], an
->addr16
[6], u
),
2379 ao
.addr16
[7], an
->addr16
[7], u
);
2384 pf_change_icmp(struct pf_addr
*ia
, u_int16_t
*ip
, struct pf_addr
*oa
,
2385 struct pf_addr
*na
, u_int16_t np
, u_int16_t
*pc
, u_int16_t
*h2c
,
2386 u_int16_t
*ic
, u_int16_t
*hc
, u_int8_t u
, sa_family_t af
)
2388 struct pf_addr oia
, ooa
;
2390 PF_ACPY(&oia
, ia
, af
);
2391 PF_ACPY(&ooa
, oa
, af
);
2393 /* Change inner protocol port, fix inner protocol checksum. */
2395 u_int16_t oip
= *ip
;
2402 *pc
= pf_cksum_fixup(*pc
, oip
, *ip
, u
);
2403 *ic
= pf_cksum_fixup(*ic
, oip
, *ip
, 0);
2405 *ic
= pf_cksum_fixup(*ic
, opc
, *pc
, 0);
2407 /* Change inner ip address, fix inner ip and icmp checksums. */
2408 PF_ACPY(ia
, na
, af
);
2412 u_int32_t oh2c
= *h2c
;
2414 *h2c
= pf_cksum_fixup(pf_cksum_fixup(*h2c
,
2415 oia
.addr16
[0], ia
->addr16
[0], 0),
2416 oia
.addr16
[1], ia
->addr16
[1], 0);
2417 *ic
= pf_cksum_fixup(pf_cksum_fixup(*ic
,
2418 oia
.addr16
[0], ia
->addr16
[0], 0),
2419 oia
.addr16
[1], ia
->addr16
[1], 0);
2420 *ic
= pf_cksum_fixup(*ic
, oh2c
, *h2c
, 0);
2426 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2427 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2428 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2429 oia
.addr16
[0], ia
->addr16
[0], u
),
2430 oia
.addr16
[1], ia
->addr16
[1], u
),
2431 oia
.addr16
[2], ia
->addr16
[2], u
),
2432 oia
.addr16
[3], ia
->addr16
[3], u
),
2433 oia
.addr16
[4], ia
->addr16
[4], u
),
2434 oia
.addr16
[5], ia
->addr16
[5], u
),
2435 oia
.addr16
[6], ia
->addr16
[6], u
),
2436 oia
.addr16
[7], ia
->addr16
[7], u
);
2440 /* Change outer ip address, fix outer ip or icmpv6 checksum. */
2441 PF_ACPY(oa
, na
, af
);
2445 *hc
= pf_cksum_fixup(pf_cksum_fixup(*hc
,
2446 ooa
.addr16
[0], oa
->addr16
[0], 0),
2447 ooa
.addr16
[1], oa
->addr16
[1], 0);
2452 *ic
= pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2453 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(
2454 pf_cksum_fixup(pf_cksum_fixup(*ic
,
2455 ooa
.addr16
[0], oa
->addr16
[0], u
),
2456 ooa
.addr16
[1], oa
->addr16
[1], u
),
2457 ooa
.addr16
[2], oa
->addr16
[2], u
),
2458 ooa
.addr16
[3], oa
->addr16
[3], u
),
2459 ooa
.addr16
[4], oa
->addr16
[4], u
),
2460 ooa
.addr16
[5], oa
->addr16
[5], u
),
2461 ooa
.addr16
[6], oa
->addr16
[6], u
),
2462 ooa
.addr16
[7], oa
->addr16
[7], u
);
2470 * Need to modulate the sequence numbers in the TCP SACK option
2471 * (credits to Krzysztof Pfaff for report and patch)
2474 pf_modulate_sack(struct mbuf
*m
, int off
, struct pf_pdesc
*pd
,
2475 struct tcphdr
*th
, struct pf_state_peer
*dst
)
2477 int hlen
= (th
->th_off
<< 2) - sizeof (*th
), thoptlen
= hlen
;
2478 u_int8_t opts
[MAX_TCPOPTLEN
], *opt
= opts
;
2479 int copyback
= 0, i
, olen
;
2480 struct sackblk sack
;
2482 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2)
2483 if (hlen
< TCPOLEN_SACKLEN
||
2484 !pf_pull_hdr(m
, off
+ sizeof (*th
), opts
, hlen
, NULL
, NULL
, pd
->af
))
2487 while (hlen
>= TCPOLEN_SACKLEN
) {
2490 case TCPOPT_EOL
: /* FALLTHROUGH */
2498 if (olen
>= TCPOLEN_SACKLEN
) {
2499 for (i
= 2; i
+ TCPOLEN_SACK
<= olen
;
2500 i
+= TCPOLEN_SACK
) {
2501 memcpy(&sack
, &opt
[i
], sizeof (sack
));
2502 pf_change_a(&sack
.start
, &th
->th_sum
,
2503 htonl(ntohl(sack
.start
) -
2505 pf_change_a(&sack
.end
, &th
->th_sum
,
2506 htonl(ntohl(sack
.end
) -
2508 memcpy(&opt
[i
], &sack
, sizeof (sack
));
2510 #ifndef NO_APPLE_EXTENSIONS
2511 copyback
= off
+ sizeof (*th
) + thoptlen
;
2525 #ifndef NO_APPLE_EXTENSIONS
2527 m
= pf_lazy_makewritable(pd
, m
, copyback
);
2530 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2534 m_copyback(m
, off
+ sizeof (*th
), thoptlen
, opts
);
2540 pf_send_tcp(const struct pf_rule
*r
, sa_family_t af
,
2541 const struct pf_addr
*saddr
, const struct pf_addr
*daddr
,
2542 u_int16_t sport
, u_int16_t dport
, u_int32_t seq
, u_int32_t ack
,
2543 u_int8_t flags
, u_int16_t win
, u_int16_t mss
, u_int8_t ttl
, int tag
,
2544 u_int16_t rtag
, struct ether_header
*eh
, struct ifnet
*ifp
)
2546 #pragma unused(eh, ifp)
2550 struct ip
*h
= NULL
;
2553 struct ip6_hdr
*h6
= NULL
;
2555 struct tcphdr
*th
= NULL
;
2557 struct pf_mtag
*pf_mtag
;
2559 /* maximum segment size tcp option */
2560 tlen
= sizeof (struct tcphdr
);
2567 len
= sizeof (struct ip
) + tlen
;
2572 len
= sizeof (struct ip6_hdr
) + tlen
;
2576 panic("pf_send_tcp: not AF_INET or AF_INET6!");
2580 /* create outgoing mbuf */
2581 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
2585 if ((pf_mtag
= pf_get_mtag(m
)) == NULL
) {
2591 pf_mtag
->flags
|= PF_TAG_GENERATED
;
2592 pf_mtag
->tag
= rtag
;
2594 if (r
!= NULL
&& PF_RTABLEID_IS_VALID(r
->rtableid
))
2595 pf_mtag
->rtableid
= r
->rtableid
;
2598 if (r
!= NULL
&& r
->qid
) {
2599 pf_mtag
->qid
= r
->qid
;
2600 /* add hints for ecn */
2601 pf_mtag
->hdr
= mtod(m
, struct ip
*);
2604 m
->m_data
+= max_linkhdr
;
2605 m
->m_pkthdr
.len
= m
->m_len
= len
;
2606 m
->m_pkthdr
.rcvif
= NULL
;
2607 bzero(m
->m_data
, len
);
2611 h
= mtod(m
, struct ip
*);
2613 /* IP header fields included in the TCP checksum */
2614 h
->ip_p
= IPPROTO_TCP
;
2615 h
->ip_len
= htons(tlen
);
2616 h
->ip_src
.s_addr
= saddr
->v4
.s_addr
;
2617 h
->ip_dst
.s_addr
= daddr
->v4
.s_addr
;
2619 th
= (struct tcphdr
*)((caddr_t
)h
+ sizeof (struct ip
));
2624 h6
= mtod(m
, struct ip6_hdr
*);
2626 /* IP header fields included in the TCP checksum */
2627 h6
->ip6_nxt
= IPPROTO_TCP
;
2628 h6
->ip6_plen
= htons(tlen
);
2629 memcpy(&h6
->ip6_src
, &saddr
->v6
, sizeof (struct in6_addr
));
2630 memcpy(&h6
->ip6_dst
, &daddr
->v6
, sizeof (struct in6_addr
));
2632 th
= (struct tcphdr
*)((caddr_t
)h6
+ sizeof (struct ip6_hdr
));
2638 th
->th_sport
= sport
;
2639 th
->th_dport
= dport
;
2640 th
->th_seq
= htonl(seq
);
2641 th
->th_ack
= htonl(ack
);
2642 th
->th_off
= tlen
>> 2;
2643 th
->th_flags
= flags
;
2644 th
->th_win
= htons(win
);
2647 opt
= (char *)(th
+ 1);
2648 opt
[0] = TCPOPT_MAXSEG
;
2650 #if BYTE_ORDER != BIG_ENDIAN
2653 bcopy((caddr_t
)&mss
, (caddr_t
)(opt
+ 2), 2);
2662 th
->th_sum
= in_cksum(m
, len
);
2664 /* Finish the IP header */
2666 h
->ip_hl
= sizeof (*h
) >> 2;
2667 h
->ip_tos
= IPTOS_LOWDELAY
;
2669 * ip_output() expects ip_len and ip_off to be in host order.
2672 h
->ip_off
= (path_mtu_discovery
? IP_DF
: 0);
2673 h
->ip_ttl
= ttl
? ttl
: ip_defttl
;
2676 bzero(&ro
, sizeof (ro
));
2677 ip_output(m
, NULL
, &ro
, 0, NULL
, NULL
);
2678 if (ro
.ro_rt
!= NULL
)
2685 struct route_in6 ro6
;
2688 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
,
2689 sizeof (struct ip6_hdr
), tlen
);
2691 h6
->ip6_vfc
|= IPV6_VERSION
;
2692 h6
->ip6_hlim
= IPV6_DEFHLIM
;
2694 bzero(&ro6
, sizeof (ro6
));
2695 ip6_output(m
, NULL
, &ro6
, 0, NULL
, NULL
, 0);
2696 if (ro6
.ro_rt
!= NULL
)
2705 pf_send_icmp(struct mbuf
*m
, u_int8_t type
, u_int8_t code
, sa_family_t af
,
2709 struct pf_mtag
*pf_mtag
;
2711 m0
= m_copy(m
, 0, M_COPYALL
);
2715 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
2718 pf_mtag
->flags
|= PF_TAG_GENERATED
;
2720 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
2721 pf_mtag
->rtableid
= r
->rtableid
;
2725 pf_mtag
->qid
= r
->qid
;
2726 /* add hints for ecn */
2727 pf_mtag
->hdr
= mtod(m0
, struct ip
*);
2733 icmp_error(m0
, type
, code
, 0, 0);
2738 icmp6_error(m0
, type
, code
, 0);
2745 * Return 1 if the addresses a and b match (with mask m), otherwise return 0.
2746 * If n is 0, they match if they are equal. If n is != 0, they match if they
2750 pf_match_addr(u_int8_t n
, struct pf_addr
*a
, struct pf_addr
*m
,
2751 struct pf_addr
*b
, sa_family_t af
)
2758 if ((a
->addr32
[0] & m
->addr32
[0]) ==
2759 (b
->addr32
[0] & m
->addr32
[0]))
2765 if (((a
->addr32
[0] & m
->addr32
[0]) ==
2766 (b
->addr32
[0] & m
->addr32
[0])) &&
2767 ((a
->addr32
[1] & m
->addr32
[1]) ==
2768 (b
->addr32
[1] & m
->addr32
[1])) &&
2769 ((a
->addr32
[2] & m
->addr32
[2]) ==
2770 (b
->addr32
[2] & m
->addr32
[2])) &&
2771 ((a
->addr32
[3] & m
->addr32
[3]) ==
2772 (b
->addr32
[3] & m
->addr32
[3])))
2791 * Return 1 if b <= a <= e, otherwise return 0.
2794 pf_match_addr_range(struct pf_addr
*b
, struct pf_addr
*e
,
2795 struct pf_addr
*a
, sa_family_t af
)
2800 if ((a
->addr32
[0] < b
->addr32
[0]) ||
2801 (a
->addr32
[0] > e
->addr32
[0]))
2810 for (i
= 0; i
< 4; ++i
)
2811 if (a
->addr32
[i
] > b
->addr32
[i
])
2813 else if (a
->addr32
[i
] < b
->addr32
[i
])
2816 for (i
= 0; i
< 4; ++i
)
2817 if (a
->addr32
[i
] < e
->addr32
[i
])
2819 else if (a
->addr32
[i
] > e
->addr32
[i
])
2829 pf_match(u_int8_t op
, u_int32_t a1
, u_int32_t a2
, u_int32_t p
)
2833 return ((p
> a1
) && (p
< a2
));
2835 return ((p
< a1
) || (p
> a2
));
2837 return ((p
>= a1
) && (p
<= a2
));
2851 return (0); /* never reached */
2855 pf_match_port(u_int8_t op
, u_int16_t a1
, u_int16_t a2
, u_int16_t p
)
2857 #if BYTE_ORDER != BIG_ENDIAN
2862 return (pf_match(op
, a1
, a2
, p
));
2865 #ifndef NO_APPLE_EXTENSIONS
2867 pf_match_xport(u_int8_t proto
, u_int8_t proto_variant
, union pf_rule_xport
*rx
,
2868 union pf_state_xport
*sx
)
2875 if (proto_variant
== PF_GRE_PPTP_VARIANT
)
2876 d
= (rx
->call_id
== sx
->call_id
);
2880 d
= (rx
->spi
== sx
->spi
);
2886 case IPPROTO_ICMPV6
:
2888 d
= pf_match_port(rx
->range
.op
,
2889 rx
->range
.port
[0], rx
->range
.port
[1],
2903 pf_match_uid(u_int8_t op
, uid_t a1
, uid_t a2
, uid_t u
)
2905 if (u
== UID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2907 return (pf_match(op
, a1
, a2
, u
));
2911 pf_match_gid(u_int8_t op
, gid_t a1
, gid_t a2
, gid_t g
)
2913 if (g
== GID_MAX
&& op
!= PF_OP_EQ
&& op
!= PF_OP_NE
)
2915 return (pf_match(op
, a1
, a2
, g
));
2919 pf_match_tag(struct mbuf
*m
, struct pf_rule
*r
, struct pf_mtag
*pf_mtag
,
2924 *tag
= pf_mtag
->tag
;
2926 return ((!r
->match_tag_not
&& r
->match_tag
== *tag
) ||
2927 (r
->match_tag_not
&& r
->match_tag
!= *tag
));
2931 pf_tag_packet(struct mbuf
*m
, struct pf_mtag
*pf_mtag
, int tag
,
2932 unsigned int rtableid
)
2934 if (tag
<= 0 && !PF_RTABLEID_IS_VALID(rtableid
))
2937 if (pf_mtag
== NULL
&& (pf_mtag
= pf_get_mtag(m
)) == NULL
)
2942 if (PF_RTABLEID_IS_VALID(rtableid
))
2943 pf_mtag
->rtableid
= rtableid
;
2949 pf_step_into_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2950 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2952 struct pf_anchor_stackframe
*f
;
2954 (*r
)->anchor
->match
= 0;
2957 if (*depth
>= (int)sizeof (pf_anchor_stack
) /
2958 (int)sizeof (pf_anchor_stack
[0])) {
2959 printf("pf_step_into_anchor: stack overflow\n");
2960 *r
= TAILQ_NEXT(*r
, entries
);
2962 } else if (*depth
== 0 && a
!= NULL
)
2964 f
= pf_anchor_stack
+ (*depth
)++;
2967 if ((*r
)->anchor_wildcard
) {
2968 f
->parent
= &(*r
)->anchor
->children
;
2969 if ((f
->child
= RB_MIN(pf_anchor_node
, f
->parent
)) ==
2974 *rs
= &f
->child
->ruleset
;
2978 *rs
= &(*r
)->anchor
->ruleset
;
2980 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
2984 pf_step_out_of_anchor(int *depth
, struct pf_ruleset
**rs
, int n
,
2985 struct pf_rule
**r
, struct pf_rule
**a
, int *match
)
2987 struct pf_anchor_stackframe
*f
;
2993 f
= pf_anchor_stack
+ *depth
- 1;
2994 if (f
->parent
!= NULL
&& f
->child
!= NULL
) {
2995 if (f
->child
->match
||
2996 (match
!= NULL
&& *match
)) {
2997 f
->r
->anchor
->match
= 1;
3000 f
->child
= RB_NEXT(pf_anchor_node
, f
->parent
, f
->child
);
3001 if (f
->child
!= NULL
) {
3002 *rs
= &f
->child
->ruleset
;
3003 *r
= TAILQ_FIRST((*rs
)->rules
[n
].active
.ptr
);
3011 if (*depth
== 0 && a
!= NULL
)
3014 if (f
->r
->anchor
->match
|| (match
!= NULL
&& *match
))
3015 quick
= f
->r
->quick
;
3016 *r
= TAILQ_NEXT(f
->r
, entries
);
3017 } while (*r
== NULL
);
3024 pf_poolmask(struct pf_addr
*naddr
, struct pf_addr
*raddr
,
3025 struct pf_addr
*rmask
, struct pf_addr
*saddr
, sa_family_t af
)
3030 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
3031 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
3035 naddr
->addr32
[0] = (raddr
->addr32
[0] & rmask
->addr32
[0]) |
3036 ((rmask
->addr32
[0] ^ 0xffffffff) & saddr
->addr32
[0]);
3037 naddr
->addr32
[1] = (raddr
->addr32
[1] & rmask
->addr32
[1]) |
3038 ((rmask
->addr32
[1] ^ 0xffffffff) & saddr
->addr32
[1]);
3039 naddr
->addr32
[2] = (raddr
->addr32
[2] & rmask
->addr32
[2]) |
3040 ((rmask
->addr32
[2] ^ 0xffffffff) & saddr
->addr32
[2]);
3041 naddr
->addr32
[3] = (raddr
->addr32
[3] & rmask
->addr32
[3]) |
3042 ((rmask
->addr32
[3] ^ 0xffffffff) & saddr
->addr32
[3]);
3048 pf_addr_inc(struct pf_addr
*addr
, sa_family_t af
)
3053 addr
->addr32
[0] = htonl(ntohl(addr
->addr32
[0]) + 1);
3057 if (addr
->addr32
[3] == 0xffffffff) {
3058 addr
->addr32
[3] = 0;
3059 if (addr
->addr32
[2] == 0xffffffff) {
3060 addr
->addr32
[2] = 0;
3061 if (addr
->addr32
[1] == 0xffffffff) {
3062 addr
->addr32
[1] = 0;
3064 htonl(ntohl(addr
->addr32
[0]) + 1);
3067 htonl(ntohl(addr
->addr32
[1]) + 1);
3070 htonl(ntohl(addr
->addr32
[2]) + 1);
3073 htonl(ntohl(addr
->addr32
[3]) + 1);
3079 #define mix(a, b, c) \
3081 a -= b; a -= c; a ^= (c >> 13); \
3082 b -= c; b -= a; b ^= (a << 8); \
3083 c -= a; c -= b; c ^= (b >> 13); \
3084 a -= b; a -= c; a ^= (c >> 12); \
3085 b -= c; b -= a; b ^= (a << 16); \
3086 c -= a; c -= b; c ^= (b >> 5); \
3087 a -= b; a -= c; a ^= (c >> 3); \
3088 b -= c; b -= a; b ^= (a << 10); \
3089 c -= a; c -= b; c ^= (b >> 15); \
3093 * hash function based on bridge_hash in if_bridge.c
3096 pf_hash(struct pf_addr
*inaddr
, struct pf_addr
*hash
,
3097 struct pf_poolhashkey
*key
, sa_family_t af
)
3099 u_int32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= key
->key32
[0];
3104 a
+= inaddr
->addr32
[0];
3107 hash
->addr32
[0] = c
+ key
->key32
[2];
3112 a
+= inaddr
->addr32
[0];
3113 b
+= inaddr
->addr32
[2];
3115 hash
->addr32
[0] = c
;
3116 a
+= inaddr
->addr32
[1];
3117 b
+= inaddr
->addr32
[3];
3120 hash
->addr32
[1] = c
;
3121 a
+= inaddr
->addr32
[2];
3122 b
+= inaddr
->addr32
[1];
3125 hash
->addr32
[2] = c
;
3126 a
+= inaddr
->addr32
[3];
3127 b
+= inaddr
->addr32
[0];
3130 hash
->addr32
[3] = c
;
3137 pf_map_addr(sa_family_t af
, struct pf_rule
*r
, struct pf_addr
*saddr
,
3138 struct pf_addr
*naddr
, struct pf_addr
*init_addr
, struct pf_src_node
**sn
)
3140 unsigned char hash
[16];
3141 struct pf_pool
*rpool
= &r
->rpool
;
3142 struct pf_addr
*raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3143 struct pf_addr
*rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3144 struct pf_pooladdr
*acur
= rpool
->cur
;
3145 struct pf_src_node k
;
3147 if (*sn
== NULL
&& r
->rpool
.opts
& PF_POOL_STICKYADDR
&&
3148 (r
->rpool
.opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3150 PF_ACPY(&k
.addr
, saddr
, af
);
3151 if (r
->rule_flag
& PFRULE_RULESRCTRACK
||
3152 r
->rpool
.opts
& PF_POOL_STICKYADDR
)
3156 pf_status
.scounters
[SCNT_SRC_NODE_SEARCH
]++;
3157 *sn
= RB_FIND(pf_src_tree
, &tree_src_tracking
, &k
);
3158 if (*sn
!= NULL
&& !PF_AZERO(&(*sn
)->raddr
, af
)) {
3159 PF_ACPY(naddr
, &(*sn
)->raddr
, af
);
3160 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
3161 printf("pf_map_addr: src tracking maps ");
3162 pf_print_host(&k
.addr
, 0, af
);
3164 pf_print_host(naddr
, 0, af
);
3171 if (rpool
->cur
->addr
.type
== PF_ADDR_NOROUTE
)
3173 if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3177 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt4
< 1 &&
3178 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3181 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr4
;
3182 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask4
;
3187 if (rpool
->cur
->addr
.p
.dyn
->pfid_acnt6
< 1 &&
3188 (rpool
->opts
& PF_POOL_TYPEMASK
) !=
3191 raddr
= &rpool
->cur
->addr
.p
.dyn
->pfid_addr6
;
3192 rmask
= &rpool
->cur
->addr
.p
.dyn
->pfid_mask6
;
3196 } else if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3197 if ((rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_ROUNDROBIN
)
3198 return (1); /* unsupported */
3200 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3201 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3204 switch (rpool
->opts
& PF_POOL_TYPEMASK
) {
3206 PF_ACPY(naddr
, raddr
, af
);
3208 case PF_POOL_BITMASK
:
3209 PF_POOLMASK(naddr
, raddr
, rmask
, saddr
, af
);
3211 case PF_POOL_RANDOM
:
3212 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
)) {
3216 rpool
->counter
.addr32
[0] = htonl(random());
3221 if (rmask
->addr32
[3] != 0xffffffff)
3222 rpool
->counter
.addr32
[3] =
3226 if (rmask
->addr32
[2] != 0xffffffff)
3227 rpool
->counter
.addr32
[2] =
3231 if (rmask
->addr32
[1] != 0xffffffff)
3232 rpool
->counter
.addr32
[1] =
3236 if (rmask
->addr32
[0] != 0xffffffff)
3237 rpool
->counter
.addr32
[0] =
3242 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3243 PF_ACPY(init_addr
, naddr
, af
);
3246 PF_AINC(&rpool
->counter
, af
);
3247 PF_POOLMASK(naddr
, raddr
, rmask
, &rpool
->counter
, af
);
3250 case PF_POOL_SRCHASH
:
3251 pf_hash(saddr
, (struct pf_addr
*)&hash
, &rpool
->key
, af
);
3252 PF_POOLMASK(naddr
, raddr
, rmask
, (struct pf_addr
*)&hash
, af
);
3254 case PF_POOL_ROUNDROBIN
:
3255 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3256 if (!pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3257 &rpool
->tblidx
, &rpool
->counter
,
3258 &raddr
, &rmask
, af
))
3260 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3261 if (!pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3262 &rpool
->tblidx
, &rpool
->counter
,
3263 &raddr
, &rmask
, af
))
3265 } else if (pf_match_addr(0, raddr
, rmask
, &rpool
->counter
, af
))
3269 if ((rpool
->cur
= TAILQ_NEXT(rpool
->cur
, entries
)) == NULL
)
3270 rpool
->cur
= TAILQ_FIRST(&rpool
->list
);
3271 if (rpool
->cur
->addr
.type
== PF_ADDR_TABLE
) {
3273 if (pfr_pool_get(rpool
->cur
->addr
.p
.tbl
,
3274 &rpool
->tblidx
, &rpool
->counter
,
3275 &raddr
, &rmask
, af
)) {
3276 /* table contains no address of type 'af' */
3277 if (rpool
->cur
!= acur
)
3281 } else if (rpool
->cur
->addr
.type
== PF_ADDR_DYNIFTL
) {
3283 if (pfr_pool_get(rpool
->cur
->addr
.p
.dyn
->pfid_kt
,
3284 &rpool
->tblidx
, &rpool
->counter
,
3285 &raddr
, &rmask
, af
)) {
3286 /* table contains no address of type 'af' */
3287 if (rpool
->cur
!= acur
)
3292 raddr
= &rpool
->cur
->addr
.v
.a
.addr
;
3293 rmask
= &rpool
->cur
->addr
.v
.a
.mask
;
3294 PF_ACPY(&rpool
->counter
, raddr
, af
);
3298 PF_ACPY(naddr
, &rpool
->counter
, af
);
3299 if (init_addr
!= NULL
&& PF_AZERO(init_addr
, af
))
3300 PF_ACPY(init_addr
, naddr
, af
);
3301 PF_AINC(&rpool
->counter
, af
);
3305 PF_ACPY(&(*sn
)->raddr
, naddr
, af
);
3307 if (pf_status
.debug
>= PF_DEBUG_MISC
&&
3308 (rpool
->opts
& PF_POOL_TYPEMASK
) != PF_POOL_NONE
) {
3309 printf("pf_map_addr: selected address ");
3310 pf_print_host(naddr
, 0, af
);
3317 #ifndef NO_APPLE_EXTENSIONS
3319 pf_get_sport(struct pf_pdesc
*pd
, struct pfi_kif
*kif
, struct pf_rule
*r
,
3320 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3321 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3322 union pf_state_xport
*nxport
, struct pf_src_node
**sn
)
3325 pf_get_sport(sa_family_t af
, u_int8_t proto
, struct pf_rule
*r
,
3326 struct pf_addr
*saddr
, struct pf_addr
*daddr
, u_int16_t dport
,
3327 struct pf_addr
*naddr
, u_int16_t
*nport
, u_int16_t low
, u_int16_t high
,
3328 struct pf_src_node
**sn
)
3332 struct pf_state_key_cmp key
;
3333 struct pf_addr init_addr
;
3334 #ifndef NO_APPLE_EXTENSIONS
3336 sa_family_t af
= pd
->af
;
3337 u_int8_t proto
= pd
->proto
;
3338 unsigned int low
= ntohs(r
->rpool
.proxy_port
[0]);
3339 unsigned int high
= ntohs(r
->rpool
.proxy_port
[1]);
3344 bzero(&init_addr
, sizeof (init_addr
));
3345 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3348 if (proto
== IPPROTO_ICMP
) {
3353 #ifndef NO_APPLE_EXTENSIONS
3355 return (0); /* No output necessary. */
3357 /*--- Special mapping rules for UDP ---*/
3358 if (proto
== IPPROTO_UDP
) {
3360 /*--- Never float IKE source port ---*/
3361 if (sxport
->port
== PF_IKE_PORT
) {
3362 nxport
->port
= sxport
->port
;
3366 /*--- Apply exterior mapping options ---*/
3367 if (r
->extmap
> PF_EXTMAP_APD
) {
3370 TAILQ_FOREACH(s
, &state_list
, entry_list
) {
3371 struct pf_state_key
*sk
= s
->state_key
;
3374 if (s
->nat_rule
.ptr
!= r
)
3376 if (sk
->proto
!= IPPROTO_UDP
|| sk
->af
!= af
)
3378 if (sk
->lan
.xport
.port
!= sxport
->port
)
3380 if (PF_ANEQ(&sk
->lan
.addr
, saddr
, af
))
3382 if (r
->extmap
< PF_EXTMAP_EI
&&
3383 PF_ANEQ(&sk
->ext
.addr
, daddr
, af
))
3386 nxport
->port
= sk
->gwy
.xport
.port
;
3396 PF_ACPY(&key
.ext
.addr
, daddr
, key
.af
);
3397 PF_ACPY(&key
.gwy
.addr
, naddr
, key
.af
);
3398 #ifndef NO_APPLE_EXTENSIONS
3401 key
.proto_variant
= r
->extfilter
;
3404 key
.proto_variant
= 0;
3408 key
.ext
.xport
= *dxport
;
3410 memset(&key
.ext
.xport
, 0, sizeof (key
.ext
.xport
));
3412 key
.ext
.port
= dport
;
3416 * port search; start random, step;
3417 * similar 2 portloop in in_pcbbind
3419 if (!(proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
||
3420 proto
== IPPROTO_ICMP
)) {
3421 #ifndef NO_APPLE_EXTENSIONS
3423 key
.gwy
.xport
= *dxport
;
3425 memset(&key
.gwy
.xport
, 0,
3426 sizeof (key
.ext
.xport
));
3428 key
.gwy
.port
= dport
;
3430 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3432 } else if (low
== 0 && high
== 0) {
3433 #ifndef NO_APPLE_EXTENSIONS
3434 key
.gwy
.xport
= *nxport
;
3436 key
.gwy
.port
= *nport
;
3438 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
)
3440 } else if (low
== high
) {
3441 #ifndef NO_APPLE_EXTENSIONS
3442 key
.gwy
.xport
.port
= htons(low
);
3443 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3444 nxport
->port
= htons(low
);
3448 key
.gwy
.port
= htons(low
);
3449 if (pf_find_state_all(&key
, PF_IN
, NULL
) == NULL
) {
3450 *nport
= htons(low
);
3455 #ifndef NO_APPLE_EXTENSIONS
3466 cut
= htonl(random()) % (1 + high
- low
) + low
;
3467 /* low <= cut <= high */
3468 for (tmp
= cut
; tmp
<= high
; ++(tmp
)) {
3469 #ifndef NO_APPLE_EXTENSIONS
3470 key
.gwy
.xport
.port
= htons(tmp
);
3471 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3473 nxport
->port
= htons(tmp
);
3477 key
.gwy
.port
= htons(tmp
);
3478 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3480 *nport
= htons(tmp
);
3485 for (tmp
= cut
- 1; tmp
>= low
; --(tmp
)) {
3486 #ifndef NO_APPLE_EXTENSIONS
3487 key
.gwy
.xport
.port
= htons(tmp
);
3488 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3490 nxport
->port
= htons(tmp
);
3494 key
.gwy
.port
= htons(tmp
);
3495 if (pf_find_state_all(&key
, PF_IN
, NULL
) ==
3497 *nport
= htons(tmp
);
3504 switch (r
->rpool
.opts
& PF_POOL_TYPEMASK
) {
3505 case PF_POOL_RANDOM
:
3506 case PF_POOL_ROUNDROBIN
:
3507 if (pf_map_addr(af
, r
, saddr
, naddr
, &init_addr
, sn
))
3511 case PF_POOL_SRCHASH
:
3512 case PF_POOL_BITMASK
:
3516 } while (!PF_AEQ(&init_addr
, naddr
, af
));
3518 return (1); /* none available */
3521 #ifndef NO_APPLE_EXTENSIONS
3522 static struct pf_rule
*
3523 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3524 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
,
3525 union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3526 union pf_state_xport
*dxport
, int rs_num
)
3529 pf_match_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3530 int direction
, struct pfi_kif
*kif
, struct pf_addr
*saddr
, u_int16_t sport
,
3531 struct pf_addr
*daddr
, u_int16_t dport
, int rs_num
)
3534 struct pf_rule
*r
, *rm
= NULL
;
3535 struct pf_ruleset
*ruleset
= NULL
;
3537 unsigned int rtableid
= IFSCOPE_NONE
;
3540 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs_num
].active
.ptr
);
3541 while (r
&& rm
== NULL
) {
3542 struct pf_rule_addr
*src
= NULL
, *dst
= NULL
;
3543 struct pf_addr_wrap
*xdst
= NULL
;
3544 #ifndef NO_APPLE_EXTENSIONS
3545 struct pf_addr_wrap
*xsrc
= NULL
;
3548 if (r
->action
== PF_BINAT
&& direction
== PF_IN
) {
3550 if (r
->rpool
.cur
!= NULL
)
3551 xdst
= &r
->rpool
.cur
->addr
;
3552 #ifndef NO_APPLE_EXTENSIONS
3553 } else if (r
->action
== PF_RDR
&& direction
== PF_OUT
) {
3556 if (r
->rpool
.cur
!= NULL
)
3557 xsrc
= &r
->rpool
.cur
->addr
;
3565 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
3566 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
3567 else if (r
->direction
&& r
->direction
!= direction
)
3568 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
3569 else if (r
->af
&& r
->af
!= pd
->af
)
3570 r
= r
->skip
[PF_SKIP_AF
].ptr
;
3571 else if (r
->proto
&& r
->proto
!= pd
->proto
)
3572 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
3573 #ifndef NO_APPLE_EXTENSIONS
3574 else if (xsrc
&& PF_MISMATCHAW(xsrc
, saddr
, pd
->af
, 0, NULL
))
3575 r
= TAILQ_NEXT(r
, entries
);
3576 else if (!xsrc
&& PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3578 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_ADDR
:
3579 PF_SKIP_DST_ADDR
].ptr
;
3580 else if (!pf_match_xport(r
->proto
, r
->proto_variant
, &src
->xport
,
3583 else if (PF_MISMATCHAW(&src
->addr
, saddr
, pd
->af
,
3585 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_ADDR
:
3586 PF_SKIP_DST_ADDR
].ptr
;
3587 else if (src
->port_op
&& !pf_match_port(src
->port_op
,
3588 src
->port
[0], src
->port
[1], sport
))
3590 r
= r
->skip
[src
== &r
->src
? PF_SKIP_SRC_PORT
:
3591 PF_SKIP_DST_PORT
].ptr
;
3592 else if (dst
!= NULL
&&
3593 PF_MISMATCHAW(&dst
->addr
, daddr
, pd
->af
, dst
->neg
, NULL
))
3594 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
3595 else if (xdst
!= NULL
&& PF_MISMATCHAW(xdst
, daddr
, pd
->af
,
3597 r
= TAILQ_NEXT(r
, entries
);
3598 #ifndef NO_APPLE_EXTENSIONS
3599 else if (dst
&& !pf_match_xport(r
->proto
, r
->proto_variant
,
3600 &dst
->xport
, dxport
))
3602 else if (dst
!= NULL
&& dst
->port_op
&&
3603 !pf_match_port(dst
->port_op
, dst
->port
[0],
3604 dst
->port
[1], dport
))
3606 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
3607 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
3608 r
= TAILQ_NEXT(r
, entries
);
3609 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&& (pd
->proto
!=
3610 IPPROTO_TCP
|| !pf_osfp_match(pf_osfp_fingerprint(pd
, m
,
3611 off
, pd
->hdr
.tcp
), r
->os_fingerprint
)))
3612 r
= TAILQ_NEXT(r
, entries
);
3616 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
3617 rtableid
= r
->rtableid
;
3618 if (r
->anchor
== NULL
) {
3621 pf_step_into_anchor(&asd
, &ruleset
, rs_num
,
3625 pf_step_out_of_anchor(&asd
, &ruleset
, rs_num
, &r
,
3628 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
))
3630 if (rm
!= NULL
&& (rm
->action
== PF_NONAT
||
3631 rm
->action
== PF_NORDR
|| rm
->action
== PF_NOBINAT
))
3636 #ifndef NO_APPLE_EXTENSIONS
3637 static struct pf_rule
*
3638 pf_get_translation_aux(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
3639 int direction
, struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3640 struct pf_addr
*saddr
, union pf_state_xport
*sxport
, struct pf_addr
*daddr
,
3641 union pf_state_xport
*dxport
, struct pf_addr
*naddr
,
3642 union pf_state_xport
*nxport
)
3645 pf_get_translation(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
, int direction
,
3646 struct pfi_kif
*kif
, struct pf_src_node
**sn
,
3647 struct pf_addr
*saddr
, u_int16_t sport
,
3648 struct pf_addr
*daddr
, u_int16_t dport
,
3649 struct pf_addr
*naddr
, u_int16_t
*nport
)
3652 struct pf_rule
*r
= NULL
;
3654 #ifndef NO_APPLE_EXTENSIONS
3655 if (direction
== PF_OUT
) {
3656 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3657 sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3659 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3660 saddr
, sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3662 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3663 saddr
, sxport
, daddr
, dxport
, PF_RULESET_NAT
);
3665 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3666 sxport
, daddr
, dxport
, PF_RULESET_RDR
);
3668 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3669 saddr
, sxport
, daddr
, dxport
, PF_RULESET_BINAT
);
3672 if (direction
== PF_OUT
) {
3673 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3674 sport
, daddr
, dport
, PF_RULESET_BINAT
);
3676 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3677 saddr
, sport
, daddr
, dport
, PF_RULESET_NAT
);
3679 r
= pf_match_translation(pd
, m
, off
, direction
, kif
, saddr
,
3680 sport
, daddr
, dport
, PF_RULESET_RDR
);
3682 r
= pf_match_translation(pd
, m
, off
, direction
, kif
,
3683 saddr
, sport
, daddr
, dport
, PF_RULESET_BINAT
);
3688 switch (r
->action
) {
3694 #ifndef NO_APPLE_EXTENSIONS
3695 if (pf_get_sport(pd
, kif
, r
, saddr
, sxport
, daddr
,
3696 dxport
, naddr
, nxport
, sn
)) {
3698 if (pf_get_sport(pd
->af
, pd
->proto
, r
, saddr
,
3699 daddr
, dport
, naddr
, nport
, r
->rpool
.proxy_port
[0],
3700 r
->rpool
.proxy_port
[1], sn
)) {
3702 DPFPRINTF(PF_DEBUG_MISC
,
3703 ("pf: NAT proxy port allocation "
3705 r
->rpool
.proxy_port
[0],
3706 r
->rpool
.proxy_port
[1]));
3711 switch (direction
) {
3713 if (r
->rpool
.cur
->addr
.type
==
3718 if (r
->rpool
.cur
->addr
.p
.dyn
->
3722 &r
->rpool
.cur
->addr
.p
.dyn
->
3724 &r
->rpool
.cur
->addr
.p
.dyn
->
3731 if (r
->rpool
.cur
->addr
.p
.dyn
->
3735 &r
->rpool
.cur
->addr
.p
.dyn
->
3737 &r
->rpool
.cur
->addr
.p
.dyn
->
3745 &r
->rpool
.cur
->addr
.v
.a
.addr
,
3746 &r
->rpool
.cur
->addr
.v
.a
.mask
,
3751 if (r
->src
.addr
.type
== PF_ADDR_DYNIFTL
) {
3755 if (r
->src
.addr
.p
.dyn
->
3759 &r
->src
.addr
.p
.dyn
->
3761 &r
->src
.addr
.p
.dyn
->
3768 if (r
->src
.addr
.p
.dyn
->
3772 &r
->src
.addr
.p
.dyn
->
3774 &r
->src
.addr
.p
.dyn
->
3782 &r
->src
.addr
.v
.a
.addr
,
3783 &r
->src
.addr
.v
.a
.mask
, daddr
,
3789 #ifndef NO_APPLE_EXTENSIONS
3790 switch (direction
) {
3792 if (r
->dst
.addr
.type
== PF_ADDR_DYNIFTL
) {
3796 if (r
->dst
.addr
.p
.dyn
->
3800 &r
->dst
.addr
.p
.dyn
->
3802 &r
->dst
.addr
.p
.dyn
->
3809 if (r
->dst
.addr
.p
.dyn
->
3813 &r
->dst
.addr
.p
.dyn
->
3815 &r
->dst
.addr
.p
.dyn
->
3823 &r
->dst
.addr
.v
.a
.addr
,
3824 &r
->dst
.addr
.v
.a
.mask
,
3827 if (nxport
&& dxport
)
3831 if (pf_map_addr(pd
->af
, r
, saddr
,
3834 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3836 PF_POOLMASK(naddr
, naddr
,
3837 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3840 if (nxport
&& dxport
) {
3841 if (r
->rpool
.proxy_port
[1]) {
3842 u_int32_t tmp_nport
;
3845 ((ntohs(dxport
->port
) -
3846 ntohs(r
->dst
.xport
.range
.
3848 (r
->rpool
.proxy_port
[1] -
3849 r
->rpool
.proxy_port
[0] +
3850 1)) + r
->rpool
.proxy_port
[0];
3852 /* wrap around if necessary */
3853 if (tmp_nport
> 65535)
3856 htons((u_int16_t
)tmp_nport
);
3857 } else if (r
->rpool
.proxy_port
[0]) {
3858 nxport
->port
= htons(r
->rpool
.
3865 if (pf_map_addr(pd
->af
, r
, saddr
, naddr
, NULL
, sn
))
3867 if ((r
->rpool
.opts
& PF_POOL_TYPEMASK
) ==
3869 PF_POOLMASK(naddr
, naddr
,
3870 &r
->rpool
.cur
->addr
.v
.a
.mask
, daddr
,
3873 if (r
->rpool
.proxy_port
[1]) {
3874 u_int32_t tmp_nport
;
3876 tmp_nport
= ((ntohs(dport
) -
3877 ntohs(r
->dst
.port
[0])) %
3878 (r
->rpool
.proxy_port
[1] -
3879 r
->rpool
.proxy_port
[0] + 1)) +
3880 r
->rpool
.proxy_port
[0];
3882 /* wrap around if necessary */
3883 if (tmp_nport
> 65535)
3885 *nport
= htons((u_int16_t
)tmp_nport
);
3886 } else if (r
->rpool
.proxy_port
[0])
3887 *nport
= htons(r
->rpool
.proxy_port
[0]);
3900 pf_socket_lookup(int direction
, struct pf_pdesc
*pd
)
3902 struct pf_addr
*saddr
, *daddr
;
3903 u_int16_t sport
, dport
;
3904 struct inpcbinfo
*pi
;
3905 struct inpcb
*inp
= NULL
;
3909 pd
->lookup
.uid
= UID_MAX
;
3910 pd
->lookup
.gid
= GID_MAX
;
3911 pd
->lookup
.pid
= NO_PID
;
3913 switch (pd
->proto
) {
3915 if (pd
->hdr
.tcp
== NULL
)
3917 sport
= pd
->hdr
.tcp
->th_sport
;
3918 dport
= pd
->hdr
.tcp
->th_dport
;
3922 if (pd
->hdr
.udp
== NULL
)
3924 sport
= pd
->hdr
.udp
->uh_sport
;
3925 dport
= pd
->hdr
.udp
->uh_dport
;
3931 if (direction
== PF_IN
) {
3946 inp
= in_pcblookup_hash(pi
, saddr
->v4
, sport
, daddr
->v4
, dport
,
3949 inp
= in_pcblookup_hash(pi
, saddr
->v4
, sport
,
3950 daddr
->v4
, dport
, INPLOOKUP_WILDCARD
, NULL
);
3958 inp
= in6_pcblookup_hash(pi
, &saddr
->v6
, sport
, &daddr
->v6
,
3961 inp
= in6_pcblookup_hash(pi
, &saddr
->v6
, sport
,
3962 &daddr
->v6
, dport
, INPLOOKUP_WILDCARD
, NULL
);
3974 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
3980 pf_get_wscale(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
3984 u_int8_t
*opt
, optlen
;
3985 u_int8_t wscale
= 0;
3987 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
3988 if (hlen
<= (int)sizeof (struct tcphdr
))
3990 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
3992 opt
= hdr
+ sizeof (struct tcphdr
);
3993 hlen
-= sizeof (struct tcphdr
);
4003 if (wscale
> TCP_MAX_WINSHIFT
)
4004 wscale
= TCP_MAX_WINSHIFT
;
4005 wscale
|= PF_WSCALE_FLAG
;
4020 pf_get_mss(struct mbuf
*m
, int off
, u_int16_t th_off
, sa_family_t af
)
4024 u_int8_t
*opt
, optlen
;
4025 u_int16_t mss
= tcp_mssdflt
;
4027 hlen
= th_off
<< 2; /* hlen <= sizeof (hdr) */
4028 if (hlen
<= (int)sizeof (struct tcphdr
))
4030 if (!pf_pull_hdr(m
, off
, hdr
, hlen
, NULL
, NULL
, af
))
4032 opt
= hdr
+ sizeof (struct tcphdr
);
4033 hlen
-= sizeof (struct tcphdr
);
4034 while (hlen
>= TCPOLEN_MAXSEG
) {
4042 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, 2);
4043 #if BYTE_ORDER != BIG_ENDIAN
4060 pf_calc_mss(struct pf_addr
*addr
, sa_family_t af
, u_int16_t offer
)
4063 struct sockaddr_in
*dst
;
4067 struct sockaddr_in6
*dst6
;
4068 struct route_in6 ro6
;
4070 struct rtentry
*rt
= NULL
;
4072 u_int16_t mss
= tcp_mssdflt
;
4077 hlen
= sizeof (struct ip
);
4078 bzero(&ro
, sizeof (ro
));
4079 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
4080 dst
->sin_family
= AF_INET
;
4081 dst
->sin_len
= sizeof (*dst
);
4082 dst
->sin_addr
= addr
->v4
;
4089 hlen
= sizeof (struct ip6_hdr
);
4090 bzero(&ro6
, sizeof (ro6
));
4091 dst6
= (struct sockaddr_in6
*)&ro6
.ro_dst
;
4092 dst6
->sin6_family
= AF_INET6
;
4093 dst6
->sin6_len
= sizeof (*dst6
);
4094 dst6
->sin6_addr
= addr
->v6
;
4095 rtalloc((struct route
*)&ro
);
4100 panic("pf_calc_mss: not AF_INET or AF_INET6!");
4104 if (rt
&& rt
->rt_ifp
) {
4105 mss
= rt
->rt_ifp
->if_mtu
- hlen
- sizeof (struct tcphdr
);
4106 mss
= max(tcp_mssdflt
, mss
);
4109 mss
= min(mss
, offer
);
4110 mss
= max(mss
, 64); /* sanity - at least max opt space */
4115 pf_set_rt_ifp(struct pf_state
*s
, struct pf_addr
*saddr
)
4117 struct pf_rule
*r
= s
->rule
.ptr
;
4120 if (!r
->rt
|| r
->rt
== PF_FASTROUTE
)
4122 switch (s
->state_key
->af
) {
4125 pf_map_addr(AF_INET
, r
, saddr
, &s
->rt_addr
, NULL
,
4127 s
->rt_kif
= r
->rpool
.cur
->kif
;
4132 pf_map_addr(AF_INET6
, r
, saddr
, &s
->rt_addr
, NULL
,
4134 s
->rt_kif
= r
->rpool
.cur
->kif
;
4141 pf_attach_state(struct pf_state_key
*sk
, struct pf_state
*s
, int tail
)
4146 /* list is sorted, if-bound states before floating */
4148 TAILQ_INSERT_TAIL(&sk
->states
, s
, next
);
4150 TAILQ_INSERT_HEAD(&sk
->states
, s
, next
);
4154 pf_detach_state(struct pf_state
*s
, int flags
)
4156 struct pf_state_key
*sk
= s
->state_key
;
4161 s
->state_key
= NULL
;
4162 TAILQ_REMOVE(&sk
->states
, s
, next
);
4163 if (--sk
->refcnt
== 0) {
4164 if (!(flags
& PF_DT_SKIP_EXTGWY
))
4165 RB_REMOVE(pf_state_tree_ext_gwy
,
4166 &pf_statetbl_ext_gwy
, sk
);
4167 if (!(flags
& PF_DT_SKIP_LANEXT
))
4168 RB_REMOVE(pf_state_tree_lan_ext
,
4169 &pf_statetbl_lan_ext
, sk
);
4170 #ifndef NO_APPLE_EXTENSIONS
4172 pool_put(&pf_app_state_pl
, sk
->app_state
);
4174 pool_put(&pf_state_key_pl
, sk
);
4178 struct pf_state_key
*
4179 pf_alloc_state_key(struct pf_state
*s
)
4181 struct pf_state_key
*sk
;
4183 if ((sk
= pool_get(&pf_state_key_pl
, PR_WAITOK
)) == NULL
)
4185 bzero(sk
, sizeof (*sk
));
4186 TAILQ_INIT(&sk
->states
);
4187 pf_attach_state(sk
, s
, 0);
4193 pf_tcp_iss(struct pf_pdesc
*pd
)
4196 u_int32_t digest
[4];
4198 if (pf_tcp_secret_init
== 0) {
4199 read_random(pf_tcp_secret
, sizeof (pf_tcp_secret
));
4200 MD5Init(&pf_tcp_secret_ctx
);
4201 MD5Update(&pf_tcp_secret_ctx
, pf_tcp_secret
,
4202 sizeof (pf_tcp_secret
));
4203 pf_tcp_secret_init
= 1;
4205 ctx
= pf_tcp_secret_ctx
;
4207 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_sport
, sizeof (u_short
));
4208 MD5Update(&ctx
, (char *)&pd
->hdr
.tcp
->th_dport
, sizeof (u_short
));
4209 if (pd
->af
== AF_INET6
) {
4210 MD5Update(&ctx
, (char *)&pd
->src
->v6
, sizeof (struct in6_addr
));
4211 MD5Update(&ctx
, (char *)&pd
->dst
->v6
, sizeof (struct in6_addr
));
4213 MD5Update(&ctx
, (char *)&pd
->src
->v4
, sizeof (struct in_addr
));
4214 MD5Update(&ctx
, (char *)&pd
->dst
->v4
, sizeof (struct in_addr
));
4216 MD5Final((u_char
*)digest
, &ctx
);
4217 pf_tcp_iss_off
+= 4096;
4218 return (digest
[0] + random() + pf_tcp_iss_off
);
4222 pf_test_rule(struct pf_rule
**rm
, struct pf_state
**sm
, int direction
,
4223 struct pfi_kif
*kif
, struct mbuf
*m
, int off
, void *h
,
4224 struct pf_pdesc
*pd
, struct pf_rule
**am
, struct pf_ruleset
**rsm
,
4225 struct ifqueue
*ifq
)
4228 struct pf_rule
*nr
= NULL
;
4229 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
4230 #ifdef NO_APPLE_EXTENSIONS
4231 u_int16_t bport
, nport
= 0;
4233 sa_family_t af
= pd
->af
;
4234 struct pf_rule
*r
, *a
= NULL
;
4235 struct pf_ruleset
*ruleset
= NULL
;
4236 struct pf_src_node
*nsn
= NULL
;
4237 struct tcphdr
*th
= pd
->hdr
.tcp
;
4239 int rewrite
= 0, hdrlen
= 0;
4241 unsigned int rtableid
= IFSCOPE_NONE
;
4245 u_int16_t mss
= tcp_mssdflt
;
4246 #ifdef NO_APPLE_EXTENSIONS
4247 u_int16_t sport
, dport
;
4249 u_int8_t icmptype
= 0, icmpcode
= 0;
4251 #ifndef NO_APPLE_EXTENSIONS
4252 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
4253 union pf_state_xport bxport
, nxport
, sxport
, dxport
;
4256 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
4258 if (direction
== PF_IN
&& pf_check_congestion(ifq
)) {
4259 REASON_SET(&reason
, PFRES_CONGEST
);
4263 #ifndef NO_APPLE_EXTENSIONS
4269 sport
= dport
= hdrlen
= 0;
4272 switch (pd
->proto
) {
4274 #ifndef NO_APPLE_EXTENSIONS
4275 sxport
.port
= th
->th_sport
;
4276 dxport
.port
= th
->th_dport
;
4278 sport
= th
->th_sport
;
4279 dport
= th
->th_dport
;
4281 hdrlen
= sizeof (*th
);
4284 #ifndef NO_APPLE_EXTENSIONS
4285 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4286 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4288 sport
= pd
->hdr
.udp
->uh_sport
;
4289 dport
= pd
->hdr
.udp
->uh_dport
;
4291 hdrlen
= sizeof (*pd
->hdr
.udp
);
4295 if (pd
->af
!= AF_INET
)
4297 #ifndef NO_APPLE_EXTENSIONS
4298 sxport
.port
= dxport
.port
= pd
->hdr
.icmp
->icmp_id
;
4299 hdrlen
= ICMP_MINLEN
;
4301 sport
= dport
= pd
->hdr
.icmp
->icmp_id
;
4303 icmptype
= pd
->hdr
.icmp
->icmp_type
;
4304 icmpcode
= pd
->hdr
.icmp
->icmp_code
;
4306 if (icmptype
== ICMP_UNREACH
||
4307 icmptype
== ICMP_SOURCEQUENCH
||
4308 icmptype
== ICMP_REDIRECT
||
4309 icmptype
== ICMP_TIMXCEED
||
4310 icmptype
== ICMP_PARAMPROB
)
4315 case IPPROTO_ICMPV6
:
4316 if (pd
->af
!= AF_INET6
)
4318 #ifndef NO_APPLE_EXTENSIONS
4319 sxport
.port
= dxport
.port
= pd
->hdr
.icmp6
->icmp6_id
;
4321 sport
= dport
= pd
->hdr
.icmp6
->icmp6_id
;
4323 hdrlen
= sizeof (*pd
->hdr
.icmp6
);
4324 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
4325 icmpcode
= pd
->hdr
.icmp6
->icmp6_code
;
4327 if (icmptype
== ICMP6_DST_UNREACH
||
4328 icmptype
== ICMP6_PACKET_TOO_BIG
||
4329 icmptype
== ICMP6_TIME_EXCEEDED
||
4330 icmptype
== ICMP6_PARAM_PROB
)
4334 #ifndef NO_APPLE_EXTENSIONS
4336 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
) {
4337 sxport
.call_id
= dxport
.call_id
=
4338 pd
->hdr
.grev1
->call_id
;
4339 hdrlen
= sizeof (*pd
->hdr
.grev1
);
4344 dxport
.spi
= pd
->hdr
.esp
->spi
;
4345 hdrlen
= sizeof (*pd
->hdr
.esp
);
4350 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
4352 if (direction
== PF_OUT
) {
4353 #ifndef NO_APPLE_EXTENSIONS
4354 bxport
= nxport
= sxport
;
4355 /* check outgoing packet for BINAT/NAT */
4356 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
4357 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4360 bport
= nport
= sport
;
4361 /* check outgoing packet for BINAT/NAT */
4362 if ((nr
= pf_get_translation(pd
, m
, off
, PF_OUT
, kif
, &nsn
,
4363 saddr
, sport
, daddr
, dport
, &pd
->naddr
, &nport
)) != NULL
) {
4365 PF_ACPY(&pd
->baddr
, saddr
, af
);
4366 switch (pd
->proto
) {
4368 #ifndef NO_APPLE_EXTENSIONS
4369 pf_change_ap(direction
, pd
->mp
, saddr
,
4370 &th
->th_sport
, pd
->ip_sum
, &th
->th_sum
,
4371 &pd
->naddr
, nxport
.port
, 0, af
);
4372 sxport
.port
= th
->th_sport
;
4374 pf_change_ap(saddr
, &th
->th_sport
, pd
->ip_sum
,
4375 &th
->th_sum
, &pd
->naddr
, nport
, 0, af
);
4376 sport
= th
->th_sport
;
4381 #ifndef NO_APPLE_EXTENSIONS
4382 pf_change_ap(direction
, pd
->mp
, saddr
,
4383 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4384 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4385 nxport
.port
, 1, af
);
4386 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4388 pf_change_ap(saddr
, &pd
->hdr
.udp
->uh_sport
,
4389 pd
->ip_sum
, &pd
->hdr
.udp
->uh_sum
,
4390 &pd
->naddr
, nport
, 1, af
);
4391 sport
= pd
->hdr
.udp
->uh_sport
;
4397 pf_change_a(&saddr
->v4
.s_addr
, pd
->ip_sum
,
4398 pd
->naddr
.v4
.s_addr
, 0);
4399 #ifndef NO_APPLE_EXTENSIONS
4400 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4401 pd
->hdr
.icmp
->icmp_cksum
, sxport
.port
,
4403 pd
->hdr
.icmp
->icmp_id
= nxport
.port
;
4406 pd
->hdr
.icmp
->icmp_cksum
= pf_cksum_fixup(
4407 pd
->hdr
.icmp
->icmp_cksum
, sport
, nport
, 0);
4408 pd
->hdr
.icmp
->icmp_id
= nport
;
4409 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
4414 case IPPROTO_ICMPV6
:
4415 pf_change_a6(saddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4420 #ifndef NO_APPLE_EXTENSIONS
4425 pf_change_a(&saddr
->v4
.s_addr
,
4426 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4431 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4442 pf_change_a(&saddr
->v4
.s_addr
,
4443 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4448 PF_ACPY(saddr
, &pd
->naddr
, AF_INET6
);
4458 pf_change_a(&saddr
->v4
.s_addr
,
4459 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4464 PF_ACPY(saddr
, &pd
->naddr
, af
);
4476 #ifndef NO_APPLE_EXTENSIONS
4477 bxport
.port
= nxport
.port
= dxport
.port
;
4478 /* check incoming packet for BINAT/RDR */
4479 if ((nr
= pf_get_translation_aux(pd
, m
, off
, PF_IN
, kif
, &nsn
,
4480 saddr
, &sxport
, daddr
, &dxport
, &pd
->naddr
, &nxport
)) !=
4483 bport
= nport
= dport
;
4484 /* check incoming packet for BINAT/RDR */
4485 if ((nr
= pf_get_translation(pd
, m
, off
, PF_IN
, kif
, &nsn
,
4486 saddr
, sport
, daddr
, dport
, &pd
->naddr
, &nport
)) != NULL
) {
4488 PF_ACPY(&pd
->baddr
, daddr
, af
);
4489 switch (pd
->proto
) {
4491 #ifndef NO_APPLE_EXTENSIONS
4492 pf_change_ap(direction
, pd
->mp
, daddr
,
4493 &th
->th_dport
, pd
->ip_sum
, &th
->th_sum
,
4494 &pd
->naddr
, nxport
.port
, 0, af
);
4495 dxport
.port
= th
->th_dport
;
4497 pf_change_ap(daddr
, &th
->th_dport
, pd
->ip_sum
,
4498 &th
->th_sum
, &pd
->naddr
, nport
, 0, af
);
4499 dport
= th
->th_dport
;
4504 #ifndef NO_APPLE_EXTENSIONS
4505 pf_change_ap(direction
, pd
->mp
, daddr
,
4506 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4507 &pd
->hdr
.udp
->uh_sum
, &pd
->naddr
,
4508 nxport
.port
, 1, af
);
4509 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4511 pf_change_ap(direction
, daddr
,
4512 &pd
->hdr
.udp
->uh_dport
,
4513 pd
->ip_sum
, &pd
->hdr
.udp
->uh_sum
,
4514 &pd
->naddr
, nport
, 1, af
);
4515 dport
= pd
->hdr
.udp
->uh_dport
;
4521 pf_change_a(&daddr
->v4
.s_addr
, pd
->ip_sum
,
4522 pd
->naddr
.v4
.s_addr
, 0);
4526 case IPPROTO_ICMPV6
:
4527 pf_change_a6(daddr
, &pd
->hdr
.icmp6
->icmp6_cksum
,
4532 #ifndef NO_APPLE_EXTENSIONS
4534 if (pd
->proto_variant
== PF_GRE_PPTP_VARIANT
)
4535 grev1
->call_id
= nxport
.call_id
;
4540 pf_change_a(&daddr
->v4
.s_addr
,
4541 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4546 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4556 pf_change_a(&daddr
->v4
.s_addr
,
4557 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4562 PF_ACPY(daddr
, &pd
->naddr
, AF_INET6
);
4572 pf_change_a(&daddr
->v4
.s_addr
,
4573 pd
->ip_sum
, pd
->naddr
.v4
.s_addr
, 0);
4578 PF_ACPY(daddr
, &pd
->naddr
, af
);
4591 #ifndef NO_APPLE_EXTENSIONS
4592 if (nr
&& nr
->tag
> 0)
4598 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
4599 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
4600 else if (r
->direction
&& r
->direction
!= direction
)
4601 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
4602 else if (r
->af
&& r
->af
!= af
)
4603 r
= r
->skip
[PF_SKIP_AF
].ptr
;
4604 else if (r
->proto
&& r
->proto
!= pd
->proto
)
4605 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
4606 else if (PF_MISMATCHAW(&r
->src
.addr
, saddr
, af
,
4608 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
4609 /* tcp/udp only. port_op always 0 in other cases */
4610 #ifndef NO_APPLE_EXTENSIONS
4611 else if (r
->proto
== pd
->proto
&&
4612 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4613 r
->src
.xport
.range
.op
&&
4614 !pf_match_port(r
->src
.xport
.range
.op
,
4615 r
->src
.xport
.range
.port
[0], r
->src
.xport
.range
.port
[1],
4618 else if (r
->src
.port_op
&& !pf_match_port(r
->src
.port_op
,
4619 r
->src
.port
[0], r
->src
.port
[1], th
->th_sport
))
4621 r
= r
->skip
[PF_SKIP_SRC_PORT
].ptr
;
4622 else if (PF_MISMATCHAW(&r
->dst
.addr
, daddr
, af
,
4624 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
4625 /* tcp/udp only. port_op always 0 in other cases */
4626 #ifndef NO_APPLE_EXTENSIONS
4627 else if (r
->proto
== pd
->proto
&&
4628 (r
->proto
== IPPROTO_TCP
|| r
->proto
== IPPROTO_UDP
) &&
4629 r
->dst
.xport
.range
.op
&&
4630 !pf_match_port(r
->dst
.xport
.range
.op
,
4631 r
->dst
.xport
.range
.port
[0], r
->dst
.xport
.range
.port
[1],
4634 else if (r
->dst
.port_op
&& !pf_match_port(r
->dst
.port_op
,
4635 r
->dst
.port
[0], r
->dst
.port
[1], th
->th_dport
))
4637 r
= r
->skip
[PF_SKIP_DST_PORT
].ptr
;
4638 /* icmp only. type always 0 in other cases */
4639 else if (r
->type
&& r
->type
!= icmptype
+ 1)
4640 r
= TAILQ_NEXT(r
, entries
);
4641 /* icmp only. type always 0 in other cases */
4642 else if (r
->code
&& r
->code
!= icmpcode
+ 1)
4643 r
= TAILQ_NEXT(r
, entries
);
4644 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
4645 r
= TAILQ_NEXT(r
, entries
);
4646 else if (r
->rule_flag
& PFRULE_FRAGMENT
)
4647 r
= TAILQ_NEXT(r
, entries
);
4648 else if (pd
->proto
== IPPROTO_TCP
&&
4649 (r
->flagset
& th
->th_flags
) != r
->flags
)
4650 r
= TAILQ_NEXT(r
, entries
);
4651 /* tcp/udp only. uid.op always 0 in other cases */
4652 else if (r
->uid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4653 pf_socket_lookup(direction
, pd
), 1)) &&
4654 !pf_match_uid(r
->uid
.op
, r
->uid
.uid
[0], r
->uid
.uid
[1],
4656 r
= TAILQ_NEXT(r
, entries
);
4657 /* tcp/udp only. gid.op always 0 in other cases */
4658 else if (r
->gid
.op
&& (pd
->lookup
.done
|| (pd
->lookup
.done
=
4659 pf_socket_lookup(direction
, pd
), 1)) &&
4660 !pf_match_gid(r
->gid
.op
, r
->gid
.gid
[0], r
->gid
.gid
[1],
4662 r
= TAILQ_NEXT(r
, entries
);
4663 else if (r
->prob
&& r
->prob
<= (random() % (UINT_MAX
- 1) + 1))
4664 r
= TAILQ_NEXT(r
, entries
);
4665 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
4666 r
= TAILQ_NEXT(r
, entries
);
4667 else if (r
->os_fingerprint
!= PF_OSFP_ANY
&&
4668 (pd
->proto
!= IPPROTO_TCP
|| !pf_osfp_match(
4669 pf_osfp_fingerprint(pd
, m
, off
, th
),
4670 r
->os_fingerprint
)))
4671 r
= TAILQ_NEXT(r
, entries
);
4675 if (PF_RTABLEID_IS_VALID(r
->rtableid
))
4676 rtableid
= r
->rtableid
;
4677 if (r
->anchor
== NULL
) {
4684 r
= TAILQ_NEXT(r
, entries
);
4686 pf_step_into_anchor(&asd
, &ruleset
,
4687 PF_RULESET_FILTER
, &r
, &a
, &match
);
4689 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
4690 PF_RULESET_FILTER
, &r
, &a
, &match
))
4697 REASON_SET(&reason
, PFRES_MATCH
);
4699 if (r
->log
|| (nr
!= NULL
&& nr
->log
)) {
4700 #ifndef NO_APPLE_EXTENSIONS
4702 if (rewrite
< off
+ hdrlen
)
4703 rewrite
= off
+ hdrlen
;
4705 m
= pf_lazy_makewritable(pd
, m
, rewrite
);
4707 REASON_SET(&reason
, PFRES_MEMORY
);
4711 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
4715 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
4717 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
->log
? r
: nr
,
4721 if ((r
->action
== PF_DROP
) &&
4722 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4723 (r
->rule_flag
& PFRULE_RETURNICMP
) ||
4724 (r
->rule_flag
& PFRULE_RETURN
))) {
4725 /* undo NAT changes, if they have taken place */
4727 if (direction
== PF_OUT
) {
4728 switch (pd
->proto
) {
4730 #ifndef NO_APPLE_EXTENSIONS
4731 pf_change_ap(direction
, pd
->mp
, saddr
,
4732 &th
->th_sport
, pd
->ip_sum
,
4733 &th
->th_sum
, &pd
->baddr
,
4734 bxport
.port
, 0, af
);
4735 sxport
.port
= th
->th_sport
;
4737 pf_change_ap(saddr
, &th
->th_sport
,
4738 pd
->ip_sum
, &th
->th_sum
,
4739 &pd
->baddr
, bport
, 0, af
);
4740 sport
= th
->th_sport
;
4745 #ifndef NO_APPLE_EXTENSIONS
4746 pf_change_ap(direction
, pd
->mp
, saddr
,
4747 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4748 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4749 bxport
.port
, 1, af
);
4750 sxport
.port
= pd
->hdr
.udp
->uh_sport
;
4753 &pd
->hdr
.udp
->uh_sport
, pd
->ip_sum
,
4754 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4756 sport
= pd
->hdr
.udp
->uh_sport
;
4762 case IPPROTO_ICMPV6
:
4766 #ifndef NO_APPLE_EXTENSIONS
4768 PF_ACPY(&pd
->baddr
, saddr
, af
);
4773 pf_change_a(&saddr
->v4
.s_addr
,
4775 pd
->baddr
.v4
.s_addr
, 0);
4780 PF_ACPY(saddr
, &pd
->baddr
,
4787 PF_ACPY(&pd
->baddr
, saddr
, af
);
4791 pf_change_a(&saddr
->v4
.s_addr
,
4793 pd
->baddr
.v4
.s_addr
, 0);
4798 PF_ACPY(saddr
, &pd
->baddr
,
4808 pf_change_a(&saddr
->v4
.s_addr
,
4810 pd
->baddr
.v4
.s_addr
, 0);
4813 PF_ACPY(saddr
, &pd
->baddr
, af
);
4818 switch (pd
->proto
) {
4820 #ifndef NO_APPLE_EXTENSIONS
4821 pf_change_ap(direction
, pd
->mp
, daddr
,
4822 &th
->th_dport
, pd
->ip_sum
,
4823 &th
->th_sum
, &pd
->baddr
,
4824 bxport
.port
, 0, af
);
4825 dxport
.port
= th
->th_dport
;
4827 pf_change_ap(daddr
, &th
->th_dport
,
4828 pd
->ip_sum
, &th
->th_sum
,
4829 &pd
->baddr
, bport
, 0, af
);
4830 dport
= th
->th_dport
;
4835 #ifndef NO_APPLE_EXTENSIONS
4836 pf_change_ap(direction
, pd
->mp
, daddr
,
4837 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4838 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4839 bxport
.port
, 1, af
);
4840 dxport
.port
= pd
->hdr
.udp
->uh_dport
;
4843 &pd
->hdr
.udp
->uh_dport
, pd
->ip_sum
,
4844 &pd
->hdr
.udp
->uh_sum
, &pd
->baddr
,
4846 dport
= pd
->hdr
.udp
->uh_dport
;
4852 case IPPROTO_ICMPV6
:
4856 #ifndef NO_APPLE_EXTENSIONS
4858 if (pd
->proto_variant
==
4859 PF_GRE_PPTP_VARIANT
)
4860 grev1
->call_id
= bxport
.call_id
;
4865 pf_change_a(&daddr
->v4
.s_addr
,
4867 pd
->baddr
.v4
.s_addr
, 0);
4872 PF_ACPY(daddr
, &pd
->baddr
,
4882 pf_change_a(&daddr
->v4
.s_addr
,
4884 pd
->baddr
.v4
.s_addr
, 0);
4889 PF_ACPY(daddr
, &pd
->baddr
,
4899 pf_change_a(&daddr
->v4
.s_addr
,
4901 pd
->baddr
.v4
.s_addr
, 0);
4905 PF_ACPY(daddr
, &pd
->baddr
, af
);
4912 if (pd
->proto
== IPPROTO_TCP
&&
4913 ((r
->rule_flag
& PFRULE_RETURNRST
) ||
4914 (r
->rule_flag
& PFRULE_RETURN
)) &&
4915 !(th
->th_flags
& TH_RST
)) {
4916 u_int32_t ack
= ntohl(th
->th_seq
) + pd
->p_len
;
4925 h4
= mtod(m
, struct ip
*);
4926 len
= ntohs(h4
->ip_len
) - off
;
4930 h6
= mtod(m
, struct ip6_hdr
*);
4931 len
= ntohs(h6
->ip6_plen
) -
4932 (off
- sizeof (*h6
));
4937 if (pf_check_proto_cksum(m
, off
, len
, IPPROTO_TCP
, af
))
4938 REASON_SET(&reason
, PFRES_PROTCKSUM
);
4940 if (th
->th_flags
& TH_SYN
)
4942 if (th
->th_flags
& TH_FIN
)
4944 pf_send_tcp(r
, af
, pd
->dst
,
4945 pd
->src
, th
->th_dport
, th
->th_sport
,
4946 ntohl(th
->th_ack
), ack
, TH_RST
|TH_ACK
, 0, 0,
4947 r
->return_ttl
, 1, 0, pd
->eh
, kif
->pfik_ifp
);
4949 } else if (pd
->proto
!= IPPROTO_ICMP
&& af
== AF_INET
&&
4950 #ifndef NO_APPLE_EXTENSIONS
4951 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4954 pf_send_icmp(m
, r
->return_icmp
>> 8,
4955 r
->return_icmp
& 255, af
, r
);
4956 else if (pd
->proto
!= IPPROTO_ICMPV6
&& af
== AF_INET6
&&
4957 #ifndef NO_APPLE_EXTENSIONS
4958 pd
->proto
!= IPPROTO_ESP
&& pd
->proto
!= IPPROTO_AH
&&
4961 pf_send_icmp(m
, r
->return_icmp6
>> 8,
4962 r
->return_icmp6
& 255, af
, r
);
4965 if (r
->action
== PF_DROP
)
4968 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, rtableid
)) {
4969 REASON_SET(&reason
, PFRES_MEMORY
);
4973 if (!state_icmp
&& (r
->keep_state
|| nr
!= NULL
||
4974 (pd
->flags
& PFDESC_TCP_NORM
))) {
4975 /* create new state */
4976 struct pf_state
*s
= NULL
;
4977 struct pf_state_key
*sk
= NULL
;
4978 struct pf_src_node
*sn
= NULL
;
4979 #ifndef NO_APPLE_EXTENSIONS
4980 struct pf_ike_hdr ike
;
4982 if (pd
->proto
== IPPROTO_UDP
) {
4983 struct udphdr
*uh
= pd
->hdr
.udp
;
4984 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
4986 if (uh
->uh_sport
== PF_IKE_PORT
&&
4987 uh
->uh_dport
== PF_IKE_PORT
&&
4988 plen
>= PF_IKE_PACKET_MINSIZE
) {
4989 if (plen
> PF_IKE_PACKET_MINSIZE
)
4990 plen
= PF_IKE_PACKET_MINSIZE
;
4991 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
4995 if (nr
!= NULL
&& pd
->proto
== IPPROTO_ESP
&&
4996 direction
== PF_OUT
) {
4997 struct pf_state_key_cmp sk0
;
4998 struct pf_state
*s0
;
5002 * This squelches state creation if the external
5003 * address matches an existing incomplete state with a
5004 * different internal address. Only one 'blocking'
5005 * partial state is allowed for each external address.
5007 memset(&sk0
, 0, sizeof (sk0
));
5009 sk0
.proto
= IPPROTO_ESP
;
5010 PF_ACPY(&sk0
.gwy
.addr
, saddr
, sk0
.af
);
5011 PF_ACPY(&sk0
.ext
.addr
, daddr
, sk0
.af
);
5012 s0
= pf_find_state(kif
, &sk0
, PF_IN
);
5014 if (s0
&& PF_ANEQ(&s0
->state_key
->lan
.addr
,
5022 /* check maximums */
5023 if (r
->max_states
&& (r
->states
>= r
->max_states
)) {
5024 pf_status
.lcounters
[LCNT_STATES
]++;
5025 REASON_SET(&reason
, PFRES_MAXSTATES
);
5028 /* src node for filter rule */
5029 if ((r
->rule_flag
& PFRULE_SRCTRACK
||
5030 r
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
5031 pf_insert_src_node(&sn
, r
, saddr
, af
) != 0) {
5032 REASON_SET(&reason
, PFRES_SRCLIMIT
);
5035 /* src node for translation rule */
5036 if (nr
!= NULL
&& (nr
->rpool
.opts
& PF_POOL_STICKYADDR
) &&
5037 ((direction
== PF_OUT
&&
5038 #ifndef NO_APPLE_EXTENSIONS
5039 nr
->action
!= PF_RDR
&&
5041 pf_insert_src_node(&nsn
, nr
, &pd
->baddr
, af
) != 0) ||
5042 (pf_insert_src_node(&nsn
, nr
, saddr
, af
) != 0))) {
5043 REASON_SET(&reason
, PFRES_SRCLIMIT
);
5046 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
5048 REASON_SET(&reason
, PFRES_MEMORY
);
5050 if (sn
!= NULL
&& sn
->states
== 0 && sn
->expire
== 0) {
5051 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, sn
);
5052 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
5053 pf_status
.src_nodes
--;
5054 pool_put(&pf_src_tree_pl
, sn
);
5056 if (nsn
!= sn
&& nsn
!= NULL
&& nsn
->states
== 0 &&
5058 RB_REMOVE(pf_src_tree
, &tree_src_tracking
, nsn
);
5059 pf_status
.scounters
[SCNT_SRC_NODE_REMOVALS
]++;
5060 pf_status
.src_nodes
--;
5061 pool_put(&pf_src_tree_pl
, nsn
);
5064 #ifndef NO_APPLE_EXTENSIONS
5066 pool_put(&pf_app_state_pl
,
5069 pool_put(&pf_state_key_pl
, sk
);
5073 bzero(s
, sizeof (*s
));
5074 #ifndef NO_APPLE_EXTENSIONS
5075 TAILQ_INIT(&s
->unlink_hooks
);
5078 s
->nat_rule
.ptr
= nr
;
5079 if (nr
&& nr
->action
== PF_RDR
&& direction
== PF_OUT
)
5081 STATE_INC_COUNTERS(s
);
5082 s
->allow_opts
= r
->allow_opts
;
5083 s
->log
= r
->log
& PF_LOG_ALL
;
5085 s
->log
|= nr
->log
& PF_LOG_ALL
;
5086 switch (pd
->proto
) {
5088 s
->src
.seqlo
= ntohl(th
->th_seq
);
5089 s
->src
.seqhi
= s
->src
.seqlo
+ pd
->p_len
+ 1;
5090 if ((th
->th_flags
& (TH_SYN
|TH_ACK
)) ==
5091 TH_SYN
&& r
->keep_state
== PF_STATE_MODULATE
) {
5092 /* Generate sequence number modulator */
5093 if ((s
->src
.seqdiff
= pf_tcp_iss(pd
) -
5096 pf_change_a(&th
->th_seq
, &th
->th_sum
,
5097 htonl(s
->src
.seqlo
+ s
->src
.seqdiff
), 0);
5098 rewrite
= off
+ sizeof (*th
);
5101 if (th
->th_flags
& TH_SYN
) {
5103 s
->src
.wscale
= pf_get_wscale(m
, off
,
5106 s
->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5107 if (s
->src
.wscale
& PF_WSCALE_MASK
) {
5108 /* Remove scale factor from initial window */
5109 int win
= s
->src
.max_win
;
5110 win
+= 1 << (s
->src
.wscale
& PF_WSCALE_MASK
);
5111 s
->src
.max_win
= (win
- 1) >>
5112 (s
->src
.wscale
& PF_WSCALE_MASK
);
5114 if (th
->th_flags
& TH_FIN
)
5118 s
->src
.state
= TCPS_SYN_SENT
;
5119 s
->dst
.state
= TCPS_CLOSED
;
5120 s
->timeout
= PFTM_TCP_FIRST_PACKET
;
5123 s
->src
.state
= PFUDPS_SINGLE
;
5124 s
->dst
.state
= PFUDPS_NO_TRAFFIC
;
5125 s
->timeout
= PFTM_UDP_FIRST_PACKET
;
5129 case IPPROTO_ICMPV6
:
5131 s
->timeout
= PFTM_ICMP_FIRST_PACKET
;
5133 #ifndef NO_APPLE_EXTENSIONS
5135 s
->src
.state
= PFGRE1S_INITIATING
;
5136 s
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5137 s
->timeout
= PFTM_GREv1_INITIATING
;
5140 s
->src
.state
= PFESPS_INITIATING
;
5141 s
->dst
.state
= PFESPS_NO_TRAFFIC
;
5142 s
->timeout
= PFTM_ESP_FIRST_PACKET
;
5146 s
->src
.state
= PFOTHERS_SINGLE
;
5147 s
->dst
.state
= PFOTHERS_NO_TRAFFIC
;
5148 s
->timeout
= PFTM_OTHER_FIRST_PACKET
;
5151 s
->creation
= pf_time_second();
5152 s
->expire
= pf_time_second();
5156 s
->src_node
->states
++;
5159 PF_ACPY(&nsn
->raddr
, &pd
->naddr
, af
);
5160 s
->nat_src_node
= nsn
;
5161 s
->nat_src_node
->states
++;
5163 if (pd
->proto
== IPPROTO_TCP
) {
5164 if ((pd
->flags
& PFDESC_TCP_NORM
) &&
5165 pf_normalize_tcp_init(m
, off
, pd
, th
, &s
->src
,
5167 REASON_SET(&reason
, PFRES_MEMORY
);
5168 pf_src_tree_remove_state(s
);
5169 STATE_DEC_COUNTERS(s
);
5170 pool_put(&pf_state_pl
, s
);
5173 if ((pd
->flags
& PFDESC_TCP_NORM
) && s
->src
.scrub
&&
5174 pf_normalize_tcp_stateful(m
, off
, pd
, &reason
,
5175 th
, s
, &s
->src
, &s
->dst
, &rewrite
)) {
5176 /* This really shouldn't happen!!! */
5177 DPFPRINTF(PF_DEBUG_URGENT
,
5178 ("pf_normalize_tcp_stateful failed on "
5180 pf_normalize_tcp_cleanup(s
);
5181 pf_src_tree_remove_state(s
);
5182 STATE_DEC_COUNTERS(s
);
5183 pool_put(&pf_state_pl
, s
);
5188 if ((sk
= pf_alloc_state_key(s
)) == NULL
) {
5189 REASON_SET(&reason
, PFRES_MEMORY
);
5193 sk
->proto
= pd
->proto
;
5194 sk
->direction
= direction
;
5196 #ifndef NO_APPLE_EXTENSIONS
5197 if (pd
->proto
== IPPROTO_UDP
) {
5198 if (pd
->hdr
.udp
->uh_sport
== PF_IKE_PORT
&&
5199 pd
->hdr
.udp
->uh_dport
== PF_IKE_PORT
) {
5200 sk
->proto_variant
= PF_EXTFILTER_APD
;
5202 sk
->proto_variant
= nr
? nr
->extfilter
:
5204 if (sk
->proto_variant
< PF_EXTFILTER_APD
)
5205 sk
->proto_variant
= PF_EXTFILTER_APD
;
5207 } else if (pd
->proto
== IPPROTO_GRE
) {
5208 sk
->proto_variant
= pd
->proto_variant
;
5211 if (direction
== PF_OUT
) {
5212 PF_ACPY(&sk
->gwy
.addr
, saddr
, af
);
5213 PF_ACPY(&sk
->ext
.addr
, daddr
, af
);
5214 switch (pd
->proto
) {
5215 #ifndef NO_APPLE_EXTENSIONS
5217 sk
->gwy
.xport
= sxport
;
5218 sk
->ext
.xport
= dxport
;
5221 sk
->gwy
.xport
.spi
= 0;
5222 sk
->ext
.xport
.spi
= pd
->hdr
.esp
->spi
;
5227 case IPPROTO_ICMPV6
:
5229 #ifndef NO_APPLE_EXTENSIONS
5230 sk
->gwy
.xport
.port
= nxport
.port
;
5231 sk
->ext
.xport
.spi
= 0;
5233 sk
->gwy
.port
= nport
;
5238 #ifndef NO_APPLE_EXTENSIONS
5239 sk
->gwy
.xport
= sxport
;
5240 sk
->ext
.xport
= dxport
;
5243 sk
->gwy
.port
= sport
;
5244 sk
->ext
.port
= dport
;
5247 #ifndef NO_APPLE_EXTENSIONS
5249 PF_ACPY(&sk
->lan
.addr
, &pd
->baddr
, af
);
5250 sk
->lan
.xport
= bxport
;
5252 PF_ACPY(&sk
->lan
.addr
, &sk
->gwy
.addr
, af
);
5253 sk
->lan
.xport
= sk
->gwy
.xport
;
5257 PF_ACPY(&sk
->lan
.addr
, &pd
->baddr
, af
);
5258 sk
->lan
.port
= bport
;
5260 PF_ACPY(&sk
->lan
.addr
, &sk
->gwy
.addr
, af
);
5261 sk
->lan
.port
= sk
->gwy
.port
;
5265 PF_ACPY(&sk
->lan
.addr
, daddr
, af
);
5266 PF_ACPY(&sk
->ext
.addr
, saddr
, af
);
5267 switch (pd
->proto
) {
5270 case IPPROTO_ICMPV6
:
5272 #ifndef NO_APPLE_EXTENSIONS
5273 sk
->lan
.xport
= nxport
;
5274 sk
->ext
.xport
.spi
= 0;
5276 sk
->lan
.port
= nport
;
5280 #ifndef NO_APPLE_EXTENSIONS
5282 sk
->ext
.xport
.spi
= 0;
5283 sk
->lan
.xport
.spi
= pd
->hdr
.esp
->spi
;
5286 sk
->lan
.xport
= dxport
;
5287 sk
->ext
.xport
= sxport
;
5291 sk
->lan
.port
= dport
;
5292 sk
->ext
.port
= sport
;
5295 #ifndef NO_APPLE_EXTENSIONS
5297 PF_ACPY(&sk
->gwy
.addr
, &pd
->baddr
, af
);
5298 sk
->gwy
.xport
= bxport
;
5300 PF_ACPY(&sk
->gwy
.addr
, &sk
->lan
.addr
, af
);
5301 sk
->gwy
.xport
= sk
->lan
.xport
;
5306 PF_ACPY(&sk
->gwy
.addr
, &pd
->baddr
, af
);
5307 sk
->gwy
.port
= bport
;
5309 PF_ACPY(&sk
->gwy
.addr
, &sk
->lan
.addr
, af
);
5310 sk
->gwy
.port
= sk
->lan
.port
;
5315 pf_set_rt_ifp(s
, saddr
); /* needs s->state_key set */
5317 #ifndef NO_APPLE_EXTENSIONS
5320 if (sk
->app_state
== 0) {
5321 switch (pd
->proto
) {
5323 u_int16_t dport
= (direction
== PF_OUT
) ?
5324 sk
->ext
.xport
.port
: sk
->gwy
.xport
.port
;
5326 if (nr
!= NULL
&& dport
== PF_PPTP_PORT
) {
5327 struct pf_app_state
*as
;
5329 as
= pool_get(&pf_app_state_pl
,
5337 bzero(as
, sizeof (*as
));
5338 as
->handler
= pf_pptp_handler
;
5339 as
->compare_lan_ext
= 0;
5340 as
->compare_ext_gwy
= 0;
5341 as
->u
.pptp
.grev1_state
= 0;
5343 (void) hook_establish(&s
->unlink_hooks
,
5344 0, (hook_fn_t
) pf_pptp_unlink
, s
);
5350 struct udphdr
*uh
= pd
->hdr
.udp
;
5352 if (nr
!= NULL
&& uh
->uh_sport
== PF_IKE_PORT
&&
5353 uh
->uh_dport
== PF_IKE_PORT
) {
5354 struct pf_app_state
*as
;
5356 as
= pool_get(&pf_app_state_pl
,
5364 bzero(as
, sizeof (*as
));
5365 as
->compare_lan_ext
= pf_ike_compare
;
5366 as
->compare_ext_gwy
= pf_ike_compare
;
5367 as
->u
.ike
.cookie
= ike
.initiator_cookie
;
5379 if (pf_insert_state(BOUND_IFACE(r
, kif
), s
)) {
5380 if (pd
->proto
== IPPROTO_TCP
)
5381 pf_normalize_tcp_cleanup(s
);
5382 REASON_SET(&reason
, PFRES_STATEINS
);
5383 pf_src_tree_remove_state(s
);
5384 STATE_DEC_COUNTERS(s
);
5385 pool_put(&pf_state_pl
, s
);
5393 if (pd
->proto
== IPPROTO_TCP
&&
5394 (th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
&&
5395 r
->keep_state
== PF_STATE_SYNPROXY
) {
5396 s
->src
.state
= PF_TCPS_PROXY_SRC
;
5398 #ifndef NO_APPLE_EXTENSIONS
5399 if (direction
== PF_OUT
) {
5400 pf_change_ap(direction
, pd
->mp
, saddr
,
5401 &th
->th_sport
, pd
->ip_sum
,
5402 &th
->th_sum
, &pd
->baddr
,
5403 bxport
.port
, 0, af
);
5404 sxport
.port
= th
->th_sport
;
5406 pf_change_ap(direction
, pd
->mp
, daddr
,
5407 &th
->th_dport
, pd
->ip_sum
,
5408 &th
->th_sum
, &pd
->baddr
,
5409 bxport
.port
, 0, af
);
5410 sxport
.port
= th
->th_dport
;
5413 if (direction
== PF_OUT
) {
5414 pf_change_ap(saddr
, &th
->th_sport
,
5415 pd
->ip_sum
, &th
->th_sum
, &pd
->baddr
,
5417 sport
= th
->th_sport
;
5419 pf_change_ap(daddr
, &th
->th_dport
,
5420 pd
->ip_sum
, &th
->th_sum
, &pd
->baddr
,
5422 sport
= th
->th_dport
;
5426 s
->src
.seqhi
= htonl(random());
5427 /* Find mss option */
5428 mss
= pf_get_mss(m
, off
, th
->th_off
, af
);
5429 mss
= pf_calc_mss(saddr
, af
, mss
);
5430 mss
= pf_calc_mss(daddr
, af
, mss
);
5432 pf_send_tcp(r
, af
, daddr
, saddr
, th
->th_dport
,
5433 th
->th_sport
, s
->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5434 TH_SYN
|TH_ACK
, 0, s
->src
.mss
, 0, 1, 0, NULL
, NULL
);
5435 REASON_SET(&reason
, PFRES_SYNPROXY
);
5436 return (PF_SYNPROXY_DROP
);
5439 #ifndef NO_APPLE_EXTENSIONS
5440 if (sk
->app_state
&& sk
->app_state
->handler
) {
5443 switch (pd
->proto
) {
5445 offx
+= th
->th_off
<< 2;
5448 offx
+= pd
->hdr
.udp
->uh_ulen
<< 2;
5451 /* ALG handlers only apply to TCP and UDP rules */
5456 sk
->app_state
->handler(s
, direction
, offx
,
5459 REASON_SET(&reason
, PFRES_MEMORY
);
5468 /* copy back packet headers if we performed NAT operations */
5469 #ifndef NO_APPLE_EXTENSIONS
5471 if (rewrite
< off
+ hdrlen
)
5472 rewrite
= off
+ hdrlen
;
5474 m
= pf_lazy_makewritable(pd
, pd
->mp
, rewrite
);
5476 REASON_SET(&reason
, PFRES_MEMORY
);
5480 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5484 m_copyback(m
, off
, hdrlen
, pd
->hdr
.any
);
5491 pf_test_fragment(struct pf_rule
**rm
, int direction
, struct pfi_kif
*kif
,
5492 struct mbuf
*m
, void *h
, struct pf_pdesc
*pd
, struct pf_rule
**am
,
5493 struct pf_ruleset
**rsm
)
5496 struct pf_rule
*r
, *a
= NULL
;
5497 struct pf_ruleset
*ruleset
= NULL
;
5498 sa_family_t af
= pd
->af
;
5504 r
= TAILQ_FIRST(pf_main_ruleset
.rules
[PF_RULESET_FILTER
].active
.ptr
);
5507 if (pfi_kif_match(r
->kif
, kif
) == r
->ifnot
)
5508 r
= r
->skip
[PF_SKIP_IFP
].ptr
;
5509 else if (r
->direction
&& r
->direction
!= direction
)
5510 r
= r
->skip
[PF_SKIP_DIR
].ptr
;
5511 else if (r
->af
&& r
->af
!= af
)
5512 r
= r
->skip
[PF_SKIP_AF
].ptr
;
5513 else if (r
->proto
&& r
->proto
!= pd
->proto
)
5514 r
= r
->skip
[PF_SKIP_PROTO
].ptr
;
5515 else if (PF_MISMATCHAW(&r
->src
.addr
, pd
->src
, af
,
5517 r
= r
->skip
[PF_SKIP_SRC_ADDR
].ptr
;
5518 else if (PF_MISMATCHAW(&r
->dst
.addr
, pd
->dst
, af
,
5520 r
= r
->skip
[PF_SKIP_DST_ADDR
].ptr
;
5521 else if (r
->tos
&& !(r
->tos
== pd
->tos
))
5522 r
= TAILQ_NEXT(r
, entries
);
5523 else if (r
->os_fingerprint
!= PF_OSFP_ANY
)
5524 r
= TAILQ_NEXT(r
, entries
);
5525 #ifndef NO_APPLE_EXTENSIONS
5526 else if (pd
->proto
== IPPROTO_UDP
&&
5527 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
))
5528 r
= TAILQ_NEXT(r
, entries
);
5529 else if (pd
->proto
== IPPROTO_TCP
&&
5530 (r
->src
.xport
.range
.op
|| r
->dst
.xport
.range
.op
||
5532 r
= TAILQ_NEXT(r
, entries
);
5534 else if (pd
->proto
== IPPROTO_UDP
&&
5535 (r
->src
.port_op
|| r
->dst
.port_op
))
5536 r
= TAILQ_NEXT(r
, entries
);
5537 else if (pd
->proto
== IPPROTO_TCP
&&
5538 (r
->src
.port_op
|| r
->dst
.port_op
|| r
->flagset
))
5539 r
= TAILQ_NEXT(r
, entries
);
5541 else if ((pd
->proto
== IPPROTO_ICMP
||
5542 pd
->proto
== IPPROTO_ICMPV6
) &&
5543 (r
->type
|| r
->code
))
5544 r
= TAILQ_NEXT(r
, entries
);
5545 else if (r
->prob
&& r
->prob
<= (random() % (UINT_MAX
- 1) + 1))
5546 r
= TAILQ_NEXT(r
, entries
);
5547 else if (r
->match_tag
&& !pf_match_tag(m
, r
, pd
->pf_mtag
, &tag
))
5548 r
= TAILQ_NEXT(r
, entries
);
5550 if (r
->anchor
== NULL
) {
5557 r
= TAILQ_NEXT(r
, entries
);
5559 pf_step_into_anchor(&asd
, &ruleset
,
5560 PF_RULESET_FILTER
, &r
, &a
, &match
);
5562 if (r
== NULL
&& pf_step_out_of_anchor(&asd
, &ruleset
,
5563 PF_RULESET_FILTER
, &r
, &a
, &match
))
5570 REASON_SET(&reason
, PFRES_MATCH
);
5573 PFLOG_PACKET(kif
, h
, m
, af
, direction
, reason
, r
, a
, ruleset
,
5576 if (r
->action
!= PF_PASS
)
5579 if (pf_tag_packet(m
, pd
->pf_mtag
, tag
, -1)) {
5580 REASON_SET(&reason
, PFRES_MEMORY
);
5587 #ifndef NO_APPLE_EXTENSIONS
5589 pf_pptp_handler(struct pf_state
*s
, int direction
, int off
,
5590 struct pf_pdesc
*pd
, struct pfi_kif
*kif
)
5592 #pragma unused(direction)
5594 struct pf_pptp_state
*as
;
5595 struct pf_pptp_ctrl_msg cm
;
5597 struct pf_state
*gs
;
5599 u_int16_t
*pac_call_id
;
5600 u_int16_t
*pns_call_id
;
5601 u_int16_t
*spoof_call_id
;
5602 u_int8_t
*pac_state
;
5603 u_int8_t
*pns_state
;
5604 enum { PF_PPTP_PASS
, PF_PPTP_INSERT_GRE
, PF_PPTP_REMOVE_GRE
} op
;
5606 struct pf_state_key
*sk
;
5607 struct pf_state_key
*gsk
;
5610 plen
= min(sizeof (cm
), m
->m_pkthdr
.len
- off
);
5611 if (plen
< PF_PPTP_CTRL_MSG_MINSIZE
)
5614 as
= &s
->state_key
->app_state
->u
.pptp
;
5615 m_copydata(m
, off
, plen
, &cm
);
5617 if (cm
.hdr
.magic
!= PF_PPTP_MAGIC_NUMBER
)
5619 if (cm
.hdr
.type
!= htons(1))
5623 gs
= as
->grev1_state
;
5625 gs
= pool_get(&pf_state_pl
, PR_WAITOK
);
5629 memcpy(gs
, s
, sizeof (*gs
));
5631 memset(&gs
->entry_id
, 0, sizeof (gs
->entry_id
));
5632 memset(&gs
->entry_list
, 0, sizeof (gs
->entry_list
));
5634 TAILQ_INIT(&gs
->unlink_hooks
);
5637 gs
->pfsync_time
= 0;
5638 gs
->packets
[0] = gs
->packets
[1] = 0;
5639 gs
->bytes
[0] = gs
->bytes
[1] = 0;
5640 gs
->timeout
= PFTM_UNLINKED
;
5641 gs
->id
= gs
->creatorid
= 0;
5642 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5643 gs
->src
.scrub
= gs
->dst
.scrub
= 0;
5645 gsk
= pf_alloc_state_key(gs
);
5647 pool_put(&pf_state_pl
, gs
);
5651 memcpy(&gsk
->lan
, &sk
->lan
, sizeof (gsk
->lan
));
5652 memcpy(&gsk
->gwy
, &sk
->gwy
, sizeof (gsk
->gwy
));
5653 memcpy(&gsk
->ext
, &sk
->ext
, sizeof (gsk
->ext
));
5655 gsk
->proto
= IPPROTO_GRE
;
5656 gsk
->proto_variant
= PF_GRE_PPTP_VARIANT
;
5658 gsk
->lan
.xport
.call_id
= 0;
5659 gsk
->gwy
.xport
.call_id
= 0;
5660 gsk
->ext
.xport
.call_id
= 0;
5662 as
->grev1_state
= gs
;
5664 gsk
= gs
->state_key
;
5667 switch (sk
->direction
) {
5669 pns_call_id
= &gsk
->ext
.xport
.call_id
;
5670 pns_state
= &gs
->dst
.state
;
5671 pac_call_id
= &gsk
->lan
.xport
.call_id
;
5672 pac_state
= &gs
->src
.state
;
5676 pns_call_id
= &gsk
->lan
.xport
.call_id
;
5677 pns_state
= &gs
->src
.state
;
5678 pac_call_id
= &gsk
->ext
.xport
.call_id
;
5679 pac_state
= &gs
->dst
.state
;
5683 DPFPRINTF(PF_DEBUG_URGENT
,
5684 ("pf_pptp_handler: bad directional!\n"));
5691 ct
= ntohs(cm
.ctrl
.type
);
5694 case PF_PPTP_CTRL_TYPE_CALL_OUT_REQ
:
5695 *pns_call_id
= cm
.msg
.call_out_req
.call_id
;
5696 *pns_state
= PFGRE1S_INITIATING
;
5697 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5698 spoof_call_id
= &cm
.msg
.call_out_req
.call_id
;
5701 case PF_PPTP_CTRL_TYPE_CALL_OUT_RPY
:
5702 *pac_call_id
= cm
.msg
.call_out_rpy
.call_id
;
5703 if (s
->nat_rule
.ptr
)
5705 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5706 &cm
.msg
.call_out_rpy
.call_id
:
5707 &cm
.msg
.call_out_rpy
.peer_call_id
;
5708 if (gs
->timeout
== PFTM_UNLINKED
) {
5709 *pac_state
= PFGRE1S_INITIATING
;
5710 op
= PF_PPTP_INSERT_GRE
;
5714 case PF_PPTP_CTRL_TYPE_CALL_IN_1ST
:
5715 *pns_call_id
= cm
.msg
.call_in_1st
.call_id
;
5716 *pns_state
= PFGRE1S_INITIATING
;
5717 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5718 spoof_call_id
= &cm
.msg
.call_in_1st
.call_id
;
5721 case PF_PPTP_CTRL_TYPE_CALL_IN_2ND
:
5722 *pac_call_id
= cm
.msg
.call_in_2nd
.call_id
;
5723 *pac_state
= PFGRE1S_INITIATING
;
5724 if (s
->nat_rule
.ptr
)
5726 (pac_call_id
== &gsk
->lan
.xport
.call_id
) ?
5727 &cm
.msg
.call_in_2nd
.call_id
:
5728 &cm
.msg
.call_in_2nd
.peer_call_id
;
5731 case PF_PPTP_CTRL_TYPE_CALL_IN_3RD
:
5732 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5733 spoof_call_id
= &cm
.msg
.call_in_3rd
.call_id
;
5734 if (cm
.msg
.call_in_3rd
.call_id
!= *pns_call_id
) {
5737 if (gs
->timeout
== PFTM_UNLINKED
)
5738 op
= PF_PPTP_INSERT_GRE
;
5741 case PF_PPTP_CTRL_TYPE_CALL_CLR
:
5742 if (cm
.msg
.call_clr
.call_id
!= *pns_call_id
)
5743 op
= PF_PPTP_REMOVE_GRE
;
5746 case PF_PPTP_CTRL_TYPE_CALL_DISC
:
5747 if (cm
.msg
.call_clr
.call_id
!= *pac_call_id
)
5748 op
= PF_PPTP_REMOVE_GRE
;
5751 case PF_PPTP_CTRL_TYPE_ERROR
:
5752 if (s
->nat_rule
.ptr
&& pns_call_id
== &gsk
->lan
.xport
.call_id
)
5753 spoof_call_id
= &cm
.msg
.error
.peer_call_id
;
5756 case PF_PPTP_CTRL_TYPE_SET_LINKINFO
:
5757 if (s
->nat_rule
.ptr
&& pac_call_id
== &gsk
->lan
.xport
.call_id
)
5758 spoof_call_id
= &cm
.msg
.set_linkinfo
.peer_call_id
;
5766 if (!gsk
->gwy
.xport
.call_id
&& gsk
->lan
.xport
.call_id
) {
5767 gsk
->gwy
.xport
.call_id
= gsk
->lan
.xport
.call_id
;
5768 if (spoof_call_id
) {
5769 u_int16_t call_id
= 0;
5771 struct pf_state_key_cmp key
;
5774 key
.proto
= IPPROTO_GRE
;
5775 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
5776 PF_ACPY(&key
.gwy
.addr
, &gsk
->gwy
.addr
, key
.af
);
5777 PF_ACPY(&key
.ext
.addr
, &gsk
->ext
.addr
, key
.af
);
5778 key
.gwy
.xport
.call_id
= gsk
->gwy
.xport
.call_id
;
5779 key
.ext
.xport
.call_id
= gsk
->ext
.xport
.call_id
;
5781 call_id
= htonl(random());
5784 while (pf_find_state_all(&key
, PF_IN
, 0)) {
5785 call_id
= ntohs(call_id
);
5787 if (--call_id
== 0) call_id
= 0xffff;
5788 call_id
= htons(call_id
);
5790 key
.gwy
.xport
.call_id
= call_id
;
5793 DPFPRINTF(PF_DEBUG_URGENT
,
5794 ("pf_pptp_handler: failed to spoof "
5796 key
.gwy
.xport
.call_id
= 0;
5801 gsk
->gwy
.xport
.call_id
= call_id
;
5807 if (spoof_call_id
&& gsk
->lan
.xport
.call_id
!= gsk
->gwy
.xport
.call_id
) {
5808 if (*spoof_call_id
== gsk
->gwy
.xport
.call_id
) {
5809 *spoof_call_id
= gsk
->lan
.xport
.call_id
;
5810 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5811 gsk
->gwy
.xport
.call_id
, gsk
->lan
.xport
.call_id
, 0);
5813 *spoof_call_id
= gsk
->gwy
.xport
.call_id
;
5814 th
->th_sum
= pf_cksum_fixup(th
->th_sum
,
5815 gsk
->lan
.xport
.call_id
, gsk
->gwy
.xport
.call_id
, 0);
5818 m
= pf_lazy_makewritable(pd
, m
, off
+ plen
);
5821 m_copyback(m
, off
, plen
, &cm
);
5825 case PF_PPTP_REMOVE_GRE
:
5826 gs
->timeout
= PFTM_PURGE
;
5827 gs
->src
.state
= gs
->dst
.state
= PFGRE1S_NO_TRAFFIC
;
5828 gsk
->lan
.xport
.call_id
= 0;
5829 gsk
->gwy
.xport
.call_id
= 0;
5830 gsk
->ext
.xport
.call_id
= 0;
5831 gs
->id
= gs
->creatorid
= 0;
5834 case PF_PPTP_INSERT_GRE
:
5835 gs
->creation
= pf_time_second();
5836 gs
->expire
= pf_time_second();
5837 gs
->timeout
= PFTM_GREv1_FIRST_PACKET
;
5838 if (gs
->src_node
) ++gs
->src_node
->states
;
5839 if (gs
->nat_src_node
) ++gs
->nat_src_node
->states
;
5840 pf_set_rt_ifp(gs
, &sk
->lan
.addr
);
5841 if (pf_insert_state(BOUND_IFACE(s
->rule
.ptr
, kif
), gs
)) {
5845 * FIX ME: insertion can fail when multiple PNS
5846 * behind the same NAT open calls to the same PAC
5847 * simultaneously because spoofed call ID numbers
5848 * are chosen before states are inserted. This is
5849 * hard to fix and happens infrequently enough that
5850 * users will normally try again and this ALG will
5851 * succeed. Failures are expected to be rare enough
5852 * that fixing this is a low priority.
5855 pf_src_tree_remove_state(gs
);
5856 STATE_DEC_COUNTERS(gs
);
5857 pool_put(&pf_state_pl
, gs
);
5858 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_pptp_handler: error "
5859 "inserting GREv1 state.\n"));
5869 pf_pptp_unlink(struct pf_state
*s
)
5871 struct pf_app_state
*as
= s
->state_key
->app_state
;
5872 struct pf_state
*gs
= as
->u
.pptp
.grev1_state
;
5875 if (gs
->timeout
< PFTM_MAX
)
5876 gs
->timeout
= PFTM_PURGE
;
5877 as
->u
.pptp
.grev1_state
= 0;
5882 pf_ike_compare(struct pf_app_state
*a
, struct pf_app_state
*b
)
5884 int64_t d
= a
->u
.ike
.cookie
- b
->u
.ike
.cookie
;
5885 return ((d
> 0) ? 1 : ((d
< 0) ? -1 : 0));
5890 pf_test_state_tcp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
5891 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
,
5895 struct pf_state_key_cmp key
;
5896 struct tcphdr
*th
= pd
->hdr
.tcp
;
5897 u_int16_t win
= ntohs(th
->th_win
);
5898 u_int32_t ack
, end
, seq
, orig_seq
;
5902 struct pf_state_peer
*src
, *dst
;
5904 #ifndef NO_APPLE_EXTENSIONS
5908 key
.proto
= IPPROTO_TCP
;
5909 if (direction
== PF_IN
) {
5910 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
5911 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
5912 #ifndef NO_APPLE_EXTENSIONS
5913 key
.ext
.xport
.port
= th
->th_sport
;
5914 key
.gwy
.xport
.port
= th
->th_dport
;
5916 key
.ext
.port
= th
->th_sport
;
5917 key
.gwy
.port
= th
->th_dport
;
5920 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
5921 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
5922 #ifndef NO_APPLE_EXTENSIONS
5923 key
.lan
.xport
.port
= th
->th_sport
;
5924 key
.ext
.xport
.port
= th
->th_dport
;
5926 key
.lan
.port
= th
->th_sport
;
5927 key
.ext
.port
= th
->th_dport
;
5933 if (direction
== (*state
)->state_key
->direction
) {
5934 src
= &(*state
)->src
;
5935 dst
= &(*state
)->dst
;
5937 src
= &(*state
)->dst
;
5938 dst
= &(*state
)->src
;
5941 if ((*state
)->src
.state
== PF_TCPS_PROXY_SRC
) {
5942 if (direction
!= (*state
)->state_key
->direction
) {
5943 REASON_SET(reason
, PFRES_SYNPROXY
);
5944 return (PF_SYNPROXY_DROP
);
5946 if (th
->th_flags
& TH_SYN
) {
5947 if (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
) {
5948 REASON_SET(reason
, PFRES_SYNPROXY
);
5951 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
5952 pd
->src
, th
->th_dport
, th
->th_sport
,
5953 (*state
)->src
.seqhi
, ntohl(th
->th_seq
) + 1,
5954 TH_SYN
|TH_ACK
, 0, (*state
)->src
.mss
, 0, 1,
5956 REASON_SET(reason
, PFRES_SYNPROXY
);
5957 return (PF_SYNPROXY_DROP
);
5958 } else if (!(th
->th_flags
& TH_ACK
) ||
5959 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5960 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5961 REASON_SET(reason
, PFRES_SYNPROXY
);
5963 } else if ((*state
)->src_node
!= NULL
&&
5964 pf_src_connlimit(state
)) {
5965 REASON_SET(reason
, PFRES_SRCLIMIT
);
5968 (*state
)->src
.state
= PF_TCPS_PROXY_DST
;
5970 if ((*state
)->src
.state
== PF_TCPS_PROXY_DST
) {
5971 struct pf_state_host
*psrc
, *pdst
;
5973 if (direction
== PF_OUT
) {
5974 psrc
= &(*state
)->state_key
->gwy
;
5975 pdst
= &(*state
)->state_key
->ext
;
5977 psrc
= &(*state
)->state_key
->ext
;
5978 pdst
= &(*state
)->state_key
->lan
;
5980 if (direction
== (*state
)->state_key
->direction
) {
5981 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_ACK
) ||
5982 (ntohl(th
->th_ack
) != (*state
)->src
.seqhi
+ 1) ||
5983 (ntohl(th
->th_seq
) != (*state
)->src
.seqlo
+ 1)) {
5984 REASON_SET(reason
, PFRES_SYNPROXY
);
5987 (*state
)->src
.max_win
= MAX(ntohs(th
->th_win
), 1);
5988 if ((*state
)->dst
.seqhi
== 1)
5989 (*state
)->dst
.seqhi
= htonl(random());
5990 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
5991 #ifndef NO_APPLE_EXTENSIONS
5992 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
5994 &pdst
->addr
, psrc
->port
, pdst
->port
,
5996 (*state
)->dst
.seqhi
, 0, TH_SYN
, 0,
5997 (*state
)->src
.mss
, 0, 0, (*state
)->tag
, NULL
, NULL
);
5998 REASON_SET(reason
, PFRES_SYNPROXY
);
5999 return (PF_SYNPROXY_DROP
);
6000 } else if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) !=
6002 (ntohl(th
->th_ack
) != (*state
)->dst
.seqhi
+ 1)) {
6003 REASON_SET(reason
, PFRES_SYNPROXY
);
6006 (*state
)->dst
.max_win
= MAX(ntohs(th
->th_win
), 1);
6007 (*state
)->dst
.seqlo
= ntohl(th
->th_seq
);
6008 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, pd
->dst
,
6009 pd
->src
, th
->th_dport
, th
->th_sport
,
6010 ntohl(th
->th_ack
), ntohl(th
->th_seq
) + 1,
6011 TH_ACK
, (*state
)->src
.max_win
, 0, 0, 0,
6012 (*state
)->tag
, NULL
, NULL
);
6013 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
, &psrc
->addr
,
6014 #ifndef NO_APPLE_EXTENSIONS
6015 &pdst
->addr
, psrc
->xport
.port
, pdst
->xport
.port
,
6017 &pdst
->addr
, psrc
->port
, pdst
->port
,
6019 (*state
)->src
.seqhi
+ 1, (*state
)->src
.seqlo
+ 1,
6020 TH_ACK
, (*state
)->dst
.max_win
, 0, 0, 1,
6022 (*state
)->src
.seqdiff
= (*state
)->dst
.seqhi
-
6023 (*state
)->src
.seqlo
;
6024 (*state
)->dst
.seqdiff
= (*state
)->src
.seqhi
-
6025 (*state
)->dst
.seqlo
;
6026 (*state
)->src
.seqhi
= (*state
)->src
.seqlo
+
6027 (*state
)->dst
.max_win
;
6028 (*state
)->dst
.seqhi
= (*state
)->dst
.seqlo
+
6029 (*state
)->src
.max_win
;
6030 (*state
)->src
.wscale
= (*state
)->dst
.wscale
= 0;
6031 (*state
)->src
.state
= (*state
)->dst
.state
=
6033 REASON_SET(reason
, PFRES_SYNPROXY
);
6034 return (PF_SYNPROXY_DROP
);
6038 if (((th
->th_flags
& (TH_SYN
|TH_ACK
)) == TH_SYN
) &&
6039 dst
->state
>= TCPS_FIN_WAIT_2
&&
6040 src
->state
>= TCPS_FIN_WAIT_2
) {
6041 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6042 printf("pf: state reuse ");
6043 pf_print_state(*state
);
6044 pf_print_flags(th
->th_flags
);
6047 /* XXX make sure it's the same direction ?? */
6048 (*state
)->src
.state
= (*state
)->dst
.state
= TCPS_CLOSED
;
6049 pf_unlink_state(*state
);
6054 if (src
->wscale
&& dst
->wscale
&& !(th
->th_flags
& TH_SYN
)) {
6055 sws
= src
->wscale
& PF_WSCALE_MASK
;
6056 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6061 * Sequence tracking algorithm from Guido van Rooij's paper:
6062 * http://www.madison-gurkha.com/publications/tcp_filtering/
6066 orig_seq
= seq
= ntohl(th
->th_seq
);
6067 if (src
->seqlo
== 0) {
6068 /* First packet from this end. Set its state */
6070 if ((pd
->flags
& PFDESC_TCP_NORM
|| dst
->scrub
) &&
6071 src
->scrub
== NULL
) {
6072 if (pf_normalize_tcp_init(m
, off
, pd
, th
, src
, dst
)) {
6073 REASON_SET(reason
, PFRES_MEMORY
);
6078 /* Deferred generation of sequence number modulator */
6079 if (dst
->seqdiff
&& !src
->seqdiff
) {
6080 /* use random iss for the TCP server */
6081 while ((src
->seqdiff
= random() - seq
) == 0)
6083 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6084 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6086 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6087 copyback
= off
+ sizeof (*th
);
6089 ack
= ntohl(th
->th_ack
);
6092 end
= seq
+ pd
->p_len
;
6093 if (th
->th_flags
& TH_SYN
) {
6095 if (dst
->wscale
& PF_WSCALE_FLAG
) {
6096 src
->wscale
= pf_get_wscale(m
, off
, th
->th_off
,
6098 if (src
->wscale
& PF_WSCALE_FLAG
) {
6100 * Remove scale factor from initial
6103 sws
= src
->wscale
& PF_WSCALE_MASK
;
6104 win
= ((u_int32_t
)win
+ (1 << sws
) - 1)
6106 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6108 /* fixup other window */
6109 dst
->max_win
<<= dst
->wscale
&
6111 /* in case of a retrans SYN|ACK */
6116 if (th
->th_flags
& TH_FIN
)
6120 if (src
->state
< TCPS_SYN_SENT
)
6121 src
->state
= TCPS_SYN_SENT
;
6124 * May need to slide the window (seqhi may have been set by
6125 * the crappy stack check or if we picked up the connection
6126 * after establishment)
6128 if (src
->seqhi
== 1 ||
6129 SEQ_GEQ(end
+ MAX(1, dst
->max_win
<< dws
), src
->seqhi
))
6130 src
->seqhi
= end
+ MAX(1, dst
->max_win
<< dws
);
6131 if (win
> src
->max_win
)
6135 ack
= ntohl(th
->th_ack
) - dst
->seqdiff
;
6137 /* Modulate sequence numbers */
6138 pf_change_a(&th
->th_seq
, &th
->th_sum
, htonl(seq
+
6140 pf_change_a(&th
->th_ack
, &th
->th_sum
, htonl(ack
), 0);
6141 copyback
= off
+ sizeof (*th
);
6143 end
= seq
+ pd
->p_len
;
6144 if (th
->th_flags
& TH_SYN
)
6146 if (th
->th_flags
& TH_FIN
)
6150 if ((th
->th_flags
& TH_ACK
) == 0) {
6151 /* Let it pass through the ack skew check */
6153 } else if ((ack
== 0 &&
6154 (th
->th_flags
& (TH_ACK
|TH_RST
)) == (TH_ACK
|TH_RST
)) ||
6155 /* broken tcp stacks do not set ack */
6156 (dst
->state
< TCPS_SYN_SENT
)) {
6158 * Many stacks (ours included) will set the ACK number in an
6159 * FIN|ACK if the SYN times out -- no sequence to ACK.
6165 /* Ease sequencing restrictions on no data packets */
6170 ackskew
= dst
->seqlo
- ack
;
6174 * Need to demodulate the sequence numbers in any TCP SACK options
6175 * (Selective ACK). We could optionally validate the SACK values
6176 * against the current ACK window, either forwards or backwards, but
6177 * I'm not confident that SACK has been implemented properly
6178 * everywhere. It wouldn't surprise me if several stacks accidently
6179 * SACK too far backwards of previously ACKed data. There really aren't
6180 * any security implications of bad SACKing unless the target stack
6181 * doesn't validate the option length correctly. Someone trying to
6182 * spoof into a TCP connection won't bother blindly sending SACK
6185 if (dst
->seqdiff
&& (th
->th_off
<< 2) > (int)sizeof (struct tcphdr
)) {
6186 #ifndef NO_APPLE_EXTENSIONS
6187 copyback
= pf_modulate_sack(m
, off
, pd
, th
, dst
);
6188 if (copyback
== -1) {
6189 REASON_SET(reason
, PFRES_MEMORY
);
6195 if (pf_modulate_sack(m
, off
, pd
, th
, dst
))
6201 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */
6202 if (SEQ_GEQ(src
->seqhi
, end
) &&
6203 /* Last octet inside other's window space */
6204 SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
)) &&
6205 /* Retrans: not more than one window back */
6206 (ackskew
>= -MAXACKWINDOW
) &&
6207 /* Acking not more than one reassembled fragment backwards */
6208 (ackskew
<= (MAXACKWINDOW
<< sws
)) &&
6209 /* Acking not more than one window forward */
6210 ((th
->th_flags
& TH_RST
) == 0 || orig_seq
== src
->seqlo
||
6211 (orig_seq
== src
->seqlo
+ 1) || (orig_seq
+ 1 == src
->seqlo
) ||
6212 (pd
->flags
& PFDESC_IP_REAS
) == 0)) {
6213 /* Require an exact/+1 sequence match on resets when possible */
6215 if (dst
->scrub
|| src
->scrub
) {
6216 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6217 *state
, src
, dst
, ©back
))
6220 #ifndef NO_APPLE_EXTENSIONS
6225 /* update max window */
6226 if (src
->max_win
< win
)
6228 /* synchronize sequencing */
6229 if (SEQ_GT(end
, src
->seqlo
))
6231 /* slide the window of what the other end can send */
6232 if (SEQ_GEQ(ack
+ (win
<< sws
), dst
->seqhi
))
6233 dst
->seqhi
= ack
+ MAX((win
<< sws
), 1);
6237 if (th
->th_flags
& TH_SYN
)
6238 if (src
->state
< TCPS_SYN_SENT
)
6239 src
->state
= TCPS_SYN_SENT
;
6240 if (th
->th_flags
& TH_FIN
)
6241 if (src
->state
< TCPS_CLOSING
)
6242 src
->state
= TCPS_CLOSING
;
6243 if (th
->th_flags
& TH_ACK
) {
6244 if (dst
->state
== TCPS_SYN_SENT
) {
6245 dst
->state
= TCPS_ESTABLISHED
;
6246 if (src
->state
== TCPS_ESTABLISHED
&&
6247 (*state
)->src_node
!= NULL
&&
6248 pf_src_connlimit(state
)) {
6249 REASON_SET(reason
, PFRES_SRCLIMIT
);
6252 } else if (dst
->state
== TCPS_CLOSING
)
6253 dst
->state
= TCPS_FIN_WAIT_2
;
6255 if (th
->th_flags
& TH_RST
)
6256 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6258 /* update expire time */
6259 (*state
)->expire
= pf_time_second();
6260 if (src
->state
>= TCPS_FIN_WAIT_2
&&
6261 dst
->state
>= TCPS_FIN_WAIT_2
)
6262 (*state
)->timeout
= PFTM_TCP_CLOSED
;
6263 else if (src
->state
>= TCPS_CLOSING
&&
6264 dst
->state
>= TCPS_CLOSING
)
6265 (*state
)->timeout
= PFTM_TCP_FIN_WAIT
;
6266 else if (src
->state
< TCPS_ESTABLISHED
||
6267 dst
->state
< TCPS_ESTABLISHED
)
6268 (*state
)->timeout
= PFTM_TCP_OPENING
;
6269 else if (src
->state
>= TCPS_CLOSING
||
6270 dst
->state
>= TCPS_CLOSING
)
6271 (*state
)->timeout
= PFTM_TCP_CLOSING
;
6273 (*state
)->timeout
= PFTM_TCP_ESTABLISHED
;
6275 /* Fall through to PASS packet */
6277 } else if ((dst
->state
< TCPS_SYN_SENT
||
6278 dst
->state
>= TCPS_FIN_WAIT_2
|| src
->state
>= TCPS_FIN_WAIT_2
) &&
6279 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) &&
6280 /* Within a window forward of the originating packet */
6281 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
)) {
6282 /* Within a window backward of the originating packet */
6285 * This currently handles three situations:
6286 * 1) Stupid stacks will shotgun SYNs before their peer
6288 * 2) When PF catches an already established stream (the
6289 * firewall rebooted, the state table was flushed, routes
6291 * 3) Packets get funky immediately after the connection
6292 * closes (this should catch Solaris spurious ACK|FINs
6293 * that web servers like to spew after a close)
6295 * This must be a little more careful than the above code
6296 * since packet floods will also be caught here. We don't
6297 * update the TTL here to mitigate the damage of a packet
6298 * flood and so the same code can handle awkward establishment
6299 * and a loosened connection close.
6300 * In the establishment case, a correct peer response will
6301 * validate the connection, go through the normal state code
6302 * and keep updating the state TTL.
6305 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6306 printf("pf: loose state match: ");
6307 pf_print_state(*state
);
6308 pf_print_flags(th
->th_flags
);
6309 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6310 "pkts=%llu:%llu dir=%s,%s\n", seq
, orig_seq
, ack
,
6311 pd
->p_len
, ackskew
, (*state
)->packets
[0],
6312 (*state
)->packets
[1],
6313 direction
== PF_IN
? "in" : "out",
6314 direction
== (*state
)->state_key
->direction
?
6318 if (dst
->scrub
|| src
->scrub
) {
6319 if (pf_normalize_tcp_stateful(m
, off
, pd
, reason
, th
,
6320 *state
, src
, dst
, ©back
))
6322 #ifndef NO_APPLE_EXTENSIONS
6327 /* update max window */
6328 if (src
->max_win
< win
)
6330 /* synchronize sequencing */
6331 if (SEQ_GT(end
, src
->seqlo
))
6333 /* slide the window of what the other end can send */
6334 if (SEQ_GEQ(ack
+ (win
<< sws
), dst
->seqhi
))
6335 dst
->seqhi
= ack
+ MAX((win
<< sws
), 1);
6338 * Cannot set dst->seqhi here since this could be a shotgunned
6339 * SYN and not an already established connection.
6342 if (th
->th_flags
& TH_FIN
)
6343 if (src
->state
< TCPS_CLOSING
)
6344 src
->state
= TCPS_CLOSING
;
6345 if (th
->th_flags
& TH_RST
)
6346 src
->state
= dst
->state
= TCPS_TIME_WAIT
;
6348 /* Fall through to PASS packet */
6351 if ((*state
)->dst
.state
== TCPS_SYN_SENT
&&
6352 (*state
)->src
.state
== TCPS_SYN_SENT
) {
6353 /* Send RST for state mismatches during handshake */
6354 if (!(th
->th_flags
& TH_RST
))
6355 pf_send_tcp((*state
)->rule
.ptr
, pd
->af
,
6356 pd
->dst
, pd
->src
, th
->th_dport
,
6357 th
->th_sport
, ntohl(th
->th_ack
), 0,
6359 (*state
)->rule
.ptr
->return_ttl
, 1, 0,
6360 pd
->eh
, kif
->pfik_ifp
);
6364 } else if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6365 printf("pf: BAD state: ");
6366 pf_print_state(*state
);
6367 pf_print_flags(th
->th_flags
);
6368 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d "
6369 "pkts=%llu:%llu dir=%s,%s\n",
6370 seq
, orig_seq
, ack
, pd
->p_len
, ackskew
,
6371 (*state
)->packets
[0], (*state
)->packets
[1],
6372 direction
== PF_IN
? "in" : "out",
6373 direction
== (*state
)->state_key
->direction
?
6375 printf("pf: State failure on: %c %c %c %c | %c %c\n",
6376 SEQ_GEQ(src
->seqhi
, end
) ? ' ' : '1',
6377 SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
)) ?
6379 (ackskew
>= -MAXACKWINDOW
) ? ' ' : '3',
6380 (ackskew
<= (MAXACKWINDOW
<< sws
)) ? ' ' : '4',
6381 SEQ_GEQ(src
->seqhi
+ MAXACKWINDOW
, end
) ?' ' :'5',
6382 SEQ_GEQ(seq
, src
->seqlo
- MAXACKWINDOW
) ?' ' :'6');
6384 REASON_SET(reason
, PFRES_BADSTATE
);
6388 /* Any packets which have gotten here are to be passed */
6390 #ifndef NO_APPLE_EXTENSIONS
6391 if ((*state
)->state_key
->app_state
&&
6392 (*state
)->state_key
->app_state
->handler
) {
6393 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6394 off
+ (th
->th_off
<< 2), pd
, kif
);
6396 REASON_SET(reason
, PFRES_MEMORY
);
6402 /* translate source/destination address, if necessary */
6403 if (STATE_TRANSLATE((*state
)->state_key
)) {
6404 if (direction
== PF_OUT
)
6405 pf_change_ap(direction
, pd
->mp
, pd
->src
, &th
->th_sport
,
6406 pd
->ip_sum
, &th
->th_sum
,
6407 &(*state
)->state_key
->gwy
.addr
,
6408 (*state
)->state_key
->gwy
.xport
.port
, 0, pd
->af
);
6410 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &th
->th_dport
,
6411 pd
->ip_sum
, &th
->th_sum
,
6412 &(*state
)->state_key
->lan
.addr
,
6413 (*state
)->state_key
->lan
.xport
.port
, 0, pd
->af
);
6414 copyback
= off
+ sizeof (*th
);
6418 m
= pf_lazy_makewritable(pd
, m
, copyback
);
6420 REASON_SET(reason
, PFRES_MEMORY
);
6424 /* Copyback sequence modulation or stateful scrub changes */
6425 m_copyback(m
, off
, sizeof (*th
), th
);
6428 /* translate source/destination address, if necessary */
6429 if (STATE_TRANSLATE((*state
)->state_key
)) {
6430 if (direction
== PF_OUT
)
6431 pf_change_ap(pd
->src
, pd
->mp
, &th
->th_sport
, pd
->ip_sum
,
6432 &th
->th_sum
, &(*state
)->state_key
->gwy
.addr
,
6433 (*state
)->state_key
->gwy
.port
, 0, pd
->af
);
6435 pf_change_ap(pd
->dst
, pd
->mp
, &th
->th_dport
, pd
->ip_sum
,
6436 &th
->th_sum
, &(*state
)->state_key
->lan
.addr
,
6437 (*state
)->state_key
->lan
.port
, 0, pd
->af
);
6438 m_copyback(m
, off
, sizeof (*th
), th
);
6439 } else if (copyback
) {
6440 /* Copyback sequence modulation or stateful scrub changes */
6441 m_copyback(m
, off
, sizeof (*th
), th
);
6449 pf_test_state_udp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6450 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
)
6453 struct pf_state_peer
*src
, *dst
;
6454 struct pf_state_key_cmp key
;
6455 struct udphdr
*uh
= pd
->hdr
.udp
;
6456 #ifndef NO_APPLE_EXTENSIONS
6457 struct pf_app_state as
;
6458 int dx
, action
, extfilter
;
6460 key
.proto_variant
= PF_EXTFILTER_APD
;
6464 key
.proto
= IPPROTO_UDP
;
6465 if (direction
== PF_IN
) {
6466 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6467 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6468 #ifndef NO_APPLE_EXTENSIONS
6469 key
.ext
.xport
.port
= uh
->uh_sport
;
6470 key
.gwy
.xport
.port
= uh
->uh_dport
;
6473 key
.ext
.port
= uh
->uh_sport
;
6474 key
.gwy
.port
= uh
->uh_dport
;
6477 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6478 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6479 #ifndef NO_APPLE_EXTENSIONS
6480 key
.lan
.xport
.port
= uh
->uh_sport
;
6481 key
.ext
.xport
.port
= uh
->uh_dport
;
6484 key
.lan
.port
= uh
->uh_sport
;
6485 key
.ext
.port
= uh
->uh_dport
;
6489 #ifndef NO_APPLE_EXTENSIONS
6490 if (uh
->uh_sport
== PF_IKE_PORT
&& uh
->uh_dport
== PF_IKE_PORT
) {
6491 struct pf_ike_hdr ike
;
6492 size_t plen
= m
->m_pkthdr
.len
- off
- sizeof (*uh
);
6493 if (plen
< PF_IKE_PACKET_MINSIZE
) {
6494 DPFPRINTF(PF_DEBUG_MISC
,
6495 ("pf: IKE message too small.\n"));
6499 if (plen
> sizeof (ike
))
6500 plen
= sizeof (ike
);
6501 m_copydata(m
, off
+ sizeof (*uh
), plen
, &ike
);
6503 if (ike
.initiator_cookie
) {
6504 key
.app_state
= &as
;
6505 as
.compare_lan_ext
= pf_ike_compare
;
6506 as
.compare_ext_gwy
= pf_ike_compare
;
6507 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
6510 * <http://tools.ietf.org/html/\
6511 * draft-ietf-ipsec-nat-t-ike-01>
6512 * Support non-standard NAT-T implementations that
6513 * push the ESP packet over the top of the IKE packet.
6514 * Do not drop packet.
6516 DPFPRINTF(PF_DEBUG_MISC
,
6517 ("pf: IKE initiator cookie = 0.\n"));
6521 *state
= pf_find_state(kif
, &key
, dx
);
6523 if (!key
.app_state
&& *state
== 0) {
6524 key
.proto_variant
= PF_EXTFILTER_AD
;
6525 *state
= pf_find_state(kif
, &key
, dx
);
6528 if (!key
.app_state
&& *state
== 0) {
6529 key
.proto_variant
= PF_EXTFILTER_EI
;
6530 *state
= pf_find_state(kif
, &key
, dx
);
6533 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
6539 if (direction
== (*state
)->state_key
->direction
) {
6540 src
= &(*state
)->src
;
6541 dst
= &(*state
)->dst
;
6543 src
= &(*state
)->dst
;
6544 dst
= &(*state
)->src
;
6548 if (src
->state
< PFUDPS_SINGLE
)
6549 src
->state
= PFUDPS_SINGLE
;
6550 if (dst
->state
== PFUDPS_SINGLE
)
6551 dst
->state
= PFUDPS_MULTIPLE
;
6553 /* update expire time */
6554 (*state
)->expire
= pf_time_second();
6555 if (src
->state
== PFUDPS_MULTIPLE
&& dst
->state
== PFUDPS_MULTIPLE
)
6556 (*state
)->timeout
= PFTM_UDP_MULTIPLE
;
6558 (*state
)->timeout
= PFTM_UDP_SINGLE
;
6560 #ifndef NO_APPLE_EXTENSIONS
6561 extfilter
= (*state
)->state_key
->proto_variant
;
6562 if (extfilter
> PF_EXTFILTER_APD
) {
6563 (*state
)->state_key
->ext
.xport
.port
= key
.ext
.xport
.port
;
6564 if (extfilter
> PF_EXTFILTER_AD
)
6565 PF_ACPY(&(*state
)->state_key
->ext
.addr
,
6566 &key
.ext
.addr
, key
.af
);
6569 if ((*state
)->state_key
->app_state
&&
6570 (*state
)->state_key
->app_state
->handler
) {
6571 (*state
)->state_key
->app_state
->handler(*state
, direction
,
6572 off
+ uh
->uh_ulen
, pd
, kif
);
6577 /* translate source/destination address, if necessary */
6578 #ifndef NO_APPLE_EXTENSIONS
6579 if (STATE_TRANSLATE((*state
)->state_key
)) {
6580 m
= pf_lazy_makewritable(pd
, m
, off
+ sizeof (*uh
));
6584 if (direction
== PF_OUT
)
6585 pf_change_ap(direction
, pd
->mp
, pd
->src
, &uh
->uh_sport
,
6586 pd
->ip_sum
, &uh
->uh_sum
,
6587 &(*state
)->state_key
->gwy
.addr
,
6588 (*state
)->state_key
->gwy
.xport
.port
, 1, pd
->af
);
6590 pf_change_ap(direction
, pd
->mp
, pd
->dst
, &uh
->uh_dport
,
6591 pd
->ip_sum
, &uh
->uh_sum
,
6592 &(*state
)->state_key
->lan
.addr
,
6593 (*state
)->state_key
->lan
.xport
.port
, 1, pd
->af
);
6594 m_copyback(m
, off
, sizeof (*uh
), uh
);
6597 if (STATE_TRANSLATE((*state
)->state_key
)) {
6598 if (direction
== PF_OUT
)
6599 pf_change_ap(pd
->src
, &uh
->uh_sport
, pd
->ip_sum
,
6600 &uh
->uh_sum
, &(*state
)->state_key
->gwy
.addr
,
6601 (*state
)->state_key
->gwy
.port
, 1, pd
->af
);
6603 pf_change_ap(pd
->dst
, &uh
->uh_dport
, pd
->ip_sum
,
6604 &uh
->uh_sum
, &(*state
)->state_key
->lan
.addr
,
6605 (*state
)->state_key
->lan
.port
, 1, pd
->af
);
6606 m_copyback(m
, off
, sizeof (*uh
), uh
);
6614 pf_test_state_icmp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
6615 struct mbuf
*m
, int off
, void *h
, struct pf_pdesc
*pd
, u_short
*reason
)
6618 struct pf_addr
*saddr
= pd
->src
, *daddr
= pd
->dst
;
6619 u_int16_t icmpid
= 0, *icmpsum
;
6622 struct pf_state_key_cmp key
;
6624 #ifndef NO_APPLE_EXTENSIONS
6625 struct pf_app_state as
;
6629 switch (pd
->proto
) {
6632 icmptype
= pd
->hdr
.icmp
->icmp_type
;
6633 icmpid
= pd
->hdr
.icmp
->icmp_id
;
6634 icmpsum
= &pd
->hdr
.icmp
->icmp_cksum
;
6636 if (icmptype
== ICMP_UNREACH
||
6637 icmptype
== ICMP_SOURCEQUENCH
||
6638 icmptype
== ICMP_REDIRECT
||
6639 icmptype
== ICMP_TIMXCEED
||
6640 icmptype
== ICMP_PARAMPROB
)
6645 case IPPROTO_ICMPV6
:
6646 icmptype
= pd
->hdr
.icmp6
->icmp6_type
;
6647 icmpid
= pd
->hdr
.icmp6
->icmp6_id
;
6648 icmpsum
= &pd
->hdr
.icmp6
->icmp6_cksum
;
6650 if (icmptype
== ICMP6_DST_UNREACH
||
6651 icmptype
== ICMP6_PACKET_TOO_BIG
||
6652 icmptype
== ICMP6_TIME_EXCEEDED
||
6653 icmptype
== ICMP6_PARAM_PROB
)
6662 * ICMP query/reply message not related to a TCP/UDP packet.
6663 * Search for an ICMP state.
6666 key
.proto
= pd
->proto
;
6667 if (direction
== PF_IN
) {
6668 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
6669 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
6670 #ifndef NO_APPLE_EXTENSIONS
6671 key
.ext
.xport
.port
= 0;
6672 key
.gwy
.xport
.port
= icmpid
;
6675 key
.gwy
.port
= icmpid
;
6678 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
6679 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
6680 #ifndef NO_APPLE_EXTENSIONS
6681 key
.lan
.xport
.port
= icmpid
;
6682 key
.ext
.xport
.port
= 0;
6684 key
.lan
.port
= icmpid
;
6691 (*state
)->expire
= pf_time_second();
6692 (*state
)->timeout
= PFTM_ICMP_ERROR_REPLY
;
6694 /* translate source/destination address, if necessary */
6695 if (STATE_TRANSLATE((*state
)->state_key
)) {
6696 if (direction
== PF_OUT
) {
6700 pf_change_a(&saddr
->v4
.s_addr
,
6702 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
6703 #ifndef NO_APPLE_EXTENSIONS
6704 pd
->hdr
.icmp
->icmp_cksum
=
6706 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6707 (*state
)->state_key
->gwy
.xport
.port
, 0);
6708 pd
->hdr
.icmp
->icmp_id
=
6709 (*state
)->state_key
->gwy
.xport
.port
;
6710 m
= pf_lazy_makewritable(pd
, m
,
6715 pd
->hdr
.icmp
->icmp_cksum
=
6717 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6718 (*state
)->state_key
->gwy
.port
, 0);
6719 pd
->hdr
.icmp
->icmp_id
=
6720 (*state
)->state_key
->gwy
.port
;
6722 m_copyback(m
, off
, ICMP_MINLEN
,
6729 &pd
->hdr
.icmp6
->icmp6_cksum
,
6730 &(*state
)->state_key
->gwy
.addr
, 0);
6731 #ifndef NO_APPLE_EXTENSIONS
6732 m
= pf_lazy_makewritable(pd
, m
,
6733 off
+ sizeof (struct icmp6_hdr
));
6738 sizeof (struct icmp6_hdr
),
6747 pf_change_a(&daddr
->v4
.s_addr
,
6749 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
6750 #ifndef NO_APPLE_EXTENSIONS
6751 pd
->hdr
.icmp
->icmp_cksum
=
6753 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6754 (*state
)->state_key
->lan
.xport
.port
, 0);
6755 pd
->hdr
.icmp
->icmp_id
=
6756 (*state
)->state_key
->lan
.xport
.port
;
6757 m
= pf_lazy_makewritable(pd
, m
,
6762 pd
->hdr
.icmp
->icmp_cksum
=
6764 pd
->hdr
.icmp
->icmp_cksum
, icmpid
,
6765 (*state
)->state_key
->lan
.port
, 0);
6766 pd
->hdr
.icmp
->icmp_id
=
6767 (*state
)->state_key
->lan
.port
;
6769 m_copyback(m
, off
, ICMP_MINLEN
,
6776 &pd
->hdr
.icmp6
->icmp6_cksum
,
6777 &(*state
)->state_key
->lan
.addr
, 0);
6778 #ifndef NO_APPLE_EXTENSIONS
6779 m
= pf_lazy_makewritable(pd
, m
,
6780 off
+ sizeof (struct icmp6_hdr
));
6785 sizeof (struct icmp6_hdr
),
6797 * ICMP error message in response to a TCP/UDP packet.
6798 * Extract the inner TCP/UDP header and search for that state.
6801 struct pf_pdesc pd2
;
6806 struct ip6_hdr h2_6
;
6812 memset(&pd2
, 0, sizeof (pd2
));
6818 /* offset of h2 in mbuf chain */
6819 ipoff2
= off
+ ICMP_MINLEN
;
6821 if (!pf_pull_hdr(m
, ipoff2
, &h2
, sizeof (h2
),
6822 NULL
, reason
, pd2
.af
)) {
6823 DPFPRINTF(PF_DEBUG_MISC
,
6824 ("pf: ICMP error message too short "
6829 * ICMP error messages don't refer to non-first
6832 if (h2
.ip_off
& htons(IP_OFFMASK
)) {
6833 REASON_SET(reason
, PFRES_FRAG
);
6837 /* offset of protocol header that follows h2 */
6838 off2
= ipoff2
+ (h2
.ip_hl
<< 2);
6840 pd2
.proto
= h2
.ip_p
;
6841 pd2
.src
= (struct pf_addr
*)&h2
.ip_src
;
6842 pd2
.dst
= (struct pf_addr
*)&h2
.ip_dst
;
6843 pd2
.ip_sum
= &h2
.ip_sum
;
6848 ipoff2
= off
+ sizeof (struct icmp6_hdr
);
6850 if (!pf_pull_hdr(m
, ipoff2
, &h2_6
, sizeof (h2_6
),
6851 NULL
, reason
, pd2
.af
)) {
6852 DPFPRINTF(PF_DEBUG_MISC
,
6853 ("pf: ICMP error message too short "
6857 pd2
.proto
= h2_6
.ip6_nxt
;
6858 pd2
.src
= (struct pf_addr
*)&h2_6
.ip6_src
;
6859 pd2
.dst
= (struct pf_addr
*)&h2_6
.ip6_dst
;
6861 off2
= ipoff2
+ sizeof (h2_6
);
6863 switch (pd2
.proto
) {
6864 case IPPROTO_FRAGMENT
:
6866 * ICMPv6 error messages for
6867 * non-first fragments
6869 REASON_SET(reason
, PFRES_FRAG
);
6872 case IPPROTO_HOPOPTS
:
6873 case IPPROTO_ROUTING
:
6874 case IPPROTO_DSTOPTS
: {
6875 /* get next header and header length */
6876 struct ip6_ext opt6
;
6878 if (!pf_pull_hdr(m
, off2
, &opt6
,
6879 sizeof (opt6
), NULL
, reason
,
6881 DPFPRINTF(PF_DEBUG_MISC
,
6882 ("pf: ICMPv6 short opt\n"));
6885 if (pd2
.proto
== IPPROTO_AH
)
6886 off2
+= (opt6
.ip6e_len
+ 2) * 4;
6888 off2
+= (opt6
.ip6e_len
+ 1) * 8;
6889 pd2
.proto
= opt6
.ip6e_nxt
;
6890 /* goto the next header */
6897 } while (!terminal
);
6902 switch (pd2
.proto
) {
6906 struct pf_state_peer
*src
, *dst
;
6911 * Only the first 8 bytes of the TCP header can be
6912 * expected. Don't access any TCP header fields after
6913 * th_seq, an ackskew test is not possible.
6915 if (!pf_pull_hdr(m
, off2
, &th
, 8, NULL
, reason
,
6917 DPFPRINTF(PF_DEBUG_MISC
,
6918 ("pf: ICMP error message too short "
6924 key
.proto
= IPPROTO_TCP
;
6925 if (direction
== PF_IN
) {
6926 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
6927 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
6928 #ifndef NO_APPLE_EXTENSIONS
6929 key
.ext
.xport
.port
= th
.th_dport
;
6930 key
.gwy
.xport
.port
= th
.th_sport
;
6932 key
.ext
.port
= th
.th_dport
;
6933 key
.gwy
.port
= th
.th_sport
;
6936 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
6937 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
6938 #ifndef NO_APPLE_EXTENSIONS
6939 key
.lan
.xport
.port
= th
.th_dport
;
6940 key
.ext
.xport
.port
= th
.th_sport
;
6942 key
.lan
.port
= th
.th_dport
;
6943 key
.ext
.port
= th
.th_sport
;
6949 if (direction
== (*state
)->state_key
->direction
) {
6950 src
= &(*state
)->dst
;
6951 dst
= &(*state
)->src
;
6953 src
= &(*state
)->src
;
6954 dst
= &(*state
)->dst
;
6957 if (src
->wscale
&& dst
->wscale
)
6958 dws
= dst
->wscale
& PF_WSCALE_MASK
;
6962 /* Demodulate sequence number */
6963 seq
= ntohl(th
.th_seq
) - src
->seqdiff
;
6965 pf_change_a(&th
.th_seq
, icmpsum
,
6970 if (!SEQ_GEQ(src
->seqhi
, seq
) ||
6971 !SEQ_GEQ(seq
, src
->seqlo
- (dst
->max_win
<< dws
))) {
6972 if (pf_status
.debug
>= PF_DEBUG_MISC
) {
6973 printf("pf: BAD ICMP %d:%d ",
6974 icmptype
, pd
->hdr
.icmp
->icmp_code
);
6975 pf_print_host(pd
->src
, 0, pd
->af
);
6977 pf_print_host(pd
->dst
, 0, pd
->af
);
6979 pf_print_state(*state
);
6980 printf(" seq=%u\n", seq
);
6982 REASON_SET(reason
, PFRES_BADSTATE
);
6986 if (STATE_TRANSLATE((*state
)->state_key
)) {
6987 if (direction
== PF_IN
) {
6988 pf_change_icmp(pd2
.src
, &th
.th_sport
,
6989 daddr
, &(*state
)->state_key
->lan
.addr
,
6990 #ifndef NO_APPLE_EXTENSIONS
6991 (*state
)->state_key
->lan
.xport
.port
, NULL
,
6993 (*state
)->state_key
->lan
.port
, NULL
,
6995 pd2
.ip_sum
, icmpsum
,
6996 pd
->ip_sum
, 0, pd2
.af
);
6998 pf_change_icmp(pd2
.dst
, &th
.th_dport
,
6999 saddr
, &(*state
)->state_key
->gwy
.addr
,
7000 #ifndef NO_APPLE_EXTENSIONS
7001 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7003 (*state
)->state_key
->gwy
.port
, NULL
,
7005 pd2
.ip_sum
, icmpsum
,
7006 pd
->ip_sum
, 0, pd2
.af
);
7012 #ifndef NO_APPLE_EXTENSIONS
7013 m
= pf_lazy_makewritable(pd
, m
, off2
+ 8);
7020 m_copyback(m
, off
, ICMP_MINLEN
,
7022 m_copyback(m
, ipoff2
, sizeof (h2
),
7029 sizeof (struct icmp6_hdr
),
7031 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7036 m_copyback(m
, off2
, 8, &th
);
7044 #ifndef NO_APPLE_EXTENSIONS
7047 if (!pf_pull_hdr(m
, off2
, &uh
, sizeof (uh
),
7048 NULL
, reason
, pd2
.af
)) {
7049 DPFPRINTF(PF_DEBUG_MISC
,
7050 ("pf: ICMP error message too short "
7056 key
.proto
= IPPROTO_UDP
;
7057 if (direction
== PF_IN
) {
7058 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7059 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7060 #ifndef NO_APPLE_EXTENSIONS
7061 key
.ext
.xport
.port
= uh
.uh_dport
;
7062 key
.gwy
.xport
.port
= uh
.uh_sport
;
7065 key
.ext
.port
= uh
.uh_dport
;
7066 key
.gwy
.port
= uh
.uh_sport
;
7069 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7070 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7071 #ifndef NO_APPLE_EXTENSIONS
7072 key
.lan
.xport
.port
= uh
.uh_dport
;
7073 key
.ext
.xport
.port
= uh
.uh_sport
;
7076 key
.lan
.port
= uh
.uh_dport
;
7077 key
.ext
.port
= uh
.uh_sport
;
7081 #ifndef NO_APPLE_EXTENSIONS
7082 key
.proto_variant
= PF_EXTFILTER_APD
;
7084 if (uh
.uh_sport
== PF_IKE_PORT
&&
7085 uh
.uh_dport
== PF_IKE_PORT
) {
7086 struct pf_ike_hdr ike
;
7088 m
->m_pkthdr
.len
- off2
- sizeof (uh
);
7089 if (direction
== PF_IN
&&
7090 plen
< 8 /* PF_IKE_PACKET_MINSIZE */) {
7091 DPFPRINTF(PF_DEBUG_MISC
, ("pf: "
7092 "ICMP error, embedded IKE message "
7097 if (plen
> sizeof (ike
))
7098 plen
= sizeof (ike
);
7099 m_copydata(m
, off
+ sizeof (uh
), plen
, &ike
);
7101 key
.app_state
= &as
;
7102 as
.compare_lan_ext
= pf_ike_compare
;
7103 as
.compare_ext_gwy
= pf_ike_compare
;
7104 as
.u
.ike
.cookie
= ike
.initiator_cookie
;
7107 *state
= pf_find_state(kif
, &key
, dx
);
7109 if (key
.app_state
&& *state
== 0) {
7111 *state
= pf_find_state(kif
, &key
, dx
);
7115 key
.proto_variant
= PF_EXTFILTER_AD
;
7116 *state
= pf_find_state(kif
, &key
, dx
);
7120 key
.proto_variant
= PF_EXTFILTER_EI
;
7121 *state
= pf_find_state(kif
, &key
, dx
);
7124 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7130 if (STATE_TRANSLATE((*state
)->state_key
)) {
7131 if (direction
== PF_IN
) {
7132 pf_change_icmp(pd2
.src
, &uh
.uh_sport
,
7133 daddr
, &(*state
)->state_key
->lan
.addr
,
7134 #ifndef NO_APPLE_EXTENSIONS
7135 (*state
)->state_key
->lan
.xport
.port
, &uh
.uh_sum
,
7137 (*state
)->state_key
->lan
.port
, &uh
.uh_sum
,
7139 pd2
.ip_sum
, icmpsum
,
7140 pd
->ip_sum
, 1, pd2
.af
);
7142 pf_change_icmp(pd2
.dst
, &uh
.uh_dport
,
7143 saddr
, &(*state
)->state_key
->gwy
.addr
,
7144 #ifndef NO_APPLE_EXTENSIONS
7145 (*state
)->state_key
->gwy
.xport
.port
, &uh
.uh_sum
,
7147 (*state
)->state_key
->gwy
.port
, &uh
.uh_sum
,
7149 pd2
.ip_sum
, icmpsum
,
7150 pd
->ip_sum
, 1, pd2
.af
);
7152 #ifndef NO_APPLE_EXTENSIONS
7153 m
= pf_lazy_makewritable(pd
, m
,
7154 off2
+ sizeof (uh
));
7161 m_copyback(m
, off
, ICMP_MINLEN
,
7163 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7169 sizeof (struct icmp6_hdr
),
7171 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7176 m_copyback(m
, off2
, sizeof (uh
), &uh
);
7183 case IPPROTO_ICMP
: {
7186 if (!pf_pull_hdr(m
, off2
, &iih
, ICMP_MINLEN
,
7187 NULL
, reason
, pd2
.af
)) {
7188 DPFPRINTF(PF_DEBUG_MISC
,
7189 ("pf: ICMP error message too short i"
7195 key
.proto
= IPPROTO_ICMP
;
7196 if (direction
== PF_IN
) {
7197 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7198 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7199 #ifndef NO_APPLE_EXTENSIONS
7200 key
.ext
.xport
.port
= 0;
7201 key
.gwy
.xport
.port
= iih
.icmp_id
;
7204 key
.gwy
.port
= iih
.icmp_id
;
7207 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7208 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7209 #ifndef NO_APPLE_EXTENSIONS
7210 key
.lan
.xport
.port
= iih
.icmp_id
;
7211 key
.ext
.xport
.port
= 0;
7213 key
.lan
.port
= iih
.icmp_id
;
7220 if (STATE_TRANSLATE((*state
)->state_key
)) {
7221 if (direction
== PF_IN
) {
7222 pf_change_icmp(pd2
.src
, &iih
.icmp_id
,
7223 daddr
, &(*state
)->state_key
->lan
.addr
,
7224 #ifndef NO_APPLE_EXTENSIONS
7225 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7227 (*state
)->state_key
->lan
.port
, NULL
,
7229 pd2
.ip_sum
, icmpsum
,
7230 pd
->ip_sum
, 0, AF_INET
);
7232 pf_change_icmp(pd2
.dst
, &iih
.icmp_id
,
7233 saddr
, &(*state
)->state_key
->gwy
.addr
,
7234 #ifndef NO_APPLE_EXTENSIONS
7235 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7237 (*state
)->state_key
->gwy
.port
, NULL
,
7239 pd2
.ip_sum
, icmpsum
,
7240 pd
->ip_sum
, 0, AF_INET
);
7242 #ifndef NO_APPLE_EXTENSIONS
7243 m
= pf_lazy_makewritable(pd
, m
, off2
+ ICMP_MINLEN
);
7247 m_copyback(m
, off
, ICMP_MINLEN
, pd
->hdr
.icmp
);
7248 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7249 m_copyback(m
, off2
, ICMP_MINLEN
, &iih
);
7257 case IPPROTO_ICMPV6
: {
7258 struct icmp6_hdr iih
;
7260 if (!pf_pull_hdr(m
, off2
, &iih
,
7261 sizeof (struct icmp6_hdr
), NULL
, reason
, pd2
.af
)) {
7262 DPFPRINTF(PF_DEBUG_MISC
,
7263 ("pf: ICMP error message too short "
7269 key
.proto
= IPPROTO_ICMPV6
;
7270 if (direction
== PF_IN
) {
7271 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7272 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7273 #ifndef NO_APPLE_EXTENSIONS
7274 key
.ext
.xport
.port
= 0;
7275 key
.gwy
.xport
.port
= iih
.icmp6_id
;
7278 key
.gwy
.port
= iih
.icmp6_id
;
7281 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7282 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7283 #ifndef NO_APPLE_EXTENSIONS
7284 key
.lan
.xport
.port
= iih
.icmp6_id
;
7285 key
.ext
.xport
.port
= 0;
7287 key
.lan
.port
= iih
.icmp6_id
;
7294 if (STATE_TRANSLATE((*state
)->state_key
)) {
7295 if (direction
== PF_IN
) {
7296 pf_change_icmp(pd2
.src
, &iih
.icmp6_id
,
7297 daddr
, &(*state
)->state_key
->lan
.addr
,
7298 #ifndef NO_APPLE_EXTENSIONS
7299 (*state
)->state_key
->lan
.xport
.port
, NULL
,
7301 (*state
)->state_key
->lan
.port
, NULL
,
7303 pd2
.ip_sum
, icmpsum
,
7304 pd
->ip_sum
, 0, AF_INET6
);
7306 pf_change_icmp(pd2
.dst
, &iih
.icmp6_id
,
7307 saddr
, &(*state
)->state_key
->gwy
.addr
,
7308 #ifndef NO_APPLE_EXTENSIONS
7309 (*state
)->state_key
->gwy
.xport
.port
, NULL
,
7311 (*state
)->state_key
->gwy
.port
, NULL
,
7313 pd2
.ip_sum
, icmpsum
,
7314 pd
->ip_sum
, 0, AF_INET6
);
7316 #ifndef NO_APPLE_EXTENSIONS
7317 m
= pf_lazy_makewritable(pd
, m
, off2
+
7318 sizeof (struct icmp6_hdr
));
7322 m_copyback(m
, off
, sizeof (struct icmp6_hdr
),
7324 m_copyback(m
, ipoff2
, sizeof (h2_6
), &h2_6
);
7325 m_copyback(m
, off2
, sizeof (struct icmp6_hdr
),
7335 key
.proto
= pd2
.proto
;
7336 if (direction
== PF_IN
) {
7337 PF_ACPY(&key
.ext
.addr
, pd2
.dst
, key
.af
);
7338 PF_ACPY(&key
.gwy
.addr
, pd2
.src
, key
.af
);
7339 #ifndef NO_APPLE_EXTENSIONS
7340 key
.ext
.xport
.port
= 0;
7341 key
.gwy
.xport
.port
= 0;
7347 PF_ACPY(&key
.lan
.addr
, pd2
.dst
, key
.af
);
7348 PF_ACPY(&key
.ext
.addr
, pd2
.src
, key
.af
);
7349 #ifndef NO_APPLE_EXTENSIONS
7350 key
.lan
.xport
.port
= 0;
7351 key
.ext
.xport
.port
= 0;
7360 if (STATE_TRANSLATE((*state
)->state_key
)) {
7361 if (direction
== PF_IN
) {
7362 pf_change_icmp(pd2
.src
, NULL
,
7363 daddr
, &(*state
)->state_key
->lan
.addr
,
7365 pd2
.ip_sum
, icmpsum
,
7366 pd
->ip_sum
, 0, pd2
.af
);
7368 pf_change_icmp(pd2
.dst
, NULL
,
7369 saddr
, &(*state
)->state_key
->gwy
.addr
,
7371 pd2
.ip_sum
, icmpsum
,
7372 pd
->ip_sum
, 0, pd2
.af
);
7377 #ifndef NO_APPLE_EXTENSIONS
7378 m
= pf_lazy_makewritable(pd
, m
,
7379 ipoff2
+ sizeof (h2
));
7383 m_copyback(m
, off
, ICMP_MINLEN
,
7385 m_copyback(m
, ipoff2
, sizeof (h2
), &h2
);
7390 #ifndef NO_APPLE_EXTENSIONS
7391 m
= pf_lazy_makewritable(pd
, m
,
7392 ipoff2
+ sizeof (h2_6
));
7397 sizeof (struct icmp6_hdr
),
7399 m_copyback(m
, ipoff2
, sizeof (h2_6
),
7413 #ifndef NO_APPLE_EXTENSIONS
7415 pf_test_state_grev1(struct pf_state
**state
, int direction
,
7416 struct pfi_kif
*kif
, int off
, struct pf_pdesc
*pd
)
7418 struct pf_state_peer
*src
;
7419 struct pf_state_peer
*dst
;
7420 struct pf_state_key_cmp key
;
7421 struct pf_grev1_hdr
*grev1
= pd
->hdr
.grev1
;
7424 #ifndef NO_APPLE_EXTENSIONS
7428 key
.proto
= IPPROTO_GRE
;
7429 key
.proto_variant
= PF_GRE_PPTP_VARIANT
;
7430 if (direction
== PF_IN
) {
7431 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7432 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7433 key
.gwy
.xport
.call_id
= grev1
->call_id
;
7435 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7436 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7437 key
.ext
.xport
.call_id
= grev1
->call_id
;
7442 if (direction
== (*state
)->state_key
->direction
) {
7443 src
= &(*state
)->src
;
7444 dst
= &(*state
)->dst
;
7446 src
= &(*state
)->dst
;
7447 dst
= &(*state
)->src
;
7451 if (src
->state
< PFGRE1S_INITIATING
)
7452 src
->state
= PFGRE1S_INITIATING
;
7454 /* update expire time */
7455 (*state
)->expire
= pf_time_second();
7456 if (src
->state
>= PFGRE1S_INITIATING
&&
7457 dst
->state
>= PFGRE1S_INITIATING
) {
7458 (*state
)->timeout
= PFTM_GREv1_ESTABLISHED
;
7459 src
->state
= PFGRE1S_ESTABLISHED
;
7460 dst
->state
= PFGRE1S_ESTABLISHED
;
7462 (*state
)->timeout
= PFTM_GREv1_INITIATING
;
7464 /* translate source/destination address, if necessary */
7465 if (STATE_GRE_TRANSLATE((*state
)->state_key
)) {
7466 if (direction
== PF_OUT
) {
7470 pf_change_a(&pd
->src
->v4
.s_addr
,
7472 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7477 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7483 grev1
->call_id
= (*state
)->state_key
->lan
.xport
.call_id
;
7488 pf_change_a(&pd
->dst
->v4
.s_addr
,
7490 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7495 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7502 m
= pf_lazy_makewritable(pd
, pd
->mp
, off
+ sizeof (*grev1
));
7505 m_copyback(m
, off
, sizeof (*grev1
), grev1
);
7512 pf_test_state_esp(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7513 int off
, struct pf_pdesc
*pd
)
7516 struct pf_state_peer
*src
;
7517 struct pf_state_peer
*dst
;
7518 struct pf_state_key_cmp key
;
7519 struct pf_esp_hdr
*esp
= pd
->hdr
.esp
;
7522 memset(&key
, 0, sizeof (key
));
7524 key
.proto
= IPPROTO_ESP
;
7525 if (direction
== PF_IN
) {
7526 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7527 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7528 key
.gwy
.xport
.spi
= esp
->spi
;
7530 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7531 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7532 key
.ext
.xport
.spi
= esp
->spi
;
7535 *state
= pf_find_state(kif
, &key
, direction
);
7542 * No matching state. Look for a blocking state. If we find
7543 * one, then use that state and move it so that it's keyed to
7544 * the SPI in the current packet.
7546 if (direction
== PF_IN
) {
7547 key
.gwy
.xport
.spi
= 0;
7549 s
= pf_find_state(kif
, &key
, direction
);
7551 struct pf_state_key
*sk
= s
->state_key
;
7553 RB_REMOVE(pf_state_tree_ext_gwy
,
7554 &pf_statetbl_ext_gwy
, sk
);
7555 sk
->lan
.xport
.spi
= sk
->gwy
.xport
.spi
=
7558 if (RB_INSERT(pf_state_tree_ext_gwy
,
7559 &pf_statetbl_ext_gwy
, sk
))
7560 pf_detach_state(s
, PF_DT_SKIP_EXTGWY
);
7565 key
.ext
.xport
.spi
= 0;
7567 s
= pf_find_state(kif
, &key
, direction
);
7569 struct pf_state_key
*sk
= s
->state_key
;
7571 RB_REMOVE(pf_state_tree_lan_ext
,
7572 &pf_statetbl_lan_ext
, sk
);
7573 sk
->ext
.xport
.spi
= esp
->spi
;
7575 if (RB_INSERT(pf_state_tree_lan_ext
,
7576 &pf_statetbl_lan_ext
, sk
))
7577 pf_detach_state(s
, PF_DT_SKIP_LANEXT
);
7586 if (s
->creatorid
== pf_status
.hostid
)
7587 pfsync_delete_state(s
);
7589 s
->timeout
= PFTM_UNLINKED
;
7590 hook_runloop(&s
->unlink_hooks
,
7591 HOOK_REMOVE
|HOOK_FREE
);
7592 pf_src_tree_remove_state(s
);
7599 if (pf_state_lookup_aux(state
, kif
, direction
, &action
))
7602 if (direction
== (*state
)->state_key
->direction
) {
7603 src
= &(*state
)->src
;
7604 dst
= &(*state
)->dst
;
7606 src
= &(*state
)->dst
;
7607 dst
= &(*state
)->src
;
7611 if (src
->state
< PFESPS_INITIATING
)
7612 src
->state
= PFESPS_INITIATING
;
7614 /* update expire time */
7615 (*state
)->expire
= pf_time_second();
7616 if (src
->state
>= PFESPS_INITIATING
&&
7617 dst
->state
>= PFESPS_INITIATING
) {
7618 (*state
)->timeout
= PFTM_ESP_ESTABLISHED
;
7619 src
->state
= PFESPS_ESTABLISHED
;
7620 dst
->state
= PFESPS_ESTABLISHED
;
7622 (*state
)->timeout
= PFTM_ESP_INITIATING
;
7624 /* translate source/destination address, if necessary */
7625 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7626 if (direction
== PF_OUT
) {
7630 pf_change_a(&pd
->src
->v4
.s_addr
,
7632 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
, 0);
7637 PF_ACPY(pd
->src
, &(*state
)->state_key
->gwy
.addr
,
7646 pf_change_a(&pd
->dst
->v4
.s_addr
,
7648 (*state
)->state_key
->lan
.addr
.v4
.s_addr
, 0);
7653 PF_ACPY(pd
->dst
, &(*state
)->state_key
->lan
.addr
,
7666 pf_test_state_other(struct pf_state
**state
, int direction
, struct pfi_kif
*kif
,
7667 struct pf_pdesc
*pd
)
7669 struct pf_state_peer
*src
, *dst
;
7670 struct pf_state_key_cmp key
;
7672 #ifndef NO_APPLE_EXTENSIONS
7676 key
.proto
= pd
->proto
;
7677 if (direction
== PF_IN
) {
7678 PF_ACPY(&key
.ext
.addr
, pd
->src
, key
.af
);
7679 PF_ACPY(&key
.gwy
.addr
, pd
->dst
, key
.af
);
7680 #ifndef NO_APPLE_EXTENSIONS
7681 key
.ext
.xport
.port
= 0;
7682 key
.gwy
.xport
.port
= 0;
7688 PF_ACPY(&key
.lan
.addr
, pd
->src
, key
.af
);
7689 PF_ACPY(&key
.ext
.addr
, pd
->dst
, key
.af
);
7690 #ifndef NO_APPLE_EXTENSIONS
7691 key
.lan
.xport
.port
= 0;
7692 key
.ext
.xport
.port
= 0;
7701 if (direction
== (*state
)->state_key
->direction
) {
7702 src
= &(*state
)->src
;
7703 dst
= &(*state
)->dst
;
7705 src
= &(*state
)->dst
;
7706 dst
= &(*state
)->src
;
7710 if (src
->state
< PFOTHERS_SINGLE
)
7711 src
->state
= PFOTHERS_SINGLE
;
7712 if (dst
->state
== PFOTHERS_SINGLE
)
7713 dst
->state
= PFOTHERS_MULTIPLE
;
7715 /* update expire time */
7716 (*state
)->expire
= pf_time_second();
7717 if (src
->state
== PFOTHERS_MULTIPLE
&& dst
->state
== PFOTHERS_MULTIPLE
)
7718 (*state
)->timeout
= PFTM_OTHER_MULTIPLE
;
7720 (*state
)->timeout
= PFTM_OTHER_SINGLE
;
7722 /* translate source/destination address, if necessary */
7723 #ifndef NO_APPLE_EXTENSIONS
7724 if (STATE_ADDR_TRANSLATE((*state
)->state_key
)) {
7726 if (STATE_TRANSLATE((*state
)->state_key
)) {
7728 if (direction
== PF_OUT
) {
7732 pf_change_a(&pd
->src
->v4
.s_addr
,
7734 (*state
)->state_key
->gwy
.addr
.v4
.s_addr
,
7741 &(*state
)->state_key
->gwy
.addr
, pd
->af
);
7749 pf_change_a(&pd
->dst
->v4
.s_addr
,
7751 (*state
)->state_key
->lan
.addr
.v4
.s_addr
,
7758 &(*state
)->state_key
->lan
.addr
, pd
->af
);
7769 * ipoff and off are measured from the start of the mbuf chain.
7770 * h must be at "ipoff" on the mbuf chain.
7773 pf_pull_hdr(struct mbuf
*m
, int off
, void *p
, int len
,
7774 u_short
*actionp
, u_short
*reasonp
, sa_family_t af
)
7779 struct ip
*h
= mtod(m
, struct ip
*);
7780 u_int16_t fragoff
= (ntohs(h
->ip_off
) & IP_OFFMASK
) << 3;
7783 if (fragoff
>= len
) {
7784 ACTION_SET(actionp
, PF_PASS
);
7786 ACTION_SET(actionp
, PF_DROP
);
7787 REASON_SET(reasonp
, PFRES_FRAG
);
7791 if (m
->m_pkthdr
.len
< off
+ len
||
7792 ntohs(h
->ip_len
) < off
+ len
) {
7793 ACTION_SET(actionp
, PF_DROP
);
7794 REASON_SET(reasonp
, PFRES_SHORT
);
7802 struct ip6_hdr
*h
= mtod(m
, struct ip6_hdr
*);
7804 if (m
->m_pkthdr
.len
< off
+ len
||
7805 (ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
)) <
7806 (unsigned)(off
+ len
)) {
7807 ACTION_SET(actionp
, PF_DROP
);
7808 REASON_SET(reasonp
, PFRES_SHORT
);
7815 m_copydata(m
, off
, len
, p
);
7820 pf_routable(struct pf_addr
*addr
, sa_family_t af
, struct pfi_kif
*kif
)
7823 struct sockaddr_in
*dst
;
7826 struct sockaddr_in6
*dst6
;
7827 struct route_in6 ro
;
7832 bzero(&ro
, sizeof (ro
));
7835 dst
= satosin(&ro
.ro_dst
);
7836 dst
->sin_family
= AF_INET
;
7837 dst
->sin_len
= sizeof (*dst
);
7838 dst
->sin_addr
= addr
->v4
;
7842 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7843 dst6
->sin6_family
= AF_INET6
;
7844 dst6
->sin6_len
= sizeof (*dst6
);
7845 dst6
->sin6_addr
= addr
->v6
;
7852 /* XXX: IFT_ENC is not currently used by anything*/
7853 /* Skip checks for ipsec interfaces */
7854 if (kif
!= NULL
&& kif
->pfik_ifp
->if_type
== IFT_ENC
)
7857 rtalloc((struct route
*)&ro
);
7860 if (ro
.ro_rt
!= NULL
)
7866 pf_rtlabel_match(struct pf_addr
*addr
, sa_family_t af
, struct pf_addr_wrap
*aw
)
7869 struct sockaddr_in
*dst
;
7871 struct sockaddr_in6
*dst6
;
7872 struct route_in6 ro
;
7878 bzero(&ro
, sizeof (ro
));
7881 dst
= satosin(&ro
.ro_dst
);
7882 dst
->sin_family
= AF_INET
;
7883 dst
->sin_len
= sizeof (*dst
);
7884 dst
->sin_addr
= addr
->v4
;
7888 dst6
= (struct sockaddr_in6
*)&ro
.ro_dst
;
7889 dst6
->sin6_family
= AF_INET6
;
7890 dst6
->sin6_len
= sizeof (*dst6
);
7891 dst6
->sin6_addr
= addr
->v6
;
7898 rtalloc((struct route
*)&ro
);
7900 if (ro
.ro_rt
!= NULL
) {
7909 pf_route(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
7910 struct pf_state
*s
, struct pf_pdesc
*pd
)
7913 struct mbuf
*m0
, *m1
;
7914 struct route iproute
;
7915 struct route
*ro
= NULL
;
7916 struct sockaddr_in
*dst
;
7918 struct ifnet
*ifp
= NULL
;
7919 struct pf_addr naddr
;
7920 struct pf_src_node
*sn
= NULL
;
7924 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
7925 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
7926 panic("pf_route: invalid parameters");
7928 if (pd
->pf_mtag
->routed
++ > 3) {
7934 if (r
->rt
== PF_DUPTO
) {
7935 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
7938 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
7943 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7944 DPFPRINTF(PF_DEBUG_URGENT
,
7945 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
7949 ip
= mtod(m0
, struct ip
*);
7952 bzero((caddr_t
)ro
, sizeof (*ro
));
7953 dst
= satosin(&ro
->ro_dst
);
7954 dst
->sin_family
= AF_INET
;
7955 dst
->sin_len
= sizeof (*dst
);
7956 dst
->sin_addr
= ip
->ip_dst
;
7958 if (r
->rt
== PF_FASTROUTE
) {
7960 if (ro
->ro_rt
== 0) {
7961 ipstat
.ips_noroute
++;
7965 ifp
= ro
->ro_rt
->rt_ifp
;
7966 ro
->ro_rt
->rt_use
++;
7968 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
7969 dst
= satosin(ro
->ro_rt
->rt_gateway
);
7971 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
7972 DPFPRINTF(PF_DEBUG_URGENT
,
7973 ("pf_route: TAILQ_EMPTY(&r->rpool.list)\n"));
7977 pf_map_addr(AF_INET
, r
, (struct pf_addr
*)&ip
->ip_src
,
7979 if (!PF_AZERO(&naddr
, AF_INET
))
7980 dst
->sin_addr
.s_addr
= naddr
.v4
.s_addr
;
7981 ifp
= r
->rpool
.cur
->kif
?
7982 r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
7984 if (!PF_AZERO(&s
->rt_addr
, AF_INET
))
7985 dst
->sin_addr
.s_addr
=
7986 s
->rt_addr
.v4
.s_addr
;
7987 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
7994 if (pf_test(PF_OUT
, ifp
, &m0
, NULL
) != PF_PASS
)
7996 else if (m0
== NULL
)
7998 if (m0
->m_len
< (int)sizeof (struct ip
)) {
7999 DPFPRINTF(PF_DEBUG_URGENT
,
8000 ("pf_route: m0->m_len < sizeof (struct ip)\n"));
8003 ip
= mtod(m0
, struct ip
*);
8006 /* Copied from ip_output. */
8008 /* Catch routing changes wrt. hardware checksumming for TCP or UDP. */
8009 m0
->m_pkthdr
.csum_flags
|= CSUM_IP
;
8010 sw_csum
= m0
->m_pkthdr
.csum_flags
&
8011 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
8013 if (ifp
->if_hwassist
& CSUM_TCP_SUM16
) {
8015 * Special case code for GMACE
8016 * frames that can be checksumed by GMACE SUM16 HW:
8017 * frame >64, no fragments, no UDP
8019 if (apple_hwcksum_tx
&& (m0
->m_pkthdr
.csum_flags
& CSUM_TCP
) &&
8020 (ntohs(ip
->ip_len
) > 50) &&
8021 (ntohs(ip
->ip_len
) <= ifp
->if_mtu
)) {
8023 * Apple GMAC HW, expects:
8024 * STUFF_OFFSET << 16 | START_OFFSET
8026 /* IP+Enet header length */
8027 u_short offset
= ((ip
->ip_hl
) << 2) + 14;
8028 u_short csumprev
= m0
->m_pkthdr
.csum_data
& 0xffff;
8029 m0
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
|
8030 CSUM_TCP_SUM16
; /* for GMAC */
8031 m0
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
8032 m0
->m_pkthdr
.csum_data
+= offset
;
8033 /* do IP hdr chksum in software */
8034 sw_csum
= CSUM_DELAY_IP
;
8036 /* let the software handle any UDP or TCP checksums */
8037 sw_csum
|= (CSUM_DELAY_DATA
& m0
->m_pkthdr
.csum_flags
);
8039 } else if (apple_hwcksum_tx
== 0) {
8040 sw_csum
|= (CSUM_DELAY_DATA
| CSUM_DELAY_IP
) &
8041 m0
->m_pkthdr
.csum_flags
;
8044 if (sw_csum
& CSUM_DELAY_DATA
) {
8045 in_delayed_cksum(m0
);
8046 sw_csum
&= ~CSUM_DELAY_DATA
;
8047 m0
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
8050 if (apple_hwcksum_tx
!= 0) {
8051 m0
->m_pkthdr
.csum_flags
&=
8052 IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
8054 m0
->m_pkthdr
.csum_flags
= 0;
8057 if (ntohs(ip
->ip_len
) <= ifp
->if_mtu
||
8058 (ifp
->if_hwassist
& CSUM_FRAGMENT
)) {
8060 if (sw_csum
& CSUM_DELAY_IP
)
8061 ip
->ip_sum
= in_cksum(m0
, ip
->ip_hl
<< 2);
8062 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
, sintosa(dst
));
8067 * Too large for interface; fragment if possible.
8068 * Must be able to put at least 8 bytes per fragment.
8070 if (ip
->ip_off
& htons(IP_DF
)) {
8071 ipstat
.ips_cantfrag
++;
8072 if (r
->rt
!= PF_DUPTO
) {
8073 icmp_error(m0
, ICMP_UNREACH
, ICMP_UNREACH_NEEDFRAG
, 0,
8081 error
= ip_fragment(m0
, ifp
, ifp
->if_mtu
, sw_csum
);
8087 for (m0
= m1
; m0
; m0
= m1
) {
8091 error
= ifnet_output(ifp
, PF_INET
, m0
, ro
,
8098 ipstat
.ips_fragmented
++;
8101 if (r
->rt
!= PF_DUPTO
)
8103 if (ro
== &iproute
&& ro
->ro_rt
)
8115 pf_route6(struct mbuf
**m
, struct pf_rule
*r
, int dir
, struct ifnet
*oifp
,
8116 struct pf_state
*s
, struct pf_pdesc
*pd
)
8120 struct route_in6 ip6route
;
8121 struct route_in6
*ro
;
8122 struct sockaddr_in6
*dst
;
8123 struct ip6_hdr
*ip6
;
8124 struct ifnet
*ifp
= NULL
;
8125 struct pf_addr naddr
;
8126 struct pf_src_node
*sn
= NULL
;
8129 if (m
== NULL
|| *m
== NULL
|| r
== NULL
||
8130 (dir
!= PF_IN
&& dir
!= PF_OUT
) || oifp
== NULL
)
8131 panic("pf_route6: invalid parameters");
8133 if (pd
->pf_mtag
->routed
++ > 3) {
8139 if (r
->rt
== PF_DUPTO
) {
8140 if ((m0
= m_copym(*m
, 0, M_COPYALL
, M_NOWAIT
)) == NULL
)
8143 if ((r
->rt
== PF_REPLYTO
) == (r
->direction
== dir
))
8148 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
8149 DPFPRINTF(PF_DEBUG_URGENT
,
8150 ("pf_route6: m0->m_len < sizeof (struct ip6_hdr)\n"));
8153 ip6
= mtod(m0
, struct ip6_hdr
*);
8156 bzero((caddr_t
)ro
, sizeof (*ro
));
8157 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
8158 dst
->sin6_family
= AF_INET6
;
8159 dst
->sin6_len
= sizeof (*dst
);
8160 dst
->sin6_addr
= ip6
->ip6_dst
;
8162 /* Cheat. XXX why only in the v6 case??? */
8163 if (r
->rt
== PF_FASTROUTE
) {
8164 struct pf_mtag
*pf_mtag
;
8166 if ((pf_mtag
= pf_get_mtag(m0
)) == NULL
)
8168 pf_mtag
->flags
|= PF_TAG_GENERATED
;
8169 ip6_output(m0
, NULL
, NULL
, 0, NULL
, NULL
, 0);
8173 if (TAILQ_EMPTY(&r
->rpool
.list
)) {
8174 DPFPRINTF(PF_DEBUG_URGENT
,
8175 ("pf_route6: TAILQ_EMPTY(&r->rpool.list)\n"));
8179 pf_map_addr(AF_INET6
, r
, (struct pf_addr
*)&ip6
->ip6_src
,
8181 if (!PF_AZERO(&naddr
, AF_INET6
))
8182 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
8184 ifp
= r
->rpool
.cur
->kif
? r
->rpool
.cur
->kif
->pfik_ifp
: NULL
;
8186 if (!PF_AZERO(&s
->rt_addr
, AF_INET6
))
8187 PF_ACPY((struct pf_addr
*)&dst
->sin6_addr
,
8188 &s
->rt_addr
, AF_INET6
);
8189 ifp
= s
->rt_kif
? s
->rt_kif
->pfik_ifp
: NULL
;
8195 if (pf_test6(PF_OUT
, ifp
, &m0
, NULL
) != PF_PASS
)
8197 else if (m0
== NULL
)
8199 if (m0
->m_len
< (int)sizeof (struct ip6_hdr
)) {
8200 DPFPRINTF(PF_DEBUG_URGENT
, ("pf_route6: m0->m_len "
8201 "< sizeof (struct ip6_hdr)\n"));
8204 ip6
= mtod(m0
, struct ip6_hdr
*);
8208 * If the packet is too large for the outgoing interface,
8209 * send back an icmp6 error.
8211 if (IN6_IS_SCOPE_EMBED(&dst
->sin6_addr
))
8212 dst
->sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
8213 if ((unsigned)m0
->m_pkthdr
.len
<= ifp
->if_mtu
) {
8214 error
= nd6_output(ifp
, ifp
, m0
, dst
, NULL
, 0);
8216 in6_ifstat_inc(ifp
, ifs6_in_toobig
);
8217 if (r
->rt
!= PF_DUPTO
)
8218 icmp6_error(m0
, ICMP6_PACKET_TOO_BIG
, 0, ifp
->if_mtu
);
8224 if (r
->rt
!= PF_DUPTO
)
8236 * check protocol (tcp/udp/icmp/icmp6) checksum and set mbuf flag
8237 * off is the offset where the protocol header starts
8238 * len is the total length of protocol header plus payload
8239 * returns 0 when the checksum is valid, otherwise returns 1.
8242 pf_check_proto_cksum(struct mbuf
*m
, int off
, int len
, u_int8_t p
,
8251 * Optimize for the common case; if the hardware calculated
8252 * value doesn't include pseudo-header checksum, or if it
8253 * is partially-computed (only 16-bit summation), do it in
8256 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
&
8257 (CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
)) &&
8258 (m
->m_pkthdr
.csum_data
^ 0xffff) == 0) {
8264 case IPPROTO_ICMPV6
:
8270 if (off
< (int)sizeof (struct ip
) || len
< (int)sizeof (struct udphdr
))
8272 if (m
->m_pkthdr
.len
< off
+ len
)
8277 if (p
== IPPROTO_ICMP
) {
8282 sum
= in_cksum(m
, len
);
8286 if (m
->m_len
< (int)sizeof (struct ip
))
8288 sum
= inet_cksum(m
, p
, off
, len
);
8294 if (m
->m_len
< (int)sizeof (struct ip6_hdr
))
8296 sum
= inet6_cksum(m
, p
, off
, len
);
8305 tcpstat
.tcps_rcvbadsum
++;
8308 udpstat
.udps_badsum
++;
8311 icmpstat
.icps_checksum
++;
8314 case IPPROTO_ICMPV6
:
8315 icmp6stat
.icp6s_checksum
++;
8325 #ifndef NO_APPLE_EXTENSIONS
8326 #define PF_APPLE_UPDATE_PDESC_IPv4() \
8328 if (m && pd.mp && m != pd.mp) { \
8330 h = mtod(m, struct ip *); \
8334 #define PF_APPLE_UPDATE_PDESC_IPv4()
8338 pf_test(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8339 struct ether_header
*eh
)
8341 struct pfi_kif
*kif
;
8342 u_short action
, reason
= 0, log
= 0;
8343 struct mbuf
*m
= *m0
;
8345 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8346 struct pf_state
*s
= NULL
;
8347 struct pf_state_key
*sk
= NULL
;
8348 struct pf_ruleset
*ruleset
= NULL
;
8350 int off
, dirndx
, pqid
= 0;
8352 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8354 if (!pf_status
.running
)
8357 memset(&pd
, 0, sizeof (pd
));
8359 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8360 DPFPRINTF(PF_DEBUG_URGENT
,
8361 ("pf_test: pf_get_mtag returned NULL\n"));
8365 if (pd
.pf_mtag
->flags
& PF_TAG_GENERATED
)
8368 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8371 DPFPRINTF(PF_DEBUG_URGENT
,
8372 ("pf_test: kif == NULL, if_name %s\n", ifp
->if_name
));
8375 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8379 if ((m
->m_flags
& M_PKTHDR
) == 0)
8380 panic("non-M_PKTHDR is passed to pf_test");
8381 #endif /* DIAGNOSTIC */
8383 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8385 REASON_SET(&reason
, PFRES_SHORT
);
8390 /* We do IP header normalization and packet reassembly here */
8391 if (pf_normalize_ip(m0
, dir
, kif
, &reason
, &pd
) != PF_PASS
) {
8395 m
= *m0
; /* pf_normalize messes with m0 */
8396 h
= mtod(m
, struct ip
*);
8398 off
= h
->ip_hl
<< 2;
8399 if (off
< (int)sizeof (*h
)) {
8401 REASON_SET(&reason
, PFRES_SHORT
);
8406 pd
.src
= (struct pf_addr
*)&h
->ip_src
;
8407 pd
.dst
= (struct pf_addr
*)&h
->ip_dst
;
8408 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET
);
8409 pd
.ip_sum
= &h
->ip_sum
;
8411 #ifndef NO_APPLE_EXTENSIONS
8412 pd
.proto_variant
= 0;
8418 pd
.tot_len
= ntohs(h
->ip_len
);
8421 /* handle fragments that didn't get reassembled by normalization */
8422 if (h
->ip_off
& htons(IP_MF
| IP_OFFMASK
)) {
8423 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8433 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8434 &action
, &reason
, AF_INET
)) {
8435 log
= action
!= PF_PASS
;
8438 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8439 if ((th
.th_flags
& TH_ACK
) && pd
.p_len
== 0)
8441 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8442 if (action
== PF_DROP
)
8444 PF_APPLE_UPDATE_PDESC_IPv4();
8445 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8447 #ifndef NO_APPLE_EXTENSIONS
8450 PF_APPLE_UPDATE_PDESC_IPv4();
8452 if (action
== PF_PASS
) {
8454 pfsync_update_state(s
);
8455 #endif /* NPFSYNC */
8459 } else if (s
== NULL
)
8460 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8461 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8469 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8470 &action
, &reason
, AF_INET
)) {
8471 log
= action
!= PF_PASS
;
8474 if (uh
.uh_dport
== 0 ||
8475 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8476 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8478 REASON_SET(&reason
, PFRES_SHORT
);
8481 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
);
8482 #ifndef NO_APPLE_EXTENSIONS
8485 PF_APPLE_UPDATE_PDESC_IPv4();
8487 if (action
== PF_PASS
) {
8489 pfsync_update_state(s
);
8490 #endif /* NPFSYNC */
8494 } else if (s
== NULL
)
8495 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8496 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8500 case IPPROTO_ICMP
: {
8504 if (!pf_pull_hdr(m
, off
, &ih
, ICMP_MINLEN
,
8505 &action
, &reason
, AF_INET
)) {
8506 log
= action
!= PF_PASS
;
8509 action
= pf_test_state_icmp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8511 #ifndef NO_APPLE_EXTENSIONS
8514 PF_APPLE_UPDATE_PDESC_IPv4();
8516 if (action
== PF_PASS
) {
8518 pfsync_update_state(s
);
8519 #endif /* NPFSYNC */
8523 } else if (s
== NULL
)
8524 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8525 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8529 #ifndef NO_APPLE_EXTENSIONS
8531 struct pf_esp_hdr esp
;
8534 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
8536 log
= action
!= PF_PASS
;
8539 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
8542 PF_APPLE_UPDATE_PDESC_IPv4();
8543 if (action
== PF_PASS
) {
8545 pfsync_update_state(s
);
8546 #endif /* NPFSYNC */
8550 } else if (s
== NULL
)
8551 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8552 m
, off
, h
, &pd
, &a
, &ruleset
, &ipintrq
);
8557 struct pf_grev1_hdr grev1
;
8558 pd
.hdr
.grev1
= &grev1
;
8559 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
8560 &reason
, AF_INET
)) {
8561 log
= (action
!= PF_PASS
);
8564 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
8565 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
8566 if (ntohs(grev1
.payload_length
) >
8567 m
->m_pkthdr
.len
- off
) {
8569 REASON_SET(&reason
, PFRES_SHORT
);
8572 pd
.proto_variant
= PF_GRE_PPTP_VARIANT
;
8573 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
8574 if (pd
.lmw
< 0) goto done
;
8575 PF_APPLE_UPDATE_PDESC_IPv4();
8576 if (action
== PF_PASS
) {
8578 pfsync_update_state(s
);
8579 #endif /* NPFSYNC */
8584 } else if (s
== NULL
) {
8585 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
8586 h
, &pd
, &a
, &ruleset
, &ipintrq
);
8587 if (action
== PF_PASS
)
8592 /* not GREv1/PPTP, so treat as ordinary GRE... */
8597 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
8598 #ifndef NO_APPLE_EXTENSIONS
8601 PF_APPLE_UPDATE_PDESC_IPv4();
8603 if (action
== PF_PASS
) {
8605 pfsync_update_state(s
);
8606 #endif /* NPFSYNC */
8610 } else if (s
== NULL
)
8611 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
8612 &pd
, &a
, &ruleset
, &ipintrq
);
8617 PF_APPLE_UPDATE_PDESC_IPv4();
8619 if (action
== PF_PASS
&& h
->ip_hl
> 5 &&
8620 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
8622 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8624 DPFPRINTF(PF_DEBUG_MISC
,
8625 ("pf: dropping packet with ip options [hlen=%u]\n",
8626 (unsigned int) h
->ip_hl
));
8629 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
))
8630 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
8634 if (action
== PF_PASS
&& r
->qid
) {
8635 if (pqid
|| (pd
.tos
& IPTOS_LOWDELAY
))
8636 pd
.pf_mtag
->qid
= r
->pqid
;
8638 pd
.pf_mtag
->qid
= r
->qid
;
8639 /* add hints for ecn */
8640 pd
.pf_mtag
->hdr
= h
;
8645 * connections redirected to loopback should not match sockets
8646 * bound specifically to loopback due to security implications,
8647 * see tcp_input() and in_pcblookup_listen().
8649 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
8650 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8651 (s
->nat_rule
.ptr
->action
== PF_RDR
||
8652 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
8653 (ntohl(pd
.dst
->v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
8654 pd
.pf_mtag
->flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
8659 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
8660 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
8661 lr
= s
->nat_rule
.ptr
;
8664 PFLOG_PACKET(kif
, h
, m
, AF_INET
, dir
, reason
, lr
, a
, ruleset
,
8668 kif
->pfik_bytes
[0][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
8669 kif
->pfik_packets
[0][dir
== PF_OUT
][action
!= PF_PASS
]++;
8671 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
8672 dirndx
= (dir
== PF_OUT
);
8673 r
->packets
[dirndx
]++;
8674 r
->bytes
[dirndx
] += pd
.tot_len
;
8676 a
->packets
[dirndx
]++;
8677 a
->bytes
[dirndx
] += pd
.tot_len
;
8681 if (s
->nat_rule
.ptr
!= NULL
) {
8682 s
->nat_rule
.ptr
->packets
[dirndx
]++;
8683 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
8685 if (s
->src_node
!= NULL
) {
8686 s
->src_node
->packets
[dirndx
]++;
8687 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
8689 if (s
->nat_src_node
!= NULL
) {
8690 s
->nat_src_node
->packets
[dirndx
]++;
8691 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
8693 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
8694 s
->packets
[dirndx
]++;
8695 s
->bytes
[dirndx
] += pd
.tot_len
;
8698 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
8702 * XXX: we need to make sure that the addresses
8703 * passed to pfr_update_stats() are the same than
8704 * the addresses used during matching (pfr_match)
8706 if (r
== &pf_default_rule
) {
8708 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8709 &pd
.baddr
: &pd
.naddr
;
8711 x
= (sk
== NULL
|| sk
->direction
== dir
) ?
8712 &pd
.naddr
: &pd
.baddr
;
8713 if (x
== &pd
.baddr
|| s
== NULL
) {
8714 /* we need to change the address */
8721 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
8722 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
8723 sk
->direction
== dir
) ?
8724 pd
.src
: pd
.dst
, pd
.af
,
8725 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8727 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
8728 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
8729 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
8730 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
8734 #ifndef NO_APPLE_EXTENSIONS
8746 if (action
== PF_SYNPROXY_DROP
) {
8751 /* pf_route can free the mbuf causing *m0 to become NULL */
8752 pf_route(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
8759 #ifndef NO_APPLE_EXTENSIONS
8760 #define PF_APPLE_UPDATE_PDESC_IPv6() \
8762 if (m && pd.mp && m != pd.mp) { \
8766 h = mtod(m, struct ip6_hdr *); \
8770 #define PF_APPLE_UPDATE_PDESC_IPv6()
8774 pf_test6(int dir
, struct ifnet
*ifp
, struct mbuf
**m0
,
8775 struct ether_header
*eh
)
8777 struct pfi_kif
*kif
;
8778 u_short action
, reason
= 0, log
= 0;
8779 struct mbuf
*m
= *m0
, *n
= NULL
;
8781 struct pf_rule
*a
= NULL
, *r
= &pf_default_rule
, *tr
, *nr
;
8782 struct pf_state
*s
= NULL
;
8783 struct pf_state_key
*sk
= NULL
;
8784 struct pf_ruleset
*ruleset
= NULL
;
8786 int off
, terminal
= 0, dirndx
, rh_cnt
= 0;
8788 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
8790 if (!pf_status
.running
)
8793 memset(&pd
, 0, sizeof (pd
));
8795 if ((pd
.pf_mtag
= pf_get_mtag(m
)) == NULL
) {
8796 DPFPRINTF(PF_DEBUG_URGENT
,
8797 ("pf_test6: pf_get_mtag returned NULL\n"));
8801 if (pd
.pf_mtag
->flags
& PF_TAG_GENERATED
)
8804 kif
= (struct pfi_kif
*)ifp
->if_pf_kif
;
8807 DPFPRINTF(PF_DEBUG_URGENT
,
8808 ("pf_test6: kif == NULL, if_name %s\n", ifp
->if_name
));
8811 if (kif
->pfik_flags
& PFI_IFLAG_SKIP
)
8815 if ((m
->m_flags
& M_PKTHDR
) == 0)
8816 panic("non-M_PKTHDR is passed to pf_test6");
8817 #endif /* DIAGNOSTIC */
8819 h
= mtod(m
, struct ip6_hdr
*);
8821 if (m
->m_pkthdr
.len
< (int)sizeof (*h
)) {
8823 REASON_SET(&reason
, PFRES_SHORT
);
8828 /* We do IP header normalization and packet reassembly here */
8829 if (pf_normalize_ip6(m0
, dir
, kif
, &reason
, &pd
) != PF_PASS
) {
8833 m
= *m0
; /* pf_normalize messes with m0 */
8834 h
= mtod(m
, struct ip6_hdr
*);
8838 * we do not support jumbogram yet. if we keep going, zero ip6_plen
8839 * will do something bad, so drop the packet for now.
8841 if (htons(h
->ip6_plen
) == 0) {
8843 REASON_SET(&reason
, PFRES_NORM
); /*XXX*/
8848 pd
.src
= (struct pf_addr
*)&h
->ip6_src
;
8849 pd
.dst
= (struct pf_addr
*)&h
->ip6_dst
;
8850 PF_ACPY(&pd
.baddr
, dir
== PF_OUT
? pd
.src
: pd
.dst
, AF_INET6
);
8854 pd
.tot_len
= ntohs(h
->ip6_plen
) + sizeof (struct ip6_hdr
);
8857 off
= ((caddr_t
)h
- m
->m_data
) + sizeof (struct ip6_hdr
);
8858 pd
.proto
= h
->ip6_nxt
;
8859 #ifndef NO_APPLE_EXTENSIONS
8860 pd
.proto_variant
= 0;
8866 case IPPROTO_FRAGMENT
:
8867 action
= pf_test_fragment(&r
, dir
, kif
, m
, h
,
8869 if (action
== PF_DROP
)
8870 REASON_SET(&reason
, PFRES_FRAG
);
8872 case IPPROTO_ROUTING
: {
8873 struct ip6_rthdr rthdr
;
8876 DPFPRINTF(PF_DEBUG_MISC
,
8877 ("pf: IPv6 more than one rthdr\n"));
8879 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8883 if (!pf_pull_hdr(m
, off
, &rthdr
, sizeof (rthdr
), NULL
,
8885 DPFPRINTF(PF_DEBUG_MISC
,
8886 ("pf: IPv6 short rthdr\n"));
8888 REASON_SET(&reason
, PFRES_SHORT
);
8892 if (rthdr
.ip6r_type
== IPV6_RTHDR_TYPE_0
) {
8893 DPFPRINTF(PF_DEBUG_MISC
,
8894 ("pf: IPv6 rthdr0\n"));
8896 REASON_SET(&reason
, PFRES_IPOPTIONS
);
8903 case IPPROTO_HOPOPTS
:
8904 case IPPROTO_DSTOPTS
: {
8905 /* get next header and header length */
8906 struct ip6_ext opt6
;
8908 if (!pf_pull_hdr(m
, off
, &opt6
, sizeof (opt6
),
8909 NULL
, &reason
, pd
.af
)) {
8910 DPFPRINTF(PF_DEBUG_MISC
,
8911 ("pf: IPv6 short opt\n"));
8916 if (pd
.proto
== IPPROTO_AH
)
8917 off
+= (opt6
.ip6e_len
+ 2) * 4;
8919 off
+= (opt6
.ip6e_len
+ 1) * 8;
8920 pd
.proto
= opt6
.ip6e_nxt
;
8921 /* goto the next header */
8928 } while (!terminal
);
8930 /* if there's no routing header, use unmodified mbuf for checksumming */
8940 if (!pf_pull_hdr(m
, off
, &th
, sizeof (th
),
8941 &action
, &reason
, AF_INET6
)) {
8942 log
= action
!= PF_PASS
;
8945 pd
.p_len
= pd
.tot_len
- off
- (th
.th_off
<< 2);
8946 action
= pf_normalize_tcp(dir
, kif
, m
, 0, off
, h
, &pd
);
8947 if (action
== PF_DROP
)
8949 PF_APPLE_UPDATE_PDESC_IPv6();
8950 action
= pf_test_state_tcp(&s
, dir
, kif
, m
, off
, h
, &pd
,
8952 #ifndef NO_APPLE_EXTENSIONS
8955 PF_APPLE_UPDATE_PDESC_IPv6();
8957 if (action
== PF_PASS
) {
8959 pfsync_update_state(s
);
8960 #endif /* NPFSYNC */
8964 } else if (s
== NULL
)
8965 action
= pf_test_rule(&r
, &s
, dir
, kif
,
8966 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
8974 if (!pf_pull_hdr(m
, off
, &uh
, sizeof (uh
),
8975 &action
, &reason
, AF_INET6
)) {
8976 log
= action
!= PF_PASS
;
8979 if (uh
.uh_dport
== 0 ||
8980 ntohs(uh
.uh_ulen
) > m
->m_pkthdr
.len
- off
||
8981 ntohs(uh
.uh_ulen
) < sizeof (struct udphdr
)) {
8983 REASON_SET(&reason
, PFRES_SHORT
);
8986 action
= pf_test_state_udp(&s
, dir
, kif
, m
, off
, h
, &pd
);
8987 #ifndef NO_APPLE_EXTENSIONS
8990 PF_APPLE_UPDATE_PDESC_IPv6();
8992 if (action
== PF_PASS
) {
8994 pfsync_update_state(s
);
8995 #endif /* NPFSYNC */
8999 } else if (s
== NULL
)
9000 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9001 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
9005 case IPPROTO_ICMPV6
: {
9006 struct icmp6_hdr ih
;
9009 if (!pf_pull_hdr(m
, off
, &ih
, sizeof (ih
),
9010 &action
, &reason
, AF_INET6
)) {
9011 log
= action
!= PF_PASS
;
9014 action
= pf_test_state_icmp(&s
, dir
, kif
,
9015 m
, off
, h
, &pd
, &reason
);
9016 #ifndef NO_APPLE_EXTENSIONS
9019 PF_APPLE_UPDATE_PDESC_IPv6();
9021 if (action
== PF_PASS
) {
9023 pfsync_update_state(s
);
9024 #endif /* NPFSYNC */
9028 } else if (s
== NULL
)
9029 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9030 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
9034 #ifndef NO_APPLE_EXTENSIONS
9036 struct pf_esp_hdr esp
;
9039 if (!pf_pull_hdr(m
, off
, &esp
, sizeof (esp
), &action
, &reason
,
9041 log
= action
!= PF_PASS
;
9044 action
= pf_test_state_esp(&s
, dir
, kif
, off
, &pd
);
9047 PF_APPLE_UPDATE_PDESC_IPv6();
9048 if (action
== PF_PASS
) {
9050 pfsync_update_state(s
);
9051 #endif /* NPFSYNC */
9055 } else if (s
== NULL
)
9056 action
= pf_test_rule(&r
, &s
, dir
, kif
,
9057 m
, off
, h
, &pd
, &a
, &ruleset
, &ip6intrq
);
9062 struct pf_grev1_hdr grev1
;
9064 pd
.hdr
.grev1
= &grev1
;
9065 if (!pf_pull_hdr(m
, off
, &grev1
, sizeof (grev1
), &action
,
9066 &reason
, AF_INET6
)) {
9067 log
= (action
!= PF_PASS
);
9070 if ((ntohs(grev1
.flags
) & PF_GRE_FLAG_VERSION_MASK
) == 1 &&
9071 ntohs(grev1
.protocol_type
) == PF_GRE_PPP_ETHERTYPE
) {
9072 if (ntohs(grev1
.payload_length
) >
9073 m
->m_pkthdr
.len
- off
) {
9075 REASON_SET(&reason
, PFRES_SHORT
);
9078 action
= pf_test_state_grev1(&s
, dir
, kif
, off
, &pd
);
9081 PF_APPLE_UPDATE_PDESC_IPv6();
9082 if (action
== PF_PASS
) {
9084 pfsync_update_state(s
);
9085 #endif /* NPFSYNC */
9090 } else if (s
== NULL
) {
9091 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
,
9092 h
, &pd
, &a
, &ruleset
, &ip6intrq
);
9093 if (action
== PF_PASS
)
9098 /* not GREv1/PPTP, so treat as ordinary GRE... */
9103 action
= pf_test_state_other(&s
, dir
, kif
, &pd
);
9104 #ifndef NO_APPLE_EXTENSIONS
9107 PF_APPLE_UPDATE_PDESC_IPv6();
9109 if (action
== PF_PASS
) {
9111 pfsync_update_state(s
);
9112 #endif /* NPFSYNC */
9116 } else if (s
== NULL
)
9117 action
= pf_test_rule(&r
, &s
, dir
, kif
, m
, off
, h
,
9118 &pd
, &a
, &ruleset
, &ip6intrq
);
9123 PF_APPLE_UPDATE_PDESC_IPv6();
9130 /* handle dangerous IPv6 extension headers. */
9131 if (action
== PF_PASS
&& rh_cnt
&&
9132 !((s
&& s
->allow_opts
) || r
->allow_opts
)) {
9134 REASON_SET(&reason
, PFRES_IPOPTIONS
);
9136 DPFPRINTF(PF_DEBUG_MISC
,
9137 ("pf: dropping packet with dangerous v6 headers\n"));
9140 if ((s
&& s
->tag
) || PF_RTABLEID_IS_VALID(r
->rtableid
))
9141 (void) pf_tag_packet(m
, pd
.pf_mtag
, s
? s
->tag
: 0,
9145 if (action
== PF_PASS
&& r
->qid
) {
9146 if (pd
.tos
& IPTOS_LOWDELAY
)
9147 pd
.pf_mtag
->qid
= r
->pqid
;
9149 pd
.pf_mtag
->qid
= r
->qid
;
9150 /* add hints for ecn */
9151 pd
.pf_mtag
->hdr
= h
;
9155 if (dir
== PF_IN
&& action
== PF_PASS
&& (pd
.proto
== IPPROTO_TCP
||
9156 pd
.proto
== IPPROTO_UDP
) && s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9157 (s
->nat_rule
.ptr
->action
== PF_RDR
||
9158 s
->nat_rule
.ptr
->action
== PF_BINAT
) &&
9159 IN6_IS_ADDR_LOOPBACK(&pd
.dst
->v6
))
9160 pd
.pf_mtag
->flags
|= PF_TAG_TRANSLATE_LOCALHOST
;
9165 if (s
!= NULL
&& s
->nat_rule
.ptr
!= NULL
&&
9166 s
->nat_rule
.ptr
->log
& PF_LOG_ALL
)
9167 lr
= s
->nat_rule
.ptr
;
9170 PFLOG_PACKET(kif
, h
, m
, AF_INET6
, dir
, reason
, lr
, a
, ruleset
,
9174 kif
->pfik_bytes
[1][dir
== PF_OUT
][action
!= PF_PASS
] += pd
.tot_len
;
9175 kif
->pfik_packets
[1][dir
== PF_OUT
][action
!= PF_PASS
]++;
9177 if (action
== PF_PASS
|| r
->action
== PF_DROP
) {
9178 dirndx
= (dir
== PF_OUT
);
9179 r
->packets
[dirndx
]++;
9180 r
->bytes
[dirndx
] += pd
.tot_len
;
9182 a
->packets
[dirndx
]++;
9183 a
->bytes
[dirndx
] += pd
.tot_len
;
9187 if (s
->nat_rule
.ptr
!= NULL
) {
9188 s
->nat_rule
.ptr
->packets
[dirndx
]++;
9189 s
->nat_rule
.ptr
->bytes
[dirndx
] += pd
.tot_len
;
9191 if (s
->src_node
!= NULL
) {
9192 s
->src_node
->packets
[dirndx
]++;
9193 s
->src_node
->bytes
[dirndx
] += pd
.tot_len
;
9195 if (s
->nat_src_node
!= NULL
) {
9196 s
->nat_src_node
->packets
[dirndx
]++;
9197 s
->nat_src_node
->bytes
[dirndx
] += pd
.tot_len
;
9199 dirndx
= (dir
== sk
->direction
) ? 0 : 1;
9200 s
->packets
[dirndx
]++;
9201 s
->bytes
[dirndx
] += pd
.tot_len
;
9204 nr
= (s
!= NULL
) ? s
->nat_rule
.ptr
: pd
.nat_rule
;
9208 * XXX: we need to make sure that the addresses
9209 * passed to pfr_update_stats() are the same than
9210 * the addresses used during matching (pfr_match)
9212 if (r
== &pf_default_rule
) {
9214 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9215 &pd
.baddr
: &pd
.naddr
;
9217 x
= (s
== NULL
|| sk
->direction
== dir
) ?
9218 &pd
.naddr
: &pd
.baddr
;
9220 if (x
== &pd
.baddr
|| s
== NULL
) {
9227 if (tr
->src
.addr
.type
== PF_ADDR_TABLE
)
9228 pfr_update_stats(tr
->src
.addr
.p
.tbl
, (sk
== NULL
||
9229 sk
->direction
== dir
) ? pd
.src
: pd
.dst
, pd
.af
,
9230 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9232 if (tr
->dst
.addr
.type
== PF_ADDR_TABLE
)
9233 pfr_update_stats(tr
->dst
.addr
.p
.tbl
, (sk
== NULL
||
9234 sk
->direction
== dir
) ? pd
.dst
: pd
.src
, pd
.af
,
9235 pd
.tot_len
, dir
== PF_OUT
, r
->action
== PF_PASS
,
9240 if (action
== PF_SYNPROXY_DROP
) {
9245 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9246 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9248 #ifndef NO_APPLE_EXTENSIONS
9259 if (action
== PF_SYNPROXY_DROP
) {
9264 if (action
== PF_PASS
) {
9266 h
= mtod(m
, struct ip6_hdr
*);
9269 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9270 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9273 if (action
!= PF_SYNPROXY_DROP
&& r
->rt
)
9274 /* pf_route6 can free the mbuf causing *m0 to become NULL */
9275 pf_route6(m0
, r
, dir
, kif
->pfik_ifp
, s
, &pd
);
9277 if (action
== PF_PASS
) {
9279 h
= mtod(m
, struct ip6_hdr
*);
9282 if (action
== PF_SYNPROXY_DROP
) {
9295 pf_check_congestion(struct ifqueue
*ifq
)
9302 pool_init(struct pool
*pp
, size_t size
, unsigned int align
, unsigned int ioff
,
9303 int flags
, const char *wchan
, void *palloc
)
9305 #pragma unused(align, ioff, flags, palloc)
9306 bzero(pp
, sizeof (*pp
));
9307 pp
->pool_zone
= zinit(size
, 1024 * size
, PAGE_SIZE
, wchan
);
9308 if (pp
->pool_zone
!= NULL
) {
9309 zone_change(pp
->pool_zone
, Z_EXPAND
, TRUE
);
9310 pp
->pool_hiwat
= pp
->pool_limit
= (unsigned int)-1;
9311 pp
->pool_name
= wchan
;
9315 /* Zones cannot be currently destroyed */
9317 pool_destroy(struct pool
*pp
)
9323 pool_sethiwat(struct pool
*pp
, int n
)
9325 pp
->pool_hiwat
= n
; /* Currently unused */
9329 pool_sethardlimit(struct pool
*pp
, int n
, const char *warnmess
, int ratecap
)
9331 #pragma unused(warnmess, ratecap)
9336 pool_get(struct pool
*pp
, int flags
)
9340 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9342 if (pp
->pool_count
> pp
->pool_limit
) {
9343 DPFPRINTF(PF_DEBUG_NOISY
,
9344 ("pf: pool %s hard limit reached (%d)\n",
9345 pp
->pool_name
!= NULL
? pp
->pool_name
: "unknown",
9351 buf
= zalloc_canblock(pp
->pool_zone
, (flags
& (PR_NOWAIT
| PR_WAITOK
)));
9354 VERIFY(pp
->pool_count
!= 0);
9360 pool_put(struct pool
*pp
, void *v
)
9362 lck_mtx_assert(pf_lock
, LCK_MTX_ASSERT_OWNED
);
9364 zfree(pp
->pool_zone
, v
);
9365 VERIFY(pp
->pool_count
!= 0);
9370 pf_find_mtag(struct mbuf
*m
)
9375 if ((mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
9376 KERNEL_TAG_TYPE_PF
, NULL
)) == NULL
)
9379 return ((struct pf_mtag
*)(mtag
+ 1));
9381 if (!(m
->m_flags
& M_PKTHDR
))
9384 return (&m
->m_pkthdr
.pf_mtag
);
9385 #endif /* PF_PKTHDR */
9389 pf_get_mtag(struct mbuf
*m
)
9394 if ((mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_PF
,
9396 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_PF
,
9397 sizeof (struct pf_mtag
), M_NOWAIT
);
9400 bzero(mtag
+ 1, sizeof (struct pf_mtag
));
9401 m_tag_prepend(m
, mtag
);
9403 return ((struct pf_mtag
*)(mtag
+ 1));
9405 return (pf_find_mtag(m
));
9406 #endif /* PF_PKTHDR */
9410 pf_time_second(void)
9419 hook_establish(struct hook_desc_head
*head
, int tail
, hook_fn_t fn
, void *arg
)
9421 struct hook_desc
*hd
;
9423 hd
= _MALLOC(sizeof(*hd
), M_DEVBUF
, M_WAITOK
);
9430 TAILQ_INSERT_TAIL(head
, hd
, hd_list
);
9432 TAILQ_INSERT_HEAD(head
, hd
, hd_list
);
9438 hook_runloop(struct hook_desc_head
*head
, int flags
)
9440 struct hook_desc
*hd
;
9442 if (!(flags
& HOOK_REMOVE
)) {
9443 if (!(flags
& HOOK_ABORT
))
9444 TAILQ_FOREACH(hd
, head
, hd_list
)
9445 hd
->hd_fn(hd
->hd_arg
);
9447 while (!!(hd
= TAILQ_FIRST(head
))) {
9448 TAILQ_REMOVE(head
, hd
, hd_list
);
9449 if (!(flags
& HOOK_ABORT
))
9450 hd
->hd_fn(hd
->hd_arg
);
9451 if (flags
& HOOK_FREE
)
9452 _FREE(hd
, M_DEVBUF
);