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