1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Daniel Hartmeier 5 * Copyright (c) 2002 - 2008 Henning Brauer 6 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * - Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * - Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Effort sponsored in part by the Defense Advanced Research Projects 34 * Agency (DARPA) and Air Force Research Laboratory, Air Force 35 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 36 * 37 * $OpenBSD: pf.c,v 1.634 2009/02/27 12:37:45 henning Exp $ 38 */ 39 40 #include <sys/cdefs.h> 41 #include "opt_bpf.h" 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 #include "opt_pf.h" 45 #include "opt_sctp.h" 46 47 #include <sys/param.h> 48 #include <sys/bus.h> 49 #include <sys/endian.h> 50 #include <sys/gsb_crc32.h> 51 #include <sys/hash.h> 52 #include <sys/interrupt.h> 53 #include <sys/kernel.h> 54 #include <sys/kthread.h> 55 #include <sys/limits.h> 56 #include <sys/mbuf.h> 57 #include <sys/random.h> 58 #include <sys/refcount.h> 59 #include <sys/sdt.h> 60 #include <sys/socket.h> 61 #include <sys/sysctl.h> 62 #include <sys/taskqueue.h> 63 #include <sys/ucred.h> 64 65 #include <crypto/sha2/sha512.h> 66 67 #include <net/if.h> 68 #include <net/if_var.h> 69 #include <net/if_private.h> 70 #include <net/if_types.h> 71 #include <net/if_vlan_var.h> 72 #include <net/route.h> 73 #include <net/route/nhop.h> 74 #include <net/vnet.h> 75 76 #include <net/pfil.h> 77 #include <net/pfvar.h> 78 #include <net/if_pflog.h> 79 #include <net/if_pfsync.h> 80 81 #include <netinet/in_pcb.h> 82 #include <netinet/in_var.h> 83 #include <netinet/in_fib.h> 84 #include <netinet/ip.h> 85 #include <netinet/ip_fw.h> 86 #include <netinet/ip_icmp.h> 87 #include <netinet/icmp_var.h> 88 #include <netinet/ip_var.h> 89 #include <netinet/tcp.h> 90 #include <netinet/tcp_fsm.h> 91 #include <netinet/tcp_seq.h> 92 #include <netinet/tcp_timer.h> 93 #include <netinet/tcp_var.h> 94 #include <netinet/udp.h> 95 #include <netinet/udp_var.h> 96 97 /* dummynet */ 98 #include <netinet/ip_dummynet.h> 99 #include <netinet/ip_fw.h> 100 #include <netpfil/ipfw/dn_heap.h> 101 #include <netpfil/ipfw/ip_fw_private.h> 102 #include <netpfil/ipfw/ip_dn_private.h> 103 104 #ifdef INET6 105 #include <netinet/ip6.h> 106 #include <netinet/icmp6.h> 107 #include <netinet6/nd6.h> 108 #include <netinet6/ip6_var.h> 109 #include <netinet6/in6_pcb.h> 110 #include <netinet6/in6_fib.h> 111 #include <netinet6/scope6_var.h> 112 #endif /* INET6 */ 113 114 #include <netinet/sctp_header.h> 115 #include <netinet/sctp_crc32.h> 116 117 #include <netipsec/ah.h> 118 119 #include <machine/in_cksum.h> 120 #include <security/mac/mac_framework.h> 121 122 SDT_PROVIDER_DEFINE(pf); 123 SDT_PROBE_DEFINE2(pf, , test, reason_set, "int", "int"); 124 SDT_PROBE_DEFINE4(pf, ip, test, done, "int", "int", "struct pf_krule *", 125 "struct pf_kstate *"); 126 SDT_PROBE_DEFINE5(pf, ip, state, lookup, "struct pfi_kkif *", 127 "struct pf_state_key_cmp *", "int", "struct pf_pdesc *", 128 "struct pf_kstate *"); 129 SDT_PROBE_DEFINE2(pf, ip, , bound_iface, "struct pf_kstate *", 130 "struct pfi_kkif *"); 131 SDT_PROBE_DEFINE4(pf, ip, route_to, entry, "struct mbuf *", 132 "struct pf_pdesc *", "struct pf_kstate *", "struct ifnet *"); 133 SDT_PROBE_DEFINE1(pf, ip, route_to, drop, "int"); 134 SDT_PROBE_DEFINE2(pf, ip, route_to, output, "struct ifnet *", "int"); 135 SDT_PROBE_DEFINE4(pf, ip6, route_to, entry, "struct mbuf *", 136 "struct pf_pdesc *", "struct pf_kstate *", "struct ifnet *"); 137 SDT_PROBE_DEFINE1(pf, ip6, route_to, drop, "int"); 138 SDT_PROBE_DEFINE2(pf, ip6, route_to, output, "struct ifnet *", "int"); 139 SDT_PROBE_DEFINE4(pf, sctp, multihome, test, "struct pfi_kkif *", 140 "struct pf_krule *", "struct mbuf *", "int"); 141 SDT_PROBE_DEFINE2(pf, sctp, multihome, add, "uint32_t", 142 "struct pf_sctp_source *"); 143 SDT_PROBE_DEFINE3(pf, sctp, multihome, remove, "uint32_t", 144 "struct pf_kstate *", "struct pf_sctp_source *"); 145 SDT_PROBE_DEFINE4(pf, sctp, multihome_scan, entry, "int", 146 "int", "struct pf_pdesc *", "int"); 147 SDT_PROBE_DEFINE2(pf, sctp, multihome_scan, param, "uint16_t", "uint16_t"); 148 SDT_PROBE_DEFINE2(pf, sctp, multihome_scan, ipv4, "struct in_addr *", 149 "int"); 150 SDT_PROBE_DEFINE2(pf, sctp, multihome_scan, ipv6, "struct in_addr6 *", 151 "int"); 152 153 SDT_PROBE_DEFINE3(pf, eth, test_rule, entry, "int", "struct ifnet *", 154 "struct mbuf *"); 155 SDT_PROBE_DEFINE2(pf, eth, test_rule, test, "int", "struct pf_keth_rule *"); 156 SDT_PROBE_DEFINE3(pf, eth, test_rule, mismatch, 157 "int", "struct pf_keth_rule *", "char *"); 158 SDT_PROBE_DEFINE2(pf, eth, test_rule, match, "int", "struct pf_keth_rule *"); 159 SDT_PROBE_DEFINE2(pf, eth, test_rule, final_match, 160 "int", "struct pf_keth_rule *"); 161 SDT_PROBE_DEFINE2(pf, purge, state, rowcount, "int", "size_t"); 162 SDT_PROBE_DEFINE2(pf, , log, log, "int", "const char *"); 163 164 /* 165 * Global variables 166 */ 167 168 /* state tables */ 169 VNET_DEFINE(struct pf_altqqueue, pf_altqs[4]); 170 VNET_DEFINE(struct pf_kpalist, pf_pabuf[3]); 171 VNET_DEFINE(struct pf_altqqueue *, pf_altqs_active); 172 VNET_DEFINE(struct pf_altqqueue *, pf_altq_ifs_active); 173 VNET_DEFINE(struct pf_altqqueue *, pf_altqs_inactive); 174 VNET_DEFINE(struct pf_altqqueue *, pf_altq_ifs_inactive); 175 VNET_DEFINE(struct pf_kstatus, pf_status); 176 177 VNET_DEFINE(u_int32_t, ticket_altqs_active); 178 VNET_DEFINE(u_int32_t, ticket_altqs_inactive); 179 VNET_DEFINE(int, altqs_inactive_open); 180 VNET_DEFINE(u_int32_t, ticket_pabuf); 181 182 static const int PF_HDR_LIMIT = 20; /* arbitrary limit */ 183 184 VNET_DEFINE(SHA512_CTX, pf_tcp_secret_ctx); 185 #define V_pf_tcp_secret_ctx VNET(pf_tcp_secret_ctx) 186 VNET_DEFINE(u_char, pf_tcp_secret[16]); 187 #define V_pf_tcp_secret VNET(pf_tcp_secret) 188 VNET_DEFINE(int, pf_tcp_secret_init); 189 #define V_pf_tcp_secret_init VNET(pf_tcp_secret_init) 190 VNET_DEFINE(int, pf_tcp_iss_off); 191 #define V_pf_tcp_iss_off VNET(pf_tcp_iss_off) 192 VNET_DECLARE(int, pf_vnet_active); 193 #define V_pf_vnet_active VNET(pf_vnet_active) 194 195 VNET_DEFINE_STATIC(uint32_t, pf_purge_idx); 196 #define V_pf_purge_idx VNET(pf_purge_idx) 197 198 #ifdef PF_WANT_32_TO_64_COUNTER 199 VNET_DEFINE_STATIC(uint32_t, pf_counter_periodic_iter); 200 #define V_pf_counter_periodic_iter VNET(pf_counter_periodic_iter) 201 202 VNET_DEFINE(struct allrulelist_head, pf_allrulelist); 203 VNET_DEFINE(size_t, pf_allrulecount); 204 VNET_DEFINE(struct pf_krule *, pf_rulemarker); 205 #endif 206 207 #define PF_SCTP_MAX_ENDPOINTS 8 208 209 struct pf_sctp_endpoint; 210 RB_HEAD(pf_sctp_endpoints, pf_sctp_endpoint); 211 struct pf_sctp_source { 212 sa_family_t af; 213 struct pf_addr addr; 214 TAILQ_ENTRY(pf_sctp_source) entry; 215 }; 216 TAILQ_HEAD(pf_sctp_sources, pf_sctp_source); 217 struct pf_sctp_endpoint 218 { 219 uint32_t v_tag; 220 struct pf_sctp_sources sources; 221 RB_ENTRY(pf_sctp_endpoint) entry; 222 }; 223 static int 224 pf_sctp_endpoint_compare(struct pf_sctp_endpoint *a, struct pf_sctp_endpoint *b) 225 { 226 return (a->v_tag - b->v_tag); 227 } 228 RB_PROTOTYPE(pf_sctp_endpoints, pf_sctp_endpoint, entry, pf_sctp_endpoint_compare); 229 RB_GENERATE(pf_sctp_endpoints, pf_sctp_endpoint, entry, pf_sctp_endpoint_compare); 230 VNET_DEFINE_STATIC(struct pf_sctp_endpoints, pf_sctp_endpoints); 231 #define V_pf_sctp_endpoints VNET(pf_sctp_endpoints) 232 static struct mtx_padalign pf_sctp_endpoints_mtx; 233 MTX_SYSINIT(pf_sctp_endpoints_mtx, &pf_sctp_endpoints_mtx, "SCTP endpoints", MTX_DEF); 234 #define PF_SCTP_ENDPOINTS_LOCK() mtx_lock(&pf_sctp_endpoints_mtx) 235 #define PF_SCTP_ENDPOINTS_UNLOCK() mtx_unlock(&pf_sctp_endpoints_mtx) 236 237 /* 238 * Queue for pf_intr() sends. 239 */ 240 static MALLOC_DEFINE(M_PFTEMP, "pf_temp", "pf(4) temporary allocations"); 241 struct pf_send_entry { 242 STAILQ_ENTRY(pf_send_entry) pfse_next; 243 struct mbuf *pfse_m; 244 enum { 245 PFSE_IP, 246 PFSE_IP6, 247 PFSE_ICMP, 248 PFSE_ICMP6, 249 } pfse_type; 250 struct { 251 int type; 252 int code; 253 int mtu; 254 } icmpopts; 255 }; 256 257 STAILQ_HEAD(pf_send_head, pf_send_entry); 258 VNET_DEFINE_STATIC(struct pf_send_head, pf_sendqueue); 259 #define V_pf_sendqueue VNET(pf_sendqueue) 260 261 static struct mtx_padalign pf_sendqueue_mtx; 262 MTX_SYSINIT(pf_sendqueue_mtx, &pf_sendqueue_mtx, "pf send queue", MTX_DEF); 263 #define PF_SENDQ_LOCK() mtx_lock(&pf_sendqueue_mtx) 264 #define PF_SENDQ_UNLOCK() mtx_unlock(&pf_sendqueue_mtx) 265 266 /* 267 * Queue for pf_overload_task() tasks. 268 */ 269 struct pf_overload_entry { 270 SLIST_ENTRY(pf_overload_entry) next; 271 struct pf_addr addr; 272 sa_family_t af; 273 uint8_t dir; 274 struct pf_krule *rule; 275 }; 276 277 SLIST_HEAD(pf_overload_head, pf_overload_entry); 278 VNET_DEFINE_STATIC(struct pf_overload_head, pf_overloadqueue); 279 #define V_pf_overloadqueue VNET(pf_overloadqueue) 280 VNET_DEFINE_STATIC(struct task, pf_overloadtask); 281 #define V_pf_overloadtask VNET(pf_overloadtask) 282 283 static struct mtx_padalign pf_overloadqueue_mtx; 284 MTX_SYSINIT(pf_overloadqueue_mtx, &pf_overloadqueue_mtx, 285 "pf overload/flush queue", MTX_DEF); 286 #define PF_OVERLOADQ_LOCK() mtx_lock(&pf_overloadqueue_mtx) 287 #define PF_OVERLOADQ_UNLOCK() mtx_unlock(&pf_overloadqueue_mtx) 288 289 VNET_DEFINE(struct pf_krulequeue, pf_unlinked_rules); 290 struct mtx_padalign pf_unlnkdrules_mtx; 291 MTX_SYSINIT(pf_unlnkdrules_mtx, &pf_unlnkdrules_mtx, "pf unlinked rules", 292 MTX_DEF); 293 294 struct sx pf_config_lock; 295 SX_SYSINIT(pf_config_lock, &pf_config_lock, "pf config"); 296 297 struct mtx_padalign pf_table_stats_lock; 298 MTX_SYSINIT(pf_table_stats_lock, &pf_table_stats_lock, "pf table stats", 299 MTX_DEF); 300 301 VNET_DEFINE_STATIC(uma_zone_t, pf_sources_z); 302 #define V_pf_sources_z VNET(pf_sources_z) 303 uma_zone_t pf_mtag_z; 304 VNET_DEFINE(uma_zone_t, pf_state_z); 305 VNET_DEFINE(uma_zone_t, pf_state_key_z); 306 VNET_DEFINE(uma_zone_t, pf_udp_mapping_z); 307 308 VNET_DEFINE(struct unrhdr64, pf_stateid); 309 310 static void pf_src_tree_remove_state(struct pf_kstate *); 311 static int pf_check_threshold(struct pf_kthreshold *); 312 313 static void pf_change_ap(struct pf_pdesc *, struct pf_addr *, u_int16_t *, 314 struct pf_addr *, u_int16_t); 315 static int pf_modulate_sack(struct pf_pdesc *, 316 struct tcphdr *, struct pf_state_peer *); 317 int pf_icmp_mapping(struct pf_pdesc *, u_int8_t, int *, 318 u_int16_t *, u_int16_t *); 319 static void pf_change_icmp(struct pf_addr *, u_int16_t *, 320 struct pf_addr *, struct pf_addr *, u_int16_t, 321 u_int16_t *, u_int16_t *, u_int16_t *, 322 u_int16_t *, u_int8_t, sa_family_t); 323 int pf_change_icmp_af(struct mbuf *, int, 324 struct pf_pdesc *, struct pf_pdesc *, 325 struct pf_addr *, struct pf_addr *, sa_family_t, 326 sa_family_t); 327 int pf_translate_icmp_af(int, void *); 328 static void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t, 329 int, sa_family_t, struct pf_krule *, int); 330 static void pf_detach_state(struct pf_kstate *); 331 static int pf_state_key_attach(struct pf_state_key *, 332 struct pf_state_key *, struct pf_kstate *); 333 static void pf_state_key_detach(struct pf_kstate *, int); 334 static int pf_state_key_ctor(void *, int, void *, int); 335 static u_int32_t pf_tcp_iss(struct pf_pdesc *); 336 static __inline void pf_dummynet_flag_remove(struct mbuf *m, 337 struct pf_mtag *pf_mtag); 338 static int pf_dummynet(struct pf_pdesc *, struct pf_kstate *, 339 struct pf_krule *, struct mbuf **); 340 static int pf_dummynet_route(struct pf_pdesc *, 341 struct pf_kstate *, struct pf_krule *, 342 struct ifnet *, const struct sockaddr *, struct mbuf **); 343 static int pf_test_eth_rule(int, struct pfi_kkif *, 344 struct mbuf **); 345 static int pf_test_rule(struct pf_krule **, struct pf_kstate **, 346 struct pf_pdesc *, struct pf_krule **, 347 struct pf_kruleset **, u_short *, struct inpcb *); 348 static int pf_create_state(struct pf_krule *, 349 struct pf_test_ctx *, 350 struct pf_kstate **, u_int16_t, u_int16_t); 351 static int pf_state_key_addr_setup(struct pf_pdesc *, 352 struct pf_state_key_cmp *, int); 353 static int pf_tcp_track_full(struct pf_kstate *, 354 struct pf_pdesc *, u_short *, int *, 355 struct pf_state_peer *, struct pf_state_peer *, 356 u_int8_t, u_int8_t); 357 static int pf_tcp_track_sloppy(struct pf_kstate *, 358 struct pf_pdesc *, u_short *, 359 struct pf_state_peer *, struct pf_state_peer *, 360 u_int8_t, u_int8_t); 361 static int pf_test_state(struct pf_kstate **, struct pf_pdesc *, 362 u_short *); 363 int pf_icmp_state_lookup(struct pf_state_key_cmp *, 364 struct pf_pdesc *, struct pf_kstate **, 365 u_int16_t, u_int16_t, int, int *, int, int); 366 static int pf_test_state_icmp(struct pf_kstate **, 367 struct pf_pdesc *, u_short *); 368 static int pf_sctp_track(struct pf_kstate *, struct pf_pdesc *, 369 u_short *); 370 static void pf_sctp_multihome_detach_addr(const struct pf_kstate *); 371 static void pf_sctp_multihome_delayed(struct pf_pdesc *, 372 struct pfi_kkif *, struct pf_kstate *, int); 373 static u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t, 374 int, u_int16_t); 375 static int pf_check_proto_cksum(struct mbuf *, int, int, 376 u_int8_t, sa_family_t); 377 static int pf_walk_option(struct pf_pdesc *, struct ip *, 378 int, int, u_short *); 379 static int pf_walk_header(struct pf_pdesc *, struct ip *, u_short *); 380 #ifdef INET6 381 static int pf_walk_option6(struct pf_pdesc *, struct ip6_hdr *, 382 int, int, u_short *); 383 static int pf_walk_header6(struct pf_pdesc *, struct ip6_hdr *, 384 u_short *); 385 #endif 386 static void pf_print_state_parts(struct pf_kstate *, 387 struct pf_state_key *, struct pf_state_key *); 388 static int pf_patch_8(struct pf_pdesc *, u_int8_t *, u_int8_t, 389 bool); 390 static int pf_find_state(struct pf_pdesc *, 391 const struct pf_state_key_cmp *, struct pf_kstate **); 392 static bool pf_src_connlimit(struct pf_kstate *); 393 static int pf_match_rcvif(struct mbuf *, struct pf_krule *); 394 static void pf_counters_inc(int, struct pf_pdesc *, 395 struct pf_kstate *, struct pf_krule *, 396 struct pf_krule *); 397 static void pf_log_matches(struct pf_pdesc *, struct pf_krule *, 398 struct pf_krule *, struct pf_kruleset *, 399 struct pf_krule_slist *); 400 static void pf_overload_task(void *v, int pending); 401 static u_short pf_insert_src_node(struct pf_ksrc_node *[PF_SN_MAX], 402 struct pf_srchash *[PF_SN_MAX], struct pf_krule *, 403 struct pf_addr *, sa_family_t, struct pf_addr *, 404 struct pfi_kkif *, sa_family_t, pf_sn_types_t); 405 static u_int pf_purge_expired_states(u_int, int); 406 static void pf_purge_unlinked_rules(void); 407 static int pf_mtag_uminit(void *, int, int); 408 static void pf_mtag_free(struct m_tag *); 409 static void pf_packet_rework_nat(struct pf_pdesc *, int, 410 struct pf_state_key *); 411 #ifdef INET 412 static int pf_route(struct pf_krule *, 413 struct ifnet *, struct pf_kstate *, 414 struct pf_pdesc *, struct inpcb *); 415 #endif /* INET */ 416 #ifdef INET6 417 static void pf_change_a6(struct pf_addr *, u_int16_t *, 418 struct pf_addr *, u_int8_t); 419 static int pf_route6(struct pf_krule *, 420 struct ifnet *, struct pf_kstate *, 421 struct pf_pdesc *, struct inpcb *); 422 #endif /* INET6 */ 423 static __inline void pf_set_protostate(struct pf_kstate *, int, u_int8_t); 424 425 int in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len); 426 427 extern int pf_end_threads; 428 extern struct proc *pf_purge_proc; 429 430 VNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 431 432 #define PACKET_UNDO_NAT(_pd, _off, _s) \ 433 do { \ 434 struct pf_state_key *nk; \ 435 if ((pd->dir) == PF_OUT) \ 436 nk = (_s)->key[PF_SK_STACK]; \ 437 else \ 438 nk = (_s)->key[PF_SK_WIRE]; \ 439 pf_packet_rework_nat(_pd, _off, nk); \ 440 } while (0) 441 442 #define PACKET_LOOPED(pd) ((pd)->pf_mtag && \ 443 (pd)->pf_mtag->flags & PF_MTAG_FLAG_PACKET_LOOPED) 444 445 static struct pfi_kkif * 446 BOUND_IFACE(struct pf_kstate *st, struct pf_pdesc *pd) 447 { 448 struct pfi_kkif *k = pd->kif; 449 450 SDT_PROBE2(pf, ip, , bound_iface, st, k); 451 452 /* Floating unless otherwise specified. */ 453 if (! (st->rule->rule_flag & PFRULE_IFBOUND)) 454 return (V_pfi_all); 455 456 /* 457 * Initially set to all, because we don't know what interface we'll be 458 * sending this out when we create the state. 459 */ 460 if (st->rule->rt == PF_REPLYTO || (pd->af != pd->naf && st->direction == PF_IN)) 461 return (V_pfi_all); 462 463 /* 464 * If this state is created based on another state (e.g. SCTP 465 * multihome) always set it floating initially. We can't know for sure 466 * what interface the actual traffic for this state will come in on. 467 */ 468 if (pd->related_rule) 469 return (V_pfi_all); 470 471 /* Don't overrule the interface for states created on incoming packets. */ 472 if (st->direction == PF_IN) 473 return (k); 474 475 /* No route-to, so don't overrule. */ 476 if (st->act.rt != PF_ROUTETO) 477 return (k); 478 479 /* Bind to the route-to interface. */ 480 return (st->act.rt_kif); 481 } 482 483 #define STATE_INC_COUNTERS(s) \ 484 do { \ 485 struct pf_krule_item *mrm; \ 486 counter_u64_add(s->rule->states_cur, 1); \ 487 counter_u64_add(s->rule->states_tot, 1); \ 488 if (s->anchor != NULL) { \ 489 counter_u64_add(s->anchor->states_cur, 1); \ 490 counter_u64_add(s->anchor->states_tot, 1); \ 491 } \ 492 if (s->nat_rule != NULL) { \ 493 counter_u64_add(s->nat_rule->states_cur, 1);\ 494 counter_u64_add(s->nat_rule->states_tot, 1);\ 495 } \ 496 SLIST_FOREACH(mrm, &s->match_rules, entry) { \ 497 counter_u64_add(mrm->r->states_cur, 1); \ 498 counter_u64_add(mrm->r->states_tot, 1); \ 499 } \ 500 } while (0) 501 502 #define STATE_DEC_COUNTERS(s) \ 503 do { \ 504 struct pf_krule_item *mrm; \ 505 if (s->nat_rule != NULL) \ 506 counter_u64_add(s->nat_rule->states_cur, -1);\ 507 if (s->anchor != NULL) \ 508 counter_u64_add(s->anchor->states_cur, -1); \ 509 counter_u64_add(s->rule->states_cur, -1); \ 510 SLIST_FOREACH(mrm, &s->match_rules, entry) \ 511 counter_u64_add(mrm->r->states_cur, -1); \ 512 } while (0) 513 514 MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures"); 515 MALLOC_DEFINE(M_PF_RULE_ITEM, "pf_krule_item", "pf(4) rule items"); 516 VNET_DEFINE(struct pf_keyhash *, pf_keyhash); 517 VNET_DEFINE(struct pf_idhash *, pf_idhash); 518 VNET_DEFINE(struct pf_srchash *, pf_srchash); 519 VNET_DEFINE(struct pf_udpendpointhash *, pf_udpendpointhash); 520 VNET_DEFINE(struct pf_udpendpointmapping *, pf_udpendpointmapping); 521 522 SYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 523 "pf(4)"); 524 525 VNET_DEFINE(u_long, pf_hashmask); 526 VNET_DEFINE(u_long, pf_srchashmask); 527 VNET_DEFINE(u_long, pf_udpendpointhashmask); 528 VNET_DEFINE_STATIC(u_long, pf_hashsize); 529 #define V_pf_hashsize VNET(pf_hashsize) 530 VNET_DEFINE_STATIC(u_long, pf_srchashsize); 531 #define V_pf_srchashsize VNET(pf_srchashsize) 532 VNET_DEFINE_STATIC(u_long, pf_udpendpointhashsize); 533 #define V_pf_udpendpointhashsize VNET(pf_udpendpointhashsize) 534 u_long pf_ioctl_maxcount = 65535; 535 536 SYSCTL_ULONG(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 537 &VNET_NAME(pf_hashsize), 0, "Size of pf(4) states hashtable"); 538 SYSCTL_ULONG(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 539 &VNET_NAME(pf_srchashsize), 0, "Size of pf(4) source nodes hashtable"); 540 SYSCTL_ULONG(_net_pf, OID_AUTO, udpendpoint_hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 541 &VNET_NAME(pf_udpendpointhashsize), 0, "Size of pf(4) endpoint hashtable"); 542 SYSCTL_ULONG(_net_pf, OID_AUTO, request_maxcount, CTLFLAG_RWTUN, 543 &pf_ioctl_maxcount, 0, "Maximum number of tables, addresses, ... in a single ioctl() call"); 544 545 VNET_DEFINE(void *, pf_swi_cookie); 546 VNET_DEFINE(struct intr_event *, pf_swi_ie); 547 548 VNET_DEFINE(uint32_t, pf_hashseed); 549 #define V_pf_hashseed VNET(pf_hashseed) 550 551 static void 552 pf_sctp_checksum(struct mbuf *m, int off) 553 { 554 uint32_t sum = 0; 555 556 /* Zero out the checksum, to enable recalculation. */ 557 m_copyback(m, off + offsetof(struct sctphdr, checksum), 558 sizeof(sum), (caddr_t)&sum); 559 560 sum = sctp_calculate_cksum(m, off); 561 562 m_copyback(m, off + offsetof(struct sctphdr, checksum), 563 sizeof(sum), (caddr_t)&sum); 564 } 565 566 int 567 pf_addr_cmp(struct pf_addr *a, struct pf_addr *b, sa_family_t af) 568 { 569 570 switch (af) { 571 #ifdef INET 572 case AF_INET: 573 if (a->addr32[0] > b->addr32[0]) 574 return (1); 575 if (a->addr32[0] < b->addr32[0]) 576 return (-1); 577 break; 578 #endif /* INET */ 579 #ifdef INET6 580 case AF_INET6: 581 if (a->addr32[3] > b->addr32[3]) 582 return (1); 583 if (a->addr32[3] < b->addr32[3]) 584 return (-1); 585 if (a->addr32[2] > b->addr32[2]) 586 return (1); 587 if (a->addr32[2] < b->addr32[2]) 588 return (-1); 589 if (a->addr32[1] > b->addr32[1]) 590 return (1); 591 if (a->addr32[1] < b->addr32[1]) 592 return (-1); 593 if (a->addr32[0] > b->addr32[0]) 594 return (1); 595 if (a->addr32[0] < b->addr32[0]) 596 return (-1); 597 break; 598 #endif /* INET6 */ 599 default: 600 unhandled_af(af); 601 } 602 return (0); 603 } 604 605 static bool 606 pf_is_loopback(sa_family_t af, struct pf_addr *addr) 607 { 608 switch (af) { 609 #ifdef INET 610 case AF_INET: 611 return IN_LOOPBACK(ntohl(addr->v4.s_addr)); 612 #endif /* INET */ 613 case AF_INET6: 614 return IN6_IS_ADDR_LOOPBACK(&addr->v6); 615 default: 616 unhandled_af(af); 617 } 618 } 619 620 static void 621 pf_packet_rework_nat(struct pf_pdesc *pd, int off, struct pf_state_key *nk) 622 { 623 624 switch (pd->virtual_proto) { 625 case IPPROTO_TCP: { 626 struct tcphdr *th = &pd->hdr.tcp; 627 628 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af)) 629 pf_change_ap(pd, pd->src, &th->th_sport, 630 &nk->addr[pd->sidx], nk->port[pd->sidx]); 631 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af)) 632 pf_change_ap(pd, pd->dst, &th->th_dport, 633 &nk->addr[pd->didx], nk->port[pd->didx]); 634 m_copyback(pd->m, off, sizeof(*th), (caddr_t)th); 635 break; 636 } 637 case IPPROTO_UDP: { 638 struct udphdr *uh = &pd->hdr.udp; 639 640 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af)) 641 pf_change_ap(pd, pd->src, &uh->uh_sport, 642 &nk->addr[pd->sidx], nk->port[pd->sidx]); 643 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af)) 644 pf_change_ap(pd, pd->dst, &uh->uh_dport, 645 &nk->addr[pd->didx], nk->port[pd->didx]); 646 m_copyback(pd->m, off, sizeof(*uh), (caddr_t)uh); 647 break; 648 } 649 case IPPROTO_SCTP: { 650 struct sctphdr *sh = &pd->hdr.sctp; 651 652 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af)) { 653 pf_change_ap(pd, pd->src, &sh->src_port, 654 &nk->addr[pd->sidx], nk->port[pd->sidx]); 655 } 656 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af)) { 657 pf_change_ap(pd, pd->dst, &sh->dest_port, 658 &nk->addr[pd->didx], nk->port[pd->didx]); 659 } 660 661 break; 662 } 663 case IPPROTO_ICMP: { 664 struct icmp *ih = &pd->hdr.icmp; 665 666 if (nk->port[pd->sidx] != ih->icmp_id) { 667 pd->hdr.icmp.icmp_cksum = pf_cksum_fixup( 668 ih->icmp_cksum, ih->icmp_id, 669 nk->port[pd->sidx], 0); 670 ih->icmp_id = nk->port[pd->sidx]; 671 pd->sport = &ih->icmp_id; 672 673 m_copyback(pd->m, off, ICMP_MINLEN, (caddr_t)ih); 674 } 675 /* FALLTHROUGH */ 676 } 677 default: 678 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af)) { 679 switch (pd->af) { 680 case AF_INET: 681 pf_change_a(&pd->src->v4.s_addr, 682 pd->ip_sum, nk->addr[pd->sidx].v4.s_addr, 683 0); 684 break; 685 case AF_INET6: 686 pf_addrcpy(pd->src, &nk->addr[pd->sidx], 687 pd->af); 688 break; 689 default: 690 unhandled_af(pd->af); 691 } 692 } 693 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af)) { 694 switch (pd->af) { 695 case AF_INET: 696 pf_change_a(&pd->dst->v4.s_addr, 697 pd->ip_sum, nk->addr[pd->didx].v4.s_addr, 698 0); 699 break; 700 case AF_INET6: 701 pf_addrcpy(pd->dst, &nk->addr[pd->didx], 702 pd->af); 703 break; 704 default: 705 unhandled_af(pd->af); 706 } 707 } 708 break; 709 } 710 } 711 712 static __inline uint32_t 713 pf_hashkey(const struct pf_state_key *sk) 714 { 715 uint32_t h; 716 717 h = murmur3_32_hash32((const uint32_t *)sk, 718 sizeof(struct pf_state_key_cmp)/sizeof(uint32_t), 719 V_pf_hashseed); 720 721 return (h & V_pf_hashmask); 722 } 723 724 __inline uint32_t 725 pf_hashsrc(struct pf_addr *addr, sa_family_t af) 726 { 727 uint32_t h; 728 729 switch (af) { 730 case AF_INET: 731 h = murmur3_32_hash32((uint32_t *)&addr->v4, 732 sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed); 733 break; 734 case AF_INET6: 735 h = murmur3_32_hash32((uint32_t *)&addr->v6, 736 sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed); 737 break; 738 default: 739 unhandled_af(af); 740 } 741 742 return (h & V_pf_srchashmask); 743 } 744 745 static inline uint32_t 746 pf_hashudpendpoint(struct pf_udp_endpoint *endpoint) 747 { 748 uint32_t h; 749 750 h = murmur3_32_hash32((uint32_t *)endpoint, 751 sizeof(struct pf_udp_endpoint_cmp)/sizeof(uint32_t), 752 V_pf_hashseed); 753 return (h & V_pf_udpendpointhashmask); 754 } 755 756 #ifdef ALTQ 757 static int 758 pf_state_hash(struct pf_kstate *s) 759 { 760 u_int32_t hv = (intptr_t)s / sizeof(*s); 761 762 hv ^= crc32(&s->src, sizeof(s->src)); 763 hv ^= crc32(&s->dst, sizeof(s->dst)); 764 if (hv == 0) 765 hv = 1; 766 return (hv); 767 } 768 #endif /* ALTQ */ 769 770 static __inline void 771 pf_set_protostate(struct pf_kstate *s, int which, u_int8_t newstate) 772 { 773 if (which == PF_PEER_DST || which == PF_PEER_BOTH) 774 s->dst.state = newstate; 775 if (which == PF_PEER_DST) 776 return; 777 if (s->src.state == newstate) 778 return; 779 if (s->creatorid == V_pf_status.hostid && 780 s->key[PF_SK_STACK] != NULL && 781 s->key[PF_SK_STACK]->proto == IPPROTO_TCP && 782 !(TCPS_HAVEESTABLISHED(s->src.state) || 783 s->src.state == TCPS_CLOSED) && 784 (TCPS_HAVEESTABLISHED(newstate) || newstate == TCPS_CLOSED)) 785 atomic_add_32(&V_pf_status.states_halfopen, -1); 786 787 s->src.state = newstate; 788 } 789 790 bool 791 pf_init_threshold(struct pf_kthreshold *threshold, 792 u_int32_t limit, u_int32_t seconds) 793 { 794 threshold->limit = limit; 795 threshold->seconds = seconds; 796 threshold->cr = counter_rate_alloc(M_NOWAIT, seconds); 797 798 return (threshold->cr != NULL); 799 } 800 801 static int 802 pf_check_threshold(struct pf_kthreshold *threshold) 803 { 804 return (counter_ratecheck(threshold->cr, threshold->limit) < 0); 805 } 806 807 static bool 808 pf_src_connlimit(struct pf_kstate *state) 809 { 810 struct pf_overload_entry *pfoe; 811 struct pf_ksrc_node *src_node = state->sns[PF_SN_LIMIT]; 812 bool limited = false; 813 814 PF_STATE_LOCK_ASSERT(state); 815 PF_SRC_NODE_LOCK(src_node); 816 817 src_node->conn++; 818 state->src.tcp_est = 1; 819 820 if (state->rule->max_src_conn && 821 state->rule->max_src_conn < 822 src_node->conn) { 823 counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONN], 1); 824 limited = true; 825 } 826 827 if (state->rule->max_src_conn_rate.limit && 828 pf_check_threshold(&src_node->conn_rate)) { 829 counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONNRATE], 1); 830 limited = true; 831 } 832 833 if (!limited) 834 goto done; 835 836 /* Kill this state. */ 837 state->timeout = PFTM_PURGE; 838 pf_set_protostate(state, PF_PEER_BOTH, TCPS_CLOSED); 839 840 if (state->rule->overload_tbl == NULL) 841 goto done; 842 843 /* Schedule overloading and flushing task. */ 844 pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT); 845 if (pfoe == NULL) 846 goto done; /* too bad :( */ 847 848 bcopy(&src_node->addr, &pfoe->addr, sizeof(pfoe->addr)); 849 pfoe->af = state->key[PF_SK_WIRE]->af; 850 pfoe->rule = state->rule; 851 pfoe->dir = state->direction; 852 PF_OVERLOADQ_LOCK(); 853 SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next); 854 PF_OVERLOADQ_UNLOCK(); 855 taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask); 856 857 done: 858 PF_SRC_NODE_UNLOCK(src_node); 859 return (limited); 860 } 861 862 static void 863 pf_overload_task(void *v, int pending) 864 { 865 struct pf_overload_head queue; 866 struct pfr_addr p; 867 struct pf_overload_entry *pfoe, *pfoe1; 868 uint32_t killed = 0; 869 870 CURVNET_SET((struct vnet *)v); 871 872 PF_OVERLOADQ_LOCK(); 873 queue = V_pf_overloadqueue; 874 SLIST_INIT(&V_pf_overloadqueue); 875 PF_OVERLOADQ_UNLOCK(); 876 877 bzero(&p, sizeof(p)); 878 SLIST_FOREACH(pfoe, &queue, next) { 879 counter_u64_add(V_pf_status.lcounters[LCNT_OVERLOAD_TABLE], 1); 880 if (V_pf_status.debug >= PF_DEBUG_MISC) { 881 printf("%s: blocking address ", __func__); 882 pf_print_host(&pfoe->addr, 0, pfoe->af); 883 printf("\n"); 884 } 885 886 p.pfra_af = pfoe->af; 887 switch (pfoe->af) { 888 #ifdef INET 889 case AF_INET: 890 p.pfra_net = 32; 891 p.pfra_ip4addr = pfoe->addr.v4; 892 break; 893 #endif /* INET */ 894 #ifdef INET6 895 case AF_INET6: 896 p.pfra_net = 128; 897 p.pfra_ip6addr = pfoe->addr.v6; 898 break; 899 #endif /* INET6 */ 900 default: 901 unhandled_af(pfoe->af); 902 } 903 904 PF_RULES_WLOCK(); 905 pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second); 906 PF_RULES_WUNLOCK(); 907 } 908 909 /* 910 * Remove those entries, that don't need flushing. 911 */ 912 SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 913 if (pfoe->rule->flush == 0) { 914 SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next); 915 free(pfoe, M_PFTEMP); 916 } else 917 counter_u64_add( 918 V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH], 1); 919 920 /* If nothing to flush, return. */ 921 if (SLIST_EMPTY(&queue)) { 922 CURVNET_RESTORE(); 923 return; 924 } 925 926 for (int i = 0; i <= V_pf_hashmask; i++) { 927 struct pf_idhash *ih = &V_pf_idhash[i]; 928 struct pf_state_key *sk; 929 struct pf_kstate *s; 930 931 PF_HASHROW_LOCK(ih); 932 LIST_FOREACH(s, &ih->states, entry) { 933 sk = s->key[PF_SK_WIRE]; 934 SLIST_FOREACH(pfoe, &queue, next) 935 if (sk->af == pfoe->af && 936 ((pfoe->rule->flush & PF_FLUSH_GLOBAL) || 937 pfoe->rule == s->rule) && 938 ((pfoe->dir == PF_OUT && 939 PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) || 940 (pfoe->dir == PF_IN && 941 PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) { 942 s->timeout = PFTM_PURGE; 943 pf_set_protostate(s, PF_PEER_BOTH, TCPS_CLOSED); 944 killed++; 945 } 946 } 947 PF_HASHROW_UNLOCK(ih); 948 } 949 SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 950 free(pfoe, M_PFTEMP); 951 if (V_pf_status.debug >= PF_DEBUG_MISC) 952 printf("%s: %u states killed", __func__, killed); 953 954 CURVNET_RESTORE(); 955 } 956 957 /* 958 * On node found always returns locked. On not found its configurable. 959 */ 960 struct pf_ksrc_node * 961 pf_find_src_node(struct pf_addr *src, struct pf_krule *rule, sa_family_t af, 962 struct pf_srchash **sh, pf_sn_types_t sn_type, bool returnlocked) 963 { 964 struct pf_ksrc_node *n; 965 966 counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_SEARCH], 1); 967 968 *sh = &V_pf_srchash[pf_hashsrc(src, af)]; 969 PF_HASHROW_LOCK(*sh); 970 LIST_FOREACH(n, &(*sh)->nodes, entry) 971 if (n->rule == rule && n->af == af && n->type == sn_type && 972 ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) || 973 (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0))) 974 break; 975 976 if (n == NULL && !returnlocked) 977 PF_HASHROW_UNLOCK(*sh); 978 979 return (n); 980 } 981 982 bool 983 pf_src_node_exists(struct pf_ksrc_node **sn, struct pf_srchash *sh) 984 { 985 struct pf_ksrc_node *cur; 986 987 if ((*sn) == NULL) 988 return (false); 989 990 KASSERT(sh != NULL, ("%s: sh is NULL", __func__)); 991 992 counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_SEARCH], 1); 993 PF_HASHROW_LOCK(sh); 994 LIST_FOREACH(cur, &(sh->nodes), entry) { 995 if (cur == (*sn) && 996 cur->expire != 1) /* Ignore nodes being killed */ 997 return (true); 998 } 999 PF_HASHROW_UNLOCK(sh); 1000 (*sn) = NULL; 1001 return (false); 1002 } 1003 1004 static void 1005 pf_free_src_node(struct pf_ksrc_node *sn) 1006 { 1007 1008 for (int i = 0; i < 2; i++) { 1009 counter_u64_free(sn->bytes[i]); 1010 counter_u64_free(sn->packets[i]); 1011 } 1012 counter_rate_free(sn->conn_rate.cr); 1013 uma_zfree(V_pf_sources_z, sn); 1014 } 1015 1016 static u_short 1017 pf_insert_src_node(struct pf_ksrc_node *sns[PF_SN_MAX], 1018 struct pf_srchash *snhs[PF_SN_MAX], struct pf_krule *rule, 1019 struct pf_addr *src, sa_family_t af, struct pf_addr *raddr, 1020 struct pfi_kkif *rkif, sa_family_t raf, pf_sn_types_t sn_type) 1021 { 1022 u_short reason = 0; 1023 struct pf_krule *r_track = rule; 1024 struct pf_ksrc_node **sn = &(sns[sn_type]); 1025 struct pf_srchash **sh = &(snhs[sn_type]); 1026 1027 KASSERT(sn_type != PF_SN_LIMIT || (raddr == NULL && rkif == NULL), 1028 ("%s: raddr and rkif must be NULL for PF_SN_LIMIT", __func__)); 1029 1030 KASSERT(sn_type != PF_SN_LIMIT || (rule->rule_flag & PFRULE_SRCTRACK), 1031 ("%s: PF_SN_LIMIT only valid for rules with PFRULE_SRCTRACK", __func__)); 1032 1033 /* 1034 * XXX: There could be a KASSERT for 1035 * sn_type == PF_SN_LIMIT || (pool->opts & PF_POOL_STICKYADDR) 1036 * but we'd need to pass pool *only* for this KASSERT. 1037 */ 1038 1039 if ( (rule->rule_flag & PFRULE_SRCTRACK) && 1040 !(rule->rule_flag & PFRULE_RULESRCTRACK)) 1041 r_track = &V_pf_default_rule; 1042 1043 /* 1044 * Request the sh to always be locked, as we might insert a new sn. 1045 */ 1046 if (*sn == NULL) 1047 *sn = pf_find_src_node(src, r_track, af, sh, sn_type, true); 1048 1049 if (*sn == NULL) { 1050 PF_HASHROW_ASSERT(*sh); 1051 1052 if (sn_type == PF_SN_LIMIT && rule->max_src_nodes && 1053 counter_u64_fetch(r_track->src_nodes[sn_type]) >= rule->max_src_nodes) { 1054 counter_u64_add(V_pf_status.lcounters[LCNT_SRCNODES], 1); 1055 reason = PFRES_SRCLIMIT; 1056 goto done; 1057 } 1058 1059 (*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO); 1060 if ((*sn) == NULL) { 1061 reason = PFRES_MEMORY; 1062 goto done; 1063 } 1064 1065 for (int i = 0; i < 2; i++) { 1066 (*sn)->bytes[i] = counter_u64_alloc(M_NOWAIT); 1067 (*sn)->packets[i] = counter_u64_alloc(M_NOWAIT); 1068 1069 if ((*sn)->bytes[i] == NULL || (*sn)->packets[i] == NULL) { 1070 pf_free_src_node(*sn); 1071 reason = PFRES_MEMORY; 1072 goto done; 1073 } 1074 } 1075 1076 if (sn_type == PF_SN_LIMIT) 1077 if (! pf_init_threshold(&(*sn)->conn_rate, 1078 rule->max_src_conn_rate.limit, 1079 rule->max_src_conn_rate.seconds)) { 1080 pf_free_src_node(*sn); 1081 reason = PFRES_MEMORY; 1082 goto done; 1083 } 1084 1085 MPASS((*sn)->lock == NULL); 1086 (*sn)->lock = &(*sh)->lock; 1087 1088 (*sn)->af = af; 1089 (*sn)->rule = r_track; 1090 pf_addrcpy(&(*sn)->addr, src, af); 1091 if (raddr != NULL) 1092 pf_addrcpy(&(*sn)->raddr, raddr, raf); 1093 (*sn)->rkif = rkif; 1094 (*sn)->raf = raf; 1095 LIST_INSERT_HEAD(&(*sh)->nodes, *sn, entry); 1096 (*sn)->creation = time_uptime; 1097 (*sn)->ruletype = rule->action; 1098 (*sn)->type = sn_type; 1099 counter_u64_add(r_track->src_nodes[sn_type], 1); 1100 counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_INSERT], 1); 1101 } else { 1102 if (sn_type == PF_SN_LIMIT && rule->max_src_states && 1103 (*sn)->states >= rule->max_src_states) { 1104 counter_u64_add(V_pf_status.lcounters[LCNT_SRCSTATES], 1105 1); 1106 reason = PFRES_SRCLIMIT; 1107 goto done; 1108 } 1109 } 1110 done: 1111 if (reason == 0) 1112 (*sn)->states++; 1113 else 1114 (*sn) = NULL; 1115 1116 PF_HASHROW_UNLOCK(*sh); 1117 return (reason); 1118 } 1119 1120 void 1121 pf_unlink_src_node(struct pf_ksrc_node *src) 1122 { 1123 PF_SRC_NODE_LOCK_ASSERT(src); 1124 1125 LIST_REMOVE(src, entry); 1126 if (src->rule) 1127 counter_u64_add(src->rule->src_nodes[src->type], -1); 1128 } 1129 1130 u_int 1131 pf_free_src_nodes(struct pf_ksrc_node_list *head) 1132 { 1133 struct pf_ksrc_node *sn, *tmp; 1134 u_int count = 0; 1135 1136 LIST_FOREACH_SAFE(sn, head, entry, tmp) { 1137 pf_free_src_node(sn); 1138 count++; 1139 } 1140 1141 counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], count); 1142 1143 return (count); 1144 } 1145 1146 void 1147 pf_mtag_initialize(void) 1148 { 1149 1150 pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) + 1151 sizeof(struct pf_mtag), NULL, NULL, pf_mtag_uminit, NULL, 1152 UMA_ALIGN_PTR, 0); 1153 } 1154 1155 /* Per-vnet data storage structures initialization. */ 1156 void 1157 pf_initialize(void) 1158 { 1159 struct pf_keyhash *kh; 1160 struct pf_idhash *ih; 1161 struct pf_srchash *sh; 1162 struct pf_udpendpointhash *uh; 1163 u_int i; 1164 1165 if (V_pf_hashsize == 0 || !powerof2(V_pf_hashsize)) 1166 V_pf_hashsize = PF_HASHSIZ; 1167 if (V_pf_srchashsize == 0 || !powerof2(V_pf_srchashsize)) 1168 V_pf_srchashsize = PF_SRCHASHSIZ; 1169 if (V_pf_udpendpointhashsize == 0 || !powerof2(V_pf_udpendpointhashsize)) 1170 V_pf_udpendpointhashsize = PF_UDPENDHASHSIZ; 1171 1172 V_pf_hashseed = arc4random(); 1173 1174 /* States and state keys storage. */ 1175 V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_kstate), 1176 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1177 V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z; 1178 uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT); 1179 uma_zone_set_warning(V_pf_state_z, "PF states limit reached"); 1180 1181 V_pf_state_key_z = uma_zcreate("pf state keys", 1182 sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL, 1183 UMA_ALIGN_PTR, 0); 1184 1185 V_pf_keyhash = mallocarray(V_pf_hashsize, sizeof(struct pf_keyhash), 1186 M_PFHASH, M_NOWAIT | M_ZERO); 1187 V_pf_idhash = mallocarray(V_pf_hashsize, sizeof(struct pf_idhash), 1188 M_PFHASH, M_NOWAIT | M_ZERO); 1189 if (V_pf_keyhash == NULL || V_pf_idhash == NULL) { 1190 printf("pf: Unable to allocate memory for " 1191 "state_hashsize %lu.\n", V_pf_hashsize); 1192 1193 free(V_pf_keyhash, M_PFHASH); 1194 free(V_pf_idhash, M_PFHASH); 1195 1196 V_pf_hashsize = PF_HASHSIZ; 1197 V_pf_keyhash = mallocarray(V_pf_hashsize, 1198 sizeof(struct pf_keyhash), M_PFHASH, M_WAITOK | M_ZERO); 1199 V_pf_idhash = mallocarray(V_pf_hashsize, 1200 sizeof(struct pf_idhash), M_PFHASH, M_WAITOK | M_ZERO); 1201 } 1202 1203 V_pf_hashmask = V_pf_hashsize - 1; 1204 for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 1205 i++, kh++, ih++) { 1206 mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK); 1207 mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF); 1208 } 1209 1210 /* Source nodes. */ 1211 V_pf_sources_z = uma_zcreate("pf source nodes", 1212 sizeof(struct pf_ksrc_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 1213 0); 1214 V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z; 1215 uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT); 1216 uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached"); 1217 1218 V_pf_srchash = mallocarray(V_pf_srchashsize, 1219 sizeof(struct pf_srchash), M_PFHASH, M_NOWAIT | M_ZERO); 1220 if (V_pf_srchash == NULL) { 1221 printf("pf: Unable to allocate memory for " 1222 "source_hashsize %lu.\n", V_pf_srchashsize); 1223 1224 V_pf_srchashsize = PF_SRCHASHSIZ; 1225 V_pf_srchash = mallocarray(V_pf_srchashsize, 1226 sizeof(struct pf_srchash), M_PFHASH, M_WAITOK | M_ZERO); 1227 } 1228 1229 V_pf_srchashmask = V_pf_srchashsize - 1; 1230 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) 1231 mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF); 1232 1233 1234 /* UDP endpoint mappings. */ 1235 V_pf_udp_mapping_z = uma_zcreate("pf UDP mappings", 1236 sizeof(struct pf_udp_mapping), NULL, NULL, NULL, NULL, 1237 UMA_ALIGN_PTR, 0); 1238 V_pf_udpendpointhash = mallocarray(V_pf_udpendpointhashsize, 1239 sizeof(struct pf_udpendpointhash), M_PFHASH, M_NOWAIT | M_ZERO); 1240 if (V_pf_udpendpointhash == NULL) { 1241 printf("pf: Unable to allocate memory for " 1242 "udpendpoint_hashsize %lu.\n", V_pf_udpendpointhashsize); 1243 1244 V_pf_udpendpointhashsize = PF_UDPENDHASHSIZ; 1245 V_pf_udpendpointhash = mallocarray(V_pf_udpendpointhashsize, 1246 sizeof(struct pf_udpendpointhash), M_PFHASH, M_WAITOK | M_ZERO); 1247 } 1248 1249 V_pf_udpendpointhashmask = V_pf_udpendpointhashsize - 1; 1250 for (i = 0, uh = V_pf_udpendpointhash; 1251 i <= V_pf_udpendpointhashmask; 1252 i++, uh++) { 1253 mtx_init(&uh->lock, "pf_udpendpointhash", NULL, 1254 MTX_DEF | MTX_DUPOK); 1255 } 1256 1257 /* Anchors */ 1258 V_pf_anchor_z = uma_zcreate("pf anchors", 1259 sizeof(struct pf_kanchor), NULL, NULL, NULL, NULL, 1260 UMA_ALIGN_PTR, 0); 1261 V_pf_limits[PF_LIMIT_ANCHORS].zone = V_pf_anchor_z; 1262 uma_zone_set_max(V_pf_anchor_z, PF_ANCHOR_HIWAT); 1263 uma_zone_set_warning(V_pf_anchor_z, "PF anchor limit reached"); 1264 1265 V_pf_eth_anchor_z = uma_zcreate("pf Ethernet anchors", 1266 sizeof(struct pf_keth_anchor), NULL, NULL, NULL, NULL, 1267 UMA_ALIGN_PTR, 0); 1268 V_pf_limits[PF_LIMIT_ETH_ANCHORS].zone = V_pf_eth_anchor_z; 1269 uma_zone_set_max(V_pf_eth_anchor_z, PF_ANCHOR_HIWAT); 1270 uma_zone_set_warning(V_pf_eth_anchor_z, "PF Ethernet anchor limit reached"); 1271 1272 /* ALTQ */ 1273 TAILQ_INIT(&V_pf_altqs[0]); 1274 TAILQ_INIT(&V_pf_altqs[1]); 1275 TAILQ_INIT(&V_pf_altqs[2]); 1276 TAILQ_INIT(&V_pf_altqs[3]); 1277 TAILQ_INIT(&V_pf_pabuf[0]); 1278 TAILQ_INIT(&V_pf_pabuf[1]); 1279 TAILQ_INIT(&V_pf_pabuf[2]); 1280 V_pf_altqs_active = &V_pf_altqs[0]; 1281 V_pf_altq_ifs_active = &V_pf_altqs[1]; 1282 V_pf_altqs_inactive = &V_pf_altqs[2]; 1283 V_pf_altq_ifs_inactive = &V_pf_altqs[3]; 1284 1285 /* Send & overload+flush queues. */ 1286 STAILQ_INIT(&V_pf_sendqueue); 1287 SLIST_INIT(&V_pf_overloadqueue); 1288 TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, curvnet); 1289 1290 /* Unlinked, but may be referenced rules. */ 1291 TAILQ_INIT(&V_pf_unlinked_rules); 1292 } 1293 1294 void 1295 pf_mtag_cleanup(void) 1296 { 1297 1298 uma_zdestroy(pf_mtag_z); 1299 } 1300 1301 void 1302 pf_cleanup(void) 1303 { 1304 struct pf_keyhash *kh; 1305 struct pf_idhash *ih; 1306 struct pf_srchash *sh; 1307 struct pf_udpendpointhash *uh; 1308 struct pf_send_entry *pfse, *next; 1309 u_int i; 1310 1311 for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; 1312 i <= V_pf_hashmask; 1313 i++, kh++, ih++) { 1314 KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty", 1315 __func__)); 1316 KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty", 1317 __func__)); 1318 mtx_destroy(&kh->lock); 1319 mtx_destroy(&ih->lock); 1320 } 1321 free(V_pf_keyhash, M_PFHASH); 1322 free(V_pf_idhash, M_PFHASH); 1323 1324 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 1325 KASSERT(LIST_EMPTY(&sh->nodes), 1326 ("%s: source node hash not empty", __func__)); 1327 mtx_destroy(&sh->lock); 1328 } 1329 free(V_pf_srchash, M_PFHASH); 1330 1331 for (i = 0, uh = V_pf_udpendpointhash; 1332 i <= V_pf_udpendpointhashmask; 1333 i++, uh++) { 1334 KASSERT(LIST_EMPTY(&uh->endpoints), 1335 ("%s: udp endpoint hash not empty", __func__)); 1336 mtx_destroy(&uh->lock); 1337 } 1338 free(V_pf_udpendpointhash, M_PFHASH); 1339 1340 STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) { 1341 m_freem(pfse->pfse_m); 1342 free(pfse, M_PFTEMP); 1343 } 1344 MPASS(RB_EMPTY(&V_pf_sctp_endpoints)); 1345 1346 uma_zdestroy(V_pf_sources_z); 1347 uma_zdestroy(V_pf_state_z); 1348 uma_zdestroy(V_pf_state_key_z); 1349 uma_zdestroy(V_pf_udp_mapping_z); 1350 uma_zdestroy(V_pf_anchor_z); 1351 uma_zdestroy(V_pf_eth_anchor_z); 1352 } 1353 1354 static int 1355 pf_mtag_uminit(void *mem, int size, int how) 1356 { 1357 struct m_tag *t; 1358 1359 t = (struct m_tag *)mem; 1360 t->m_tag_cookie = MTAG_ABI_COMPAT; 1361 t->m_tag_id = PACKET_TAG_PF; 1362 t->m_tag_len = sizeof(struct pf_mtag); 1363 t->m_tag_free = pf_mtag_free; 1364 1365 return (0); 1366 } 1367 1368 static void 1369 pf_mtag_free(struct m_tag *t) 1370 { 1371 1372 uma_zfree(pf_mtag_z, t); 1373 } 1374 1375 struct pf_mtag * 1376 pf_get_mtag(struct mbuf *m) 1377 { 1378 struct m_tag *mtag; 1379 1380 if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL) 1381 return ((struct pf_mtag *)(mtag + 1)); 1382 1383 mtag = uma_zalloc(pf_mtag_z, M_NOWAIT); 1384 if (mtag == NULL) 1385 return (NULL); 1386 bzero(mtag + 1, sizeof(struct pf_mtag)); 1387 m_tag_prepend(m, mtag); 1388 1389 return ((struct pf_mtag *)(mtag + 1)); 1390 } 1391 1392 static int 1393 pf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks, 1394 struct pf_kstate *s) 1395 { 1396 struct pf_keyhash *khs, *khw, *kh; 1397 struct pf_state_key *sk, *cur; 1398 struct pf_kstate *si, *olds = NULL; 1399 int idx; 1400 1401 NET_EPOCH_ASSERT(); 1402 KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1403 KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__)); 1404 KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__)); 1405 1406 /* 1407 * We need to lock hash slots of both keys. To avoid deadlock 1408 * we always lock the slot with lower address first. Unlock order 1409 * isn't important. 1410 * 1411 * We also need to lock ID hash slot before dropping key 1412 * locks. On success we return with ID hash slot locked. 1413 */ 1414 1415 if (skw == sks) { 1416 khs = khw = &V_pf_keyhash[pf_hashkey(skw)]; 1417 PF_HASHROW_LOCK(khs); 1418 } else { 1419 khs = &V_pf_keyhash[pf_hashkey(sks)]; 1420 khw = &V_pf_keyhash[pf_hashkey(skw)]; 1421 if (khs == khw) { 1422 PF_HASHROW_LOCK(khs); 1423 } else if (khs < khw) { 1424 PF_HASHROW_LOCK(khs); 1425 PF_HASHROW_LOCK(khw); 1426 } else { 1427 PF_HASHROW_LOCK(khw); 1428 PF_HASHROW_LOCK(khs); 1429 } 1430 } 1431 1432 #define KEYS_UNLOCK() do { \ 1433 if (khs != khw) { \ 1434 PF_HASHROW_UNLOCK(khs); \ 1435 PF_HASHROW_UNLOCK(khw); \ 1436 } else \ 1437 PF_HASHROW_UNLOCK(khs); \ 1438 } while (0) 1439 1440 /* 1441 * First run: start with wire key. 1442 */ 1443 sk = skw; 1444 kh = khw; 1445 idx = PF_SK_WIRE; 1446 1447 MPASS(s->lock == NULL); 1448 s->lock = &V_pf_idhash[PF_IDHASH(s)].lock; 1449 1450 keyattach: 1451 LIST_FOREACH(cur, &kh->keys, entry) 1452 if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0) 1453 break; 1454 1455 if (cur != NULL) { 1456 /* Key exists. Check for same kif, if none, add to key. */ 1457 TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) { 1458 struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)]; 1459 1460 PF_HASHROW_LOCK(ih); 1461 if (si->kif == s->kif && 1462 ((si->key[PF_SK_WIRE]->af == sk->af && 1463 si->direction == s->direction) || 1464 (si->key[PF_SK_WIRE]->af != 1465 si->key[PF_SK_STACK]->af && 1466 sk->af == si->key[PF_SK_STACK]->af && 1467 si->direction != s->direction))) { 1468 bool reuse = false; 1469 1470 if (sk->proto == IPPROTO_TCP && 1471 si->src.state >= TCPS_FIN_WAIT_2 && 1472 si->dst.state >= TCPS_FIN_WAIT_2) 1473 reuse = true; 1474 1475 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1476 printf("pf: %s key attach " 1477 "%s on %s: ", 1478 (idx == PF_SK_WIRE) ? 1479 "wire" : "stack", 1480 reuse ? "reuse" : "failed", 1481 s->kif->pfik_name); 1482 pf_print_state_parts(s, 1483 (idx == PF_SK_WIRE) ? 1484 sk : NULL, 1485 (idx == PF_SK_STACK) ? 1486 sk : NULL); 1487 printf(", existing: "); 1488 pf_print_state_parts(si, 1489 (idx == PF_SK_WIRE) ? 1490 sk : NULL, 1491 (idx == PF_SK_STACK) ? 1492 sk : NULL); 1493 printf("\n"); 1494 } 1495 1496 if (reuse) { 1497 /* 1498 * New state matches an old >FIN_WAIT_2 1499 * state. We can't drop key hash locks, 1500 * thus we can't unlink it properly. 1501 * 1502 * As a workaround we drop it into 1503 * TCPS_CLOSED state, schedule purge 1504 * ASAP and push it into the very end 1505 * of the slot TAILQ, so that it won't 1506 * conflict with our new state. 1507 */ 1508 pf_set_protostate(si, PF_PEER_BOTH, 1509 TCPS_CLOSED); 1510 si->timeout = PFTM_PURGE; 1511 olds = si; 1512 } else { 1513 s->timeout = PFTM_UNLINKED; 1514 if (idx == PF_SK_STACK) 1515 /* 1516 * Remove the wire key from 1517 * the hash. Other threads 1518 * can't be referencing it 1519 * because we still hold the 1520 * hash lock. 1521 */ 1522 pf_state_key_detach(s, 1523 PF_SK_WIRE); 1524 PF_HASHROW_UNLOCK(ih); 1525 KEYS_UNLOCK(); 1526 if (idx == PF_SK_WIRE) 1527 /* 1528 * We've not inserted either key. 1529 * Free both. 1530 */ 1531 uma_zfree(V_pf_state_key_z, skw); 1532 if (skw != sks) 1533 uma_zfree( 1534 V_pf_state_key_z, 1535 sks); 1536 return (EEXIST); /* collision! */ 1537 } 1538 } 1539 PF_HASHROW_UNLOCK(ih); 1540 } 1541 uma_zfree(V_pf_state_key_z, sk); 1542 s->key[idx] = cur; 1543 } else { 1544 LIST_INSERT_HEAD(&kh->keys, sk, entry); 1545 s->key[idx] = sk; 1546 } 1547 1548 stateattach: 1549 /* List is sorted, if-bound states before floating. */ 1550 if (s->kif == V_pfi_all) 1551 TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]); 1552 else 1553 TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]); 1554 1555 if (olds) { 1556 TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]); 1557 TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds, 1558 key_list[idx]); 1559 olds = NULL; 1560 } 1561 1562 /* 1563 * Attach done. See how should we (or should not?) 1564 * attach a second key. 1565 */ 1566 if (sks == skw) { 1567 s->key[PF_SK_STACK] = s->key[PF_SK_WIRE]; 1568 idx = PF_SK_STACK; 1569 sks = NULL; 1570 goto stateattach; 1571 } else if (sks != NULL) { 1572 /* 1573 * Continue attaching with stack key. 1574 */ 1575 sk = sks; 1576 kh = khs; 1577 idx = PF_SK_STACK; 1578 sks = NULL; 1579 goto keyattach; 1580 } 1581 1582 PF_STATE_LOCK(s); 1583 KEYS_UNLOCK(); 1584 1585 KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL, 1586 ("%s failure", __func__)); 1587 1588 return (0); 1589 #undef KEYS_UNLOCK 1590 } 1591 1592 static void 1593 pf_detach_state(struct pf_kstate *s) 1594 { 1595 struct pf_state_key *sks = s->key[PF_SK_STACK]; 1596 struct pf_keyhash *kh; 1597 1598 NET_EPOCH_ASSERT(); 1599 MPASS(s->timeout >= PFTM_MAX); 1600 1601 pf_sctp_multihome_detach_addr(s); 1602 1603 if ((s->state_flags & PFSTATE_PFLOW) && V_pflow_export_state_ptr) 1604 V_pflow_export_state_ptr(s); 1605 1606 if (sks != NULL) { 1607 kh = &V_pf_keyhash[pf_hashkey(sks)]; 1608 PF_HASHROW_LOCK(kh); 1609 if (s->key[PF_SK_STACK] != NULL) 1610 pf_state_key_detach(s, PF_SK_STACK); 1611 /* 1612 * If both point to same key, then we are done. 1613 */ 1614 if (sks == s->key[PF_SK_WIRE]) { 1615 pf_state_key_detach(s, PF_SK_WIRE); 1616 PF_HASHROW_UNLOCK(kh); 1617 return; 1618 } 1619 PF_HASHROW_UNLOCK(kh); 1620 } 1621 1622 if (s->key[PF_SK_WIRE] != NULL) { 1623 kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])]; 1624 PF_HASHROW_LOCK(kh); 1625 if (s->key[PF_SK_WIRE] != NULL) 1626 pf_state_key_detach(s, PF_SK_WIRE); 1627 PF_HASHROW_UNLOCK(kh); 1628 } 1629 } 1630 1631 static void 1632 pf_state_key_detach(struct pf_kstate *s, int idx) 1633 { 1634 struct pf_state_key *sk = s->key[idx]; 1635 #ifdef INVARIANTS 1636 struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)]; 1637 1638 PF_HASHROW_ASSERT(kh); 1639 #endif /* INVARIANTS */ 1640 TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]); 1641 s->key[idx] = NULL; 1642 1643 if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) { 1644 LIST_REMOVE(sk, entry); 1645 uma_zfree(V_pf_state_key_z, sk); 1646 } 1647 } 1648 1649 static int 1650 pf_state_key_ctor(void *mem, int size, void *arg, int flags) 1651 { 1652 struct pf_state_key *sk = mem; 1653 1654 bzero(sk, sizeof(struct pf_state_key_cmp)); 1655 TAILQ_INIT(&sk->states[PF_SK_WIRE]); 1656 TAILQ_INIT(&sk->states[PF_SK_STACK]); 1657 1658 return (0); 1659 } 1660 1661 static int 1662 pf_state_key_addr_setup(struct pf_pdesc *pd, 1663 struct pf_state_key_cmp *key, int multi) 1664 { 1665 struct pf_addr *saddr = pd->src; 1666 struct pf_addr *daddr = pd->dst; 1667 #ifdef INET6 1668 struct nd_neighbor_solicit nd; 1669 struct pf_addr *target; 1670 u_short action, reason; 1671 1672 if (pd->af == AF_INET || pd->proto != IPPROTO_ICMPV6) 1673 goto copy; 1674 1675 switch (pd->hdr.icmp6.icmp6_type) { 1676 case ND_NEIGHBOR_SOLICIT: 1677 if (multi) 1678 return (-1); 1679 if (!pf_pull_hdr(pd->m, pd->off, &nd, sizeof(nd), &action, &reason, pd->af)) 1680 return (-1); 1681 target = (struct pf_addr *)&nd.nd_ns_target; 1682 daddr = target; 1683 break; 1684 case ND_NEIGHBOR_ADVERT: 1685 if (multi) 1686 return (-1); 1687 if (!pf_pull_hdr(pd->m, pd->off, &nd, sizeof(nd), &action, &reason, pd->af)) 1688 return (-1); 1689 target = (struct pf_addr *)&nd.nd_ns_target; 1690 saddr = target; 1691 if (IN6_IS_ADDR_MULTICAST(&pd->dst->v6)) { 1692 key->addr[pd->didx].addr32[0] = 0; 1693 key->addr[pd->didx].addr32[1] = 0; 1694 key->addr[pd->didx].addr32[2] = 0; 1695 key->addr[pd->didx].addr32[3] = 0; 1696 daddr = NULL; /* overwritten */ 1697 } 1698 break; 1699 default: 1700 if (multi) { 1701 key->addr[pd->sidx].addr32[0] = IPV6_ADDR_INT32_MLL; 1702 key->addr[pd->sidx].addr32[1] = 0; 1703 key->addr[pd->sidx].addr32[2] = 0; 1704 key->addr[pd->sidx].addr32[3] = IPV6_ADDR_INT32_ONE; 1705 saddr = NULL; /* overwritten */ 1706 } 1707 } 1708 copy: 1709 #endif /* INET6 */ 1710 if (saddr) 1711 pf_addrcpy(&key->addr[pd->sidx], saddr, pd->af); 1712 if (daddr) 1713 pf_addrcpy(&key->addr[pd->didx], daddr, pd->af); 1714 1715 return (0); 1716 } 1717 1718 int 1719 pf_state_key_setup(struct pf_pdesc *pd, u_int16_t sport, u_int16_t dport, 1720 struct pf_state_key **sk, struct pf_state_key **nk) 1721 { 1722 *sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1723 if (*sk == NULL) 1724 return (ENOMEM); 1725 1726 if (pf_state_key_addr_setup(pd, (struct pf_state_key_cmp *)*sk, 1727 0)) { 1728 uma_zfree(V_pf_state_key_z, *sk); 1729 *sk = NULL; 1730 return (ENOMEM); 1731 } 1732 1733 (*sk)->port[pd->sidx] = sport; 1734 (*sk)->port[pd->didx] = dport; 1735 (*sk)->proto = pd->proto; 1736 (*sk)->af = pd->af; 1737 1738 *nk = pf_state_key_clone(*sk); 1739 if (*nk == NULL) { 1740 uma_zfree(V_pf_state_key_z, *sk); 1741 *sk = NULL; 1742 return (ENOMEM); 1743 } 1744 1745 if (pd->af != pd->naf) { 1746 (*sk)->port[pd->sidx] = pd->osport; 1747 (*sk)->port[pd->didx] = pd->odport; 1748 1749 (*nk)->af = pd->naf; 1750 1751 /* 1752 * We're overwriting an address here, so potentially there's bits of an IPv6 1753 * address left in here. Clear that out first. 1754 */ 1755 bzero(&(*nk)->addr[0], sizeof((*nk)->addr[0])); 1756 bzero(&(*nk)->addr[1], sizeof((*nk)->addr[1])); 1757 if (pd->dir == PF_IN) { 1758 pf_addrcpy(&(*nk)->addr[pd->didx], &pd->nsaddr, 1759 pd->naf); 1760 pf_addrcpy(&(*nk)->addr[pd->sidx], &pd->ndaddr, 1761 pd->naf); 1762 (*nk)->port[pd->didx] = pd->nsport; 1763 (*nk)->port[pd->sidx] = pd->ndport; 1764 } else { 1765 pf_addrcpy(&(*nk)->addr[pd->sidx], &pd->nsaddr, 1766 pd->naf); 1767 pf_addrcpy(&(*nk)->addr[pd->didx], &pd->ndaddr, 1768 pd->naf); 1769 (*nk)->port[pd->sidx] = pd->nsport; 1770 (*nk)->port[pd->didx] = pd->ndport; 1771 } 1772 1773 switch (pd->proto) { 1774 case IPPROTO_ICMP: 1775 (*nk)->proto = IPPROTO_ICMPV6; 1776 break; 1777 case IPPROTO_ICMPV6: 1778 (*nk)->proto = IPPROTO_ICMP; 1779 break; 1780 default: 1781 (*nk)->proto = pd->proto; 1782 } 1783 } 1784 1785 return (0); 1786 } 1787 1788 struct pf_state_key * 1789 pf_state_key_clone(const struct pf_state_key *orig) 1790 { 1791 struct pf_state_key *sk; 1792 1793 sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1794 if (sk == NULL) 1795 return (NULL); 1796 1797 bcopy(orig, sk, sizeof(struct pf_state_key_cmp)); 1798 1799 return (sk); 1800 } 1801 1802 int 1803 pf_state_insert(struct pfi_kkif *kif, struct pfi_kkif *orig_kif, 1804 struct pf_state_key *skw, struct pf_state_key *sks, struct pf_kstate *s) 1805 { 1806 struct pf_idhash *ih; 1807 struct pf_kstate *cur; 1808 int error; 1809 1810 NET_EPOCH_ASSERT(); 1811 1812 KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]), 1813 ("%s: sks not pristine", __func__)); 1814 KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]), 1815 ("%s: skw not pristine", __func__)); 1816 KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1817 1818 s->kif = kif; 1819 s->orig_kif = orig_kif; 1820 1821 if (s->id == 0 && s->creatorid == 0) { 1822 s->id = alloc_unr64(&V_pf_stateid); 1823 s->id = htobe64(s->id); 1824 s->creatorid = V_pf_status.hostid; 1825 } 1826 1827 /* Returns with ID locked on success. */ 1828 if ((error = pf_state_key_attach(skw, sks, s)) != 0) 1829 return (error); 1830 skw = sks = NULL; 1831 1832 ih = &V_pf_idhash[PF_IDHASH(s)]; 1833 PF_HASHROW_ASSERT(ih); 1834 LIST_FOREACH(cur, &ih->states, entry) 1835 if (cur->id == s->id && cur->creatorid == s->creatorid) 1836 break; 1837 1838 if (cur != NULL) { 1839 s->timeout = PFTM_UNLINKED; 1840 PF_HASHROW_UNLOCK(ih); 1841 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1842 printf("pf: state ID collision: " 1843 "id: %016llx creatorid: %08x\n", 1844 (unsigned long long)be64toh(s->id), 1845 ntohl(s->creatorid)); 1846 } 1847 pf_detach_state(s); 1848 return (EEXIST); 1849 } 1850 LIST_INSERT_HEAD(&ih->states, s, entry); 1851 /* One for keys, one for ID hash. */ 1852 refcount_init(&s->refs, 2); 1853 1854 pf_counter_u64_add(&V_pf_status.fcounters[FCNT_STATE_INSERT], 1); 1855 if (V_pfsync_insert_state_ptr != NULL) 1856 V_pfsync_insert_state_ptr(s); 1857 1858 /* Returns locked. */ 1859 return (0); 1860 } 1861 1862 /* 1863 * Find state by ID: returns with locked row on success. 1864 */ 1865 struct pf_kstate * 1866 pf_find_state_byid(uint64_t id, uint32_t creatorid) 1867 { 1868 struct pf_idhash *ih; 1869 struct pf_kstate *s; 1870 1871 pf_counter_u64_add(&V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1872 1873 ih = &V_pf_idhash[PF_IDHASHID(id)]; 1874 1875 PF_HASHROW_LOCK(ih); 1876 LIST_FOREACH(s, &ih->states, entry) 1877 if (s->id == id && s->creatorid == creatorid) 1878 break; 1879 1880 if (s == NULL) 1881 PF_HASHROW_UNLOCK(ih); 1882 1883 return (s); 1884 } 1885 1886 /* 1887 * Find state by key. 1888 * Returns with ID hash slot locked on success. 1889 */ 1890 static int 1891 pf_find_state(struct pf_pdesc *pd, const struct pf_state_key_cmp *key, 1892 struct pf_kstate **state) 1893 { 1894 struct pf_keyhash *kh; 1895 struct pf_state_key *sk; 1896 struct pf_kstate *s; 1897 int idx; 1898 1899 *state = NULL; 1900 1901 pf_counter_u64_add(&V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1902 1903 kh = &V_pf_keyhash[pf_hashkey((const struct pf_state_key *)key)]; 1904 1905 PF_HASHROW_LOCK(kh); 1906 LIST_FOREACH(sk, &kh->keys, entry) 1907 if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1908 break; 1909 if (sk == NULL) { 1910 PF_HASHROW_UNLOCK(kh); 1911 return (PF_DROP); 1912 } 1913 1914 idx = (pd->dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK); 1915 1916 /* List is sorted, if-bound states before floating ones. */ 1917 TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) 1918 if (s->kif == V_pfi_all || s->kif == pd->kif || 1919 s->orig_kif == pd->kif) { 1920 PF_STATE_LOCK(s); 1921 PF_HASHROW_UNLOCK(kh); 1922 if (__predict_false(s->timeout >= PFTM_MAX)) { 1923 /* 1924 * State is either being processed by 1925 * pf_remove_state() in an other thread, or 1926 * is scheduled for immediate expiry. 1927 */ 1928 PF_STATE_UNLOCK(s); 1929 SDT_PROBE5(pf, ip, state, lookup, pd->kif, 1930 key, (pd->dir), pd, *state); 1931 return (PF_DROP); 1932 } 1933 goto out; 1934 } 1935 1936 /* Look through the other list, in case of AF-TO */ 1937 idx = idx == PF_SK_WIRE ? PF_SK_STACK : PF_SK_WIRE; 1938 TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 1939 if (s->key[PF_SK_WIRE]->af == s->key[PF_SK_STACK]->af) 1940 continue; 1941 if (s->kif == V_pfi_all || s->kif == pd->kif || 1942 s->orig_kif == pd->kif) { 1943 PF_STATE_LOCK(s); 1944 PF_HASHROW_UNLOCK(kh); 1945 if (__predict_false(s->timeout >= PFTM_MAX)) { 1946 /* 1947 * State is either being processed by 1948 * pf_remove_state() in an other thread, or 1949 * is scheduled for immediate expiry. 1950 */ 1951 PF_STATE_UNLOCK(s); 1952 SDT_PROBE5(pf, ip, state, lookup, pd->kif, 1953 key, (pd->dir), pd, NULL); 1954 return (PF_DROP); 1955 } 1956 goto out; 1957 } 1958 } 1959 1960 PF_HASHROW_UNLOCK(kh); 1961 1962 out: 1963 SDT_PROBE5(pf, ip, state, lookup, pd->kif, key, (pd->dir), pd, *state); 1964 1965 if (s == NULL || s->timeout == PFTM_PURGE) { 1966 if (s) 1967 PF_STATE_UNLOCK(s); 1968 return (PF_DROP); 1969 } 1970 1971 if ((s)->rule->pktrate.limit && pd->dir == (s)->direction) { 1972 if (pf_check_threshold(&(s)->rule->pktrate)) { 1973 PF_STATE_UNLOCK(s); 1974 return (PF_DROP); 1975 } 1976 } 1977 if (PACKET_LOOPED(pd)) { 1978 PF_STATE_UNLOCK(s); 1979 return (PF_PASS); 1980 } 1981 1982 *state = s; 1983 1984 return (PF_MATCH); 1985 } 1986 1987 /* 1988 * Returns with ID hash slot locked on success. 1989 */ 1990 struct pf_kstate * 1991 pf_find_state_all(const struct pf_state_key_cmp *key, u_int dir, int *more) 1992 { 1993 struct pf_keyhash *kh; 1994 struct pf_state_key *sk; 1995 struct pf_kstate *s, *ret = NULL; 1996 int idx, inout = 0; 1997 1998 if (more != NULL) 1999 *more = 0; 2000 2001 pf_counter_u64_add(&V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 2002 2003 kh = &V_pf_keyhash[pf_hashkey((const struct pf_state_key *)key)]; 2004 2005 PF_HASHROW_LOCK(kh); 2006 LIST_FOREACH(sk, &kh->keys, entry) 2007 if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 2008 break; 2009 if (sk == NULL) { 2010 PF_HASHROW_UNLOCK(kh); 2011 return (NULL); 2012 } 2013 switch (dir) { 2014 case PF_IN: 2015 idx = PF_SK_WIRE; 2016 break; 2017 case PF_OUT: 2018 idx = PF_SK_STACK; 2019 break; 2020 case PF_INOUT: 2021 idx = PF_SK_WIRE; 2022 inout = 1; 2023 break; 2024 default: 2025 panic("%s: dir %u", __func__, dir); 2026 } 2027 second_run: 2028 TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 2029 if (more == NULL) { 2030 PF_STATE_LOCK(s); 2031 PF_HASHROW_UNLOCK(kh); 2032 return (s); 2033 } 2034 2035 if (ret) 2036 (*more)++; 2037 else { 2038 ret = s; 2039 PF_STATE_LOCK(s); 2040 } 2041 } 2042 if (inout == 1) { 2043 inout = 0; 2044 idx = PF_SK_STACK; 2045 goto second_run; 2046 } 2047 PF_HASHROW_UNLOCK(kh); 2048 2049 return (ret); 2050 } 2051 2052 /* 2053 * FIXME 2054 * This routine is inefficient -- locks the state only to unlock immediately on 2055 * return. 2056 * It is racy -- after the state is unlocked nothing stops other threads from 2057 * removing it. 2058 */ 2059 bool 2060 pf_find_state_all_exists(const struct pf_state_key_cmp *key, u_int dir) 2061 { 2062 struct pf_kstate *s; 2063 2064 s = pf_find_state_all(key, dir, NULL); 2065 if (s != NULL) { 2066 PF_STATE_UNLOCK(s); 2067 return (true); 2068 } 2069 return (false); 2070 } 2071 2072 void 2073 pf_state_peer_hton(const struct pf_state_peer *s, struct pf_state_peer_export *d) 2074 { 2075 d->seqlo = htonl(s->seqlo); 2076 d->seqhi = htonl(s->seqhi); 2077 d->seqdiff = htonl(s->seqdiff); 2078 d->max_win = htons(s->max_win); 2079 d->mss = htons(s->mss); 2080 d->state = s->state; 2081 d->wscale = s->wscale; 2082 if (s->scrub) { 2083 d->scrub.pfss_flags = htons( 2084 s->scrub->pfss_flags & PFSS_TIMESTAMP); 2085 d->scrub.pfss_ttl = (s)->scrub->pfss_ttl; 2086 d->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod); 2087 d->scrub.scrub_flag = PF_SCRUB_FLAG_VALID; 2088 } 2089 } 2090 2091 void 2092 pf_state_peer_ntoh(const struct pf_state_peer_export *s, struct pf_state_peer *d) 2093 { 2094 d->seqlo = ntohl(s->seqlo); 2095 d->seqhi = ntohl(s->seqhi); 2096 d->seqdiff = ntohl(s->seqdiff); 2097 d->max_win = ntohs(s->max_win); 2098 d->mss = ntohs(s->mss); 2099 d->state = s->state; 2100 d->wscale = s->wscale; 2101 if (s->scrub.scrub_flag == PF_SCRUB_FLAG_VALID && 2102 d->scrub != NULL) { 2103 d->scrub->pfss_flags = ntohs(s->scrub.pfss_flags) & 2104 PFSS_TIMESTAMP; 2105 d->scrub->pfss_ttl = s->scrub.pfss_ttl; 2106 d->scrub->pfss_ts_mod = ntohl(s->scrub.pfss_ts_mod); 2107 } 2108 } 2109 2110 struct pf_udp_mapping * 2111 pf_udp_mapping_create(sa_family_t af, struct pf_addr *src_addr, uint16_t src_port, 2112 struct pf_addr *nat_addr, uint16_t nat_port) 2113 { 2114 struct pf_udp_mapping *mapping; 2115 2116 mapping = uma_zalloc(V_pf_udp_mapping_z, M_NOWAIT | M_ZERO); 2117 if (mapping == NULL) 2118 return (NULL); 2119 pf_addrcpy(&mapping->endpoints[0].addr, src_addr, af); 2120 mapping->endpoints[0].port = src_port; 2121 mapping->endpoints[0].af = af; 2122 mapping->endpoints[0].mapping = mapping; 2123 pf_addrcpy(&mapping->endpoints[1].addr, nat_addr, af); 2124 mapping->endpoints[1].port = nat_port; 2125 mapping->endpoints[1].af = af; 2126 mapping->endpoints[1].mapping = mapping; 2127 refcount_init(&mapping->refs, 1); 2128 return (mapping); 2129 } 2130 2131 int 2132 pf_udp_mapping_insert(struct pf_udp_mapping *mapping) 2133 { 2134 struct pf_udpendpointhash *h0, *h1; 2135 struct pf_udp_endpoint *endpoint; 2136 int ret = EEXIST; 2137 2138 h0 = &V_pf_udpendpointhash[pf_hashudpendpoint(&mapping->endpoints[0])]; 2139 h1 = &V_pf_udpendpointhash[pf_hashudpendpoint(&mapping->endpoints[1])]; 2140 if (h0 == h1) { 2141 PF_HASHROW_LOCK(h0); 2142 } else if (h0 < h1) { 2143 PF_HASHROW_LOCK(h0); 2144 PF_HASHROW_LOCK(h1); 2145 } else { 2146 PF_HASHROW_LOCK(h1); 2147 PF_HASHROW_LOCK(h0); 2148 } 2149 2150 LIST_FOREACH(endpoint, &h0->endpoints, entry) { 2151 if (bcmp(endpoint, &mapping->endpoints[0], 2152 sizeof(struct pf_udp_endpoint_cmp)) == 0) 2153 break; 2154 } 2155 if (endpoint != NULL) 2156 goto cleanup; 2157 LIST_FOREACH(endpoint, &h1->endpoints, entry) { 2158 if (bcmp(endpoint, &mapping->endpoints[1], 2159 sizeof(struct pf_udp_endpoint_cmp)) == 0) 2160 break; 2161 } 2162 if (endpoint != NULL) 2163 goto cleanup; 2164 LIST_INSERT_HEAD(&h0->endpoints, &mapping->endpoints[0], entry); 2165 LIST_INSERT_HEAD(&h1->endpoints, &mapping->endpoints[1], entry); 2166 ret = 0; 2167 2168 cleanup: 2169 if (h0 != h1) { 2170 PF_HASHROW_UNLOCK(h0); 2171 PF_HASHROW_UNLOCK(h1); 2172 } else { 2173 PF_HASHROW_UNLOCK(h0); 2174 } 2175 return (ret); 2176 } 2177 2178 void 2179 pf_udp_mapping_release(struct pf_udp_mapping *mapping) 2180 { 2181 /* refcount is synchronized on the source endpoint's row lock */ 2182 struct pf_udpendpointhash *h0, *h1; 2183 2184 if (mapping == NULL) 2185 return; 2186 2187 h0 = &V_pf_udpendpointhash[pf_hashudpendpoint(&mapping->endpoints[0])]; 2188 PF_HASHROW_LOCK(h0); 2189 if (refcount_release(&mapping->refs)) { 2190 LIST_REMOVE(&mapping->endpoints[0], entry); 2191 PF_HASHROW_UNLOCK(h0); 2192 h1 = &V_pf_udpendpointhash[pf_hashudpendpoint(&mapping->endpoints[1])]; 2193 PF_HASHROW_LOCK(h1); 2194 LIST_REMOVE(&mapping->endpoints[1], entry); 2195 PF_HASHROW_UNLOCK(h1); 2196 2197 uma_zfree(V_pf_udp_mapping_z, mapping); 2198 } else { 2199 PF_HASHROW_UNLOCK(h0); 2200 } 2201 } 2202 2203 2204 struct pf_udp_mapping * 2205 pf_udp_mapping_find(struct pf_udp_endpoint_cmp *key) 2206 { 2207 struct pf_udpendpointhash *uh; 2208 struct pf_udp_endpoint *endpoint; 2209 2210 uh = &V_pf_udpendpointhash[pf_hashudpendpoint((struct pf_udp_endpoint*)key)]; 2211 2212 PF_HASHROW_LOCK(uh); 2213 LIST_FOREACH(endpoint, &uh->endpoints, entry) { 2214 if (bcmp(endpoint, key, sizeof(struct pf_udp_endpoint_cmp)) == 0 && 2215 bcmp(endpoint, &endpoint->mapping->endpoints[0], 2216 sizeof(struct pf_udp_endpoint_cmp)) == 0) 2217 break; 2218 } 2219 if (endpoint == NULL) { 2220 PF_HASHROW_UNLOCK(uh); 2221 return (NULL); 2222 } 2223 refcount_acquire(&endpoint->mapping->refs); 2224 PF_HASHROW_UNLOCK(uh); 2225 return (endpoint->mapping); 2226 } 2227 /* END state table stuff */ 2228 2229 static void 2230 pf_send(struct pf_send_entry *pfse) 2231 { 2232 2233 PF_SENDQ_LOCK(); 2234 STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next); 2235 PF_SENDQ_UNLOCK(); 2236 swi_sched(V_pf_swi_cookie, 0); 2237 } 2238 2239 static bool 2240 pf_isforlocal(struct mbuf *m, int af) 2241 { 2242 switch (af) { 2243 #ifdef INET 2244 case AF_INET: { 2245 struct ip *ip = mtod(m, struct ip *); 2246 2247 return (in_localip(ip->ip_dst)); 2248 } 2249 #endif /* INET */ 2250 #ifdef INET6 2251 case AF_INET6: { 2252 struct ip6_hdr *ip6; 2253 struct in6_ifaddr *ia; 2254 ip6 = mtod(m, struct ip6_hdr *); 2255 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 2256 if (ia == NULL) 2257 return (false); 2258 return (! (ia->ia6_flags & IN6_IFF_NOTREADY)); 2259 } 2260 #endif /* INET6 */ 2261 default: 2262 unhandled_af(af); 2263 } 2264 2265 return (false); 2266 } 2267 2268 int 2269 pf_icmp_mapping(struct pf_pdesc *pd, u_int8_t type, 2270 int *icmp_dir, u_int16_t *virtual_id, u_int16_t *virtual_type) 2271 { 2272 /* 2273 * ICMP types marked with PF_OUT are typically responses to 2274 * PF_IN, and will match states in the opposite direction. 2275 * PF_IN ICMP types need to match a state with that type. 2276 */ 2277 *icmp_dir = PF_OUT; 2278 2279 /* Queries (and responses) */ 2280 switch (pd->af) { 2281 #ifdef INET 2282 case AF_INET: 2283 switch (type) { 2284 case ICMP_ECHO: 2285 *icmp_dir = PF_IN; 2286 /* FALLTHROUGH */ 2287 case ICMP_ECHOREPLY: 2288 *virtual_type = ICMP_ECHO; 2289 *virtual_id = pd->hdr.icmp.icmp_id; 2290 break; 2291 2292 case ICMP_TSTAMP: 2293 *icmp_dir = PF_IN; 2294 /* FALLTHROUGH */ 2295 case ICMP_TSTAMPREPLY: 2296 *virtual_type = ICMP_TSTAMP; 2297 *virtual_id = pd->hdr.icmp.icmp_id; 2298 break; 2299 2300 case ICMP_IREQ: 2301 *icmp_dir = PF_IN; 2302 /* FALLTHROUGH */ 2303 case ICMP_IREQREPLY: 2304 *virtual_type = ICMP_IREQ; 2305 *virtual_id = pd->hdr.icmp.icmp_id; 2306 break; 2307 2308 case ICMP_MASKREQ: 2309 *icmp_dir = PF_IN; 2310 /* FALLTHROUGH */ 2311 case ICMP_MASKREPLY: 2312 *virtual_type = ICMP_MASKREQ; 2313 *virtual_id = pd->hdr.icmp.icmp_id; 2314 break; 2315 2316 case ICMP_IPV6_WHEREAREYOU: 2317 *icmp_dir = PF_IN; 2318 /* FALLTHROUGH */ 2319 case ICMP_IPV6_IAMHERE: 2320 *virtual_type = ICMP_IPV6_WHEREAREYOU; 2321 *virtual_id = 0; /* Nothing sane to match on! */ 2322 break; 2323 2324 case ICMP_MOBILE_REGREQUEST: 2325 *icmp_dir = PF_IN; 2326 /* FALLTHROUGH */ 2327 case ICMP_MOBILE_REGREPLY: 2328 *virtual_type = ICMP_MOBILE_REGREQUEST; 2329 *virtual_id = 0; /* Nothing sane to match on! */ 2330 break; 2331 2332 case ICMP_ROUTERSOLICIT: 2333 *icmp_dir = PF_IN; 2334 /* FALLTHROUGH */ 2335 case ICMP_ROUTERADVERT: 2336 *virtual_type = ICMP_ROUTERSOLICIT; 2337 *virtual_id = 0; /* Nothing sane to match on! */ 2338 break; 2339 2340 /* These ICMP types map to other connections */ 2341 case ICMP_UNREACH: 2342 case ICMP_SOURCEQUENCH: 2343 case ICMP_REDIRECT: 2344 case ICMP_TIMXCEED: 2345 case ICMP_PARAMPROB: 2346 /* These will not be used, but set them anyway */ 2347 *icmp_dir = PF_IN; 2348 *virtual_type = type; 2349 *virtual_id = 0; 2350 *virtual_type = htons(*virtual_type); 2351 return (1); /* These types match to another state */ 2352 2353 /* 2354 * All remaining ICMP types get their own states, 2355 * and will only match in one direction. 2356 */ 2357 default: 2358 *icmp_dir = PF_IN; 2359 *virtual_type = type; 2360 *virtual_id = 0; 2361 break; 2362 } 2363 break; 2364 #endif /* INET */ 2365 #ifdef INET6 2366 case AF_INET6: 2367 switch (type) { 2368 case ICMP6_ECHO_REQUEST: 2369 *icmp_dir = PF_IN; 2370 /* FALLTHROUGH */ 2371 case ICMP6_ECHO_REPLY: 2372 *virtual_type = ICMP6_ECHO_REQUEST; 2373 *virtual_id = pd->hdr.icmp6.icmp6_id; 2374 break; 2375 2376 case MLD_LISTENER_QUERY: 2377 case MLD_LISTENER_REPORT: { 2378 /* 2379 * Listener Report can be sent by clients 2380 * without an associated Listener Query. 2381 * In addition to that, when Report is sent as a 2382 * reply to a Query its source and destination 2383 * address are different. 2384 */ 2385 *icmp_dir = PF_IN; 2386 *virtual_type = MLD_LISTENER_QUERY; 2387 *virtual_id = 0; 2388 break; 2389 } 2390 case MLD_MTRACE: 2391 *icmp_dir = PF_IN; 2392 /* FALLTHROUGH */ 2393 case MLD_MTRACE_RESP: 2394 *virtual_type = MLD_MTRACE; 2395 *virtual_id = 0; /* Nothing sane to match on! */ 2396 break; 2397 2398 case ND_NEIGHBOR_SOLICIT: 2399 *icmp_dir = PF_IN; 2400 /* FALLTHROUGH */ 2401 case ND_NEIGHBOR_ADVERT: { 2402 *virtual_type = ND_NEIGHBOR_SOLICIT; 2403 *virtual_id = 0; 2404 break; 2405 } 2406 2407 /* 2408 * These ICMP types map to other connections. 2409 * ND_REDIRECT can't be in this list because the triggering 2410 * packet header is optional. 2411 */ 2412 case ICMP6_DST_UNREACH: 2413 case ICMP6_PACKET_TOO_BIG: 2414 case ICMP6_TIME_EXCEEDED: 2415 case ICMP6_PARAM_PROB: 2416 /* These will not be used, but set them anyway */ 2417 *icmp_dir = PF_IN; 2418 *virtual_type = type; 2419 *virtual_id = 0; 2420 *virtual_type = htons(*virtual_type); 2421 return (1); /* These types match to another state */ 2422 /* 2423 * All remaining ICMP6 types get their own states, 2424 * and will only match in one direction. 2425 */ 2426 default: 2427 *icmp_dir = PF_IN; 2428 *virtual_type = type; 2429 *virtual_id = 0; 2430 break; 2431 } 2432 break; 2433 #endif /* INET6 */ 2434 default: 2435 unhandled_af(pd->af); 2436 } 2437 *virtual_type = htons(*virtual_type); 2438 return (0); /* These types match to their own state */ 2439 } 2440 2441 void 2442 pf_intr(void *v) 2443 { 2444 struct epoch_tracker et; 2445 struct pf_send_head queue; 2446 struct pf_send_entry *pfse, *next; 2447 2448 CURVNET_SET((struct vnet *)v); 2449 2450 PF_SENDQ_LOCK(); 2451 queue = V_pf_sendqueue; 2452 STAILQ_INIT(&V_pf_sendqueue); 2453 PF_SENDQ_UNLOCK(); 2454 2455 NET_EPOCH_ENTER(et); 2456 2457 STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) { 2458 switch (pfse->pfse_type) { 2459 #ifdef INET 2460 case PFSE_IP: { 2461 if (pf_isforlocal(pfse->pfse_m, AF_INET)) { 2462 KASSERT(pfse->pfse_m->m_pkthdr.rcvif == V_loif, 2463 ("%s: rcvif != loif", __func__)); 2464 2465 pfse->pfse_m->m_flags |= M_SKIP_FIREWALL; 2466 pfse->pfse_m->m_pkthdr.csum_flags |= 2467 CSUM_IP_VALID | CSUM_IP_CHECKED | 2468 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2469 pfse->pfse_m->m_pkthdr.csum_data = 0xffff; 2470 ip_input(pfse->pfse_m); 2471 } else { 2472 ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, 2473 NULL); 2474 } 2475 break; 2476 } 2477 case PFSE_ICMP: 2478 icmp_error(pfse->pfse_m, pfse->icmpopts.type, 2479 pfse->icmpopts.code, 0, pfse->icmpopts.mtu); 2480 break; 2481 #endif /* INET */ 2482 #ifdef INET6 2483 case PFSE_IP6: 2484 if (pf_isforlocal(pfse->pfse_m, AF_INET6)) { 2485 KASSERT(pfse->pfse_m->m_pkthdr.rcvif == V_loif, 2486 ("%s: rcvif != loif", __func__)); 2487 2488 pfse->pfse_m->m_flags |= M_SKIP_FIREWALL | 2489 M_LOOP; 2490 pfse->pfse_m->m_pkthdr.csum_flags |= 2491 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2492 pfse->pfse_m->m_pkthdr.csum_data = 0xffff; 2493 ip6_input(pfse->pfse_m); 2494 } else { 2495 ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, 2496 NULL, NULL); 2497 } 2498 break; 2499 case PFSE_ICMP6: 2500 icmp6_error(pfse->pfse_m, pfse->icmpopts.type, 2501 pfse->icmpopts.code, pfse->icmpopts.mtu); 2502 break; 2503 #endif /* INET6 */ 2504 default: 2505 panic("%s: unknown type", __func__); 2506 } 2507 free(pfse, M_PFTEMP); 2508 } 2509 NET_EPOCH_EXIT(et); 2510 CURVNET_RESTORE(); 2511 } 2512 2513 #define pf_purge_thread_period (hz / 10) 2514 2515 #ifdef PF_WANT_32_TO_64_COUNTER 2516 static void 2517 pf_status_counter_u64_periodic(void) 2518 { 2519 2520 PF_RULES_RASSERT(); 2521 2522 if ((V_pf_counter_periodic_iter % (pf_purge_thread_period * 10 * 60)) != 0) { 2523 return; 2524 } 2525 2526 for (int i = 0; i < FCNT_MAX; i++) { 2527 pf_counter_u64_periodic(&V_pf_status.fcounters[i]); 2528 } 2529 } 2530 2531 static void 2532 pf_kif_counter_u64_periodic(void) 2533 { 2534 struct pfi_kkif *kif; 2535 size_t r, run; 2536 2537 PF_RULES_RASSERT(); 2538 2539 if (__predict_false(V_pf_allkifcount == 0)) { 2540 return; 2541 } 2542 2543 if ((V_pf_counter_periodic_iter % (pf_purge_thread_period * 10 * 300)) != 0) { 2544 return; 2545 } 2546 2547 run = V_pf_allkifcount / 10; 2548 if (run < 5) 2549 run = 5; 2550 2551 for (r = 0; r < run; r++) { 2552 kif = LIST_NEXT(V_pf_kifmarker, pfik_allkiflist); 2553 if (kif == NULL) { 2554 LIST_REMOVE(V_pf_kifmarker, pfik_allkiflist); 2555 LIST_INSERT_HEAD(&V_pf_allkiflist, V_pf_kifmarker, pfik_allkiflist); 2556 break; 2557 } 2558 2559 LIST_REMOVE(V_pf_kifmarker, pfik_allkiflist); 2560 LIST_INSERT_AFTER(kif, V_pf_kifmarker, pfik_allkiflist); 2561 2562 for (int i = 0; i < 2; i++) { 2563 for (int j = 0; j < 2; j++) { 2564 for (int k = 0; k < 2; k++) { 2565 pf_counter_u64_periodic(&kif->pfik_packets[i][j][k]); 2566 pf_counter_u64_periodic(&kif->pfik_bytes[i][j][k]); 2567 } 2568 } 2569 } 2570 } 2571 } 2572 2573 static void 2574 pf_rule_counter_u64_periodic(void) 2575 { 2576 struct pf_krule *rule; 2577 size_t r, run; 2578 2579 PF_RULES_RASSERT(); 2580 2581 if (__predict_false(V_pf_allrulecount == 0)) { 2582 return; 2583 } 2584 2585 if ((V_pf_counter_periodic_iter % (pf_purge_thread_period * 10 * 300)) != 0) { 2586 return; 2587 } 2588 2589 run = V_pf_allrulecount / 10; 2590 if (run < 5) 2591 run = 5; 2592 2593 for (r = 0; r < run; r++) { 2594 rule = LIST_NEXT(V_pf_rulemarker, allrulelist); 2595 if (rule == NULL) { 2596 LIST_REMOVE(V_pf_rulemarker, allrulelist); 2597 LIST_INSERT_HEAD(&V_pf_allrulelist, V_pf_rulemarker, allrulelist); 2598 break; 2599 } 2600 2601 LIST_REMOVE(V_pf_rulemarker, allrulelist); 2602 LIST_INSERT_AFTER(rule, V_pf_rulemarker, allrulelist); 2603 2604 pf_counter_u64_periodic(&rule->evaluations); 2605 for (int i = 0; i < 2; i++) { 2606 pf_counter_u64_periodic(&rule->packets[i]); 2607 pf_counter_u64_periodic(&rule->bytes[i]); 2608 } 2609 } 2610 } 2611 2612 static void 2613 pf_counter_u64_periodic_main(void) 2614 { 2615 PF_RULES_RLOCK_TRACKER; 2616 2617 V_pf_counter_periodic_iter++; 2618 2619 PF_RULES_RLOCK(); 2620 pf_counter_u64_critical_enter(); 2621 pf_status_counter_u64_periodic(); 2622 pf_kif_counter_u64_periodic(); 2623 pf_rule_counter_u64_periodic(); 2624 pf_counter_u64_critical_exit(); 2625 PF_RULES_RUNLOCK(); 2626 } 2627 #else 2628 #define pf_counter_u64_periodic_main() do { } while (0) 2629 #endif 2630 2631 void 2632 pf_purge_thread(void *unused __unused) 2633 { 2634 struct epoch_tracker et; 2635 2636 VNET_ITERATOR_DECL(vnet_iter); 2637 2638 sx_xlock(&pf_end_lock); 2639 while (pf_end_threads == 0) { 2640 sx_sleep(pf_purge_thread, &pf_end_lock, 0, "pftm", pf_purge_thread_period); 2641 2642 VNET_LIST_RLOCK(); 2643 NET_EPOCH_ENTER(et); 2644 VNET_FOREACH(vnet_iter) { 2645 CURVNET_SET(vnet_iter); 2646 2647 /* Wait until V_pf_default_rule is initialized. */ 2648 if (V_pf_vnet_active == 0) { 2649 CURVNET_RESTORE(); 2650 continue; 2651 } 2652 2653 pf_counter_u64_periodic_main(); 2654 2655 /* 2656 * Process 1/interval fraction of the state 2657 * table every run. 2658 */ 2659 V_pf_purge_idx = 2660 pf_purge_expired_states(V_pf_purge_idx, V_pf_hashmask / 2661 (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10)); 2662 2663 /* 2664 * Purge other expired types every 2665 * PFTM_INTERVAL seconds. 2666 */ 2667 if (V_pf_purge_idx == 0) { 2668 /* 2669 * Order is important: 2670 * - states and src nodes reference rules 2671 * - states and rules reference kifs 2672 */ 2673 pf_purge_expired_fragments(); 2674 pf_purge_expired_src_nodes(); 2675 pf_purge_unlinked_rules(); 2676 pfi_kkif_purge(); 2677 } 2678 CURVNET_RESTORE(); 2679 } 2680 NET_EPOCH_EXIT(et); 2681 VNET_LIST_RUNLOCK(); 2682 } 2683 2684 pf_end_threads++; 2685 sx_xunlock(&pf_end_lock); 2686 kproc_exit(0); 2687 } 2688 2689 void 2690 pf_unload_vnet_purge(void) 2691 { 2692 2693 /* 2694 * To cleanse up all kifs and rules we need 2695 * two runs: first one clears reference flags, 2696 * then pf_purge_expired_states() doesn't 2697 * raise them, and then second run frees. 2698 */ 2699 pf_purge_unlinked_rules(); 2700 pfi_kkif_purge(); 2701 2702 /* 2703 * Now purge everything. 2704 */ 2705 pf_purge_expired_states(0, V_pf_hashmask); 2706 pf_purge_fragments(UINT_MAX); 2707 pf_purge_expired_src_nodes(); 2708 2709 /* 2710 * Now all kifs & rules should be unreferenced, 2711 * thus should be successfully freed. 2712 */ 2713 pf_purge_unlinked_rules(); 2714 pfi_kkif_purge(); 2715 } 2716 2717 u_int32_t 2718 pf_state_expires(const struct pf_kstate *state) 2719 { 2720 u_int32_t timeout; 2721 u_int32_t start; 2722 u_int32_t end; 2723 u_int32_t states; 2724 2725 /* handle all PFTM_* > PFTM_MAX here */ 2726 if (state->timeout == PFTM_PURGE) 2727 return (time_uptime); 2728 KASSERT(state->timeout != PFTM_UNLINKED, 2729 ("pf_state_expires: timeout == PFTM_UNLINKED")); 2730 KASSERT((state->timeout < PFTM_MAX), 2731 ("pf_state_expires: timeout > PFTM_MAX")); 2732 timeout = state->rule->timeout[state->timeout]; 2733 if (!timeout) 2734 timeout = V_pf_default_rule.timeout[state->timeout]; 2735 start = state->rule->timeout[PFTM_ADAPTIVE_START]; 2736 if (start && state->rule != &V_pf_default_rule) { 2737 end = state->rule->timeout[PFTM_ADAPTIVE_END]; 2738 states = counter_u64_fetch(state->rule->states_cur); 2739 } else { 2740 start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START]; 2741 end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END]; 2742 states = V_pf_status.states; 2743 } 2744 if (end && states > start && start < end) { 2745 if (states < end) { 2746 timeout = (u_int64_t)timeout * (end - states) / 2747 (end - start); 2748 return ((state->expire / 1000) + timeout); 2749 } 2750 else 2751 return (time_uptime); 2752 } 2753 return ((state->expire / 1000) + timeout); 2754 } 2755 2756 void 2757 pf_purge_expired_src_nodes(void) 2758 { 2759 struct pf_ksrc_node_list freelist; 2760 struct pf_srchash *sh; 2761 struct pf_ksrc_node *cur, *next; 2762 int i; 2763 2764 LIST_INIT(&freelist); 2765 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 2766 PF_HASHROW_LOCK(sh); 2767 LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next) 2768 if (cur->states == 0 && cur->expire <= time_uptime) { 2769 pf_unlink_src_node(cur); 2770 LIST_INSERT_HEAD(&freelist, cur, entry); 2771 } else if (cur->rule != NULL) 2772 cur->rule->rule_ref |= PFRULE_REFS; 2773 PF_HASHROW_UNLOCK(sh); 2774 } 2775 2776 pf_free_src_nodes(&freelist); 2777 2778 V_pf_status.src_nodes = uma_zone_get_cur(V_pf_sources_z); 2779 } 2780 2781 static void 2782 pf_src_tree_remove_state(struct pf_kstate *s) 2783 { 2784 uint32_t timeout; 2785 2786 timeout = s->rule->timeout[PFTM_SRC_NODE] ? 2787 s->rule->timeout[PFTM_SRC_NODE] : 2788 V_pf_default_rule.timeout[PFTM_SRC_NODE]; 2789 2790 for (pf_sn_types_t sn_type=0; sn_type<PF_SN_MAX; sn_type++) { 2791 if (s->sns[sn_type] == NULL) 2792 continue; 2793 PF_SRC_NODE_LOCK(s->sns[sn_type]); 2794 if (sn_type == PF_SN_LIMIT && s->src.tcp_est) 2795 --(s->sns[sn_type]->conn); 2796 if (--(s->sns[sn_type]->states) == 0) 2797 s->sns[sn_type]->expire = time_uptime + timeout; 2798 PF_SRC_NODE_UNLOCK(s->sns[sn_type]); 2799 s->sns[sn_type] = NULL; 2800 } 2801 2802 } 2803 2804 /* 2805 * Unlink and potentilly free a state. Function may be 2806 * called with ID hash row locked, but always returns 2807 * unlocked, since it needs to go through key hash locking. 2808 */ 2809 int 2810 pf_remove_state(struct pf_kstate *s) 2811 { 2812 struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; 2813 2814 NET_EPOCH_ASSERT(); 2815 PF_HASHROW_ASSERT(ih); 2816 2817 if (s->timeout == PFTM_UNLINKED) { 2818 /* 2819 * State is being processed 2820 * by pf_remove_state() in 2821 * an other thread. 2822 */ 2823 PF_HASHROW_UNLOCK(ih); 2824 return (0); /* XXXGL: undefined actually */ 2825 } 2826 2827 if (s->src.state == PF_TCPS_PROXY_DST) { 2828 /* XXX wire key the right one? */ 2829 pf_send_tcp(s->rule, s->key[PF_SK_WIRE]->af, 2830 &s->key[PF_SK_WIRE]->addr[1], 2831 &s->key[PF_SK_WIRE]->addr[0], 2832 s->key[PF_SK_WIRE]->port[1], 2833 s->key[PF_SK_WIRE]->port[0], 2834 s->src.seqhi, s->src.seqlo + 1, 2835 TH_RST|TH_ACK, 0, 0, 0, M_SKIP_FIREWALL, s->tag, 0, 2836 s->act.rtableid); 2837 } 2838 2839 LIST_REMOVE(s, entry); 2840 pf_src_tree_remove_state(s); 2841 2842 if (V_pfsync_delete_state_ptr != NULL) 2843 V_pfsync_delete_state_ptr(s); 2844 2845 STATE_DEC_COUNTERS(s); 2846 2847 s->timeout = PFTM_UNLINKED; 2848 2849 /* Ensure we remove it from the list of halfopen states, if needed. */ 2850 if (s->key[PF_SK_STACK] != NULL && 2851 s->key[PF_SK_STACK]->proto == IPPROTO_TCP) 2852 pf_set_protostate(s, PF_PEER_BOTH, TCPS_CLOSED); 2853 2854 PF_HASHROW_UNLOCK(ih); 2855 2856 pf_detach_state(s); 2857 2858 pf_udp_mapping_release(s->udp_mapping); 2859 2860 /* pf_state_insert() initialises refs to 2 */ 2861 return (pf_release_staten(s, 2)); 2862 } 2863 2864 struct pf_kstate * 2865 pf_alloc_state(int flags) 2866 { 2867 2868 return (uma_zalloc(V_pf_state_z, flags | M_ZERO)); 2869 } 2870 2871 void 2872 pf_free_state(struct pf_kstate *cur) 2873 { 2874 struct pf_krule_item *ri; 2875 2876 KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 2877 KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 2878 cur->timeout)); 2879 2880 while ((ri = SLIST_FIRST(&cur->match_rules))) { 2881 SLIST_REMOVE_HEAD(&cur->match_rules, entry); 2882 free(ri, M_PF_RULE_ITEM); 2883 } 2884 2885 pf_normalize_tcp_cleanup(cur); 2886 uma_zfree(V_pf_state_z, cur); 2887 pf_counter_u64_add(&V_pf_status.fcounters[FCNT_STATE_REMOVALS], 1); 2888 } 2889 2890 /* 2891 * Called only from pf_purge_thread(), thus serialized. 2892 */ 2893 static u_int 2894 pf_purge_expired_states(u_int i, int maxcheck) 2895 { 2896 struct pf_idhash *ih; 2897 struct pf_kstate *s; 2898 struct pf_krule_item *mrm; 2899 size_t count __unused; 2900 2901 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 2902 2903 /* 2904 * Go through hash and unlink states that expire now. 2905 */ 2906 while (maxcheck > 0) { 2907 count = 0; 2908 ih = &V_pf_idhash[i]; 2909 2910 /* only take the lock if we expect to do work */ 2911 if (!LIST_EMPTY(&ih->states)) { 2912 relock: 2913 PF_HASHROW_LOCK(ih); 2914 LIST_FOREACH(s, &ih->states, entry) { 2915 if (pf_state_expires(s) <= time_uptime) { 2916 V_pf_status.states -= 2917 pf_remove_state(s); 2918 goto relock; 2919 } 2920 s->rule->rule_ref |= PFRULE_REFS; 2921 if (s->nat_rule != NULL) 2922 s->nat_rule->rule_ref |= PFRULE_REFS; 2923 if (s->anchor != NULL) 2924 s->anchor->rule_ref |= PFRULE_REFS; 2925 s->kif->pfik_flags |= PFI_IFLAG_REFS; 2926 SLIST_FOREACH(mrm, &s->match_rules, entry) 2927 mrm->r->rule_ref |= PFRULE_REFS; 2928 if (s->act.rt_kif) 2929 s->act.rt_kif->pfik_flags |= PFI_IFLAG_REFS; 2930 count++; 2931 } 2932 PF_HASHROW_UNLOCK(ih); 2933 } 2934 2935 SDT_PROBE2(pf, purge, state, rowcount, i, count); 2936 2937 /* Return when we hit end of hash. */ 2938 if (++i > V_pf_hashmask) { 2939 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 2940 return (0); 2941 } 2942 2943 maxcheck--; 2944 } 2945 2946 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 2947 2948 return (i); 2949 } 2950 2951 static void 2952 pf_purge_unlinked_rules(void) 2953 { 2954 struct pf_krulequeue tmpq; 2955 struct pf_krule *r, *r1; 2956 2957 /* 2958 * If we have overloading task pending, then we'd 2959 * better skip purging this time. There is a tiny 2960 * probability that overloading task references 2961 * an already unlinked rule. 2962 */ 2963 PF_OVERLOADQ_LOCK(); 2964 if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 2965 PF_OVERLOADQ_UNLOCK(); 2966 return; 2967 } 2968 PF_OVERLOADQ_UNLOCK(); 2969 2970 /* 2971 * Do naive mark-and-sweep garbage collecting of old rules. 2972 * Reference flag is raised by pf_purge_expired_states() 2973 * and pf_purge_expired_src_nodes(). 2974 * 2975 * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 2976 * use a temporary queue. 2977 */ 2978 TAILQ_INIT(&tmpq); 2979 PF_UNLNKDRULES_LOCK(); 2980 TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 2981 if (!(r->rule_ref & PFRULE_REFS)) { 2982 TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 2983 TAILQ_INSERT_TAIL(&tmpq, r, entries); 2984 } else 2985 r->rule_ref &= ~PFRULE_REFS; 2986 } 2987 PF_UNLNKDRULES_UNLOCK(); 2988 2989 if (!TAILQ_EMPTY(&tmpq)) { 2990 PF_CONFIG_LOCK(); 2991 PF_RULES_WLOCK(); 2992 TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 2993 TAILQ_REMOVE(&tmpq, r, entries); 2994 pf_free_rule(r); 2995 } 2996 PF_RULES_WUNLOCK(); 2997 PF_CONFIG_UNLOCK(); 2998 } 2999 } 3000 3001 void 3002 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 3003 { 3004 switch (af) { 3005 #ifdef INET 3006 case AF_INET: { 3007 u_int32_t a = ntohl(addr->addr32[0]); 3008 printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 3009 (a>>8)&255, a&255); 3010 if (p) { 3011 p = ntohs(p); 3012 printf(":%u", p); 3013 } 3014 break; 3015 } 3016 #endif /* INET */ 3017 #ifdef INET6 3018 case AF_INET6: { 3019 u_int16_t b; 3020 u_int8_t i, curstart, curend, maxstart, maxend; 3021 curstart = curend = maxstart = maxend = 255; 3022 for (i = 0; i < 8; i++) { 3023 if (!addr->addr16[i]) { 3024 if (curstart == 255) 3025 curstart = i; 3026 curend = i; 3027 } else { 3028 if ((curend - curstart) > 3029 (maxend - maxstart)) { 3030 maxstart = curstart; 3031 maxend = curend; 3032 } 3033 curstart = curend = 255; 3034 } 3035 } 3036 if ((curend - curstart) > 3037 (maxend - maxstart)) { 3038 maxstart = curstart; 3039 maxend = curend; 3040 } 3041 for (i = 0; i < 8; i++) { 3042 if (i >= maxstart && i <= maxend) { 3043 if (i == 0) 3044 printf(":"); 3045 if (i == maxend) 3046 printf(":"); 3047 } else { 3048 b = ntohs(addr->addr16[i]); 3049 printf("%x", b); 3050 if (i < 7) 3051 printf(":"); 3052 } 3053 } 3054 if (p) { 3055 p = ntohs(p); 3056 printf("[%u]", p); 3057 } 3058 break; 3059 } 3060 #endif /* INET6 */ 3061 default: 3062 unhandled_af(af); 3063 } 3064 } 3065 3066 void 3067 pf_print_state(struct pf_kstate *s) 3068 { 3069 pf_print_state_parts(s, NULL, NULL); 3070 } 3071 3072 static void 3073 pf_print_state_parts(struct pf_kstate *s, 3074 struct pf_state_key *skwp, struct pf_state_key *sksp) 3075 { 3076 struct pf_state_key *skw, *sks; 3077 u_int8_t proto, dir; 3078 3079 /* Do our best to fill these, but they're skipped if NULL */ 3080 skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 3081 sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 3082 proto = skw ? skw->proto : (sks ? sks->proto : 0); 3083 dir = s ? s->direction : 0; 3084 3085 switch (proto) { 3086 case IPPROTO_IPV4: 3087 printf("IPv4"); 3088 break; 3089 case IPPROTO_IPV6: 3090 printf("IPv6"); 3091 break; 3092 case IPPROTO_TCP: 3093 printf("TCP"); 3094 break; 3095 case IPPROTO_UDP: 3096 printf("UDP"); 3097 break; 3098 case IPPROTO_ICMP: 3099 printf("ICMP"); 3100 break; 3101 case IPPROTO_ICMPV6: 3102 printf("ICMPv6"); 3103 break; 3104 default: 3105 printf("%u", proto); 3106 break; 3107 } 3108 switch (dir) { 3109 case PF_IN: 3110 printf(" in"); 3111 break; 3112 case PF_OUT: 3113 printf(" out"); 3114 break; 3115 } 3116 if (skw) { 3117 printf(" wire: "); 3118 pf_print_host(&skw->addr[0], skw->port[0], skw->af); 3119 printf(" "); 3120 pf_print_host(&skw->addr[1], skw->port[1], skw->af); 3121 } 3122 if (sks) { 3123 printf(" stack: "); 3124 if (sks != skw) { 3125 pf_print_host(&sks->addr[0], sks->port[0], sks->af); 3126 printf(" "); 3127 pf_print_host(&sks->addr[1], sks->port[1], sks->af); 3128 } else 3129 printf("-"); 3130 } 3131 if (s) { 3132 if (proto == IPPROTO_TCP) { 3133 printf(" [lo=%u high=%u win=%u modulator=%u", 3134 s->src.seqlo, s->src.seqhi, 3135 s->src.max_win, s->src.seqdiff); 3136 if (s->src.wscale && s->dst.wscale) 3137 printf(" wscale=%u", 3138 s->src.wscale & PF_WSCALE_MASK); 3139 printf("]"); 3140 printf(" [lo=%u high=%u win=%u modulator=%u", 3141 s->dst.seqlo, s->dst.seqhi, 3142 s->dst.max_win, s->dst.seqdiff); 3143 if (s->src.wscale && s->dst.wscale) 3144 printf(" wscale=%u", 3145 s->dst.wscale & PF_WSCALE_MASK); 3146 printf("]"); 3147 } 3148 printf(" %u:%u", s->src.state, s->dst.state); 3149 if (s->rule) 3150 printf(" @%d", s->rule->nr); 3151 } 3152 } 3153 3154 void 3155 pf_print_flags(uint16_t f) 3156 { 3157 if (f) 3158 printf(" "); 3159 if (f & TH_FIN) 3160 printf("F"); 3161 if (f & TH_SYN) 3162 printf("S"); 3163 if (f & TH_RST) 3164 printf("R"); 3165 if (f & TH_PUSH) 3166 printf("P"); 3167 if (f & TH_ACK) 3168 printf("A"); 3169 if (f & TH_URG) 3170 printf("U"); 3171 if (f & TH_ECE) 3172 printf("E"); 3173 if (f & TH_CWR) 3174 printf("W"); 3175 if (f & TH_AE) 3176 printf("e"); 3177 } 3178 3179 #define PF_SET_SKIP_STEPS(i) \ 3180 do { \ 3181 while (head[i] != cur) { \ 3182 head[i]->skip[i] = cur; \ 3183 head[i] = TAILQ_NEXT(head[i], entries); \ 3184 } \ 3185 } while (0) 3186 3187 void 3188 pf_calc_skip_steps(struct pf_krulequeue *rules) 3189 { 3190 struct pf_krule *cur, *prev, *head[PF_SKIP_COUNT]; 3191 int i; 3192 3193 cur = TAILQ_FIRST(rules); 3194 prev = cur; 3195 for (i = 0; i < PF_SKIP_COUNT; ++i) 3196 head[i] = cur; 3197 while (cur != NULL) { 3198 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 3199 PF_SET_SKIP_STEPS(PF_SKIP_IFP); 3200 if (cur->direction != prev->direction) 3201 PF_SET_SKIP_STEPS(PF_SKIP_DIR); 3202 if (cur->af != prev->af) 3203 PF_SET_SKIP_STEPS(PF_SKIP_AF); 3204 if (cur->proto != prev->proto) 3205 PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 3206 if (cur->src.neg != prev->src.neg || 3207 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 3208 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 3209 if (cur->dst.neg != prev->dst.neg || 3210 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 3211 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 3212 if (cur->src.port[0] != prev->src.port[0] || 3213 cur->src.port[1] != prev->src.port[1] || 3214 cur->src.port_op != prev->src.port_op) 3215 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 3216 if (cur->dst.port[0] != prev->dst.port[0] || 3217 cur->dst.port[1] != prev->dst.port[1] || 3218 cur->dst.port_op != prev->dst.port_op) 3219 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 3220 3221 prev = cur; 3222 cur = TAILQ_NEXT(cur, entries); 3223 } 3224 for (i = 0; i < PF_SKIP_COUNT; ++i) 3225 PF_SET_SKIP_STEPS(i); 3226 } 3227 3228 int 3229 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 3230 { 3231 if (aw1->type != aw2->type) 3232 return (1); 3233 switch (aw1->type) { 3234 case PF_ADDR_ADDRMASK: 3235 case PF_ADDR_RANGE: 3236 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, AF_INET6)) 3237 return (1); 3238 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, AF_INET6)) 3239 return (1); 3240 return (0); 3241 case PF_ADDR_DYNIFTL: 3242 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 3243 case PF_ADDR_NONE: 3244 case PF_ADDR_NOROUTE: 3245 case PF_ADDR_URPFFAILED: 3246 return (0); 3247 case PF_ADDR_TABLE: 3248 return (aw1->p.tbl != aw2->p.tbl); 3249 default: 3250 printf("invalid address type: %d\n", aw1->type); 3251 return (1); 3252 } 3253 } 3254 3255 /** 3256 * Checksum updates are a little complicated because the checksum in the TCP/UDP 3257 * header isn't always a full checksum. In some cases (i.e. output) it's a 3258 * pseudo-header checksum, which is a partial checksum over src/dst IP 3259 * addresses, protocol number and length. 3260 * 3261 * That means we have the following cases: 3262 * * Input or forwarding: we don't have TSO, the checksum fields are full 3263 * checksums, we need to update the checksum whenever we change anything. 3264 * * Output (i.e. the checksum is a pseudo-header checksum): 3265 * x The field being updated is src/dst address or affects the length of 3266 * the packet. We need to update the pseudo-header checksum (note that this 3267 * checksum is not ones' complement). 3268 * x Some other field is being modified (e.g. src/dst port numbers): We 3269 * don't have to update anything. 3270 **/ 3271 u_int16_t 3272 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 3273 { 3274 u_int32_t x; 3275 3276 x = cksum + old - new; 3277 x = (x + (x >> 16)) & 0xffff; 3278 3279 /* optimise: eliminate a branch when not udp */ 3280 if (udp && cksum == 0x0000) 3281 return cksum; 3282 if (udp && x == 0x0000) 3283 x = 0xffff; 3284 3285 return (u_int16_t)(x); 3286 } 3287 3288 static int 3289 pf_patch_8(struct pf_pdesc *pd, u_int8_t *f, u_int8_t v, bool hi) 3290 { 3291 int rewrite = 0; 3292 3293 if (*f != v) { 3294 uint16_t old = htons(hi ? (*f << 8) : *f); 3295 uint16_t new = htons(hi ? ( v << 8) : v); 3296 3297 *f = v; 3298 3299 if (! (pd->m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | 3300 CSUM_DELAY_DATA_IPV6))) 3301 *pd->pcksum = pf_cksum_fixup(*pd->pcksum, old, new, 3302 pd->proto == IPPROTO_UDP); 3303 3304 rewrite = 1; 3305 } 3306 3307 return (rewrite); 3308 } 3309 3310 int 3311 pf_patch_16(struct pf_pdesc *pd, void *f, u_int16_t v, bool hi) 3312 { 3313 int rewrite = 0; 3314 u_int8_t *fb = (u_int8_t *)f; 3315 u_int8_t *vb = (u_int8_t *)&v; 3316 3317 rewrite += pf_patch_8(pd, fb++, *vb++, hi); 3318 rewrite += pf_patch_8(pd, fb++, *vb++, !hi); 3319 3320 return (rewrite); 3321 } 3322 3323 int 3324 pf_patch_32(struct pf_pdesc *pd, void *f, u_int32_t v, bool hi) 3325 { 3326 int rewrite = 0; 3327 u_int8_t *fb = (u_int8_t *)f; 3328 u_int8_t *vb = (u_int8_t *)&v; 3329 3330 rewrite += pf_patch_8(pd, fb++, *vb++, hi); 3331 rewrite += pf_patch_8(pd, fb++, *vb++, !hi); 3332 rewrite += pf_patch_8(pd, fb++, *vb++, hi); 3333 rewrite += pf_patch_8(pd, fb++, *vb++, !hi); 3334 3335 return (rewrite); 3336 } 3337 3338 u_int16_t 3339 pf_proto_cksum_fixup(struct mbuf *m, u_int16_t cksum, u_int16_t old, 3340 u_int16_t new, u_int8_t udp) 3341 { 3342 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 3343 return (cksum); 3344 3345 return (pf_cksum_fixup(cksum, old, new, udp)); 3346 } 3347 3348 static void 3349 pf_change_ap(struct pf_pdesc *pd, struct pf_addr *a, u_int16_t *p, 3350 struct pf_addr *an, u_int16_t pn) 3351 { 3352 struct pf_addr ao; 3353 u_int16_t po; 3354 uint8_t u = pd->virtual_proto == IPPROTO_UDP; 3355 3356 MPASS(pd->pcksum); 3357 if (pd->af == AF_INET) { 3358 MPASS(pd->ip_sum); 3359 } 3360 3361 pf_addrcpy(&ao, a, pd->af); 3362 if (pd->af == pd->naf) 3363 pf_addrcpy(a, an, pd->af); 3364 3365 if (pd->m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 3366 *pd->pcksum = ~*pd->pcksum; 3367 3368 if (p == NULL) /* no port -> done. no cksum to worry about. */ 3369 return; 3370 po = *p; 3371 *p = pn; 3372 3373 switch (pd->af) { 3374 #ifdef INET 3375 case AF_INET: 3376 switch (pd->naf) { 3377 case AF_INET: 3378 *pd->ip_sum = pf_cksum_fixup(pf_cksum_fixup(*pd->ip_sum, 3379 ao.addr16[0], an->addr16[0], 0), 3380 ao.addr16[1], an->addr16[1], 0); 3381 *p = pn; 3382 3383 *pd->pcksum = pf_cksum_fixup(pf_cksum_fixup(*pd->pcksum, 3384 ao.addr16[0], an->addr16[0], u), 3385 ao.addr16[1], an->addr16[1], u); 3386 3387 *pd->pcksum = pf_proto_cksum_fixup(pd->m, *pd->pcksum, po, pn, u); 3388 break; 3389 #ifdef INET6 3390 case AF_INET6: 3391 *pd->pcksum = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3392 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3393 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pd->pcksum, 3394 ao.addr16[0], an->addr16[0], u), 3395 ao.addr16[1], an->addr16[1], u), 3396 0, an->addr16[2], u), 3397 0, an->addr16[3], u), 3398 0, an->addr16[4], u), 3399 0, an->addr16[5], u), 3400 0, an->addr16[6], u), 3401 0, an->addr16[7], u), 3402 po, pn, u); 3403 break; 3404 #endif /* INET6 */ 3405 default: 3406 unhandled_af(pd->naf); 3407 } 3408 break; 3409 #endif /* INET */ 3410 #ifdef INET6 3411 case AF_INET6: 3412 switch (pd->naf) { 3413 #ifdef INET 3414 case AF_INET: 3415 *pd->pcksum = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3416 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3417 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pd->pcksum, 3418 ao.addr16[0], an->addr16[0], u), 3419 ao.addr16[1], an->addr16[1], u), 3420 ao.addr16[2], 0, u), 3421 ao.addr16[3], 0, u), 3422 ao.addr16[4], 0, u), 3423 ao.addr16[5], 0, u), 3424 ao.addr16[6], 0, u), 3425 ao.addr16[7], 0, u), 3426 po, pn, u); 3427 break; 3428 #endif /* INET */ 3429 case AF_INET6: 3430 *pd->pcksum = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3431 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3432 pf_cksum_fixup(pf_cksum_fixup(*pd->pcksum, 3433 ao.addr16[0], an->addr16[0], u), 3434 ao.addr16[1], an->addr16[1], u), 3435 ao.addr16[2], an->addr16[2], u), 3436 ao.addr16[3], an->addr16[3], u), 3437 ao.addr16[4], an->addr16[4], u), 3438 ao.addr16[5], an->addr16[5], u), 3439 ao.addr16[6], an->addr16[6], u), 3440 ao.addr16[7], an->addr16[7], u); 3441 3442 *pd->pcksum = pf_proto_cksum_fixup(pd->m, *pd->pcksum, po, pn, u); 3443 break; 3444 default: 3445 unhandled_af(pd->naf); 3446 } 3447 break; 3448 #endif /* INET6 */ 3449 default: 3450 unhandled_af(pd->af); 3451 } 3452 3453 if (pd->m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | 3454 CSUM_DELAY_DATA_IPV6)) { 3455 *pd->pcksum = ~*pd->pcksum; 3456 if (! *pd->pcksum) 3457 *pd->pcksum = 0xffff; 3458 } 3459 } 3460 3461 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 3462 void 3463 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 3464 { 3465 u_int32_t ao; 3466 3467 memcpy(&ao, a, sizeof(ao)); 3468 memcpy(a, &an, sizeof(u_int32_t)); 3469 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 3470 ao % 65536, an % 65536, u); 3471 } 3472 3473 void 3474 pf_change_proto_a(struct mbuf *m, void *a, u_int16_t *c, u_int32_t an, u_int8_t udp) 3475 { 3476 u_int32_t ao; 3477 3478 memcpy(&ao, a, sizeof(ao)); 3479 memcpy(a, &an, sizeof(u_int32_t)); 3480 3481 *c = pf_proto_cksum_fixup(m, 3482 pf_proto_cksum_fixup(m, *c, ao / 65536, an / 65536, udp), 3483 ao % 65536, an % 65536, udp); 3484 } 3485 3486 #ifdef INET6 3487 static void 3488 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 3489 { 3490 struct pf_addr ao; 3491 3492 pf_addrcpy(&ao, a, AF_INET6); 3493 pf_addrcpy(a, an, AF_INET6); 3494 3495 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3496 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3497 pf_cksum_fixup(pf_cksum_fixup(*c, 3498 ao.addr16[0], an->addr16[0], u), 3499 ao.addr16[1], an->addr16[1], u), 3500 ao.addr16[2], an->addr16[2], u), 3501 ao.addr16[3], an->addr16[3], u), 3502 ao.addr16[4], an->addr16[4], u), 3503 ao.addr16[5], an->addr16[5], u), 3504 ao.addr16[6], an->addr16[6], u), 3505 ao.addr16[7], an->addr16[7], u); 3506 } 3507 #endif /* INET6 */ 3508 3509 static void 3510 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 3511 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 3512 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 3513 { 3514 struct pf_addr oia, ooa; 3515 3516 pf_addrcpy(&oia, ia, af); 3517 if (oa) 3518 pf_addrcpy(&ooa, oa, af); 3519 3520 /* Change inner protocol port, fix inner protocol checksum. */ 3521 if (ip != NULL) { 3522 u_int16_t oip = *ip; 3523 u_int32_t opc; 3524 3525 if (pc != NULL) 3526 opc = *pc; 3527 *ip = np; 3528 if (pc != NULL) 3529 *pc = pf_cksum_fixup(*pc, oip, *ip, u); 3530 *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 3531 if (pc != NULL) 3532 *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 3533 } 3534 /* Change inner ip address, fix inner ip and icmp checksums. */ 3535 pf_addrcpy(ia, na, af); 3536 switch (af) { 3537 #ifdef INET 3538 case AF_INET: { 3539 u_int32_t oh2c = *h2c; 3540 3541 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 3542 oia.addr16[0], ia->addr16[0], 0), 3543 oia.addr16[1], ia->addr16[1], 0); 3544 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 3545 oia.addr16[0], ia->addr16[0], 0), 3546 oia.addr16[1], ia->addr16[1], 0); 3547 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 3548 break; 3549 } 3550 #endif /* INET */ 3551 #ifdef INET6 3552 case AF_INET6: 3553 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3554 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3555 pf_cksum_fixup(pf_cksum_fixup(*ic, 3556 oia.addr16[0], ia->addr16[0], u), 3557 oia.addr16[1], ia->addr16[1], u), 3558 oia.addr16[2], ia->addr16[2], u), 3559 oia.addr16[3], ia->addr16[3], u), 3560 oia.addr16[4], ia->addr16[4], u), 3561 oia.addr16[5], ia->addr16[5], u), 3562 oia.addr16[6], ia->addr16[6], u), 3563 oia.addr16[7], ia->addr16[7], u); 3564 break; 3565 #endif /* INET6 */ 3566 } 3567 /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 3568 if (oa) { 3569 pf_addrcpy(oa, na, af); 3570 switch (af) { 3571 #ifdef INET 3572 case AF_INET: 3573 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 3574 ooa.addr16[0], oa->addr16[0], 0), 3575 ooa.addr16[1], oa->addr16[1], 0); 3576 break; 3577 #endif /* INET */ 3578 #ifdef INET6 3579 case AF_INET6: 3580 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3581 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 3582 pf_cksum_fixup(pf_cksum_fixup(*ic, 3583 ooa.addr16[0], oa->addr16[0], u), 3584 ooa.addr16[1], oa->addr16[1], u), 3585 ooa.addr16[2], oa->addr16[2], u), 3586 ooa.addr16[3], oa->addr16[3], u), 3587 ooa.addr16[4], oa->addr16[4], u), 3588 ooa.addr16[5], oa->addr16[5], u), 3589 ooa.addr16[6], oa->addr16[6], u), 3590 ooa.addr16[7], oa->addr16[7], u); 3591 break; 3592 #endif /* INET6 */ 3593 } 3594 } 3595 } 3596 3597 int 3598 pf_translate_af(struct pf_pdesc *pd) 3599 { 3600 #if defined(INET) && defined(INET6) 3601 struct mbuf *mp; 3602 struct ip *ip4; 3603 struct ip6_hdr *ip6; 3604 struct icmp6_hdr *icmp; 3605 struct m_tag *mtag; 3606 struct pf_fragment_tag *ftag; 3607 int hlen; 3608 3609 hlen = pd->naf == AF_INET ? sizeof(*ip4) : sizeof(*ip6); 3610 3611 /* trim the old header */ 3612 m_adj(pd->m, pd->off); 3613 3614 /* prepend a new one */ 3615 M_PREPEND(pd->m, hlen, M_NOWAIT); 3616 if (pd->m == NULL) 3617 return (-1); 3618 3619 switch (pd->naf) { 3620 case AF_INET: 3621 ip4 = mtod(pd->m, struct ip *); 3622 bzero(ip4, hlen); 3623 ip4->ip_v = IPVERSION; 3624 ip4->ip_hl = hlen >> 2; 3625 ip4->ip_tos = pd->tos; 3626 ip4->ip_len = htons(hlen + (pd->tot_len - pd->off)); 3627 ip_fillid(ip4, V_ip_random_id); 3628 ip4->ip_ttl = pd->ttl; 3629 ip4->ip_p = pd->proto; 3630 ip4->ip_src = pd->nsaddr.v4; 3631 ip4->ip_dst = pd->ndaddr.v4; 3632 pd->src = (struct pf_addr *)&ip4->ip_src; 3633 pd->dst = (struct pf_addr *)&ip4->ip_dst; 3634 pd->off = sizeof(struct ip); 3635 break; 3636 case AF_INET6: 3637 ip6 = mtod(pd->m, struct ip6_hdr *); 3638 bzero(ip6, hlen); 3639 ip6->ip6_vfc = IPV6_VERSION; 3640 ip6->ip6_flow |= htonl((u_int32_t)pd->tos << 20); 3641 ip6->ip6_plen = htons(pd->tot_len - pd->off); 3642 ip6->ip6_nxt = pd->proto; 3643 if (!pd->ttl || pd->ttl > IPV6_DEFHLIM) 3644 ip6->ip6_hlim = IPV6_DEFHLIM; 3645 else 3646 ip6->ip6_hlim = pd->ttl; 3647 ip6->ip6_src = pd->nsaddr.v6; 3648 ip6->ip6_dst = pd->ndaddr.v6; 3649 pd->src = (struct pf_addr *)&ip6->ip6_src; 3650 pd->dst = (struct pf_addr *)&ip6->ip6_dst; 3651 pd->off = sizeof(struct ip6_hdr); 3652 3653 /* 3654 * If we're dealing with a reassembled packet we need to adjust 3655 * the header length from the IPv4 header size to IPv6 header 3656 * size. 3657 */ 3658 mtag = m_tag_find(pd->m, PACKET_TAG_PF_REASSEMBLED, NULL); 3659 if (mtag) { 3660 ftag = (struct pf_fragment_tag *)(mtag + 1); 3661 ftag->ft_hdrlen = sizeof(*ip6); 3662 ftag->ft_maxlen -= sizeof(struct ip6_hdr) - 3663 sizeof(struct ip) + sizeof(struct ip6_frag); 3664 } 3665 break; 3666 default: 3667 return (-1); 3668 } 3669 3670 /* recalculate icmp/icmp6 checksums */ 3671 if (pd->proto == IPPROTO_ICMP || pd->proto == IPPROTO_ICMPV6) { 3672 int off; 3673 if ((mp = m_pulldown(pd->m, hlen, sizeof(*icmp), &off)) == 3674 NULL) { 3675 pd->m = NULL; 3676 return (-1); 3677 } 3678 icmp = (struct icmp6_hdr *)(mp->m_data + off); 3679 icmp->icmp6_cksum = 0; 3680 icmp->icmp6_cksum = pd->naf == AF_INET ? 3681 in4_cksum(pd->m, 0, hlen, ntohs(ip4->ip_len) - hlen) : 3682 in6_cksum(pd->m, IPPROTO_ICMPV6, hlen, 3683 ntohs(ip6->ip6_plen)); 3684 } 3685 #endif /* INET && INET6 */ 3686 3687 return (0); 3688 } 3689 3690 int 3691 pf_change_icmp_af(struct mbuf *m, int off, struct pf_pdesc *pd, 3692 struct pf_pdesc *pd2, struct pf_addr *src, struct pf_addr *dst, 3693 sa_family_t af, sa_family_t naf) 3694 { 3695 #if defined(INET) && defined(INET6) 3696 struct mbuf *n = NULL; 3697 struct ip *ip4; 3698 struct ip6_hdr *ip6; 3699 int hlen, olen, mlen; 3700 3701 if (af == naf || (af != AF_INET && af != AF_INET6) || 3702 (naf != AF_INET && naf != AF_INET6)) 3703 return (-1); 3704 3705 /* split the mbuf chain on the inner ip/ip6 header boundary */ 3706 if ((n = m_split(m, off, M_NOWAIT)) == NULL) 3707 return (-1); 3708 3709 /* old header */ 3710 olen = pd2->off - off; 3711 /* new header */ 3712 hlen = naf == AF_INET ? sizeof(*ip4) : sizeof(*ip6); 3713 3714 /* trim old header */ 3715 m_adj(n, olen); 3716 3717 /* prepend a new one */ 3718 M_PREPEND(n, hlen, M_NOWAIT); 3719 if (n == NULL) 3720 return (-1); 3721 3722 /* translate inner ip/ip6 header */ 3723 switch (naf) { 3724 case AF_INET: 3725 ip4 = mtod(n, struct ip *); 3726 bzero(ip4, sizeof(*ip4)); 3727 ip4->ip_v = IPVERSION; 3728 ip4->ip_hl = sizeof(*ip4) >> 2; 3729 ip4->ip_len = htons(sizeof(*ip4) + pd2->tot_len - olen); 3730 ip_fillid(ip4, V_ip_random_id); 3731 ip4->ip_off = htons(IP_DF); 3732 ip4->ip_ttl = pd2->ttl; 3733 if (pd2->proto == IPPROTO_ICMPV6) 3734 ip4->ip_p = IPPROTO_ICMP; 3735 else 3736 ip4->ip_p = pd2->proto; 3737 ip4->ip_src = src->v4; 3738 ip4->ip_dst = dst->v4; 3739 ip4->ip_sum = in_cksum(n, ip4->ip_hl << 2); 3740 break; 3741 case AF_INET6: 3742 ip6 = mtod(n, struct ip6_hdr *); 3743 bzero(ip6, sizeof(*ip6)); 3744 ip6->ip6_vfc = IPV6_VERSION; 3745 ip6->ip6_plen = htons(pd2->tot_len - olen); 3746 if (pd2->proto == IPPROTO_ICMP) 3747 ip6->ip6_nxt = IPPROTO_ICMPV6; 3748 else 3749 ip6->ip6_nxt = pd2->proto; 3750 if (!pd2->ttl || pd2->ttl > IPV6_DEFHLIM) 3751 ip6->ip6_hlim = IPV6_DEFHLIM; 3752 else 3753 ip6->ip6_hlim = pd2->ttl; 3754 ip6->ip6_src = src->v6; 3755 ip6->ip6_dst = dst->v6; 3756 break; 3757 default: 3758 unhandled_af(naf); 3759 } 3760 3761 /* adjust payload offset and total packet length */ 3762 pd2->off += hlen - olen; 3763 pd->tot_len += hlen - olen; 3764 3765 /* merge modified inner packet with the original header */ 3766 mlen = n->m_pkthdr.len; 3767 m_cat(m, n); 3768 m->m_pkthdr.len += mlen; 3769 #endif /* INET && INET6 */ 3770 3771 return (0); 3772 } 3773 3774 #define PTR_IP(field) (offsetof(struct ip, field)) 3775 #define PTR_IP6(field) (offsetof(struct ip6_hdr, field)) 3776 3777 int 3778 pf_translate_icmp_af(int af, void *arg) 3779 { 3780 #if defined(INET) && defined(INET6) 3781 struct icmp *icmp4; 3782 struct icmp6_hdr *icmp6; 3783 u_int32_t mtu; 3784 int32_t ptr = -1; 3785 u_int8_t type; 3786 u_int8_t code; 3787 3788 switch (af) { 3789 case AF_INET: 3790 icmp6 = arg; 3791 type = icmp6->icmp6_type; 3792 code = icmp6->icmp6_code; 3793 mtu = ntohl(icmp6->icmp6_mtu); 3794 3795 switch (type) { 3796 case ICMP6_ECHO_REQUEST: 3797 type = ICMP_ECHO; 3798 break; 3799 case ICMP6_ECHO_REPLY: 3800 type = ICMP_ECHOREPLY; 3801 break; 3802 case ICMP6_DST_UNREACH: 3803 type = ICMP_UNREACH; 3804 switch (code) { 3805 case ICMP6_DST_UNREACH_NOROUTE: 3806 case ICMP6_DST_UNREACH_BEYONDSCOPE: 3807 case ICMP6_DST_UNREACH_ADDR: 3808 code = ICMP_UNREACH_HOST; 3809 break; 3810 case ICMP6_DST_UNREACH_ADMIN: 3811 code = ICMP_UNREACH_HOST_PROHIB; 3812 break; 3813 case ICMP6_DST_UNREACH_NOPORT: 3814 code = ICMP_UNREACH_PORT; 3815 break; 3816 default: 3817 return (-1); 3818 } 3819 break; 3820 case ICMP6_PACKET_TOO_BIG: 3821 type = ICMP_UNREACH; 3822 code = ICMP_UNREACH_NEEDFRAG; 3823 mtu -= 20; 3824 break; 3825 case ICMP6_TIME_EXCEEDED: 3826 type = ICMP_TIMXCEED; 3827 break; 3828 case ICMP6_PARAM_PROB: 3829 switch (code) { 3830 case ICMP6_PARAMPROB_HEADER: 3831 type = ICMP_PARAMPROB; 3832 code = ICMP_PARAMPROB_ERRATPTR; 3833 ptr = ntohl(icmp6->icmp6_pptr); 3834 3835 if (ptr == PTR_IP6(ip6_vfc)) 3836 ; /* preserve */ 3837 else if (ptr == PTR_IP6(ip6_vfc) + 1) 3838 ptr = PTR_IP(ip_tos); 3839 else if (ptr == PTR_IP6(ip6_plen) || 3840 ptr == PTR_IP6(ip6_plen) + 1) 3841 ptr = PTR_IP(ip_len); 3842 else if (ptr == PTR_IP6(ip6_nxt)) 3843 ptr = PTR_IP(ip_p); 3844 else if (ptr == PTR_IP6(ip6_hlim)) 3845 ptr = PTR_IP(ip_ttl); 3846 else if (ptr >= PTR_IP6(ip6_src) && 3847 ptr < PTR_IP6(ip6_dst)) 3848 ptr = PTR_IP(ip_src); 3849 else if (ptr >= PTR_IP6(ip6_dst) && 3850 ptr < sizeof(struct ip6_hdr)) 3851 ptr = PTR_IP(ip_dst); 3852 else { 3853 return (-1); 3854 } 3855 break; 3856 case ICMP6_PARAMPROB_NEXTHEADER: 3857 type = ICMP_UNREACH; 3858 code = ICMP_UNREACH_PROTOCOL; 3859 break; 3860 default: 3861 return (-1); 3862 } 3863 break; 3864 default: 3865 return (-1); 3866 } 3867 if (icmp6->icmp6_type != type) { 3868 icmp6->icmp6_cksum = pf_cksum_fixup(icmp6->icmp6_cksum, 3869 icmp6->icmp6_type, type, 0); 3870 icmp6->icmp6_type = type; 3871 } 3872 if (icmp6->icmp6_code != code) { 3873 icmp6->icmp6_cksum = pf_cksum_fixup(icmp6->icmp6_cksum, 3874 icmp6->icmp6_code, code, 0); 3875 icmp6->icmp6_code = code; 3876 } 3877 if (icmp6->icmp6_mtu != htonl(mtu)) { 3878 icmp6->icmp6_cksum = pf_cksum_fixup(icmp6->icmp6_cksum, 3879 htons(ntohl(icmp6->icmp6_mtu)), htons(mtu), 0); 3880 /* aligns well with a icmpv4 nextmtu */ 3881 icmp6->icmp6_mtu = htonl(mtu); 3882 } 3883 if (ptr >= 0 && icmp6->icmp6_pptr != htonl(ptr)) { 3884 icmp6->icmp6_cksum = pf_cksum_fixup(icmp6->icmp6_cksum, 3885 htons(ntohl(icmp6->icmp6_pptr)), htons(ptr), 0); 3886 /* icmpv4 pptr is a one most significant byte */ 3887 icmp6->icmp6_pptr = htonl(ptr << 24); 3888 } 3889 break; 3890 case AF_INET6: 3891 icmp4 = arg; 3892 type = icmp4->icmp_type; 3893 code = icmp4->icmp_code; 3894 mtu = ntohs(icmp4->icmp_nextmtu); 3895 3896 switch (type) { 3897 case ICMP_ECHO: 3898 type = ICMP6_ECHO_REQUEST; 3899 break; 3900 case ICMP_ECHOREPLY: 3901 type = ICMP6_ECHO_REPLY; 3902 break; 3903 case ICMP_UNREACH: 3904 type = ICMP6_DST_UNREACH; 3905 switch (code) { 3906 case ICMP_UNREACH_NET: 3907 case ICMP_UNREACH_HOST: 3908 case ICMP_UNREACH_NET_UNKNOWN: 3909 case ICMP_UNREACH_HOST_UNKNOWN: 3910 case ICMP_UNREACH_ISOLATED: 3911 case ICMP_UNREACH_TOSNET: 3912 case ICMP_UNREACH_TOSHOST: 3913 code = ICMP6_DST_UNREACH_NOROUTE; 3914 break; 3915 case ICMP_UNREACH_PORT: 3916 code = ICMP6_DST_UNREACH_NOPORT; 3917 break; 3918 case ICMP_UNREACH_NET_PROHIB: 3919 case ICMP_UNREACH_HOST_PROHIB: 3920 case ICMP_UNREACH_FILTER_PROHIB: 3921 case ICMP_UNREACH_PRECEDENCE_CUTOFF: 3922 code = ICMP6_DST_UNREACH_ADMIN; 3923 break; 3924 case ICMP_UNREACH_PROTOCOL: 3925 type = ICMP6_PARAM_PROB; 3926 code = ICMP6_PARAMPROB_NEXTHEADER; 3927 ptr = offsetof(struct ip6_hdr, ip6_nxt); 3928 break; 3929 case ICMP_UNREACH_NEEDFRAG: 3930 type = ICMP6_PACKET_TOO_BIG; 3931 code = 0; 3932 mtu += 20; 3933 break; 3934 default: 3935 return (-1); 3936 } 3937 break; 3938 case ICMP_TIMXCEED: 3939 type = ICMP6_TIME_EXCEEDED; 3940 break; 3941 case ICMP_PARAMPROB: 3942 type = ICMP6_PARAM_PROB; 3943 switch (code) { 3944 case ICMP_PARAMPROB_ERRATPTR: 3945 code = ICMP6_PARAMPROB_HEADER; 3946 break; 3947 case ICMP_PARAMPROB_LENGTH: 3948 code = ICMP6_PARAMPROB_HEADER; 3949 break; 3950 default: 3951 return (-1); 3952 } 3953 3954 ptr = icmp4->icmp_pptr; 3955 if (ptr == 0 || ptr == PTR_IP(ip_tos)) 3956 ; /* preserve */ 3957 else if (ptr == PTR_IP(ip_len) || 3958 ptr == PTR_IP(ip_len) + 1) 3959 ptr = PTR_IP6(ip6_plen); 3960 else if (ptr == PTR_IP(ip_ttl)) 3961 ptr = PTR_IP6(ip6_hlim); 3962 else if (ptr == PTR_IP(ip_p)) 3963 ptr = PTR_IP6(ip6_nxt); 3964 else if (ptr >= PTR_IP(ip_src) && ptr < PTR_IP(ip_dst)) 3965 ptr = PTR_IP6(ip6_src); 3966 else if (ptr >= PTR_IP(ip_dst) && 3967 ptr < sizeof(struct ip)) 3968 ptr = PTR_IP6(ip6_dst); 3969 else { 3970 return (-1); 3971 } 3972 break; 3973 default: 3974 return (-1); 3975 } 3976 if (icmp4->icmp_type != type) { 3977 icmp4->icmp_cksum = pf_cksum_fixup(icmp4->icmp_cksum, 3978 icmp4->icmp_type, type, 0); 3979 icmp4->icmp_type = type; 3980 } 3981 if (icmp4->icmp_code != code) { 3982 icmp4->icmp_cksum = pf_cksum_fixup(icmp4->icmp_cksum, 3983 icmp4->icmp_code, code, 0); 3984 icmp4->icmp_code = code; 3985 } 3986 if (icmp4->icmp_nextmtu != htons(mtu)) { 3987 icmp4->icmp_cksum = pf_cksum_fixup(icmp4->icmp_cksum, 3988 icmp4->icmp_nextmtu, htons(mtu), 0); 3989 icmp4->icmp_nextmtu = htons(mtu); 3990 } 3991 if (ptr >= 0 && icmp4->icmp_void != ptr) { 3992 icmp4->icmp_cksum = pf_cksum_fixup(icmp4->icmp_cksum, 3993 htons(icmp4->icmp_pptr), htons(ptr), 0); 3994 icmp4->icmp_void = htonl(ptr); 3995 } 3996 break; 3997 default: 3998 unhandled_af(af); 3999 } 4000 #endif /* INET && INET6 */ 4001 4002 return (0); 4003 } 4004 4005 /* 4006 * Need to modulate the sequence numbers in the TCP SACK option 4007 * (credits to Krzysztof Pfaff for report and patch) 4008 */ 4009 static int 4010 pf_modulate_sack(struct pf_pdesc *pd, struct tcphdr *th, 4011 struct pf_state_peer *dst) 4012 { 4013 struct sackblk sack; 4014 int copyback = 0, i; 4015 int olen, optsoff; 4016 uint8_t opts[MAX_TCPOPTLEN], *opt, *eoh; 4017 4018 olen = (pd->hdr.tcp.th_off << 2) - sizeof(struct tcphdr); 4019 optsoff = pd->off + sizeof(struct tcphdr); 4020 #define TCPOLEN_MINSACK (TCPOLEN_SACK + 2) 4021 if (olen < TCPOLEN_MINSACK || 4022 !pf_pull_hdr(pd->m, optsoff, opts, olen, NULL, NULL, pd->af)) 4023 return (0); 4024 4025 eoh = opts + olen; 4026 opt = opts; 4027 while ((opt = pf_find_tcpopt(opt, opts, olen, 4028 TCPOPT_SACK, TCPOLEN_MINSACK)) != NULL) 4029 { 4030 size_t safelen = MIN(opt[1], (eoh - opt)); 4031 for (i = 2; i + TCPOLEN_SACK <= safelen; i += TCPOLEN_SACK) { 4032 size_t startoff = (opt + i) - opts; 4033 memcpy(&sack, &opt[i], sizeof(sack)); 4034 pf_patch_32(pd, &sack.start, 4035 htonl(ntohl(sack.start) - dst->seqdiff), 4036 PF_ALGNMNT(startoff)); 4037 pf_patch_32(pd, &sack.end, 4038 htonl(ntohl(sack.end) - dst->seqdiff), 4039 PF_ALGNMNT(startoff + sizeof(sack.start))); 4040 memcpy(&opt[i], &sack, sizeof(sack)); 4041 } 4042 copyback = 1; 4043 opt += opt[1]; 4044 } 4045 4046 if (copyback) 4047 m_copyback(pd->m, optsoff, olen, (caddr_t)opts); 4048 4049 return (copyback); 4050 } 4051 4052 struct mbuf * 4053 pf_build_tcp(const struct pf_krule *r, sa_family_t af, 4054 const struct pf_addr *saddr, const struct pf_addr *daddr, 4055 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 4056 u_int8_t tcp_flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, 4057 int mbuf_flags, u_int16_t mtag_tag, u_int16_t mtag_flags, u_int sack, 4058 int rtableid) 4059 { 4060 struct mbuf *m; 4061 int len, tlen; 4062 #ifdef INET 4063 struct ip *h = NULL; 4064 #endif /* INET */ 4065 #ifdef INET6 4066 struct ip6_hdr *h6 = NULL; 4067 #endif /* INET6 */ 4068 struct tcphdr *th; 4069 char *opt; 4070 struct pf_mtag *pf_mtag; 4071 4072 len = 0; 4073 th = NULL; 4074 4075 /* maximum segment size tcp option */ 4076 tlen = sizeof(struct tcphdr); 4077 if (mss) 4078 tlen += 4; 4079 if (sack) 4080 tlen += 2; 4081 4082 switch (af) { 4083 #ifdef INET 4084 case AF_INET: 4085 len = sizeof(struct ip) + tlen; 4086 break; 4087 #endif /* INET */ 4088 #ifdef INET6 4089 case AF_INET6: 4090 len = sizeof(struct ip6_hdr) + tlen; 4091 break; 4092 #endif /* INET6 */ 4093 default: 4094 unhandled_af(af); 4095 } 4096 4097 m = m_gethdr(M_NOWAIT, MT_DATA); 4098 if (m == NULL) 4099 return (NULL); 4100 4101 #ifdef MAC 4102 mac_netinet_firewall_send(m); 4103 #endif 4104 if ((pf_mtag = pf_get_mtag(m)) == NULL) { 4105 m_freem(m); 4106 return (NULL); 4107 } 4108 m->m_flags |= mbuf_flags; 4109 pf_mtag->tag = mtag_tag; 4110 pf_mtag->flags = mtag_flags; 4111 4112 if (rtableid >= 0) 4113 M_SETFIB(m, rtableid); 4114 4115 #ifdef ALTQ 4116 if (r != NULL && r->qid) { 4117 pf_mtag->qid = r->qid; 4118 4119 /* add hints for ecn */ 4120 pf_mtag->hdr = mtod(m, struct ip *); 4121 } 4122 #endif /* ALTQ */ 4123 m->m_data += max_linkhdr; 4124 m->m_pkthdr.len = m->m_len = len; 4125 /* The rest of the stack assumes a rcvif, so provide one. 4126 * This is a locally generated packet, so .. close enough. */ 4127 m->m_pkthdr.rcvif = V_loif; 4128 bzero(m->m_data, len); 4129 switch (af) { 4130 #ifdef INET 4131 case AF_INET: 4132 m->m_pkthdr.csum_flags |= CSUM_TCP; 4133 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 4134 4135 h = mtod(m, struct ip *); 4136 4137 h->ip_p = IPPROTO_TCP; 4138 h->ip_len = htons(tlen); 4139 h->ip_v = 4; 4140 h->ip_hl = sizeof(*h) >> 2; 4141 h->ip_tos = IPTOS_LOWDELAY; 4142 h->ip_len = htons(len); 4143 h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 4144 h->ip_ttl = ttl ? ttl : V_ip_defttl; 4145 h->ip_sum = 0; 4146 h->ip_src.s_addr = saddr->v4.s_addr; 4147 h->ip_dst.s_addr = daddr->v4.s_addr; 4148 4149 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 4150 th->th_sum = in_pseudo(h->ip_src.s_addr, h->ip_dst.s_addr, 4151 htons(len - sizeof(struct ip) + IPPROTO_TCP)); 4152 break; 4153 #endif /* INET */ 4154 #ifdef INET6 4155 case AF_INET6: 4156 m->m_pkthdr.csum_flags |= CSUM_TCP_IPV6; 4157 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 4158 4159 h6 = mtod(m, struct ip6_hdr *); 4160 4161 /* IP header fields included in the TCP checksum */ 4162 h6->ip6_nxt = IPPROTO_TCP; 4163 h6->ip6_plen = htons(tlen); 4164 h6->ip6_vfc |= IPV6_VERSION; 4165 h6->ip6_hlim = V_ip6_defhlim; 4166 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 4167 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 4168 4169 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 4170 th->th_sum = in6_cksum_pseudo(h6, len - sizeof(struct ip6_hdr), 4171 IPPROTO_TCP, 0); 4172 break; 4173 #endif /* INET6 */ 4174 } 4175 4176 /* TCP header */ 4177 th->th_sport = sport; 4178 th->th_dport = dport; 4179 th->th_seq = htonl(seq); 4180 th->th_ack = htonl(ack); 4181 th->th_off = tlen >> 2; 4182 tcp_set_flags(th, tcp_flags); 4183 th->th_win = htons(win); 4184 4185 opt = (char *)(th + 1); 4186 if (mss) { 4187 opt = (char *)(th + 1); 4188 opt[0] = TCPOPT_MAXSEG; 4189 opt[1] = 4; 4190 mss = htons(mss); 4191 memcpy((opt + 2), &mss, 2); 4192 opt += 4; 4193 } 4194 if (sack) { 4195 opt[0] = TCPOPT_SACK_PERMITTED; 4196 opt[1] = 2; 4197 opt += 2; 4198 } 4199 4200 return (m); 4201 } 4202 4203 static void 4204 pf_send_sctp_abort(sa_family_t af, struct pf_pdesc *pd, 4205 uint8_t ttl, int rtableid) 4206 { 4207 struct mbuf *m; 4208 #ifdef INET 4209 struct ip *h = NULL; 4210 #endif /* INET */ 4211 #ifdef INET6 4212 struct ip6_hdr *h6 = NULL; 4213 #endif /* INET6 */ 4214 struct sctphdr *hdr; 4215 struct sctp_chunkhdr *chunk; 4216 struct pf_send_entry *pfse; 4217 int off = 0; 4218 4219 MPASS(af == pd->af); 4220 4221 m = m_gethdr(M_NOWAIT, MT_DATA); 4222 if (m == NULL) 4223 return; 4224 4225 m->m_data += max_linkhdr; 4226 m->m_flags |= M_SKIP_FIREWALL; 4227 /* The rest of the stack assumes a rcvif, so provide one. 4228 * This is a locally generated packet, so .. close enough. */ 4229 m->m_pkthdr.rcvif = V_loif; 4230 4231 /* IPv4|6 header */ 4232 switch (af) { 4233 #ifdef INET 4234 case AF_INET: 4235 bzero(m->m_data, sizeof(struct ip) + sizeof(*hdr) + sizeof(*chunk)); 4236 4237 h = mtod(m, struct ip *); 4238 4239 /* IP header fields included in the TCP checksum */ 4240 4241 h->ip_p = IPPROTO_SCTP; 4242 h->ip_len = htons(sizeof(*h) + sizeof(*hdr) + sizeof(*chunk)); 4243 h->ip_ttl = ttl ? ttl : V_ip_defttl; 4244 h->ip_src = pd->dst->v4; 4245 h->ip_dst = pd->src->v4; 4246 4247 off += sizeof(struct ip); 4248 break; 4249 #endif /* INET */ 4250 #ifdef INET6 4251 case AF_INET6: 4252 bzero(m->m_data, sizeof(struct ip6_hdr) + sizeof(*hdr) + sizeof(*chunk)); 4253 4254 h6 = mtod(m, struct ip6_hdr *); 4255 4256 /* IP header fields included in the TCP checksum */ 4257 h6->ip6_vfc |= IPV6_VERSION; 4258 h6->ip6_nxt = IPPROTO_SCTP; 4259 h6->ip6_plen = htons(sizeof(*h6) + sizeof(*hdr) + sizeof(*chunk)); 4260 h6->ip6_hlim = ttl ? ttl : V_ip6_defhlim; 4261 memcpy(&h6->ip6_src, &pd->dst->v6, sizeof(struct in6_addr)); 4262 memcpy(&h6->ip6_dst, &pd->src->v6, sizeof(struct in6_addr)); 4263 4264 off += sizeof(struct ip6_hdr); 4265 break; 4266 #endif /* INET6 */ 4267 default: 4268 unhandled_af(af); 4269 } 4270 4271 /* SCTP header */ 4272 hdr = mtodo(m, off); 4273 4274 hdr->src_port = pd->hdr.sctp.dest_port; 4275 hdr->dest_port = pd->hdr.sctp.src_port; 4276 hdr->v_tag = pd->sctp_initiate_tag; 4277 hdr->checksum = 0; 4278 4279 /* Abort chunk. */ 4280 off += sizeof(struct sctphdr); 4281 chunk = mtodo(m, off); 4282 4283 chunk->chunk_type = SCTP_ABORT_ASSOCIATION; 4284 chunk->chunk_length = htons(sizeof(*chunk)); 4285 4286 /* SCTP checksum */ 4287 off += sizeof(*chunk); 4288 m->m_pkthdr.len = m->m_len = off; 4289 4290 pf_sctp_checksum(m, off - sizeof(*hdr) - sizeof(*chunk)); 4291 4292 if (rtableid >= 0) 4293 M_SETFIB(m, rtableid); 4294 4295 /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 4296 pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 4297 if (pfse == NULL) { 4298 m_freem(m); 4299 return; 4300 } 4301 4302 switch (af) { 4303 #ifdef INET 4304 case AF_INET: 4305 pfse->pfse_type = PFSE_IP; 4306 break; 4307 #endif /* INET */ 4308 #ifdef INET6 4309 case AF_INET6: 4310 pfse->pfse_type = PFSE_IP6; 4311 break; 4312 #endif /* INET6 */ 4313 } 4314 4315 pfse->pfse_m = m; 4316 pf_send(pfse); 4317 } 4318 4319 void 4320 pf_send_tcp(const struct pf_krule *r, sa_family_t af, 4321 const struct pf_addr *saddr, const struct pf_addr *daddr, 4322 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 4323 u_int8_t tcp_flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, 4324 int mbuf_flags, u_int16_t mtag_tag, u_int16_t mtag_flags, int rtableid) 4325 { 4326 struct pf_send_entry *pfse; 4327 struct mbuf *m; 4328 4329 m = pf_build_tcp(r, af, saddr, daddr, sport, dport, seq, ack, tcp_flags, 4330 win, mss, ttl, mbuf_flags, mtag_tag, mtag_flags, 0, rtableid); 4331 if (m == NULL) 4332 return; 4333 4334 /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 4335 pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 4336 if (pfse == NULL) { 4337 m_freem(m); 4338 return; 4339 } 4340 4341 switch (af) { 4342 #ifdef INET 4343 case AF_INET: 4344 pfse->pfse_type = PFSE_IP; 4345 break; 4346 #endif /* INET */ 4347 #ifdef INET6 4348 case AF_INET6: 4349 pfse->pfse_type = PFSE_IP6; 4350 break; 4351 #endif /* INET6 */ 4352 default: 4353 unhandled_af(af); 4354 } 4355 4356 pfse->pfse_m = m; 4357 pf_send(pfse); 4358 } 4359 4360 static void 4361 pf_undo_nat(struct pf_krule *nr, struct pf_pdesc *pd, uint16_t bip_sum) 4362 { 4363 /* undo NAT changes, if they have taken place */ 4364 if (nr != NULL) { 4365 pf_addrcpy(pd->src, &pd->osrc, pd->af); 4366 pf_addrcpy(pd->dst, &pd->odst, pd->af); 4367 if (pd->sport) 4368 *pd->sport = pd->osport; 4369 if (pd->dport) 4370 *pd->dport = pd->odport; 4371 if (pd->ip_sum) 4372 *pd->ip_sum = bip_sum; 4373 m_copyback(pd->m, pd->off, pd->hdrlen, pd->hdr.any); 4374 } 4375 } 4376 4377 static void 4378 pf_return(struct pf_krule *r, struct pf_krule *nr, struct pf_pdesc *pd, 4379 struct tcphdr *th, u_int16_t bproto_sum, u_int16_t bip_sum, 4380 u_short *reason, int rtableid) 4381 { 4382 pf_undo_nat(nr, pd, bip_sum); 4383 4384 if (pd->proto == IPPROTO_TCP && 4385 ((r->rule_flag & PFRULE_RETURNRST) || 4386 (r->rule_flag & PFRULE_RETURN)) && 4387 !(tcp_get_flags(th) & TH_RST)) { 4388 u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 4389 4390 if (pf_check_proto_cksum(pd->m, pd->off, pd->tot_len - pd->off, 4391 IPPROTO_TCP, pd->af)) 4392 REASON_SET(reason, PFRES_PROTCKSUM); 4393 else { 4394 if (tcp_get_flags(th) & TH_SYN) 4395 ack++; 4396 if (tcp_get_flags(th) & TH_FIN) 4397 ack++; 4398 pf_send_tcp(r, pd->af, pd->dst, 4399 pd->src, th->th_dport, th->th_sport, 4400 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 4401 r->return_ttl, M_SKIP_FIREWALL, 0, 0, rtableid); 4402 } 4403 } else if (pd->proto == IPPROTO_SCTP && 4404 (r->rule_flag & PFRULE_RETURN)) { 4405 pf_send_sctp_abort(pd->af, pd, r->return_ttl, rtableid); 4406 } else if (pd->proto != IPPROTO_ICMP && pd->af == AF_INET && 4407 r->return_icmp) 4408 pf_send_icmp(pd->m, r->return_icmp >> 8, 4409 r->return_icmp & 255, 0, pd->af, r, rtableid); 4410 else if (pd->proto != IPPROTO_ICMPV6 && pd->af == AF_INET6 && 4411 r->return_icmp6) 4412 pf_send_icmp(pd->m, r->return_icmp6 >> 8, 4413 r->return_icmp6 & 255, 0, pd->af, r, rtableid); 4414 } 4415 4416 static int 4417 pf_match_ieee8021q_pcp(u_int8_t prio, struct mbuf *m) 4418 { 4419 struct m_tag *mtag; 4420 u_int8_t mpcp; 4421 4422 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 4423 if (mtag == NULL) 4424 return (0); 4425 4426 if (prio == PF_PRIO_ZERO) 4427 prio = 0; 4428 4429 mpcp = *(uint8_t *)(mtag + 1); 4430 4431 return (mpcp == prio); 4432 } 4433 4434 static int 4435 pf_icmp_to_bandlim(uint8_t type) 4436 { 4437 switch (type) { 4438 case ICMP_ECHO: 4439 case ICMP_ECHOREPLY: 4440 return (BANDLIM_ICMP_ECHO); 4441 case ICMP_TSTAMP: 4442 case ICMP_TSTAMPREPLY: 4443 return (BANDLIM_ICMP_TSTAMP); 4444 case ICMP_UNREACH: 4445 default: 4446 return (BANDLIM_ICMP_UNREACH); 4447 } 4448 } 4449 4450 static void 4451 pf_send_challenge_ack(struct pf_pdesc *pd, struct pf_kstate *s, 4452 struct pf_state_peer *src, struct pf_state_peer *dst) 4453 { 4454 /* 4455 * We are sending challenge ACK as a response to SYN packet, which 4456 * matches existing state (modulo TCP window check). Therefore packet 4457 * must be sent on behalf of destination. 4458 * 4459 * We expect sender to remain either silent, or send RST packet 4460 * so both, firewall and remote peer, can purge dead state from 4461 * memory. 4462 */ 4463 pf_send_tcp(s->rule, pd->af, pd->dst, pd->src, 4464 pd->hdr.tcp.th_dport, pd->hdr.tcp.th_sport, dst->seqlo, 4465 src->seqlo, TH_ACK, 0, 0, s->rule->return_ttl, 0, 0, 0, 4466 s->rule->rtableid); 4467 } 4468 4469 static void 4470 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, int mtu, 4471 sa_family_t af, struct pf_krule *r, int rtableid) 4472 { 4473 struct pf_send_entry *pfse; 4474 struct mbuf *m0; 4475 struct pf_mtag *pf_mtag; 4476 4477 /* ICMP packet rate limitation. */ 4478 switch (af) { 4479 #ifdef INET6 4480 case AF_INET6: 4481 if (icmp6_ratelimit(NULL, type, code)) 4482 return; 4483 break; 4484 #endif /* INET6 */ 4485 #ifdef INET 4486 case AF_INET: 4487 if (badport_bandlim(pf_icmp_to_bandlim(type)) != 0) 4488 return; 4489 break; 4490 #endif /* INET */ 4491 } 4492 4493 /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 4494 pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 4495 if (pfse == NULL) 4496 return; 4497 4498 if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 4499 free(pfse, M_PFTEMP); 4500 return; 4501 } 4502 4503 if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 4504 free(pfse, M_PFTEMP); 4505 return; 4506 } 4507 /* XXX: revisit */ 4508 m0->m_flags |= M_SKIP_FIREWALL; 4509 4510 if (rtableid >= 0) 4511 M_SETFIB(m0, rtableid); 4512 4513 #ifdef ALTQ 4514 if (r->qid) { 4515 pf_mtag->qid = r->qid; 4516 /* add hints for ecn */ 4517 pf_mtag->hdr = mtod(m0, struct ip *); 4518 } 4519 #endif /* ALTQ */ 4520 4521 switch (af) { 4522 #ifdef INET 4523 case AF_INET: 4524 pfse->pfse_type = PFSE_ICMP; 4525 break; 4526 #endif /* INET */ 4527 #ifdef INET6 4528 case AF_INET6: 4529 pfse->pfse_type = PFSE_ICMP6; 4530 break; 4531 #endif /* INET6 */ 4532 } 4533 pfse->pfse_m = m0; 4534 pfse->icmpopts.type = type; 4535 pfse->icmpopts.code = code; 4536 pfse->icmpopts.mtu = mtu; 4537 pf_send(pfse); 4538 } 4539 4540 /* 4541 * Return ((n = 0) == (a = b [with mask m])) 4542 * Note: n != 0 => returns (a != b [with mask m]) 4543 */ 4544 int 4545 pf_match_addr(u_int8_t n, const struct pf_addr *a, const struct pf_addr *m, 4546 const struct pf_addr *b, sa_family_t af) 4547 { 4548 switch (af) { 4549 #ifdef INET 4550 case AF_INET: 4551 if (IN_ARE_MASKED_ADDR_EQUAL(a->v4, b->v4, m->v4)) 4552 return (n == 0); 4553 break; 4554 #endif /* INET */ 4555 #ifdef INET6 4556 case AF_INET6: 4557 if (IN6_ARE_MASKED_ADDR_EQUAL(&a->v6, &b->v6, &m->v6)) 4558 return (n == 0); 4559 break; 4560 #endif /* INET6 */ 4561 } 4562 4563 return (n != 0); 4564 } 4565 4566 /* 4567 * Return 1 if b <= a <= e, otherwise return 0. 4568 */ 4569 int 4570 pf_match_addr_range(const struct pf_addr *b, const struct pf_addr *e, 4571 const struct pf_addr *a, sa_family_t af) 4572 { 4573 switch (af) { 4574 #ifdef INET 4575 case AF_INET: 4576 if ((ntohl(a->addr32[0]) < ntohl(b->addr32[0])) || 4577 (ntohl(a->addr32[0]) > ntohl(e->addr32[0]))) 4578 return (0); 4579 break; 4580 #endif /* INET */ 4581 #ifdef INET6 4582 case AF_INET6: { 4583 int i; 4584 4585 /* check a >= b */ 4586 for (i = 0; i < 4; ++i) 4587 if (ntohl(a->addr32[i]) > ntohl(b->addr32[i])) 4588 break; 4589 else if (ntohl(a->addr32[i]) < ntohl(b->addr32[i])) 4590 return (0); 4591 /* check a <= e */ 4592 for (i = 0; i < 4; ++i) 4593 if (ntohl(a->addr32[i]) < ntohl(e->addr32[i])) 4594 break; 4595 else if (ntohl(a->addr32[i]) > ntohl(e->addr32[i])) 4596 return (0); 4597 break; 4598 } 4599 #endif /* INET6 */ 4600 } 4601 return (1); 4602 } 4603 4604 static int 4605 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 4606 { 4607 switch (op) { 4608 case PF_OP_IRG: 4609 return ((p > a1) && (p < a2)); 4610 case PF_OP_XRG: 4611 return ((p < a1) || (p > a2)); 4612 case PF_OP_RRG: 4613 return ((p >= a1) && (p <= a2)); 4614 case PF_OP_EQ: 4615 return (p == a1); 4616 case PF_OP_NE: 4617 return (p != a1); 4618 case PF_OP_LT: 4619 return (p < a1); 4620 case PF_OP_LE: 4621 return (p <= a1); 4622 case PF_OP_GT: 4623 return (p > a1); 4624 case PF_OP_GE: 4625 return (p >= a1); 4626 } 4627 return (0); /* never reached */ 4628 } 4629 4630 int 4631 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 4632 { 4633 return (pf_match(op, ntohs(a1), ntohs(a2), ntohs(p))); 4634 } 4635 4636 static int 4637 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 4638 { 4639 if (u == -1 && op != PF_OP_EQ && op != PF_OP_NE) 4640 return (0); 4641 return (pf_match(op, a1, a2, u)); 4642 } 4643 4644 static int 4645 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 4646 { 4647 if (g == -1 && op != PF_OP_EQ && op != PF_OP_NE) 4648 return (0); 4649 return (pf_match(op, a1, a2, g)); 4650 } 4651 4652 int 4653 pf_match_tag(struct mbuf *m, struct pf_krule *r, int *tag, int mtag) 4654 { 4655 if (*tag == -1) 4656 *tag = mtag; 4657 4658 return ((!r->match_tag_not && r->match_tag == *tag) || 4659 (r->match_tag_not && r->match_tag != *tag)); 4660 } 4661 4662 static int 4663 pf_match_rcvif(struct mbuf *m, struct pf_krule *r) 4664 { 4665 struct ifnet *ifp = m->m_pkthdr.rcvif; 4666 struct pfi_kkif *kif; 4667 4668 if (ifp == NULL) 4669 return (0); 4670 4671 kif = (struct pfi_kkif *)ifp->if_pf_kif; 4672 4673 if (kif == NULL) { 4674 DPFPRINTF(PF_DEBUG_URGENT, 4675 "%s: kif == NULL, @%d via %s", __func__, r->nr, 4676 r->rcv_ifname); 4677 return (0); 4678 } 4679 4680 return (pfi_kkif_match(r->rcv_kif, kif)); 4681 } 4682 4683 int 4684 pf_tag_packet(struct pf_pdesc *pd, int tag) 4685 { 4686 4687 KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 4688 4689 if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(pd->m)) == NULL)) 4690 return (ENOMEM); 4691 4692 pd->pf_mtag->tag = tag; 4693 4694 return (0); 4695 } 4696 4697 /* 4698 * XXX: We rely on malloc(9) returning pointer aligned addresses. 4699 */ 4700 #define PF_ANCHORSTACK_MATCH 0x00000001 4701 #define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 4702 4703 #define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 4704 #define PF_ANCHOR_RULE(f) (struct pf_krule *) \ 4705 ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 4706 #define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 4707 ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 4708 } while (0) 4709 4710 enum pf_test_status 4711 pf_step_into_anchor(struct pf_test_ctx *ctx, struct pf_krule *r) 4712 { 4713 enum pf_test_status rv; 4714 4715 PF_RULES_RASSERT(); 4716 4717 if (ctx->depth >= PF_ANCHOR_STACK_MAX) { 4718 printf("%s: anchor stack overflow on %s\n", 4719 __func__, r->anchor->name); 4720 return (PF_TEST_FAIL); 4721 } 4722 4723 ctx->depth++; 4724 4725 if (r->anchor_wildcard) { 4726 struct pf_kanchor *child; 4727 rv = PF_TEST_OK; 4728 RB_FOREACH(child, pf_kanchor_node, &r->anchor->children) { 4729 rv = pf_match_rule(ctx, &child->ruleset); 4730 if ((rv == PF_TEST_QUICK) || (rv == PF_TEST_FAIL)) { 4731 /* 4732 * we either hit a rule with quick action 4733 * (more likely), or hit some runtime 4734 * error (e.g. pool_get() failure). 4735 */ 4736 break; 4737 } 4738 } 4739 } else { 4740 rv = pf_match_rule(ctx, &r->anchor->ruleset); 4741 /* 4742 * Unless errors occured, stop iff any rule matched 4743 * within quick anchors. 4744 */ 4745 if (rv != PF_TEST_FAIL && r->quick == PF_TEST_QUICK && 4746 *ctx->am == r) 4747 rv = PF_TEST_QUICK; 4748 } 4749 4750 ctx->depth--; 4751 4752 return (rv); 4753 } 4754 4755 struct pf_keth_anchor_stackframe { 4756 struct pf_keth_ruleset *rs; 4757 struct pf_keth_rule *r; /* XXX: + match bit */ 4758 struct pf_keth_anchor *child; 4759 }; 4760 4761 #define PF_ETH_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 4762 #define PF_ETH_ANCHOR_RULE(f) (struct pf_keth_rule *) \ 4763 ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 4764 #define PF_ETH_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 4765 ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 4766 } while (0) 4767 4768 void 4769 pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *stack, int *depth, 4770 struct pf_keth_ruleset **rs, struct pf_keth_rule **r, 4771 struct pf_keth_rule **a, int *match) 4772 { 4773 struct pf_keth_anchor_stackframe *f; 4774 4775 NET_EPOCH_ASSERT(); 4776 4777 if (match) 4778 *match = 0; 4779 if (*depth >= PF_ANCHOR_STACK_MAX) { 4780 printf("%s: anchor stack overflow on %s\n", 4781 __func__, (*r)->anchor->name); 4782 *r = TAILQ_NEXT(*r, entries); 4783 return; 4784 } else if (*depth == 0 && a != NULL) 4785 *a = *r; 4786 f = stack + (*depth)++; 4787 f->rs = *rs; 4788 f->r = *r; 4789 if ((*r)->anchor_wildcard) { 4790 struct pf_keth_anchor_node *parent = &(*r)->anchor->children; 4791 4792 if ((f->child = RB_MIN(pf_keth_anchor_node, parent)) == NULL) { 4793 *r = NULL; 4794 return; 4795 } 4796 *rs = &f->child->ruleset; 4797 } else { 4798 f->child = NULL; 4799 *rs = &(*r)->anchor->ruleset; 4800 } 4801 *r = TAILQ_FIRST((*rs)->active.rules); 4802 } 4803 4804 int 4805 pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *stack, int *depth, 4806 struct pf_keth_ruleset **rs, struct pf_keth_rule **r, 4807 struct pf_keth_rule **a, int *match) 4808 { 4809 struct pf_keth_anchor_stackframe *f; 4810 struct pf_keth_rule *fr; 4811 int quick = 0; 4812 4813 NET_EPOCH_ASSERT(); 4814 4815 do { 4816 if (*depth <= 0) 4817 break; 4818 f = stack + *depth - 1; 4819 fr = PF_ETH_ANCHOR_RULE(f); 4820 if (f->child != NULL) { 4821 /* 4822 * This block traverses through 4823 * a wildcard anchor. 4824 */ 4825 if (match != NULL && *match) { 4826 /* 4827 * If any of "*" matched, then 4828 * "foo/ *" matched, mark frame 4829 * appropriately. 4830 */ 4831 PF_ETH_ANCHOR_SET_MATCH(f); 4832 *match = 0; 4833 } 4834 f->child = RB_NEXT(pf_keth_anchor_node, 4835 &fr->anchor->children, f->child); 4836 if (f->child != NULL) { 4837 *rs = &f->child->ruleset; 4838 *r = TAILQ_FIRST((*rs)->active.rules); 4839 if (*r == NULL) 4840 continue; 4841 else 4842 break; 4843 } 4844 } 4845 (*depth)--; 4846 if (*depth == 0 && a != NULL) 4847 *a = NULL; 4848 *rs = f->rs; 4849 if (PF_ETH_ANCHOR_MATCH(f) || (match != NULL && *match)) 4850 quick = fr->quick; 4851 *r = TAILQ_NEXT(fr, entries); 4852 } while (*r == NULL); 4853 4854 return (quick); 4855 } 4856 4857 void 4858 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 4859 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 4860 { 4861 switch (af) { 4862 #ifdef INET 4863 case AF_INET: 4864 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 4865 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 4866 break; 4867 #endif /* INET */ 4868 #ifdef INET6 4869 case AF_INET6: 4870 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 4871 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 4872 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 4873 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 4874 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 4875 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 4876 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 4877 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 4878 break; 4879 #endif /* INET6 */ 4880 } 4881 } 4882 4883 void 4884 pf_addr_inc(struct pf_addr *addr, sa_family_t af) 4885 { 4886 switch (af) { 4887 #ifdef INET 4888 case AF_INET: 4889 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 4890 break; 4891 #endif /* INET */ 4892 #ifdef INET6 4893 case AF_INET6: 4894 if (addr->addr32[3] == 0xffffffff) { 4895 addr->addr32[3] = 0; 4896 if (addr->addr32[2] == 0xffffffff) { 4897 addr->addr32[2] = 0; 4898 if (addr->addr32[1] == 0xffffffff) { 4899 addr->addr32[1] = 0; 4900 addr->addr32[0] = 4901 htonl(ntohl(addr->addr32[0]) + 1); 4902 } else 4903 addr->addr32[1] = 4904 htonl(ntohl(addr->addr32[1]) + 1); 4905 } else 4906 addr->addr32[2] = 4907 htonl(ntohl(addr->addr32[2]) + 1); 4908 } else 4909 addr->addr32[3] = 4910 htonl(ntohl(addr->addr32[3]) + 1); 4911 break; 4912 #endif /* INET6 */ 4913 } 4914 } 4915 4916 void 4917 pf_rule_to_actions(struct pf_krule *r, struct pf_rule_actions *a) 4918 { 4919 /* 4920 * Modern rules use the same flags in rules as they do in states. 4921 */ 4922 a->flags |= (r->scrub_flags & (PFSTATE_NODF|PFSTATE_RANDOMID| 4923 PFSTATE_SCRUB_TCP|PFSTATE_SETPRIO)); 4924 4925 /* 4926 * Old-style scrub rules have different flags which need to be translated. 4927 */ 4928 if (r->rule_flag & PFRULE_RANDOMID) 4929 a->flags |= PFSTATE_RANDOMID; 4930 if (r->scrub_flags & PFSTATE_SETTOS || r->rule_flag & PFRULE_SET_TOS ) { 4931 a->flags |= PFSTATE_SETTOS; 4932 a->set_tos = r->set_tos; 4933 } 4934 4935 if (r->qid) 4936 a->qid = r->qid; 4937 if (r->pqid) 4938 a->pqid = r->pqid; 4939 if (r->rtableid >= 0) 4940 a->rtableid = r->rtableid; 4941 a->log |= r->log; 4942 if (r->min_ttl) 4943 a->min_ttl = r->min_ttl; 4944 if (r->max_mss) 4945 a->max_mss = r->max_mss; 4946 if (r->dnpipe) 4947 a->dnpipe = r->dnpipe; 4948 if (r->dnrpipe) 4949 a->dnrpipe = r->dnrpipe; 4950 if (r->dnpipe || r->dnrpipe) { 4951 if (r->free_flags & PFRULE_DN_IS_PIPE) 4952 a->flags |= PFSTATE_DN_IS_PIPE; 4953 else 4954 a->flags &= ~PFSTATE_DN_IS_PIPE; 4955 } 4956 if (r->scrub_flags & PFSTATE_SETPRIO) { 4957 a->set_prio[0] = r->set_prio[0]; 4958 a->set_prio[1] = r->set_prio[1]; 4959 } 4960 if (r->allow_opts) 4961 a->allow_opts = r->allow_opts; 4962 if (r->max_pkt_size) 4963 a->max_pkt_size = r->max_pkt_size; 4964 } 4965 4966 int 4967 pf_socket_lookup(struct pf_pdesc *pd) 4968 { 4969 struct pf_addr *saddr, *daddr; 4970 u_int16_t sport, dport; 4971 struct inpcbinfo *pi; 4972 struct inpcb *inp; 4973 4974 pd->lookup.uid = -1; 4975 pd->lookup.gid = -1; 4976 4977 switch (pd->proto) { 4978 case IPPROTO_TCP: 4979 sport = pd->hdr.tcp.th_sport; 4980 dport = pd->hdr.tcp.th_dport; 4981 pi = &V_tcbinfo; 4982 break; 4983 case IPPROTO_UDP: 4984 sport = pd->hdr.udp.uh_sport; 4985 dport = pd->hdr.udp.uh_dport; 4986 pi = &V_udbinfo; 4987 break; 4988 default: 4989 return (-1); 4990 } 4991 if (pd->dir == PF_IN) { 4992 saddr = pd->src; 4993 daddr = pd->dst; 4994 } else { 4995 u_int16_t p; 4996 4997 p = sport; 4998 sport = dport; 4999 dport = p; 5000 saddr = pd->dst; 5001 daddr = pd->src; 5002 } 5003 switch (pd->af) { 5004 #ifdef INET 5005 case AF_INET: 5006 inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 5007 dport, INPLOOKUP_RLOCKPCB, NULL, pd->m); 5008 if (inp == NULL) { 5009 inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 5010 daddr->v4, dport, INPLOOKUP_WILDCARD | 5011 INPLOOKUP_RLOCKPCB, NULL, pd->m); 5012 if (inp == NULL) 5013 return (-1); 5014 } 5015 break; 5016 #endif /* INET */ 5017 #ifdef INET6 5018 case AF_INET6: 5019 inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 5020 dport, INPLOOKUP_RLOCKPCB, NULL, pd->m); 5021 if (inp == NULL) { 5022 inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 5023 &daddr->v6, dport, INPLOOKUP_WILDCARD | 5024 INPLOOKUP_RLOCKPCB, NULL, pd->m); 5025 if (inp == NULL) 5026 return (-1); 5027 } 5028 break; 5029 #endif /* INET6 */ 5030 default: 5031 unhandled_af(pd->af); 5032 } 5033 INP_RLOCK_ASSERT(inp); 5034 pd->lookup.uid = inp->inp_cred->cr_uid; 5035 pd->lookup.gid = inp->inp_cred->cr_gid; 5036 INP_RUNLOCK(inp); 5037 5038 return (1); 5039 } 5040 5041 /* post: r => (r[0] == type /\ r[1] >= min_typelen >= 2 "validity" 5042 * /\ (eoh - r) >= min_typelen >= 2 "safety" ) 5043 * 5044 * warning: r + r[1] may exceed opts bounds for r[1] > min_typelen 5045 */ 5046 uint8_t* 5047 pf_find_tcpopt(u_int8_t *opt, u_int8_t *opts, size_t hlen, u_int8_t type, 5048 u_int8_t min_typelen) 5049 { 5050 uint8_t *eoh = opts + hlen; 5051 5052 if (min_typelen < 2) 5053 return (NULL); 5054 5055 while ((eoh - opt) >= min_typelen) { 5056 switch (*opt) { 5057 case TCPOPT_EOL: 5058 /* FALLTHROUGH - Workaround the failure of some 5059 systems to NOP-pad their bzero'd option buffers, 5060 producing spurious EOLs */ 5061 case TCPOPT_NOP: 5062 opt++; 5063 continue; 5064 default: 5065 if (opt[0] == type && 5066 opt[1] >= min_typelen) 5067 return (opt); 5068 } 5069 5070 opt += MAX(opt[1], 2); /* evade infinite loops */ 5071 } 5072 5073 return (NULL); 5074 } 5075 5076 u_int8_t 5077 pf_get_wscale(struct pf_pdesc *pd) 5078 { 5079 int olen; 5080 uint8_t opts[MAX_TCPOPTLEN], *opt; 5081 uint8_t wscale = 0; 5082 5083 olen = (pd->hdr.tcp.th_off << 2) - sizeof(struct tcphdr); 5084 if (olen < TCPOLEN_WINDOW || !pf_pull_hdr(pd->m, 5085 pd->off + sizeof(struct tcphdr), opts, olen, NULL, NULL, pd->af)) 5086 return (0); 5087 5088 opt = opts; 5089 while ((opt = pf_find_tcpopt(opt, opts, olen, 5090 TCPOPT_WINDOW, TCPOLEN_WINDOW)) != NULL) { 5091 wscale = opt[2]; 5092 wscale = MIN(wscale, TCP_MAX_WINSHIFT); 5093 wscale |= PF_WSCALE_FLAG; 5094 5095 opt += opt[1]; 5096 } 5097 5098 return (wscale); 5099 } 5100 5101 u_int16_t 5102 pf_get_mss(struct pf_pdesc *pd) 5103 { 5104 int olen; 5105 uint8_t opts[MAX_TCPOPTLEN], *opt; 5106 u_int16_t mss = V_tcp_mssdflt; 5107 5108 olen = (pd->hdr.tcp.th_off << 2) - sizeof(struct tcphdr); 5109 if (olen < TCPOLEN_MAXSEG || !pf_pull_hdr(pd->m, 5110 pd->off + sizeof(struct tcphdr), opts, olen, NULL, NULL, pd->af)) 5111 return (0); 5112 5113 opt = opts; 5114 while ((opt = pf_find_tcpopt(opt, opts, olen, 5115 TCPOPT_MAXSEG, TCPOLEN_MAXSEG)) != NULL) { 5116 memcpy(&mss, (opt + 2), 2); 5117 mss = ntohs(mss); 5118 opt += opt[1]; 5119 } 5120 5121 return (mss); 5122 } 5123 5124 static u_int16_t 5125 pf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 5126 { 5127 struct nhop_object *nh; 5128 #ifdef INET6 5129 struct in6_addr dst6; 5130 uint32_t scopeid; 5131 #endif /* INET6 */ 5132 int hlen = 0; 5133 uint16_t mss = 0; 5134 5135 NET_EPOCH_ASSERT(); 5136 5137 switch (af) { 5138 #ifdef INET 5139 case AF_INET: 5140 hlen = sizeof(struct ip); 5141 nh = fib4_lookup(rtableid, addr->v4, 0, 0, 0); 5142 if (nh != NULL) 5143 mss = nh->nh_mtu - hlen - sizeof(struct tcphdr); 5144 break; 5145 #endif /* INET */ 5146 #ifdef INET6 5147 case AF_INET6: 5148 hlen = sizeof(struct ip6_hdr); 5149 in6_splitscope(&addr->v6, &dst6, &scopeid); 5150 nh = fib6_lookup(rtableid, &dst6, scopeid, 0, 0); 5151 if (nh != NULL) 5152 mss = nh->nh_mtu - hlen - sizeof(struct tcphdr); 5153 break; 5154 #endif /* INET6 */ 5155 } 5156 5157 mss = max(V_tcp_mssdflt, mss); 5158 mss = min(mss, offer); 5159 mss = max(mss, 64); /* sanity - at least max opt space */ 5160 return (mss); 5161 } 5162 5163 static u_int32_t 5164 pf_tcp_iss(struct pf_pdesc *pd) 5165 { 5166 SHA512_CTX ctx; 5167 union { 5168 uint8_t bytes[SHA512_DIGEST_LENGTH]; 5169 uint32_t words[1]; 5170 } digest; 5171 5172 if (V_pf_tcp_secret_init == 0) { 5173 arc4random_buf(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 5174 SHA512_Init(&V_pf_tcp_secret_ctx); 5175 SHA512_Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 5176 sizeof(V_pf_tcp_secret)); 5177 V_pf_tcp_secret_init = 1; 5178 } 5179 5180 ctx = V_pf_tcp_secret_ctx; 5181 5182 SHA512_Update(&ctx, &pd->hdr.tcp.th_sport, sizeof(u_short)); 5183 SHA512_Update(&ctx, &pd->hdr.tcp.th_dport, sizeof(u_short)); 5184 switch (pd->af) { 5185 case AF_INET6: 5186 SHA512_Update(&ctx, &pd->src->v6, sizeof(struct in6_addr)); 5187 SHA512_Update(&ctx, &pd->dst->v6, sizeof(struct in6_addr)); 5188 break; 5189 case AF_INET: 5190 SHA512_Update(&ctx, &pd->src->v4, sizeof(struct in_addr)); 5191 SHA512_Update(&ctx, &pd->dst->v4, sizeof(struct in_addr)); 5192 break; 5193 } 5194 SHA512_Final(digest.bytes, &ctx); 5195 V_pf_tcp_iss_off += 4096; 5196 #define ISN_RANDOM_INCREMENT (4096 - 1) 5197 return (digest.words[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 5198 V_pf_tcp_iss_off); 5199 #undef ISN_RANDOM_INCREMENT 5200 } 5201 5202 static bool 5203 pf_match_eth_addr(const uint8_t *a, const struct pf_keth_rule_addr *r) 5204 { 5205 bool match = true; 5206 5207 /* Always matches if not set */ 5208 if (! r->isset) 5209 return (!r->neg); 5210 5211 for (int i = 0; i < ETHER_ADDR_LEN; i++) { 5212 if ((a[i] & r->mask[i]) != (r->addr[i] & r->mask[i])) { 5213 match = false; 5214 break; 5215 } 5216 } 5217 5218 return (match ^ r->neg); 5219 } 5220 5221 static int 5222 pf_match_eth_tag(struct mbuf *m, struct pf_keth_rule *r, int *tag, int mtag) 5223 { 5224 if (*tag == -1) 5225 *tag = mtag; 5226 5227 return ((!r->match_tag_not && r->match_tag == *tag) || 5228 (r->match_tag_not && r->match_tag != *tag)); 5229 } 5230 5231 static void 5232 pf_bridge_to(struct ifnet *ifp, struct mbuf *m) 5233 { 5234 /* If we don't have the interface drop the packet. */ 5235 if (ifp == NULL) { 5236 m_freem(m); 5237 return; 5238 } 5239 5240 switch (ifp->if_type) { 5241 case IFT_ETHER: 5242 case IFT_XETHER: 5243 case IFT_L2VLAN: 5244 case IFT_BRIDGE: 5245 case IFT_IEEE8023ADLAG: 5246 break; 5247 default: 5248 m_freem(m); 5249 return; 5250 } 5251 5252 ifp->if_transmit(ifp, m); 5253 } 5254 5255 static int 5256 pf_test_eth_rule(int dir, struct pfi_kkif *kif, struct mbuf **m0) 5257 { 5258 #ifdef INET 5259 struct ip ip; 5260 #endif /* INET */ 5261 #ifdef INET6 5262 struct ip6_hdr ip6; 5263 #endif /* INET6 */ 5264 struct mbuf *m = *m0; 5265 struct ether_header *e; 5266 struct pf_keth_rule *r, *rm, *a = NULL; 5267 struct pf_keth_ruleset *ruleset = NULL; 5268 struct pf_mtag *mtag; 5269 struct pf_keth_ruleq *rules; 5270 struct pf_addr *src = NULL, *dst = NULL; 5271 struct pfi_kkif *bridge_to; 5272 sa_family_t af = 0; 5273 uint16_t proto; 5274 int asd = 0, match = 0; 5275 int tag = -1; 5276 uint8_t action; 5277 struct pf_keth_anchor_stackframe anchor_stack[PF_ANCHOR_STACK_MAX]; 5278 5279 MPASS(kif->pfik_ifp->if_vnet == curvnet); 5280 NET_EPOCH_ASSERT(); 5281 5282 PF_RULES_RLOCK_TRACKER; 5283 5284 SDT_PROBE3(pf, eth, test_rule, entry, dir, kif->pfik_ifp, m); 5285 5286 mtag = pf_find_mtag(m); 5287 if (mtag != NULL && mtag->flags & PF_MTAG_FLAG_DUMMYNET) { 5288 /* Dummynet re-injects packets after they've 5289 * completed their delay. We've already 5290 * processed them, so pass unconditionally. */ 5291 5292 /* But only once. We may see the packet multiple times (e.g. 5293 * PFIL_IN/PFIL_OUT). */ 5294 pf_dummynet_flag_remove(m, mtag); 5295 5296 return (PF_PASS); 5297 } 5298 5299 if (__predict_false(m->m_len < sizeof(struct ether_header)) && 5300 (m = *m0 = m_pullup(*m0, sizeof(struct ether_header))) == NULL) { 5301 DPFPRINTF(PF_DEBUG_URGENT, 5302 "%s: m_len < sizeof(struct ether_header)" 5303 ", pullup failed", __func__); 5304 return (PF_DROP); 5305 } 5306 e = mtod(m, struct ether_header *); 5307 proto = ntohs(e->ether_type); 5308 5309 switch (proto) { 5310 #ifdef INET 5311 case ETHERTYPE_IP: { 5312 if (m_length(m, NULL) < (sizeof(struct ether_header) + 5313 sizeof(ip))) 5314 return (PF_DROP); 5315 5316 af = AF_INET; 5317 m_copydata(m, sizeof(struct ether_header), sizeof(ip), 5318 (caddr_t)&ip); 5319 src = (struct pf_addr *)&ip.ip_src; 5320 dst = (struct pf_addr *)&ip.ip_dst; 5321 break; 5322 } 5323 #endif /* INET */ 5324 #ifdef INET6 5325 case ETHERTYPE_IPV6: { 5326 if (m_length(m, NULL) < (sizeof(struct ether_header) + 5327 sizeof(ip6))) 5328 return (PF_DROP); 5329 5330 af = AF_INET6; 5331 m_copydata(m, sizeof(struct ether_header), sizeof(ip6), 5332 (caddr_t)&ip6); 5333 src = (struct pf_addr *)&ip6.ip6_src; 5334 dst = (struct pf_addr *)&ip6.ip6_dst; 5335 break; 5336 } 5337 #endif /* INET6 */ 5338 } 5339 5340 PF_RULES_RLOCK(); 5341 5342 ruleset = V_pf_keth; 5343 rules = atomic_load_ptr(&ruleset->active.rules); 5344 for (r = TAILQ_FIRST(rules), rm = NULL; r != NULL;) { 5345 counter_u64_add(r->evaluations, 1); 5346 SDT_PROBE2(pf, eth, test_rule, test, r->nr, r); 5347 5348 if (pfi_kkif_match(r->kif, kif) == r->ifnot) { 5349 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5350 "kif"); 5351 r = r->skip[PFE_SKIP_IFP].ptr; 5352 } 5353 else if (r->direction && r->direction != dir) { 5354 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5355 "dir"); 5356 r = r->skip[PFE_SKIP_DIR].ptr; 5357 } 5358 else if (r->proto && r->proto != proto) { 5359 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5360 "proto"); 5361 r = r->skip[PFE_SKIP_PROTO].ptr; 5362 } 5363 else if (! pf_match_eth_addr(e->ether_shost, &r->src)) { 5364 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5365 "src"); 5366 r = r->skip[PFE_SKIP_SRC_ADDR].ptr; 5367 } 5368 else if (! pf_match_eth_addr(e->ether_dhost, &r->dst)) { 5369 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5370 "dst"); 5371 r = r->skip[PFE_SKIP_DST_ADDR].ptr; 5372 } 5373 else if (src != NULL && PF_MISMATCHAW(&r->ipsrc.addr, src, af, 5374 r->ipsrc.neg, kif, M_GETFIB(m))) { 5375 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5376 "ip_src"); 5377 r = r->skip[PFE_SKIP_SRC_IP_ADDR].ptr; 5378 } 5379 else if (dst != NULL && PF_MISMATCHAW(&r->ipdst.addr, dst, af, 5380 r->ipdst.neg, kif, M_GETFIB(m))) { 5381 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5382 "ip_dst"); 5383 r = r->skip[PFE_SKIP_DST_IP_ADDR].ptr; 5384 } 5385 else if (r->match_tag && !pf_match_eth_tag(m, r, &tag, 5386 mtag ? mtag->tag : 0)) { 5387 SDT_PROBE3(pf, eth, test_rule, mismatch, r->nr, r, 5388 "match_tag"); 5389 r = TAILQ_NEXT(r, entries); 5390 } 5391 else { 5392 if (r->tag) 5393 tag = r->tag; 5394 if (r->anchor == NULL) { 5395 /* Rule matches */ 5396 rm = r; 5397 5398 SDT_PROBE2(pf, eth, test_rule, match, r->nr, r); 5399 5400 if (r->quick) 5401 break; 5402 5403 r = TAILQ_NEXT(r, entries); 5404 } else { 5405 pf_step_into_keth_anchor(anchor_stack, &asd, 5406 &ruleset, &r, &a, &match); 5407 } 5408 } 5409 if (r == NULL && pf_step_out_of_keth_anchor(anchor_stack, &asd, 5410 &ruleset, &r, &a, &match)) 5411 break; 5412 } 5413 5414 r = rm; 5415 5416 SDT_PROBE2(pf, eth, test_rule, final_match, (r != NULL ? r->nr : -1), r); 5417 5418 /* Default to pass. */ 5419 if (r == NULL) { 5420 PF_RULES_RUNLOCK(); 5421 return (PF_PASS); 5422 } 5423 5424 /* Execute action. */ 5425 counter_u64_add(r->packets[dir == PF_OUT], 1); 5426 counter_u64_add(r->bytes[dir == PF_OUT], m_length(m, NULL)); 5427 pf_update_timestamp(r); 5428 5429 /* Shortcut. Don't tag if we're just going to drop anyway. */ 5430 if (r->action == PF_DROP) { 5431 PF_RULES_RUNLOCK(); 5432 return (PF_DROP); 5433 } 5434 5435 if (tag > 0) { 5436 if (mtag == NULL) 5437 mtag = pf_get_mtag(m); 5438 if (mtag == NULL) { 5439 PF_RULES_RUNLOCK(); 5440 counter_u64_add(V_pf_status.counters[PFRES_MEMORY], 1); 5441 return (PF_DROP); 5442 } 5443 mtag->tag = tag; 5444 } 5445 5446 if (r->qid != 0) { 5447 if (mtag == NULL) 5448 mtag = pf_get_mtag(m); 5449 if (mtag == NULL) { 5450 PF_RULES_RUNLOCK(); 5451 counter_u64_add(V_pf_status.counters[PFRES_MEMORY], 1); 5452 return (PF_DROP); 5453 } 5454 mtag->qid = r->qid; 5455 } 5456 5457 action = r->action; 5458 bridge_to = r->bridge_to; 5459 5460 /* Dummynet */ 5461 if (r->dnpipe) { 5462 struct ip_fw_args dnflow; 5463 5464 /* Drop packet if dummynet is not loaded. */ 5465 if (ip_dn_io_ptr == NULL) { 5466 PF_RULES_RUNLOCK(); 5467 m_freem(m); 5468 counter_u64_add(V_pf_status.counters[PFRES_MEMORY], 1); 5469 return (PF_DROP); 5470 } 5471 if (mtag == NULL) 5472 mtag = pf_get_mtag(m); 5473 if (mtag == NULL) { 5474 PF_RULES_RUNLOCK(); 5475 counter_u64_add(V_pf_status.counters[PFRES_MEMORY], 1); 5476 return (PF_DROP); 5477 } 5478 5479 bzero(&dnflow, sizeof(dnflow)); 5480 5481 /* We don't have port numbers here, so we set 0. That means 5482 * that we'll be somewhat limited in distinguishing flows (i.e. 5483 * only based on IP addresses, not based on port numbers), but 5484 * it's better than nothing. */ 5485 dnflow.f_id.dst_port = 0; 5486 dnflow.f_id.src_port = 0; 5487 dnflow.f_id.proto = 0; 5488 5489 dnflow.rule.info = r->dnpipe; 5490 dnflow.rule.info |= IPFW_IS_DUMMYNET; 5491 if (r->dnflags & PFRULE_DN_IS_PIPE) 5492 dnflow.rule.info |= IPFW_IS_PIPE; 5493 5494 dnflow.f_id.extra = dnflow.rule.info; 5495 5496 dnflow.flags = dir == PF_IN ? IPFW_ARGS_IN : IPFW_ARGS_OUT; 5497 dnflow.flags |= IPFW_ARGS_ETHER; 5498 dnflow.ifp = kif->pfik_ifp; 5499 5500 switch (af) { 5501 case AF_INET: 5502 dnflow.f_id.addr_type = 4; 5503 dnflow.f_id.src_ip = src->v4.s_addr; 5504 dnflow.f_id.dst_ip = dst->v4.s_addr; 5505 break; 5506 case AF_INET6: 5507 dnflow.flags |= IPFW_ARGS_IP6; 5508 dnflow.f_id.addr_type = 6; 5509 dnflow.f_id.src_ip6 = src->v6; 5510 dnflow.f_id.dst_ip6 = dst->v6; 5511 break; 5512 } 5513 5514 PF_RULES_RUNLOCK(); 5515 5516 mtag->flags |= PF_MTAG_FLAG_DUMMYNET; 5517 ip_dn_io_ptr(m0, &dnflow); 5518 if (*m0 != NULL) 5519 pf_dummynet_flag_remove(m, mtag); 5520 } else { 5521 PF_RULES_RUNLOCK(); 5522 } 5523 5524 if (action == PF_PASS && bridge_to) { 5525 pf_bridge_to(bridge_to->pfik_ifp, *m0); 5526 *m0 = NULL; /* We've eaten the packet. */ 5527 } 5528 5529 return (action); 5530 } 5531 5532 #define PF_TEST_ATTRIB(t, a) \ 5533 if (t) { \ 5534 r = a; \ 5535 continue; \ 5536 } else do { \ 5537 } while (0) 5538 5539 static __inline u_short 5540 pf_rule_apply_nat(struct pf_test_ctx *ctx, struct pf_krule *r) 5541 { 5542 struct pf_pdesc *pd = ctx->pd; 5543 u_short transerror; 5544 u_int8_t nat_action; 5545 5546 if (r->rule_flag & PFRULE_AFTO) { 5547 /* Don't translate if there was an old style NAT rule */ 5548 if (ctx->nr != NULL) 5549 return (PFRES_TRANSLATE); 5550 5551 /* pass af-to rules, unsupported on match rules */ 5552 KASSERT(r->action != PF_MATCH, ("%s: af-to on match rule", __func__)); 5553 /* XXX I can imagine scenarios where we have both NAT and RDR source tracking */ 5554 ctx->nat_pool = &(r->nat); 5555 ctx->nr = r; 5556 pd->naf = r->naf; 5557 if (pf_get_transaddr_af(ctx->nr, pd) == -1) { 5558 return (PFRES_TRANSLATE); 5559 } 5560 return (PFRES_MATCH); 5561 } else if (r->rdr.cur || r->nat.cur) { 5562 /* Don't translate if there was an old style NAT rule */ 5563 if (ctx->nr != NULL) 5564 return (PFRES_TRANSLATE); 5565 5566 /* match/pass nat-to/rdr-to rules */ 5567 ctx->nr = r; 5568 if (r->nat.cur) { 5569 nat_action = PF_NAT; 5570 ctx->nat_pool = &(r->nat); 5571 } else { 5572 nat_action = PF_RDR; 5573 ctx->nat_pool = &(r->rdr); 5574 } 5575 5576 transerror = pf_get_transaddr(ctx, ctx->nr, 5577 nat_action, ctx->nat_pool); 5578 if (transerror == PFRES_MATCH) { 5579 ctx->rewrite += pf_translate_compat(ctx); 5580 return(PFRES_MATCH); 5581 } 5582 return (transerror); 5583 } 5584 5585 return (PFRES_MAX); 5586 } 5587 5588 enum pf_test_status 5589 pf_match_rule(struct pf_test_ctx *ctx, struct pf_kruleset *ruleset) 5590 { 5591 struct pf_krule_item *ri; 5592 struct pf_krule *r; 5593 struct pf_krule *save_a; 5594 struct pf_kruleset *save_aruleset; 5595 struct pf_pdesc *pd = ctx->pd; 5596 u_short transerror; 5597 5598 r = TAILQ_FIRST(ruleset->rules[PF_RULESET_FILTER].active.ptr); 5599 while (r != NULL) { 5600 if (ctx->pd->related_rule) { 5601 *ctx->rm = ctx->pd->related_rule; 5602 break; 5603 } 5604 pf_counter_u64_add(&r->evaluations, 1); 5605 PF_TEST_ATTRIB(pfi_kkif_match(r->kif, pd->kif) == r->ifnot, 5606 r->skip[PF_SKIP_IFP]); 5607 PF_TEST_ATTRIB(r->direction && r->direction != pd->dir, 5608 r->skip[PF_SKIP_DIR]); 5609 PF_TEST_ATTRIB(r->af && r->af != pd->af, 5610 r->skip[PF_SKIP_AF]); 5611 PF_TEST_ATTRIB(r->proto && r->proto != pd->proto, 5612 r->skip[PF_SKIP_PROTO]); 5613 PF_TEST_ATTRIB(PF_MISMATCHAW(&r->src.addr, &pd->nsaddr, pd->naf, 5614 r->src.neg, pd->kif, M_GETFIB(pd->m)), 5615 r->skip[PF_SKIP_SRC_ADDR]); 5616 PF_TEST_ATTRIB(PF_MISMATCHAW(&r->dst.addr, &pd->ndaddr, pd->af, 5617 r->dst.neg, NULL, M_GETFIB(pd->m)), 5618 r->skip[PF_SKIP_DST_ADDR]); 5619 switch (pd->virtual_proto) { 5620 case PF_VPROTO_FRAGMENT: 5621 /* tcp/udp only. port_op always 0 in other cases */ 5622 PF_TEST_ATTRIB((r->src.port_op || r->dst.port_op), 5623 TAILQ_NEXT(r, entries)); 5624 PF_TEST_ATTRIB((pd->proto == IPPROTO_TCP && r->flagset), 5625 TAILQ_NEXT(r, entries)); 5626 /* icmp only. type/code always 0 in other cases */ 5627 PF_TEST_ATTRIB((r->type || r->code), 5628 TAILQ_NEXT(r, entries)); 5629 /* tcp/udp only. {uid|gid}.op always 0 in other cases */ 5630 PF_TEST_ATTRIB((r->gid.op || r->uid.op), 5631 TAILQ_NEXT(r, entries)); 5632 break; 5633 5634 case IPPROTO_TCP: 5635 PF_TEST_ATTRIB((r->flagset & tcp_get_flags(ctx->th)) 5636 != r->flags, 5637 TAILQ_NEXT(r, entries)); 5638 /* FALLTHROUGH */ 5639 case IPPROTO_SCTP: 5640 case IPPROTO_UDP: 5641 /* tcp/udp only. port_op always 0 in other cases */ 5642 PF_TEST_ATTRIB(r->src.port_op && !pf_match_port(r->src.port_op, 5643 r->src.port[0], r->src.port[1], pd->nsport), 5644 r->skip[PF_SKIP_SRC_PORT]); 5645 /* tcp/udp only. port_op always 0 in other cases */ 5646 PF_TEST_ATTRIB(r->dst.port_op && !pf_match_port(r->dst.port_op, 5647 r->dst.port[0], r->dst.port[1], pd->ndport), 5648 r->skip[PF_SKIP_DST_PORT]); 5649 /* tcp/udp only. uid.op always 0 in other cases */ 5650 PF_TEST_ATTRIB(r->uid.op && (pd->lookup.done || (pd->lookup.done = 5651 pf_socket_lookup(pd), 1)) && 5652 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 5653 pd->lookup.uid), 5654 TAILQ_NEXT(r, entries)); 5655 /* tcp/udp only. gid.op always 0 in other cases */ 5656 PF_TEST_ATTRIB(r->gid.op && (pd->lookup.done || (pd->lookup.done = 5657 pf_socket_lookup(pd), 1)) && 5658 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 5659 pd->lookup.gid), 5660 TAILQ_NEXT(r, entries)); 5661 break; 5662 5663 case IPPROTO_ICMP: 5664 case IPPROTO_ICMPV6: 5665 /* icmp only. type always 0 in other cases */ 5666 PF_TEST_ATTRIB(r->type && r->type != ctx->icmptype + 1, 5667 TAILQ_NEXT(r, entries)); 5668 /* icmp only. type always 0 in other cases */ 5669 PF_TEST_ATTRIB(r->code && r->code != ctx->icmpcode + 1, 5670 TAILQ_NEXT(r, entries)); 5671 break; 5672 5673 default: 5674 break; 5675 } 5676 PF_TEST_ATTRIB(r->tos && !(r->tos == pd->tos), 5677 TAILQ_NEXT(r, entries)); 5678 PF_TEST_ATTRIB(r->prio && 5679 !pf_match_ieee8021q_pcp(r->prio, pd->m), 5680 TAILQ_NEXT(r, entries)); 5681 PF_TEST_ATTRIB(r->prob && 5682 r->prob <= arc4random(), 5683 TAILQ_NEXT(r, entries)); 5684 PF_TEST_ATTRIB(r->match_tag && !pf_match_tag(pd->m, r, 5685 &ctx->tag, pd->pf_mtag ? pd->pf_mtag->tag : 0), 5686 TAILQ_NEXT(r, entries)); 5687 PF_TEST_ATTRIB((r->rcv_kif && pf_match_rcvif(pd->m, r) == 5688 r->rcvifnot), 5689 TAILQ_NEXT(r, entries)); 5690 PF_TEST_ATTRIB((r->rule_flag & PFRULE_FRAGMENT && 5691 pd->virtual_proto != PF_VPROTO_FRAGMENT), 5692 TAILQ_NEXT(r, entries)); 5693 PF_TEST_ATTRIB(r->os_fingerprint != PF_OSFP_ANY && 5694 (pd->virtual_proto != IPPROTO_TCP || !pf_osfp_match( 5695 pf_osfp_fingerprint(pd, ctx->th), 5696 r->os_fingerprint)), 5697 TAILQ_NEXT(r, entries)); 5698 /* must be last! */ 5699 if (r->pktrate.limit) { 5700 PF_TEST_ATTRIB((pf_check_threshold(&r->pktrate)), 5701 TAILQ_NEXT(r, entries)); 5702 } 5703 /* FALLTHROUGH */ 5704 if (r->tag) 5705 ctx->tag = r->tag; 5706 if (r->anchor == NULL) { 5707 if (r->action == PF_MATCH) { 5708 /* 5709 * Apply translations before increasing counters, 5710 * in case it fails. 5711 */ 5712 transerror = pf_rule_apply_nat(ctx, r); 5713 switch (transerror) { 5714 case PFRES_MATCH: 5715 /* Translation action found in rule and applied successfully */ 5716 case PFRES_MAX: 5717 /* No translation action found in rule */ 5718 break; 5719 default: 5720 /* Translation action found in rule but failed to apply */ 5721 REASON_SET(&ctx->reason, transerror); 5722 return (PF_TEST_FAIL); 5723 } 5724 ri = malloc(sizeof(struct pf_krule_item), M_PF_RULE_ITEM, M_NOWAIT | M_ZERO); 5725 if (ri == NULL) { 5726 REASON_SET(&ctx->reason, PFRES_MEMORY); 5727 return (PF_TEST_FAIL); 5728 } 5729 ri->r = r; 5730 SLIST_INSERT_HEAD(&ctx->rules, ri, entry); 5731 pf_counter_u64_critical_enter(); 5732 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1); 5733 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len); 5734 pf_counter_u64_critical_exit(); 5735 pf_rule_to_actions(r, &pd->act); 5736 if (r->log) 5737 PFLOG_PACKET(r->action, PFRES_MATCH, r, 5738 ctx->a, ruleset, pd, 1, NULL); 5739 } else { 5740 /* 5741 * found matching r 5742 */ 5743 *ctx->rm = r; 5744 /* 5745 * anchor, with ruleset, where r belongs to 5746 */ 5747 *ctx->am = ctx->a; 5748 /* 5749 * ruleset where r belongs to 5750 */ 5751 *ctx->rsm = ruleset; 5752 /* 5753 * ruleset, where anchor belongs to. 5754 */ 5755 ctx->arsm = ctx->aruleset; 5756 } 5757 if (pd->act.log & PF_LOG_MATCHES) 5758 pf_log_matches(pd, r, ctx->a, ruleset, &ctx->rules); 5759 if (r->quick) { 5760 ctx->test_status = PF_TEST_QUICK; 5761 break; 5762 } 5763 } else { 5764 save_a = ctx->a; 5765 save_aruleset = ctx->aruleset; 5766 5767 ctx->a = r; /* remember anchor */ 5768 ctx->aruleset = ruleset; /* and its ruleset */ 5769 if (ctx->a->quick) 5770 ctx->test_status = PF_TEST_QUICK; 5771 /* 5772 * Note: we don't need to restore if we are not going 5773 * to continue with ruleset evaluation. 5774 */ 5775 if (pf_step_into_anchor(ctx, r) != PF_TEST_OK) { 5776 break; 5777 } 5778 ctx->a = save_a; 5779 ctx->aruleset = save_aruleset; 5780 } 5781 r = TAILQ_NEXT(r, entries); 5782 } 5783 5784 return (ctx->test_status); 5785 } 5786 5787 static int 5788 pf_test_rule(struct pf_krule **rm, struct pf_kstate **sm, 5789 struct pf_pdesc *pd, struct pf_krule **am, 5790 struct pf_kruleset **rsm, u_short *reason, struct inpcb *inp) 5791 { 5792 struct pf_krule *r = NULL; 5793 struct pf_kruleset *ruleset = NULL; 5794 struct pf_krule_item *ri; 5795 struct pf_test_ctx ctx; 5796 u_short transerror; 5797 int action = PF_PASS; 5798 u_int16_t bproto_sum = 0, bip_sum = 0; 5799 enum pf_test_status rv; 5800 5801 PF_RULES_RASSERT(); 5802 5803 bzero(&ctx, sizeof(ctx)); 5804 ctx.tag = -1; 5805 ctx.pd = pd; 5806 ctx.rm = rm; 5807 ctx.am = am; 5808 ctx.rsm = rsm; 5809 ctx.th = &pd->hdr.tcp; 5810 ctx.reason = *reason; 5811 SLIST_INIT(&ctx.rules); 5812 5813 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 5814 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 5815 5816 if (inp != NULL) { 5817 INP_LOCK_ASSERT(inp); 5818 pd->lookup.uid = inp->inp_cred->cr_uid; 5819 pd->lookup.gid = inp->inp_cred->cr_gid; 5820 pd->lookup.done = 1; 5821 } 5822 5823 if (pd->ip_sum) 5824 bip_sum = *pd->ip_sum; 5825 5826 switch (pd->virtual_proto) { 5827 case IPPROTO_TCP: 5828 bproto_sum = ctx.th->th_sum; 5829 pd->nsport = ctx.th->th_sport; 5830 pd->ndport = ctx.th->th_dport; 5831 break; 5832 case IPPROTO_UDP: 5833 bproto_sum = pd->hdr.udp.uh_sum; 5834 pd->nsport = pd->hdr.udp.uh_sport; 5835 pd->ndport = pd->hdr.udp.uh_dport; 5836 break; 5837 case IPPROTO_SCTP: 5838 pd->nsport = pd->hdr.sctp.src_port; 5839 pd->ndport = pd->hdr.sctp.dest_port; 5840 break; 5841 #ifdef INET 5842 case IPPROTO_ICMP: 5843 MPASS(pd->af == AF_INET); 5844 ctx.icmptype = pd->hdr.icmp.icmp_type; 5845 ctx.icmpcode = pd->hdr.icmp.icmp_code; 5846 ctx.state_icmp = pf_icmp_mapping(pd, ctx.icmptype, 5847 &ctx.icmp_dir, &ctx.virtual_id, &ctx.virtual_type); 5848 if (ctx.icmp_dir == PF_IN) { 5849 pd->nsport = ctx.virtual_id; 5850 pd->ndport = ctx.virtual_type; 5851 } else { 5852 pd->nsport = ctx.virtual_type; 5853 pd->ndport = ctx.virtual_id; 5854 } 5855 break; 5856 #endif /* INET */ 5857 #ifdef INET6 5858 case IPPROTO_ICMPV6: 5859 MPASS(pd->af == AF_INET6); 5860 ctx.icmptype = pd->hdr.icmp6.icmp6_type; 5861 ctx.icmpcode = pd->hdr.icmp6.icmp6_code; 5862 ctx.state_icmp = pf_icmp_mapping(pd, ctx.icmptype, 5863 &ctx.icmp_dir, &ctx.virtual_id, &ctx.virtual_type); 5864 if (ctx.icmp_dir == PF_IN) { 5865 pd->nsport = ctx.virtual_id; 5866 pd->ndport = ctx.virtual_type; 5867 } else { 5868 pd->nsport = ctx.virtual_type; 5869 pd->ndport = ctx.virtual_id; 5870 } 5871 5872 break; 5873 #endif /* INET6 */ 5874 default: 5875 pd->nsport = pd->ndport = 0; 5876 break; 5877 } 5878 pd->osport = pd->nsport; 5879 pd->odport = pd->ndport; 5880 5881 /* check packet for BINAT/NAT/RDR */ 5882 transerror = pf_get_translation(&ctx); 5883 switch (transerror) { 5884 default: 5885 /* A translation error occurred. */ 5886 REASON_SET(&ctx.reason, transerror); 5887 goto cleanup; 5888 case PFRES_MAX: 5889 /* No match. */ 5890 break; 5891 case PFRES_MATCH: 5892 KASSERT(ctx.sk != NULL, ("%s: null sk", __func__)); 5893 KASSERT(ctx.nk != NULL, ("%s: null nk", __func__)); 5894 if (ctx.nr->log) { 5895 PFLOG_PACKET(ctx.nr->action, PFRES_MATCH, ctx.nr, ctx.a, 5896 ruleset, pd, 1, NULL); 5897 } 5898 5899 ctx.rewrite += pf_translate_compat(&ctx); 5900 ctx.nat_pool = &(ctx.nr->rdr); 5901 } 5902 5903 ruleset = &pf_main_ruleset; 5904 rv = pf_match_rule(&ctx, ruleset); 5905 if (rv == PF_TEST_FAIL) { 5906 /* 5907 * Reason has been set in pf_match_rule() already. 5908 */ 5909 goto cleanup; 5910 } 5911 5912 r = *ctx.rm; /* matching rule */ 5913 ctx.a = *ctx.am; /* rule that defines an anchor containing 'r' */ 5914 ruleset = *ctx.rsm; /* ruleset of the anchor defined by the rule 'a' */ 5915 ctx.aruleset = ctx.arsm; /* ruleset of the 'a' rule itself */ 5916 5917 REASON_SET(&ctx.reason, PFRES_MATCH); 5918 5919 /* apply actions for last matching pass/block rule */ 5920 pf_rule_to_actions(r, &pd->act); 5921 transerror = pf_rule_apply_nat(&ctx, r); 5922 switch (transerror) { 5923 case PFRES_MATCH: 5924 /* Translation action found in rule and applied successfully */ 5925 case PFRES_MAX: 5926 /* No translation action found in rule */ 5927 break; 5928 default: 5929 /* Translation action found in rule but failed to apply */ 5930 REASON_SET(&ctx.reason, transerror); 5931 goto cleanup; 5932 } 5933 5934 if (r->log) { 5935 if (ctx.rewrite) 5936 m_copyback(pd->m, pd->off, pd->hdrlen, pd->hdr.any); 5937 PFLOG_PACKET(r->action, ctx.reason, r, ctx.a, ruleset, pd, 1, NULL); 5938 } 5939 if (pd->act.log & PF_LOG_MATCHES) 5940 pf_log_matches(pd, r, ctx.a, ruleset, &ctx.rules); 5941 if (pd->virtual_proto != PF_VPROTO_FRAGMENT && 5942 (r->action == PF_DROP) && 5943 ((r->rule_flag & PFRULE_RETURNRST) || 5944 (r->rule_flag & PFRULE_RETURNICMP) || 5945 (r->rule_flag & PFRULE_RETURN))) { 5946 pf_return(r, ctx.nr, pd, ctx.th, bproto_sum, 5947 bip_sum, &ctx.reason, r->rtableid); 5948 } 5949 5950 if (r->action == PF_DROP) 5951 goto cleanup; 5952 5953 if (ctx.tag > 0 && pf_tag_packet(pd, ctx.tag)) { 5954 REASON_SET(&ctx.reason, PFRES_MEMORY); 5955 goto cleanup; 5956 } 5957 if (pd->act.rtableid >= 0) 5958 M_SETFIB(pd->m, pd->act.rtableid); 5959 5960 if (r->rt) { 5961 /* 5962 * Set act.rt here instead of in pf_rule_to_actions() because 5963 * it is applied only from the last pass rule. 5964 */ 5965 pd->act.rt = r->rt; 5966 if (r->rt == PF_REPLYTO) 5967 pd->act.rt_af = pd->af; 5968 else 5969 pd->act.rt_af = pd->naf; 5970 if ((transerror = pf_map_addr_sn(pd->af, r, pd->src, 5971 &pd->act.rt_addr, &pd->act.rt_af, &pd->act.rt_kif, NULL, 5972 &(r->route), PF_SN_ROUTE)) != PFRES_MATCH) { 5973 REASON_SET(&ctx.reason, transerror); 5974 goto cleanup; 5975 } 5976 } 5977 5978 if (pd->virtual_proto != PF_VPROTO_FRAGMENT && 5979 (!ctx.state_icmp && (r->keep_state || ctx.nr != NULL || 5980 (pd->flags & PFDESC_TCP_NORM)))) { 5981 bool nat64; 5982 5983 action = pf_create_state(r, &ctx, sm, bproto_sum, bip_sum); 5984 ctx.sk = ctx.nk = NULL; 5985 if (action != PF_PASS) { 5986 pf_udp_mapping_release(ctx.udp_mapping); 5987 if (r->log || (ctx.nr != NULL && ctx.nr->log) || 5988 ctx.reason == PFRES_MEMORY) 5989 pd->act.log |= PF_LOG_FORCE; 5990 if (action == PF_DROP && 5991 (r->rule_flag & PFRULE_RETURN)) 5992 pf_return(r, ctx.nr, pd, ctx.th, 5993 bproto_sum, bip_sum, &ctx.reason, 5994 pd->act.rtableid); 5995 *reason = ctx.reason; 5996 return (action); 5997 } 5998 5999 nat64 = pd->af != pd->naf; 6000 if (nat64) { 6001 int ret; 6002 6003 if (ctx.sk == NULL) 6004 ctx.sk = (*sm)->key[pd->dir == PF_IN ? PF_SK_STACK : PF_SK_WIRE]; 6005 if (ctx.nk == NULL) 6006 ctx.nk = (*sm)->key[pd->dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK]; 6007 6008 if (pd->dir == PF_IN) { 6009 ret = pf_translate(pd, &ctx.sk->addr[pd->didx], 6010 ctx.sk->port[pd->didx], &ctx.sk->addr[pd->sidx], 6011 ctx.sk->port[pd->sidx], ctx.virtual_type, 6012 ctx.icmp_dir); 6013 } else { 6014 ret = pf_translate(pd, &ctx.sk->addr[pd->sidx], 6015 ctx.sk->port[pd->sidx], &ctx.sk->addr[pd->didx], 6016 ctx.sk->port[pd->didx], ctx.virtual_type, 6017 ctx.icmp_dir); 6018 } 6019 6020 if (ret < 0) 6021 goto cleanup; 6022 6023 ctx.rewrite += ret; 6024 6025 if (ctx.rewrite && ctx.sk->af != ctx.nk->af) 6026 action = PF_AFRT; 6027 } 6028 } else { 6029 while ((ri = SLIST_FIRST(&ctx.rules))) { 6030 SLIST_REMOVE_HEAD(&ctx.rules, entry); 6031 free(ri, M_PF_RULE_ITEM); 6032 } 6033 6034 uma_zfree(V_pf_state_key_z, ctx.sk); 6035 uma_zfree(V_pf_state_key_z, ctx.nk); 6036 ctx.sk = ctx.nk = NULL; 6037 pf_udp_mapping_release(ctx.udp_mapping); 6038 } 6039 6040 /* copy back packet headers if we performed NAT operations */ 6041 if (ctx.rewrite) 6042 m_copyback(pd->m, pd->off, pd->hdrlen, pd->hdr.any); 6043 6044 if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 6045 pd->dir == PF_OUT && 6046 V_pfsync_defer_ptr != NULL && V_pfsync_defer_ptr(*sm, pd->m)) { 6047 /* 6048 * We want the state created, but we dont 6049 * want to send this in case a partner 6050 * firewall has to know about it to allow 6051 * replies through it. 6052 */ 6053 *reason = ctx.reason; 6054 return (PF_DEFER); 6055 } 6056 6057 *reason = ctx.reason; 6058 return (action); 6059 6060 cleanup: 6061 while ((ri = SLIST_FIRST(&ctx.rules))) { 6062 SLIST_REMOVE_HEAD(&ctx.rules, entry); 6063 free(ri, M_PF_RULE_ITEM); 6064 } 6065 6066 uma_zfree(V_pf_state_key_z, ctx.sk); 6067 uma_zfree(V_pf_state_key_z, ctx.nk); 6068 pf_udp_mapping_release(ctx.udp_mapping); 6069 *reason = ctx.reason; 6070 6071 return (PF_DROP); 6072 } 6073 6074 static int 6075 pf_create_state(struct pf_krule *r, struct pf_test_ctx *ctx, 6076 struct pf_kstate **sm, u_int16_t bproto_sum, u_int16_t bip_sum) 6077 { 6078 struct pf_pdesc *pd = ctx->pd; 6079 struct pf_kstate *s = NULL; 6080 struct pf_ksrc_node *sns[PF_SN_MAX] = { NULL }; 6081 /* 6082 * XXXKS: The hash for PF_SN_LIMIT and PF_SN_ROUTE should be the same 6083 * but for PF_SN_NAT it is different. Don't try optimizing it, 6084 * just store all 3 hashes. 6085 */ 6086 struct pf_srchash *snhs[PF_SN_MAX] = { NULL }; 6087 struct tcphdr *th = &pd->hdr.tcp; 6088 u_int16_t mss = V_tcp_mssdflt; 6089 u_short sn_reason; 6090 struct pf_krule_item *ri; 6091 6092 /* check maximums */ 6093 if (r->max_states && 6094 (counter_u64_fetch(r->states_cur) >= r->max_states)) { 6095 counter_u64_add(V_pf_status.lcounters[LCNT_STATES], 1); 6096 REASON_SET(&ctx->reason, PFRES_MAXSTATES); 6097 goto csfailed; 6098 } 6099 /* src node for limits */ 6100 if ((r->rule_flag & PFRULE_SRCTRACK) && 6101 (sn_reason = pf_insert_src_node(sns, snhs, r, pd->src, pd->af, 6102 NULL, NULL, pd->af, PF_SN_LIMIT)) != 0) { 6103 REASON_SET(&ctx->reason, sn_reason); 6104 goto csfailed; 6105 } 6106 /* src node for route-to rule */ 6107 if (r->rt) { 6108 if ((r->route.opts & PF_POOL_STICKYADDR) && 6109 (sn_reason = pf_insert_src_node(sns, snhs, r, pd->src, 6110 pd->af, &pd->act.rt_addr, pd->act.rt_kif, pd->act.rt_af, 6111 PF_SN_ROUTE)) != 0) { 6112 REASON_SET(&ctx->reason, sn_reason); 6113 goto csfailed; 6114 } 6115 } 6116 /* src node for translation rule */ 6117 if (ctx->nr != NULL) { 6118 KASSERT(ctx->nat_pool != NULL, ("%s: nat_pool is NULL", __func__)); 6119 /* 6120 * The NAT addresses are chosen during ruleset parsing. 6121 * The new afto code stores post-nat addresses in nsaddr. 6122 * The old nat code (also used for new nat-to rules) creates 6123 * state keys and stores addresses in them. 6124 */ 6125 if ((ctx->nat_pool->opts & PF_POOL_STICKYADDR) && 6126 (sn_reason = pf_insert_src_node(sns, snhs, ctx->nr, 6127 ctx->sk ? &(ctx->sk->addr[pd->sidx]) : pd->src, pd->af, 6128 ctx->nk ? &(ctx->nk->addr[1]) : &(pd->nsaddr), NULL, 6129 pd->naf, PF_SN_NAT)) != 0 ) { 6130 REASON_SET(&ctx->reason, sn_reason); 6131 goto csfailed; 6132 } 6133 } 6134 s = pf_alloc_state(M_NOWAIT); 6135 if (s == NULL) { 6136 REASON_SET(&ctx->reason, PFRES_MEMORY); 6137 goto csfailed; 6138 } 6139 s->rule = r; 6140 s->nat_rule = ctx->nr; 6141 s->anchor = ctx->a; 6142 memcpy(&s->match_rules, &ctx->rules, sizeof(s->match_rules)); 6143 memcpy(&s->act, &pd->act, sizeof(struct pf_rule_actions)); 6144 6145 if (pd->act.allow_opts) 6146 s->state_flags |= PFSTATE_ALLOWOPTS; 6147 if (r->rule_flag & PFRULE_STATESLOPPY) 6148 s->state_flags |= PFSTATE_SLOPPY; 6149 if (pd->flags & PFDESC_TCP_NORM) /* Set by old-style scrub rules */ 6150 s->state_flags |= PFSTATE_SCRUB_TCP; 6151 if ((r->rule_flag & PFRULE_PFLOW) || 6152 (ctx->nr != NULL && ctx->nr->rule_flag & PFRULE_PFLOW)) 6153 s->state_flags |= PFSTATE_PFLOW; 6154 6155 s->act.log = pd->act.log & PF_LOG_ALL; 6156 s->sync_state = PFSYNC_S_NONE; 6157 s->state_flags |= pd->act.flags; /* Only needed for pfsync and state export */ 6158 6159 if (ctx->nr != NULL) 6160 s->act.log |= ctx->nr->log & PF_LOG_ALL; 6161 switch (pd->proto) { 6162 case IPPROTO_TCP: 6163 s->src.seqlo = ntohl(th->th_seq); 6164 s->src.seqhi = s->src.seqlo + pd->p_len + 1; 6165 if ((tcp_get_flags(th) & (TH_SYN|TH_ACK)) == TH_SYN && 6166 r->keep_state == PF_STATE_MODULATE) { 6167 /* Generate sequence number modulator */ 6168 if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 6169 0) 6170 s->src.seqdiff = 1; 6171 pf_change_proto_a(pd->m, &th->th_seq, &th->th_sum, 6172 htonl(s->src.seqlo + s->src.seqdiff), 0); 6173 ctx->rewrite = 1; 6174 } else 6175 s->src.seqdiff = 0; 6176 if (tcp_get_flags(th) & TH_SYN) { 6177 s->src.seqhi++; 6178 s->src.wscale = pf_get_wscale(pd); 6179 } 6180 s->src.max_win = MAX(ntohs(th->th_win), 1); 6181 if (s->src.wscale & PF_WSCALE_MASK) { 6182 /* Remove scale factor from initial window */ 6183 int win = s->src.max_win; 6184 win += 1 << (s->src.wscale & PF_WSCALE_MASK); 6185 s->src.max_win = (win - 1) >> 6186 (s->src.wscale & PF_WSCALE_MASK); 6187 } 6188 if (tcp_get_flags(th) & TH_FIN) 6189 s->src.seqhi++; 6190 s->dst.seqhi = 1; 6191 s->dst.max_win = 1; 6192 pf_set_protostate(s, PF_PEER_SRC, TCPS_SYN_SENT); 6193 pf_set_protostate(s, PF_PEER_DST, TCPS_CLOSED); 6194 s->timeout = PFTM_TCP_FIRST_PACKET; 6195 atomic_add_32(&V_pf_status.states_halfopen, 1); 6196 break; 6197 case IPPROTO_UDP: 6198 pf_set_protostate(s, PF_PEER_SRC, PFUDPS_SINGLE); 6199 pf_set_protostate(s, PF_PEER_DST, PFUDPS_NO_TRAFFIC); 6200 s->timeout = PFTM_UDP_FIRST_PACKET; 6201 break; 6202 case IPPROTO_SCTP: 6203 pf_set_protostate(s, PF_PEER_SRC, SCTP_COOKIE_WAIT); 6204 pf_set_protostate(s, PF_PEER_DST, SCTP_CLOSED); 6205 s->timeout = PFTM_SCTP_FIRST_PACKET; 6206 break; 6207 case IPPROTO_ICMP: 6208 #ifdef INET6 6209 case IPPROTO_ICMPV6: 6210 #endif /* INET6 */ 6211 s->timeout = PFTM_ICMP_FIRST_PACKET; 6212 break; 6213 default: 6214 pf_set_protostate(s, PF_PEER_SRC, PFOTHERS_SINGLE); 6215 pf_set_protostate(s, PF_PEER_DST, PFOTHERS_NO_TRAFFIC); 6216 s->timeout = PFTM_OTHER_FIRST_PACKET; 6217 } 6218 6219 s->creation = s->expire = pf_get_uptime(); 6220 6221 if (pd->proto == IPPROTO_TCP) { 6222 if (s->state_flags & PFSTATE_SCRUB_TCP && 6223 pf_normalize_tcp_init(pd, th, &s->src)) { 6224 REASON_SET(&ctx->reason, PFRES_MEMORY); 6225 goto csfailed; 6226 } 6227 if (s->state_flags & PFSTATE_SCRUB_TCP && s->src.scrub && 6228 pf_normalize_tcp_stateful(pd, &ctx->reason, th, s, 6229 &s->src, &s->dst, &ctx->rewrite)) { 6230 /* This really shouldn't happen!!! */ 6231 DPFPRINTF(PF_DEBUG_URGENT, 6232 "%s: tcp normalize failed on first " 6233 "pkt", __func__); 6234 goto csfailed; 6235 } 6236 } else if (pd->proto == IPPROTO_SCTP) { 6237 if (pf_normalize_sctp_init(pd, &s->src, &s->dst)) 6238 goto csfailed; 6239 if (! (pd->sctp_flags & (PFDESC_SCTP_INIT | PFDESC_SCTP_ADD_IP))) 6240 goto csfailed; 6241 } 6242 s->direction = pd->dir; 6243 6244 /* 6245 * sk/nk could already been setup by pf_get_translation(). 6246 */ 6247 if (ctx->sk == NULL && ctx->nk == NULL) { 6248 MPASS(pd->sport == NULL || (pd->osport == *pd->sport)); 6249 MPASS(pd->dport == NULL || (pd->odport == *pd->dport)); 6250 if (pf_state_key_setup(pd, pd->nsport, pd->ndport, 6251 &ctx->sk, &ctx->nk)) { 6252 goto csfailed; 6253 } 6254 } else 6255 KASSERT((ctx->sk != NULL && ctx->nk != NULL), ("%s: nr %p sk %p, nk %p", 6256 __func__, ctx->nr, ctx->sk, ctx->nk)); 6257 6258 /* Swap sk/nk for PF_OUT. */ 6259 if (pf_state_insert(BOUND_IFACE(s, pd), pd->kif, 6260 (pd->dir == PF_IN) ? ctx->sk : ctx->nk, 6261 (pd->dir == PF_IN) ? ctx->nk : ctx->sk, s)) { 6262 REASON_SET(&ctx->reason, PFRES_STATEINS); 6263 goto drop; 6264 } else 6265 *sm = s; 6266 ctx->sk = ctx->nk = NULL; 6267 6268 STATE_INC_COUNTERS(s); 6269 6270 /* 6271 * Lock order is important: first state, then source node. 6272 */ 6273 for (pf_sn_types_t sn_type=0; sn_type<PF_SN_MAX; sn_type++) { 6274 if (pf_src_node_exists(&sns[sn_type], snhs[sn_type])) { 6275 s->sns[sn_type] = sns[sn_type]; 6276 PF_HASHROW_UNLOCK(snhs[sn_type]); 6277 } 6278 } 6279 6280 if (ctx->tag > 0) 6281 s->tag = ctx->tag; 6282 if (pd->proto == IPPROTO_TCP && (tcp_get_flags(th) & (TH_SYN|TH_ACK)) == 6283 TH_SYN && r->keep_state == PF_STATE_SYNPROXY && pd->dir == PF_IN) { 6284 pf_set_protostate(s, PF_PEER_SRC, PF_TCPS_PROXY_SRC); 6285 pf_undo_nat(ctx->nr, pd, bip_sum); 6286 s->src.seqhi = arc4random(); 6287 /* Find mss option */ 6288 int rtid = M_GETFIB(pd->m); 6289 mss = pf_get_mss(pd); 6290 mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 6291 mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 6292 s->src.mss = mss; 6293 pf_send_tcp(r, pd->af, pd->dst, pd->src, th->th_dport, 6294 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 6295 TH_SYN|TH_ACK, 0, s->src.mss, 0, M_SKIP_FIREWALL, 0, 0, 6296 pd->act.rtableid); 6297 REASON_SET(&ctx->reason, PFRES_SYNPROXY); 6298 return (PF_SYNPROXY_DROP); 6299 } 6300 6301 s->udp_mapping = ctx->udp_mapping; 6302 6303 return (PF_PASS); 6304 6305 csfailed: 6306 while ((ri = SLIST_FIRST(&ctx->rules))) { 6307 SLIST_REMOVE_HEAD(&ctx->rules, entry); 6308 free(ri, M_PF_RULE_ITEM); 6309 } 6310 6311 uma_zfree(V_pf_state_key_z, ctx->sk); 6312 uma_zfree(V_pf_state_key_z, ctx->nk); 6313 6314 for (pf_sn_types_t sn_type=0; sn_type<PF_SN_MAX; sn_type++) { 6315 if (pf_src_node_exists(&sns[sn_type], snhs[sn_type])) { 6316 if (--sns[sn_type]->states == 0 && 6317 sns[sn_type]->expire == 0) { 6318 pf_unlink_src_node(sns[sn_type]); 6319 pf_free_src_node(sns[sn_type]); 6320 counter_u64_add( 6321 V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1); 6322 } 6323 PF_HASHROW_UNLOCK(snhs[sn_type]); 6324 } 6325 } 6326 6327 drop: 6328 if (s != NULL) { 6329 pf_src_tree_remove_state(s); 6330 s->timeout = PFTM_UNLINKED; 6331 pf_free_state(s); 6332 } 6333 6334 return (PF_DROP); 6335 } 6336 6337 int 6338 pf_translate(struct pf_pdesc *pd, struct pf_addr *saddr, u_int16_t sport, 6339 struct pf_addr *daddr, u_int16_t dport, u_int16_t virtual_type, 6340 int icmp_dir) 6341 { 6342 /* 6343 * pf_translate() implements OpenBSD's "new" NAT approach. 6344 * We don't follow it, because it involves a breaking syntax change 6345 * (removing nat/rdr rules, moving it into regular pf rules.) 6346 * It also moves NAT processing to be done after normal rules evaluation 6347 * whereas in FreeBSD that's done before rules processing. 6348 * 6349 * We adopt the function only for nat64, and keep other NAT processing 6350 * before rules processing. 6351 */ 6352 int rewrite = 0; 6353 int afto = pd->af != pd->naf; 6354 6355 MPASS(afto); 6356 6357 switch (pd->proto) { 6358 case IPPROTO_TCP: 6359 case IPPROTO_UDP: 6360 case IPPROTO_SCTP: 6361 if (afto || *pd->sport != sport) { 6362 pf_change_ap(pd, pd->src, pd->sport, 6363 saddr, sport); 6364 rewrite = 1; 6365 } 6366 if (afto || *pd->dport != dport) { 6367 pf_change_ap(pd, pd->dst, pd->dport, 6368 daddr, dport); 6369 rewrite = 1; 6370 } 6371 break; 6372 6373 #ifdef INET 6374 case IPPROTO_ICMP: 6375 /* pf_translate() is also used when logging invalid packets */ 6376 if (pd->af != AF_INET) 6377 return (0); 6378 6379 if (afto) { 6380 if (pf_translate_icmp_af(AF_INET6, &pd->hdr.icmp)) 6381 return (-1); 6382 pd->proto = IPPROTO_ICMPV6; 6383 rewrite = 1; 6384 } 6385 if (virtual_type == htons(ICMP_ECHO)) { 6386 u_int16_t icmpid = (icmp_dir == PF_IN) ? sport : dport; 6387 6388 if (icmpid != pd->hdr.icmp.icmp_id) { 6389 pd->hdr.icmp.icmp_cksum = pf_cksum_fixup( 6390 pd->hdr.icmp.icmp_cksum, 6391 pd->hdr.icmp.icmp_id, icmpid, 0); 6392 pd->hdr.icmp.icmp_id = icmpid; 6393 /* XXX TODO copyback. */ 6394 rewrite = 1; 6395 } 6396 } 6397 break; 6398 #endif /* INET */ 6399 6400 #ifdef INET6 6401 case IPPROTO_ICMPV6: 6402 /* pf_translate() is also used when logging invalid packets */ 6403 if (pd->af != AF_INET6) 6404 return (0); 6405 6406 if (afto) { 6407 /* ip_sum will be recalculated in pf_translate_af */ 6408 if (pf_translate_icmp_af(AF_INET, &pd->hdr.icmp6)) 6409 return (0); 6410 pd->proto = IPPROTO_ICMP; 6411 rewrite = 1; 6412 } 6413 break; 6414 #endif /* INET6 */ 6415 6416 default: 6417 break; 6418 } 6419 6420 return (rewrite); 6421 } 6422 6423 int 6424 pf_translate_compat(struct pf_test_ctx *ctx) 6425 { 6426 struct pf_pdesc *pd = ctx->pd; 6427 struct pf_state_key *nk = ctx->nk; 6428 struct tcphdr *th = &pd->hdr.tcp; 6429 int rewrite = 0; 6430 6431 KASSERT(ctx->sk != NULL, ("%s: null sk", __func__)); 6432 KASSERT(ctx->nk != NULL, ("%s: null nk", __func__)); 6433 6434 switch (pd->virtual_proto) { 6435 case IPPROTO_TCP: 6436 if (PF_ANEQ(&pd->nsaddr, &nk->addr[pd->sidx], pd->af) || 6437 nk->port[pd->sidx] != pd->nsport) { 6438 pf_change_ap(pd, pd->src, &th->th_sport, 6439 &nk->addr[pd->sidx], nk->port[pd->sidx]); 6440 pd->sport = &th->th_sport; 6441 pd->nsport = th->th_sport; 6442 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6443 } 6444 6445 if (PF_ANEQ(&pd->ndaddr, &nk->addr[pd->didx], pd->af) || 6446 nk->port[pd->didx] != pd->ndport) { 6447 pf_change_ap(pd, pd->dst, &th->th_dport, 6448 &nk->addr[pd->didx], nk->port[pd->didx]); 6449 pd->dport = &th->th_dport; 6450 pd->ndport = th->th_dport; 6451 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6452 } 6453 rewrite++; 6454 break; 6455 case IPPROTO_UDP: 6456 if (PF_ANEQ(&pd->nsaddr, &nk->addr[pd->sidx], pd->af) || 6457 nk->port[pd->sidx] != pd->nsport) { 6458 pf_change_ap(pd, pd->src, 6459 &pd->hdr.udp.uh_sport, 6460 &nk->addr[pd->sidx], 6461 nk->port[pd->sidx]); 6462 pd->sport = &pd->hdr.udp.uh_sport; 6463 pd->nsport = pd->hdr.udp.uh_sport; 6464 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6465 } 6466 6467 if (PF_ANEQ(&pd->ndaddr, &nk->addr[pd->didx], pd->af) || 6468 nk->port[pd->didx] != pd->ndport) { 6469 pf_change_ap(pd, pd->dst, 6470 &pd->hdr.udp.uh_dport, 6471 &nk->addr[pd->didx], 6472 nk->port[pd->didx]); 6473 pd->dport = &pd->hdr.udp.uh_dport; 6474 pd->ndport = pd->hdr.udp.uh_dport; 6475 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6476 } 6477 rewrite++; 6478 break; 6479 case IPPROTO_SCTP: { 6480 if (PF_ANEQ(&pd->nsaddr, &nk->addr[pd->sidx], pd->af) || 6481 nk->port[pd->sidx] != pd->nsport) { 6482 pf_change_ap(pd, pd->src, 6483 &pd->hdr.sctp.src_port, 6484 &nk->addr[pd->sidx], 6485 nk->port[pd->sidx]); 6486 pd->sport = &pd->hdr.sctp.src_port; 6487 pd->nsport = pd->hdr.sctp.src_port; 6488 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6489 } 6490 if (PF_ANEQ(&pd->ndaddr, &nk->addr[pd->didx], pd->af) || 6491 nk->port[pd->didx] != pd->ndport) { 6492 pf_change_ap(pd, pd->dst, 6493 &pd->hdr.sctp.dest_port, 6494 &nk->addr[pd->didx], 6495 nk->port[pd->didx]); 6496 pd->dport = &pd->hdr.sctp.dest_port; 6497 pd->ndport = pd->hdr.sctp.dest_port; 6498 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6499 } 6500 break; 6501 } 6502 #ifdef INET 6503 case IPPROTO_ICMP: 6504 if (PF_ANEQ(&pd->nsaddr, &nk->addr[pd->sidx], AF_INET)) { 6505 pf_change_a(&pd->src->v4.s_addr, pd->ip_sum, 6506 nk->addr[pd->sidx].v4.s_addr, 0); 6507 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6508 } 6509 6510 if (PF_ANEQ(&pd->ndaddr, &nk->addr[pd->didx], AF_INET)) { 6511 pf_change_a(&pd->dst->v4.s_addr, pd->ip_sum, 6512 nk->addr[pd->didx].v4.s_addr, 0); 6513 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6514 } 6515 6516 if (ctx->virtual_type == htons(ICMP_ECHO) && 6517 nk->port[pd->sidx] != pd->hdr.icmp.icmp_id) { 6518 pd->hdr.icmp.icmp_cksum = pf_cksum_fixup( 6519 pd->hdr.icmp.icmp_cksum, pd->nsport, 6520 nk->port[pd->sidx], 0); 6521 pd->hdr.icmp.icmp_id = nk->port[pd->sidx]; 6522 pd->sport = &pd->hdr.icmp.icmp_id; 6523 } 6524 m_copyback(pd->m, pd->off, ICMP_MINLEN, (caddr_t)&pd->hdr.icmp); 6525 break; 6526 #endif /* INET */ 6527 #ifdef INET6 6528 case IPPROTO_ICMPV6: 6529 if (PF_ANEQ(&pd->nsaddr, &nk->addr[pd->sidx], AF_INET6)) { 6530 pf_change_a6(pd->src, &pd->hdr.icmp6.icmp6_cksum, 6531 &nk->addr[pd->sidx], 0); 6532 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6533 } 6534 6535 if (PF_ANEQ(&pd->ndaddr, &nk->addr[pd->didx], AF_INET6)) { 6536 pf_change_a6(pd->dst, &pd->hdr.icmp6.icmp6_cksum, 6537 &nk->addr[pd->didx], 0); 6538 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6539 } 6540 rewrite++; 6541 break; 6542 #endif /* INET */ 6543 default: 6544 switch (pd->af) { 6545 #ifdef INET 6546 case AF_INET: 6547 if (PF_ANEQ(&pd->nsaddr, 6548 &nk->addr[pd->sidx], AF_INET)) { 6549 pf_change_a(&pd->src->v4.s_addr, 6550 pd->ip_sum, 6551 nk->addr[pd->sidx].v4.s_addr, 0); 6552 pf_addrcpy(&pd->nsaddr, pd->src, pd->af); 6553 } 6554 6555 if (PF_ANEQ(&pd->ndaddr, 6556 &nk->addr[pd->didx], AF_INET)) { 6557 pf_change_a(&pd->dst->v4.s_addr, 6558 pd->ip_sum, 6559 nk->addr[pd->didx].v4.s_addr, 0); 6560 pf_addrcpy(&pd->ndaddr, pd->dst, pd->af); 6561 } 6562 break; 6563 #endif /* INET */ 6564 #ifdef INET6 6565 case AF_INET6: 6566 if (PF_ANEQ(&pd->nsaddr, 6567 &nk->addr[pd->sidx], AF_INET6)) { 6568 pf_addrcpy(&pd->nsaddr, &nk->addr[pd->sidx], 6569 pd->af); 6570 pf_addrcpy(pd->src, &nk->addr[pd->sidx], pd->af); 6571 } 6572 6573 if (PF_ANEQ(&pd->ndaddr, 6574 &nk->addr[pd->didx], AF_INET6)) { 6575 pf_addrcpy(&pd->ndaddr, &nk->addr[pd->didx], 6576 pd->af); 6577 pf_addrcpy(pd->dst, &nk->addr[pd->didx], 6578 pd->af); 6579 } 6580 break; 6581 #endif /* INET6 */ 6582 } 6583 break; 6584 } 6585 return (rewrite); 6586 } 6587 6588 static int 6589 pf_tcp_track_full(struct pf_kstate *state, struct pf_pdesc *pd, 6590 u_short *reason, int *copyback, struct pf_state_peer *src, 6591 struct pf_state_peer *dst, u_int8_t psrc, u_int8_t pdst) 6592 { 6593 struct tcphdr *th = &pd->hdr.tcp; 6594 u_int16_t win = ntohs(th->th_win); 6595 u_int32_t ack, end, data_end, seq, orig_seq; 6596 u_int8_t sws, dws; 6597 int ackskew; 6598 6599 if (src->wscale && dst->wscale && !(tcp_get_flags(th) & TH_SYN)) { 6600 sws = src->wscale & PF_WSCALE_MASK; 6601 dws = dst->wscale & PF_WSCALE_MASK; 6602 } else 6603 sws = dws = 0; 6604 6605 /* 6606 * Sequence tracking algorithm from Guido van Rooij's paper: 6607 * http://www.madison-gurkha.com/publications/tcp_filtering/ 6608 * tcp_filtering.ps 6609 */ 6610 6611 orig_seq = seq = ntohl(th->th_seq); 6612 if (src->seqlo == 0) { 6613 /* First packet from this end. Set its state */ 6614 6615 if ((state->state_flags & PFSTATE_SCRUB_TCP || dst->scrub) && 6616 src->scrub == NULL) { 6617 if (pf_normalize_tcp_init(pd, th, src)) { 6618 REASON_SET(reason, PFRES_MEMORY); 6619 return (PF_DROP); 6620 } 6621 } 6622 6623 /* Deferred generation of sequence number modulator */ 6624 if (dst->seqdiff && !src->seqdiff) { 6625 /* use random iss for the TCP server */ 6626 while ((src->seqdiff = arc4random() - seq) == 0) 6627 ; 6628 ack = ntohl(th->th_ack) - dst->seqdiff; 6629 pf_change_proto_a(pd->m, &th->th_seq, &th->th_sum, htonl(seq + 6630 src->seqdiff), 0); 6631 pf_change_proto_a(pd->m, &th->th_ack, &th->th_sum, htonl(ack), 0); 6632 *copyback = 1; 6633 } else { 6634 ack = ntohl(th->th_ack); 6635 } 6636 6637 end = seq + pd->p_len; 6638 if (tcp_get_flags(th) & TH_SYN) { 6639 end++; 6640 if (dst->wscale & PF_WSCALE_FLAG) { 6641 src->wscale = pf_get_wscale(pd); 6642 if (src->wscale & PF_WSCALE_FLAG) { 6643 /* Remove scale factor from initial 6644 * window */ 6645 sws = src->wscale & PF_WSCALE_MASK; 6646 win = ((u_int32_t)win + (1 << sws) - 1) 6647 >> sws; 6648 dws = dst->wscale & PF_WSCALE_MASK; 6649 } else { 6650 /* fixup other window */ 6651 dst->max_win = MIN(TCP_MAXWIN, 6652 (u_int32_t)dst->max_win << 6653 (dst->wscale & PF_WSCALE_MASK)); 6654 /* in case of a retrans SYN|ACK */ 6655 dst->wscale = 0; 6656 } 6657 } 6658 } 6659 data_end = end; 6660 if (tcp_get_flags(th) & TH_FIN) 6661 end++; 6662 6663 src->seqlo = seq; 6664 if (src->state < TCPS_SYN_SENT) 6665 pf_set_protostate(state, psrc, TCPS_SYN_SENT); 6666 6667 /* 6668 * May need to slide the window (seqhi may have been set by 6669 * the crappy stack check or if we picked up the connection 6670 * after establishment) 6671 */ 6672 if (src->seqhi == 1 || 6673 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 6674 src->seqhi = end + MAX(1, dst->max_win << dws); 6675 if (win > src->max_win) 6676 src->max_win = win; 6677 6678 } else { 6679 ack = ntohl(th->th_ack) - dst->seqdiff; 6680 if (src->seqdiff) { 6681 /* Modulate sequence numbers */ 6682 pf_change_proto_a(pd->m, &th->th_seq, &th->th_sum, htonl(seq + 6683 src->seqdiff), 0); 6684 pf_change_proto_a(pd->m, &th->th_ack, &th->th_sum, htonl(ack), 0); 6685 *copyback = 1; 6686 } 6687 end = seq + pd->p_len; 6688 if (tcp_get_flags(th) & TH_SYN) 6689 end++; 6690 data_end = end; 6691 if (tcp_get_flags(th) & TH_FIN) 6692 end++; 6693 } 6694 6695 if ((tcp_get_flags(th) & TH_ACK) == 0) { 6696 /* Let it pass through the ack skew check */ 6697 ack = dst->seqlo; 6698 } else if ((ack == 0 && 6699 (tcp_get_flags(th) & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 6700 /* broken tcp stacks do not set ack */ 6701 (dst->state < TCPS_SYN_SENT)) { 6702 /* 6703 * Many stacks (ours included) will set the ACK number in an 6704 * FIN|ACK if the SYN times out -- no sequence to ACK. 6705 */ 6706 ack = dst->seqlo; 6707 } 6708 6709 if (seq == end) { 6710 /* Ease sequencing restrictions on no data packets */ 6711 seq = src->seqlo; 6712 data_end = end = seq; 6713 } 6714 6715 ackskew = dst->seqlo - ack; 6716 6717 /* 6718 * Need to demodulate the sequence numbers in any TCP SACK options 6719 * (Selective ACK). We could optionally validate the SACK values 6720 * against the current ACK window, either forwards or backwards, but 6721 * I'm not confident that SACK has been implemented properly 6722 * everywhere. It wouldn't surprise me if several stacks accidentally 6723 * SACK too far backwards of previously ACKed data. There really aren't 6724 * any security implications of bad SACKing unless the target stack 6725 * doesn't validate the option length correctly. Someone trying to 6726 * spoof into a TCP connection won't bother blindly sending SACK 6727 * options anyway. 6728 */ 6729 if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 6730 if (pf_modulate_sack(pd, th, dst)) 6731 *copyback = 1; 6732 } 6733 6734 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 6735 if (SEQ_GEQ(src->seqhi, data_end) && 6736 /* Last octet inside other's window space */ 6737 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 6738 /* Retrans: not more than one window back */ 6739 (ackskew >= -MAXACKWINDOW) && 6740 /* Acking not more than one reassembled fragment backwards */ 6741 (ackskew <= (MAXACKWINDOW << sws)) && 6742 /* Acking not more than one window forward */ 6743 ((tcp_get_flags(th) & TH_RST) == 0 || orig_seq == src->seqlo || 6744 (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo))) { 6745 /* Require an exact/+1 sequence match on resets when possible */ 6746 6747 if (dst->scrub || src->scrub) { 6748 if (pf_normalize_tcp_stateful(pd, reason, th, 6749 state, src, dst, copyback)) 6750 return (PF_DROP); 6751 } 6752 6753 /* update max window */ 6754 if (src->max_win < win) 6755 src->max_win = win; 6756 /* synchronize sequencing */ 6757 if (SEQ_GT(end, src->seqlo)) 6758 src->seqlo = end; 6759 /* slide the window of what the other end can send */ 6760 if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 6761 dst->seqhi = ack + MAX((win << sws), 1); 6762 6763 /* update states */ 6764 if (tcp_get_flags(th) & TH_SYN) 6765 if (src->state < TCPS_SYN_SENT) 6766 pf_set_protostate(state, psrc, TCPS_SYN_SENT); 6767 if (tcp_get_flags(th) & TH_FIN) 6768 if (src->state < TCPS_CLOSING) 6769 pf_set_protostate(state, psrc, TCPS_CLOSING); 6770 if (tcp_get_flags(th) & TH_ACK) { 6771 if (dst->state == TCPS_SYN_SENT) { 6772 pf_set_protostate(state, pdst, 6773 TCPS_ESTABLISHED); 6774 if (src->state == TCPS_ESTABLISHED && 6775 state->sns[PF_SN_LIMIT] != NULL && 6776 pf_src_connlimit(state)) { 6777 REASON_SET(reason, PFRES_SRCLIMIT); 6778 return (PF_DROP); 6779 } 6780 } else if (dst->state == TCPS_CLOSING) 6781 pf_set_protostate(state, pdst, 6782 TCPS_FIN_WAIT_2); 6783 } 6784 if (tcp_get_flags(th) & TH_RST) 6785 pf_set_protostate(state, PF_PEER_BOTH, TCPS_TIME_WAIT); 6786 6787 /* update expire time */ 6788 state->expire = pf_get_uptime(); 6789 if (src->state >= TCPS_FIN_WAIT_2 && 6790 dst->state >= TCPS_FIN_WAIT_2) 6791 state->timeout = PFTM_TCP_CLOSED; 6792 else if (src->state >= TCPS_CLOSING && 6793 dst->state >= TCPS_CLOSING) 6794 state->timeout = PFTM_TCP_FIN_WAIT; 6795 else if (src->state < TCPS_ESTABLISHED || 6796 dst->state < TCPS_ESTABLISHED) 6797 state->timeout = PFTM_TCP_OPENING; 6798 else if (src->state >= TCPS_CLOSING || 6799 dst->state >= TCPS_CLOSING) 6800 state->timeout = PFTM_TCP_CLOSING; 6801 else 6802 state->timeout = PFTM_TCP_ESTABLISHED; 6803 6804 /* Fall through to PASS packet */ 6805 6806 } else if ((dst->state < TCPS_SYN_SENT || 6807 dst->state >= TCPS_FIN_WAIT_2 || 6808 src->state >= TCPS_FIN_WAIT_2) && 6809 SEQ_GEQ(src->seqhi + MAXACKWINDOW, data_end) && 6810 /* Within a window forward of the originating packet */ 6811 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 6812 /* Within a window backward of the originating packet */ 6813 6814 /* 6815 * This currently handles three situations: 6816 * 1) Stupid stacks will shotgun SYNs before their peer 6817 * replies. 6818 * 2) When PF catches an already established stream (the 6819 * firewall rebooted, the state table was flushed, routes 6820 * changed...) 6821 * 3) Packets get funky immediately after the connection 6822 * closes (this should catch Solaris spurious ACK|FINs 6823 * that web servers like to spew after a close) 6824 * 6825 * This must be a little more careful than the above code 6826 * since packet floods will also be caught here. We don't 6827 * update the TTL here to mitigate the damage of a packet 6828 * flood and so the same code can handle awkward establishment 6829 * and a loosened connection close. 6830 * In the establishment case, a correct peer response will 6831 * validate the connection, go through the normal state code 6832 * and keep updating the state TTL. 6833 */ 6834 6835 if (V_pf_status.debug >= PF_DEBUG_MISC) { 6836 printf("pf: loose state match: "); 6837 pf_print_state(state); 6838 pf_print_flags(tcp_get_flags(th)); 6839 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 6840 "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 6841 pd->p_len, ackskew, (unsigned long long)state->packets[0], 6842 (unsigned long long)state->packets[1], 6843 pd->dir == PF_IN ? "in" : "out", 6844 pd->dir == state->direction ? "fwd" : "rev"); 6845 } 6846 6847 if (dst->scrub || src->scrub) { 6848 if (pf_normalize_tcp_stateful(pd, reason, th, 6849 state, src, dst, copyback)) 6850 return (PF_DROP); 6851 } 6852 6853 /* update max window */ 6854 if (src->max_win < win) 6855 src->max_win = win; 6856 /* synchronize sequencing */ 6857 if (SEQ_GT(end, src->seqlo)) 6858 src->seqlo = end; 6859 /* slide the window of what the other end can send */ 6860 if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 6861 dst->seqhi = ack + MAX((win << sws), 1); 6862 6863 /* 6864 * Cannot set dst->seqhi here since this could be a shotgunned 6865 * SYN and not an already established connection. 6866 */ 6867 6868 if (tcp_get_flags(th) & TH_FIN) 6869 if (src->state < TCPS_CLOSING) 6870 pf_set_protostate(state, psrc, TCPS_CLOSING); 6871 if (tcp_get_flags(th) & TH_RST) 6872 pf_set_protostate(state, PF_PEER_BOTH, TCPS_TIME_WAIT); 6873 6874 /* Fall through to PASS packet */ 6875 6876 } else { 6877 if (state->dst.state == TCPS_SYN_SENT && 6878 state->src.state == TCPS_SYN_SENT) { 6879 /* Send RST for state mismatches during handshake */ 6880 if (!(tcp_get_flags(th) & TH_RST)) 6881 pf_send_tcp(state->rule, pd->af, 6882 pd->dst, pd->src, th->th_dport, 6883 th->th_sport, ntohl(th->th_ack), 0, 6884 TH_RST, 0, 0, 6885 state->rule->return_ttl, M_SKIP_FIREWALL, 6886 0, 0, state->act.rtableid); 6887 src->seqlo = 0; 6888 src->seqhi = 1; 6889 src->max_win = 1; 6890 } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 6891 printf("pf: BAD state: "); 6892 pf_print_state(state); 6893 pf_print_flags(tcp_get_flags(th)); 6894 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 6895 "pkts=%llu:%llu dir=%s,%s\n", 6896 seq, orig_seq, ack, pd->p_len, ackskew, 6897 (unsigned long long)state->packets[0], 6898 (unsigned long long)state->packets[1], 6899 pd->dir == PF_IN ? "in" : "out", 6900 pd->dir == state->direction ? "fwd" : "rev"); 6901 printf("pf: State failure on: %c %c %c %c | %c %c\n", 6902 SEQ_GEQ(src->seqhi, data_end) ? ' ' : '1', 6903 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 6904 ' ': '2', 6905 (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 6906 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 6907 SEQ_GEQ(src->seqhi + MAXACKWINDOW, data_end) ?' ' :'5', 6908 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 6909 } 6910 REASON_SET(reason, PFRES_BADSTATE); 6911 return (PF_DROP); 6912 } 6913 6914 return (PF_PASS); 6915 } 6916 6917 static int 6918 pf_tcp_track_sloppy(struct pf_kstate *state, struct pf_pdesc *pd, 6919 u_short *reason, struct pf_state_peer *src, struct pf_state_peer *dst, 6920 u_int8_t psrc, u_int8_t pdst) 6921 { 6922 struct tcphdr *th = &pd->hdr.tcp; 6923 6924 if (tcp_get_flags(th) & TH_SYN) 6925 if (src->state < TCPS_SYN_SENT) 6926 pf_set_protostate(state, psrc, TCPS_SYN_SENT); 6927 if (tcp_get_flags(th) & TH_FIN) 6928 if (src->state < TCPS_CLOSING) 6929 pf_set_protostate(state, psrc, TCPS_CLOSING); 6930 if (tcp_get_flags(th) & TH_ACK) { 6931 if (dst->state == TCPS_SYN_SENT) { 6932 pf_set_protostate(state, pdst, TCPS_ESTABLISHED); 6933 if (src->state == TCPS_ESTABLISHED && 6934 state->sns[PF_SN_LIMIT] != NULL && 6935 pf_src_connlimit(state)) { 6936 REASON_SET(reason, PFRES_SRCLIMIT); 6937 return (PF_DROP); 6938 } 6939 } else if (dst->state == TCPS_CLOSING) { 6940 pf_set_protostate(state, pdst, TCPS_FIN_WAIT_2); 6941 } else if (src->state == TCPS_SYN_SENT && 6942 dst->state < TCPS_SYN_SENT) { 6943 /* 6944 * Handle a special sloppy case where we only see one 6945 * half of the connection. If there is a ACK after 6946 * the initial SYN without ever seeing a packet from 6947 * the destination, set the connection to established. 6948 */ 6949 pf_set_protostate(state, PF_PEER_BOTH, 6950 TCPS_ESTABLISHED); 6951 dst->state = src->state = TCPS_ESTABLISHED; 6952 if (state->sns[PF_SN_LIMIT] != NULL && 6953 pf_src_connlimit(state)) { 6954 REASON_SET(reason, PFRES_SRCLIMIT); 6955 return (PF_DROP); 6956 } 6957 } else if (src->state == TCPS_CLOSING && 6958 dst->state == TCPS_ESTABLISHED && 6959 dst->seqlo == 0) { 6960 /* 6961 * Handle the closing of half connections where we 6962 * don't see the full bidirectional FIN/ACK+ACK 6963 * handshake. 6964 */ 6965 pf_set_protostate(state, pdst, TCPS_CLOSING); 6966 } 6967 } 6968 if (tcp_get_flags(th) & TH_RST) 6969 pf_set_protostate(state, PF_PEER_BOTH, TCPS_TIME_WAIT); 6970 6971 /* update expire time */ 6972 state->expire = pf_get_uptime(); 6973 if (src->state >= TCPS_FIN_WAIT_2 && 6974 dst->state >= TCPS_FIN_WAIT_2) 6975 state->timeout = PFTM_TCP_CLOSED; 6976 else if (src->state >= TCPS_CLOSING && 6977 dst->state >= TCPS_CLOSING) 6978 state->timeout = PFTM_TCP_FIN_WAIT; 6979 else if (src->state < TCPS_ESTABLISHED || 6980 dst->state < TCPS_ESTABLISHED) 6981 state->timeout = PFTM_TCP_OPENING; 6982 else if (src->state >= TCPS_CLOSING || 6983 dst->state >= TCPS_CLOSING) 6984 state->timeout = PFTM_TCP_CLOSING; 6985 else 6986 state->timeout = PFTM_TCP_ESTABLISHED; 6987 6988 return (PF_PASS); 6989 } 6990 6991 static int 6992 pf_synproxy(struct pf_pdesc *pd, struct pf_kstate *state, u_short *reason) 6993 { 6994 struct pf_state_key *sk = state->key[pd->didx]; 6995 struct tcphdr *th = &pd->hdr.tcp; 6996 6997 if (state->src.state == PF_TCPS_PROXY_SRC) { 6998 if (pd->dir != state->direction) { 6999 REASON_SET(reason, PFRES_SYNPROXY); 7000 return (PF_SYNPROXY_DROP); 7001 } 7002 if (tcp_get_flags(th) & TH_SYN) { 7003 if (ntohl(th->th_seq) != state->src.seqlo) { 7004 REASON_SET(reason, PFRES_SYNPROXY); 7005 return (PF_DROP); 7006 } 7007 pf_send_tcp(state->rule, pd->af, pd->dst, 7008 pd->src, th->th_dport, th->th_sport, 7009 state->src.seqhi, ntohl(th->th_seq) + 1, 7010 TH_SYN|TH_ACK, 0, state->src.mss, 0, 7011 M_SKIP_FIREWALL, 0, 0, state->act.rtableid); 7012 REASON_SET(reason, PFRES_SYNPROXY); 7013 return (PF_SYNPROXY_DROP); 7014 } else if ((tcp_get_flags(th) & (TH_ACK|TH_RST|TH_FIN)) != TH_ACK || 7015 (ntohl(th->th_ack) != state->src.seqhi + 1) || 7016 (ntohl(th->th_seq) != state->src.seqlo + 1)) { 7017 REASON_SET(reason, PFRES_SYNPROXY); 7018 return (PF_DROP); 7019 } else if (state->sns[PF_SN_LIMIT] != NULL && 7020 pf_src_connlimit(state)) { 7021 REASON_SET(reason, PFRES_SRCLIMIT); 7022 return (PF_DROP); 7023 } else 7024 pf_set_protostate(state, PF_PEER_SRC, 7025 PF_TCPS_PROXY_DST); 7026 } 7027 if (state->src.state == PF_TCPS_PROXY_DST) { 7028 if (pd->dir == state->direction) { 7029 if (((tcp_get_flags(th) & (TH_SYN|TH_ACK)) != TH_ACK) || 7030 (ntohl(th->th_ack) != state->src.seqhi + 1) || 7031 (ntohl(th->th_seq) != state->src.seqlo + 1)) { 7032 REASON_SET(reason, PFRES_SYNPROXY); 7033 return (PF_DROP); 7034 } 7035 state->src.max_win = MAX(ntohs(th->th_win), 1); 7036 if (state->dst.seqhi == 1) 7037 state->dst.seqhi = arc4random(); 7038 pf_send_tcp(state->rule, pd->af, 7039 &sk->addr[pd->sidx], &sk->addr[pd->didx], 7040 sk->port[pd->sidx], sk->port[pd->didx], 7041 state->dst.seqhi, 0, TH_SYN, 0, 7042 state->src.mss, 0, 7043 state->orig_kif->pfik_ifp == V_loif ? M_LOOP : 0, 7044 state->tag, 0, state->act.rtableid); 7045 REASON_SET(reason, PFRES_SYNPROXY); 7046 return (PF_SYNPROXY_DROP); 7047 } else if (((tcp_get_flags(th) & (TH_SYN|TH_ACK)) != 7048 (TH_SYN|TH_ACK)) || 7049 (ntohl(th->th_ack) != state->dst.seqhi + 1)) { 7050 REASON_SET(reason, PFRES_SYNPROXY); 7051 return (PF_DROP); 7052 } else { 7053 state->dst.max_win = MAX(ntohs(th->th_win), 1); 7054 state->dst.seqlo = ntohl(th->th_seq); 7055 pf_send_tcp(state->rule, pd->af, pd->dst, 7056 pd->src, th->th_dport, th->th_sport, 7057 ntohl(th->th_ack), ntohl(th->th_seq) + 1, 7058 TH_ACK, state->src.max_win, 0, 0, 0, 7059 state->tag, 0, state->act.rtableid); 7060 pf_send_tcp(state->rule, pd->af, 7061 &sk->addr[pd->sidx], &sk->addr[pd->didx], 7062 sk->port[pd->sidx], sk->port[pd->didx], 7063 state->src.seqhi + 1, state->src.seqlo + 1, 7064 TH_ACK, state->dst.max_win, 0, 0, 7065 M_SKIP_FIREWALL, 0, 0, state->act.rtableid); 7066 state->src.seqdiff = state->dst.seqhi - 7067 state->src.seqlo; 7068 state->dst.seqdiff = state->src.seqhi - 7069 state->dst.seqlo; 7070 state->src.seqhi = state->src.seqlo + 7071 state->dst.max_win; 7072 state->dst.seqhi = state->dst.seqlo + 7073 state->src.max_win; 7074 state->src.wscale = state->dst.wscale = 0; 7075 pf_set_protostate(state, PF_PEER_BOTH, 7076 TCPS_ESTABLISHED); 7077 REASON_SET(reason, PFRES_SYNPROXY); 7078 return (PF_SYNPROXY_DROP); 7079 } 7080 } 7081 7082 return (PF_PASS); 7083 } 7084 7085 static int 7086 pf_test_state(struct pf_kstate **state, struct pf_pdesc *pd, u_short *reason) 7087 { 7088 struct pf_state_key_cmp key; 7089 int copyback = 0; 7090 struct pf_state_peer *src, *dst; 7091 uint8_t psrc, pdst; 7092 int action; 7093 7094 bzero(&key, sizeof(key)); 7095 key.af = pd->af; 7096 key.proto = pd->virtual_proto; 7097 pf_addrcpy(&key.addr[pd->sidx], pd->src, key.af); 7098 pf_addrcpy(&key.addr[pd->didx], pd->dst, key.af); 7099 key.port[pd->sidx] = pd->osport; 7100 key.port[pd->didx] = pd->odport; 7101 7102 action = pf_find_state(pd, &key, state); 7103 if (action != PF_MATCH) 7104 return (action); 7105 7106 action = PF_PASS; 7107 if (pd->dir == (*state)->direction) { 7108 if (PF_REVERSED_KEY(*state, pd->af)) { 7109 src = &(*state)->dst; 7110 dst = &(*state)->src; 7111 psrc = PF_PEER_DST; 7112 pdst = PF_PEER_SRC; 7113 } else { 7114 src = &(*state)->src; 7115 dst = &(*state)->dst; 7116 psrc = PF_PEER_SRC; 7117 pdst = PF_PEER_DST; 7118 } 7119 } else { 7120 if (PF_REVERSED_KEY(*state, pd->af)) { 7121 src = &(*state)->src; 7122 dst = &(*state)->dst; 7123 psrc = PF_PEER_SRC; 7124 pdst = PF_PEER_DST; 7125 } else { 7126 src = &(*state)->dst; 7127 dst = &(*state)->src; 7128 psrc = PF_PEER_DST; 7129 pdst = PF_PEER_SRC; 7130 } 7131 } 7132 7133 switch (pd->virtual_proto) { 7134 case IPPROTO_TCP: { 7135 struct tcphdr *th = &pd->hdr.tcp; 7136 7137 if ((action = pf_synproxy(pd, *state, reason)) != PF_PASS) 7138 return (action); 7139 if (((tcp_get_flags(th) & (TH_SYN | TH_ACK)) == TH_SYN) || 7140 ((th->th_flags & (TH_SYN | TH_ACK | TH_RST)) == TH_ACK && 7141 pf_syncookie_check(pd) && pd->dir == PF_IN)) { 7142 if ((*state)->src.state >= TCPS_FIN_WAIT_2 && 7143 (*state)->dst.state >= TCPS_FIN_WAIT_2) { 7144 if (V_pf_status.debug >= PF_DEBUG_MISC) { 7145 printf("pf: state reuse "); 7146 pf_print_state(*state); 7147 pf_print_flags(tcp_get_flags(th)); 7148 printf("\n"); 7149 } 7150 /* XXX make sure it's the same direction ?? */ 7151 pf_set_protostate(*state, PF_PEER_BOTH, TCPS_CLOSED); 7152 pf_remove_state(*state); 7153 *state = NULL; 7154 return (PF_DROP); 7155 } else if ((*state)->src.state >= TCPS_ESTABLISHED && 7156 (*state)->dst.state >= TCPS_ESTABLISHED) { 7157 /* 7158 * SYN matches existing state??? 7159 * Typically happens when sender boots up after 7160 * sudden panic. Certain protocols (NFSv3) are 7161 * always using same port numbers. Challenge 7162 * ACK enables all parties (firewall and peers) 7163 * to get in sync again. 7164 */ 7165 pf_send_challenge_ack(pd, *state, src, dst); 7166 return (PF_DROP); 7167 } 7168 } 7169 if ((*state)->state_flags & PFSTATE_SLOPPY) { 7170 if (pf_tcp_track_sloppy(*state, pd, reason, src, dst, 7171 psrc, pdst) == PF_DROP) 7172 return (PF_DROP); 7173 } else { 7174 int ret; 7175 7176 ret = pf_tcp_track_full(*state, pd, reason, 7177 ©back, src, dst, psrc, pdst); 7178 if (ret == PF_DROP) 7179 return (PF_DROP); 7180 } 7181 break; 7182 } 7183 case IPPROTO_UDP: 7184 /* update states */ 7185 if (src->state < PFUDPS_SINGLE) 7186 pf_set_protostate(*state, psrc, PFUDPS_SINGLE); 7187 if (dst->state == PFUDPS_SINGLE) 7188 pf_set_protostate(*state, pdst, PFUDPS_MULTIPLE); 7189 7190 /* update expire time */ 7191 (*state)->expire = pf_get_uptime(); 7192 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 7193 (*state)->timeout = PFTM_UDP_MULTIPLE; 7194 else 7195 (*state)->timeout = PFTM_UDP_SINGLE; 7196 break; 7197 case IPPROTO_SCTP: 7198 if ((src->state >= SCTP_SHUTDOWN_SENT || src->state == SCTP_CLOSED) && 7199 (dst->state >= SCTP_SHUTDOWN_SENT || dst->state == SCTP_CLOSED) && 7200 pd->sctp_flags & PFDESC_SCTP_INIT) { 7201 pf_set_protostate(*state, PF_PEER_BOTH, SCTP_CLOSED); 7202 pf_remove_state(*state); 7203 *state = NULL; 7204 return (PF_DROP); 7205 } 7206 7207 if (pf_sctp_track(*state, pd, reason) != PF_PASS) 7208 return (PF_DROP); 7209 7210 /* Track state. */ 7211 if (pd->sctp_flags & PFDESC_SCTP_INIT) { 7212 if (src->state < SCTP_COOKIE_WAIT) { 7213 pf_set_protostate(*state, psrc, SCTP_COOKIE_WAIT); 7214 (*state)->timeout = PFTM_SCTP_OPENING; 7215 } 7216 } 7217 if (pd->sctp_flags & PFDESC_SCTP_INIT_ACK) { 7218 MPASS(dst->scrub != NULL); 7219 if (dst->scrub->pfss_v_tag == 0) 7220 dst->scrub->pfss_v_tag = pd->sctp_initiate_tag; 7221 } 7222 7223 /* 7224 * Bind to the correct interface if we're if-bound. For multihomed 7225 * extra associations we don't know which interface that will be until 7226 * here, so we've inserted the state on V_pf_all. Fix that now. 7227 */ 7228 if ((*state)->kif == V_pfi_all && 7229 (*state)->rule->rule_flag & PFRULE_IFBOUND) 7230 (*state)->kif = pd->kif; 7231 7232 if (pd->sctp_flags & (PFDESC_SCTP_COOKIE | PFDESC_SCTP_HEARTBEAT_ACK)) { 7233 if (src->state < SCTP_ESTABLISHED) { 7234 pf_set_protostate(*state, psrc, SCTP_ESTABLISHED); 7235 (*state)->timeout = PFTM_SCTP_ESTABLISHED; 7236 } 7237 } 7238 if (pd->sctp_flags & (PFDESC_SCTP_SHUTDOWN | 7239 PFDESC_SCTP_SHUTDOWN_COMPLETE)) { 7240 if (src->state < SCTP_SHUTDOWN_PENDING) { 7241 pf_set_protostate(*state, psrc, SCTP_SHUTDOWN_PENDING); 7242 (*state)->timeout = PFTM_SCTP_CLOSING; 7243 } 7244 } 7245 if (pd->sctp_flags & (PFDESC_SCTP_SHUTDOWN_COMPLETE | PFDESC_SCTP_ABORT)) { 7246 pf_set_protostate(*state, psrc, SCTP_CLOSED); 7247 (*state)->timeout = PFTM_SCTP_CLOSED; 7248 } 7249 7250 (*state)->expire = pf_get_uptime(); 7251 break; 7252 default: 7253 /* update states */ 7254 if (src->state < PFOTHERS_SINGLE) 7255 pf_set_protostate(*state, psrc, PFOTHERS_SINGLE); 7256 if (dst->state == PFOTHERS_SINGLE) 7257 pf_set_protostate(*state, pdst, PFOTHERS_MULTIPLE); 7258 7259 /* update expire time */ 7260 (*state)->expire = pf_get_uptime(); 7261 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 7262 (*state)->timeout = PFTM_OTHER_MULTIPLE; 7263 else 7264 (*state)->timeout = PFTM_OTHER_SINGLE; 7265 break; 7266 } 7267 7268 /* translate source/destination address, if necessary */ 7269 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 7270 struct pf_state_key *nk; 7271 int afto, sidx, didx; 7272 7273 if (PF_REVERSED_KEY(*state, pd->af)) 7274 nk = (*state)->key[pd->sidx]; 7275 else 7276 nk = (*state)->key[pd->didx]; 7277 7278 afto = pd->af != nk->af; 7279 7280 if (afto && (*state)->direction == PF_IN) { 7281 sidx = pd->didx; 7282 didx = pd->sidx; 7283 } else { 7284 sidx = pd->sidx; 7285 didx = pd->didx; 7286 } 7287 7288 if (afto) { 7289 pf_addrcpy(&pd->nsaddr, &nk->addr[sidx], nk->af); 7290 pf_addrcpy(&pd->ndaddr, &nk->addr[didx], nk->af); 7291 pd->naf = nk->af; 7292 action = PF_AFRT; 7293 } 7294 7295 if (afto || PF_ANEQ(pd->src, &nk->addr[sidx], pd->af) || 7296 nk->port[sidx] != pd->osport) 7297 pf_change_ap(pd, pd->src, pd->sport, 7298 &nk->addr[sidx], nk->port[sidx]); 7299 7300 if (afto || PF_ANEQ(pd->dst, &nk->addr[didx], pd->af) || 7301 nk->port[didx] != pd->odport) 7302 pf_change_ap(pd, pd->dst, pd->dport, 7303 &nk->addr[didx], nk->port[didx]); 7304 7305 copyback = 1; 7306 } 7307 7308 if (copyback && pd->hdrlen > 0) 7309 m_copyback(pd->m, pd->off, pd->hdrlen, pd->hdr.any); 7310 7311 return (action); 7312 } 7313 7314 static int 7315 pf_sctp_track(struct pf_kstate *state, struct pf_pdesc *pd, 7316 u_short *reason) 7317 { 7318 struct pf_state_peer *src; 7319 if (pd->dir == state->direction) { 7320 if (PF_REVERSED_KEY(state, pd->af)) 7321 src = &state->dst; 7322 else 7323 src = &state->src; 7324 } else { 7325 if (PF_REVERSED_KEY(state, pd->af)) 7326 src = &state->src; 7327 else 7328 src = &state->dst; 7329 } 7330 7331 if (src->scrub != NULL) { 7332 if (src->scrub->pfss_v_tag == 0) 7333 src->scrub->pfss_v_tag = pd->hdr.sctp.v_tag; 7334 else if (src->scrub->pfss_v_tag != pd->hdr.sctp.v_tag) 7335 return (PF_DROP); 7336 } 7337 7338 return (PF_PASS); 7339 } 7340 7341 static void 7342 pf_sctp_multihome_detach_addr(const struct pf_kstate *s) 7343 { 7344 struct pf_sctp_endpoint key; 7345 struct pf_sctp_endpoint *ep; 7346 struct pf_state_key *sks = s->key[PF_SK_STACK]; 7347 struct pf_sctp_source *i, *tmp; 7348 7349 if (sks == NULL || sks->proto != IPPROTO_SCTP || s->dst.scrub == NULL) 7350 return; 7351 7352 PF_SCTP_ENDPOINTS_LOCK(); 7353 7354 key.v_tag = s->dst.scrub->pfss_v_tag; 7355 ep = RB_FIND(pf_sctp_endpoints, &V_pf_sctp_endpoints, &key); 7356 if (ep != NULL) { 7357 TAILQ_FOREACH_SAFE(i, &ep->sources, entry, tmp) { 7358 if (pf_addr_cmp(&i->addr, 7359 &s->key[PF_SK_WIRE]->addr[s->direction == PF_OUT], 7360 s->key[PF_SK_WIRE]->af) == 0) { 7361 SDT_PROBE3(pf, sctp, multihome, remove, 7362 key.v_tag, s, i); 7363 TAILQ_REMOVE(&ep->sources, i, entry); 7364 free(i, M_PFTEMP); 7365 break; 7366 } 7367 } 7368 7369 if (TAILQ_EMPTY(&ep->sources)) { 7370 RB_REMOVE(pf_sctp_endpoints, &V_pf_sctp_endpoints, ep); 7371 free(ep, M_PFTEMP); 7372 } 7373 } 7374 7375 /* Other direction. */ 7376 key.v_tag = s->src.scrub->pfss_v_tag; 7377 ep = RB_FIND(pf_sctp_endpoints, &V_pf_sctp_endpoints, &key); 7378 if (ep != NULL) { 7379 TAILQ_FOREACH_SAFE(i, &ep->sources, entry, tmp) { 7380 if (pf_addr_cmp(&i->addr, 7381 &s->key[PF_SK_WIRE]->addr[s->direction == PF_IN], 7382 s->key[PF_SK_WIRE]->af) == 0) { 7383 SDT_PROBE3(pf, sctp, multihome, remove, 7384 key.v_tag, s, i); 7385 TAILQ_REMOVE(&ep->sources, i, entry); 7386 free(i, M_PFTEMP); 7387 break; 7388 } 7389 } 7390 7391 if (TAILQ_EMPTY(&ep->sources)) { 7392 RB_REMOVE(pf_sctp_endpoints, &V_pf_sctp_endpoints, ep); 7393 free(ep, M_PFTEMP); 7394 } 7395 } 7396 7397 PF_SCTP_ENDPOINTS_UNLOCK(); 7398 } 7399 7400 static void 7401 pf_sctp_multihome_add_addr(struct pf_pdesc *pd, struct pf_addr *a, uint32_t v_tag) 7402 { 7403 struct pf_sctp_endpoint key = { 7404 .v_tag = v_tag, 7405 }; 7406 struct pf_sctp_source *i; 7407 struct pf_sctp_endpoint *ep; 7408 int count; 7409 7410 PF_SCTP_ENDPOINTS_LOCK(); 7411 7412 ep = RB_FIND(pf_sctp_endpoints, &V_pf_sctp_endpoints, &key); 7413 if (ep == NULL) { 7414 ep = malloc(sizeof(struct pf_sctp_endpoint), 7415 M_PFTEMP, M_NOWAIT); 7416 if (ep == NULL) { 7417 PF_SCTP_ENDPOINTS_UNLOCK(); 7418 return; 7419 } 7420 7421 ep->v_tag = v_tag; 7422 TAILQ_INIT(&ep->sources); 7423 RB_INSERT(pf_sctp_endpoints, &V_pf_sctp_endpoints, ep); 7424 } 7425 7426 /* Avoid inserting duplicates. */ 7427 count = 0; 7428 TAILQ_FOREACH(i, &ep->sources, entry) { 7429 count++; 7430 if (pf_addr_cmp(&i->addr, a, pd->af) == 0) { 7431 PF_SCTP_ENDPOINTS_UNLOCK(); 7432 return; 7433 } 7434 } 7435 7436 /* Limit the number of addresses per endpoint. */ 7437 if (count >= PF_SCTP_MAX_ENDPOINTS) { 7438 PF_SCTP_ENDPOINTS_UNLOCK(); 7439 return; 7440 } 7441 7442 i = malloc(sizeof(*i), M_PFTEMP, M_NOWAIT); 7443 if (i == NULL) { 7444 PF_SCTP_ENDPOINTS_UNLOCK(); 7445 return; 7446 } 7447 7448 i->af = pd->af; 7449 memcpy(&i->addr, a, sizeof(*a)); 7450 TAILQ_INSERT_TAIL(&ep->sources, i, entry); 7451 SDT_PROBE2(pf, sctp, multihome, add, v_tag, i); 7452 7453 PF_SCTP_ENDPOINTS_UNLOCK(); 7454 } 7455 7456 static void 7457 pf_sctp_multihome_delayed(struct pf_pdesc *pd, struct pfi_kkif *kif, 7458 struct pf_kstate *s, int action) 7459 { 7460 struct pf_sctp_multihome_job *j, *tmp; 7461 struct pf_sctp_source *i; 7462 int ret; 7463 struct pf_kstate *sm = NULL; 7464 struct pf_krule *ra = NULL; 7465 struct pf_krule *r = &V_pf_default_rule; 7466 struct pf_kruleset *rs = NULL; 7467 u_short reason; 7468 bool do_extra = true; 7469 7470 PF_RULES_RLOCK_TRACKER; 7471 7472 again: 7473 TAILQ_FOREACH_SAFE(j, &pd->sctp_multihome_jobs, next, tmp) { 7474 if (s == NULL || action != PF_PASS) 7475 goto free; 7476 7477 /* Confirm we don't recurse here. */ 7478 MPASS(! (pd->sctp_flags & PFDESC_SCTP_ADD_IP)); 7479 7480 switch (j->op) { 7481 case SCTP_ADD_IP_ADDRESS: { 7482 uint32_t v_tag = pd->sctp_initiate_tag; 7483 7484 if (v_tag == 0) { 7485 if (s->direction == pd->dir) 7486 v_tag = s->src.scrub->pfss_v_tag; 7487 else 7488 v_tag = s->dst.scrub->pfss_v_tag; 7489 } 7490 7491 /* 7492 * Avoid duplicating states. We'll already have 7493 * created a state based on the source address of 7494 * the packet, but SCTP endpoints may also list this 7495 * address again in the INIT(_ACK) parameters. 7496 */ 7497 if (pf_addr_cmp(&j->src, pd->src, pd->af) == 0) { 7498 break; 7499 } 7500 7501 j->pd.sctp_flags |= PFDESC_SCTP_ADD_IP; 7502 PF_RULES_RLOCK(); 7503 sm = NULL; 7504 if (s->rule->rule_flag & PFRULE_ALLOW_RELATED) { 7505 j->pd.related_rule = s->rule; 7506 } 7507 ret = pf_test_rule(&r, &sm, 7508 &j->pd, &ra, &rs, &reason, NULL); 7509 PF_RULES_RUNLOCK(); 7510 SDT_PROBE4(pf, sctp, multihome, test, kif, r, j->pd.m, ret); 7511 if (ret != PF_DROP && sm != NULL) { 7512 /* Inherit v_tag values. */ 7513 if (sm->direction == s->direction) { 7514 sm->src.scrub->pfss_v_tag = s->src.scrub->pfss_v_tag; 7515 sm->dst.scrub->pfss_v_tag = s->dst.scrub->pfss_v_tag; 7516 } else { 7517 sm->src.scrub->pfss_v_tag = s->dst.scrub->pfss_v_tag; 7518 sm->dst.scrub->pfss_v_tag = s->src.scrub->pfss_v_tag; 7519 } 7520 PF_STATE_UNLOCK(sm); 7521 } else { 7522 /* If we try duplicate inserts? */ 7523 break; 7524 } 7525 7526 /* Only add the address if we've actually allowed the state. */ 7527 pf_sctp_multihome_add_addr(pd, &j->src, v_tag); 7528 7529 if (! do_extra) { 7530 break; 7531 } 7532 /* 7533 * We need to do this for each of our source addresses. 7534 * Find those based on the verification tag. 7535 */ 7536 struct pf_sctp_endpoint key = { 7537 .v_tag = pd->hdr.sctp.v_tag, 7538 }; 7539 struct pf_sctp_endpoint *ep; 7540 7541 PF_SCTP_ENDPOINTS_LOCK(); 7542 ep = RB_FIND(pf_sctp_endpoints, &V_pf_sctp_endpoints, &key); 7543 if (ep == NULL) { 7544 PF_SCTP_ENDPOINTS_UNLOCK(); 7545 break; 7546 } 7547 MPASS(ep != NULL); 7548 7549 TAILQ_FOREACH(i, &ep->sources, entry) { 7550 struct pf_sctp_multihome_job *nj; 7551 7552 /* SCTP can intermingle IPv4 and IPv6. */ 7553 if (i->af != pd->af) 7554 continue; 7555 7556 nj = malloc(sizeof(*nj), M_PFTEMP, M_NOWAIT | M_ZERO); 7557 if (! nj) { 7558 continue; 7559 } 7560 memcpy(&nj->pd, &j->pd, sizeof(j->pd)); 7561 memcpy(&nj->src, &j->src, sizeof(nj->src)); 7562 nj->pd.src = &nj->src; 7563 // New destination address! 7564 memcpy(&nj->dst, &i->addr, sizeof(nj->dst)); 7565 nj->pd.dst = &nj->dst; 7566 nj->pd.m = j->pd.m; 7567 nj->op = j->op; 7568 7569 TAILQ_INSERT_TAIL(&pd->sctp_multihome_jobs, nj, next); 7570 } 7571 PF_SCTP_ENDPOINTS_UNLOCK(); 7572 7573 break; 7574 } 7575 case SCTP_DEL_IP_ADDRESS: { 7576 struct pf_state_key_cmp key; 7577 uint8_t psrc; 7578 int action; 7579 7580 bzero(&key, sizeof(key)); 7581 key.af = j->pd.af; 7582 key.proto = IPPROTO_SCTP; 7583 if (j->pd.dir == PF_IN) { /* wire side, straight */ 7584 pf_addrcpy(&key.addr[0], j->pd.src, key.af); 7585 pf_addrcpy(&key.addr[1], j->pd.dst, key.af); 7586 key.port[0] = j->pd.hdr.sctp.src_port; 7587 key.port[1] = j->pd.hdr.sctp.dest_port; 7588 } else { /* stack side, reverse */ 7589 pf_addrcpy(&key.addr[1], j->pd.src, key.af); 7590 pf_addrcpy(&key.addr[0], j->pd.dst, key.af); 7591 key.port[1] = j->pd.hdr.sctp.src_port; 7592 key.port[0] = j->pd.hdr.sctp.dest_port; 7593 } 7594 7595 action = pf_find_state(&j->pd, &key, &sm); 7596 if (action == PF_MATCH) { 7597 PF_STATE_LOCK_ASSERT(sm); 7598 if (j->pd.dir == sm->direction) { 7599 psrc = PF_PEER_SRC; 7600 } else { 7601 psrc = PF_PEER_DST; 7602 } 7603 pf_set_protostate(sm, psrc, SCTP_SHUTDOWN_PENDING); 7604 sm->timeout = PFTM_SCTP_CLOSING; 7605 PF_STATE_UNLOCK(sm); 7606 } 7607 break; 7608 default: 7609 panic("Unknown op %#x", j->op); 7610 } 7611 } 7612 7613 free: 7614 TAILQ_REMOVE(&pd->sctp_multihome_jobs, j, next); 7615 free(j, M_PFTEMP); 7616 } 7617 7618 /* We may have inserted extra work while processing the list. */ 7619 if (! TAILQ_EMPTY(&pd->sctp_multihome_jobs)) { 7620 do_extra = false; 7621 goto again; 7622 } 7623 } 7624 7625 static int 7626 pf_multihome_scan(int start, int len, struct pf_pdesc *pd, int op) 7627 { 7628 int off = 0; 7629 struct pf_sctp_multihome_job *job; 7630 7631 SDT_PROBE4(pf, sctp, multihome_scan, entry, start, len, pd, op); 7632 7633 while (off < len) { 7634 struct sctp_paramhdr h; 7635 7636 if (!pf_pull_hdr(pd->m, start + off, &h, sizeof(h), NULL, NULL, 7637 pd->af)) 7638 return (PF_DROP); 7639 7640 /* Parameters are at least 4 bytes. */ 7641 if (ntohs(h.param_length) < 4) 7642 return (PF_DROP); 7643 7644 SDT_PROBE2(pf, sctp, multihome_scan, param, ntohs(h.param_type), 7645 ntohs(h.param_length)); 7646 7647 switch (ntohs(h.param_type)) { 7648 case SCTP_IPV4_ADDRESS: { 7649 struct in_addr t; 7650 7651 if (ntohs(h.param_length) != 7652 (sizeof(struct sctp_paramhdr) + sizeof(t))) 7653 return (PF_DROP); 7654 7655 if (!pf_pull_hdr(pd->m, start + off + sizeof(h), &t, sizeof(t), 7656 NULL, NULL, pd->af)) 7657 return (PF_DROP); 7658 7659 if (in_nullhost(t)) 7660 t.s_addr = pd->src->v4.s_addr; 7661 7662 /* 7663 * We hold the state lock (idhash) here, which means 7664 * that we can't acquire the keyhash, or we'll get a 7665 * LOR (and potentially double-lock things too). We also 7666 * can't release the state lock here, so instead we'll 7667 * enqueue this for async handling. 7668 * There's a relatively small race here, in that a 7669 * packet using the new addresses could arrive already, 7670 * but that's just though luck for it. 7671 */ 7672 job = malloc(sizeof(*job), M_PFTEMP, M_NOWAIT | M_ZERO); 7673 if (! job) 7674 return (PF_DROP); 7675 7676 SDT_PROBE2(pf, sctp, multihome_scan, ipv4, &t, op); 7677 7678 memcpy(&job->pd, pd, sizeof(*pd)); 7679 7680 // New source address! 7681 memcpy(&job->src, &t, sizeof(t)); 7682 job->pd.src = &job->src; 7683 memcpy(&job->dst, pd->dst, sizeof(job->dst)); 7684 job->pd.dst = &job->dst; 7685 job->pd.m = pd->m; 7686 job->op = op; 7687 7688 TAILQ_INSERT_TAIL(&pd->sctp_multihome_jobs, job, next); 7689 break; 7690 } 7691 #ifdef INET6 7692 case SCTP_IPV6_ADDRESS: { 7693 struct in6_addr t; 7694 7695 if (ntohs(h.param_length) != 7696 (sizeof(struct sctp_paramhdr) + sizeof(t))) 7697 return (PF_DROP); 7698 7699 if (!pf_pull_hdr(pd->m, start + off + sizeof(h), &t, sizeof(t), 7700 NULL, NULL, pd->af)) 7701 return (PF_DROP); 7702 if (memcmp(&t, &pd->src->v6, sizeof(t)) == 0) 7703 break; 7704 if (memcmp(&t, &in6addr_any, sizeof(t)) == 0) 7705 memcpy(&t, &pd->src->v6, sizeof(t)); 7706 7707 job = malloc(sizeof(*job), M_PFTEMP, M_NOWAIT | M_ZERO); 7708 if (! job) 7709 return (PF_DROP); 7710 7711 SDT_PROBE2(pf, sctp, multihome_scan, ipv6, &t, op); 7712 7713 memcpy(&job->pd, pd, sizeof(*pd)); 7714 memcpy(&job->src, &t, sizeof(t)); 7715 job->pd.src = &job->src; 7716 memcpy(&job->dst, pd->dst, sizeof(job->dst)); 7717 job->pd.dst = &job->dst; 7718 job->pd.m = pd->m; 7719 job->op = op; 7720 7721 TAILQ_INSERT_TAIL(&pd->sctp_multihome_jobs, job, next); 7722 break; 7723 } 7724 #endif /* INET6 */ 7725 case SCTP_ADD_IP_ADDRESS: { 7726 int ret; 7727 struct sctp_asconf_paramhdr ah; 7728 7729 if (!pf_pull_hdr(pd->m, start + off, &ah, sizeof(ah), 7730 NULL, NULL, pd->af)) 7731 return (PF_DROP); 7732 7733 ret = pf_multihome_scan(start + off + sizeof(ah), 7734 ntohs(ah.ph.param_length) - sizeof(ah), pd, 7735 SCTP_ADD_IP_ADDRESS); 7736 if (ret != PF_PASS) 7737 return (ret); 7738 break; 7739 } 7740 case SCTP_DEL_IP_ADDRESS: { 7741 int ret; 7742 struct sctp_asconf_paramhdr ah; 7743 7744 if (!pf_pull_hdr(pd->m, start + off, &ah, sizeof(ah), 7745 NULL, NULL, pd->af)) 7746 return (PF_DROP); 7747 ret = pf_multihome_scan(start + off + sizeof(ah), 7748 ntohs(ah.ph.param_length) - sizeof(ah), pd, 7749 SCTP_DEL_IP_ADDRESS); 7750 if (ret != PF_PASS) 7751 return (ret); 7752 break; 7753 } 7754 default: 7755 break; 7756 } 7757 7758 off += roundup(ntohs(h.param_length), 4); 7759 } 7760 7761 return (PF_PASS); 7762 } 7763 7764 int 7765 pf_multihome_scan_init(int start, int len, struct pf_pdesc *pd) 7766 { 7767 start += sizeof(struct sctp_init_chunk); 7768 len -= sizeof(struct sctp_init_chunk); 7769 7770 return (pf_multihome_scan(start, len, pd, SCTP_ADD_IP_ADDRESS)); 7771 } 7772 7773 int 7774 pf_multihome_scan_asconf(int start, int len, struct pf_pdesc *pd) 7775 { 7776 start += sizeof(struct sctp_asconf_chunk); 7777 len -= sizeof(struct sctp_asconf_chunk); 7778 7779 return (pf_multihome_scan(start, len, pd, SCTP_ADD_IP_ADDRESS)); 7780 } 7781 7782 int 7783 pf_icmp_state_lookup(struct pf_state_key_cmp *key, struct pf_pdesc *pd, 7784 struct pf_kstate **state, u_int16_t icmpid, u_int16_t type, int icmp_dir, 7785 int *iidx, int multi, int inner) 7786 { 7787 int action, direction = pd->dir; 7788 7789 key->af = pd->af; 7790 key->proto = pd->proto; 7791 if (icmp_dir == PF_IN) { 7792 *iidx = pd->sidx; 7793 key->port[pd->sidx] = icmpid; 7794 key->port[pd->didx] = type; 7795 } else { 7796 *iidx = pd->didx; 7797 key->port[pd->sidx] = type; 7798 key->port[pd->didx] = icmpid; 7799 } 7800 if (pf_state_key_addr_setup(pd, key, multi)) 7801 return (PF_DROP); 7802 7803 action = pf_find_state(pd, key, state); 7804 if (action != PF_MATCH) 7805 return (action); 7806 7807 if ((*state)->state_flags & PFSTATE_SLOPPY) 7808 return (-1); 7809 7810 /* Is this ICMP message flowing in right direction? */ 7811 if ((*state)->key[PF_SK_WIRE]->af != (*state)->key[PF_SK_STACK]->af) 7812 direction = (pd->af == (*state)->key[PF_SK_WIRE]->af) ? 7813 PF_IN : PF_OUT; 7814 else 7815 direction = (*state)->direction; 7816 if ((*state)->rule->type && 7817 (((!inner && direction == pd->dir) || 7818 (inner && direction != pd->dir)) ? 7819 PF_IN : PF_OUT) != icmp_dir) { 7820 if (V_pf_status.debug >= PF_DEBUG_MISC) { 7821 printf("pf: icmp type %d in wrong direction (%d): ", 7822 ntohs(type), icmp_dir); 7823 pf_print_state(*state); 7824 printf("\n"); 7825 } 7826 PF_STATE_UNLOCK(*state); 7827 *state = NULL; 7828 return (PF_DROP); 7829 } 7830 return (-1); 7831 } 7832 7833 static int 7834 pf_test_state_icmp(struct pf_kstate **state, struct pf_pdesc *pd, 7835 u_short *reason) 7836 { 7837 struct pf_addr *saddr = pd->src, *daddr = pd->dst; 7838 u_int16_t *icmpsum, virtual_id, virtual_type; 7839 u_int8_t icmptype, icmpcode; 7840 int icmp_dir, iidx, ret; 7841 struct pf_state_key_cmp key; 7842 #ifdef INET 7843 u_int16_t icmpid; 7844 #endif /* INET*/ 7845 7846 MPASS(*state == NULL); 7847 7848 bzero(&key, sizeof(key)); 7849 switch (pd->proto) { 7850 #ifdef INET 7851 case IPPROTO_ICMP: 7852 icmptype = pd->hdr.icmp.icmp_type; 7853 icmpcode = pd->hdr.icmp.icmp_code; 7854 icmpid = pd->hdr.icmp.icmp_id; 7855 icmpsum = &pd->hdr.icmp.icmp_cksum; 7856 break; 7857 #endif /* INET */ 7858 #ifdef INET6 7859 case IPPROTO_ICMPV6: 7860 icmptype = pd->hdr.icmp6.icmp6_type; 7861 icmpcode = pd->hdr.icmp6.icmp6_code; 7862 #ifdef INET 7863 icmpid = pd->hdr.icmp6.icmp6_id; 7864 #endif /* INET */ 7865 icmpsum = &pd->hdr.icmp6.icmp6_cksum; 7866 break; 7867 #endif /* INET6 */ 7868 default: 7869 panic("unhandled proto %d", pd->proto); 7870 } 7871 7872 if (pf_icmp_mapping(pd, icmptype, &icmp_dir, &virtual_id, 7873 &virtual_type) == 0) { 7874 /* 7875 * ICMP query/reply message not related to a TCP/UDP/SCTP 7876 * packet. Search for an ICMP state. 7877 */ 7878 ret = pf_icmp_state_lookup(&key, pd, state, virtual_id, 7879 virtual_type, icmp_dir, &iidx, 0, 0); 7880 /* IPv6? try matching a multicast address */ 7881 if (ret == PF_DROP && pd->af == AF_INET6 && icmp_dir == PF_OUT) { 7882 MPASS(*state == NULL); 7883 ret = pf_icmp_state_lookup(&key, pd, state, 7884 virtual_id, virtual_type, 7885 icmp_dir, &iidx, 1, 0); 7886 } 7887 if (ret >= 0) { 7888 MPASS(*state == NULL); 7889 return (ret); 7890 } 7891 7892 (*state)->expire = pf_get_uptime(); 7893 (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 7894 7895 /* translate source/destination address, if necessary */ 7896 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 7897 struct pf_state_key *nk; 7898 int afto, sidx, didx; 7899 7900 if (PF_REVERSED_KEY(*state, pd->af)) 7901 nk = (*state)->key[pd->sidx]; 7902 else 7903 nk = (*state)->key[pd->didx]; 7904 7905 afto = pd->af != nk->af; 7906 7907 if (afto && (*state)->direction == PF_IN) { 7908 sidx = pd->didx; 7909 didx = pd->sidx; 7910 iidx = !iidx; 7911 } else { 7912 sidx = pd->sidx; 7913 didx = pd->didx; 7914 } 7915 7916 switch (pd->af) { 7917 #ifdef INET 7918 case AF_INET: 7919 #ifdef INET6 7920 if (afto) { 7921 if (pf_translate_icmp_af(AF_INET6, 7922 &pd->hdr.icmp)) 7923 return (PF_DROP); 7924 pd->proto = IPPROTO_ICMPV6; 7925 } 7926 #endif /* INET6 */ 7927 if (!afto && 7928 PF_ANEQ(pd->src, &nk->addr[sidx], AF_INET)) 7929 pf_change_a(&saddr->v4.s_addr, 7930 pd->ip_sum, 7931 nk->addr[sidx].v4.s_addr, 7932 0); 7933 7934 if (!afto && PF_ANEQ(pd->dst, 7935 &nk->addr[didx], AF_INET)) 7936 pf_change_a(&daddr->v4.s_addr, 7937 pd->ip_sum, 7938 nk->addr[didx].v4.s_addr, 0); 7939 7940 if (nk->port[iidx] != 7941 pd->hdr.icmp.icmp_id) { 7942 pd->hdr.icmp.icmp_cksum = 7943 pf_cksum_fixup( 7944 pd->hdr.icmp.icmp_cksum, icmpid, 7945 nk->port[iidx], 0); 7946 pd->hdr.icmp.icmp_id = 7947 nk->port[iidx]; 7948 } 7949 7950 m_copyback(pd->m, pd->off, ICMP_MINLEN, 7951 (caddr_t )&pd->hdr.icmp); 7952 break; 7953 #endif /* INET */ 7954 #ifdef INET6 7955 case AF_INET6: 7956 #ifdef INET 7957 if (afto) { 7958 if (pf_translate_icmp_af(AF_INET, 7959 &pd->hdr.icmp6)) 7960 return (PF_DROP); 7961 pd->proto = IPPROTO_ICMP; 7962 } 7963 #endif /* INET */ 7964 if (!afto && 7965 PF_ANEQ(pd->src, &nk->addr[sidx], AF_INET6)) 7966 pf_change_a6(saddr, 7967 &pd->hdr.icmp6.icmp6_cksum, 7968 &nk->addr[sidx], 0); 7969 7970 if (!afto && PF_ANEQ(pd->dst, 7971 &nk->addr[didx], AF_INET6)) 7972 pf_change_a6(daddr, 7973 &pd->hdr.icmp6.icmp6_cksum, 7974 &nk->addr[didx], 0); 7975 7976 if (nk->port[iidx] != pd->hdr.icmp6.icmp6_id) 7977 pd->hdr.icmp6.icmp6_id = 7978 nk->port[iidx]; 7979 7980 m_copyback(pd->m, pd->off, sizeof(struct icmp6_hdr), 7981 (caddr_t )&pd->hdr.icmp6); 7982 break; 7983 #endif /* INET6 */ 7984 } 7985 if (afto) { 7986 pf_addrcpy(&pd->nsaddr, &nk->addr[sidx], 7987 nk->af); 7988 pf_addrcpy(&pd->ndaddr, &nk->addr[didx], 7989 nk->af); 7990 pd->naf = nk->af; 7991 return (PF_AFRT); 7992 } 7993 } 7994 return (PF_PASS); 7995 7996 } else { 7997 /* 7998 * ICMP error message in response to a TCP/UDP packet. 7999 * Extract the inner TCP/UDP header and search for that state. 8000 */ 8001 8002 struct pf_pdesc pd2; 8003 bzero(&pd2, sizeof pd2); 8004 #ifdef INET 8005 struct ip h2; 8006 #endif /* INET */ 8007 #ifdef INET6 8008 struct ip6_hdr h2_6; 8009 #endif /* INET6 */ 8010 int ipoff2 = 0; 8011 8012 pd2.af = pd->af; 8013 pd2.dir = pd->dir; 8014 /* Payload packet is from the opposite direction. */ 8015 pd2.sidx = (pd->dir == PF_IN) ? 1 : 0; 8016 pd2.didx = (pd->dir == PF_IN) ? 0 : 1; 8017 pd2.m = pd->m; 8018 pd2.pf_mtag = pd->pf_mtag; 8019 pd2.kif = pd->kif; 8020 switch (pd->af) { 8021 #ifdef INET 8022 case AF_INET: 8023 /* offset of h2 in mbuf chain */ 8024 ipoff2 = pd->off + ICMP_MINLEN; 8025 8026 if (!pf_pull_hdr(pd->m, ipoff2, &h2, sizeof(h2), 8027 NULL, reason, pd2.af)) { 8028 DPFPRINTF(PF_DEBUG_MISC, 8029 "pf: ICMP error message too short " 8030 "(ip)"); 8031 return (PF_DROP); 8032 } 8033 /* 8034 * ICMP error messages don't refer to non-first 8035 * fragments 8036 */ 8037 if (h2.ip_off & htons(IP_OFFMASK)) { 8038 REASON_SET(reason, PFRES_FRAG); 8039 return (PF_DROP); 8040 } 8041 8042 /* offset of protocol header that follows h2 */ 8043 pd2.off = ipoff2; 8044 if (pf_walk_header(&pd2, &h2, reason) != PF_PASS) 8045 return (PF_DROP); 8046 8047 pd2.tot_len = ntohs(h2.ip_len); 8048 pd2.src = (struct pf_addr *)&h2.ip_src; 8049 pd2.dst = (struct pf_addr *)&h2.ip_dst; 8050 pd2.ip_sum = &h2.ip_sum; 8051 break; 8052 #endif /* INET */ 8053 #ifdef INET6 8054 case AF_INET6: 8055 ipoff2 = pd->off + sizeof(struct icmp6_hdr); 8056 8057 if (!pf_pull_hdr(pd->m, ipoff2, &h2_6, sizeof(h2_6), 8058 NULL, reason, pd2.af)) { 8059 DPFPRINTF(PF_DEBUG_MISC, 8060 "pf: ICMP error message too short " 8061 "(ip6)"); 8062 return (PF_DROP); 8063 } 8064 pd2.off = ipoff2; 8065 if (pf_walk_header6(&pd2, &h2_6, reason) != PF_PASS) 8066 return (PF_DROP); 8067 8068 pd2.tot_len = ntohs(h2_6.ip6_plen) + 8069 sizeof(struct ip6_hdr); 8070 pd2.src = (struct pf_addr *)&h2_6.ip6_src; 8071 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 8072 pd2.ip_sum = NULL; 8073 break; 8074 #endif /* INET6 */ 8075 default: 8076 unhandled_af(pd->af); 8077 } 8078 8079 if (PF_ANEQ(pd->dst, pd2.src, pd->af)) { 8080 if (V_pf_status.debug >= PF_DEBUG_MISC) { 8081 printf("pf: BAD ICMP %d:%d outer dst: ", 8082 icmptype, icmpcode); 8083 pf_print_host(pd->src, 0, pd->af); 8084 printf(" -> "); 8085 pf_print_host(pd->dst, 0, pd->af); 8086 printf(" inner src: "); 8087 pf_print_host(pd2.src, 0, pd2.af); 8088 printf(" -> "); 8089 pf_print_host(pd2.dst, 0, pd2.af); 8090 printf("\n"); 8091 } 8092 REASON_SET(reason, PFRES_BADSTATE); 8093 return (PF_DROP); 8094 } 8095 8096 switch (pd2.proto) { 8097 case IPPROTO_TCP: { 8098 struct tcphdr *th = &pd2.hdr.tcp; 8099 u_int32_t seq; 8100 struct pf_state_peer *src, *dst; 8101 u_int8_t dws; 8102 int copyback = 0; 8103 int action; 8104 8105 /* 8106 * Only the first 8 bytes of the TCP header can be 8107 * expected. Don't access any TCP header fields after 8108 * th_seq, an ackskew test is not possible. 8109 */ 8110 if (!pf_pull_hdr(pd->m, pd2.off, th, 8, NULL, reason, 8111 pd2.af)) { 8112 DPFPRINTF(PF_DEBUG_MISC, 8113 "pf: ICMP error message too short " 8114 "(tcp)"); 8115 return (PF_DROP); 8116 } 8117 pd2.pcksum = &pd2.hdr.tcp.th_sum; 8118 8119 key.af = pd2.af; 8120 key.proto = IPPROTO_TCP; 8121 pf_addrcpy(&key.addr[pd2.sidx], pd2.src, key.af); 8122 pf_addrcpy(&key.addr[pd2.didx], pd2.dst, key.af); 8123 key.port[pd2.sidx] = th->th_sport; 8124 key.port[pd2.didx] = th->th_dport; 8125 8126 action = pf_find_state(&pd2, &key, state); 8127 if (action != PF_MATCH) 8128 return (action); 8129 8130 if (pd->dir == (*state)->direction) { 8131 if (PF_REVERSED_KEY(*state, pd->af)) { 8132 src = &(*state)->src; 8133 dst = &(*state)->dst; 8134 } else { 8135 src = &(*state)->dst; 8136 dst = &(*state)->src; 8137 } 8138 } else { 8139 if (PF_REVERSED_KEY(*state, pd->af)) { 8140 src = &(*state)->dst; 8141 dst = &(*state)->src; 8142 } else { 8143 src = &(*state)->src; 8144 dst = &(*state)->dst; 8145 } 8146 } 8147 8148 if (src->wscale && dst->wscale) 8149 dws = dst->wscale & PF_WSCALE_MASK; 8150 else 8151 dws = 0; 8152 8153 /* Demodulate sequence number */ 8154 seq = ntohl(th->th_seq) - src->seqdiff; 8155 if (src->seqdiff) { 8156 pf_change_a(&th->th_seq, icmpsum, 8157 htonl(seq), 0); 8158 copyback = 1; 8159 } 8160 8161 if (!((*state)->state_flags & PFSTATE_SLOPPY) && 8162 (!SEQ_GEQ(src->seqhi, seq) || 8163 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 8164 if (V_pf_status.debug >= PF_DEBUG_MISC) { 8165 printf("pf: BAD ICMP %d:%d ", 8166 icmptype, icmpcode); 8167 pf_print_host(pd->src, 0, pd->af); 8168 printf(" -> "); 8169 pf_print_host(pd->dst, 0, pd->af); 8170 printf(" state: "); 8171 pf_print_state(*state); 8172 printf(" seq=%u\n", seq); 8173 } 8174 REASON_SET(reason, PFRES_BADSTATE); 8175 return (PF_DROP); 8176 } else { 8177 if (V_pf_status.debug >= PF_DEBUG_MISC) { 8178 printf("pf: OK ICMP %d:%d ", 8179 icmptype, icmpcode); 8180 pf_print_host(pd->src, 0, pd->af); 8181 printf(" -> "); 8182 pf_print_host(pd->dst, 0, pd->af); 8183 printf(" state: "); 8184 pf_print_state(*state); 8185 printf(" seq=%u\n", seq); 8186 } 8187 } 8188 8189 /* translate source/destination address, if necessary */ 8190 if ((*state)->key[PF_SK_WIRE] != 8191 (*state)->key[PF_SK_STACK]) { 8192 8193 struct pf_state_key *nk; 8194 8195 if (PF_REVERSED_KEY(*state, pd->af)) 8196 nk = (*state)->key[pd->sidx]; 8197 else 8198 nk = (*state)->key[pd->didx]; 8199 8200 #if defined(INET) && defined(INET6) 8201 int afto, sidx, didx; 8202 8203 afto = pd->af != nk->af; 8204 8205 if (afto && (*state)->direction == PF_IN) { 8206 sidx = pd2.didx; 8207 didx = pd2.sidx; 8208 } else { 8209 sidx = pd2.sidx; 8210 didx = pd2.didx; 8211 } 8212 8213 if (afto) { 8214 if (pf_translate_icmp_af(nk->af, 8215 &pd->hdr.icmp)) 8216 return (PF_DROP); 8217 m_copyback(pd->m, pd->off, 8218 sizeof(struct icmp6_hdr), 8219 (c_caddr_t)&pd->hdr.icmp6); 8220 if (pf_change_icmp_af(pd->m, ipoff2, pd, 8221 &pd2, &nk->addr[sidx], 8222 &nk->addr[didx], pd->af, 8223 nk->af)) 8224 return (PF_DROP); 8225 pf_addrcpy(&pd->nsaddr, 8226 &nk->addr[pd2.sidx], nk->af); 8227 pf_addrcpy(&pd->ndaddr, 8228 &nk->addr[pd2.didx], nk->af); 8229 if (nk->af == AF_INET) { 8230 pd->proto = IPPROTO_ICMP; 8231 } else { 8232 pd->proto = IPPROTO_ICMPV6; 8233 /* 8234 * IPv4 becomes IPv6 so we must 8235 * copy IPv4 src addr to least 8236 * 32bits in IPv6 address to 8237 * keep traceroute/icmp 8238 * working. 8239 */ 8240 pd->nsaddr.addr32[3] = 8241 pd->src->addr32[0]; 8242 } 8243 pd->naf = pd2.naf = nk->af; 8244 pf_change_ap(&pd2, pd2.src, &th->th_sport, 8245 &nk->addr[pd2.sidx], nk->port[sidx]); 8246 pf_change_ap(&pd2, pd2.dst, &th->th_dport, 8247 &nk->addr[pd2.didx], nk->port[didx]); 8248 m_copyback(pd2.m, pd2.off, 8, (c_caddr_t)th); 8249 return (PF_AFRT); 8250 } 8251 #endif /* INET && INET6 */ 8252 8253 if (PF_ANEQ(pd2.src, 8254 &nk->addr[pd2.sidx], pd2.af) || 8255 nk->port[pd2.sidx] != th->th_sport) 8256 pf_change_icmp(pd2.src, &th->th_sport, 8257 daddr, &nk->addr[pd2.sidx], 8258 nk->port[pd2.sidx], NULL, 8259 pd2.ip_sum, icmpsum, 8260 pd->ip_sum, 0, pd2.af); 8261 8262 if (PF_ANEQ(pd2.dst, 8263 &nk->addr[pd2.didx], pd2.af) || 8264 nk->port[pd2.didx] != th->th_dport) 8265 pf_change_icmp(pd2.dst, &th->th_dport, 8266 saddr, &nk->addr[pd2.didx], 8267 nk->port[pd2.didx], NULL, 8268 pd2.ip_sum, icmpsum, 8269 pd->ip_sum, 0, pd2.af); 8270 copyback = 1; 8271 } 8272 8273 if (copyback) { 8274 switch (pd2.af) { 8275 #ifdef INET 8276 case AF_INET: 8277 m_copyback(pd->m, pd->off, ICMP_MINLEN, 8278 (caddr_t )&pd->hdr.icmp); 8279 m_copyback(pd->m, ipoff2, sizeof(h2), 8280 (caddr_t )&h2); 8281 break; 8282 #endif /* INET */ 8283 #ifdef INET6 8284 case AF_INET6: 8285 m_copyback(pd->m, pd->off, 8286 sizeof(struct icmp6_hdr), 8287 (caddr_t )&pd->hdr.icmp6); 8288 m_copyback(pd->m, ipoff2, sizeof(h2_6), 8289 (caddr_t )&h2_6); 8290 break; 8291 #endif /* INET6 */ 8292 default: 8293 unhandled_af(pd->af); 8294 } 8295 m_copyback(pd->m, pd2.off, 8, (caddr_t)th); 8296 } 8297 8298 return (PF_PASS); 8299 break; 8300 } 8301 case IPPROTO_UDP: { 8302 struct udphdr *uh = &pd2.hdr.udp; 8303 int action; 8304 8305 if (!pf_pull_hdr(pd->m, pd2.off, uh, sizeof(*uh), 8306 NULL, reason, pd2.af)) { 8307 DPFPRINTF(PF_DEBUG_MISC, 8308 "pf: ICMP error message too short " 8309 "(udp)"); 8310 return (PF_DROP); 8311 } 8312 pd2.pcksum = &pd2.hdr.udp.uh_sum; 8313 8314 key.af = pd2.af; 8315 key.proto = IPPROTO_UDP; 8316 pf_addrcpy(&key.addr[pd2.sidx], pd2.src, key.af); 8317 pf_addrcpy(&key.addr[pd2.didx], pd2.dst, key.af); 8318 key.port[pd2.sidx] = uh->uh_sport; 8319 key.port[pd2.didx] = uh->uh_dport; 8320 8321 action = pf_find_state(&pd2, &key, state); 8322 if (action != PF_MATCH) 8323 return (action); 8324 8325 /* translate source/destination address, if necessary */ 8326 if ((*state)->key[PF_SK_WIRE] != 8327 (*state)->key[PF_SK_STACK]) { 8328 struct pf_state_key *nk; 8329 8330 if (PF_REVERSED_KEY(*state, pd->af)) 8331 nk = (*state)->key[pd->sidx]; 8332 else 8333 nk = (*state)->key[pd->didx]; 8334 8335 #if defined(INET) && defined(INET6) 8336 int afto, sidx, didx; 8337 8338 afto = pd->af != nk->af; 8339 8340 if (afto && (*state)->direction == PF_IN) { 8341 sidx = pd2.didx; 8342 didx = pd2.sidx; 8343 } else { 8344 sidx = pd2.sidx; 8345 didx = pd2.didx; 8346 } 8347 8348 if (afto) { 8349 if (pf_translate_icmp_af(nk->af, 8350 &pd->hdr.icmp)) 8351 return (PF_DROP); 8352 m_copyback(pd->m, pd->off, 8353 sizeof(struct icmp6_hdr), 8354 (c_caddr_t)&pd->hdr.icmp6); 8355 if (pf_change_icmp_af(pd->m, ipoff2, pd, 8356 &pd2, &nk->addr[sidx], 8357 &nk->addr[didx], pd->af, 8358 nk->af)) 8359 return (PF_DROP); 8360 pf_addrcpy(&pd->nsaddr, 8361 &nk->addr[pd2.sidx], nk->af); 8362 pf_addrcpy(&pd->ndaddr, 8363 &nk->addr[pd2.didx], nk->af); 8364 if (nk->af == AF_INET) { 8365 pd->proto = IPPROTO_ICMP; 8366 } else { 8367 pd->proto = IPPROTO_ICMPV6; 8368 /* 8369 * IPv4 becomes IPv6 so we must 8370 * copy IPv4 src addr to least 8371 * 32bits in IPv6 address to 8372 * keep traceroute/icmp 8373 * working. 8374 */ 8375 pd->nsaddr.addr32[3] = 8376 pd->src->addr32[0]; 8377 } 8378 pd->naf = pd2.naf = nk->af; 8379 pf_change_ap(&pd2, pd2.src, &uh->uh_sport, 8380 &nk->addr[pd2.sidx], nk->port[sidx]); 8381 pf_change_ap(&pd2, pd2.dst, &uh->uh_dport, 8382 &nk->addr[pd2.didx], nk->port[didx]); 8383 m_copyback(pd2.m, pd2.off, sizeof(*uh), 8384 (c_caddr_t)uh); 8385 return (PF_AFRT); 8386 } 8387 #endif /* INET && INET6 */ 8388 8389 if (PF_ANEQ(pd2.src, 8390 &nk->addr[pd2.sidx], pd2.af) || 8391 nk->port[pd2.sidx] != uh->uh_sport) 8392 pf_change_icmp(pd2.src, &uh->uh_sport, 8393 daddr, &nk->addr[pd2.sidx], 8394 nk->port[pd2.sidx], &uh->uh_sum, 8395 pd2.ip_sum, icmpsum, 8396 pd->ip_sum, 1, pd2.af); 8397 8398 if (PF_ANEQ(pd2.dst, 8399 &nk->addr[pd2.didx], pd2.af) || 8400 nk->port[pd2.didx] != uh->uh_dport) 8401 pf_change_icmp(pd2.dst, &uh->uh_dport, 8402 saddr, &nk->addr[pd2.didx], 8403 nk->port[pd2.didx], &uh->uh_sum, 8404 pd2.ip_sum, icmpsum, 8405 pd->ip_sum, 1, pd2.af); 8406 8407 switch (pd2.af) { 8408 #ifdef INET 8409 case AF_INET: 8410 m_copyback(pd->m, pd->off, ICMP_MINLEN, 8411 (caddr_t )&pd->hdr.icmp); 8412 m_copyback(pd->m, ipoff2, sizeof(h2), (caddr_t)&h2); 8413 break; 8414 #endif /* INET */ 8415 #ifdef INET6 8416 case AF_INET6: 8417 m_copyback(pd->m, pd->off, 8418 sizeof(struct icmp6_hdr), 8419 (caddr_t )&pd->hdr.icmp6); 8420 m_copyback(pd->m, ipoff2, sizeof(h2_6), 8421 (caddr_t )&h2_6); 8422 break; 8423 #endif /* INET6 */ 8424 } 8425 m_copyback(pd->m, pd2.off, sizeof(*uh), (caddr_t)uh); 8426 } 8427 return (PF_PASS); 8428 break; 8429 } 8430 #ifdef INET 8431 case IPPROTO_SCTP: { 8432 struct sctphdr *sh = &pd2.hdr.sctp; 8433 struct pf_state_peer *src; 8434 int copyback = 0; 8435 int action; 8436 8437 if (! pf_pull_hdr(pd->m, pd2.off, sh, sizeof(*sh), NULL, reason, 8438 pd2.af)) { 8439 DPFPRINTF(PF_DEBUG_MISC, 8440 "pf: ICMP error message too short " 8441 "(sctp)"); 8442 return (PF_DROP); 8443 } 8444 pd2.pcksum = &pd2.sctp_dummy_sum; 8445 8446 key.af = pd2.af; 8447 key.proto = IPPROTO_SCTP; 8448 pf_addrcpy(&key.addr[pd2.sidx], pd2.src, key.af); 8449 pf_addrcpy(&key.addr[pd2.didx], pd2.dst, key.af); 8450 key.port[pd2.sidx] = sh->src_port; 8451 key.port[pd2.didx] = sh->dest_port; 8452 8453 action = pf_find_state(&pd2, &key, state); 8454 if (action != PF_MATCH) 8455 return (action); 8456 8457 if (pd->dir == (*state)->direction) { 8458 if (PF_REVERSED_KEY(*state, pd->af)) 8459 src = &(*state)->src; 8460 else 8461 src = &(*state)->dst; 8462 } else { 8463 if (PF_REVERSED_KEY(*state, pd->af)) 8464 src = &(*state)->dst; 8465 else 8466 src = &(*state)->src; 8467 } 8468 8469 if (src->scrub->pfss_v_tag != sh->v_tag) { 8470 DPFPRINTF(PF_DEBUG_MISC, 8471 "pf: ICMP error message has incorrect " 8472 "SCTP v_tag"); 8473 return (PF_DROP); 8474 } 8475 8476 /* translate source/destination address, if necessary */ 8477 if ((*state)->key[PF_SK_WIRE] != 8478 (*state)->key[PF_SK_STACK]) { 8479 8480 struct pf_state_key *nk; 8481 8482 if (PF_REVERSED_KEY(*state, pd->af)) 8483 nk = (*state)->key[pd->sidx]; 8484 else 8485 nk = (*state)->key[pd->didx]; 8486 8487 #if defined(INET) && defined(INET6) 8488 int afto, sidx, didx; 8489 8490 afto = pd->af != nk->af; 8491 8492 if (afto && (*state)->direction == PF_IN) { 8493 sidx = pd2.didx; 8494 didx = pd2.sidx; 8495 } else { 8496 sidx = pd2.sidx; 8497 didx = pd2.didx; 8498 } 8499 8500 if (afto) { 8501 if (pf_translate_icmp_af(nk->af, 8502 &pd->hdr.icmp)) 8503 return (PF_DROP); 8504 m_copyback(pd->m, pd->off, 8505 sizeof(struct icmp6_hdr), 8506 (c_caddr_t)&pd->hdr.icmp6); 8507 if (pf_change_icmp_af(pd->m, ipoff2, pd, 8508 &pd2, &nk->addr[sidx], 8509 &nk->addr[didx], pd->af, 8510 nk->af)) 8511 return (PF_DROP); 8512 sh->src_port = nk->port[sidx]; 8513 sh->dest_port = nk->port[didx]; 8514 m_copyback(pd2.m, pd2.off, sizeof(*sh), (c_caddr_t)sh); 8515 pf_addrcpy(&pd->nsaddr, 8516 &nk->addr[pd2.sidx], nk->af); 8517 pf_addrcpy(&pd->ndaddr, 8518 &nk->addr[pd2.didx], nk->af); 8519 if (nk->af == AF_INET) { 8520 pd->proto = IPPROTO_ICMP; 8521 } else { 8522 pd->proto = IPPROTO_ICMPV6; 8523 /* 8524 * IPv4 becomes IPv6 so we must 8525 * copy IPv4 src addr to least 8526 * 32bits in IPv6 address to 8527 * keep traceroute/icmp 8528 * working. 8529 */ 8530 pd->nsaddr.addr32[3] = 8531 pd->src->addr32[0]; 8532 } 8533 pd->naf = nk->af; 8534 return (PF_AFRT); 8535 } 8536 #endif /* INET && INET6 */ 8537 8538 if (PF_ANEQ(pd2.src, 8539 &nk->addr[pd2.sidx], pd2.af) || 8540 nk->port[pd2.sidx] != sh->src_port) 8541 pf_change_icmp(pd2.src, &sh->src_port, 8542 daddr, &nk->addr[pd2.sidx], 8543 nk->port[pd2.sidx], NULL, 8544 pd2.ip_sum, icmpsum, 8545 pd->ip_sum, 0, pd2.af); 8546 8547 if (PF_ANEQ(pd2.dst, 8548 &nk->addr[pd2.didx], pd2.af) || 8549 nk->port[pd2.didx] != sh->dest_port) 8550 pf_change_icmp(pd2.dst, &sh->dest_port, 8551 saddr, &nk->addr[pd2.didx], 8552 nk->port[pd2.didx], NULL, 8553 pd2.ip_sum, icmpsum, 8554 pd->ip_sum, 0, pd2.af); 8555 copyback = 1; 8556 } 8557 8558 if (copyback) { 8559 switch (pd2.af) { 8560 #ifdef INET 8561 case AF_INET: 8562 m_copyback(pd->m, pd->off, ICMP_MINLEN, 8563 (caddr_t )&pd->hdr.icmp); 8564 m_copyback(pd->m, ipoff2, sizeof(h2), 8565 (caddr_t )&h2); 8566 break; 8567 #endif /* INET */ 8568 #ifdef INET6 8569 case AF_INET6: 8570 m_copyback(pd->m, pd->off, 8571 sizeof(struct icmp6_hdr), 8572 (caddr_t )&pd->hdr.icmp6); 8573 m_copyback(pd->m, ipoff2, sizeof(h2_6), 8574 (caddr_t )&h2_6); 8575 break; 8576 #endif /* INET6 */ 8577 } 8578 m_copyback(pd->m, pd2.off, sizeof(*sh), (caddr_t)sh); 8579 } 8580 8581 return (PF_PASS); 8582 break; 8583 } 8584 case IPPROTO_ICMP: { 8585 struct icmp *iih = &pd2.hdr.icmp; 8586 8587 if (pd2.af != AF_INET) { 8588 REASON_SET(reason, PFRES_NORM); 8589 return (PF_DROP); 8590 } 8591 8592 if (!pf_pull_hdr(pd->m, pd2.off, iih, ICMP_MINLEN, 8593 NULL, reason, pd2.af)) { 8594 DPFPRINTF(PF_DEBUG_MISC, 8595 "pf: ICMP error message too short i" 8596 "(icmp)"); 8597 return (PF_DROP); 8598 } 8599 pd2.pcksum = &pd2.hdr.icmp.icmp_cksum; 8600 8601 icmpid = iih->icmp_id; 8602 pf_icmp_mapping(&pd2, iih->icmp_type, 8603 &icmp_dir, &virtual_id, &virtual_type); 8604 8605 ret = pf_icmp_state_lookup(&key, &pd2, state, 8606 virtual_id, virtual_type, icmp_dir, &iidx, 0, 1); 8607 if (ret >= 0) { 8608 MPASS(*state == NULL); 8609 return (ret); 8610 } 8611 8612 /* translate source/destination address, if necessary */ 8613 if ((*state)->key[PF_SK_WIRE] != 8614 (*state)->key[PF_SK_STACK]) { 8615 struct pf_state_key *nk; 8616 8617 if (PF_REVERSED_KEY(*state, pd->af)) 8618 nk = (*state)->key[pd->sidx]; 8619 else 8620 nk = (*state)->key[pd->didx]; 8621 8622 #if defined(INET) && defined(INET6) 8623 int afto, sidx, didx; 8624 8625 afto = pd->af != nk->af; 8626 8627 if (afto && (*state)->direction == PF_IN) { 8628 sidx = pd2.didx; 8629 didx = pd2.sidx; 8630 iidx = !iidx; 8631 } else { 8632 sidx = pd2.sidx; 8633 didx = pd2.didx; 8634 } 8635 8636 if (afto) { 8637 if (nk->af != AF_INET6) 8638 return (PF_DROP); 8639 if (pf_translate_icmp_af(nk->af, 8640 &pd->hdr.icmp)) 8641 return (PF_DROP); 8642 m_copyback(pd->m, pd->off, 8643 sizeof(struct icmp6_hdr), 8644 (c_caddr_t)&pd->hdr.icmp6); 8645 if (pf_change_icmp_af(pd->m, ipoff2, pd, 8646 &pd2, &nk->addr[sidx], 8647 &nk->addr[didx], pd->af, 8648 nk->af)) 8649 return (PF_DROP); 8650 pd->proto = IPPROTO_ICMPV6; 8651 if (pf_translate_icmp_af(nk->af, iih)) 8652 return (PF_DROP); 8653 if (virtual_type == htons(ICMP_ECHO) && 8654 nk->port[iidx] != iih->icmp_id) 8655 iih->icmp_id = nk->port[iidx]; 8656 m_copyback(pd2.m, pd2.off, ICMP_MINLEN, 8657 (c_caddr_t)iih); 8658 pf_addrcpy(&pd->nsaddr, 8659 &nk->addr[pd2.sidx], nk->af); 8660 pf_addrcpy(&pd->ndaddr, 8661 &nk->addr[pd2.didx], nk->af); 8662 /* 8663 * IPv4 becomes IPv6 so we must copy 8664 * IPv4 src addr to least 32bits in 8665 * IPv6 address to keep traceroute 8666 * working. 8667 */ 8668 pd->nsaddr.addr32[3] = 8669 pd->src->addr32[0]; 8670 pd->naf = nk->af; 8671 return (PF_AFRT); 8672 } 8673 #endif /* INET && INET6 */ 8674 8675 if (PF_ANEQ(pd2.src, 8676 &nk->addr[pd2.sidx], pd2.af) || 8677 (virtual_type == htons(ICMP_ECHO) && 8678 nk->port[iidx] != iih->icmp_id)) 8679 pf_change_icmp(pd2.src, 8680 (virtual_type == htons(ICMP_ECHO)) ? 8681 &iih->icmp_id : NULL, 8682 daddr, &nk->addr[pd2.sidx], 8683 (virtual_type == htons(ICMP_ECHO)) ? 8684 nk->port[iidx] : 0, NULL, 8685 pd2.ip_sum, icmpsum, 8686 pd->ip_sum, 0, AF_INET); 8687 8688 if (PF_ANEQ(pd2.dst, 8689 &nk->addr[pd2.didx], pd2.af)) 8690 pf_change_icmp(pd2.dst, NULL, NULL, 8691 &nk->addr[pd2.didx], 0, NULL, 8692 pd2.ip_sum, icmpsum, pd->ip_sum, 0, 8693 AF_INET); 8694 8695 m_copyback(pd->m, pd->off, ICMP_MINLEN, (caddr_t)&pd->hdr.icmp); 8696 m_copyback(pd->m, ipoff2, sizeof(h2), (caddr_t)&h2); 8697 m_copyback(pd->m, pd2.off, ICMP_MINLEN, (caddr_t)iih); 8698 } 8699 return (PF_PASS); 8700 break; 8701 } 8702 #endif /* INET */ 8703 #ifdef INET6 8704 case IPPROTO_ICMPV6: { 8705 struct icmp6_hdr *iih = &pd2.hdr.icmp6; 8706 8707 if (pd2.af != AF_INET6) { 8708 REASON_SET(reason, PFRES_NORM); 8709 return (PF_DROP); 8710 } 8711 8712 if (!pf_pull_hdr(pd->m, pd2.off, iih, 8713 sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 8714 DPFPRINTF(PF_DEBUG_MISC, 8715 "pf: ICMP error message too short " 8716 "(icmp6)"); 8717 return (PF_DROP); 8718 } 8719 pd2.pcksum = &pd2.hdr.icmp6.icmp6_cksum; 8720 8721 pf_icmp_mapping(&pd2, iih->icmp6_type, 8722 &icmp_dir, &virtual_id, &virtual_type); 8723 8724 ret = pf_icmp_state_lookup(&key, &pd2, state, 8725 virtual_id, virtual_type, icmp_dir, &iidx, 0, 1); 8726 /* IPv6? try matching a multicast address */ 8727 if (ret == PF_DROP && pd2.af == AF_INET6 && 8728 icmp_dir == PF_OUT) { 8729 MPASS(*state == NULL); 8730 ret = pf_icmp_state_lookup(&key, &pd2, 8731 state, virtual_id, virtual_type, 8732 icmp_dir, &iidx, 1, 1); 8733 } 8734 if (ret >= 0) { 8735 MPASS(*state == NULL); 8736 return (ret); 8737 } 8738 8739 /* translate source/destination address, if necessary */ 8740 if ((*state)->key[PF_SK_WIRE] != 8741 (*state)->key[PF_SK_STACK]) { 8742 struct pf_state_key *nk; 8743 8744 if (PF_REVERSED_KEY(*state, pd->af)) 8745 nk = (*state)->key[pd->sidx]; 8746 else 8747 nk = (*state)->key[pd->didx]; 8748 8749 #if defined(INET) && defined(INET6) 8750 int afto, sidx, didx; 8751 8752 afto = pd->af != nk->af; 8753 8754 if (afto && (*state)->direction == PF_IN) { 8755 sidx = pd2.didx; 8756 didx = pd2.sidx; 8757 iidx = !iidx; 8758 } else { 8759 sidx = pd2.sidx; 8760 didx = pd2.didx; 8761 } 8762 8763 if (afto) { 8764 if (nk->af != AF_INET) 8765 return (PF_DROP); 8766 if (pf_translate_icmp_af(nk->af, 8767 &pd->hdr.icmp)) 8768 return (PF_DROP); 8769 m_copyback(pd->m, pd->off, 8770 sizeof(struct icmp6_hdr), 8771 (c_caddr_t)&pd->hdr.icmp6); 8772 if (pf_change_icmp_af(pd->m, ipoff2, pd, 8773 &pd2, &nk->addr[sidx], 8774 &nk->addr[didx], pd->af, 8775 nk->af)) 8776 return (PF_DROP); 8777 pd->proto = IPPROTO_ICMP; 8778 if (pf_translate_icmp_af(nk->af, iih)) 8779 return (PF_DROP); 8780 if (virtual_type == 8781 htons(ICMP6_ECHO_REQUEST) && 8782 nk->port[iidx] != iih->icmp6_id) 8783 iih->icmp6_id = nk->port[iidx]; 8784 m_copyback(pd2.m, pd2.off, 8785 sizeof(struct icmp6_hdr), (c_caddr_t)iih); 8786 pf_addrcpy(&pd->nsaddr, 8787 &nk->addr[pd2.sidx], nk->af); 8788 pf_addrcpy(&pd->ndaddr, 8789 &nk->addr[pd2.didx], nk->af); 8790 pd->naf = nk->af; 8791 return (PF_AFRT); 8792 } 8793 #endif /* INET && INET6 */ 8794 8795 if (PF_ANEQ(pd2.src, 8796 &nk->addr[pd2.sidx], pd2.af) || 8797 ((virtual_type == htons(ICMP6_ECHO_REQUEST)) && 8798 nk->port[pd2.sidx] != iih->icmp6_id)) 8799 pf_change_icmp(pd2.src, 8800 (virtual_type == htons(ICMP6_ECHO_REQUEST)) 8801 ? &iih->icmp6_id : NULL, 8802 daddr, &nk->addr[pd2.sidx], 8803 (virtual_type == htons(ICMP6_ECHO_REQUEST)) 8804 ? nk->port[iidx] : 0, NULL, 8805 pd2.ip_sum, icmpsum, 8806 pd->ip_sum, 0, AF_INET6); 8807 8808 if (PF_ANEQ(pd2.dst, 8809 &nk->addr[pd2.didx], pd2.af)) 8810 pf_change_icmp(pd2.dst, NULL, NULL, 8811 &nk->addr[pd2.didx], 0, NULL, 8812 pd2.ip_sum, icmpsum, 8813 pd->ip_sum, 0, AF_INET6); 8814 8815 m_copyback(pd->m, pd->off, sizeof(struct icmp6_hdr), 8816 (caddr_t)&pd->hdr.icmp6); 8817 m_copyback(pd->m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 8818 m_copyback(pd->m, pd2.off, sizeof(struct icmp6_hdr), 8819 (caddr_t)iih); 8820 } 8821 return (PF_PASS); 8822 break; 8823 } 8824 #endif /* INET6 */ 8825 default: { 8826 int action; 8827 8828 key.af = pd2.af; 8829 key.proto = pd2.proto; 8830 pf_addrcpy(&key.addr[pd2.sidx], pd2.src, key.af); 8831 pf_addrcpy(&key.addr[pd2.didx], pd2.dst, key.af); 8832 key.port[0] = key.port[1] = 0; 8833 8834 action = pf_find_state(&pd2, &key, state); 8835 if (action != PF_MATCH) 8836 return (action); 8837 8838 /* translate source/destination address, if necessary */ 8839 if ((*state)->key[PF_SK_WIRE] != 8840 (*state)->key[PF_SK_STACK]) { 8841 struct pf_state_key *nk = 8842 (*state)->key[pd->didx]; 8843 8844 if (PF_ANEQ(pd2.src, 8845 &nk->addr[pd2.sidx], pd2.af)) 8846 pf_change_icmp(pd2.src, NULL, daddr, 8847 &nk->addr[pd2.sidx], 0, NULL, 8848 pd2.ip_sum, icmpsum, 8849 pd->ip_sum, 0, pd2.af); 8850 8851 if (PF_ANEQ(pd2.dst, 8852 &nk->addr[pd2.didx], pd2.af)) 8853 pf_change_icmp(pd2.dst, NULL, saddr, 8854 &nk->addr[pd2.didx], 0, NULL, 8855 pd2.ip_sum, icmpsum, 8856 pd->ip_sum, 0, pd2.af); 8857 8858 switch (pd2.af) { 8859 #ifdef INET 8860 case AF_INET: 8861 m_copyback(pd->m, pd->off, ICMP_MINLEN, 8862 (caddr_t)&pd->hdr.icmp); 8863 m_copyback(pd->m, ipoff2, sizeof(h2), (caddr_t)&h2); 8864 break; 8865 #endif /* INET */ 8866 #ifdef INET6 8867 case AF_INET6: 8868 m_copyback(pd->m, pd->off, 8869 sizeof(struct icmp6_hdr), 8870 (caddr_t )&pd->hdr.icmp6); 8871 m_copyback(pd->m, ipoff2, sizeof(h2_6), 8872 (caddr_t )&h2_6); 8873 break; 8874 #endif /* INET6 */ 8875 } 8876 } 8877 return (PF_PASS); 8878 break; 8879 } 8880 } 8881 } 8882 } 8883 8884 /* 8885 * ipoff and off are measured from the start of the mbuf chain. 8886 * h must be at "ipoff" on the mbuf chain. 8887 */ 8888 void * 8889 pf_pull_hdr(const struct mbuf *m, int off, void *p, int len, 8890 u_short *actionp, u_short *reasonp, sa_family_t af) 8891 { 8892 int iplen = 0; 8893 switch (af) { 8894 #ifdef INET 8895 case AF_INET: { 8896 const struct ip *h = mtod(m, struct ip *); 8897 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 8898 8899 if (fragoff) { 8900 if (fragoff >= len) 8901 ACTION_SET(actionp, PF_PASS); 8902 else { 8903 ACTION_SET(actionp, PF_DROP); 8904 REASON_SET(reasonp, PFRES_FRAG); 8905 } 8906 return (NULL); 8907 } 8908 iplen = ntohs(h->ip_len); 8909 break; 8910 } 8911 #endif /* INET */ 8912 #ifdef INET6 8913 case AF_INET6: { 8914 const struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 8915 8916 iplen = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 8917 break; 8918 } 8919 #endif /* INET6 */ 8920 } 8921 if (m->m_pkthdr.len < off + len || iplen < off + len) { 8922 ACTION_SET(actionp, PF_DROP); 8923 REASON_SET(reasonp, PFRES_SHORT); 8924 return (NULL); 8925 } 8926 m_copydata(m, off, len, p); 8927 return (p); 8928 } 8929 8930 int 8931 pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *kif, 8932 int rtableid) 8933 { 8934 struct ifnet *ifp; 8935 8936 /* 8937 * Skip check for addresses with embedded interface scope, 8938 * as they would always match anyway. 8939 */ 8940 if (af == AF_INET6 && IN6_IS_SCOPE_EMBED(&addr->v6)) 8941 return (1); 8942 8943 if (af != AF_INET && af != AF_INET6) 8944 return (0); 8945 8946 if (kif == V_pfi_all) 8947 return (1); 8948 8949 /* Skip checks for ipsec interfaces */ 8950 if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 8951 return (1); 8952 8953 ifp = (kif != NULL) ? kif->pfik_ifp : NULL; 8954 8955 switch (af) { 8956 #ifdef INET6 8957 case AF_INET6: 8958 return (fib6_check_urpf(rtableid, &addr->v6, 0, NHR_NONE, 8959 ifp)); 8960 #endif /* INET6 */ 8961 #ifdef INET 8962 case AF_INET: 8963 return (fib4_check_urpf(rtableid, addr->v4, 0, NHR_NONE, 8964 ifp)); 8965 #endif /* INET */ 8966 } 8967 8968 return (0); 8969 } 8970 8971 #ifdef INET 8972 static int 8973 pf_route(struct pf_krule *r, struct ifnet *oifp, 8974 struct pf_kstate *s, struct pf_pdesc *pd, struct inpcb *inp) 8975 { 8976 struct mbuf *m0, *m1, *md; 8977 struct route ro; 8978 const struct sockaddr *gw = &ro.ro_dst; 8979 struct sockaddr_in *dst; 8980 struct ip *ip; 8981 struct ifnet *ifp = NULL; 8982 int error = 0; 8983 uint16_t ip_len, ip_off; 8984 uint16_t tmp; 8985 int r_dir; 8986 bool skip_test = false; 8987 int action = PF_PASS; 8988 8989 KASSERT(pd->m && r && oifp, ("%s: invalid parameters", __func__)); 8990 8991 SDT_PROBE4(pf, ip, route_to, entry, pd->m, pd, s, oifp); 8992 8993 if (s) { 8994 r_dir = s->direction; 8995 } else { 8996 r_dir = r->direction; 8997 } 8998 8999 KASSERT(pd->dir == PF_IN || pd->dir == PF_OUT || 9000 r_dir == PF_IN || r_dir == PF_OUT, ("%s: invalid direction", 9001 __func__)); 9002 9003 if ((pd->pf_mtag == NULL && 9004 ((pd->pf_mtag = pf_get_mtag(pd->m)) == NULL)) || 9005 pd->pf_mtag->routed++ > 3) { 9006 m0 = pd->m; 9007 pd->m = NULL; 9008 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9009 action = PF_DROP; 9010 goto bad_locked; 9011 } 9012 9013 if (pd->act.rt_kif != NULL) 9014 ifp = pd->act.rt_kif->pfik_ifp; 9015 9016 if (pd->act.rt == PF_DUPTO) { 9017 if ((pd->pf_mtag->flags & PF_MTAG_FLAG_DUPLICATED)) { 9018 if (s != NULL) { 9019 PF_STATE_UNLOCK(s); 9020 } 9021 if (ifp == oifp) { 9022 /* When the 2nd interface is not skipped */ 9023 return (action); 9024 } else { 9025 m0 = pd->m; 9026 pd->m = NULL; 9027 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9028 action = PF_DROP; 9029 goto bad; 9030 } 9031 } else { 9032 pd->pf_mtag->flags |= PF_MTAG_FLAG_DUPLICATED; 9033 if (((m0 = m_dup(pd->m, M_NOWAIT)) == NULL)) { 9034 if (s) 9035 PF_STATE_UNLOCK(s); 9036 return (action); 9037 } 9038 } 9039 } else { 9040 if ((pd->act.rt == PF_REPLYTO) == (r_dir == pd->dir)) { 9041 if (pd->af == pd->naf) { 9042 pf_dummynet(pd, s, r, &pd->m); 9043 if (s) 9044 PF_STATE_UNLOCK(s); 9045 return (action); 9046 } else { 9047 if (r_dir == PF_IN) { 9048 skip_test = true; 9049 } 9050 } 9051 } 9052 9053 /* 9054 * If we're actually doing route-to and af-to and are in the 9055 * reply direction. 9056 */ 9057 if (pd->act.rt_kif && pd->act.rt_kif->pfik_ifp && 9058 pd->af != pd->naf) { 9059 if (pd->act.rt == PF_ROUTETO && r->naf != AF_INET) { 9060 /* Un-set ifp so we do a plain route lookup. */ 9061 ifp = NULL; 9062 } 9063 if (pd->act.rt == PF_REPLYTO && r->naf != AF_INET6) { 9064 /* Un-set ifp so we do a plain route lookup. */ 9065 ifp = NULL; 9066 } 9067 } 9068 m0 = pd->m; 9069 } 9070 9071 ip = mtod(m0, struct ip *); 9072 9073 bzero(&ro, sizeof(ro)); 9074 dst = (struct sockaddr_in *)&ro.ro_dst; 9075 dst->sin_family = AF_INET; 9076 dst->sin_len = sizeof(struct sockaddr_in); 9077 dst->sin_addr.s_addr = pd->act.rt_addr.v4.s_addr; 9078 9079 if (pd->dir == PF_IN) { 9080 if (ip->ip_ttl <= IPTTLDEC) { 9081 if (r->rt != PF_DUPTO) 9082 pf_send_icmp(m0, ICMP_TIMXCEED, 9083 ICMP_TIMXCEED_INTRANS, 0, pd->af, r, 9084 pd->act.rtableid); 9085 action = PF_DROP; 9086 goto bad_locked; 9087 } 9088 ip->ip_ttl -= IPTTLDEC; 9089 } 9090 9091 if (s != NULL) { 9092 if (ifp == NULL && (pd->af != pd->naf)) { 9093 /* We're in the AFTO case. Do a route lookup. */ 9094 const struct nhop_object *nh; 9095 nh = fib4_lookup(M_GETFIB(m0), ip->ip_dst, 0, NHR_NONE, 0); 9096 if (nh) { 9097 ifp = nh->nh_ifp; 9098 9099 /* Use the gateway if needed. */ 9100 if (nh->nh_flags & NHF_GATEWAY) { 9101 gw = &nh->gw_sa; 9102 ro.ro_flags |= RT_HAS_GW; 9103 } else { 9104 dst->sin_addr = ip->ip_dst; 9105 } 9106 9107 /* 9108 * Bind to the correct interface if we're 9109 * if-bound. We don't know which interface 9110 * that will be until here, so we've inserted 9111 * the state on V_pf_all. Fix that now. 9112 */ 9113 if (s->kif == V_pfi_all && ifp != NULL && 9114 r->rule_flag & PFRULE_IFBOUND) 9115 s->kif = ifp->if_pf_kif; 9116 } 9117 } 9118 9119 if (r->rule_flag & PFRULE_IFBOUND && 9120 pd->act.rt == PF_REPLYTO && 9121 s->kif == V_pfi_all) { 9122 s->kif = pd->act.rt_kif; 9123 s->orig_kif = oifp->if_pf_kif; 9124 } 9125 9126 PF_STATE_UNLOCK(s); 9127 } 9128 9129 if (ifp == NULL) { 9130 m0 = pd->m; 9131 pd->m = NULL; 9132 action = PF_DROP; 9133 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9134 goto bad; 9135 } 9136 9137 if (r->rt == PF_DUPTO) 9138 skip_test = true; 9139 9140 if (pd->dir == PF_IN && !skip_test) { 9141 if (pf_test(AF_INET, PF_OUT, PFIL_FWD, ifp, &m0, inp, 9142 &pd->act) != PF_PASS) { 9143 action = PF_DROP; 9144 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9145 goto bad; 9146 } else if (m0 == NULL) { 9147 action = PF_DROP; 9148 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9149 goto done; 9150 } 9151 if (m0->m_len < sizeof(struct ip)) { 9152 DPFPRINTF(PF_DEBUG_URGENT, 9153 "%s: m0->m_len < sizeof(struct ip)", __func__); 9154 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9155 action = PF_DROP; 9156 goto bad; 9157 } 9158 ip = mtod(m0, struct ip *); 9159 } 9160 9161 if (ifp->if_flags & IFF_LOOPBACK) 9162 m0->m_flags |= M_SKIP_FIREWALL; 9163 9164 ip_len = ntohs(ip->ip_len); 9165 ip_off = ntohs(ip->ip_off); 9166 9167 /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 9168 m0->m_pkthdr.csum_flags |= CSUM_IP; 9169 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 9170 in_delayed_cksum(m0); 9171 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 9172 } 9173 if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 9174 pf_sctp_checksum(m0, (uint32_t)(ip->ip_hl << 2)); 9175 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 9176 } 9177 9178 if (pd->dir == PF_IN) { 9179 /* 9180 * Make sure dummynet gets the correct direction, in case it needs to 9181 * re-inject later. 9182 */ 9183 pd->dir = PF_OUT; 9184 9185 /* 9186 * The following processing is actually the rest of the inbound processing, even 9187 * though we've marked it as outbound (so we don't look through dummynet) and it 9188 * happens after the outbound processing (pf_test(PF_OUT) above). 9189 * Swap the dummynet pipe numbers, because it's going to come to the wrong 9190 * conclusion about what direction it's processing, and we can't fix it or it 9191 * will re-inject incorrectly. Swapping the pipe numbers means that its incorrect 9192 * decision will pick the right pipe, and everything will mostly work as expected. 9193 */ 9194 tmp = pd->act.dnrpipe; 9195 pd->act.dnrpipe = pd->act.dnpipe; 9196 pd->act.dnpipe = tmp; 9197 } 9198 9199 /* 9200 * If small enough for interface, or the interface will take 9201 * care of the fragmentation for us, we can just send directly. 9202 */ 9203 if (ip_len <= ifp->if_mtu || 9204 (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) { 9205 ip->ip_sum = 0; 9206 if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 9207 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 9208 m0->m_pkthdr.csum_flags &= ~CSUM_IP; 9209 } 9210 m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 9211 9212 md = m0; 9213 error = pf_dummynet_route(pd, s, r, ifp, gw, &md); 9214 if (md != NULL) { 9215 error = (*ifp->if_output)(ifp, md, gw, &ro); 9216 SDT_PROBE2(pf, ip, route_to, output, ifp, error); 9217 } 9218 goto done; 9219 } 9220 9221 /* Balk when DF bit is set or the interface didn't support TSO. */ 9222 if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 9223 error = EMSGSIZE; 9224 KMOD_IPSTAT_INC(ips_cantfrag); 9225 if (pd->act.rt != PF_DUPTO) { 9226 if (s && s->nat_rule != NULL) { 9227 MPASS(m0 == pd->m); 9228 PACKET_UNDO_NAT(pd, 9229 (ip->ip_hl << 2) + (ip_off & IP_OFFMASK), 9230 s); 9231 } 9232 9233 pf_send_icmp(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 9234 ifp->if_mtu, pd->af, r, pd->act.rtableid); 9235 } 9236 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9237 action = PF_DROP; 9238 goto bad; 9239 } 9240 9241 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 9242 if (error) { 9243 SDT_PROBE1(pf, ip, route_to, drop, __LINE__); 9244 action = PF_DROP; 9245 goto bad; 9246 } 9247 9248 for (; m0; m0 = m1) { 9249 m1 = m0->m_nextpkt; 9250 m0->m_nextpkt = NULL; 9251 if (error == 0) { 9252 m_clrprotoflags(m0); 9253 md = m0; 9254 pd->pf_mtag = pf_find_mtag(md); 9255 error = pf_dummynet_route(pd, s, r, ifp, 9256 gw, &md); 9257 if (md != NULL) { 9258 error = (*ifp->if_output)(ifp, md, gw, &ro); 9259 SDT_PROBE2(pf, ip, route_to, output, ifp, error); 9260 } 9261 } else 9262 m_freem(m0); 9263 } 9264 9265 if (error == 0) 9266 KMOD_IPSTAT_INC(ips_fragmented); 9267 9268 done: 9269 if (pd->act.rt != PF_DUPTO) 9270 pd->m = NULL; 9271 else 9272 action = PF_PASS; 9273 return (action); 9274 9275 bad_locked: 9276 if (s) 9277 PF_STATE_UNLOCK(s); 9278 bad: 9279 m_freem(m0); 9280 goto done; 9281 } 9282 #endif /* INET */ 9283 9284 #ifdef INET6 9285 static int 9286 pf_route6(struct pf_krule *r, struct ifnet *oifp, 9287 struct pf_kstate *s, struct pf_pdesc *pd, struct inpcb *inp) 9288 { 9289 struct mbuf *m0, *md; 9290 struct m_tag *mtag; 9291 struct sockaddr_in6 dst; 9292 struct ip6_hdr *ip6; 9293 struct ifnet *ifp = NULL; 9294 int r_dir; 9295 bool skip_test = false; 9296 int action = PF_PASS; 9297 9298 KASSERT(pd->m && r && oifp, ("%s: invalid parameters", __func__)); 9299 9300 SDT_PROBE4(pf, ip6, route_to, entry, pd->m, pd, s, oifp); 9301 9302 if (s) { 9303 r_dir = s->direction; 9304 } else { 9305 r_dir = r->direction; 9306 } 9307 9308 KASSERT(pd->dir == PF_IN || pd->dir == PF_OUT || 9309 r_dir == PF_IN || r_dir == PF_OUT, ("%s: invalid direction", 9310 __func__)); 9311 9312 if ((pd->pf_mtag == NULL && 9313 ((pd->pf_mtag = pf_get_mtag(pd->m)) == NULL)) || 9314 pd->pf_mtag->routed++ > 3) { 9315 m0 = pd->m; 9316 pd->m = NULL; 9317 action = PF_DROP; 9318 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9319 goto bad_locked; 9320 } 9321 9322 if (pd->act.rt_kif != NULL) 9323 ifp = pd->act.rt_kif->pfik_ifp; 9324 9325 if (pd->act.rt == PF_DUPTO) { 9326 if ((pd->pf_mtag->flags & PF_MTAG_FLAG_DUPLICATED)) { 9327 if (s != NULL) { 9328 PF_STATE_UNLOCK(s); 9329 } 9330 if (ifp == oifp) { 9331 /* When the 2nd interface is not skipped */ 9332 return (action); 9333 } else { 9334 m0 = pd->m; 9335 pd->m = NULL; 9336 action = PF_DROP; 9337 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9338 goto bad; 9339 } 9340 } else { 9341 pd->pf_mtag->flags |= PF_MTAG_FLAG_DUPLICATED; 9342 if (((m0 = m_dup(pd->m, M_NOWAIT)) == NULL)) { 9343 if (s) 9344 PF_STATE_UNLOCK(s); 9345 return (action); 9346 } 9347 } 9348 } else { 9349 if ((pd->act.rt == PF_REPLYTO) == (r_dir == pd->dir)) { 9350 if (pd->af == pd->naf) { 9351 pf_dummynet(pd, s, r, &pd->m); 9352 if (s) 9353 PF_STATE_UNLOCK(s); 9354 return (action); 9355 } else { 9356 if (r_dir == PF_IN) { 9357 skip_test = true; 9358 } 9359 } 9360 } 9361 9362 /* 9363 * If we're actually doing route-to and af-to and are in the 9364 * reply direction. 9365 */ 9366 if (pd->act.rt_kif && pd->act.rt_kif->pfik_ifp && 9367 pd->af != pd->naf) { 9368 if (pd->act.rt == PF_ROUTETO && r->naf != AF_INET6) { 9369 /* Un-set ifp so we do a plain route lookup. */ 9370 ifp = NULL; 9371 } 9372 if (pd->act.rt == PF_REPLYTO && r->naf != AF_INET) { 9373 /* Un-set ifp so we do a plain route lookup. */ 9374 ifp = NULL; 9375 } 9376 } 9377 m0 = pd->m; 9378 } 9379 9380 ip6 = mtod(m0, struct ip6_hdr *); 9381 9382 bzero(&dst, sizeof(dst)); 9383 dst.sin6_family = AF_INET6; 9384 dst.sin6_len = sizeof(dst); 9385 pf_addrcpy((struct pf_addr *)&dst.sin6_addr, &pd->act.rt_addr, 9386 AF_INET6); 9387 9388 if (pd->dir == PF_IN) { 9389 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 9390 if (r->rt != PF_DUPTO) 9391 pf_send_icmp(m0, ICMP6_TIME_EXCEEDED, 9392 ICMP6_TIME_EXCEED_TRANSIT, 0, pd->af, r, 9393 pd->act.rtableid); 9394 action = PF_DROP; 9395 goto bad_locked; 9396 } 9397 ip6->ip6_hlim -= IPV6_HLIMDEC; 9398 } 9399 9400 if (s != NULL) { 9401 if (ifp == NULL && (pd->af != pd->naf)) { 9402 const struct nhop_object *nh; 9403 nh = fib6_lookup(M_GETFIB(m0), &ip6->ip6_dst, 0, NHR_NONE, 0); 9404 if (nh) { 9405 ifp = nh->nh_ifp; 9406 9407 /* Use the gateway if needed. */ 9408 if (nh->nh_flags & NHF_GATEWAY) 9409 bcopy(&nh->gw6_sa.sin6_addr, &dst.sin6_addr, 9410 sizeof(dst.sin6_addr)); 9411 else 9412 dst.sin6_addr = ip6->ip6_dst; 9413 9414 /* 9415 * Bind to the correct interface if we're 9416 * if-bound. We don't know which interface 9417 * that will be until here, so we've inserted 9418 * the state on V_pf_all. Fix that now. 9419 */ 9420 if (s->kif == V_pfi_all && ifp != NULL && 9421 r->rule_flag & PFRULE_IFBOUND) 9422 s->kif = ifp->if_pf_kif; 9423 } 9424 } 9425 9426 if (r->rule_flag & PFRULE_IFBOUND && 9427 pd->act.rt == PF_REPLYTO && 9428 s->kif == V_pfi_all) { 9429 s->kif = pd->act.rt_kif; 9430 s->orig_kif = oifp->if_pf_kif; 9431 } 9432 9433 PF_STATE_UNLOCK(s); 9434 } 9435 9436 if (pd->af != pd->naf) { 9437 struct udphdr *uh = &pd->hdr.udp; 9438 9439 if (pd->proto == IPPROTO_UDP && uh->uh_sum == 0) { 9440 uh->uh_sum = in6_cksum_pseudo(ip6, 9441 ntohs(uh->uh_ulen), IPPROTO_UDP, 0); 9442 m_copyback(m0, pd->off, sizeof(*uh), pd->hdr.any); 9443 } 9444 } 9445 9446 if (ifp == NULL) { 9447 m0 = pd->m; 9448 pd->m = NULL; 9449 action = PF_DROP; 9450 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9451 goto bad; 9452 } 9453 9454 if (r->rt == PF_DUPTO) 9455 skip_test = true; 9456 9457 if (pd->dir == PF_IN && !skip_test) { 9458 if (pf_test(AF_INET6, PF_OUT, PFIL_FWD | PF_PFIL_NOREFRAGMENT, 9459 ifp, &m0, inp, &pd->act) != PF_PASS) { 9460 action = PF_DROP; 9461 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9462 goto bad; 9463 } else if (m0 == NULL) { 9464 action = PF_DROP; 9465 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9466 goto done; 9467 } 9468 if (m0->m_len < sizeof(struct ip6_hdr)) { 9469 DPFPRINTF(PF_DEBUG_URGENT, 9470 "%s: m0->m_len < sizeof(struct ip6_hdr)", 9471 __func__); 9472 action = PF_DROP; 9473 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9474 goto bad; 9475 } 9476 ip6 = mtod(m0, struct ip6_hdr *); 9477 } 9478 9479 if (ifp->if_flags & IFF_LOOPBACK) 9480 m0->m_flags |= M_SKIP_FIREWALL; 9481 9482 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 & 9483 ~ifp->if_hwassist) { 9484 uint32_t plen = m0->m_pkthdr.len - sizeof(*ip6); 9485 in6_delayed_cksum(m0, plen, sizeof(struct ip6_hdr)); 9486 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 9487 } 9488 9489 if (pd->dir == PF_IN) { 9490 uint16_t tmp; 9491 /* 9492 * Make sure dummynet gets the correct direction, in case it needs to 9493 * re-inject later. 9494 */ 9495 pd->dir = PF_OUT; 9496 9497 /* 9498 * The following processing is actually the rest of the inbound processing, even 9499 * though we've marked it as outbound (so we don't look through dummynet) and it 9500 * happens after the outbound processing (pf_test(PF_OUT) above). 9501 * Swap the dummynet pipe numbers, because it's going to come to the wrong 9502 * conclusion about what direction it's processing, and we can't fix it or it 9503 * will re-inject incorrectly. Swapping the pipe numbers means that its incorrect 9504 * decision will pick the right pipe, and everything will mostly work as expected. 9505 */ 9506 tmp = pd->act.dnrpipe; 9507 pd->act.dnrpipe = pd->act.dnpipe; 9508 pd->act.dnpipe = tmp; 9509 } 9510 9511 /* 9512 * If the packet is too large for the outgoing interface, 9513 * send back an icmp6 error. 9514 */ 9515 if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 9516 dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 9517 mtag = m_tag_find(m0, PACKET_TAG_PF_REASSEMBLED, NULL); 9518 if (mtag != NULL) { 9519 int ret __sdt_used; 9520 ret = pf_refragment6(ifp, &m0, mtag, ifp, true); 9521 SDT_PROBE2(pf, ip6, route_to, output, ifp, ret); 9522 goto done; 9523 } 9524 9525 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) { 9526 md = m0; 9527 pf_dummynet_route(pd, s, r, ifp, sintosa(&dst), &md); 9528 if (md != NULL) { 9529 int ret __sdt_used; 9530 ret = nd6_output_ifp(ifp, ifp, md, &dst, NULL); 9531 SDT_PROBE2(pf, ip6, route_to, output, ifp, ret); 9532 } 9533 } 9534 else { 9535 in6_ifstat_inc(ifp, ifs6_in_toobig); 9536 if (pd->act.rt != PF_DUPTO) { 9537 if (s && s->nat_rule != NULL) { 9538 MPASS(m0 == pd->m); 9539 PACKET_UNDO_NAT(pd, 9540 ((caddr_t)ip6 - m0->m_data) + 9541 sizeof(struct ip6_hdr), s); 9542 } 9543 9544 if (r->rt != PF_DUPTO) 9545 pf_send_icmp(m0, ICMP6_PACKET_TOO_BIG, 0, 9546 ifp->if_mtu, pd->af, r, pd->act.rtableid); 9547 } 9548 action = PF_DROP; 9549 SDT_PROBE1(pf, ip6, route_to, drop, __LINE__); 9550 goto bad; 9551 } 9552 9553 done: 9554 if (pd->act.rt != PF_DUPTO) 9555 pd->m = NULL; 9556 else 9557 action = PF_PASS; 9558 return (action); 9559 9560 bad_locked: 9561 if (s) 9562 PF_STATE_UNLOCK(s); 9563 bad: 9564 m_freem(m0); 9565 goto done; 9566 } 9567 #endif /* INET6 */ 9568 9569 /* 9570 * FreeBSD supports cksum offloads for the following drivers. 9571 * em(4), fxp(4), lge(4), nge(4), re(4), ti(4), txp(4), xl(4) 9572 * 9573 * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 9574 * network driver performed cksum including pseudo header, need to verify 9575 * csum_data 9576 * CSUM_DATA_VALID : 9577 * network driver performed cksum, needs to additional pseudo header 9578 * cksum computation with partial csum_data(i.e. lack of H/W support for 9579 * pseudo header, for instance sk(4) and possibly gem(4)) 9580 * 9581 * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 9582 * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 9583 * TCP/UDP layer. 9584 * Also, set csum_data to 0xffff to force cksum validation. 9585 */ 9586 static int 9587 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 9588 { 9589 u_int16_t sum = 0; 9590 int hw_assist = 0; 9591 struct ip *ip; 9592 9593 if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 9594 return (1); 9595 if (m->m_pkthdr.len < off + len) 9596 return (1); 9597 9598 switch (p) { 9599 case IPPROTO_TCP: 9600 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 9601 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 9602 sum = m->m_pkthdr.csum_data; 9603 } else { 9604 ip = mtod(m, struct ip *); 9605 sum = in_pseudo(ip->ip_src.s_addr, 9606 ip->ip_dst.s_addr, htonl((u_short)len + 9607 m->m_pkthdr.csum_data + IPPROTO_TCP)); 9608 } 9609 sum ^= 0xffff; 9610 ++hw_assist; 9611 } 9612 break; 9613 case IPPROTO_UDP: 9614 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 9615 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 9616 sum = m->m_pkthdr.csum_data; 9617 } else { 9618 ip = mtod(m, struct ip *); 9619 sum = in_pseudo(ip->ip_src.s_addr, 9620 ip->ip_dst.s_addr, htonl((u_short)len + 9621 m->m_pkthdr.csum_data + IPPROTO_UDP)); 9622 } 9623 sum ^= 0xffff; 9624 ++hw_assist; 9625 } 9626 break; 9627 case IPPROTO_ICMP: 9628 #ifdef INET6 9629 case IPPROTO_ICMPV6: 9630 #endif /* INET6 */ 9631 break; 9632 default: 9633 return (1); 9634 } 9635 9636 if (!hw_assist) { 9637 switch (af) { 9638 case AF_INET: 9639 if (m->m_len < sizeof(struct ip)) 9640 return (1); 9641 sum = in4_cksum(m, (p == IPPROTO_ICMP ? 0 : p), off, len); 9642 break; 9643 #ifdef INET6 9644 case AF_INET6: 9645 if (m->m_len < sizeof(struct ip6_hdr)) 9646 return (1); 9647 sum = in6_cksum(m, p, off, len); 9648 break; 9649 #endif /* INET6 */ 9650 } 9651 } 9652 if (sum) { 9653 switch (p) { 9654 case IPPROTO_TCP: 9655 { 9656 KMOD_TCPSTAT_INC(tcps_rcvbadsum); 9657 break; 9658 } 9659 case IPPROTO_UDP: 9660 { 9661 KMOD_UDPSTAT_INC(udps_badsum); 9662 break; 9663 } 9664 #ifdef INET 9665 case IPPROTO_ICMP: 9666 { 9667 KMOD_ICMPSTAT_INC(icps_checksum); 9668 break; 9669 } 9670 #endif 9671 #ifdef INET6 9672 case IPPROTO_ICMPV6: 9673 { 9674 KMOD_ICMP6STAT_INC(icp6s_checksum); 9675 break; 9676 } 9677 #endif /* INET6 */ 9678 } 9679 return (1); 9680 } else { 9681 if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 9682 m->m_pkthdr.csum_flags |= 9683 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 9684 m->m_pkthdr.csum_data = 0xffff; 9685 } 9686 } 9687 return (0); 9688 } 9689 9690 static bool 9691 pf_pdesc_to_dnflow(const struct pf_pdesc *pd, const struct pf_krule *r, 9692 const struct pf_kstate *s, struct ip_fw_args *dnflow) 9693 { 9694 int dndir = r->direction; 9695 9696 if (s && dndir == PF_INOUT) { 9697 dndir = s->direction; 9698 } else if (dndir == PF_INOUT) { 9699 /* Assume primary direction. Happens when we've set dnpipe in 9700 * the ethernet level code. */ 9701 dndir = pd->dir; 9702 } 9703 9704 if (pd->pf_mtag->flags & PF_MTAG_FLAG_DUMMYNETED) 9705 return (false); 9706 9707 memset(dnflow, 0, sizeof(*dnflow)); 9708 9709 if (pd->dport != NULL) 9710 dnflow->f_id.dst_port = ntohs(*pd->dport); 9711 if (pd->sport != NULL) 9712 dnflow->f_id.src_port = ntohs(*pd->sport); 9713 9714 if (pd->dir == PF_IN) 9715 dnflow->flags |= IPFW_ARGS_IN; 9716 else 9717 dnflow->flags |= IPFW_ARGS_OUT; 9718 9719 if (pd->dir != dndir && pd->act.dnrpipe) { 9720 dnflow->rule.info = pd->act.dnrpipe; 9721 } 9722 else if (pd->dir == dndir && pd->act.dnpipe) { 9723 dnflow->rule.info = pd->act.dnpipe; 9724 } 9725 else { 9726 return (false); 9727 } 9728 9729 dnflow->rule.info |= IPFW_IS_DUMMYNET; 9730 if (r->free_flags & PFRULE_DN_IS_PIPE || pd->act.flags & PFSTATE_DN_IS_PIPE) 9731 dnflow->rule.info |= IPFW_IS_PIPE; 9732 9733 dnflow->f_id.proto = pd->proto; 9734 dnflow->f_id.extra = dnflow->rule.info; 9735 switch (pd->naf) { 9736 case AF_INET: 9737 dnflow->f_id.addr_type = 4; 9738 dnflow->f_id.src_ip = ntohl(pd->src->v4.s_addr); 9739 dnflow->f_id.dst_ip = ntohl(pd->dst->v4.s_addr); 9740 break; 9741 case AF_INET6: 9742 dnflow->flags |= IPFW_ARGS_IP6; 9743 dnflow->f_id.addr_type = 6; 9744 dnflow->f_id.src_ip6 = pd->src->v6; 9745 dnflow->f_id.dst_ip6 = pd->dst->v6; 9746 break; 9747 } 9748 9749 return (true); 9750 } 9751 9752 int 9753 pf_test_eth(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, 9754 struct inpcb *inp) 9755 { 9756 struct pfi_kkif *kif; 9757 struct mbuf *m = *m0; 9758 9759 M_ASSERTPKTHDR(m); 9760 MPASS(ifp->if_vnet == curvnet); 9761 NET_EPOCH_ASSERT(); 9762 9763 if (!V_pf_status.running) 9764 return (PF_PASS); 9765 9766 kif = (struct pfi_kkif *)ifp->if_pf_kif; 9767 9768 if (kif == NULL) { 9769 DPFPRINTF(PF_DEBUG_URGENT, 9770 "%s: kif == NULL, if_xname %s", __func__, ifp->if_xname); 9771 return (PF_DROP); 9772 } 9773 if (kif->pfik_flags & PFI_IFLAG_SKIP) 9774 return (PF_PASS); 9775 9776 if (m->m_flags & M_SKIP_FIREWALL) 9777 return (PF_PASS); 9778 9779 if (__predict_false(! M_WRITABLE(*m0))) { 9780 m = *m0 = m_unshare(*m0, M_NOWAIT); 9781 if (*m0 == NULL) 9782 return (PF_DROP); 9783 } 9784 9785 /* Stateless! */ 9786 return (pf_test_eth_rule(dir, kif, m0)); 9787 } 9788 9789 static __inline void 9790 pf_dummynet_flag_remove(struct mbuf *m, struct pf_mtag *pf_mtag) 9791 { 9792 struct m_tag *mtag; 9793 9794 pf_mtag->flags &= ~PF_MTAG_FLAG_DUMMYNET; 9795 9796 /* dummynet adds this tag, but pf does not need it, 9797 * and keeping it creates unexpected behavior, 9798 * e.g. in case of divert(4) usage right after dummynet. */ 9799 mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 9800 if (mtag != NULL) 9801 m_tag_delete(m, mtag); 9802 } 9803 9804 static int 9805 pf_dummynet(struct pf_pdesc *pd, struct pf_kstate *s, 9806 struct pf_krule *r, struct mbuf **m0) 9807 { 9808 return (pf_dummynet_route(pd, s, r, NULL, NULL, m0)); 9809 } 9810 9811 static int 9812 pf_dummynet_route(struct pf_pdesc *pd, struct pf_kstate *s, 9813 struct pf_krule *r, struct ifnet *ifp, const struct sockaddr *sa, 9814 struct mbuf **m0) 9815 { 9816 struct ip_fw_args dnflow; 9817 9818 NET_EPOCH_ASSERT(); 9819 9820 if (pd->act.dnpipe == 0 && pd->act.dnrpipe == 0) 9821 return (0); 9822 9823 if (ip_dn_io_ptr == NULL) { 9824 m_freem(*m0); 9825 *m0 = NULL; 9826 return (ENOMEM); 9827 } 9828 9829 if (pd->pf_mtag == NULL && 9830 ((pd->pf_mtag = pf_get_mtag(*m0)) == NULL)) { 9831 m_freem(*m0); 9832 *m0 = NULL; 9833 return (ENOMEM); 9834 } 9835 9836 if (ifp != NULL) { 9837 pd->pf_mtag->flags |= PF_MTAG_FLAG_ROUTE_TO; 9838 9839 pd->pf_mtag->if_index = ifp->if_index; 9840 pd->pf_mtag->if_idxgen = ifp->if_idxgen; 9841 9842 MPASS(sa != NULL); 9843 9844 switch (sa->sa_family) { 9845 case AF_INET: 9846 memcpy(&pd->pf_mtag->dst, sa, 9847 sizeof(struct sockaddr_in)); 9848 break; 9849 case AF_INET6: 9850 memcpy(&pd->pf_mtag->dst, sa, 9851 sizeof(struct sockaddr_in6)); 9852 break; 9853 } 9854 } 9855 9856 if (s != NULL && s->nat_rule != NULL && 9857 s->nat_rule->action == PF_RDR && 9858 ( 9859 #ifdef INET 9860 (pd->af == AF_INET && IN_LOOPBACK(ntohl(pd->dst->v4.s_addr))) || 9861 #endif /* INET */ 9862 (pd->af == AF_INET6 && IN6_IS_ADDR_LOOPBACK(&pd->dst->v6)))) { 9863 /* 9864 * If we're redirecting to loopback mark this packet 9865 * as being local. Otherwise it might get dropped 9866 * if dummynet re-injects. 9867 */ 9868 (*m0)->m_pkthdr.rcvif = V_loif; 9869 } 9870 9871 if (pf_pdesc_to_dnflow(pd, r, s, &dnflow)) { 9872 pd->pf_mtag->flags |= PF_MTAG_FLAG_DUMMYNET; 9873 pd->pf_mtag->flags |= PF_MTAG_FLAG_DUMMYNETED; 9874 ip_dn_io_ptr(m0, &dnflow); 9875 if (*m0 != NULL) { 9876 pd->pf_mtag->flags &= ~PF_MTAG_FLAG_ROUTE_TO; 9877 pf_dummynet_flag_remove(*m0, pd->pf_mtag); 9878 } 9879 } 9880 9881 return (0); 9882 } 9883 9884 static int 9885 pf_walk_option(struct pf_pdesc *pd, struct ip *h, int off, int end, 9886 u_short *reason) 9887 { 9888 uint8_t type, length, opts[15 * 4 - sizeof(struct ip)]; 9889 9890 /* IP header in payload of ICMP packet may be too short */ 9891 if (pd->m->m_pkthdr.len < end) { 9892 DPFPRINTF(PF_DEBUG_MISC, "IP option too short"); 9893 REASON_SET(reason, PFRES_SHORT); 9894 return (PF_DROP); 9895 } 9896 9897 MPASS(end - off <= sizeof(opts)); 9898 m_copydata(pd->m, off, end - off, opts); 9899 end -= off; 9900 off = 0; 9901 9902 while (off < end) { 9903 type = opts[off]; 9904 if (type == IPOPT_EOL) 9905 break; 9906 if (type == IPOPT_NOP) { 9907 off++; 9908 continue; 9909 } 9910 if (off + 2 > end) { 9911 DPFPRINTF(PF_DEBUG_MISC, "IP length opt"); 9912 REASON_SET(reason, PFRES_IPOPTIONS); 9913 return (PF_DROP); 9914 } 9915 length = opts[off + 1]; 9916 if (length < 2) { 9917 DPFPRINTF(PF_DEBUG_MISC, "IP short opt"); 9918 REASON_SET(reason, PFRES_IPOPTIONS); 9919 return (PF_DROP); 9920 } 9921 if (off + length > end) { 9922 DPFPRINTF(PF_DEBUG_MISC, "IP long opt"); 9923 REASON_SET(reason, PFRES_IPOPTIONS); 9924 return (PF_DROP); 9925 } 9926 switch (type) { 9927 case IPOPT_RA: 9928 pd->badopts |= PF_OPT_ROUTER_ALERT; 9929 break; 9930 default: 9931 pd->badopts |= PF_OPT_OTHER; 9932 break; 9933 } 9934 off += length; 9935 } 9936 9937 return (PF_PASS); 9938 } 9939 9940 static int 9941 pf_walk_header(struct pf_pdesc *pd, struct ip *h, u_short *reason) 9942 { 9943 struct ah ext; 9944 u_int32_t hlen, end; 9945 int hdr_cnt; 9946 9947 hlen = h->ip_hl << 2; 9948 if (hlen < sizeof(struct ip) || hlen > ntohs(h->ip_len)) { 9949 REASON_SET(reason, PFRES_SHORT); 9950 return (PF_DROP); 9951 } 9952 if (hlen != sizeof(struct ip)) { 9953 if (pf_walk_option(pd, h, pd->off + sizeof(struct ip), 9954 pd->off + hlen, reason) != PF_PASS) 9955 return (PF_DROP); 9956 /* header options which contain only padding is fishy */ 9957 if (pd->badopts == 0) 9958 pd->badopts |= PF_OPT_OTHER; 9959 } 9960 end = pd->off + ntohs(h->ip_len); 9961 pd->off += hlen; 9962 pd->proto = h->ip_p; 9963 /* IGMP packets have router alert options, allow them */ 9964 if (pd->proto == IPPROTO_IGMP) { 9965 /* According to RFC 1112 ttl must be set to 1. */ 9966 if ((h->ip_ttl != 1) || 9967 !IN_MULTICAST(ntohl(h->ip_dst.s_addr))) { 9968 DPFPRINTF(PF_DEBUG_MISC, "Invalid IGMP"); 9969 REASON_SET(reason, PFRES_IPOPTIONS); 9970 return (PF_DROP); 9971 } 9972 pd->badopts &= ~PF_OPT_ROUTER_ALERT; 9973 } 9974 /* stop walking over non initial fragments */ 9975 if ((h->ip_off & htons(IP_OFFMASK)) != 0) 9976 return (PF_PASS); 9977 for (hdr_cnt = 0; hdr_cnt < PF_HDR_LIMIT; hdr_cnt++) { 9978 switch (pd->proto) { 9979 case IPPROTO_AH: 9980 /* fragments may be short */ 9981 if ((h->ip_off & htons(IP_MF | IP_OFFMASK)) != 0 && 9982 end < pd->off + sizeof(ext)) 9983 return (PF_PASS); 9984 if (!pf_pull_hdr(pd->m, pd->off, &ext, sizeof(ext), 9985 NULL, reason, AF_INET)) { 9986 DPFPRINTF(PF_DEBUG_MISC, "IP short exthdr"); 9987 return (PF_DROP); 9988 } 9989 pd->off += (ext.ah_len + 2) * 4; 9990 pd->proto = ext.ah_nxt; 9991 break; 9992 default: 9993 return (PF_PASS); 9994 } 9995 } 9996 DPFPRINTF(PF_DEBUG_MISC, "IPv4 nested authentication header limit"); 9997 REASON_SET(reason, PFRES_IPOPTIONS); 9998 return (PF_DROP); 9999 } 10000 10001 #ifdef INET6 10002 static int 10003 pf_walk_option6(struct pf_pdesc *pd, struct ip6_hdr *h, int off, int end, 10004 u_short *reason) 10005 { 10006 struct ip6_opt opt; 10007 struct ip6_opt_jumbo jumbo; 10008 10009 while (off < end) { 10010 if (!pf_pull_hdr(pd->m, off, &opt.ip6o_type, 10011 sizeof(opt.ip6o_type), NULL, reason, AF_INET6)) { 10012 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short opt type"); 10013 return (PF_DROP); 10014 } 10015 if (opt.ip6o_type == IP6OPT_PAD1) { 10016 off++; 10017 continue; 10018 } 10019 if (!pf_pull_hdr(pd->m, off, &opt, sizeof(opt), NULL, 10020 reason, AF_INET6)) { 10021 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short opt"); 10022 return (PF_DROP); 10023 } 10024 if (off + sizeof(opt) + opt.ip6o_len > end) { 10025 DPFPRINTF(PF_DEBUG_MISC, "IPv6 long opt"); 10026 REASON_SET(reason, PFRES_IPOPTIONS); 10027 return (PF_DROP); 10028 } 10029 switch (opt.ip6o_type) { 10030 case IP6OPT_PADN: 10031 break; 10032 case IP6OPT_JUMBO: 10033 pd->badopts |= PF_OPT_JUMBO; 10034 if (pd->jumbolen != 0) { 10035 DPFPRINTF(PF_DEBUG_MISC, "IPv6 multiple jumbo"); 10036 REASON_SET(reason, PFRES_IPOPTIONS); 10037 return (PF_DROP); 10038 } 10039 if (ntohs(h->ip6_plen) != 0) { 10040 DPFPRINTF(PF_DEBUG_MISC, "IPv6 bad jumbo plen"); 10041 REASON_SET(reason, PFRES_IPOPTIONS); 10042 return (PF_DROP); 10043 } 10044 if (!pf_pull_hdr(pd->m, off, &jumbo, sizeof(jumbo), NULL, 10045 reason, AF_INET6)) { 10046 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short jumbo"); 10047 return (PF_DROP); 10048 } 10049 memcpy(&pd->jumbolen, jumbo.ip6oj_jumbo_len, 10050 sizeof(pd->jumbolen)); 10051 pd->jumbolen = ntohl(pd->jumbolen); 10052 if (pd->jumbolen < IPV6_MAXPACKET) { 10053 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short jumbolen"); 10054 REASON_SET(reason, PFRES_IPOPTIONS); 10055 return (PF_DROP); 10056 } 10057 break; 10058 case IP6OPT_ROUTER_ALERT: 10059 pd->badopts |= PF_OPT_ROUTER_ALERT; 10060 break; 10061 default: 10062 pd->badopts |= PF_OPT_OTHER; 10063 break; 10064 } 10065 off += sizeof(opt) + opt.ip6o_len; 10066 } 10067 10068 return (PF_PASS); 10069 } 10070 10071 int 10072 pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason) 10073 { 10074 struct ip6_frag frag; 10075 struct ip6_ext ext; 10076 struct icmp6_hdr icmp6; 10077 struct ip6_rthdr rthdr; 10078 uint32_t end; 10079 int hdr_cnt, fraghdr_cnt = 0, rthdr_cnt = 0; 10080 10081 pd->off += sizeof(struct ip6_hdr); 10082 end = pd->off + ntohs(h->ip6_plen); 10083 pd->fragoff = pd->extoff = pd->jumbolen = 0; 10084 pd->proto = h->ip6_nxt; 10085 for (hdr_cnt = 0; hdr_cnt < PF_HDR_LIMIT; hdr_cnt++) { 10086 switch (pd->proto) { 10087 case IPPROTO_ROUTING: 10088 case IPPROTO_DSTOPTS: 10089 pd->badopts |= PF_OPT_OTHER; 10090 break; 10091 case IPPROTO_HOPOPTS: 10092 if (!pf_pull_hdr(pd->m, pd->off, &ext, sizeof(ext), 10093 NULL, reason, AF_INET6)) { 10094 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short exthdr"); 10095 return (PF_DROP); 10096 } 10097 if (pf_walk_option6(pd, h, pd->off + sizeof(ext), 10098 pd->off + (ext.ip6e_len + 1) * 8, 10099 reason) != PF_PASS) 10100 return (PF_DROP); 10101 /* option header which contains only padding is fishy */ 10102 if (pd->badopts == 0) 10103 pd->badopts |= PF_OPT_OTHER; 10104 break; 10105 } 10106 switch (pd->proto) { 10107 case IPPROTO_FRAGMENT: 10108 if (fraghdr_cnt++) { 10109 DPFPRINTF(PF_DEBUG_MISC, "IPv6 multiple fragment"); 10110 REASON_SET(reason, PFRES_FRAG); 10111 return (PF_DROP); 10112 } 10113 /* jumbo payload packets cannot be fragmented */ 10114 if (pd->jumbolen != 0) { 10115 DPFPRINTF(PF_DEBUG_MISC, "IPv6 fragmented jumbo"); 10116 REASON_SET(reason, PFRES_FRAG); 10117 return (PF_DROP); 10118 } 10119 if (!pf_pull_hdr(pd->m, pd->off, &frag, sizeof(frag), 10120 NULL, reason, AF_INET6)) { 10121 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short fragment"); 10122 return (PF_DROP); 10123 } 10124 /* stop walking over non initial fragments */ 10125 if (ntohs((frag.ip6f_offlg & IP6F_OFF_MASK)) != 0) { 10126 pd->fragoff = pd->off; 10127 return (PF_PASS); 10128 } 10129 /* RFC6946: reassemble only non atomic fragments */ 10130 if (frag.ip6f_offlg & IP6F_MORE_FRAG) 10131 pd->fragoff = pd->off; 10132 pd->off += sizeof(frag); 10133 pd->proto = frag.ip6f_nxt; 10134 break; 10135 case IPPROTO_ROUTING: 10136 if (rthdr_cnt++) { 10137 DPFPRINTF(PF_DEBUG_MISC, "IPv6 multiple rthdr"); 10138 REASON_SET(reason, PFRES_IPOPTIONS); 10139 return (PF_DROP); 10140 } 10141 /* fragments may be short */ 10142 if (pd->fragoff != 0 && end < pd->off + sizeof(rthdr)) { 10143 pd->off = pd->fragoff; 10144 pd->proto = IPPROTO_FRAGMENT; 10145 return (PF_PASS); 10146 } 10147 if (!pf_pull_hdr(pd->m, pd->off, &rthdr, sizeof(rthdr), 10148 NULL, reason, AF_INET6)) { 10149 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short rthdr"); 10150 return (PF_DROP); 10151 } 10152 if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 10153 DPFPRINTF(PF_DEBUG_MISC, "IPv6 rthdr0"); 10154 REASON_SET(reason, PFRES_IPOPTIONS); 10155 return (PF_DROP); 10156 } 10157 /* FALLTHROUGH */ 10158 case IPPROTO_HOPOPTS: 10159 /* RFC2460 4.1: Hop-by-Hop only after IPv6 header */ 10160 if (pd->proto == IPPROTO_HOPOPTS && hdr_cnt > 0) { 10161 DPFPRINTF(PF_DEBUG_MISC, "IPv6 hopopts not first"); 10162 REASON_SET(reason, PFRES_IPOPTIONS); 10163 return (PF_DROP); 10164 } 10165 /* FALLTHROUGH */ 10166 case IPPROTO_AH: 10167 case IPPROTO_DSTOPTS: 10168 if (!pf_pull_hdr(pd->m, pd->off, &ext, sizeof(ext), 10169 NULL, reason, AF_INET6)) { 10170 DPFPRINTF(PF_DEBUG_MISC, "IPv6 short exthdr"); 10171 return (PF_DROP); 10172 } 10173 /* fragments may be short */ 10174 if (pd->fragoff != 0 && end < pd->off + sizeof(ext)) { 10175 pd->off = pd->fragoff; 10176 pd->proto = IPPROTO_FRAGMENT; 10177 return (PF_PASS); 10178 } 10179 /* reassembly needs the ext header before the frag */ 10180 if (pd->fragoff == 0) 10181 pd->extoff = pd->off; 10182 if (pd->proto == IPPROTO_HOPOPTS && pd->fragoff == 0 && 10183 ntohs(h->ip6_plen) == 0 && pd->jumbolen != 0) { 10184 DPFPRINTF(PF_DEBUG_MISC, "IPv6 missing jumbo"); 10185 REASON_SET(reason, PFRES_IPOPTIONS); 10186 return (PF_DROP); 10187 } 10188 if (pd->proto == IPPROTO_AH) 10189 pd->off += (ext.ip6e_len + 2) * 4; 10190 else 10191 pd->off += (ext.ip6e_len + 1) * 8; 10192 pd->proto = ext.ip6e_nxt; 10193 break; 10194 case IPPROTO_ICMPV6: 10195 /* fragments may be short, ignore inner header then */ 10196 if (pd->fragoff != 0 && end < pd->off + sizeof(icmp6)) { 10197 pd->off = pd->fragoff; 10198 pd->proto = IPPROTO_FRAGMENT; 10199 return (PF_PASS); 10200 } 10201 if (!pf_pull_hdr(pd->m, pd->off, &icmp6, sizeof(icmp6), 10202 NULL, reason, AF_INET6)) { 10203 DPFPRINTF(PF_DEBUG_MISC, 10204 "IPv6 short icmp6hdr"); 10205 return (PF_DROP); 10206 } 10207 /* ICMP multicast packets have router alert options */ 10208 switch (icmp6.icmp6_type) { 10209 case MLD_LISTENER_QUERY: 10210 case MLD_LISTENER_REPORT: 10211 case MLD_LISTENER_DONE: 10212 case MLDV2_LISTENER_REPORT: 10213 /* 10214 * According to RFC 2710 all MLD messages are 10215 * sent with hop-limit (ttl) set to 1, and link 10216 * local source address. If either one is 10217 * missing then MLD message is invalid and 10218 * should be discarded. 10219 */ 10220 if ((h->ip6_hlim != 1) || 10221 !IN6_IS_ADDR_LINKLOCAL(&h->ip6_src)) { 10222 DPFPRINTF(PF_DEBUG_MISC, "Invalid MLD"); 10223 REASON_SET(reason, PFRES_IPOPTIONS); 10224 return (PF_DROP); 10225 } 10226 pd->badopts &= ~PF_OPT_ROUTER_ALERT; 10227 break; 10228 } 10229 return (PF_PASS); 10230 case IPPROTO_TCP: 10231 case IPPROTO_UDP: 10232 case IPPROTO_SCTP: 10233 /* fragments may be short, ignore inner header then */ 10234 if (pd->fragoff != 0 && end < pd->off + 10235 (pd->proto == IPPROTO_TCP ? sizeof(struct tcphdr) : 10236 pd->proto == IPPROTO_UDP ? sizeof(struct udphdr) : 10237 pd->proto == IPPROTO_SCTP ? sizeof(struct sctphdr) : 10238 sizeof(struct icmp6_hdr))) { 10239 pd->off = pd->fragoff; 10240 pd->proto = IPPROTO_FRAGMENT; 10241 } 10242 /* FALLTHROUGH */ 10243 default: 10244 return (PF_PASS); 10245 } 10246 } 10247 DPFPRINTF(PF_DEBUG_MISC, "IPv6 nested extension header limit"); 10248 REASON_SET(reason, PFRES_IPOPTIONS); 10249 return (PF_DROP); 10250 } 10251 #endif /* INET6 */ 10252 10253 static void 10254 pf_init_pdesc(struct pf_pdesc *pd, struct mbuf *m) 10255 { 10256 memset(pd, 0, sizeof(*pd)); 10257 pd->pf_mtag = pf_find_mtag(m); 10258 pd->m = m; 10259 } 10260 10261 static int 10262 pf_setup_pdesc(sa_family_t af, int dir, struct pf_pdesc *pd, struct mbuf **m0, 10263 u_short *action, u_short *reason, struct pfi_kkif *kif, 10264 struct pf_rule_actions *default_actions) 10265 { 10266 pd->dir = dir; 10267 pd->kif = kif; 10268 pd->m = *m0; 10269 pd->sidx = (dir == PF_IN) ? 0 : 1; 10270 pd->didx = (dir == PF_IN) ? 1 : 0; 10271 pd->af = pd->naf = af; 10272 10273 PF_RULES_ASSERT(); 10274 10275 TAILQ_INIT(&pd->sctp_multihome_jobs); 10276 if (default_actions != NULL) 10277 memcpy(&pd->act, default_actions, sizeof(pd->act)); 10278 10279 if (pd->pf_mtag && pd->pf_mtag->dnpipe) { 10280 pd->act.dnpipe = pd->pf_mtag->dnpipe; 10281 pd->act.flags = pd->pf_mtag->dnflags; 10282 } 10283 10284 switch (af) { 10285 #ifdef INET 10286 case AF_INET: { 10287 struct ip *h; 10288 10289 if (__predict_false((*m0)->m_len < sizeof(struct ip)) && 10290 (pd->m = *m0 = m_pullup(*m0, sizeof(struct ip))) == NULL) { 10291 DPFPRINTF(PF_DEBUG_URGENT, 10292 "%s: m_len < sizeof(struct ip), pullup failed", 10293 __func__); 10294 *action = PF_DROP; 10295 REASON_SET(reason, PFRES_SHORT); 10296 return (-1); 10297 } 10298 10299 h = mtod(pd->m, struct ip *); 10300 if (pd->m->m_pkthdr.len < ntohs(h->ip_len)) { 10301 *action = PF_DROP; 10302 REASON_SET(reason, PFRES_SHORT); 10303 return (-1); 10304 } 10305 10306 if (pf_normalize_ip(reason, pd) != PF_PASS) { 10307 /* We do IP header normalization and packet reassembly here */ 10308 *m0 = pd->m; 10309 *action = PF_DROP; 10310 return (-1); 10311 } 10312 *m0 = pd->m; 10313 h = mtod(pd->m, struct ip *); 10314 10315 if (pf_walk_header(pd, h, reason) != PF_PASS) { 10316 *action = PF_DROP; 10317 return (-1); 10318 } 10319 10320 pd->src = (struct pf_addr *)&h->ip_src; 10321 pd->dst = (struct pf_addr *)&h->ip_dst; 10322 pf_addrcpy(&pd->osrc, pd->src, af); 10323 pf_addrcpy(&pd->odst, pd->dst, af); 10324 pd->ip_sum = &h->ip_sum; 10325 pd->tos = h->ip_tos & ~IPTOS_ECN_MASK; 10326 pd->ttl = h->ip_ttl; 10327 pd->tot_len = ntohs(h->ip_len); 10328 pd->act.rtableid = -1; 10329 pd->df = h->ip_off & htons(IP_DF); 10330 pd->virtual_proto = (h->ip_off & htons(IP_MF | IP_OFFMASK)) ? 10331 PF_VPROTO_FRAGMENT : pd->proto; 10332 10333 break; 10334 } 10335 #endif /* INET */ 10336 #ifdef INET6 10337 case AF_INET6: { 10338 struct ip6_hdr *h; 10339 10340 if (__predict_false((*m0)->m_len < sizeof(struct ip6_hdr)) && 10341 (pd->m = *m0 = m_pullup(*m0, sizeof(struct ip6_hdr))) == NULL) { 10342 DPFPRINTF(PF_DEBUG_URGENT, 10343 "%s: m_len < sizeof(struct ip6_hdr)" 10344 ", pullup failed", __func__); 10345 *action = PF_DROP; 10346 REASON_SET(reason, PFRES_SHORT); 10347 return (-1); 10348 } 10349 10350 h = mtod(pd->m, struct ip6_hdr *); 10351 if (pd->m->m_pkthdr.len < 10352 sizeof(struct ip6_hdr) + ntohs(h->ip6_plen)) { 10353 *action = PF_DROP; 10354 REASON_SET(reason, PFRES_SHORT); 10355 return (-1); 10356 } 10357 10358 /* 10359 * we do not support jumbogram. if we keep going, zero ip6_plen 10360 * will do something bad, so drop the packet for now. 10361 */ 10362 if (htons(h->ip6_plen) == 0) { 10363 *action = PF_DROP; 10364 return (-1); 10365 } 10366 10367 if (pf_walk_header6(pd, h, reason) != PF_PASS) { 10368 *action = PF_DROP; 10369 return (-1); 10370 } 10371 10372 h = mtod(pd->m, struct ip6_hdr *); 10373 pd->src = (struct pf_addr *)&h->ip6_src; 10374 pd->dst = (struct pf_addr *)&h->ip6_dst; 10375 pf_addrcpy(&pd->osrc, pd->src, af); 10376 pf_addrcpy(&pd->odst, pd->dst, af); 10377 pd->ip_sum = NULL; 10378 pd->tos = IPV6_DSCP(h); 10379 pd->ttl = h->ip6_hlim; 10380 pd->tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 10381 pd->act.rtableid = -1; 10382 10383 pd->virtual_proto = (pd->fragoff != 0) ? 10384 PF_VPROTO_FRAGMENT : pd->proto; 10385 10386 /* We do IP header normalization and packet reassembly here */ 10387 if (pf_normalize_ip6(pd->fragoff, reason, pd) != 10388 PF_PASS) { 10389 *m0 = pd->m; 10390 *action = PF_DROP; 10391 return (-1); 10392 } 10393 *m0 = pd->m; 10394 if (pd->m == NULL) { 10395 /* packet sits in reassembly queue, no error */ 10396 *action = PF_PASS; 10397 return (-1); 10398 } 10399 10400 /* Update pointers into the packet. */ 10401 h = mtod(pd->m, struct ip6_hdr *); 10402 pd->src = (struct pf_addr *)&h->ip6_src; 10403 pd->dst = (struct pf_addr *)&h->ip6_dst; 10404 10405 pd->off = 0; 10406 10407 if (pf_walk_header6(pd, h, reason) != PF_PASS) { 10408 *action = PF_DROP; 10409 return (-1); 10410 } 10411 10412 if (m_tag_find(pd->m, PACKET_TAG_PF_REASSEMBLED, NULL) != NULL) { 10413 /* 10414 * Reassembly may have changed the next protocol from 10415 * fragment to something else, so update. 10416 */ 10417 pd->virtual_proto = pd->proto; 10418 MPASS(pd->fragoff == 0); 10419 } 10420 10421 if (pd->fragoff != 0) 10422 pd->virtual_proto = PF_VPROTO_FRAGMENT; 10423 10424 break; 10425 } 10426 #endif /* INET6 */ 10427 default: 10428 panic("pf_setup_pdesc called with illegal af %u", af); 10429 } 10430 10431 switch (pd->virtual_proto) { 10432 case IPPROTO_TCP: { 10433 struct tcphdr *th = &pd->hdr.tcp; 10434 10435 if (!pf_pull_hdr(pd->m, pd->off, th, sizeof(*th), action, 10436 reason, af)) { 10437 *action = PF_DROP; 10438 REASON_SET(reason, PFRES_SHORT); 10439 return (-1); 10440 } 10441 pd->hdrlen = sizeof(*th); 10442 pd->p_len = pd->tot_len - pd->off - (th->th_off << 2); 10443 pd->sport = &th->th_sport; 10444 pd->dport = &th->th_dport; 10445 pd->pcksum = &th->th_sum; 10446 break; 10447 } 10448 case IPPROTO_UDP: { 10449 struct udphdr *uh = &pd->hdr.udp; 10450 10451 if (!pf_pull_hdr(pd->m, pd->off, uh, sizeof(*uh), action, 10452 reason, af)) { 10453 *action = PF_DROP; 10454 REASON_SET(reason, PFRES_SHORT); 10455 return (-1); 10456 } 10457 pd->hdrlen = sizeof(*uh); 10458 if (uh->uh_dport == 0 || 10459 ntohs(uh->uh_ulen) > pd->m->m_pkthdr.len - pd->off || 10460 ntohs(uh->uh_ulen) < sizeof(struct udphdr)) { 10461 *action = PF_DROP; 10462 REASON_SET(reason, PFRES_SHORT); 10463 return (-1); 10464 } 10465 pd->sport = &uh->uh_sport; 10466 pd->dport = &uh->uh_dport; 10467 pd->pcksum = &uh->uh_sum; 10468 break; 10469 } 10470 case IPPROTO_SCTP: { 10471 if (!pf_pull_hdr(pd->m, pd->off, &pd->hdr.sctp, sizeof(pd->hdr.sctp), 10472 action, reason, af)) { 10473 *action = PF_DROP; 10474 REASON_SET(reason, PFRES_SHORT); 10475 return (-1); 10476 } 10477 pd->hdrlen = sizeof(pd->hdr.sctp); 10478 pd->p_len = pd->tot_len - pd->off; 10479 10480 pd->sport = &pd->hdr.sctp.src_port; 10481 pd->dport = &pd->hdr.sctp.dest_port; 10482 if (pd->hdr.sctp.src_port == 0 || pd->hdr.sctp.dest_port == 0) { 10483 *action = PF_DROP; 10484 REASON_SET(reason, PFRES_SHORT); 10485 return (-1); 10486 } 10487 if (pf_scan_sctp(pd) != PF_PASS) { 10488 *action = PF_DROP; 10489 REASON_SET(reason, PFRES_SHORT); 10490 return (-1); 10491 } 10492 /* 10493 * Placeholder. The SCTP checksum is 32-bits, but 10494 * pf_test_state() expects to update a 16-bit checksum. 10495 * Provide a dummy value which we'll subsequently ignore. 10496 */ 10497 pd->pcksum = &pd->sctp_dummy_sum; 10498 break; 10499 } 10500 case IPPROTO_ICMP: { 10501 if (!pf_pull_hdr(pd->m, pd->off, &pd->hdr.icmp, ICMP_MINLEN, 10502 action, reason, af)) { 10503 *action = PF_DROP; 10504 REASON_SET(reason, PFRES_SHORT); 10505 return (-1); 10506 } 10507 pd->pcksum = &pd->hdr.icmp.icmp_cksum; 10508 pd->hdrlen = ICMP_MINLEN; 10509 break; 10510 } 10511 #ifdef INET6 10512 case IPPROTO_ICMPV6: { 10513 size_t icmp_hlen = sizeof(struct icmp6_hdr); 10514 10515 if (!pf_pull_hdr(pd->m, pd->off, &pd->hdr.icmp6, icmp_hlen, 10516 action, reason, af)) { 10517 *action = PF_DROP; 10518 REASON_SET(reason, PFRES_SHORT); 10519 return (-1); 10520 } 10521 /* ICMP headers we look further into to match state */ 10522 switch (pd->hdr.icmp6.icmp6_type) { 10523 case MLD_LISTENER_QUERY: 10524 case MLD_LISTENER_REPORT: 10525 icmp_hlen = sizeof(struct mld_hdr); 10526 break; 10527 case ND_NEIGHBOR_SOLICIT: 10528 case ND_NEIGHBOR_ADVERT: 10529 icmp_hlen = sizeof(struct nd_neighbor_solicit); 10530 /* FALLTHROUGH */ 10531 case ND_ROUTER_SOLICIT: 10532 case ND_ROUTER_ADVERT: 10533 case ND_REDIRECT: 10534 if (pd->ttl != 255) { 10535 REASON_SET(reason, PFRES_NORM); 10536 return (PF_DROP); 10537 } 10538 break; 10539 } 10540 if (icmp_hlen > sizeof(struct icmp6_hdr) && 10541 !pf_pull_hdr(pd->m, pd->off, &pd->hdr.icmp6, icmp_hlen, 10542 action, reason, af)) { 10543 *action = PF_DROP; 10544 REASON_SET(reason, PFRES_SHORT); 10545 return (-1); 10546 } 10547 pd->hdrlen = icmp_hlen; 10548 pd->pcksum = &pd->hdr.icmp6.icmp6_cksum; 10549 break; 10550 } 10551 #endif /* INET6 */ 10552 } 10553 10554 if (pd->sport) 10555 pd->osport = pd->nsport = *pd->sport; 10556 if (pd->dport) 10557 pd->odport = pd->ndport = *pd->dport; 10558 10559 return (0); 10560 } 10561 10562 static void 10563 pf_counters_inc(int action, struct pf_pdesc *pd, 10564 struct pf_kstate *s, struct pf_krule *r, struct pf_krule *a) 10565 { 10566 struct pf_krule *tr; 10567 int dir = pd->dir; 10568 int dirndx; 10569 10570 pf_counter_u64_critical_enter(); 10571 pf_counter_u64_add_protected( 10572 &pd->kif->pfik_bytes[pd->af == AF_INET6][dir == PF_OUT][action != PF_PASS], 10573 pd->tot_len); 10574 pf_counter_u64_add_protected( 10575 &pd->kif->pfik_packets[pd->af == AF_INET6][dir == PF_OUT][action != PF_PASS], 10576 1); 10577 10578 if (action == PF_PASS || action == PF_AFRT || r->action == PF_DROP) { 10579 dirndx = (dir == PF_OUT); 10580 pf_counter_u64_add_protected(&r->packets[dirndx], 1); 10581 pf_counter_u64_add_protected(&r->bytes[dirndx], pd->tot_len); 10582 pf_update_timestamp(r); 10583 10584 if (a != NULL) { 10585 pf_counter_u64_add_protected(&a->packets[dirndx], 1); 10586 pf_counter_u64_add_protected(&a->bytes[dirndx], pd->tot_len); 10587 } 10588 if (s != NULL) { 10589 struct pf_krule_item *ri; 10590 10591 if (s->nat_rule != NULL) { 10592 pf_counter_u64_add_protected(&s->nat_rule->packets[dirndx], 10593 1); 10594 pf_counter_u64_add_protected(&s->nat_rule->bytes[dirndx], 10595 pd->tot_len); 10596 } 10597 /* 10598 * Source nodes are accessed unlocked here. 10599 * But since we are operating with stateful tracking 10600 * and the state is locked, those SNs could not have 10601 * been freed. 10602 */ 10603 for (pf_sn_types_t sn_type=0; sn_type<PF_SN_MAX; sn_type++) { 10604 if (s->sns[sn_type] != NULL) { 10605 counter_u64_add( 10606 s->sns[sn_type]->packets[dirndx], 10607 1); 10608 counter_u64_add( 10609 s->sns[sn_type]->bytes[dirndx], 10610 pd->tot_len); 10611 } 10612 } 10613 dirndx = (dir == s->direction) ? 0 : 1; 10614 s->packets[dirndx]++; 10615 s->bytes[dirndx] += pd->tot_len; 10616 10617 SLIST_FOREACH(ri, &s->match_rules, entry) { 10618 pf_counter_u64_add_protected(&ri->r->packets[dirndx], 1); 10619 pf_counter_u64_add_protected(&ri->r->bytes[dirndx], pd->tot_len); 10620 10621 if (ri->r->src.addr.type == PF_ADDR_TABLE) 10622 pfr_update_stats(ri->r->src.addr.p.tbl, 10623 (s == NULL) ? pd->src : 10624 &s->key[(s->direction == PF_IN)]-> 10625 addr[(s->direction == PF_OUT)], 10626 pd->af, pd->tot_len, dir == PF_OUT, 10627 r->action == PF_PASS, ri->r->src.neg); 10628 if (ri->r->dst.addr.type == PF_ADDR_TABLE) 10629 pfr_update_stats(ri->r->dst.addr.p.tbl, 10630 (s == NULL) ? pd->dst : 10631 &s->key[(s->direction == PF_IN)]-> 10632 addr[(s->direction == PF_IN)], 10633 pd->af, pd->tot_len, dir == PF_OUT, 10634 r->action == PF_PASS, ri->r->dst.neg); 10635 } 10636 } 10637 10638 tr = r; 10639 if (s != NULL && s->nat_rule != NULL && 10640 r == &V_pf_default_rule) 10641 tr = s->nat_rule; 10642 10643 if (tr->src.addr.type == PF_ADDR_TABLE) 10644 pfr_update_stats(tr->src.addr.p.tbl, 10645 (s == NULL) ? pd->src : 10646 &s->key[(s->direction == PF_IN)]-> 10647 addr[(s->direction == PF_OUT)], 10648 pd->af, pd->tot_len, dir == PF_OUT, 10649 r->action == PF_PASS, tr->src.neg); 10650 if (tr->dst.addr.type == PF_ADDR_TABLE) 10651 pfr_update_stats(tr->dst.addr.p.tbl, 10652 (s == NULL) ? pd->dst : 10653 &s->key[(s->direction == PF_IN)]-> 10654 addr[(s->direction == PF_IN)], 10655 pd->af, pd->tot_len, dir == PF_OUT, 10656 r->action == PF_PASS, tr->dst.neg); 10657 } 10658 pf_counter_u64_critical_exit(); 10659 } 10660 static void 10661 pf_log_matches(struct pf_pdesc *pd, struct pf_krule *rm, 10662 struct pf_krule *am, struct pf_kruleset *ruleset, 10663 struct pf_krule_slist *matchrules) 10664 { 10665 struct pf_krule_item *ri; 10666 10667 /* if this is the log(matches) rule, packet has been logged already */ 10668 if (rm->log & PF_LOG_MATCHES) 10669 return; 10670 10671 SLIST_FOREACH(ri, matchrules, entry) 10672 if (ri->r->log & PF_LOG_MATCHES) 10673 PFLOG_PACKET(rm->action, PFRES_MATCH, rm, am, 10674 ruleset, pd, 1, ri->r); 10675 } 10676 10677 #if defined(INET) || defined(INET6) 10678 int 10679 pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, 10680 struct inpcb *inp, struct pf_rule_actions *default_actions) 10681 { 10682 struct pfi_kkif *kif; 10683 u_short action, reason = 0; 10684 struct m_tag *mtag; 10685 struct pf_krule *a = NULL, *r = &V_pf_default_rule; 10686 struct pf_kstate *s = NULL; 10687 struct pf_kruleset *ruleset = NULL; 10688 struct pf_pdesc pd; 10689 int use_2nd_queue = 0; 10690 uint16_t tag; 10691 10692 PF_RULES_RLOCK_TRACKER; 10693 KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: bad direction %d\n", __func__, dir)); 10694 M_ASSERTPKTHDR(*m0); 10695 NET_EPOCH_ASSERT(); 10696 10697 if (!V_pf_status.running) 10698 return (PF_PASS); 10699 10700 kif = (struct pfi_kkif *)ifp->if_pf_kif; 10701 10702 if (__predict_false(kif == NULL)) { 10703 DPFPRINTF(PF_DEBUG_URGENT, 10704 "%s: kif == NULL, if_xname %s", 10705 __func__, ifp->if_xname); 10706 return (PF_DROP); 10707 } 10708 if (kif->pfik_flags & PFI_IFLAG_SKIP) { 10709 return (PF_PASS); 10710 } 10711 10712 if ((*m0)->m_flags & M_SKIP_FIREWALL) { 10713 return (PF_PASS); 10714 } 10715 10716 if (__predict_false(! M_WRITABLE(*m0))) { 10717 *m0 = m_unshare(*m0, M_NOWAIT); 10718 if (*m0 == NULL) { 10719 return (PF_DROP); 10720 } 10721 } 10722 10723 pf_init_pdesc(&pd, *m0); 10724 10725 if (pd.pf_mtag != NULL && (pd.pf_mtag->flags & PF_MTAG_FLAG_ROUTE_TO)) { 10726 pd.pf_mtag->flags &= ~PF_MTAG_FLAG_ROUTE_TO; 10727 10728 ifp = ifnet_byindexgen(pd.pf_mtag->if_index, 10729 pd.pf_mtag->if_idxgen); 10730 if (ifp == NULL || ifp->if_flags & IFF_DYING) { 10731 m_freem(*m0); 10732 *m0 = NULL; 10733 return (PF_PASS); 10734 } 10735 (ifp->if_output)(ifp, *m0, sintosa(&pd.pf_mtag->dst), NULL); 10736 *m0 = NULL; 10737 return (PF_PASS); 10738 } 10739 10740 if (ip_dn_io_ptr != NULL && pd.pf_mtag != NULL && 10741 pd.pf_mtag->flags & PF_MTAG_FLAG_DUMMYNET) { 10742 /* Dummynet re-injects packets after they've 10743 * completed their delay. We've already 10744 * processed them, so pass unconditionally. */ 10745 10746 /* But only once. We may see the packet multiple times (e.g. 10747 * PFIL_IN/PFIL_OUT). */ 10748 pf_dummynet_flag_remove(pd.m, pd.pf_mtag); 10749 10750 return (PF_PASS); 10751 } 10752 10753 PF_RULES_RLOCK(); 10754 10755 if (pf_setup_pdesc(af, dir, &pd, m0, &action, &reason, 10756 kif, default_actions) == -1) { 10757 if (action != PF_PASS) 10758 pd.act.log |= PF_LOG_FORCE; 10759 goto done; 10760 } 10761 10762 #ifdef INET 10763 if (af == AF_INET && dir == PF_OUT && pflags & PFIL_FWD && 10764 pd.df && (*m0)->m_pkthdr.len > ifp->if_mtu) { 10765 PF_RULES_RUNLOCK(); 10766 icmp_error(*m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 10767 0, ifp->if_mtu); 10768 *m0 = NULL; 10769 return (PF_DROP); 10770 } 10771 #endif /* INET */ 10772 #ifdef INET6 10773 /* 10774 * If we end up changing IP addresses (e.g. binat) the stack may get 10775 * confused and fail to send the icmp6 packet too big error. Just send 10776 * it here, before we do any NAT. 10777 */ 10778 if (af == AF_INET6 && dir == PF_OUT && pflags & PFIL_FWD && 10779 IN6_LINKMTU(ifp) < pf_max_frag_size(*m0)) { 10780 PF_RULES_RUNLOCK(); 10781 icmp6_error(*m0, ICMP6_PACKET_TOO_BIG, 0, IN6_LINKMTU(ifp)); 10782 *m0 = NULL; 10783 return (PF_DROP); 10784 } 10785 #endif /* INET6 */ 10786 10787 if (__predict_false(ip_divert_ptr != NULL) && 10788 ((mtag = m_tag_locate(pd.m, MTAG_PF_DIVERT, 0, NULL)) != NULL)) { 10789 struct pf_divert_mtag *dt = (struct pf_divert_mtag *)(mtag+1); 10790 if ((dt->idir == PF_DIVERT_MTAG_DIR_IN && dir == PF_IN) || 10791 (dt->idir == PF_DIVERT_MTAG_DIR_OUT && dir == PF_OUT)) { 10792 if (pd.pf_mtag == NULL && 10793 ((pd.pf_mtag = pf_get_mtag(pd.m)) == NULL)) { 10794 action = PF_DROP; 10795 goto done; 10796 } 10797 pd.pf_mtag->flags |= PF_MTAG_FLAG_PACKET_LOOPED; 10798 } 10799 if (pd.pf_mtag && pd.pf_mtag->flags & PF_MTAG_FLAG_FASTFWD_OURS_PRESENT) { 10800 pd.m->m_flags |= M_FASTFWD_OURS; 10801 pd.pf_mtag->flags &= ~PF_MTAG_FLAG_FASTFWD_OURS_PRESENT; 10802 } 10803 m_tag_delete(pd.m, mtag); 10804 10805 mtag = m_tag_locate(pd.m, MTAG_IPFW_RULE, 0, NULL); 10806 if (mtag != NULL) 10807 m_tag_delete(pd.m, mtag); 10808 } 10809 10810 switch (pd.virtual_proto) { 10811 case PF_VPROTO_FRAGMENT: 10812 /* 10813 * handle fragments that aren't reassembled by 10814 * normalization 10815 */ 10816 if (kif == NULL || r == NULL) /* pflog */ 10817 action = PF_DROP; 10818 else 10819 action = pf_test_rule(&r, &s, &pd, &a, 10820 &ruleset, &reason, inp); 10821 if (action != PF_PASS) 10822 REASON_SET(&reason, PFRES_FRAG); 10823 break; 10824 10825 case IPPROTO_TCP: { 10826 /* Respond to SYN with a syncookie. */ 10827 if ((tcp_get_flags(&pd.hdr.tcp) & (TH_SYN|TH_ACK|TH_RST)) == TH_SYN && 10828 pd.dir == PF_IN && pf_synflood_check(&pd)) { 10829 pf_syncookie_send(&pd); 10830 action = PF_DROP; 10831 break; 10832 } 10833 10834 if ((tcp_get_flags(&pd.hdr.tcp) & TH_ACK) && pd.p_len == 0) 10835 use_2nd_queue = 1; 10836 action = pf_normalize_tcp(&pd); 10837 if (action == PF_DROP) 10838 break; 10839 action = pf_test_state(&s, &pd, &reason); 10840 if (action == PF_PASS || action == PF_AFRT) { 10841 if (V_pfsync_update_state_ptr != NULL) 10842 V_pfsync_update_state_ptr(s); 10843 r = s->rule; 10844 a = s->anchor; 10845 } else if (s == NULL) { 10846 /* Validate remote SYN|ACK, re-create original SYN if 10847 * valid. */ 10848 if ((tcp_get_flags(&pd.hdr.tcp) & (TH_SYN|TH_ACK|TH_RST)) == 10849 TH_ACK && pf_syncookie_validate(&pd) && 10850 pd.dir == PF_IN) { 10851 struct mbuf *msyn; 10852 10853 msyn = pf_syncookie_recreate_syn(&pd); 10854 if (msyn == NULL) { 10855 action = PF_DROP; 10856 break; 10857 } 10858 10859 action = pf_test(af, dir, pflags, ifp, &msyn, inp, 10860 &pd.act); 10861 m_freem(msyn); 10862 if (action != PF_PASS) 10863 break; 10864 10865 action = pf_test_state(&s, &pd, &reason); 10866 if (action != PF_PASS || s == NULL) { 10867 action = PF_DROP; 10868 break; 10869 } 10870 10871 s->src.seqhi = ntohl(pd.hdr.tcp.th_ack) - 1; 10872 s->src.seqlo = ntohl(pd.hdr.tcp.th_seq) - 1; 10873 pf_set_protostate(s, PF_PEER_SRC, PF_TCPS_PROXY_DST); 10874 action = pf_synproxy(&pd, s, &reason); 10875 break; 10876 } else { 10877 action = pf_test_rule(&r, &s, &pd, 10878 &a, &ruleset, &reason, inp); 10879 } 10880 } 10881 break; 10882 } 10883 10884 case IPPROTO_SCTP: 10885 action = pf_normalize_sctp(&pd); 10886 if (action == PF_DROP) 10887 break; 10888 /* fallthrough */ 10889 case IPPROTO_UDP: 10890 default: 10891 action = pf_test_state(&s, &pd, &reason); 10892 if (action == PF_PASS || action == PF_AFRT) { 10893 if (V_pfsync_update_state_ptr != NULL) 10894 V_pfsync_update_state_ptr(s); 10895 r = s->rule; 10896 a = s->anchor; 10897 } else if (s == NULL) { 10898 action = pf_test_rule(&r, &s, 10899 &pd, &a, &ruleset, &reason, inp); 10900 } 10901 break; 10902 10903 case IPPROTO_ICMP: 10904 case IPPROTO_ICMPV6: { 10905 if (pd.virtual_proto == IPPROTO_ICMP && af != AF_INET) { 10906 action = PF_DROP; 10907 REASON_SET(&reason, PFRES_NORM); 10908 DPFPRINTF(PF_DEBUG_MISC, 10909 "dropping IPv6 packet with ICMPv4 payload"); 10910 break; 10911 } 10912 if (pd.virtual_proto == IPPROTO_ICMPV6 && af != AF_INET6) { 10913 action = PF_DROP; 10914 REASON_SET(&reason, PFRES_NORM); 10915 DPFPRINTF(PF_DEBUG_MISC, 10916 "pf: dropping IPv4 packet with ICMPv6 payload"); 10917 break; 10918 } 10919 action = pf_test_state_icmp(&s, &pd, &reason); 10920 if (action == PF_PASS || action == PF_AFRT) { 10921 if (V_pfsync_update_state_ptr != NULL) 10922 V_pfsync_update_state_ptr(s); 10923 r = s->rule; 10924 a = s->anchor; 10925 } else if (s == NULL) 10926 action = pf_test_rule(&r, &s, &pd, 10927 &a, &ruleset, &reason, inp); 10928 break; 10929 } 10930 10931 } 10932 10933 done: 10934 PF_RULES_RUNLOCK(); 10935 10936 if (pd.m == NULL) 10937 goto eat_pkt; 10938 10939 if (s) 10940 memcpy(&pd.act, &s->act, sizeof(s->act)); 10941 10942 if (action == PF_PASS && pd.badopts != 0 && !pd.act.allow_opts) { 10943 action = PF_DROP; 10944 REASON_SET(&reason, PFRES_IPOPTIONS); 10945 pd.act.log = PF_LOG_FORCE; 10946 DPFPRINTF(PF_DEBUG_MISC, 10947 "pf: dropping packet with dangerous headers"); 10948 } 10949 10950 if (pd.act.max_pkt_size && pd.act.max_pkt_size && 10951 pd.tot_len > pd.act.max_pkt_size) { 10952 action = PF_DROP; 10953 REASON_SET(&reason, PFRES_NORM); 10954 pd.act.log = PF_LOG_FORCE; 10955 DPFPRINTF(PF_DEBUG_MISC, 10956 "pf: dropping overly long packet"); 10957 } 10958 10959 if (s) { 10960 uint8_t log = pd.act.log; 10961 memcpy(&pd.act, &s->act, sizeof(struct pf_rule_actions)); 10962 pd.act.log |= log; 10963 tag = s->tag; 10964 } else { 10965 tag = r->tag; 10966 } 10967 10968 if (tag > 0 && pf_tag_packet(&pd, tag)) { 10969 action = PF_DROP; 10970 REASON_SET(&reason, PFRES_MEMORY); 10971 } 10972 10973 pf_scrub(&pd); 10974 if (pd.proto == IPPROTO_TCP && pd.act.max_mss) 10975 pf_normalize_mss(&pd); 10976 10977 if (pd.act.rtableid >= 0) 10978 M_SETFIB(pd.m, pd.act.rtableid); 10979 10980 if (pd.act.flags & PFSTATE_SETPRIO) { 10981 if (pd.tos & IPTOS_LOWDELAY) 10982 use_2nd_queue = 1; 10983 if (vlan_set_pcp(pd.m, pd.act.set_prio[use_2nd_queue])) { 10984 action = PF_DROP; 10985 REASON_SET(&reason, PFRES_MEMORY); 10986 pd.act.log = PF_LOG_FORCE; 10987 DPFPRINTF(PF_DEBUG_MISC, 10988 "pf: failed to allocate 802.1q mtag"); 10989 } 10990 } 10991 10992 #ifdef ALTQ 10993 if (action == PF_PASS && pd.act.qid) { 10994 if (pd.pf_mtag == NULL && 10995 ((pd.pf_mtag = pf_get_mtag(pd.m)) == NULL)) { 10996 action = PF_DROP; 10997 REASON_SET(&reason, PFRES_MEMORY); 10998 } else { 10999 if (s != NULL) 11000 pd.pf_mtag->qid_hash = pf_state_hash(s); 11001 if (use_2nd_queue || (pd.tos & IPTOS_LOWDELAY)) 11002 pd.pf_mtag->qid = pd.act.pqid; 11003 else 11004 pd.pf_mtag->qid = pd.act.qid; 11005 /* Add hints for ecn. */ 11006 pd.pf_mtag->hdr = mtod(pd.m, void *); 11007 } 11008 } 11009 #endif /* ALTQ */ 11010 11011 /* 11012 * connections redirected to loopback should not match sockets 11013 * bound specifically to loopback due to security implications, 11014 * see tcp_input() and in_pcblookup_listen(). 11015 */ 11016 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 11017 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule != NULL && 11018 (s->nat_rule->action == PF_RDR || 11019 s->nat_rule->action == PF_BINAT) && 11020 pf_is_loopback(af, pd.dst)) 11021 pd.m->m_flags |= M_SKIP_FIREWALL; 11022 11023 if (af == AF_INET && __predict_false(ip_divert_ptr != NULL) && 11024 action == PF_PASS && r->divert.port && !PACKET_LOOPED(&pd)) { 11025 mtag = m_tag_alloc(MTAG_PF_DIVERT, 0, 11026 sizeof(struct pf_divert_mtag), M_NOWAIT | M_ZERO); 11027 if (mtag != NULL) { 11028 ((struct pf_divert_mtag *)(mtag+1))->port = 11029 ntohs(r->divert.port); 11030 ((struct pf_divert_mtag *)(mtag+1))->idir = 11031 (dir == PF_IN) ? PF_DIVERT_MTAG_DIR_IN : 11032 PF_DIVERT_MTAG_DIR_OUT; 11033 11034 if (s) 11035 PF_STATE_UNLOCK(s); 11036 11037 m_tag_prepend(pd.m, mtag); 11038 if (pd.m->m_flags & M_FASTFWD_OURS) { 11039 if (pd.pf_mtag == NULL && 11040 ((pd.pf_mtag = pf_get_mtag(pd.m)) == NULL)) { 11041 action = PF_DROP; 11042 REASON_SET(&reason, PFRES_MEMORY); 11043 pd.act.log = PF_LOG_FORCE; 11044 DPFPRINTF(PF_DEBUG_MISC, 11045 "pf: failed to allocate tag"); 11046 } else { 11047 pd.pf_mtag->flags |= 11048 PF_MTAG_FLAG_FASTFWD_OURS_PRESENT; 11049 pd.m->m_flags &= ~M_FASTFWD_OURS; 11050 } 11051 } 11052 ip_divert_ptr(*m0, dir == PF_IN); 11053 *m0 = NULL; 11054 11055 return (action); 11056 } else { 11057 /* XXX: ipfw has the same behaviour! */ 11058 action = PF_DROP; 11059 REASON_SET(&reason, PFRES_MEMORY); 11060 pd.act.log = PF_LOG_FORCE; 11061 DPFPRINTF(PF_DEBUG_MISC, 11062 "pf: failed to allocate divert tag"); 11063 } 11064 } 11065 /* XXX: Anybody working on it?! */ 11066 if (af == AF_INET6 && r->divert.port) 11067 printf("pf: divert(9) is not supported for IPv6\n"); 11068 11069 /* this flag will need revising if the pkt is forwarded */ 11070 if (pd.pf_mtag) 11071 pd.pf_mtag->flags &= ~PF_MTAG_FLAG_PACKET_LOOPED; 11072 11073 if (pd.act.log) { 11074 struct pf_krule *lr; 11075 struct pf_krule_item *ri; 11076 11077 if (s != NULL && s->nat_rule != NULL && 11078 s->nat_rule->log & PF_LOG_ALL) 11079 lr = s->nat_rule; 11080 else 11081 lr = r; 11082 11083 if (pd.act.log & PF_LOG_FORCE || lr->log & PF_LOG_ALL) 11084 PFLOG_PACKET(action, reason, lr, a, 11085 ruleset, &pd, (s == NULL), NULL); 11086 if (s) { 11087 SLIST_FOREACH(ri, &s->match_rules, entry) 11088 if (ri->r->log & PF_LOG_ALL) 11089 PFLOG_PACKET(action, 11090 reason, ri->r, a, ruleset, &pd, 0, NULL); 11091 } 11092 } 11093 11094 pf_counters_inc(action, &pd, s, r, a); 11095 11096 switch (action) { 11097 case PF_SYNPROXY_DROP: 11098 m_freem(*m0); 11099 case PF_DEFER: 11100 *m0 = NULL; 11101 action = PF_PASS; 11102 break; 11103 case PF_DROP: 11104 m_freem(*m0); 11105 *m0 = NULL; 11106 break; 11107 case PF_AFRT: 11108 if (pf_translate_af(&pd)) { 11109 *m0 = pd.m; 11110 action = PF_DROP; 11111 break; 11112 } 11113 #ifdef INET 11114 if (pd.naf == AF_INET) { 11115 action = pf_route(r, kif->pfik_ifp, s, &pd, 11116 inp); 11117 } 11118 #endif /* INET */ 11119 #ifdef INET6 11120 if (pd.naf == AF_INET6) { 11121 action = pf_route6(r, kif->pfik_ifp, s, &pd, 11122 inp); 11123 } 11124 #endif /* INET6 */ 11125 *m0 = pd.m; 11126 goto out; 11127 break; 11128 default: 11129 if (pd.act.rt) { 11130 switch (af) { 11131 #ifdef INET 11132 case AF_INET: 11133 /* pf_route() returns unlocked. */ 11134 action = pf_route(r, kif->pfik_ifp, s, &pd, 11135 inp); 11136 break; 11137 #endif /* INET */ 11138 #ifdef INET6 11139 case AF_INET6: 11140 /* pf_route6() returns unlocked. */ 11141 action = pf_route6(r, kif->pfik_ifp, s, &pd, 11142 inp); 11143 break; 11144 #endif /* INET6 */ 11145 } 11146 *m0 = pd.m; 11147 goto out; 11148 } 11149 if (pf_dummynet(&pd, s, r, m0) != 0) { 11150 action = PF_DROP; 11151 REASON_SET(&reason, PFRES_MEMORY); 11152 } 11153 break; 11154 } 11155 11156 eat_pkt: 11157 SDT_PROBE4(pf, ip, test, done, action, reason, r, s); 11158 11159 if (s && action != PF_DROP) { 11160 if (!s->if_index_in && dir == PF_IN) 11161 s->if_index_in = ifp->if_index; 11162 else if (!s->if_index_out && dir == PF_OUT) 11163 s->if_index_out = ifp->if_index; 11164 } 11165 11166 if (s) 11167 PF_STATE_UNLOCK(s); 11168 11169 out: 11170 #ifdef INET6 11171 /* If reassembled packet passed, create new fragments. */ 11172 if (af == AF_INET6 && action == PF_PASS && *m0 && dir == PF_OUT && 11173 (! (pflags & PF_PFIL_NOREFRAGMENT)) && 11174 (mtag = m_tag_find(pd.m, PACKET_TAG_PF_REASSEMBLED, NULL)) != NULL) 11175 action = pf_refragment6(ifp, m0, mtag, NULL, pflags & PFIL_FWD); 11176 #endif /* INET6 */ 11177 11178 pf_sctp_multihome_delayed(&pd, kif, s, action); 11179 11180 return (action); 11181 } 11182 #endif /* INET || INET6 */ 11183