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