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