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