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