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