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