1 /* 2 * Copyright (C) 1993-2003 by Darren Reed. 3 * 4 * See the IPFILTER.LICENCE file for details on licencing. 5 * 6 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 7 * Use is subject to license terms. 8 */ 9 10 #if defined(KERNEL) || defined(_KERNEL) 11 # undef KERNEL 12 # undef _KERNEL 13 # define KERNEL 1 14 # define _KERNEL 1 15 #endif 16 #include <sys/errno.h> 17 #include <sys/types.h> 18 #include <sys/param.h> 19 #include <sys/time.h> 20 #if defined(__NetBSD__) 21 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL) 22 # include "opt_ipfilter_log.h" 23 # endif 24 #endif 25 #if defined(_KERNEL) && defined(__FreeBSD_version) && \ 26 (__FreeBSD_version >= 220000) 27 # if (__FreeBSD_version >= 400000) 28 # if !defined(IPFILTER_LKM) 29 # include "opt_inet6.h" 30 # endif 31 # if (__FreeBSD_version == 400019) 32 # define CSUM_DELAY_DATA 33 # endif 34 # endif 35 # include <sys/filio.h> 36 #else 37 # include <sys/ioctl.h> 38 #endif 39 #if !defined(_AIX51) 40 # include <sys/fcntl.h> 41 #endif 42 #if defined(_KERNEL) 43 # include <sys/systm.h> 44 # include <sys/file.h> 45 #else 46 # include <stdio.h> 47 # include <string.h> 48 # include <stdlib.h> 49 # include <stddef.h> 50 # include <sys/file.h> 51 # define _KERNEL 52 # ifdef __OpenBSD__ 53 struct file; 54 # endif 55 # include <sys/uio.h> 56 # undef _KERNEL 57 #endif 58 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \ 59 !defined(linux) 60 # include <sys/mbuf.h> 61 #else 62 # if !defined(linux) 63 # include <sys/byteorder.h> 64 # endif 65 # if (SOLARIS2 < 5) && defined(sun) 66 # include <sys/dditypes.h> 67 # endif 68 #endif 69 #ifdef __hpux 70 # define _NET_ROUTE_INCLUDED 71 #endif 72 #if !defined(linux) 73 # include <sys/protosw.h> 74 #endif 75 #include <sys/socket.h> 76 #include <net/if.h> 77 #ifdef sun 78 # include <net/af.h> 79 #endif 80 #if !defined(_KERNEL) && defined(__FreeBSD__) 81 # include "radix_ipf.h" 82 #endif 83 #include <net/route.h> 84 #include <netinet/in.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #if !defined(linux) 88 # include <netinet/ip_var.h> 89 #endif 90 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */ 91 # include <sys/hashing.h> 92 # include <netinet/in_var.h> 93 #endif 94 #include <netinet/tcp.h> 95 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL) 96 # include <netinet/udp.h> 97 # include <netinet/ip_icmp.h> 98 #endif 99 #ifdef __hpux 100 # undef _NET_ROUTE_INCLUDED 101 #endif 102 #include "netinet/ip_compat.h" 103 #ifdef USE_INET6 104 # include <netinet/icmp6.h> 105 # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux) 106 # include <netinet6/in6_var.h> 107 # endif 108 #endif 109 #include <netinet/tcpip.h> 110 #include "netinet/ip_fil.h" 111 #include "netinet/ip_nat.h" 112 #include "netinet/ip_frag.h" 113 #include "netinet/ip_state.h" 114 #include "netinet/ip_proxy.h" 115 #include "netinet/ip_auth.h" 116 #include "netinet/ipf_stack.h" 117 #ifdef IPFILTER_SCAN 118 # include "netinet/ip_scan.h" 119 #endif 120 #ifdef IPFILTER_SYNC 121 # include "netinet/ip_sync.h" 122 #endif 123 #include "netinet/ip_pool.h" 124 #include "netinet/ip_htable.h" 125 #ifdef IPFILTER_COMPILED 126 # include "netinet/ip_rules.h" 127 #endif 128 #if defined(IPFILTER_BPF) && defined(_KERNEL) 129 # include <net/bpf.h> 130 #endif 131 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000) 132 # include <sys/malloc.h> 133 # if defined(_KERNEL) && !defined(IPFILTER_LKM) 134 # include "opt_ipfilter.h" 135 # endif 136 #endif 137 #include "netinet/ipl.h" 138 /* END OF INCLUDES */ 139 140 #if !defined(lint) 141 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed"; 142 static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $"; 143 #endif 144 145 #ifndef _KERNEL 146 # include "ipf.h" 147 # include "ipt.h" 148 # include "bpf-ipf.h" 149 extern int opts; 150 151 # define FR_VERBOSE(verb_pr) verbose verb_pr 152 # define FR_DEBUG(verb_pr) debug verb_pr 153 #else /* #ifndef _KERNEL */ 154 # define FR_VERBOSE(verb_pr) 155 # define FR_DEBUG(verb_pr) 156 #endif /* _KERNEL */ 157 158 159 char ipfilter_version[] = IPL_VERSION; 160 int fr_features = 0 161 #ifdef IPFILTER_LKM 162 | IPF_FEAT_LKM 163 #endif 164 #ifdef IPFILTER_LOG 165 | IPF_FEAT_LOG 166 #endif 167 #ifdef IPFILTER_LOOKUP 168 | IPF_FEAT_LOOKUP 169 #endif 170 #ifdef IPFILTER_BPF 171 | IPF_FEAT_BPF 172 #endif 173 #ifdef IPFILTER_COMPILED 174 | IPF_FEAT_COMPILED 175 #endif 176 #ifdef IPFILTER_CKSUM 177 | IPF_FEAT_CKSUM 178 #endif 179 #ifdef IPFILTER_SYNC 180 | IPF_FEAT_SYNC 181 #endif 182 #ifdef IPFILTER_SCAN 183 | IPF_FEAT_SCAN 184 #endif 185 #ifdef USE_INET6 186 | IPF_FEAT_IPV6 187 #endif 188 ; 189 190 #define IPF_BUMP(x) (x)++ 191 192 static INLINE int fr_ipfcheck __P((fr_info_t *, frentry_t *, int)); 193 static INLINE int fr_ipfcheck __P((fr_info_t *, frentry_t *, int)); 194 static int fr_portcheck __P((frpcmp_t *, u_short *)); 195 static int frflushlist __P((int, minor_t, int *, frentry_t **, 196 ipf_stack_t *)); 197 static ipfunc_t fr_findfunc __P((ipfunc_t)); 198 static frentry_t *fr_firewall __P((fr_info_t *, u_32_t *)); 199 static int fr_funcinit __P((frentry_t *fr, ipf_stack_t *)); 200 static INLINE void frpr_ah __P((fr_info_t *)); 201 static INLINE void frpr_esp __P((fr_info_t *)); 202 static INLINE void frpr_gre __P((fr_info_t *)); 203 static INLINE void frpr_udp __P((fr_info_t *)); 204 static INLINE void frpr_tcp __P((fr_info_t *)); 205 static INLINE void frpr_icmp __P((fr_info_t *)); 206 static INLINE void frpr_ipv4hdr __P((fr_info_t *)); 207 static INLINE int frpr_pullup __P((fr_info_t *, int)); 208 static INLINE void frpr_short __P((fr_info_t *, int)); 209 static INLINE void frpr_tcpcommon __P((fr_info_t *)); 210 static INLINE void frpr_udpcommon __P((fr_info_t *)); 211 static INLINE int fr_updateipid __P((fr_info_t *)); 212 #ifdef IPFILTER_LOOKUP 213 static int fr_grpmapinit __P((frentry_t *fr, ipf_stack_t *)); 214 static INLINE void *fr_resolvelookup __P((u_int, u_int, lookupfunc_t *, 215 ipf_stack_t *)); 216 #endif 217 static void frsynclist __P((int, int, void *, char *, frentry_t *, 218 ipf_stack_t *)); 219 static void *fr_ifsync __P((int, int, char *, char *, 220 void *, void *, ipf_stack_t *)); 221 static ipftuneable_t *fr_findtunebyname __P((const char *, ipf_stack_t *)); 222 static ipftuneable_t *fr_findtunebycookie __P((void *, void **, ipf_stack_t *)); 223 224 /* 225 * bit values for identifying presence of individual IP options 226 * All of these tables should be ordered by increasing key value on the left 227 * hand side to allow for binary searching of the array and include a trailer 228 * with a 0 for the bitmask for linear searches to easily find the end with. 229 */ 230 const struct optlist ipopts[20] = { 231 { IPOPT_NOP, 0x000001 }, 232 { IPOPT_RR, 0x000002 }, 233 { IPOPT_ZSU, 0x000004 }, 234 { IPOPT_MTUP, 0x000008 }, 235 { IPOPT_MTUR, 0x000010 }, 236 { IPOPT_ENCODE, 0x000020 }, 237 { IPOPT_TS, 0x000040 }, 238 { IPOPT_TR, 0x000080 }, 239 { IPOPT_SECURITY, 0x000100 }, 240 { IPOPT_LSRR, 0x000200 }, 241 { IPOPT_E_SEC, 0x000400 }, 242 { IPOPT_CIPSO, 0x000800 }, 243 { IPOPT_SATID, 0x001000 }, 244 { IPOPT_SSRR, 0x002000 }, 245 { IPOPT_ADDEXT, 0x004000 }, 246 { IPOPT_VISA, 0x008000 }, 247 { IPOPT_IMITD, 0x010000 }, 248 { IPOPT_EIP, 0x020000 }, 249 { IPOPT_FINN, 0x040000 }, 250 { 0, 0x000000 } 251 }; 252 253 #ifdef USE_INET6 254 struct optlist ip6exthdr[] = { 255 { IPPROTO_HOPOPTS, 0x000001 }, 256 { IPPROTO_IPV6, 0x000002 }, 257 { IPPROTO_ROUTING, 0x000004 }, 258 { IPPROTO_FRAGMENT, 0x000008 }, 259 { IPPROTO_ESP, 0x000010 }, 260 { IPPROTO_AH, 0x000020 }, 261 { IPPROTO_NONE, 0x000040 }, 262 { IPPROTO_DSTOPTS, 0x000080 }, 263 { 0, 0 } 264 }; 265 #endif 266 267 struct optlist tcpopts[] = { 268 { TCPOPT_NOP, 0x000001 }, 269 { TCPOPT_MAXSEG, 0x000002 }, 270 { TCPOPT_WINDOW, 0x000004 }, 271 { TCPOPT_SACK_PERMITTED, 0x000008 }, 272 { TCPOPT_SACK, 0x000010 }, 273 { TCPOPT_TIMESTAMP, 0x000020 }, 274 { 0, 0x000000 } 275 }; 276 277 /* 278 * bit values for identifying presence of individual IP security options 279 */ 280 const struct optlist secopt[8] = { 281 { IPSO_CLASS_RES4, 0x01 }, 282 { IPSO_CLASS_TOPS, 0x02 }, 283 { IPSO_CLASS_SECR, 0x04 }, 284 { IPSO_CLASS_RES3, 0x08 }, 285 { IPSO_CLASS_CONF, 0x10 }, 286 { IPSO_CLASS_UNCL, 0x20 }, 287 { IPSO_CLASS_RES2, 0x40 }, 288 { IPSO_CLASS_RES1, 0x80 } 289 }; 290 291 292 /* 293 * Table of functions available for use with call rules. 294 */ 295 static ipfunc_resolve_t fr_availfuncs[] = { 296 #ifdef IPFILTER_LOOKUP 297 { "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit }, 298 { "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit }, 299 #endif 300 { "", NULL } 301 }; 302 303 304 /* 305 * Below we declare a list of constants used only by the ipf_extraflush() 306 * routine. We are placing it here, instead of in ipf_extraflush() itself, 307 * because we want to make it visible to tools such as mdb, nm etc., so the 308 * values can easily be altered during debugging. 309 */ 310 static const int idletime_tab[] = { 311 IPF_TTLVAL(30), /* 30 seconds */ 312 IPF_TTLVAL(1800), /* 30 minutes */ 313 IPF_TTLVAL(43200), /* 12 hours */ 314 IPF_TTLVAL(345600), /* 4 days */ 315 }; 316 317 318 /* 319 * The next section of code is a a collection of small routines that set 320 * fields in the fr_info_t structure passed based on properties of the 321 * current packet. There are different routines for the same protocol 322 * for each of IPv4 and IPv6. Adding a new protocol, for which there 323 * will "special" inspection for setup, is now more easily done by adding 324 * a new routine and expanding the frpr_ipinit*() function rather than by 325 * adding more code to a growing switch statement. 326 */ 327 #ifdef USE_INET6 328 static INLINE int frpr_ah6 __P((fr_info_t *)); 329 static INLINE void frpr_esp6 __P((fr_info_t *)); 330 static INLINE void frpr_gre6 __P((fr_info_t *)); 331 static INLINE void frpr_udp6 __P((fr_info_t *)); 332 static INLINE void frpr_tcp6 __P((fr_info_t *)); 333 static INLINE void frpr_icmp6 __P((fr_info_t *)); 334 static INLINE void frpr_ipv6hdr __P((fr_info_t *)); 335 static INLINE void frpr_short6 __P((fr_info_t *, int)); 336 static INLINE int frpr_hopopts6 __P((fr_info_t *)); 337 static INLINE int frpr_routing6 __P((fr_info_t *)); 338 static INLINE int frpr_dstopts6 __P((fr_info_t *)); 339 static INLINE int frpr_fragment6 __P((fr_info_t *)); 340 static INLINE int frpr_ipv6exthdr __P((fr_info_t *, int, int)); 341 342 343 /* ------------------------------------------------------------------------ */ 344 /* Function: frpr_short6 */ 345 /* Returns: void */ 346 /* Parameters: fin(I) - pointer to packet information */ 347 /* */ 348 /* IPv6 Only */ 349 /* This is function enforces the 'is a packet too short to be legit' rule */ 350 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */ 351 /* for frpr_short() for more details. */ 352 /* ------------------------------------------------------------------------ */ 353 static INLINE void frpr_short6(fin, xmin) 354 fr_info_t *fin; 355 int xmin; 356 { 357 358 if (fin->fin_dlen < xmin) 359 fin->fin_flx |= FI_SHORT; 360 } 361 362 363 /* ------------------------------------------------------------------------ */ 364 /* Function: frpr_ipv6hdr */ 365 /* Returns: Nil */ 366 /* Parameters: fin(I) - pointer to packet information */ 367 /* */ 368 /* IPv6 Only */ 369 /* Copy values from the IPv6 header into the fr_info_t struct and call the */ 370 /* per-protocol analyzer if it exists. */ 371 /* ------------------------------------------------------------------------ */ 372 static INLINE void frpr_ipv6hdr(fin) 373 fr_info_t *fin; 374 { 375 ip6_t *ip6 = (ip6_t *)fin->fin_ip; 376 int p, go = 1, i, hdrcount; 377 fr_ip_t *fi = &fin->fin_fi; 378 379 fin->fin_off = 0; 380 381 fi->fi_tos = 0; 382 fi->fi_optmsk = 0; 383 fi->fi_secmsk = 0; 384 fi->fi_auth = 0; 385 386 p = ip6->ip6_nxt; 387 fi->fi_ttl = ip6->ip6_hlim; 388 fi->fi_src.in6 = ip6->ip6_src; 389 fi->fi_dst.in6 = ip6->ip6_dst; 390 fin->fin_id = 0; 391 392 hdrcount = 0; 393 while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) { 394 switch (p) 395 { 396 case IPPROTO_UDP : 397 frpr_udp6(fin); 398 go = 0; 399 break; 400 401 case IPPROTO_TCP : 402 frpr_tcp6(fin); 403 go = 0; 404 break; 405 406 case IPPROTO_ICMPV6 : 407 frpr_icmp6(fin); 408 go = 0; 409 break; 410 411 case IPPROTO_GRE : 412 frpr_gre6(fin); 413 go = 0; 414 break; 415 416 case IPPROTO_HOPOPTS : 417 /* 418 * hop by hop ext header is only allowed 419 * right after IPv6 header. 420 */ 421 if (hdrcount != 0) { 422 fin->fin_flx |= FI_BAD; 423 p = IPPROTO_NONE; 424 } else { 425 p = frpr_hopopts6(fin); 426 } 427 break; 428 429 case IPPROTO_DSTOPTS : 430 p = frpr_dstopts6(fin); 431 break; 432 433 case IPPROTO_ROUTING : 434 p = frpr_routing6(fin); 435 break; 436 437 case IPPROTO_AH : 438 p = frpr_ah6(fin); 439 break; 440 441 case IPPROTO_ESP : 442 frpr_esp6(fin); 443 go = 0; 444 break; 445 446 case IPPROTO_IPV6 : 447 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 448 if (ip6exthdr[i].ol_val == p) { 449 fin->fin_flx |= ip6exthdr[i].ol_bit; 450 break; 451 } 452 go = 0; 453 break; 454 455 case IPPROTO_NONE : 456 go = 0; 457 break; 458 459 case IPPROTO_FRAGMENT : 460 p = frpr_fragment6(fin); 461 if (fin->fin_off != 0) /* Not the first frag */ 462 go = 0; 463 break; 464 465 default : 466 go = 0; 467 break; 468 } 469 hdrcount++; 470 471 /* 472 * It is important to note that at this point, for the 473 * extension headers (go != 0), the entire header may not have 474 * been pulled up when the code gets to this point. This is 475 * only done for "go != 0" because the other header handlers 476 * will all pullup their complete header. The other indicator 477 * of an incomplete packet is that this was just an extension 478 * header. 479 */ 480 if ((go != 0) && (p != IPPROTO_NONE) && 481 (frpr_pullup(fin, 0) == -1)) { 482 p = IPPROTO_NONE; 483 go = 0; 484 } 485 } 486 fi->fi_p = p; 487 } 488 489 490 /* ------------------------------------------------------------------------ */ 491 /* Function: frpr_ipv6exthdr */ 492 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 493 /* Parameters: fin(I) - pointer to packet information */ 494 /* multiple(I) - flag indicating yes/no if multiple occurances */ 495 /* of this extension header are allowed. */ 496 /* proto(I) - protocol number for this extension header */ 497 /* */ 498 /* IPv6 Only */ 499 /* This function expects to find an IPv6 extension header at fin_dp. */ 500 /* There must be at least 8 bytes of data at fin_dp for there to be a valid */ 501 /* extension header present. If a good one is found, fin_dp is advanced to */ 502 /* point at the first piece of data after the extension header, fin_exthdr */ 503 /* points to the start of the extension header and the "protocol" of the */ 504 /* *NEXT* header is returned. */ 505 /* ------------------------------------------------------------------------ */ 506 static INLINE int frpr_ipv6exthdr(fin, multiple, proto) 507 fr_info_t *fin; 508 int multiple, proto; 509 { 510 struct ip6_ext *hdr; 511 u_short shift; 512 int i; 513 514 fin->fin_flx |= FI_V6EXTHDR; 515 516 /* 8 is default length of extension hdr */ 517 if ((fin->fin_dlen - 8) < 0) { 518 fin->fin_flx |= FI_SHORT; 519 return IPPROTO_NONE; 520 } 521 522 if (frpr_pullup(fin, 8) == -1) 523 return IPPROTO_NONE; 524 525 hdr = fin->fin_dp; 526 shift = 8 + (hdr->ip6e_len << 3); 527 if (shift > fin->fin_dlen) { /* Nasty extension header length? */ 528 fin->fin_flx |= FI_BAD; 529 return IPPROTO_NONE; 530 } 531 532 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 533 if (ip6exthdr[i].ol_val == proto) { 534 /* 535 * Most IPv6 extension headers are only allowed once. 536 */ 537 if ((multiple == 0) && 538 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) 539 fin->fin_flx |= FI_BAD; 540 else 541 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 542 break; 543 } 544 545 fin->fin_dp = (char *)fin->fin_dp + shift; 546 fin->fin_dlen -= shift; 547 548 return hdr->ip6e_nxt; 549 } 550 551 552 /* ------------------------------------------------------------------------ */ 553 /* Function: frpr_hopopts6 */ 554 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 555 /* Parameters: fin(I) - pointer to packet information */ 556 /* */ 557 /* IPv6 Only */ 558 /* This is function checks pending hop by hop options extension header */ 559 /* ------------------------------------------------------------------------ */ 560 static INLINE int frpr_hopopts6(fin) 561 fr_info_t *fin; 562 { 563 return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 564 } 565 566 567 /* ------------------------------------------------------------------------ */ 568 /* Function: frpr_routing6 */ 569 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 570 /* Parameters: fin(I) - pointer to packet information */ 571 /* */ 572 /* IPv6 Only */ 573 /* This is function checks pending routing extension header */ 574 /* ------------------------------------------------------------------------ */ 575 static INLINE int frpr_routing6(fin) 576 fr_info_t *fin; 577 { 578 struct ip6_ext *hdr; 579 int shift; 580 581 hdr = fin->fin_dp; 582 if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE) 583 return IPPROTO_NONE; 584 585 shift = 8 + (hdr->ip6e_len << 3); 586 /* 587 * Nasty extension header length? 588 */ 589 if ((hdr->ip6e_len << 3) & 15) { 590 fin->fin_flx |= FI_BAD; 591 /* 592 * Compensate for the changes made in frpr_ipv6exthdr() 593 */ 594 fin->fin_dlen += shift; 595 fin->fin_dp = (char *)fin->fin_dp - shift; 596 return IPPROTO_NONE; 597 } 598 599 return hdr->ip6e_nxt; 600 } 601 602 603 /* ------------------------------------------------------------------------ */ 604 /* Function: frpr_fragment6 */ 605 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 606 /* Parameters: fin(I) - pointer to packet information */ 607 /* */ 608 /* IPv6 Only */ 609 /* Examine the IPv6 fragment header and extract fragment offset information.*/ 610 /* */ 611 /* We don't know where the transport layer header (or whatever is next is), */ 612 /* as it could be behind destination options (amongst others). Because */ 613 /* there is no fragment cache, there is no knowledge about whether or not an*/ 614 /* upper layer header has been seen (or where it ends) and thus we are not */ 615 /* able to continue processing beyond this header with any confidence. */ 616 /* ------------------------------------------------------------------------ */ 617 static INLINE int frpr_fragment6(fin) 618 fr_info_t *fin; 619 { 620 struct ip6_frag *frag; 621 int extoff = 0; 622 623 fin->fin_flx |= FI_FRAG; 624 625 /* 626 * A fragmented IPv6 packet implies that there must be something 627 * else after the fragment. 628 */ 629 if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE) 630 goto badv6frag; 631 632 if (frpr_pullup(fin, sizeof(*frag)) == -1) 633 goto badv6frag; 634 635 frag = (struct ip6_frag *)((char *)fin->fin_dp - sizeof(*frag)); 636 /* 637 * Fragment but no fragmentation info set? Bad packet... 638 */ 639 if (frag->ip6f_offlg == 0) 640 goto badv6frag; 641 642 fin->fin_id = frag->ip6f_ident; 643 fin->fin_dp = (char *)frag + sizeof(*frag); 644 fin->fin_dlen -= sizeof(*frag); 645 /* length of hdrs(after frag hdr) + data */ 646 extoff = sizeof(*frag); 647 648 /* 649 * If the frag is not the last one and the payload length 650 * is not multiple of 8, it must be dropped. 651 */ 652 if (((frag->ip6f_offlg & IP6F_MORE_FRAG) && (fin->fin_dlen & 7)) || 653 (frag->ip6f_offlg == 0)) { 654 badv6frag: 655 fin->fin_dp = (char *)fin->fin_dp - extoff; 656 fin->fin_dlen += extoff; 657 fin->fin_flx |= FI_BAD; 658 return IPPROTO_NONE; 659 } 660 661 fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK); 662 if (fin->fin_off != 0) 663 fin->fin_flx |= FI_FRAGBODY; 664 665 if (frag->ip6f_offlg & IP6F_MORE_FRAG) 666 fin->fin_flx |= FI_MOREFRAG; 667 668 return frag->ip6f_nxt; 669 } 670 671 672 /* ------------------------------------------------------------------------ */ 673 /* Function: frpr_dstopts6 */ 674 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 675 /* Parameters: fin(I) - pointer to packet information */ 676 /* nextheader(I) - stores next header value */ 677 /* */ 678 /* IPv6 Only */ 679 /* This is function checks pending destination options extension header */ 680 /* ------------------------------------------------------------------------ */ 681 static INLINE int frpr_dstopts6(fin) 682 fr_info_t *fin; 683 { 684 return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS); 685 } 686 687 688 /* ------------------------------------------------------------------------ */ 689 /* Function: frpr_icmp6 */ 690 /* Returns: void */ 691 /* Parameters: fin(I) - pointer to packet information */ 692 /* */ 693 /* IPv6 Only */ 694 /* This routine is mainly concerned with determining the minimum valid size */ 695 /* for an ICMPv6 packet. */ 696 /* ------------------------------------------------------------------------ */ 697 static INLINE void frpr_icmp6(fin) 698 fr_info_t *fin; 699 { 700 int minicmpsz = sizeof(struct icmp6_hdr); 701 struct icmp6_hdr *icmp6; 702 703 if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) 704 return; 705 706 if (fin->fin_dlen > 1) { 707 icmp6 = fin->fin_dp; 708 709 fin->fin_data[0] = *(u_short *)icmp6; 710 711 if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0) 712 fin->fin_flx |= FI_ICMPQUERY; 713 714 switch (icmp6->icmp6_type) 715 { 716 case ICMP6_ECHO_REPLY : 717 case ICMP6_ECHO_REQUEST : 718 if (fin->fin_dlen >= 6) 719 fin->fin_data[1] = icmp6->icmp6_id; 720 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t); 721 break; 722 case ICMP6_DST_UNREACH : 723 case ICMP6_PACKET_TOO_BIG : 724 case ICMP6_TIME_EXCEEDED : 725 case ICMP6_PARAM_PROB : 726 if ((fin->fin_m != NULL) && 727 (M_LEN(fin->fin_m) < fin->fin_plen)) { 728 if (fr_coalesce(fin) != 1) 729 return; 730 } 731 fin->fin_flx |= FI_ICMPERR; 732 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t); 733 break; 734 default : 735 break; 736 } 737 } 738 739 frpr_short6(fin, minicmpsz); 740 } 741 742 743 /* ------------------------------------------------------------------------ */ 744 /* Function: frpr_udp6 */ 745 /* Returns: void */ 746 /* Parameters: fin(I) - pointer to packet information */ 747 /* */ 748 /* IPv6 Only */ 749 /* Analyse the packet for IPv6/UDP properties. */ 750 /* Is not expected to be called for fragmented packets. */ 751 /* ------------------------------------------------------------------------ */ 752 static INLINE void frpr_udp6(fin) 753 fr_info_t *fin; 754 { 755 756 fr_checkv6sum(fin); 757 758 frpr_short6(fin, sizeof(struct udphdr)); 759 if (frpr_pullup(fin, sizeof(struct udphdr)) == -1) 760 return; 761 762 frpr_udpcommon(fin); 763 } 764 765 766 /* ------------------------------------------------------------------------ */ 767 /* Function: frpr_tcp6 */ 768 /* Returns: void */ 769 /* Parameters: fin(I) - pointer to packet information */ 770 /* */ 771 /* IPv6 Only */ 772 /* Analyse the packet for IPv6/TCP properties. */ 773 /* Is not expected to be called for fragmented packets. */ 774 /* ------------------------------------------------------------------------ */ 775 static INLINE void frpr_tcp6(fin) 776 fr_info_t *fin; 777 { 778 779 fr_checkv6sum(fin); 780 781 frpr_short6(fin, sizeof(struct tcphdr)); 782 if (frpr_pullup(fin, sizeof(struct tcphdr)) == -1) 783 return; 784 785 frpr_tcpcommon(fin); 786 } 787 788 789 /* ------------------------------------------------------------------------ */ 790 /* Function: frpr_esp6 */ 791 /* Returns: void */ 792 /* Parameters: fin(I) - pointer to packet information */ 793 /* */ 794 /* IPv6 Only */ 795 /* Analyse the packet for ESP properties. */ 796 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 797 /* even though the newer ESP packets must also have a sequence number that */ 798 /* is 32bits as well, it is not possible(?) to determine the version from a */ 799 /* simple packet header. */ 800 /* ------------------------------------------------------------------------ */ 801 static INLINE void frpr_esp6(fin) 802 fr_info_t *fin; 803 { 804 int i; 805 frpr_short6(fin, sizeof(grehdr_t)); 806 807 (void) frpr_pullup(fin, 8); 808 809 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 810 if (ip6exthdr[i].ol_val == IPPROTO_ESP) { 811 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 812 break; 813 } 814 } 815 816 817 /* ------------------------------------------------------------------------ */ 818 /* Function: frpr_ah6 */ 819 /* Returns: void */ 820 /* Parameters: fin(I) - pointer to packet information */ 821 /* */ 822 /* IPv6 Only */ 823 /* Analyse the packet for AH properties. */ 824 /* The minimum length is taken to be the combination of all fields in the */ 825 /* header being present and no authentication data (null algorithm used.) */ 826 /* ------------------------------------------------------------------------ */ 827 static INLINE int frpr_ah6(fin) 828 fr_info_t *fin; 829 { 830 authhdr_t *ah; 831 int i, shift; 832 833 frpr_short6(fin, 12); 834 835 if (frpr_pullup(fin, sizeof(*ah)) == -1) 836 return IPPROTO_NONE; 837 838 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 839 if (ip6exthdr[i].ol_val == IPPROTO_AH) { 840 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 841 break; 842 } 843 844 ah = (authhdr_t *)fin->fin_dp; 845 846 shift = (ah->ah_plen + 2) * 4; 847 fin->fin_dlen -= shift; 848 fin->fin_dp = (char*)fin->fin_dp + shift; 849 850 return ah->ah_next; 851 } 852 853 854 /* ------------------------------------------------------------------------ */ 855 /* Function: frpr_gre6 */ 856 /* Returns: void */ 857 /* Parameters: fin(I) - pointer to packet information */ 858 /* */ 859 /* Analyse the packet for GRE properties. */ 860 /* ------------------------------------------------------------------------ */ 861 static INLINE void frpr_gre6(fin) 862 fr_info_t *fin; 863 { 864 grehdr_t *gre; 865 866 frpr_short6(fin, sizeof(grehdr_t)); 867 868 if (frpr_pullup(fin, sizeof(grehdr_t)) == -1) 869 return; 870 871 gre = fin->fin_dp; 872 if (GRE_REV(gre->gr_flags) == 1) 873 fin->fin_data[0] = gre->gr_call; 874 } 875 #endif /* USE_INET6 */ 876 877 878 /* ------------------------------------------------------------------------ */ 879 /* Function: frpr_pullup */ 880 /* Returns: int - 0 == pullup succeeded, -1 == failure */ 881 /* Parameters: fin(I) - pointer to packet information */ 882 /* plen(I) - length (excluding L3 header) to pullup */ 883 /* */ 884 /* Short inline function to cut down on code duplication to perform a call */ 885 /* to fr_pullup to ensure there is the required amount of data, */ 886 /* consecutively in the packet buffer. */ 887 /* ------------------------------------------------------------------------ */ 888 static INLINE int frpr_pullup(fin, plen) 889 fr_info_t *fin; 890 int plen; 891 { 892 #if defined(_KERNEL) 893 if (fin->fin_m != NULL) { 894 int ipoff; 895 896 ipoff = (char *)fin->fin_ip - MTOD(fin->fin_m, char *); 897 898 if (fin->fin_dp != NULL) 899 plen += (char *)fin->fin_dp - 900 ((char *)fin->fin_ip + fin->fin_hlen); 901 plen += fin->fin_hlen; 902 /* 903 * We don't do 'plen += ipoff;' here. The fr_pullup() will 904 * do it for us. 905 */ 906 if (M_LEN(fin->fin_m) < plen + ipoff) { 907 if (fr_pullup(fin->fin_m, fin, plen) == NULL) 908 return -1; 909 } 910 } 911 #endif 912 return 0; 913 } 914 915 916 /* ------------------------------------------------------------------------ */ 917 /* Function: frpr_short */ 918 /* Returns: void */ 919 /* Parameters: fin(I) - pointer to packet information */ 920 /* xmin(I) - minimum header size */ 921 /* */ 922 /* Check if a packet is "short" as defined by xmin. The rule we are */ 923 /* applying here is that the packet must not be fragmented within the layer */ 924 /* 4 header. That is, it must not be a fragment that has its offset set to */ 925 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */ 926 /* entire layer 4 header must be present (min). */ 927 /* ------------------------------------------------------------------------ */ 928 static INLINE void frpr_short(fin, xmin) 929 fr_info_t *fin; 930 int xmin; 931 { 932 933 if (fin->fin_off == 0) { 934 if (fin->fin_dlen < xmin) 935 fin->fin_flx |= FI_SHORT; 936 } else if (fin->fin_off < xmin) { 937 fin->fin_flx |= FI_SHORT; 938 } 939 } 940 941 942 /* ------------------------------------------------------------------------ */ 943 /* Function: frpr_icmp */ 944 /* Returns: void */ 945 /* Parameters: fin(I) - pointer to packet information */ 946 /* */ 947 /* IPv4 Only */ 948 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */ 949 /* except extrememly bad packets, both type and code will be present. */ 950 /* The expected minimum size of an ICMP packet is very much dependent on */ 951 /* the type of it. */ 952 /* */ 953 /* XXX - other ICMP sanity checks? */ 954 /* ------------------------------------------------------------------------ */ 955 static INLINE void frpr_icmp(fin) 956 fr_info_t *fin; 957 { 958 int minicmpsz = sizeof(struct icmp); 959 icmphdr_t *icmp; 960 ip_t *oip; 961 ipf_stack_t *ifs = fin->fin_ifs; 962 963 if (fin->fin_off != 0) { 964 frpr_short(fin, ICMPERR_ICMPHLEN); 965 return; 966 } 967 968 if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1) 969 return; 970 971 fr_checkv4sum(fin); 972 973 /* 974 * This is a right place to set icmp pointer, since the memory 975 * referenced by fin_dp could get reallocated. The code down below can 976 * rely on fact icmp variable always points to ICMP header. 977 */ 978 icmp = fin->fin_dp; 979 fin->fin_data[0] = *(u_short *)icmp; 980 fin->fin_data[1] = icmp->icmp_id; 981 982 switch (icmp->icmp_type) 983 { 984 case ICMP_ECHOREPLY : 985 case ICMP_ECHO : 986 /* Router discovery messaes - RFC 1256 */ 987 case ICMP_ROUTERADVERT : 988 case ICMP_ROUTERSOLICIT : 989 minicmpsz = ICMP_MINLEN; 990 break; 991 /* 992 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 993 * 3 * timestamp(3 * 4) 994 */ 995 case ICMP_TSTAMP : 996 case ICMP_TSTAMPREPLY : 997 minicmpsz = 20; 998 break; 999 /* 1000 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 1001 * mask(4) 1002 */ 1003 case ICMP_MASKREQ : 1004 case ICMP_MASKREPLY : 1005 minicmpsz = 12; 1006 break; 1007 /* 1008 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+) 1009 */ 1010 case ICMP_UNREACH : 1011 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) { 1012 if (icmp->icmp_nextmtu < ifs->ifs_fr_icmpminfragmtu) 1013 fin->fin_flx |= FI_BAD; 1014 } 1015 /* FALLTHRU */ 1016 case ICMP_SOURCEQUENCH : 1017 case ICMP_REDIRECT : 1018 case ICMP_TIMXCEED : 1019 case ICMP_PARAMPROB : 1020 fin->fin_flx |= FI_ICMPERR; 1021 if (fr_coalesce(fin) != 1) 1022 return; 1023 /* 1024 * ICMP error packets should not be generated for IP 1025 * packets that are a fragment that isn't the first 1026 * fragment. 1027 */ 1028 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN); 1029 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) 1030 fin->fin_flx |= FI_BAD; 1031 break; 1032 default : 1033 break; 1034 } 1035 1036 frpr_short(fin, minicmpsz); 1037 } 1038 1039 1040 /* ------------------------------------------------------------------------ */ 1041 /* Function: frpr_tcpcommon */ 1042 /* Returns: void */ 1043 /* Parameters: fin(I) - pointer to packet information */ 1044 /* */ 1045 /* TCP header sanity checking. Look for bad combinations of TCP flags, */ 1046 /* and make some checks with how they interact with other fields. */ 1047 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */ 1048 /* valid and mark the packet as bad if not. */ 1049 /* ------------------------------------------------------------------------ */ 1050 static INLINE void frpr_tcpcommon(fin) 1051 fr_info_t *fin; 1052 { 1053 int flags, tlen; 1054 tcphdr_t *tcp; 1055 1056 fin->fin_flx |= FI_TCPUDP; 1057 if (fin->fin_off != 0) 1058 return; 1059 1060 if (frpr_pullup(fin, sizeof(*tcp)) == -1) 1061 return; 1062 tcp = fin->fin_dp; 1063 1064 if (fin->fin_dlen > 3) { 1065 fin->fin_sport = ntohs(tcp->th_sport); 1066 fin->fin_dport = ntohs(tcp->th_dport); 1067 } 1068 1069 if ((fin->fin_flx & FI_SHORT) != 0) 1070 return; 1071 1072 /* 1073 * Use of the TCP data offset *must* result in a value that is at 1074 * least the same size as the TCP header. 1075 */ 1076 tlen = TCP_OFF(tcp) << 2; 1077 if (tlen < sizeof(tcphdr_t)) { 1078 fin->fin_flx |= FI_BAD; 1079 return; 1080 } 1081 1082 flags = tcp->th_flags; 1083 fin->fin_tcpf = tcp->th_flags; 1084 1085 /* 1086 * If the urgent flag is set, then the urgent pointer must 1087 * also be set and vice versa. Good TCP packets do not have 1088 * just one of these set. 1089 */ 1090 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) { 1091 fin->fin_flx |= FI_BAD; 1092 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) { 1093 /* Ignore this case, it shows up in "real" traffic with */ 1094 /* bogus values in the urgent pointer field. */ 1095 flags = flags; /* LINT */ 1096 } else if (((flags & (TH_SYN|TH_FIN)) != 0) && 1097 ((flags & (TH_RST|TH_ACK)) == TH_RST)) { 1098 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */ 1099 fin->fin_flx |= FI_BAD; 1100 } else if (!(flags & TH_ACK)) { 1101 /* 1102 * If the ack bit isn't set, then either the SYN or 1103 * RST bit must be set. If the SYN bit is set, then 1104 * we expect the ACK field to be 0. If the ACK is 1105 * not set and if URG, PSH or FIN are set, consdier 1106 * that to indicate a bad TCP packet. 1107 */ 1108 if ((flags == TH_SYN) && (tcp->th_ack != 0)) { 1109 /* 1110 * Cisco PIX sets the ACK field to a random value. 1111 * In light of this, do not set FI_BAD until a patch 1112 * is available from Cisco to ensure that 1113 * interoperability between existing systems is 1114 * achieved. 1115 */ 1116 /*fin->fin_flx |= FI_BAD*/; 1117 flags = flags; /* LINT */ 1118 } else if (!(flags & (TH_RST|TH_SYN))) { 1119 fin->fin_flx |= FI_BAD; 1120 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) { 1121 fin->fin_flx |= FI_BAD; 1122 } 1123 } 1124 1125 /* 1126 * At this point, it's not exactly clear what is to be gained by 1127 * marking up which TCP options are and are not present. The one we 1128 * are most interested in is the TCP window scale. This is only in 1129 * a SYN packet [RFC1323] so we don't need this here...? 1130 * Now if we were to analyse the header for passive fingerprinting, 1131 * then that might add some weight to adding this... 1132 */ 1133 if (tlen == sizeof(tcphdr_t)) 1134 return; 1135 1136 if (frpr_pullup(fin, tlen) == -1) 1137 return; 1138 1139 #if 0 1140 ip = fin->fin_ip; 1141 s = (u_char *)(tcp + 1); 1142 off = IP_HL(ip) << 2; 1143 # ifdef _KERNEL 1144 if (fin->fin_mp != NULL) { 1145 mb_t *m = *fin->fin_mp; 1146 1147 if (off + tlen > M_LEN(m)) 1148 return; 1149 } 1150 # endif 1151 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) { 1152 opt = *s; 1153 if (opt == '\0') 1154 break; 1155 else if (opt == TCPOPT_NOP) 1156 ol = 1; 1157 else { 1158 if (tlen < 2) 1159 break; 1160 ol = (int)*(s + 1); 1161 if (ol < 2 || ol > tlen) 1162 break; 1163 } 1164 1165 for (i = 9, mv = 4; mv >= 0; ) { 1166 op = ipopts + i; 1167 if (opt == (u_char)op->ol_val) { 1168 optmsk |= op->ol_bit; 1169 break; 1170 } 1171 } 1172 tlen -= ol; 1173 s += ol; 1174 } 1175 #endif /* 0 */ 1176 } 1177 1178 1179 1180 /* ------------------------------------------------------------------------ */ 1181 /* Function: frpr_udpcommon */ 1182 /* Returns: void */ 1183 /* Parameters: fin(I) - pointer to packet information */ 1184 /* */ 1185 /* Extract the UDP source and destination ports, if present. If compiled */ 1186 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */ 1187 /* ------------------------------------------------------------------------ */ 1188 static INLINE void frpr_udpcommon(fin) 1189 fr_info_t *fin; 1190 { 1191 udphdr_t *udp; 1192 1193 fin->fin_flx |= FI_TCPUDP; 1194 1195 if (!fin->fin_off && (fin->fin_dlen > 3)) { 1196 if (frpr_pullup(fin, sizeof(*udp)) == -1) { 1197 fin->fin_flx |= FI_SHORT; 1198 return; 1199 } 1200 1201 udp = fin->fin_dp; 1202 1203 fin->fin_sport = ntohs(udp->uh_sport); 1204 fin->fin_dport = ntohs(udp->uh_dport); 1205 } 1206 } 1207 1208 1209 /* ------------------------------------------------------------------------ */ 1210 /* Function: frpr_tcp */ 1211 /* Returns: void */ 1212 /* Parameters: fin(I) - pointer to packet information */ 1213 /* */ 1214 /* IPv4 Only */ 1215 /* Analyse the packet for IPv4/TCP properties. */ 1216 /* ------------------------------------------------------------------------ */ 1217 static INLINE void frpr_tcp(fin) 1218 fr_info_t *fin; 1219 { 1220 1221 fr_checkv4sum(fin); 1222 1223 frpr_short(fin, sizeof(tcphdr_t)); 1224 1225 frpr_tcpcommon(fin); 1226 } 1227 1228 1229 /* ------------------------------------------------------------------------ */ 1230 /* Function: frpr_udp */ 1231 /* Returns: void */ 1232 /* Parameters: fin(I) - pointer to packet information */ 1233 /* */ 1234 /* IPv4 Only */ 1235 /* Analyse the packet for IPv4/UDP properties. */ 1236 /* ------------------------------------------------------------------------ */ 1237 static INLINE void frpr_udp(fin) 1238 fr_info_t *fin; 1239 { 1240 1241 fr_checkv4sum(fin); 1242 1243 frpr_short(fin, sizeof(udphdr_t)); 1244 1245 frpr_udpcommon(fin); 1246 } 1247 1248 1249 /* ------------------------------------------------------------------------ */ 1250 /* Function: frpr_esp */ 1251 /* Returns: void */ 1252 /* Parameters: fin(I) - pointer to packet information */ 1253 /* */ 1254 /* Analyse the packet for ESP properties. */ 1255 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 1256 /* even though the newer ESP packets must also have a sequence number that */ 1257 /* is 32bits as well, it is not possible(?) to determine the version from a */ 1258 /* simple packet header. */ 1259 /* ------------------------------------------------------------------------ */ 1260 static INLINE void frpr_esp(fin) 1261 fr_info_t *fin; 1262 { 1263 if ((fin->fin_off == 0) && (frpr_pullup(fin, 8) == -1)) 1264 return; 1265 1266 frpr_short(fin, 8); 1267 } 1268 1269 1270 /* ------------------------------------------------------------------------ */ 1271 /* Function: frpr_ah */ 1272 /* Returns: void */ 1273 /* Parameters: fin(I) - pointer to packet information */ 1274 /* */ 1275 /* Analyse the packet for AH properties. */ 1276 /* The minimum length is taken to be the combination of all fields in the */ 1277 /* header being present and no authentication data (null algorithm used.) */ 1278 /* ------------------------------------------------------------------------ */ 1279 static INLINE void frpr_ah(fin) 1280 fr_info_t *fin; 1281 { 1282 authhdr_t *ah; 1283 int len; 1284 1285 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(*ah)) == -1)) 1286 return; 1287 1288 ah = (authhdr_t *)fin->fin_dp; 1289 1290 len = (ah->ah_plen + 2) << 2; 1291 frpr_short(fin, len); 1292 } 1293 1294 1295 /* ------------------------------------------------------------------------ */ 1296 /* Function: frpr_gre */ 1297 /* Returns: void */ 1298 /* Parameters: fin(I) - pointer to packet information */ 1299 /* */ 1300 /* Analyse the packet for GRE properties. */ 1301 /* ------------------------------------------------------------------------ */ 1302 static INLINE void frpr_gre(fin) 1303 fr_info_t *fin; 1304 { 1305 grehdr_t *gre; 1306 1307 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(grehdr_t)) == -1)) 1308 return; 1309 1310 frpr_short(fin, sizeof(grehdr_t)); 1311 1312 if (fin->fin_off == 0) { 1313 gre = fin->fin_dp; 1314 if (GRE_REV(gre->gr_flags) == 1) 1315 fin->fin_data[0] = gre->gr_call; 1316 } 1317 } 1318 1319 1320 /* ------------------------------------------------------------------------ */ 1321 /* Function: frpr_ipv4hdr */ 1322 /* Returns: void */ 1323 /* Parameters: fin(I) - pointer to packet information */ 1324 /* */ 1325 /* IPv4 Only */ 1326 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */ 1327 /* Check all options present and flag their presence if any exist. */ 1328 /* ------------------------------------------------------------------------ */ 1329 static INLINE void frpr_ipv4hdr(fin) 1330 fr_info_t *fin; 1331 { 1332 u_short optmsk = 0, secmsk = 0, auth = 0; 1333 int hlen, ol, mv, p, i; 1334 const struct optlist *op; 1335 u_char *s, opt; 1336 u_short off; 1337 fr_ip_t *fi; 1338 ip_t *ip; 1339 1340 fi = &fin->fin_fi; 1341 hlen = fin->fin_hlen; 1342 1343 ip = fin->fin_ip; 1344 p = ip->ip_p; 1345 fi->fi_p = p; 1346 fi->fi_tos = ip->ip_tos; 1347 fin->fin_id = ip->ip_id; 1348 off = ip->ip_off; 1349 1350 /* Get both TTL and protocol */ 1351 fi->fi_p = ip->ip_p; 1352 fi->fi_ttl = ip->ip_ttl; 1353 #if 0 1354 (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4)); 1355 #endif 1356 1357 /* Zero out bits not used in IPv6 address */ 1358 fi->fi_src.i6[1] = 0; 1359 fi->fi_src.i6[2] = 0; 1360 fi->fi_src.i6[3] = 0; 1361 fi->fi_dst.i6[1] = 0; 1362 fi->fi_dst.i6[2] = 0; 1363 fi->fi_dst.i6[3] = 0; 1364 1365 fi->fi_saddr = ip->ip_src.s_addr; 1366 fi->fi_daddr = ip->ip_dst.s_addr; 1367 1368 /* 1369 * set packet attribute flags based on the offset and 1370 * calculate the byte offset that it represents. 1371 */ 1372 off &= IP_MF|IP_OFFMASK; 1373 if (off != 0) { 1374 int morefrag = off & IP_MF; 1375 1376 fi->fi_flx |= FI_FRAG; 1377 if (morefrag) 1378 fi->fi_flx |= FI_MOREFRAG; 1379 off &= IP_OFFMASK; 1380 if (off != 0) { 1381 fin->fin_flx |= FI_FRAGBODY; 1382 off <<= 3; 1383 if ((off + fin->fin_dlen > 65535) || 1384 (fin->fin_dlen == 0) || 1385 ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) { 1386 /* 1387 * The length of the packet, starting at its 1388 * offset cannot exceed 65535 (0xffff) as the 1389 * length of an IP packet is only 16 bits. 1390 * 1391 * Any fragment that isn't the last fragment 1392 * must have a length greater than 0 and it 1393 * must be an even multiple of 8. 1394 */ 1395 fi->fi_flx |= FI_BAD; 1396 } 1397 } 1398 } 1399 fin->fin_off = off; 1400 1401 /* 1402 * Call per-protocol setup and checking 1403 */ 1404 switch (p) 1405 { 1406 case IPPROTO_UDP : 1407 frpr_udp(fin); 1408 break; 1409 case IPPROTO_TCP : 1410 frpr_tcp(fin); 1411 break; 1412 case IPPROTO_ICMP : 1413 frpr_icmp(fin); 1414 break; 1415 case IPPROTO_AH : 1416 frpr_ah(fin); 1417 break; 1418 case IPPROTO_ESP : 1419 frpr_esp(fin); 1420 break; 1421 case IPPROTO_GRE : 1422 frpr_gre(fin); 1423 break; 1424 } 1425 1426 ip = fin->fin_ip; 1427 if (ip == NULL) 1428 return; 1429 1430 /* 1431 * If it is a standard IP header (no options), set the flag fields 1432 * which relate to options to 0. 1433 */ 1434 if (hlen == sizeof(*ip)) { 1435 fi->fi_optmsk = 0; 1436 fi->fi_secmsk = 0; 1437 fi->fi_auth = 0; 1438 return; 1439 } 1440 1441 /* 1442 * So the IP header has some IP options attached. Walk the entire 1443 * list of options present with this packet and set flags to indicate 1444 * which ones are here and which ones are not. For the somewhat out 1445 * of date and obscure security classification options, set a flag to 1446 * represent which classification is present. 1447 */ 1448 fi->fi_flx |= FI_OPTIONS; 1449 1450 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) { 1451 opt = *s; 1452 if (opt == '\0') 1453 break; 1454 else if (opt == IPOPT_NOP) 1455 ol = 1; 1456 else { 1457 if (hlen < 2) 1458 break; 1459 ol = (int)*(s + 1); 1460 if (ol < 2 || ol > hlen) 1461 break; 1462 } 1463 for (i = 9, mv = 4; mv >= 0; ) { 1464 op = ipopts + i; 1465 if ((opt == (u_char)op->ol_val) && (ol > 4)) { 1466 optmsk |= op->ol_bit; 1467 if (opt == IPOPT_SECURITY) { 1468 const struct optlist *sp; 1469 u_char sec; 1470 int j, m; 1471 1472 sec = *(s + 2); /* classification */ 1473 for (j = 3, m = 2; m >= 0; ) { 1474 sp = secopt + j; 1475 if (sec == sp->ol_val) { 1476 secmsk |= sp->ol_bit; 1477 auth = *(s + 3); 1478 auth *= 256; 1479 auth += *(s + 4); 1480 break; 1481 } 1482 if (sec < sp->ol_val) 1483 j -= m; 1484 else 1485 j += m; 1486 m--; 1487 } 1488 } 1489 break; 1490 } 1491 if (opt < op->ol_val) 1492 i -= mv; 1493 else 1494 i += mv; 1495 mv--; 1496 } 1497 hlen -= ol; 1498 s += ol; 1499 } 1500 1501 /* 1502 * 1503 */ 1504 if (auth && !(auth & 0x0100)) 1505 auth &= 0xff00; 1506 fi->fi_optmsk = optmsk; 1507 fi->fi_secmsk = secmsk; 1508 fi->fi_auth = auth; 1509 } 1510 1511 1512 /* ------------------------------------------------------------------------ */ 1513 /* Function: fr_makefrip */ 1514 /* Returns: int - 1 == hdr checking error, 0 == OK */ 1515 /* Parameters: hlen(I) - length of IP packet header */ 1516 /* ip(I) - pointer to the IP header */ 1517 /* fin(IO) - pointer to packet information */ 1518 /* */ 1519 /* Compact the IP header into a structure which contains just the info. */ 1520 /* which is useful for comparing IP headers with and store this information */ 1521 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */ 1522 /* this function will be called with either an IPv4 or IPv6 packet. */ 1523 /* ------------------------------------------------------------------------ */ 1524 int fr_makefrip(hlen, ip, fin) 1525 int hlen; 1526 ip_t *ip; 1527 fr_info_t *fin; 1528 { 1529 int v; 1530 1531 fin->fin_depth = 0; 1532 fin->fin_hlen = (u_short)hlen; 1533 fin->fin_ip = ip; 1534 fin->fin_rule = 0xffffffff; 1535 fin->fin_group[0] = -1; 1536 fin->fin_group[1] = '\0'; 1537 fin->fin_dlen = fin->fin_plen - hlen; 1538 fin->fin_dp = (char *)ip + hlen; 1539 1540 v = fin->fin_v; 1541 if (v == 4) 1542 frpr_ipv4hdr(fin); 1543 #ifdef USE_INET6 1544 else if (v == 6) 1545 frpr_ipv6hdr(fin); 1546 #endif 1547 if (fin->fin_ip == NULL) 1548 return -1; 1549 return 0; 1550 } 1551 1552 1553 /* ------------------------------------------------------------------------ */ 1554 /* Function: fr_portcheck */ 1555 /* Returns: int - 1 == port matched, 0 == port match failed */ 1556 /* Parameters: frp(I) - pointer to port check `expression' */ 1557 /* pop(I) - pointer to port number to evaluate */ 1558 /* */ 1559 /* Perform a comparison of a port number against some other(s), using a */ 1560 /* structure with compare information stored in it. */ 1561 /* ------------------------------------------------------------------------ */ 1562 static INLINE int fr_portcheck(frp, pop) 1563 frpcmp_t *frp; 1564 u_short *pop; 1565 { 1566 u_short tup, po; 1567 int err = 1; 1568 1569 tup = *pop; 1570 po = frp->frp_port; 1571 1572 /* 1573 * Do opposite test to that required and continue if that succeeds. 1574 */ 1575 switch (frp->frp_cmp) 1576 { 1577 case FR_EQUAL : 1578 if (tup != po) /* EQUAL */ 1579 err = 0; 1580 break; 1581 case FR_NEQUAL : 1582 if (tup == po) /* NOTEQUAL */ 1583 err = 0; 1584 break; 1585 case FR_LESST : 1586 if (tup >= po) /* LESSTHAN */ 1587 err = 0; 1588 break; 1589 case FR_GREATERT : 1590 if (tup <= po) /* GREATERTHAN */ 1591 err = 0; 1592 break; 1593 case FR_LESSTE : 1594 if (tup > po) /* LT or EQ */ 1595 err = 0; 1596 break; 1597 case FR_GREATERTE : 1598 if (tup < po) /* GT or EQ */ 1599 err = 0; 1600 break; 1601 case FR_OUTRANGE : 1602 if (tup >= po && tup <= frp->frp_top) /* Out of range */ 1603 err = 0; 1604 break; 1605 case FR_INRANGE : 1606 if (tup <= po || tup >= frp->frp_top) /* In range */ 1607 err = 0; 1608 break; 1609 case FR_INCRANGE : 1610 if (tup < po || tup > frp->frp_top) /* Inclusive range */ 1611 err = 0; 1612 break; 1613 default : 1614 break; 1615 } 1616 return err; 1617 } 1618 1619 1620 /* ------------------------------------------------------------------------ */ 1621 /* Function: fr_tcpudpchk */ 1622 /* Returns: int - 1 == protocol matched, 0 == check failed */ 1623 /* Parameters: fin(I) - pointer to packet information */ 1624 /* ft(I) - pointer to structure with comparison data */ 1625 /* */ 1626 /* Compares the current pcket (assuming it is TCP/UDP) information with a */ 1627 /* structure containing information that we want to match against. */ 1628 /* ------------------------------------------------------------------------ */ 1629 int fr_tcpudpchk(fin, ft) 1630 fr_info_t *fin; 1631 frtuc_t *ft; 1632 { 1633 int err = 1; 1634 1635 /* 1636 * Both ports should *always* be in the first fragment. 1637 * So far, I cannot find any cases where they can not be. 1638 * 1639 * compare destination ports 1640 */ 1641 if (ft->ftu_dcmp) 1642 err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport); 1643 1644 /* 1645 * compare source ports 1646 */ 1647 if (err && ft->ftu_scmp) 1648 err = fr_portcheck(&ft->ftu_src, &fin->fin_sport); 1649 1650 /* 1651 * If we don't have all the TCP/UDP header, then how can we 1652 * expect to do any sort of match on it ? If we were looking for 1653 * TCP flags, then NO match. If not, then match (which should 1654 * satisfy the "short" class too). 1655 */ 1656 if (err && (fin->fin_p == IPPROTO_TCP)) { 1657 if (fin->fin_flx & FI_SHORT) 1658 return !(ft->ftu_tcpf | ft->ftu_tcpfm); 1659 /* 1660 * Match the flags ? If not, abort this match. 1661 */ 1662 if (ft->ftu_tcpfm && 1663 ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) { 1664 FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf, 1665 ft->ftu_tcpfm, ft->ftu_tcpf)); 1666 err = 0; 1667 } 1668 } 1669 return err; 1670 } 1671 1672 1673 /* ------------------------------------------------------------------------ */ 1674 /* Function: fr_ipfcheck */ 1675 /* Returns: int - 0 == match, 1 == no match */ 1676 /* Parameters: fin(I) - pointer to packet information */ 1677 /* fr(I) - pointer to filter rule */ 1678 /* portcmp(I) - flag indicating whether to attempt matching on */ 1679 /* TCP/UDP port data. */ 1680 /* */ 1681 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */ 1682 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */ 1683 /* this function. */ 1684 /* ------------------------------------------------------------------------ */ 1685 static INLINE int fr_ipfcheck(fin, fr, portcmp) 1686 fr_info_t *fin; 1687 frentry_t *fr; 1688 int portcmp; 1689 { 1690 u_32_t *ld, *lm, *lip; 1691 fripf_t *fri; 1692 fr_ip_t *fi; 1693 int i; 1694 ipf_stack_t *ifs = fin->fin_ifs; 1695 1696 fi = &fin->fin_fi; 1697 fri = fr->fr_ipf; 1698 lip = (u_32_t *)fi; 1699 lm = (u_32_t *)&fri->fri_mip; 1700 ld = (u_32_t *)&fri->fri_ip; 1701 1702 /* 1703 * first 32 bits to check coversion: 1704 * IP version, TOS, TTL, protocol 1705 */ 1706 i = ((*lip & *lm) != *ld); 1707 FR_DEBUG(("0. %#08x & %#08x != %#08x\n", 1708 *lip, *lm, *ld)); 1709 if (i) 1710 return 1; 1711 1712 /* 1713 * Next 32 bits is a constructed bitmask indicating which IP options 1714 * are present (if any) in this packet. 1715 */ 1716 lip++, lm++, ld++; 1717 i |= ((*lip & *lm) != *ld); 1718 FR_DEBUG(("1. %#08x & %#08x != %#08x\n", 1719 *lip, *lm, *ld)); 1720 if (i) 1721 return 1; 1722 1723 lip++, lm++, ld++; 1724 /* 1725 * Unrolled loops (4 each, for 32 bits) for address checks. 1726 */ 1727 /* 1728 * Check the source address. 1729 */ 1730 #ifdef IPFILTER_LOOKUP 1731 if (fr->fr_satype == FRI_LOOKUP) { 1732 i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip, ifs); 1733 if (i == -1) 1734 return 1; 1735 lip += 3; 1736 lm += 3; 1737 ld += 3; 1738 } else { 1739 #endif 1740 i = ((*lip & *lm) != *ld); 1741 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n", 1742 *lip, *lm, *ld)); 1743 if (fi->fi_v == 6) { 1744 lip++, lm++, ld++; 1745 i |= ((*lip & *lm) != *ld); 1746 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n", 1747 *lip, *lm, *ld)); 1748 lip++, lm++, ld++; 1749 i |= ((*lip & *lm) != *ld); 1750 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n", 1751 *lip, *lm, *ld)); 1752 lip++, lm++, ld++; 1753 i |= ((*lip & *lm) != *ld); 1754 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n", 1755 *lip, *lm, *ld)); 1756 } else { 1757 lip += 3; 1758 lm += 3; 1759 ld += 3; 1760 } 1761 #ifdef IPFILTER_LOOKUP 1762 } 1763 #endif 1764 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6; 1765 if (i) 1766 return 1; 1767 1768 /* 1769 * Check the destination address. 1770 */ 1771 lip++, lm++, ld++; 1772 #ifdef IPFILTER_LOOKUP 1773 if (fr->fr_datype == FRI_LOOKUP) { 1774 i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip, ifs); 1775 if (i == -1) 1776 return 1; 1777 lip += 3; 1778 lm += 3; 1779 ld += 3; 1780 } else { 1781 #endif 1782 i = ((*lip & *lm) != *ld); 1783 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n", 1784 *lip, *lm, *ld)); 1785 if (fi->fi_v == 6) { 1786 lip++, lm++, ld++; 1787 i |= ((*lip & *lm) != *ld); 1788 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n", 1789 *lip, *lm, *ld)); 1790 lip++, lm++, ld++; 1791 i |= ((*lip & *lm) != *ld); 1792 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n", 1793 *lip, *lm, *ld)); 1794 lip++, lm++, ld++; 1795 i |= ((*lip & *lm) != *ld); 1796 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n", 1797 *lip, *lm, *ld)); 1798 } else { 1799 lip += 3; 1800 lm += 3; 1801 ld += 3; 1802 } 1803 #ifdef IPFILTER_LOOKUP 1804 } 1805 #endif 1806 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7; 1807 if (i) 1808 return 1; 1809 /* 1810 * IP addresses matched. The next 32bits contains: 1811 * mast of old IP header security & authentication bits. 1812 */ 1813 lip++, lm++, ld++; 1814 i |= ((*lip & *lm) != *ld); 1815 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", 1816 *lip, *lm, *ld)); 1817 1818 /* 1819 * Next we have 32 bits of packet flags. 1820 */ 1821 lip++, lm++, ld++; 1822 i |= ((*lip & *lm) != *ld); 1823 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", 1824 *lip, *lm, *ld)); 1825 1826 if (i == 0) { 1827 /* 1828 * If a fragment, then only the first has what we're 1829 * looking for here... 1830 */ 1831 if (portcmp) { 1832 if (!fr_tcpudpchk(fin, &fr->fr_tuc)) 1833 i = 1; 1834 } else { 1835 if (fr->fr_dcmp || fr->fr_scmp || 1836 fr->fr_tcpf || fr->fr_tcpfm) 1837 i = 1; 1838 if (fr->fr_icmpm || fr->fr_icmp) { 1839 if (((fi->fi_p != IPPROTO_ICMP) && 1840 (fi->fi_p != IPPROTO_ICMPV6)) || 1841 fin->fin_off || (fin->fin_dlen < 2)) 1842 i = 1; 1843 else if ((fin->fin_data[0] & fr->fr_icmpm) != 1844 fr->fr_icmp) { 1845 FR_DEBUG(("i. %#x & %#x != %#x\n", 1846 fin->fin_data[0], 1847 fr->fr_icmpm, fr->fr_icmp)); 1848 i = 1; 1849 } 1850 } 1851 } 1852 } 1853 return i; 1854 } 1855 1856 1857 /* ------------------------------------------------------------------------ */ 1858 /* Function: fr_scanlist */ 1859 /* Returns: int - result flags of scanning filter list */ 1860 /* Parameters: fin(I) - pointer to packet information */ 1861 /* pass(I) - default result to return for filtering */ 1862 /* */ 1863 /* Check the input/output list of rules for a match to the current packet. */ 1864 /* If a match is found, the value of fr_flags from the rule becomes the */ 1865 /* return value and fin->fin_fr points to the matched rule. */ 1866 /* */ 1867 /* This function may be called recusively upto 16 times (limit inbuilt.) */ 1868 /* When unwinding, it should finish up with fin_depth as 0. */ 1869 /* */ 1870 /* Could be per interface, but this gets real nasty when you don't have, */ 1871 /* or can't easily change, the kernel source code to . */ 1872 /* ------------------------------------------------------------------------ */ 1873 int fr_scanlist(fin, pass) 1874 fr_info_t *fin; 1875 u_32_t pass; 1876 { 1877 int rulen, portcmp, off, logged, skip; 1878 struct frentry *fr, *fnext; 1879 u_32_t passt, passo; 1880 ipf_stack_t *ifs = fin->fin_ifs; 1881 1882 /* 1883 * Do not allow nesting deeper than 16 levels. 1884 */ 1885 if (fin->fin_depth >= 16) 1886 return pass; 1887 1888 fr = fin->fin_fr; 1889 1890 /* 1891 * If there are no rules in this list, return now. 1892 */ 1893 if (fr == NULL) 1894 return pass; 1895 1896 skip = 0; 1897 logged = 0; 1898 portcmp = 0; 1899 fin->fin_depth++; 1900 fin->fin_fr = NULL; 1901 off = fin->fin_off; 1902 1903 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off) 1904 portcmp = 1; 1905 1906 for (rulen = 0; fr; fr = fnext, rulen++) { 1907 fnext = fr->fr_next; 1908 if (skip != 0) { 1909 FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags)); 1910 skip--; 1911 continue; 1912 } 1913 1914 /* 1915 * In all checks below, a null (zero) value in the 1916 * filter struture is taken to mean a wildcard. 1917 * 1918 * check that we are working for the right interface 1919 */ 1920 #ifdef _KERNEL 1921 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 1922 continue; 1923 #else 1924 if (opts & (OPT_VERBOSE|OPT_DEBUG)) 1925 printf("\n"); 1926 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' : 1927 FR_ISPASS(pass) ? 'p' : 1928 FR_ISACCOUNT(pass) ? 'A' : 1929 FR_ISAUTH(pass) ? 'a' : 1930 (pass & FR_NOMATCH) ? 'n' :'b')); 1931 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 1932 continue; 1933 FR_VERBOSE((":i")); 1934 #endif 1935 1936 switch (fr->fr_type) 1937 { 1938 case FR_T_IPF : 1939 case FR_T_IPF|FR_T_BUILTIN : 1940 if (fr_ipfcheck(fin, fr, portcmp)) 1941 continue; 1942 break; 1943 #if defined(IPFILTER_BPF) 1944 case FR_T_BPFOPC : 1945 case FR_T_BPFOPC|FR_T_BUILTIN : 1946 { 1947 u_char *mc; 1948 1949 if (*fin->fin_mp == NULL) 1950 continue; 1951 if (fin->fin_v != fr->fr_v) 1952 continue; 1953 mc = (u_char *)fin->fin_m; 1954 if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0)) 1955 continue; 1956 break; 1957 } 1958 #endif 1959 case FR_T_CALLFUNC|FR_T_BUILTIN : 1960 { 1961 frentry_t *f; 1962 1963 f = (*fr->fr_func)(fin, &pass); 1964 if (f != NULL) 1965 fr = f; 1966 else 1967 continue; 1968 break; 1969 } 1970 default : 1971 break; 1972 } 1973 1974 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { 1975 if (fin->fin_nattag == NULL) 1976 continue; 1977 if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0) 1978 continue; 1979 } 1980 FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen)); 1981 1982 passt = fr->fr_flags; 1983 1984 /* 1985 * Allowing a rule with the "keep state" flag set to match 1986 * packets that have been tagged "out of window" by the TCP 1987 * state tracking is foolish as the attempt to add a new 1988 * state entry to the table will fail. 1989 */ 1990 if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW)) 1991 continue; 1992 1993 /* 1994 * If the rule is a "call now" rule, then call the function 1995 * in the rule, if it exists and use the results from that. 1996 * If the function pointer is bad, just make like we ignore 1997 * it, except for increasing the hit counter. 1998 */ 1999 if ((passt & FR_CALLNOW) != 0) { 2000 IPF_BUMP(fr->fr_hits); 2001 if ((fr->fr_func != NULL) && 2002 (fr->fr_func != (ipfunc_t)-1)) { 2003 frentry_t *frs; 2004 2005 frs = fin->fin_fr; 2006 fin->fin_fr = fr; 2007 fr = (*fr->fr_func)(fin, &passt); 2008 if (fr == NULL) { 2009 fin->fin_fr = frs; 2010 continue; 2011 } 2012 passt = fr->fr_flags; 2013 fin->fin_fr = fr; 2014 } 2015 } else { 2016 fin->fin_fr = fr; 2017 } 2018 2019 #ifdef IPFILTER_LOG 2020 /* 2021 * Just log this packet... 2022 */ 2023 if ((passt & FR_LOGMASK) == FR_LOG) { 2024 if (ipflog(fin, passt) == -1) { 2025 if (passt & FR_LOGORBLOCK) { 2026 passt &= ~FR_CMDMASK; 2027 passt |= FR_BLOCK|FR_QUICK; 2028 } 2029 IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_skip); 2030 } 2031 IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_pkl); 2032 logged = 1; 2033 } 2034 #endif /* IPFILTER_LOG */ 2035 fr->fr_bytes += (U_QUAD_T)fin->fin_plen; 2036 passo = pass; 2037 if (FR_ISSKIP(passt)) 2038 skip = fr->fr_arg; 2039 else if ((passt & FR_LOGMASK) != FR_LOG) 2040 pass = passt; 2041 if (passt & (FR_RETICMP|FR_FAKEICMP)) 2042 fin->fin_icode = fr->fr_icode; 2043 FR_DEBUG(("pass %#x\n", pass)); 2044 IPF_BUMP(fr->fr_hits); 2045 fin->fin_rule = rulen; 2046 (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN); 2047 if (fr->fr_grp != NULL) { 2048 fin->fin_fr = *fr->fr_grp; 2049 pass = fr_scanlist(fin, pass); 2050 if (fin->fin_fr == NULL) { 2051 fin->fin_rule = rulen; 2052 (void) strncpy(fin->fin_group, fr->fr_group, 2053 FR_GROUPLEN); 2054 fin->fin_fr = fr; 2055 } 2056 if (fin->fin_flx & FI_DONTCACHE) 2057 logged = 1; 2058 } 2059 2060 if (pass & FR_QUICK) { 2061 /* 2062 * Finally, if we've asked to track state for this 2063 * packet, set it up. Add state for "quick" rules 2064 * here so that if the action fails we can consider 2065 * the rule to "not match" and keep on processing 2066 * filter rules. 2067 */ 2068 if ((pass & FR_KEEPSTATE) && 2069 !(fin->fin_flx & FI_STATE)) { 2070 int out = fin->fin_out; 2071 2072 if (fr_addstate(fin, NULL, 0) != NULL) { 2073 IPF_BUMP(ifs->ifs_frstats[out].fr_ads); 2074 } else { 2075 IPF_BUMP(ifs->ifs_frstats[out].fr_bads); 2076 pass = passo; 2077 continue; 2078 } 2079 } 2080 break; 2081 } 2082 } 2083 if (logged) 2084 fin->fin_flx |= FI_DONTCACHE; 2085 fin->fin_depth--; 2086 return pass; 2087 } 2088 2089 2090 /* ------------------------------------------------------------------------ */ 2091 /* Function: fr_acctpkt */ 2092 /* Returns: frentry_t* - always returns NULL */ 2093 /* Parameters: fin(I) - pointer to packet information */ 2094 /* passp(IO) - pointer to current/new filter decision (unused) */ 2095 /* */ 2096 /* Checks a packet against accounting rules, if there are any for the given */ 2097 /* IP protocol version. */ 2098 /* */ 2099 /* N.B.: this function returns NULL to match the prototype used by other */ 2100 /* functions called from the IPFilter "mainline" in fr_check(). */ 2101 /* ------------------------------------------------------------------------ */ 2102 frentry_t *fr_acctpkt(fin, passp) 2103 fr_info_t *fin; 2104 u_32_t *passp; 2105 { 2106 char group[FR_GROUPLEN]; 2107 frentry_t *fr, *frsave; 2108 u_32_t pass, rulen; 2109 ipf_stack_t *ifs = fin->fin_ifs; 2110 2111 passp = passp; 2112 #ifdef USE_INET6 2113 if (fin->fin_v == 6) 2114 fr = ifs->ifs_ipacct6[fin->fin_out][ifs->ifs_fr_active]; 2115 else 2116 #endif 2117 fr = ifs->ifs_ipacct[fin->fin_out][ifs->ifs_fr_active]; 2118 2119 if (fr != NULL) { 2120 frsave = fin->fin_fr; 2121 bcopy(fin->fin_group, group, FR_GROUPLEN); 2122 rulen = fin->fin_rule; 2123 fin->fin_fr = fr; 2124 pass = fr_scanlist(fin, FR_NOMATCH); 2125 if (FR_ISACCOUNT(pass)) { 2126 IPF_BUMP(ifs->ifs_frstats[0].fr_acct); 2127 } 2128 fin->fin_fr = frsave; 2129 bcopy(group, fin->fin_group, FR_GROUPLEN); 2130 fin->fin_rule = rulen; 2131 } 2132 return NULL; 2133 } 2134 2135 2136 /* ------------------------------------------------------------------------ */ 2137 /* Function: fr_firewall */ 2138 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */ 2139 /* were found, returns NULL. */ 2140 /* Parameters: fin(I) - pointer to packet information */ 2141 /* passp(IO) - pointer to current/new filter decision (unused) */ 2142 /* */ 2143 /* Applies an appropriate set of firewall rules to the packet, to see if */ 2144 /* there are any matches. The first check is to see if a match can be seen */ 2145 /* in the cache. If not, then search an appropriate list of rules. Once a */ 2146 /* matching rule is found, take any appropriate actions as defined by the */ 2147 /* rule - except logging. */ 2148 /* ------------------------------------------------------------------------ */ 2149 static frentry_t *fr_firewall(fin, passp) 2150 fr_info_t *fin; 2151 u_32_t *passp; 2152 { 2153 frentry_t *fr; 2154 fr_info_t *fc; 2155 u_32_t pass; 2156 int out; 2157 ipf_stack_t *ifs = fin->fin_ifs; 2158 2159 out = fin->fin_out; 2160 pass = *passp; 2161 2162 #ifdef USE_INET6 2163 if (fin->fin_v == 6) 2164 fin->fin_fr = ifs->ifs_ipfilter6[out][ifs->ifs_fr_active]; 2165 else 2166 #endif 2167 fin->fin_fr = ifs->ifs_ipfilter[out][ifs->ifs_fr_active]; 2168 2169 /* 2170 * If there are no rules loaded skip all checks and return. 2171 */ 2172 if (fin->fin_fr == NULL) { 2173 2174 if ((pass & FR_NOMATCH)) { 2175 IPF_BUMP(ifs->ifs_frstats[out].fr_nom); 2176 } 2177 2178 return (NULL); 2179 } 2180 2181 fc = &ifs->ifs_frcache[out][CACHE_HASH(fin)]; 2182 READ_ENTER(&ifs->ifs_ipf_frcache); 2183 if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) { 2184 /* 2185 * copy cached data so we can unlock the mutexes earlier. 2186 */ 2187 bcopy((char *)fc, (char *)fin, FI_COPYSIZE); 2188 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 2189 IPF_BUMP(ifs->ifs_frstats[out].fr_chit); 2190 2191 if ((fr = fin->fin_fr) != NULL) { 2192 IPF_BUMP(fr->fr_hits); 2193 pass = fr->fr_flags; 2194 } 2195 } else { 2196 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 2197 2198 pass = fr_scanlist(fin, ifs->ifs_fr_pass); 2199 2200 if (((pass & FR_KEEPSTATE) == 0) && 2201 ((fin->fin_flx & FI_DONTCACHE) == 0)) { 2202 WRITE_ENTER(&ifs->ifs_ipf_frcache); 2203 bcopy((char *)fin, (char *)fc, FI_COPYSIZE); 2204 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 2205 } 2206 2207 fr = fin->fin_fr; 2208 } 2209 2210 if ((pass & FR_NOMATCH)) { 2211 IPF_BUMP(ifs->ifs_frstats[out].fr_nom); 2212 } 2213 2214 /* 2215 * Apply packets per second rate-limiting to a rule as required. 2216 */ 2217 if ((fr != NULL) && (fr->fr_pps != 0) && 2218 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) { 2219 pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST); 2220 pass |= FR_BLOCK; 2221 IPF_BUMP(ifs->ifs_frstats[out].fr_ppshit); 2222 } 2223 2224 /* 2225 * If we fail to add a packet to the authorization queue, then we 2226 * drop the packet later. However, if it was added then pretend 2227 * we've dropped it already. 2228 */ 2229 if (FR_ISAUTH(pass)) { 2230 if (fr_newauth(fin->fin_m, fin) != 0) { 2231 #ifdef _KERNEL 2232 fin->fin_m = *fin->fin_mp = NULL; 2233 #else 2234 ; 2235 #endif 2236 fin->fin_error = 0; 2237 } else 2238 fin->fin_error = ENOSPC; 2239 } 2240 2241 if ((fr != NULL) && (fr->fr_func != NULL) && 2242 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW)) 2243 (void) (*fr->fr_func)(fin, &pass); 2244 2245 /* 2246 * If a rule is a pre-auth rule, check again in the list of rules 2247 * loaded for authenticated use. It does not particulary matter 2248 * if this search fails because a "preauth" result, from a rule, 2249 * is treated as "not a pass", hence the packet is blocked. 2250 */ 2251 if (FR_ISPREAUTH(pass)) { 2252 if ((fin->fin_fr = ifs->ifs_ipauth) != NULL) 2253 pass = fr_scanlist(fin, ifs->ifs_fr_pass); 2254 } 2255 2256 /* 2257 * If the rule has "keep frag" and the packet is actually a fragment, 2258 * then create a fragment state entry. 2259 */ 2260 if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) { 2261 if (fin->fin_flx & FI_FRAG) { 2262 if (fr_newfrag(fin, pass) == -1) { 2263 IPF_BUMP(ifs->ifs_frstats[out].fr_bnfr); 2264 } else { 2265 IPF_BUMP(ifs->ifs_frstats[out].fr_nfr); 2266 } 2267 } else { 2268 IPF_BUMP(ifs->ifs_frstats[out].fr_cfr); 2269 } 2270 } 2271 2272 /* 2273 * Finally, if we've asked to track state for this packet, set it up. 2274 */ 2275 if ((pass & FR_KEEPSTATE) && !(fin->fin_flx & FI_STATE)) { 2276 if (fr_addstate(fin, NULL, 0) != NULL) { 2277 IPF_BUMP(ifs->ifs_frstats[out].fr_ads); 2278 } else { 2279 IPF_BUMP(ifs->ifs_frstats[out].fr_bads); 2280 if (FR_ISPASS(pass)) { 2281 pass &= ~FR_CMDMASK; 2282 pass |= FR_BLOCK; 2283 } 2284 } 2285 } 2286 2287 fr = fin->fin_fr; 2288 2289 if (passp != NULL) 2290 *passp = pass; 2291 2292 return fr; 2293 } 2294 2295 /* ------------------------------------------------------------------------ */ 2296 /* Function: fr_check */ 2297 /* Returns: int - 0 == packet allowed through, */ 2298 /* User space: */ 2299 /* -1 == packet blocked */ 2300 /* 1 == packet not matched */ 2301 /* -2 == requires authentication */ 2302 /* Kernel: */ 2303 /* > 0 == filter error # for packet */ 2304 /* Parameters: ip(I) - pointer to start of IPv4/6 packet */ 2305 /* hlen(I) - length of header */ 2306 /* ifp(I) - pointer to interface this packet is on */ 2307 /* out(I) - 0 == packet going in, 1 == packet going out */ 2308 /* mp(IO) - pointer to caller's buffer pointer that holds this */ 2309 /* IP packet. */ 2310 /* Solaris & HP-UX ONLY : */ 2311 /* qpi(I) - pointer to STREAMS queue information for this */ 2312 /* interface & direction. */ 2313 /* */ 2314 /* fr_check() is the master function for all IPFilter packet processing. */ 2315 /* It orchestrates: Network Address Translation (NAT), checking for packet */ 2316 /* authorisation (or pre-authorisation), presence of related state info., */ 2317 /* generating log entries, IP packet accounting, routing of packets as */ 2318 /* directed by firewall rules and of course whether or not to allow the */ 2319 /* packet to be further processed by the kernel. */ 2320 /* */ 2321 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */ 2322 /* freed. Packets passed may be returned with the pointer pointed to by */ 2323 /* by "mp" changed to a new buffer. */ 2324 /* ------------------------------------------------------------------------ */ 2325 int fr_check(ip, hlen, ifp, out 2326 #if defined(_KERNEL) && defined(MENTAT) 2327 , qif, mp, ifs) 2328 void *qif; 2329 #else 2330 , mp, ifs) 2331 #endif 2332 mb_t **mp; 2333 ip_t *ip; 2334 int hlen; 2335 void *ifp; 2336 int out; 2337 ipf_stack_t *ifs; 2338 { 2339 /* 2340 * The above really sucks, but short of writing a diff 2341 */ 2342 fr_info_t frinfo; 2343 fr_info_t *fin = &frinfo; 2344 u_32_t pass; 2345 frentry_t *fr = NULL; 2346 int v = IP_V(ip); 2347 mb_t *mc = NULL; 2348 mb_t *m; 2349 #ifdef USE_INET6 2350 ip6_t *ip6; 2351 #endif 2352 #ifdef _KERNEL 2353 # ifdef MENTAT 2354 qpktinfo_t *qpi = qif; 2355 #endif 2356 #endif 2357 2358 SPL_INT(s); 2359 pass = ifs->ifs_fr_pass; 2360 2361 /* 2362 * The first part of fr_check() deals with making sure that what goes 2363 * into the filtering engine makes some sense. Information about the 2364 * the packet is distilled, collected into a fr_info_t structure and 2365 * the an attempt to ensure the buffer the packet is in is big enough 2366 * to hold all the required packet headers. 2367 */ 2368 #ifdef _KERNEL 2369 # ifdef MENTAT 2370 if (!OK_32PTR(ip)) 2371 return 2; 2372 # endif 2373 2374 2375 if (ifs->ifs_fr_running <= 0) { 2376 return 0; 2377 } 2378 2379 bzero((char *)fin, sizeof(*fin)); 2380 2381 # ifdef MENTAT 2382 fin->fin_flx = qpi->qpi_flags & (FI_NOCKSUM|FI_MBCAST|FI_MULTICAST| 2383 FI_BROADCAST); 2384 m = qpi->qpi_m; 2385 fin->fin_qfm = m; 2386 fin->fin_qpi = qpi; 2387 # else /* MENTAT */ 2388 2389 m = *mp; 2390 2391 # if defined(M_MCAST) 2392 if ((m->m_flags & M_MCAST) != 0) 2393 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2394 # endif 2395 # if defined(M_MLOOP) 2396 if ((m->m_flags & M_MLOOP) != 0) 2397 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 2398 # endif 2399 # if defined(M_BCAST) 2400 if ((m->m_flags & M_BCAST) != 0) 2401 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 2402 # endif 2403 # ifdef M_CANFASTFWD 2404 /* 2405 * XXX For now, IP Filter and fast-forwarding of cached flows 2406 * XXX are mutually exclusive. Eventually, IP Filter should 2407 * XXX get a "can-fast-forward" filter rule. 2408 */ 2409 m->m_flags &= ~M_CANFASTFWD; 2410 # endif /* M_CANFASTFWD */ 2411 # ifdef CSUM_DELAY_DATA 2412 /* 2413 * disable delayed checksums. 2414 */ 2415 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2416 in_delayed_cksum(m); 2417 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2418 } 2419 # endif /* CSUM_DELAY_DATA */ 2420 # endif /* MENTAT */ 2421 #else 2422 2423 bzero((char *)fin, sizeof(*fin)); 2424 m = *mp; 2425 #endif /* _KERNEL */ 2426 2427 fin->fin_v = v; 2428 fin->fin_m = m; 2429 fin->fin_ip = ip; 2430 fin->fin_mp = mp; 2431 fin->fin_out = out; 2432 fin->fin_ifp = ifp; 2433 fin->fin_error = ENETUNREACH; 2434 fin->fin_hlen = (u_short)hlen; 2435 fin->fin_dp = (char *)ip + hlen; 2436 fin->fin_ipoff = (char *)ip - MTOD(m, char *); 2437 fin->fin_ifs = ifs; 2438 2439 SPL_NET(s); 2440 2441 #ifdef USE_INET6 2442 if (v == 6) { 2443 IPF_BUMP(ifs->ifs_frstats[out].fr_ipv6); 2444 /* 2445 * Jumbo grams are quite likely too big for internal buffer 2446 * structures to handle comfortably, for now, so just drop 2447 * them. 2448 */ 2449 ip6 = (ip6_t *)ip; 2450 fin->fin_plen = ntohs(ip6->ip6_plen); 2451 if (fin->fin_plen == 0) { 2452 READ_ENTER(&ifs->ifs_ipf_mutex); 2453 pass = FR_BLOCK|FR_NOMATCH; 2454 goto filtered; 2455 } 2456 fin->fin_plen += sizeof(ip6_t); 2457 } else 2458 #endif 2459 { 2460 #if (OpenBSD >= 200311) && defined(_KERNEL) 2461 ip->ip_len = ntohs(ip->ip_len); 2462 ip->ip_off = ntohs(ip->ip_off); 2463 #endif 2464 fin->fin_plen = ip->ip_len; 2465 } 2466 2467 if (fr_makefrip(hlen, ip, fin) == -1) { 2468 READ_ENTER(&ifs->ifs_ipf_mutex); 2469 pass = FR_BLOCK; 2470 goto filtered; 2471 } 2472 2473 /* 2474 * For at least IPv6 packets, if a m_pullup() fails then this pointer 2475 * becomes NULL and so we have no packet to free. 2476 */ 2477 if (*fin->fin_mp == NULL) 2478 goto finished; 2479 2480 if (!out) { 2481 if (v == 4) { 2482 #ifdef _KERNEL 2483 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) { 2484 IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc); 2485 fin->fin_flx |= FI_BADSRC; 2486 } 2487 #endif 2488 if (fin->fin_ip->ip_ttl < ifs->ifs_fr_minttl) { 2489 IPF_BUMP(ifs->ifs_frstats[0].fr_badttl); 2490 fin->fin_flx |= FI_LOWTTL; 2491 } 2492 } 2493 #ifdef USE_INET6 2494 else if (v == 6) { 2495 ip6 = (ip6_t *)ip; 2496 #ifdef _KERNEL 2497 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) { 2498 IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc); 2499 fin->fin_flx |= FI_BADSRC; 2500 } 2501 #endif 2502 if (ip6->ip6_hlim < ifs->ifs_fr_minttl) { 2503 IPF_BUMP(ifs->ifs_frstats[0].fr_badttl); 2504 fin->fin_flx |= FI_LOWTTL; 2505 } 2506 } 2507 #endif 2508 } 2509 2510 if (fin->fin_flx & FI_SHORT) { 2511 IPF_BUMP(ifs->ifs_frstats[out].fr_short); 2512 } 2513 2514 READ_ENTER(&ifs->ifs_ipf_mutex); 2515 2516 /* 2517 * Check auth now. This, combined with the check below to see if apass 2518 * is 0 is to ensure that we don't count the packet twice, which can 2519 * otherwise occur when we reprocess it. As it is, we only count it 2520 * after it has no auth. table matchup. This also stops NAT from 2521 * occuring until after the packet has been auth'd. 2522 */ 2523 fr = fr_checkauth(fin, &pass); 2524 if (!out) { 2525 switch (fin->fin_v) 2526 { 2527 case 4 : 2528 if (fr_checknatin(fin, &pass) == -1) { 2529 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2530 goto finished; 2531 } 2532 break; 2533 #ifdef USE_INET6 2534 case 6 : 2535 if (fr_checknat6in(fin, &pass) == -1) { 2536 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2537 goto finished; 2538 } 2539 break; 2540 #endif 2541 default : 2542 break; 2543 } 2544 } 2545 if (!out) 2546 (void) fr_acctpkt(fin, NULL); 2547 2548 if (fr == NULL) 2549 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG) 2550 fr = fr_knownfrag(fin, &pass); 2551 if (fr == NULL) 2552 fr = fr_checkstate(fin, &pass); 2553 2554 if ((pass & FR_NOMATCH) || (fr == NULL)) 2555 fr = fr_firewall(fin, &pass); 2556 2557 fin->fin_fr = fr; 2558 2559 /* 2560 * Only count/translate packets which will be passed on, out the 2561 * interface. 2562 */ 2563 if (out && FR_ISPASS(pass)) { 2564 (void) fr_acctpkt(fin, NULL); 2565 2566 switch (fin->fin_v) 2567 { 2568 case 4 : 2569 if (fr_checknatout(fin, &pass) == -1) { 2570 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2571 goto finished; 2572 } 2573 break; 2574 #ifdef USE_INET6 2575 case 6 : 2576 if (fr_checknat6out(fin, &pass) == -1) { 2577 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2578 goto finished; 2579 } 2580 break; 2581 #endif 2582 default : 2583 break; 2584 } 2585 2586 if ((ifs->ifs_fr_update_ipid != 0) && (v == 4)) { 2587 if (fr_updateipid(fin) == -1) { 2588 IPF_BUMP(ifs->ifs_frstats[1].fr_ipud); 2589 pass &= ~FR_CMDMASK; 2590 pass |= FR_BLOCK; 2591 } else { 2592 IPF_BUMP(ifs->ifs_frstats[0].fr_ipud); 2593 } 2594 } 2595 } 2596 2597 #ifdef IPFILTER_LOG 2598 if ((ifs->ifs_fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) { 2599 (void) fr_dolog(fin, &pass); 2600 } 2601 #endif 2602 2603 /* 2604 * The FI_STATE flag is cleared here so that calling fr_checkstate 2605 * will work when called from inside of fr_fastroute. Although 2606 * there is a similar flag, FI_NATED, for NAT, it does have the same 2607 * impact on code execution. 2608 */ 2609 fin->fin_flx &= ~FI_STATE; 2610 2611 /* 2612 * Only allow FR_DUP to work if a rule matched - it makes no sense to 2613 * set FR_DUP as a "default" as there are no instructions about where 2614 * to send the packet. Use fin_m here because it may have changed 2615 * (without an update of 'm') in prior processing. 2616 */ 2617 if ((fr != NULL) && (pass & FR_DUP)) { 2618 mc = M_DUPLICATE(fin->fin_m); 2619 } 2620 2621 if (pass & (FR_RETRST|FR_RETICMP)) { 2622 /* 2623 * Should we return an ICMP packet to indicate error 2624 * status passing through the packet filter ? 2625 * WARNING: ICMP error packets AND TCP RST packets should 2626 * ONLY be sent in repsonse to incoming packets. Sending them 2627 * in response to outbound packets can result in a panic on 2628 * some operating systems. 2629 */ 2630 if (!out) { 2631 if (pass & FR_RETICMP) { 2632 int dst; 2633 2634 if ((pass & FR_RETMASK) == FR_FAKEICMP) 2635 dst = 1; 2636 else 2637 dst = 0; 2638 #if defined(_KERNEL) && (SOLARIS2 >= 10) 2639 /* 2640 * Assume it's possible to enter insane rule: 2641 * pass return-icmp in proto udp ... 2642 * then we have no other option than to forward 2643 * packet on loopback and give up any attempt 2644 * to create a fake response. 2645 */ 2646 if (IPF_IS_LOOPBACK(qpi->qpi_flags) && 2647 FR_ISBLOCK(pass)) { 2648 2649 if (fr_make_icmp(fin) == 0) { 2650 IPF_BUMP( 2651 ifs->ifs_frstats[out].fr_ret); 2652 } 2653 /* 2654 * we drop packet silently in case we 2655 * failed assemble fake response for it 2656 */ 2657 else if (*mp != NULL) { 2658 FREE_MB_T(*mp); 2659 m = *mp = NULL; 2660 } 2661 2662 IPF_BUMP( 2663 ifs->ifs_frstats[out].fr_block); 2664 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2665 2666 return (0); 2667 } 2668 #endif /* _KERNEL && SOLARIS2 >= 10 */ 2669 2670 (void) fr_send_icmp_err(ICMP_UNREACH, fin, dst); 2671 IPF_BUMP(ifs->ifs_frstats[out].fr_ret); 2672 2673 } else if (((pass & FR_RETMASK) == FR_RETRST) && 2674 !(fin->fin_flx & FI_SHORT)) { 2675 2676 #if defined(_KERNEL) && (SOLARIS2 >= 10) 2677 /* 2678 * Assume it's possible to enter insane rule: 2679 * pass return-rst in proto tcp ... 2680 * then we have no other option than to forward 2681 * packet on loopback and give up any attempt 2682 * to create a fake response. 2683 */ 2684 if (IPF_IS_LOOPBACK(qpi->qpi_flags) && 2685 FR_ISBLOCK(pass)) { 2686 if (fr_make_rst(fin) == 0) { 2687 IPF_BUMP( 2688 ifs->ifs_frstats[out].fr_ret); 2689 } 2690 else if (mp != NULL) { 2691 /* 2692 * we drop packet silently in case we 2693 * failed assemble fake response for it 2694 */ 2695 FREE_MB_T(*mp); 2696 m = *mp = NULL; 2697 } 2698 2699 IPF_BUMP( 2700 ifs->ifs_frstats[out].fr_block); 2701 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2702 2703 return (0); 2704 } 2705 #endif /* _KERNEL && _SOLARIS2 >= 10 */ 2706 if (fr_send_reset(fin) == 0) { 2707 IPF_BUMP(ifs->ifs_frstats[1].fr_ret); 2708 } 2709 } 2710 } else { 2711 if (pass & FR_RETRST) 2712 fin->fin_error = ECONNRESET; 2713 } 2714 } 2715 2716 /* 2717 * If we didn't drop off the bottom of the list of rules (and thus 2718 * the 'current' rule fr is not NULL), then we may have some extra 2719 * instructions about what to do with a packet. 2720 * Once we're finished return to our caller, freeing the packet if 2721 * we are dropping it (* BSD ONLY *). 2722 * Reassign m from fin_m as we may have a new buffer, now. 2723 */ 2724 filtered: 2725 m = fin->fin_m; 2726 2727 if (fr != NULL) { 2728 frdest_t *fdp; 2729 2730 fdp = &fr->fr_tifs[fin->fin_rev]; 2731 2732 if (!out && (pass & FR_FASTROUTE)) { 2733 /* 2734 * For fastroute rule, no destioation interface defined 2735 * so pass NULL as the frdest_t parameter 2736 */ 2737 (void) fr_fastroute(m, mp, fin, NULL); 2738 m = *mp = NULL; 2739 } else if ((fdp->fd_ifp != NULL) && 2740 (fdp->fd_ifp != (struct ifnet *)-1)) { 2741 /* this is for to rules: */ 2742 (void) fr_fastroute(m, mp, fin, fdp); 2743 m = *mp = NULL; 2744 } 2745 2746 /* 2747 * Generate a duplicated packet. 2748 */ 2749 if (mc != NULL) 2750 (void) fr_fastroute(mc, &mc, fin, &fr->fr_dif); 2751 } 2752 2753 if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT)) 2754 nat_uncreate(fin); 2755 2756 /* 2757 * This late because the likes of fr_fastroute() use fin_fr. 2758 */ 2759 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 2760 2761 finished: 2762 if (!FR_ISPASS(pass)) { 2763 IPF_BUMP(ifs->ifs_frstats[out].fr_block); 2764 if (*mp != NULL) { 2765 FREE_MB_T(*mp); 2766 m = *mp = NULL; 2767 } 2768 } else { 2769 IPF_BUMP(ifs->ifs_frstats[out].fr_pass); 2770 #if defined(_KERNEL) && defined(__sgi) 2771 if ((fin->fin_hbuf != NULL) && 2772 (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) { 2773 COPYBACK(m, 0, fin->fin_plen, fin->fin_hbuf); 2774 } 2775 #endif 2776 } 2777 2778 SPL_X(s); 2779 2780 #ifdef _KERNEL 2781 # if OpenBSD >= 200311 2782 if (FR_ISPASS(pass) && (v == 4)) { 2783 ip = fin->fin_ip; 2784 ip->ip_len = ntohs(ip->ip_len); 2785 ip->ip_off = ntohs(ip->ip_off); 2786 } 2787 # endif 2788 return (FR_ISPASS(pass)) ? 0 : fin->fin_error; 2789 #else /* _KERNEL */ 2790 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass)); 2791 if ((pass & FR_NOMATCH) != 0) 2792 return 1; 2793 2794 if ((pass & FR_RETMASK) != 0) 2795 switch (pass & FR_RETMASK) 2796 { 2797 case FR_RETRST : 2798 return 3; 2799 case FR_RETICMP : 2800 return 4; 2801 case FR_FAKEICMP : 2802 return 5; 2803 } 2804 2805 switch (pass & FR_CMDMASK) 2806 { 2807 case FR_PASS : 2808 return 0; 2809 case FR_BLOCK : 2810 return -1; 2811 case FR_AUTH : 2812 return -2; 2813 case FR_ACCOUNT : 2814 return -3; 2815 case FR_PREAUTH : 2816 return -4; 2817 } 2818 return 2; 2819 #endif /* _KERNEL */ 2820 } 2821 2822 2823 #ifdef IPFILTER_LOG 2824 /* ------------------------------------------------------------------------ */ 2825 /* Function: fr_dolog */ 2826 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */ 2827 /* Parameters: fin(I) - pointer to packet information */ 2828 /* passp(IO) - pointer to current/new filter decision (unused) */ 2829 /* */ 2830 /* Checks flags set to see how a packet should be logged, if it is to be */ 2831 /* logged. Adjust statistics based on its success or not. */ 2832 /* ------------------------------------------------------------------------ */ 2833 frentry_t *fr_dolog(fin, passp) 2834 fr_info_t *fin; 2835 u_32_t *passp; 2836 { 2837 u_32_t pass; 2838 int out; 2839 ipf_stack_t *ifs = fin->fin_ifs; 2840 2841 out = fin->fin_out; 2842 pass = *passp; 2843 2844 if ((ifs->ifs_fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) { 2845 pass |= FF_LOGNOMATCH; 2846 IPF_BUMP(ifs->ifs_frstats[out].fr_npkl); 2847 goto logit; 2848 } else if (((pass & FR_LOGMASK) == FR_LOGP) || 2849 (FR_ISPASS(pass) && (ifs->ifs_fr_flags & FF_LOGPASS))) { 2850 if ((pass & FR_LOGMASK) != FR_LOGP) 2851 pass |= FF_LOGPASS; 2852 IPF_BUMP(ifs->ifs_frstats[out].fr_ppkl); 2853 goto logit; 2854 } else if (((pass & FR_LOGMASK) == FR_LOGB) || 2855 (FR_ISBLOCK(pass) && (ifs->ifs_fr_flags & FF_LOGBLOCK))) { 2856 if ((pass & FR_LOGMASK) != FR_LOGB) 2857 pass |= FF_LOGBLOCK; 2858 IPF_BUMP(ifs->ifs_frstats[out].fr_bpkl); 2859 logit: 2860 if (ipflog(fin, pass) == -1) { 2861 IPF_BUMP(ifs->ifs_frstats[out].fr_skip); 2862 2863 /* 2864 * If the "or-block" option has been used then 2865 * block the packet if we failed to log it. 2866 */ 2867 if ((pass & FR_LOGORBLOCK) && 2868 FR_ISPASS(pass)) { 2869 pass &= ~FR_CMDMASK; 2870 pass |= FR_BLOCK; 2871 } 2872 } 2873 *passp = pass; 2874 } 2875 2876 return fin->fin_fr; 2877 } 2878 #endif /* IPFILTER_LOG */ 2879 2880 2881 /* ------------------------------------------------------------------------ */ 2882 /* Function: ipf_cksum */ 2883 /* Returns: u_short - IP header checksum */ 2884 /* Parameters: addr(I) - pointer to start of buffer to checksum */ 2885 /* len(I) - length of buffer in bytes */ 2886 /* */ 2887 /* Calculate the two's complement 16 bit checksum of the buffer passed. */ 2888 /* */ 2889 /* N.B.: addr should be 16bit aligned. */ 2890 /* ------------------------------------------------------------------------ */ 2891 u_short ipf_cksum(addr, len) 2892 u_short *addr; 2893 int len; 2894 { 2895 u_32_t sum = 0; 2896 2897 for (sum = 0; len > 1; len -= 2) 2898 sum += *addr++; 2899 2900 /* mop up an odd byte, if necessary */ 2901 if (len == 1) 2902 sum += *(u_char *)addr; 2903 2904 /* 2905 * add back carry outs from top 16 bits to low 16 bits 2906 */ 2907 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 2908 sum += (sum >> 16); /* add carry */ 2909 return (u_short)(~sum); 2910 } 2911 2912 2913 /* ------------------------------------------------------------------------ */ 2914 /* Function: fr_cksum */ 2915 /* Returns: u_short - layer 4 checksum */ 2916 /* Parameters: m(I ) - pointer to buffer holding packet */ 2917 /* ip(I) - pointer to IP header */ 2918 /* l4proto(I) - protocol to caclulate checksum for */ 2919 /* l4hdr(I) - pointer to layer 4 header */ 2920 /* */ 2921 /* Calculates the TCP checksum for the packet held in "m", using the data */ 2922 /* in the IP header "ip" to seed it. */ 2923 /* */ 2924 /* NB: This function assumes we've pullup'd enough for all of the IP header */ 2925 /* and the TCP header. We also assume that data blocks aren't allocated in */ 2926 /* odd sizes. */ 2927 /* */ 2928 /* Expects ip_len to be in host byte order when called. */ 2929 /* ------------------------------------------------------------------------ */ 2930 u_short fr_cksum(m, ip, l4proto, l4hdr) 2931 mb_t *m; 2932 ip_t *ip; 2933 int l4proto; 2934 void *l4hdr; 2935 { 2936 u_short *sp, slen, sumsave, l4hlen, *csump; 2937 u_int sum, sum2; 2938 int hlen; 2939 #ifdef USE_INET6 2940 ip6_t *ip6; 2941 #endif 2942 2943 csump = NULL; 2944 sumsave = 0; 2945 l4hlen = 0; 2946 sp = NULL; 2947 slen = 0; 2948 hlen = 0; 2949 sum = 0; 2950 2951 /* 2952 * Add up IP Header portion 2953 */ 2954 #ifdef USE_INET6 2955 if (IP_V(ip) == 4) { 2956 #endif 2957 hlen = IP_HL(ip) << 2; 2958 slen = ip->ip_len - hlen; 2959 sum = htons((u_short)l4proto); 2960 sum += htons(slen); 2961 sp = (u_short *)&ip->ip_src; 2962 sum += *sp++; /* ip_src */ 2963 sum += *sp++; 2964 sum += *sp++; /* ip_dst */ 2965 sum += *sp++; 2966 #ifdef USE_INET6 2967 } else if (IP_V(ip) == 6) { 2968 ip6 = (ip6_t *)ip; 2969 hlen = sizeof(*ip6); 2970 slen = ntohs(ip6->ip6_plen); 2971 sum = htons((u_short)l4proto); 2972 sum += htons(slen); 2973 sp = (u_short *)&ip6->ip6_src; 2974 sum += *sp++; /* ip6_src */ 2975 sum += *sp++; 2976 sum += *sp++; 2977 sum += *sp++; 2978 sum += *sp++; 2979 sum += *sp++; 2980 sum += *sp++; 2981 sum += *sp++; 2982 sum += *sp++; /* ip6_dst */ 2983 sum += *sp++; 2984 sum += *sp++; 2985 sum += *sp++; 2986 sum += *sp++; 2987 sum += *sp++; 2988 sum += *sp++; 2989 sum += *sp++; 2990 } 2991 #endif 2992 2993 switch (l4proto) 2994 { 2995 case IPPROTO_UDP : 2996 csump = &((udphdr_t *)l4hdr)->uh_sum; 2997 l4hlen = sizeof(udphdr_t); 2998 break; 2999 3000 case IPPROTO_TCP : 3001 csump = &((tcphdr_t *)l4hdr)->th_sum; 3002 l4hlen = sizeof(tcphdr_t); 3003 break; 3004 case IPPROTO_ICMP : 3005 csump = &((icmphdr_t *)l4hdr)->icmp_cksum; 3006 l4hlen = 4; 3007 sum = 0; 3008 break; 3009 default : 3010 break; 3011 } 3012 3013 if (csump != NULL) { 3014 sumsave = *csump; 3015 *csump = 0; 3016 } 3017 3018 l4hlen = l4hlen; /* LINT */ 3019 3020 #ifdef _KERNEL 3021 # ifdef MENTAT 3022 { 3023 void *rp = m->b_rptr; 3024 3025 if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr) 3026 m->b_rptr = (u_char *)ip; 3027 sum2 = ip_cksum(m, hlen, sum); /* hlen == offset */ 3028 m->b_rptr = rp; 3029 sum2 = (sum2 & 0xffff) + (sum2 >> 16); 3030 sum2 = ~sum2 & 0xffff; 3031 } 3032 # else /* MENTAT */ 3033 # if defined(BSD) || defined(sun) 3034 # if BSD >= 199103 3035 m->m_data += hlen; 3036 # else 3037 m->m_off += hlen; 3038 # endif 3039 m->m_len -= hlen; 3040 sum2 = in_cksum(m, slen); 3041 m->m_len += hlen; 3042 # if BSD >= 199103 3043 m->m_data -= hlen; 3044 # else 3045 m->m_off -= hlen; 3046 # endif 3047 /* 3048 * Both sum and sum2 are partial sums, so combine them together. 3049 */ 3050 sum += ~sum2 & 0xffff; 3051 while (sum > 0xffff) 3052 sum = (sum & 0xffff) + (sum >> 16); 3053 sum2 = ~sum & 0xffff; 3054 # else /* defined(BSD) || defined(sun) */ 3055 { 3056 union { 3057 u_char c[2]; 3058 u_short s; 3059 } bytes; 3060 u_short len = ip->ip_len; 3061 # if defined(__sgi) 3062 int add; 3063 # endif 3064 3065 /* 3066 * Add up IP Header portion 3067 */ 3068 if (sp != (u_short *)l4hdr) 3069 sp = (u_short *)l4hdr; 3070 3071 switch (l4proto) 3072 { 3073 case IPPROTO_UDP : 3074 sum += *sp++; /* sport */ 3075 sum += *sp++; /* dport */ 3076 sum += *sp++; /* udp length */ 3077 sum += *sp++; /* checksum */ 3078 break; 3079 3080 case IPPROTO_TCP : 3081 sum += *sp++; /* sport */ 3082 sum += *sp++; /* dport */ 3083 sum += *sp++; /* seq */ 3084 sum += *sp++; 3085 sum += *sp++; /* ack */ 3086 sum += *sp++; 3087 sum += *sp++; /* off */ 3088 sum += *sp++; /* win */ 3089 sum += *sp++; /* checksum */ 3090 sum += *sp++; /* urp */ 3091 break; 3092 case IPPROTO_ICMP : 3093 sum = *sp++; /* type/code */ 3094 sum += *sp++; /* checksum */ 3095 break; 3096 } 3097 3098 # ifdef __sgi 3099 /* 3100 * In case we had to copy the IP & TCP header out of mbufs, 3101 * skip over the mbuf bits which are the header 3102 */ 3103 if ((caddr_t)ip != mtod(m, caddr_t)) { 3104 hlen = (caddr_t)sp - (caddr_t)ip; 3105 while (hlen) { 3106 add = MIN(hlen, m->m_len); 3107 sp = (u_short *)(mtod(m, caddr_t) + add); 3108 hlen -= add; 3109 if (add == m->m_len) { 3110 m = m->m_next; 3111 if (!hlen) { 3112 if (!m) 3113 break; 3114 sp = mtod(m, u_short *); 3115 } 3116 PANIC((!m),("fr_cksum(1): not enough data")); 3117 } 3118 } 3119 } 3120 # endif 3121 3122 len -= (l4hlen + hlen); 3123 if (len <= 0) 3124 goto nodata; 3125 3126 while (len > 1) { 3127 if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) { 3128 m = m->m_next; 3129 PANIC((!m),("fr_cksum(2): not enough data")); 3130 sp = mtod(m, u_short *); 3131 } 3132 if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) { 3133 bytes.c[0] = *(u_char *)sp; 3134 m = m->m_next; 3135 PANIC((!m),("fr_cksum(3): not enough data")); 3136 sp = mtod(m, u_short *); 3137 bytes.c[1] = *(u_char *)sp; 3138 sum += bytes.s; 3139 sp = (u_short *)((u_char *)sp + 1); 3140 } 3141 if ((u_long)sp & 1) { 3142 bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s)); 3143 sum += bytes.s; 3144 } else 3145 sum += *sp++; 3146 len -= 2; 3147 } 3148 3149 if (len != 0) 3150 sum += ntohs(*(u_char *)sp << 8); 3151 nodata: 3152 while (sum > 0xffff) 3153 sum = (sum & 0xffff) + (sum >> 16); 3154 sum2 = (u_short)(~sum & 0xffff); 3155 } 3156 # endif /* defined(BSD) || defined(sun) */ 3157 # endif /* MENTAT */ 3158 #else /* _KERNEL */ 3159 for (; slen > 1; slen -= 2) 3160 sum += *sp++; 3161 if (slen) 3162 sum += ntohs(*(u_char *)sp << 8); 3163 while (sum > 0xffff) 3164 sum = (sum & 0xffff) + (sum >> 16); 3165 sum2 = (u_short)(~sum & 0xffff); 3166 #endif /* _KERNEL */ 3167 if (csump != NULL) 3168 *csump = sumsave; 3169 return sum2; 3170 } 3171 3172 3173 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \ 3174 defined(__sgi) ) && !defined(linux) && !defined(_AIX51) 3175 /* 3176 * Copyright (c) 1982, 1986, 1988, 1991, 1993 3177 * The Regents of the University of California. All rights reserved. 3178 * 3179 * Redistribution and use in source and binary forms, with or without 3180 * modification, are permitted provided that the following conditions 3181 * are met: 3182 * 1. Redistributions of source code must retain the above copyright 3183 * notice, this list of conditions and the following disclaimer. 3184 * 2. Redistributions in binary form must reproduce the above copyright 3185 * notice, this list of conditions and the following disclaimer in the 3186 * documentation and/or other materials provided with the distribution. 3187 * 3. Neither the name of the University nor the names of its contributors 3188 * may be used to endorse or promote products derived from this software 3189 * without specific prior written permission. 3190 * 3191 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3192 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3193 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3194 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3195 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3196 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3197 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3198 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3199 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3200 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3201 * SUCH DAMAGE. 3202 * 3203 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 3204 * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $ 3205 */ 3206 /* 3207 * Copy data from an mbuf chain starting "off" bytes from the beginning, 3208 * continuing for "len" bytes, into the indicated buffer. 3209 */ 3210 void 3211 m_copydata(m, off, len, cp) 3212 mb_t *m; 3213 int off; 3214 int len; 3215 caddr_t cp; 3216 { 3217 unsigned count; 3218 3219 if (off < 0 || len < 0) 3220 panic("m_copydata"); 3221 while (off > 0) { 3222 if (m == 0) 3223 panic("m_copydata"); 3224 if (off < m->m_len) 3225 break; 3226 off -= m->m_len; 3227 m = m->m_next; 3228 } 3229 while (len > 0) { 3230 if (m == 0) 3231 panic("m_copydata"); 3232 count = MIN(m->m_len - off, len); 3233 bcopy(mtod(m, caddr_t) + off, cp, count); 3234 len -= count; 3235 cp += count; 3236 off = 0; 3237 m = m->m_next; 3238 } 3239 } 3240 3241 3242 /* 3243 * Copy data from a buffer back into the indicated mbuf chain, 3244 * starting "off" bytes from the beginning, extending the mbuf 3245 * chain if necessary. 3246 */ 3247 void 3248 m_copyback(m0, off, len, cp) 3249 struct mbuf *m0; 3250 int off; 3251 int len; 3252 caddr_t cp; 3253 { 3254 int mlen; 3255 struct mbuf *m = m0, *n; 3256 int totlen = 0; 3257 3258 if (m0 == 0) 3259 return; 3260 while (off > (mlen = m->m_len)) { 3261 off -= mlen; 3262 totlen += mlen; 3263 if (m->m_next == 0) { 3264 n = m_getclr(M_DONTWAIT, m->m_type); 3265 if (n == 0) 3266 goto out; 3267 n->m_len = min(MLEN, len + off); 3268 m->m_next = n; 3269 } 3270 m = m->m_next; 3271 } 3272 while (len > 0) { 3273 mlen = min(m->m_len - off, len); 3274 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 3275 cp += mlen; 3276 len -= mlen; 3277 mlen += off; 3278 off = 0; 3279 totlen += mlen; 3280 if (len == 0) 3281 break; 3282 if (m->m_next == 0) { 3283 n = m_get(M_DONTWAIT, m->m_type); 3284 if (n == 0) 3285 break; 3286 n->m_len = min(MLEN, len); 3287 m->m_next = n; 3288 } 3289 m = m->m_next; 3290 } 3291 out: 3292 #if 0 3293 if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 3294 m->m_pkthdr.len = totlen; 3295 #endif 3296 return; 3297 } 3298 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */ 3299 3300 3301 /* ------------------------------------------------------------------------ */ 3302 /* Function: fr_findgroup */ 3303 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */ 3304 /* Parameters: group(I) - group name to search for */ 3305 /* unit(I) - device to which this group belongs */ 3306 /* set(I) - which set of rules (inactive/inactive) this is */ 3307 /* fgpp(O) - pointer to place to store pointer to the pointer */ 3308 /* to where to add the next (last) group or where */ 3309 /* to delete group from. */ 3310 /* */ 3311 /* Search amongst the defined groups for a particular group number. */ 3312 /* ------------------------------------------------------------------------ */ 3313 frgroup_t *fr_findgroup(group, unit, set, fgpp, ifs) 3314 char *group; 3315 minor_t unit; 3316 int set; 3317 frgroup_t ***fgpp; 3318 ipf_stack_t *ifs; 3319 { 3320 frgroup_t *fg, **fgp; 3321 3322 /* 3323 * Which list of groups to search in is dependent on which list of 3324 * rules are being operated on. 3325 */ 3326 fgp = &ifs->ifs_ipfgroups[unit][set]; 3327 3328 while ((fg = *fgp) != NULL) { 3329 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0) 3330 break; 3331 else 3332 fgp = &fg->fg_next; 3333 } 3334 if (fgpp != NULL) 3335 *fgpp = fgp; 3336 return fg; 3337 } 3338 3339 3340 /* ------------------------------------------------------------------------ */ 3341 /* Function: fr_addgroup */ 3342 /* Returns: frgroup_t * - NULL == did not create group, */ 3343 /* != NULL == pointer to the group */ 3344 /* Parameters: num(I) - group number to add */ 3345 /* head(I) - rule pointer that is using this as the head */ 3346 /* flags(I) - rule flags which describe the type of rule it is */ 3347 /* unit(I) - device to which this group will belong to */ 3348 /* set(I) - which set of rules (inactive/inactive) this is */ 3349 /* Write Locks: ipf_mutex */ 3350 /* */ 3351 /* Add a new group head, or if it already exists, increase the reference */ 3352 /* count to it. */ 3353 /* ------------------------------------------------------------------------ */ 3354 frgroup_t *fr_addgroup(group, head, flags, unit, set, ifs) 3355 char *group; 3356 void *head; 3357 u_32_t flags; 3358 minor_t unit; 3359 int set; 3360 ipf_stack_t *ifs; 3361 { 3362 frgroup_t *fg, **fgp; 3363 u_32_t gflags; 3364 3365 if (group == NULL) 3366 return NULL; 3367 3368 if (unit == IPL_LOGIPF && *group == '\0') 3369 return NULL; 3370 3371 fgp = NULL; 3372 gflags = flags & FR_INOUT; 3373 3374 fg = fr_findgroup(group, unit, set, &fgp, ifs); 3375 if (fg != NULL) { 3376 if (fg->fg_flags == 0) 3377 fg->fg_flags = gflags; 3378 else if (gflags != fg->fg_flags) 3379 return NULL; 3380 fg->fg_ref++; 3381 return fg; 3382 } 3383 KMALLOC(fg, frgroup_t *); 3384 if (fg != NULL) { 3385 fg->fg_head = head; 3386 fg->fg_start = NULL; 3387 fg->fg_next = *fgp; 3388 bcopy(group, fg->fg_name, FR_GROUPLEN); 3389 fg->fg_flags = gflags; 3390 fg->fg_ref = 1; 3391 *fgp = fg; 3392 } 3393 return fg; 3394 } 3395 3396 3397 /* ------------------------------------------------------------------------ */ 3398 /* Function: fr_delgroup */ 3399 /* Returns: Nil */ 3400 /* Parameters: group(I) - group name to delete */ 3401 /* unit(I) - device to which this group belongs */ 3402 /* set(I) - which set of rules (inactive/inactive) this is */ 3403 /* Write Locks: ipf_mutex */ 3404 /* */ 3405 /* Attempt to delete a group head. */ 3406 /* Only do this when its reference count reaches 0. */ 3407 /* ------------------------------------------------------------------------ */ 3408 void fr_delgroup(group, unit, set, ifs) 3409 char *group; 3410 minor_t unit; 3411 int set; 3412 ipf_stack_t *ifs; 3413 { 3414 frgroup_t *fg, **fgp; 3415 3416 fg = fr_findgroup(group, unit, set, &fgp, ifs); 3417 if (fg == NULL) 3418 return; 3419 3420 fg->fg_ref--; 3421 if (fg->fg_ref == 0) { 3422 *fgp = fg->fg_next; 3423 KFREE(fg); 3424 } 3425 } 3426 3427 3428 /* ------------------------------------------------------------------------ */ 3429 /* Function: fr_getrulen */ 3430 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */ 3431 /* Parameters: unit(I) - device for which to count the rule's number */ 3432 /* flags(I) - which set of rules to find the rule in */ 3433 /* group(I) - group name */ 3434 /* n(I) - rule number to find */ 3435 /* */ 3436 /* Find rule # n in group # g and return a pointer to it. Return NULl if */ 3437 /* group # g doesn't exist or there are less than n rules in the group. */ 3438 /* ------------------------------------------------------------------------ */ 3439 frentry_t *fr_getrulen(unit, group, n, ifs) 3440 int unit; 3441 char *group; 3442 u_32_t n; 3443 ipf_stack_t *ifs; 3444 { 3445 frentry_t *fr; 3446 frgroup_t *fg; 3447 3448 fg = fr_findgroup(group, unit, ifs->ifs_fr_active, NULL, ifs); 3449 if (fg == NULL) 3450 return NULL; 3451 for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--) 3452 ; 3453 if (n != 0) 3454 return NULL; 3455 return fr; 3456 } 3457 3458 3459 /* ------------------------------------------------------------------------ */ 3460 /* Function: fr_rulen */ 3461 /* Returns: int - >= 0 - rule number, -1 == search failed */ 3462 /* Parameters: unit(I) - device for which to count the rule's number */ 3463 /* fr(I) - pointer to rule to match */ 3464 /* */ 3465 /* Return the number for a rule on a specific filtering device. */ 3466 /* ------------------------------------------------------------------------ */ 3467 int fr_rulen(unit, fr, ifs) 3468 int unit; 3469 frentry_t *fr; 3470 ipf_stack_t *ifs; 3471 { 3472 frentry_t *fh; 3473 frgroup_t *fg; 3474 u_32_t n = 0; 3475 3476 if (fr == NULL) 3477 return -1; 3478 fg = fr_findgroup(fr->fr_group, unit, ifs->ifs_fr_active, NULL, ifs); 3479 if (fg == NULL) 3480 return -1; 3481 for (fh = fg->fg_head; fh; n++, fh = fh->fr_next) 3482 if (fh == fr) 3483 break; 3484 if (fh == NULL) 3485 return -1; 3486 return n; 3487 } 3488 3489 3490 /* ------------------------------------------------------------------------ */ 3491 /* Function: frflushlist */ 3492 /* Returns: int - >= 0 - number of flushed rules */ 3493 /* Parameters: set(I) - which set of rules (inactive/inactive) this is */ 3494 /* unit(I) - device for which to flush rules */ 3495 /* flags(I) - which set of rules to flush */ 3496 /* nfreedp(O) - pointer to int where flush count is stored */ 3497 /* listp(I) - pointer to list to flush pointer */ 3498 /* Write Locks: ipf_mutex */ 3499 /* */ 3500 /* Recursively flush rules from the list, descending groups as they are */ 3501 /* encountered. if a rule is the head of a group and it has lost all its */ 3502 /* group members, then also delete the group reference. nfreedp is needed */ 3503 /* to store the accumulating count of rules removed, whereas the returned */ 3504 /* value is just the number removed from the current list. The latter is */ 3505 /* needed to correctly adjust reference counts on rules that define groups. */ 3506 /* */ 3507 /* NOTE: Rules not loaded from user space cannot be flushed. */ 3508 /* ------------------------------------------------------------------------ */ 3509 static int frflushlist(set, unit, nfreedp, listp, ifs) 3510 int set; 3511 minor_t unit; 3512 int *nfreedp; 3513 frentry_t **listp; 3514 ipf_stack_t *ifs; 3515 { 3516 int freed = 0; 3517 frentry_t *fp; 3518 3519 while ((fp = *listp) != NULL) { 3520 if ((fp->fr_type & FR_T_BUILTIN) || 3521 !(fp->fr_flags & FR_COPIED)) { 3522 listp = &fp->fr_next; 3523 continue; 3524 } 3525 *listp = fp->fr_next; 3526 if (fp->fr_grp != NULL) { 3527 (void) frflushlist(set, unit, nfreedp, fp->fr_grp, ifs); 3528 } 3529 3530 if (fp->fr_grhead != NULL) { 3531 fr_delgroup(fp->fr_grhead, unit, set, ifs); 3532 *fp->fr_grhead = '\0'; 3533 } 3534 3535 ASSERT(fp->fr_ref > 0); 3536 fp->fr_next = NULL; 3537 if (fr_derefrule(&fp, ifs) == 0) 3538 freed++; 3539 } 3540 *nfreedp += freed; 3541 return freed; 3542 } 3543 3544 3545 /* ------------------------------------------------------------------------ */ 3546 /* Function: frflush */ 3547 /* Returns: int - >= 0 - number of flushed rules */ 3548 /* Parameters: unit(I) - device for which to flush rules */ 3549 /* flags(I) - which set of rules to flush */ 3550 /* */ 3551 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */ 3552 /* and IPv6) as defined by the value of flags. */ 3553 /* ------------------------------------------------------------------------ */ 3554 int frflush(unit, proto, flags, ifs) 3555 minor_t unit; 3556 int proto, flags; 3557 ipf_stack_t *ifs; 3558 { 3559 int flushed = 0, set; 3560 3561 WRITE_ENTER(&ifs->ifs_ipf_mutex); 3562 bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache)); 3563 3564 set = ifs->ifs_fr_active; 3565 if ((flags & FR_INACTIVE) == FR_INACTIVE) 3566 set = 1 - set; 3567 3568 if (flags & FR_OUTQUE) { 3569 if (proto == 0 || proto == 6) { 3570 (void) frflushlist(set, unit, 3571 &flushed, &ifs->ifs_ipfilter6[1][set], ifs); 3572 (void) frflushlist(set, unit, 3573 &flushed, &ifs->ifs_ipacct6[1][set], ifs); 3574 } 3575 if (proto == 0 || proto == 4) { 3576 (void) frflushlist(set, unit, 3577 &flushed, &ifs->ifs_ipfilter[1][set], ifs); 3578 (void) frflushlist(set, unit, 3579 &flushed, &ifs->ifs_ipacct[1][set], ifs); 3580 } 3581 } 3582 if (flags & FR_INQUE) { 3583 if (proto == 0 || proto == 6) { 3584 (void) frflushlist(set, unit, 3585 &flushed, &ifs->ifs_ipfilter6[0][set], ifs); 3586 (void) frflushlist(set, unit, 3587 &flushed, &ifs->ifs_ipacct6[0][set], ifs); 3588 } 3589 if (proto == 0 || proto == 4) { 3590 (void) frflushlist(set, unit, 3591 &flushed, &ifs->ifs_ipfilter[0][set], ifs); 3592 (void) frflushlist(set, unit, 3593 &flushed, &ifs->ifs_ipacct[0][set], ifs); 3594 } 3595 } 3596 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 3597 3598 if (unit == IPL_LOGIPF) { 3599 int tmp; 3600 3601 tmp = frflush(IPL_LOGCOUNT, proto, flags, ifs); 3602 if (tmp >= 0) 3603 flushed += tmp; 3604 } 3605 return flushed; 3606 } 3607 3608 3609 /* ------------------------------------------------------------------------ */ 3610 /* Function: memstr */ 3611 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */ 3612 /* Parameters: src(I) - pointer to byte sequence to match */ 3613 /* dst(I) - pointer to byte sequence to search */ 3614 /* slen(I) - match length */ 3615 /* dlen(I) - length available to search in */ 3616 /* */ 3617 /* Search dst for a sequence of bytes matching those at src and extend for */ 3618 /* slen bytes. */ 3619 /* ------------------------------------------------------------------------ */ 3620 char *memstr(src, dst, slen, dlen) 3621 char *src, *dst; 3622 int slen, dlen; 3623 { 3624 char *s = NULL; 3625 3626 while (dlen >= slen) { 3627 if (bcmp(src, dst, slen) == 0) { 3628 s = dst; 3629 break; 3630 } 3631 dst++; 3632 dlen--; 3633 } 3634 return s; 3635 } 3636 /* ------------------------------------------------------------------------ */ 3637 /* Function: fr_fixskip */ 3638 /* Returns: Nil */ 3639 /* Parameters: listp(IO) - pointer to start of list with skip rule */ 3640 /* rp(I) - rule added/removed with skip in it. */ 3641 /* addremove(I) - adjustment (-1/+1) to make to skip count, */ 3642 /* depending on whether a rule was just added */ 3643 /* or removed. */ 3644 /* */ 3645 /* Adjust all the rules in a list which would have skip'd past the position */ 3646 /* where we are inserting to skip to the right place given the change. */ 3647 /* ------------------------------------------------------------------------ */ 3648 void fr_fixskip(listp, rp, addremove) 3649 frentry_t **listp, *rp; 3650 int addremove; 3651 { 3652 int rules, rn; 3653 frentry_t *fp; 3654 3655 rules = 0; 3656 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next) 3657 rules++; 3658 3659 if (!fp) 3660 return; 3661 3662 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++) 3663 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules)) 3664 fp->fr_arg += addremove; 3665 } 3666 3667 3668 #ifdef _KERNEL 3669 /* ------------------------------------------------------------------------ */ 3670 /* Function: count4bits */ 3671 /* Returns: int - >= 0 - number of consecutive bits in input */ 3672 /* Parameters: ip(I) - 32bit IP address */ 3673 /* */ 3674 /* IPv4 ONLY */ 3675 /* count consecutive 1's in bit mask. If the mask generated by counting */ 3676 /* consecutive 1's is different to that passed, return -1, else return # */ 3677 /* of bits. */ 3678 /* ------------------------------------------------------------------------ */ 3679 int count4bits(ip) 3680 u_32_t ip; 3681 { 3682 u_32_t ipn; 3683 int cnt = 0, i, j; 3684 3685 ip = ipn = ntohl(ip); 3686 for (i = 32; i; i--, ipn *= 2) 3687 if (ipn & 0x80000000) 3688 cnt++; 3689 else 3690 break; 3691 ipn = 0; 3692 for (i = 32, j = cnt; i; i--, j--) { 3693 ipn *= 2; 3694 if (j > 0) 3695 ipn++; 3696 } 3697 if (ipn == ip) 3698 return cnt; 3699 return -1; 3700 } 3701 3702 3703 #ifdef USE_INET6 3704 /* ------------------------------------------------------------------------ */ 3705 /* Function: count6bits */ 3706 /* Returns: int - >= 0 - number of consecutive bits in input */ 3707 /* Parameters: msk(I) - pointer to start of IPv6 bitmask */ 3708 /* */ 3709 /* IPv6 ONLY */ 3710 /* count consecutive 1's in bit mask. */ 3711 /* ------------------------------------------------------------------------ */ 3712 int count6bits(msk) 3713 u_32_t *msk; 3714 { 3715 int i = 0, k; 3716 u_32_t j; 3717 3718 for (k = 3; k >= 0; k--) 3719 if (msk[k] == 0xffffffff) 3720 i += 32; 3721 else { 3722 for (j = msk[k]; j; j <<= 1) 3723 if (j & 0x80000000) 3724 i++; 3725 } 3726 return i; 3727 } 3728 # endif 3729 #endif /* _KERNEL */ 3730 3731 3732 /* ------------------------------------------------------------------------ */ 3733 /* Function: fr_ifsync */ 3734 /* Returns: void * - new interface identifier */ 3735 /* Parameters: action(I) - type of synchronisation to do */ 3736 /* v(I) - IP version being sync'd (v4 or v6) */ 3737 /* newifp(I) - interface identifier being introduced/removed */ 3738 /* oldifp(I) - interface identifier in a filter rule */ 3739 /* newname(I) - name associated with newifp interface */ 3740 /* oldname(I) - name associated with oldifp interface */ 3741 /* ifs - pointer to IPF stack instance */ 3742 /* */ 3743 /* This function returns what the new value for "oldifp" should be for its */ 3744 /* caller. In some cases it will not change, in some it will. */ 3745 /* action == IPFSYNC_RESYNC */ 3746 /* a new value for oldifp will always be looked up, according to oldname, */ 3747 /* the values of newname and newifp are ignored. */ 3748 /* action == IPFSYNC_NEWIFP */ 3749 /* if oldname matches newname then we are doing a sync for the matching */ 3750 /* interface, so we return newifp to be used in place of oldifp. If the */ 3751 /* the names don't match, just return oldifp. */ 3752 /* action == IPFSYNC_OLDIFP */ 3753 /* if oldifp matches newifp then we are are doing a sync to remove any */ 3754 /* references to oldifp, so we return "-1". */ 3755 /* ----- */ 3756 /* NOTE: */ 3757 /* This function processes NIC event from PF_HOOKS. The action parameter */ 3758 /* is set in ipf_nic_event_v4()/ipf_nic_event_v6() function. There is */ 3759 /* one single switch statement() in ipf_nic_event_vx() function, which */ 3760 /* translates the HOOK event type to action parameter passed to fr_ifsync. */ 3761 /* The translation table looks as follows: */ 3762 /* event | action */ 3763 /* ----------------+------------- */ 3764 /* NE_PLUMB | IPFSYNC_NEWIFP */ 3765 /* NE_UNPLUMB | IPFSYNC_OLDIFP */ 3766 /* NE_ADDRESS_CHANGE | IPFSYNC_RESYNC */ 3767 /* */ 3768 /* The oldname and oldifp parameters are taken from IPF entry (rule, state */ 3769 /* table entry, NAT table entry, fragment ...). The newname and newifp */ 3770 /* parameters come from hook event data, parameters are taken from event */ 3771 /* in ipf_nic_event_vx() functions. Any time NIC changes, the IPF is */ 3772 /* notified by hook function. */ 3773 /* */ 3774 /* We get NE_UNPLUMB event from PF_HOOKS even if someone coincidently tries */ 3775 /* to plumb the interface, which is already plumbed. In such case we always */ 3776 /* get the event from PF_HOOKS as follows: */ 3777 /* event: NE_PLUMB */ 3778 /* NIC: 0x0 */ 3779 /* ------------------------------------------------------------------------ */ 3780 static void *fr_ifsync(action, v, newname, oldname, newifp, oldifp, ifs) 3781 int action, v; 3782 char *newname, *oldname; 3783 void *newifp, *oldifp; 3784 ipf_stack_t *ifs; 3785 { 3786 void *rval = oldifp; 3787 3788 switch (action) 3789 { 3790 case IPFSYNC_RESYNC : 3791 if (oldname[0] != '\0') { 3792 rval = fr_resolvenic(oldname, v, ifs); 3793 } 3794 break; 3795 case IPFSYNC_NEWIFP : 3796 if (!strncmp(newname, oldname, LIFNAMSIZ)) 3797 rval = newifp; 3798 break; 3799 case IPFSYNC_OLDIFP : 3800 /* 3801 * If interface gets unplumbed it must be invalidated, which 3802 * means set all existing references to the interface to -1. 3803 * We don't want to invalidate references for wildcard 3804 * (unbound) rules (entries). 3805 */ 3806 if (newifp == oldifp) 3807 rval = (oldifp) ? (void *)-1 : NULL; 3808 break; 3809 } 3810 3811 return rval; 3812 } 3813 3814 3815 /* ------------------------------------------------------------------------ */ 3816 /* Function: frsynclist */ 3817 /* Returns: void */ 3818 /* Parameters: action(I) - type of synchronisation to do */ 3819 /* v(I) - IP version being sync'd (v4 or v6) */ 3820 /* ifp(I) - interface identifier associated with action */ 3821 /* ifname(I) - name associated with ifp parameter */ 3822 /* fr(I) - pointer to filter rule */ 3823 /* ifs - pointer to IPF stack instance */ 3824 /* Write Locks: ipf_mutex */ 3825 /* */ 3826 /* Walk through a list of filter rules and resolve any interface names into */ 3827 /* pointers. Where dynamic addresses are used, also update the IP address */ 3828 /* used in the rule. The interface pointer is used to limit the lookups to */ 3829 /* a specific set of matching names if it is non-NULL. */ 3830 /* ------------------------------------------------------------------------ */ 3831 static void frsynclist(action, v, ifp, ifname, fr, ifs) 3832 int action, v; 3833 void *ifp; 3834 char *ifname; 3835 frentry_t *fr; 3836 ipf_stack_t *ifs; 3837 { 3838 frdest_t *fdp; 3839 int rv, i; 3840 3841 for (; fr; fr = fr->fr_next) { 3842 rv = fr->fr_v; 3843 if (v != 0 && v != rv) 3844 continue; 3845 3846 /* 3847 * Lookup all the interface names that are part of the rule. 3848 */ 3849 for (i = 0; i < 4; i++) { 3850 fr->fr_ifas[i] = fr_ifsync(action, rv, ifname, 3851 fr->fr_ifnames[i], 3852 ifp, fr->fr_ifas[i], 3853 ifs); 3854 } 3855 3856 fdp = &fr->fr_tifs[0]; 3857 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 3858 ifp, fdp->fd_ifp, ifs); 3859 3860 fdp = &fr->fr_tifs[1]; 3861 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 3862 ifp, fdp->fd_ifp, ifs); 3863 3864 fdp = &fr->fr_dif; 3865 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 3866 ifp, fdp->fd_ifp, ifs); 3867 3868 if (action != IPFSYNC_RESYNC) 3869 continue; 3870 3871 if (fr->fr_type == FR_T_IPF) { 3872 if (fr->fr_satype != FRI_NORMAL && 3873 fr->fr_satype != FRI_LOOKUP) { 3874 (void)fr_ifpaddr(rv, fr->fr_satype, 3875 fr->fr_ifas[fr->fr_sifpidx], 3876 &fr->fr_src, &fr->fr_smsk, 3877 ifs); 3878 } 3879 if (fr->fr_datype != FRI_NORMAL && 3880 fr->fr_datype != FRI_LOOKUP) { 3881 (void)fr_ifpaddr(rv, fr->fr_datype, 3882 fr->fr_ifas[fr->fr_difpidx], 3883 &fr->fr_dst, &fr->fr_dmsk, 3884 ifs); 3885 } 3886 } 3887 3888 #ifdef IPFILTER_LOOKUP 3889 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP && 3890 fr->fr_srcptr == NULL) { 3891 fr->fr_srcptr = fr_resolvelookup(fr->fr_srctype, 3892 fr->fr_srcnum, 3893 &fr->fr_srcfunc, ifs); 3894 } 3895 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP && 3896 fr->fr_dstptr == NULL) { 3897 fr->fr_dstptr = fr_resolvelookup(fr->fr_dsttype, 3898 fr->fr_dstnum, 3899 &fr->fr_dstfunc, ifs); 3900 } 3901 #endif 3902 } 3903 } 3904 3905 3906 #ifdef _KERNEL 3907 /* ------------------------------------------------------------------------ */ 3908 /* Function: frsync */ 3909 /* Returns: void */ 3910 /* Parameters: action(I) - type of synchronisation to do */ 3911 /* v(I) - IP version being sync'd (v4 or v6) */ 3912 /* ifp(I) - interface identifier associated with action */ 3913 /* name(I) - name associated with ifp parameter */ 3914 /* */ 3915 /* frsync() is called when we suspect that the interface list or */ 3916 /* information about interfaces (like IP#) has changed. Go through all */ 3917 /* filter rules, NAT entries and the state table and check if anything */ 3918 /* needs to be changed/updated. */ 3919 /* With the filtering hooks added to Solaris, we needed to change the manner*/ 3920 /* in which this was done to support three different types of sync: */ 3921 /* - complete resync of all interface name/identifiers */ 3922 /* - new interface being announced with its name and identifier */ 3923 /* - interface removal being announced by only its identifier */ 3924 /* ------------------------------------------------------------------------ */ 3925 void frsync(action, v, ifp, name, ifs) 3926 int action, v; 3927 void *ifp; 3928 char *name; 3929 ipf_stack_t *ifs; 3930 { 3931 int i; 3932 3933 WRITE_ENTER(&ifs->ifs_ipf_mutex); 3934 frsynclist(action, v, ifp, name, ifs->ifs_ipacct[0][ifs->ifs_fr_active], ifs); 3935 frsynclist(action, v, ifp, name, ifs->ifs_ipacct[1][ifs->ifs_fr_active], ifs); 3936 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[0][ifs->ifs_fr_active], ifs); 3937 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[1][ifs->ifs_fr_active], ifs); 3938 frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[0][ifs->ifs_fr_active], ifs); 3939 frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[1][ifs->ifs_fr_active], ifs); 3940 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[0][ifs->ifs_fr_active], ifs); 3941 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[1][ifs->ifs_fr_active], ifs); 3942 3943 for (i = 0; i < IPL_LOGSIZE; i++) { 3944 frgroup_t *g; 3945 3946 for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next) 3947 frsynclist(action, v, ifp, name, g->fg_start, ifs); 3948 for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next) 3949 frsynclist(action, v, ifp, name, g->fg_start, ifs); 3950 } 3951 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 3952 } 3953 3954 3955 /* 3956 * In the functions below, bcopy() is called because the pointer being 3957 * copied _from_ in this instance is a pointer to a char buf (which could 3958 * end up being unaligned) and on the kernel's local stack. 3959 */ 3960 /* ------------------------------------------------------------------------ */ 3961 /* Function: copyinptr */ 3962 /* Returns: int - 0 = success, else failure */ 3963 /* Parameters: src(I) - pointer to the source address */ 3964 /* dst(I) - destination address */ 3965 /* size(I) - number of bytes to copy */ 3966 /* */ 3967 /* Copy a block of data in from user space, given a pointer to the pointer */ 3968 /* to start copying from (src) and a pointer to where to store it (dst). */ 3969 /* NB: src - pointer to user space pointer, dst - kernel space pointer */ 3970 /* ------------------------------------------------------------------------ */ 3971 int copyinptr(src, dst, size) 3972 void *src, *dst; 3973 size_t size; 3974 { 3975 caddr_t ca; 3976 int err; 3977 3978 # if SOLARIS 3979 err = COPYIN(src, (caddr_t)&ca, sizeof(ca)); 3980 if (err != 0) 3981 return err; 3982 # else 3983 bcopy(src, (caddr_t)&ca, sizeof(ca)); 3984 # endif 3985 err = COPYIN(ca, dst, size); 3986 return err; 3987 } 3988 3989 3990 /* ------------------------------------------------------------------------ */ 3991 /* Function: copyoutptr */ 3992 /* Returns: int - 0 = success, else failure */ 3993 /* Parameters: src(I) - pointer to the source address */ 3994 /* dst(I) - destination address */ 3995 /* size(I) - number of bytes to copy */ 3996 /* */ 3997 /* Copy a block of data out to user space, given a pointer to the pointer */ 3998 /* to start copying from (src) and a pointer to where to store it (dst). */ 3999 /* NB: src - kernel space pointer, dst - pointer to user space pointer. */ 4000 /* ------------------------------------------------------------------------ */ 4001 int copyoutptr(src, dst, size) 4002 void *src, *dst; 4003 size_t size; 4004 { 4005 caddr_t ca; 4006 int err; 4007 4008 # if SOLARIS 4009 err = COPYIN(dst, (caddr_t)&ca, sizeof(ca)); 4010 if (err != 0) 4011 return err; 4012 # else 4013 bcopy(dst, (caddr_t)&ca, sizeof(ca)); 4014 # endif 4015 err = COPYOUT(src, ca, size); 4016 return err; 4017 } 4018 #endif 4019 4020 4021 /* ------------------------------------------------------------------------ */ 4022 /* Function: fr_lock */ 4023 /* Returns: int - 0 = success, else error */ 4024 /* Parameters: data(I) - pointer to lock value to set */ 4025 /* lockp(O) - pointer to location to store old lock value */ 4026 /* */ 4027 /* Get the new value for the lock integer, set it and return the old value */ 4028 /* in *lockp. */ 4029 /* ------------------------------------------------------------------------ */ 4030 int fr_lock(data, lockp) 4031 caddr_t data; 4032 int *lockp; 4033 { 4034 int arg, err; 4035 4036 err = BCOPYIN(data, (caddr_t)&arg, sizeof(arg)); 4037 if (err != 0) 4038 return (EFAULT); 4039 err = BCOPYOUT((caddr_t)lockp, data, sizeof(*lockp)); 4040 if (err != 0) 4041 return (EFAULT); 4042 *lockp = arg; 4043 return (0); 4044 } 4045 4046 4047 /* ------------------------------------------------------------------------ */ 4048 /* Function: fr_getstat */ 4049 /* Returns: Nil */ 4050 /* Parameters: fiop(I) - pointer to ipfilter stats structure */ 4051 /* */ 4052 /* Stores a copy of current pointers, counters, etc, in the friostat */ 4053 /* structure. */ 4054 /* ------------------------------------------------------------------------ */ 4055 void fr_getstat(fiop, ifs) 4056 friostat_t *fiop; 4057 ipf_stack_t *ifs; 4058 { 4059 int i, j; 4060 4061 bcopy((char *)&ifs->ifs_frstats, (char *)fiop->f_st, 4062 sizeof(filterstats_t) * 2); 4063 fiop->f_locks[IPL_LOGSTATE] = ifs->ifs_fr_state_lock; 4064 fiop->f_locks[IPL_LOGNAT] = ifs->ifs_fr_nat_lock; 4065 fiop->f_locks[IPL_LOGIPF] = ifs->ifs_fr_frag_lock; 4066 fiop->f_locks[IPL_LOGAUTH] = ifs->ifs_fr_auth_lock; 4067 4068 for (i = 0; i < 2; i++) 4069 for (j = 0; j < 2; j++) { 4070 fiop->f_ipf[i][j] = ifs->ifs_ipfilter[i][j]; 4071 fiop->f_acct[i][j] = ifs->ifs_ipacct[i][j]; 4072 fiop->f_ipf6[i][j] = ifs->ifs_ipfilter6[i][j]; 4073 fiop->f_acct6[i][j] = ifs->ifs_ipacct6[i][j]; 4074 } 4075 4076 fiop->f_ticks = ifs->ifs_fr_ticks; 4077 fiop->f_active = ifs->ifs_fr_active; 4078 fiop->f_froute[0] = ifs->ifs_fr_frouteok[0]; 4079 fiop->f_froute[1] = ifs->ifs_fr_frouteok[1]; 4080 4081 fiop->f_running = ifs->ifs_fr_running; 4082 for (i = 0; i < IPL_LOGSIZE; i++) { 4083 fiop->f_groups[i][0] = ifs->ifs_ipfgroups[i][0]; 4084 fiop->f_groups[i][1] = ifs->ifs_ipfgroups[i][1]; 4085 } 4086 #ifdef IPFILTER_LOG 4087 fiop->f_logging = 1; 4088 #else 4089 fiop->f_logging = 0; 4090 #endif 4091 fiop->f_defpass = ifs->ifs_fr_pass; 4092 fiop->f_features = fr_features; 4093 (void) strncpy(fiop->f_version, ipfilter_version, 4094 sizeof(fiop->f_version)); 4095 } 4096 4097 4098 #ifdef USE_INET6 4099 int icmptoicmp6types[ICMP_MAXTYPE+1] = { 4100 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */ 4101 -1, /* 1: UNUSED */ 4102 -1, /* 2: UNUSED */ 4103 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */ 4104 -1, /* 4: ICMP_SOURCEQUENCH */ 4105 ND_REDIRECT, /* 5: ICMP_REDIRECT */ 4106 -1, /* 6: UNUSED */ 4107 -1, /* 7: UNUSED */ 4108 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */ 4109 -1, /* 9: UNUSED */ 4110 -1, /* 10: UNUSED */ 4111 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */ 4112 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */ 4113 -1, /* 13: ICMP_TSTAMP */ 4114 -1, /* 14: ICMP_TSTAMPREPLY */ 4115 -1, /* 15: ICMP_IREQ */ 4116 -1, /* 16: ICMP_IREQREPLY */ 4117 -1, /* 17: ICMP_MASKREQ */ 4118 -1, /* 18: ICMP_MASKREPLY */ 4119 }; 4120 4121 4122 int icmptoicmp6unreach[ICMP_MAX_UNREACH] = { 4123 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */ 4124 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */ 4125 -1, /* 2: ICMP_UNREACH_PROTOCOL */ 4126 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */ 4127 -1, /* 4: ICMP_UNREACH_NEEDFRAG */ 4128 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */ 4129 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */ 4130 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */ 4131 -1, /* 8: ICMP_UNREACH_ISOLATED */ 4132 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */ 4133 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */ 4134 -1, /* 11: ICMP_UNREACH_TOSNET */ 4135 -1, /* 12: ICMP_UNREACH_TOSHOST */ 4136 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */ 4137 }; 4138 int icmpreplytype6[ICMP6_MAXTYPE + 1]; 4139 #endif 4140 4141 int icmpreplytype4[ICMP_MAXTYPE + 1]; 4142 4143 4144 /* ------------------------------------------------------------------------ */ 4145 /* Function: fr_matchicmpqueryreply */ 4146 /* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */ 4147 /* Parameters: v(I) - IP protocol version (4 or 6) */ 4148 /* ic(I) - ICMP information */ 4149 /* icmp(I) - ICMP packet header */ 4150 /* rev(I) - direction (0 = forward/1 = reverse) of packet */ 4151 /* */ 4152 /* Check if the ICMP packet defined by the header pointed to by icmp is a */ 4153 /* reply to one as described by what's in ic. If it is a match, return 1, */ 4154 /* else return 0 for no match. */ 4155 /* ------------------------------------------------------------------------ */ 4156 int fr_matchicmpqueryreply(v, ic, icmp, rev) 4157 int v; 4158 icmpinfo_t *ic; 4159 icmphdr_t *icmp; 4160 int rev; 4161 { 4162 int ictype; 4163 4164 ictype = ic->ici_type; 4165 4166 if (v == 4) { 4167 /* 4168 * If we matched its type on the way in, then when going out 4169 * it will still be the same type. 4170 */ 4171 if ((!rev && (icmp->icmp_type == ictype)) || 4172 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) { 4173 if (icmp->icmp_type != ICMP_ECHOREPLY) 4174 return 1; 4175 if (icmp->icmp_id == ic->ici_id) 4176 return 1; 4177 } 4178 } 4179 #ifdef USE_INET6 4180 else if (v == 6) { 4181 if ((!rev && (icmp->icmp_type == ictype)) || 4182 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) { 4183 if (icmp->icmp_type != ICMP6_ECHO_REPLY) 4184 return 1; 4185 if (icmp->icmp_id == ic->ici_id) 4186 return 1; 4187 } 4188 } 4189 #endif 4190 return 0; 4191 } 4192 4193 4194 #ifdef IPFILTER_LOOKUP 4195 /* ------------------------------------------------------------------------ */ 4196 /* Function: fr_resolvelookup */ 4197 /* Returns: void * - NULL = failure, else success. */ 4198 /* Parameters: type(I) - type of lookup these parameters are for. */ 4199 /* number(I) - table number to use when searching */ 4200 /* funcptr(IO) - pointer to pointer for storing IP address */ 4201 /* searching function. */ 4202 /* */ 4203 /* Search for the "table" number passed in amongst those configured for */ 4204 /* that particular type. If the type is recognised then the function to */ 4205 /* call to do the IP address search will be change, regardless of whether */ 4206 /* or not the "table" number exists. */ 4207 /* ------------------------------------------------------------------------ */ 4208 static void *fr_resolvelookup(type, number, funcptr, ifs) 4209 u_int type, number; 4210 lookupfunc_t *funcptr; 4211 ipf_stack_t *ifs; 4212 { 4213 char name[FR_GROUPLEN]; 4214 iphtable_t *iph; 4215 ip_pool_t *ipo; 4216 void *ptr; 4217 4218 #if defined(SNPRINTF) && defined(_KERNEL) 4219 (void) SNPRINTF(name, sizeof(name), "%u", number); 4220 #else 4221 (void) sprintf(name, "%u", number); 4222 #endif 4223 4224 READ_ENTER(&ifs->ifs_ip_poolrw); 4225 4226 switch (type) 4227 { 4228 case IPLT_POOL : 4229 # if (defined(__osf__) && defined(_KERNEL)) 4230 ptr = NULL; 4231 *funcptr = NULL; 4232 # else 4233 ipo = ip_pool_find(IPL_LOGIPF, name, ifs); 4234 ptr = ipo; 4235 if (ipo != NULL) { 4236 ATOMIC_INC32(ipo->ipo_ref); 4237 } 4238 *funcptr = ip_pool_search; 4239 # endif 4240 break; 4241 case IPLT_HASH : 4242 iph = fr_findhtable(IPL_LOGIPF, name, ifs); 4243 ptr = iph; 4244 if (iph != NULL) { 4245 ATOMIC_INC32(iph->iph_ref); 4246 } 4247 *funcptr = fr_iphmfindip; 4248 break; 4249 default: 4250 ptr = NULL; 4251 *funcptr = NULL; 4252 break; 4253 } 4254 RWLOCK_EXIT(&ifs->ifs_ip_poolrw); 4255 4256 return ptr; 4257 } 4258 #endif 4259 4260 4261 /* ------------------------------------------------------------------------ */ 4262 /* Function: frrequest */ 4263 /* Returns: int - 0 == success, > 0 == errno value */ 4264 /* Parameters: unit(I) - device for which this is for */ 4265 /* req(I) - ioctl command (SIOC*) */ 4266 /* data(I) - pointr to ioctl data */ 4267 /* set(I) - 1 or 0 (filter set) */ 4268 /* makecopy(I) - flag indicating whether data points to a rule */ 4269 /* in kernel space & hence doesn't need copying. */ 4270 /* */ 4271 /* This function handles all the requests which operate on the list of */ 4272 /* filter rules. This includes adding, deleting, insertion. It is also */ 4273 /* responsible for creating groups when a "head" rule is loaded. Interface */ 4274 /* names are resolved here and other sanity checks are made on the content */ 4275 /* of the rule structure being loaded. If a rule has user defined timeouts */ 4276 /* then make sure they are created and initialised before exiting. */ 4277 /* ------------------------------------------------------------------------ */ 4278 int frrequest(unit, req, data, set, makecopy, ifs) 4279 int unit; 4280 ioctlcmd_t req; 4281 int set, makecopy; 4282 caddr_t data; 4283 ipf_stack_t *ifs; 4284 { 4285 frentry_t frd, *fp, *f, **fprev, **ftail; 4286 int error = 0, in, v; 4287 void *ptr, *uptr; 4288 u_int *p, *pp; 4289 frgroup_t *fg; 4290 char *group; 4291 4292 fg = NULL; 4293 fp = &frd; 4294 if (makecopy != 0) { 4295 error = fr_inobj(data, fp, IPFOBJ_FRENTRY); 4296 if (error) 4297 return EFAULT; 4298 if ((fp->fr_flags & FR_T_BUILTIN) != 0) 4299 return EINVAL; 4300 fp->fr_ref = 0; 4301 fp->fr_flags |= FR_COPIED; 4302 } else { 4303 fp = (frentry_t *)data; 4304 if ((fp->fr_type & FR_T_BUILTIN) == 0) 4305 return EINVAL; 4306 fp->fr_flags &= ~FR_COPIED; 4307 } 4308 4309 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) || 4310 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) 4311 return EINVAL; 4312 4313 v = fp->fr_v; 4314 uptr = fp->fr_data; 4315 4316 /* 4317 * Only filter rules for IPv4 or IPv6 are accepted. 4318 */ 4319 if (v == 4) 4320 /*EMPTY*/; 4321 #ifdef USE_INET6 4322 else if (v == 6) 4323 /*EMPTY*/; 4324 #endif 4325 else { 4326 return EINVAL; 4327 } 4328 4329 /* 4330 * If the rule is being loaded from user space, i.e. we had to copy it 4331 * into kernel space, then do not trust the function pointer in the 4332 * rule. 4333 */ 4334 if ((makecopy == 1) && (fp->fr_func != NULL)) { 4335 if (fr_findfunc(fp->fr_func) == NULL) 4336 return ESRCH; 4337 error = fr_funcinit(fp, ifs); 4338 if (error != 0) 4339 return error; 4340 } 4341 4342 ptr = NULL; 4343 /* 4344 * Check that the group number does exist and that its use (in/out) 4345 * matches what the rule is. 4346 */ 4347 if (!strncmp(fp->fr_grhead, "0", FR_GROUPLEN)) 4348 *fp->fr_grhead = '\0'; 4349 group = fp->fr_group; 4350 if (!strncmp(group, "0", FR_GROUPLEN)) 4351 *group = '\0'; 4352 4353 if (FR_ISACCOUNT(fp->fr_flags)) 4354 unit = IPL_LOGCOUNT; 4355 4356 if ((req != (int)SIOCZRLST) && (*group != '\0')) { 4357 fg = fr_findgroup(group, unit, set, NULL, ifs); 4358 if (fg == NULL) 4359 return ESRCH; 4360 if (fg->fg_flags == 0) 4361 fg->fg_flags = fp->fr_flags & FR_INOUT; 4362 else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) 4363 return ESRCH; 4364 } 4365 4366 in = (fp->fr_flags & FR_INQUE) ? 0 : 1; 4367 4368 /* 4369 * Work out which rule list this change is being applied to. 4370 */ 4371 ftail = NULL; 4372 fprev = NULL; 4373 if (unit == IPL_LOGAUTH) 4374 fprev = &ifs->ifs_ipauth; 4375 else if (v == 4) { 4376 if (FR_ISACCOUNT(fp->fr_flags)) 4377 fprev = &ifs->ifs_ipacct[in][set]; 4378 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 4379 fprev = &ifs->ifs_ipfilter[in][set]; 4380 } else if (v == 6) { 4381 if (FR_ISACCOUNT(fp->fr_flags)) 4382 fprev = &ifs->ifs_ipacct6[in][set]; 4383 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 4384 fprev = &ifs->ifs_ipfilter6[in][set]; 4385 } 4386 if (fprev == NULL) 4387 return ESRCH; 4388 4389 if (*group != '\0') { 4390 if (!fg && !(fg = fr_findgroup(group, unit, set, NULL, ifs))) 4391 return ESRCH; 4392 fprev = &fg->fg_start; 4393 } 4394 4395 ftail = fprev; 4396 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) { 4397 if (fp->fr_collect <= f->fr_collect) { 4398 ftail = fprev; 4399 f = NULL; 4400 break; 4401 } 4402 fprev = ftail; 4403 } 4404 4405 /* 4406 * Copy in extra data for the rule. 4407 */ 4408 if (fp->fr_dsize != 0) { 4409 if (makecopy != 0) { 4410 KMALLOCS(ptr, void *, fp->fr_dsize); 4411 if (!ptr) 4412 return ENOMEM; 4413 error = COPYIN(uptr, ptr, fp->fr_dsize); 4414 } else { 4415 ptr = uptr; 4416 error = 0; 4417 } 4418 if (error != 0) { 4419 KFREES(ptr, fp->fr_dsize); 4420 return EFAULT; 4421 } 4422 fp->fr_data = ptr; 4423 } else 4424 fp->fr_data = NULL; 4425 4426 /* 4427 * Perform per-rule type sanity checks of their members. 4428 */ 4429 switch (fp->fr_type & ~FR_T_BUILTIN) 4430 { 4431 #if defined(IPFILTER_BPF) 4432 case FR_T_BPFOPC : 4433 if (fp->fr_dsize == 0) 4434 return EINVAL; 4435 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) { 4436 if (makecopy && fp->fr_data != NULL) { 4437 KFREES(fp->fr_data, fp->fr_dsize); 4438 } 4439 return EINVAL; 4440 } 4441 break; 4442 #endif 4443 case FR_T_IPF : 4444 if (fp->fr_dsize != sizeof(fripf_t)) { 4445 if (makecopy && fp->fr_data != NULL) { 4446 KFREES(fp->fr_data, fp->fr_dsize); 4447 } 4448 return EINVAL; 4449 } 4450 4451 /* 4452 * Allowing a rule with both "keep state" and "with oow" is 4453 * pointless because adding a state entry to the table will 4454 * fail with the out of window (oow) flag set. 4455 */ 4456 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) { 4457 if (makecopy && fp->fr_data != NULL) { 4458 KFREES(fp->fr_data, fp->fr_dsize); 4459 } 4460 return EINVAL; 4461 } 4462 4463 switch (fp->fr_satype) 4464 { 4465 case FRI_BROADCAST : 4466 case FRI_DYNAMIC : 4467 case FRI_NETWORK : 4468 case FRI_NETMASKED : 4469 case FRI_PEERADDR : 4470 if (fp->fr_sifpidx < 0 || fp->fr_sifpidx > 3) { 4471 if (makecopy && fp->fr_data != NULL) { 4472 KFREES(fp->fr_data, fp->fr_dsize); 4473 } 4474 return EINVAL; 4475 } 4476 break; 4477 #ifdef IPFILTER_LOOKUP 4478 case FRI_LOOKUP : 4479 fp->fr_srcptr = fr_resolvelookup(fp->fr_srctype, 4480 fp->fr_srcnum, 4481 &fp->fr_srcfunc, ifs); 4482 break; 4483 #endif 4484 default : 4485 break; 4486 } 4487 4488 switch (fp->fr_datype) 4489 { 4490 case FRI_BROADCAST : 4491 case FRI_DYNAMIC : 4492 case FRI_NETWORK : 4493 case FRI_NETMASKED : 4494 case FRI_PEERADDR : 4495 if (fp->fr_difpidx < 0 || fp->fr_difpidx > 3) { 4496 if (makecopy && fp->fr_data != NULL) { 4497 KFREES(fp->fr_data, fp->fr_dsize); 4498 } 4499 return EINVAL; 4500 } 4501 break; 4502 #ifdef IPFILTER_LOOKUP 4503 case FRI_LOOKUP : 4504 fp->fr_dstptr = fr_resolvelookup(fp->fr_dsttype, 4505 fp->fr_dstnum, 4506 &fp->fr_dstfunc, ifs); 4507 break; 4508 #endif 4509 default : 4510 break; 4511 } 4512 break; 4513 case FR_T_NONE : 4514 break; 4515 case FR_T_CALLFUNC : 4516 break; 4517 case FR_T_COMPIPF : 4518 break; 4519 default : 4520 if (makecopy && fp->fr_data != NULL) { 4521 KFREES(fp->fr_data, fp->fr_dsize); 4522 } 4523 return EINVAL; 4524 } 4525 4526 /* 4527 * Lookup all the interface names that are part of the rule. 4528 */ 4529 frsynclist(0, 0, NULL, NULL, fp, ifs); 4530 fp->fr_statecnt = 0; 4531 4532 /* 4533 * Look for an existing matching filter rule, but don't include the 4534 * next or interface pointer in the comparison (fr_next, fr_ifa). 4535 * This elminates rules which are indentical being loaded. Checksum 4536 * the constant part of the filter rule to make comparisons quicker 4537 * (this meaning no pointers are included). 4538 */ 4539 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum; 4540 p < pp; p++) 4541 fp->fr_cksum += *p; 4542 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize); 4543 for (p = (u_int *)fp->fr_data; p < pp; p++) 4544 fp->fr_cksum += *p; 4545 4546 WRITE_ENTER(&ifs->ifs_ipf_mutex); 4547 bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache)); 4548 4549 for (; (f = *ftail) != NULL; ftail = &f->fr_next) { 4550 if ((fp->fr_cksum != f->fr_cksum) || 4551 (f->fr_dsize != fp->fr_dsize)) 4552 continue; 4553 if (bcmp((char *)&f->fr_func, (char *)&fp->fr_func, FR_CMPSIZ)) 4554 continue; 4555 if ((!ptr && !f->fr_data) || 4556 (ptr && f->fr_data && 4557 !bcmp((char *)ptr, (char *)f->fr_data, f->fr_dsize))) 4558 break; 4559 } 4560 4561 /* 4562 * If zero'ing statistics, copy current to caller and zero. 4563 */ 4564 if (req == (ioctlcmd_t)SIOCZRLST) { 4565 if (f == NULL) 4566 error = ESRCH; 4567 else { 4568 /* 4569 * Copy and reduce lock because of impending copyout. 4570 * Well we should, but if we do then the atomicity of 4571 * this call and the correctness of fr_hits and 4572 * fr_bytes cannot be guaranteed. As it is, this code 4573 * only resets them to 0 if they are successfully 4574 * copied out into user space. 4575 */ 4576 bcopy((char *)f, (char *)fp, sizeof(*f)); 4577 4578 /* 4579 * When we copy this rule back out, set the data 4580 * pointer to be what it was in user space. 4581 */ 4582 fp->fr_data = uptr; 4583 error = fr_outobj(data, fp, IPFOBJ_FRENTRY); 4584 4585 if (error == 0) { 4586 if ((f->fr_dsize != 0) && (uptr != NULL)) 4587 error = COPYOUT(f->fr_data, uptr, 4588 f->fr_dsize); 4589 if (error == 0) { 4590 f->fr_hits = 0; 4591 f->fr_bytes = 0; 4592 } 4593 } 4594 } 4595 4596 if ((ptr != NULL) && (makecopy != 0)) { 4597 KFREES(ptr, fp->fr_dsize); 4598 } 4599 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 4600 return error; 4601 } 4602 4603 if (!f) { 4604 /* 4605 * At the end of this, ftail must point to the place where the 4606 * new rule is to be saved/inserted/added. 4607 * For SIOCAD*FR, this should be the last rule in the group of 4608 * rules that have equal fr_collect fields. 4609 * For SIOCIN*FR, ... 4610 */ 4611 if (req == (ioctlcmd_t)SIOCADAFR || 4612 req == (ioctlcmd_t)SIOCADIFR) { 4613 4614 for (ftail = fprev; (f = *ftail) != NULL; ) { 4615 if (f->fr_collect > fp->fr_collect) 4616 break; 4617 ftail = &f->fr_next; 4618 } 4619 f = NULL; 4620 ptr = NULL; 4621 error = 0; 4622 } else if (req == (ioctlcmd_t)SIOCINAFR || 4623 req == (ioctlcmd_t)SIOCINIFR) { 4624 while ((f = *fprev) != NULL) { 4625 if (f->fr_collect >= fp->fr_collect) 4626 break; 4627 fprev = &f->fr_next; 4628 } 4629 ftail = fprev; 4630 if (fp->fr_hits != 0) { 4631 while (fp->fr_hits && (f = *ftail)) { 4632 if (f->fr_collect != fp->fr_collect) 4633 break; 4634 fprev = ftail; 4635 ftail = &f->fr_next; 4636 fp->fr_hits--; 4637 } 4638 } 4639 f = NULL; 4640 ptr = NULL; 4641 error = 0; 4642 } 4643 } 4644 4645 /* 4646 * Request to remove a rule. 4647 */ 4648 if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) { 4649 if (!f) 4650 error = ESRCH; 4651 else { 4652 /* 4653 * Do not allow activity from user space to interfere 4654 * with rules not loaded that way. 4655 */ 4656 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) { 4657 error = EPERM; 4658 goto done; 4659 } 4660 4661 /* 4662 * Return EBUSY if the rule is being reference by 4663 * something else (eg state information. 4664 */ 4665 if (f->fr_ref > 1) { 4666 error = EBUSY; 4667 goto done; 4668 } 4669 #ifdef IPFILTER_SCAN 4670 if (f->fr_isctag[0] != '\0' && 4671 (f->fr_isc != (struct ipscan *)-1)) 4672 ipsc_detachfr(f); 4673 #endif 4674 if (unit == IPL_LOGAUTH) { 4675 error = fr_preauthcmd(req, f, ftail, ifs); 4676 goto done; 4677 } 4678 if (*f->fr_grhead != '\0') 4679 fr_delgroup(f->fr_grhead, unit, set, ifs); 4680 fr_fixskip(ftail, f, -1); 4681 *ftail = f->fr_next; 4682 f->fr_next = NULL; 4683 (void)fr_derefrule(&f, ifs); 4684 } 4685 } else { 4686 /* 4687 * Not removing, so we must be adding/inserting a rule. 4688 */ 4689 if (f) 4690 error = EEXIST; 4691 else { 4692 if (unit == IPL_LOGAUTH) { 4693 error = fr_preauthcmd(req, fp, ftail, ifs); 4694 goto done; 4695 } 4696 if (makecopy) { 4697 KMALLOC(f, frentry_t *); 4698 } else 4699 f = fp; 4700 if (f != NULL) { 4701 if (fp != f) 4702 bcopy((char *)fp, (char *)f, 4703 sizeof(*f)); 4704 MUTEX_NUKE(&f->fr_lock); 4705 MUTEX_INIT(&f->fr_lock, "filter rule lock"); 4706 #ifdef IPFILTER_SCAN 4707 if (f->fr_isctag[0] != '\0' && 4708 ipsc_attachfr(f)) 4709 f->fr_isc = (struct ipscan *)-1; 4710 #endif 4711 f->fr_hits = 0; 4712 if (makecopy != 0) 4713 f->fr_ref = 1; 4714 f->fr_next = *ftail; 4715 *ftail = f; 4716 if (req == (ioctlcmd_t)SIOCINIFR || 4717 req == (ioctlcmd_t)SIOCINAFR) 4718 fr_fixskip(ftail, f, 1); 4719 f->fr_grp = NULL; 4720 group = f->fr_grhead; 4721 if (*group != '\0') { 4722 fg = fr_addgroup(group, f, f->fr_flags, 4723 unit, set, ifs); 4724 if (fg != NULL) 4725 f->fr_grp = &fg->fg_start; 4726 } 4727 } else 4728 error = ENOMEM; 4729 } 4730 } 4731 done: 4732 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 4733 if ((ptr != NULL) && (error != 0) && (makecopy != 0)) { 4734 KFREES(ptr, fp->fr_dsize); 4735 } 4736 return (error); 4737 } 4738 4739 4740 /* ------------------------------------------------------------------------ */ 4741 /* Function: fr_funcinit */ 4742 /* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */ 4743 /* Parameters: fr(I) - pointer to filter rule */ 4744 /* */ 4745 /* If a rule is a call rule, then check if the function it points to needs */ 4746 /* an init function to be called now the rule has been loaded. */ 4747 /* ------------------------------------------------------------------------ */ 4748 static int fr_funcinit(fr, ifs) 4749 frentry_t *fr; 4750 ipf_stack_t *ifs; 4751 { 4752 ipfunc_resolve_t *ft; 4753 int err; 4754 4755 err = ESRCH; 4756 4757 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 4758 if (ft->ipfu_addr == fr->fr_func) { 4759 err = 0; 4760 if (ft->ipfu_init != NULL) 4761 err = (*ft->ipfu_init)(fr, ifs); 4762 break; 4763 } 4764 return err; 4765 } 4766 4767 4768 /* ------------------------------------------------------------------------ */ 4769 /* Function: fr_findfunc */ 4770 /* Returns: ipfunc_t - pointer to function if found, else NULL */ 4771 /* Parameters: funcptr(I) - function pointer to lookup */ 4772 /* */ 4773 /* Look for a function in the table of known functions. */ 4774 /* ------------------------------------------------------------------------ */ 4775 static ipfunc_t fr_findfunc(funcptr) 4776 ipfunc_t funcptr; 4777 { 4778 ipfunc_resolve_t *ft; 4779 4780 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 4781 if (ft->ipfu_addr == funcptr) 4782 return funcptr; 4783 return NULL; 4784 } 4785 4786 4787 /* ------------------------------------------------------------------------ */ 4788 /* Function: fr_resolvefunc */ 4789 /* Returns: int - 0 == success, else error */ 4790 /* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */ 4791 /* */ 4792 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */ 4793 /* This will either be the function name (if the pointer is set) or the */ 4794 /* function pointer if the name is set. When found, fill in the other one */ 4795 /* so that the entire, complete, structure can be copied back to user space.*/ 4796 /* ------------------------------------------------------------------------ */ 4797 int fr_resolvefunc(data) 4798 void *data; 4799 { 4800 ipfunc_resolve_t res, *ft; 4801 int err; 4802 4803 err = BCOPYIN(data, &res, sizeof(res)); 4804 if (err != 0) 4805 return EFAULT; 4806 4807 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') { 4808 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 4809 if (strncmp(res.ipfu_name, ft->ipfu_name, 4810 sizeof(res.ipfu_name)) == 0) { 4811 res.ipfu_addr = ft->ipfu_addr; 4812 res.ipfu_init = ft->ipfu_init; 4813 if (COPYOUT(&res, data, sizeof(res)) != 0) 4814 return EFAULT; 4815 return 0; 4816 } 4817 } 4818 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') { 4819 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 4820 if (ft->ipfu_addr == res.ipfu_addr) { 4821 (void) strncpy(res.ipfu_name, ft->ipfu_name, 4822 sizeof(res.ipfu_name)); 4823 res.ipfu_init = ft->ipfu_init; 4824 if (COPYOUT(&res, data, sizeof(res)) != 0) 4825 return EFAULT; 4826 return 0; 4827 } 4828 } 4829 return ESRCH; 4830 } 4831 4832 4833 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \ 4834 (defined(__FreeBSD__) && (__FreeBSD_version < 490000)) || \ 4835 (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \ 4836 (defined(__OpenBSD__) && (OpenBSD < 200006)) 4837 /* 4838 * From: NetBSD 4839 * ppsratecheck(): packets (or events) per second limitation. 4840 */ 4841 int 4842 ppsratecheck(lasttime, curpps, maxpps) 4843 struct timeval *lasttime; 4844 int *curpps; 4845 int maxpps; /* maximum pps allowed */ 4846 { 4847 struct timeval tv, delta; 4848 int rv; 4849 4850 GETKTIME(&tv); 4851 4852 delta.tv_sec = tv.tv_sec - lasttime->tv_sec; 4853 delta.tv_usec = tv.tv_usec - lasttime->tv_usec; 4854 if (delta.tv_usec < 0) { 4855 delta.tv_sec--; 4856 delta.tv_usec += 1000000; 4857 } 4858 4859 /* 4860 * check for 0,0 is so that the message will be seen at least once. 4861 * if more than one second have passed since the last update of 4862 * lasttime, reset the counter. 4863 * 4864 * we do increment *curpps even in *curpps < maxpps case, as some may 4865 * try to use *curpps for stat purposes as well. 4866 */ 4867 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 4868 delta.tv_sec >= 1) { 4869 *lasttime = tv; 4870 *curpps = 0; 4871 rv = 1; 4872 } else if (maxpps < 0) 4873 rv = 1; 4874 else if (*curpps < maxpps) 4875 rv = 1; 4876 else 4877 rv = 0; 4878 *curpps = *curpps + 1; 4879 4880 return (rv); 4881 } 4882 #endif 4883 4884 4885 /* ------------------------------------------------------------------------ */ 4886 /* Function: fr_derefrule */ 4887 /* Returns: int - 0 == rule freed up, else rule not freed */ 4888 /* Parameters: fr(I) - pointer to filter rule */ 4889 /* */ 4890 /* Decrement the reference counter to a rule by one. If it reaches zero, */ 4891 /* free it and any associated storage space being used by it. */ 4892 /* ------------------------------------------------------------------------ */ 4893 int fr_derefrule(frp, ifs) 4894 frentry_t **frp; 4895 ipf_stack_t *ifs; 4896 { 4897 frentry_t *fr; 4898 4899 fr = *frp; 4900 4901 MUTEX_ENTER(&fr->fr_lock); 4902 fr->fr_ref--; 4903 if (fr->fr_ref == 0) { 4904 MUTEX_EXIT(&fr->fr_lock); 4905 MUTEX_DESTROY(&fr->fr_lock); 4906 4907 #ifdef IPFILTER_LOOKUP 4908 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP) 4909 ip_lookup_deref(fr->fr_srctype, fr->fr_srcptr, ifs); 4910 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP) 4911 ip_lookup_deref(fr->fr_dsttype, fr->fr_dstptr, ifs); 4912 #endif 4913 4914 if (fr->fr_dsize) { 4915 KFREES(fr->fr_data, fr->fr_dsize); 4916 } 4917 if ((fr->fr_flags & FR_COPIED) != 0) { 4918 KFREE(fr); 4919 return 0; 4920 } 4921 return 1; 4922 } else { 4923 MUTEX_EXIT(&fr->fr_lock); 4924 } 4925 *frp = NULL; 4926 return -1; 4927 } 4928 4929 4930 #ifdef IPFILTER_LOOKUP 4931 /* ------------------------------------------------------------------------ */ 4932 /* Function: fr_grpmapinit */ 4933 /* Returns: int - 0 == success, else ESRCH because table entry not found*/ 4934 /* Parameters: fr(I) - pointer to rule to find hash table for */ 4935 /* */ 4936 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */ 4937 /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap. */ 4938 /* ------------------------------------------------------------------------ */ 4939 static int fr_grpmapinit(fr, ifs) 4940 frentry_t *fr; 4941 ipf_stack_t *ifs; 4942 { 4943 char name[FR_GROUPLEN]; 4944 iphtable_t *iph; 4945 4946 #if defined(SNPRINTF) && defined(_KERNEL) 4947 (void) SNPRINTF(name, sizeof(name), "%d", fr->fr_arg); 4948 #else 4949 (void) sprintf(name, "%d", fr->fr_arg); 4950 #endif 4951 iph = fr_findhtable(IPL_LOGIPF, name, ifs); 4952 if (iph == NULL) 4953 return ESRCH; 4954 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) 4955 return ESRCH; 4956 fr->fr_ptr = iph; 4957 return 0; 4958 } 4959 4960 4961 /* ------------------------------------------------------------------------ */ 4962 /* Function: fr_srcgrpmap */ 4963 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 4964 /* Parameters: fin(I) - pointer to packet information */ 4965 /* passp(IO) - pointer to current/new filter decision (unused) */ 4966 /* */ 4967 /* Look for a rule group head in a hash table, using the source address as */ 4968 /* the key, and descend into that group and continue matching rules against */ 4969 /* the packet. */ 4970 /* ------------------------------------------------------------------------ */ 4971 frentry_t *fr_srcgrpmap(fin, passp) 4972 fr_info_t *fin; 4973 u_32_t *passp; 4974 { 4975 frgroup_t *fg; 4976 void *rval; 4977 ipf_stack_t *ifs = fin->fin_ifs; 4978 4979 rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_src, ifs); 4980 if (rval == NULL) 4981 return NULL; 4982 4983 fg = rval; 4984 fin->fin_fr = fg->fg_start; 4985 (void) fr_scanlist(fin, *passp); 4986 return fin->fin_fr; 4987 } 4988 4989 4990 /* ------------------------------------------------------------------------ */ 4991 /* Function: fr_dstgrpmap */ 4992 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 4993 /* Parameters: fin(I) - pointer to packet information */ 4994 /* passp(IO) - pointer to current/new filter decision (unused) */ 4995 /* */ 4996 /* Look for a rule group head in a hash table, using the destination */ 4997 /* address as the key, and descend into that group and continue matching */ 4998 /* rules against the packet. */ 4999 /* ------------------------------------------------------------------------ */ 5000 frentry_t *fr_dstgrpmap(fin, passp) 5001 fr_info_t *fin; 5002 u_32_t *passp; 5003 { 5004 frgroup_t *fg; 5005 void *rval; 5006 ipf_stack_t *ifs = fin->fin_ifs; 5007 5008 rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_dst, ifs); 5009 if (rval == NULL) 5010 return NULL; 5011 5012 fg = rval; 5013 fin->fin_fr = fg->fg_start; 5014 (void) fr_scanlist(fin, *passp); 5015 return fin->fin_fr; 5016 } 5017 #endif /* IPFILTER_LOOKUP */ 5018 5019 /* 5020 * Queue functions 5021 * =============== 5022 * These functions manage objects on queues for efficient timeouts. There are 5023 * a number of system defined queues as well as user defined timeouts. It is 5024 * expected that a lock is held in the domain in which the queue belongs 5025 * (i.e. either state or NAT) when calling any of these functions that prevents 5026 * fr_freetimeoutqueue() from being called at the same time as any other. 5027 */ 5028 5029 5030 /* ------------------------------------------------------------------------ */ 5031 /* Function: fr_addtimeoutqueue */ 5032 /* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */ 5033 /* timeout queue with given interval. */ 5034 /* Parameters: parent(I) - pointer to pointer to parent node of this list */ 5035 /* of interface queues. */ 5036 /* seconds(I) - timeout value in seconds for this queue. */ 5037 /* */ 5038 /* This routine first looks for a timeout queue that matches the interval */ 5039 /* being requested. If it finds one, increments the reference counter and */ 5040 /* returns a pointer to it. If none are found, it allocates a new one and */ 5041 /* inserts it at the top of the list. */ 5042 /* */ 5043 /* Locking. */ 5044 /* It is assumed that the caller of this function has an appropriate lock */ 5045 /* held (exclusively) in the domain that encompases 'parent'. */ 5046 /* ------------------------------------------------------------------------ */ 5047 ipftq_t *fr_addtimeoutqueue(parent, seconds, ifs) 5048 ipftq_t **parent; 5049 u_int seconds; 5050 ipf_stack_t *ifs; 5051 { 5052 ipftq_t *ifq; 5053 u_int period; 5054 5055 period = seconds * IPF_HZ_DIVIDE; 5056 5057 MUTEX_ENTER(&ifs->ifs_ipf_timeoutlock); 5058 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) { 5059 if (ifq->ifq_ttl == period) { 5060 /* 5061 * Reset the delete flag, if set, so the structure 5062 * gets reused rather than freed and reallocated. 5063 */ 5064 MUTEX_ENTER(&ifq->ifq_lock); 5065 ifq->ifq_flags &= ~IFQF_DELETE; 5066 ifq->ifq_ref++; 5067 MUTEX_EXIT(&ifq->ifq_lock); 5068 MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock); 5069 5070 return ifq; 5071 } 5072 } 5073 5074 KMALLOC(ifq, ipftq_t *); 5075 if (ifq != NULL) { 5076 ifq->ifq_ttl = period; 5077 ifq->ifq_head = NULL; 5078 ifq->ifq_tail = &ifq->ifq_head; 5079 ifq->ifq_next = *parent; 5080 ifq->ifq_pnext = parent; 5081 ifq->ifq_ref = 1; 5082 ifq->ifq_flags = IFQF_USER; 5083 *parent = ifq; 5084 ifs->ifs_fr_userifqs++; 5085 MUTEX_NUKE(&ifq->ifq_lock); 5086 MUTEX_INIT(&ifq->ifq_lock, "ipftq mutex"); 5087 } 5088 MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock); 5089 return ifq; 5090 } 5091 5092 5093 /* ------------------------------------------------------------------------ */ 5094 /* Function: fr_deletetimeoutqueue */ 5095 /* Returns: int - new reference count value of the timeout queue */ 5096 /* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5097 /* Locks: ifq->ifq_lock */ 5098 /* */ 5099 /* This routine must be called when we're discarding a pointer to a timeout */ 5100 /* queue object, taking care of the reference counter. */ 5101 /* */ 5102 /* Now that this just sets a DELETE flag, it requires the expire code to */ 5103 /* check the list of user defined timeout queues and call the free function */ 5104 /* below (currently commented out) to stop memory leaking. It is done this */ 5105 /* way because the locking may not be sufficient to safely do a free when */ 5106 /* this function is called. */ 5107 /* ------------------------------------------------------------------------ */ 5108 int fr_deletetimeoutqueue(ifq) 5109 ipftq_t *ifq; 5110 { 5111 5112 ifq->ifq_ref--; 5113 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) { 5114 ifq->ifq_flags |= IFQF_DELETE; 5115 } 5116 5117 return ifq->ifq_ref; 5118 } 5119 5120 5121 /* ------------------------------------------------------------------------ */ 5122 /* Function: fr_freetimeoutqueue */ 5123 /* Parameters: ifq(I) - timeout queue which is losing a reference. */ 5124 /* Returns: Nil */ 5125 /* */ 5126 /* Locking: */ 5127 /* It is assumed that the caller of this function has an appropriate lock */ 5128 /* held (exclusively) in the domain that encompases the callers "domain". */ 5129 /* The ifq_lock for this structure should not be held. */ 5130 /* */ 5131 /* Remove a user definde timeout queue from the list of queues it is in and */ 5132 /* tidy up after this is done. */ 5133 /* ------------------------------------------------------------------------ */ 5134 void fr_freetimeoutqueue(ifq, ifs) 5135 ipftq_t *ifq; 5136 ipf_stack_t *ifs; 5137 { 5138 5139 5140 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) || 5141 ((ifq->ifq_flags & IFQF_USER) == 0)) { 5142 printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n", 5143 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl, 5144 ifq->ifq_ref); 5145 return; 5146 } 5147 5148 /* 5149 * Remove from its position in the list. 5150 */ 5151 *ifq->ifq_pnext = ifq->ifq_next; 5152 if (ifq->ifq_next != NULL) 5153 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext; 5154 5155 MUTEX_DESTROY(&ifq->ifq_lock); 5156 ifs->ifs_fr_userifqs--; 5157 KFREE(ifq); 5158 } 5159 5160 5161 /* ------------------------------------------------------------------------ */ 5162 /* Function: fr_deletequeueentry */ 5163 /* Returns: Nil */ 5164 /* Parameters: tqe(I) - timeout queue entry to delete */ 5165 /* ifq(I) - timeout queue to remove entry from */ 5166 /* */ 5167 /* Remove a tail queue entry from its queue and make it an orphan. */ 5168 /* fr_deletetimeoutqueue is called to make sure the reference count on the */ 5169 /* queue is correct. We can't, however, call fr_freetimeoutqueue because */ 5170 /* the correct lock(s) may not be held that would make it safe to do so. */ 5171 /* ------------------------------------------------------------------------ */ 5172 void fr_deletequeueentry(tqe) 5173 ipftqent_t *tqe; 5174 { 5175 ipftq_t *ifq; 5176 5177 ifq = tqe->tqe_ifq; 5178 if (ifq == NULL) 5179 return; 5180 5181 MUTEX_ENTER(&ifq->ifq_lock); 5182 5183 if (tqe->tqe_pnext != NULL) { 5184 *tqe->tqe_pnext = tqe->tqe_next; 5185 if (tqe->tqe_next != NULL) 5186 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5187 else /* we must be the tail anyway */ 5188 ifq->ifq_tail = tqe->tqe_pnext; 5189 5190 tqe->tqe_pnext = NULL; 5191 tqe->tqe_ifq = NULL; 5192 } 5193 5194 (void) fr_deletetimeoutqueue(ifq); 5195 5196 MUTEX_EXIT(&ifq->ifq_lock); 5197 } 5198 5199 5200 /* ------------------------------------------------------------------------ */ 5201 /* Function: fr_queuefront */ 5202 /* Returns: Nil */ 5203 /* Parameters: tqe(I) - pointer to timeout queue entry */ 5204 /* */ 5205 /* Move a queue entry to the front of the queue, if it isn't already there. */ 5206 /* ------------------------------------------------------------------------ */ 5207 void fr_queuefront(tqe) 5208 ipftqent_t *tqe; 5209 { 5210 ipftq_t *ifq; 5211 5212 ifq = tqe->tqe_ifq; 5213 if (ifq == NULL) 5214 return; 5215 5216 MUTEX_ENTER(&ifq->ifq_lock); 5217 if (ifq->ifq_head != tqe) { 5218 *tqe->tqe_pnext = tqe->tqe_next; 5219 if (tqe->tqe_next) 5220 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5221 else 5222 ifq->ifq_tail = tqe->tqe_pnext; 5223 5224 tqe->tqe_next = ifq->ifq_head; 5225 ifq->ifq_head->tqe_pnext = &tqe->tqe_next; 5226 ifq->ifq_head = tqe; 5227 tqe->tqe_pnext = &ifq->ifq_head; 5228 } 5229 MUTEX_EXIT(&ifq->ifq_lock); 5230 } 5231 5232 5233 /* ------------------------------------------------------------------------ */ 5234 /* Function: fr_queueback */ 5235 /* Returns: Nil */ 5236 /* Parameters: tqe(I) - pointer to timeout queue entry */ 5237 /* */ 5238 /* Move a queue entry to the back of the queue, if it isn't already there. */ 5239 /* ------------------------------------------------------------------------ */ 5240 void fr_queueback(tqe, ifs) 5241 ipftqent_t *tqe; 5242 ipf_stack_t *ifs; 5243 { 5244 ipftq_t *ifq; 5245 5246 ifq = tqe->tqe_ifq; 5247 if (ifq == NULL) 5248 return; 5249 tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl; 5250 5251 MUTEX_ENTER(&ifq->ifq_lock); 5252 if (tqe->tqe_next == NULL) { /* at the end already ? */ 5253 MUTEX_EXIT(&ifq->ifq_lock); 5254 return; 5255 } 5256 5257 /* 5258 * Remove from list 5259 */ 5260 *tqe->tqe_pnext = tqe->tqe_next; 5261 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5262 5263 /* 5264 * Make it the last entry. 5265 */ 5266 tqe->tqe_next = NULL; 5267 tqe->tqe_pnext = ifq->ifq_tail; 5268 *ifq->ifq_tail = tqe; 5269 ifq->ifq_tail = &tqe->tqe_next; 5270 MUTEX_EXIT(&ifq->ifq_lock); 5271 } 5272 5273 5274 /* ------------------------------------------------------------------------ */ 5275 /* Function: fr_queueappend */ 5276 /* Returns: Nil */ 5277 /* Parameters: tqe(I) - pointer to timeout queue entry */ 5278 /* ifq(I) - pointer to timeout queue */ 5279 /* parent(I) - owing object pointer */ 5280 /* */ 5281 /* Add a new item to this queue and put it on the very end. */ 5282 /* ------------------------------------------------------------------------ */ 5283 void fr_queueappend(tqe, ifq, parent, ifs) 5284 ipftqent_t *tqe; 5285 ipftq_t *ifq; 5286 void *parent; 5287 ipf_stack_t *ifs; 5288 { 5289 5290 MUTEX_ENTER(&ifq->ifq_lock); 5291 tqe->tqe_parent = parent; 5292 tqe->tqe_pnext = ifq->ifq_tail; 5293 *ifq->ifq_tail = tqe; 5294 ifq->ifq_tail = &tqe->tqe_next; 5295 tqe->tqe_next = NULL; 5296 tqe->tqe_ifq = ifq; 5297 tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl; 5298 ifq->ifq_ref++; 5299 MUTEX_EXIT(&ifq->ifq_lock); 5300 } 5301 5302 5303 /* ------------------------------------------------------------------------ */ 5304 /* Function: fr_movequeue */ 5305 /* Returns: Nil */ 5306 /* Parameters: tq(I) - pointer to timeout queue information */ 5307 /* oifp(I) - old timeout queue entry was on */ 5308 /* nifp(I) - new timeout queue to put entry on */ 5309 /* ifs - ipf stack instance */ 5310 /* */ 5311 /* Move a queue entry from one timeout queue to another timeout queue. */ 5312 /* If it notices that the current entry is already last and does not need */ 5313 /* to move queue, the return. */ 5314 /* ------------------------------------------------------------------------ */ 5315 void fr_movequeue(tqe, oifq, nifq, ifs) 5316 ipftqent_t *tqe; 5317 ipftq_t *oifq, *nifq; 5318 ipf_stack_t *ifs; 5319 { 5320 /* 5321 * If the queue isn't changing, and the clock hasn't ticked 5322 * since the last update, the operation will be a no-op. 5323 */ 5324 if (oifq == nifq && tqe->tqe_touched == ifs->ifs_fr_ticks) 5325 return; 5326 5327 /* 5328 * Grab the lock and update the timers. 5329 */ 5330 MUTEX_ENTER(&oifq->ifq_lock); 5331 tqe->tqe_touched = ifs->ifs_fr_ticks; 5332 tqe->tqe_die = ifs->ifs_fr_ticks + nifq->ifq_ttl; 5333 5334 /* 5335 * The remainder of the operation can still be a no-op. 5336 * 5337 * If the queue isn't changing, check to see if 5338 * an update would be meaningless. 5339 */ 5340 if (oifq == nifq) { 5341 if ((tqe->tqe_next == NULL) || 5342 (tqe->tqe_next->tqe_die == tqe->tqe_die)) { 5343 MUTEX_EXIT(&oifq->ifq_lock); 5344 return; 5345 } 5346 } 5347 5348 /* 5349 * Remove from the old queue 5350 */ 5351 *tqe->tqe_pnext = tqe->tqe_next; 5352 if (tqe->tqe_next) 5353 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 5354 else 5355 oifq->ifq_tail = tqe->tqe_pnext; 5356 tqe->tqe_next = NULL; 5357 5358 /* 5359 * If we're moving from one queue to another, release the lock on the 5360 * old queue and get a lock on the new queue. For user defined queues, 5361 * if we're moving off it, call delete in case it can now be freed. 5362 */ 5363 if (oifq != nifq) { 5364 tqe->tqe_ifq = NULL; 5365 5366 (void) fr_deletetimeoutqueue(oifq); 5367 5368 MUTEX_EXIT(&oifq->ifq_lock); 5369 5370 MUTEX_ENTER(&nifq->ifq_lock); 5371 5372 tqe->tqe_ifq = nifq; 5373 nifq->ifq_ref++; 5374 } 5375 5376 /* 5377 * Add to the bottom of the new queue 5378 */ 5379 tqe->tqe_pnext = nifq->ifq_tail; 5380 *nifq->ifq_tail = tqe; 5381 nifq->ifq_tail = &tqe->tqe_next; 5382 MUTEX_EXIT(&nifq->ifq_lock); 5383 } 5384 5385 5386 /* ------------------------------------------------------------------------ */ 5387 /* Function: fr_updateipid */ 5388 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */ 5389 /* Parameters: fin(I) - pointer to packet information */ 5390 /* */ 5391 /* When we are doing NAT, change the IP of every packet to represent a */ 5392 /* single sequence of packets coming from the host, hiding any host */ 5393 /* specific sequencing that might otherwise be revealed. If the packet is */ 5394 /* a fragment, then store the 'new' IPid in the fragment cache and look up */ 5395 /* the fragment cache for non-leading fragments. If a non-leading fragment */ 5396 /* has no match in the cache, return an error. */ 5397 /* ------------------------------------------------------------------------ */ 5398 static INLINE int fr_updateipid(fin) 5399 fr_info_t *fin; 5400 { 5401 u_short id, ido, sums; 5402 u_32_t sumd, sum; 5403 ip_t *ip; 5404 5405 if (fin->fin_off != 0) { 5406 sum = fr_ipid_knownfrag(fin); 5407 if (sum == 0xffffffff) 5408 return -1; 5409 sum &= 0xffff; 5410 id = (u_short)sum; 5411 } else { 5412 id = fr_nextipid(fin); 5413 if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0) 5414 (void) fr_ipid_newfrag(fin, (u_32_t)id); 5415 } 5416 5417 ip = fin->fin_ip; 5418 ido = ntohs(ip->ip_id); 5419 if (id == ido) 5420 return 0; 5421 ip->ip_id = htons(id); 5422 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */ 5423 sum = (~ntohs(ip->ip_sum)) & 0xffff; 5424 sum += sumd; 5425 sum = (sum >> 16) + (sum & 0xffff); 5426 sum = (sum >> 16) + (sum & 0xffff); 5427 sums = ~(u_short)sum; 5428 ip->ip_sum = htons(sums); 5429 return 0; 5430 } 5431 5432 5433 #ifdef NEED_FRGETIFNAME 5434 /* ------------------------------------------------------------------------ */ 5435 /* Function: fr_getifname */ 5436 /* Returns: char * - pointer to interface name */ 5437 /* Parameters: ifp(I) - pointer to network interface */ 5438 /* buffer(O) - pointer to where to store interface name */ 5439 /* */ 5440 /* Constructs an interface name in the buffer passed. The buffer passed is */ 5441 /* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */ 5442 /* as a NULL pointer then return a pointer to a static array. */ 5443 /* ------------------------------------------------------------------------ */ 5444 char *fr_getifname(ifp, buffer) 5445 struct ifnet *ifp; 5446 char *buffer; 5447 { 5448 static char namebuf[LIFNAMSIZ]; 5449 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 5450 defined(__sgi) || defined(linux) || defined(_AIX51) || \ 5451 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 5452 int unit, space; 5453 char temp[20]; 5454 char *s; 5455 # endif 5456 5457 ASSERT(buffer != NULL); 5458 #ifdef notdef 5459 if (buffer == NULL) 5460 buffer = namebuf; 5461 #endif 5462 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ); 5463 buffer[LIFNAMSIZ - 1] = '\0'; 5464 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 5465 defined(__sgi) || defined(_AIX51) || \ 5466 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 5467 for (s = buffer; *s; s++) 5468 ; 5469 unit = ifp->if_unit; 5470 space = LIFNAMSIZ - (s - buffer); 5471 if (space > 0) { 5472 # if defined(SNPRINTF) && defined(_KERNEL) 5473 (void) SNPRINTF(temp, sizeof(temp), "%d", unit); 5474 # else 5475 (void) sprintf(temp, "%d", unit); 5476 # endif 5477 (void) strncpy(s, temp, space); 5478 } 5479 # endif 5480 return buffer; 5481 } 5482 #endif 5483 5484 5485 /* ------------------------------------------------------------------------ */ 5486 /* Function: fr_ioctlswitch */ 5487 /* Returns: int - -1 continue processing, else ioctl return value */ 5488 /* Parameters: unit(I) - device unit opened */ 5489 /* data(I) - pointer to ioctl data */ 5490 /* cmd(I) - ioctl command */ 5491 /* mode(I) - mode value */ 5492 /* */ 5493 /* Based on the value of unit, call the appropriate ioctl handler or return */ 5494 /* EIO if ipfilter is not running. Also checks if write perms are req'd */ 5495 /* for the device in order to execute the ioctl. */ 5496 /* ------------------------------------------------------------------------ */ 5497 INLINE int fr_ioctlswitch(unit, data, cmd, mode, uid, ctx, ifs) 5498 int unit, mode, uid; 5499 ioctlcmd_t cmd; 5500 void *data, *ctx; 5501 ipf_stack_t *ifs; 5502 { 5503 int error = 0; 5504 5505 switch (unit) 5506 { 5507 case IPL_LOGIPF : 5508 error = -1; 5509 break; 5510 case IPL_LOGNAT : 5511 if (ifs->ifs_fr_running > 0) 5512 error = fr_nat_ioctl(data, cmd, mode, uid, ctx, ifs); 5513 else 5514 error = EIO; 5515 break; 5516 case IPL_LOGSTATE : 5517 if (ifs->ifs_fr_running > 0) 5518 error = fr_state_ioctl(data, cmd, mode, uid, ctx, ifs); 5519 else 5520 error = EIO; 5521 break; 5522 case IPL_LOGAUTH : 5523 if (ifs->ifs_fr_running > 0) { 5524 if ((cmd == (ioctlcmd_t)SIOCADAFR) || 5525 (cmd == (ioctlcmd_t)SIOCRMAFR)) { 5526 if (!(mode & FWRITE)) { 5527 error = EPERM; 5528 } else { 5529 error = frrequest(unit, cmd, data, 5530 ifs->ifs_fr_active, 1, ifs); 5531 } 5532 } else { 5533 error = fr_auth_ioctl(data, cmd, mode, uid, ctx, ifs); 5534 } 5535 } else 5536 error = EIO; 5537 break; 5538 case IPL_LOGSYNC : 5539 #ifdef IPFILTER_SYNC 5540 if (ifs->ifs_fr_running > 0) 5541 error = fr_sync_ioctl(data, cmd, mode, ifs); 5542 else 5543 #endif 5544 error = EIO; 5545 break; 5546 case IPL_LOGSCAN : 5547 #ifdef IPFILTER_SCAN 5548 if (ifs->ifs_fr_running > 0) 5549 error = fr_scan_ioctl(data, cmd, mode, ifs); 5550 else 5551 #endif 5552 error = EIO; 5553 break; 5554 case IPL_LOGLOOKUP : 5555 #ifdef IPFILTER_LOOKUP 5556 if (ifs->ifs_fr_running > 0) 5557 error = ip_lookup_ioctl(data, cmd, mode, uid, ctx, ifs); 5558 else 5559 #endif 5560 error = EIO; 5561 break; 5562 default : 5563 error = EIO; 5564 break; 5565 } 5566 5567 return error; 5568 } 5569 5570 5571 /* 5572 * This array defines the expected size of objects coming into the kernel 5573 * for the various recognised object types. 5574 */ 5575 #define NUM_OBJ_TYPES 19 5576 5577 static int fr_objbytes[NUM_OBJ_TYPES][2] = { 5578 { 1, sizeof(struct frentry) }, /* frentry */ 5579 { 0, sizeof(struct friostat) }, 5580 { 0, sizeof(struct fr_info) }, 5581 { 0, sizeof(struct fr_authstat) }, 5582 { 0, sizeof(struct ipfrstat) }, 5583 { 0, sizeof(struct ipnat) }, 5584 { 0, sizeof(struct natstat) }, 5585 { 0, sizeof(struct ipstate_save) }, 5586 { 1, sizeof(struct nat_save) }, /* nat_save */ 5587 { 0, sizeof(struct natlookup) }, 5588 { 1, sizeof(struct ipstate) }, /* ipstate */ 5589 { 0, sizeof(struct ips_stat) }, 5590 { 0, sizeof(struct frauth) }, 5591 { 0, sizeof(struct ipftune) }, 5592 { 0, sizeof(struct nat) }, /* nat_t */ 5593 { 0, sizeof(struct ipfruleiter) }, 5594 { 0, sizeof(struct ipfgeniter) }, 5595 { 0, sizeof(struct ipftable) }, 5596 { 0, sizeof(struct ipflookupiter) } 5597 }; 5598 5599 5600 /* ------------------------------------------------------------------------ */ 5601 /* Function: fr_inobj */ 5602 /* Returns: int - 0 = success, else failure */ 5603 /* Parameters: data(I) - pointer to ioctl data */ 5604 /* ptr(I) - pointer to store real data in */ 5605 /* type(I) - type of structure being moved */ 5606 /* */ 5607 /* Copy in the contents of what the ipfobj_t points to. In future, we */ 5608 /* add things to check for version numbers, sizes, etc, to make it backward */ 5609 /* compatible at the ABI for user land. */ 5610 /* ------------------------------------------------------------------------ */ 5611 int fr_inobj(data, ptr, type) 5612 void *data; 5613 void *ptr; 5614 int type; 5615 { 5616 ipfobj_t obj; 5617 int error = 0; 5618 5619 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 5620 return EINVAL; 5621 5622 error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 5623 if (error != 0) 5624 return EFAULT; 5625 5626 if (obj.ipfo_type != type) 5627 return EINVAL; 5628 5629 #ifndef IPFILTER_COMPAT 5630 if ((fr_objbytes[type][0] & 1) != 0) { 5631 if (obj.ipfo_size < fr_objbytes[type][1]) 5632 return EINVAL; 5633 } else if (obj.ipfo_size != fr_objbytes[type][1]) 5634 return EINVAL; 5635 #else 5636 if (obj.ipfo_rev != IPFILTER_VERSION) { 5637 error = fr_incomptrans(&obj, ptr); 5638 return error; 5639 } 5640 5641 if ((fr_objbytes[type][0] & 1) != 0 && 5642 obj.ipfo_size < fr_objbytes[type][1] || 5643 obj.ipfo_size != fr_objbytes[type][1]) 5644 return EINVAL; 5645 #endif 5646 5647 if ((fr_objbytes[type][0] & 1) != 0) { 5648 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, 5649 fr_objbytes[type][1]); 5650 } else { 5651 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, 5652 obj.ipfo_size); 5653 } 5654 return error; 5655 } 5656 5657 5658 /* ------------------------------------------------------------------------ */ 5659 /* Function: fr_inobjsz */ 5660 /* Returns: int - 0 = success, else failure */ 5661 /* Parameters: data(I) - pointer to ioctl data */ 5662 /* ptr(I) - pointer to store real data in */ 5663 /* type(I) - type of structure being moved */ 5664 /* sz(I) - size of data to copy */ 5665 /* */ 5666 /* As per fr_inobj, except the size of the object to copy in is passed in */ 5667 /* but it must not be smaller than the size defined for the type and the */ 5668 /* type must allow for varied sized objects. The extra requirement here is */ 5669 /* that sz must match the size of the object being passed in - this is not */ 5670 /* not possible nor required in fr_inobj(). */ 5671 /* ------------------------------------------------------------------------ */ 5672 int fr_inobjsz(data, ptr, type, sz) 5673 void *data; 5674 void *ptr; 5675 int type, sz; 5676 { 5677 ipfobj_t obj; 5678 int error; 5679 5680 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 5681 return EINVAL; 5682 if (((fr_objbytes[type][0] & 1) == 0) || (sz < fr_objbytes[type][1])) 5683 return EINVAL; 5684 5685 error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 5686 if (error != 0) 5687 return EFAULT; 5688 5689 if (obj.ipfo_type != type) 5690 return EINVAL; 5691 5692 #ifndef IPFILTER_COMPAT 5693 if (obj.ipfo_size != sz) 5694 return EINVAL; 5695 #else 5696 if (obj.ipfo_rev != IPFILTER_VERSION) 5697 /*XXX compatibility hook here */ 5698 /*EMPTY*/; 5699 if (obj.ipfo_size != sz) 5700 /* XXX compatibility hook here */ 5701 return EINVAL; 5702 #endif 5703 5704 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, sz); 5705 return error; 5706 } 5707 5708 5709 /* ------------------------------------------------------------------------ */ 5710 /* Function: fr_outobjsz */ 5711 /* Returns: int - 0 = success, else failure */ 5712 /* Parameters: data(I) - pointer to ioctl data */ 5713 /* ptr(I) - pointer to store real data in */ 5714 /* type(I) - type of structure being moved */ 5715 /* sz(I) - size of data to copy */ 5716 /* */ 5717 /* As per fr_outobj, except the size of the object to copy out is passed in */ 5718 /* but it must not be smaller than the size defined for the type and the */ 5719 /* type must allow for varied sized objects. The extra requirement here is */ 5720 /* that sz must match the size of the object being passed in - this is not */ 5721 /* not possible nor required in fr_outobj(). */ 5722 /* ------------------------------------------------------------------------ */ 5723 int fr_outobjsz(data, ptr, type, sz) 5724 void *data; 5725 void *ptr; 5726 int type, sz; 5727 { 5728 ipfobj_t obj; 5729 int error; 5730 5731 if ((type < 0) || (type > NUM_OBJ_TYPES-1) || 5732 ((fr_objbytes[type][0] & 1) == 0) || 5733 (sz < fr_objbytes[type][1])) 5734 return EINVAL; 5735 5736 error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 5737 if (error != 0) 5738 return EFAULT; 5739 5740 if (obj.ipfo_type != type) 5741 return EINVAL; 5742 5743 #ifndef IPFILTER_COMPAT 5744 if (obj.ipfo_size != sz) 5745 return EINVAL; 5746 #else 5747 if (obj.ipfo_rev != IPFILTER_VERSION) 5748 /* XXX compatibility hook here */ 5749 /*EMPTY*/; 5750 if (obj.ipfo_size != sz) 5751 /* XXX compatibility hook here */ 5752 return EINVAL; 5753 #endif 5754 5755 error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, sz); 5756 return error; 5757 } 5758 5759 5760 /* ------------------------------------------------------------------------ */ 5761 /* Function: fr_outobj */ 5762 /* Returns: int - 0 = success, else failure */ 5763 /* Parameters: data(I) - pointer to ioctl data */ 5764 /* ptr(I) - pointer to store real data in */ 5765 /* type(I) - type of structure being moved */ 5766 /* */ 5767 /* Copy out the contents of what ptr is to where ipfobj points to. In */ 5768 /* future, we add things to check for version numbers, sizes, etc, to make */ 5769 /* it backward compatible at the ABI for user land. */ 5770 /* ------------------------------------------------------------------------ */ 5771 int fr_outobj(data, ptr, type) 5772 void *data; 5773 void *ptr; 5774 int type; 5775 { 5776 ipfobj_t obj; 5777 int error; 5778 5779 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 5780 return EINVAL; 5781 5782 error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 5783 if (error != 0) 5784 return EFAULT; 5785 5786 if (obj.ipfo_type != type) 5787 return EINVAL; 5788 5789 #ifndef IPFILTER_COMPAT 5790 if ((fr_objbytes[type][0] & 1) != 0) { 5791 if (obj.ipfo_size < fr_objbytes[type][1]) 5792 return EINVAL; 5793 } else if (obj.ipfo_size != fr_objbytes[type][1]) 5794 return EINVAL; 5795 #else 5796 if (obj.ipfo_rev != IPFILTER_VERSION) { 5797 error = fr_outcomptrans(&obj, ptr); 5798 return error; 5799 } 5800 5801 if ((fr_objbytes[type][0] & 1) != 0 && 5802 obj.ipfo_size < fr_objbytes[type][1] || 5803 obj.ipfo_size != fr_objbytes[type][1]) 5804 return EINVAL; 5805 #endif 5806 5807 error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, obj.ipfo_size); 5808 return error; 5809 } 5810 5811 5812 /* ------------------------------------------------------------------------ */ 5813 /* Function: fr_checkl4sum */ 5814 /* Returns: int - 0 = good, -1 = bad, 1 = cannot check */ 5815 /* Parameters: fin(I) - pointer to packet information */ 5816 /* */ 5817 /* If possible, calculate the layer 4 checksum for the packet. If this is */ 5818 /* not possible, return without indicating a failure or success but in a */ 5819 /* way that is ditinguishable. */ 5820 /* ------------------------------------------------------------------------ */ 5821 int fr_checkl4sum(fin) 5822 fr_info_t *fin; 5823 { 5824 u_short sum, hdrsum, *csump; 5825 udphdr_t *udp; 5826 int dosum; 5827 ipf_stack_t *ifs = fin->fin_ifs; 5828 5829 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 5830 net_handle_t net_data_p; 5831 if (fin->fin_v == 4) 5832 net_data_p = ifs->ifs_ipf_ipv4; 5833 else 5834 net_data_p = ifs->ifs_ipf_ipv6; 5835 #endif 5836 5837 if ((fin->fin_flx & FI_NOCKSUM) != 0) 5838 return 0; 5839 5840 /* 5841 * If the TCP packet isn't a fragment, isn't too short and otherwise 5842 * isn't already considered "bad", then validate the checksum. If 5843 * this check fails then considered the packet to be "bad". 5844 */ 5845 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0) 5846 return 1; 5847 5848 csump = NULL; 5849 hdrsum = 0; 5850 dosum = 0; 5851 sum = 0; 5852 5853 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 5854 ASSERT(fin->fin_m != NULL); 5855 if (NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) || 5856 NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) { 5857 hdrsum = 0; 5858 sum = 0; 5859 } else { 5860 #endif 5861 switch (fin->fin_p) 5862 { 5863 case IPPROTO_TCP : 5864 csump = &((tcphdr_t *)fin->fin_dp)->th_sum; 5865 dosum = 1; 5866 break; 5867 5868 case IPPROTO_UDP : 5869 udp = fin->fin_dp; 5870 if (udp->uh_sum != 0) { 5871 csump = &udp->uh_sum; 5872 dosum = 1; 5873 } 5874 break; 5875 5876 case IPPROTO_ICMP : 5877 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum; 5878 dosum = 1; 5879 break; 5880 5881 default : 5882 return 1; 5883 /*NOTREACHED*/ 5884 } 5885 5886 if (csump != NULL) 5887 hdrsum = *csump; 5888 5889 if (dosum) 5890 sum = fr_cksum(fin->fin_m, fin->fin_ip, 5891 fin->fin_p, fin->fin_dp); 5892 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 5893 } 5894 #endif 5895 #if !defined(_KERNEL) 5896 if (sum == hdrsum) { 5897 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum)); 5898 } else { 5899 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum)); 5900 } 5901 #endif 5902 if (hdrsum == sum) 5903 return 0; 5904 return -1; 5905 } 5906 5907 5908 /* ------------------------------------------------------------------------ */ 5909 /* Function: fr_ifpfillv4addr */ 5910 /* Returns: int - 0 = address update, -1 = address not updated */ 5911 /* Parameters: atype(I) - type of network address update to perform */ 5912 /* sin(I) - pointer to source of address information */ 5913 /* mask(I) - pointer to source of netmask information */ 5914 /* inp(I) - pointer to destination address store */ 5915 /* inpmask(I) - pointer to destination netmask store */ 5916 /* */ 5917 /* Given a type of network address update (atype) to perform, copy */ 5918 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 5919 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 5920 /* which case the operation fails. For all values of atype other than */ 5921 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 5922 /* value. */ 5923 /* ------------------------------------------------------------------------ */ 5924 int fr_ifpfillv4addr(atype, sin, mask, inp, inpmask) 5925 int atype; 5926 struct sockaddr_in *sin, *mask; 5927 struct in_addr *inp, *inpmask; 5928 { 5929 if (inpmask != NULL && atype != FRI_NETMASKED) 5930 inpmask->s_addr = 0xffffffff; 5931 5932 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 5933 if (atype == FRI_NETMASKED) { 5934 if (inpmask == NULL) 5935 return -1; 5936 inpmask->s_addr = mask->sin_addr.s_addr; 5937 } 5938 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr; 5939 } else { 5940 inp->s_addr = sin->sin_addr.s_addr; 5941 } 5942 return 0; 5943 } 5944 5945 5946 #ifdef USE_INET6 5947 /* ------------------------------------------------------------------------ */ 5948 /* Function: fr_ifpfillv6addr */ 5949 /* Returns: int - 0 = address update, -1 = address not updated */ 5950 /* Parameters: atype(I) - type of network address update to perform */ 5951 /* sin(I) - pointer to source of address information */ 5952 /* mask(I) - pointer to source of netmask information */ 5953 /* inp(I) - pointer to destination address store */ 5954 /* inpmask(I) - pointer to destination netmask store */ 5955 /* */ 5956 /* Given a type of network address update (atype) to perform, copy */ 5957 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 5958 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 5959 /* which case the operation fails. For all values of atype other than */ 5960 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 5961 /* value. */ 5962 /* ------------------------------------------------------------------------ */ 5963 int fr_ifpfillv6addr(atype, sin, mask, inp, inpmask) 5964 int atype; 5965 struct sockaddr_in6 *sin, *mask; 5966 struct in_addr *inp, *inpmask; 5967 { 5968 i6addr_t *src, *dst, *and, *dmask; 5969 5970 src = (i6addr_t *)&sin->sin6_addr; 5971 and = (i6addr_t *)&mask->sin6_addr; 5972 dst = (i6addr_t *)inp; 5973 dmask = (i6addr_t *)inpmask; 5974 5975 if (inpmask != NULL && atype != FRI_NETMASKED) { 5976 dmask->i6[0] = 0xffffffff; 5977 dmask->i6[1] = 0xffffffff; 5978 dmask->i6[2] = 0xffffffff; 5979 dmask->i6[3] = 0xffffffff; 5980 } 5981 5982 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 5983 if (atype == FRI_NETMASKED) { 5984 if (inpmask == NULL) 5985 return -1; 5986 dmask->i6[0] = and->i6[0]; 5987 dmask->i6[1] = and->i6[1]; 5988 dmask->i6[2] = and->i6[2]; 5989 dmask->i6[3] = and->i6[3]; 5990 } 5991 5992 dst->i6[0] = src->i6[0] & and->i6[0]; 5993 dst->i6[1] = src->i6[1] & and->i6[1]; 5994 dst->i6[2] = src->i6[2] & and->i6[2]; 5995 dst->i6[3] = src->i6[3] & and->i6[3]; 5996 } else { 5997 dst->i6[0] = src->i6[0]; 5998 dst->i6[1] = src->i6[1]; 5999 dst->i6[2] = src->i6[2]; 6000 dst->i6[3] = src->i6[3]; 6001 } 6002 return 0; 6003 } 6004 #endif 6005 6006 6007 /* ------------------------------------------------------------------------ */ 6008 /* Function: fr_matchtag */ 6009 /* Returns: 0 == mismatch, 1 == match. */ 6010 /* Parameters: tag1(I) - pointer to first tag to compare */ 6011 /* tag2(I) - pointer to second tag to compare */ 6012 /* */ 6013 /* Returns true (non-zero) or false(0) if the two tag structures can be */ 6014 /* considered to be a match or not match, respectively. The tag is 16 */ 6015 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so */ 6016 /* compare the ints instead, for speed. tag1 is the master of the */ 6017 /* comparison. This function should only be called with both tag1 and tag2 */ 6018 /* as non-NULL pointers. */ 6019 /* ------------------------------------------------------------------------ */ 6020 int fr_matchtag(tag1, tag2) 6021 ipftag_t *tag1, *tag2; 6022 { 6023 if (tag1 == tag2) 6024 return 1; 6025 6026 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0)) 6027 return 1; 6028 6029 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) && 6030 (tag1->ipt_num[1] == tag2->ipt_num[1]) && 6031 (tag1->ipt_num[2] == tag2->ipt_num[2]) && 6032 (tag1->ipt_num[3] == tag2->ipt_num[3])) 6033 return 1; 6034 return 0; 6035 } 6036 6037 6038 /* ------------------------------------------------------------------------ */ 6039 /* Function: fr_coalesce */ 6040 /* Returns: 1 == success, -1 == failure, 0 == no change */ 6041 /* Parameters: fin(I) - pointer to packet information */ 6042 /* */ 6043 /* Attempt to get all of the packet data into a single, contiguous buffer. */ 6044 /* If this call returns a failure then the buffers have also been freed. */ 6045 /* ------------------------------------------------------------------------ */ 6046 int fr_coalesce(fin) 6047 fr_info_t *fin; 6048 { 6049 ipf_stack_t *ifs = fin->fin_ifs; 6050 if ((fin->fin_flx & FI_COALESCE) != 0) 6051 return 1; 6052 6053 /* 6054 * If the mbuf pointers indicate that there is no mbuf to work with, 6055 * return but do not indicate success or failure. 6056 */ 6057 if (fin->fin_m == NULL || fin->fin_mp == NULL) 6058 return 0; 6059 6060 #if defined(_KERNEL) 6061 if (fr_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) { 6062 IPF_BUMP(ifs->ifs_fr_badcoalesces[fin->fin_out]); 6063 # ifdef MENTAT 6064 FREE_MB_T(*fin->fin_mp); 6065 # endif 6066 *fin->fin_mp = NULL; 6067 fin->fin_m = NULL; 6068 return -1; 6069 } 6070 #else 6071 fin = fin; /* LINT */ 6072 #endif 6073 return 1; 6074 } 6075 6076 6077 /* 6078 * The following table lists all of the tunable variables that can be 6079 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT. The format of each row 6080 * in the table below is as follows: 6081 * 6082 * pointer to value, name of value, minimum, maximum, size of the value's 6083 * container, value attribute flags 6084 * 6085 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED 6086 * means the value can only be written to when IPFilter is loaded but disabled. 6087 * The obvious implication is if neither of these are set then the value can be 6088 * changed at any time without harm. 6089 */ 6090 ipftuneable_t lcl_ipf_tuneables[] = { 6091 /* filtering */ 6092 { { NULL }, "fr_flags", 0, 0xffffffff, 6093 0, 0 }, 6094 { { NULL }, "fr_active", 0, 0, 6095 0, IPFT_RDONLY }, 6096 { { NULL }, "fr_control_forwarding", 0, 1, 6097 0, 0 }, 6098 { { NULL }, "fr_update_ipid", 0, 1, 6099 0, 0 }, 6100 { { NULL }, "fr_chksrc", 0, 1, 6101 0, 0 }, 6102 { { NULL }, "fr_minttl", 0, 1, 6103 0, 0 }, 6104 { { NULL }, "fr_icmpminfragmtu", 0, 1, 6105 0, 0 }, 6106 { { NULL }, "fr_pass", 0, 0xffffffff, 6107 0, 0 }, 6108 #if SOLARIS2 >= 10 6109 { { NULL }, "ipf_loopback", 0, 1, 6110 0, IPFT_WRDISABLED }, 6111 #endif 6112 /* state */ 6113 { { NULL }, "fr_tcpidletimeout", 1, 0x7fffffff, 6114 0, IPFT_WRDISABLED }, 6115 { { NULL }, "fr_tcpclosewait", 1, 0x7fffffff, 6116 0, IPFT_WRDISABLED }, 6117 { { NULL }, "fr_tcplastack", 1, 0x7fffffff, 6118 0, IPFT_WRDISABLED }, 6119 { { NULL }, "fr_tcptimeout", 1, 0x7fffffff, 6120 0, IPFT_WRDISABLED }, 6121 { { NULL }, "fr_tcpclosed", 1, 0x7fffffff, 6122 0, IPFT_WRDISABLED }, 6123 { { NULL }, "fr_tcphalfclosed", 1, 0x7fffffff, 6124 0, IPFT_WRDISABLED }, 6125 { { NULL }, "fr_udptimeout", 1, 0x7fffffff, 6126 0, IPFT_WRDISABLED }, 6127 { { NULL }, "fr_udpacktimeout", 1, 0x7fffffff, 6128 0, IPFT_WRDISABLED }, 6129 { { NULL }, "fr_icmptimeout", 1, 0x7fffffff, 6130 0, IPFT_WRDISABLED }, 6131 { { NULL }, "fr_icmpacktimeout", 1, 0x7fffffff, 6132 0, IPFT_WRDISABLED }, 6133 { { NULL }, "fr_iptimeout", 1, 0x7fffffff, 6134 0, IPFT_WRDISABLED }, 6135 { { NULL }, "fr_statemax", 1, 0x7fffffff, 6136 0, 0 }, 6137 { { NULL }, "fr_statesize", 1, 0x7fffffff, 6138 0, IPFT_WRDISABLED }, 6139 { { NULL }, "fr_state_lock", 0, 1, 6140 0, IPFT_RDONLY }, 6141 { { NULL }, "fr_state_maxbucket", 1, 0x7fffffff, 6142 0, IPFT_WRDISABLED }, 6143 { { NULL }, "fr_state_maxbucket_reset", 0, 1, 6144 0, IPFT_WRDISABLED }, 6145 { { NULL }, "ipstate_logging", 0, 1, 6146 0, 0 }, 6147 { { NULL }, "state_flush_level_hi", 1, 100, 6148 0, 0 }, 6149 { { NULL }, "state_flush_level_lo", 1, 100, 6150 0, 0 }, 6151 /* nat */ 6152 { { NULL }, "fr_nat_lock", 0, 1, 6153 0, IPFT_RDONLY }, 6154 { { NULL }, "ipf_nattable_sz", 1, 0x7fffffff, 6155 0, IPFT_WRDISABLED }, 6156 { { NULL }, "ipf_nattable_max", 1, 0x7fffffff, 6157 0, 0 }, 6158 { { NULL }, "ipf_natrules_sz", 1, 0x7fffffff, 6159 0, IPFT_WRDISABLED }, 6160 { { NULL }, "ipf_rdrrules_sz", 1, 0x7fffffff, 6161 0, IPFT_WRDISABLED }, 6162 { { NULL }, "ipf_hostmap_sz", 1, 0x7fffffff, 6163 0, IPFT_WRDISABLED }, 6164 { { NULL }, "fr_nat_maxbucket", 1, 0x7fffffff, 6165 0, IPFT_WRDISABLED }, 6166 { { NULL }, "fr_nat_maxbucket_reset", 0, 1, 6167 0, IPFT_WRDISABLED }, 6168 { { NULL }, "nat_logging", 0, 1, 6169 0, 0 }, 6170 { { NULL }, "fr_defnatage", 1, 0x7fffffff, 6171 0, IPFT_WRDISABLED }, 6172 { { NULL }, "fr_defnatipage", 1, 0x7fffffff, 6173 0, IPFT_WRDISABLED }, 6174 { { NULL }, "fr_defnaticmpage", 1, 0x7fffffff, 6175 0, IPFT_WRDISABLED }, 6176 { { NULL }, "nat_flush_level_hi", 1, 100, 6177 0, 0 }, 6178 { { NULL }, "nat_flush_level_lo", 1, 100, 6179 0, 0 }, 6180 /* frag */ 6181 { { NULL }, "ipfr_size", 1, 0x7fffffff, 6182 0, IPFT_WRDISABLED }, 6183 { { NULL }, "fr_ipfrttl", 1, 0x7fffffff, 6184 0, IPFT_WRDISABLED }, 6185 #ifdef IPFILTER_LOG 6186 /* log */ 6187 { { NULL }, "ipl_suppress", 0, 1, 6188 0, 0 }, 6189 { { NULL }, "ipl_buffer_sz", 0, 0, 6190 0, IPFT_RDONLY }, 6191 { { NULL }, "ipl_logmax", 0, 0x7fffffff, 6192 0, IPFT_WRDISABLED }, 6193 { { NULL }, "ipl_logall", 0, 1, 6194 0, 0 }, 6195 { { NULL }, "ipl_logsize", 0, 0x80000, 6196 0, 0 }, 6197 #endif 6198 { { NULL }, NULL, 0, 0 } 6199 }; 6200 6201 static ipftuneable_t * 6202 tune_lookup(ipf_stack_t *ifs, char *name) 6203 { 6204 int i; 6205 6206 for (i = 0; ifs->ifs_ipf_tuneables[i].ipft_name != NULL; i++) { 6207 if (strcmp(ifs->ifs_ipf_tuneables[i].ipft_name, name) == 0) 6208 return (&ifs->ifs_ipf_tuneables[i]); 6209 } 6210 return (NULL); 6211 } 6212 6213 #ifdef _KERNEL 6214 extern dev_info_t *ipf_dev_info; 6215 extern int ipf_property_update __P((dev_info_t *, ipf_stack_t *)); 6216 #endif 6217 6218 /* -------------------------------------------------------------------- */ 6219 /* Function: ipftuneable_setdefs() */ 6220 /* Returns: void */ 6221 /* Parameters: ifs - pointer to newly allocated IPF instance */ 6222 /* assigned to IP instance */ 6223 /* */ 6224 /* Function initializes IPF instance variables. Function is invoked */ 6225 /* from ipftuneable_alloc(). ipftuneable_alloc() is called only one */ 6226 /* time during IP instance lifetime - at the time of IP instance */ 6227 /* creation. Anytime IP instance is being created new private IPF */ 6228 /* instance is allocated and assigned to it. The moment of IP */ 6229 /* instance creation is the right time to initialize those IPF */ 6230 /* variables. */ 6231 /* */ 6232 /* -------------------------------------------------------------------- */ 6233 static void ipftuneable_setdefs(ipf_stack_t *ifs) 6234 { 6235 ifs->ifs_ipfr_size = IPFT_SIZE; 6236 ifs->ifs_fr_ipfrttl = 120; /* 60 seconds */ 6237 6238 /* it comes from fr_authinit() in IPF auth */ 6239 ifs->ifs_fr_authsize = FR_NUMAUTH; 6240 ifs->ifs_fr_defaultauthage = 600; 6241 6242 /* it comes from fr_stateinit() in IPF state */ 6243 ifs->ifs_fr_tcpidletimeout = IPF_TTLVAL(3600 * 24 * 5); /* five days */ 6244 ifs->ifs_fr_tcpclosewait = IPF_TTLVAL(TCP_MSL); 6245 ifs->ifs_fr_tcplastack = IPF_TTLVAL(TCP_MSL); 6246 ifs->ifs_fr_tcptimeout = IPF_TTLVAL(TCP_MSL); 6247 ifs->ifs_fr_tcpclosed = IPF_TTLVAL(60); 6248 ifs->ifs_fr_tcphalfclosed = IPF_TTLVAL(2 * 3600); /* 2 hours */ 6249 ifs->ifs_fr_udptimeout = IPF_TTLVAL(120); 6250 ifs->ifs_fr_udpacktimeout = IPF_TTLVAL(12); 6251 ifs->ifs_fr_icmptimeout = IPF_TTLVAL(60); 6252 ifs->ifs_fr_icmpacktimeout = IPF_TTLVAL(6); 6253 ifs->ifs_fr_iptimeout = IPF_TTLVAL(60); 6254 ifs->ifs_fr_statemax = IPSTATE_MAX; 6255 ifs->ifs_fr_statesize = IPSTATE_SIZE; 6256 ifs->ifs_fr_state_maxbucket_reset = 1; 6257 ifs->ifs_state_flush_level_hi = ST_FLUSH_HI; 6258 ifs->ifs_state_flush_level_lo = ST_FLUSH_LO; 6259 6260 /* it comes from fr_natinit() in ipnat */ 6261 ifs->ifs_ipf_nattable_sz = NAT_TABLE_SZ; 6262 ifs->ifs_ipf_nattable_max = NAT_TABLE_MAX; 6263 ifs->ifs_ipf_natrules_sz = NAT_SIZE; 6264 ifs->ifs_ipf_rdrrules_sz = RDR_SIZE; 6265 ifs->ifs_ipf_hostmap_sz = HOSTMAP_SIZE; 6266 ifs->ifs_fr_nat_maxbucket_reset = 1; 6267 ifs->ifs_fr_defnatage = DEF_NAT_AGE; 6268 ifs->ifs_fr_defnatipage = 120; /* 60 seconds */ 6269 ifs->ifs_fr_defnaticmpage = 6; /* 3 seconds */ 6270 ifs->ifs_nat_flush_level_hi = NAT_FLUSH_HI; 6271 ifs->ifs_nat_flush_level_lo = NAT_FLUSH_LO; 6272 6273 #ifdef IPFILTER_LOG 6274 /* it comes from fr_loginit() in IPF log */ 6275 ifs->ifs_ipl_suppress = 1; 6276 ifs->ifs_ipl_logmax = IPL_LOGMAX; 6277 ifs->ifs_ipl_logsize = IPFILTER_LOGSIZE; 6278 6279 /* from fr_natinit() */ 6280 ifs->ifs_nat_logging = 1; 6281 6282 /* from fr_stateinit() */ 6283 ifs->ifs_ipstate_logging = 1; 6284 #else 6285 /* from fr_natinit() */ 6286 ifs->ifs_nat_logging = 0; 6287 6288 /* from fr_stateinit() */ 6289 ifs->ifs_ipstate_logging = 0; 6290 #endif 6291 ifs->ifs_ipf_loopback = 0; 6292 6293 } 6294 /* 6295 * Allocate a per-stack tuneable and copy in the names. Then 6296 * set it to point to each of the per-stack tunables. 6297 */ 6298 void 6299 ipftuneable_alloc(ipf_stack_t *ifs) 6300 { 6301 ipftuneable_t *item; 6302 6303 KMALLOCS(ifs->ifs_ipf_tuneables, ipftuneable_t *, 6304 sizeof (lcl_ipf_tuneables)); 6305 bcopy(lcl_ipf_tuneables, ifs->ifs_ipf_tuneables, 6306 sizeof (lcl_ipf_tuneables)); 6307 6308 #define TUNE_SET(_ifs, _name, _field) \ 6309 item = tune_lookup((_ifs), (_name)); \ 6310 if (item != NULL) { \ 6311 item->ipft_una.ipftp_int = (unsigned int *)&((_ifs)->_field); \ 6312 item->ipft_sz = sizeof ((_ifs)->_field); \ 6313 } 6314 6315 TUNE_SET(ifs, "fr_flags", ifs_fr_flags); 6316 TUNE_SET(ifs, "fr_active", ifs_fr_active); 6317 TUNE_SET(ifs, "fr_control_forwarding", ifs_fr_control_forwarding); 6318 TUNE_SET(ifs, "fr_update_ipid", ifs_fr_update_ipid); 6319 TUNE_SET(ifs, "fr_chksrc", ifs_fr_chksrc); 6320 TUNE_SET(ifs, "fr_minttl", ifs_fr_minttl); 6321 TUNE_SET(ifs, "fr_icmpminfragmtu", ifs_fr_icmpminfragmtu); 6322 TUNE_SET(ifs, "fr_pass", ifs_fr_pass); 6323 TUNE_SET(ifs, "fr_tcpidletimeout", ifs_fr_tcpidletimeout); 6324 TUNE_SET(ifs, "fr_tcpclosewait", ifs_fr_tcpclosewait); 6325 TUNE_SET(ifs, "fr_tcplastack", ifs_fr_tcplastack); 6326 TUNE_SET(ifs, "fr_tcptimeout", ifs_fr_tcptimeout); 6327 TUNE_SET(ifs, "fr_tcpclosed", ifs_fr_tcpclosed); 6328 TUNE_SET(ifs, "fr_tcphalfclosed", ifs_fr_tcphalfclosed); 6329 TUNE_SET(ifs, "fr_udptimeout", ifs_fr_udptimeout); 6330 TUNE_SET(ifs, "fr_udpacktimeout", ifs_fr_udpacktimeout); 6331 TUNE_SET(ifs, "fr_icmptimeout", ifs_fr_icmptimeout); 6332 TUNE_SET(ifs, "fr_icmpacktimeout", ifs_fr_icmpacktimeout); 6333 TUNE_SET(ifs, "fr_iptimeout", ifs_fr_iptimeout); 6334 TUNE_SET(ifs, "fr_statemax", ifs_fr_statemax); 6335 TUNE_SET(ifs, "fr_statesize", ifs_fr_statesize); 6336 TUNE_SET(ifs, "fr_state_lock", ifs_fr_state_lock); 6337 TUNE_SET(ifs, "fr_state_maxbucket", ifs_fr_state_maxbucket); 6338 TUNE_SET(ifs, "fr_state_maxbucket_reset", ifs_fr_state_maxbucket_reset); 6339 TUNE_SET(ifs, "ipstate_logging", ifs_ipstate_logging); 6340 TUNE_SET(ifs, "fr_nat_lock", ifs_fr_nat_lock); 6341 TUNE_SET(ifs, "ipf_nattable_sz", ifs_ipf_nattable_sz); 6342 TUNE_SET(ifs, "ipf_nattable_max", ifs_ipf_nattable_max); 6343 TUNE_SET(ifs, "ipf_natrules_sz", ifs_ipf_natrules_sz); 6344 TUNE_SET(ifs, "ipf_rdrrules_sz", ifs_ipf_rdrrules_sz); 6345 TUNE_SET(ifs, "ipf_hostmap_sz", ifs_ipf_hostmap_sz); 6346 TUNE_SET(ifs, "fr_nat_maxbucket", ifs_fr_nat_maxbucket); 6347 TUNE_SET(ifs, "fr_nat_maxbucket_reset", ifs_fr_nat_maxbucket_reset); 6348 TUNE_SET(ifs, "nat_logging", ifs_nat_logging); 6349 TUNE_SET(ifs, "fr_defnatage", ifs_fr_defnatage); 6350 TUNE_SET(ifs, "fr_defnatipage", ifs_fr_defnatipage); 6351 TUNE_SET(ifs, "fr_defnaticmpage", ifs_fr_defnaticmpage); 6352 TUNE_SET(ifs, "nat_flush_level_hi", ifs_nat_flush_level_hi); 6353 TUNE_SET(ifs, "nat_flush_level_lo", ifs_nat_flush_level_lo); 6354 TUNE_SET(ifs, "state_flush_level_hi", ifs_state_flush_level_hi); 6355 TUNE_SET(ifs, "state_flush_level_lo", ifs_state_flush_level_lo); 6356 TUNE_SET(ifs, "ipfr_size", ifs_ipfr_size); 6357 TUNE_SET(ifs, "fr_ipfrttl", ifs_fr_ipfrttl); 6358 TUNE_SET(ifs, "ipf_loopback", ifs_ipf_loopback); 6359 #ifdef IPFILTER_LOG 6360 TUNE_SET(ifs, "ipl_suppress", ifs_ipl_suppress); 6361 TUNE_SET(ifs, "ipl_buffer_sz", ifs_ipl_buffer_sz); 6362 TUNE_SET(ifs, "ipl_logmax", ifs_ipl_logmax); 6363 TUNE_SET(ifs, "ipl_logall", ifs_ipl_logall); 6364 TUNE_SET(ifs, "ipl_logsize", ifs_ipl_logsize); 6365 #endif 6366 #undef TUNE_SET 6367 6368 ipftuneable_setdefs(ifs); 6369 6370 #ifdef _KERNEL 6371 (void) ipf_property_update(ipf_dev_info, ifs); 6372 #endif 6373 } 6374 6375 void 6376 ipftuneable_free(ipf_stack_t *ifs) 6377 { 6378 KFREES(ifs->ifs_ipf_tuneables, sizeof (lcl_ipf_tuneables)); 6379 ifs->ifs_ipf_tuneables = NULL; 6380 } 6381 6382 /* ------------------------------------------------------------------------ */ 6383 /* Function: fr_findtunebycookie */ 6384 /* Returns: NULL = search failed, else pointer to tune struct */ 6385 /* Parameters: cookie(I) - cookie value to search for amongst tuneables */ 6386 /* next(O) - pointer to place to store the cookie for the */ 6387 /* "next" tuneable, if it is desired. */ 6388 /* */ 6389 /* This function is used to walk through all of the existing tunables with */ 6390 /* successive calls. It searches the known tunables for the one which has */ 6391 /* a matching value for "cookie" - ie its address. When returning a match, */ 6392 /* the next one to be found may be returned inside next. */ 6393 /* ------------------------------------------------------------------------ */ 6394 static ipftuneable_t *fr_findtunebycookie(cookie, next, ifs) 6395 void *cookie, **next; 6396 ipf_stack_t * ifs; 6397 { 6398 ipftuneable_t *ta, **tap; 6399 6400 for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++) 6401 if (ta == cookie) { 6402 if (next != NULL) { 6403 /* 6404 * If the next entry in the array has a name 6405 * present, then return a pointer to it for 6406 * where to go next, else return a pointer to 6407 * the dynaminc list as a key to search there 6408 * next. This facilitates a weak linking of 6409 * the two "lists" together. 6410 */ 6411 if ((ta + 1)->ipft_name != NULL) 6412 *next = ta + 1; 6413 else 6414 *next = &ifs->ifs_ipf_tunelist; 6415 } 6416 return ta; 6417 } 6418 6419 for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next) 6420 if (tap == cookie) { 6421 if (next != NULL) 6422 *next = &ta->ipft_next; 6423 return ta; 6424 } 6425 6426 if (next != NULL) 6427 *next = NULL; 6428 return NULL; 6429 } 6430 6431 6432 /* ------------------------------------------------------------------------ */ 6433 /* Function: fr_findtunebyname */ 6434 /* Returns: NULL = search failed, else pointer to tune struct */ 6435 /* Parameters: name(I) - name of the tuneable entry to find. */ 6436 /* */ 6437 /* Search the static array of tuneables and the list of dynamic tuneables */ 6438 /* for an entry with a matching name. If we can find one, return a pointer */ 6439 /* to the matching structure. */ 6440 /* ------------------------------------------------------------------------ */ 6441 static ipftuneable_t *fr_findtunebyname(name, ifs) 6442 const char *name; 6443 ipf_stack_t *ifs; 6444 { 6445 ipftuneable_t *ta; 6446 6447 for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++) 6448 if (!strcmp(ta->ipft_name, name)) { 6449 return ta; 6450 } 6451 6452 for (ta = ifs->ifs_ipf_tunelist; ta != NULL; ta = ta->ipft_next) 6453 if (!strcmp(ta->ipft_name, name)) { 6454 return ta; 6455 } 6456 6457 return NULL; 6458 } 6459 6460 6461 /* ------------------------------------------------------------------------ */ 6462 /* Function: fr_addipftune */ 6463 /* Returns: int - 0 == success, else failure */ 6464 /* Parameters: newtune - pointer to new tune struct to add to tuneables */ 6465 /* */ 6466 /* Appends the tune structure pointer to by "newtune" to the end of the */ 6467 /* current list of "dynamic" tuneable parameters. Once added, the owner */ 6468 /* of the object is not expected to ever change "ipft_next". */ 6469 /* ------------------------------------------------------------------------ */ 6470 int fr_addipftune(newtune, ifs) 6471 ipftuneable_t *newtune; 6472 ipf_stack_t *ifs; 6473 { 6474 ipftuneable_t *ta, **tap; 6475 6476 ta = fr_findtunebyname(newtune->ipft_name, ifs); 6477 if (ta != NULL) 6478 return EEXIST; 6479 6480 for (tap = &ifs->ifs_ipf_tunelist; *tap != NULL; tap = &(*tap)->ipft_next) 6481 ; 6482 6483 newtune->ipft_next = NULL; 6484 *tap = newtune; 6485 return 0; 6486 } 6487 6488 6489 /* ------------------------------------------------------------------------ */ 6490 /* Function: fr_delipftune */ 6491 /* Returns: int - 0 == success, else failure */ 6492 /* Parameters: oldtune - pointer to tune struct to remove from the list of */ 6493 /* current dynamic tuneables */ 6494 /* */ 6495 /* Search for the tune structure, by pointer, in the list of those that are */ 6496 /* dynamically added at run time. If found, adjust the list so that this */ 6497 /* structure is no longer part of it. */ 6498 /* ------------------------------------------------------------------------ */ 6499 int fr_delipftune(oldtune, ifs) 6500 ipftuneable_t *oldtune; 6501 ipf_stack_t *ifs; 6502 { 6503 ipftuneable_t *ta, **tap; 6504 6505 for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next) 6506 if (ta == oldtune) { 6507 *tap = oldtune->ipft_next; 6508 oldtune->ipft_next = NULL; 6509 return 0; 6510 } 6511 6512 return ESRCH; 6513 } 6514 6515 6516 /* ------------------------------------------------------------------------ */ 6517 /* Function: fr_ipftune */ 6518 /* Returns: int - 0 == success, else failure */ 6519 /* Parameters: cmd(I) - ioctl command number */ 6520 /* data(I) - pointer to ioctl data structure */ 6521 /* */ 6522 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */ 6523 /* three ioctls provide the means to access and control global variables */ 6524 /* within IPFilter, allowing (for example) timeouts and table sizes to be */ 6525 /* changed without rebooting, reloading or recompiling. The initialisation */ 6526 /* and 'destruction' routines of the various components of ipfilter are all */ 6527 /* each responsible for handling their own values being too big. */ 6528 /* ------------------------------------------------------------------------ */ 6529 int fr_ipftune(cmd, data, ifs) 6530 ioctlcmd_t cmd; 6531 void *data; 6532 ipf_stack_t *ifs; 6533 { 6534 ipftuneable_t *ta; 6535 ipftune_t tu; 6536 void *cookie; 6537 int error; 6538 6539 error = fr_inobj(data, &tu, IPFOBJ_TUNEABLE); 6540 if (error != 0) 6541 return error; 6542 6543 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 6544 cookie = tu.ipft_cookie; 6545 ta = NULL; 6546 6547 switch (cmd) 6548 { 6549 case SIOCIPFGETNEXT : 6550 /* 6551 * If cookie is non-NULL, assume it to be a pointer to the last 6552 * entry we looked at, so find it (if possible) and return a 6553 * pointer to the next one after it. The last entry in the 6554 * the table is a NULL entry, so when we get to it, set cookie 6555 * to NULL and return that, indicating end of list, erstwhile 6556 * if we come in with cookie set to NULL, we are starting anew 6557 * at the front of the list. 6558 */ 6559 if (cookie != NULL) { 6560 ta = fr_findtunebycookie(cookie, &tu.ipft_cookie, ifs); 6561 } else { 6562 ta = ifs->ifs_ipf_tuneables; 6563 tu.ipft_cookie = ta + 1; 6564 } 6565 if (ta != NULL) { 6566 /* 6567 * Entry found, but does the data pointed to by that 6568 * row fit in what we can return? 6569 */ 6570 if (ta->ipft_sz > sizeof(tu.ipft_un)) 6571 return EINVAL; 6572 6573 tu.ipft_vlong = 0; 6574 if (ta->ipft_sz == sizeof(u_long)) 6575 tu.ipft_vlong = *ta->ipft_plong; 6576 else if (ta->ipft_sz == sizeof(u_int)) 6577 tu.ipft_vint = *ta->ipft_pint; 6578 else if (ta->ipft_sz == sizeof(u_short)) 6579 tu.ipft_vshort = *ta->ipft_pshort; 6580 else if (ta->ipft_sz == sizeof(u_char)) 6581 tu.ipft_vchar = *ta->ipft_pchar; 6582 6583 tu.ipft_sz = ta->ipft_sz; 6584 tu.ipft_min = ta->ipft_min; 6585 tu.ipft_max = ta->ipft_max; 6586 tu.ipft_flags = ta->ipft_flags; 6587 bcopy(ta->ipft_name, tu.ipft_name, 6588 MIN(sizeof(tu.ipft_name), 6589 strlen(ta->ipft_name) + 1)); 6590 } 6591 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 6592 break; 6593 6594 case SIOCIPFGET : 6595 case SIOCIPFSET : 6596 /* 6597 * Search by name or by cookie value for a particular entry 6598 * in the tuning paramter table. 6599 */ 6600 error = ESRCH; 6601 if (cookie != NULL) { 6602 ta = fr_findtunebycookie(cookie, NULL, ifs); 6603 if (ta != NULL) 6604 error = 0; 6605 } else if (tu.ipft_name[0] != '\0') { 6606 ta = fr_findtunebyname(tu.ipft_name, ifs); 6607 if (ta != NULL) 6608 error = 0; 6609 } 6610 if (error != 0) 6611 break; 6612 6613 if (cmd == (ioctlcmd_t)SIOCIPFGET) { 6614 /* 6615 * Fetch the tuning parameters for a particular value 6616 */ 6617 tu.ipft_vlong = 0; 6618 if (ta->ipft_sz == sizeof(u_long)) 6619 tu.ipft_vlong = *ta->ipft_plong; 6620 else if (ta->ipft_sz == sizeof(u_int)) 6621 tu.ipft_vint = *ta->ipft_pint; 6622 else if (ta->ipft_sz == sizeof(u_short)) 6623 tu.ipft_vshort = *ta->ipft_pshort; 6624 else if (ta->ipft_sz == sizeof(u_char)) 6625 tu.ipft_vchar = *ta->ipft_pchar; 6626 tu.ipft_cookie = ta; 6627 tu.ipft_sz = ta->ipft_sz; 6628 tu.ipft_min = ta->ipft_min; 6629 tu.ipft_max = ta->ipft_max; 6630 tu.ipft_flags = ta->ipft_flags; 6631 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 6632 6633 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) { 6634 /* 6635 * Set an internal parameter. The hard part here is 6636 * getting the new value safely and correctly out of 6637 * the kernel (given we only know its size, not type.) 6638 */ 6639 u_long in; 6640 6641 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) && 6642 (ifs->ifs_fr_running > 0)) { 6643 error = EBUSY; 6644 break; 6645 } 6646 6647 in = tu.ipft_vlong; 6648 if (in < ta->ipft_min || in > ta->ipft_max) { 6649 error = EINVAL; 6650 break; 6651 } 6652 6653 if (ta->ipft_sz == sizeof(u_long)) { 6654 tu.ipft_vlong = *ta->ipft_plong; 6655 *ta->ipft_plong = in; 6656 } else if (ta->ipft_sz == sizeof(u_int)) { 6657 tu.ipft_vint = *ta->ipft_pint; 6658 *ta->ipft_pint = (u_int)(in & 0xffffffff); 6659 } else if (ta->ipft_sz == sizeof(u_short)) { 6660 tu.ipft_vshort = *ta->ipft_pshort; 6661 *ta->ipft_pshort = (u_short)(in & 0xffff); 6662 } else if (ta->ipft_sz == sizeof(u_char)) { 6663 tu.ipft_vchar = *ta->ipft_pchar; 6664 *ta->ipft_pchar = (u_char)(in & 0xff); 6665 } 6666 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 6667 } 6668 break; 6669 6670 default : 6671 error = EINVAL; 6672 break; 6673 } 6674 6675 return error; 6676 } 6677 6678 6679 /* ------------------------------------------------------------------------ */ 6680 /* Function: fr_initialise */ 6681 /* Returns: int - 0 == success, < 0 == failure */ 6682 /* Parameters: None. */ 6683 /* */ 6684 /* Call of the initialise functions for all the various subsystems inside */ 6685 /* of IPFilter. If any of them should fail, return immeadiately a failure */ 6686 /* BUT do not try to recover from the error here. */ 6687 /* ------------------------------------------------------------------------ */ 6688 int fr_initialise(ifs) 6689 ipf_stack_t *ifs; 6690 { 6691 int i; 6692 6693 #ifdef IPFILTER_LOG 6694 i = fr_loginit(ifs); 6695 if (i < 0) 6696 return -10 + i; 6697 #endif 6698 i = fr_natinit(ifs); 6699 if (i < 0) 6700 return -20 + i; 6701 6702 i = fr_stateinit(ifs); 6703 if (i < 0) 6704 return -30 + i; 6705 6706 i = fr_authinit(ifs); 6707 if (i < 0) 6708 return -40 + i; 6709 6710 i = fr_fraginit(ifs); 6711 if (i < 0) 6712 return -50 + i; 6713 6714 i = appr_init(ifs); 6715 if (i < 0) 6716 return -60 + i; 6717 6718 #ifdef IPFILTER_SYNC 6719 i = ipfsync_init(ifs); 6720 if (i < 0) 6721 return -70 + i; 6722 #endif 6723 #ifdef IPFILTER_SCAN 6724 i = ipsc_init(ifs); 6725 if (i < 0) 6726 return -80 + i; 6727 #endif 6728 #ifdef IPFILTER_LOOKUP 6729 i = ip_lookup_init(ifs); 6730 if (i < 0) 6731 return -90 + i; 6732 #endif 6733 #ifdef IPFILTER_COMPILED 6734 ipfrule_add(ifs); 6735 #endif 6736 return 0; 6737 } 6738 6739 6740 /* ------------------------------------------------------------------------ */ 6741 /* Function: fr_deinitialise */ 6742 /* Returns: None. */ 6743 /* Parameters: None. */ 6744 /* */ 6745 /* Call all the various subsystem cleanup routines to deallocate memory or */ 6746 /* destroy locks or whatever they've done that they need to now undo. */ 6747 /* The order here IS important as there are some cross references of */ 6748 /* internal data structures. */ 6749 /* ------------------------------------------------------------------------ */ 6750 void fr_deinitialise(ifs) 6751 ipf_stack_t *ifs; 6752 { 6753 fr_fragunload(ifs); 6754 fr_authunload(ifs); 6755 fr_natunload(ifs); 6756 fr_stateunload(ifs); 6757 #ifdef IPFILTER_SCAN 6758 fr_scanunload(ifs); 6759 #endif 6760 appr_unload(ifs); 6761 6762 #ifdef IPFILTER_COMPILED 6763 ipfrule_remove(ifs); 6764 #endif 6765 6766 (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs); 6767 (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE, ifs); 6768 (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs); 6769 (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE, ifs); 6770 6771 #ifdef IPFILTER_LOOKUP 6772 ip_lookup_unload(ifs); 6773 #endif 6774 6775 #ifdef IPFILTER_LOG 6776 fr_logunload(ifs); 6777 #endif 6778 } 6779 6780 6781 /* ------------------------------------------------------------------------ */ 6782 /* Function: fr_zerostats */ 6783 /* Returns: int - 0 = success, else failure */ 6784 /* Parameters: data(O) - pointer to pointer for copying data back to */ 6785 /* */ 6786 /* Copies the current statistics out to userspace and then zero's the */ 6787 /* current ones in the kernel. The lock is only held across the bzero() as */ 6788 /* the copyout may result in paging (ie network activity.) */ 6789 /* ------------------------------------------------------------------------ */ 6790 int fr_zerostats(data, ifs) 6791 caddr_t data; 6792 ipf_stack_t *ifs; 6793 { 6794 friostat_t fio; 6795 int error; 6796 6797 fr_getstat(&fio, ifs); 6798 error = copyoutptr(&fio, data, sizeof(fio)); 6799 if (error) 6800 return EFAULT; 6801 6802 WRITE_ENTER(&ifs->ifs_ipf_mutex); 6803 bzero((char *)ifs->ifs_frstats, sizeof(*ifs->ifs_frstats) * 2); 6804 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 6805 6806 return 0; 6807 } 6808 6809 6810 #ifdef _KERNEL 6811 /* ------------------------------------------------------------------------ */ 6812 /* Function: fr_resolvedest */ 6813 /* Returns: Nil */ 6814 /* Parameters: fdp(IO) - pointer to destination information to resolve */ 6815 /* v(I) - IP protocol version to match */ 6816 /* */ 6817 /* Looks up an interface name in the frdest structure pointed to by fdp and */ 6818 /* if a matching name can be found for the particular IP protocol version */ 6819 /* then store the interface pointer in the frdest struct. If no match is */ 6820 /* found, then set the interface pointer to be -1 as NULL is considered to */ 6821 /* indicate there is no information at all in the structure. */ 6822 /* ------------------------------------------------------------------------ */ 6823 void fr_resolvedest(fdp, v, ifs) 6824 frdest_t *fdp; 6825 int v; 6826 ipf_stack_t *ifs; 6827 { 6828 fdp->fd_ifp = NULL; 6829 6830 if (*fdp->fd_ifname != '\0') { 6831 fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs); 6832 if (fdp->fd_ifp == NULL) 6833 fdp->fd_ifp = (void *)-1; 6834 } 6835 } 6836 #endif /* _KERNEL */ 6837 6838 6839 /* ------------------------------------------------------------------------ */ 6840 /* Function: fr_resolvenic */ 6841 /* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */ 6842 /* pointer to interface structure for NIC */ 6843 /* Parameters: name(I) - complete interface name */ 6844 /* v(I) - IP protocol version */ 6845 /* */ 6846 /* Look for a network interface structure that firstly has a matching name */ 6847 /* to that passed in and that is also being used for that IP protocol */ 6848 /* version (necessary on some platforms where there are separate listings */ 6849 /* for both IPv4 and IPv6 on the same physical NIC. */ 6850 /* */ 6851 /* One might wonder why name gets terminated with a \0 byte in here. The */ 6852 /* reason is an interface name could get into the kernel structures of ipf */ 6853 /* in any number of ways and so long as they all use the same sized array */ 6854 /* to put the name in, it makes sense to ensure it gets null terminated */ 6855 /* before it is used for its intended purpose - finding its match in the */ 6856 /* kernel's list of configured interfaces. */ 6857 /* */ 6858 /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an */ 6859 /* array for the name that is LIFNAMSIZ bytes (at least) in length. */ 6860 /* ------------------------------------------------------------------------ */ 6861 void *fr_resolvenic(name, v, ifs) 6862 char *name; 6863 int v; 6864 ipf_stack_t *ifs; 6865 { 6866 void *nic; 6867 6868 if (name[0] == '\0') 6869 return NULL; 6870 6871 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) { 6872 return NULL; 6873 } 6874 6875 name[LIFNAMSIZ - 1] = '\0'; 6876 6877 nic = GETIFP(name, v, ifs); 6878 if (nic == NULL) 6879 nic = (void *)-1; 6880 return nic; 6881 } 6882 6883 6884 /* ------------------------------------------------------------------------ */ 6885 /* Function: ipf_expiretokens */ 6886 /* Returns: None. */ 6887 /* Parameters: ifs - ipf stack instance */ 6888 /* */ 6889 /* This function is run every ipf tick to see if there are any tokens that */ 6890 /* have been held for too long and need to be freed up. */ 6891 /* ------------------------------------------------------------------------ */ 6892 void ipf_expiretokens(ifs) 6893 ipf_stack_t *ifs; 6894 { 6895 ipftoken_t *it; 6896 6897 WRITE_ENTER(&ifs->ifs_ipf_tokens); 6898 while ((it = ifs->ifs_ipftokenhead) != NULL) { 6899 if (it->ipt_die > ifs->ifs_fr_ticks) 6900 break; 6901 6902 ipf_freetoken(it, ifs); 6903 } 6904 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 6905 } 6906 6907 6908 /* ------------------------------------------------------------------------ */ 6909 /* Function: ipf_deltoken */ 6910 /* Returns: int - 0 = success, else error */ 6911 /* Parameters: type(I) - the token type to match */ 6912 /* uid(I) - uid owning the token */ 6913 /* ptr(I) - context pointer for the token */ 6914 /* ifs - ipf stack instance */ 6915 /* */ 6916 /* This function looks for a a token in the current list that matches up */ 6917 /* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */ 6918 /* call ipf_freetoken() to remove it from the list. */ 6919 /* ------------------------------------------------------------------------ */ 6920 int ipf_deltoken(type, uid, ptr, ifs) 6921 int type, uid; 6922 void *ptr; 6923 ipf_stack_t *ifs; 6924 { 6925 ipftoken_t *it; 6926 int error = ESRCH; 6927 6928 WRITE_ENTER(&ifs->ifs_ipf_tokens); 6929 for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) 6930 if (ptr == it->ipt_ctx && type == it->ipt_type && 6931 uid == it->ipt_uid) { 6932 ipf_freetoken(it, ifs); 6933 error = 0; 6934 break; 6935 } 6936 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 6937 6938 return error; 6939 } 6940 6941 6942 /* ------------------------------------------------------------------------ */ 6943 /* Function: ipf_unlinktoken */ 6944 /* Returns: None. */ 6945 /* Parameters: token(I) - pointer to token structure */ 6946 /* ifs - ipf stack instance */ 6947 /* */ 6948 /* This function unlinks a token structure from the linked list of tokens */ 6949 /* that it belongs to. The head pointer never needs to be explicitly */ 6950 /* adjusted, but the tail does due to the linked list implementation. */ 6951 /* ------------------------------------------------------------------------ */ 6952 static void ipf_unlinktoken(token, ifs) 6953 ipftoken_t *token; 6954 ipf_stack_t *ifs; 6955 { 6956 6957 if (ifs->ifs_ipftokentail == &token->ipt_next) 6958 ifs->ifs_ipftokentail = token->ipt_pnext; 6959 6960 *token->ipt_pnext = token->ipt_next; 6961 if (token->ipt_next != NULL) 6962 token->ipt_next->ipt_pnext = token->ipt_pnext; 6963 } 6964 6965 6966 /* ------------------------------------------------------------------------ */ 6967 /* Function: ipf_findtoken */ 6968 /* Returns: ipftoken_t * - NULL if no memory, else pointer to token */ 6969 /* Parameters: type(I) - the token type to match */ 6970 /* uid(I) - uid owning the token */ 6971 /* ptr(I) - context pointer for the token */ 6972 /* ifs - ipf stack instance */ 6973 /* */ 6974 /* This function looks for a live token in the list of current tokens that */ 6975 /* matches the tuple (type, uid, ptr). If one cannot be found then one is */ 6976 /* allocated. If one is found then it is moved to the top of the list of */ 6977 /* currently active tokens. */ 6978 /* */ 6979 /* NOTE: It is by design that this function returns holding a read lock on */ 6980 /* ipf_tokens. Callers must make sure they release it! */ 6981 /* ------------------------------------------------------------------------ */ 6982 ipftoken_t *ipf_findtoken(type, uid, ptr, ifs) 6983 int type, uid; 6984 void *ptr; 6985 ipf_stack_t *ifs; 6986 { 6987 ipftoken_t *it, *new; 6988 6989 KMALLOC(new, ipftoken_t *); 6990 6991 WRITE_ENTER(&ifs->ifs_ipf_tokens); 6992 for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) { 6993 if (it->ipt_alive == 0) 6994 continue; 6995 if (ptr == it->ipt_ctx && type == it->ipt_type && 6996 uid == it->ipt_uid) 6997 break; 6998 } 6999 7000 if (it == NULL) { 7001 it = new; 7002 new = NULL; 7003 if (it == NULL) 7004 return NULL; 7005 it->ipt_data = NULL; 7006 it->ipt_ctx = ptr; 7007 it->ipt_uid = uid; 7008 it->ipt_type = type; 7009 it->ipt_next = NULL; 7010 it->ipt_alive = 1; 7011 } else { 7012 if (new != NULL) { 7013 KFREE(new); 7014 new = NULL; 7015 } 7016 7017 ipf_unlinktoken(it, ifs); 7018 } 7019 it->ipt_pnext = ifs->ifs_ipftokentail; 7020 *ifs->ifs_ipftokentail = it; 7021 ifs->ifs_ipftokentail = &it->ipt_next; 7022 it->ipt_next = NULL; 7023 7024 it->ipt_die = ifs->ifs_fr_ticks + 2; 7025 7026 MUTEX_DOWNGRADE(&ifs->ifs_ipf_tokens); 7027 7028 return it; 7029 } 7030 7031 7032 /* ------------------------------------------------------------------------ */ 7033 /* Function: ipf_freetoken */ 7034 /* Returns: None. */ 7035 /* Parameters: token(I) - pointer to token structure */ 7036 /* ifs - ipf stack instance */ 7037 /* */ 7038 /* This function unlinks a token from the linked list and on the path to */ 7039 /* free'ing the data, it calls the dereference function that is associated */ 7040 /* with the type of data pointed to by the token as it is considered to */ 7041 /* hold a reference to it. */ 7042 /* ------------------------------------------------------------------------ */ 7043 void ipf_freetoken(token, ifs) 7044 ipftoken_t *token; 7045 ipf_stack_t *ifs; 7046 { 7047 void *data, **datap; 7048 7049 ipf_unlinktoken(token, ifs); 7050 7051 data = token->ipt_data; 7052 datap = &data; 7053 7054 if ((data != NULL) && (data != (void *)-1)) { 7055 switch (token->ipt_type) 7056 { 7057 case IPFGENITER_IPF : 7058 (void)fr_derefrule((frentry_t **)datap, ifs); 7059 break; 7060 case IPFGENITER_IPNAT : 7061 WRITE_ENTER(&ifs->ifs_ipf_nat); 7062 fr_ipnatderef((ipnat_t **)datap, ifs); 7063 RWLOCK_EXIT(&ifs->ifs_ipf_nat); 7064 break; 7065 case IPFGENITER_NAT : 7066 fr_natderef((nat_t **)datap, ifs); 7067 break; 7068 case IPFGENITER_STATE : 7069 fr_statederef((ipstate_t **)datap, ifs); 7070 break; 7071 case IPFGENITER_FRAG : 7072 fr_fragderef((ipfr_t **)datap, &ifs->ifs_ipf_frag, ifs); 7073 break; 7074 case IPFGENITER_NATFRAG : 7075 fr_fragderef((ipfr_t **)datap, 7076 &ifs->ifs_ipf_natfrag, ifs); 7077 break; 7078 case IPFGENITER_HOSTMAP : 7079 WRITE_ENTER(&ifs->ifs_ipf_nat); 7080 fr_hostmapdel((hostmap_t **)datap); 7081 RWLOCK_EXIT(&ifs->ifs_ipf_nat); 7082 break; 7083 default : 7084 (void) ip_lookup_iterderef(token->ipt_type, data, ifs); 7085 break; 7086 } 7087 } 7088 7089 KFREE(token); 7090 } 7091 7092 7093 /* ------------------------------------------------------------------------ */ 7094 /* Function: ipf_getnextrule */ 7095 /* Returns: int - 0 = success, else error */ 7096 /* Parameters: t(I) - pointer to destination information to resolve */ 7097 /* ptr(I) - pointer to ipfobj_t to copyin from user space */ 7098 /* ifs - ipf stack instance */ 7099 /* */ 7100 /* This function's first job is to bring in the ipfruleiter_t structure via */ 7101 /* the ipfobj_t structure to determine what should be the next rule to */ 7102 /* return. Once the ipfruleiter_t has been brought in, it then tries to */ 7103 /* find the 'next rule'. This may include searching rule group lists or */ 7104 /* just be as simple as looking at the 'next' field in the rule structure. */ 7105 /* When we have found the rule to return, increase its reference count and */ 7106 /* if we used an existing rule to get here, decrease its reference count. */ 7107 /* ------------------------------------------------------------------------ */ 7108 int ipf_getnextrule(t, ptr, ifs) 7109 ipftoken_t *t; 7110 void *ptr; 7111 ipf_stack_t *ifs; 7112 { 7113 frentry_t *fr, *next, zero; 7114 int error, out, count; 7115 ipfruleiter_t it; 7116 frgroup_t *fg; 7117 char *dst; 7118 7119 if (t == NULL || ptr == NULL) 7120 return EFAULT; 7121 error = fr_inobj(ptr, &it, IPFOBJ_IPFITER); 7122 if (error != 0) 7123 return error; 7124 if ((it.iri_ver != AF_INET) && (it.iri_ver != AF_INET6)) 7125 return EINVAL; 7126 if ((it.iri_inout < 0) || (it.iri_inout > 3)) 7127 return EINVAL; 7128 if (it.iri_nrules == 0) 7129 return EINVAL; 7130 if ((it.iri_active != 0) && (it.iri_active != 1)) 7131 return EINVAL; 7132 if (it.iri_rule == NULL) 7133 return EFAULT; 7134 7135 /* 7136 * Use bitmask on it.iri_inout to determine direction. 7137 * F_OUT (1) and F_ACOUT (3) mask to out = 1, while 7138 * F_IN (0) and F_ACIN (2) mask to out = 0. 7139 */ 7140 out = it.iri_inout & F_OUT; 7141 READ_ENTER(&ifs->ifs_ipf_mutex); 7142 7143 /* 7144 * Retrieve "previous" entry from token and find the next entry. 7145 */ 7146 fr = t->ipt_data; 7147 if (fr == NULL) { 7148 if (*it.iri_group == '\0') { 7149 /* 7150 * Use bitmask again to determine accounting or not. 7151 * F_ACIN will mask to accounting cases F_ACIN (2) 7152 * or F_ACOUT (3), but not F_IN or F_OUT. 7153 */ 7154 if ((it.iri_inout & F_ACIN) != 0) { 7155 if (it.iri_ver == AF_INET) 7156 next = ifs->ifs_ipacct 7157 [out][it.iri_active]; 7158 else 7159 next = ifs->ifs_ipacct6 7160 [out][it.iri_active]; 7161 } else { 7162 if (it.iri_ver == AF_INET) 7163 next = ifs->ifs_ipfilter 7164 [out][it.iri_active]; 7165 else 7166 next = ifs->ifs_ipfilter6 7167 [out][it.iri_active]; 7168 } 7169 } else { 7170 fg = fr_findgroup(it.iri_group, IPL_LOGIPF, 7171 it.iri_active, NULL, ifs); 7172 if (fg != NULL) 7173 next = fg->fg_start; 7174 else 7175 next = NULL; 7176 } 7177 } else { 7178 next = fr->fr_next; 7179 } 7180 7181 dst = (char *)it.iri_rule; 7182 /* 7183 * The ipfruleiter may ask for more than 1 rule at a time to be 7184 * copied out, so long as that many exist in the list to start with! 7185 */ 7186 for (count = it.iri_nrules; count > 0; count--) { 7187 /* 7188 * If we found an entry, add reference to it and update token. 7189 * Otherwise, zero out data to be returned and NULL out token. 7190 */ 7191 if (next != NULL) { 7192 MUTEX_ENTER(&next->fr_lock); 7193 next->fr_ref++; 7194 MUTEX_EXIT(&next->fr_lock); 7195 t->ipt_data = next; 7196 } else { 7197 bzero(&zero, sizeof(zero)); 7198 next = &zero; 7199 t->ipt_data = NULL; 7200 } 7201 7202 /* 7203 * Now that we have ref, it's save to give up lock. 7204 */ 7205 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 7206 7207 /* 7208 * Copy out data and clean up references and token as needed. 7209 */ 7210 error = COPYOUT(next, dst, sizeof(*next)); 7211 if (error != 0) 7212 error = EFAULT; 7213 if (t->ipt_data == NULL) { 7214 ipf_freetoken(t, ifs); 7215 break; 7216 } else { 7217 if (fr != NULL) 7218 (void) fr_derefrule(&fr, ifs); 7219 if (next->fr_data != NULL) { 7220 dst += sizeof(*next); 7221 error = COPYOUT(next->fr_data, dst, 7222 next->fr_dsize); 7223 if (error != 0) 7224 error = EFAULT; 7225 else 7226 dst += next->fr_dsize; 7227 } 7228 if (next->fr_next == NULL) { 7229 ipf_freetoken(t, ifs); 7230 break; 7231 } 7232 } 7233 7234 if ((count == 1) || (error != 0)) 7235 break; 7236 7237 READ_ENTER(&ifs->ifs_ipf_mutex); 7238 fr = next; 7239 next = fr->fr_next; 7240 } 7241 7242 return error; 7243 } 7244 7245 7246 /* ------------------------------------------------------------------------ */ 7247 /* Function: fr_frruleiter */ 7248 /* Returns: int - 0 = success, else error */ 7249 /* Parameters: data(I) - the token type to match */ 7250 /* uid(I) - uid owning the token */ 7251 /* ptr(I) - context pointer for the token */ 7252 /* ifs - ipf stack instance */ 7253 /* */ 7254 /* This function serves as a stepping stone between fr_ipf_ioctl and */ 7255 /* ipf_getnextrule. It's role is to find the right token in the kernel for */ 7256 /* the process doing the ioctl and use that to ask for the next rule. */ 7257 /* ------------------------------------------------------------------------ */ 7258 int ipf_frruleiter(data, uid, ctx, ifs) 7259 void *data, *ctx; 7260 int uid; 7261 ipf_stack_t *ifs; 7262 { 7263 ipftoken_t *token; 7264 int error; 7265 7266 token = ipf_findtoken(IPFGENITER_IPF, uid, ctx, ifs); 7267 if (token != NULL) 7268 error = ipf_getnextrule(token, data, ifs); 7269 else 7270 error = EFAULT; 7271 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 7272 7273 return error; 7274 } 7275 7276 7277 /* ------------------------------------------------------------------------ */ 7278 /* Function: ipf_geniter */ 7279 /* Returns: int - 0 = success, else error */ 7280 /* Parameters: token(I) - pointer to ipftoken structure */ 7281 /* itp(I) - pointer to ipfgeniter structure */ 7282 /* ifs - ipf stack instance */ 7283 /* */ 7284 /* Generic iterator called from ipf_genericiter. Currently only used for */ 7285 /* walking through list of fragments. */ 7286 /* ------------------------------------------------------------------------ */ 7287 int ipf_geniter(token, itp, ifs) 7288 ipftoken_t *token; 7289 ipfgeniter_t *itp; 7290 ipf_stack_t *ifs; 7291 { 7292 int error; 7293 7294 switch (itp->igi_type) 7295 { 7296 case IPFGENITER_FRAG : 7297 error = fr_nextfrag(token, itp, &ifs->ifs_ipfr_list, 7298 &ifs->ifs_ipfr_tail, &ifs->ifs_ipf_frag, 7299 ifs); 7300 break; 7301 default : 7302 error = EINVAL; 7303 break; 7304 } 7305 7306 return error; 7307 } 7308 7309 7310 /* ------------------------------------------------------------------------ */ 7311 /* Function: ipf_genericiter */ 7312 /* Returns: int - 0 = success, else error */ 7313 /* Parameters: data(I) - the token type to match */ 7314 /* uid(I) - uid owning the token */ 7315 /* ptr(I) - context pointer for the token */ 7316 /* ifs - ipf stack instance */ 7317 /* */ 7318 /* This function serves as a stepping stone between fr_ipf_ioctl and */ 7319 /* ipf_geniter when handling SIOCGENITER. It's role is to find the right */ 7320 /* token in the kernel for the process using the ioctl, and to use that */ 7321 /* token when calling ipf_geniter. */ 7322 /* ------------------------------------------------------------------------ */ 7323 int ipf_genericiter(data, uid, ctx, ifs) 7324 void *data, *ctx; 7325 int uid; 7326 ipf_stack_t *ifs; 7327 { 7328 ipftoken_t *token; 7329 ipfgeniter_t iter; 7330 int error; 7331 7332 error = fr_inobj(data, &iter, IPFOBJ_GENITER); 7333 if (error != 0) 7334 return error; 7335 7336 token = ipf_findtoken(iter.igi_type, uid, ctx, ifs); 7337 if (token != NULL) { 7338 token->ipt_subtype = iter.igi_type; 7339 error = ipf_geniter(token, &iter, ifs); 7340 } else 7341 error = EFAULT; 7342 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 7343 7344 return error; 7345 } 7346 7347 7348 /* --------------------------------------------------------------------- */ 7349 /* Function: ipf_earlydrop */ 7350 /* Returns: number of dropped/removed entries from the queue */ 7351 /* Parameters: flushtype - which table we're cleaning (NAT or State) */ 7352 /* ifq - pointer to queue with entries to be deleted */ 7353 /* idletime - entry must be idle this long to be deleted */ 7354 /* ifs - ipf stack instance */ 7355 /* */ 7356 /* Function is invoked from state/NAT flush routines to remove entries */ 7357 /* from specified timeout queue, based on how long they've sat idle, */ 7358 /* without waiting for it to happen on its own. */ 7359 /* --------------------------------------------------------------------- */ 7360 int ipf_earlydrop(flushtype, ifq, idletime, ifs) 7361 int flushtype; 7362 ipftq_t *ifq; 7363 int idletime; 7364 ipf_stack_t *ifs; 7365 { 7366 ipftqent_t *tqe, *tqn; 7367 unsigned int dropped; 7368 int droptick; 7369 void *ent; 7370 7371 if (ifq == NULL) 7372 return (0); 7373 7374 dropped = 0; 7375 7376 /* 7377 * Determine the tick representing the idle time we're interested 7378 * in. If an entry exists in the queue, and it was touched before 7379 * that tick, then it's been idle longer than idletime, so it should 7380 * be deleted. 7381 */ 7382 droptick = ifs->ifs_fr_ticks - idletime; 7383 tqn = ifq->ifq_head; 7384 while ((tqe = tqn) != NULL && tqe->tqe_touched < droptick) { 7385 tqn = tqe->tqe_next; 7386 ent = tqe->tqe_parent; 7387 switch (flushtype) 7388 { 7389 case NAT_FLUSH: 7390 if (nat_delete((nat_t *)ent, NL_FLUSH, ifs) == 0) 7391 dropped++; 7392 break; 7393 case STATE_FLUSH: 7394 if (fr_delstate((ipstate_t *)ent, ISL_FLUSH, ifs) == 0) 7395 dropped++; 7396 break; 7397 default: 7398 return (0); 7399 } 7400 } 7401 return (dropped); 7402 } 7403 7404 7405 /* --------------------------------------------------------------------- */ 7406 /* Function: ipf_flushclosing */ 7407 /* Returns: int - number of entries deleted */ 7408 /* Parameters: flushtype - which table we're cleaning (NAT or State) */ 7409 /* stateval - TCP state at which to start removing entries */ 7410 /* ipfqs - pointer to timeout queues */ 7411 /* userqs - pointer to user defined queues */ 7412 /* ifs - ipf stack instance */ 7413 /* */ 7414 /* Remove state/NAT table entries for TCP connections which are in the */ 7415 /* process of closing, and have at least reached the state specified by */ 7416 /* the 'stateval' parameter. */ 7417 /* --------------------------------------------------------------------- */ 7418 int ipf_flushclosing(flushtype, stateval, ipfqs, userqs, ifs) 7419 int flushtype, stateval; 7420 ipftq_t *ipfqs, *userqs; 7421 ipf_stack_t *ifs; 7422 { 7423 ipftq_t *ifq, *ifqn; 7424 ipftqent_t *tqe, *tqn; 7425 int dropped; 7426 void *ent; 7427 nat_t *nat; 7428 ipstate_t *is; 7429 7430 dropped = 0; 7431 7432 /* 7433 * Start by deleting any entries in specific timeout queues. 7434 */ 7435 ifqn = &ipfqs[stateval]; 7436 while ((ifq = ifqn) != NULL) { 7437 ifqn = ifq->ifq_next; 7438 dropped += ipf_earlydrop(flushtype, ifq, (int)0, ifs); 7439 } 7440 7441 /* 7442 * Next, look through user defined queues for closing entries. 7443 */ 7444 ifqn = userqs; 7445 while ((ifq = ifqn) != NULL) { 7446 ifqn = ifq->ifq_next; 7447 tqn = ifq->ifq_head; 7448 while ((tqe = tqn) != NULL) { 7449 tqn = tqe->tqe_next; 7450 ent = tqe->tqe_parent; 7451 switch (flushtype) 7452 { 7453 case NAT_FLUSH: 7454 nat = (nat_t *)ent; 7455 if ((nat->nat_p == IPPROTO_TCP) && 7456 (nat->nat_tcpstate[0] >= stateval) && 7457 (nat->nat_tcpstate[1] >= stateval) && 7458 (nat_delete(nat, NL_EXPIRE, ifs) == 0)) 7459 dropped++; 7460 break; 7461 case STATE_FLUSH: 7462 is = (ipstate_t *)ent; 7463 if ((is->is_p == IPPROTO_TCP) && 7464 (is->is_state[0] >= stateval) && 7465 (is->is_state[1] >= stateval) && 7466 (fr_delstate(is, ISL_EXPIRE, ifs) == 0)) 7467 dropped++; 7468 break; 7469 default: 7470 return (0); 7471 } 7472 } 7473 } 7474 return (dropped); 7475 } 7476 7477 7478 /* --------------------------------------------------------------------- */ 7479 /* Function: ipf_extraflush */ 7480 /* Returns: int - number of entries flushed (0 = none) */ 7481 /* Parameters: flushtype - which table we're cleaning (NAT or State) */ 7482 /* ipfqs - pointer to 'established' timeout queue */ 7483 /* userqs - pointer to user defined queues */ 7484 /* ifs - ipf stack instance */ 7485 /* */ 7486 /* This function gets called when either NAT or state tables fill up. */ 7487 /* We need to try a bit harder to free up some space. The function will */ 7488 /* flush entries for TCP connections which have been idle a long time. */ 7489 /* */ 7490 /* Currently, the idle time is checked using values from ideltime_tab[] */ 7491 /* --------------------------------------------------------------------- */ 7492 int ipf_extraflush(flushtype, ipfqs, userqs, ifs) 7493 int flushtype; 7494 ipftq_t *ipfqs, *userqs; 7495 ipf_stack_t *ifs; 7496 { 7497 ipftq_t *ifq, *ifqn; 7498 int idletime, removed, idle_idx; 7499 7500 removed = 0; 7501 7502 /* 7503 * Determine initial threshold for minimum idle time based on 7504 * how long ipfilter has been running. Ipfilter needs to have 7505 * been up as long as the smallest interval to continue on. 7506 * 7507 * Minimum idle times stored in idletime_tab and indexed by 7508 * idle_idx. Start at upper end of array and work backwards. 7509 * 7510 * Once the index is found, set the initial idle time to the 7511 * first interval before the current ipfilter run time. 7512 */ 7513 if (ifs->ifs_fr_ticks < idletime_tab[0]) 7514 return (0); 7515 idle_idx = (sizeof (idletime_tab) / sizeof (int)) - 1; 7516 if (ifs->ifs_fr_ticks > idletime_tab[idle_idx]) { 7517 idletime = idletime_tab[idle_idx]; 7518 } else { 7519 while ((idle_idx > 0) && 7520 (ifs->ifs_fr_ticks < idletime_tab[idle_idx])) 7521 idle_idx--; 7522 7523 idletime = (ifs->ifs_fr_ticks / 7524 idletime_tab[idle_idx]) * 7525 idletime_tab[idle_idx]; 7526 } 7527 7528 while (idle_idx >= 0) { 7529 /* 7530 * Check to see if we need to delete more entries. 7531 * If we do, start with appropriate timeout queue. 7532 */ 7533 if (flushtype == NAT_FLUSH) { 7534 if (NAT_TAB_WATER_LEVEL(ifs) <= 7535 ifs->ifs_nat_flush_level_lo) 7536 break; 7537 } else if (flushtype == STATE_FLUSH) { 7538 if (ST_TAB_WATER_LEVEL(ifs) <= 7539 ifs->ifs_state_flush_level_lo) 7540 break; 7541 } else { 7542 break; 7543 } 7544 7545 removed += ipf_earlydrop(flushtype, ipfqs, idletime, ifs); 7546 7547 /* 7548 * Next, check the user defined queues. But first, make 7549 * certain that timeout queue deletions didn't do enough. 7550 */ 7551 if (flushtype == NAT_FLUSH) { 7552 if (NAT_TAB_WATER_LEVEL(ifs) <= 7553 ifs->ifs_nat_flush_level_lo) 7554 break; 7555 } else { 7556 if (ST_TAB_WATER_LEVEL(ifs) <= 7557 ifs->ifs_state_flush_level_lo) 7558 break; 7559 } 7560 ifqn = userqs; 7561 while ((ifq = ifqn) != NULL) { 7562 ifqn = ifq->ifq_next; 7563 removed += ipf_earlydrop(flushtype, ifq, idletime, ifs); 7564 } 7565 7566 /* 7567 * Adjust the granularity of idle time. 7568 * 7569 * If we reach an interval boundary, we need to 7570 * either adjust the idle time accordingly or exit 7571 * the loop altogether (if this is very last check). 7572 */ 7573 idletime -= idletime_tab[idle_idx]; 7574 if (idletime < idletime_tab[idle_idx]) { 7575 if (idle_idx != 0) { 7576 idletime = idletime_tab[idle_idx] - 7577 idletime_tab[idle_idx - 1]; 7578 idle_idx--; 7579 } else { 7580 break; 7581 } 7582 } 7583 } 7584 7585 return (removed); 7586 } 7587