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