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