1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 2001 Niels Provos <provos@citi.umich.edu> 5 * Copyright 2011-2018 Alexander Bluhm <bluhm@openbsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $OpenBSD: pf_norm.c,v 1.114 2009/01/29 14:11:45 henning Exp $ 29 */ 30 31 #include <sys/cdefs.h> 32 #include "opt_inet.h" 33 #include "opt_inet6.h" 34 #include "opt_pf.h" 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/mbuf.h> 40 #include <sys/mutex.h> 41 #include <sys/refcount.h> 42 #include <sys/socket.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_private.h> 47 #include <net/vnet.h> 48 #include <net/pfvar.h> 49 #include <net/if_pflog.h> 50 51 #include <netinet/in.h> 52 #include <netinet/ip.h> 53 #include <netinet/ip_var.h> 54 #include <netinet6/in6_var.h> 55 #include <netinet6/nd6.h> 56 #include <netinet6/ip6_var.h> 57 #include <netinet6/scope6_var.h> 58 #include <netinet/tcp.h> 59 #include <netinet/tcp_fsm.h> 60 #include <netinet/tcp_seq.h> 61 #include <netinet/sctp_constants.h> 62 #include <netinet/sctp_header.h> 63 64 #ifdef INET6 65 #include <netinet/ip6.h> 66 #endif /* INET6 */ 67 68 struct pf_frent { 69 TAILQ_ENTRY(pf_frent) fr_next; 70 struct mbuf *fe_m; 71 uint16_t fe_hdrlen; /* ipv4 header length with ip options 72 ipv6, extension, fragment header */ 73 uint16_t fe_extoff; /* last extension header offset or 0 */ 74 uint16_t fe_len; /* fragment length */ 75 uint16_t fe_off; /* fragment offset */ 76 uint16_t fe_mff; /* more fragment flag */ 77 }; 78 79 struct pf_fragment_cmp { 80 struct pf_addr frc_src; 81 struct pf_addr frc_dst; 82 uint32_t frc_id; 83 sa_family_t frc_af; 84 uint8_t frc_proto; 85 }; 86 87 struct pf_fragment { 88 struct pf_fragment_cmp fr_key; 89 #define fr_src fr_key.frc_src 90 #define fr_dst fr_key.frc_dst 91 #define fr_id fr_key.frc_id 92 #define fr_af fr_key.frc_af 93 #define fr_proto fr_key.frc_proto 94 95 /* pointers to queue element */ 96 struct pf_frent *fr_firstoff[PF_FRAG_ENTRY_POINTS]; 97 /* count entries between pointers */ 98 uint8_t fr_entries[PF_FRAG_ENTRY_POINTS]; 99 RB_ENTRY(pf_fragment) fr_entry; 100 TAILQ_ENTRY(pf_fragment) frag_next; 101 uint32_t fr_timeout; 102 TAILQ_HEAD(pf_fragq, pf_frent) fr_queue; 103 uint16_t fr_maxlen; /* maximum length of single fragment */ 104 u_int16_t fr_holes; /* number of holes in the queue */ 105 }; 106 107 VNET_DEFINE_STATIC(struct mtx, pf_frag_mtx); 108 #define V_pf_frag_mtx VNET(pf_frag_mtx) 109 #define PF_FRAG_LOCK() mtx_lock(&V_pf_frag_mtx) 110 #define PF_FRAG_UNLOCK() mtx_unlock(&V_pf_frag_mtx) 111 #define PF_FRAG_ASSERT() mtx_assert(&V_pf_frag_mtx, MA_OWNED) 112 113 VNET_DEFINE(uma_zone_t, pf_state_scrub_z); /* XXX: shared with pfsync */ 114 115 VNET_DEFINE_STATIC(uma_zone_t, pf_frent_z); 116 #define V_pf_frent_z VNET(pf_frent_z) 117 VNET_DEFINE_STATIC(uma_zone_t, pf_frag_z); 118 #define V_pf_frag_z VNET(pf_frag_z) 119 120 TAILQ_HEAD(pf_fragqueue, pf_fragment); 121 TAILQ_HEAD(pf_cachequeue, pf_fragment); 122 VNET_DEFINE_STATIC(struct pf_fragqueue, pf_fragqueue); 123 #define V_pf_fragqueue VNET(pf_fragqueue) 124 RB_HEAD(pf_frag_tree, pf_fragment); 125 VNET_DEFINE_STATIC(struct pf_frag_tree, pf_frag_tree); 126 #define V_pf_frag_tree VNET(pf_frag_tree) 127 static int pf_frag_compare(struct pf_fragment *, 128 struct pf_fragment *); 129 static RB_PROTOTYPE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare); 130 static RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare); 131 132 static void pf_flush_fragments(void); 133 static void pf_free_fragment(struct pf_fragment *); 134 135 static struct pf_frent *pf_create_fragment(u_short *); 136 static int pf_frent_holes(struct pf_frent *frent); 137 static struct pf_fragment *pf_find_fragment(struct pf_fragment_cmp *key, 138 struct pf_frag_tree *tree); 139 static inline int pf_frent_index(struct pf_frent *); 140 static int pf_frent_insert(struct pf_fragment *, 141 struct pf_frent *, struct pf_frent *); 142 void pf_frent_remove(struct pf_fragment *, 143 struct pf_frent *); 144 struct pf_frent *pf_frent_previous(struct pf_fragment *, 145 struct pf_frent *); 146 static struct pf_fragment *pf_fillup_fragment(struct pf_fragment_cmp *, 147 struct pf_frent *, u_short *); 148 static struct mbuf *pf_join_fragment(struct pf_fragment *); 149 #ifdef INET 150 static int pf_reassemble(struct mbuf **, u_short *); 151 #endif /* INET */ 152 #ifdef INET6 153 static int pf_reassemble6(struct mbuf **, 154 struct ip6_frag *, uint16_t, uint16_t, u_short *); 155 #endif /* INET6 */ 156 157 #define DPFPRINTF(x) do { \ 158 if (V_pf_status.debug >= PF_DEBUG_MISC) { \ 159 printf("%s: ", __func__); \ 160 printf x ; \ 161 } \ 162 } while(0) 163 164 #ifdef INET 165 static void 166 pf_ip2key(struct ip *ip, struct pf_fragment_cmp *key) 167 { 168 169 key->frc_src.v4 = ip->ip_src; 170 key->frc_dst.v4 = ip->ip_dst; 171 key->frc_af = AF_INET; 172 key->frc_proto = ip->ip_p; 173 key->frc_id = ip->ip_id; 174 } 175 #endif /* INET */ 176 177 void 178 pf_normalize_init(void) 179 { 180 181 V_pf_frag_z = uma_zcreate("pf frags", sizeof(struct pf_fragment), 182 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 183 V_pf_frent_z = uma_zcreate("pf frag entries", sizeof(struct pf_frent), 184 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 185 V_pf_state_scrub_z = uma_zcreate("pf state scrubs", 186 sizeof(struct pf_state_scrub), NULL, NULL, NULL, NULL, 187 UMA_ALIGN_PTR, 0); 188 189 mtx_init(&V_pf_frag_mtx, "pf fragments", NULL, MTX_DEF); 190 191 V_pf_limits[PF_LIMIT_FRAGS].zone = V_pf_frent_z; 192 V_pf_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT; 193 uma_zone_set_max(V_pf_frent_z, PFFRAG_FRENT_HIWAT); 194 uma_zone_set_warning(V_pf_frent_z, "PF frag entries limit reached"); 195 196 TAILQ_INIT(&V_pf_fragqueue); 197 } 198 199 void 200 pf_normalize_cleanup(void) 201 { 202 203 uma_zdestroy(V_pf_state_scrub_z); 204 uma_zdestroy(V_pf_frent_z); 205 uma_zdestroy(V_pf_frag_z); 206 207 mtx_destroy(&V_pf_frag_mtx); 208 } 209 210 static int 211 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b) 212 { 213 int diff; 214 215 if ((diff = a->fr_id - b->fr_id) != 0) 216 return (diff); 217 if ((diff = a->fr_proto - b->fr_proto) != 0) 218 return (diff); 219 if ((diff = a->fr_af - b->fr_af) != 0) 220 return (diff); 221 if ((diff = pf_addr_cmp(&a->fr_src, &b->fr_src, a->fr_af)) != 0) 222 return (diff); 223 if ((diff = pf_addr_cmp(&a->fr_dst, &b->fr_dst, a->fr_af)) != 0) 224 return (diff); 225 return (0); 226 } 227 228 void 229 pf_purge_expired_fragments(void) 230 { 231 u_int32_t expire = time_uptime - 232 V_pf_default_rule.timeout[PFTM_FRAG]; 233 234 pf_purge_fragments(expire); 235 } 236 237 void 238 pf_purge_fragments(uint32_t expire) 239 { 240 struct pf_fragment *frag; 241 242 PF_FRAG_LOCK(); 243 while ((frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue)) != NULL) { 244 if (frag->fr_timeout > expire) 245 break; 246 247 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag)); 248 pf_free_fragment(frag); 249 } 250 251 PF_FRAG_UNLOCK(); 252 } 253 254 /* 255 * Try to flush old fragments to make space for new ones 256 */ 257 static void 258 pf_flush_fragments(void) 259 { 260 struct pf_fragment *frag; 261 int goal; 262 263 PF_FRAG_ASSERT(); 264 265 goal = uma_zone_get_cur(V_pf_frent_z) * 9 / 10; 266 DPFPRINTF(("trying to free %d frag entriess\n", goal)); 267 while (goal < uma_zone_get_cur(V_pf_frent_z)) { 268 frag = TAILQ_LAST(&V_pf_fragqueue, pf_fragqueue); 269 if (frag) 270 pf_free_fragment(frag); 271 else 272 break; 273 } 274 } 275 276 /* 277 * Remove a fragment from the fragment queue, free its fragment entries, 278 * and free the fragment itself. 279 */ 280 static void 281 pf_free_fragment(struct pf_fragment *frag) 282 { 283 struct pf_frent *frent; 284 285 PF_FRAG_ASSERT(); 286 287 RB_REMOVE(pf_frag_tree, &V_pf_frag_tree, frag); 288 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next); 289 290 /* Free all fragment entries */ 291 while ((frent = TAILQ_FIRST(&frag->fr_queue)) != NULL) { 292 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next); 293 294 m_freem(frent->fe_m); 295 uma_zfree(V_pf_frent_z, frent); 296 } 297 298 uma_zfree(V_pf_frag_z, frag); 299 } 300 301 static struct pf_fragment * 302 pf_find_fragment(struct pf_fragment_cmp *key, struct pf_frag_tree *tree) 303 { 304 struct pf_fragment *frag; 305 306 PF_FRAG_ASSERT(); 307 308 frag = RB_FIND(pf_frag_tree, tree, (struct pf_fragment *)key); 309 if (frag != NULL) { 310 TAILQ_REMOVE(&V_pf_fragqueue, frag, frag_next); 311 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next); 312 } 313 314 return (frag); 315 } 316 317 static struct pf_frent * 318 pf_create_fragment(u_short *reason) 319 { 320 struct pf_frent *frent; 321 322 PF_FRAG_ASSERT(); 323 324 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT); 325 if (frent == NULL) { 326 pf_flush_fragments(); 327 frent = uma_zalloc(V_pf_frent_z, M_NOWAIT); 328 if (frent == NULL) { 329 REASON_SET(reason, PFRES_MEMORY); 330 return (NULL); 331 } 332 } 333 334 return (frent); 335 } 336 337 /* 338 * Calculate the additional holes that were created in the fragment 339 * queue by inserting this fragment. A fragment in the middle 340 * creates one more hole by splitting. For each connected side, 341 * it loses one hole. 342 * Fragment entry must be in the queue when calling this function. 343 */ 344 static int 345 pf_frent_holes(struct pf_frent *frent) 346 { 347 struct pf_frent *prev = TAILQ_PREV(frent, pf_fragq, fr_next); 348 struct pf_frent *next = TAILQ_NEXT(frent, fr_next); 349 int holes = 1; 350 351 if (prev == NULL) { 352 if (frent->fe_off == 0) 353 holes--; 354 } else { 355 KASSERT(frent->fe_off != 0, ("frent->fe_off != 0")); 356 if (frent->fe_off == prev->fe_off + prev->fe_len) 357 holes--; 358 } 359 if (next == NULL) { 360 if (!frent->fe_mff) 361 holes--; 362 } else { 363 KASSERT(frent->fe_mff, ("frent->fe_mff")); 364 if (next->fe_off == frent->fe_off + frent->fe_len) 365 holes--; 366 } 367 return holes; 368 } 369 370 static inline int 371 pf_frent_index(struct pf_frent *frent) 372 { 373 /* 374 * We have an array of 16 entry points to the queue. A full size 375 * 65535 octet IP packet can have 8192 fragments. So the queue 376 * traversal length is at most 512 and at most 16 entry points are 377 * checked. We need 128 additional bytes on a 64 bit architecture. 378 */ 379 CTASSERT(((u_int16_t)0xffff &~ 7) / (0x10000 / PF_FRAG_ENTRY_POINTS) == 380 16 - 1); 381 CTASSERT(((u_int16_t)0xffff >> 3) / PF_FRAG_ENTRY_POINTS == 512 - 1); 382 383 return frent->fe_off / (0x10000 / PF_FRAG_ENTRY_POINTS); 384 } 385 386 static int 387 pf_frent_insert(struct pf_fragment *frag, struct pf_frent *frent, 388 struct pf_frent *prev) 389 { 390 int index; 391 392 CTASSERT(PF_FRAG_ENTRY_LIMIT <= 0xff); 393 394 /* 395 * A packet has at most 65536 octets. With 16 entry points, each one 396 * spawns 4096 octets. We limit these to 64 fragments each, which 397 * means on average every fragment must have at least 64 octets. 398 */ 399 index = pf_frent_index(frent); 400 if (frag->fr_entries[index] >= PF_FRAG_ENTRY_LIMIT) 401 return ENOBUFS; 402 frag->fr_entries[index]++; 403 404 if (prev == NULL) { 405 TAILQ_INSERT_HEAD(&frag->fr_queue, frent, fr_next); 406 } else { 407 KASSERT(prev->fe_off + prev->fe_len <= frent->fe_off, 408 ("overlapping fragment")); 409 TAILQ_INSERT_AFTER(&frag->fr_queue, prev, frent, fr_next); 410 } 411 412 if (frag->fr_firstoff[index] == NULL) { 413 KASSERT(prev == NULL || pf_frent_index(prev) < index, 414 ("prev == NULL || pf_frent_index(pref) < index")); 415 frag->fr_firstoff[index] = frent; 416 } else { 417 if (frent->fe_off < frag->fr_firstoff[index]->fe_off) { 418 KASSERT(prev == NULL || pf_frent_index(prev) < index, 419 ("prev == NULL || pf_frent_index(pref) < index")); 420 frag->fr_firstoff[index] = frent; 421 } else { 422 KASSERT(prev != NULL, ("prev != NULL")); 423 KASSERT(pf_frent_index(prev) == index, 424 ("pf_frent_index(prev) == index")); 425 } 426 } 427 428 frag->fr_holes += pf_frent_holes(frent); 429 430 return 0; 431 } 432 433 void 434 pf_frent_remove(struct pf_fragment *frag, struct pf_frent *frent) 435 { 436 #ifdef INVARIANTS 437 struct pf_frent *prev = TAILQ_PREV(frent, pf_fragq, fr_next); 438 #endif /* INVARIANTS */ 439 struct pf_frent *next = TAILQ_NEXT(frent, fr_next); 440 int index; 441 442 frag->fr_holes -= pf_frent_holes(frent); 443 444 index = pf_frent_index(frent); 445 KASSERT(frag->fr_firstoff[index] != NULL, ("frent not found")); 446 if (frag->fr_firstoff[index]->fe_off == frent->fe_off) { 447 if (next == NULL) { 448 frag->fr_firstoff[index] = NULL; 449 } else { 450 KASSERT(frent->fe_off + frent->fe_len <= next->fe_off, 451 ("overlapping fragment")); 452 if (pf_frent_index(next) == index) { 453 frag->fr_firstoff[index] = next; 454 } else { 455 frag->fr_firstoff[index] = NULL; 456 } 457 } 458 } else { 459 KASSERT(frag->fr_firstoff[index]->fe_off < frent->fe_off, 460 ("frag->fr_firstoff[index]->fe_off < frent->fe_off")); 461 KASSERT(prev != NULL, ("prev != NULL")); 462 KASSERT(prev->fe_off + prev->fe_len <= frent->fe_off, 463 ("overlapping fragment")); 464 KASSERT(pf_frent_index(prev) == index, 465 ("pf_frent_index(prev) == index")); 466 } 467 468 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next); 469 470 KASSERT(frag->fr_entries[index] > 0, ("No fragments remaining")); 471 frag->fr_entries[index]--; 472 } 473 474 struct pf_frent * 475 pf_frent_previous(struct pf_fragment *frag, struct pf_frent *frent) 476 { 477 struct pf_frent *prev, *next; 478 int index; 479 480 /* 481 * If there are no fragments after frag, take the final one. Assume 482 * that the global queue is not empty. 483 */ 484 prev = TAILQ_LAST(&frag->fr_queue, pf_fragq); 485 KASSERT(prev != NULL, ("prev != NULL")); 486 if (prev->fe_off <= frent->fe_off) 487 return prev; 488 /* 489 * We want to find a fragment entry that is before frag, but still 490 * close to it. Find the first fragment entry that is in the same 491 * entry point or in the first entry point after that. As we have 492 * already checked that there are entries behind frag, this will 493 * succeed. 494 */ 495 for (index = pf_frent_index(frent); index < PF_FRAG_ENTRY_POINTS; 496 index++) { 497 prev = frag->fr_firstoff[index]; 498 if (prev != NULL) 499 break; 500 } 501 KASSERT(prev != NULL, ("prev != NULL")); 502 /* 503 * In prev we may have a fragment from the same entry point that is 504 * before frent, or one that is just one position behind frent. 505 * In the latter case, we go back one step and have the predecessor. 506 * There may be none if the new fragment will be the first one. 507 */ 508 if (prev->fe_off > frent->fe_off) { 509 prev = TAILQ_PREV(prev, pf_fragq, fr_next); 510 if (prev == NULL) 511 return NULL; 512 KASSERT(prev->fe_off <= frent->fe_off, 513 ("prev->fe_off <= frent->fe_off")); 514 return prev; 515 } 516 /* 517 * In prev is the first fragment of the entry point. The offset 518 * of frag is behind it. Find the closest previous fragment. 519 */ 520 for (next = TAILQ_NEXT(prev, fr_next); next != NULL; 521 next = TAILQ_NEXT(next, fr_next)) { 522 if (next->fe_off > frent->fe_off) 523 break; 524 prev = next; 525 } 526 return prev; 527 } 528 529 static struct pf_fragment * 530 pf_fillup_fragment(struct pf_fragment_cmp *key, struct pf_frent *frent, 531 u_short *reason) 532 { 533 struct pf_frent *after, *next, *prev; 534 struct pf_fragment *frag; 535 uint16_t total; 536 537 PF_FRAG_ASSERT(); 538 539 /* No empty fragments. */ 540 if (frent->fe_len == 0) { 541 DPFPRINTF(("bad fragment: len 0\n")); 542 goto bad_fragment; 543 } 544 545 /* All fragments are 8 byte aligned. */ 546 if (frent->fe_mff && (frent->fe_len & 0x7)) { 547 DPFPRINTF(("bad fragment: mff and len %d\n", frent->fe_len)); 548 goto bad_fragment; 549 } 550 551 /* Respect maximum length, IP_MAXPACKET == IPV6_MAXPACKET. */ 552 if (frent->fe_off + frent->fe_len > IP_MAXPACKET) { 553 DPFPRINTF(("bad fragment: max packet %d\n", 554 frent->fe_off + frent->fe_len)); 555 goto bad_fragment; 556 } 557 558 DPFPRINTF((key->frc_af == AF_INET ? 559 "reass frag %d @ %d-%d\n" : "reass frag %#08x @ %d-%d\n", 560 key->frc_id, frent->fe_off, frent->fe_off + frent->fe_len)); 561 562 /* Fully buffer all of the fragments in this fragment queue. */ 563 frag = pf_find_fragment(key, &V_pf_frag_tree); 564 565 /* Create a new reassembly queue for this packet. */ 566 if (frag == NULL) { 567 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT); 568 if (frag == NULL) { 569 pf_flush_fragments(); 570 frag = uma_zalloc(V_pf_frag_z, M_NOWAIT); 571 if (frag == NULL) { 572 REASON_SET(reason, PFRES_MEMORY); 573 goto drop_fragment; 574 } 575 } 576 577 *(struct pf_fragment_cmp *)frag = *key; 578 memset(frag->fr_firstoff, 0, sizeof(frag->fr_firstoff)); 579 memset(frag->fr_entries, 0, sizeof(frag->fr_entries)); 580 frag->fr_timeout = time_uptime; 581 TAILQ_INIT(&frag->fr_queue); 582 frag->fr_maxlen = frent->fe_len; 583 frag->fr_holes = 1; 584 585 RB_INSERT(pf_frag_tree, &V_pf_frag_tree, frag); 586 TAILQ_INSERT_HEAD(&V_pf_fragqueue, frag, frag_next); 587 588 /* We do not have a previous fragment, cannot fail. */ 589 pf_frent_insert(frag, frent, NULL); 590 591 return (frag); 592 } 593 594 KASSERT(!TAILQ_EMPTY(&frag->fr_queue), ("!TAILQ_EMPTY()->fr_queue")); 595 596 /* Remember maximum fragment len for refragmentation. */ 597 if (frent->fe_len > frag->fr_maxlen) 598 frag->fr_maxlen = frent->fe_len; 599 600 /* Maximum data we have seen already. */ 601 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off + 602 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len; 603 604 /* Non terminal fragments must have more fragments flag. */ 605 if (frent->fe_off + frent->fe_len < total && !frent->fe_mff) 606 goto free_ipv6_fragment; 607 608 /* Check if we saw the last fragment already. */ 609 if (!TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_mff) { 610 if (frent->fe_off + frent->fe_len > total || 611 (frent->fe_off + frent->fe_len == total && frent->fe_mff)) 612 goto free_ipv6_fragment; 613 } else { 614 if (frent->fe_off + frent->fe_len == total && !frent->fe_mff) 615 goto free_ipv6_fragment; 616 } 617 618 /* Find neighbors for newly inserted fragment */ 619 prev = pf_frent_previous(frag, frent); 620 if (prev == NULL) { 621 after = TAILQ_FIRST(&frag->fr_queue); 622 KASSERT(after != NULL, ("after != NULL")); 623 } else { 624 after = TAILQ_NEXT(prev, fr_next); 625 } 626 627 if (prev != NULL && prev->fe_off + prev->fe_len > frent->fe_off) { 628 uint16_t precut; 629 630 if (frag->fr_af == AF_INET6) 631 goto free_fragment; 632 633 precut = prev->fe_off + prev->fe_len - frent->fe_off; 634 if (precut >= frent->fe_len) { 635 DPFPRINTF(("new frag overlapped\n")); 636 goto drop_fragment; 637 } 638 DPFPRINTF(("frag head overlap %d\n", precut)); 639 m_adj(frent->fe_m, precut); 640 frent->fe_off += precut; 641 frent->fe_len -= precut; 642 } 643 644 for (; after != NULL && frent->fe_off + frent->fe_len > after->fe_off; 645 after = next) { 646 uint16_t aftercut; 647 648 aftercut = frent->fe_off + frent->fe_len - after->fe_off; 649 if (aftercut < after->fe_len) { 650 DPFPRINTF(("frag tail overlap %d", aftercut)); 651 m_adj(after->fe_m, aftercut); 652 /* Fragment may switch queue as fe_off changes */ 653 pf_frent_remove(frag, after); 654 after->fe_off += aftercut; 655 after->fe_len -= aftercut; 656 /* Insert into correct queue */ 657 if (pf_frent_insert(frag, after, prev)) { 658 DPFPRINTF(("fragment requeue limit exceeded")); 659 m_freem(after->fe_m); 660 uma_zfree(V_pf_frent_z, after); 661 /* There is not way to recover */ 662 goto free_fragment; 663 } 664 break; 665 } 666 667 /* This fragment is completely overlapped, lose it. */ 668 DPFPRINTF(("old frag overlapped\n")); 669 next = TAILQ_NEXT(after, fr_next); 670 pf_frent_remove(frag, after); 671 m_freem(after->fe_m); 672 uma_zfree(V_pf_frent_z, after); 673 } 674 675 /* If part of the queue gets too long, there is not way to recover. */ 676 if (pf_frent_insert(frag, frent, prev)) { 677 DPFPRINTF(("fragment queue limit exceeded\n")); 678 goto bad_fragment; 679 } 680 681 return (frag); 682 683 free_ipv6_fragment: 684 if (frag->fr_af == AF_INET) 685 goto bad_fragment; 686 free_fragment: 687 /* 688 * RFC 5722, Errata 3089: When reassembling an IPv6 datagram, if one 689 * or more its constituent fragments is determined to be an overlapping 690 * fragment, the entire datagram (and any constituent fragments) MUST 691 * be silently discarded. 692 */ 693 DPFPRINTF(("flush overlapping fragments\n")); 694 pf_free_fragment(frag); 695 696 bad_fragment: 697 REASON_SET(reason, PFRES_FRAG); 698 drop_fragment: 699 uma_zfree(V_pf_frent_z, frent); 700 return (NULL); 701 } 702 703 static struct mbuf * 704 pf_join_fragment(struct pf_fragment *frag) 705 { 706 struct mbuf *m, *m2; 707 struct pf_frent *frent; 708 709 frent = TAILQ_FIRST(&frag->fr_queue); 710 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next); 711 712 m = frent->fe_m; 713 if ((frent->fe_hdrlen + frent->fe_len) < m->m_pkthdr.len) 714 m_adj(m, (frent->fe_hdrlen + frent->fe_len) - m->m_pkthdr.len); 715 uma_zfree(V_pf_frent_z, frent); 716 while ((frent = TAILQ_FIRST(&frag->fr_queue)) != NULL) { 717 TAILQ_REMOVE(&frag->fr_queue, frent, fr_next); 718 719 m2 = frent->fe_m; 720 /* Strip off ip header. */ 721 m_adj(m2, frent->fe_hdrlen); 722 /* Strip off any trailing bytes. */ 723 if (frent->fe_len < m2->m_pkthdr.len) 724 m_adj(m2, frent->fe_len - m2->m_pkthdr.len); 725 726 uma_zfree(V_pf_frent_z, frent); 727 m_cat(m, m2); 728 } 729 730 /* Remove from fragment queue. */ 731 pf_free_fragment(frag); 732 733 return (m); 734 } 735 736 #ifdef INET 737 static int 738 pf_reassemble(struct mbuf **m0, u_short *reason) 739 { 740 struct mbuf *m = *m0; 741 struct ip *ip = mtod(m, struct ip *); 742 struct pf_frent *frent; 743 struct pf_fragment *frag; 744 struct m_tag *mtag; 745 struct pf_fragment_tag *ftag; 746 struct pf_fragment_cmp key; 747 uint16_t total, hdrlen; 748 uint32_t frag_id; 749 uint16_t maxlen; 750 751 /* Get an entry for the fragment queue */ 752 if ((frent = pf_create_fragment(reason)) == NULL) 753 return (PF_DROP); 754 755 frent->fe_m = m; 756 frent->fe_hdrlen = ip->ip_hl << 2; 757 frent->fe_extoff = 0; 758 frent->fe_len = ntohs(ip->ip_len) - (ip->ip_hl << 2); 759 frent->fe_off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3; 760 frent->fe_mff = ntohs(ip->ip_off) & IP_MF; 761 762 pf_ip2key(ip, &key); 763 764 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) 765 return (PF_DROP); 766 767 /* The mbuf is part of the fragment entry, no direct free or access */ 768 m = *m0 = NULL; 769 770 if (frag->fr_holes) { 771 DPFPRINTF(("frag %d, holes %d\n", frag->fr_id, frag->fr_holes)); 772 return (PF_PASS); /* drop because *m0 is NULL, no error */ 773 } 774 775 /* We have all the data */ 776 frent = TAILQ_FIRST(&frag->fr_queue); 777 KASSERT(frent != NULL, ("frent != NULL")); 778 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off + 779 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len; 780 hdrlen = frent->fe_hdrlen; 781 782 maxlen = frag->fr_maxlen; 783 frag_id = frag->fr_id; 784 m = *m0 = pf_join_fragment(frag); 785 frag = NULL; 786 787 if (m->m_flags & M_PKTHDR) { 788 int plen = 0; 789 for (m = *m0; m; m = m->m_next) 790 plen += m->m_len; 791 m = *m0; 792 m->m_pkthdr.len = plen; 793 } 794 795 if ((mtag = m_tag_get(PACKET_TAG_PF_REASSEMBLED, 796 sizeof(struct pf_fragment_tag), M_NOWAIT)) == NULL) { 797 REASON_SET(reason, PFRES_SHORT); 798 /* PF_DROP requires a valid mbuf *m0 in pf_test() */ 799 return (PF_DROP); 800 } 801 ftag = (struct pf_fragment_tag *)(mtag + 1); 802 ftag->ft_hdrlen = hdrlen; 803 ftag->ft_extoff = 0; 804 ftag->ft_maxlen = maxlen; 805 ftag->ft_id = frag_id; 806 m_tag_prepend(m, mtag); 807 808 ip = mtod(m, struct ip *); 809 ip->ip_sum = pf_cksum_fixup(ip->ip_sum, ip->ip_len, 810 htons(hdrlen + total), 0); 811 ip->ip_len = htons(hdrlen + total); 812 ip->ip_sum = pf_cksum_fixup(ip->ip_sum, ip->ip_off, 813 ip->ip_off & ~(IP_MF|IP_OFFMASK), 0); 814 ip->ip_off &= ~(IP_MF|IP_OFFMASK); 815 816 if (hdrlen + total > IP_MAXPACKET) { 817 DPFPRINTF(("drop: too big: %d\n", total)); 818 ip->ip_len = 0; 819 REASON_SET(reason, PFRES_SHORT); 820 /* PF_DROP requires a valid mbuf *m0 in pf_test() */ 821 return (PF_DROP); 822 } 823 824 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len))); 825 return (PF_PASS); 826 } 827 #endif /* INET */ 828 829 #ifdef INET6 830 static int 831 pf_reassemble6(struct mbuf **m0, struct ip6_frag *fraghdr, 832 uint16_t hdrlen, uint16_t extoff, u_short *reason) 833 { 834 struct mbuf *m = *m0; 835 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 836 struct pf_frent *frent; 837 struct pf_fragment *frag; 838 struct pf_fragment_cmp key; 839 struct m_tag *mtag; 840 struct pf_fragment_tag *ftag; 841 int off; 842 uint32_t frag_id; 843 uint16_t total, maxlen; 844 uint8_t proto; 845 846 PF_FRAG_LOCK(); 847 848 /* Get an entry for the fragment queue. */ 849 if ((frent = pf_create_fragment(reason)) == NULL) { 850 PF_FRAG_UNLOCK(); 851 return (PF_DROP); 852 } 853 854 frent->fe_m = m; 855 frent->fe_hdrlen = hdrlen; 856 frent->fe_extoff = extoff; 857 frent->fe_len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - hdrlen; 858 frent->fe_off = ntohs(fraghdr->ip6f_offlg & IP6F_OFF_MASK); 859 frent->fe_mff = fraghdr->ip6f_offlg & IP6F_MORE_FRAG; 860 861 key.frc_src.v6 = ip6->ip6_src; 862 key.frc_dst.v6 = ip6->ip6_dst; 863 key.frc_af = AF_INET6; 864 /* Only the first fragment's protocol is relevant. */ 865 key.frc_proto = 0; 866 key.frc_id = fraghdr->ip6f_ident; 867 868 if ((frag = pf_fillup_fragment(&key, frent, reason)) == NULL) { 869 PF_FRAG_UNLOCK(); 870 return (PF_DROP); 871 } 872 873 /* The mbuf is part of the fragment entry, no direct free or access. */ 874 m = *m0 = NULL; 875 876 if (frag->fr_holes) { 877 DPFPRINTF(("frag %d, holes %d\n", frag->fr_id, 878 frag->fr_holes)); 879 PF_FRAG_UNLOCK(); 880 return (PF_PASS); /* Drop because *m0 is NULL, no error. */ 881 } 882 883 /* We have all the data. */ 884 frent = TAILQ_FIRST(&frag->fr_queue); 885 KASSERT(frent != NULL, ("frent != NULL")); 886 extoff = frent->fe_extoff; 887 maxlen = frag->fr_maxlen; 888 frag_id = frag->fr_id; 889 total = TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_off + 890 TAILQ_LAST(&frag->fr_queue, pf_fragq)->fe_len; 891 hdrlen = frent->fe_hdrlen - sizeof(struct ip6_frag); 892 893 m = *m0 = pf_join_fragment(frag); 894 frag = NULL; 895 896 PF_FRAG_UNLOCK(); 897 898 /* Take protocol from first fragment header. */ 899 m = m_getptr(m, hdrlen + offsetof(struct ip6_frag, ip6f_nxt), &off); 900 KASSERT(m, ("%s: short mbuf chain", __func__)); 901 proto = *(mtod(m, uint8_t *) + off); 902 m = *m0; 903 904 /* Delete frag6 header */ 905 if (ip6_deletefraghdr(m, hdrlen, M_NOWAIT) != 0) 906 goto fail; 907 908 if (m->m_flags & M_PKTHDR) { 909 int plen = 0; 910 for (m = *m0; m; m = m->m_next) 911 plen += m->m_len; 912 m = *m0; 913 m->m_pkthdr.len = plen; 914 } 915 916 if ((mtag = m_tag_get(PACKET_TAG_PF_REASSEMBLED, 917 sizeof(struct pf_fragment_tag), M_NOWAIT)) == NULL) 918 goto fail; 919 ftag = (struct pf_fragment_tag *)(mtag + 1); 920 ftag->ft_hdrlen = hdrlen; 921 ftag->ft_extoff = extoff; 922 ftag->ft_maxlen = maxlen; 923 ftag->ft_id = frag_id; 924 m_tag_prepend(m, mtag); 925 926 ip6 = mtod(m, struct ip6_hdr *); 927 ip6->ip6_plen = htons(hdrlen - sizeof(struct ip6_hdr) + total); 928 if (extoff) { 929 /* Write protocol into next field of last extension header. */ 930 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt), 931 &off); 932 KASSERT(m, ("%s: short mbuf chain", __func__)); 933 *(mtod(m, char *) + off) = proto; 934 m = *m0; 935 } else 936 ip6->ip6_nxt = proto; 937 938 if (hdrlen - sizeof(struct ip6_hdr) + total > IPV6_MAXPACKET) { 939 DPFPRINTF(("drop: too big: %d\n", total)); 940 ip6->ip6_plen = 0; 941 REASON_SET(reason, PFRES_SHORT); 942 /* PF_DROP requires a valid mbuf *m0 in pf_test6(). */ 943 return (PF_DROP); 944 } 945 946 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip6->ip6_plen))); 947 return (PF_PASS); 948 949 fail: 950 REASON_SET(reason, PFRES_MEMORY); 951 /* PF_DROP requires a valid mbuf *m0 in pf_test6(), will free later. */ 952 return (PF_DROP); 953 } 954 #endif /* INET6 */ 955 956 #ifdef INET6 957 int 958 pf_max_frag_size(struct mbuf *m) 959 { 960 struct m_tag *tag; 961 struct pf_fragment_tag *ftag; 962 963 tag = m_tag_find(m, PACKET_TAG_PF_REASSEMBLED, NULL); 964 if (tag == NULL) 965 return (m->m_pkthdr.len); 966 967 ftag = (struct pf_fragment_tag *)(tag + 1); 968 969 return (ftag->ft_maxlen); 970 } 971 972 int 973 pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag, 974 struct ifnet *rt, bool forward) 975 { 976 struct mbuf *m = *m0, *t; 977 struct ip6_hdr *hdr; 978 struct pf_fragment_tag *ftag = (struct pf_fragment_tag *)(mtag + 1); 979 struct pf_pdesc pd; 980 uint32_t frag_id; 981 uint16_t hdrlen, extoff, maxlen; 982 uint8_t proto; 983 int error, action; 984 985 hdrlen = ftag->ft_hdrlen; 986 extoff = ftag->ft_extoff; 987 maxlen = ftag->ft_maxlen; 988 frag_id = ftag->ft_id; 989 m_tag_delete(m, mtag); 990 mtag = NULL; 991 ftag = NULL; 992 993 if (extoff) { 994 int off; 995 996 /* Use protocol from next field of last extension header */ 997 m = m_getptr(m, extoff + offsetof(struct ip6_ext, ip6e_nxt), 998 &off); 999 KASSERT((m != NULL), ("pf_refragment6: short mbuf chain")); 1000 proto = *(mtod(m, uint8_t *) + off); 1001 *(mtod(m, char *) + off) = IPPROTO_FRAGMENT; 1002 m = *m0; 1003 } else { 1004 hdr = mtod(m, struct ip6_hdr *); 1005 proto = hdr->ip6_nxt; 1006 hdr->ip6_nxt = IPPROTO_FRAGMENT; 1007 } 1008 1009 /* In case of link-local traffic we'll need a scope set. */ 1010 hdr = mtod(m, struct ip6_hdr *); 1011 1012 in6_setscope(&hdr->ip6_src, ifp, NULL); 1013 in6_setscope(&hdr->ip6_dst, ifp, NULL); 1014 1015 /* The MTU must be a multiple of 8 bytes, or we risk doing the 1016 * fragmentation wrong. */ 1017 maxlen = maxlen & ~7; 1018 1019 /* 1020 * Maxlen may be less than 8 if there was only a single 1021 * fragment. As it was fragmented before, add a fragment 1022 * header also for a single fragment. If total or maxlen 1023 * is less than 8, ip6_fragment() will return EMSGSIZE and 1024 * we drop the packet. 1025 */ 1026 error = ip6_fragment(ifp, m, hdrlen, proto, maxlen, frag_id); 1027 m = (*m0)->m_nextpkt; 1028 (*m0)->m_nextpkt = NULL; 1029 if (error == 0) { 1030 /* The first mbuf contains the unfragmented packet. */ 1031 m_freem(*m0); 1032 *m0 = NULL; 1033 action = PF_PASS; 1034 } else { 1035 /* Drop expects an mbuf to free. */ 1036 DPFPRINTF(("refragment error %d\n", error)); 1037 action = PF_DROP; 1038 } 1039 for (; m; m = t) { 1040 t = m->m_nextpkt; 1041 m->m_nextpkt = NULL; 1042 m->m_flags |= M_SKIP_FIREWALL; 1043 memset(&pd, 0, sizeof(pd)); 1044 pd.pf_mtag = pf_find_mtag(m); 1045 if (error != 0) { 1046 m_freem(m); 1047 continue; 1048 } 1049 if (rt != NULL) { 1050 struct sockaddr_in6 dst; 1051 hdr = mtod(m, struct ip6_hdr *); 1052 1053 bzero(&dst, sizeof(dst)); 1054 dst.sin6_family = AF_INET6; 1055 dst.sin6_len = sizeof(dst); 1056 dst.sin6_addr = hdr->ip6_dst; 1057 1058 if (m->m_pkthdr.len <= if_getmtu(ifp)) { 1059 nd6_output_ifp(rt, rt, m, &dst, NULL); 1060 } else { 1061 in6_ifstat_inc(ifp, ifs6_in_toobig); 1062 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, 1063 if_getmtu(ifp)); 1064 } 1065 } else if (forward) { 1066 MPASS(m->m_pkthdr.rcvif != NULL); 1067 ip6_forward(m, 0); 1068 } else { 1069 (void)ip6_output(m, NULL, NULL, 0, NULL, NULL, 1070 NULL); 1071 } 1072 } 1073 1074 return (action); 1075 } 1076 #endif /* INET6 */ 1077 1078 #ifdef INET 1079 int 1080 pf_normalize_ip(u_short *reason, struct pf_pdesc *pd) 1081 { 1082 struct pf_krule *r; 1083 struct ip *h = mtod(pd->m, struct ip *); 1084 int mff = (ntohs(h->ip_off) & IP_MF); 1085 int hlen = h->ip_hl << 2; 1086 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 1087 u_int16_t max; 1088 int ip_len; 1089 int tag = -1; 1090 int verdict; 1091 bool scrub_compat; 1092 1093 PF_RULES_RASSERT(); 1094 1095 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); 1096 /* 1097 * Check if there are any scrub rules, matching or not. 1098 * Lack of scrub rules means: 1099 * - enforced packet normalization operation just like in OpenBSD 1100 * - fragment reassembly depends on V_pf_status.reass 1101 * With scrub rules: 1102 * - packet normalization is performed if there is a matching scrub rule 1103 * - fragment reassembly is performed if the matching rule has no 1104 * PFRULE_FRAGMENT_NOREASS flag 1105 */ 1106 scrub_compat = (r != NULL); 1107 while (r != NULL) { 1108 pf_counter_u64_add(&r->evaluations, 1); 1109 if (pfi_kkif_match(r->kif, pd->kif) == r->ifnot) 1110 r = r->skip[PF_SKIP_IFP]; 1111 else if (r->direction && r->direction != pd->dir) 1112 r = r->skip[PF_SKIP_DIR]; 1113 else if (r->af && r->af != AF_INET) 1114 r = r->skip[PF_SKIP_AF]; 1115 else if (r->proto && r->proto != h->ip_p) 1116 r = r->skip[PF_SKIP_PROTO]; 1117 else if (PF_MISMATCHAW(&r->src.addr, 1118 (struct pf_addr *)&h->ip_src.s_addr, AF_INET, 1119 r->src.neg, pd->kif, M_GETFIB(pd->m))) 1120 r = r->skip[PF_SKIP_SRC_ADDR]; 1121 else if (PF_MISMATCHAW(&r->dst.addr, 1122 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET, 1123 r->dst.neg, NULL, M_GETFIB(pd->m))) 1124 r = r->skip[PF_SKIP_DST_ADDR]; 1125 else if (r->match_tag && !pf_match_tag(pd->m, r, &tag, 1126 pd->pf_mtag ? pd->pf_mtag->tag : 0)) 1127 r = TAILQ_NEXT(r, entries); 1128 else 1129 break; 1130 } 1131 1132 if (scrub_compat) { 1133 /* With scrub rules present IPv4 normalization happens only 1134 * if one of rules has matched and it's not a "no scrub" rule */ 1135 if (r == NULL || r->action == PF_NOSCRUB) 1136 return (PF_PASS); 1137 1138 pf_counter_u64_critical_enter(); 1139 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1); 1140 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len); 1141 pf_counter_u64_critical_exit(); 1142 pf_rule_to_actions(r, &pd->act); 1143 } 1144 1145 /* Check for illegal packets */ 1146 if (hlen < (int)sizeof(struct ip)) { 1147 REASON_SET(reason, PFRES_NORM); 1148 goto drop; 1149 } 1150 1151 if (hlen > ntohs(h->ip_len)) { 1152 REASON_SET(reason, PFRES_NORM); 1153 goto drop; 1154 } 1155 1156 /* Clear IP_DF if the rule uses the no-df option or we're in no-df mode */ 1157 if (((!scrub_compat && V_pf_status.reass & PF_REASS_NODF) || 1158 (r != NULL && r->rule_flag & PFRULE_NODF)) && 1159 (h->ip_off & htons(IP_DF)) 1160 ) { 1161 u_int16_t ip_off = h->ip_off; 1162 1163 h->ip_off &= htons(~IP_DF); 1164 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0); 1165 } 1166 1167 /* We will need other tests here */ 1168 if (!fragoff && !mff) 1169 goto no_fragment; 1170 1171 /* We're dealing with a fragment now. Don't allow fragments 1172 * with IP_DF to enter the cache. If the flag was cleared by 1173 * no-df above, fine. Otherwise drop it. 1174 */ 1175 if (h->ip_off & htons(IP_DF)) { 1176 DPFPRINTF(("IP_DF\n")); 1177 goto bad; 1178 } 1179 1180 ip_len = ntohs(h->ip_len) - hlen; 1181 1182 /* All fragments are 8 byte aligned */ 1183 if (mff && (ip_len & 0x7)) { 1184 DPFPRINTF(("mff and %d\n", ip_len)); 1185 goto bad; 1186 } 1187 1188 /* Respect maximum length */ 1189 if (fragoff + ip_len > IP_MAXPACKET) { 1190 DPFPRINTF(("max packet %d\n", fragoff + ip_len)); 1191 goto bad; 1192 } 1193 1194 if ((!scrub_compat && V_pf_status.reass) || 1195 (r != NULL && !(r->rule_flag & PFRULE_FRAGMENT_NOREASS)) 1196 ) { 1197 max = fragoff + ip_len; 1198 1199 /* Fully buffer all of the fragments 1200 * Might return a completely reassembled mbuf, or NULL */ 1201 PF_FRAG_LOCK(); 1202 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max)); 1203 verdict = pf_reassemble(&pd->m, reason); 1204 PF_FRAG_UNLOCK(); 1205 1206 if (verdict != PF_PASS) 1207 return (PF_DROP); 1208 1209 if (pd->m == NULL) 1210 return (PF_DROP); 1211 1212 h = mtod(pd->m, struct ip *); 1213 pd->tot_len = htons(h->ip_len); 1214 1215 no_fragment: 1216 /* At this point, only IP_DF is allowed in ip_off */ 1217 if (h->ip_off & ~htons(IP_DF)) { 1218 u_int16_t ip_off = h->ip_off; 1219 1220 h->ip_off &= htons(IP_DF); 1221 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0); 1222 } 1223 } 1224 1225 return (PF_PASS); 1226 1227 bad: 1228 DPFPRINTF(("dropping bad fragment\n")); 1229 REASON_SET(reason, PFRES_FRAG); 1230 drop: 1231 if (r != NULL && r->log) 1232 PFLOG_PACKET(PF_DROP, *reason, r, NULL, NULL, pd, 1, NULL); 1233 1234 return (PF_DROP); 1235 } 1236 #endif 1237 1238 #ifdef INET6 1239 int 1240 pf_normalize_ip6(int off, u_short *reason, 1241 struct pf_pdesc *pd) 1242 { 1243 struct pf_krule *r; 1244 struct ip6_hdr *h; 1245 struct ip6_frag frag; 1246 bool scrub_compat; 1247 1248 PF_RULES_RASSERT(); 1249 1250 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); 1251 /* 1252 * Check if there are any scrub rules, matching or not. 1253 * Lack of scrub rules means: 1254 * - enforced packet normalization operation just like in OpenBSD 1255 * With scrub rules: 1256 * - packet normalization is performed if there is a matching scrub rule 1257 * XXX: Fragment reassembly always performed for IPv6! 1258 */ 1259 scrub_compat = (r != NULL); 1260 while (r != NULL) { 1261 pf_counter_u64_add(&r->evaluations, 1); 1262 if (pfi_kkif_match(r->kif, pd->kif) == r->ifnot) 1263 r = r->skip[PF_SKIP_IFP]; 1264 else if (r->direction && r->direction != pd->dir) 1265 r = r->skip[PF_SKIP_DIR]; 1266 else if (r->af && r->af != AF_INET6) 1267 r = r->skip[PF_SKIP_AF]; 1268 else if (r->proto && r->proto != pd->proto) 1269 r = r->skip[PF_SKIP_PROTO]; 1270 else if (PF_MISMATCHAW(&r->src.addr, 1271 (struct pf_addr *)&pd->src, AF_INET6, 1272 r->src.neg, pd->kif, M_GETFIB(pd->m))) 1273 r = r->skip[PF_SKIP_SRC_ADDR]; 1274 else if (PF_MISMATCHAW(&r->dst.addr, 1275 (struct pf_addr *)&pd->dst, AF_INET6, 1276 r->dst.neg, NULL, M_GETFIB(pd->m))) 1277 r = r->skip[PF_SKIP_DST_ADDR]; 1278 else 1279 break; 1280 } 1281 1282 if (scrub_compat) { 1283 /* With scrub rules present IPv6 normalization happens only 1284 * if one of rules has matched and it's not a "no scrub" rule */ 1285 if (r == NULL || r->action == PF_NOSCRUB) 1286 return (PF_PASS); 1287 1288 pf_counter_u64_critical_enter(); 1289 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1); 1290 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len); 1291 pf_counter_u64_critical_exit(); 1292 pf_rule_to_actions(r, &pd->act); 1293 } 1294 1295 if (!pf_pull_hdr(pd->m, off, &frag, sizeof(frag), NULL, reason, AF_INET6)) 1296 return (PF_DROP); 1297 1298 /* Offset now points to data portion. */ 1299 off += sizeof(frag); 1300 1301 if (pd->virtual_proto == PF_VPROTO_FRAGMENT) { 1302 /* Returns PF_DROP or *m0 is NULL or completely reassembled 1303 * mbuf. */ 1304 if (pf_reassemble6(&pd->m, &frag, off, pd->extoff, reason) != PF_PASS) 1305 return (PF_DROP); 1306 if (pd->m == NULL) 1307 return (PF_DROP); 1308 h = mtod(pd->m, struct ip6_hdr *); 1309 pd->tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 1310 } 1311 1312 return (PF_PASS); 1313 } 1314 #endif /* INET6 */ 1315 1316 int 1317 pf_normalize_tcp(struct pf_pdesc *pd) 1318 { 1319 struct pf_krule *r, *rm = NULL; 1320 struct tcphdr *th = &pd->hdr.tcp; 1321 int rewrite = 0; 1322 u_short reason; 1323 u_int16_t flags; 1324 sa_family_t af = pd->af; 1325 int srs; 1326 1327 PF_RULES_RASSERT(); 1328 1329 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); 1330 /* Check if there any scrub rules. Lack of scrub rules means enforced 1331 * packet normalization operation just like in OpenBSD. */ 1332 srs = (r != NULL); 1333 while (r != NULL) { 1334 pf_counter_u64_add(&r->evaluations, 1); 1335 if (pfi_kkif_match(r->kif, pd->kif) == r->ifnot) 1336 r = r->skip[PF_SKIP_IFP]; 1337 else if (r->direction && r->direction != pd->dir) 1338 r = r->skip[PF_SKIP_DIR]; 1339 else if (r->af && r->af != af) 1340 r = r->skip[PF_SKIP_AF]; 1341 else if (r->proto && r->proto != pd->proto) 1342 r = r->skip[PF_SKIP_PROTO]; 1343 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 1344 r->src.neg, pd->kif, M_GETFIB(pd->m))) 1345 r = r->skip[PF_SKIP_SRC_ADDR]; 1346 else if (r->src.port_op && !pf_match_port(r->src.port_op, 1347 r->src.port[0], r->src.port[1], th->th_sport)) 1348 r = r->skip[PF_SKIP_SRC_PORT]; 1349 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 1350 r->dst.neg, NULL, M_GETFIB(pd->m))) 1351 r = r->skip[PF_SKIP_DST_ADDR]; 1352 else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 1353 r->dst.port[0], r->dst.port[1], th->th_dport)) 1354 r = r->skip[PF_SKIP_DST_PORT]; 1355 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match( 1356 pf_osfp_fingerprint(pd, th), 1357 r->os_fingerprint)) 1358 r = TAILQ_NEXT(r, entries); 1359 else { 1360 rm = r; 1361 break; 1362 } 1363 } 1364 1365 if (srs) { 1366 /* With scrub rules present TCP normalization happens only 1367 * if one of rules has matched and it's not a "no scrub" rule */ 1368 if (rm == NULL || rm->action == PF_NOSCRUB) 1369 return (PF_PASS); 1370 1371 pf_counter_u64_critical_enter(); 1372 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1); 1373 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len); 1374 pf_counter_u64_critical_exit(); 1375 pf_rule_to_actions(rm, &pd->act); 1376 } 1377 1378 if (rm && rm->rule_flag & PFRULE_REASSEMBLE_TCP) 1379 pd->flags |= PFDESC_TCP_NORM; 1380 1381 flags = tcp_get_flags(th); 1382 if (flags & TH_SYN) { 1383 /* Illegal packet */ 1384 if (flags & TH_RST) 1385 goto tcp_drop; 1386 1387 if (flags & TH_FIN) 1388 goto tcp_drop; 1389 } else { 1390 /* Illegal packet */ 1391 if (!(flags & (TH_ACK|TH_RST))) 1392 goto tcp_drop; 1393 } 1394 1395 if (!(flags & TH_ACK)) { 1396 /* These flags are only valid if ACK is set */ 1397 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG)) 1398 goto tcp_drop; 1399 } 1400 1401 /* Check for illegal header length */ 1402 if (th->th_off < (sizeof(struct tcphdr) >> 2)) 1403 goto tcp_drop; 1404 1405 /* If flags changed, or reserved data set, then adjust */ 1406 if (flags != tcp_get_flags(th) || 1407 (tcp_get_flags(th) & (TH_RES1|TH_RES2|TH_RES2)) != 0) { 1408 u_int16_t ov, nv; 1409 1410 ov = *(u_int16_t *)(&th->th_ack + 1); 1411 flags &= ~(TH_RES1 | TH_RES2 | TH_RES3); 1412 tcp_set_flags(th, flags); 1413 nv = *(u_int16_t *)(&th->th_ack + 1); 1414 1415 th->th_sum = pf_proto_cksum_fixup(pd->m, th->th_sum, ov, nv, 0); 1416 rewrite = 1; 1417 } 1418 1419 /* Remove urgent pointer, if TH_URG is not set */ 1420 if (!(flags & TH_URG) && th->th_urp) { 1421 th->th_sum = pf_proto_cksum_fixup(pd->m, th->th_sum, th->th_urp, 1422 0, 0); 1423 th->th_urp = 0; 1424 rewrite = 1; 1425 } 1426 1427 /* copy back packet headers if we sanitized */ 1428 if (rewrite) 1429 m_copyback(pd->m, pd->off, sizeof(*th), (caddr_t)th); 1430 1431 return (PF_PASS); 1432 1433 tcp_drop: 1434 REASON_SET(&reason, PFRES_NORM); 1435 if (rm != NULL && r->log) 1436 PFLOG_PACKET(PF_DROP, reason, r, NULL, NULL, pd, 1, NULL); 1437 return (PF_DROP); 1438 } 1439 1440 int 1441 pf_normalize_tcp_init(struct pf_pdesc *pd, struct tcphdr *th, 1442 struct pf_state_peer *src) 1443 { 1444 u_int32_t tsval, tsecr; 1445 u_int8_t hdr[60]; 1446 u_int8_t *opt; 1447 1448 KASSERT((src->scrub == NULL), 1449 ("pf_normalize_tcp_init: src->scrub != NULL")); 1450 1451 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT); 1452 if (src->scrub == NULL) 1453 return (1); 1454 1455 switch (pd->af) { 1456 #ifdef INET 1457 case AF_INET: { 1458 struct ip *h = mtod(pd->m, struct ip *); 1459 src->scrub->pfss_ttl = h->ip_ttl; 1460 break; 1461 } 1462 #endif /* INET */ 1463 #ifdef INET6 1464 case AF_INET6: { 1465 struct ip6_hdr *h = mtod(pd->m, struct ip6_hdr *); 1466 src->scrub->pfss_ttl = h->ip6_hlim; 1467 break; 1468 } 1469 #endif /* INET6 */ 1470 default: 1471 unhandled_af(pd->af); 1472 } 1473 1474 /* 1475 * All normalizations below are only begun if we see the start of 1476 * the connections. They must all set an enabled bit in pfss_flags 1477 */ 1478 if ((tcp_get_flags(th) & TH_SYN) == 0) 1479 return (0); 1480 1481 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub && 1482 pf_pull_hdr(pd->m, pd->off, hdr, th->th_off << 2, NULL, NULL, pd->af)) { 1483 /* Diddle with TCP options */ 1484 int hlen; 1485 opt = hdr + sizeof(struct tcphdr); 1486 hlen = (th->th_off << 2) - sizeof(struct tcphdr); 1487 while (hlen >= TCPOLEN_TIMESTAMP) { 1488 switch (*opt) { 1489 case TCPOPT_EOL: /* FALLTHROUGH */ 1490 case TCPOPT_NOP: 1491 opt++; 1492 hlen--; 1493 break; 1494 case TCPOPT_TIMESTAMP: 1495 if (opt[1] >= TCPOLEN_TIMESTAMP) { 1496 src->scrub->pfss_flags |= 1497 PFSS_TIMESTAMP; 1498 src->scrub->pfss_ts_mod = 1499 arc4random(); 1500 1501 /* note PFSS_PAWS not set yet */ 1502 memcpy(&tsval, &opt[2], 1503 sizeof(u_int32_t)); 1504 memcpy(&tsecr, &opt[6], 1505 sizeof(u_int32_t)); 1506 src->scrub->pfss_tsval0 = ntohl(tsval); 1507 src->scrub->pfss_tsval = ntohl(tsval); 1508 src->scrub->pfss_tsecr = ntohl(tsecr); 1509 getmicrouptime(&src->scrub->pfss_last); 1510 } 1511 /* FALLTHROUGH */ 1512 default: 1513 hlen -= MAX(opt[1], 2); 1514 opt += MAX(opt[1], 2); 1515 break; 1516 } 1517 } 1518 } 1519 1520 return (0); 1521 } 1522 1523 void 1524 pf_normalize_tcp_cleanup(struct pf_kstate *state) 1525 { 1526 /* XXX Note: this also cleans up SCTP. */ 1527 uma_zfree(V_pf_state_scrub_z, state->src.scrub); 1528 uma_zfree(V_pf_state_scrub_z, state->dst.scrub); 1529 1530 /* Someday... flush the TCP segment reassembly descriptors. */ 1531 } 1532 int 1533 pf_normalize_sctp_init(struct pf_pdesc *pd, struct pf_state_peer *src, 1534 struct pf_state_peer *dst) 1535 { 1536 src->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT); 1537 if (src->scrub == NULL) 1538 return (1); 1539 1540 dst->scrub = uma_zalloc(V_pf_state_scrub_z, M_ZERO | M_NOWAIT); 1541 if (dst->scrub == NULL) { 1542 uma_zfree(V_pf_state_scrub_z, src); 1543 return (1); 1544 } 1545 1546 dst->scrub->pfss_v_tag = pd->sctp_initiate_tag; 1547 1548 return (0); 1549 } 1550 1551 int 1552 pf_normalize_tcp_stateful(struct pf_pdesc *pd, 1553 u_short *reason, struct tcphdr *th, struct pf_kstate *state, 1554 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback) 1555 { 1556 struct timeval uptime; 1557 u_int32_t tsval, tsecr; 1558 u_int tsval_from_last; 1559 u_int8_t hdr[60]; 1560 u_int8_t *opt; 1561 int copyback = 0; 1562 int got_ts = 0; 1563 size_t startoff; 1564 1565 KASSERT((src->scrub || dst->scrub), 1566 ("%s: src->scrub && dst->scrub!", __func__)); 1567 1568 /* 1569 * Enforce the minimum TTL seen for this connection. Negate a common 1570 * technique to evade an intrusion detection system and confuse 1571 * firewall state code. 1572 */ 1573 switch (pd->af) { 1574 #ifdef INET 1575 case AF_INET: { 1576 if (src->scrub) { 1577 struct ip *h = mtod(pd->m, struct ip *); 1578 if (h->ip_ttl > src->scrub->pfss_ttl) 1579 src->scrub->pfss_ttl = h->ip_ttl; 1580 h->ip_ttl = src->scrub->pfss_ttl; 1581 } 1582 break; 1583 } 1584 #endif /* INET */ 1585 #ifdef INET6 1586 case AF_INET6: { 1587 if (src->scrub) { 1588 struct ip6_hdr *h = mtod(pd->m, struct ip6_hdr *); 1589 if (h->ip6_hlim > src->scrub->pfss_ttl) 1590 src->scrub->pfss_ttl = h->ip6_hlim; 1591 h->ip6_hlim = src->scrub->pfss_ttl; 1592 } 1593 break; 1594 } 1595 #endif /* INET6 */ 1596 default: 1597 unhandled_af(pd->af); 1598 } 1599 1600 if (th->th_off > (sizeof(struct tcphdr) >> 2) && 1601 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) || 1602 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) && 1603 pf_pull_hdr(pd->m, pd->off, hdr, th->th_off << 2, NULL, NULL, pd->af)) { 1604 /* Diddle with TCP options */ 1605 int hlen; 1606 opt = hdr + sizeof(struct tcphdr); 1607 hlen = (th->th_off << 2) - sizeof(struct tcphdr); 1608 while (hlen >= TCPOLEN_TIMESTAMP) { 1609 startoff = opt - (hdr + sizeof(struct tcphdr)); 1610 switch (*opt) { 1611 case TCPOPT_EOL: /* FALLTHROUGH */ 1612 case TCPOPT_NOP: 1613 opt++; 1614 hlen--; 1615 break; 1616 case TCPOPT_TIMESTAMP: 1617 /* Modulate the timestamps. Can be used for 1618 * NAT detection, OS uptime determination or 1619 * reboot detection. 1620 */ 1621 1622 if (got_ts) { 1623 /* Huh? Multiple timestamps!? */ 1624 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1625 DPFPRINTF(("multiple TS??\n")); 1626 pf_print_state(state); 1627 printf("\n"); 1628 } 1629 REASON_SET(reason, PFRES_TS); 1630 return (PF_DROP); 1631 } 1632 if (opt[1] >= TCPOLEN_TIMESTAMP) { 1633 memcpy(&tsval, &opt[2], 1634 sizeof(u_int32_t)); 1635 if (tsval && src->scrub && 1636 (src->scrub->pfss_flags & 1637 PFSS_TIMESTAMP)) { 1638 tsval = ntohl(tsval); 1639 copyback += pf_patch_32(pd, 1640 &opt[2], 1641 htonl(tsval + 1642 src->scrub->pfss_ts_mod), 1643 PF_ALGNMNT(startoff)); 1644 } 1645 1646 /* Modulate TS reply iff valid (!0) */ 1647 memcpy(&tsecr, &opt[6], 1648 sizeof(u_int32_t)); 1649 if (tsecr && dst->scrub && 1650 (dst->scrub->pfss_flags & 1651 PFSS_TIMESTAMP)) { 1652 tsecr = ntohl(tsecr) 1653 - dst->scrub->pfss_ts_mod; 1654 copyback += pf_patch_32(pd, 1655 &opt[6], 1656 htonl(tsecr), 1657 PF_ALGNMNT(startoff)); 1658 } 1659 got_ts = 1; 1660 } 1661 /* FALLTHROUGH */ 1662 default: 1663 hlen -= MAX(opt[1], 2); 1664 opt += MAX(opt[1], 2); 1665 break; 1666 } 1667 } 1668 if (copyback) { 1669 /* Copyback the options, caller copys back header */ 1670 *writeback = 1; 1671 m_copyback(pd->m, pd->off + sizeof(struct tcphdr), 1672 (th->th_off << 2) - sizeof(struct tcphdr), hdr + 1673 sizeof(struct tcphdr)); 1674 } 1675 } 1676 1677 /* 1678 * Must invalidate PAWS checks on connections idle for too long. 1679 * The fastest allowed timestamp clock is 1ms. That turns out to 1680 * be about 24 days before it wraps. XXX Right now our lowerbound 1681 * TS echo check only works for the first 12 days of a connection 1682 * when the TS has exhausted half its 32bit space 1683 */ 1684 #define TS_MAX_IDLE (24*24*60*60) 1685 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */ 1686 1687 getmicrouptime(&uptime); 1688 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) && 1689 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE || 1690 time_uptime - (state->creation / 1000) > TS_MAX_CONN)) { 1691 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1692 DPFPRINTF(("src idled out of PAWS\n")); 1693 pf_print_state(state); 1694 printf("\n"); 1695 } 1696 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS) 1697 | PFSS_PAWS_IDLED; 1698 } 1699 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) && 1700 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) { 1701 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1702 DPFPRINTF(("dst idled out of PAWS\n")); 1703 pf_print_state(state); 1704 printf("\n"); 1705 } 1706 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS) 1707 | PFSS_PAWS_IDLED; 1708 } 1709 1710 if (got_ts && src->scrub && dst->scrub && 1711 (src->scrub->pfss_flags & PFSS_PAWS) && 1712 (dst->scrub->pfss_flags & PFSS_PAWS)) { 1713 /* Validate that the timestamps are "in-window". 1714 * RFC1323 describes TCP Timestamp options that allow 1715 * measurement of RTT (round trip time) and PAWS 1716 * (protection against wrapped sequence numbers). PAWS 1717 * gives us a set of rules for rejecting packets on 1718 * long fat pipes (packets that were somehow delayed 1719 * in transit longer than the time it took to send the 1720 * full TCP sequence space of 4Gb). We can use these 1721 * rules and infer a few others that will let us treat 1722 * the 32bit timestamp and the 32bit echoed timestamp 1723 * as sequence numbers to prevent a blind attacker from 1724 * inserting packets into a connection. 1725 * 1726 * RFC1323 tells us: 1727 * - The timestamp on this packet must be greater than 1728 * or equal to the last value echoed by the other 1729 * endpoint. The RFC says those will be discarded 1730 * since it is a dup that has already been acked. 1731 * This gives us a lowerbound on the timestamp. 1732 * timestamp >= other last echoed timestamp 1733 * - The timestamp will be less than or equal to 1734 * the last timestamp plus the time between the 1735 * last packet and now. The RFC defines the max 1736 * clock rate as 1ms. We will allow clocks to be 1737 * up to 10% fast and will allow a total difference 1738 * or 30 seconds due to a route change. And this 1739 * gives us an upperbound on the timestamp. 1740 * timestamp <= last timestamp + max ticks 1741 * We have to be careful here. Windows will send an 1742 * initial timestamp of zero and then initialize it 1743 * to a random value after the 3whs; presumably to 1744 * avoid a DoS by having to call an expensive RNG 1745 * during a SYN flood. Proof MS has at least one 1746 * good security geek. 1747 * 1748 * - The TCP timestamp option must also echo the other 1749 * endpoints timestamp. The timestamp echoed is the 1750 * one carried on the earliest unacknowledged segment 1751 * on the left edge of the sequence window. The RFC 1752 * states that the host will reject any echoed 1753 * timestamps that were larger than any ever sent. 1754 * This gives us an upperbound on the TS echo. 1755 * tescr <= largest_tsval 1756 * - The lowerbound on the TS echo is a little more 1757 * tricky to determine. The other endpoint's echoed 1758 * values will not decrease. But there may be 1759 * network conditions that re-order packets and 1760 * cause our view of them to decrease. For now the 1761 * only lowerbound we can safely determine is that 1762 * the TS echo will never be less than the original 1763 * TS. XXX There is probably a better lowerbound. 1764 * Remove TS_MAX_CONN with better lowerbound check. 1765 * tescr >= other original TS 1766 * 1767 * It is also important to note that the fastest 1768 * timestamp clock of 1ms will wrap its 32bit space in 1769 * 24 days. So we just disable TS checking after 24 1770 * days of idle time. We actually must use a 12d 1771 * connection limit until we can come up with a better 1772 * lowerbound to the TS echo check. 1773 */ 1774 struct timeval delta_ts; 1775 int ts_fudge; 1776 1777 /* 1778 * PFTM_TS_DIFF is how many seconds of leeway to allow 1779 * a host's timestamp. This can happen if the previous 1780 * packet got delayed in transit for much longer than 1781 * this packet. 1782 */ 1783 if ((ts_fudge = state->rule->timeout[PFTM_TS_DIFF]) == 0) 1784 ts_fudge = V_pf_default_rule.timeout[PFTM_TS_DIFF]; 1785 1786 /* Calculate max ticks since the last timestamp */ 1787 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */ 1788 #define TS_MICROSECS 1000000 /* microseconds per second */ 1789 delta_ts = uptime; 1790 timevalsub(&delta_ts, &src->scrub->pfss_last); 1791 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ; 1792 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ); 1793 1794 if ((src->state >= TCPS_ESTABLISHED && 1795 dst->state >= TCPS_ESTABLISHED) && 1796 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) || 1797 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) || 1798 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) || 1799 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) { 1800 /* Bad RFC1323 implementation or an insertion attack. 1801 * 1802 * - Solaris 2.6 and 2.7 are known to send another ACK 1803 * after the FIN,FIN|ACK,ACK closing that carries 1804 * an old timestamp. 1805 */ 1806 1807 DPFPRINTF(("Timestamp failed %c%c%c%c\n", 1808 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ', 1809 SEQ_GT(tsval, src->scrub->pfss_tsval + 1810 tsval_from_last) ? '1' : ' ', 1811 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ', 1812 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' ')); 1813 DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u " 1814 "idle: %jus %lums\n", 1815 tsval, tsecr, tsval_from_last, 1816 (uintmax_t)delta_ts.tv_sec, 1817 delta_ts.tv_usec / 1000)); 1818 DPFPRINTF((" src->tsval: %u tsecr: %u\n", 1819 src->scrub->pfss_tsval, src->scrub->pfss_tsecr)); 1820 DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u" 1821 "\n", dst->scrub->pfss_tsval, 1822 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0)); 1823 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1824 pf_print_state(state); 1825 pf_print_flags(tcp_get_flags(th)); 1826 printf("\n"); 1827 } 1828 REASON_SET(reason, PFRES_TS); 1829 return (PF_DROP); 1830 } 1831 1832 /* XXX I'd really like to require tsecr but it's optional */ 1833 1834 } else if (!got_ts && (tcp_get_flags(th) & TH_RST) == 0 && 1835 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED) 1836 || pd->p_len > 0 || (tcp_get_flags(th) & TH_SYN)) && 1837 src->scrub && dst->scrub && 1838 (src->scrub->pfss_flags & PFSS_PAWS) && 1839 (dst->scrub->pfss_flags & PFSS_PAWS)) { 1840 /* Didn't send a timestamp. Timestamps aren't really useful 1841 * when: 1842 * - connection opening or closing (often not even sent). 1843 * but we must not let an attacker to put a FIN on a 1844 * data packet to sneak it through our ESTABLISHED check. 1845 * - on a TCP reset. RFC suggests not even looking at TS. 1846 * - on an empty ACK. The TS will not be echoed so it will 1847 * probably not help keep the RTT calculation in sync and 1848 * there isn't as much danger when the sequence numbers 1849 * got wrapped. So some stacks don't include TS on empty 1850 * ACKs :-( 1851 * 1852 * To minimize the disruption to mostly RFC1323 conformant 1853 * stacks, we will only require timestamps on data packets. 1854 * 1855 * And what do ya know, we cannot require timestamps on data 1856 * packets. There appear to be devices that do legitimate 1857 * TCP connection hijacking. There are HTTP devices that allow 1858 * a 3whs (with timestamps) and then buffer the HTTP request. 1859 * If the intermediate device has the HTTP response cache, it 1860 * will spoof the response but not bother timestamping its 1861 * packets. So we can look for the presence of a timestamp in 1862 * the first data packet and if there, require it in all future 1863 * packets. 1864 */ 1865 1866 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) { 1867 /* 1868 * Hey! Someone tried to sneak a packet in. Or the 1869 * stack changed its RFC1323 behavior?!?! 1870 */ 1871 if (V_pf_status.debug >= PF_DEBUG_MISC) { 1872 DPFPRINTF(("Did not receive expected RFC1323 " 1873 "timestamp\n")); 1874 pf_print_state(state); 1875 pf_print_flags(tcp_get_flags(th)); 1876 printf("\n"); 1877 } 1878 REASON_SET(reason, PFRES_TS); 1879 return (PF_DROP); 1880 } 1881 } 1882 1883 /* 1884 * We will note if a host sends his data packets with or without 1885 * timestamps. And require all data packets to contain a timestamp 1886 * if the first does. PAWS implicitly requires that all data packets be 1887 * timestamped. But I think there are middle-man devices that hijack 1888 * TCP streams immediately after the 3whs and don't timestamp their 1889 * packets (seen in a WWW accelerator or cache). 1890 */ 1891 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags & 1892 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) { 1893 if (got_ts) 1894 src->scrub->pfss_flags |= PFSS_DATA_TS; 1895 else { 1896 src->scrub->pfss_flags |= PFSS_DATA_NOTS; 1897 if (V_pf_status.debug >= PF_DEBUG_MISC && dst->scrub && 1898 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) { 1899 /* Don't warn if other host rejected RFC1323 */ 1900 DPFPRINTF(("Broken RFC1323 stack did not " 1901 "timestamp data packet. Disabled PAWS " 1902 "security.\n")); 1903 pf_print_state(state); 1904 pf_print_flags(tcp_get_flags(th)); 1905 printf("\n"); 1906 } 1907 } 1908 } 1909 1910 /* 1911 * Update PAWS values 1912 */ 1913 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags & 1914 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) { 1915 getmicrouptime(&src->scrub->pfss_last); 1916 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) || 1917 (src->scrub->pfss_flags & PFSS_PAWS) == 0) 1918 src->scrub->pfss_tsval = tsval; 1919 1920 if (tsecr) { 1921 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) || 1922 (src->scrub->pfss_flags & PFSS_PAWS) == 0) 1923 src->scrub->pfss_tsecr = tsecr; 1924 1925 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 && 1926 (SEQ_LT(tsval, src->scrub->pfss_tsval0) || 1927 src->scrub->pfss_tsval0 == 0)) { 1928 /* tsval0 MUST be the lowest timestamp */ 1929 src->scrub->pfss_tsval0 = tsval; 1930 } 1931 1932 /* Only fully initialized after a TS gets echoed */ 1933 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0) 1934 src->scrub->pfss_flags |= PFSS_PAWS; 1935 } 1936 } 1937 1938 /* I have a dream.... TCP segment reassembly.... */ 1939 return (0); 1940 } 1941 1942 int 1943 pf_normalize_mss(struct pf_pdesc *pd) 1944 { 1945 struct tcphdr *th = &pd->hdr.tcp; 1946 u_int16_t *mss; 1947 int thoff; 1948 int opt, cnt, optlen = 0; 1949 u_char opts[TCP_MAXOLEN]; 1950 u_char *optp = opts; 1951 size_t startoff; 1952 1953 thoff = th->th_off << 2; 1954 cnt = thoff - sizeof(struct tcphdr); 1955 1956 if (cnt <= 0 || cnt > MAX_TCPOPTLEN || !pf_pull_hdr(pd->m, 1957 pd->off + sizeof(*th), opts, cnt, NULL, NULL, pd->af)) 1958 return (0); 1959 1960 for (; cnt > 0; cnt -= optlen, optp += optlen) { 1961 startoff = optp - opts; 1962 opt = optp[0]; 1963 if (opt == TCPOPT_EOL) 1964 break; 1965 if (opt == TCPOPT_NOP) 1966 optlen = 1; 1967 else { 1968 if (cnt < 2) 1969 break; 1970 optlen = optp[1]; 1971 if (optlen < 2 || optlen > cnt) 1972 break; 1973 } 1974 switch (opt) { 1975 case TCPOPT_MAXSEG: 1976 mss = (u_int16_t *)(optp + 2); 1977 if ((ntohs(*mss)) > pd->act.max_mss) { 1978 pf_patch_16(pd, 1979 mss, htons(pd->act.max_mss), 1980 PF_ALGNMNT(startoff)); 1981 m_copyback(pd->m, pd->off + sizeof(*th), 1982 thoff - sizeof(*th), opts); 1983 m_copyback(pd->m, pd->off, sizeof(*th), (caddr_t)th); 1984 } 1985 break; 1986 default: 1987 break; 1988 } 1989 } 1990 1991 return (0); 1992 } 1993 1994 int 1995 pf_scan_sctp(struct pf_pdesc *pd) 1996 { 1997 struct sctp_chunkhdr ch = { }; 1998 int chunk_off = sizeof(struct sctphdr); 1999 int chunk_start; 2000 int ret; 2001 2002 while (pd->off + chunk_off < pd->tot_len) { 2003 if (!pf_pull_hdr(pd->m, pd->off + chunk_off, &ch, sizeof(ch), NULL, 2004 NULL, pd->af)) 2005 return (PF_DROP); 2006 2007 /* Length includes the header, this must be at least 4. */ 2008 if (ntohs(ch.chunk_length) < 4) 2009 return (PF_DROP); 2010 2011 chunk_start = chunk_off; 2012 chunk_off += roundup(ntohs(ch.chunk_length), 4); 2013 2014 switch (ch.chunk_type) { 2015 case SCTP_INITIATION: 2016 case SCTP_INITIATION_ACK: { 2017 struct sctp_init_chunk init; 2018 2019 if (!pf_pull_hdr(pd->m, pd->off + chunk_start, &init, 2020 sizeof(init), NULL, NULL, pd->af)) 2021 return (PF_DROP); 2022 2023 /* 2024 * RFC 9620, Section 3.3.2, "The Initiate Tag is allowed to have 2025 * any value except 0." 2026 */ 2027 if (init.init.initiate_tag == 0) 2028 return (PF_DROP); 2029 if (init.init.num_inbound_streams == 0) 2030 return (PF_DROP); 2031 if (init.init.num_outbound_streams == 0) 2032 return (PF_DROP); 2033 if (ntohl(init.init.a_rwnd) < SCTP_MIN_RWND) 2034 return (PF_DROP); 2035 2036 /* 2037 * RFC 9260, Section 3.1, INIT chunks MUST have zero 2038 * verification tag. 2039 */ 2040 if (ch.chunk_type == SCTP_INITIATION && 2041 pd->hdr.sctp.v_tag != 0) 2042 return (PF_DROP); 2043 2044 pd->sctp_initiate_tag = init.init.initiate_tag; 2045 2046 if (ch.chunk_type == SCTP_INITIATION) 2047 pd->sctp_flags |= PFDESC_SCTP_INIT; 2048 else 2049 pd->sctp_flags |= PFDESC_SCTP_INIT_ACK; 2050 2051 ret = pf_multihome_scan_init(pd->off + chunk_start, 2052 ntohs(init.ch.chunk_length), pd); 2053 if (ret != PF_PASS) 2054 return (ret); 2055 2056 break; 2057 } 2058 case SCTP_ABORT_ASSOCIATION: 2059 pd->sctp_flags |= PFDESC_SCTP_ABORT; 2060 break; 2061 case SCTP_SHUTDOWN: 2062 case SCTP_SHUTDOWN_ACK: 2063 pd->sctp_flags |= PFDESC_SCTP_SHUTDOWN; 2064 break; 2065 case SCTP_SHUTDOWN_COMPLETE: 2066 pd->sctp_flags |= PFDESC_SCTP_SHUTDOWN_COMPLETE; 2067 break; 2068 case SCTP_COOKIE_ECHO: 2069 pd->sctp_flags |= PFDESC_SCTP_COOKIE; 2070 break; 2071 case SCTP_COOKIE_ACK: 2072 pd->sctp_flags |= PFDESC_SCTP_COOKIE_ACK; 2073 break; 2074 case SCTP_DATA: 2075 pd->sctp_flags |= PFDESC_SCTP_DATA; 2076 break; 2077 case SCTP_HEARTBEAT_REQUEST: 2078 pd->sctp_flags |= PFDESC_SCTP_HEARTBEAT; 2079 break; 2080 case SCTP_HEARTBEAT_ACK: 2081 pd->sctp_flags |= PFDESC_SCTP_HEARTBEAT_ACK; 2082 break; 2083 case SCTP_ASCONF: 2084 pd->sctp_flags |= PFDESC_SCTP_ASCONF; 2085 2086 ret = pf_multihome_scan_asconf(pd->off + chunk_start, 2087 ntohs(ch.chunk_length), pd); 2088 if (ret != PF_PASS) 2089 return (ret); 2090 break; 2091 default: 2092 pd->sctp_flags |= PFDESC_SCTP_OTHER; 2093 break; 2094 } 2095 } 2096 2097 /* Validate chunk lengths vs. packet length. */ 2098 if (pd->off + chunk_off != pd->tot_len) 2099 return (PF_DROP); 2100 2101 /* 2102 * INIT, INIT_ACK or SHUTDOWN_COMPLETE chunks must always be the only 2103 * one in a packet. 2104 */ 2105 if ((pd->sctp_flags & PFDESC_SCTP_INIT) && 2106 (pd->sctp_flags & ~PFDESC_SCTP_INIT)) 2107 return (PF_DROP); 2108 if ((pd->sctp_flags & PFDESC_SCTP_INIT_ACK) && 2109 (pd->sctp_flags & ~PFDESC_SCTP_INIT_ACK)) 2110 return (PF_DROP); 2111 if ((pd->sctp_flags & PFDESC_SCTP_SHUTDOWN_COMPLETE) && 2112 (pd->sctp_flags & ~PFDESC_SCTP_SHUTDOWN_COMPLETE)) 2113 return (PF_DROP); 2114 if ((pd->sctp_flags & PFDESC_SCTP_ABORT) && 2115 (pd->sctp_flags & PFDESC_SCTP_DATA)) { 2116 /* 2117 * RFC4960 3.3.7: DATA chunks MUST NOT be 2118 * bundled with ABORT. 2119 */ 2120 return (PF_DROP); 2121 } 2122 2123 return (PF_PASS); 2124 } 2125 2126 int 2127 pf_normalize_sctp(struct pf_pdesc *pd) 2128 { 2129 struct pf_krule *r, *rm = NULL; 2130 struct sctphdr *sh = &pd->hdr.sctp; 2131 u_short reason; 2132 sa_family_t af = pd->af; 2133 int srs; 2134 2135 PF_RULES_RASSERT(); 2136 2137 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); 2138 /* Check if there any scrub rules. Lack of scrub rules means enforced 2139 * packet normalization operation just like in OpenBSD. */ 2140 srs = (r != NULL); 2141 while (r != NULL) { 2142 pf_counter_u64_add(&r->evaluations, 1); 2143 if (pfi_kkif_match(r->kif, pd->kif) == r->ifnot) 2144 r = r->skip[PF_SKIP_IFP]; 2145 else if (r->direction && r->direction != pd->dir) 2146 r = r->skip[PF_SKIP_DIR]; 2147 else if (r->af && r->af != af) 2148 r = r->skip[PF_SKIP_AF]; 2149 else if (r->proto && r->proto != pd->proto) 2150 r = r->skip[PF_SKIP_PROTO]; 2151 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 2152 r->src.neg, pd->kif, M_GETFIB(pd->m))) 2153 r = r->skip[PF_SKIP_SRC_ADDR]; 2154 else if (r->src.port_op && !pf_match_port(r->src.port_op, 2155 r->src.port[0], r->src.port[1], sh->src_port)) 2156 r = r->skip[PF_SKIP_SRC_PORT]; 2157 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 2158 r->dst.neg, NULL, M_GETFIB(pd->m))) 2159 r = r->skip[PF_SKIP_DST_ADDR]; 2160 else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 2161 r->dst.port[0], r->dst.port[1], sh->dest_port)) 2162 r = r->skip[PF_SKIP_DST_PORT]; 2163 else { 2164 rm = r; 2165 break; 2166 } 2167 } 2168 2169 if (srs) { 2170 /* With scrub rules present SCTP normalization happens only 2171 * if one of rules has matched and it's not a "no scrub" rule */ 2172 if (rm == NULL || rm->action == PF_NOSCRUB) 2173 return (PF_PASS); 2174 2175 pf_counter_u64_critical_enter(); 2176 pf_counter_u64_add_protected(&r->packets[pd->dir == PF_OUT], 1); 2177 pf_counter_u64_add_protected(&r->bytes[pd->dir == PF_OUT], pd->tot_len); 2178 pf_counter_u64_critical_exit(); 2179 } 2180 2181 /* Verify we're a multiple of 4 bytes long */ 2182 if ((pd->tot_len - pd->off - sizeof(struct sctphdr)) % 4) 2183 goto sctp_drop; 2184 2185 /* INIT chunk needs to be the only chunk */ 2186 if (pd->sctp_flags & PFDESC_SCTP_INIT) 2187 if (pd->sctp_flags & ~PFDESC_SCTP_INIT) 2188 goto sctp_drop; 2189 2190 return (PF_PASS); 2191 2192 sctp_drop: 2193 REASON_SET(&reason, PFRES_NORM); 2194 if (rm != NULL && r->log) 2195 PFLOG_PACKET(PF_DROP, reason, r, NULL, NULL, pd, 2196 1, NULL); 2197 2198 return (PF_DROP); 2199 } 2200 2201 #if defined(INET) || defined(INET6) 2202 void 2203 pf_scrub(struct pf_pdesc *pd) 2204 { 2205 2206 struct ip *h = mtod(pd->m, struct ip *); 2207 #ifdef INET6 2208 struct ip6_hdr *h6 = mtod(pd->m, struct ip6_hdr *); 2209 #endif /* INET6 */ 2210 2211 /* Clear IP_DF if no-df was requested */ 2212 if (pd->af == AF_INET && pd->act.flags & PFSTATE_NODF && 2213 h->ip_off & htons(IP_DF)) 2214 { 2215 u_int16_t ip_off = h->ip_off; 2216 2217 h->ip_off &= htons(~IP_DF); 2218 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_off, h->ip_off, 0); 2219 } 2220 2221 /* Enforce a minimum ttl, may cause endless packet loops */ 2222 if (pd->af == AF_INET && pd->act.min_ttl && 2223 h->ip_ttl < pd->act.min_ttl) { 2224 u_int16_t ip_ttl = h->ip_ttl; 2225 2226 h->ip_ttl = pd->act.min_ttl; 2227 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0); 2228 } 2229 #ifdef INET6 2230 /* Enforce a minimum ttl, may cause endless packet loops */ 2231 if (pd->af == AF_INET6 && pd->act.min_ttl && 2232 h6->ip6_hlim < pd->act.min_ttl) 2233 h6->ip6_hlim = pd->act.min_ttl; 2234 #endif /* INET6 */ 2235 /* Enforce tos */ 2236 if (pd->act.flags & PFSTATE_SETTOS) { 2237 switch (pd->af) { 2238 case AF_INET: { 2239 u_int16_t ov, nv; 2240 2241 ov = *(u_int16_t *)h; 2242 h->ip_tos = pd->act.set_tos | (h->ip_tos & IPTOS_ECN_MASK); 2243 nv = *(u_int16_t *)h; 2244 2245 h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0); 2246 break; 2247 } 2248 #ifdef INET6 2249 case AF_INET6: 2250 h6->ip6_flow &= IPV6_FLOWLABEL_MASK | IPV6_VERSION_MASK; 2251 h6->ip6_flow |= htonl((pd->act.set_tos | IPV6_ECN(h6)) << 20); 2252 break; 2253 #endif /* INET6 */ 2254 } 2255 } 2256 2257 /* random-id, but not for fragments */ 2258 #ifdef INET 2259 if (pd->af == AF_INET && 2260 pd->act.flags & PFSTATE_RANDOMID && !(h->ip_off & ~htons(IP_DF))) { 2261 uint16_t ip_id = h->ip_id; 2262 2263 ip_fillid(h, V_ip_random_id); 2264 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0); 2265 } 2266 #endif /* INET */ 2267 } 2268 #endif /* INET || INET6 */ 2269