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