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