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