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