ip_state.c revision 113799
1/* 2 * Copyright (C) 1995-2002 by Darren Reed. 3 * 4 * See the IPFILTER.LICENCE file for details on licencing. 5 */ 6 7#if defined(__sgi) && (IRIX > 602) 8# include <sys/ptimers.h> 9#endif 10#include <sys/errno.h> 11#include <sys/types.h> 12#include <sys/param.h> 13#include <sys/file.h> 14#if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \ 15 defined(_KERNEL) 16# include "opt_ipfilter_log.h" 17#endif 18#if defined(_KERNEL) && defined(__FreeBSD_version) && \ 19 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE) 20#include "opt_inet6.h" 21#endif 22#if !defined(_KERNEL) && !defined(KERNEL) && !defined(__KERNEL__) 23# include <stdio.h> 24# include <stdlib.h> 25# include <string.h> 26#else 27# ifdef linux 28# include <linux/kernel.h> 29# include <linux/module.h> 30# endif 31#endif 32#if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000) 33# include <sys/filio.h> 34# include <sys/fcntl.h> 35# if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM) 36# include "opt_ipfilter.h" 37# endif 38#else 39# include <sys/ioctl.h> 40#endif 41#include <sys/time.h> 42#ifndef linux 43# include <sys/protosw.h> 44#endif 45#include <sys/socket.h> 46#if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux) 47# include <sys/systm.h> 48#endif 49#if !defined(__SVR4) && !defined(__svr4__) 50# ifndef linux 51# include <sys/mbuf.h> 52# endif 53#else 54# include <sys/filio.h> 55# include <sys/byteorder.h> 56# ifdef _KERNEL 57# include <sys/dditypes.h> 58# endif 59# include <sys/stream.h> 60# include <sys/kmem.h> 61#endif 62 63#include <net/if.h> 64#ifdef sun 65# include <net/af.h> 66#endif 67#include <net/route.h> 68#include <netinet/in.h> 69#include <netinet/in_systm.h> 70#include <netinet/ip.h> 71#include <netinet/tcp.h> 72#ifndef linux 73# include <netinet/ip_var.h> 74# include <netinet/tcp_fsm.h> 75#endif 76#include <netinet/udp.h> 77#include <netinet/ip_icmp.h> 78#include "netinet/ip_compat.h" 79#include <netinet/tcpip.h> 80#include "netinet/ip_fil.h" 81#include "netinet/ip_nat.h" 82#include "netinet/ip_frag.h" 83#include "netinet/ip_state.h" 84#ifdef USE_INET6 85#include <netinet/icmp6.h> 86#endif 87#if (__FreeBSD_version >= 300000) 88# include <sys/malloc.h> 89# if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM) 90# include <sys/libkern.h> 91# include <sys/systm.h> 92# endif 93#endif 94 95#if !defined(lint) 96static const char sccsid[] = "@(#)ip_state.c 1.8 6/5/96 (C) 1993-2000 Darren Reed"; 97/* static const char rcsid[] = "@(#)$Id: ip_state.c,v 2.30.2.38 2001/07/23 13:49:46 darrenr Exp $"; */ 98static const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/ip_state.c 113799 2003-04-21 16:27:46Z obrien $"; 99#endif 100 101#ifndef MIN 102# define MIN(a,b) (((a)<(b))?(a):(b)) 103#endif 104 105#define TCP_CLOSE (TH_FIN|TH_RST) 106 107static ipstate_t **ips_table = NULL; 108static int ips_num = 0; 109static int ips_wild = 0; 110static ips_stat_t ips_stats; 111#if (SOLARIS || defined(__sgi)) && defined(_KERNEL) 112extern KRWLOCK_T ipf_state, ipf_mutex; 113extern kmutex_t ipf_rw; 114#endif 115 116#ifdef USE_INET6 117static frentry_t *fr_checkicmp6matchingstate __P((ip6_t *, fr_info_t *)); 118#endif 119static int fr_matchsrcdst __P((ipstate_t *, union i6addr, union i6addr, 120 fr_info_t *, tcphdr_t *)); 121static frentry_t *fr_checkicmpmatchingstate __P((ip_t *, fr_info_t *)); 122static int fr_matchicmpqueryreply __P((int, ipstate_t *, icmphdr_t *, int)); 123static int fr_state_flush __P((int, int)); 124static ips_stat_t *fr_statetstats __P((void)); 125static void fr_delstate __P((ipstate_t *)); 126static int fr_state_remove __P((caddr_t)); 127static void fr_ipsmove __P((ipstate_t **, ipstate_t *, u_int)); 128static int fr_tcpoptions __P((tcphdr_t *)); 129int fr_stputent __P((caddr_t)); 130int fr_stgetent __P((caddr_t)); 131void fr_stinsert __P((ipstate_t *)); 132 133 134#define FIVE_DAYS (2 * 5 * 86400) /* 5 days: half closed session */ 135 136#define TCP_MSL 240 /* 2 minutes */ 137u_long fr_tcpidletimeout = FIVE_DAYS, 138 fr_tcpclosewait = 2 * TCP_MSL, 139 fr_tcplastack = 2 * TCP_MSL, 140 fr_tcptimeout = 2 * TCP_MSL, 141 fr_tcpclosed = 120, 142 fr_tcphalfclosed = 2 * 2 * 3600, /* 2 hours */ 143 fr_udptimeout = 240, 144 fr_udpacktimeout = 24, 145 fr_icmptimeout = 120, 146 fr_icmpacktimeout = 12; 147int fr_statemax = IPSTATE_MAX, 148 fr_statesize = IPSTATE_SIZE; 149int fr_state_doflush = 0, 150 fr_state_lock = 0; 151ipstate_t *ips_list = NULL; 152 153static int icmpreplytype4[ICMP_MAXTYPE + 1]; 154#ifdef USE_INET6 155static int icmpreplytype6[ICMP6_MAXTYPE + 1]; 156#endif 157 158int fr_stateinit() 159{ 160 int i; 161 162 KMALLOCS(ips_table, ipstate_t **, fr_statesize * sizeof(ipstate_t *)); 163 if (ips_table != NULL) 164 bzero((char *)ips_table, fr_statesize * sizeof(ipstate_t *)); 165 else 166 return -1; 167 168 /* fill icmp reply type table */ 169 for (i = 0; i <= ICMP_MAXTYPE; i++) 170 icmpreplytype4[i] = -1; 171 icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY; 172 icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY; 173 icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY; 174 icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY; 175#ifdef USE_INET6 176 /* fill icmp reply type table */ 177 for (i = 0; i <= ICMP6_MAXTYPE; i++) 178 icmpreplytype6[i] = -1; 179 icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY; 180 icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT; 181 icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY; 182 icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT; 183 icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT; 184#endif 185 186 return 0; 187} 188 189 190static ips_stat_t *fr_statetstats() 191{ 192 ips_stats.iss_active = ips_num; 193 ips_stats.iss_table = ips_table; 194 ips_stats.iss_list = ips_list; 195 return &ips_stats; 196} 197 198 199/* 200 * flush state tables. two actions currently defined: 201 * which == 0 : flush all state table entries 202 * which == 1 : flush TCP connections which have started to close but are 203 * stuck for some reason. 204 * which == 2 : flush TCP connections which have been idle for a long time, 205 * starting at > 4 days idle and working back in successive half- 206 * days to at most 12 hours old. 207 */ 208static int fr_state_flush(which, proto) 209int which, proto; 210{ 211 ipstate_t *is, **isp; 212#if defined(_KERNEL) && !SOLARIS 213 int s; 214#endif 215 int delete, removed = 0, try; 216 217 SPL_NET(s); 218 for (isp = &ips_list; (is = *isp); ) { 219 delete = 0; 220 221 if ((proto != 0) && (is->is_v != proto)) 222 continue; 223 224 switch (which) 225 { 226 case 0 : 227 delete = 1; 228 break; 229 case 1 : 230 case 2 : 231 if (is->is_p != IPPROTO_TCP) 232 break; 233 if ((is->is_state[0] != TCPS_ESTABLISHED) || 234 (is->is_state[1] != TCPS_ESTABLISHED)) 235 delete = 1; 236 break; 237 } 238 239 if (delete) { 240 if (is->is_p == IPPROTO_TCP) 241 ips_stats.iss_fin++; 242 else 243 ips_stats.iss_expire++; 244#ifdef IPFILTER_LOG 245 ipstate_log(is, ISL_FLUSH); 246#endif 247 fr_delstate(is); 248 removed++; 249 } else 250 isp = &is->is_next; 251 } 252 253 /* 254 * Asked to remove inactive entries, try again if first attempt 255 * failed. In this case, 86400 is half a day because the counter is 256 * activated every half second. 257 */ 258 if ((which == 2) && (removed == 0)) { 259 try = 86400; /* half a day */ 260 for (; (try < FIVE_DAYS) && (removed == 0); try += 86400) { 261 for (isp = &ips_list; (is = *isp); ) { 262 delete = 0; 263 if ((is->is_p == IPPROTO_TCP) && 264 ((is->is_state[0] == TCPS_ESTABLISHED) || 265 (is->is_state[1] == TCPS_ESTABLISHED)) && 266 (is->is_age < try)) { 267 ips_stats.iss_fin++; 268 delete = 1; 269 } else if ((is->is_p != IPPROTO_TCP) && 270 (is->is_pkts > 1)) { 271 ips_stats.iss_expire++; 272 delete = 1; 273 } 274 if (delete) { 275#ifdef IPFILTER_LOG 276 ipstate_log(is, ISL_FLUSH); 277#endif 278 fr_delstate(is); 279 removed++; 280 } else 281 isp = &is->is_next; 282 } 283 } 284 } 285 286 SPL_X(s); 287 return removed; 288} 289 290 291static int fr_state_remove(data) 292caddr_t data; 293{ 294 ipstate_t *sp, st; 295 int error; 296 297 sp = &st; 298 error = IRCOPYPTR(data, (caddr_t)&st, sizeof(st)); 299 if (error) 300 return EFAULT; 301 302 WRITE_ENTER(&ipf_state); 303 for (sp = ips_list; sp; sp = sp->is_next) 304 if ((sp->is_p == st.is_p) && (sp->is_v == st.is_v) && 305 !bcmp((char *)&sp->is_src, (char *)&st.is_src, 306 sizeof(st.is_src)) && 307 !bcmp((char *)&sp->is_dst, (char *)&st.is_dst, 308 sizeof(st.is_dst)) && 309 !bcmp((char *)&sp->is_ps, (char *)&st.is_ps, 310 sizeof(st.is_ps))) { 311#ifdef IPFILTER_LOG 312 ipstate_log(sp, ISL_REMOVE); 313#endif 314 fr_delstate(sp); 315 RWLOCK_EXIT(&ipf_state); 316 return 0; 317 } 318 RWLOCK_EXIT(&ipf_state); 319 return ESRCH; 320} 321 322 323int fr_state_ioctl(data, cmd, mode) 324caddr_t data; 325#if defined(__NetBSD__) || defined(__OpenBSD__) 326u_long cmd; 327#else 328int cmd; 329#endif 330int mode; 331{ 332 int arg, ret, error = 0; 333 334 switch (cmd) 335 { 336 case SIOCDELST : 337 error = fr_state_remove(data); 338 break; 339 case SIOCIPFFL : 340 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg)); 341 if (error) 342 break; 343 if (arg == 0 || arg == 1) { 344 WRITE_ENTER(&ipf_state); 345 ret = fr_state_flush(arg, 4); 346 RWLOCK_EXIT(&ipf_state); 347 error = IWCOPY((caddr_t)&ret, data, sizeof(ret)); 348 } else 349 error = EINVAL; 350 break; 351#ifdef USE_INET6 352 case SIOCIPFL6 : 353 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg)); 354 if (error) 355 break; 356 if (arg == 0 || arg == 1) { 357 WRITE_ENTER(&ipf_state); 358 ret = fr_state_flush(arg, 6); 359 RWLOCK_EXIT(&ipf_state); 360 error = IWCOPY((caddr_t)&ret, data, sizeof(ret)); 361 } else 362 error = EINVAL; 363 break; 364#endif 365#ifdef IPFILTER_LOG 366 case SIOCIPFFB : 367 if (!(mode & FWRITE)) 368 error = EPERM; 369 else { 370 int tmp; 371 372 tmp = ipflog_clear(IPL_LOGSTATE); 373 IWCOPY((char *)&tmp, data, sizeof(tmp)); 374 } 375 break; 376#endif 377 case SIOCGETFS : 378 error = IWCOPYPTR((caddr_t)fr_statetstats(), data, 379 sizeof(ips_stat_t)); 380 break; 381 case FIONREAD : 382#ifdef IPFILTER_LOG 383 arg = (int)iplused[IPL_LOGSTATE]; 384 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg)); 385#endif 386 break; 387 case SIOCSTLCK : 388 error = fr_lock(data, &fr_state_lock); 389 break; 390 case SIOCSTPUT : 391 if (!fr_state_lock) { 392 error = EACCES; 393 break; 394 } 395 error = fr_stputent(data); 396 break; 397 case SIOCSTGET : 398 if (!fr_state_lock) { 399 error = EACCES; 400 break; 401 } 402 error = fr_stgetent(data); 403 break; 404 default : 405 error = EINVAL; 406 break; 407 } 408 return error; 409} 410 411 412/* 413 * Copy out state information from the kernel to a user space process. 414 */ 415int fr_stgetent(data) 416caddr_t data; 417{ 418 register ipstate_t *is, *isn; 419 ipstate_save_t ips; 420 int error; 421 422 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips)); 423 if (error) 424 return error; 425 426 isn = ips.ips_next; 427 if (!isn) { 428 isn = ips_list; 429 if (isn == NULL) { 430 if (ips.ips_next == NULL) 431 return ENOENT; 432 return 0; 433 } 434 } else { 435 /* 436 * Make sure the pointer we're copying from exists in the 437 * current list of entries. Security precaution to prevent 438 * copying of random kernel data. 439 */ 440 for (is = ips_list; is; is = is->is_next) 441 if (is == isn) 442 break; 443 if (!is) 444 return ESRCH; 445 } 446 ips.ips_next = isn->is_next; 447 bcopy((char *)isn, (char *)&ips.ips_is, sizeof(ips.ips_is)); 448 if (isn->is_rule) 449 bcopy((char *)isn->is_rule, (char *)&ips.ips_fr, 450 sizeof(ips.ips_fr)); 451 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips)); 452 if (error) 453 error = EFAULT; 454 return error; 455} 456 457 458int fr_stputent(data) 459caddr_t data; 460{ 461 register ipstate_t *is, *isn; 462 ipstate_save_t ips; 463 int error, out, i; 464 frentry_t *fr; 465 char *name; 466 467 error = IRCOPYPTR(data, (caddr_t)&ips, sizeof(ips)); 468 if (error) 469 return error; 470 471 KMALLOC(isn, ipstate_t *); 472 if (isn == NULL) 473 return ENOMEM; 474 475 bcopy((char *)&ips.ips_is, (char *)isn, sizeof(*isn)); 476 fr = isn->is_rule; 477 if (fr != NULL) { 478 if (isn->is_flags & FI_NEWFR) { 479 KMALLOC(fr, frentry_t *); 480 if (fr == NULL) { 481 KFREE(isn); 482 return ENOMEM; 483 } 484 bcopy((char *)&ips.ips_fr, (char *)fr, sizeof(*fr)); 485 out = fr->fr_flags & FR_OUTQUE ? 1 : 0; 486 isn->is_rule = fr; 487 ips.ips_is.is_rule = fr; 488 489 /* 490 * Look up all the interface names in the rule. 491 */ 492 for (i = 0; i < 4; i++) { 493 name = fr->fr_ifnames[i]; 494 if ((name[1] == '\0') && 495 ((name[0] == '-') || (name[0] == '*'))) { 496 fr->fr_ifas[i] = NULL; 497 } else if (*name != '\0') { 498 fr->fr_ifas[i] = GETUNIT(name, 499 fr->fr_v); 500 if (fr->fr_ifas[i] == NULL) 501 fr->fr_ifas[i] = (void *)-1; 502 else { 503 strncpy(isn->is_ifname[i], 504 IFNAME(fr->fr_ifas[i]), 505 IFNAMSIZ); 506 } 507 } 508 isn->is_ifp[out] = fr->fr_ifas[i]; 509 } 510 511 /* 512 * send a copy back to userland of what we ended up 513 * to allow for verification. 514 */ 515 error = IWCOPYPTR((caddr_t)&ips, data, sizeof(ips)); 516 if (error) { 517 KFREE(isn); 518 KFREE(fr); 519 return EFAULT; 520 } 521 } else { 522 for (is = ips_list; is; is = is->is_next) 523 if (is->is_rule == fr) 524 break; 525 if (!is) { 526 KFREE(isn); 527 return ESRCH; 528 } 529 } 530 } 531 fr_stinsert(isn); 532 return 0; 533} 534 535 536/* 537 * Insert a state table entry manually. 538 */ 539void fr_stinsert(is) 540register ipstate_t *is; 541{ 542 register u_int hv = is->is_hv; 543 char *name; 544 int i; 545 546 MUTEX_INIT(&is->is_lock, "ipf state entry", NULL); 547 548 /* 549 * Look up all the interface names in the state entry. 550 */ 551 for (i = 0; i < 4; i++) { 552 name = is->is_ifname[i]; 553 if ((name[1] == '\0') && 554 ((name[0] == '-') || (name[0] == '*'))) { 555 is->is_ifp[0] = NULL; 556 } else if (*name != '\0') { 557 is->is_ifp[i] = GETUNIT(name, is->is_v); 558 if (is->is_ifp[i] == NULL) 559 is->is_ifp[i] = (void *)-1; 560 } 561 } 562 563 564 /* 565 * add into list table. 566 */ 567 if (ips_list) 568 ips_list->is_pnext = &is->is_next; 569 is->is_pnext = &ips_list; 570 is->is_next = ips_list; 571 ips_list = is; 572 if (ips_table[hv]) 573 ips_table[hv]->is_phnext = &is->is_hnext; 574 else 575 ips_stats.iss_inuse++; 576 is->is_phnext = ips_table + hv; 577 is->is_hnext = ips_table[hv]; 578 ips_table[hv] = is; 579 ips_num++; 580} 581 582 583/* 584 * Create a new ipstate structure and hang it off the hash table. 585 */ 586ipstate_t *fr_addstate(ip, fin, stsave, flags) 587ip_t *ip; 588fr_info_t *fin; 589ipstate_t **stsave; 590u_int flags; 591{ 592 register tcphdr_t *tcp = NULL; 593 register ipstate_t *is; 594 register u_int hv; 595 struct icmp *ic; 596 ipstate_t ips; 597 int out, ws; 598 u_int pass; 599 void *ifp; 600 601 if (fr_state_lock || (fin->fin_off != 0) || (fin->fin_fl & FI_SHORT) || 602 (fin->fin_misc & FM_BADSTATE)) 603 return NULL; 604 if (ips_num == fr_statemax) { 605 ips_stats.iss_max++; 606 fr_state_doflush = 1; 607 return NULL; 608 } 609 out = fin->fin_out; 610 is = &ips; 611 bzero((char *)is, sizeof(*is)); 612 ips.is_age = 1; 613 /* 614 * Copy and calculate... 615 */ 616 hv = (is->is_p = fin->fin_fi.fi_p); 617 is->is_src = fin->fin_fi.fi_src; 618 hv += is->is_saddr; 619 is->is_dst = fin->fin_fi.fi_dst; 620 hv += is->is_daddr; 621#ifdef USE_INET6 622 if (fin->fin_v == 6) { 623 if ((is->is_p == IPPROTO_ICMPV6) && 624 IN6_IS_ADDR_MULTICAST(&is->is_dst.in6)) { 625 /* 626 * So you can do keep state with neighbour discovery. 627 */ 628 flags |= FI_W_DADDR; 629 hv -= is->is_daddr; 630 } else { 631 hv += is->is_dst.i6[1]; 632 hv += is->is_dst.i6[2]; 633 hv += is->is_dst.i6[3]; 634 } 635 hv += is->is_src.i6[1]; 636 hv += is->is_src.i6[2]; 637 hv += is->is_src.i6[3]; 638 } 639#endif 640 641 switch (is->is_p) 642 { 643 int off; 644 645#ifdef USE_INET6 646 case IPPROTO_ICMPV6 : 647 ic = (struct icmp *)fin->fin_dp; 648 if ((ic->icmp_type & ICMP6_INFOMSG_MASK) == 0) 649 return NULL; 650 651 switch (ic->icmp_type) 652 { 653 case ICMP6_ECHO_REQUEST : 654 is->is_icmp.ics_type = ic->icmp_type; 655 hv += (is->is_icmp.ics_id = ic->icmp_id); 656 hv += (is->is_icmp.ics_seq = ic->icmp_seq); 657 break; 658 case ICMP6_MEMBERSHIP_QUERY : 659 case ND_ROUTER_SOLICIT : 660 case ND_NEIGHBOR_SOLICIT : 661 case ICMP6_NI_QUERY : 662 is->is_icmp.ics_type = ic->icmp_type; 663 break; 664 default : 665 return NULL; 666 } 667 ATOMIC_INCL(ips_stats.iss_icmp); 668 is->is_age = fr_icmptimeout; 669 break; 670#endif 671 case IPPROTO_ICMP : 672 ic = (struct icmp *)fin->fin_dp; 673 674 switch (ic->icmp_type) 675 { 676 case ICMP_ECHO : 677 case ICMP_TSTAMP : 678 case ICMP_IREQ : 679 case ICMP_MASKREQ : 680 is->is_icmp.ics_type = ic->icmp_type; 681 hv += (is->is_icmp.ics_id = ic->icmp_id); 682 hv += (is->is_icmp.ics_seq = ic->icmp_seq); 683 break; 684 default : 685 return NULL; 686 } 687 ATOMIC_INCL(ips_stats.iss_icmp); 688 is->is_age = fr_icmptimeout; 689 break; 690 case IPPROTO_TCP : 691 tcp = (tcphdr_t *)fin->fin_dp; 692 693 if (tcp->th_flags & TH_RST) 694 return NULL; 695 /* 696 * The endian of the ports doesn't matter, but the ack and 697 * sequence numbers do as we do mathematics on them later. 698 */ 699 is->is_sport = htons(fin->fin_data[0]); 700 is->is_dport = htons(fin->fin_data[1]); 701 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) { 702 hv += is->is_sport; 703 hv += is->is_dport; 704 } 705 if ((flags & FI_IGNOREPKT) == 0) { 706 is->is_send = ntohl(tcp->th_seq) + fin->fin_dlen - 707 (off = (tcp->th_off << 2)) + 708 ((tcp->th_flags & TH_SYN) ? 1 : 0) + 709 ((tcp->th_flags & TH_FIN) ? 1 : 0); 710 is->is_maxsend = is->is_send; 711 712 if ((tcp->th_flags & TH_SYN) && 713 ((tcp->th_off << 2) >= (sizeof(*tcp) + 4))) { 714 ws = fr_tcpoptions(tcp); 715 if (ws >= 0) 716 is->is_swscale = ws; 717 } 718 } 719 720 is->is_maxdwin = 1; 721 is->is_maxswin = ntohs(tcp->th_win); 722 if (is->is_maxswin == 0) 723 is->is_maxswin = 1; 724 725 if ((tcp->th_flags & TH_OPENING) == TH_SYN) 726 is->is_fsm = 1; 727 728 /* 729 * If we're creating state for a starting connection, start the 730 * timer on it as we'll never see an error if it fails to 731 * connect. 732 */ 733 ATOMIC_INCL(ips_stats.iss_tcp); 734 break; 735 736 case IPPROTO_UDP : 737 tcp = (tcphdr_t *)fin->fin_dp; 738 739 is->is_sport = htons(fin->fin_data[0]); 740 is->is_dport = htons(fin->fin_data[1]); 741 if ((flags & (FI_W_DPORT|FI_W_SPORT)) == 0) { 742 hv += is->is_sport; 743 hv += is->is_dport; 744 } 745 ATOMIC_INCL(ips_stats.iss_udp); 746 is->is_age = fr_udptimeout; 747 break; 748 default : 749 is->is_age = fr_udptimeout; 750 break; 751 } 752 753 KMALLOC(is, ipstate_t *); 754 if (is == NULL) { 755 ATOMIC_INCL(ips_stats.iss_nomem); 756 return NULL; 757 } 758 bcopy((char *)&ips, (char *)is, sizeof(*is)); 759 hv %= fr_statesize; 760 is->is_hv = hv; 761 is->is_rule = fin->fin_fr; 762 if (is->is_rule != NULL) { 763 is->is_group = is->is_rule->fr_group; 764 ATOMIC_INC32(is->is_rule->fr_ref); 765 pass = is->is_rule->fr_flags; 766 is->is_frage[0] = is->is_rule->fr_age[0]; 767 is->is_frage[1] = is->is_rule->fr_age[1]; 768 if (is->is_frage[0] != 0) 769 is->is_age = is->is_frage[0]; 770 771 is->is_ifp[(out << 1) + 1] = is->is_rule->fr_ifas[1]; 772 is->is_ifp[(1 - out) << 1] = is->is_rule->fr_ifas[2]; 773 is->is_ifp[((1 - out) << 1) + 1] = is->is_rule->fr_ifas[3]; 774 775 if (((ifp = is->is_rule->fr_ifas[1]) != NULL) && 776 (ifp != (void *)-1)) 777 strncpy(is->is_ifname[(out << 1) + 1], 778 IFNAME(ifp), IFNAMSIZ); 779 if (((ifp = is->is_rule->fr_ifas[2]) != NULL) && 780 (ifp != (void *)-1)) 781 strncpy(is->is_ifname[(1 - out) << 1], 782 IFNAME(ifp), IFNAMSIZ); 783 if (((ifp = is->is_rule->fr_ifas[3]) != NULL) && 784 (ifp != (void *)-1)) 785 strncpy(is->is_ifname[((1 - out) << 1) + 1], 786 IFNAME(ifp), IFNAMSIZ); 787 } else 788 pass = fr_flags; 789 790 is->is_ifp[out << 1] = fin->fin_ifp; 791 strncpy(is->is_ifname[out << 1], IFNAME(fin->fin_ifp), IFNAMSIZ); 792 793 WRITE_ENTER(&ipf_state); 794 795 is->is_pass = pass; 796 if ((flags & FI_IGNOREPKT) == 0) { 797 is->is_pkts = 1; 798 is->is_bytes = fin->fin_dlen + fin->fin_hlen; 799 } 800 /* 801 * We want to check everything that is a property of this packet, 802 * but we don't (automatically) care about it's fragment status as 803 * this may change. 804 */ 805 is->is_v = fin->fin_v; 806 is->is_rulen = fin->fin_rule; 807 is->is_opt = fin->fin_fi.fi_optmsk; 808 is->is_optmsk = 0xffffffff; 809 is->is_sec = fin->fin_fi.fi_secmsk; 810 is->is_secmsk = 0xffff; 811 is->is_auth = fin->fin_fi.fi_auth; 812 is->is_authmsk = 0xffff; 813 is->is_flags = fin->fin_fl & FI_CMP; 814 is->is_flags |= FI_CMP << 4; 815 is->is_flags |= flags & (FI_WILDP|FI_WILDA); 816 if (flags & (FI_WILDP|FI_WILDA)) 817 ips_wild++; 818 819 if (pass & FR_LOGFIRST) 820 is->is_pass &= ~(FR_LOGFIRST|FR_LOG); 821 fr_stinsert(is); 822 is->is_me = stsave; 823 if (is->is_p == IPPROTO_TCP) { 824 fr_tcp_age(&is->is_age, is->is_state, fin, 825 0, is->is_fsm); /* 0 = packet from the source */ 826 } 827#ifdef IPFILTER_LOG 828 ipstate_log(is, ISL_NEW); 829#endif 830 RWLOCK_EXIT(&ipf_state); 831 fin->fin_rev = IP6NEQ(is->is_dst, fin->fin_fi.fi_dst); 832 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG)) 833 ipfr_newfrag(ip, fin); 834 return is; 835} 836 837 838static int fr_tcpoptions(tcp) 839tcphdr_t *tcp; 840{ 841 u_char *opt, *last; 842 int wscale; 843 844 opt = (u_char *) (tcp + 1); 845 last = ((u_char *)tcp) + (tcp->th_off << 2); 846 847 /* If we don't find wscale here, we need to clear it */ 848 wscale = -2; 849 850 /* Termination condition picked such that opt[0 .. 2] exist */ 851 while ((opt < last - 2) && (*opt != TCPOPT_EOL)) { 852 switch (*opt) { 853 case TCPOPT_NOP: 854 opt++; 855 continue; 856 case TCPOPT_WSCALE: 857 /* Proper length ? */ 858 if (opt[1] == 3) { 859 if (opt[2] > 14) 860 wscale = 14; 861 else 862 wscale = opt[2]; 863 } 864 break; 865 default: 866 /* Unknown options must be two bytes+ */ 867 if (opt[1] < 2) 868 break; 869 opt += opt[1]; 870 continue; 871 } 872 break; 873 } 874 return wscale; 875} 876 877 878 879/* 880 * check to see if a packet with TCP headers fits within the TCP window. 881 * change timeout depending on whether new packet is a SYN-ACK returning for a 882 * SYN or a RST or FIN which indicate time to close up shop. 883 */ 884int fr_tcpstate(is, fin, ip, tcp) 885register ipstate_t *is; 886fr_info_t *fin; 887ip_t *ip; 888tcphdr_t *tcp; 889{ 890 register tcp_seq seq, ack, end; 891 register int ackskew; 892 tcpdata_t *fdata, *tdata; 893 u_32_t win, maxwin; 894 int ret = 0, off; 895 int source; 896 int wscale; 897 898 /* 899 * Find difference between last checked packet and this packet. 900 */ 901 source = IP6EQ(fin->fin_fi.fi_src, is->is_src); 902 if (source && (ntohs(is->is_sport) != fin->fin_data[0])) 903 source = 0; 904 fdata = &is->is_tcp.ts_data[!source]; 905 tdata = &is->is_tcp.ts_data[source]; 906 off = tcp->th_off << 2; 907 seq = ntohl(tcp->th_seq); 908 ack = ntohl(tcp->th_ack); 909 win = ntohs(tcp->th_win); 910 end = seq + fin->fin_dlen - off + 911 ((tcp->th_flags & TH_SYN) ? 1 : 0) + 912 ((tcp->th_flags & TH_FIN) ? 1 : 0); 913 914 915 if ((tcp->th_flags & TH_SYN) && (off >= sizeof(*tcp) + 4)) 916 wscale = fr_tcpoptions(tcp); 917 else 918 wscale = -1; 919 920 MUTEX_ENTER(&is->is_lock); 921 922 if (wscale >= 0) 923 fdata->td_wscale = wscale; 924 else if (wscale == -2) 925 fdata->td_wscale = tdata->td_wscale = 0; 926 win <<= fdata->td_wscale; 927 928 if ((fdata->td_end == 0) && 929 (!is->is_fsm || ((tcp->th_flags & TH_OPENING) == TH_OPENING))) { 930 /* 931 * Must be a (outgoing) SYN-ACK in reply to a SYN. 932 */ 933 fdata->td_end = end; 934 fdata->td_maxwin = 1; 935 fdata->td_maxend = end + win; 936 if (win == 0) 937 fdata->td_maxend++; 938 } 939 940 if (!(tcp->th_flags & TH_ACK)) { /* Pretend an ack was sent */ 941 ack = tdata->td_end; 942 } else if (((tcp->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) && 943 (ack == 0)) { 944 /* gross hack to get around certain broken tcp stacks */ 945 ack = tdata->td_end; 946 } 947 948 if (seq == end) 949 seq = end = fdata->td_end; 950 951 maxwin = tdata->td_maxwin; 952 ackskew = tdata->td_end - ack; 953 954#define SEQ_GE(a,b) ((int)((a) - (b)) >= 0) 955#define SEQ_GT(a,b) ((int)((a) - (b)) > 0) 956 if ((SEQ_GE(fdata->td_maxend, end)) && 957 (SEQ_GE(seq, fdata->td_end - maxwin)) && 958/* XXX what about big packets */ 959#define MAXACKWINDOW 66000 960 (ackskew >= -MAXACKWINDOW) && 961 (ackskew <= MAXACKWINDOW)) { 962 /* if ackskew < 0 then this should be due to fragented 963 * packets. There is no way to know the length of the 964 * total packet in advance. 965 * We do know the total length from the fragment cache though. 966 * Note however that there might be more sessions with 967 * exactly the same source and destination paramters in the 968 * state cache (and source and destination is the only stuff 969 * that is saved in the fragment cache). Note further that 970 * some TCP connections in the state cache are hashed with 971 * sport and dport as well which makes it not worthwhile to 972 * look for them. 973 * Thus, when ackskew is negative but still seems to belong 974 * to this session, we bump up the destinations end value. 975 */ 976 /* 977 * Nearing end of connection, start timeout. 978 */ 979 /* source ? 0 : 1 -> !source */ 980 if (fr_tcp_age(&is->is_age, is->is_state, fin, !source, 981 (int)is->is_fsm) == 0) { 982 if (ackskew < 0) 983 tdata->td_end = ack; 984 985 /* update max window seen */ 986 if (fdata->td_maxwin < win) 987 fdata->td_maxwin = win; 988 if (SEQ_GT(end, fdata->td_end)) 989 fdata->td_end = end; 990 if (SEQ_GE(ack + win, tdata->td_maxend)) { 991 tdata->td_maxend = ack + win; 992 if (win == 0) 993 tdata->td_maxend++; 994 } 995 996 ATOMIC_INCL(ips_stats.iss_hits); 997 ret = 1; 998 } 999 } 1000 MUTEX_EXIT(&is->is_lock); 1001 if ((ret == 0) && ((tcp->th_flags & TH_OPENING) != TH_SYN)) 1002 fin->fin_misc |= FM_BADSTATE; 1003 return ret; 1004} 1005 1006 1007/* 1008 * Match a state table entry against an IP packet. 1009 */ 1010static int fr_matchsrcdst(is, src, dst, fin, tcp) 1011ipstate_t *is; 1012union i6addr src, dst; 1013fr_info_t *fin; 1014tcphdr_t *tcp; 1015{ 1016 int ret = 0, rev, out, flags, idx; 1017 u_short sp, dp; 1018 void *ifp; 1019 1020 rev = IP6NEQ(is->is_dst, dst); 1021 ifp = fin->fin_ifp; 1022 out = fin->fin_out; 1023 flags = is->is_flags & (FI_WILDA|FI_WILDP); 1024 sp = 0; 1025 dp = 0; 1026 1027 if (tcp != NULL) { 1028 flags = is->is_flags; 1029 sp = tcp->th_sport; 1030 dp = tcp->th_dport; 1031 if (!rev) { 1032 if (!(flags & FI_W_SPORT) && (sp != is->is_sport)) 1033 rev = 1; 1034 else if (!(flags & FI_W_DPORT) && (dp != is->is_dport)) 1035 rev = 1; 1036 } 1037 } 1038 1039 idx = (out << 1) + rev; 1040 1041 if ((is->is_ifp[idx] == NULL && 1042 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) || 1043 is->is_ifp[idx] == ifp) 1044 ret = 1; 1045 1046 if (ret == 0) 1047 return 0; 1048 ret = 0; 1049 1050 if (rev == 0) { 1051 if ((IP6EQ(is->is_dst, dst) || (flags & FI_W_DADDR)) && 1052 (IP6EQ(is->is_src, src) || (flags & FI_W_SADDR)) && 1053 (!tcp || ((sp == is->is_sport || flags & FI_W_SPORT) && 1054 (dp == is->is_dport || flags & FI_W_DPORT)))) { 1055 ret = 1; 1056 } 1057 } else { 1058 if ((IP6EQ(is->is_dst, src) || (flags & FI_W_DADDR)) && 1059 (IP6EQ(is->is_src, dst) || (flags & FI_W_SADDR)) && 1060 (!tcp || ((sp == is->is_dport || flags & FI_W_DPORT) && 1061 (dp == is->is_sport || flags & FI_W_SPORT)))) { 1062 ret = 1; 1063 } 1064 } 1065 if (ret == 0) 1066 return 0; 1067 1068 /* 1069 * Whether or not this should be here, is questionable, but the aim 1070 * is to get this out of the main line. 1071 */ 1072 if (tcp == NULL) 1073 flags = is->is_flags & (FI_CMP|(FI_CMP<<4)); 1074 1075 if (((fin->fin_fl & (flags >> 4)) != (flags & FI_CMP)) || 1076 (fin->fin_fi.fi_optmsk != is->is_opt) || 1077 (fin->fin_fi.fi_secmsk != is->is_sec) || 1078 (fin->fin_fi.fi_auth != is->is_auth)) 1079 return 0; 1080 1081 flags = is->is_flags & (FI_WILDA|FI_WILDP); 1082 if ((flags & (FI_W_SADDR|FI_W_DADDR))) { 1083 if ((flags & FI_W_SADDR) != 0) { 1084 if (rev == 0) { 1085 is->is_src = fin->fin_fi.fi_src; 1086 } else { 1087 is->is_src = fin->fin_fi.fi_dst; 1088 } 1089 } else if ((flags & FI_W_DADDR) != 0) { 1090 if (rev == 0) { 1091 is->is_dst = fin->fin_fi.fi_dst; 1092 } else { 1093 is->is_dst = fin->fin_fi.fi_src; 1094 } 1095 } 1096 is->is_flags &= ~(FI_W_SADDR|FI_W_DADDR); 1097 if ((is->is_flags & (FI_WILDA|FI_WILDP)) == 0) 1098 ips_wild--; 1099 } 1100 1101 if ((flags & (FI_W_SPORT|FI_W_DPORT))) { 1102 if ((flags & FI_W_SPORT) != 0) { 1103 if (rev == 0) { 1104 is->is_sport = sp; 1105 is->is_send = htonl(tcp->th_seq); 1106 } else { 1107 is->is_sport = dp; 1108 is->is_send = htonl(tcp->th_ack); 1109 } 1110 is->is_maxsend = is->is_send + 1; 1111 } else if ((flags & FI_W_DPORT) != 0) { 1112 if (rev == 0) { 1113 is->is_dport = dp; 1114 is->is_dend = htonl(tcp->th_ack); 1115 } else { 1116 is->is_dport = sp; 1117 is->is_dend = htonl(tcp->th_seq); 1118 } 1119 is->is_maxdend = is->is_dend + 1; 1120 } 1121 is->is_flags &= ~(FI_W_SPORT|FI_W_DPORT); 1122 ips_wild--; 1123 } 1124 1125 ret = -1; 1126 1127 if (is->is_ifp[idx] == NULL && 1128 (*is->is_ifname[idx] == '\0' || *is->is_ifname[idx] == '*')) 1129 ret = idx; 1130 1131 if (ret >= 0) { 1132 is->is_ifp[ret] = ifp; 1133 strncpy(is->is_ifname[ret], IFNAME(ifp), 1134 sizeof(is->is_ifname[ret])); 1135 } 1136 fin->fin_rev = rev; 1137 return 1; 1138} 1139 1140static int fr_matchicmpqueryreply(v, is, icmp, rev) 1141int v; 1142ipstate_t *is; 1143icmphdr_t *icmp; 1144int rev; 1145{ 1146 if (v == 4) { 1147 /* 1148 * If we matched its type on the way in, then when going out 1149 * it will still be the same type. 1150 */ 1151 if ((!rev && (icmp->icmp_type == is->is_type)) || 1152 (rev && (icmpreplytype4[is->is_type] == icmp->icmp_type))) { 1153 if (icmp->icmp_type != ICMP_ECHOREPLY) 1154 return 1; 1155 if ((icmp->icmp_id == is->is_icmp.ics_id) && 1156 (icmp->icmp_seq == is->is_icmp.ics_seq)) 1157 return 1; 1158 } 1159 } 1160#ifdef USE_INET6 1161 else if (is->is_v == 6) { 1162 if ((!rev && (icmp->icmp_type == is->is_type)) || 1163 (rev && (icmpreplytype6[is->is_type] == icmp->icmp_type))) { 1164 if (icmp->icmp_type != ICMP6_ECHO_REPLY) 1165 return 1; 1166 if ((icmp->icmp_id == is->is_icmp.ics_id) && 1167 (icmp->icmp_seq == is->is_icmp.ics_seq)) 1168 return 1; 1169 } 1170 } 1171#endif 1172 return 0; 1173} 1174 1175static frentry_t *fr_checkicmpmatchingstate(ip, fin) 1176ip_t *ip; 1177fr_info_t *fin; 1178{ 1179 register ipstate_t *is, **isp; 1180 register u_short sport, dport; 1181 register u_char pr; 1182 u_short savelen, ohlen; 1183 union i6addr dst, src; 1184 struct icmp *ic; 1185 icmphdr_t *icmp; 1186 fr_info_t ofin; 1187 int type, len; 1188 tcphdr_t *tcp; 1189 frentry_t *fr; 1190 ip_t *oip; 1191 u_int hv; 1192 1193 /* 1194 * Does it at least have the return (basic) IP header ? 1195 * Only a basic IP header (no options) should be with 1196 * an ICMP error header. 1197 */ 1198 if (((ip->ip_v != 4) || (ip->ip_hl != 5)) || 1199 (fin->fin_plen < ICMPERR_MINPKTLEN)) 1200 return NULL; 1201 1202 ic = (struct icmp *)fin->fin_dp; 1203 type = ic->icmp_type; 1204 /* 1205 * If it's not an error type, then return 1206 */ 1207 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) && 1208 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) && 1209 (type != ICMP_PARAMPROB)) 1210 return NULL; 1211 1212 oip = (ip_t *)((char *)ic + ICMPERR_ICMPHLEN); 1213 ohlen = oip->ip_hl << 2; 1214 if (fin->fin_plen < ICMPERR_MAXPKTLEN + ohlen - sizeof(*oip)) 1215 return NULL; 1216 1217 /* 1218 * Sanity checks. 1219 */ 1220 len = fin->fin_dlen - ICMPERR_ICMPHLEN; 1221 if ((len <= 0) || (ohlen > len)) 1222 return NULL; 1223 1224 /* 1225 * Is the buffer big enough for all of it ? It's the size of the IP 1226 * header claimed in the encapsulated part which is of concern. It 1227 * may be too big to be in this buffer but not so big that it's 1228 * outside the ICMP packet, leading to TCP deref's causing problems. 1229 * This is possible because we don't know how big oip_hl is when we 1230 * do the pullup early in fr_check() and thus can't gaurantee it is 1231 * all here now. 1232 */ 1233#ifdef _KERNEL 1234 { 1235 mb_t *m; 1236 1237# if SOLARIS 1238 m = fin->fin_qfm; 1239 if ((char *)oip + len > (char *)m->b_wptr) 1240 return NULL; 1241# else 1242 m = *(mb_t **)fin->fin_mp; 1243 if ((char *)oip + len > (char *)ip + m->m_len) 1244 return NULL; 1245# endif 1246 } 1247#endif 1248 1249 /* 1250 * in the IPv4 case we must zero the i6addr union otherwise 1251 * the IP6EQ and IP6NEQ macros produce the wrong results because 1252 * of the 'junk' in the unused part of the union 1253 */ 1254 bzero((char *)&src, sizeof(src)); 1255 bzero((char *)&dst, sizeof(dst)); 1256 bzero((char *)&ofin, sizeof(ofin)); 1257 ofin.fin_ifp = fin->fin_ifp; 1258 ofin.fin_out = !fin->fin_out; 1259 ofin.fin_v = 4; 1260 fr = NULL; 1261 1262 switch (oip->ip_p) 1263 { 1264 case IPPROTO_ICMP : 1265 icmp = (icmphdr_t *)((char *)oip + ohlen); 1266 1267 /* 1268 * a ICMP error can only be generated as a result of an 1269 * ICMP query, not as the response on an ICMP error 1270 * 1271 * XXX theoretically ICMP_ECHOREP and the other reply's are 1272 * ICMP query's as well, but adding them here seems strange XXX 1273 */ 1274 if ((icmp->icmp_type != ICMP_ECHO) && 1275 (icmp->icmp_type != ICMP_TSTAMP) && 1276 (icmp->icmp_type != ICMP_IREQ) && 1277 (icmp->icmp_type != ICMP_MASKREQ)) 1278 return NULL; 1279 1280 /* 1281 * perform a lookup of the ICMP packet in the state table 1282 */ 1283 hv = (pr = oip->ip_p); 1284 src.in4 = oip->ip_src; 1285 hv += src.in4.s_addr; 1286 dst.in4 = oip->ip_dst; 1287 hv += dst.in4.s_addr; 1288 hv += icmp->icmp_id; 1289 hv += icmp->icmp_seq; 1290 hv %= fr_statesize; 1291 1292 savelen = oip->ip_len; 1293 oip->ip_len = len; 1294 fr_makefrip(ohlen, oip, &ofin); 1295 oip->ip_len = savelen; 1296 1297 READ_ENTER(&ipf_state); 1298 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) 1299 if ((is->is_p == pr) && (is->is_v == 4) && 1300 fr_matchsrcdst(is, src, dst, &ofin, NULL) && 1301 fr_matchicmpqueryreply(is->is_v, is, icmp, fin->fin_rev)) { 1302 ips_stats.iss_hits++; 1303 is->is_pkts++; 1304 is->is_bytes += ip->ip_len; 1305 fr = is->is_rule; 1306 break; 1307 } 1308 RWLOCK_EXIT(&ipf_state); 1309 return fr; 1310 1311 case IPPROTO_TCP : 1312 case IPPROTO_UDP : 1313 if (fin->fin_plen < ICMPERR_MAXPKTLEN) 1314 return NULL; 1315 break; 1316 default : 1317 return NULL; 1318 } 1319 1320 tcp = (tcphdr_t *)((char *)oip + ohlen); 1321 dport = tcp->th_dport; 1322 sport = tcp->th_sport; 1323 1324 hv = (pr = oip->ip_p); 1325 src.in4 = oip->ip_src; 1326 hv += src.in4.s_addr; 1327 dst.in4 = oip->ip_dst; 1328 hv += dst.in4.s_addr; 1329 hv += dport; 1330 hv += sport; 1331 hv %= fr_statesize; 1332 /* 1333 * we make an fin entry to be able to feed it to 1334 * matchsrcdst note that not all fields are encessary 1335 * but this is the cleanest way. Note further we fill 1336 * in fin_mp such that if someone uses it we'll get 1337 * a kernel panic. fr_matchsrcdst does not use this. 1338 * 1339 * watch out here, as ip is in host order and oip in network 1340 * order. Any change we make must be undone afterwards. 1341 */ 1342 savelen = oip->ip_len; 1343 oip->ip_len = len; 1344 fr_makefrip(ohlen, oip, &ofin); 1345 oip->ip_len = savelen; 1346 READ_ENTER(&ipf_state); 1347 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) { 1348 /* 1349 * Only allow this icmp though if the 1350 * encapsulated packet was allowed through the 1351 * other way around. Note that the minimal amount 1352 * of info present does not allow for checking against 1353 * tcp internals such as seq and ack numbers. 1354 */ 1355 if ((is->is_p == pr) && (is->is_v == 4) && 1356 fr_matchsrcdst(is, src, dst, &ofin, tcp)) { 1357 fr = is->is_rule; 1358 ips_stats.iss_hits++; 1359 is->is_pkts++; 1360 is->is_bytes += fin->fin_plen; 1361 /* 1362 * we deliberately do not touch the timeouts 1363 * for the accompanying state table entry. 1364 * It remains to be seen if that is correct. XXX 1365 */ 1366 break; 1367 } 1368 } 1369 RWLOCK_EXIT(&ipf_state); 1370 return fr; 1371} 1372 1373 1374/* 1375 * Move a state hash table entry from its old location at is->is_hv to 1376 * its new location, indexed by hv % fr_statesize. 1377 */ 1378static void fr_ipsmove(isp, is, hv) 1379ipstate_t **isp, *is; 1380u_int hv; 1381{ 1382 u_int hvm; 1383 1384 hvm = is->is_hv; 1385 /* 1386 * Remove the hash from the old location... 1387 */ 1388 if (is->is_hnext) 1389 is->is_hnext->is_phnext = isp; 1390 *isp = is->is_hnext; 1391 if (ips_table[hvm] == NULL) 1392 ips_stats.iss_inuse--; 1393 1394 /* 1395 * ...and put the hash in the new one. 1396 */ 1397 hvm = hv % fr_statesize; 1398 is->is_hv = hvm; 1399 isp = &ips_table[hvm]; 1400 if (*isp) 1401 (*isp)->is_phnext = &is->is_hnext; 1402 else 1403 ips_stats.iss_inuse++; 1404 is->is_phnext = isp; 1405 is->is_hnext = *isp; 1406 *isp = is; 1407} 1408 1409 1410/* 1411 * Check if a packet has a registered state. 1412 */ 1413frentry_t *fr_checkstate(ip, fin) 1414ip_t *ip; 1415fr_info_t *fin; 1416{ 1417 union i6addr dst, src; 1418 register ipstate_t *is, **isp; 1419 register u_char pr; 1420 u_int hv, hvm, hlen, tryagain, pass, v; 1421 struct icmp *ic; 1422 frentry_t *fr; 1423 tcphdr_t *tcp; 1424 int rev; 1425 1426 if ((ips_list == NULL) || (fin->fin_off != 0) || fr_state_lock || 1427 (fin->fin_fl & FI_SHORT)) 1428 return NULL; 1429 1430 is = NULL; 1431 hlen = fin->fin_hlen; 1432 tcp = (tcphdr_t *)((char *)ip + hlen); 1433 ic = (struct icmp *)tcp; 1434 hv = (pr = fin->fin_fi.fi_p); 1435 src = fin->fin_fi.fi_src; 1436 dst = fin->fin_fi.fi_dst; 1437 hv += src.in4.s_addr; 1438 hv += dst.in4.s_addr; 1439 1440 /* 1441 * Search the hash table for matching packet header info. 1442 * At the bottom of this switch statement, the following is expected: 1443 * is == NULL, no lock on ipf_state is held. 1444 * is != NULL, a lock on ipf_state is held. 1445 */ 1446 v = fin->fin_fi.fi_v; 1447#ifdef USE_INET6 1448 if (v == 6) { 1449 hv += fin->fin_fi.fi_src.i6[1]; 1450 hv += fin->fin_fi.fi_src.i6[2]; 1451 hv += fin->fin_fi.fi_src.i6[3]; 1452 1453 if ((fin->fin_p == IPPROTO_ICMPV6) && 1454 IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_dst.in6)) { 1455 hv -= dst.in4.s_addr; 1456 } else { 1457 hv += fin->fin_fi.fi_dst.i6[1]; 1458 hv += fin->fin_fi.fi_dst.i6[2]; 1459 hv += fin->fin_fi.fi_dst.i6[3]; 1460 } 1461 } 1462#endif 1463 1464 switch (fin->fin_p) 1465 { 1466#ifdef USE_INET6 1467 case IPPROTO_ICMPV6 : 1468 tcp = NULL; 1469 tryagain = 0; 1470 if (v == 6) { 1471 if ((ic->icmp_type == ICMP6_ECHO_REQUEST) || 1472 (ic->icmp_type == ICMP6_ECHO_REPLY)) { 1473 hv += ic->icmp_id; 1474 hv += ic->icmp_seq; 1475 } 1476 } 1477 READ_ENTER(&ipf_state); 1478icmp6again: 1479 hvm = hv % fr_statesize; 1480 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext) 1481 if ((is->is_p == pr) && (is->is_v == v) && 1482 fr_matchsrcdst(is, src, dst, fin, NULL) && 1483 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) { 1484 rev = fin->fin_rev; 1485 if (is->is_frage[rev] != 0) 1486 is->is_age = is->is_frage[rev]; 1487 else if (rev != 0) 1488 is->is_age = fr_icmpacktimeout; 1489 else 1490 is->is_age = fr_icmptimeout; 1491 break; 1492 } 1493 1494 if (is != NULL) { 1495 if (tryagain && !(is->is_flags & FI_W_DADDR)) { 1496 hv += fin->fin_fi.fi_src.i6[0]; 1497 hv += fin->fin_fi.fi_src.i6[1]; 1498 hv += fin->fin_fi.fi_src.i6[2]; 1499 hv += fin->fin_fi.fi_src.i6[3]; 1500 fr_ipsmove(isp, is, hv); 1501 MUTEX_DOWNGRADE(&ipf_state); 1502 } 1503 break; 1504 } 1505 RWLOCK_EXIT(&ipf_state); 1506 1507 /* 1508 * No matching icmp state entry. Perhaps this is a 1509 * response to another state entry. 1510 */ 1511 if ((ips_wild != 0) && (v == 6) && (tryagain == 0) && 1512 !IN6_IS_ADDR_MULTICAST(&fin->fin_fi.fi_src.in6)) { 1513 hv -= fin->fin_fi.fi_src.i6[0]; 1514 hv -= fin->fin_fi.fi_src.i6[1]; 1515 hv -= fin->fin_fi.fi_src.i6[2]; 1516 hv -= fin->fin_fi.fi_src.i6[3]; 1517 tryagain = 1; 1518 WRITE_ENTER(&ipf_state); 1519 goto icmp6again; 1520 } 1521 1522 fr = fr_checkicmp6matchingstate((ip6_t *)ip, fin); 1523 if (fr) 1524 return fr; 1525 break; 1526#endif 1527 case IPPROTO_ICMP : 1528 tcp = NULL; 1529 if (v == 4) { 1530 hv += ic->icmp_id; 1531 hv += ic->icmp_seq; 1532 } 1533 hvm = hv % fr_statesize; 1534 READ_ENTER(&ipf_state); 1535 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext) 1536 if ((is->is_p == pr) && (is->is_v == v) && 1537 fr_matchsrcdst(is, src, dst, fin, NULL) && 1538 fr_matchicmpqueryreply(v, is, ic, fin->fin_rev)) { 1539 rev = fin->fin_rev; 1540 if (is->is_frage[rev] != 0) 1541 is->is_age = is->is_frage[rev]; 1542 else if (fin->fin_rev) 1543 is->is_age = fr_icmpacktimeout; 1544 else 1545 is->is_age = fr_icmptimeout; 1546 break; 1547 } 1548 1549 if (is != NULL) 1550 break; 1551 RWLOCK_EXIT(&ipf_state); 1552 /* 1553 * No matching icmp state entry. Perhaps this is a 1554 * response to another state entry. 1555 */ 1556 fr = fr_checkicmpmatchingstate(ip, fin); 1557 if (fr) 1558 return fr; 1559 break; 1560 case IPPROTO_TCP : 1561 /* 1562 * Just plain ignore RST flag set with either FIN or SYN. 1563 */ 1564 if ((tcp->th_flags & TH_RST) && 1565 ((tcp->th_flags & (TH_FIN|TH_SYN|TH_RST)) != TH_RST)) 1566 break; 1567 case IPPROTO_UDP : 1568 { 1569 register u_short dport, sport; 1570 1571 dport = tcp->th_dport; 1572 sport = tcp->th_sport; 1573 tryagain = 0; 1574 hv += dport; 1575 hv += sport; 1576 READ_ENTER(&ipf_state); 1577retry_tcpudp: 1578 hvm = hv % fr_statesize; 1579 for (isp = &ips_table[hvm]; (is = *isp); isp = &is->is_hnext) 1580 if ((is->is_p == pr) && (is->is_v == v) && 1581 fr_matchsrcdst(is, src, dst, fin, tcp)) { 1582 rev = fin->fin_rev; 1583 if ((pr == IPPROTO_TCP)) { 1584 if (!fr_tcpstate(is, fin, ip, tcp)) 1585 is = NULL; 1586 } else if ((pr == IPPROTO_UDP)) { 1587 if (is->is_frage[rev] != 0) 1588 is->is_age = is->is_frage[rev]; 1589 else if (fin->fin_rev) 1590 is->is_age = fr_udpacktimeout; 1591 else 1592 is->is_age = fr_udptimeout; 1593 } 1594 break; 1595 } 1596 if (is != NULL) { 1597 if (tryagain && 1598 !(is->is_flags & (FI_WILDP|FI_WILDA))) { 1599 hv += dport; 1600 hv += sport; 1601 fr_ipsmove(isp, is, hv); 1602 MUTEX_DOWNGRADE(&ipf_state); 1603 } 1604 break; 1605 } 1606 1607 RWLOCK_EXIT(&ipf_state); 1608 if (!tryagain && ips_wild) { 1609 hv -= dport; 1610 hv -= sport; 1611 tryagain = 1; 1612 WRITE_ENTER(&ipf_state); 1613 goto retry_tcpudp; 1614 } 1615 break; 1616 } 1617 default : 1618 tcp = NULL; 1619 hv %= fr_statesize; 1620 READ_ENTER(&ipf_state); 1621 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) { 1622 if ((is->is_p == pr) && (is->is_v == v) && 1623 fr_matchsrcdst(is, src, dst, fin, NULL)) { 1624 rev = fin->fin_rev; 1625 if (is->is_frage[rev] != 0) 1626 is->is_age = is->is_frage[rev]; 1627 else 1628 is->is_age = fr_udptimeout; 1629 break; 1630 } 1631 } 1632 if (is == NULL) { 1633 RWLOCK_EXIT(&ipf_state); 1634 } 1635 break; 1636 } 1637 1638 if (is == NULL) { 1639 ATOMIC_INCL(ips_stats.iss_miss); 1640 return NULL; 1641 } 1642 1643 MUTEX_ENTER(&is->is_lock); 1644 is->is_bytes += fin->fin_plen; 1645 ips_stats.iss_hits++; 1646 is->is_pkts++; 1647 MUTEX_EXIT(&is->is_lock); 1648 fr = is->is_rule; 1649 fin->fin_rule = is->is_rulen; 1650 if (fr != NULL) { 1651 fin->fin_group = fr->fr_group; 1652 fin->fin_icode = fr->fr_icode; 1653 } 1654 fin->fin_fr = fr; 1655 pass = is->is_pass; 1656 RWLOCK_EXIT(&ipf_state); 1657 if ((fin->fin_fl & FI_FRAG) && (pass & FR_KEEPFRAG)) 1658 ipfr_newfrag(ip, fin); 1659#ifndef _KERNEL 1660 if ((tcp != NULL) && (tcp->th_flags & TCP_CLOSE)) 1661 fr_delstate(is); 1662#endif 1663 return fr; 1664} 1665 1666 1667/* 1668 * Sync. state entries. If interfaces come or go or just change position, 1669 * this is needed. 1670 */ 1671void ip_statesync(ifp) 1672void *ifp; 1673{ 1674 register ipstate_t *is; 1675 int i; 1676 1677 WRITE_ENTER(&ipf_state); 1678 for (is = ips_list; is; is = is->is_next) { 1679 for (i = 0; i < 4; i++) { 1680 if (is->is_ifp[i] == ifp) { 1681 is->is_ifp[i] = GETUNIT(is->is_ifname[i], 1682 is->is_v); 1683 if (!is->is_ifp[i]) 1684 is->is_ifp[i] = (void *)-1; 1685 } 1686 } 1687 } 1688 RWLOCK_EXIT(&ipf_state); 1689} 1690 1691 1692/* 1693 * Must always be called with fr_ipfstate held as a write lock. 1694 */ 1695static void fr_delstate(is) 1696ipstate_t *is; 1697{ 1698 frentry_t *fr; 1699 1700 if (is->is_flags & (FI_WILDP|FI_WILDA)) 1701 ips_wild--; 1702 if (is->is_next) 1703 is->is_next->is_pnext = is->is_pnext; 1704 *is->is_pnext = is->is_next; 1705 if (is->is_hnext) 1706 is->is_hnext->is_phnext = is->is_phnext; 1707 *is->is_phnext = is->is_hnext; 1708 if (ips_table[is->is_hv] == NULL) 1709 ips_stats.iss_inuse--; 1710 if (is->is_me) 1711 *is->is_me = NULL; 1712 1713 fr = is->is_rule; 1714 if (fr != NULL) { 1715 fr->fr_ref--; 1716 if (fr->fr_ref == 0) { 1717 KFREE(fr); 1718 } 1719 } 1720#ifdef _KERNEL 1721 MUTEX_DESTROY(&is->is_lock); 1722#endif 1723 KFREE(is); 1724 ips_num--; 1725} 1726 1727 1728/* 1729 * Free memory in use by all state info. kept. 1730 */ 1731void fr_stateunload() 1732{ 1733 register ipstate_t *is; 1734 1735 WRITE_ENTER(&ipf_state); 1736 while ((is = ips_list)) 1737 fr_delstate(is); 1738 ips_stats.iss_inuse = 0; 1739 ips_num = 0; 1740 RWLOCK_EXIT(&ipf_state); 1741 if (ips_table) 1742 KFREES(ips_table, fr_statesize * sizeof(ipstate_t *)); 1743 ips_table = NULL; 1744} 1745 1746 1747/* 1748 * Slowly expire held state for thingslike UDP and ICMP. Timeouts are set 1749 * in expectation of this being called twice per second. 1750 */ 1751void fr_timeoutstate() 1752{ 1753 register ipstate_t *is, **isp; 1754#if defined(_KERNEL) && !SOLARIS 1755 int s; 1756#endif 1757 1758 SPL_NET(s); 1759 WRITE_ENTER(&ipf_state); 1760 for (isp = &ips_list; (is = *isp); ) 1761 if (is->is_age && !--is->is_age) { 1762 if (is->is_p == IPPROTO_TCP) 1763 ips_stats.iss_fin++; 1764 else 1765 ips_stats.iss_expire++; 1766#ifdef IPFILTER_LOG 1767 ipstate_log(is, ISL_EXPIRE); 1768#endif 1769 fr_delstate(is); 1770 } else 1771 isp = &is->is_next; 1772 if (fr_state_doflush) { 1773 (void) fr_state_flush(2, 0); 1774 fr_state_doflush = 0; 1775 } 1776 RWLOCK_EXIT(&ipf_state); 1777 SPL_X(s); 1778} 1779 1780 1781/* 1782 * Original idea freom Pradeep Krishnan for use primarily with NAT code. 1783 * (pkrishna@netcom.com) 1784 * 1785 * Rewritten by Arjan de Vet <Arjan.deVet@adv.iae.nl>, 2000-07-29: 1786 * 1787 * - (try to) base state transitions on real evidence only, 1788 * i.e. packets that are sent and have been received by ipfilter; 1789 * diagram 18.12 of TCP/IP volume 1 by W. Richard Stevens was used. 1790 * 1791 * - deal with half-closed connections correctly; 1792 * 1793 * - store the state of the source in state[0] such that ipfstat 1794 * displays the state as source/dest instead of dest/source; the calls 1795 * to fr_tcp_age have been changed accordingly. 1796 * 1797 * Parameters: 1798 * 1799 * state[0] = state of source (host that initiated connection) 1800 * state[1] = state of dest (host that accepted the connection) 1801 * 1802 * dir == 0 : a packet from source to dest 1803 * dir == 1 : a packet from dest to source 1804 * 1805 */ 1806int fr_tcp_age(age, state, fin, dir, fsm) 1807u_long *age; 1808u_char *state; 1809fr_info_t *fin; 1810int dir, fsm; 1811{ 1812 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp; 1813 u_char flags = tcp->th_flags; 1814 int dlen, ostate; 1815 u_long newage; 1816 1817 ostate = state[1 - dir]; 1818 1819 dlen = fin->fin_plen - fin->fin_hlen - (tcp->th_off << 2); 1820 1821 if (flags & TH_RST) { 1822 if (!(tcp->th_flags & TH_PUSH) && !dlen) { 1823 *age = fr_tcpclosed; 1824 state[dir] = TCPS_CLOSED; 1825 } else { 1826 *age = fr_tcpclosewait; 1827 state[dir] = TCPS_CLOSE_WAIT; 1828 } 1829 return 0; 1830 } 1831 1832 newage = 0; 1833 1834 switch(state[dir]) 1835 { 1836 case TCPS_CLOSED: /* 0 */ 1837 if ((flags & TH_OPENING) == TH_OPENING) { 1838 /* 1839 * 'dir' received an S and sends SA in response, 1840 * CLOSED -> SYN_RECEIVED 1841 */ 1842 state[dir] = TCPS_SYN_RECEIVED; 1843 newage = fr_tcptimeout; 1844 } else if ((flags & TH_OPENING) == TH_SYN) { 1845 /* 'dir' sent S, CLOSED -> SYN_SENT */ 1846 state[dir] = TCPS_SYN_SENT; 1847 newage = fr_tcptimeout; 1848 } 1849 1850 /* 1851 * It is apparently possible that a hosts sends two syncs 1852 * before the remote party is able to respond with a SA. In 1853 * such a case the remote server sometimes ACK's the second 1854 * sync, and then responds with a SA. The following code 1855 * is used to prevent this ack from being blocked. 1856 * 1857 * We do not reset the timeout here to fr_tcptimeout because 1858 * a connection connect timeout does not renew after every 1859 * packet that is sent. We need to set newage to something 1860 * to indicate the packet has passed the check for its flags 1861 * being valid in the TCP FSM. 1862 */ 1863 else if ((ostate == TCPS_SYN_SENT) && 1864 ((flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK)) { 1865 newage = *age; 1866 } 1867 1868 /* 1869 * The next piece of code makes it possible to get 1870 * already established connections into the state table 1871 * after a restart or reload of the filter rules; this 1872 * does not work when a strict 'flags S keep state' is 1873 * used for tcp connections of course, however, use a 1874 * lower time-out so the state disappears quickly if 1875 * the other side does not pick it up. 1876 */ 1877 else if (!fsm && 1878 (flags & (TH_FIN|TH_SYN|TH_RST|TH_ACK)) == TH_ACK) { 1879 /* we saw an A, guess 'dir' is in ESTABLISHED mode */ 1880 if (ostate == TCPS_CLOSED) { 1881 state[dir] = TCPS_ESTABLISHED; 1882 newage = fr_tcptimeout; 1883 } else if (ostate == TCPS_ESTABLISHED) { 1884 state[dir] = TCPS_ESTABLISHED; 1885 newage = fr_tcpidletimeout; 1886 } 1887 } 1888 /* 1889 * TODO: besides regular ACK packets we can have other 1890 * packets as well; it is yet to be determined how we 1891 * should initialize the states in those cases 1892 */ 1893 break; 1894 1895 case TCPS_LISTEN: /* 1 */ 1896 /* NOT USED */ 1897 break; 1898 1899 case TCPS_SYN_SENT: /* 2 */ 1900 if ((flags & ~(TH_ECN|TH_CWR)) == TH_SYN) { 1901 /* 1902 * A retransmitted SYN packet. We do not reset the 1903 * timeout here to fr_tcptimeout because a connection 1904 * connect timeout does not renew after every packet 1905 * that is sent. We need to set newage to something 1906 * to indicate the packet has passed the check for its 1907 * flags being valid in the TCP FSM. 1908 */ 1909 newage = *age; 1910 } else if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) { 1911 /* 1912 * We see an A from 'dir' which is in SYN_SENT 1913 * state: 'dir' sent an A in response to an SA 1914 * which it received, SYN_SENT -> ESTABLISHED 1915 */ 1916 state[dir] = TCPS_ESTABLISHED; 1917 newage = fr_tcpidletimeout; 1918 } else if (flags & TH_FIN) { 1919 /* 1920 * We see an F from 'dir' which is in SYN_SENT 1921 * state and wants to close its side of the 1922 * connection; SYN_SENT -> FIN_WAIT_1 1923 */ 1924 state[dir] = TCPS_FIN_WAIT_1; 1925 newage = fr_tcpidletimeout; /* or fr_tcptimeout? */ 1926 } else if ((flags & TH_OPENING) == TH_OPENING) { 1927 /* 1928 * We see an SA from 'dir' which is already in 1929 * SYN_SENT state, this means we have a 1930 * simultaneous open; SYN_SENT -> SYN_RECEIVED 1931 */ 1932 state[dir] = TCPS_SYN_RECEIVED; 1933 newage = fr_tcptimeout; 1934 } 1935 break; 1936 1937 case TCPS_SYN_RECEIVED: /* 3 */ 1938 if ((flags & (TH_SYN|TH_FIN|TH_ACK)) == TH_ACK) { 1939 /* 1940 * We see an A from 'dir' which was in SYN_RECEIVED 1941 * state so it must now be in established state, 1942 * SYN_RECEIVED -> ESTABLISHED 1943 */ 1944 state[dir] = TCPS_ESTABLISHED; 1945 newage = fr_tcpidletimeout; 1946 } else if ((flags & ~(TH_ECN|TH_CWR)) == TH_OPENING) { 1947 /* 1948 * We see an SA from 'dir' which is already in 1949 * SYN_RECEIVED state. 1950 */ 1951 newage = fr_tcptimeout; 1952 } else if (flags & TH_FIN) { 1953 /* 1954 * We see an F from 'dir' which is in SYN_RECEIVED 1955 * state and wants to close its side of the connection; 1956 * SYN_RECEIVED -> FIN_WAIT_1 1957 */ 1958 state[dir] = TCPS_FIN_WAIT_1; 1959 newage = fr_tcpidletimeout; 1960 } 1961 break; 1962 1963 case TCPS_ESTABLISHED: /* 4 */ 1964 if (flags & TH_FIN) { 1965 /* 1966 * 'dir' closed its side of the connection; this 1967 * gives us a half-closed connection; 1968 * ESTABLISHED -> FIN_WAIT_1 1969 */ 1970 state[dir] = TCPS_FIN_WAIT_1; 1971 newage = fr_tcphalfclosed; 1972 } else if (flags & TH_ACK) { 1973 /* an ACK, should we exclude other flags here? */ 1974 if (ostate == TCPS_FIN_WAIT_1) { 1975 /* 1976 * We know the other side did an active close, 1977 * so we are ACKing the recvd FIN packet (does 1978 * the window matching code guarantee this?) 1979 * and go into CLOSE_WAIT state; this gives us 1980 * a half-closed connection 1981 */ 1982 state[dir] = TCPS_CLOSE_WAIT; 1983 newage = fr_tcphalfclosed; 1984 } else if (ostate < TCPS_CLOSE_WAIT) 1985 /* 1986 * Still a fully established connection, 1987 * reset timeout 1988 */ 1989 newage = fr_tcpidletimeout; 1990 } 1991 break; 1992 1993 case TCPS_CLOSE_WAIT: /* 5 */ 1994 if (flags & TH_FIN) { 1995 /* 1996 * Application closed and 'dir' sent a FIN, we're now 1997 * going into LAST_ACK state 1998 */ 1999 newage = fr_tcplastack; 2000 state[dir] = TCPS_LAST_ACK; 2001 } else { 2002 /* 2003 * We remain in CLOSE_WAIT because the other side has 2004 * closed already and we did not close our side yet; 2005 * reset timeout 2006 */ 2007 newage = fr_tcphalfclosed; 2008 } 2009 break; 2010 2011 case TCPS_FIN_WAIT_1: /* 6 */ 2012 if ((flags & TH_ACK) && ostate > TCPS_CLOSE_WAIT) { 2013 /* 2014 * If the other side is not active anymore it has sent 2015 * us a FIN packet that we are ack'ing now with an ACK; 2016 * this means both sides have now closed the connection 2017 * and we go into TIME_WAIT 2018 */ 2019 /* 2020 * XXX: how do we know we really are ACKing the FIN 2021 * packet here? does the window code guarantee that? 2022 */ 2023 state[dir] = TCPS_TIME_WAIT; 2024 newage = fr_tcptimeout; 2025 } else 2026 /* 2027 * We closed our side of the connection already but the 2028 * other side is still active (ESTABLISHED/CLOSE_WAIT); 2029 * continue with this half-closed connection 2030 */ 2031 newage = fr_tcphalfclosed; 2032 break; 2033 2034 case TCPS_CLOSING: /* 7 */ 2035 /* NOT USED */ 2036 break; 2037 2038 case TCPS_LAST_ACK: /* 8 */ 2039 if (flags & TH_ACK) { 2040 if ((flags & TH_PUSH) || dlen) 2041 /* 2042 * There is still data to be delivered, reset 2043 * timeout 2044 */ 2045 newage = fr_tcplastack; 2046 else 2047 newage = *age; 2048 } 2049 /* 2050 * We cannot detect when we go out of LAST_ACK state to CLOSED 2051 * because that is based on the reception of ACK packets; 2052 * ipfilter can only detect that a packet has been sent by a 2053 * host 2054 */ 2055 break; 2056 2057 case TCPS_FIN_WAIT_2: /* 9 */ 2058 /* NOT USED */ 2059 break; 2060 2061 case TCPS_TIME_WAIT: /* 10 */ 2062 newage = fr_tcptimeout; /* default 4 mins */ 2063 /* we're in 2MSL timeout now */ 2064 break; 2065 } 2066 2067 if (newage != 0) { 2068 *age = newage; 2069 return 0; 2070 } 2071 return -1; 2072} 2073 2074 2075#ifdef IPFILTER_LOG 2076void ipstate_log(is, type) 2077struct ipstate *is; 2078u_int type; 2079{ 2080 struct ipslog ipsl; 2081 void *items[1]; 2082 size_t sizes[1]; 2083 int types[1]; 2084 2085 ipsl.isl_type = type; 2086 ipsl.isl_pkts = is->is_pkts; 2087 ipsl.isl_bytes = is->is_bytes; 2088 ipsl.isl_src = is->is_src; 2089 ipsl.isl_dst = is->is_dst; 2090 ipsl.isl_p = is->is_p; 2091 ipsl.isl_v = is->is_v; 2092 ipsl.isl_flags = is->is_flags; 2093 ipsl.isl_rulen = is->is_rulen; 2094 ipsl.isl_group = is->is_group; 2095 if (ipsl.isl_p == IPPROTO_TCP || ipsl.isl_p == IPPROTO_UDP) { 2096 ipsl.isl_sport = is->is_sport; 2097 ipsl.isl_dport = is->is_dport; 2098 if (ipsl.isl_p == IPPROTO_TCP) { 2099 ipsl.isl_state[0] = is->is_state[0]; 2100 ipsl.isl_state[1] = is->is_state[1]; 2101 } 2102 } else if (ipsl.isl_p == IPPROTO_ICMP) { 2103 ipsl.isl_itype = is->is_icmp.ics_type; 2104 } else if (ipsl.isl_p == IPPROTO_ICMPV6) { 2105 ipsl.isl_itype = is->is_icmp.ics_type; 2106 } else { 2107 ipsl.isl_ps.isl_filler[0] = 0; 2108 ipsl.isl_ps.isl_filler[1] = 0; 2109 } 2110 items[0] = &ipsl; 2111 sizes[0] = sizeof(ipsl); 2112 types[0] = 0; 2113 2114 (void) ipllog(IPL_LOGSTATE, NULL, items, sizes, types, 1); 2115} 2116#endif 2117 2118 2119#ifdef USE_INET6 2120frentry_t *fr_checkicmp6matchingstate(ip, fin) 2121ip6_t *ip; 2122fr_info_t *fin; 2123{ 2124 register ipstate_t *is, **isp; 2125 register u_short sport, dport; 2126 register u_char pr; 2127 struct icmp6_hdr *ic, *oic; 2128 union i6addr dst, src; 2129 u_short savelen; 2130 fr_info_t ofin; 2131 tcphdr_t *tcp; 2132 frentry_t *fr; 2133 ip6_t *oip; 2134 int type; 2135 u_int hv; 2136 2137 /* 2138 * Does it at least have the return (basic) IP header ? 2139 * Only a basic IP header (no options) should be with 2140 * an ICMP error header. 2141 */ 2142 if ((fin->fin_v != 6) || (fin->fin_plen < ICMP6ERR_MINPKTLEN)) 2143 return NULL; 2144 ic = (struct icmp6_hdr *)fin->fin_dp; 2145 type = ic->icmp6_type; 2146 /* 2147 * If it's not an error type, then return 2148 */ 2149 if ((type != ICMP6_DST_UNREACH) && (type != ICMP6_PACKET_TOO_BIG) && 2150 (type != ICMP6_TIME_EXCEEDED) && (type != ICMP6_PARAM_PROB)) 2151 return NULL; 2152 2153 oip = (ip6_t *)((char *)ic + ICMPERR_ICMPHLEN); 2154 if (fin->fin_plen < sizeof(*oip)) 2155 return NULL; 2156 2157 if ((oip->ip6_nxt != IPPROTO_TCP) && (oip->ip6_nxt != IPPROTO_UDP) && 2158 (oip->ip6_nxt != IPPROTO_ICMPV6)) 2159 return NULL; 2160 2161 bzero((char *)&ofin, sizeof(ofin)); 2162 ofin.fin_out = !fin->fin_out; 2163 ofin.fin_ifp = fin->fin_ifp; 2164 ofin.fin_v = 6; 2165 2166 if (oip->ip6_nxt == IPPROTO_ICMPV6) { 2167 oic = (struct icmp6_hdr *)(oip + 1); 2168 /* 2169 * a ICMP error can only be generated as a result of an 2170 * ICMP query, not as the response on an ICMP error 2171 * 2172 * XXX theoretically ICMP_ECHOREP and the other reply's are 2173 * ICMP query's as well, but adding them here seems strange XXX 2174 */ 2175 if (!(oic->icmp6_type & ICMP6_INFOMSG_MASK)) 2176 return NULL; 2177 2178 /* 2179 * perform a lookup of the ICMP packet in the state table 2180 */ 2181 hv = (pr = oip->ip6_nxt); 2182 src.in6 = oip->ip6_src; 2183 hv += src.in4.s_addr; 2184 dst.in6 = oip->ip6_dst; 2185 hv += dst.in4.s_addr; 2186 hv += oic->icmp6_id; 2187 hv += oic->icmp6_seq; 2188 hv %= fr_statesize; 2189 2190 oip->ip6_plen = ntohs(oip->ip6_plen); 2191 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin); 2192 oip->ip6_plen = htons(oip->ip6_plen); 2193 2194 READ_ENTER(&ipf_state); 2195 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) 2196 if ((is->is_p == pr) && 2197 (oic->icmp6_id == is->is_icmp.ics_id) && 2198 (oic->icmp6_seq == is->is_icmp.ics_seq) && 2199 fr_matchsrcdst(is, src, dst, &ofin, NULL)) { 2200 /* 2201 * in the state table ICMP query's are stored 2202 * with the type of the corresponding ICMP 2203 * response. Correct here 2204 */ 2205 if (((is->is_type == ICMP6_ECHO_REPLY) && 2206 (oic->icmp6_type == ICMP6_ECHO_REQUEST)) || 2207 (is->is_type - 1 == oic->icmp6_type )) { 2208 ips_stats.iss_hits++; 2209 is->is_pkts++; 2210 is->is_bytes += fin->fin_plen; 2211 return is->is_rule; 2212 } 2213 } 2214 RWLOCK_EXIT(&ipf_state); 2215 2216 return NULL; 2217 } 2218 2219 tcp = (tcphdr_t *)(oip + 1); 2220 dport = tcp->th_dport; 2221 sport = tcp->th_sport; 2222 2223 hv = (pr = oip->ip6_nxt); 2224 src.in6 = oip->ip6_src; 2225 hv += src.in4.s_addr; 2226 hv += src.i6[1]; 2227 hv += src.i6[2]; 2228 hv += src.i6[3]; 2229 dst.in6 = oip->ip6_dst; 2230 hv += dst.in4.s_addr; 2231 hv += dst.i6[1]; 2232 hv += dst.i6[2]; 2233 hv += dst.i6[3]; 2234 hv += dport; 2235 hv += sport; 2236 hv %= fr_statesize; 2237 /* 2238 * we make an fin entry to be able to feed it to 2239 * matchsrcdst note that not all fields are encessary 2240 * but this is the cleanest way. Note further we fill 2241 * in fin_mp such that if someone uses it we'll get 2242 * a kernel panic. fr_matchsrcdst does not use this. 2243 * 2244 * watch out here, as ip is in host order and oip in network 2245 * order. Any change we make must be undone afterwards. 2246 */ 2247 savelen = oip->ip6_plen; 2248 oip->ip6_plen = ip->ip6_plen - sizeof(*ip) - ICMPERR_ICMPHLEN; 2249 fr_makefrip(sizeof(*oip), (ip_t *)oip, &ofin); 2250 oip->ip6_plen = savelen; 2251 READ_ENTER(&ipf_state); 2252 for (isp = &ips_table[hv]; (is = *isp); isp = &is->is_hnext) { 2253 /* 2254 * Only allow this icmp though if the 2255 * encapsulated packet was allowed through the 2256 * other way around. Note that the minimal amount 2257 * of info present does not allow for checking against 2258 * tcp internals such as seq and ack numbers. 2259 */ 2260 if ((is->is_p == pr) && (is->is_v == 6) && 2261 fr_matchsrcdst(is, src, dst, &ofin, tcp)) { 2262 fr = is->is_rule; 2263 ips_stats.iss_hits++; 2264 is->is_pkts++; 2265 is->is_bytes += fin->fin_plen; 2266 /* 2267 * we deliberately do not touch the timeouts 2268 * for the accompanying state table entry. 2269 * It remains to be seen if that is correct. XXX 2270 */ 2271 RWLOCK_EXIT(&ipf_state); 2272 return fr; 2273 } 2274 } 2275 RWLOCK_EXIT(&ipf_state); 2276 return NULL; 2277} 2278#endif 2279