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