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