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