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, so we can never release the 1736 * last reference here, only in pf_release_state(). */ 1737 (void)refcount_release(&s->refs); 1738 1739 return (pf_release_state(s)); 1740 } 1741 1742 void 1743 pf_free_state(struct pf_state *cur) 1744 { 1745 1746 KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 1747 KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 1748 cur->timeout)); 1749 1750 for (int i = 0; i < 2; i++) { 1751 counter_u64_free(cur->bytes[i]); 1752 counter_u64_free(cur->packets[i]); 1753 } 1754 1755 pf_normalize_tcp_cleanup(cur); 1756 uma_zfree(V_pf_state_z, cur); 1757 counter_u64_add(V_pf_status.fcounters[FCNT_STATE_REMOVALS], 1); 1758 } 1759 1760 /* 1761 * Called only from pf_purge_thread(), thus serialized. 1762 */ 1763 static u_int 1764 pf_purge_expired_states(u_int i, int maxcheck) 1765 { 1766 struct pf_idhash *ih; 1767 struct pf_state *s; 1768 1769 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1770 1771 /* 1772 * Go through hash and unlink states that expire now. 1773 */ 1774 while (maxcheck > 0) { 1775 ih = &V_pf_idhash[i]; 1776 1777 /* only take the lock if we expect to do work */ 1778 if (!LIST_EMPTY(&ih->states)) { 1779 relock: 1780 PF_HASHROW_LOCK(ih); 1781 LIST_FOREACH(s, &ih->states, entry) { 1782 if (pf_state_expires(s) <= time_uptime) { 1783 V_pf_status.states -= 1784 pf_unlink_state(s, PF_ENTER_LOCKED); 1785 goto relock; 1786 } 1787 s->rule.ptr->rule_ref |= PFRULE_REFS; 1788 if (s->nat_rule.ptr != NULL) 1789 s->nat_rule.ptr->rule_ref |= PFRULE_REFS; 1790 if (s->anchor.ptr != NULL) 1791 s->anchor.ptr->rule_ref |= PFRULE_REFS; 1792 s->kif->pfik_flags |= PFI_IFLAG_REFS; 1793 if (s->rt_kif) 1794 s->rt_kif->pfik_flags |= PFI_IFLAG_REFS; 1795 } 1796 PF_HASHROW_UNLOCK(ih); 1797 } 1798 1799 /* Return when we hit end of hash. */ 1800 if (++i > pf_hashmask) { 1801 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1802 return (0); 1803 } 1804 1805 maxcheck--; 1806 } 1807 1808 V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1809 1810 return (i); 1811 } 1812 1813 static void 1814 pf_purge_unlinked_rules() 1815 { 1816 struct pf_krulequeue tmpq; 1817 struct pf_krule *r, *r1; 1818 1819 /* 1820 * If we have overloading task pending, then we'd 1821 * better skip purging this time. There is a tiny 1822 * probability that overloading task references 1823 * an already unlinked rule. 1824 */ 1825 PF_OVERLOADQ_LOCK(); 1826 if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 1827 PF_OVERLOADQ_UNLOCK(); 1828 return; 1829 } 1830 PF_OVERLOADQ_UNLOCK(); 1831 1832 /* 1833 * Do naive mark-and-sweep garbage collecting of old rules. 1834 * Reference flag is raised by pf_purge_expired_states() 1835 * and pf_purge_expired_src_nodes(). 1836 * 1837 * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 1838 * use a temporary queue. 1839 */ 1840 TAILQ_INIT(&tmpq); 1841 PF_UNLNKDRULES_LOCK(); 1842 TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 1843 if (!(r->rule_ref & PFRULE_REFS)) { 1844 TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 1845 TAILQ_INSERT_TAIL(&tmpq, r, entries); 1846 } else 1847 r->rule_ref &= ~PFRULE_REFS; 1848 } 1849 PF_UNLNKDRULES_UNLOCK(); 1850 1851 if (!TAILQ_EMPTY(&tmpq)) { 1852 PF_RULES_WLOCK(); 1853 TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 1854 TAILQ_REMOVE(&tmpq, r, entries); 1855 pf_free_rule(r); 1856 } 1857 PF_RULES_WUNLOCK(); 1858 } 1859 } 1860 1861 void 1862 pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 1863 { 1864 switch (af) { 1865 #ifdef INET 1866 case AF_INET: { 1867 u_int32_t a = ntohl(addr->addr32[0]); 1868 printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 1869 (a>>8)&255, a&255); 1870 if (p) { 1871 p = ntohs(p); 1872 printf(":%u", p); 1873 } 1874 break; 1875 } 1876 #endif /* INET */ 1877 #ifdef INET6 1878 case AF_INET6: { 1879 u_int16_t b; 1880 u_int8_t i, curstart, curend, maxstart, maxend; 1881 curstart = curend = maxstart = maxend = 255; 1882 for (i = 0; i < 8; i++) { 1883 if (!addr->addr16[i]) { 1884 if (curstart == 255) 1885 curstart = i; 1886 curend = i; 1887 } else { 1888 if ((curend - curstart) > 1889 (maxend - maxstart)) { 1890 maxstart = curstart; 1891 maxend = curend; 1892 } 1893 curstart = curend = 255; 1894 } 1895 } 1896 if ((curend - curstart) > 1897 (maxend - maxstart)) { 1898 maxstart = curstart; 1899 maxend = curend; 1900 } 1901 for (i = 0; i < 8; i++) { 1902 if (i >= maxstart && i <= maxend) { 1903 if (i == 0) 1904 printf(":"); 1905 if (i == maxend) 1906 printf(":"); 1907 } else { 1908 b = ntohs(addr->addr16[i]); 1909 printf("%x", b); 1910 if (i < 7) 1911 printf(":"); 1912 } 1913 } 1914 if (p) { 1915 p = ntohs(p); 1916 printf("[%u]", p); 1917 } 1918 break; 1919 } 1920 #endif /* INET6 */ 1921 } 1922 } 1923 1924 void 1925 pf_print_state(struct pf_state *s) 1926 { 1927 pf_print_state_parts(s, NULL, NULL); 1928 } 1929 1930 static void 1931 pf_print_state_parts(struct pf_state *s, 1932 struct pf_state_key *skwp, struct pf_state_key *sksp) 1933 { 1934 struct pf_state_key *skw, *sks; 1935 u_int8_t proto, dir; 1936 1937 /* Do our best to fill these, but they're skipped if NULL */ 1938 skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 1939 sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 1940 proto = skw ? skw->proto : (sks ? sks->proto : 0); 1941 dir = s ? s->direction : 0; 1942 1943 switch (proto) { 1944 case IPPROTO_IPV4: 1945 printf("IPv4"); 1946 break; 1947 case IPPROTO_IPV6: 1948 printf("IPv6"); 1949 break; 1950 case IPPROTO_TCP: 1951 printf("TCP"); 1952 break; 1953 case IPPROTO_UDP: 1954 printf("UDP"); 1955 break; 1956 case IPPROTO_ICMP: 1957 printf("ICMP"); 1958 break; 1959 case IPPROTO_ICMPV6: 1960 printf("ICMPv6"); 1961 break; 1962 default: 1963 printf("%u", proto); 1964 break; 1965 } 1966 switch (dir) { 1967 case PF_IN: 1968 printf(" in"); 1969 break; 1970 case PF_OUT: 1971 printf(" out"); 1972 break; 1973 } 1974 if (skw) { 1975 printf(" wire: "); 1976 pf_print_host(&skw->addr[0], skw->port[0], skw->af); 1977 printf(" "); 1978 pf_print_host(&skw->addr[1], skw->port[1], skw->af); 1979 } 1980 if (sks) { 1981 printf(" stack: "); 1982 if (sks != skw) { 1983 pf_print_host(&sks->addr[0], sks->port[0], sks->af); 1984 printf(" "); 1985 pf_print_host(&sks->addr[1], sks->port[1], sks->af); 1986 } else 1987 printf("-"); 1988 } 1989 if (s) { 1990 if (proto == IPPROTO_TCP) { 1991 printf(" [lo=%u high=%u win=%u modulator=%u", 1992 s->src.seqlo, s->src.seqhi, 1993 s->src.max_win, s->src.seqdiff); 1994 if (s->src.wscale && s->dst.wscale) 1995 printf(" wscale=%u", 1996 s->src.wscale & PF_WSCALE_MASK); 1997 printf("]"); 1998 printf(" [lo=%u high=%u win=%u modulator=%u", 1999 s->dst.seqlo, s->dst.seqhi, 2000 s->dst.max_win, s->dst.seqdiff); 2001 if (s->src.wscale && s->dst.wscale) 2002 printf(" wscale=%u", 2003 s->dst.wscale & PF_WSCALE_MASK); 2004 printf("]"); 2005 } 2006 printf(" %u:%u", s->src.state, s->dst.state); 2007 } 2008 } 2009 2010 void 2011 pf_print_flags(u_int8_t f) 2012 { 2013 if (f) 2014 printf(" "); 2015 if (f & TH_FIN) 2016 printf("F"); 2017 if (f & TH_SYN) 2018 printf("S"); 2019 if (f & TH_RST) 2020 printf("R"); 2021 if (f & TH_PUSH) 2022 printf("P"); 2023 if (f & TH_ACK) 2024 printf("A"); 2025 if (f & TH_URG) 2026 printf("U"); 2027 if (f & TH_ECE) 2028 printf("E"); 2029 if (f & TH_CWR) 2030 printf("W"); 2031 } 2032 2033 #define PF_SET_SKIP_STEPS(i) \ 2034 do { \ 2035 while (head[i] != cur) { \ 2036 head[i]->skip[i].ptr = cur; \ 2037 head[i] = TAILQ_NEXT(head[i], entries); \ 2038 } \ 2039 } while (0) 2040 2041 void 2042 pf_calc_skip_steps(struct pf_krulequeue *rules) 2043 { 2044 struct pf_krule *cur, *prev, *head[PF_SKIP_COUNT]; 2045 int i; 2046 2047 cur = TAILQ_FIRST(rules); 2048 prev = cur; 2049 for (i = 0; i < PF_SKIP_COUNT; ++i) 2050 head[i] = cur; 2051 while (cur != NULL) { 2052 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 2053 PF_SET_SKIP_STEPS(PF_SKIP_IFP); 2054 if (cur->direction != prev->direction) 2055 PF_SET_SKIP_STEPS(PF_SKIP_DIR); 2056 if (cur->af != prev->af) 2057 PF_SET_SKIP_STEPS(PF_SKIP_AF); 2058 if (cur->proto != prev->proto) 2059 PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 2060 if (cur->src.neg != prev->src.neg || 2061 pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 2062 PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 2063 if (cur->src.port[0] != prev->src.port[0] || 2064 cur->src.port[1] != prev->src.port[1] || 2065 cur->src.port_op != prev->src.port_op) 2066 PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 2067 if (cur->dst.neg != prev->dst.neg || 2068 pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 2069 PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 2070 if (cur->dst.port[0] != prev->dst.port[0] || 2071 cur->dst.port[1] != prev->dst.port[1] || 2072 cur->dst.port_op != prev->dst.port_op) 2073 PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 2074 2075 prev = cur; 2076 cur = TAILQ_NEXT(cur, entries); 2077 } 2078 for (i = 0; i < PF_SKIP_COUNT; ++i) 2079 PF_SET_SKIP_STEPS(i); 2080 } 2081 2082 static int 2083 pf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 2084 { 2085 if (aw1->type != aw2->type) 2086 return (1); 2087 switch (aw1->type) { 2088 case PF_ADDR_ADDRMASK: 2089 case PF_ADDR_RANGE: 2090 if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, AF_INET6)) 2091 return (1); 2092 if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, AF_INET6)) 2093 return (1); 2094 return (0); 2095 case PF_ADDR_DYNIFTL: 2096 return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 2097 case PF_ADDR_NOROUTE: 2098 case PF_ADDR_URPFFAILED: 2099 return (0); 2100 case PF_ADDR_TABLE: 2101 return (aw1->p.tbl != aw2->p.tbl); 2102 default: 2103 printf("invalid address type: %d\n", aw1->type); 2104 return (1); 2105 } 2106 } 2107 2108 /** 2109 * Checksum updates are a little complicated because the checksum in the TCP/UDP 2110 * header isn't always a full checksum. In some cases (i.e. output) it's a 2111 * pseudo-header checksum, which is a partial checksum over src/dst IP 2112 * addresses, protocol number and length. 2113 * 2114 * That means we have the following cases: 2115 * * Input or forwarding: we don't have TSO, the checksum fields are full 2116 * checksums, we need to update the checksum whenever we change anything. 2117 * * Output (i.e. the checksum is a pseudo-header checksum): 2118 * x The field being updated is src/dst address or affects the length of 2119 * the packet. We need to update the pseudo-header checksum (note that this 2120 * checksum is not ones' complement). 2121 * x Some other field is being modified (e.g. src/dst port numbers): We 2122 * don't have to update anything. 2123 **/ 2124 u_int16_t 2125 pf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 2126 { 2127 u_int32_t x; 2128 2129 x = cksum + old - new; 2130 x = (x + (x >> 16)) & 0xffff; 2131 2132 /* optimise: eliminate a branch when not udp */ 2133 if (udp && cksum == 0x0000) 2134 return cksum; 2135 if (udp && x == 0x0000) 2136 x = 0xffff; 2137 2138 return (u_int16_t)(x); 2139 } 2140 2141 static void 2142 pf_patch_8(struct mbuf *m, u_int16_t *cksum, u_int8_t *f, u_int8_t v, bool hi, 2143 u_int8_t udp) 2144 { 2145 u_int16_t old = htons(hi ? (*f << 8) : *f); 2146 u_int16_t new = htons(hi ? ( v << 8) : v); 2147 2148 if (*f == v) 2149 return; 2150 2151 *f = v; 2152 2153 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 2154 return; 2155 2156 *cksum = pf_cksum_fixup(*cksum, old, new, udp); 2157 } 2158 2159 void 2160 pf_patch_16_unaligned(struct mbuf *m, u_int16_t *cksum, void *f, u_int16_t v, 2161 bool hi, u_int8_t udp) 2162 { 2163 u_int8_t *fb = (u_int8_t *)f; 2164 u_int8_t *vb = (u_int8_t *)&v; 2165 2166 pf_patch_8(m, cksum, fb++, *vb++, hi, udp); 2167 pf_patch_8(m, cksum, fb++, *vb++, !hi, udp); 2168 } 2169 2170 void 2171 pf_patch_32_unaligned(struct mbuf *m, u_int16_t *cksum, void *f, u_int32_t v, 2172 bool hi, u_int8_t udp) 2173 { 2174 u_int8_t *fb = (u_int8_t *)f; 2175 u_int8_t *vb = (u_int8_t *)&v; 2176 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 pf_patch_8(m, cksum, fb++, *vb++, !hi, udp); 2181 } 2182 2183 u_int16_t 2184 pf_proto_cksum_fixup(struct mbuf *m, u_int16_t cksum, u_int16_t old, 2185 u_int16_t new, u_int8_t udp) 2186 { 2187 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 2188 return (cksum); 2189 2190 return (pf_cksum_fixup(cksum, old, new, udp)); 2191 } 2192 2193 static void 2194 pf_change_ap(struct mbuf *m, struct pf_addr *a, u_int16_t *p, u_int16_t *ic, 2195 u_int16_t *pc, struct pf_addr *an, u_int16_t pn, u_int8_t u, 2196 sa_family_t af) 2197 { 2198 struct pf_addr ao; 2199 u_int16_t po = *p; 2200 2201 PF_ACPY(&ao, a, af); 2202 PF_ACPY(a, an, af); 2203 2204 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 2205 *pc = ~*pc; 2206 2207 *p = pn; 2208 2209 switch (af) { 2210 #ifdef INET 2211 case AF_INET: 2212 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2213 ao.addr16[0], an->addr16[0], 0), 2214 ao.addr16[1], an->addr16[1], 0); 2215 *p = pn; 2216 2217 *pc = pf_cksum_fixup(pf_cksum_fixup(*pc, 2218 ao.addr16[0], an->addr16[0], u), 2219 ao.addr16[1], an->addr16[1], u); 2220 2221 *pc = pf_proto_cksum_fixup(m, *pc, po, pn, u); 2222 break; 2223 #endif /* INET */ 2224 #ifdef INET6 2225 case AF_INET6: 2226 *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2227 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2228 pf_cksum_fixup(pf_cksum_fixup(*pc, 2229 ao.addr16[0], an->addr16[0], u), 2230 ao.addr16[1], an->addr16[1], u), 2231 ao.addr16[2], an->addr16[2], u), 2232 ao.addr16[3], an->addr16[3], u), 2233 ao.addr16[4], an->addr16[4], u), 2234 ao.addr16[5], an->addr16[5], u), 2235 ao.addr16[6], an->addr16[6], u), 2236 ao.addr16[7], an->addr16[7], u); 2237 2238 *pc = pf_proto_cksum_fixup(m, *pc, po, pn, u); 2239 break; 2240 #endif /* INET6 */ 2241 } 2242 2243 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | 2244 CSUM_DELAY_DATA_IPV6)) { 2245 *pc = ~*pc; 2246 if (! *pc) 2247 *pc = 0xffff; 2248 } 2249 } 2250 2251 /* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 2252 void 2253 pf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 2254 { 2255 u_int32_t ao; 2256 2257 memcpy(&ao, a, sizeof(ao)); 2258 memcpy(a, &an, sizeof(u_int32_t)); 2259 *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 2260 ao % 65536, an % 65536, u); 2261 } 2262 2263 void 2264 pf_change_proto_a(struct mbuf *m, void *a, u_int16_t *c, u_int32_t an, u_int8_t udp) 2265 { 2266 u_int32_t ao; 2267 2268 memcpy(&ao, a, sizeof(ao)); 2269 memcpy(a, &an, sizeof(u_int32_t)); 2270 2271 *c = pf_proto_cksum_fixup(m, 2272 pf_proto_cksum_fixup(m, *c, ao / 65536, an / 65536, udp), 2273 ao % 65536, an % 65536, udp); 2274 } 2275 2276 #ifdef INET6 2277 static void 2278 pf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 2279 { 2280 struct pf_addr ao; 2281 2282 PF_ACPY(&ao, a, AF_INET6); 2283 PF_ACPY(a, an, AF_INET6); 2284 2285 *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2286 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2287 pf_cksum_fixup(pf_cksum_fixup(*c, 2288 ao.addr16[0], an->addr16[0], u), 2289 ao.addr16[1], an->addr16[1], u), 2290 ao.addr16[2], an->addr16[2], u), 2291 ao.addr16[3], an->addr16[3], u), 2292 ao.addr16[4], an->addr16[4], u), 2293 ao.addr16[5], an->addr16[5], u), 2294 ao.addr16[6], an->addr16[6], u), 2295 ao.addr16[7], an->addr16[7], u); 2296 } 2297 #endif /* INET6 */ 2298 2299 static void 2300 pf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 2301 struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 2302 u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 2303 { 2304 struct pf_addr oia, ooa; 2305 2306 PF_ACPY(&oia, ia, af); 2307 if (oa) 2308 PF_ACPY(&ooa, oa, af); 2309 2310 /* Change inner protocol port, fix inner protocol checksum. */ 2311 if (ip != NULL) { 2312 u_int16_t oip = *ip; 2313 u_int32_t opc; 2314 2315 if (pc != NULL) 2316 opc = *pc; 2317 *ip = np; 2318 if (pc != NULL) 2319 *pc = pf_cksum_fixup(*pc, oip, *ip, u); 2320 *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 2321 if (pc != NULL) 2322 *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 2323 } 2324 /* Change inner ip address, fix inner ip and icmp checksums. */ 2325 PF_ACPY(ia, na, af); 2326 switch (af) { 2327 #ifdef INET 2328 case AF_INET: { 2329 u_int32_t oh2c = *h2c; 2330 2331 *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 2332 oia.addr16[0], ia->addr16[0], 0), 2333 oia.addr16[1], ia->addr16[1], 0); 2334 *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2335 oia.addr16[0], ia->addr16[0], 0), 2336 oia.addr16[1], ia->addr16[1], 0); 2337 *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 2338 break; 2339 } 2340 #endif /* INET */ 2341 #ifdef INET6 2342 case AF_INET6: 2343 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2344 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2345 pf_cksum_fixup(pf_cksum_fixup(*ic, 2346 oia.addr16[0], ia->addr16[0], u), 2347 oia.addr16[1], ia->addr16[1], u), 2348 oia.addr16[2], ia->addr16[2], u), 2349 oia.addr16[3], ia->addr16[3], u), 2350 oia.addr16[4], ia->addr16[4], u), 2351 oia.addr16[5], ia->addr16[5], u), 2352 oia.addr16[6], ia->addr16[6], u), 2353 oia.addr16[7], ia->addr16[7], u); 2354 break; 2355 #endif /* INET6 */ 2356 } 2357 /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 2358 if (oa) { 2359 PF_ACPY(oa, na, af); 2360 switch (af) { 2361 #ifdef INET 2362 case AF_INET: 2363 *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 2364 ooa.addr16[0], oa->addr16[0], 0), 2365 ooa.addr16[1], oa->addr16[1], 0); 2366 break; 2367 #endif /* INET */ 2368 #ifdef INET6 2369 case AF_INET6: 2370 *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2371 pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2372 pf_cksum_fixup(pf_cksum_fixup(*ic, 2373 ooa.addr16[0], oa->addr16[0], u), 2374 ooa.addr16[1], oa->addr16[1], u), 2375 ooa.addr16[2], oa->addr16[2], u), 2376 ooa.addr16[3], oa->addr16[3], u), 2377 ooa.addr16[4], oa->addr16[4], u), 2378 ooa.addr16[5], oa->addr16[5], u), 2379 ooa.addr16[6], oa->addr16[6], u), 2380 ooa.addr16[7], oa->addr16[7], u); 2381 break; 2382 #endif /* INET6 */ 2383 } 2384 } 2385 } 2386 2387 /* 2388 * Need to modulate the sequence numbers in the TCP SACK option 2389 * (credits to Krzysztof Pfaff for report and patch) 2390 */ 2391 static int 2392 pf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd, 2393 struct tcphdr *th, struct pf_state_peer *dst) 2394 { 2395 int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen; 2396 u_int8_t opts[TCP_MAXOLEN], *opt = opts; 2397 int copyback = 0, i, olen; 2398 struct sackblk sack; 2399 2400 #define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2) 2401 if (hlen < TCPOLEN_SACKLEN || 2402 !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af)) 2403 return 0; 2404 2405 while (hlen >= TCPOLEN_SACKLEN) { 2406 size_t startoff = opt - opts; 2407 olen = opt[1]; 2408 switch (*opt) { 2409 case TCPOPT_EOL: /* FALLTHROUGH */ 2410 case TCPOPT_NOP: 2411 opt++; 2412 hlen--; 2413 break; 2414 case TCPOPT_SACK: 2415 if (olen > hlen) 2416 olen = hlen; 2417 if (olen >= TCPOLEN_SACKLEN) { 2418 for (i = 2; i + TCPOLEN_SACK <= olen; 2419 i += TCPOLEN_SACK) { 2420 memcpy(&sack, &opt[i], sizeof(sack)); 2421 pf_patch_32_unaligned(m, 2422 &th->th_sum, &sack.start, 2423 htonl(ntohl(sack.start) - dst->seqdiff), 2424 PF_ALGNMNT(startoff), 2425 0); 2426 pf_patch_32_unaligned(m, &th->th_sum, 2427 &sack.end, 2428 htonl(ntohl(sack.end) - dst->seqdiff), 2429 PF_ALGNMNT(startoff), 2430 0); 2431 memcpy(&opt[i], &sack, sizeof(sack)); 2432 } 2433 copyback = 1; 2434 } 2435 /* FALLTHROUGH */ 2436 default: 2437 if (olen < 2) 2438 olen = 2; 2439 hlen -= olen; 2440 opt += olen; 2441 } 2442 } 2443 2444 if (copyback) 2445 m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts); 2446 return (copyback); 2447 } 2448 2449 static void 2450 pf_send_tcp(struct mbuf *replyto, const struct pf_krule *r, sa_family_t af, 2451 const struct pf_addr *saddr, const struct pf_addr *daddr, 2452 u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 2453 u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag, 2454 u_int16_t rtag, struct ifnet *ifp) 2455 { 2456 struct pf_send_entry *pfse; 2457 struct mbuf *m; 2458 int len, tlen; 2459 #ifdef INET 2460 struct ip *h = NULL; 2461 #endif /* INET */ 2462 #ifdef INET6 2463 struct ip6_hdr *h6 = NULL; 2464 #endif /* INET6 */ 2465 struct tcphdr *th; 2466 char *opt; 2467 struct pf_mtag *pf_mtag; 2468 2469 len = 0; 2470 th = NULL; 2471 2472 /* maximum segment size tcp option */ 2473 tlen = sizeof(struct tcphdr); 2474 if (mss) 2475 tlen += 4; 2476 2477 switch (af) { 2478 #ifdef INET 2479 case AF_INET: 2480 len = sizeof(struct ip) + tlen; 2481 break; 2482 #endif /* INET */ 2483 #ifdef INET6 2484 case AF_INET6: 2485 len = sizeof(struct ip6_hdr) + tlen; 2486 break; 2487 #endif /* INET6 */ 2488 default: 2489 panic("%s: unsupported af %d", __func__, af); 2490 } 2491 2492 /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2493 pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2494 if (pfse == NULL) 2495 return; 2496 m = m_gethdr(M_NOWAIT, MT_DATA); 2497 if (m == NULL) { 2498 free(pfse, M_PFTEMP); 2499 return; 2500 } 2501 #ifdef MAC 2502 mac_netinet_firewall_send(m); 2503 #endif 2504 if ((pf_mtag = pf_get_mtag(m)) == NULL) { 2505 free(pfse, M_PFTEMP); 2506 m_freem(m); 2507 return; 2508 } 2509 if (tag) 2510 m->m_flags |= M_SKIP_FIREWALL; 2511 pf_mtag->tag = rtag; 2512 2513 if (r != NULL && r->rtableid >= 0) 2514 M_SETFIB(m, r->rtableid); 2515 2516 #ifdef ALTQ 2517 if (r != NULL && r->qid) { 2518 pf_mtag->qid = r->qid; 2519 2520 /* add hints for ecn */ 2521 pf_mtag->hdr = mtod(m, struct ip *); 2522 } 2523 #endif /* ALTQ */ 2524 m->m_data += max_linkhdr; 2525 m->m_pkthdr.len = m->m_len = len; 2526 m->m_pkthdr.rcvif = NULL; 2527 bzero(m->m_data, len); 2528 switch (af) { 2529 #ifdef INET 2530 case AF_INET: 2531 h = mtod(m, struct ip *); 2532 2533 /* IP header fields included in the TCP checksum */ 2534 h->ip_p = IPPROTO_TCP; 2535 h->ip_len = htons(tlen); 2536 h->ip_src.s_addr = saddr->v4.s_addr; 2537 h->ip_dst.s_addr = daddr->v4.s_addr; 2538 2539 th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 2540 break; 2541 #endif /* INET */ 2542 #ifdef INET6 2543 case AF_INET6: 2544 h6 = mtod(m, struct ip6_hdr *); 2545 2546 /* IP header fields included in the TCP checksum */ 2547 h6->ip6_nxt = IPPROTO_TCP; 2548 h6->ip6_plen = htons(tlen); 2549 memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 2550 memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 2551 2552 th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 2553 break; 2554 #endif /* INET6 */ 2555 } 2556 2557 /* TCP header */ 2558 th->th_sport = sport; 2559 th->th_dport = dport; 2560 th->th_seq = htonl(seq); 2561 th->th_ack = htonl(ack); 2562 th->th_off = tlen >> 2; 2563 th->th_flags = flags; 2564 th->th_win = htons(win); 2565 2566 if (mss) { 2567 opt = (char *)(th + 1); 2568 opt[0] = TCPOPT_MAXSEG; 2569 opt[1] = 4; 2570 HTONS(mss); 2571 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2); 2572 } 2573 2574 switch (af) { 2575 #ifdef INET 2576 case AF_INET: 2577 /* TCP checksum */ 2578 th->th_sum = in_cksum(m, len); 2579 2580 /* Finish the IP header */ 2581 h->ip_v = 4; 2582 h->ip_hl = sizeof(*h) >> 2; 2583 h->ip_tos = IPTOS_LOWDELAY; 2584 h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 2585 h->ip_len = htons(len); 2586 h->ip_ttl = ttl ? ttl : V_ip_defttl; 2587 h->ip_sum = 0; 2588 2589 pfse->pfse_type = PFSE_IP; 2590 break; 2591 #endif /* INET */ 2592 #ifdef INET6 2593 case AF_INET6: 2594 /* TCP checksum */ 2595 th->th_sum = in6_cksum(m, IPPROTO_TCP, 2596 sizeof(struct ip6_hdr), tlen); 2597 2598 h6->ip6_vfc |= IPV6_VERSION; 2599 h6->ip6_hlim = IPV6_DEFHLIM; 2600 2601 pfse->pfse_type = PFSE_IP6; 2602 break; 2603 #endif /* INET6 */ 2604 } 2605 pfse->pfse_m = m; 2606 pf_send(pfse); 2607 } 2608 2609 static void 2610 pf_return(struct pf_krule *r, struct pf_krule *nr, struct pf_pdesc *pd, 2611 struct pf_state_key *sk, int off, struct mbuf *m, struct tcphdr *th, 2612 struct pfi_kkif *kif, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen, 2613 u_short *reason) 2614 { 2615 struct pf_addr * const saddr = pd->src; 2616 struct pf_addr * const daddr = pd->dst; 2617 sa_family_t af = pd->af; 2618 2619 /* undo NAT changes, if they have taken place */ 2620 if (nr != NULL) { 2621 PF_ACPY(saddr, &sk->addr[pd->sidx], af); 2622 PF_ACPY(daddr, &sk->addr[pd->didx], af); 2623 if (pd->sport) 2624 *pd->sport = sk->port[pd->sidx]; 2625 if (pd->dport) 2626 *pd->dport = sk->port[pd->didx]; 2627 if (pd->proto_sum) 2628 *pd->proto_sum = bproto_sum; 2629 if (pd->ip_sum) 2630 *pd->ip_sum = bip_sum; 2631 m_copyback(m, off, hdrlen, pd->hdr.any); 2632 } 2633 if (pd->proto == IPPROTO_TCP && 2634 ((r->rule_flag & PFRULE_RETURNRST) || 2635 (r->rule_flag & PFRULE_RETURN)) && 2636 !(th->th_flags & TH_RST)) { 2637 u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 2638 int len = 0; 2639 #ifdef INET 2640 struct ip *h4; 2641 #endif 2642 #ifdef INET6 2643 struct ip6_hdr *h6; 2644 #endif 2645 2646 switch (af) { 2647 #ifdef INET 2648 case AF_INET: 2649 h4 = mtod(m, struct ip *); 2650 len = ntohs(h4->ip_len) - off; 2651 break; 2652 #endif 2653 #ifdef INET6 2654 case AF_INET6: 2655 h6 = mtod(m, struct ip6_hdr *); 2656 len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); 2657 break; 2658 #endif 2659 } 2660 2661 if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) 2662 REASON_SET(reason, PFRES_PROTCKSUM); 2663 else { 2664 if (th->th_flags & TH_SYN) 2665 ack++; 2666 if (th->th_flags & TH_FIN) 2667 ack++; 2668 pf_send_tcp(m, r, af, pd->dst, 2669 pd->src, th->th_dport, th->th_sport, 2670 ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 2671 r->return_ttl, 1, 0, kif->pfik_ifp); 2672 } 2673 } else if (pd->proto != IPPROTO_ICMP && af == AF_INET && 2674 r->return_icmp) 2675 pf_send_icmp(m, r->return_icmp >> 8, 2676 r->return_icmp & 255, af, r); 2677 else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && 2678 r->return_icmp6) 2679 pf_send_icmp(m, r->return_icmp6 >> 8, 2680 r->return_icmp6 & 255, af, r); 2681 } 2682 2683 static int 2684 pf_ieee8021q_setpcp(struct mbuf *m, u_int8_t prio) 2685 { 2686 struct m_tag *mtag; 2687 2688 KASSERT(prio <= PF_PRIO_MAX, 2689 ("%s with invalid pcp", __func__)); 2690 2691 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL); 2692 if (mtag == NULL) { 2693 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_OUT, 2694 sizeof(uint8_t), M_NOWAIT); 2695 if (mtag == NULL) 2696 return (ENOMEM); 2697 m_tag_prepend(m, mtag); 2698 } 2699 2700 *(uint8_t *)(mtag + 1) = prio; 2701 return (0); 2702 } 2703 2704 static int 2705 pf_match_ieee8021q_pcp(u_int8_t prio, struct mbuf *m) 2706 { 2707 struct m_tag *mtag; 2708 u_int8_t mpcp; 2709 2710 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL); 2711 if (mtag == NULL) 2712 return (0); 2713 2714 if (prio == PF_PRIO_ZERO) 2715 prio = 0; 2716 2717 mpcp = *(uint8_t *)(mtag + 1); 2718 2719 return (mpcp == prio); 2720 } 2721 2722 static void 2723 pf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af, 2724 struct pf_krule *r) 2725 { 2726 struct pf_send_entry *pfse; 2727 struct mbuf *m0; 2728 struct pf_mtag *pf_mtag; 2729 2730 /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2731 pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2732 if (pfse == NULL) 2733 return; 2734 2735 if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 2736 free(pfse, M_PFTEMP); 2737 return; 2738 } 2739 2740 if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 2741 free(pfse, M_PFTEMP); 2742 return; 2743 } 2744 /* XXX: revisit */ 2745 m0->m_flags |= M_SKIP_FIREWALL; 2746 2747 if (r->rtableid >= 0) 2748 M_SETFIB(m0, r->rtableid); 2749 2750 #ifdef ALTQ 2751 if (r->qid) { 2752 pf_mtag->qid = r->qid; 2753 /* add hints for ecn */ 2754 pf_mtag->hdr = mtod(m0, struct ip *); 2755 } 2756 #endif /* ALTQ */ 2757 2758 switch (af) { 2759 #ifdef INET 2760 case AF_INET: 2761 pfse->pfse_type = PFSE_ICMP; 2762 break; 2763 #endif /* INET */ 2764 #ifdef INET6 2765 case AF_INET6: 2766 pfse->pfse_type = PFSE_ICMP6; 2767 break; 2768 #endif /* INET6 */ 2769 } 2770 pfse->pfse_m = m0; 2771 pfse->icmpopts.type = type; 2772 pfse->icmpopts.code = code; 2773 pf_send(pfse); 2774 } 2775 2776 /* 2777 * Return 1 if the addresses a and b match (with mask m), otherwise return 0. 2778 * If n is 0, they match if they are equal. If n is != 0, they match if they 2779 * are different. 2780 */ 2781 int 2782 pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, 2783 struct pf_addr *b, sa_family_t af) 2784 { 2785 int match = 0; 2786 2787 switch (af) { 2788 #ifdef INET 2789 case AF_INET: 2790 if ((a->addr32[0] & m->addr32[0]) == 2791 (b->addr32[0] & m->addr32[0])) 2792 match++; 2793 break; 2794 #endif /* INET */ 2795 #ifdef INET6 2796 case AF_INET6: 2797 if (((a->addr32[0] & m->addr32[0]) == 2798 (b->addr32[0] & m->addr32[0])) && 2799 ((a->addr32[1] & m->addr32[1]) == 2800 (b->addr32[1] & m->addr32[1])) && 2801 ((a->addr32[2] & m->addr32[2]) == 2802 (b->addr32[2] & m->addr32[2])) && 2803 ((a->addr32[3] & m->addr32[3]) == 2804 (b->addr32[3] & m->addr32[3]))) 2805 match++; 2806 break; 2807 #endif /* INET6 */ 2808 } 2809 if (match) { 2810 if (n) 2811 return (0); 2812 else 2813 return (1); 2814 } else { 2815 if (n) 2816 return (1); 2817 else 2818 return (0); 2819 } 2820 } 2821 2822 /* 2823 * Return 1 if b <= a <= e, otherwise return 0. 2824 */ 2825 int 2826 pf_match_addr_range(struct pf_addr *b, struct pf_addr *e, 2827 struct pf_addr *a, sa_family_t af) 2828 { 2829 switch (af) { 2830 #ifdef INET 2831 case AF_INET: 2832 if ((ntohl(a->addr32[0]) < ntohl(b->addr32[0])) || 2833 (ntohl(a->addr32[0]) > ntohl(e->addr32[0]))) 2834 return (0); 2835 break; 2836 #endif /* INET */ 2837 #ifdef INET6 2838 case AF_INET6: { 2839 int i; 2840 2841 /* check a >= b */ 2842 for (i = 0; i < 4; ++i) 2843 if (ntohl(a->addr32[i]) > ntohl(b->addr32[i])) 2844 break; 2845 else if (ntohl(a->addr32[i]) < ntohl(b->addr32[i])) 2846 return (0); 2847 /* check a <= e */ 2848 for (i = 0; i < 4; ++i) 2849 if (ntohl(a->addr32[i]) < ntohl(e->addr32[i])) 2850 break; 2851 else if (ntohl(a->addr32[i]) > ntohl(e->addr32[i])) 2852 return (0); 2853 break; 2854 } 2855 #endif /* INET6 */ 2856 } 2857 return (1); 2858 } 2859 2860 static int 2861 pf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 2862 { 2863 switch (op) { 2864 case PF_OP_IRG: 2865 return ((p > a1) && (p < a2)); 2866 case PF_OP_XRG: 2867 return ((p < a1) || (p > a2)); 2868 case PF_OP_RRG: 2869 return ((p >= a1) && (p <= a2)); 2870 case PF_OP_EQ: 2871 return (p == a1); 2872 case PF_OP_NE: 2873 return (p != a1); 2874 case PF_OP_LT: 2875 return (p < a1); 2876 case PF_OP_LE: 2877 return (p <= a1); 2878 case PF_OP_GT: 2879 return (p > a1); 2880 case PF_OP_GE: 2881 return (p >= a1); 2882 } 2883 return (0); /* never reached */ 2884 } 2885 2886 int 2887 pf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 2888 { 2889 NTOHS(a1); 2890 NTOHS(a2); 2891 NTOHS(p); 2892 return (pf_match(op, a1, a2, p)); 2893 } 2894 2895 static int 2896 pf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 2897 { 2898 if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2899 return (0); 2900 return (pf_match(op, a1, a2, u)); 2901 } 2902 2903 static int 2904 pf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 2905 { 2906 if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2907 return (0); 2908 return (pf_match(op, a1, a2, g)); 2909 } 2910 2911 int 2912 pf_match_tag(struct mbuf *m, struct pf_krule *r, int *tag, int mtag) 2913 { 2914 if (*tag == -1) 2915 *tag = mtag; 2916 2917 return ((!r->match_tag_not && r->match_tag == *tag) || 2918 (r->match_tag_not && r->match_tag != *tag)); 2919 } 2920 2921 int 2922 pf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag) 2923 { 2924 2925 KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 2926 2927 if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL)) 2928 return (ENOMEM); 2929 2930 pd->pf_mtag->tag = tag; 2931 2932 return (0); 2933 } 2934 2935 #define PF_ANCHOR_STACKSIZE 32 2936 struct pf_kanchor_stackframe { 2937 struct pf_kruleset *rs; 2938 struct pf_krule *r; /* XXX: + match bit */ 2939 struct pf_kanchor *child; 2940 }; 2941 2942 /* 2943 * XXX: We rely on malloc(9) returning pointer aligned addresses. 2944 */ 2945 #define PF_ANCHORSTACK_MATCH 0x00000001 2946 #define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 2947 2948 #define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 2949 #define PF_ANCHOR_RULE(f) (struct pf_krule *) \ 2950 ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 2951 #define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 2952 ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 2953 } while (0) 2954 2955 void 2956 pf_step_into_anchor(struct pf_kanchor_stackframe *stack, int *depth, 2957 struct pf_kruleset **rs, int n, struct pf_krule **r, struct pf_krule **a, 2958 int *match) 2959 { 2960 struct pf_kanchor_stackframe *f; 2961 2962 PF_RULES_RASSERT(); 2963 2964 if (match) 2965 *match = 0; 2966 if (*depth >= PF_ANCHOR_STACKSIZE) { 2967 printf("%s: anchor stack overflow on %s\n", 2968 __func__, (*r)->anchor->name); 2969 *r = TAILQ_NEXT(*r, entries); 2970 return; 2971 } else if (*depth == 0 && a != NULL) 2972 *a = *r; 2973 f = stack + (*depth)++; 2974 f->rs = *rs; 2975 f->r = *r; 2976 if ((*r)->anchor_wildcard) { 2977 struct pf_kanchor_node *parent = &(*r)->anchor->children; 2978 2979 if ((f->child = RB_MIN(pf_kanchor_node, parent)) == NULL) { 2980 *r = NULL; 2981 return; 2982 } 2983 *rs = &f->child->ruleset; 2984 } else { 2985 f->child = NULL; 2986 *rs = &(*r)->anchor->ruleset; 2987 } 2988 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2989 } 2990 2991 int 2992 pf_step_out_of_anchor(struct pf_kanchor_stackframe *stack, int *depth, 2993 struct pf_kruleset **rs, int n, struct pf_krule **r, struct pf_krule **a, 2994 int *match) 2995 { 2996 struct pf_kanchor_stackframe *f; 2997 struct pf_krule *fr; 2998 int quick = 0; 2999 3000 PF_RULES_RASSERT(); 3001 3002 do { 3003 if (*depth <= 0) 3004 break; 3005 f = stack + *depth - 1; 3006 fr = PF_ANCHOR_RULE(f); 3007 if (f->child != NULL) { 3008 struct pf_kanchor_node *parent; 3009 3010 /* 3011 * This block traverses through 3012 * a wildcard anchor. 3013 */ 3014 parent = &fr->anchor->children; 3015 if (match != NULL && *match) { 3016 /* 3017 * If any of "*" matched, then 3018 * "foo/ *" matched, mark frame 3019 * appropriately. 3020 */ 3021 PF_ANCHOR_SET_MATCH(f); 3022 *match = 0; 3023 } 3024 f->child = RB_NEXT(pf_kanchor_node, parent, f->child); 3025 if (f->child != NULL) { 3026 *rs = &f->child->ruleset; 3027 *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 3028 if (*r == NULL) 3029 continue; 3030 else 3031 break; 3032 } 3033 } 3034 (*depth)--; 3035 if (*depth == 0 && a != NULL) 3036 *a = NULL; 3037 *rs = f->rs; 3038 if (PF_ANCHOR_MATCH(f) || (match != NULL && *match)) 3039 quick = fr->quick; 3040 *r = TAILQ_NEXT(fr, entries); 3041 } while (*r == NULL); 3042 3043 return (quick); 3044 } 3045 3046 #ifdef INET6 3047 void 3048 pf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 3049 struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 3050 { 3051 switch (af) { 3052 #ifdef INET 3053 case AF_INET: 3054 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 3055 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 3056 break; 3057 #endif /* INET */ 3058 case AF_INET6: 3059 naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 3060 ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 3061 naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 3062 ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 3063 naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 3064 ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 3065 naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 3066 ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 3067 break; 3068 } 3069 } 3070 3071 void 3072 pf_addr_inc(struct pf_addr *addr, sa_family_t af) 3073 { 3074 switch (af) { 3075 #ifdef INET 3076 case AF_INET: 3077 addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 3078 break; 3079 #endif /* INET */ 3080 case AF_INET6: 3081 if (addr->addr32[3] == 0xffffffff) { 3082 addr->addr32[3] = 0; 3083 if (addr->addr32[2] == 0xffffffff) { 3084 addr->addr32[2] = 0; 3085 if (addr->addr32[1] == 0xffffffff) { 3086 addr->addr32[1] = 0; 3087 addr->addr32[0] = 3088 htonl(ntohl(addr->addr32[0]) + 1); 3089 } else 3090 addr->addr32[1] = 3091 htonl(ntohl(addr->addr32[1]) + 1); 3092 } else 3093 addr->addr32[2] = 3094 htonl(ntohl(addr->addr32[2]) + 1); 3095 } else 3096 addr->addr32[3] = 3097 htonl(ntohl(addr->addr32[3]) + 1); 3098 break; 3099 } 3100 } 3101 #endif /* INET6 */ 3102 3103 int 3104 pf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m) 3105 { 3106 struct pf_addr *saddr, *daddr; 3107 u_int16_t sport, dport; 3108 struct inpcbinfo *pi; 3109 struct inpcb *inp; 3110 3111 pd->lookup.uid = UID_MAX; 3112 pd->lookup.gid = GID_MAX; 3113 3114 switch (pd->proto) { 3115 case IPPROTO_TCP: 3116 sport = pd->hdr.tcp.th_sport; 3117 dport = pd->hdr.tcp.th_dport; 3118 pi = &V_tcbinfo; 3119 break; 3120 case IPPROTO_UDP: 3121 sport = pd->hdr.udp.uh_sport; 3122 dport = pd->hdr.udp.uh_dport; 3123 pi = &V_udbinfo; 3124 break; 3125 default: 3126 return (-1); 3127 } 3128 if (direction == PF_IN) { 3129 saddr = pd->src; 3130 daddr = pd->dst; 3131 } else { 3132 u_int16_t p; 3133 3134 p = sport; 3135 sport = dport; 3136 dport = p; 3137 saddr = pd->dst; 3138 daddr = pd->src; 3139 } 3140 switch (pd->af) { 3141 #ifdef INET 3142 case AF_INET: 3143 inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 3144 dport, INPLOOKUP_RLOCKPCB, NULL, m); 3145 if (inp == NULL) { 3146 inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 3147 daddr->v4, dport, INPLOOKUP_WILDCARD | 3148 INPLOOKUP_RLOCKPCB, NULL, m); 3149 if (inp == NULL) 3150 return (-1); 3151 } 3152 break; 3153 #endif /* INET */ 3154 #ifdef INET6 3155 case AF_INET6: 3156 inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 3157 dport, INPLOOKUP_RLOCKPCB, NULL, m); 3158 if (inp == NULL) { 3159 inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 3160 &daddr->v6, dport, INPLOOKUP_WILDCARD | 3161 INPLOOKUP_RLOCKPCB, NULL, m); 3162 if (inp == NULL) 3163 return (-1); 3164 } 3165 break; 3166 #endif /* INET6 */ 3167 3168 default: 3169 return (-1); 3170 } 3171 INP_RLOCK_ASSERT(inp); 3172 pd->lookup.uid = inp->inp_cred->cr_uid; 3173 pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3174 INP_RUNLOCK(inp); 3175 3176 return (1); 3177 } 3178 3179 static u_int8_t 3180 pf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 3181 { 3182 int hlen; 3183 u_int8_t hdr[60]; 3184 u_int8_t *opt, optlen; 3185 u_int8_t wscale = 0; 3186 3187 hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 3188 if (hlen <= sizeof(struct tcphdr)) 3189 return (0); 3190 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 3191 return (0); 3192 opt = hdr + sizeof(struct tcphdr); 3193 hlen -= sizeof(struct tcphdr); 3194 while (hlen >= 3) { 3195 switch (*opt) { 3196 case TCPOPT_EOL: 3197 case TCPOPT_NOP: 3198 ++opt; 3199 --hlen; 3200 break; 3201 case TCPOPT_WINDOW: 3202 wscale = opt[2]; 3203 if (wscale > TCP_MAX_WINSHIFT) 3204 wscale = TCP_MAX_WINSHIFT; 3205 wscale |= PF_WSCALE_FLAG; 3206 /* FALLTHROUGH */ 3207 default: 3208 optlen = opt[1]; 3209 if (optlen < 2) 3210 optlen = 2; 3211 hlen -= optlen; 3212 opt += optlen; 3213 break; 3214 } 3215 } 3216 return (wscale); 3217 } 3218 3219 static u_int16_t 3220 pf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 3221 { 3222 int hlen; 3223 u_int8_t hdr[60]; 3224 u_int8_t *opt, optlen; 3225 u_int16_t mss = V_tcp_mssdflt; 3226 3227 hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 3228 if (hlen <= sizeof(struct tcphdr)) 3229 return (0); 3230 if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 3231 return (0); 3232 opt = hdr + sizeof(struct tcphdr); 3233 hlen -= sizeof(struct tcphdr); 3234 while (hlen >= TCPOLEN_MAXSEG) { 3235 switch (*opt) { 3236 case TCPOPT_EOL: 3237 case TCPOPT_NOP: 3238 ++opt; 3239 --hlen; 3240 break; 3241 case TCPOPT_MAXSEG: 3242 bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2); 3243 NTOHS(mss); 3244 /* FALLTHROUGH */ 3245 default: 3246 optlen = opt[1]; 3247 if (optlen < 2) 3248 optlen = 2; 3249 hlen -= optlen; 3250 opt += optlen; 3251 break; 3252 } 3253 } 3254 return (mss); 3255 } 3256 3257 static u_int16_t 3258 pf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 3259 { 3260 struct nhop_object *nh; 3261 #ifdef INET6 3262 struct in6_addr dst6; 3263 uint32_t scopeid; 3264 #endif /* INET6 */ 3265 int hlen = 0; 3266 uint16_t mss = 0; 3267 3268 NET_EPOCH_ASSERT(); 3269 3270 switch (af) { 3271 #ifdef INET 3272 case AF_INET: 3273 hlen = sizeof(struct ip); 3274 nh = fib4_lookup(rtableid, addr->v4, 0, 0, 0); 3275 if (nh != NULL) 3276 mss = nh->nh_mtu - hlen - sizeof(struct tcphdr); 3277 break; 3278 #endif /* INET */ 3279 #ifdef INET6 3280 case AF_INET6: 3281 hlen = sizeof(struct ip6_hdr); 3282 in6_splitscope(&addr->v6, &dst6, &scopeid); 3283 nh = fib6_lookup(rtableid, &dst6, scopeid, 0, 0); 3284 if (nh != NULL) 3285 mss = nh->nh_mtu - hlen - sizeof(struct tcphdr); 3286 break; 3287 #endif /* INET6 */ 3288 } 3289 3290 mss = max(V_tcp_mssdflt, mss); 3291 mss = min(mss, offer); 3292 mss = max(mss, 64); /* sanity - at least max opt space */ 3293 return (mss); 3294 } 3295 3296 static u_int32_t 3297 pf_tcp_iss(struct pf_pdesc *pd) 3298 { 3299 MD5_CTX ctx; 3300 u_int32_t digest[4]; 3301 3302 if (V_pf_tcp_secret_init == 0) { 3303 arc4random_buf(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 3304 MD5Init(&V_pf_tcp_secret_ctx); 3305 MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 3306 sizeof(V_pf_tcp_secret)); 3307 V_pf_tcp_secret_init = 1; 3308 } 3309 3310 ctx = V_pf_tcp_secret_ctx; 3311 3312 MD5Update(&ctx, (char *)&pd->hdr.tcp.th_sport, sizeof(u_short)); 3313 MD5Update(&ctx, (char *)&pd->hdr.tcp.th_dport, sizeof(u_short)); 3314 if (pd->af == AF_INET6) { 3315 MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr)); 3316 MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr)); 3317 } else { 3318 MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr)); 3319 MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr)); 3320 } 3321 MD5Final((u_char *)digest, &ctx); 3322 V_pf_tcp_iss_off += 4096; 3323 #define ISN_RANDOM_INCREMENT (4096 - 1) 3324 return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 3325 V_pf_tcp_iss_off); 3326 #undef ISN_RANDOM_INCREMENT 3327 } 3328 3329 static int 3330 pf_test_rule(struct pf_krule **rm, struct pf_state **sm, int direction, 3331 struct pfi_kkif *kif, struct mbuf *m, int off, struct pf_pdesc *pd, 3332 struct pf_krule **am, struct pf_kruleset **rsm, struct inpcb *inp) 3333 { 3334 struct pf_krule *nr = NULL; 3335 struct pf_addr * const saddr = pd->src; 3336 struct pf_addr * const daddr = pd->dst; 3337 sa_family_t af = pd->af; 3338 struct pf_krule *r, *a = NULL; 3339 struct pf_kruleset *ruleset = NULL; 3340 struct pf_ksrc_node *nsn = NULL; 3341 struct tcphdr *th = &pd->hdr.tcp; 3342 struct pf_state_key *sk = NULL, *nk = NULL; 3343 u_short reason; 3344 int rewrite = 0, hdrlen = 0; 3345 int tag = -1, rtableid = -1; 3346 int asd = 0; 3347 int match = 0; 3348 int state_icmp = 0; 3349 u_int16_t sport = 0, dport = 0; 3350 u_int16_t bproto_sum = 0, bip_sum = 0; 3351 u_int8_t icmptype = 0, icmpcode = 0; 3352 struct pf_kanchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3353 3354 PF_RULES_RASSERT(); 3355 3356 if (inp != NULL) { 3357 INP_LOCK_ASSERT(inp); 3358 pd->lookup.uid = inp->inp_cred->cr_uid; 3359 pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3360 pd->lookup.done = 1; 3361 } 3362 3363 switch (pd->proto) { 3364 case IPPROTO_TCP: 3365 sport = th->th_sport; 3366 dport = th->th_dport; 3367 hdrlen = sizeof(*th); 3368 break; 3369 case IPPROTO_UDP: 3370 sport = pd->hdr.udp.uh_sport; 3371 dport = pd->hdr.udp.uh_dport; 3372 hdrlen = sizeof(pd->hdr.udp); 3373 break; 3374 #ifdef INET 3375 case IPPROTO_ICMP: 3376 if (pd->af != AF_INET) 3377 break; 3378 sport = dport = pd->hdr.icmp.icmp_id; 3379 hdrlen = sizeof(pd->hdr.icmp); 3380 icmptype = pd->hdr.icmp.icmp_type; 3381 icmpcode = pd->hdr.icmp.icmp_code; 3382 3383 if (icmptype == ICMP_UNREACH || 3384 icmptype == ICMP_SOURCEQUENCH || 3385 icmptype == ICMP_REDIRECT || 3386 icmptype == ICMP_TIMXCEED || 3387 icmptype == ICMP_PARAMPROB) 3388 state_icmp++; 3389 break; 3390 #endif /* INET */ 3391 #ifdef INET6 3392 case IPPROTO_ICMPV6: 3393 if (af != AF_INET6) 3394 break; 3395 sport = dport = pd->hdr.icmp6.icmp6_id; 3396 hdrlen = sizeof(pd->hdr.icmp6); 3397 icmptype = pd->hdr.icmp6.icmp6_type; 3398 icmpcode = pd->hdr.icmp6.icmp6_code; 3399 3400 if (icmptype == ICMP6_DST_UNREACH || 3401 icmptype == ICMP6_PACKET_TOO_BIG || 3402 icmptype == ICMP6_TIME_EXCEEDED || 3403 icmptype == ICMP6_PARAM_PROB) 3404 state_icmp++; 3405 break; 3406 #endif /* INET6 */ 3407 default: 3408 sport = dport = hdrlen = 0; 3409 break; 3410 } 3411 3412 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3413 3414 /* check packet for BINAT/NAT/RDR */ 3415 if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk, 3416 &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) { 3417 KASSERT(sk != NULL, ("%s: null sk", __func__)); 3418 KASSERT(nk != NULL, ("%s: null nk", __func__)); 3419 3420 if (pd->ip_sum) 3421 bip_sum = *pd->ip_sum; 3422 3423 switch (pd->proto) { 3424 case IPPROTO_TCP: 3425 bproto_sum = th->th_sum; 3426 pd->proto_sum = &th->th_sum; 3427 3428 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3429 nk->port[pd->sidx] != sport) { 3430 pf_change_ap(m, saddr, &th->th_sport, pd->ip_sum, 3431 &th->th_sum, &nk->addr[pd->sidx], 3432 nk->port[pd->sidx], 0, af); 3433 pd->sport = &th->th_sport; 3434 sport = th->th_sport; 3435 } 3436 3437 if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3438 nk->port[pd->didx] != dport) { 3439 pf_change_ap(m, daddr, &th->th_dport, pd->ip_sum, 3440 &th->th_sum, &nk->addr[pd->didx], 3441 nk->port[pd->didx], 0, af); 3442 dport = th->th_dport; 3443 pd->dport = &th->th_dport; 3444 } 3445 rewrite++; 3446 break; 3447 case IPPROTO_UDP: 3448 bproto_sum = pd->hdr.udp.uh_sum; 3449 pd->proto_sum = &pd->hdr.udp.uh_sum; 3450 3451 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3452 nk->port[pd->sidx] != sport) { 3453 pf_change_ap(m, saddr, &pd->hdr.udp.uh_sport, 3454 pd->ip_sum, &pd->hdr.udp.uh_sum, 3455 &nk->addr[pd->sidx], 3456 nk->port[pd->sidx], 1, af); 3457 sport = pd->hdr.udp.uh_sport; 3458 pd->sport = &pd->hdr.udp.uh_sport; 3459 } 3460 3461 if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3462 nk->port[pd->didx] != dport) { 3463 pf_change_ap(m, daddr, &pd->hdr.udp.uh_dport, 3464 pd->ip_sum, &pd->hdr.udp.uh_sum, 3465 &nk->addr[pd->didx], 3466 nk->port[pd->didx], 1, af); 3467 dport = pd->hdr.udp.uh_dport; 3468 pd->dport = &pd->hdr.udp.uh_dport; 3469 } 3470 rewrite++; 3471 break; 3472 #ifdef INET 3473 case IPPROTO_ICMP: 3474 nk->port[0] = nk->port[1]; 3475 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET)) 3476 pf_change_a(&saddr->v4.s_addr, pd->ip_sum, 3477 nk->addr[pd->sidx].v4.s_addr, 0); 3478 3479 if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET)) 3480 pf_change_a(&daddr->v4.s_addr, pd->ip_sum, 3481 nk->addr[pd->didx].v4.s_addr, 0); 3482 3483 if (nk->port[1] != pd->hdr.icmp.icmp_id) { 3484 pd->hdr.icmp.icmp_cksum = pf_cksum_fixup( 3485 pd->hdr.icmp.icmp_cksum, sport, 3486 nk->port[1], 0); 3487 pd->hdr.icmp.icmp_id = nk->port[1]; 3488 pd->sport = &pd->hdr.icmp.icmp_id; 3489 } 3490 m_copyback(m, off, ICMP_MINLEN, (caddr_t)&pd->hdr.icmp); 3491 break; 3492 #endif /* INET */ 3493 #ifdef INET6 3494 case IPPROTO_ICMPV6: 3495 nk->port[0] = nk->port[1]; 3496 if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6)) 3497 pf_change_a6(saddr, &pd->hdr.icmp6.icmp6_cksum, 3498 &nk->addr[pd->sidx], 0); 3499 3500 if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6)) 3501 pf_change_a6(daddr, &pd->hdr.icmp6.icmp6_cksum, 3502 &nk->addr[pd->didx], 0); 3503 rewrite++; 3504 break; 3505 #endif /* INET */ 3506 default: 3507 switch (af) { 3508 #ifdef INET 3509 case AF_INET: 3510 if (PF_ANEQ(saddr, 3511 &nk->addr[pd->sidx], AF_INET)) 3512 pf_change_a(&saddr->v4.s_addr, 3513 pd->ip_sum, 3514 nk->addr[pd->sidx].v4.s_addr, 0); 3515 3516 if (PF_ANEQ(daddr, 3517 &nk->addr[pd->didx], AF_INET)) 3518 pf_change_a(&daddr->v4.s_addr, 3519 pd->ip_sum, 3520 nk->addr[pd->didx].v4.s_addr, 0); 3521 break; 3522 #endif /* INET */ 3523 #ifdef INET6 3524 case AF_INET6: 3525 if (PF_ANEQ(saddr, 3526 &nk->addr[pd->sidx], AF_INET6)) 3527 PF_ACPY(saddr, &nk->addr[pd->sidx], af); 3528 3529 if (PF_ANEQ(daddr, 3530 &nk->addr[pd->didx], AF_INET6)) 3531 PF_ACPY(daddr, &nk->addr[pd->didx], af); 3532 break; 3533 #endif /* INET */ 3534 } 3535 break; 3536 } 3537 if (nr->natpass) 3538 r = NULL; 3539 pd->nat_rule = nr; 3540 } 3541 3542 while (r != NULL) { 3543 counter_u64_add(r->evaluations, 1); 3544 if (pfi_kkif_match(r->kif, kif) == r->ifnot) 3545 r = r->skip[PF_SKIP_IFP].ptr; 3546 else if (r->direction && r->direction != direction) 3547 r = r->skip[PF_SKIP_DIR].ptr; 3548 else if (r->af && r->af != af) 3549 r = r->skip[PF_SKIP_AF].ptr; 3550 else if (r->proto && r->proto != pd->proto) 3551 r = r->skip[PF_SKIP_PROTO].ptr; 3552 else if (PF_MISMATCHAW(&r->src.addr, saddr, af, 3553 r->src.neg, kif, M_GETFIB(m))) 3554 r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3555 /* tcp/udp only. port_op always 0 in other cases */ 3556 else if (r->src.port_op && !pf_match_port(r->src.port_op, 3557 r->src.port[0], r->src.port[1], sport)) 3558 r = r->skip[PF_SKIP_SRC_PORT].ptr; 3559 else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, 3560 r->dst.neg, NULL, M_GETFIB(m))) 3561 r = r->skip[PF_SKIP_DST_ADDR].ptr; 3562 /* tcp/udp only. port_op always 0 in other cases */ 3563 else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 3564 r->dst.port[0], r->dst.port[1], dport)) 3565 r = r->skip[PF_SKIP_DST_PORT].ptr; 3566 /* icmp only. type always 0 in other cases */ 3567 else if (r->type && r->type != icmptype + 1) 3568 r = TAILQ_NEXT(r, entries); 3569 /* icmp only. type always 0 in other cases */ 3570 else if (r->code && r->code != icmpcode + 1) 3571 r = TAILQ_NEXT(r, entries); 3572 else if (r->tos && !(r->tos == pd->tos)) 3573 r = TAILQ_NEXT(r, entries); 3574 else if (r->rule_flag & PFRULE_FRAGMENT) 3575 r = TAILQ_NEXT(r, entries); 3576 else if (pd->proto == IPPROTO_TCP && 3577 (r->flagset & th->th_flags) != r->flags) 3578 r = TAILQ_NEXT(r, entries); 3579 /* tcp/udp only. uid.op always 0 in other cases */ 3580 else if (r->uid.op && (pd->lookup.done || (pd->lookup.done = 3581 pf_socket_lookup(direction, pd, m), 1)) && 3582 !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 3583 pd->lookup.uid)) 3584 r = TAILQ_NEXT(r, entries); 3585 /* tcp/udp only. gid.op always 0 in other cases */ 3586 else if (r->gid.op && (pd->lookup.done || (pd->lookup.done = 3587 pf_socket_lookup(direction, pd, m), 1)) && 3588 !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 3589 pd->lookup.gid)) 3590 r = TAILQ_NEXT(r, entries); 3591 else if (r->prio && 3592 !pf_match_ieee8021q_pcp(r->prio, m)) 3593 r = TAILQ_NEXT(r, entries); 3594 else if (r->prob && 3595 r->prob <= arc4random()) 3596 r = TAILQ_NEXT(r, entries); 3597 else if (r->match_tag && !pf_match_tag(m, r, &tag, 3598 pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3599 r = TAILQ_NEXT(r, entries); 3600 else if (r->os_fingerprint != PF_OSFP_ANY && 3601 (pd->proto != IPPROTO_TCP || !pf_osfp_match( 3602 pf_osfp_fingerprint(pd, m, off, th), 3603 r->os_fingerprint))) 3604 r = TAILQ_NEXT(r, entries); 3605 else { 3606 if (r->tag) 3607 tag = r->tag; 3608 if (r->rtableid >= 0) 3609 rtableid = r->rtableid; 3610 if (r->anchor == NULL) { 3611 match = 1; 3612 *rm = r; 3613 *am = a; 3614 *rsm = ruleset; 3615 if ((*rm)->quick) 3616 break; 3617 r = TAILQ_NEXT(r, entries); 3618 } else 3619 pf_step_into_anchor(anchor_stack, &asd, 3620 &ruleset, PF_RULESET_FILTER, &r, &a, 3621 &match); 3622 } 3623 if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3624 &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3625 break; 3626 } 3627 r = *rm; 3628 a = *am; 3629 ruleset = *rsm; 3630 3631 REASON_SET(&reason, PFRES_MATCH); 3632 3633 if (r->log || (nr != NULL && nr->log)) { 3634 if (rewrite) 3635 m_copyback(m, off, hdrlen, pd->hdr.any); 3636 PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, 3637 ruleset, pd, 1); 3638 } 3639 3640 if ((r->action == PF_DROP) && 3641 ((r->rule_flag & PFRULE_RETURNRST) || 3642 (r->rule_flag & PFRULE_RETURNICMP) || 3643 (r->rule_flag & PFRULE_RETURN))) { 3644 pf_return(r, nr, pd, sk, off, m, th, kif, bproto_sum, 3645 bip_sum, hdrlen, &reason); 3646 } 3647 3648 if (r->action == PF_DROP) 3649 goto cleanup; 3650 3651 if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3652 REASON_SET(&reason, PFRES_MEMORY); 3653 goto cleanup; 3654 } 3655 if (rtableid >= 0) 3656 M_SETFIB(m, rtableid); 3657 3658 if (!state_icmp && (r->keep_state || nr != NULL || 3659 (pd->flags & PFDESC_TCP_NORM))) { 3660 int action; 3661 action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, 3662 sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, 3663 hdrlen); 3664 if (action != PF_PASS) { 3665 if (action == PF_DROP && 3666 (r->rule_flag & PFRULE_RETURN)) 3667 pf_return(r, nr, pd, sk, off, m, th, kif, 3668 bproto_sum, bip_sum, hdrlen, &reason); 3669 return (action); 3670 } 3671 } else { 3672 if (sk != NULL) 3673 uma_zfree(V_pf_state_key_z, sk); 3674 if (nk != NULL) 3675 uma_zfree(V_pf_state_key_z, nk); 3676 } 3677 3678 /* copy back packet headers if we performed NAT operations */ 3679 if (rewrite) 3680 m_copyback(m, off, hdrlen, pd->hdr.any); 3681 3682 if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 3683 direction == PF_OUT && 3684 V_pfsync_defer_ptr != NULL && V_pfsync_defer_ptr(*sm, m)) 3685 /* 3686 * We want the state created, but we dont 3687 * want to send this in case a partner 3688 * firewall has to know about it to allow 3689 * replies through it. 3690 */ 3691 return (PF_DEFER); 3692 3693 return (PF_PASS); 3694 3695 cleanup: 3696 if (sk != NULL) 3697 uma_zfree(V_pf_state_key_z, sk); 3698 if (nk != NULL) 3699 uma_zfree(V_pf_state_key_z, nk); 3700 return (PF_DROP); 3701 } 3702 3703 static int 3704 pf_create_state(struct pf_krule *r, struct pf_krule *nr, struct pf_krule *a, 3705 struct pf_pdesc *pd, struct pf_ksrc_node *nsn, struct pf_state_key *nk, 3706 struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport, 3707 u_int16_t dport, int *rewrite, struct pfi_kkif *kif, struct pf_state **sm, 3708 int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen) 3709 { 3710 struct pf_state *s = NULL; 3711 struct pf_ksrc_node *sn = NULL; 3712 struct tcphdr *th = &pd->hdr.tcp; 3713 u_int16_t mss = V_tcp_mssdflt; 3714 u_short reason; 3715 3716 /* check maximums */ 3717 if (r->max_states && 3718 (counter_u64_fetch(r->states_cur) >= r->max_states)) { 3719 counter_u64_add(V_pf_status.lcounters[LCNT_STATES], 1); 3720 REASON_SET(&reason, PFRES_MAXSTATES); 3721 goto csfailed; 3722 } 3723 /* src node for filter rule */ 3724 if ((r->rule_flag & PFRULE_SRCTRACK || 3725 r->rpool.opts & PF_POOL_STICKYADDR) && 3726 pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) { 3727 REASON_SET(&reason, PFRES_SRCLIMIT); 3728 goto csfailed; 3729 } 3730 /* src node for translation rule */ 3731 if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) && 3732 pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) { 3733 REASON_SET(&reason, PFRES_SRCLIMIT); 3734 goto csfailed; 3735 } 3736 s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO); 3737 if (s == NULL) { 3738 REASON_SET(&reason, PFRES_MEMORY); 3739 goto csfailed; 3740 } 3741 for (int i = 0; i < 2; i++) { 3742 s->bytes[i] = counter_u64_alloc(M_NOWAIT); 3743 s->packets[i] = counter_u64_alloc(M_NOWAIT); 3744 3745 if (s->bytes[i] == NULL || s->packets[i] == NULL) { 3746 pf_free_state(s); 3747 REASON_SET(&reason, PFRES_MEMORY); 3748 goto csfailed; 3749 } 3750 } 3751 s->rule.ptr = r; 3752 s->nat_rule.ptr = nr; 3753 s->anchor.ptr = a; 3754 STATE_INC_COUNTERS(s); 3755 if (r->allow_opts) 3756 s->state_flags |= PFSTATE_ALLOWOPTS; 3757 if (r->rule_flag & PFRULE_STATESLOPPY) 3758 s->state_flags |= PFSTATE_SLOPPY; 3759 s->log = r->log & PF_LOG_ALL; 3760 s->sync_state = PFSYNC_S_NONE; 3761 if (nr != NULL) 3762 s->log |= nr->log & PF_LOG_ALL; 3763 switch (pd->proto) { 3764 case IPPROTO_TCP: 3765 s->src.seqlo = ntohl(th->th_seq); 3766 s->src.seqhi = s->src.seqlo + pd->p_len + 1; 3767 if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN && 3768 r->keep_state == PF_STATE_MODULATE) { 3769 /* Generate sequence number modulator */ 3770 if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 3771 0) 3772 s->src.seqdiff = 1; 3773 pf_change_proto_a(m, &th->th_seq, &th->th_sum, 3774 htonl(s->src.seqlo + s->src.seqdiff), 0); 3775 *rewrite = 1; 3776 } else 3777 s->src.seqdiff = 0; 3778 if (th->th_flags & TH_SYN) { 3779 s->src.seqhi++; 3780 s->src.wscale = pf_get_wscale(m, off, 3781 th->th_off, pd->af); 3782 } 3783 s->src.max_win = MAX(ntohs(th->th_win), 1); 3784 if (s->src.wscale & PF_WSCALE_MASK) { 3785 /* Remove scale factor from initial window */ 3786 int win = s->src.max_win; 3787 win += 1 << (s->src.wscale & PF_WSCALE_MASK); 3788 s->src.max_win = (win - 1) >> 3789 (s->src.wscale & PF_WSCALE_MASK); 3790 } 3791 if (th->th_flags & TH_FIN) 3792 s->src.seqhi++; 3793 s->dst.seqhi = 1; 3794 s->dst.max_win = 1; 3795 s->src.state = TCPS_SYN_SENT; 3796 s->dst.state = TCPS_CLOSED; 3797 s->timeout = PFTM_TCP_FIRST_PACKET; 3798 break; 3799 case IPPROTO_UDP: 3800 s->src.state = PFUDPS_SINGLE; 3801 s->dst.state = PFUDPS_NO_TRAFFIC; 3802 s->timeout = PFTM_UDP_FIRST_PACKET; 3803 break; 3804 case IPPROTO_ICMP: 3805 #ifdef INET6 3806 case IPPROTO_ICMPV6: 3807 #endif 3808 s->timeout = PFTM_ICMP_FIRST_PACKET; 3809 break; 3810 default: 3811 s->src.state = PFOTHERS_SINGLE; 3812 s->dst.state = PFOTHERS_NO_TRAFFIC; 3813 s->timeout = PFTM_OTHER_FIRST_PACKET; 3814 } 3815 3816 if (r->rt) { 3817 if (pf_map_addr(pd->af, r, pd->src, &s->rt_addr, NULL, &sn)) { 3818 REASON_SET(&reason, PFRES_MAPFAILED); 3819 pf_src_tree_remove_state(s); 3820 STATE_DEC_COUNTERS(s); 3821 uma_zfree(V_pf_state_z, s); 3822 goto csfailed; 3823 } 3824 s->rt_kif = r->rpool.cur->kif; 3825 } 3826 3827 s->creation = time_uptime; 3828 s->expire = time_uptime; 3829 3830 if (sn != NULL) 3831 s->src_node = sn; 3832 if (nsn != NULL) { 3833 /* XXX We only modify one side for now. */ 3834 PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af); 3835 s->nat_src_node = nsn; 3836 } 3837 if (pd->proto == IPPROTO_TCP) { 3838 if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m, 3839 off, pd, th, &s->src, &s->dst)) { 3840 REASON_SET(&reason, PFRES_MEMORY); 3841 pf_src_tree_remove_state(s); 3842 STATE_DEC_COUNTERS(s); 3843 uma_zfree(V_pf_state_z, s); 3844 return (PF_DROP); 3845 } 3846 if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub && 3847 pf_normalize_tcp_stateful(m, off, pd, &reason, th, s, 3848 &s->src, &s->dst, rewrite)) { 3849 /* This really shouldn't happen!!! */ 3850 DPFPRINTF(PF_DEBUG_URGENT, 3851 ("pf_normalize_tcp_stateful failed on first " 3852 "pkt\n")); 3853 pf_normalize_tcp_cleanup(s); 3854 pf_src_tree_remove_state(s); 3855 STATE_DEC_COUNTERS(s); 3856 uma_zfree(V_pf_state_z, s); 3857 return (PF_DROP); 3858 } 3859 } 3860 s->direction = pd->dir; 3861 3862 /* 3863 * sk/nk could already been setup by pf_get_translation(). 3864 */ 3865 if (nr == NULL) { 3866 KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p", 3867 __func__, nr, sk, nk)); 3868 sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport); 3869 if (sk == NULL) 3870 goto csfailed; 3871 nk = sk; 3872 } else 3873 KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p", 3874 __func__, nr, sk, nk)); 3875 3876 /* Swap sk/nk for PF_OUT. */ 3877 if (pf_state_insert(BOUND_IFACE(r, kif), kif, 3878 (pd->dir == PF_IN) ? sk : nk, 3879 (pd->dir == PF_IN) ? nk : sk, s)) { 3880 if (pd->proto == IPPROTO_TCP) 3881 pf_normalize_tcp_cleanup(s); 3882 REASON_SET(&reason, PFRES_STATEINS); 3883 pf_src_tree_remove_state(s); 3884 STATE_DEC_COUNTERS(s); 3885 uma_zfree(V_pf_state_z, s); 3886 return (PF_DROP); 3887 } else 3888 *sm = s; 3889 3890 if (tag > 0) 3891 s->tag = tag; 3892 if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) == 3893 TH_SYN && r->keep_state == PF_STATE_SYNPROXY) { 3894 s->src.state = PF_TCPS_PROXY_SRC; 3895 /* undo NAT changes, if they have taken place */ 3896 if (nr != NULL) { 3897 struct pf_state_key *skt = s->key[PF_SK_WIRE]; 3898 if (pd->dir == PF_OUT) 3899 skt = s->key[PF_SK_STACK]; 3900 PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af); 3901 PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af); 3902 if (pd->sport) 3903 *pd->sport = skt->port[pd->sidx]; 3904 if (pd->dport) 3905 *pd->dport = skt->port[pd->didx]; 3906 if (pd->proto_sum) 3907 *pd->proto_sum = bproto_sum; 3908 if (pd->ip_sum) 3909 *pd->ip_sum = bip_sum; 3910 m_copyback(m, off, hdrlen, pd->hdr.any); 3911 } 3912 s->src.seqhi = htonl(arc4random()); 3913 /* Find mss option */ 3914 int rtid = M_GETFIB(m); 3915 mss = pf_get_mss(m, off, th->th_off, pd->af); 3916 mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 3917 mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 3918 s->src.mss = mss; 3919 pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport, 3920 th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 3921 TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL); 3922 REASON_SET(&reason, PFRES_SYNPROXY); 3923 return (PF_SYNPROXY_DROP); 3924 } 3925 3926 return (PF_PASS); 3927 3928 csfailed: 3929 if (sk != NULL) 3930 uma_zfree(V_pf_state_key_z, sk); 3931 if (nk != NULL) 3932 uma_zfree(V_pf_state_key_z, nk); 3933 3934 if (sn != NULL) { 3935 struct pf_srchash *sh; 3936 3937 sh = &V_pf_srchash[pf_hashsrc(&sn->addr, sn->af)]; 3938 PF_HASHROW_LOCK(sh); 3939 if (--sn->states == 0 && sn->expire == 0) { 3940 pf_unlink_src_node(sn); 3941 uma_zfree(V_pf_sources_z, sn); 3942 counter_u64_add( 3943 V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1); 3944 } 3945 PF_HASHROW_UNLOCK(sh); 3946 } 3947 3948 if (nsn != sn && nsn != NULL) { 3949 struct pf_srchash *sh; 3950 3951 sh = &V_pf_srchash[pf_hashsrc(&nsn->addr, nsn->af)]; 3952 PF_HASHROW_LOCK(sh); 3953 if (--nsn->states == 0 && nsn->expire == 0) { 3954 pf_unlink_src_node(nsn); 3955 uma_zfree(V_pf_sources_z, nsn); 3956 counter_u64_add( 3957 V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1); 3958 } 3959 PF_HASHROW_UNLOCK(sh); 3960 } 3961 3962 return (PF_DROP); 3963 } 3964 3965 static int 3966 pf_test_fragment(struct pf_krule **rm, int direction, struct pfi_kkif *kif, 3967 struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_krule **am, 3968 struct pf_kruleset **rsm) 3969 { 3970 struct pf_krule *r, *a = NULL; 3971 struct pf_kruleset *ruleset = NULL; 3972 sa_family_t af = pd->af; 3973 u_short reason; 3974 int tag = -1; 3975 int asd = 0; 3976 int match = 0; 3977 struct pf_kanchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3978 3979 PF_RULES_RASSERT(); 3980 3981 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3982 while (r != NULL) { 3983 counter_u64_add(r->evaluations, 1); 3984 if (pfi_kkif_match(r->kif, kif) == r->ifnot) 3985 r = r->skip[PF_SKIP_IFP].ptr; 3986 else if (r->direction && r->direction != direction) 3987 r = r->skip[PF_SKIP_DIR].ptr; 3988 else if (r->af && r->af != af) 3989 r = r->skip[PF_SKIP_AF].ptr; 3990 else if (r->proto && r->proto != pd->proto) 3991 r = r->skip[PF_SKIP_PROTO].ptr; 3992 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 3993 r->src.neg, kif, M_GETFIB(m))) 3994 r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3995 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 3996 r->dst.neg, NULL, M_GETFIB(m))) 3997 r = r->skip[PF_SKIP_DST_ADDR].ptr; 3998 else if (r->tos && !(r->tos == pd->tos)) 3999 r = TAILQ_NEXT(r, entries); 4000 else if (r->os_fingerprint != PF_OSFP_ANY) 4001 r = TAILQ_NEXT(r, entries); 4002 else if (pd->proto == IPPROTO_UDP && 4003 (r->src.port_op || r->dst.port_op)) 4004 r = TAILQ_NEXT(r, entries); 4005 else if (pd->proto == IPPROTO_TCP && 4006 (r->src.port_op || r->dst.port_op || r->flagset)) 4007 r = TAILQ_NEXT(r, entries); 4008 else if ((pd->proto == IPPROTO_ICMP || 4009 pd->proto == IPPROTO_ICMPV6) && 4010 (r->type || r->code)) 4011 r = TAILQ_NEXT(r, entries); 4012 else if (r->prio && 4013 !pf_match_ieee8021q_pcp(r->prio, m)) 4014 r = TAILQ_NEXT(r, entries); 4015 else if (r->prob && r->prob <= 4016 (arc4random() % (UINT_MAX - 1) + 1)) 4017 r = TAILQ_NEXT(r, entries); 4018 else if (r->match_tag && !pf_match_tag(m, r, &tag, 4019 pd->pf_mtag ? pd->pf_mtag->tag : 0)) 4020 r = TAILQ_NEXT(r, entries); 4021 else { 4022 if (r->anchor == NULL) { 4023 match = 1; 4024 *rm = r; 4025 *am = a; 4026 *rsm = ruleset; 4027 if ((*rm)->quick) 4028 break; 4029 r = TAILQ_NEXT(r, entries); 4030 } else 4031 pf_step_into_anchor(anchor_stack, &asd, 4032 &ruleset, PF_RULESET_FILTER, &r, &a, 4033 &match); 4034 } 4035 if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 4036 &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 4037 break; 4038 } 4039 r = *rm; 4040 a = *am; 4041 ruleset = *rsm; 4042 4043 REASON_SET(&reason, PFRES_MATCH); 4044 4045 if (r->log) 4046 PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd, 4047 1); 4048 4049 if (r->action != PF_PASS) 4050 return (PF_DROP); 4051 4052 if (tag > 0 && pf_tag_packet(m, pd, tag)) { 4053 REASON_SET(&reason, PFRES_MEMORY); 4054 return (PF_DROP); 4055 } 4056 4057 return (PF_PASS); 4058 } 4059 4060 static int 4061 pf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst, 4062 struct pf_state **state, struct pfi_kkif *kif, struct mbuf *m, int off, 4063 struct pf_pdesc *pd, u_short *reason, int *copyback) 4064 { 4065 struct tcphdr *th = &pd->hdr.tcp; 4066 u_int16_t win = ntohs(th->th_win); 4067 u_int32_t ack, end, seq, orig_seq; 4068 u_int8_t sws, dws; 4069 int ackskew; 4070 4071 if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) { 4072 sws = src->wscale & PF_WSCALE_MASK; 4073 dws = dst->wscale & PF_WSCALE_MASK; 4074 } else 4075 sws = dws = 0; 4076 4077 /* 4078 * Sequence tracking algorithm from Guido van Rooij's paper: 4079 * http://www.madison-gurkha.com/publications/tcp_filtering/ 4080 * tcp_filtering.ps 4081 */ 4082 4083 orig_seq = seq = ntohl(th->th_seq); 4084 if (src->seqlo == 0) { 4085 /* First packet from this end. Set its state */ 4086 4087 if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) && 4088 src->scrub == NULL) { 4089 if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) { 4090 REASON_SET(reason, PFRES_MEMORY); 4091 return (PF_DROP); 4092 } 4093 } 4094 4095 /* Deferred generation of sequence number modulator */ 4096 if (dst->seqdiff && !src->seqdiff) { 4097 /* use random iss for the TCP server */ 4098 while ((src->seqdiff = arc4random() - seq) == 0) 4099 ; 4100 ack = ntohl(th->th_ack) - dst->seqdiff; 4101 pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq + 4102 src->seqdiff), 0); 4103 pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0); 4104 *copyback = 1; 4105 } else { 4106 ack = ntohl(th->th_ack); 4107 } 4108 4109 end = seq + pd->p_len; 4110 if (th->th_flags & TH_SYN) { 4111 end++; 4112 if (dst->wscale & PF_WSCALE_FLAG) { 4113 src->wscale = pf_get_wscale(m, off, th->th_off, 4114 pd->af); 4115 if (src->wscale & PF_WSCALE_FLAG) { 4116 /* Remove scale factor from initial 4117 * window */ 4118 sws = src->wscale & PF_WSCALE_MASK; 4119 win = ((u_int32_t)win + (1 << sws) - 1) 4120 >> sws; 4121 dws = dst->wscale & PF_WSCALE_MASK; 4122 } else { 4123 /* fixup other window */ 4124 dst->max_win <<= dst->wscale & 4125 PF_WSCALE_MASK; 4126 /* in case of a retrans SYN|ACK */ 4127 dst->wscale = 0; 4128 } 4129 } 4130 } 4131 if (th->th_flags & TH_FIN) 4132 end++; 4133 4134 src->seqlo = seq; 4135 if (src->state < TCPS_SYN_SENT) 4136 src->state = TCPS_SYN_SENT; 4137 4138 /* 4139 * May need to slide the window (seqhi may have been set by 4140 * the crappy stack check or if we picked up the connection 4141 * after establishment) 4142 */ 4143 if (src->seqhi == 1 || 4144 SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 4145 src->seqhi = end + MAX(1, dst->max_win << dws); 4146 if (win > src->max_win) 4147 src->max_win = win; 4148 4149 } else { 4150 ack = ntohl(th->th_ack) - dst->seqdiff; 4151 if (src->seqdiff) { 4152 /* Modulate sequence numbers */ 4153 pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq + 4154 src->seqdiff), 0); 4155 pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0); 4156 *copyback = 1; 4157 } 4158 end = seq + pd->p_len; 4159 if (th->th_flags & TH_SYN) 4160 end++; 4161 if (th->th_flags & TH_FIN) 4162 end++; 4163 } 4164 4165 if ((th->th_flags & TH_ACK) == 0) { 4166 /* Let it pass through the ack skew check */ 4167 ack = dst->seqlo; 4168 } else if ((ack == 0 && 4169 (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 4170 /* broken tcp stacks do not set ack */ 4171 (dst->state < TCPS_SYN_SENT)) { 4172 /* 4173 * Many stacks (ours included) will set the ACK number in an 4174 * FIN|ACK if the SYN times out -- no sequence to ACK. 4175 */ 4176 ack = dst->seqlo; 4177 } 4178 4179 if (seq == end) { 4180 /* Ease sequencing restrictions on no data packets */ 4181 seq = src->seqlo; 4182 end = seq; 4183 } 4184 4185 ackskew = dst->seqlo - ack; 4186 4187 /* 4188 * Need to demodulate the sequence numbers in any TCP SACK options 4189 * (Selective ACK). We could optionally validate the SACK values 4190 * against the current ACK window, either forwards or backwards, but 4191 * I'm not confident that SACK has been implemented properly 4192 * everywhere. It wouldn't surprise me if several stacks accidentally 4193 * SACK too far backwards of previously ACKed data. There really aren't 4194 * any security implications of bad SACKing unless the target stack 4195 * doesn't validate the option length correctly. Someone trying to 4196 * spoof into a TCP connection won't bother blindly sending SACK 4197 * options anyway. 4198 */ 4199 if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 4200 if (pf_modulate_sack(m, off, pd, th, dst)) 4201 *copyback = 1; 4202 } 4203 4204 #define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 4205 if (SEQ_GEQ(src->seqhi, end) && 4206 /* Last octet inside other's window space */ 4207 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 4208 /* Retrans: not more than one window back */ 4209 (ackskew >= -MAXACKWINDOW) && 4210 /* Acking not more than one reassembled fragment backwards */ 4211 (ackskew <= (MAXACKWINDOW << sws)) && 4212 /* Acking not more than one window forward */ 4213 ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo || 4214 (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) || 4215 (pd->flags & PFDESC_IP_REAS) == 0)) { 4216 /* Require an exact/+1 sequence match on resets when possible */ 4217 4218 if (dst->scrub || src->scrub) { 4219 if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 4220 *state, src, dst, copyback)) 4221 return (PF_DROP); 4222 } 4223 4224 /* update max window */ 4225 if (src->max_win < win) 4226 src->max_win = win; 4227 /* synchronize sequencing */ 4228 if (SEQ_GT(end, src->seqlo)) 4229 src->seqlo = end; 4230 /* slide the window of what the other end can send */ 4231 if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 4232 dst->seqhi = ack + MAX((win << sws), 1); 4233 4234 /* update states */ 4235 if (th->th_flags & TH_SYN) 4236 if (src->state < TCPS_SYN_SENT) 4237 src->state = TCPS_SYN_SENT; 4238 if (th->th_flags & TH_FIN) 4239 if (src->state < TCPS_CLOSING) 4240 src->state = TCPS_CLOSING; 4241 if (th->th_flags & TH_ACK) { 4242 if (dst->state == TCPS_SYN_SENT) { 4243 dst->state = TCPS_ESTABLISHED; 4244 if (src->state == TCPS_ESTABLISHED && 4245 (*state)->src_node != NULL && 4246 pf_src_connlimit(state)) { 4247 REASON_SET(reason, PFRES_SRCLIMIT); 4248 return (PF_DROP); 4249 } 4250 } else if (dst->state == TCPS_CLOSING) 4251 dst->state = TCPS_FIN_WAIT_2; 4252 } 4253 if (th->th_flags & TH_RST) 4254 src->state = dst->state = TCPS_TIME_WAIT; 4255 4256 /* update expire time */ 4257 (*state)->expire = time_uptime; 4258 if (src->state >= TCPS_FIN_WAIT_2 && 4259 dst->state >= TCPS_FIN_WAIT_2) 4260 (*state)->timeout = PFTM_TCP_CLOSED; 4261 else if (src->state >= TCPS_CLOSING && 4262 dst->state >= TCPS_CLOSING) 4263 (*state)->timeout = PFTM_TCP_FIN_WAIT; 4264 else if (src->state < TCPS_ESTABLISHED || 4265 dst->state < TCPS_ESTABLISHED) 4266 (*state)->timeout = PFTM_TCP_OPENING; 4267 else if (src->state >= TCPS_CLOSING || 4268 dst->state >= TCPS_CLOSING) 4269 (*state)->timeout = PFTM_TCP_CLOSING; 4270 else 4271 (*state)->timeout = PFTM_TCP_ESTABLISHED; 4272 4273 /* Fall through to PASS packet */ 4274 4275 } else if ((dst->state < TCPS_SYN_SENT || 4276 dst->state >= TCPS_FIN_WAIT_2 || 4277 src->state >= TCPS_FIN_WAIT_2) && 4278 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) && 4279 /* Within a window forward of the originating packet */ 4280 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 4281 /* Within a window backward of the originating packet */ 4282 4283 /* 4284 * This currently handles three situations: 4285 * 1) Stupid stacks will shotgun SYNs before their peer 4286 * replies. 4287 * 2) When PF catches an already established stream (the 4288 * firewall rebooted, the state table was flushed, routes 4289 * changed...) 4290 * 3) Packets get funky immediately after the connection 4291 * closes (this should catch Solaris spurious ACK|FINs 4292 * that web servers like to spew after a close) 4293 * 4294 * This must be a little more careful than the above code 4295 * since packet floods will also be caught here. We don't 4296 * update the TTL here to mitigate the damage of a packet 4297 * flood and so the same code can handle awkward establishment 4298 * and a loosened connection close. 4299 * In the establishment case, a correct peer response will 4300 * validate the connection, go through the normal state code 4301 * and keep updating the state TTL. 4302 */ 4303 4304 if (V_pf_status.debug >= PF_DEBUG_MISC) { 4305 printf("pf: loose state match: "); 4306 pf_print_state(*state); 4307 pf_print_flags(th->th_flags); 4308 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4309 "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 4310 pd->p_len, ackskew, 4311 (unsigned long long)counter_u64_fetch((*state)->packets[0]), 4312 (unsigned long long)counter_u64_fetch((*state)->packets[1]), 4313 pd->dir == PF_IN ? "in" : "out", 4314 pd->dir == (*state)->direction ? "fwd" : "rev"); 4315 } 4316 4317 if (dst->scrub || src->scrub) { 4318 if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 4319 *state, src, dst, copyback)) 4320 return (PF_DROP); 4321 } 4322 4323 /* update max window */ 4324 if (src->max_win < win) 4325 src->max_win = win; 4326 /* synchronize sequencing */ 4327 if (SEQ_GT(end, src->seqlo)) 4328 src->seqlo = end; 4329 /* slide the window of what the other end can send */ 4330 if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 4331 dst->seqhi = ack + MAX((win << sws), 1); 4332 4333 /* 4334 * Cannot set dst->seqhi here since this could be a shotgunned 4335 * SYN and not an already established connection. 4336 */ 4337 4338 if (th->th_flags & TH_FIN) 4339 if (src->state < TCPS_CLOSING) 4340 src->state = TCPS_CLOSING; 4341 if (th->th_flags & TH_RST) 4342 src->state = dst->state = TCPS_TIME_WAIT; 4343 4344 /* Fall through to PASS packet */ 4345 4346 } else { 4347 if ((*state)->dst.state == TCPS_SYN_SENT && 4348 (*state)->src.state == TCPS_SYN_SENT) { 4349 /* Send RST for state mismatches during handshake */ 4350 if (!(th->th_flags & TH_RST)) 4351 pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4352 pd->dst, pd->src, th->th_dport, 4353 th->th_sport, ntohl(th->th_ack), 0, 4354 TH_RST, 0, 0, 4355 (*state)->rule.ptr->return_ttl, 1, 0, 4356 kif->pfik_ifp); 4357 src->seqlo = 0; 4358 src->seqhi = 1; 4359 src->max_win = 1; 4360 } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 4361 printf("pf: BAD state: "); 4362 pf_print_state(*state); 4363 pf_print_flags(th->th_flags); 4364 printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4365 "pkts=%llu:%llu dir=%s,%s\n", 4366 seq, orig_seq, ack, pd->p_len, ackskew, 4367 (unsigned long long)counter_u64_fetch((*state)->packets[0]), 4368 (unsigned long long)counter_u64_fetch((*state)->packets[1]), 4369 pd->dir == PF_IN ? "in" : "out", 4370 pd->dir == (*state)->direction ? "fwd" : "rev"); 4371 printf("pf: State failure on: %c %c %c %c | %c %c\n", 4372 SEQ_GEQ(src->seqhi, end) ? ' ' : '1', 4373 SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 4374 ' ': '2', 4375 (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 4376 (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 4377 SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5', 4378 SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 4379 } 4380 REASON_SET(reason, PFRES_BADSTATE); 4381 return (PF_DROP); 4382 } 4383 4384 return (PF_PASS); 4385 } 4386 4387 static int 4388 pf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst, 4389 struct pf_state **state, struct pf_pdesc *pd, u_short *reason) 4390 { 4391 struct tcphdr *th = &pd->hdr.tcp; 4392 4393 if (th->th_flags & TH_SYN) 4394 if (src->state < TCPS_SYN_SENT) 4395 src->state = TCPS_SYN_SENT; 4396 if (th->th_flags & TH_FIN) 4397 if (src->state < TCPS_CLOSING) 4398 src->state = TCPS_CLOSING; 4399 if (th->th_flags & TH_ACK) { 4400 if (dst->state == TCPS_SYN_SENT) { 4401 dst->state = TCPS_ESTABLISHED; 4402 if (src->state == TCPS_ESTABLISHED && 4403 (*state)->src_node != NULL && 4404 pf_src_connlimit(state)) { 4405 REASON_SET(reason, PFRES_SRCLIMIT); 4406 return (PF_DROP); 4407 } 4408 } else if (dst->state == TCPS_CLOSING) { 4409 dst->state = TCPS_FIN_WAIT_2; 4410 } else if (src->state == TCPS_SYN_SENT && 4411 dst->state < TCPS_SYN_SENT) { 4412 /* 4413 * Handle a special sloppy case where we only see one 4414 * half of the connection. If there is a ACK after 4415 * the initial SYN without ever seeing a packet from 4416 * the destination, set the connection to established. 4417 */ 4418 dst->state = src->state = TCPS_ESTABLISHED; 4419 if ((*state)->src_node != NULL && 4420 pf_src_connlimit(state)) { 4421 REASON_SET(reason, PFRES_SRCLIMIT); 4422 return (PF_DROP); 4423 } 4424 } else if (src->state == TCPS_CLOSING && 4425 dst->state == TCPS_ESTABLISHED && 4426 dst->seqlo == 0) { 4427 /* 4428 * Handle the closing of half connections where we 4429 * don't see the full bidirectional FIN/ACK+ACK 4430 * handshake. 4431 */ 4432 dst->state = TCPS_CLOSING; 4433 } 4434 } 4435 if (th->th_flags & TH_RST) 4436 src->state = dst->state = TCPS_TIME_WAIT; 4437 4438 /* update expire time */ 4439 (*state)->expire = time_uptime; 4440 if (src->state >= TCPS_FIN_WAIT_2 && 4441 dst->state >= TCPS_FIN_WAIT_2) 4442 (*state)->timeout = PFTM_TCP_CLOSED; 4443 else if (src->state >= TCPS_CLOSING && 4444 dst->state >= TCPS_CLOSING) 4445 (*state)->timeout = PFTM_TCP_FIN_WAIT; 4446 else if (src->state < TCPS_ESTABLISHED || 4447 dst->state < TCPS_ESTABLISHED) 4448 (*state)->timeout = PFTM_TCP_OPENING; 4449 else if (src->state >= TCPS_CLOSING || 4450 dst->state >= TCPS_CLOSING) 4451 (*state)->timeout = PFTM_TCP_CLOSING; 4452 else 4453 (*state)->timeout = PFTM_TCP_ESTABLISHED; 4454 4455 return (PF_PASS); 4456 } 4457 4458 static int 4459 pf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kkif *kif, 4460 struct mbuf *m, int off, void *h, struct pf_pdesc *pd, 4461 u_short *reason) 4462 { 4463 struct pf_state_key_cmp key; 4464 struct tcphdr *th = &pd->hdr.tcp; 4465 int copyback = 0; 4466 struct pf_state_peer *src, *dst; 4467 struct pf_state_key *sk; 4468 4469 bzero(&key, sizeof(key)); 4470 key.af = pd->af; 4471 key.proto = IPPROTO_TCP; 4472 if (direction == PF_IN) { /* wire side, straight */ 4473 PF_ACPY(&key.addr[0], pd->src, key.af); 4474 PF_ACPY(&key.addr[1], pd->dst, key.af); 4475 key.port[0] = th->th_sport; 4476 key.port[1] = th->th_dport; 4477 } else { /* stack side, reverse */ 4478 PF_ACPY(&key.addr[1], pd->src, key.af); 4479 PF_ACPY(&key.addr[0], pd->dst, key.af); 4480 key.port[1] = th->th_sport; 4481 key.port[0] = th->th_dport; 4482 } 4483 4484 STATE_LOOKUP(kif, &key, direction, *state, pd); 4485 4486 if (direction == (*state)->direction) { 4487 src = &(*state)->src; 4488 dst = &(*state)->dst; 4489 } else { 4490 src = &(*state)->dst; 4491 dst = &(*state)->src; 4492 } 4493 4494 sk = (*state)->key[pd->didx]; 4495 4496 if ((*state)->src.state == PF_TCPS_PROXY_SRC) { 4497 if (direction != (*state)->direction) { 4498 REASON_SET(reason, PFRES_SYNPROXY); 4499 return (PF_SYNPROXY_DROP); 4500 } 4501 if (th->th_flags & TH_SYN) { 4502 if (ntohl(th->th_seq) != (*state)->src.seqlo) { 4503 REASON_SET(reason, PFRES_SYNPROXY); 4504 return (PF_DROP); 4505 } 4506 pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4507 pd->src, th->th_dport, th->th_sport, 4508 (*state)->src.seqhi, ntohl(th->th_seq) + 1, 4509 TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL); 4510 REASON_SET(reason, PFRES_SYNPROXY); 4511 return (PF_SYNPROXY_DROP); 4512 } else if ((th->th_flags & (TH_ACK|TH_RST|TH_FIN)) != TH_ACK || 4513 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4514 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4515 REASON_SET(reason, PFRES_SYNPROXY); 4516 return (PF_DROP); 4517 } else if ((*state)->src_node != NULL && 4518 pf_src_connlimit(state)) { 4519 REASON_SET(reason, PFRES_SRCLIMIT); 4520 return (PF_DROP); 4521 } else 4522 (*state)->src.state = PF_TCPS_PROXY_DST; 4523 } 4524 if ((*state)->src.state == PF_TCPS_PROXY_DST) { 4525 if (direction == (*state)->direction) { 4526 if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) || 4527 (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4528 (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4529 REASON_SET(reason, PFRES_SYNPROXY); 4530 return (PF_DROP); 4531 } 4532 (*state)->src.max_win = MAX(ntohs(th->th_win), 1); 4533 if ((*state)->dst.seqhi == 1) 4534 (*state)->dst.seqhi = htonl(arc4random()); 4535 pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4536 &sk->addr[pd->sidx], &sk->addr[pd->didx], 4537 sk->port[pd->sidx], sk->port[pd->didx], 4538 (*state)->dst.seqhi, 0, TH_SYN, 0, 4539 (*state)->src.mss, 0, 0, (*state)->tag, NULL); 4540 REASON_SET(reason, PFRES_SYNPROXY); 4541 return (PF_SYNPROXY_DROP); 4542 } else if (((th->th_flags & (TH_SYN|TH_ACK)) != 4543 (TH_SYN|TH_ACK)) || 4544 (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) { 4545 REASON_SET(reason, PFRES_SYNPROXY); 4546 return (PF_DROP); 4547 } else { 4548 (*state)->dst.max_win = MAX(ntohs(th->th_win), 1); 4549 (*state)->dst.seqlo = ntohl(th->th_seq); 4550 pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4551 pd->src, th->th_dport, th->th_sport, 4552 ntohl(th->th_ack), ntohl(th->th_seq) + 1, 4553 TH_ACK, (*state)->src.max_win, 0, 0, 0, 4554 (*state)->tag, NULL); 4555 pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4556 &sk->addr[pd->sidx], &sk->addr[pd->didx], 4557 sk->port[pd->sidx], sk->port[pd->didx], 4558 (*state)->src.seqhi + 1, (*state)->src.seqlo + 1, 4559 TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL); 4560 (*state)->src.seqdiff = (*state)->dst.seqhi - 4561 (*state)->src.seqlo; 4562 (*state)->dst.seqdiff = (*state)->src.seqhi - 4563 (*state)->dst.seqlo; 4564 (*state)->src.seqhi = (*state)->src.seqlo + 4565 (*state)->dst.max_win; 4566 (*state)->dst.seqhi = (*state)->dst.seqlo + 4567 (*state)->src.max_win; 4568 (*state)->src.wscale = (*state)->dst.wscale = 0; 4569 (*state)->src.state = (*state)->dst.state = 4570 TCPS_ESTABLISHED; 4571 REASON_SET(reason, PFRES_SYNPROXY); 4572 return (PF_SYNPROXY_DROP); 4573 } 4574 } 4575 4576 if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) && 4577 dst->state >= TCPS_FIN_WAIT_2 && 4578 src->state >= TCPS_FIN_WAIT_2) { 4579 if (V_pf_status.debug >= PF_DEBUG_MISC) { 4580 printf("pf: state reuse "); 4581 pf_print_state(*state); 4582 pf_print_flags(th->th_flags); 4583 printf("\n"); 4584 } 4585 /* XXX make sure it's the same direction ?? */ 4586 (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 4587 pf_unlink_state(*state, PF_ENTER_LOCKED); 4588 *state = NULL; 4589 return (PF_DROP); 4590 } 4591 4592 if ((*state)->state_flags & PFSTATE_SLOPPY) { 4593 if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP) 4594 return (PF_DROP); 4595 } else { 4596 if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason, 4597 ©back) == PF_DROP) 4598 return (PF_DROP); 4599 } 4600 4601 /* translate source/destination address, if necessary */ 4602 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4603 struct pf_state_key *nk = (*state)->key[pd->didx]; 4604 4605 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4606 nk->port[pd->sidx] != th->th_sport) 4607 pf_change_ap(m, pd->src, &th->th_sport, 4608 pd->ip_sum, &th->th_sum, &nk->addr[pd->sidx], 4609 nk->port[pd->sidx], 0, pd->af); 4610 4611 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4612 nk->port[pd->didx] != th->th_dport) 4613 pf_change_ap(m, pd->dst, &th->th_dport, 4614 pd->ip_sum, &th->th_sum, &nk->addr[pd->didx], 4615 nk->port[pd->didx], 0, pd->af); 4616 copyback = 1; 4617 } 4618 4619 /* Copyback sequence modulation or stateful scrub changes if needed */ 4620 if (copyback) 4621 m_copyback(m, off, sizeof(*th), (caddr_t)th); 4622 4623 return (PF_PASS); 4624 } 4625 4626 static int 4627 pf_test_state_udp(struct pf_state **state, int direction, struct pfi_kkif *kif, 4628 struct mbuf *m, int off, void *h, struct pf_pdesc *pd) 4629 { 4630 struct pf_state_peer *src, *dst; 4631 struct pf_state_key_cmp key; 4632 struct udphdr *uh = &pd->hdr.udp; 4633 4634 bzero(&key, sizeof(key)); 4635 key.af = pd->af; 4636 key.proto = IPPROTO_UDP; 4637 if (direction == PF_IN) { /* wire side, straight */ 4638 PF_ACPY(&key.addr[0], pd->src, key.af); 4639 PF_ACPY(&key.addr[1], pd->dst, key.af); 4640 key.port[0] = uh->uh_sport; 4641 key.port[1] = uh->uh_dport; 4642 } else { /* stack side, reverse */ 4643 PF_ACPY(&key.addr[1], pd->src, key.af); 4644 PF_ACPY(&key.addr[0], pd->dst, key.af); 4645 key.port[1] = uh->uh_sport; 4646 key.port[0] = uh->uh_dport; 4647 } 4648 4649 STATE_LOOKUP(kif, &key, direction, *state, pd); 4650 4651 if (direction == (*state)->direction) { 4652 src = &(*state)->src; 4653 dst = &(*state)->dst; 4654 } else { 4655 src = &(*state)->dst; 4656 dst = &(*state)->src; 4657 } 4658 4659 /* update states */ 4660 if (src->state < PFUDPS_SINGLE) 4661 src->state = PFUDPS_SINGLE; 4662 if (dst->state == PFUDPS_SINGLE) 4663 dst->state = PFUDPS_MULTIPLE; 4664 4665 /* update expire time */ 4666 (*state)->expire = time_uptime; 4667 if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 4668 (*state)->timeout = PFTM_UDP_MULTIPLE; 4669 else 4670 (*state)->timeout = PFTM_UDP_SINGLE; 4671 4672 /* translate source/destination address, if necessary */ 4673 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4674 struct pf_state_key *nk = (*state)->key[pd->didx]; 4675 4676 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4677 nk->port[pd->sidx] != uh->uh_sport) 4678 pf_change_ap(m, pd->src, &uh->uh_sport, pd->ip_sum, 4679 &uh->uh_sum, &nk->addr[pd->sidx], 4680 nk->port[pd->sidx], 1, pd->af); 4681 4682 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4683 nk->port[pd->didx] != uh->uh_dport) 4684 pf_change_ap(m, pd->dst, &uh->uh_dport, pd->ip_sum, 4685 &uh->uh_sum, &nk->addr[pd->didx], 4686 nk->port[pd->didx], 1, pd->af); 4687 m_copyback(m, off, sizeof(*uh), (caddr_t)uh); 4688 } 4689 4690 return (PF_PASS); 4691 } 4692 4693 static int 4694 pf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kkif *kif, 4695 struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason) 4696 { 4697 struct pf_addr *saddr = pd->src, *daddr = pd->dst; 4698 u_int16_t icmpid = 0, *icmpsum; 4699 u_int8_t icmptype, icmpcode; 4700 int state_icmp = 0; 4701 struct pf_state_key_cmp key; 4702 4703 bzero(&key, sizeof(key)); 4704 switch (pd->proto) { 4705 #ifdef INET 4706 case IPPROTO_ICMP: 4707 icmptype = pd->hdr.icmp.icmp_type; 4708 icmpcode = pd->hdr.icmp.icmp_code; 4709 icmpid = pd->hdr.icmp.icmp_id; 4710 icmpsum = &pd->hdr.icmp.icmp_cksum; 4711 4712 if (icmptype == ICMP_UNREACH || 4713 icmptype == ICMP_SOURCEQUENCH || 4714 icmptype == ICMP_REDIRECT || 4715 icmptype == ICMP_TIMXCEED || 4716 icmptype == ICMP_PARAMPROB) 4717 state_icmp++; 4718 break; 4719 #endif /* INET */ 4720 #ifdef INET6 4721 case IPPROTO_ICMPV6: 4722 icmptype = pd->hdr.icmp6.icmp6_type; 4723 icmpcode = pd->hdr.icmp6.icmp6_code; 4724 icmpid = pd->hdr.icmp6.icmp6_id; 4725 icmpsum = &pd->hdr.icmp6.icmp6_cksum; 4726 4727 if (icmptype == ICMP6_DST_UNREACH || 4728 icmptype == ICMP6_PACKET_TOO_BIG || 4729 icmptype == ICMP6_TIME_EXCEEDED || 4730 icmptype == ICMP6_PARAM_PROB) 4731 state_icmp++; 4732 break; 4733 #endif /* INET6 */ 4734 } 4735 4736 if (!state_icmp) { 4737 /* 4738 * ICMP query/reply message not related to a TCP/UDP packet. 4739 * Search for an ICMP state. 4740 */ 4741 key.af = pd->af; 4742 key.proto = pd->proto; 4743 key.port[0] = key.port[1] = icmpid; 4744 if (direction == PF_IN) { /* wire side, straight */ 4745 PF_ACPY(&key.addr[0], pd->src, key.af); 4746 PF_ACPY(&key.addr[1], pd->dst, key.af); 4747 } else { /* stack side, reverse */ 4748 PF_ACPY(&key.addr[1], pd->src, key.af); 4749 PF_ACPY(&key.addr[0], pd->dst, key.af); 4750 } 4751 4752 STATE_LOOKUP(kif, &key, direction, *state, pd); 4753 4754 (*state)->expire = time_uptime; 4755 (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 4756 4757 /* translate source/destination address, if necessary */ 4758 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4759 struct pf_state_key *nk = (*state)->key[pd->didx]; 4760 4761 switch (pd->af) { 4762 #ifdef INET 4763 case AF_INET: 4764 if (PF_ANEQ(pd->src, 4765 &nk->addr[pd->sidx], AF_INET)) 4766 pf_change_a(&saddr->v4.s_addr, 4767 pd->ip_sum, 4768 nk->addr[pd->sidx].v4.s_addr, 0); 4769 4770 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], 4771 AF_INET)) 4772 pf_change_a(&daddr->v4.s_addr, 4773 pd->ip_sum, 4774 nk->addr[pd->didx].v4.s_addr, 0); 4775 4776 if (nk->port[0] != 4777 pd->hdr.icmp.icmp_id) { 4778 pd->hdr.icmp.icmp_cksum = 4779 pf_cksum_fixup( 4780 pd->hdr.icmp.icmp_cksum, icmpid, 4781 nk->port[pd->sidx], 0); 4782 pd->hdr.icmp.icmp_id = 4783 nk->port[pd->sidx]; 4784 } 4785 4786 m_copyback(m, off, ICMP_MINLEN, 4787 (caddr_t )&pd->hdr.icmp); 4788 break; 4789 #endif /* INET */ 4790 #ifdef INET6 4791 case AF_INET6: 4792 if (PF_ANEQ(pd->src, 4793 &nk->addr[pd->sidx], AF_INET6)) 4794 pf_change_a6(saddr, 4795 &pd->hdr.icmp6.icmp6_cksum, 4796 &nk->addr[pd->sidx], 0); 4797 4798 if (PF_ANEQ(pd->dst, 4799 &nk->addr[pd->didx], AF_INET6)) 4800 pf_change_a6(daddr, 4801 &pd->hdr.icmp6.icmp6_cksum, 4802 &nk->addr[pd->didx], 0); 4803 4804 m_copyback(m, off, sizeof(struct icmp6_hdr), 4805 (caddr_t )&pd->hdr.icmp6); 4806 break; 4807 #endif /* INET6 */ 4808 } 4809 } 4810 return (PF_PASS); 4811 4812 } else { 4813 /* 4814 * ICMP error message in response to a TCP/UDP packet. 4815 * Extract the inner TCP/UDP header and search for that state. 4816 */ 4817 4818 struct pf_pdesc pd2; 4819 bzero(&pd2, sizeof pd2); 4820 #ifdef INET 4821 struct ip h2; 4822 #endif /* INET */ 4823 #ifdef INET6 4824 struct ip6_hdr h2_6; 4825 int terminal = 0; 4826 #endif /* INET6 */ 4827 int ipoff2 = 0; 4828 int off2 = 0; 4829 4830 pd2.af = pd->af; 4831 /* Payload packet is from the opposite direction. */ 4832 pd2.sidx = (direction == PF_IN) ? 1 : 0; 4833 pd2.didx = (direction == PF_IN) ? 0 : 1; 4834 switch (pd->af) { 4835 #ifdef INET 4836 case AF_INET: 4837 /* offset of h2 in mbuf chain */ 4838 ipoff2 = off + ICMP_MINLEN; 4839 4840 if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2), 4841 NULL, reason, pd2.af)) { 4842 DPFPRINTF(PF_DEBUG_MISC, 4843 ("pf: ICMP error message too short " 4844 "(ip)\n")); 4845 return (PF_DROP); 4846 } 4847 /* 4848 * ICMP error messages don't refer to non-first 4849 * fragments 4850 */ 4851 if (h2.ip_off & htons(IP_OFFMASK)) { 4852 REASON_SET(reason, PFRES_FRAG); 4853 return (PF_DROP); 4854 } 4855 4856 /* offset of protocol header that follows h2 */ 4857 off2 = ipoff2 + (h2.ip_hl << 2); 4858 4859 pd2.proto = h2.ip_p; 4860 pd2.src = (struct pf_addr *)&h2.ip_src; 4861 pd2.dst = (struct pf_addr *)&h2.ip_dst; 4862 pd2.ip_sum = &h2.ip_sum; 4863 break; 4864 #endif /* INET */ 4865 #ifdef INET6 4866 case AF_INET6: 4867 ipoff2 = off + sizeof(struct icmp6_hdr); 4868 4869 if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6), 4870 NULL, reason, pd2.af)) { 4871 DPFPRINTF(PF_DEBUG_MISC, 4872 ("pf: ICMP error message too short " 4873 "(ip6)\n")); 4874 return (PF_DROP); 4875 } 4876 pd2.proto = h2_6.ip6_nxt; 4877 pd2.src = (struct pf_addr *)&h2_6.ip6_src; 4878 pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 4879 pd2.ip_sum = NULL; 4880 off2 = ipoff2 + sizeof(h2_6); 4881 do { 4882 switch (pd2.proto) { 4883 case IPPROTO_FRAGMENT: 4884 /* 4885 * ICMPv6 error messages for 4886 * non-first fragments 4887 */ 4888 REASON_SET(reason, PFRES_FRAG); 4889 return (PF_DROP); 4890 case IPPROTO_AH: 4891 case IPPROTO_HOPOPTS: 4892 case IPPROTO_ROUTING: 4893 case IPPROTO_DSTOPTS: { 4894 /* get next header and header length */ 4895 struct ip6_ext opt6; 4896 4897 if (!pf_pull_hdr(m, off2, &opt6, 4898 sizeof(opt6), NULL, reason, 4899 pd2.af)) { 4900 DPFPRINTF(PF_DEBUG_MISC, 4901 ("pf: ICMPv6 short opt\n")); 4902 return (PF_DROP); 4903 } 4904 if (pd2.proto == IPPROTO_AH) 4905 off2 += (opt6.ip6e_len + 2) * 4; 4906 else 4907 off2 += (opt6.ip6e_len + 1) * 8; 4908 pd2.proto = opt6.ip6e_nxt; 4909 /* goto the next header */ 4910 break; 4911 } 4912 default: 4913 terminal++; 4914 break; 4915 } 4916 } while (!terminal); 4917 break; 4918 #endif /* INET6 */ 4919 } 4920 4921 if (PF_ANEQ(pd->dst, pd2.src, pd->af)) { 4922 if (V_pf_status.debug >= PF_DEBUG_MISC) { 4923 printf("pf: BAD ICMP %d:%d outer dst: ", 4924 icmptype, icmpcode); 4925 pf_print_host(pd->src, 0, pd->af); 4926 printf(" -> "); 4927 pf_print_host(pd->dst, 0, pd->af); 4928 printf(" inner src: "); 4929 pf_print_host(pd2.src, 0, pd2.af); 4930 printf(" -> "); 4931 pf_print_host(pd2.dst, 0, pd2.af); 4932 printf("\n"); 4933 } 4934 REASON_SET(reason, PFRES_BADSTATE); 4935 return (PF_DROP); 4936 } 4937 4938 switch (pd2.proto) { 4939 case IPPROTO_TCP: { 4940 struct tcphdr th; 4941 u_int32_t seq; 4942 struct pf_state_peer *src, *dst; 4943 u_int8_t dws; 4944 int copyback = 0; 4945 4946 /* 4947 * Only the first 8 bytes of the TCP header can be 4948 * expected. Don't access any TCP header fields after 4949 * th_seq, an ackskew test is not possible. 4950 */ 4951 if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason, 4952 pd2.af)) { 4953 DPFPRINTF(PF_DEBUG_MISC, 4954 ("pf: ICMP error message too short " 4955 "(tcp)\n")); 4956 return (PF_DROP); 4957 } 4958 4959 key.af = pd2.af; 4960 key.proto = IPPROTO_TCP; 4961 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4962 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4963 key.port[pd2.sidx] = th.th_sport; 4964 key.port[pd2.didx] = th.th_dport; 4965 4966 STATE_LOOKUP(kif, &key, direction, *state, pd); 4967 4968 if (direction == (*state)->direction) { 4969 src = &(*state)->dst; 4970 dst = &(*state)->src; 4971 } else { 4972 src = &(*state)->src; 4973 dst = &(*state)->dst; 4974 } 4975 4976 if (src->wscale && dst->wscale) 4977 dws = dst->wscale & PF_WSCALE_MASK; 4978 else 4979 dws = 0; 4980 4981 /* Demodulate sequence number */ 4982 seq = ntohl(th.th_seq) - src->seqdiff; 4983 if (src->seqdiff) { 4984 pf_change_a(&th.th_seq, icmpsum, 4985 htonl(seq), 0); 4986 copyback = 1; 4987 } 4988 4989 if (!((*state)->state_flags & PFSTATE_SLOPPY) && 4990 (!SEQ_GEQ(src->seqhi, seq) || 4991 !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 4992 if (V_pf_status.debug >= PF_DEBUG_MISC) { 4993 printf("pf: BAD ICMP %d:%d ", 4994 icmptype, icmpcode); 4995 pf_print_host(pd->src, 0, pd->af); 4996 printf(" -> "); 4997 pf_print_host(pd->dst, 0, pd->af); 4998 printf(" state: "); 4999 pf_print_state(*state); 5000 printf(" seq=%u\n", seq); 5001 } 5002 REASON_SET(reason, PFRES_BADSTATE); 5003 return (PF_DROP); 5004 } else { 5005 if (V_pf_status.debug >= PF_DEBUG_MISC) { 5006 printf("pf: OK ICMP %d:%d ", 5007 icmptype, icmpcode); 5008 pf_print_host(pd->src, 0, pd->af); 5009 printf(" -> "); 5010 pf_print_host(pd->dst, 0, pd->af); 5011 printf(" state: "); 5012 pf_print_state(*state); 5013 printf(" seq=%u\n", seq); 5014 } 5015 } 5016 5017 /* translate source/destination address, if necessary */ 5018 if ((*state)->key[PF_SK_WIRE] != 5019 (*state)->key[PF_SK_STACK]) { 5020 struct pf_state_key *nk = 5021 (*state)->key[pd->didx]; 5022 5023 if (PF_ANEQ(pd2.src, 5024 &nk->addr[pd2.sidx], pd2.af) || 5025 nk->port[pd2.sidx] != th.th_sport) 5026 pf_change_icmp(pd2.src, &th.th_sport, 5027 daddr, &nk->addr[pd2.sidx], 5028 nk->port[pd2.sidx], NULL, 5029 pd2.ip_sum, icmpsum, 5030 pd->ip_sum, 0, pd2.af); 5031 5032 if (PF_ANEQ(pd2.dst, 5033 &nk->addr[pd2.didx], pd2.af) || 5034 nk->port[pd2.didx] != th.th_dport) 5035 pf_change_icmp(pd2.dst, &th.th_dport, 5036 saddr, &nk->addr[pd2.didx], 5037 nk->port[pd2.didx], NULL, 5038 pd2.ip_sum, icmpsum, 5039 pd->ip_sum, 0, pd2.af); 5040 copyback = 1; 5041 } 5042 5043 if (copyback) { 5044 switch (pd2.af) { 5045 #ifdef INET 5046 case AF_INET: 5047 m_copyback(m, off, ICMP_MINLEN, 5048 (caddr_t )&pd->hdr.icmp); 5049 m_copyback(m, ipoff2, sizeof(h2), 5050 (caddr_t )&h2); 5051 break; 5052 #endif /* INET */ 5053 #ifdef INET6 5054 case AF_INET6: 5055 m_copyback(m, off, 5056 sizeof(struct icmp6_hdr), 5057 (caddr_t )&pd->hdr.icmp6); 5058 m_copyback(m, ipoff2, sizeof(h2_6), 5059 (caddr_t )&h2_6); 5060 break; 5061 #endif /* INET6 */ 5062 } 5063 m_copyback(m, off2, 8, (caddr_t)&th); 5064 } 5065 5066 return (PF_PASS); 5067 break; 5068 } 5069 case IPPROTO_UDP: { 5070 struct udphdr uh; 5071 5072 if (!pf_pull_hdr(m, off2, &uh, sizeof(uh), 5073 NULL, reason, pd2.af)) { 5074 DPFPRINTF(PF_DEBUG_MISC, 5075 ("pf: ICMP error message too short " 5076 "(udp)\n")); 5077 return (PF_DROP); 5078 } 5079 5080 key.af = pd2.af; 5081 key.proto = IPPROTO_UDP; 5082 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 5083 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 5084 key.port[pd2.sidx] = uh.uh_sport; 5085 key.port[pd2.didx] = uh.uh_dport; 5086 5087 STATE_LOOKUP(kif, &key, direction, *state, pd); 5088 5089 /* translate source/destination address, if necessary */ 5090 if ((*state)->key[PF_SK_WIRE] != 5091 (*state)->key[PF_SK_STACK]) { 5092 struct pf_state_key *nk = 5093 (*state)->key[pd->didx]; 5094 5095 if (PF_ANEQ(pd2.src, 5096 &nk->addr[pd2.sidx], pd2.af) || 5097 nk->port[pd2.sidx] != uh.uh_sport) 5098 pf_change_icmp(pd2.src, &uh.uh_sport, 5099 daddr, &nk->addr[pd2.sidx], 5100 nk->port[pd2.sidx], &uh.uh_sum, 5101 pd2.ip_sum, icmpsum, 5102 pd->ip_sum, 1, pd2.af); 5103 5104 if (PF_ANEQ(pd2.dst, 5105 &nk->addr[pd2.didx], pd2.af) || 5106 nk->port[pd2.didx] != uh.uh_dport) 5107 pf_change_icmp(pd2.dst, &uh.uh_dport, 5108 saddr, &nk->addr[pd2.didx], 5109 nk->port[pd2.didx], &uh.uh_sum, 5110 pd2.ip_sum, icmpsum, 5111 pd->ip_sum, 1, pd2.af); 5112 5113 switch (pd2.af) { 5114 #ifdef INET 5115 case AF_INET: 5116 m_copyback(m, off, ICMP_MINLEN, 5117 (caddr_t )&pd->hdr.icmp); 5118 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 5119 break; 5120 #endif /* INET */ 5121 #ifdef INET6 5122 case AF_INET6: 5123 m_copyback(m, off, 5124 sizeof(struct icmp6_hdr), 5125 (caddr_t )&pd->hdr.icmp6); 5126 m_copyback(m, ipoff2, sizeof(h2_6), 5127 (caddr_t )&h2_6); 5128 break; 5129 #endif /* INET6 */ 5130 } 5131 m_copyback(m, off2, sizeof(uh), (caddr_t)&uh); 5132 } 5133 return (PF_PASS); 5134 break; 5135 } 5136 #ifdef INET 5137 case IPPROTO_ICMP: { 5138 struct icmp iih; 5139 5140 if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN, 5141 NULL, reason, pd2.af)) { 5142 DPFPRINTF(PF_DEBUG_MISC, 5143 ("pf: ICMP error message too short i" 5144 "(icmp)\n")); 5145 return (PF_DROP); 5146 } 5147 5148 key.af = pd2.af; 5149 key.proto = IPPROTO_ICMP; 5150 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 5151 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 5152 key.port[0] = key.port[1] = iih.icmp_id; 5153 5154 STATE_LOOKUP(kif, &key, direction, *state, pd); 5155 5156 /* translate source/destination address, if necessary */ 5157 if ((*state)->key[PF_SK_WIRE] != 5158 (*state)->key[PF_SK_STACK]) { 5159 struct pf_state_key *nk = 5160 (*state)->key[pd->didx]; 5161 5162 if (PF_ANEQ(pd2.src, 5163 &nk->addr[pd2.sidx], pd2.af) || 5164 nk->port[pd2.sidx] != iih.icmp_id) 5165 pf_change_icmp(pd2.src, &iih.icmp_id, 5166 daddr, &nk->addr[pd2.sidx], 5167 nk->port[pd2.sidx], NULL, 5168 pd2.ip_sum, icmpsum, 5169 pd->ip_sum, 0, AF_INET); 5170 5171 if (PF_ANEQ(pd2.dst, 5172 &nk->addr[pd2.didx], pd2.af) || 5173 nk->port[pd2.didx] != iih.icmp_id) 5174 pf_change_icmp(pd2.dst, &iih.icmp_id, 5175 saddr, &nk->addr[pd2.didx], 5176 nk->port[pd2.didx], NULL, 5177 pd2.ip_sum, icmpsum, 5178 pd->ip_sum, 0, AF_INET); 5179 5180 m_copyback(m, off, ICMP_MINLEN, (caddr_t)&pd->hdr.icmp); 5181 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 5182 m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih); 5183 } 5184 return (PF_PASS); 5185 break; 5186 } 5187 #endif /* INET */ 5188 #ifdef INET6 5189 case IPPROTO_ICMPV6: { 5190 struct icmp6_hdr iih; 5191 5192 if (!pf_pull_hdr(m, off2, &iih, 5193 sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 5194 DPFPRINTF(PF_DEBUG_MISC, 5195 ("pf: ICMP error message too short " 5196 "(icmp6)\n")); 5197 return (PF_DROP); 5198 } 5199 5200 key.af = pd2.af; 5201 key.proto = IPPROTO_ICMPV6; 5202 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 5203 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 5204 key.port[0] = key.port[1] = iih.icmp6_id; 5205 5206 STATE_LOOKUP(kif, &key, direction, *state, pd); 5207 5208 /* translate source/destination address, if necessary */ 5209 if ((*state)->key[PF_SK_WIRE] != 5210 (*state)->key[PF_SK_STACK]) { 5211 struct pf_state_key *nk = 5212 (*state)->key[pd->didx]; 5213 5214 if (PF_ANEQ(pd2.src, 5215 &nk->addr[pd2.sidx], pd2.af) || 5216 nk->port[pd2.sidx] != iih.icmp6_id) 5217 pf_change_icmp(pd2.src, &iih.icmp6_id, 5218 daddr, &nk->addr[pd2.sidx], 5219 nk->port[pd2.sidx], NULL, 5220 pd2.ip_sum, icmpsum, 5221 pd->ip_sum, 0, AF_INET6); 5222 5223 if (PF_ANEQ(pd2.dst, 5224 &nk->addr[pd2.didx], pd2.af) || 5225 nk->port[pd2.didx] != iih.icmp6_id) 5226 pf_change_icmp(pd2.dst, &iih.icmp6_id, 5227 saddr, &nk->addr[pd2.didx], 5228 nk->port[pd2.didx], NULL, 5229 pd2.ip_sum, icmpsum, 5230 pd->ip_sum, 0, AF_INET6); 5231 5232 m_copyback(m, off, sizeof(struct icmp6_hdr), 5233 (caddr_t)&pd->hdr.icmp6); 5234 m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 5235 m_copyback(m, off2, sizeof(struct icmp6_hdr), 5236 (caddr_t)&iih); 5237 } 5238 return (PF_PASS); 5239 break; 5240 } 5241 #endif /* INET6 */ 5242 default: { 5243 key.af = pd2.af; 5244 key.proto = pd2.proto; 5245 PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 5246 PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 5247 key.port[0] = key.port[1] = 0; 5248 5249 STATE_LOOKUP(kif, &key, direction, *state, pd); 5250 5251 /* translate source/destination address, if necessary */ 5252 if ((*state)->key[PF_SK_WIRE] != 5253 (*state)->key[PF_SK_STACK]) { 5254 struct pf_state_key *nk = 5255 (*state)->key[pd->didx]; 5256 5257 if (PF_ANEQ(pd2.src, 5258 &nk->addr[pd2.sidx], pd2.af)) 5259 pf_change_icmp(pd2.src, NULL, daddr, 5260 &nk->addr[pd2.sidx], 0, NULL, 5261 pd2.ip_sum, icmpsum, 5262 pd->ip_sum, 0, pd2.af); 5263 5264 if (PF_ANEQ(pd2.dst, 5265 &nk->addr[pd2.didx], pd2.af)) 5266 pf_change_icmp(pd2.dst, NULL, saddr, 5267 &nk->addr[pd2.didx], 0, NULL, 5268 pd2.ip_sum, icmpsum, 5269 pd->ip_sum, 0, pd2.af); 5270 5271 switch (pd2.af) { 5272 #ifdef INET 5273 case AF_INET: 5274 m_copyback(m, off, ICMP_MINLEN, 5275 (caddr_t)&pd->hdr.icmp); 5276 m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 5277 break; 5278 #endif /* INET */ 5279 #ifdef INET6 5280 case AF_INET6: 5281 m_copyback(m, off, 5282 sizeof(struct icmp6_hdr), 5283 (caddr_t )&pd->hdr.icmp6); 5284 m_copyback(m, ipoff2, sizeof(h2_6), 5285 (caddr_t )&h2_6); 5286 break; 5287 #endif /* INET6 */ 5288 } 5289 } 5290 return (PF_PASS); 5291 break; 5292 } 5293 } 5294 } 5295 } 5296 5297 static int 5298 pf_test_state_other(struct pf_state **state, int direction, struct pfi_kkif *kif, 5299 struct mbuf *m, struct pf_pdesc *pd) 5300 { 5301 struct pf_state_peer *src, *dst; 5302 struct pf_state_key_cmp key; 5303 5304 bzero(&key, sizeof(key)); 5305 key.af = pd->af; 5306 key.proto = pd->proto; 5307 if (direction == PF_IN) { 5308 PF_ACPY(&key.addr[0], pd->src, key.af); 5309 PF_ACPY(&key.addr[1], pd->dst, key.af); 5310 key.port[0] = key.port[1] = 0; 5311 } else { 5312 PF_ACPY(&key.addr[1], pd->src, key.af); 5313 PF_ACPY(&key.addr[0], pd->dst, key.af); 5314 key.port[1] = key.port[0] = 0; 5315 } 5316 5317 STATE_LOOKUP(kif, &key, direction, *state, pd); 5318 5319 if (direction == (*state)->direction) { 5320 src = &(*state)->src; 5321 dst = &(*state)->dst; 5322 } else { 5323 src = &(*state)->dst; 5324 dst = &(*state)->src; 5325 } 5326 5327 /* update states */ 5328 if (src->state < PFOTHERS_SINGLE) 5329 src->state = PFOTHERS_SINGLE; 5330 if (dst->state == PFOTHERS_SINGLE) 5331 dst->state = PFOTHERS_MULTIPLE; 5332 5333 /* update expire time */ 5334 (*state)->expire = time_uptime; 5335 if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 5336 (*state)->timeout = PFTM_OTHER_MULTIPLE; 5337 else 5338 (*state)->timeout = PFTM_OTHER_SINGLE; 5339 5340 /* translate source/destination address, if necessary */ 5341 if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 5342 struct pf_state_key *nk = (*state)->key[pd->didx]; 5343 5344 KASSERT(nk, ("%s: nk is null", __func__)); 5345 KASSERT(pd, ("%s: pd is null", __func__)); 5346 KASSERT(pd->src, ("%s: pd->src is null", __func__)); 5347 KASSERT(pd->dst, ("%s: pd->dst is null", __func__)); 5348 switch (pd->af) { 5349 #ifdef INET 5350 case AF_INET: 5351 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5352 pf_change_a(&pd->src->v4.s_addr, 5353 pd->ip_sum, 5354 nk->addr[pd->sidx].v4.s_addr, 5355 0); 5356 5357 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5358 pf_change_a(&pd->dst->v4.s_addr, 5359 pd->ip_sum, 5360 nk->addr[pd->didx].v4.s_addr, 5361 0); 5362 5363 break; 5364 #endif /* INET */ 5365 #ifdef INET6 5366 case AF_INET6: 5367 if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5368 PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af); 5369 5370 if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5371 PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af); 5372 #endif /* INET6 */ 5373 } 5374 } 5375 return (PF_PASS); 5376 } 5377 5378 /* 5379 * ipoff and off are measured from the start of the mbuf chain. 5380 * h must be at "ipoff" on the mbuf chain. 5381 */ 5382 void * 5383 pf_pull_hdr(struct mbuf *m, int off, void *p, int len, 5384 u_short *actionp, u_short *reasonp, sa_family_t af) 5385 { 5386 switch (af) { 5387 #ifdef INET 5388 case AF_INET: { 5389 struct ip *h = mtod(m, struct ip *); 5390 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 5391 5392 if (fragoff) { 5393 if (fragoff >= len) 5394 ACTION_SET(actionp, PF_PASS); 5395 else { 5396 ACTION_SET(actionp, PF_DROP); 5397 REASON_SET(reasonp, PFRES_FRAG); 5398 } 5399 return (NULL); 5400 } 5401 if (m->m_pkthdr.len < off + len || 5402 ntohs(h->ip_len) < off + len) { 5403 ACTION_SET(actionp, PF_DROP); 5404 REASON_SET(reasonp, PFRES_SHORT); 5405 return (NULL); 5406 } 5407 break; 5408 } 5409 #endif /* INET */ 5410 #ifdef INET6 5411 case AF_INET6: { 5412 struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 5413 5414 if (m->m_pkthdr.len < off + len || 5415 (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) < 5416 (unsigned)(off + len)) { 5417 ACTION_SET(actionp, PF_DROP); 5418 REASON_SET(reasonp, PFRES_SHORT); 5419 return (NULL); 5420 } 5421 break; 5422 } 5423 #endif /* INET6 */ 5424 } 5425 m_copydata(m, off, len, p); 5426 return (p); 5427 } 5428 5429 int 5430 pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *kif, 5431 int rtableid) 5432 { 5433 struct ifnet *ifp; 5434 5435 /* 5436 * Skip check for addresses with embedded interface scope, 5437 * as they would always match anyway. 5438 */ 5439 if (af == AF_INET6 && IN6_IS_SCOPE_EMBED(&addr->v6)) 5440 return (1); 5441 5442 if (af != AF_INET && af != AF_INET6) 5443 return (0); 5444 5445 /* Skip checks for ipsec interfaces */ 5446 if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 5447 return (1); 5448 5449 ifp = (kif != NULL) ? kif->pfik_ifp : NULL; 5450 5451 switch (af) { 5452 #ifdef INET6 5453 case AF_INET6: 5454 return (fib6_check_urpf(rtableid, &addr->v6, 0, NHR_NONE, 5455 ifp)); 5456 #endif 5457 #ifdef INET 5458 case AF_INET: 5459 return (fib4_check_urpf(rtableid, addr->v4, 0, NHR_NONE, 5460 ifp)); 5461 #endif 5462 } 5463 5464 return (0); 5465 } 5466 5467 #ifdef INET 5468 static void 5469 pf_route(struct mbuf **m, struct pf_krule *r, int dir, struct ifnet *oifp, 5470 struct pf_state *s, struct pf_pdesc *pd, struct inpcb *inp) 5471 { 5472 struct mbuf *m0, *m1; 5473 struct sockaddr_in dst; 5474 struct ip *ip; 5475 struct ifnet *ifp = NULL; 5476 struct pf_addr naddr; 5477 struct pf_ksrc_node *sn = NULL; 5478 int error = 0; 5479 uint16_t ip_len, ip_off; 5480 5481 KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5482 KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5483 __func__)); 5484 5485 if ((pd->pf_mtag == NULL && 5486 ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5487 pd->pf_mtag->routed++ > 3) { 5488 m0 = *m; 5489 *m = NULL; 5490 goto bad_locked; 5491 } 5492 5493 if (r->rt == PF_DUPTO) { 5494 if ((pd->pf_mtag->flags & PF_DUPLICATED)) { 5495 if (s == NULL) { 5496 ifp = r->rpool.cur->kif ? 5497 r->rpool.cur->kif->pfik_ifp : NULL; 5498 } else { 5499 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5500 PF_STATE_UNLOCK(s); 5501 } 5502 if (ifp == oifp) { 5503 /* When the 2nd interface is not skipped */ 5504 return; 5505 } else { 5506 m0 = *m; 5507 *m = NULL; 5508 goto bad; 5509 } 5510 } else { 5511 pd->pf_mtag->flags |= PF_DUPLICATED; 5512 if (((m0 = m_dup(*m, M_NOWAIT)) == NULL)) { 5513 if (s) 5514 PF_STATE_UNLOCK(s); 5515 return; 5516 } 5517 } 5518 } else { 5519 if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5520 if (s) 5521 PF_STATE_UNLOCK(s); 5522 return; 5523 } 5524 m0 = *m; 5525 } 5526 5527 ip = mtod(m0, struct ip *); 5528 5529 bzero(&dst, sizeof(dst)); 5530 dst.sin_family = AF_INET; 5531 dst.sin_len = sizeof(dst); 5532 dst.sin_addr = ip->ip_dst; 5533 5534 bzero(&naddr, sizeof(naddr)); 5535 5536 if (TAILQ_EMPTY(&r->rpool.list)) { 5537 DPFPRINTF(PF_DEBUG_URGENT, 5538 ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5539 goto bad_locked; 5540 } 5541 if (s == NULL) { 5542 pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src, 5543 &naddr, NULL, &sn); 5544 if (!PF_AZERO(&naddr, AF_INET)) 5545 dst.sin_addr.s_addr = naddr.v4.s_addr; 5546 ifp = r->rpool.cur->kif ? 5547 r->rpool.cur->kif->pfik_ifp : NULL; 5548 } else { 5549 if (!PF_AZERO(&s->rt_addr, AF_INET)) 5550 dst.sin_addr.s_addr = 5551 s->rt_addr.v4.s_addr; 5552 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5553 PF_STATE_UNLOCK(s); 5554 } 5555 if (ifp == NULL) 5556 goto bad; 5557 5558 if (dir == PF_IN) { 5559 if (pf_test(PF_OUT, 0, ifp, &m0, inp) != PF_PASS) 5560 goto bad; 5561 else if (m0 == NULL) 5562 goto done; 5563 if (m0->m_len < sizeof(struct ip)) { 5564 DPFPRINTF(PF_DEBUG_URGENT, 5565 ("%s: m0->m_len < sizeof(struct ip)\n", __func__)); 5566 goto bad; 5567 } 5568 ip = mtod(m0, struct ip *); 5569 } 5570 5571 if (ifp->if_flags & IFF_LOOPBACK) 5572 m0->m_flags |= M_SKIP_FIREWALL; 5573 5574 ip_len = ntohs(ip->ip_len); 5575 ip_off = ntohs(ip->ip_off); 5576 5577 /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 5578 m0->m_pkthdr.csum_flags |= CSUM_IP; 5579 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 5580 m0 = mb_unmapped_to_ext(m0); 5581 if (m0 == NULL) 5582 goto done; 5583 in_delayed_cksum(m0); 5584 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 5585 } 5586 #if defined(SCTP) || defined(SCTP_SUPPORT) 5587 if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5588 m0 = mb_unmapped_to_ext(m0); 5589 if (m0 == NULL) 5590 goto done; 5591 sctp_delayed_cksum(m0, (uint32_t)(ip->ip_hl << 2)); 5592 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5593 } 5594 #endif 5595 5596 /* 5597 * If small enough for interface, or the interface will take 5598 * care of the fragmentation for us, we can just send directly. 5599 */ 5600 if (ip_len <= ifp->if_mtu || 5601 (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) { 5602 ip->ip_sum = 0; 5603 if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 5604 ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 5605 m0->m_pkthdr.csum_flags &= ~CSUM_IP; 5606 } 5607 m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 5608 error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5609 goto done; 5610 } 5611 5612 /* Balk when DF bit is set or the interface didn't support TSO. */ 5613 if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 5614 error = EMSGSIZE; 5615 KMOD_IPSTAT_INC(ips_cantfrag); 5616 if (r->rt != PF_DUPTO) { 5617 icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, 5618 ifp->if_mtu); 5619 goto done; 5620 } else 5621 goto bad; 5622 } 5623 5624 error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 5625 if (error) 5626 goto bad; 5627 5628 for (; m0; m0 = m1) { 5629 m1 = m0->m_nextpkt; 5630 m0->m_nextpkt = NULL; 5631 if (error == 0) { 5632 m_clrprotoflags(m0); 5633 error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5634 } else 5635 m_freem(m0); 5636 } 5637 5638 if (error == 0) 5639 KMOD_IPSTAT_INC(ips_fragmented); 5640 5641 done: 5642 if (r->rt != PF_DUPTO) 5643 *m = NULL; 5644 return; 5645 5646 bad_locked: 5647 if (s) 5648 PF_STATE_UNLOCK(s); 5649 bad: 5650 m_freem(m0); 5651 goto done; 5652 } 5653 #endif /* INET */ 5654 5655 #ifdef INET6 5656 static void 5657 pf_route6(struct mbuf **m, struct pf_krule *r, int dir, struct ifnet *oifp, 5658 struct pf_state *s, struct pf_pdesc *pd, struct inpcb *inp) 5659 { 5660 struct mbuf *m0; 5661 struct sockaddr_in6 dst; 5662 struct ip6_hdr *ip6; 5663 struct ifnet *ifp = NULL; 5664 struct pf_addr naddr; 5665 struct pf_ksrc_node *sn = NULL; 5666 5667 KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5668 KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5669 __func__)); 5670 5671 if ((pd->pf_mtag == NULL && 5672 ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5673 pd->pf_mtag->routed++ > 3) { 5674 m0 = *m; 5675 *m = NULL; 5676 goto bad_locked; 5677 } 5678 5679 if (r->rt == PF_DUPTO) { 5680 if ((pd->pf_mtag->flags & PF_DUPLICATED)) { 5681 if (s == NULL) { 5682 ifp = r->rpool.cur->kif ? 5683 r->rpool.cur->kif->pfik_ifp : NULL; 5684 } else { 5685 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5686 PF_STATE_UNLOCK(s); 5687 } 5688 if (ifp == oifp) { 5689 /* When the 2nd interface is not skipped */ 5690 return; 5691 } else { 5692 m0 = *m; 5693 *m = NULL; 5694 goto bad; 5695 } 5696 } else { 5697 pd->pf_mtag->flags |= PF_DUPLICATED; 5698 if (((m0 = m_dup(*m, M_NOWAIT)) == NULL)) { 5699 if (s) 5700 PF_STATE_UNLOCK(s); 5701 return; 5702 } 5703 } 5704 } else { 5705 if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5706 if (s) 5707 PF_STATE_UNLOCK(s); 5708 return; 5709 } 5710 m0 = *m; 5711 } 5712 5713 ip6 = mtod(m0, struct ip6_hdr *); 5714 5715 bzero(&dst, sizeof(dst)); 5716 dst.sin6_family = AF_INET6; 5717 dst.sin6_len = sizeof(dst); 5718 dst.sin6_addr = ip6->ip6_dst; 5719 5720 bzero(&naddr, sizeof(naddr)); 5721 5722 if (TAILQ_EMPTY(&r->rpool.list)) { 5723 DPFPRINTF(PF_DEBUG_URGENT, 5724 ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5725 goto bad_locked; 5726 } 5727 if (s == NULL) { 5728 pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src, 5729 &naddr, NULL, &sn); 5730 if (!PF_AZERO(&naddr, AF_INET6)) 5731 PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5732 &naddr, AF_INET6); 5733 ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; 5734 } else { 5735 if (!PF_AZERO(&s->rt_addr, AF_INET6)) 5736 PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5737 &s->rt_addr, AF_INET6); 5738 ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5739 } 5740 5741 if (s) 5742 PF_STATE_UNLOCK(s); 5743 5744 if (ifp == NULL) 5745 goto bad; 5746 5747 if (dir == PF_IN) { 5748 if (pf_test6(PF_OUT, PFIL_FWD, ifp, &m0, inp) != PF_PASS) 5749 goto bad; 5750 else if (m0 == NULL) 5751 goto done; 5752 if (m0->m_len < sizeof(struct ip6_hdr)) { 5753 DPFPRINTF(PF_DEBUG_URGENT, 5754 ("%s: m0->m_len < sizeof(struct ip6_hdr)\n", 5755 __func__)); 5756 goto bad; 5757 } 5758 ip6 = mtod(m0, struct ip6_hdr *); 5759 } 5760 5761 if (ifp->if_flags & IFF_LOOPBACK) 5762 m0->m_flags |= M_SKIP_FIREWALL; 5763 5764 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 & 5765 ~ifp->if_hwassist) { 5766 uint32_t plen = m0->m_pkthdr.len - sizeof(*ip6); 5767 m0 = mb_unmapped_to_ext(m0); 5768 if (m0 == NULL) 5769 goto done; 5770 in6_delayed_cksum(m0, plen, sizeof(struct ip6_hdr)); 5771 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 5772 } 5773 5774 /* 5775 * If the packet is too large for the outgoing interface, 5776 * send back an icmp6 error. 5777 */ 5778 if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 5779 dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 5780 if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) 5781 nd6_output_ifp(ifp, ifp, m0, &dst, NULL); 5782 else { 5783 in6_ifstat_inc(ifp, ifs6_in_toobig); 5784 if (r->rt != PF_DUPTO) 5785 icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); 5786 else 5787 goto bad; 5788 } 5789 5790 done: 5791 if (r->rt != PF_DUPTO) 5792 *m = NULL; 5793 return; 5794 5795 bad_locked: 5796 if (s) 5797 PF_STATE_UNLOCK(s); 5798 bad: 5799 m_freem(m0); 5800 goto done; 5801 } 5802 #endif /* INET6 */ 5803 5804 /* 5805 * FreeBSD supports cksum offloads for the following drivers. 5806 * em(4), fxp(4), lge(4), nge(4), re(4), ti(4), txp(4), xl(4) 5807 * 5808 * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 5809 * network driver performed cksum including pseudo header, need to verify 5810 * csum_data 5811 * CSUM_DATA_VALID : 5812 * network driver performed cksum, needs to additional pseudo header 5813 * cksum computation with partial csum_data(i.e. lack of H/W support for 5814 * pseudo header, for instance sk(4) and possibly gem(4)) 5815 * 5816 * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 5817 * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 5818 * TCP/UDP layer. 5819 * Also, set csum_data to 0xffff to force cksum validation. 5820 */ 5821 static int 5822 pf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 5823 { 5824 u_int16_t sum = 0; 5825 int hw_assist = 0; 5826 struct ip *ip; 5827 5828 if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 5829 return (1); 5830 if (m->m_pkthdr.len < off + len) 5831 return (1); 5832 5833 switch (p) { 5834 case IPPROTO_TCP: 5835 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5836 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5837 sum = m->m_pkthdr.csum_data; 5838 } else { 5839 ip = mtod(m, struct ip *); 5840 sum = in_pseudo(ip->ip_src.s_addr, 5841 ip->ip_dst.s_addr, htonl((u_short)len + 5842 m->m_pkthdr.csum_data + IPPROTO_TCP)); 5843 } 5844 sum ^= 0xffff; 5845 ++hw_assist; 5846 } 5847 break; 5848 case IPPROTO_UDP: 5849 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5850 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5851 sum = m->m_pkthdr.csum_data; 5852 } else { 5853 ip = mtod(m, struct ip *); 5854 sum = in_pseudo(ip->ip_src.s_addr, 5855 ip->ip_dst.s_addr, htonl((u_short)len + 5856 m->m_pkthdr.csum_data + IPPROTO_UDP)); 5857 } 5858 sum ^= 0xffff; 5859 ++hw_assist; 5860 } 5861 break; 5862 case IPPROTO_ICMP: 5863 #ifdef INET6 5864 case IPPROTO_ICMPV6: 5865 #endif /* INET6 */ 5866 break; 5867 default: 5868 return (1); 5869 } 5870 5871 if (!hw_assist) { 5872 switch (af) { 5873 case AF_INET: 5874 if (p == IPPROTO_ICMP) { 5875 if (m->m_len < off) 5876 return (1); 5877 m->m_data += off; 5878 m->m_len -= off; 5879 sum = in_cksum(m, len); 5880 m->m_data -= off; 5881 m->m_len += off; 5882 } else { 5883 if (m->m_len < sizeof(struct ip)) 5884 return (1); 5885 sum = in4_cksum(m, p, off, len); 5886 } 5887 break; 5888 #ifdef INET6 5889 case AF_INET6: 5890 if (m->m_len < sizeof(struct ip6_hdr)) 5891 return (1); 5892 sum = in6_cksum(m, p, off, len); 5893 break; 5894 #endif /* INET6 */ 5895 default: 5896 return (1); 5897 } 5898 } 5899 if (sum) { 5900 switch (p) { 5901 case IPPROTO_TCP: 5902 { 5903 KMOD_TCPSTAT_INC(tcps_rcvbadsum); 5904 break; 5905 } 5906 case IPPROTO_UDP: 5907 { 5908 KMOD_UDPSTAT_INC(udps_badsum); 5909 break; 5910 } 5911 #ifdef INET 5912 case IPPROTO_ICMP: 5913 { 5914 KMOD_ICMPSTAT_INC(icps_checksum); 5915 break; 5916 } 5917 #endif 5918 #ifdef INET6 5919 case IPPROTO_ICMPV6: 5920 { 5921 KMOD_ICMP6STAT_INC(icp6s_checksum); 5922 break; 5923 } 5924 #endif /* INET6 */ 5925 } 5926 return (1); 5927 } else { 5928 if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 5929 m->m_pkthdr.csum_flags |= 5930 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 5931 m->m_pkthdr.csum_data = 0xffff; 5932 } 5933 } 5934 return (0); 5935 } 5936 5937 #ifdef INET 5938 int 5939 pf_test(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5940 { 5941 struct pfi_kkif *kif; 5942 u_short action, reason = 0, log = 0; 5943 struct mbuf *m = *m0; 5944 struct ip *h = NULL; 5945 struct m_tag *ipfwtag; 5946 struct pf_krule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5947 struct pf_state *s = NULL; 5948 struct pf_kruleset *ruleset = NULL; 5949 struct pf_pdesc pd; 5950 int off, dirndx, pqid = 0; 5951 5952 PF_RULES_RLOCK_TRACKER; 5953 5954 M_ASSERTPKTHDR(m); 5955 5956 if (!V_pf_status.running) 5957 return (PF_PASS); 5958 5959 memset(&pd, 0, sizeof(pd)); 5960 5961 kif = (struct pfi_kkif *)ifp->if_pf_kif; 5962 5963 if (kif == NULL) { 5964 DPFPRINTF(PF_DEBUG_URGENT, 5965 ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname)); 5966 return (PF_DROP); 5967 } 5968 if (kif->pfik_flags & PFI_IFLAG_SKIP) 5969 return (PF_PASS); 5970 5971 if (m->m_flags & M_SKIP_FIREWALL) 5972 return (PF_PASS); 5973 5974 pd.pf_mtag = pf_find_mtag(m); 5975 5976 PF_RULES_RLOCK(); 5977 5978 if (ip_divert_ptr != NULL && 5979 ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) { 5980 struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1); 5981 if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) { 5982 if (pd.pf_mtag == NULL && 5983 ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5984 action = PF_DROP; 5985 goto done; 5986 } 5987 pd.pf_mtag->flags |= PF_PACKET_LOOPED; 5988 m_tag_delete(m, ipfwtag); 5989 } 5990 if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) { 5991 m->m_flags |= M_FASTFWD_OURS; 5992 pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT; 5993 } 5994 } else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) { 5995 /* We do IP header normalization and packet reassembly here */ 5996 action = PF_DROP; 5997 goto done; 5998 } 5999 m = *m0; /* pf_normalize messes with m0 */ 6000 h = mtod(m, struct ip *); 6001 6002 off = h->ip_hl << 2; 6003 if (off < (int)sizeof(struct ip)) { 6004 action = PF_DROP; 6005 REASON_SET(&reason, PFRES_SHORT); 6006 log = 1; 6007 goto done; 6008 } 6009 6010 pd.src = (struct pf_addr *)&h->ip_src; 6011 pd.dst = (struct pf_addr *)&h->ip_dst; 6012 pd.sport = pd.dport = NULL; 6013 pd.ip_sum = &h->ip_sum; 6014 pd.proto_sum = NULL; 6015 pd.proto = h->ip_p; 6016 pd.dir = dir; 6017 pd.sidx = (dir == PF_IN) ? 0 : 1; 6018 pd.didx = (dir == PF_IN) ? 1 : 0; 6019 pd.af = AF_INET; 6020 pd.tos = h->ip_tos & ~IPTOS_ECN_MASK; 6021 pd.tot_len = ntohs(h->ip_len); 6022 6023 /* handle fragments that didn't get reassembled by normalization */ 6024 if (h->ip_off & htons(IP_MF | IP_OFFMASK)) { 6025 action = pf_test_fragment(&r, dir, kif, m, h, 6026 &pd, &a, &ruleset); 6027 goto done; 6028 } 6029 6030 switch (h->ip_p) { 6031 case IPPROTO_TCP: { 6032 if (!pf_pull_hdr(m, off, &pd.hdr.tcp, sizeof(pd.hdr.tcp), 6033 &action, &reason, AF_INET)) { 6034 log = action != PF_PASS; 6035 goto done; 6036 } 6037 pd.p_len = pd.tot_len - off - (pd.hdr.tcp.th_off << 2); 6038 if ((pd.hdr.tcp.th_flags & TH_ACK) && pd.p_len == 0) 6039 pqid = 1; 6040 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6041 if (action == PF_DROP) 6042 goto done; 6043 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6044 &reason); 6045 if (action == PF_PASS) { 6046 if (V_pfsync_update_state_ptr != NULL) 6047 V_pfsync_update_state_ptr(s); 6048 r = s->rule.ptr; 6049 a = s->anchor.ptr; 6050 log = s->log; 6051 } else if (s == NULL) 6052 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6053 &a, &ruleset, inp); 6054 break; 6055 } 6056 6057 case IPPROTO_UDP: { 6058 if (!pf_pull_hdr(m, off, &pd.hdr.udp, sizeof(pd.hdr.udp), 6059 &action, &reason, AF_INET)) { 6060 log = action != PF_PASS; 6061 goto done; 6062 } 6063 if (pd.hdr.udp.uh_dport == 0 || 6064 ntohs(pd.hdr.udp.uh_ulen) > m->m_pkthdr.len - off || 6065 ntohs(pd.hdr.udp.uh_ulen) < sizeof(struct udphdr)) { 6066 action = PF_DROP; 6067 REASON_SET(&reason, PFRES_SHORT); 6068 goto done; 6069 } 6070 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6071 if (action == PF_PASS) { 6072 if (V_pfsync_update_state_ptr != NULL) 6073 V_pfsync_update_state_ptr(s); 6074 r = s->rule.ptr; 6075 a = s->anchor.ptr; 6076 log = s->log; 6077 } else if (s == NULL) 6078 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6079 &a, &ruleset, inp); 6080 break; 6081 } 6082 6083 case IPPROTO_ICMP: { 6084 if (!pf_pull_hdr(m, off, &pd.hdr.icmp, ICMP_MINLEN, 6085 &action, &reason, AF_INET)) { 6086 log = action != PF_PASS; 6087 goto done; 6088 } 6089 action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd, 6090 &reason); 6091 if (action == PF_PASS) { 6092 if (V_pfsync_update_state_ptr != NULL) 6093 V_pfsync_update_state_ptr(s); 6094 r = s->rule.ptr; 6095 a = s->anchor.ptr; 6096 log = s->log; 6097 } else if (s == NULL) 6098 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6099 &a, &ruleset, inp); 6100 break; 6101 } 6102 6103 #ifdef INET6 6104 case IPPROTO_ICMPV6: { 6105 action = PF_DROP; 6106 DPFPRINTF(PF_DEBUG_MISC, 6107 ("pf: dropping IPv4 packet with ICMPv6 payload\n")); 6108 goto done; 6109 } 6110 #endif 6111 6112 default: 6113 action = pf_test_state_other(&s, dir, kif, m, &pd); 6114 if (action == PF_PASS) { 6115 if (V_pfsync_update_state_ptr != NULL) 6116 V_pfsync_update_state_ptr(s); 6117 r = s->rule.ptr; 6118 a = s->anchor.ptr; 6119 log = s->log; 6120 } else if (s == NULL) 6121 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6122 &a, &ruleset, inp); 6123 break; 6124 } 6125 6126 done: 6127 PF_RULES_RUNLOCK(); 6128 if (action == PF_PASS && h->ip_hl > 5 && 6129 !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6130 action = PF_DROP; 6131 REASON_SET(&reason, PFRES_IPOPTIONS); 6132 log = r->log; 6133 DPFPRINTF(PF_DEBUG_MISC, 6134 ("pf: dropping packet with ip options\n")); 6135 } 6136 6137 if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6138 action = PF_DROP; 6139 REASON_SET(&reason, PFRES_MEMORY); 6140 } 6141 if (r->rtableid >= 0) 6142 M_SETFIB(m, r->rtableid); 6143 6144 if (r->scrub_flags & PFSTATE_SETPRIO) { 6145 if (pd.tos & IPTOS_LOWDELAY) 6146 pqid = 1; 6147 if (pf_ieee8021q_setpcp(m, r->set_prio[pqid])) { 6148 action = PF_DROP; 6149 REASON_SET(&reason, PFRES_MEMORY); 6150 log = 1; 6151 DPFPRINTF(PF_DEBUG_MISC, 6152 ("pf: failed to allocate 802.1q mtag\n")); 6153 } 6154 } 6155 6156 #ifdef ALTQ 6157 if (action == PF_PASS && r->qid) { 6158 if (pd.pf_mtag == NULL && 6159 ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6160 action = PF_DROP; 6161 REASON_SET(&reason, PFRES_MEMORY); 6162 } else { 6163 if (s != NULL) 6164 pd.pf_mtag->qid_hash = pf_state_hash(s); 6165 if (pqid || (pd.tos & IPTOS_LOWDELAY)) 6166 pd.pf_mtag->qid = r->pqid; 6167 else 6168 pd.pf_mtag->qid = r->qid; 6169 /* Add hints for ecn. */ 6170 pd.pf_mtag->hdr = h; 6171 } 6172 } 6173 #endif /* ALTQ */ 6174 6175 /* 6176 * connections redirected to loopback should not match sockets 6177 * bound specifically to loopback due to security implications, 6178 * see tcp_input() and in_pcblookup_listen(). 6179 */ 6180 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6181 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6182 (s->nat_rule.ptr->action == PF_RDR || 6183 s->nat_rule.ptr->action == PF_BINAT) && 6184 IN_LOOPBACK(ntohl(pd.dst->v4.s_addr))) 6185 m->m_flags |= M_SKIP_FIREWALL; 6186 6187 if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL && 6188 !PACKET_LOOPED(&pd)) { 6189 ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 6190 sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 6191 if (ipfwtag != NULL) { 6192 ((struct ipfw_rule_ref *)(ipfwtag+1))->info = 6193 ntohs(r->divert.port); 6194 ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir; 6195 6196 if (s) 6197 PF_STATE_UNLOCK(s); 6198 6199 m_tag_prepend(m, ipfwtag); 6200 if (m->m_flags & M_FASTFWD_OURS) { 6201 if (pd.pf_mtag == NULL && 6202 ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6203 action = PF_DROP; 6204 REASON_SET(&reason, PFRES_MEMORY); 6205 log = 1; 6206 DPFPRINTF(PF_DEBUG_MISC, 6207 ("pf: failed to allocate tag\n")); 6208 } else { 6209 pd.pf_mtag->flags |= 6210 PF_FASTFWD_OURS_PRESENT; 6211 m->m_flags &= ~M_FASTFWD_OURS; 6212 } 6213 } 6214 ip_divert_ptr(*m0, dir == PF_IN); 6215 *m0 = NULL; 6216 6217 return (action); 6218 } else { 6219 /* XXX: ipfw has the same behaviour! */ 6220 action = PF_DROP; 6221 REASON_SET(&reason, PFRES_MEMORY); 6222 log = 1; 6223 DPFPRINTF(PF_DEBUG_MISC, 6224 ("pf: failed to allocate divert tag\n")); 6225 } 6226 } 6227 6228 if (log) { 6229 struct pf_krule *lr; 6230 6231 if (s != NULL && s->nat_rule.ptr != NULL && 6232 s->nat_rule.ptr->log & PF_LOG_ALL) 6233 lr = s->nat_rule.ptr; 6234 else 6235 lr = r; 6236 PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd, 6237 (s == NULL)); 6238 } 6239 6240 counter_u64_add(kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS], 6241 pd.tot_len); 6242 counter_u64_add(kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS], 6243 1); 6244 6245 if (action == PF_PASS || r->action == PF_DROP) { 6246 dirndx = (dir == PF_OUT); 6247 counter_u64_add(r->packets[dirndx], 1); 6248 counter_u64_add(r->bytes[dirndx], pd.tot_len); 6249 if (a != NULL) { 6250 counter_u64_add(a->packets[dirndx], 1); 6251 counter_u64_add(a->bytes[dirndx], pd.tot_len); 6252 } 6253 if (s != NULL) { 6254 if (s->nat_rule.ptr != NULL) { 6255 counter_u64_add(s->nat_rule.ptr->packets[dirndx], 6256 1); 6257 counter_u64_add(s->nat_rule.ptr->bytes[dirndx], 6258 pd.tot_len); 6259 } 6260 if (s->src_node != NULL) { 6261 counter_u64_add(s->src_node->packets[dirndx], 6262 1); 6263 counter_u64_add(s->src_node->bytes[dirndx], 6264 pd.tot_len); 6265 } 6266 if (s->nat_src_node != NULL) { 6267 counter_u64_add(s->nat_src_node->packets[dirndx], 6268 1); 6269 counter_u64_add(s->nat_src_node->bytes[dirndx], 6270 pd.tot_len); 6271 } 6272 dirndx = (dir == s->direction) ? 0 : 1; 6273 counter_u64_add(s->packets[dirndx], 1); 6274 counter_u64_add(s->bytes[dirndx], pd.tot_len); 6275 } 6276 tr = r; 6277 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6278 if (nr != NULL && r == &V_pf_default_rule) 6279 tr = nr; 6280 if (tr->src.addr.type == PF_ADDR_TABLE) 6281 pfr_update_stats(tr->src.addr.p.tbl, 6282 (s == NULL) ? pd.src : 6283 &s->key[(s->direction == PF_IN)]-> 6284 addr[(s->direction == PF_OUT)], 6285 pd.af, pd.tot_len, dir == PF_OUT, 6286 r->action == PF_PASS, tr->src.neg); 6287 if (tr->dst.addr.type == PF_ADDR_TABLE) 6288 pfr_update_stats(tr->dst.addr.p.tbl, 6289 (s == NULL) ? pd.dst : 6290 &s->key[(s->direction == PF_IN)]-> 6291 addr[(s->direction == PF_IN)], 6292 pd.af, pd.tot_len, dir == PF_OUT, 6293 r->action == PF_PASS, tr->dst.neg); 6294 } 6295 6296 switch (action) { 6297 case PF_SYNPROXY_DROP: 6298 m_freem(*m0); 6299 case PF_DEFER: 6300 *m0 = NULL; 6301 action = PF_PASS; 6302 break; 6303 case PF_DROP: 6304 m_freem(*m0); 6305 *m0 = NULL; 6306 break; 6307 default: 6308 /* pf_route() returns unlocked. */ 6309 if (r->rt) { 6310 pf_route(m0, r, dir, kif->pfik_ifp, s, &pd, inp); 6311 return (action); 6312 } 6313 break; 6314 } 6315 if (s) 6316 PF_STATE_UNLOCK(s); 6317 6318 SDT_PROBE4(pf, ip, test, done, action, reason, r, s); 6319 6320 return (action); 6321 } 6322 #endif /* INET */ 6323 6324 #ifdef INET6 6325 int 6326 pf_test6(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 6327 { 6328 struct pfi_kkif *kif; 6329 u_short action, reason = 0, log = 0; 6330 struct mbuf *m = *m0, *n = NULL; 6331 struct m_tag *mtag; 6332 struct ip6_hdr *h = NULL; 6333 struct pf_krule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 6334 struct pf_state *s = NULL; 6335 struct pf_kruleset *ruleset = NULL; 6336 struct pf_pdesc pd; 6337 int off, terminal = 0, dirndx, rh_cnt = 0, pqid = 0; 6338 6339 PF_RULES_RLOCK_TRACKER; 6340 M_ASSERTPKTHDR(m); 6341 6342 if (!V_pf_status.running) 6343 return (PF_PASS); 6344 6345 memset(&pd, 0, sizeof(pd)); 6346 pd.pf_mtag = pf_find_mtag(m); 6347 6348 if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED) 6349 return (PF_PASS); 6350 6351 kif = (struct pfi_kkif *)ifp->if_pf_kif; 6352 if (kif == NULL) { 6353 DPFPRINTF(PF_DEBUG_URGENT, 6354 ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname)); 6355 return (PF_DROP); 6356 } 6357 if (kif->pfik_flags & PFI_IFLAG_SKIP) 6358 return (PF_PASS); 6359 6360 if (m->m_flags & M_SKIP_FIREWALL) 6361 return (PF_PASS); 6362 6363 PF_RULES_RLOCK(); 6364 6365 /* We do IP header normalization and packet reassembly here */ 6366 if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) { 6367 action = PF_DROP; 6368 goto done; 6369 } 6370 m = *m0; /* pf_normalize messes with m0 */ 6371 h = mtod(m, struct ip6_hdr *); 6372 6373 /* 6374 * we do not support jumbogram. if we keep going, zero ip6_plen 6375 * will do something bad, so drop the packet for now. 6376 */ 6377 if (htons(h->ip6_plen) == 0) { 6378 action = PF_DROP; 6379 REASON_SET(&reason, PFRES_NORM); /*XXX*/ 6380 goto done; 6381 } 6382 6383 pd.src = (struct pf_addr *)&h->ip6_src; 6384 pd.dst = (struct pf_addr *)&h->ip6_dst; 6385 pd.sport = pd.dport = NULL; 6386 pd.ip_sum = NULL; 6387 pd.proto_sum = NULL; 6388 pd.dir = dir; 6389 pd.sidx = (dir == PF_IN) ? 0 : 1; 6390 pd.didx = (dir == PF_IN) ? 1 : 0; 6391 pd.af = AF_INET6; 6392 pd.tos = IPV6_DSCP(h); 6393 pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 6394 6395 off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr); 6396 pd.proto = h->ip6_nxt; 6397 do { 6398 switch (pd.proto) { 6399 case IPPROTO_FRAGMENT: 6400 action = pf_test_fragment(&r, dir, kif, m, h, 6401 &pd, &a, &ruleset); 6402 if (action == PF_DROP) 6403 REASON_SET(&reason, PFRES_FRAG); 6404 goto done; 6405 case IPPROTO_ROUTING: { 6406 struct ip6_rthdr rthdr; 6407 6408 if (rh_cnt++) { 6409 DPFPRINTF(PF_DEBUG_MISC, 6410 ("pf: IPv6 more than one rthdr\n")); 6411 action = PF_DROP; 6412 REASON_SET(&reason, PFRES_IPOPTIONS); 6413 log = 1; 6414 goto done; 6415 } 6416 if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL, 6417 &reason, pd.af)) { 6418 DPFPRINTF(PF_DEBUG_MISC, 6419 ("pf: IPv6 short rthdr\n")); 6420 action = PF_DROP; 6421 REASON_SET(&reason, PFRES_SHORT); 6422 log = 1; 6423 goto done; 6424 } 6425 if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 6426 DPFPRINTF(PF_DEBUG_MISC, 6427 ("pf: IPv6 rthdr0\n")); 6428 action = PF_DROP; 6429 REASON_SET(&reason, PFRES_IPOPTIONS); 6430 log = 1; 6431 goto done; 6432 } 6433 /* FALLTHROUGH */ 6434 } 6435 case IPPROTO_AH: 6436 case IPPROTO_HOPOPTS: 6437 case IPPROTO_DSTOPTS: { 6438 /* get next header and header length */ 6439 struct ip6_ext opt6; 6440 6441 if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6), 6442 NULL, &reason, pd.af)) { 6443 DPFPRINTF(PF_DEBUG_MISC, 6444 ("pf: IPv6 short opt\n")); 6445 action = PF_DROP; 6446 log = 1; 6447 goto done; 6448 } 6449 if (pd.proto == IPPROTO_AH) 6450 off += (opt6.ip6e_len + 2) * 4; 6451 else 6452 off += (opt6.ip6e_len + 1) * 8; 6453 pd.proto = opt6.ip6e_nxt; 6454 /* goto the next header */ 6455 break; 6456 } 6457 default: 6458 terminal++; 6459 break; 6460 } 6461 } while (!terminal); 6462 6463 /* if there's no routing header, use unmodified mbuf for checksumming */ 6464 if (!n) 6465 n = m; 6466 6467 switch (pd.proto) { 6468 case IPPROTO_TCP: { 6469 if (!pf_pull_hdr(m, off, &pd.hdr.tcp, sizeof(pd.hdr.tcp), 6470 &action, &reason, AF_INET6)) { 6471 log = action != PF_PASS; 6472 goto done; 6473 } 6474 pd.p_len = pd.tot_len - off - (pd.hdr.tcp.th_off << 2); 6475 action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6476 if (action == PF_DROP) 6477 goto done; 6478 action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6479 &reason); 6480 if (action == PF_PASS) { 6481 if (V_pfsync_update_state_ptr != NULL) 6482 V_pfsync_update_state_ptr(s); 6483 r = s->rule.ptr; 6484 a = s->anchor.ptr; 6485 log = s->log; 6486 } else if (s == NULL) 6487 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6488 &a, &ruleset, inp); 6489 break; 6490 } 6491 6492 case IPPROTO_UDP: { 6493 if (!pf_pull_hdr(m, off, &pd.hdr.udp, sizeof(pd.hdr.udp), 6494 &action, &reason, AF_INET6)) { 6495 log = action != PF_PASS; 6496 goto done; 6497 } 6498 if (pd.hdr.udp.uh_dport == 0 || 6499 ntohs(pd.hdr.udp.uh_ulen) > m->m_pkthdr.len - off || 6500 ntohs(pd.hdr.udp.uh_ulen) < sizeof(struct udphdr)) { 6501 action = PF_DROP; 6502 REASON_SET(&reason, PFRES_SHORT); 6503 goto done; 6504 } 6505 action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6506 if (action == PF_PASS) { 6507 if (V_pfsync_update_state_ptr != NULL) 6508 V_pfsync_update_state_ptr(s); 6509 r = s->rule.ptr; 6510 a = s->anchor.ptr; 6511 log = s->log; 6512 } else if (s == NULL) 6513 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6514 &a, &ruleset, inp); 6515 break; 6516 } 6517 6518 case IPPROTO_ICMP: { 6519 action = PF_DROP; 6520 DPFPRINTF(PF_DEBUG_MISC, 6521 ("pf: dropping IPv6 packet with ICMPv4 payload\n")); 6522 goto done; 6523 } 6524 6525 case IPPROTO_ICMPV6: { 6526 if (!pf_pull_hdr(m, off, &pd.hdr.icmp6, sizeof(pd.hdr.icmp6), 6527 &action, &reason, AF_INET6)) { 6528 log = action != PF_PASS; 6529 goto done; 6530 } 6531 action = pf_test_state_icmp(&s, dir, kif, 6532 m, off, h, &pd, &reason); 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 default: 6546 action = pf_test_state_other(&s, dir, kif, m, &pd); 6547 if (action == PF_PASS) { 6548 if (V_pfsync_update_state_ptr != NULL) 6549 V_pfsync_update_state_ptr(s); 6550 r = s->rule.ptr; 6551 a = s->anchor.ptr; 6552 log = s->log; 6553 } else if (s == NULL) 6554 action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6555 &a, &ruleset, inp); 6556 break; 6557 } 6558 6559 done: 6560 PF_RULES_RUNLOCK(); 6561 if (n != m) { 6562 m_freem(n); 6563 n = NULL; 6564 } 6565 6566 /* handle dangerous IPv6 extension headers. */ 6567 if (action == PF_PASS && rh_cnt && 6568 !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6569 action = PF_DROP; 6570 REASON_SET(&reason, PFRES_IPOPTIONS); 6571 log = r->log; 6572 DPFPRINTF(PF_DEBUG_MISC, 6573 ("pf: dropping packet with dangerous v6 headers\n")); 6574 } 6575 6576 if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6577 action = PF_DROP; 6578 REASON_SET(&reason, PFRES_MEMORY); 6579 } 6580 if (r->rtableid >= 0) 6581 M_SETFIB(m, r->rtableid); 6582 6583 if (r->scrub_flags & PFSTATE_SETPRIO) { 6584 if (pd.tos & IPTOS_LOWDELAY) 6585 pqid = 1; 6586 if (pf_ieee8021q_setpcp(m, r->set_prio[pqid])) { 6587 action = PF_DROP; 6588 REASON_SET(&reason, PFRES_MEMORY); 6589 log = 1; 6590 DPFPRINTF(PF_DEBUG_MISC, 6591 ("pf: failed to allocate 802.1q mtag\n")); 6592 } 6593 } 6594 6595 #ifdef ALTQ 6596 if (action == PF_PASS && r->qid) { 6597 if (pd.pf_mtag == NULL && 6598 ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6599 action = PF_DROP; 6600 REASON_SET(&reason, PFRES_MEMORY); 6601 } else { 6602 if (s != NULL) 6603 pd.pf_mtag->qid_hash = pf_state_hash(s); 6604 if (pd.tos & IPTOS_LOWDELAY) 6605 pd.pf_mtag->qid = r->pqid; 6606 else 6607 pd.pf_mtag->qid = r->qid; 6608 /* Add hints for ecn. */ 6609 pd.pf_mtag->hdr = h; 6610 } 6611 } 6612 #endif /* ALTQ */ 6613 6614 if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6615 pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6616 (s->nat_rule.ptr->action == PF_RDR || 6617 s->nat_rule.ptr->action == PF_BINAT) && 6618 IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) 6619 m->m_flags |= M_SKIP_FIREWALL; 6620 6621 /* XXX: Anybody working on it?! */ 6622 if (r->divert.port) 6623 printf("pf: divert(9) is not supported for IPv6\n"); 6624 6625 if (log) { 6626 struct pf_krule *lr; 6627 6628 if (s != NULL && s->nat_rule.ptr != NULL && 6629 s->nat_rule.ptr->log & PF_LOG_ALL) 6630 lr = s->nat_rule.ptr; 6631 else 6632 lr = r; 6633 PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset, 6634 &pd, (s == NULL)); 6635 } 6636 6637 counter_u64_add(kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS], 6638 pd.tot_len); 6639 counter_u64_add(kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS], 6640 1); 6641 6642 if (action == PF_PASS || r->action == PF_DROP) { 6643 dirndx = (dir == PF_OUT); 6644 counter_u64_add(r->packets[dirndx], 1); 6645 counter_u64_add(r->bytes[dirndx], pd.tot_len); 6646 if (a != NULL) { 6647 counter_u64_add(a->packets[dirndx], 1); 6648 counter_u64_add(a->bytes[dirndx], pd.tot_len); 6649 } 6650 if (s != NULL) { 6651 if (s->nat_rule.ptr != NULL) { 6652 counter_u64_add(s->nat_rule.ptr->packets[dirndx], 6653 1); 6654 counter_u64_add(s->nat_rule.ptr->bytes[dirndx], 6655 pd.tot_len); 6656 } 6657 if (s->src_node != NULL) { 6658 counter_u64_add(s->src_node->packets[dirndx], 6659 1); 6660 counter_u64_add(s->src_node->bytes[dirndx], 6661 pd.tot_len); 6662 } 6663 if (s->nat_src_node != NULL) { 6664 counter_u64_add(s->nat_src_node->packets[dirndx], 6665 1); 6666 counter_u64_add(s->nat_src_node->bytes[dirndx], 6667 pd.tot_len); 6668 } 6669 dirndx = (dir == s->direction) ? 0 : 1; 6670 counter_u64_add(s->packets[dirndx], 1); 6671 counter_u64_add(s->bytes[dirndx], pd.tot_len); 6672 } 6673 tr = r; 6674 nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6675 if (nr != NULL && r == &V_pf_default_rule) 6676 tr = nr; 6677 if (tr->src.addr.type == PF_ADDR_TABLE) 6678 pfr_update_stats(tr->src.addr.p.tbl, 6679 (s == NULL) ? pd.src : 6680 &s->key[(s->direction == PF_IN)]->addr[0], 6681 pd.af, pd.tot_len, dir == PF_OUT, 6682 r->action == PF_PASS, tr->src.neg); 6683 if (tr->dst.addr.type == PF_ADDR_TABLE) 6684 pfr_update_stats(tr->dst.addr.p.tbl, 6685 (s == NULL) ? pd.dst : 6686 &s->key[(s->direction == PF_IN)]->addr[1], 6687 pd.af, pd.tot_len, dir == PF_OUT, 6688 r->action == PF_PASS, tr->dst.neg); 6689 } 6690 6691 switch (action) { 6692 case PF_SYNPROXY_DROP: 6693 m_freem(*m0); 6694 case PF_DEFER: 6695 *m0 = NULL; 6696 action = PF_PASS; 6697 break; 6698 case PF_DROP: 6699 m_freem(*m0); 6700 *m0 = NULL; 6701 break; 6702 default: 6703 /* pf_route6() returns unlocked. */ 6704 if (r->rt) { 6705 pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd, inp); 6706 return (action); 6707 } 6708 break; 6709 } 6710 6711 if (s) 6712 PF_STATE_UNLOCK(s); 6713 6714 /* If reassembled packet passed, create new fragments. */ 6715 if (action == PF_PASS && *m0 && (pflags & PFIL_FWD) && 6716 (mtag = m_tag_find(m, PF_REASSEMBLED, NULL)) != NULL) 6717 action = pf_refragment6(ifp, m0, mtag); 6718 6719 SDT_PROBE4(pf, ip, test6, done, action, reason, r, s); 6720 6721 return (action); 6722 } 6723 #endif /* INET6 */ 6724