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