12393Syz155240 /* 22393Syz155240 * Copyright (C) 1993-2003 by Darren Reed. 32393Syz155240 * 42393Syz155240 * See the IPFILTER.LICENCE file for details on licencing. 52393Syz155240 * 63448Sdh155122 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 72393Syz155240 * Use is subject to license terms. 82393Syz155240 */ 92393Syz155240 102393Syz155240 #pragma ident "%Z%%M% %I% %E% SMI" 112393Syz155240 122393Syz155240 #if defined(KERNEL) || defined(_KERNEL) 132393Syz155240 # undef KERNEL 142393Syz155240 # undef _KERNEL 152393Syz155240 # define KERNEL 1 162393Syz155240 # define _KERNEL 1 172393Syz155240 #endif 182393Syz155240 #include <sys/errno.h> 192393Syz155240 #include <sys/types.h> 202393Syz155240 #include <sys/param.h> 212393Syz155240 #include <sys/time.h> 222393Syz155240 #if defined(__NetBSD__) 232393Syz155240 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL) 242393Syz155240 # include "opt_ipfilter_log.h" 252393Syz155240 # endif 262393Syz155240 #endif 272393Syz155240 #if defined(_KERNEL) && defined(__FreeBSD_version) && \ 282393Syz155240 (__FreeBSD_version >= 220000) 292393Syz155240 # if (__FreeBSD_version >= 400000) 302393Syz155240 # if !defined(IPFILTER_LKM) 312393Syz155240 # include "opt_inet6.h" 322393Syz155240 # endif 332393Syz155240 # if (__FreeBSD_version == 400019) 342393Syz155240 # define CSUM_DELAY_DATA 352393Syz155240 # endif 362393Syz155240 # endif 372393Syz155240 # include <sys/filio.h> 382393Syz155240 #else 392393Syz155240 # include <sys/ioctl.h> 402393Syz155240 #endif 412393Syz155240 #if !defined(_AIX51) 422393Syz155240 # include <sys/fcntl.h> 432393Syz155240 #endif 442393Syz155240 #if defined(_KERNEL) 452393Syz155240 # include <sys/systm.h> 462393Syz155240 # include <sys/file.h> 472393Syz155240 #else 482393Syz155240 # include <stdio.h> 492393Syz155240 # include <string.h> 502393Syz155240 # include <stdlib.h> 512393Syz155240 # include <stddef.h> 522393Syz155240 # include <sys/file.h> 532393Syz155240 # define _KERNEL 542393Syz155240 # ifdef __OpenBSD__ 552393Syz155240 struct file; 562393Syz155240 # endif 572393Syz155240 # include <sys/uio.h> 582393Syz155240 # undef _KERNEL 592393Syz155240 #endif 602393Syz155240 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \ 612393Syz155240 !defined(linux) 622393Syz155240 # include <sys/mbuf.h> 632393Syz155240 #else 642393Syz155240 # if !defined(linux) 652393Syz155240 # include <sys/byteorder.h> 662393Syz155240 # endif 672393Syz155240 # if (SOLARIS2 < 5) && defined(sun) 682393Syz155240 # include <sys/dditypes.h> 692393Syz155240 # endif 702393Syz155240 #endif 712393Syz155240 #ifdef __hpux 722393Syz155240 # define _NET_ROUTE_INCLUDED 732393Syz155240 #endif 742393Syz155240 #if !defined(linux) 752393Syz155240 # include <sys/protosw.h> 762393Syz155240 #endif 772393Syz155240 #include <sys/socket.h> 782393Syz155240 #include <net/if.h> 792393Syz155240 #ifdef sun 802393Syz155240 # include <net/af.h> 812393Syz155240 #endif 822393Syz155240 #if !defined(_KERNEL) && defined(__FreeBSD__) 832393Syz155240 # include "radix_ipf.h" 842393Syz155240 #endif 852393Syz155240 #include <net/route.h> 862393Syz155240 #include <netinet/in.h> 872393Syz155240 #include <netinet/in_systm.h> 882393Syz155240 #include <netinet/ip.h> 892393Syz155240 #if !defined(linux) 902393Syz155240 # include <netinet/ip_var.h> 912393Syz155240 #endif 922393Syz155240 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */ 932393Syz155240 # include <sys/hashing.h> 942393Syz155240 # include <netinet/in_var.h> 952393Syz155240 #endif 962393Syz155240 #include <netinet/tcp.h> 972393Syz155240 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL) 982393Syz155240 # include <netinet/udp.h> 992393Syz155240 # include <netinet/ip_icmp.h> 1002393Syz155240 #endif 1012393Syz155240 #ifdef __hpux 1022393Syz155240 # undef _NET_ROUTE_INCLUDED 1032393Syz155240 #endif 1042393Syz155240 #include "netinet/ip_compat.h" 1052393Syz155240 #ifdef USE_INET6 1062393Syz155240 # include <netinet/icmp6.h> 1072393Syz155240 # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux) 1082393Syz155240 # include <netinet6/in6_var.h> 1092393Syz155240 # endif 1102393Syz155240 #endif 1112393Syz155240 #include <netinet/tcpip.h> 1122393Syz155240 #include "netinet/ip_fil.h" 1132393Syz155240 #include "netinet/ip_nat.h" 1142393Syz155240 #include "netinet/ip_frag.h" 1152393Syz155240 #include "netinet/ip_state.h" 1162393Syz155240 #include "netinet/ip_proxy.h" 1172393Syz155240 #include "netinet/ip_auth.h" 1183448Sdh155122 #include "netinet/ipf_stack.h" 1192393Syz155240 #ifdef IPFILTER_SCAN 1202393Syz155240 # include "netinet/ip_scan.h" 1212393Syz155240 #endif 1222393Syz155240 #ifdef IPFILTER_SYNC 1232393Syz155240 # include "netinet/ip_sync.h" 1242393Syz155240 #endif 1252393Syz155240 #include "netinet/ip_pool.h" 1262393Syz155240 #include "netinet/ip_htable.h" 1272393Syz155240 #ifdef IPFILTER_COMPILED 1282393Syz155240 # include "netinet/ip_rules.h" 1292393Syz155240 #endif 1302393Syz155240 #if defined(IPFILTER_BPF) && defined(_KERNEL) 1312393Syz155240 # include <net/bpf.h> 1322393Syz155240 #endif 1332393Syz155240 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000) 1342393Syz155240 # include <sys/malloc.h> 1352393Syz155240 # if defined(_KERNEL) && !defined(IPFILTER_LKM) 1362393Syz155240 # include "opt_ipfilter.h" 1372393Syz155240 # endif 1382393Syz155240 #endif 1392393Syz155240 #include "netinet/ipl.h" 1402393Syz155240 /* END OF INCLUDES */ 1412393Syz155240 1422393Syz155240 #if !defined(lint) 1432393Syz155240 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed"; 1442393Syz155240 static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $"; 1452393Syz155240 #endif 1462393Syz155240 1472393Syz155240 #ifndef _KERNEL 1482393Syz155240 # include "ipf.h" 1492393Syz155240 # include "ipt.h" 1502393Syz155240 # include "bpf-ipf.h" 1512393Syz155240 extern int opts; 1522393Syz155240 1532393Syz155240 # define FR_VERBOSE(verb_pr) verbose verb_pr 1542393Syz155240 # define FR_DEBUG(verb_pr) debug verb_pr 1552393Syz155240 #else /* #ifndef _KERNEL */ 1562393Syz155240 # define FR_VERBOSE(verb_pr) 1572393Syz155240 # define FR_DEBUG(verb_pr) 1582393Syz155240 #endif /* _KERNEL */ 1592393Syz155240 1602393Syz155240 1612393Syz155240 char ipfilter_version[] = IPL_VERSION; 1622393Syz155240 int fr_features = 0 1632393Syz155240 #ifdef IPFILTER_LKM 1642393Syz155240 | IPF_FEAT_LKM 1652393Syz155240 #endif 1662393Syz155240 #ifdef IPFILTER_LOG 1672393Syz155240 | IPF_FEAT_LOG 1682393Syz155240 #endif 1692393Syz155240 #ifdef IPFILTER_LOOKUP 1702393Syz155240 | IPF_FEAT_LOOKUP 1712393Syz155240 #endif 1722393Syz155240 #ifdef IPFILTER_BPF 1732393Syz155240 | IPF_FEAT_BPF 1742393Syz155240 #endif 1752393Syz155240 #ifdef IPFILTER_COMPILED 1762393Syz155240 | IPF_FEAT_COMPILED 1772393Syz155240 #endif 1782393Syz155240 #ifdef IPFILTER_CKSUM 1792393Syz155240 | IPF_FEAT_CKSUM 1802393Syz155240 #endif 1812393Syz155240 #ifdef IPFILTER_SYNC 1822393Syz155240 | IPF_FEAT_SYNC 1832393Syz155240 #endif 1842393Syz155240 #ifdef IPFILTER_SCAN 1852393Syz155240 | IPF_FEAT_SCAN 1862393Syz155240 #endif 1872393Syz155240 #ifdef USE_INET6 1882393Syz155240 | IPF_FEAT_IPV6 1892393Syz155240 #endif 1902393Syz155240 ; 1912393Syz155240 1922393Syz155240 static INLINE int fr_ipfcheck __P((fr_info_t *, frentry_t *, int)); 1932393Syz155240 static int fr_portcheck __P((frpcmp_t *, u_short *)); 1943448Sdh155122 static int frflushlist __P((int, minor_t, int *, frentry_t **, 1953448Sdh155122 ipf_stack_t *)); 1962393Syz155240 static ipfunc_t fr_findfunc __P((ipfunc_t)); 1972393Syz155240 static frentry_t *fr_firewall __P((fr_info_t *, u_32_t *)); 1983448Sdh155122 static int fr_funcinit __P((frentry_t *fr, ipf_stack_t *)); 1992393Syz155240 static INLINE void frpr_ah __P((fr_info_t *)); 2002393Syz155240 static INLINE void frpr_esp __P((fr_info_t *)); 2012393Syz155240 static INLINE void frpr_gre __P((fr_info_t *)); 2022393Syz155240 static INLINE void frpr_udp __P((fr_info_t *)); 2032393Syz155240 static INLINE void frpr_tcp __P((fr_info_t *)); 2042393Syz155240 static INLINE void frpr_icmp __P((fr_info_t *)); 2052393Syz155240 static INLINE void frpr_ipv4hdr __P((fr_info_t *)); 2062393Syz155240 static INLINE int frpr_pullup __P((fr_info_t *, int)); 2072393Syz155240 static INLINE void frpr_short __P((fr_info_t *, int)); 2082393Syz155240 static INLINE void frpr_tcpcommon __P((fr_info_t *)); 2092393Syz155240 static INLINE void frpr_udpcommon __P((fr_info_t *)); 2102393Syz155240 static INLINE int fr_updateipid __P((fr_info_t *)); 2112393Syz155240 #ifdef IPFILTER_LOOKUP 2123448Sdh155122 static int fr_grpmapinit __P((frentry_t *fr, ipf_stack_t *)); 2133448Sdh155122 static INLINE void *fr_resolvelookup __P((u_int, u_int, lookupfunc_t *, 2143448Sdh155122 ipf_stack_t *)); 2152393Syz155240 #endif 2163448Sdh155122 static void frsynclist __P((int, int, void *, char *, frentry_t *, 2173448Sdh155122 ipf_stack_t *)); 2182958Sdr146992 static void *fr_ifsync __P((int, int, char *, char *, 2193448Sdh155122 void *, void *, ipf_stack_t *)); 2203448Sdh155122 static ipftuneable_t *fr_findtunebyname __P((const char *, ipf_stack_t *)); 2213448Sdh155122 static ipftuneable_t *fr_findtunebycookie __P((void *, void **, ipf_stack_t *)); 2222393Syz155240 2232393Syz155240 2242393Syz155240 /* 2252393Syz155240 * bit values for identifying presence of individual IP options 2262393Syz155240 * All of these tables should be ordered by increasing key value on the left 2272393Syz155240 * hand side to allow for binary searching of the array and include a trailer 2282393Syz155240 * with a 0 for the bitmask for linear searches to easily find the end with. 2292393Syz155240 */ 2302393Syz155240 const struct optlist ipopts[20] = { 2312393Syz155240 { IPOPT_NOP, 0x000001 }, 2322393Syz155240 { IPOPT_RR, 0x000002 }, 2332393Syz155240 { IPOPT_ZSU, 0x000004 }, 2342393Syz155240 { IPOPT_MTUP, 0x000008 }, 2352393Syz155240 { IPOPT_MTUR, 0x000010 }, 2362393Syz155240 { IPOPT_ENCODE, 0x000020 }, 2372393Syz155240 { IPOPT_TS, 0x000040 }, 2382393Syz155240 { IPOPT_TR, 0x000080 }, 2392393Syz155240 { IPOPT_SECURITY, 0x000100 }, 2402393Syz155240 { IPOPT_LSRR, 0x000200 }, 2412393Syz155240 { IPOPT_E_SEC, 0x000400 }, 2422393Syz155240 { IPOPT_CIPSO, 0x000800 }, 2432393Syz155240 { IPOPT_SATID, 0x001000 }, 2442393Syz155240 { IPOPT_SSRR, 0x002000 }, 2452393Syz155240 { IPOPT_ADDEXT, 0x004000 }, 2462393Syz155240 { IPOPT_VISA, 0x008000 }, 2472393Syz155240 { IPOPT_IMITD, 0x010000 }, 2482393Syz155240 { IPOPT_EIP, 0x020000 }, 2492393Syz155240 { IPOPT_FINN, 0x040000 }, 2502393Syz155240 { 0, 0x000000 } 2512393Syz155240 }; 2522393Syz155240 2532393Syz155240 #ifdef USE_INET6 2542393Syz155240 struct optlist ip6exthdr[] = { 2552393Syz155240 { IPPROTO_HOPOPTS, 0x000001 }, 2562393Syz155240 { IPPROTO_IPV6, 0x000002 }, 2572393Syz155240 { IPPROTO_ROUTING, 0x000004 }, 2582393Syz155240 { IPPROTO_FRAGMENT, 0x000008 }, 2592393Syz155240 { IPPROTO_ESP, 0x000010 }, 2602393Syz155240 { IPPROTO_AH, 0x000020 }, 2612393Syz155240 { IPPROTO_NONE, 0x000040 }, 2622393Syz155240 { IPPROTO_DSTOPTS, 0x000080 }, 2632393Syz155240 { 0, 0 } 2642393Syz155240 }; 2652393Syz155240 #endif 2662393Syz155240 2672393Syz155240 struct optlist tcpopts[] = { 2682393Syz155240 { TCPOPT_NOP, 0x000001 }, 2692393Syz155240 { TCPOPT_MAXSEG, 0x000002 }, 2702393Syz155240 { TCPOPT_WINDOW, 0x000004 }, 2712393Syz155240 { TCPOPT_SACK_PERMITTED, 0x000008 }, 2722393Syz155240 { TCPOPT_SACK, 0x000010 }, 2732393Syz155240 { TCPOPT_TIMESTAMP, 0x000020 }, 2742393Syz155240 { 0, 0x000000 } 2752393Syz155240 }; 2762393Syz155240 2772393Syz155240 /* 2782393Syz155240 * bit values for identifying presence of individual IP security options 2792393Syz155240 */ 2802393Syz155240 const struct optlist secopt[8] = { 2812393Syz155240 { IPSO_CLASS_RES4, 0x01 }, 2822393Syz155240 { IPSO_CLASS_TOPS, 0x02 }, 2832393Syz155240 { IPSO_CLASS_SECR, 0x04 }, 2842393Syz155240 { IPSO_CLASS_RES3, 0x08 }, 2852393Syz155240 { IPSO_CLASS_CONF, 0x10 }, 2862393Syz155240 { IPSO_CLASS_UNCL, 0x20 }, 2872393Syz155240 { IPSO_CLASS_RES2, 0x40 }, 2882393Syz155240 { IPSO_CLASS_RES1, 0x80 } 2892393Syz155240 }; 2902393Syz155240 2912393Syz155240 2922393Syz155240 /* 2932393Syz155240 * Table of functions available for use with call rules. 2942393Syz155240 */ 2952393Syz155240 static ipfunc_resolve_t fr_availfuncs[] = { 2962393Syz155240 #ifdef IPFILTER_LOOKUP 2972393Syz155240 { "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit }, 2982393Syz155240 { "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit }, 2992393Syz155240 #endif 3002393Syz155240 { "", NULL } 3012393Syz155240 }; 3022393Syz155240 3032393Syz155240 3042393Syz155240 /* 3052393Syz155240 * The next section of code is a a collection of small routines that set 3062393Syz155240 * fields in the fr_info_t structure passed based on properties of the 3072393Syz155240 * current packet. There are different routines for the same protocol 3082393Syz155240 * for each of IPv4 and IPv6. Adding a new protocol, for which there 3092393Syz155240 * will "special" inspection for setup, is now more easily done by adding 3102393Syz155240 * a new routine and expanding the frpr_ipinit*() function rather than by 3112393Syz155240 * adding more code to a growing switch statement. 3122393Syz155240 */ 3132393Syz155240 #ifdef USE_INET6 3142393Syz155240 static INLINE int frpr_ah6 __P((fr_info_t *)); 3152393Syz155240 static INLINE void frpr_esp6 __P((fr_info_t *)); 3162393Syz155240 static INLINE void frpr_gre6 __P((fr_info_t *)); 3172393Syz155240 static INLINE void frpr_udp6 __P((fr_info_t *)); 3182393Syz155240 static INLINE void frpr_tcp6 __P((fr_info_t *)); 3192393Syz155240 static INLINE void frpr_icmp6 __P((fr_info_t *)); 3202393Syz155240 static INLINE int frpr_ipv6hdr __P((fr_info_t *)); 3212393Syz155240 static INLINE void frpr_short6 __P((fr_info_t *, int)); 3222393Syz155240 static INLINE int frpr_hopopts6 __P((fr_info_t *)); 3232393Syz155240 static INLINE int frpr_routing6 __P((fr_info_t *)); 3242393Syz155240 static INLINE int frpr_dstopts6 __P((fr_info_t *)); 3252393Syz155240 static INLINE int frpr_fragment6 __P((fr_info_t *)); 3262393Syz155240 static INLINE int frpr_ipv6exthdr __P((fr_info_t *, int, int)); 3272393Syz155240 3282393Syz155240 3292393Syz155240 /* ------------------------------------------------------------------------ */ 3302393Syz155240 /* Function: frpr_short6 */ 3312393Syz155240 /* Returns: void */ 3322393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 3332393Syz155240 /* */ 3342393Syz155240 /* IPv6 Only */ 3352393Syz155240 /* This is function enforces the 'is a packet too short to be legit' rule */ 3362393Syz155240 /* for IPv6 and marks the packet with FI_SHORT if so. See function comment */ 3372393Syz155240 /* for frpr_short() for more details. */ 3382393Syz155240 /* ------------------------------------------------------------------------ */ 3392393Syz155240 static INLINE void frpr_short6(fin, xmin) 3402393Syz155240 fr_info_t *fin; 3412393Syz155240 int xmin; 3422393Syz155240 { 3432393Syz155240 3442393Syz155240 if (fin->fin_dlen < xmin) 3452393Syz155240 fin->fin_flx |= FI_SHORT; 3462393Syz155240 } 3472393Syz155240 3482393Syz155240 3492393Syz155240 /* ------------------------------------------------------------------------ */ 3502393Syz155240 /* Function: frpr_ipv6hdr */ 3512393Syz155240 /* Returns: int */ 3522393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 3532393Syz155240 /* */ 3542393Syz155240 /* IPv6 Only */ 3552393Syz155240 /* Copy values from the IPv6 header into the fr_info_t struct and call the */ 3562393Syz155240 /* per-protocol analyzer if it exists. */ 3572393Syz155240 /* ------------------------------------------------------------------------ */ 3582393Syz155240 static INLINE int frpr_ipv6hdr(fin) 3592393Syz155240 fr_info_t *fin; 3602393Syz155240 { 3612393Syz155240 ip6_t *ip6 = (ip6_t *)fin->fin_ip; 3622393Syz155240 int p, go = 1, i, hdrcount; 3632393Syz155240 fr_ip_t *fi = &fin->fin_fi; 3642393Syz155240 3652393Syz155240 fin->fin_off = 0; 3662393Syz155240 3672393Syz155240 fi->fi_tos = 0; 3682393Syz155240 fi->fi_optmsk = 0; 3692393Syz155240 fi->fi_secmsk = 0; 3702393Syz155240 fi->fi_auth = 0; 3712393Syz155240 3722393Syz155240 p = ip6->ip6_nxt; 3732393Syz155240 fi->fi_ttl = ip6->ip6_hlim; 3742393Syz155240 fi->fi_src.in6 = ip6->ip6_src; 3752393Syz155240 fi->fi_dst.in6 = ip6->ip6_dst; 3762393Syz155240 fin->fin_id = 0; 3772393Syz155240 3782393Syz155240 hdrcount = 0; 3792393Syz155240 while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) { 3802393Syz155240 switch (p) 3812393Syz155240 { 3822393Syz155240 case IPPROTO_UDP : 3832393Syz155240 frpr_udp6(fin); 3842393Syz155240 go = 0; 3852393Syz155240 break; 3862393Syz155240 3872393Syz155240 case IPPROTO_TCP : 3882393Syz155240 frpr_tcp6(fin); 3892393Syz155240 go = 0; 3902393Syz155240 break; 3912393Syz155240 3922393Syz155240 case IPPROTO_ICMPV6 : 3932393Syz155240 frpr_icmp6(fin); 3942393Syz155240 go = 0; 3952393Syz155240 break; 3962393Syz155240 3972393Syz155240 case IPPROTO_GRE : 3982393Syz155240 frpr_gre6(fin); 3992393Syz155240 go = 0; 4002393Syz155240 break; 4012393Syz155240 4022393Syz155240 case IPPROTO_HOPOPTS : 4032393Syz155240 /* 4042393Syz155240 * hop by hop ext header is only allowed 4052393Syz155240 * right after IPv6 header. 4062393Syz155240 */ 4072393Syz155240 if (hdrcount != 0) { 4082393Syz155240 fin->fin_flx |= FI_BAD; 4092393Syz155240 p = IPPROTO_NONE; 4102393Syz155240 } else { 4112393Syz155240 p = frpr_hopopts6(fin); 4122393Syz155240 } 4132393Syz155240 break; 4142393Syz155240 4152393Syz155240 case IPPROTO_DSTOPTS : 4162393Syz155240 p = frpr_dstopts6(fin); 4172393Syz155240 break; 4182393Syz155240 4192393Syz155240 case IPPROTO_ROUTING : 4202393Syz155240 p = frpr_routing6(fin); 4212393Syz155240 break; 4222393Syz155240 4232393Syz155240 case IPPROTO_AH : 4242393Syz155240 p = frpr_ah6(fin); 4252393Syz155240 break; 4262393Syz155240 4272393Syz155240 case IPPROTO_ESP : 4282393Syz155240 frpr_esp6(fin); 4292393Syz155240 go = 0; 4302393Syz155240 break; 4312393Syz155240 4322393Syz155240 case IPPROTO_IPV6 : 4332393Syz155240 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 4342393Syz155240 if (ip6exthdr[i].ol_val == p) { 4352393Syz155240 fin->fin_flx |= ip6exthdr[i].ol_bit; 4362393Syz155240 break; 4372393Syz155240 } 4382393Syz155240 go = 0; 4392393Syz155240 break; 4402393Syz155240 4412393Syz155240 case IPPROTO_NONE : 4422393Syz155240 go = 0; 4432393Syz155240 break; 4442393Syz155240 4452393Syz155240 case IPPROTO_FRAGMENT : 4462393Syz155240 p = frpr_fragment6(fin); 4472393Syz155240 if (fin->fin_off != 0) /* Not the first frag */ 4482393Syz155240 go = 0; 4492393Syz155240 break; 4502393Syz155240 4512393Syz155240 default : 4522393Syz155240 go = 0; 4532393Syz155240 break; 4542393Syz155240 } 4552393Syz155240 hdrcount++; 4562393Syz155240 4572393Syz155240 /* 4582393Syz155240 * It is important to note that at this point, for the 4592393Syz155240 * extension headers (go != 0), the entire header may not have 4602393Syz155240 * been pulled up when the code gets to this point. This is 4612393Syz155240 * only done for "go != 0" because the other header handlers 4622393Syz155240 * will all pullup their complete header. The other indicator 4632393Syz155240 * of an incomplete packet is that this was just an extension 4642393Syz155240 * header. 4652393Syz155240 */ 4662393Syz155240 if ((go != 0) && (p != IPPROTO_NONE) && 4672393Syz155240 (frpr_pullup(fin, 0) == -1)) { 4682393Syz155240 p = IPPROTO_NONE; 4692393Syz155240 go = 0; 4702393Syz155240 } 4712393Syz155240 } 4722393Syz155240 fi->fi_p = p; 4732393Syz155240 4742393Syz155240 if (fin->fin_flx & FI_BAD) 4752393Syz155240 return -1; 4762393Syz155240 4772393Syz155240 return 0; 4782393Syz155240 } 4792393Syz155240 4802393Syz155240 4812393Syz155240 /* ------------------------------------------------------------------------ */ 4822393Syz155240 /* Function: frpr_ipv6exthdr */ 4832393Syz155240 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 4842393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 4852393Syz155240 /* multiple(I) - flag indicating yes/no if multiple occurances */ 4862393Syz155240 /* of this extension header are allowed. */ 4872393Syz155240 /* proto(I) - protocol number for this extension header */ 4882393Syz155240 /* */ 4892393Syz155240 /* IPv6 Only */ 4902393Syz155240 /* ------------------------------------------------------------------------ */ 4912393Syz155240 static INLINE int frpr_ipv6exthdr(fin, multiple, proto) 4922393Syz155240 fr_info_t *fin; 4932393Syz155240 int multiple, proto; 4942393Syz155240 { 4952393Syz155240 struct ip6_ext *hdr; 4962393Syz155240 u_short shift; 4972393Syz155240 int i; 4982393Syz155240 4992393Syz155240 fin->fin_flx |= FI_V6EXTHDR; 5002393Syz155240 5012393Syz155240 /* 8 is default length of extension hdr */ 5022393Syz155240 if ((fin->fin_dlen - 8) < 0) { 5032393Syz155240 fin->fin_flx |= FI_SHORT; 5042393Syz155240 return IPPROTO_NONE; 5052393Syz155240 } 5062393Syz155240 5072393Syz155240 if (frpr_pullup(fin, 8) == -1) 5082393Syz155240 return IPPROTO_NONE; 5092393Syz155240 5102393Syz155240 hdr = fin->fin_dp; 5112393Syz155240 shift = 8 + (hdr->ip6e_len << 3); 5122393Syz155240 if (shift > fin->fin_dlen) { /* Nasty extension header length? */ 5132393Syz155240 fin->fin_flx |= FI_BAD; 5142393Syz155240 return IPPROTO_NONE; 5152393Syz155240 } 5162393Syz155240 5172393Syz155240 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 5182393Syz155240 if (ip6exthdr[i].ol_val == proto) { 5192393Syz155240 /* 5202393Syz155240 * Most IPv6 extension headers are only allowed once. 5212393Syz155240 */ 5222393Syz155240 if ((multiple == 0) && 5232393Syz155240 ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) 5242393Syz155240 fin->fin_flx |= FI_BAD; 5252393Syz155240 else 5262393Syz155240 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 5272393Syz155240 break; 5282393Syz155240 } 5292393Syz155240 5302393Syz155240 fin->fin_dp = (char *)fin->fin_dp + shift; 5312393Syz155240 fin->fin_dlen -= shift; 5322393Syz155240 5332393Syz155240 return hdr->ip6e_nxt; 5342393Syz155240 } 5352393Syz155240 5362393Syz155240 5372393Syz155240 /* ------------------------------------------------------------------------ */ 5382393Syz155240 /* Function: frpr_hopopts6 */ 5392393Syz155240 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 5402393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 5412393Syz155240 /* */ 5422393Syz155240 /* IPv6 Only */ 5432393Syz155240 /* This is function checks pending hop by hop options extension header */ 5442393Syz155240 /* ------------------------------------------------------------------------ */ 5452393Syz155240 static INLINE int frpr_hopopts6(fin) 5462393Syz155240 fr_info_t *fin; 5472393Syz155240 { 5482393Syz155240 return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS); 5492393Syz155240 } 5502393Syz155240 5512393Syz155240 5522393Syz155240 /* ------------------------------------------------------------------------ */ 5532393Syz155240 /* Function: frpr_routing6 */ 5542393Syz155240 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 5552393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 5562393Syz155240 /* */ 5572393Syz155240 /* IPv6 Only */ 5582393Syz155240 /* This is function checks pending routing extension header */ 5592393Syz155240 /* ------------------------------------------------------------------------ */ 5602393Syz155240 static INLINE int frpr_routing6(fin) 5612393Syz155240 fr_info_t *fin; 5622393Syz155240 { 5632393Syz155240 struct ip6_ext *hdr; 5642393Syz155240 int shift; 5652393Syz155240 5662393Syz155240 hdr = fin->fin_dp; 5672393Syz155240 if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE) 5682393Syz155240 return IPPROTO_NONE; 5692393Syz155240 5702393Syz155240 shift = 8 + (hdr->ip6e_len << 3); 5712393Syz155240 /* 5722393Syz155240 * Nasty extension header length? 5732393Syz155240 */ 5742393Syz155240 if ((hdr->ip6e_len << 3) & 15) { 5752393Syz155240 fin->fin_flx |= FI_BAD; 5762393Syz155240 /* 5772393Syz155240 * Compensate for the changes made in frpr_ipv6exthdr() 5782393Syz155240 */ 5792393Syz155240 fin->fin_dlen += shift; 5802393Syz155240 fin->fin_dp = (char *)fin->fin_dp - shift; 5812393Syz155240 return IPPROTO_NONE; 5822393Syz155240 } 5832393Syz155240 5842393Syz155240 return hdr->ip6e_nxt; 5852393Syz155240 } 5862393Syz155240 5872393Syz155240 5882393Syz155240 /* ------------------------------------------------------------------------ */ 5892393Syz155240 /* Function: frpr_fragment6 */ 5902393Syz155240 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 5912393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 5922393Syz155240 /* */ 5932393Syz155240 /* IPv6 Only */ 5942393Syz155240 /* Examine the IPv6 fragment header and extract fragment offset information.*/ 5952393Syz155240 /* */ 5962393Syz155240 /* We don't know where the transport layer header (or whatever is next is), */ 5972393Syz155240 /* as it could be behind destination options (amongst others). Because */ 5982393Syz155240 /* there is no fragment cache, there is no knowledge about whether or not an*/ 5992393Syz155240 /* upper layer header has been seen (or where it ends) and thus we are not */ 6002393Syz155240 /* able to continue processing beyond this header with any confidence. */ 6012393Syz155240 /* ------------------------------------------------------------------------ */ 6022393Syz155240 static INLINE int frpr_fragment6(fin) 6032393Syz155240 fr_info_t *fin; 6042393Syz155240 { 6052393Syz155240 struct ip6_frag *frag; 6062393Syz155240 int dlen; 6072393Syz155240 6082393Syz155240 fin->fin_flx |= FI_FRAG; 6092393Syz155240 6102393Syz155240 dlen = fin->fin_dlen; 6112393Syz155240 if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE) 6122393Syz155240 return IPPROTO_NONE; 6132393Syz155240 6142393Syz155240 if (frpr_pullup(fin, sizeof(*frag)) == -1) 6152393Syz155240 return IPPROTO_NONE; 6162393Syz155240 6172393Syz155240 frpr_short6(fin, sizeof(*frag)); 6182393Syz155240 6192393Syz155240 if ((fin->fin_flx & FI_SHORT) != 0) 6202393Syz155240 return IPPROTO_NONE; 6212393Syz155240 6222393Syz155240 frag = (struct ip6_frag *)((char *)fin->fin_dp - sizeof(*frag)); 6232393Syz155240 /* 6242393Syz155240 * Fragment but no fragmentation info set? Bad packet... 6252393Syz155240 */ 6262393Syz155240 if (frag->ip6f_offlg == 0) { 6272393Syz155240 fin->fin_flx |= FI_BAD; 6282393Syz155240 return IPPROTO_NONE; 6292393Syz155240 } 6302393Syz155240 6312393Syz155240 fin->fin_id = frag->ip6f_ident; 6322393Syz155240 fin->fin_off = frag->ip6f_offlg & IP6F_OFF_MASK; 6332393Syz155240 fin->fin_off = ntohs(fin->fin_off); 6342393Syz155240 if (fin->fin_off != 0) 6352393Syz155240 fin->fin_flx |= FI_FRAGBODY; 6362393Syz155240 6372393Syz155240 fin->fin_dp = (char *)frag + sizeof(*frag); 6382393Syz155240 fin->fin_dlen = dlen - sizeof(*frag); 6392393Syz155240 6402393Syz155240 /* length of hdrs(after frag hdr) + data */ 6412393Syz155240 fin->fin_flen = fin->fin_dlen; 6422393Syz155240 6432393Syz155240 /* 6442393Syz155240 * If the frag is not the last one and the payload length 6452393Syz155240 * is not multiple of 8, it must be dropped. 6462393Syz155240 */ 6472393Syz155240 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) && (dlen % 8)) { 6482393Syz155240 fin->fin_flx |= FI_BAD; 6492393Syz155240 return IPPROTO_NONE; 6502393Syz155240 } 6512393Syz155240 6522393Syz155240 return frag->ip6f_nxt; 6532393Syz155240 } 6542393Syz155240 6552393Syz155240 6562393Syz155240 /* ------------------------------------------------------------------------ */ 6572393Syz155240 /* Function: frpr_dstopts6 */ 6582393Syz155240 /* Returns: int - value of the next header or IPPROTO_NONE if error */ 6592393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 6602393Syz155240 /* nextheader(I) - stores next header value */ 6612393Syz155240 /* */ 6622393Syz155240 /* IPv6 Only */ 6632393Syz155240 /* This is function checks pending destination options extension header */ 6642393Syz155240 /* ------------------------------------------------------------------------ */ 6652393Syz155240 static INLINE int frpr_dstopts6(fin) 6662393Syz155240 fr_info_t *fin; 6672393Syz155240 { 6682393Syz155240 return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS); 6692393Syz155240 } 6702393Syz155240 6712393Syz155240 6722393Syz155240 /* ------------------------------------------------------------------------ */ 6732393Syz155240 /* Function: frpr_icmp6 */ 6742393Syz155240 /* Returns: void */ 6752393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 6762393Syz155240 /* */ 6772393Syz155240 /* IPv6 Only */ 6782393Syz155240 /* This routine is mainly concerned with determining the minimum valid size */ 6792393Syz155240 /* for an ICMPv6 packet. */ 6802393Syz155240 /* ------------------------------------------------------------------------ */ 6812393Syz155240 static INLINE void frpr_icmp6(fin) 6822393Syz155240 fr_info_t *fin; 6832393Syz155240 { 6842393Syz155240 int minicmpsz = sizeof(struct icmp6_hdr); 6852393Syz155240 struct icmp6_hdr *icmp6; 6862393Syz155240 6872393Syz155240 if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) 6882393Syz155240 return; 6892393Syz155240 6902393Syz155240 if (fin->fin_dlen > 1) { 6912393Syz155240 icmp6 = fin->fin_dp; 6922393Syz155240 6932393Syz155240 fin->fin_data[0] = *(u_short *)icmp6; 6942393Syz155240 6952393Syz155240 switch (icmp6->icmp6_type) 6962393Syz155240 { 6972393Syz155240 case ICMP6_ECHO_REPLY : 6982393Syz155240 case ICMP6_ECHO_REQUEST : 6992393Syz155240 minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t); 7002393Syz155240 break; 7012393Syz155240 case ICMP6_DST_UNREACH : 7022393Syz155240 case ICMP6_PACKET_TOO_BIG : 7032393Syz155240 case ICMP6_TIME_EXCEEDED : 7042393Syz155240 case ICMP6_PARAM_PROB : 7052393Syz155240 if ((fin->fin_m != NULL) && 7062393Syz155240 (M_LEN(fin->fin_m) < fin->fin_plen)) { 7072393Syz155240 if (fr_coalesce(fin) != 1) 7082393Syz155240 return; 7092393Syz155240 } 7102393Syz155240 fin->fin_flx |= FI_ICMPERR; 7112393Syz155240 minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t); 7122393Syz155240 break; 7132393Syz155240 default : 7142393Syz155240 break; 7152393Syz155240 } 7162393Syz155240 } 7172393Syz155240 7182393Syz155240 frpr_short6(fin, minicmpsz); 7192393Syz155240 fin->fin_flen -= fin->fin_dlen - minicmpsz; 7202393Syz155240 } 7212393Syz155240 7222393Syz155240 7232393Syz155240 /* ------------------------------------------------------------------------ */ 7242393Syz155240 /* Function: frpr_udp6 */ 7252393Syz155240 /* Returns: void */ 7262393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 7272393Syz155240 /* */ 7282393Syz155240 /* IPv6 Only */ 7292393Syz155240 /* Analyse the packet for IPv6/UDP properties. */ 7302393Syz155240 /* Is not expected to be called for fragmented packets. */ 7312393Syz155240 /* ------------------------------------------------------------------------ */ 7322393Syz155240 static INLINE void frpr_udp6(fin) 7332393Syz155240 fr_info_t *fin; 7342393Syz155240 { 7352393Syz155240 7362393Syz155240 fr_checkv6sum(fin); 7372393Syz155240 7382393Syz155240 frpr_short6(fin, sizeof(struct udphdr)); 7392393Syz155240 if (frpr_pullup(fin, sizeof(struct udphdr)) == -1) 7402393Syz155240 return; 7412393Syz155240 7422393Syz155240 fin->fin_flen -= fin->fin_dlen - sizeof(struct udphdr); 7432393Syz155240 7442393Syz155240 frpr_udpcommon(fin); 7452393Syz155240 } 7462393Syz155240 7472393Syz155240 7482393Syz155240 /* ------------------------------------------------------------------------ */ 7492393Syz155240 /* Function: frpr_tcp6 */ 7502393Syz155240 /* Returns: void */ 7512393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 7522393Syz155240 /* */ 7532393Syz155240 /* IPv6 Only */ 7542393Syz155240 /* Analyse the packet for IPv6/TCP properties. */ 7552393Syz155240 /* Is not expected to be called for fragmented packets. */ 7562393Syz155240 /* ------------------------------------------------------------------------ */ 7572393Syz155240 static INLINE void frpr_tcp6(fin) 7582393Syz155240 fr_info_t *fin; 7592393Syz155240 { 7602393Syz155240 7612393Syz155240 fr_checkv6sum(fin); 7622393Syz155240 7632393Syz155240 frpr_short6(fin, sizeof(struct tcphdr)); 7642393Syz155240 if (frpr_pullup(fin, sizeof(struct tcphdr)) == -1) 7652393Syz155240 return; 7662393Syz155240 7672393Syz155240 fin->fin_flen -= fin->fin_dlen - sizeof(struct tcphdr); 7682393Syz155240 7692393Syz155240 frpr_tcpcommon(fin); 7702393Syz155240 } 7712393Syz155240 7722393Syz155240 7732393Syz155240 /* ------------------------------------------------------------------------ */ 7742393Syz155240 /* Function: frpr_esp6 */ 7752393Syz155240 /* Returns: void */ 7762393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 7772393Syz155240 /* */ 7782393Syz155240 /* IPv6 Only */ 7792393Syz155240 /* Analyse the packet for ESP properties. */ 7802393Syz155240 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 7812393Syz155240 /* even though the newer ESP packets must also have a sequence number that */ 7822393Syz155240 /* is 32bits as well, it is not possible(?) to determine the version from a */ 7832393Syz155240 /* simple packet header. */ 7842393Syz155240 /* ------------------------------------------------------------------------ */ 7852393Syz155240 static INLINE void frpr_esp6(fin) 7862393Syz155240 fr_info_t *fin; 7872393Syz155240 { 7882393Syz155240 int i; 7892393Syz155240 frpr_short6(fin, sizeof(grehdr_t)); 7902393Syz155240 7912393Syz155240 (void) frpr_pullup(fin, 8); 7922393Syz155240 7932393Syz155240 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 7942393Syz155240 if (ip6exthdr[i].ol_val == IPPROTO_ESP) { 7952393Syz155240 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 7962393Syz155240 break; 7972393Syz155240 } 7982393Syz155240 } 7992393Syz155240 8002393Syz155240 8012393Syz155240 /* ------------------------------------------------------------------------ */ 8022393Syz155240 /* Function: frpr_ah6 */ 8032393Syz155240 /* Returns: void */ 8042393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 8052393Syz155240 /* */ 8062393Syz155240 /* IPv6 Only */ 8072393Syz155240 /* Analyse the packet for AH properties. */ 8082393Syz155240 /* The minimum length is taken to be the combination of all fields in the */ 8092393Syz155240 /* header being present and no authentication data (null algorithm used.) */ 8102393Syz155240 /* ------------------------------------------------------------------------ */ 8112393Syz155240 static INLINE int frpr_ah6(fin) 8122393Syz155240 fr_info_t *fin; 8132393Syz155240 { 8142393Syz155240 authhdr_t *ah; 8152393Syz155240 int i, shift; 8162393Syz155240 8172393Syz155240 frpr_short6(fin, 12); 8182393Syz155240 8192393Syz155240 if (frpr_pullup(fin, sizeof(*ah)) == -1) 8202393Syz155240 return IPPROTO_NONE; 8212393Syz155240 8222393Syz155240 for (i = 0; ip6exthdr[i].ol_bit != 0; i++) 8232393Syz155240 if (ip6exthdr[i].ol_val == IPPROTO_AH) { 8242393Syz155240 fin->fin_optmsk |= ip6exthdr[i].ol_bit; 8252393Syz155240 break; 8262393Syz155240 } 8272393Syz155240 8282393Syz155240 ah = (authhdr_t *)fin->fin_dp; 8292393Syz155240 8302393Syz155240 shift = (ah->ah_plen + 2) * 4; 8312393Syz155240 fin->fin_dlen -= shift; 8322393Syz155240 fin->fin_dp = (char*)fin->fin_dp + shift; 8332393Syz155240 8342393Syz155240 return ah->ah_next; 8352393Syz155240 } 8362393Syz155240 8372393Syz155240 8382393Syz155240 /* ------------------------------------------------------------------------ */ 8392393Syz155240 /* Function: frpr_gre6 */ 8402393Syz155240 /* Returns: void */ 8412393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 8422393Syz155240 /* */ 8432393Syz155240 /* Analyse the packet for GRE properties. */ 8442393Syz155240 /* ------------------------------------------------------------------------ */ 8452393Syz155240 static INLINE void frpr_gre6(fin) 8462393Syz155240 fr_info_t *fin; 8472393Syz155240 { 8482393Syz155240 grehdr_t *gre; 8492393Syz155240 8502393Syz155240 frpr_short6(fin, sizeof(grehdr_t)); 8512393Syz155240 8522393Syz155240 if (frpr_pullup(fin, sizeof(grehdr_t)) == -1) 8532393Syz155240 return; 8542393Syz155240 8552393Syz155240 gre = fin->fin_dp; 8562393Syz155240 if (GRE_REV(gre->gr_flags) == 1) 8572393Syz155240 fin->fin_data[0] = gre->gr_call; 8582393Syz155240 } 8592393Syz155240 #endif /* USE_INET6 */ 8602393Syz155240 8612393Syz155240 8622393Syz155240 /* ------------------------------------------------------------------------ */ 8632393Syz155240 /* Function: frpr_pullup */ 8642393Syz155240 /* Returns: int - 0 == pullup succeeded, -1 == failure */ 8652393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 8662393Syz155240 /* plen(I) - length (excluding L3 header) to pullup */ 8672393Syz155240 /* */ 8682393Syz155240 /* Short inline function to cut down on code duplication to perform a call */ 8692393Syz155240 /* to fr_pullup to ensure there is the required amount of data, */ 8702393Syz155240 /* consecutively in the packet buffer. */ 8712393Syz155240 /* ------------------------------------------------------------------------ */ 8722393Syz155240 static INLINE int frpr_pullup(fin, plen) 8732393Syz155240 fr_info_t *fin; 8742393Syz155240 int plen; 8752393Syz155240 { 8762393Syz155240 #if defined(_KERNEL) 8772393Syz155240 if (fin->fin_m != NULL) { 8782393Syz155240 if (fin->fin_dp != NULL) 8792393Syz155240 plen += (char *)fin->fin_dp - 8802393Syz155240 ((char *)fin->fin_ip + fin->fin_hlen); 8812393Syz155240 plen += ((char *)fin->fin_ip - MTOD(fin->fin_m, char *)) + 8822393Syz155240 fin->fin_hlen; 8832393Syz155240 if (M_LEN(fin->fin_m) < plen) { 8842393Syz155240 if (fr_pullup(fin->fin_m, fin, plen) == NULL) 8852393Syz155240 return -1; 8862393Syz155240 } 8872393Syz155240 } 8882393Syz155240 #endif 8892393Syz155240 return 0; 8902393Syz155240 } 8912393Syz155240 8922393Syz155240 8932393Syz155240 /* ------------------------------------------------------------------------ */ 8942393Syz155240 /* Function: frpr_short */ 8952393Syz155240 /* Returns: void */ 8962393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 8972393Syz155240 /* xmin(I) - minimum header size */ 8982393Syz155240 /* */ 8992393Syz155240 /* Check if a packet is "short" as defined by xmin. The rule we are */ 9002393Syz155240 /* applying here is that the packet must not be fragmented within the layer */ 9012393Syz155240 /* 4 header. That is, it must not be a fragment that has its offset set to */ 9022393Syz155240 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the */ 9032393Syz155240 /* entire layer 4 header must be present (min). */ 9042393Syz155240 /* ------------------------------------------------------------------------ */ 9052393Syz155240 static INLINE void frpr_short(fin, xmin) 9062393Syz155240 fr_info_t *fin; 9072393Syz155240 int xmin; 9082393Syz155240 { 9092393Syz155240 9102393Syz155240 if (fin->fin_off == 0) { 9112393Syz155240 if (fin->fin_dlen < xmin) 9122393Syz155240 fin->fin_flx |= FI_SHORT; 9132393Syz155240 } else if (fin->fin_off < xmin) { 9142393Syz155240 fin->fin_flx |= FI_SHORT; 9152393Syz155240 } 9162393Syz155240 } 9172393Syz155240 9182393Syz155240 9192393Syz155240 /* ------------------------------------------------------------------------ */ 9202393Syz155240 /* Function: frpr_icmp */ 9212393Syz155240 /* Returns: void */ 9222393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 9232393Syz155240 /* */ 9242393Syz155240 /* IPv4 Only */ 9252393Syz155240 /* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */ 9262393Syz155240 /* except extrememly bad packets, both type and code will be present. */ 9272393Syz155240 /* The expected minimum size of an ICMP packet is very much dependent on */ 9282393Syz155240 /* the type of it. */ 9292393Syz155240 /* */ 9302393Syz155240 /* XXX - other ICMP sanity checks? */ 9312393Syz155240 /* ------------------------------------------------------------------------ */ 9322393Syz155240 static INLINE void frpr_icmp(fin) 9332393Syz155240 fr_info_t *fin; 9342393Syz155240 { 9352393Syz155240 int minicmpsz = sizeof(struct icmp); 9362393Syz155240 icmphdr_t *icmp; 9372393Syz155240 ip_t *oip; 9383448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 9392393Syz155240 9402393Syz155240 if (fin->fin_off != 0) { 9412393Syz155240 frpr_short(fin, ICMPERR_ICMPHLEN); 9422393Syz155240 return; 9432393Syz155240 } 9442393Syz155240 9452393Syz155240 if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1) 9462393Syz155240 return; 9472393Syz155240 9482393Syz155240 fr_checkv4sum(fin); 9492393Syz155240 9502393Syz155240 if (fin->fin_dlen > 1) { 9512393Syz155240 icmp = fin->fin_dp; 9522393Syz155240 9532393Syz155240 fin->fin_data[0] = *(u_short *)icmp; 9542393Syz155240 9552393Syz155240 switch (icmp->icmp_type) 9562393Syz155240 { 9572393Syz155240 case ICMP_ECHOREPLY : 9582393Syz155240 case ICMP_ECHO : 9592393Syz155240 /* Router discovery messaes - RFC 1256 */ 9602393Syz155240 case ICMP_ROUTERADVERT : 9612393Syz155240 case ICMP_ROUTERSOLICIT : 9622393Syz155240 minicmpsz = ICMP_MINLEN; 9632393Syz155240 break; 9642393Syz155240 /* 9652393Syz155240 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 9662393Syz155240 * 3 * timestamp(3 * 4) 9672393Syz155240 */ 9682393Syz155240 case ICMP_TSTAMP : 9692393Syz155240 case ICMP_TSTAMPREPLY : 9702393Syz155240 minicmpsz = 20; 9712393Syz155240 break; 9722393Syz155240 /* 9732393Syz155240 * type(1) + code(1) + cksum(2) + id(2) seq(2) + 9742393Syz155240 * mask(4) 9752393Syz155240 */ 9762393Syz155240 case ICMP_MASKREQ : 9772393Syz155240 case ICMP_MASKREPLY : 9782393Syz155240 minicmpsz = 12; 9792393Syz155240 break; 9802393Syz155240 /* 9812393Syz155240 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+) 9822393Syz155240 */ 9832393Syz155240 case ICMP_UNREACH : 9842393Syz155240 if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) { 9853448Sdh155122 if (icmp->icmp_nextmtu < ifs->ifs_fr_icmpminfragmtu) 9862393Syz155240 fin->fin_flx |= FI_BAD; 9872393Syz155240 } 9882393Syz155240 /* FALLTHRU */ 9892393Syz155240 case ICMP_SOURCEQUENCH : 9902393Syz155240 case ICMP_REDIRECT : 9912393Syz155240 case ICMP_TIMXCEED : 9922393Syz155240 case ICMP_PARAMPROB : 9932393Syz155240 fin->fin_flx |= FI_ICMPERR; 9942393Syz155240 if (fr_coalesce(fin) != 1) 9952393Syz155240 return; 9962393Syz155240 /* 9972393Syz155240 * ICMP error packets should not be generated for IP 9982393Syz155240 * packets that are a fragment that isn't the first 9992393Syz155240 * fragment. 10002393Syz155240 */ 10012393Syz155240 oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN); 10022393Syz155240 if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) 10032393Syz155240 fin->fin_flx |= FI_BAD; 10042393Syz155240 break; 10052393Syz155240 default : 10062393Syz155240 break; 10072393Syz155240 } 10082393Syz155240 10092393Syz155240 if (fin->fin_dlen >= 6) /* ID field */ 10102393Syz155240 fin->fin_data[1] = icmp->icmp_id; 10112393Syz155240 } 10122393Syz155240 10132393Syz155240 frpr_short(fin, minicmpsz); 10142393Syz155240 } 10152393Syz155240 10162393Syz155240 10172393Syz155240 /* ------------------------------------------------------------------------ */ 10182393Syz155240 /* Function: frpr_tcpcommon */ 10192393Syz155240 /* Returns: void */ 10202393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 10212393Syz155240 /* */ 10222393Syz155240 /* TCP header sanity checking. Look for bad combinations of TCP flags, */ 10232393Syz155240 /* and make some checks with how they interact with other fields. */ 10242393Syz155240 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */ 10252393Syz155240 /* valid and mark the packet as bad if not. */ 10262393Syz155240 /* ------------------------------------------------------------------------ */ 10272393Syz155240 static INLINE void frpr_tcpcommon(fin) 10282393Syz155240 fr_info_t *fin; 10292393Syz155240 { 10302393Syz155240 int flags, tlen; 10312393Syz155240 tcphdr_t *tcp; 10322393Syz155240 10332393Syz155240 fin->fin_flx |= FI_TCPUDP; 10342393Syz155240 if (fin->fin_off != 0) 10352393Syz155240 return; 10362393Syz155240 10372393Syz155240 if (frpr_pullup(fin, sizeof(*tcp)) == -1) 10382393Syz155240 return; 10392393Syz155240 tcp = fin->fin_dp; 10402393Syz155240 10412393Syz155240 if (fin->fin_dlen > 3) { 10422393Syz155240 fin->fin_sport = ntohs(tcp->th_sport); 10432393Syz155240 fin->fin_dport = ntohs(tcp->th_dport); 10442393Syz155240 } 10452393Syz155240 10462393Syz155240 if ((fin->fin_flx & FI_SHORT) != 0) 10472393Syz155240 return; 10482393Syz155240 10492393Syz155240 /* 10502393Syz155240 * Use of the TCP data offset *must* result in a value that is at 10512393Syz155240 * least the same size as the TCP header. 10522393Syz155240 */ 10532393Syz155240 tlen = TCP_OFF(tcp) << 2; 10542393Syz155240 if (tlen < sizeof(tcphdr_t)) { 10552393Syz155240 fin->fin_flx |= FI_BAD; 10562393Syz155240 return; 10572393Syz155240 } 10582393Syz155240 10592393Syz155240 flags = tcp->th_flags; 10602393Syz155240 fin->fin_tcpf = tcp->th_flags; 10612393Syz155240 10622393Syz155240 /* 10632393Syz155240 * If the urgent flag is set, then the urgent pointer must 10642393Syz155240 * also be set and vice versa. Good TCP packets do not have 10652393Syz155240 * just one of these set. 10662393Syz155240 */ 10672393Syz155240 if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) { 10682393Syz155240 fin->fin_flx |= FI_BAD; 10692393Syz155240 } else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) { 10702393Syz155240 /* Ignore this case, it shows up in "real" traffic with */ 10712393Syz155240 /* bogus values in the urgent pointer field. */ 10722393Syz155240 flags = flags; /* LINT */ 10732393Syz155240 } else if (((flags & (TH_SYN|TH_FIN)) != 0) && 10742393Syz155240 ((flags & (TH_RST|TH_ACK)) == TH_RST)) { 10752393Syz155240 /* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */ 10762393Syz155240 fin->fin_flx |= FI_BAD; 10772393Syz155240 } else if (!(flags & TH_ACK)) { 10782393Syz155240 /* 10792393Syz155240 * If the ack bit isn't set, then either the SYN or 10802393Syz155240 * RST bit must be set. If the SYN bit is set, then 10812393Syz155240 * we expect the ACK field to be 0. If the ACK is 10822393Syz155240 * not set and if URG, PSH or FIN are set, consdier 10832393Syz155240 * that to indicate a bad TCP packet. 10842393Syz155240 */ 10852393Syz155240 if ((flags == TH_SYN) && (tcp->th_ack != 0)) { 10862393Syz155240 /* 10872393Syz155240 * Cisco PIX sets the ACK field to a random value. 10882393Syz155240 * In light of this, do not set FI_BAD until a patch 10892393Syz155240 * is available from Cisco to ensure that 10902393Syz155240 * interoperability between existing systems is 10912393Syz155240 * achieved. 10922393Syz155240 */ 10932393Syz155240 /*fin->fin_flx |= FI_BAD*/; 10942393Syz155240 flags = flags; /* LINT */ 10952393Syz155240 } else if (!(flags & (TH_RST|TH_SYN))) { 10962393Syz155240 fin->fin_flx |= FI_BAD; 10972393Syz155240 } else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) { 10982393Syz155240 fin->fin_flx |= FI_BAD; 10992393Syz155240 } 11002393Syz155240 } 11012393Syz155240 11022393Syz155240 /* 11032393Syz155240 * At this point, it's not exactly clear what is to be gained by 11042393Syz155240 * marking up which TCP options are and are not present. The one we 11052393Syz155240 * are most interested in is the TCP window scale. This is only in 11062393Syz155240 * a SYN packet [RFC1323] so we don't need this here...? 11072393Syz155240 * Now if we were to analyse the header for passive fingerprinting, 11082393Syz155240 * then that might add some weight to adding this... 11092393Syz155240 */ 11102393Syz155240 if (tlen == sizeof(tcphdr_t)) 11112393Syz155240 return; 11122393Syz155240 11132393Syz155240 if (frpr_pullup(fin, tlen) == -1) 11142393Syz155240 return; 11152393Syz155240 11162393Syz155240 #if 0 11172393Syz155240 ip = fin->fin_ip; 11182393Syz155240 s = (u_char *)(tcp + 1); 11192393Syz155240 off = IP_HL(ip) << 2; 11202393Syz155240 # ifdef _KERNEL 11212393Syz155240 if (fin->fin_mp != NULL) { 11222393Syz155240 mb_t *m = *fin->fin_mp; 11232393Syz155240 11242393Syz155240 if (off + tlen > M_LEN(m)) 11252393Syz155240 return; 11262393Syz155240 } 11272393Syz155240 # endif 11282393Syz155240 for (tlen -= (int)sizeof(*tcp); tlen > 0; ) { 11292393Syz155240 opt = *s; 11302393Syz155240 if (opt == '\0') 11312393Syz155240 break; 11322393Syz155240 else if (opt == TCPOPT_NOP) 11332393Syz155240 ol = 1; 11342393Syz155240 else { 11352393Syz155240 if (tlen < 2) 11362393Syz155240 break; 11372393Syz155240 ol = (int)*(s + 1); 11382393Syz155240 if (ol < 2 || ol > tlen) 11392393Syz155240 break; 11402393Syz155240 } 11412393Syz155240 11422393Syz155240 for (i = 9, mv = 4; mv >= 0; ) { 11432393Syz155240 op = ipopts + i; 11442393Syz155240 if (opt == (u_char)op->ol_val) { 11452393Syz155240 optmsk |= op->ol_bit; 11462393Syz155240 break; 11472393Syz155240 } 11482393Syz155240 } 11492393Syz155240 tlen -= ol; 11502393Syz155240 s += ol; 11512393Syz155240 } 11522393Syz155240 #endif /* 0 */ 11532393Syz155240 } 11542393Syz155240 11552393Syz155240 11562393Syz155240 11572393Syz155240 /* ------------------------------------------------------------------------ */ 11582393Syz155240 /* Function: frpr_udpcommon */ 11592393Syz155240 /* Returns: void */ 11602393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 11612393Syz155240 /* */ 11622393Syz155240 /* Extract the UDP source and destination ports, if present. If compiled */ 11632393Syz155240 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */ 11642393Syz155240 /* ------------------------------------------------------------------------ */ 11652393Syz155240 static INLINE void frpr_udpcommon(fin) 11662393Syz155240 fr_info_t *fin; 11672393Syz155240 { 11682393Syz155240 udphdr_t *udp; 11692393Syz155240 11702393Syz155240 fin->fin_flx |= FI_TCPUDP; 11712393Syz155240 11722393Syz155240 if (!fin->fin_off && (fin->fin_dlen > 3)) { 11732393Syz155240 if (frpr_pullup(fin, sizeof(*udp)) == -1) { 11742393Syz155240 fin->fin_flx |= FI_SHORT; 11752393Syz155240 return; 11762393Syz155240 } 11772393Syz155240 11782393Syz155240 udp = fin->fin_dp; 11792393Syz155240 11802393Syz155240 fin->fin_sport = ntohs(udp->uh_sport); 11812393Syz155240 fin->fin_dport = ntohs(udp->uh_dport); 11822393Syz155240 } 11832393Syz155240 } 11842393Syz155240 11852393Syz155240 11862393Syz155240 /* ------------------------------------------------------------------------ */ 11872393Syz155240 /* Function: frpr_tcp */ 11882393Syz155240 /* Returns: void */ 11892393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 11902393Syz155240 /* */ 11912393Syz155240 /* IPv4 Only */ 11922393Syz155240 /* Analyse the packet for IPv4/TCP properties. */ 11932393Syz155240 /* ------------------------------------------------------------------------ */ 11942393Syz155240 static INLINE void frpr_tcp(fin) 11952393Syz155240 fr_info_t *fin; 11962393Syz155240 { 11972393Syz155240 11982393Syz155240 fr_checkv4sum(fin); 11992393Syz155240 12002393Syz155240 frpr_short(fin, sizeof(tcphdr_t)); 12012393Syz155240 12022393Syz155240 frpr_tcpcommon(fin); 12032393Syz155240 } 12042393Syz155240 12052393Syz155240 12062393Syz155240 /* ------------------------------------------------------------------------ */ 12072393Syz155240 /* Function: frpr_udp */ 12082393Syz155240 /* Returns: void */ 12092393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 12102393Syz155240 /* */ 12112393Syz155240 /* IPv4 Only */ 12122393Syz155240 /* Analyse the packet for IPv4/UDP properties. */ 12132393Syz155240 /* ------------------------------------------------------------------------ */ 12142393Syz155240 static INLINE void frpr_udp(fin) 12152393Syz155240 fr_info_t *fin; 12162393Syz155240 { 12172393Syz155240 12182393Syz155240 fr_checkv4sum(fin); 12192393Syz155240 12202393Syz155240 frpr_short(fin, sizeof(udphdr_t)); 12212393Syz155240 12222393Syz155240 frpr_udpcommon(fin); 12232393Syz155240 } 12242393Syz155240 12252393Syz155240 12262393Syz155240 /* ------------------------------------------------------------------------ */ 12272393Syz155240 /* Function: frpr_esp */ 12282393Syz155240 /* Returns: void */ 12292393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 12302393Syz155240 /* */ 12312393Syz155240 /* Analyse the packet for ESP properties. */ 12322393Syz155240 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */ 12332393Syz155240 /* even though the newer ESP packets must also have a sequence number that */ 12342393Syz155240 /* is 32bits as well, it is not possible(?) to determine the version from a */ 12352393Syz155240 /* simple packet header. */ 12362393Syz155240 /* ------------------------------------------------------------------------ */ 12372393Syz155240 static INLINE void frpr_esp(fin) 12382393Syz155240 fr_info_t *fin; 12392393Syz155240 { 12402393Syz155240 if ((fin->fin_off == 0) && (frpr_pullup(fin, 8) == -1)) 12412393Syz155240 return; 12422393Syz155240 12432393Syz155240 frpr_short(fin, 8); 12442393Syz155240 } 12452393Syz155240 12462393Syz155240 12472393Syz155240 /* ------------------------------------------------------------------------ */ 12482393Syz155240 /* Function: frpr_ah */ 12492393Syz155240 /* Returns: void */ 12502393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 12512393Syz155240 /* */ 12522393Syz155240 /* Analyse the packet for AH properties. */ 12532393Syz155240 /* The minimum length is taken to be the combination of all fields in the */ 12542393Syz155240 /* header being present and no authentication data (null algorithm used.) */ 12552393Syz155240 /* ------------------------------------------------------------------------ */ 12562393Syz155240 static INLINE void frpr_ah(fin) 12572393Syz155240 fr_info_t *fin; 12582393Syz155240 { 12592393Syz155240 authhdr_t *ah; 12602393Syz155240 int len; 12612393Syz155240 12622393Syz155240 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(*ah)) == -1)) 12632393Syz155240 return; 12642393Syz155240 12652393Syz155240 ah = (authhdr_t *)fin->fin_dp; 12662393Syz155240 12672393Syz155240 len = (ah->ah_plen + 2) << 2; 12682393Syz155240 frpr_short(fin, len); 12692393Syz155240 } 12702393Syz155240 12712393Syz155240 12722393Syz155240 /* ------------------------------------------------------------------------ */ 12732393Syz155240 /* Function: frpr_gre */ 12742393Syz155240 /* Returns: void */ 12752393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 12762393Syz155240 /* */ 12772393Syz155240 /* Analyse the packet for GRE properties. */ 12782393Syz155240 /* ------------------------------------------------------------------------ */ 12792393Syz155240 static INLINE void frpr_gre(fin) 12802393Syz155240 fr_info_t *fin; 12812393Syz155240 { 12822393Syz155240 grehdr_t *gre; 12832393Syz155240 12842393Syz155240 if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(grehdr_t)) == -1)) 12852393Syz155240 return; 12862393Syz155240 12872393Syz155240 frpr_short(fin, sizeof(grehdr_t)); 12882393Syz155240 12892393Syz155240 if (fin->fin_off == 0) { 12902393Syz155240 gre = fin->fin_dp; 12912393Syz155240 if (GRE_REV(gre->gr_flags) == 1) 12922393Syz155240 fin->fin_data[0] = gre->gr_call; 12932393Syz155240 } 12942393Syz155240 } 12952393Syz155240 12962393Syz155240 12972393Syz155240 /* ------------------------------------------------------------------------ */ 12982393Syz155240 /* Function: frpr_ipv4hdr */ 12992393Syz155240 /* Returns: void */ 13002393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 13012393Syz155240 /* */ 13022393Syz155240 /* IPv4 Only */ 13032393Syz155240 /* Analyze the IPv4 header and set fields in the fr_info_t structure. */ 13042393Syz155240 /* Check all options present and flag their presence if any exist. */ 13052393Syz155240 /* ------------------------------------------------------------------------ */ 13062393Syz155240 static INLINE void frpr_ipv4hdr(fin) 13072393Syz155240 fr_info_t *fin; 13082393Syz155240 { 13092393Syz155240 u_short optmsk = 0, secmsk = 0, auth = 0; 13102393Syz155240 int hlen, ol, mv, p, i; 13112393Syz155240 const struct optlist *op; 13122393Syz155240 u_char *s, opt; 13132393Syz155240 u_short off; 13142393Syz155240 fr_ip_t *fi; 13152393Syz155240 ip_t *ip; 13162393Syz155240 13172393Syz155240 fi = &fin->fin_fi; 13182393Syz155240 hlen = fin->fin_hlen; 13192393Syz155240 13202393Syz155240 ip = fin->fin_ip; 13212393Syz155240 p = ip->ip_p; 13222393Syz155240 fi->fi_p = p; 13232393Syz155240 fi->fi_tos = ip->ip_tos; 13242393Syz155240 fin->fin_id = ip->ip_id; 13252393Syz155240 off = ip->ip_off; 13262393Syz155240 13272393Syz155240 /* Get both TTL and protocol */ 13282393Syz155240 fi->fi_p = ip->ip_p; 13292393Syz155240 fi->fi_ttl = ip->ip_ttl; 13302393Syz155240 #if 0 13312393Syz155240 (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4)); 13322393Syz155240 #endif 13332393Syz155240 13342393Syz155240 /* Zero out bits not used in IPv6 address */ 13352393Syz155240 fi->fi_src.i6[1] = 0; 13362393Syz155240 fi->fi_src.i6[2] = 0; 13372393Syz155240 fi->fi_src.i6[3] = 0; 13382393Syz155240 fi->fi_dst.i6[1] = 0; 13392393Syz155240 fi->fi_dst.i6[2] = 0; 13402393Syz155240 fi->fi_dst.i6[3] = 0; 13412393Syz155240 13422393Syz155240 fi->fi_saddr = ip->ip_src.s_addr; 13432393Syz155240 fi->fi_daddr = ip->ip_dst.s_addr; 13442393Syz155240 13452393Syz155240 /* 13462393Syz155240 * set packet attribute flags based on the offset and 13472393Syz155240 * calculate the byte offset that it represents. 13482393Syz155240 */ 13492393Syz155240 off &= IP_MF|IP_OFFMASK; 13502393Syz155240 if (off != 0) { 13512393Syz155240 fi->fi_flx |= FI_FRAG; 13522393Syz155240 off &= IP_OFFMASK; 13532393Syz155240 if (off != 0) { 13542393Syz155240 fin->fin_flx |= FI_FRAGBODY; 13552393Syz155240 off <<= 3; 13562393Syz155240 if ((off + fin->fin_dlen > 65535) || 13572393Syz155240 (fin->fin_dlen == 0) || 13582393Syz155240 ((ip->ip_off & IP_MF) && (fin->fin_dlen & 7))) { 13592393Syz155240 /* 13602393Syz155240 * The length of the packet, starting at its 13612393Syz155240 * offset cannot exceed 65535 (0xffff) as the 13622393Syz155240 * length of an IP packet is only 16 bits. 13632393Syz155240 * 13642393Syz155240 * Any fragment that isn't the last fragment 13652393Syz155240 * must have a length greater than 0 and it 13662393Syz155240 * must be an even multiple of 8. 13672393Syz155240 */ 13682393Syz155240 fi->fi_flx |= FI_BAD; 13692393Syz155240 } 13702393Syz155240 } 13712393Syz155240 } 13722393Syz155240 fin->fin_off = off; 13732393Syz155240 13742393Syz155240 /* 13752393Syz155240 * Call per-protocol setup and checking 13762393Syz155240 */ 13772393Syz155240 switch (p) 13782393Syz155240 { 13792393Syz155240 case IPPROTO_UDP : 13802393Syz155240 frpr_udp(fin); 13812393Syz155240 break; 13822393Syz155240 case IPPROTO_TCP : 13832393Syz155240 frpr_tcp(fin); 13842393Syz155240 break; 13852393Syz155240 case IPPROTO_ICMP : 13862393Syz155240 frpr_icmp(fin); 13872393Syz155240 break; 13882393Syz155240 case IPPROTO_AH : 13892393Syz155240 frpr_ah(fin); 13902393Syz155240 break; 13912393Syz155240 case IPPROTO_ESP : 13922393Syz155240 frpr_esp(fin); 13932393Syz155240 break; 13942393Syz155240 case IPPROTO_GRE : 13952393Syz155240 frpr_gre(fin); 13962393Syz155240 break; 13972393Syz155240 } 13982393Syz155240 13992393Syz155240 ip = fin->fin_ip; 14002393Syz155240 if (ip == NULL) 14012393Syz155240 return; 14022393Syz155240 14032393Syz155240 /* 14042393Syz155240 * If it is a standard IP header (no options), set the flag fields 14052393Syz155240 * which relate to options to 0. 14062393Syz155240 */ 14072393Syz155240 if (hlen == sizeof(*ip)) { 14082393Syz155240 fi->fi_optmsk = 0; 14092393Syz155240 fi->fi_secmsk = 0; 14102393Syz155240 fi->fi_auth = 0; 14112393Syz155240 return; 14122393Syz155240 } 14132393Syz155240 14142393Syz155240 /* 14152393Syz155240 * So the IP header has some IP options attached. Walk the entire 14162393Syz155240 * list of options present with this packet and set flags to indicate 14172393Syz155240 * which ones are here and which ones are not. For the somewhat out 14182393Syz155240 * of date and obscure security classification options, set a flag to 14192393Syz155240 * represent which classification is present. 14202393Syz155240 */ 14212393Syz155240 fi->fi_flx |= FI_OPTIONS; 14222393Syz155240 14232393Syz155240 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) { 14242393Syz155240 opt = *s; 14252393Syz155240 if (opt == '\0') 14262393Syz155240 break; 14272393Syz155240 else if (opt == IPOPT_NOP) 14282393Syz155240 ol = 1; 14292393Syz155240 else { 14302393Syz155240 if (hlen < 2) 14312393Syz155240 break; 14322393Syz155240 ol = (int)*(s + 1); 14332393Syz155240 if (ol < 2 || ol > hlen) 14342393Syz155240 break; 14352393Syz155240 } 14362393Syz155240 for (i = 9, mv = 4; mv >= 0; ) { 14372393Syz155240 op = ipopts + i; 14382393Syz155240 if ((opt == (u_char)op->ol_val) && (ol > 4)) { 14392393Syz155240 optmsk |= op->ol_bit; 14402393Syz155240 if (opt == IPOPT_SECURITY) { 14412393Syz155240 const struct optlist *sp; 14422393Syz155240 u_char sec; 14432393Syz155240 int j, m; 14442393Syz155240 14452393Syz155240 sec = *(s + 2); /* classification */ 14462393Syz155240 for (j = 3, m = 2; m >= 0; ) { 14472393Syz155240 sp = secopt + j; 14482393Syz155240 if (sec == sp->ol_val) { 14492393Syz155240 secmsk |= sp->ol_bit; 14502393Syz155240 auth = *(s + 3); 14512393Syz155240 auth *= 256; 14522393Syz155240 auth += *(s + 4); 14532393Syz155240 break; 14542393Syz155240 } 14552393Syz155240 if (sec < sp->ol_val) 14562393Syz155240 j -= m; 14572393Syz155240 else 14582393Syz155240 j += m; 14592393Syz155240 m--; 14602393Syz155240 } 14612393Syz155240 } 14622393Syz155240 break; 14632393Syz155240 } 14642393Syz155240 if (opt < op->ol_val) 14652393Syz155240 i -= mv; 14662393Syz155240 else 14672393Syz155240 i += mv; 14682393Syz155240 mv--; 14692393Syz155240 } 14702393Syz155240 hlen -= ol; 14712393Syz155240 s += ol; 14722393Syz155240 } 14732393Syz155240 14742393Syz155240 /* 14752393Syz155240 * 14762393Syz155240 */ 14772393Syz155240 if (auth && !(auth & 0x0100)) 14782393Syz155240 auth &= 0xff00; 14792393Syz155240 fi->fi_optmsk = optmsk; 14802393Syz155240 fi->fi_secmsk = secmsk; 14812393Syz155240 fi->fi_auth = auth; 14822393Syz155240 } 14832393Syz155240 14842393Syz155240 14852393Syz155240 /* ------------------------------------------------------------------------ */ 14862393Syz155240 /* Function: fr_makefrip */ 14872393Syz155240 /* Returns: int - 1 == hdr checking error, 0 == OK */ 14882393Syz155240 /* Parameters: hlen(I) - length of IP packet header */ 14892393Syz155240 /* ip(I) - pointer to the IP header */ 14902393Syz155240 /* fin(IO) - pointer to packet information */ 14912393Syz155240 /* */ 14922393Syz155240 /* Compact the IP header into a structure which contains just the info. */ 14932393Syz155240 /* which is useful for comparing IP headers with and store this information */ 14942393Syz155240 /* in the fr_info_t structure pointer to by fin. At present, it is assumed */ 14952393Syz155240 /* this function will be called with either an IPv4 or IPv6 packet. */ 14962393Syz155240 /* ------------------------------------------------------------------------ */ 14972393Syz155240 int fr_makefrip(hlen, ip, fin) 14982393Syz155240 int hlen; 14992393Syz155240 ip_t *ip; 15002393Syz155240 fr_info_t *fin; 15012393Syz155240 { 15022393Syz155240 int v; 15032393Syz155240 15042393Syz155240 fin->fin_nat = NULL; 15052393Syz155240 fin->fin_state = NULL; 15062393Syz155240 fin->fin_depth = 0; 15072393Syz155240 fin->fin_hlen = (u_short)hlen; 15082393Syz155240 fin->fin_ip = ip; 15092393Syz155240 fin->fin_rule = 0xffffffff; 15102393Syz155240 fin->fin_group[0] = -1; 15112393Syz155240 fin->fin_group[1] = '\0'; 15122393Syz155240 fin->fin_dlen = fin->fin_plen - hlen; 15132393Syz155240 fin->fin_dp = (char *)ip + hlen; 15142393Syz155240 15152393Syz155240 v = fin->fin_v; 15162393Syz155240 if (v == 4) 15172393Syz155240 frpr_ipv4hdr(fin); 15182393Syz155240 #ifdef USE_INET6 15192393Syz155240 else if (v == 6) { 15202393Syz155240 if (frpr_ipv6hdr(fin) == -1) 15212393Syz155240 return -1; 15222393Syz155240 } 15232393Syz155240 #endif 15242393Syz155240 if (fin->fin_ip == NULL) 15252393Syz155240 return -1; 15262393Syz155240 return 0; 15272393Syz155240 } 15282393Syz155240 15292393Syz155240 15302393Syz155240 /* ------------------------------------------------------------------------ */ 15312393Syz155240 /* Function: fr_portcheck */ 15322393Syz155240 /* Returns: int - 1 == port matched, 0 == port match failed */ 15332393Syz155240 /* Parameters: frp(I) - pointer to port check `expression' */ 15342393Syz155240 /* pop(I) - pointer to port number to evaluate */ 15352393Syz155240 /* */ 15362393Syz155240 /* Perform a comparison of a port number against some other(s), using a */ 15372393Syz155240 /* structure with compare information stored in it. */ 15382393Syz155240 /* ------------------------------------------------------------------------ */ 15392393Syz155240 static INLINE int fr_portcheck(frp, pop) 15402393Syz155240 frpcmp_t *frp; 15412393Syz155240 u_short *pop; 15422393Syz155240 { 15432393Syz155240 u_short tup, po; 15442393Syz155240 int err = 1; 15452393Syz155240 15462393Syz155240 tup = *pop; 15472393Syz155240 po = frp->frp_port; 15482393Syz155240 15492393Syz155240 /* 15502393Syz155240 * Do opposite test to that required and continue if that succeeds. 15512393Syz155240 */ 15522393Syz155240 switch (frp->frp_cmp) 15532393Syz155240 { 15542393Syz155240 case FR_EQUAL : 15552393Syz155240 if (tup != po) /* EQUAL */ 15562393Syz155240 err = 0; 15572393Syz155240 break; 15582393Syz155240 case FR_NEQUAL : 15592393Syz155240 if (tup == po) /* NOTEQUAL */ 15602393Syz155240 err = 0; 15612393Syz155240 break; 15622393Syz155240 case FR_LESST : 15632393Syz155240 if (tup >= po) /* LESSTHAN */ 15642393Syz155240 err = 0; 15652393Syz155240 break; 15662393Syz155240 case FR_GREATERT : 15672393Syz155240 if (tup <= po) /* GREATERTHAN */ 15682393Syz155240 err = 0; 15692393Syz155240 break; 15702393Syz155240 case FR_LESSTE : 15712393Syz155240 if (tup > po) /* LT or EQ */ 15722393Syz155240 err = 0; 15732393Syz155240 break; 15742393Syz155240 case FR_GREATERTE : 15752393Syz155240 if (tup < po) /* GT or EQ */ 15762393Syz155240 err = 0; 15772393Syz155240 break; 15782393Syz155240 case FR_OUTRANGE : 15792393Syz155240 if (tup >= po && tup <= frp->frp_top) /* Out of range */ 15802393Syz155240 err = 0; 15812393Syz155240 break; 15822393Syz155240 case FR_INRANGE : 15832393Syz155240 if (tup <= po || tup >= frp->frp_top) /* In range */ 15842393Syz155240 err = 0; 15852393Syz155240 break; 15862393Syz155240 case FR_INCRANGE : 15872393Syz155240 if (tup < po || tup > frp->frp_top) /* Inclusive range */ 15882393Syz155240 err = 0; 15892393Syz155240 break; 15902393Syz155240 default : 15912393Syz155240 break; 15922393Syz155240 } 15932393Syz155240 return err; 15942393Syz155240 } 15952393Syz155240 15962393Syz155240 15972393Syz155240 /* ------------------------------------------------------------------------ */ 15982393Syz155240 /* Function: fr_tcpudpchk */ 15992393Syz155240 /* Returns: int - 1 == protocol matched, 0 == check failed */ 16002393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 16012393Syz155240 /* ft(I) - pointer to structure with comparison data */ 16022393Syz155240 /* */ 16032393Syz155240 /* Compares the current pcket (assuming it is TCP/UDP) information with a */ 16042393Syz155240 /* structure containing information that we want to match against. */ 16052393Syz155240 /* ------------------------------------------------------------------------ */ 16062393Syz155240 int fr_tcpudpchk(fin, ft) 16072393Syz155240 fr_info_t *fin; 16082393Syz155240 frtuc_t *ft; 16092393Syz155240 { 16102393Syz155240 int err = 1; 16112393Syz155240 16122393Syz155240 /* 16132393Syz155240 * Both ports should *always* be in the first fragment. 16142393Syz155240 * So far, I cannot find any cases where they can not be. 16152393Syz155240 * 16162393Syz155240 * compare destination ports 16172393Syz155240 */ 16182393Syz155240 if (ft->ftu_dcmp) 16192393Syz155240 err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport); 16202393Syz155240 16212393Syz155240 /* 16222393Syz155240 * compare source ports 16232393Syz155240 */ 16242393Syz155240 if (err && ft->ftu_scmp) 16252393Syz155240 err = fr_portcheck(&ft->ftu_src, &fin->fin_sport); 16262393Syz155240 16272393Syz155240 /* 16282393Syz155240 * If we don't have all the TCP/UDP header, then how can we 16292393Syz155240 * expect to do any sort of match on it ? If we were looking for 16302393Syz155240 * TCP flags, then NO match. If not, then match (which should 16312393Syz155240 * satisfy the "short" class too). 16322393Syz155240 */ 16332393Syz155240 if (err && (fin->fin_p == IPPROTO_TCP)) { 16342393Syz155240 if (fin->fin_flx & FI_SHORT) 16352393Syz155240 return !(ft->ftu_tcpf | ft->ftu_tcpfm); 16362393Syz155240 /* 16372393Syz155240 * Match the flags ? If not, abort this match. 16382393Syz155240 */ 16392393Syz155240 if (ft->ftu_tcpfm && 16402393Syz155240 ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) { 16412393Syz155240 FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf, 16422393Syz155240 ft->ftu_tcpfm, ft->ftu_tcpf)); 16432393Syz155240 err = 0; 16442393Syz155240 } 16452393Syz155240 } 16462393Syz155240 return err; 16472393Syz155240 } 16482393Syz155240 16492393Syz155240 16502393Syz155240 /* ------------------------------------------------------------------------ */ 16512393Syz155240 /* Function: fr_ipfcheck */ 16522393Syz155240 /* Returns: int - 0 == match, 1 == no match */ 16532393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 16542393Syz155240 /* fr(I) - pointer to filter rule */ 16552393Syz155240 /* portcmp(I) - flag indicating whether to attempt matching on */ 16562393Syz155240 /* TCP/UDP port data. */ 16572393Syz155240 /* */ 16582393Syz155240 /* Check to see if a packet matches an IPFilter rule. Checks of addresses, */ 16592393Syz155240 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */ 16602393Syz155240 /* this function. */ 16612393Syz155240 /* ------------------------------------------------------------------------ */ 16622393Syz155240 static INLINE int fr_ipfcheck(fin, fr, portcmp) 16632393Syz155240 fr_info_t *fin; 16642393Syz155240 frentry_t *fr; 16652393Syz155240 int portcmp; 16662393Syz155240 { 16672393Syz155240 u_32_t *ld, *lm, *lip; 16682393Syz155240 fripf_t *fri; 16692393Syz155240 fr_ip_t *fi; 16702393Syz155240 int i; 16713448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 16722393Syz155240 16732393Syz155240 fi = &fin->fin_fi; 16742393Syz155240 fri = fr->fr_ipf; 16752393Syz155240 lip = (u_32_t *)fi; 16762393Syz155240 lm = (u_32_t *)&fri->fri_mip; 16772393Syz155240 ld = (u_32_t *)&fri->fri_ip; 16782393Syz155240 16792393Syz155240 /* 16802393Syz155240 * first 32 bits to check coversion: 16812393Syz155240 * IP version, TOS, TTL, protocol 16822393Syz155240 */ 16832393Syz155240 i = ((*lip & *lm) != *ld); 16842393Syz155240 FR_DEBUG(("0. %#08x & %#08x != %#08x\n", 16852393Syz155240 *lip, *lm, *ld)); 16862393Syz155240 if (i) 16872393Syz155240 return 1; 16882393Syz155240 16892393Syz155240 /* 16902393Syz155240 * Next 32 bits is a constructed bitmask indicating which IP options 16912393Syz155240 * are present (if any) in this packet. 16922393Syz155240 */ 16932393Syz155240 lip++, lm++, ld++; 16942393Syz155240 i |= ((*lip & *lm) != *ld); 16952393Syz155240 FR_DEBUG(("1. %#08x & %#08x != %#08x\n", 16962393Syz155240 *lip, *lm, *ld)); 16972393Syz155240 if (i) 16982393Syz155240 return 1; 16992393Syz155240 17002393Syz155240 lip++, lm++, ld++; 17012393Syz155240 /* 17022393Syz155240 * Unrolled loops (4 each, for 32 bits) for address checks. 17032393Syz155240 */ 17042393Syz155240 /* 17052393Syz155240 * Check the source address. 17062393Syz155240 */ 17072393Syz155240 #ifdef IPFILTER_LOOKUP 17082393Syz155240 if (fr->fr_satype == FRI_LOOKUP) { 17093448Sdh155122 i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip, ifs); 17102393Syz155240 if (i == -1) 17112393Syz155240 return 1; 17122393Syz155240 lip += 3; 17132393Syz155240 lm += 3; 17142393Syz155240 ld += 3; 17152393Syz155240 } else { 17162393Syz155240 #endif 17172393Syz155240 i = ((*lip & *lm) != *ld); 17182393Syz155240 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n", 17192393Syz155240 *lip, *lm, *ld)); 17202393Syz155240 if (fi->fi_v == 6) { 17212393Syz155240 lip++, lm++, ld++; 17222393Syz155240 i |= ((*lip & *lm) != *ld); 17232393Syz155240 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n", 17242393Syz155240 *lip, *lm, *ld)); 17252393Syz155240 lip++, lm++, ld++; 17262393Syz155240 i |= ((*lip & *lm) != *ld); 17272393Syz155240 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n", 17282393Syz155240 *lip, *lm, *ld)); 17292393Syz155240 lip++, lm++, ld++; 17302393Syz155240 i |= ((*lip & *lm) != *ld); 17312393Syz155240 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n", 17322393Syz155240 *lip, *lm, *ld)); 17332393Syz155240 } else { 17342393Syz155240 lip += 3; 17352393Syz155240 lm += 3; 17362393Syz155240 ld += 3; 17372393Syz155240 } 17382393Syz155240 #ifdef IPFILTER_LOOKUP 17392393Syz155240 } 17402393Syz155240 #endif 17412393Syz155240 i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6; 17422393Syz155240 if (i) 17432393Syz155240 return 1; 17442393Syz155240 17452393Syz155240 /* 17462393Syz155240 * Check the destination address. 17472393Syz155240 */ 17482393Syz155240 lip++, lm++, ld++; 17492393Syz155240 #ifdef IPFILTER_LOOKUP 17502393Syz155240 if (fr->fr_datype == FRI_LOOKUP) { 17513448Sdh155122 i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip, ifs); 17522393Syz155240 if (i == -1) 17532393Syz155240 return 1; 17542393Syz155240 lip += 3; 17552393Syz155240 lm += 3; 17562393Syz155240 ld += 3; 17572393Syz155240 } else { 17582393Syz155240 #endif 17592393Syz155240 i = ((*lip & *lm) != *ld); 17602393Syz155240 FR_DEBUG(("3a. %#08x & %#08x != %#08x\n", 17612393Syz155240 *lip, *lm, *ld)); 17622393Syz155240 if (fi->fi_v == 6) { 17632393Syz155240 lip++, lm++, ld++; 17642393Syz155240 i |= ((*lip & *lm) != *ld); 17652393Syz155240 FR_DEBUG(("3b. %#08x & %#08x != %#08x\n", 17662393Syz155240 *lip, *lm, *ld)); 17672393Syz155240 lip++, lm++, ld++; 17682393Syz155240 i |= ((*lip & *lm) != *ld); 17692393Syz155240 FR_DEBUG(("3c. %#08x & %#08x != %#08x\n", 17702393Syz155240 *lip, *lm, *ld)); 17712393Syz155240 lip++, lm++, ld++; 17722393Syz155240 i |= ((*lip & *lm) != *ld); 17732393Syz155240 FR_DEBUG(("3d. %#08x & %#08x != %#08x\n", 17742393Syz155240 *lip, *lm, *ld)); 17752393Syz155240 } else { 17762393Syz155240 lip += 3; 17772393Syz155240 lm += 3; 17782393Syz155240 ld += 3; 17792393Syz155240 } 17802393Syz155240 #ifdef IPFILTER_LOOKUP 17812393Syz155240 } 17822393Syz155240 #endif 17832393Syz155240 i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7; 17842393Syz155240 if (i) 17852393Syz155240 return 1; 17862393Syz155240 /* 17872393Syz155240 * IP addresses matched. The next 32bits contains: 17882393Syz155240 * mast of old IP header security & authentication bits. 17892393Syz155240 */ 17902393Syz155240 lip++, lm++, ld++; 17912393Syz155240 i |= ((*lip & *lm) != *ld); 17922393Syz155240 FR_DEBUG(("4. %#08x & %#08x != %#08x\n", 17932393Syz155240 *lip, *lm, *ld)); 17942393Syz155240 17952393Syz155240 /* 17962393Syz155240 * Next we have 32 bits of packet flags. 17972393Syz155240 */ 17982393Syz155240 lip++, lm++, ld++; 17992393Syz155240 i |= ((*lip & *lm) != *ld); 18002393Syz155240 FR_DEBUG(("5. %#08x & %#08x != %#08x\n", 18012393Syz155240 *lip, *lm, *ld)); 18022393Syz155240 18032393Syz155240 if (i == 0) { 18042393Syz155240 /* 18052393Syz155240 * If a fragment, then only the first has what we're 18062393Syz155240 * looking for here... 18072393Syz155240 */ 18082393Syz155240 if (portcmp) { 18092393Syz155240 if (!fr_tcpudpchk(fin, &fr->fr_tuc)) 18102393Syz155240 i = 1; 18112393Syz155240 } else { 18122393Syz155240 if (fr->fr_dcmp || fr->fr_scmp || 18132393Syz155240 fr->fr_tcpf || fr->fr_tcpfm) 18142393Syz155240 i = 1; 18152393Syz155240 if (fr->fr_icmpm || fr->fr_icmp) { 18162393Syz155240 if (((fi->fi_p != IPPROTO_ICMP) && 18172393Syz155240 (fi->fi_p != IPPROTO_ICMPV6)) || 18182393Syz155240 fin->fin_off || (fin->fin_dlen < 2)) 18192393Syz155240 i = 1; 18202393Syz155240 else if ((fin->fin_data[0] & fr->fr_icmpm) != 18212393Syz155240 fr->fr_icmp) { 18222393Syz155240 FR_DEBUG(("i. %#x & %#x != %#x\n", 18232393Syz155240 fin->fin_data[0], 18242393Syz155240 fr->fr_icmpm, fr->fr_icmp)); 18252393Syz155240 i = 1; 18262393Syz155240 } 18272393Syz155240 } 18282393Syz155240 } 18292393Syz155240 } 18302393Syz155240 return i; 18312393Syz155240 } 18322393Syz155240 18332393Syz155240 18342393Syz155240 /* ------------------------------------------------------------------------ */ 18352393Syz155240 /* Function: fr_scanlist */ 18362393Syz155240 /* Returns: int - result flags of scanning filter list */ 18372393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 18382393Syz155240 /* pass(I) - default result to return for filtering */ 18392393Syz155240 /* */ 18402393Syz155240 /* Check the input/output list of rules for a match to the current packet. */ 18412393Syz155240 /* If a match is found, the value of fr_flags from the rule becomes the */ 18422393Syz155240 /* return value and fin->fin_fr points to the matched rule. */ 18432393Syz155240 /* */ 18442393Syz155240 /* This function may be called recusively upto 16 times (limit inbuilt.) */ 18452393Syz155240 /* When unwinding, it should finish up with fin_depth as 0. */ 18462393Syz155240 /* */ 18472393Syz155240 /* Could be per interface, but this gets real nasty when you don't have, */ 18482393Syz155240 /* or can't easily change, the kernel source code to . */ 18492393Syz155240 /* ------------------------------------------------------------------------ */ 18502393Syz155240 int fr_scanlist(fin, pass) 18512393Syz155240 fr_info_t *fin; 18522393Syz155240 u_32_t pass; 18532393Syz155240 { 18542393Syz155240 int rulen, portcmp, off, logged, skip; 18552393Syz155240 struct frentry *fr, *fnext; 18562393Syz155240 u_32_t passt, passo; 18573448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 18582393Syz155240 18592393Syz155240 /* 18602393Syz155240 * Do not allow nesting deeper than 16 levels. 18612393Syz155240 */ 18622393Syz155240 if (fin->fin_depth >= 16) 18632393Syz155240 return pass; 18642393Syz155240 18652393Syz155240 fr = fin->fin_fr; 18662393Syz155240 18672393Syz155240 /* 18682393Syz155240 * If there are no rules in this list, return now. 18692393Syz155240 */ 18702393Syz155240 if (fr == NULL) 18712393Syz155240 return pass; 18722393Syz155240 18732393Syz155240 skip = 0; 18742393Syz155240 logged = 0; 18752393Syz155240 portcmp = 0; 18762393Syz155240 fin->fin_depth++; 18772393Syz155240 fin->fin_fr = NULL; 18782393Syz155240 off = fin->fin_off; 18792393Syz155240 18802393Syz155240 if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off) 18812393Syz155240 portcmp = 1; 18822393Syz155240 18832393Syz155240 for (rulen = 0; fr; fr = fnext, rulen++) { 18842393Syz155240 fnext = fr->fr_next; 18852393Syz155240 if (skip != 0) { 18862393Syz155240 FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags)); 18872393Syz155240 skip--; 18882393Syz155240 continue; 18892393Syz155240 } 18902393Syz155240 18912393Syz155240 /* 18922393Syz155240 * In all checks below, a null (zero) value in the 18932393Syz155240 * filter struture is taken to mean a wildcard. 18942393Syz155240 * 18952393Syz155240 * check that we are working for the right interface 18962393Syz155240 */ 18972393Syz155240 #ifdef _KERNEL 18982393Syz155240 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 18992393Syz155240 continue; 19002393Syz155240 #else 19012393Syz155240 if (opts & (OPT_VERBOSE|OPT_DEBUG)) 19022393Syz155240 printf("\n"); 19032393Syz155240 FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' : 19042393Syz155240 FR_ISPASS(pass) ? 'p' : 19052393Syz155240 FR_ISACCOUNT(pass) ? 'A' : 19062393Syz155240 FR_ISAUTH(pass) ? 'a' : 19072393Syz155240 (pass & FR_NOMATCH) ? 'n' :'b')); 19082393Syz155240 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp) 19092393Syz155240 continue; 19102393Syz155240 FR_VERBOSE((":i")); 19112393Syz155240 #endif 19122393Syz155240 19132393Syz155240 switch (fr->fr_type) 19142393Syz155240 { 19152393Syz155240 case FR_T_IPF : 19162393Syz155240 case FR_T_IPF|FR_T_BUILTIN : 19172393Syz155240 if (fr_ipfcheck(fin, fr, portcmp)) 19182393Syz155240 continue; 19192393Syz155240 break; 19202393Syz155240 #if defined(IPFILTER_BPF) 19212393Syz155240 case FR_T_BPFOPC : 19222393Syz155240 case FR_T_BPFOPC|FR_T_BUILTIN : 19232393Syz155240 { 19242393Syz155240 u_char *mc; 19252393Syz155240 19262393Syz155240 if (*fin->fin_mp == NULL) 19272393Syz155240 continue; 19282393Syz155240 if (fin->fin_v != fr->fr_v) 19292393Syz155240 continue; 19302393Syz155240 mc = (u_char *)fin->fin_m; 19312393Syz155240 if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0)) 19322393Syz155240 continue; 19332393Syz155240 break; 19342393Syz155240 } 19352393Syz155240 #endif 19362393Syz155240 case FR_T_CALLFUNC|FR_T_BUILTIN : 19372393Syz155240 { 19382393Syz155240 frentry_t *f; 19392393Syz155240 19402393Syz155240 f = (*fr->fr_func)(fin, &pass); 19412393Syz155240 if (f != NULL) 19422393Syz155240 fr = f; 19432393Syz155240 else 19442393Syz155240 continue; 19452393Syz155240 break; 19462393Syz155240 } 19472393Syz155240 default : 19482393Syz155240 break; 19492393Syz155240 } 19502393Syz155240 19512393Syz155240 if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) { 19522393Syz155240 if (fin->fin_nattag == NULL) 19532393Syz155240 continue; 19542393Syz155240 if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0) 19552393Syz155240 continue; 19562393Syz155240 } 19572393Syz155240 FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen)); 19582393Syz155240 19592393Syz155240 passt = fr->fr_flags; 19602393Syz155240 19612393Syz155240 /* 19622393Syz155240 * Allowing a rule with the "keep state" flag set to match 19632393Syz155240 * packets that have been tagged "out of window" by the TCP 19642393Syz155240 * state tracking is foolish as the attempt to add a new 19652393Syz155240 * state entry to the table will fail. 19662393Syz155240 */ 19672393Syz155240 if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW)) 19682393Syz155240 continue; 19692393Syz155240 19702393Syz155240 /* 19712393Syz155240 * If the rule is a "call now" rule, then call the function 19722393Syz155240 * in the rule, if it exists and use the results from that. 19732393Syz155240 * If the function pointer is bad, just make like we ignore 19742393Syz155240 * it, except for increasing the hit counter. 19752393Syz155240 */ 19762393Syz155240 if ((passt & FR_CALLNOW) != 0) { 19772393Syz155240 ATOMIC_INC64(fr->fr_hits); 19782393Syz155240 if ((fr->fr_func != NULL) && 19792393Syz155240 (fr->fr_func != (ipfunc_t)-1)) { 19802393Syz155240 frentry_t *frs; 19812393Syz155240 19822393Syz155240 frs = fin->fin_fr; 19832393Syz155240 fin->fin_fr = fr; 19842393Syz155240 fr = (*fr->fr_func)(fin, &passt); 19852393Syz155240 if (fr == NULL) { 19862393Syz155240 fin->fin_fr = frs; 19872393Syz155240 continue; 19882393Syz155240 } 19892393Syz155240 passt = fr->fr_flags; 19902393Syz155240 fin->fin_fr = fr; 19912393Syz155240 } 19922393Syz155240 } else { 19932393Syz155240 fin->fin_fr = fr; 19942393Syz155240 } 19952393Syz155240 19962393Syz155240 #ifdef IPFILTER_LOG 19972393Syz155240 /* 19982393Syz155240 * Just log this packet... 19992393Syz155240 */ 20002393Syz155240 if ((passt & FR_LOGMASK) == FR_LOG) { 20012393Syz155240 if (ipflog(fin, passt) == -1) { 20022393Syz155240 if (passt & FR_LOGORBLOCK) { 20032393Syz155240 passt &= ~FR_CMDMASK; 20042393Syz155240 passt |= FR_BLOCK|FR_QUICK; 20052393Syz155240 } 20063448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[fin->fin_out].fr_skip); 20072393Syz155240 } 20083448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[fin->fin_out].fr_pkl); 20092393Syz155240 logged = 1; 20102393Syz155240 } 20112393Syz155240 #endif /* IPFILTER_LOG */ 20122393Syz155240 fr->fr_bytes += (U_QUAD_T)fin->fin_plen; 20132393Syz155240 passo = pass; 20142393Syz155240 if (FR_ISSKIP(passt)) 20152393Syz155240 skip = fr->fr_arg; 20162393Syz155240 else if ((passt & FR_LOGMASK) != FR_LOG) 20172393Syz155240 pass = passt; 20182393Syz155240 if (passt & (FR_RETICMP|FR_FAKEICMP)) 20192393Syz155240 fin->fin_icode = fr->fr_icode; 20202393Syz155240 FR_DEBUG(("pass %#x\n", pass)); 20212393Syz155240 ATOMIC_INC64(fr->fr_hits); 20222393Syz155240 fin->fin_rule = rulen; 20232393Syz155240 (void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN); 20242393Syz155240 if (fr->fr_grp != NULL) { 20252393Syz155240 fin->fin_fr = *fr->fr_grp; 20262393Syz155240 pass = fr_scanlist(fin, pass); 20272393Syz155240 if (fin->fin_fr == NULL) { 20282393Syz155240 fin->fin_rule = rulen; 20292393Syz155240 (void) strncpy(fin->fin_group, fr->fr_group, 20302393Syz155240 FR_GROUPLEN); 20312393Syz155240 fin->fin_fr = fr; 20322393Syz155240 } 20332393Syz155240 if (fin->fin_flx & FI_DONTCACHE) 20342393Syz155240 logged = 1; 20352393Syz155240 } 20362393Syz155240 20372393Syz155240 if (pass & FR_QUICK) { 20382393Syz155240 /* 20392393Syz155240 * Finally, if we've asked to track state for this 20402393Syz155240 * packet, set it up. Add state for "quick" rules 20412393Syz155240 * here so that if the action fails we can consider 20422393Syz155240 * the rule to "not match" and keep on processing 20432393Syz155240 * filter rules. 20442393Syz155240 */ 20452393Syz155240 if ((pass & FR_KEEPSTATE) && 20462393Syz155240 !(fin->fin_flx & FI_STATE)) { 20472393Syz155240 int out = fin->fin_out; 20482393Syz155240 20492393Syz155240 if (fr_addstate(fin, NULL, 0) != NULL) { 20503448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_ads); 20512393Syz155240 } else { 20523448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_bads); 20532393Syz155240 pass = passo; 20542393Syz155240 continue; 20552393Syz155240 } 20562393Syz155240 } 20572393Syz155240 break; 20582393Syz155240 } 20592393Syz155240 } 20602393Syz155240 if (logged) 20612393Syz155240 fin->fin_flx |= FI_DONTCACHE; 20622393Syz155240 fin->fin_depth--; 20632393Syz155240 return pass; 20642393Syz155240 } 20652393Syz155240 20662393Syz155240 20672393Syz155240 /* ------------------------------------------------------------------------ */ 20682393Syz155240 /* Function: fr_acctpkt */ 20692393Syz155240 /* Returns: frentry_t* - always returns NULL */ 20702393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 20712393Syz155240 /* passp(IO) - pointer to current/new filter decision (unused) */ 20722393Syz155240 /* */ 20732393Syz155240 /* Checks a packet against accounting rules, if there are any for the given */ 20742393Syz155240 /* IP protocol version. */ 20752393Syz155240 /* */ 20762393Syz155240 /* N.B.: this function returns NULL to match the prototype used by other */ 20772393Syz155240 /* functions called from the IPFilter "mainline" in fr_check(). */ 20782393Syz155240 /* ------------------------------------------------------------------------ */ 20792393Syz155240 frentry_t *fr_acctpkt(fin, passp) 20802393Syz155240 fr_info_t *fin; 20812393Syz155240 u_32_t *passp; 20822393Syz155240 { 20832393Syz155240 char group[FR_GROUPLEN]; 20842393Syz155240 frentry_t *fr, *frsave; 20852393Syz155240 u_32_t pass, rulen; 20863448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 20872393Syz155240 20882393Syz155240 passp = passp; 20892393Syz155240 #ifdef USE_INET6 20902393Syz155240 if (fin->fin_v == 6) 20913448Sdh155122 fr = ifs->ifs_ipacct6[fin->fin_out][ifs->ifs_fr_active]; 20922393Syz155240 else 20932393Syz155240 #endif 20943448Sdh155122 fr = ifs->ifs_ipacct[fin->fin_out][ifs->ifs_fr_active]; 20952393Syz155240 20962393Syz155240 if (fr != NULL) { 20972393Syz155240 frsave = fin->fin_fr; 20982393Syz155240 bcopy(fin->fin_group, group, FR_GROUPLEN); 20992393Syz155240 rulen = fin->fin_rule; 21002393Syz155240 fin->fin_fr = fr; 21012393Syz155240 pass = fr_scanlist(fin, FR_NOMATCH); 21022393Syz155240 if (FR_ISACCOUNT(pass)) { 21033448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_acct); 21042393Syz155240 } 21052393Syz155240 fin->fin_fr = frsave; 21062393Syz155240 bcopy(group, fin->fin_group, FR_GROUPLEN); 21072393Syz155240 fin->fin_rule = rulen; 21082393Syz155240 } 21092393Syz155240 return NULL; 21102393Syz155240 } 21112393Syz155240 21122393Syz155240 21132393Syz155240 /* ------------------------------------------------------------------------ */ 21142393Syz155240 /* Function: fr_firewall */ 21152393Syz155240 /* Returns: frentry_t* - returns pointer to matched rule, if no matches */ 21162393Syz155240 /* were found, returns NULL. */ 21172393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 21182393Syz155240 /* passp(IO) - pointer to current/new filter decision (unused) */ 21192393Syz155240 /* */ 21202393Syz155240 /* Applies an appropriate set of firewall rules to the packet, to see if */ 21212393Syz155240 /* there are any matches. The first check is to see if a match can be seen */ 21222393Syz155240 /* in the cache. If not, then search an appropriate list of rules. Once a */ 21232393Syz155240 /* matching rule is found, take any appropriate actions as defined by the */ 21242393Syz155240 /* rule - except logging. */ 21252393Syz155240 /* ------------------------------------------------------------------------ */ 21262393Syz155240 static frentry_t *fr_firewall(fin, passp) 21272393Syz155240 fr_info_t *fin; 21282393Syz155240 u_32_t *passp; 21292393Syz155240 { 21302393Syz155240 frentry_t *fr; 21312393Syz155240 fr_info_t *fc; 21322393Syz155240 u_32_t pass; 21332393Syz155240 int out; 21343448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 21352393Syz155240 21362393Syz155240 out = fin->fin_out; 21372393Syz155240 pass = *passp; 21382393Syz155240 21392393Syz155240 /* 21402393Syz155240 * If a packet is found in the auth table, then skip checking 21412393Syz155240 * the access lists for permission but we do need to consider 21422393Syz155240 * the result as if it were from the ACL's. 21432393Syz155240 */ 21443448Sdh155122 fc = &ifs->ifs_frcache[out][CACHE_HASH(fin)]; 21453448Sdh155122 READ_ENTER(&ifs->ifs_ipf_frcache); 21462393Syz155240 if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) { 21472393Syz155240 /* 21482393Syz155240 * copy cached data so we can unlock the mutexes earlier. 21492393Syz155240 */ 21502393Syz155240 bcopy((char *)fc, (char *)fin, FI_COPYSIZE); 21513448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 21523448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_chit); 21532393Syz155240 21542393Syz155240 if ((fr = fin->fin_fr) != NULL) { 21552393Syz155240 ATOMIC_INC64(fr->fr_hits); 21562393Syz155240 pass = fr->fr_flags; 21572393Syz155240 } 21582393Syz155240 } else { 21593448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 21602393Syz155240 21612393Syz155240 #ifdef USE_INET6 21622393Syz155240 if (fin->fin_v == 6) 21633448Sdh155122 fin->fin_fr = ifs->ifs_ipfilter6[out][ifs->ifs_fr_active]; 21642393Syz155240 else 21652393Syz155240 #endif 21663448Sdh155122 fin->fin_fr = ifs->ifs_ipfilter[out][ifs->ifs_fr_active]; 21672393Syz155240 if (fin->fin_fr != NULL) 21683448Sdh155122 pass = fr_scanlist(fin, ifs->ifs_fr_pass); 21692393Syz155240 21702393Syz155240 if (((pass & FR_KEEPSTATE) == 0) && 21712393Syz155240 ((fin->fin_flx & FI_DONTCACHE) == 0)) { 21723448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_frcache); 21732393Syz155240 bcopy((char *)fin, (char *)fc, FI_COPYSIZE); 21743448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_frcache); 21752393Syz155240 } 21762393Syz155240 if ((pass & FR_NOMATCH)) { 21773448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_nom); 21782393Syz155240 } 21792393Syz155240 fr = fin->fin_fr; 21802393Syz155240 } 21812393Syz155240 21822393Syz155240 /* 21832393Syz155240 * Apply packets per second rate-limiting to a rule as required. 21842393Syz155240 */ 21852393Syz155240 if ((fr != NULL) && (fr->fr_pps != 0) && 21862393Syz155240 !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) { 21872393Syz155240 pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST); 21882393Syz155240 pass |= FR_BLOCK; 21893448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_ppshit); 21902393Syz155240 } 21912393Syz155240 21922393Syz155240 /* 21932393Syz155240 * If we fail to add a packet to the authorization queue, then we 21942393Syz155240 * drop the packet later. However, if it was added then pretend 21952393Syz155240 * we've dropped it already. 21962393Syz155240 */ 21972393Syz155240 if (FR_ISAUTH(pass)) { 21982393Syz155240 if (fr_newauth(fin->fin_m, fin) != 0) { 21992393Syz155240 #ifdef _KERNEL 22002393Syz155240 fin->fin_m = *fin->fin_mp = NULL; 22012393Syz155240 #else 22022393Syz155240 ; 22032393Syz155240 #endif 22042393Syz155240 fin->fin_error = 0; 22052393Syz155240 } else 22062393Syz155240 fin->fin_error = ENOSPC; 22072393Syz155240 } 22082393Syz155240 22092393Syz155240 if ((fr != NULL) && (fr->fr_func != NULL) && 22102393Syz155240 (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW)) 22112393Syz155240 (void) (*fr->fr_func)(fin, &pass); 22122393Syz155240 22132393Syz155240 /* 22142393Syz155240 * If a rule is a pre-auth rule, check again in the list of rules 22152393Syz155240 * loaded for authenticated use. It does not particulary matter 22162393Syz155240 * if this search fails because a "preauth" result, from a rule, 22172393Syz155240 * is treated as "not a pass", hence the packet is blocked. 22182393Syz155240 */ 22192393Syz155240 if (FR_ISPREAUTH(pass)) { 22203448Sdh155122 if ((fin->fin_fr = ifs->ifs_ipauth) != NULL) 22213448Sdh155122 pass = fr_scanlist(fin, ifs->ifs_fr_pass); 22222393Syz155240 } 22232393Syz155240 22242393Syz155240 /* 22252393Syz155240 * If the rule has "keep frag" and the packet is actually a fragment, 22262393Syz155240 * then create a fragment state entry. 22272393Syz155240 */ 22282393Syz155240 if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) { 22292393Syz155240 if (fin->fin_flx & FI_FRAG) { 22302393Syz155240 if (fr_newfrag(fin, pass) == -1) { 22313448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_bnfr); 22322393Syz155240 } else { 22333448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_nfr); 22342393Syz155240 } 22352393Syz155240 } else { 22363448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_cfr); 22372393Syz155240 } 22382393Syz155240 } 22392393Syz155240 22402393Syz155240 /* 22412393Syz155240 * Finally, if we've asked to track state for this packet, set it up. 22422393Syz155240 */ 22432393Syz155240 if ((pass & FR_KEEPSTATE) && !(fin->fin_flx & FI_STATE)) { 22442393Syz155240 if (fr_addstate(fin, NULL, 0) != NULL) { 22453448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_ads); 22462393Syz155240 } else { 22473448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_bads); 22482393Syz155240 if (FR_ISPASS(pass)) { 22492393Syz155240 pass &= ~FR_CMDMASK; 22502393Syz155240 pass |= FR_BLOCK; 22512393Syz155240 } 22522393Syz155240 } 22532393Syz155240 } 22542393Syz155240 22552393Syz155240 fr = fin->fin_fr; 22562393Syz155240 22572393Syz155240 if (passp != NULL) 22582393Syz155240 *passp = pass; 22592393Syz155240 22602393Syz155240 return fr; 22612393Syz155240 } 22622393Syz155240 22632393Syz155240 22642393Syz155240 /* ------------------------------------------------------------------------ */ 22652393Syz155240 /* Function: fr_check */ 22662393Syz155240 /* Returns: int - 0 == packet allowed through, */ 22672393Syz155240 /* User space: */ 22682393Syz155240 /* -1 == packet blocked */ 22692393Syz155240 /* 1 == packet not matched */ 22702393Syz155240 /* -2 == requires authentication */ 22712393Syz155240 /* Kernel: */ 22722393Syz155240 /* > 0 == filter error # for packet */ 22732393Syz155240 /* Parameters: ip(I) - pointer to start of IPv4/6 packet */ 22742393Syz155240 /* hlen(I) - length of header */ 22752393Syz155240 /* ifp(I) - pointer to interface this packet is on */ 22762393Syz155240 /* out(I) - 0 == packet going in, 1 == packet going out */ 22772393Syz155240 /* mp(IO) - pointer to caller's buffer pointer that holds this */ 22782393Syz155240 /* IP packet. */ 22792393Syz155240 /* Solaris & HP-UX ONLY : */ 22802393Syz155240 /* qpi(I) - pointer to STREAMS queue information for this */ 22812393Syz155240 /* interface & direction. */ 22822393Syz155240 /* */ 22832393Syz155240 /* fr_check() is the master function for all IPFilter packet processing. */ 22842393Syz155240 /* It orchestrates: Network Address Translation (NAT), checking for packet */ 22852393Syz155240 /* authorisation (or pre-authorisation), presence of related state info., */ 22862393Syz155240 /* generating log entries, IP packet accounting, routing of packets as */ 22872393Syz155240 /* directed by firewall rules and of course whether or not to allow the */ 22882393Syz155240 /* packet to be further processed by the kernel. */ 22892393Syz155240 /* */ 22902393Syz155240 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer */ 22912393Syz155240 /* freed. Packets passed may be returned with the pointer pointed to by */ 22922393Syz155240 /* by "mp" changed to a new buffer. */ 22932393Syz155240 /* ------------------------------------------------------------------------ */ 22942393Syz155240 int fr_check(ip, hlen, ifp, out 22952393Syz155240 #if defined(_KERNEL) && defined(MENTAT) 22963448Sdh155122 , qif, mp, ifs) 22972393Syz155240 void *qif; 22982393Syz155240 #else 22993448Sdh155122 , mp, ifs) 23002393Syz155240 #endif 23012393Syz155240 mb_t **mp; 23022393Syz155240 ip_t *ip; 23032393Syz155240 int hlen; 23042393Syz155240 void *ifp; 23052393Syz155240 int out; 23063448Sdh155122 ipf_stack_t *ifs; 23072393Syz155240 { 23082393Syz155240 /* 23092393Syz155240 * The above really sucks, but short of writing a diff 23102393Syz155240 */ 23112393Syz155240 fr_info_t frinfo; 23122393Syz155240 fr_info_t *fin = &frinfo; 23133448Sdh155122 u_32_t pass; 23142393Syz155240 frentry_t *fr = NULL; 23152393Syz155240 int v = IP_V(ip); 23162393Syz155240 mb_t *mc = NULL; 23172393Syz155240 mb_t *m; 23182393Syz155240 #ifdef USE_INET6 23192393Syz155240 ip6_t *ip6; 23202393Syz155240 #endif 23212393Syz155240 #ifdef _KERNEL 23222393Syz155240 # ifdef MENTAT 23232393Syz155240 qpktinfo_t *qpi = qif; 23242393Syz155240 #endif 23252393Syz155240 #endif 23263448Sdh155122 23272393Syz155240 SPL_INT(s); 23283448Sdh155122 pass = ifs->ifs_fr_pass; 23292393Syz155240 23302393Syz155240 /* 23312393Syz155240 * The first part of fr_check() deals with making sure that what goes 23322393Syz155240 * into the filtering engine makes some sense. Information about the 23332393Syz155240 * the packet is distilled, collected into a fr_info_t structure and 23342393Syz155240 * the an attempt to ensure the buffer the packet is in is big enough 23352393Syz155240 * to hold all the required packet headers. 23362393Syz155240 */ 23372393Syz155240 #ifdef _KERNEL 23382393Syz155240 # ifdef MENTAT 23392393Syz155240 if (!OK_32PTR(ip)) 23402393Syz155240 return 2; 23412393Syz155240 # endif 23422393Syz155240 23433448Sdh155122 READ_ENTER(&ifs->ifs_ipf_global); 23443448Sdh155122 23453448Sdh155122 if (ifs->ifs_fr_running <= 0) { 23463448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_global); 23472393Syz155240 return 0; 23482393Syz155240 } 23492393Syz155240 23502393Syz155240 bzero((char *)fin, sizeof(*fin)); 23512393Syz155240 23522393Syz155240 # ifdef MENTAT 23532958Sdr146992 if (qpi->qpi_flags & QPI_NOCKSUM) 23542958Sdr146992 fin->fin_flx |= FI_NOCKSUM; 23552393Syz155240 m = qpi->qpi_m; 23562393Syz155240 fin->fin_qfm = m; 23572393Syz155240 fin->fin_qpi = qpi; 23582393Syz155240 # else /* MENTAT */ 23592393Syz155240 23602393Syz155240 m = *mp; 23612393Syz155240 23622393Syz155240 # if defined(M_MCAST) 23632393Syz155240 if ((m->m_flags & M_MCAST) != 0) 23642393Syz155240 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 23652393Syz155240 # endif 23662393Syz155240 # if defined(M_MLOOP) 23672393Syz155240 if ((m->m_flags & M_MLOOP) != 0) 23682393Syz155240 fin->fin_flx |= FI_MBCAST|FI_MULTICAST; 23692393Syz155240 # endif 23702393Syz155240 # if defined(M_BCAST) 23712393Syz155240 if ((m->m_flags & M_BCAST) != 0) 23722393Syz155240 fin->fin_flx |= FI_MBCAST|FI_BROADCAST; 23732393Syz155240 # endif 23742393Syz155240 # ifdef M_CANFASTFWD 23752393Syz155240 /* 23762393Syz155240 * XXX For now, IP Filter and fast-forwarding of cached flows 23772393Syz155240 * XXX are mutually exclusive. Eventually, IP Filter should 23782393Syz155240 * XXX get a "can-fast-forward" filter rule. 23792393Syz155240 */ 23802393Syz155240 m->m_flags &= ~M_CANFASTFWD; 23812393Syz155240 # endif /* M_CANFASTFWD */ 23822393Syz155240 # ifdef CSUM_DELAY_DATA 23832393Syz155240 /* 23842393Syz155240 * disable delayed checksums. 23852393Syz155240 */ 23862393Syz155240 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 23872393Syz155240 in_delayed_cksum(m); 23882393Syz155240 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 23892393Syz155240 } 23902393Syz155240 # endif /* CSUM_DELAY_DATA */ 23912393Syz155240 # endif /* MENTAT */ 23922393Syz155240 #else 23933448Sdh155122 READ_ENTER(&ifs->ifs_ipf_global); 23942393Syz155240 23952393Syz155240 bzero((char *)fin, sizeof(*fin)); 23962393Syz155240 m = *mp; 23972393Syz155240 #endif /* _KERNEL */ 23982393Syz155240 23992393Syz155240 fin->fin_v = v; 24002393Syz155240 fin->fin_m = m; 24012393Syz155240 fin->fin_ip = ip; 24022393Syz155240 fin->fin_mp = mp; 24032393Syz155240 fin->fin_out = out; 24042393Syz155240 fin->fin_ifp = ifp; 24052393Syz155240 fin->fin_error = ENETUNREACH; 24062393Syz155240 fin->fin_hlen = (u_short)hlen; 24072393Syz155240 fin->fin_dp = (char *)ip + hlen; 24082393Syz155240 fin->fin_ipoff = (char *)ip - MTOD(m, char *); 24093448Sdh155122 fin->fin_ifs = ifs; 24102393Syz155240 24112393Syz155240 SPL_NET(s); 24122393Syz155240 24132393Syz155240 #ifdef USE_INET6 24142393Syz155240 if (v == 6) { 24153448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_ipv6); 24162393Syz155240 /* 24172393Syz155240 * Jumbo grams are quite likely too big for internal buffer 24182393Syz155240 * structures to handle comfortably, for now, so just drop 24192393Syz155240 * them. 24202393Syz155240 */ 24212393Syz155240 ip6 = (ip6_t *)ip; 24222393Syz155240 fin->fin_plen = ntohs(ip6->ip6_plen); 24232393Syz155240 if (fin->fin_plen == 0) { 24243448Sdh155122 READ_ENTER(&ifs->ifs_ipf_mutex); 24252393Syz155240 pass = FR_BLOCK|FR_NOMATCH; 24262393Syz155240 goto filtered; 24272393Syz155240 } 24282393Syz155240 fin->fin_plen += sizeof(ip6_t); 24292393Syz155240 } else 24302393Syz155240 #endif 24312393Syz155240 { 24322393Syz155240 #if (OpenBSD >= 200311) && defined(_KERNEL) 24332393Syz155240 ip->ip_len = ntohs(ip->ip_len); 24342393Syz155240 ip->ip_off = ntohs(ip->ip_off); 24352393Syz155240 #endif 24362393Syz155240 fin->fin_plen = ip->ip_len; 24372393Syz155240 } 24382393Syz155240 24392393Syz155240 if (fr_makefrip(hlen, ip, fin) == -1) { 24403448Sdh155122 READ_ENTER(&ifs->ifs_ipf_mutex); 24412393Syz155240 pass = FR_BLOCK; 24422393Syz155240 goto filtered; 24432393Syz155240 } 24442393Syz155240 24452393Syz155240 /* 24462393Syz155240 * For at least IPv6 packets, if a m_pullup() fails then this pointer 24472393Syz155240 * becomes NULL and so we have no packet to free. 24482393Syz155240 */ 24492393Syz155240 if (*fin->fin_mp == NULL) 24502393Syz155240 goto finished; 24512393Syz155240 24522393Syz155240 if (!out) { 24532393Syz155240 if (v == 4) { 24542393Syz155240 #ifdef _KERNEL 24553448Sdh155122 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) { 24563448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_badsrc); 24572393Syz155240 fin->fin_flx |= FI_BADSRC; 24582393Syz155240 } 24592393Syz155240 #endif 24603448Sdh155122 if (fin->fin_ip->ip_ttl < ifs->ifs_fr_minttl) { 24613448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_badttl); 24622393Syz155240 fin->fin_flx |= FI_LOWTTL; 24632393Syz155240 } 24642393Syz155240 } 24652393Syz155240 #ifdef USE_INET6 24662393Syz155240 else if (v == 6) { 24672393Syz155240 ip6 = (ip6_t *)ip; 24682393Syz155240 #ifdef _KERNEL 24693448Sdh155122 if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) { 24703448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_badsrc); 24712393Syz155240 fin->fin_flx |= FI_BADSRC; 24722393Syz155240 } 24732393Syz155240 #endif 24743448Sdh155122 if (ip6->ip6_hlim < ifs->ifs_fr_minttl) { 24753448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_badttl); 24762393Syz155240 fin->fin_flx |= FI_LOWTTL; 24772393Syz155240 } 24782393Syz155240 } 24792393Syz155240 #endif 24802393Syz155240 } 24812393Syz155240 24822393Syz155240 if (fin->fin_flx & FI_SHORT) { 24833448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_short); 24842393Syz155240 } 24852393Syz155240 24863448Sdh155122 READ_ENTER(&ifs->ifs_ipf_mutex); 24872393Syz155240 24882393Syz155240 /* 24892393Syz155240 * Check auth now. This, combined with the check below to see if apass 24902393Syz155240 * is 0 is to ensure that we don't count the packet twice, which can 24912393Syz155240 * otherwise occur when we reprocess it. As it is, we only count it 24922393Syz155240 * after it has no auth. table matchup. This also stops NAT from 24932393Syz155240 * occuring until after the packet has been auth'd. 24942393Syz155240 */ 24952393Syz155240 fr = fr_checkauth(fin, &pass); 24962393Syz155240 if (!out) { 24972393Syz155240 if (fr_checknatin(fin, &pass) == -1) { 24983448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 24992393Syz155240 goto finished; 25002393Syz155240 } 25012393Syz155240 } 25022393Syz155240 if (!out) 25032393Syz155240 (void) fr_acctpkt(fin, NULL); 25042393Syz155240 25052393Syz155240 if (fr == NULL) 25062393Syz155240 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG) 25072393Syz155240 fr = fr_knownfrag(fin, &pass); 25082393Syz155240 if (fr == NULL) 25092393Syz155240 fr = fr_checkstate(fin, &pass); 25102393Syz155240 25112393Syz155240 if ((pass & FR_NOMATCH) || (fr == NULL)) 25122393Syz155240 fr = fr_firewall(fin, &pass); 25132393Syz155240 25142393Syz155240 fin->fin_fr = fr; 25152393Syz155240 25162393Syz155240 /* 25172393Syz155240 * Only count/translate packets which will be passed on, out the 25182393Syz155240 * interface. 25192393Syz155240 */ 25202393Syz155240 if (out && FR_ISPASS(pass)) { 25212393Syz155240 (void) fr_acctpkt(fin, NULL); 25222393Syz155240 25232393Syz155240 if (fr_checknatout(fin, &pass) == -1) { 25243448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 25252393Syz155240 goto finished; 25263448Sdh155122 } else if ((ifs->ifs_fr_update_ipid != 0) && (v == 4)) { 25272393Syz155240 if (fr_updateipid(fin) == -1) { 25283448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[1].fr_ipud); 25292393Syz155240 pass &= ~FR_CMDMASK; 25302393Syz155240 pass |= FR_BLOCK; 25312393Syz155240 } else { 25323448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_ipud); 25332393Syz155240 } 25342393Syz155240 } 25352393Syz155240 } 25362393Syz155240 25372393Syz155240 #ifdef IPFILTER_LOG 25383448Sdh155122 if ((ifs->ifs_fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) { 25392393Syz155240 (void) fr_dolog(fin, &pass); 25402393Syz155240 } 25412393Syz155240 #endif 25422393Syz155240 25432393Syz155240 if (fin->fin_state != NULL) 25443448Sdh155122 fr_statederef(fin, (ipstate_t **)&fin->fin_state, ifs); 25452393Syz155240 25462393Syz155240 if (fin->fin_nat != NULL) 25473448Sdh155122 fr_natderef((nat_t **)&fin->fin_nat, ifs); 25482393Syz155240 25492393Syz155240 /* 25502393Syz155240 * Only allow FR_DUP to work if a rule matched - it makes no sense to 25512393Syz155240 * set FR_DUP as a "default" as there are no instructions about where 25522393Syz155240 * to send the packet. Use fin_m here because it may have changed 25532393Syz155240 * (without an update of 'm') in prior processing. 25542393Syz155240 */ 25552393Syz155240 if ((fr != NULL) && (pass & FR_DUP)) { 25562393Syz155240 mc = M_DUPLICATE(fin->fin_m); 25572393Syz155240 } 25582393Syz155240 25592393Syz155240 if (pass & (FR_RETRST|FR_RETICMP)) { 25602393Syz155240 /* 25612393Syz155240 * Should we return an ICMP packet to indicate error 25622393Syz155240 * status passing through the packet filter ? 25632393Syz155240 * WARNING: ICMP error packets AND TCP RST packets should 25642393Syz155240 * ONLY be sent in repsonse to incoming packets. Sending them 25652393Syz155240 * in response to outbound packets can result in a panic on 25662393Syz155240 * some operating systems. 25672393Syz155240 */ 25682393Syz155240 if (!out) { 25692393Syz155240 if (pass & FR_RETICMP) { 25702393Syz155240 int dst; 25712393Syz155240 25722393Syz155240 if ((pass & FR_RETMASK) == FR_FAKEICMP) 25732393Syz155240 dst = 1; 25742393Syz155240 else 25752393Syz155240 dst = 0; 25762393Syz155240 (void) fr_send_icmp_err(ICMP_UNREACH, fin, dst); 25773448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[0].fr_ret); 25782393Syz155240 } else if (((pass & FR_RETMASK) == FR_RETRST) && 25792393Syz155240 !(fin->fin_flx & FI_SHORT)) { 25802393Syz155240 if (fr_send_reset(fin) == 0) { 25813448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[1].fr_ret); 25822393Syz155240 } 25832393Syz155240 } 25842393Syz155240 } else { 25852393Syz155240 if (pass & FR_RETRST) 25862393Syz155240 fin->fin_error = ECONNRESET; 25872393Syz155240 } 25882393Syz155240 } 25892393Syz155240 25902393Syz155240 /* 25912393Syz155240 * If we didn't drop off the bottom of the list of rules (and thus 25922393Syz155240 * the 'current' rule fr is not NULL), then we may have some extra 25932393Syz155240 * instructions about what to do with a packet. 25942393Syz155240 * Once we're finished return to our caller, freeing the packet if 25952393Syz155240 * we are dropping it (* BSD ONLY *). 25962393Syz155240 * Reassign m from fin_m as we may have a new buffer, now. 25972393Syz155240 */ 25982393Syz155240 filtered: 25992393Syz155240 m = fin->fin_m; 26002393Syz155240 26012393Syz155240 if (fr != NULL) { 26022393Syz155240 frdest_t *fdp; 26032393Syz155240 26042393Syz155240 fdp = &fr->fr_tifs[fin->fin_rev]; 26052393Syz155240 26062393Syz155240 if (!out && (pass & FR_FASTROUTE)) { 26072393Syz155240 /* 26082393Syz155240 * For fastroute rule, no destioation interface defined 26092393Syz155240 * so pass NULL as the frdest_t parameter 26102393Syz155240 */ 26112393Syz155240 (void) fr_fastroute(m, mp, fin, NULL); 26122393Syz155240 m = *mp = NULL; 26132393Syz155240 } else if ((fdp->fd_ifp != NULL) && 26142393Syz155240 (fdp->fd_ifp != (struct ifnet *)-1)) { 26152393Syz155240 /* this is for to rules: */ 26162393Syz155240 (void) fr_fastroute(m, mp, fin, fdp); 26172393Syz155240 m = *mp = NULL; 26182393Syz155240 } 26192393Syz155240 26202393Syz155240 /* 26212393Syz155240 * Generate a duplicated packet. 26222393Syz155240 */ 26232393Syz155240 if (mc != NULL) 26242393Syz155240 (void) fr_fastroute(mc, &mc, fin, &fr->fr_dif); 26252393Syz155240 } 26262393Syz155240 26272393Syz155240 /* 26282393Syz155240 * This late because the likes of fr_fastroute() use fin_fr. 26292393Syz155240 */ 26303448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 26312393Syz155240 26322393Syz155240 finished: 26332393Syz155240 if (!FR_ISPASS(pass)) { 26343448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_block); 26352393Syz155240 if (*mp != NULL) { 26362393Syz155240 FREE_MB_T(*mp); 26372393Syz155240 m = *mp = NULL; 26382393Syz155240 } 26392393Syz155240 } else { 26403448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_pass); 26412393Syz155240 #if defined(_KERNEL) && defined(__sgi) 26422393Syz155240 if ((fin->fin_hbuf != NULL) && 26432393Syz155240 (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) { 26442393Syz155240 COPYBACK(m, 0, fin->fin_plen, fin->fin_hbuf); 26452393Syz155240 } 26462393Syz155240 #endif 26472393Syz155240 } 26482393Syz155240 26492393Syz155240 SPL_X(s); 26503448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_global); 26512393Syz155240 26522393Syz155240 #ifdef _KERNEL 26532393Syz155240 # if OpenBSD >= 200311 26542393Syz155240 if (FR_ISPASS(pass) && (v == 4)) { 26552393Syz155240 ip = fin->fin_ip; 26562393Syz155240 ip->ip_len = ntohs(ip->ip_len); 26572393Syz155240 ip->ip_off = ntohs(ip->ip_off); 26582393Syz155240 } 26592393Syz155240 # endif 26602393Syz155240 return (FR_ISPASS(pass)) ? 0 : fin->fin_error; 26612393Syz155240 #else /* _KERNEL */ 26622393Syz155240 FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass)); 26632393Syz155240 if ((pass & FR_NOMATCH) != 0) 26642393Syz155240 return 1; 26652393Syz155240 26662393Syz155240 if ((pass & FR_RETMASK) != 0) 26672393Syz155240 switch (pass & FR_RETMASK) 26682393Syz155240 { 26692393Syz155240 case FR_RETRST : 26702393Syz155240 return 3; 26712393Syz155240 case FR_RETICMP : 26722393Syz155240 return 4; 26732393Syz155240 case FR_FAKEICMP : 26742393Syz155240 return 5; 26752393Syz155240 } 26762393Syz155240 26772393Syz155240 switch (pass & FR_CMDMASK) 26782393Syz155240 { 26792393Syz155240 case FR_PASS : 26802393Syz155240 return 0; 26812393Syz155240 case FR_BLOCK : 26822393Syz155240 return -1; 26832393Syz155240 case FR_AUTH : 26842393Syz155240 return -2; 26852393Syz155240 case FR_ACCOUNT : 26862393Syz155240 return -3; 26872393Syz155240 case FR_PREAUTH : 26882393Syz155240 return -4; 26892393Syz155240 } 26902393Syz155240 return 2; 26912393Syz155240 #endif /* _KERNEL */ 26922393Syz155240 } 26932393Syz155240 26942393Syz155240 26952393Syz155240 #ifdef IPFILTER_LOG 26962393Syz155240 /* ------------------------------------------------------------------------ */ 26972393Syz155240 /* Function: fr_dolog */ 26982393Syz155240 /* Returns: frentry_t* - returns contents of fin_fr (no change made) */ 26992393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 27002393Syz155240 /* passp(IO) - pointer to current/new filter decision (unused) */ 27012393Syz155240 /* */ 27022393Syz155240 /* Checks flags set to see how a packet should be logged, if it is to be */ 27032393Syz155240 /* logged. Adjust statistics based on its success or not. */ 27042393Syz155240 /* ------------------------------------------------------------------------ */ 27052393Syz155240 frentry_t *fr_dolog(fin, passp) 27062393Syz155240 fr_info_t *fin; 27072393Syz155240 u_32_t *passp; 27082393Syz155240 { 27092393Syz155240 u_32_t pass; 27102393Syz155240 int out; 27113448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 27122393Syz155240 27132393Syz155240 out = fin->fin_out; 27142393Syz155240 pass = *passp; 27152393Syz155240 27163448Sdh155122 if ((ifs->ifs_fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) { 27172393Syz155240 pass |= FF_LOGNOMATCH; 27183448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_npkl); 27192393Syz155240 goto logit; 27202393Syz155240 } else if (((pass & FR_LOGMASK) == FR_LOGP) || 27213448Sdh155122 (FR_ISPASS(pass) && (ifs->ifs_fr_flags & FF_LOGPASS))) { 27222393Syz155240 if ((pass & FR_LOGMASK) != FR_LOGP) 27232393Syz155240 pass |= FF_LOGPASS; 27243448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_ppkl); 27252393Syz155240 goto logit; 27262393Syz155240 } else if (((pass & FR_LOGMASK) == FR_LOGB) || 27273448Sdh155122 (FR_ISBLOCK(pass) && (ifs->ifs_fr_flags & FF_LOGBLOCK))) { 27282393Syz155240 if ((pass & FR_LOGMASK) != FR_LOGB) 27292393Syz155240 pass |= FF_LOGBLOCK; 27303448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_bpkl); 27312393Syz155240 logit: 27322393Syz155240 if (ipflog(fin, pass) == -1) { 27333448Sdh155122 ATOMIC_INCL(ifs->ifs_frstats[out].fr_skip); 27342393Syz155240 27352393Syz155240 /* 27362393Syz155240 * If the "or-block" option has been used then 27372393Syz155240 * block the packet if we failed to log it. 27382393Syz155240 */ 27392393Syz155240 if ((pass & FR_LOGORBLOCK) && 27402393Syz155240 FR_ISPASS(pass)) { 27412393Syz155240 pass &= ~FR_CMDMASK; 27422393Syz155240 pass |= FR_BLOCK; 27432393Syz155240 } 27442393Syz155240 } 27452393Syz155240 *passp = pass; 27462393Syz155240 } 27472393Syz155240 27482393Syz155240 return fin->fin_fr; 27492393Syz155240 } 27502393Syz155240 #endif /* IPFILTER_LOG */ 27512393Syz155240 27522393Syz155240 27532393Syz155240 /* ------------------------------------------------------------------------ */ 27542393Syz155240 /* Function: ipf_cksum */ 27552393Syz155240 /* Returns: u_short - IP header checksum */ 27562393Syz155240 /* Parameters: addr(I) - pointer to start of buffer to checksum */ 27572393Syz155240 /* len(I) - length of buffer in bytes */ 27582393Syz155240 /* */ 27592393Syz155240 /* Calculate the two's complement 16 bit checksum of the buffer passed. */ 27602393Syz155240 /* */ 27612393Syz155240 /* N.B.: addr should be 16bit aligned. */ 27622393Syz155240 /* ------------------------------------------------------------------------ */ 27632393Syz155240 u_short ipf_cksum(addr, len) 27642393Syz155240 u_short *addr; 27652393Syz155240 int len; 27662393Syz155240 { 27672393Syz155240 u_32_t sum = 0; 27682393Syz155240 27692393Syz155240 for (sum = 0; len > 1; len -= 2) 27702393Syz155240 sum += *addr++; 27712393Syz155240 27722393Syz155240 /* mop up an odd byte, if necessary */ 27732393Syz155240 if (len == 1) 27742393Syz155240 sum += *(u_char *)addr; 27752393Syz155240 27762393Syz155240 /* 27772393Syz155240 * add back carry outs from top 16 bits to low 16 bits 27782393Syz155240 */ 27792393Syz155240 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 27802393Syz155240 sum += (sum >> 16); /* add carry */ 27812393Syz155240 return (u_short)(~sum); 27822393Syz155240 } 27832393Syz155240 27842393Syz155240 27852393Syz155240 /* ------------------------------------------------------------------------ */ 27862393Syz155240 /* Function: fr_cksum */ 27872393Syz155240 /* Returns: u_short - layer 4 checksum */ 27882393Syz155240 /* Parameters: m(I ) - pointer to buffer holding packet */ 27892393Syz155240 /* ip(I) - pointer to IP header */ 27902393Syz155240 /* l4proto(I) - protocol to caclulate checksum for */ 27912393Syz155240 /* l4hdr(I) - pointer to layer 4 header */ 27922393Syz155240 /* */ 27932393Syz155240 /* Calculates the TCP checksum for the packet held in "m", using the data */ 27942393Syz155240 /* in the IP header "ip" to seed it. */ 27952393Syz155240 /* */ 27962393Syz155240 /* NB: This function assumes we've pullup'd enough for all of the IP header */ 27972393Syz155240 /* and the TCP header. We also assume that data blocks aren't allocated in */ 27982393Syz155240 /* odd sizes. */ 27992393Syz155240 /* */ 28002393Syz155240 /* Expects ip_len to be in host byte order when called. */ 28012393Syz155240 /* ------------------------------------------------------------------------ */ 28022393Syz155240 u_short fr_cksum(m, ip, l4proto, l4hdr) 28032393Syz155240 mb_t *m; 28042393Syz155240 ip_t *ip; 28052393Syz155240 int l4proto; 28062393Syz155240 void *l4hdr; 28072393Syz155240 { 28082393Syz155240 u_short *sp, slen, sumsave, l4hlen, *csump; 28092393Syz155240 u_int sum, sum2; 28102393Syz155240 int hlen; 28112393Syz155240 #ifdef USE_INET6 28122393Syz155240 ip6_t *ip6; 28132393Syz155240 #endif 28142393Syz155240 28152393Syz155240 csump = NULL; 28162393Syz155240 sumsave = 0; 28172393Syz155240 l4hlen = 0; 28182393Syz155240 sp = NULL; 28192393Syz155240 slen = 0; 28202393Syz155240 hlen = 0; 28212393Syz155240 sum = 0; 28222393Syz155240 28232393Syz155240 /* 28242393Syz155240 * Add up IP Header portion 28252393Syz155240 */ 28262393Syz155240 #ifdef USE_INET6 28272393Syz155240 if (IP_V(ip) == 4) { 28282393Syz155240 #endif 28292393Syz155240 hlen = IP_HL(ip) << 2; 28302393Syz155240 slen = ip->ip_len - hlen; 28312393Syz155240 sum = htons((u_short)l4proto); 28322393Syz155240 sum += htons(slen); 28332393Syz155240 sp = (u_short *)&ip->ip_src; 28342393Syz155240 sum += *sp++; /* ip_src */ 28352393Syz155240 sum += *sp++; 28362393Syz155240 sum += *sp++; /* ip_dst */ 28372393Syz155240 sum += *sp++; 28382393Syz155240 #ifdef USE_INET6 28392393Syz155240 } else if (IP_V(ip) == 6) { 28402393Syz155240 ip6 = (ip6_t *)ip; 28412393Syz155240 hlen = sizeof(*ip6); 28422393Syz155240 slen = ntohs(ip6->ip6_plen); 28432393Syz155240 sum = htons((u_short)l4proto); 28442393Syz155240 sum += htons(slen); 28452393Syz155240 sp = (u_short *)&ip6->ip6_src; 28462393Syz155240 sum += *sp++; /* ip6_src */ 28472393Syz155240 sum += *sp++; 28482393Syz155240 sum += *sp++; 28492393Syz155240 sum += *sp++; 28502393Syz155240 sum += *sp++; 28512393Syz155240 sum += *sp++; 28522393Syz155240 sum += *sp++; 28532393Syz155240 sum += *sp++; 28542393Syz155240 sum += *sp++; /* ip6_dst */ 28552393Syz155240 sum += *sp++; 28562393Syz155240 sum += *sp++; 28572393Syz155240 sum += *sp++; 28582393Syz155240 sum += *sp++; 28592393Syz155240 sum += *sp++; 28602393Syz155240 sum += *sp++; 28612393Syz155240 sum += *sp++; 28622393Syz155240 } 28632393Syz155240 #endif 28642393Syz155240 28652393Syz155240 switch (l4proto) 28662393Syz155240 { 28672393Syz155240 case IPPROTO_UDP : 28682393Syz155240 csump = &((udphdr_t *)l4hdr)->uh_sum; 28692393Syz155240 l4hlen = sizeof(udphdr_t); 28702393Syz155240 break; 28712393Syz155240 28722393Syz155240 case IPPROTO_TCP : 28732393Syz155240 csump = &((tcphdr_t *)l4hdr)->th_sum; 28742393Syz155240 l4hlen = sizeof(tcphdr_t); 28752393Syz155240 break; 28762393Syz155240 case IPPROTO_ICMP : 28772393Syz155240 csump = &((icmphdr_t *)l4hdr)->icmp_cksum; 28782393Syz155240 l4hlen = 4; 28792393Syz155240 sum = 0; 28802393Syz155240 break; 28812393Syz155240 default : 28822393Syz155240 break; 28832393Syz155240 } 28842393Syz155240 28852393Syz155240 if (csump != NULL) { 28862393Syz155240 sumsave = *csump; 28872393Syz155240 *csump = 0; 28882393Syz155240 } 28892393Syz155240 28902393Syz155240 l4hlen = l4hlen; /* LINT */ 28912393Syz155240 28922393Syz155240 #ifdef _KERNEL 28932393Syz155240 # ifdef MENTAT 28942393Syz155240 { 28952393Syz155240 void *rp = m->b_rptr; 28962393Syz155240 28972393Syz155240 if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr) 28982393Syz155240 m->b_rptr = (u_char *)ip; 28992393Syz155240 sum2 = ip_cksum(m, hlen, sum); /* hlen == offset */ 29002393Syz155240 m->b_rptr = rp; 29012393Syz155240 sum2 = (sum2 & 0xffff) + (sum2 >> 16); 29022393Syz155240 sum2 = ~sum2 & 0xffff; 29032393Syz155240 } 29042393Syz155240 # else /* MENTAT */ 29052393Syz155240 # if defined(BSD) || defined(sun) 29062393Syz155240 # if BSD >= 199103 29072393Syz155240 m->m_data += hlen; 29082393Syz155240 # else 29092393Syz155240 m->m_off += hlen; 29102393Syz155240 # endif 29112393Syz155240 m->m_len -= hlen; 29122393Syz155240 sum2 = in_cksum(m, slen); 29132393Syz155240 m->m_len += hlen; 29142393Syz155240 # if BSD >= 199103 29152393Syz155240 m->m_data -= hlen; 29162393Syz155240 # else 29172393Syz155240 m->m_off -= hlen; 29182393Syz155240 # endif 29192393Syz155240 /* 29202393Syz155240 * Both sum and sum2 are partial sums, so combine them together. 29212393Syz155240 */ 29222393Syz155240 sum += ~sum2 & 0xffff; 29232393Syz155240 while (sum > 0xffff) 29242393Syz155240 sum = (sum & 0xffff) + (sum >> 16); 29252393Syz155240 sum2 = ~sum & 0xffff; 29262393Syz155240 # else /* defined(BSD) || defined(sun) */ 29272393Syz155240 { 29282393Syz155240 union { 29292393Syz155240 u_char c[2]; 29302393Syz155240 u_short s; 29312393Syz155240 } bytes; 29322393Syz155240 u_short len = ip->ip_len; 29332393Syz155240 # if defined(__sgi) 29342393Syz155240 int add; 29352393Syz155240 # endif 29362393Syz155240 29372393Syz155240 /* 29382393Syz155240 * Add up IP Header portion 29392393Syz155240 */ 29402393Syz155240 if (sp != (u_short *)l4hdr) 29412393Syz155240 sp = (u_short *)l4hdr; 29422393Syz155240 29432393Syz155240 switch (l4proto) 29442393Syz155240 { 29452393Syz155240 case IPPROTO_UDP : 29462393Syz155240 sum += *sp++; /* sport */ 29472393Syz155240 sum += *sp++; /* dport */ 29482393Syz155240 sum += *sp++; /* udp length */ 29492393Syz155240 sum += *sp++; /* checksum */ 29502393Syz155240 break; 29512393Syz155240 29522393Syz155240 case IPPROTO_TCP : 29532393Syz155240 sum += *sp++; /* sport */ 29542393Syz155240 sum += *sp++; /* dport */ 29552393Syz155240 sum += *sp++; /* seq */ 29562393Syz155240 sum += *sp++; 29572393Syz155240 sum += *sp++; /* ack */ 29582393Syz155240 sum += *sp++; 29592393Syz155240 sum += *sp++; /* off */ 29602393Syz155240 sum += *sp++; /* win */ 29612393Syz155240 sum += *sp++; /* checksum */ 29622393Syz155240 sum += *sp++; /* urp */ 29632393Syz155240 break; 29642393Syz155240 case IPPROTO_ICMP : 29652393Syz155240 sum = *sp++; /* type/code */ 29662393Syz155240 sum += *sp++; /* checksum */ 29672393Syz155240 break; 29682393Syz155240 } 29692393Syz155240 29702393Syz155240 # ifdef __sgi 29712393Syz155240 /* 29722393Syz155240 * In case we had to copy the IP & TCP header out of mbufs, 29732393Syz155240 * skip over the mbuf bits which are the header 29742393Syz155240 */ 29752393Syz155240 if ((caddr_t)ip != mtod(m, caddr_t)) { 29762393Syz155240 hlen = (caddr_t)sp - (caddr_t)ip; 29772393Syz155240 while (hlen) { 29782393Syz155240 add = MIN(hlen, m->m_len); 29792393Syz155240 sp = (u_short *)(mtod(m, caddr_t) + add); 29802393Syz155240 hlen -= add; 29812393Syz155240 if (add == m->m_len) { 29822393Syz155240 m = m->m_next; 29832393Syz155240 if (!hlen) { 29842393Syz155240 if (!m) 29852393Syz155240 break; 29862393Syz155240 sp = mtod(m, u_short *); 29872393Syz155240 } 29882393Syz155240 PANIC((!m),("fr_cksum(1): not enough data")); 29892393Syz155240 } 29902393Syz155240 } 29912393Syz155240 } 29922393Syz155240 # endif 29932393Syz155240 29942393Syz155240 len -= (l4hlen + hlen); 29952393Syz155240 if (len <= 0) 29962393Syz155240 goto nodata; 29972393Syz155240 29982393Syz155240 while (len > 1) { 29992393Syz155240 if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) { 30002393Syz155240 m = m->m_next; 30012393Syz155240 PANIC((!m),("fr_cksum(2): not enough data")); 30022393Syz155240 sp = mtod(m, u_short *); 30032393Syz155240 } 30042393Syz155240 if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) { 30052393Syz155240 bytes.c[0] = *(u_char *)sp; 30062393Syz155240 m = m->m_next; 30072393Syz155240 PANIC((!m),("fr_cksum(3): not enough data")); 30082393Syz155240 sp = mtod(m, u_short *); 30092393Syz155240 bytes.c[1] = *(u_char *)sp; 30102393Syz155240 sum += bytes.s; 30112393Syz155240 sp = (u_short *)((u_char *)sp + 1); 30122393Syz155240 } 30132393Syz155240 if ((u_long)sp & 1) { 30142393Syz155240 bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s)); 30152393Syz155240 sum += bytes.s; 30162393Syz155240 } else 30172393Syz155240 sum += *sp++; 30182393Syz155240 len -= 2; 30192393Syz155240 } 30202393Syz155240 30212393Syz155240 if (len != 0) 30222393Syz155240 sum += ntohs(*(u_char *)sp << 8); 30232393Syz155240 nodata: 30242393Syz155240 while (sum > 0xffff) 30252393Syz155240 sum = (sum & 0xffff) + (sum >> 16); 30262393Syz155240 sum2 = (u_short)(~sum & 0xffff); 30272393Syz155240 } 30282393Syz155240 # endif /* defined(BSD) || defined(sun) */ 30292393Syz155240 # endif /* MENTAT */ 30302393Syz155240 #else /* _KERNEL */ 30312393Syz155240 for (; slen > 1; slen -= 2) 30322393Syz155240 sum += *sp++; 30332393Syz155240 if (slen) 30342393Syz155240 sum += ntohs(*(u_char *)sp << 8); 30352393Syz155240 while (sum > 0xffff) 30362393Syz155240 sum = (sum & 0xffff) + (sum >> 16); 30372393Syz155240 sum2 = (u_short)(~sum & 0xffff); 30382393Syz155240 #endif /* _KERNEL */ 30392393Syz155240 if (csump != NULL) 30402393Syz155240 *csump = sumsave; 30412393Syz155240 return sum2; 30422393Syz155240 } 30432393Syz155240 30442393Syz155240 30452393Syz155240 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \ 30462393Syz155240 defined(__sgi) ) && !defined(linux) && !defined(_AIX51) 30472393Syz155240 /* 30482393Syz155240 * Copyright (c) 1982, 1986, 1988, 1991, 1993 30492393Syz155240 * The Regents of the University of California. All rights reserved. 30502393Syz155240 * 30512393Syz155240 * Redistribution and use in source and binary forms, with or without 30522393Syz155240 * modification, are permitted provided that the following conditions 30532393Syz155240 * are met: 30542393Syz155240 * 1. Redistributions of source code must retain the above copyright 30552393Syz155240 * notice, this list of conditions and the following disclaimer. 30562393Syz155240 * 2. Redistributions in binary form must reproduce the above copyright 30572393Syz155240 * notice, this list of conditions and the following disclaimer in the 30582393Syz155240 * documentation and/or other materials provided with the distribution. 30592393Syz155240 * 3. Neither the name of the University nor the names of its contributors 30602393Syz155240 * may be used to endorse or promote products derived from this software 30612393Syz155240 * without specific prior written permission. 30622393Syz155240 * 30632393Syz155240 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30642393Syz155240 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30652393Syz155240 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30662393Syz155240 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30672393Syz155240 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30682393Syz155240 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30692393Syz155240 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30702393Syz155240 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30712393Syz155240 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30722393Syz155240 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30732393Syz155240 * SUCH DAMAGE. 30742393Syz155240 * 30752393Syz155240 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 30762393Syz155240 * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $ 30772393Syz155240 */ 30782393Syz155240 /* 30792393Syz155240 * Copy data from an mbuf chain starting "off" bytes from the beginning, 30802393Syz155240 * continuing for "len" bytes, into the indicated buffer. 30812393Syz155240 */ 30822393Syz155240 void 30832393Syz155240 m_copydata(m, off, len, cp) 30842393Syz155240 mb_t *m; 30852393Syz155240 int off; 30862393Syz155240 int len; 30872393Syz155240 caddr_t cp; 30882393Syz155240 { 30892393Syz155240 unsigned count; 30902393Syz155240 30912393Syz155240 if (off < 0 || len < 0) 30922393Syz155240 panic("m_copydata"); 30932393Syz155240 while (off > 0) { 30942393Syz155240 if (m == 0) 30952393Syz155240 panic("m_copydata"); 30962393Syz155240 if (off < m->m_len) 30972393Syz155240 break; 30982393Syz155240 off -= m->m_len; 30992393Syz155240 m = m->m_next; 31002393Syz155240 } 31012393Syz155240 while (len > 0) { 31022393Syz155240 if (m == 0) 31032393Syz155240 panic("m_copydata"); 31042393Syz155240 count = MIN(m->m_len - off, len); 31052393Syz155240 bcopy(mtod(m, caddr_t) + off, cp, count); 31062393Syz155240 len -= count; 31072393Syz155240 cp += count; 31082393Syz155240 off = 0; 31092393Syz155240 m = m->m_next; 31102393Syz155240 } 31112393Syz155240 } 31122393Syz155240 31132393Syz155240 31142393Syz155240 /* 31152393Syz155240 * Copy data from a buffer back into the indicated mbuf chain, 31162393Syz155240 * starting "off" bytes from the beginning, extending the mbuf 31172393Syz155240 * chain if necessary. 31182393Syz155240 */ 31192393Syz155240 void 31202393Syz155240 m_copyback(m0, off, len, cp) 31212393Syz155240 struct mbuf *m0; 31222393Syz155240 int off; 31232393Syz155240 int len; 31242393Syz155240 caddr_t cp; 31252393Syz155240 { 31262393Syz155240 int mlen; 31272393Syz155240 struct mbuf *m = m0, *n; 31282393Syz155240 int totlen = 0; 31292393Syz155240 31302393Syz155240 if (m0 == 0) 31312393Syz155240 return; 31322393Syz155240 while (off > (mlen = m->m_len)) { 31332393Syz155240 off -= mlen; 31342393Syz155240 totlen += mlen; 31352393Syz155240 if (m->m_next == 0) { 31362393Syz155240 n = m_getclr(M_DONTWAIT, m->m_type); 31372393Syz155240 if (n == 0) 31382393Syz155240 goto out; 31392393Syz155240 n->m_len = min(MLEN, len + off); 31402393Syz155240 m->m_next = n; 31412393Syz155240 } 31422393Syz155240 m = m->m_next; 31432393Syz155240 } 31442393Syz155240 while (len > 0) { 31452393Syz155240 mlen = min(m->m_len - off, len); 31462393Syz155240 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 31472393Syz155240 cp += mlen; 31482393Syz155240 len -= mlen; 31492393Syz155240 mlen += off; 31502393Syz155240 off = 0; 31512393Syz155240 totlen += mlen; 31522393Syz155240 if (len == 0) 31532393Syz155240 break; 31542393Syz155240 if (m->m_next == 0) { 31552393Syz155240 n = m_get(M_DONTWAIT, m->m_type); 31562393Syz155240 if (n == 0) 31572393Syz155240 break; 31582393Syz155240 n->m_len = min(MLEN, len); 31592393Syz155240 m->m_next = n; 31602393Syz155240 } 31612393Syz155240 m = m->m_next; 31622393Syz155240 } 31632393Syz155240 out: 31642393Syz155240 #if 0 31652393Syz155240 if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 31662393Syz155240 m->m_pkthdr.len = totlen; 31672393Syz155240 #endif 31682393Syz155240 return; 31692393Syz155240 } 31702393Syz155240 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */ 31712393Syz155240 31722393Syz155240 31732393Syz155240 /* ------------------------------------------------------------------------ */ 31742393Syz155240 /* Function: fr_findgroup */ 31752393Syz155240 /* Returns: frgroup_t * - NULL = group not found, else pointer to group */ 31762393Syz155240 /* Parameters: group(I) - group name to search for */ 31772393Syz155240 /* unit(I) - device to which this group belongs */ 31782393Syz155240 /* set(I) - which set of rules (inactive/inactive) this is */ 31792393Syz155240 /* fgpp(O) - pointer to place to store pointer to the pointer */ 31802393Syz155240 /* to where to add the next (last) group or where */ 31812393Syz155240 /* to delete group from. */ 31822393Syz155240 /* */ 31832393Syz155240 /* Search amongst the defined groups for a particular group number. */ 31842393Syz155240 /* ------------------------------------------------------------------------ */ 31853448Sdh155122 frgroup_t *fr_findgroup(group, unit, set, fgpp, ifs) 31862393Syz155240 char *group; 31872393Syz155240 minor_t unit; 31882393Syz155240 int set; 31892393Syz155240 frgroup_t ***fgpp; 31903448Sdh155122 ipf_stack_t *ifs; 31912393Syz155240 { 31922393Syz155240 frgroup_t *fg, **fgp; 31932393Syz155240 31942393Syz155240 /* 31952393Syz155240 * Which list of groups to search in is dependent on which list of 31962393Syz155240 * rules are being operated on. 31972393Syz155240 */ 31983448Sdh155122 fgp = &ifs->ifs_ipfgroups[unit][set]; 31992393Syz155240 32002393Syz155240 while ((fg = *fgp) != NULL) { 32012393Syz155240 if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0) 32022393Syz155240 break; 32032393Syz155240 else 32042393Syz155240 fgp = &fg->fg_next; 32052393Syz155240 } 32062393Syz155240 if (fgpp != NULL) 32072393Syz155240 *fgpp = fgp; 32082393Syz155240 return fg; 32092393Syz155240 } 32102393Syz155240 32112393Syz155240 32122393Syz155240 /* ------------------------------------------------------------------------ */ 32132393Syz155240 /* Function: fr_addgroup */ 32142393Syz155240 /* Returns: frgroup_t * - NULL == did not create group, */ 32152393Syz155240 /* != NULL == pointer to the group */ 32162393Syz155240 /* Parameters: num(I) - group number to add */ 32172393Syz155240 /* head(I) - rule pointer that is using this as the head */ 32182393Syz155240 /* flags(I) - rule flags which describe the type of rule it is */ 32192393Syz155240 /* unit(I) - device to which this group will belong to */ 32202393Syz155240 /* set(I) - which set of rules (inactive/inactive) this is */ 32212393Syz155240 /* Write Locks: ipf_mutex */ 32222393Syz155240 /* */ 32232393Syz155240 /* Add a new group head, or if it already exists, increase the reference */ 32242393Syz155240 /* count to it. */ 32252393Syz155240 /* ------------------------------------------------------------------------ */ 32263448Sdh155122 frgroup_t *fr_addgroup(group, head, flags, unit, set, ifs) 32272393Syz155240 char *group; 32282393Syz155240 void *head; 32292393Syz155240 u_32_t flags; 32302393Syz155240 minor_t unit; 32312393Syz155240 int set; 32323448Sdh155122 ipf_stack_t *ifs; 32332393Syz155240 { 32342393Syz155240 frgroup_t *fg, **fgp; 32352393Syz155240 u_32_t gflags; 32362393Syz155240 32372393Syz155240 if (group == NULL) 32382393Syz155240 return NULL; 32392393Syz155240 32402393Syz155240 if (unit == IPL_LOGIPF && *group == '\0') 32412393Syz155240 return NULL; 32422393Syz155240 32432393Syz155240 fgp = NULL; 32442393Syz155240 gflags = flags & FR_INOUT; 32452393Syz155240 32463448Sdh155122 fg = fr_findgroup(group, unit, set, &fgp, ifs); 32472393Syz155240 if (fg != NULL) { 32482393Syz155240 if (fg->fg_flags == 0) 32492393Syz155240 fg->fg_flags = gflags; 32502393Syz155240 else if (gflags != fg->fg_flags) 32512393Syz155240 return NULL; 32522393Syz155240 fg->fg_ref++; 32532393Syz155240 return fg; 32542393Syz155240 } 32552393Syz155240 KMALLOC(fg, frgroup_t *); 32562393Syz155240 if (fg != NULL) { 32572393Syz155240 fg->fg_head = head; 32582393Syz155240 fg->fg_start = NULL; 32592393Syz155240 fg->fg_next = *fgp; 32602393Syz155240 bcopy(group, fg->fg_name, FR_GROUPLEN); 32612393Syz155240 fg->fg_flags = gflags; 32622393Syz155240 fg->fg_ref = 1; 32632393Syz155240 *fgp = fg; 32642393Syz155240 } 32652393Syz155240 return fg; 32662393Syz155240 } 32672393Syz155240 32682393Syz155240 32692393Syz155240 /* ------------------------------------------------------------------------ */ 32702393Syz155240 /* Function: fr_delgroup */ 32712393Syz155240 /* Returns: Nil */ 32722393Syz155240 /* Parameters: group(I) - group name to delete */ 32732393Syz155240 /* unit(I) - device to which this group belongs */ 32742393Syz155240 /* set(I) - which set of rules (inactive/inactive) this is */ 32752393Syz155240 /* Write Locks: ipf_mutex */ 32762393Syz155240 /* */ 32772393Syz155240 /* Attempt to delete a group head. */ 32782393Syz155240 /* Only do this when its reference count reaches 0. */ 32792393Syz155240 /* ------------------------------------------------------------------------ */ 32803448Sdh155122 void fr_delgroup(group, unit, set, ifs) 32812393Syz155240 char *group; 32822393Syz155240 minor_t unit; 32832393Syz155240 int set; 32843448Sdh155122 ipf_stack_t *ifs; 32852393Syz155240 { 32862393Syz155240 frgroup_t *fg, **fgp; 32872393Syz155240 32883448Sdh155122 fg = fr_findgroup(group, unit, set, &fgp, ifs); 32892393Syz155240 if (fg == NULL) 32902393Syz155240 return; 32912393Syz155240 32922393Syz155240 fg->fg_ref--; 32932393Syz155240 if (fg->fg_ref == 0) { 32942393Syz155240 *fgp = fg->fg_next; 32952393Syz155240 KFREE(fg); 32962393Syz155240 } 32972393Syz155240 } 32982393Syz155240 32992393Syz155240 33002393Syz155240 /* ------------------------------------------------------------------------ */ 33012393Syz155240 /* Function: fr_getrulen */ 33022393Syz155240 /* Returns: frentry_t * - NULL == not found, else pointer to rule n */ 33032393Syz155240 /* Parameters: unit(I) - device for which to count the rule's number */ 33042393Syz155240 /* flags(I) - which set of rules to find the rule in */ 33052393Syz155240 /* group(I) - group name */ 33062393Syz155240 /* n(I) - rule number to find */ 33072393Syz155240 /* */ 33082393Syz155240 /* Find rule # n in group # g and return a pointer to it. Return NULl if */ 33092393Syz155240 /* group # g doesn't exist or there are less than n rules in the group. */ 33102393Syz155240 /* ------------------------------------------------------------------------ */ 33113448Sdh155122 frentry_t *fr_getrulen(unit, group, n, ifs) 33122393Syz155240 int unit; 33132393Syz155240 char *group; 33142393Syz155240 u_32_t n; 33153448Sdh155122 ipf_stack_t *ifs; 33162393Syz155240 { 33172393Syz155240 frentry_t *fr; 33182393Syz155240 frgroup_t *fg; 33192393Syz155240 33203448Sdh155122 fg = fr_findgroup(group, unit, ifs->ifs_fr_active, NULL, ifs); 33212393Syz155240 if (fg == NULL) 33222393Syz155240 return NULL; 33232393Syz155240 for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--) 33242393Syz155240 ; 33252393Syz155240 if (n != 0) 33262393Syz155240 return NULL; 33272393Syz155240 return fr; 33282393Syz155240 } 33292393Syz155240 33302393Syz155240 33312393Syz155240 /* ------------------------------------------------------------------------ */ 33322393Syz155240 /* Function: fr_rulen */ 33332393Syz155240 /* Returns: int - >= 0 - rule number, -1 == search failed */ 33342393Syz155240 /* Parameters: unit(I) - device for which to count the rule's number */ 33352393Syz155240 /* fr(I) - pointer to rule to match */ 33362393Syz155240 /* */ 33372393Syz155240 /* Return the number for a rule on a specific filtering device. */ 33382393Syz155240 /* ------------------------------------------------------------------------ */ 33393448Sdh155122 int fr_rulen(unit, fr, ifs) 33402393Syz155240 int unit; 33412393Syz155240 frentry_t *fr; 33423448Sdh155122 ipf_stack_t *ifs; 33432393Syz155240 { 33442393Syz155240 frentry_t *fh; 33452393Syz155240 frgroup_t *fg; 33462393Syz155240 u_32_t n = 0; 33472393Syz155240 33482393Syz155240 if (fr == NULL) 33492393Syz155240 return -1; 33503448Sdh155122 fg = fr_findgroup(fr->fr_group, unit, ifs->ifs_fr_active, NULL, ifs); 33512393Syz155240 if (fg == NULL) 33522393Syz155240 return -1; 33532393Syz155240 for (fh = fg->fg_head; fh; n++, fh = fh->fr_next) 33542393Syz155240 if (fh == fr) 33552393Syz155240 break; 33562393Syz155240 if (fh == NULL) 33572393Syz155240 return -1; 33582393Syz155240 return n; 33592393Syz155240 } 33602393Syz155240 33612393Syz155240 33622393Syz155240 /* ------------------------------------------------------------------------ */ 33632393Syz155240 /* Function: frflushlist */ 33642393Syz155240 /* Returns: int - >= 0 - number of flushed rules */ 33652393Syz155240 /* Parameters: set(I) - which set of rules (inactive/inactive) this is */ 33662393Syz155240 /* unit(I) - device for which to flush rules */ 33672393Syz155240 /* flags(I) - which set of rules to flush */ 33682393Syz155240 /* nfreedp(O) - pointer to int where flush count is stored */ 33692393Syz155240 /* listp(I) - pointer to list to flush pointer */ 33702393Syz155240 /* Write Locks: ipf_mutex */ 33712393Syz155240 /* */ 33722393Syz155240 /* Recursively flush rules from the list, descending groups as they are */ 33732393Syz155240 /* encountered. if a rule is the head of a group and it has lost all its */ 33742393Syz155240 /* group members, then also delete the group reference. nfreedp is needed */ 33752393Syz155240 /* to store the accumulating count of rules removed, whereas the returned */ 33762393Syz155240 /* value is just the number removed from the current list. The latter is */ 33772393Syz155240 /* needed to correctly adjust reference counts on rules that define groups. */ 33782393Syz155240 /* */ 33792393Syz155240 /* NOTE: Rules not loaded from user space cannot be flushed. */ 33802393Syz155240 /* ------------------------------------------------------------------------ */ 33813448Sdh155122 static int frflushlist(set, unit, nfreedp, listp, ifs) 33822393Syz155240 int set; 33832393Syz155240 minor_t unit; 33842393Syz155240 int *nfreedp; 33852393Syz155240 frentry_t **listp; 33863448Sdh155122 ipf_stack_t *ifs; 33872393Syz155240 { 33882393Syz155240 int freed = 0, i; 33892393Syz155240 frentry_t *fp; 33902393Syz155240 33912393Syz155240 while ((fp = *listp) != NULL) { 33922393Syz155240 if ((fp->fr_type & FR_T_BUILTIN) || 33932393Syz155240 !(fp->fr_flags & FR_COPIED)) { 33942393Syz155240 listp = &fp->fr_next; 33952393Syz155240 continue; 33962393Syz155240 } 33972393Syz155240 *listp = fp->fr_next; 33982393Syz155240 if (fp->fr_grp != NULL) { 33993448Sdh155122 i = frflushlist(set, unit, nfreedp, fp->fr_grp, ifs); 34002393Syz155240 fp->fr_ref -= i; 34012393Syz155240 } 34022393Syz155240 34032393Syz155240 if (fp->fr_grhead != NULL) { 34043448Sdh155122 fr_delgroup(fp->fr_grhead, unit, set, ifs); 34052393Syz155240 *fp->fr_grhead = '\0'; 34062393Syz155240 } 34072393Syz155240 34082393Syz155240 ASSERT(fp->fr_ref > 0); 34092393Syz155240 fp->fr_next = NULL; 34103448Sdh155122 if (fr_derefrule(&fp, ifs) == 0) 34112393Syz155240 freed++; 34122393Syz155240 } 34132393Syz155240 *nfreedp += freed; 34142393Syz155240 return freed; 34152393Syz155240 } 34162393Syz155240 34172393Syz155240 34182393Syz155240 /* ------------------------------------------------------------------------ */ 34192393Syz155240 /* Function: frflush */ 34202393Syz155240 /* Returns: int - >= 0 - number of flushed rules */ 34212393Syz155240 /* Parameters: unit(I) - device for which to flush rules */ 34222393Syz155240 /* flags(I) - which set of rules to flush */ 34232393Syz155240 /* */ 34242393Syz155240 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */ 34252393Syz155240 /* and IPv6) as defined by the value of flags. */ 34262393Syz155240 /* ------------------------------------------------------------------------ */ 34273448Sdh155122 int frflush(unit, proto, flags, ifs) 34282393Syz155240 minor_t unit; 34292393Syz155240 int proto, flags; 34303448Sdh155122 ipf_stack_t *ifs; 34312393Syz155240 { 34322393Syz155240 int flushed = 0, set; 34332393Syz155240 34343448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_mutex); 34353448Sdh155122 bzero((char *)&ifs->ifs_frcache, sizeof (ifs->ifs_frcache)); 34363448Sdh155122 34373448Sdh155122 set = ifs->ifs_fr_active; 34382393Syz155240 if ((flags & FR_INACTIVE) == FR_INACTIVE) 34392393Syz155240 set = 1 - set; 34402393Syz155240 34412393Syz155240 if (flags & FR_OUTQUE) { 34422393Syz155240 if (proto == 0 || proto == 6) { 34432393Syz155240 (void) frflushlist(set, unit, 34443448Sdh155122 &flushed, &ifs->ifs_ipfilter6[1][set], ifs); 34452393Syz155240 (void) frflushlist(set, unit, 34463448Sdh155122 &flushed, &ifs->ifs_ipacct6[1][set], ifs); 34472393Syz155240 } 34482393Syz155240 if (proto == 0 || proto == 4) { 34492393Syz155240 (void) frflushlist(set, unit, 34503448Sdh155122 &flushed, &ifs->ifs_ipfilter[1][set], ifs); 34512393Syz155240 (void) frflushlist(set, unit, 34523448Sdh155122 &flushed, &ifs->ifs_ipacct[1][set], ifs); 34532393Syz155240 } 34542393Syz155240 } 34552393Syz155240 if (flags & FR_INQUE) { 34562393Syz155240 if (proto == 0 || proto == 6) { 34572393Syz155240 (void) frflushlist(set, unit, 34583448Sdh155122 &flushed, &ifs->ifs_ipfilter6[0][set], ifs); 34592393Syz155240 (void) frflushlist(set, unit, 34603448Sdh155122 &flushed, &ifs->ifs_ipacct6[0][set], ifs); 34612393Syz155240 } 34622393Syz155240 if (proto == 0 || proto == 4) { 34632393Syz155240 (void) frflushlist(set, unit, 34643448Sdh155122 &flushed, &ifs->ifs_ipfilter[0][set], ifs); 34652393Syz155240 (void) frflushlist(set, unit, 34663448Sdh155122 &flushed, &ifs->ifs_ipacct[0][set], ifs); 34672393Syz155240 } 34682393Syz155240 } 34693448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 34702393Syz155240 34712393Syz155240 if (unit == IPL_LOGIPF) { 34722393Syz155240 int tmp; 34732393Syz155240 34743448Sdh155122 tmp = frflush(IPL_LOGCOUNT, proto, flags, ifs); 34752393Syz155240 if (tmp >= 0) 34762393Syz155240 flushed += tmp; 34772393Syz155240 } 34782393Syz155240 return flushed; 34792393Syz155240 } 34802393Syz155240 34812393Syz155240 34822393Syz155240 /* ------------------------------------------------------------------------ */ 34832393Syz155240 /* Function: memstr */ 34842393Syz155240 /* Returns: char * - NULL if failed, != NULL pointer to matching bytes */ 34852393Syz155240 /* Parameters: src(I) - pointer to byte sequence to match */ 34862393Syz155240 /* dst(I) - pointer to byte sequence to search */ 34872393Syz155240 /* slen(I) - match length */ 34882393Syz155240 /* dlen(I) - length available to search in */ 34892393Syz155240 /* */ 34902393Syz155240 /* Search dst for a sequence of bytes matching those at src and extend for */ 34912393Syz155240 /* slen bytes. */ 34922393Syz155240 /* ------------------------------------------------------------------------ */ 34932393Syz155240 char *memstr(src, dst, slen, dlen) 34942393Syz155240 char *src, *dst; 34952393Syz155240 int slen, dlen; 34962393Syz155240 { 34972393Syz155240 char *s = NULL; 34982393Syz155240 34992393Syz155240 while (dlen >= slen) { 35002393Syz155240 if (bcmp(src, dst, slen) == 0) { 35012393Syz155240 s = dst; 35022393Syz155240 break; 35032393Syz155240 } 35042393Syz155240 dst++; 35052393Syz155240 dlen--; 35062393Syz155240 } 35072393Syz155240 return s; 35082393Syz155240 } 35092393Syz155240 /* ------------------------------------------------------------------------ */ 35102393Syz155240 /* Function: fr_fixskip */ 35112393Syz155240 /* Returns: Nil */ 35122393Syz155240 /* Parameters: listp(IO) - pointer to start of list with skip rule */ 35132393Syz155240 /* rp(I) - rule added/removed with skip in it. */ 35142393Syz155240 /* addremove(I) - adjustment (-1/+1) to make to skip count, */ 35152393Syz155240 /* depending on whether a rule was just added */ 35162393Syz155240 /* or removed. */ 35172393Syz155240 /* */ 35182393Syz155240 /* Adjust all the rules in a list which would have skip'd past the position */ 35192393Syz155240 /* where we are inserting to skip to the right place given the change. */ 35202393Syz155240 /* ------------------------------------------------------------------------ */ 35212393Syz155240 void fr_fixskip(listp, rp, addremove) 35222393Syz155240 frentry_t **listp, *rp; 35232393Syz155240 int addremove; 35242393Syz155240 { 35252393Syz155240 int rules, rn; 35262393Syz155240 frentry_t *fp; 35272393Syz155240 35282393Syz155240 rules = 0; 35292393Syz155240 for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next) 35302393Syz155240 rules++; 35312393Syz155240 35322393Syz155240 if (!fp) 35332393Syz155240 return; 35342393Syz155240 35352393Syz155240 for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++) 35362393Syz155240 if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules)) 35372393Syz155240 fp->fr_arg += addremove; 35382393Syz155240 } 35392393Syz155240 35402393Syz155240 35412393Syz155240 #ifdef _KERNEL 35422393Syz155240 /* ------------------------------------------------------------------------ */ 35432393Syz155240 /* Function: count4bits */ 35442393Syz155240 /* Returns: int - >= 0 - number of consecutive bits in input */ 35452393Syz155240 /* Parameters: ip(I) - 32bit IP address */ 35462393Syz155240 /* */ 35472393Syz155240 /* IPv4 ONLY */ 35482393Syz155240 /* count consecutive 1's in bit mask. If the mask generated by counting */ 35492393Syz155240 /* consecutive 1's is different to that passed, return -1, else return # */ 35502393Syz155240 /* of bits. */ 35512393Syz155240 /* ------------------------------------------------------------------------ */ 35522393Syz155240 int count4bits(ip) 35532393Syz155240 u_32_t ip; 35542393Syz155240 { 35552393Syz155240 u_32_t ipn; 35562393Syz155240 int cnt = 0, i, j; 35572393Syz155240 35582393Syz155240 ip = ipn = ntohl(ip); 35592393Syz155240 for (i = 32; i; i--, ipn *= 2) 35602393Syz155240 if (ipn & 0x80000000) 35612393Syz155240 cnt++; 35622393Syz155240 else 35632393Syz155240 break; 35642393Syz155240 ipn = 0; 35652393Syz155240 for (i = 32, j = cnt; i; i--, j--) { 35662393Syz155240 ipn *= 2; 35672393Syz155240 if (j > 0) 35682393Syz155240 ipn++; 35692393Syz155240 } 35702393Syz155240 if (ipn == ip) 35712393Syz155240 return cnt; 35722393Syz155240 return -1; 35732393Syz155240 } 35742393Syz155240 35752393Syz155240 35762393Syz155240 #ifdef USE_INET6 35772393Syz155240 /* ------------------------------------------------------------------------ */ 35782393Syz155240 /* Function: count6bits */ 35792393Syz155240 /* Returns: int - >= 0 - number of consecutive bits in input */ 35802393Syz155240 /* Parameters: msk(I) - pointer to start of IPv6 bitmask */ 35812393Syz155240 /* */ 35822393Syz155240 /* IPv6 ONLY */ 35832393Syz155240 /* count consecutive 1's in bit mask. */ 35842393Syz155240 /* ------------------------------------------------------------------------ */ 35852393Syz155240 int count6bits(msk) 35862393Syz155240 u_32_t *msk; 35872393Syz155240 { 35882393Syz155240 int i = 0, k; 35892393Syz155240 u_32_t j; 35902393Syz155240 35912393Syz155240 for (k = 3; k >= 0; k--) 35922393Syz155240 if (msk[k] == 0xffffffff) 35932393Syz155240 i += 32; 35942393Syz155240 else { 35952393Syz155240 for (j = msk[k]; j; j <<= 1) 35962393Syz155240 if (j & 0x80000000) 35972393Syz155240 i++; 35982393Syz155240 } 35992393Syz155240 return i; 36002393Syz155240 } 36012393Syz155240 # endif 36022393Syz155240 #endif /* _KERNEL */ 36032393Syz155240 36042393Syz155240 36052393Syz155240 /* ------------------------------------------------------------------------ */ 36062958Sdr146992 /* Function: fr_ifsync */ 36072958Sdr146992 /* Returns: void * - new interface identifier */ 36082958Sdr146992 /* Parameters: action(I) - type of synchronisation to do */ 36092958Sdr146992 /* v(I) - IP version being sync'd (v4 or v6) */ 36102958Sdr146992 /* newifp(I) - interface identifier being introduced/removed */ 36112958Sdr146992 /* oldifp(I) - interface identifier in a filter rule */ 36122958Sdr146992 /* newname(I) - name associated with oldifp interface */ 36132958Sdr146992 /* oldname(I) - name associated with newifp interface */ 36142958Sdr146992 /* */ 36152958Sdr146992 /* This function returns what the new value for "oldifp" should be for its */ 36162958Sdr146992 /* caller. In some cases it will not change, in some it will. */ 36172958Sdr146992 /* action == IPFSYNC_RESYNC */ 36182958Sdr146992 /* a new value for oldifp will always be looked up, according to oldname, */ 36192958Sdr146992 /* the values of newname and newifp are ignored. */ 36202958Sdr146992 /* action == IPFSYNC_NEWIFP */ 36212958Sdr146992 /* if oldname matches newname then we are doing a sync for the matching */ 36222958Sdr146992 /* interface, so we return newifp to be used in place of oldifp. If the */ 36232958Sdr146992 /* the names don't match, just return oldifp. */ 36242958Sdr146992 /* action == IPFSYNC_OLDIFP */ 36252958Sdr146992 /* if oldifp matches newifp then we are are doing a sync to remove any */ 36262958Sdr146992 /* references to oldifp, so we return "-1". */ 36272958Sdr146992 /* ------------------------------------------------------------------------ */ 36283448Sdh155122 static void *fr_ifsync(action, v, newname, oldname, newifp, oldifp, ifs) 36292958Sdr146992 int action, v; 36302958Sdr146992 char *newname, *oldname; 36312958Sdr146992 void *newifp, *oldifp; 36323448Sdh155122 ipf_stack_t *ifs; 36332958Sdr146992 { 36342958Sdr146992 void *rval = oldifp; 36352958Sdr146992 36362958Sdr146992 switch (action) 36372958Sdr146992 { 36382958Sdr146992 case IPFSYNC_RESYNC : 36392958Sdr146992 if (oldname[0] != '\0') { 36403448Sdh155122 rval = fr_resolvenic(oldname, v, ifs); 36412958Sdr146992 } 36422958Sdr146992 break; 36432958Sdr146992 case IPFSYNC_NEWIFP : 36442958Sdr146992 if (!strncmp(newname, oldname, LIFNAMSIZ)) 36452958Sdr146992 rval = newifp; 36462958Sdr146992 break; 36472958Sdr146992 case IPFSYNC_OLDIFP : 36482958Sdr146992 if (newifp == oldifp) 36492958Sdr146992 rval = (void *)-1; 36502958Sdr146992 break; 36512958Sdr146992 } 36522958Sdr146992 36532958Sdr146992 return rval; 36542958Sdr146992 } 36552958Sdr146992 36562958Sdr146992 36572958Sdr146992 /* ------------------------------------------------------------------------ */ 36582393Syz155240 /* Function: frsynclist */ 36592393Syz155240 /* Returns: void */ 36602958Sdr146992 /* Parameters: action(I) - type of synchronisation to do */ 36612958Sdr146992 /* v(I) - IP version being sync'd (v4 or v6) */ 36622958Sdr146992 /* ifp(I) - interface identifier associated with action */ 36632958Sdr146992 /* name(I) - name associated with ifp parameter */ 36642393Syz155240 /* Write Locks: ipf_mutex */ 36652393Syz155240 /* */ 36662393Syz155240 /* Walk through a list of filter rules and resolve any interface names into */ 36672393Syz155240 /* pointers. Where dynamic addresses are used, also update the IP address */ 36682393Syz155240 /* used in the rule. The interface pointer is used to limit the lookups to */ 36692393Syz155240 /* a specific set of matching names if it is non-NULL. */ 36702393Syz155240 /* ------------------------------------------------------------------------ */ 36713448Sdh155122 static void frsynclist(action, v, ifp, ifname, fr, ifs) 36722958Sdr146992 int action, v; 36732958Sdr146992 void *ifp; 36742958Sdr146992 char *ifname; 36752393Syz155240 frentry_t *fr; 36763448Sdh155122 ipf_stack_t *ifs; 36772393Syz155240 { 36782393Syz155240 frdest_t *fdp; 36792958Sdr146992 int rv, i; 36802393Syz155240 36812393Syz155240 for (; fr; fr = fr->fr_next) { 36822958Sdr146992 rv = fr->fr_v; 36832958Sdr146992 if (v != 0 && v != rv) 36842958Sdr146992 continue; 36852393Syz155240 36862393Syz155240 /* 36872393Syz155240 * Lookup all the interface names that are part of the rule. 36882393Syz155240 */ 36892393Syz155240 for (i = 0; i < 4; i++) { 36902958Sdr146992 fr->fr_ifas[i] = fr_ifsync(action, rv, ifname, 36912958Sdr146992 fr->fr_ifnames[i], 36923448Sdh155122 ifp, fr->fr_ifas[i], 36933448Sdh155122 ifs); 36942393Syz155240 } 36952393Syz155240 36962958Sdr146992 fdp = &fr->fr_tifs[0]; 36972958Sdr146992 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 36983448Sdh155122 ifp, fdp->fd_ifp, ifs); 36992958Sdr146992 37002958Sdr146992 fdp = &fr->fr_tifs[1]; 37012958Sdr146992 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 37023448Sdh155122 ifp, fdp->fd_ifp, ifs); 37032958Sdr146992 37042958Sdr146992 fdp = &fr->fr_dif; 37052958Sdr146992 fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname, 37063448Sdh155122 ifp, fdp->fd_ifp, ifs); 37072958Sdr146992 37082958Sdr146992 if (action != IPFSYNC_RESYNC) 3709*4118Sdr146992 continue; 37102958Sdr146992 37112393Syz155240 if (fr->fr_type == FR_T_IPF) { 37122393Syz155240 if (fr->fr_satype != FRI_NORMAL && 37132393Syz155240 fr->fr_satype != FRI_LOOKUP) { 37142958Sdr146992 (void)fr_ifpaddr(rv, fr->fr_satype, 37152393Syz155240 fr->fr_ifas[fr->fr_sifpidx], 37163448Sdh155122 &fr->fr_src, &fr->fr_smsk, 37173448Sdh155122 ifs); 37182393Syz155240 } 37192393Syz155240 if (fr->fr_datype != FRI_NORMAL && 37202393Syz155240 fr->fr_datype != FRI_LOOKUP) { 37212958Sdr146992 (void)fr_ifpaddr(rv, fr->fr_datype, 37222393Syz155240 fr->fr_ifas[fr->fr_difpidx], 37233448Sdh155122 &fr->fr_dst, &fr->fr_dmsk, 37243448Sdh155122 ifs); 37252393Syz155240 } 37262393Syz155240 } 37272393Syz155240 37282393Syz155240 #ifdef IPFILTER_LOOKUP 37292393Syz155240 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP && 37302393Syz155240 fr->fr_srcptr == NULL) { 37312393Syz155240 fr->fr_srcptr = fr_resolvelookup(fr->fr_srctype, 37322393Syz155240 fr->fr_srcnum, 37333448Sdh155122 &fr->fr_srcfunc, ifs); 37342393Syz155240 } 37352393Syz155240 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP && 37362393Syz155240 fr->fr_dstptr == NULL) { 37372393Syz155240 fr->fr_dstptr = fr_resolvelookup(fr->fr_dsttype, 37382393Syz155240 fr->fr_dstnum, 37393448Sdh155122 &fr->fr_dstfunc, ifs); 37402393Syz155240 } 37412393Syz155240 #endif 37422393Syz155240 } 37432393Syz155240 } 37442393Syz155240 37452393Syz155240 37462393Syz155240 #ifdef _KERNEL 37472393Syz155240 /* ------------------------------------------------------------------------ */ 37482393Syz155240 /* Function: frsync */ 37492393Syz155240 /* Returns: void */ 37502958Sdr146992 /* Parameters: action(I) - type of synchronisation to do */ 37512958Sdr146992 /* v(I) - IP version being sync'd (v4 or v6) */ 37522958Sdr146992 /* ifp(I) - interface identifier associated with action */ 37532958Sdr146992 /* name(I) - name associated with ifp parameter */ 37542393Syz155240 /* */ 37552393Syz155240 /* frsync() is called when we suspect that the interface list or */ 37562393Syz155240 /* information about interfaces (like IP#) has changed. Go through all */ 37572393Syz155240 /* filter rules, NAT entries and the state table and check if anything */ 37582393Syz155240 /* needs to be changed/updated. */ 37592958Sdr146992 /* With the filtering hooks added to Solaris, we needed to change the manner*/ 37602958Sdr146992 /* in which this was done to support three different types of sync: */ 37612958Sdr146992 /* - complete resync of all interface name/identifiers */ 37622958Sdr146992 /* - new interface being announced with its name and identifier */ 37632958Sdr146992 /* - interface removal being announced by only its identifier */ 37642958Sdr146992 /* ------------------------------------------------------------------------ */ 37653448Sdh155122 void frsync(action, v, ifp, name, ifs) 37662958Sdr146992 int action, v; 37672393Syz155240 void *ifp; 37682958Sdr146992 char *name; 37693448Sdh155122 ipf_stack_t *ifs; 37702393Syz155240 { 37712393Syz155240 int i; 37722393Syz155240 37733448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_mutex); 37743448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipacct[0][ifs->ifs_fr_active], ifs); 37753448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipacct[1][ifs->ifs_fr_active], ifs); 37763448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[0][ifs->ifs_fr_active], ifs); 37773448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[1][ifs->ifs_fr_active], ifs); 37783448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[0][ifs->ifs_fr_active], ifs); 37793448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[1][ifs->ifs_fr_active], ifs); 37803448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[0][ifs->ifs_fr_active], ifs); 37813448Sdh155122 frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[1][ifs->ifs_fr_active], ifs); 37822393Syz155240 37832393Syz155240 for (i = 0; i < IPL_LOGSIZE; i++) { 37842393Syz155240 frgroup_t *g; 37852393Syz155240 37863448Sdh155122 for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next) 37873448Sdh155122 frsynclist(action, v, ifp, name, g->fg_start, ifs); 37883448Sdh155122 for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next) 37893448Sdh155122 frsynclist(action, v, ifp, name, g->fg_start, ifs); 37902393Syz155240 } 37913448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 37922393Syz155240 } 37932393Syz155240 37942393Syz155240 37952393Syz155240 /* 37962393Syz155240 * In the functions below, bcopy() is called because the pointer being 37972393Syz155240 * copied _from_ in this instance is a pointer to a char buf (which could 37982393Syz155240 * end up being unaligned) and on the kernel's local stack. 37992393Syz155240 */ 38002393Syz155240 /* ------------------------------------------------------------------------ */ 38012393Syz155240 /* Function: copyinptr */ 38022393Syz155240 /* Returns: int - 0 = success, else failure */ 38032393Syz155240 /* Parameters: src(I) - pointer to the source address */ 38042393Syz155240 /* dst(I) - destination address */ 38052393Syz155240 /* size(I) - number of bytes to copy */ 38062393Syz155240 /* */ 38072393Syz155240 /* Copy a block of data in from user space, given a pointer to the pointer */ 38082393Syz155240 /* to start copying from (src) and a pointer to where to store it (dst). */ 38092393Syz155240 /* NB: src - pointer to user space pointer, dst - kernel space pointer */ 38102393Syz155240 /* ------------------------------------------------------------------------ */ 38112393Syz155240 int copyinptr(src, dst, size) 38122393Syz155240 void *src, *dst; 38132393Syz155240 size_t size; 38142393Syz155240 { 38152393Syz155240 caddr_t ca; 38162393Syz155240 int err; 38172393Syz155240 38182393Syz155240 # if SOLARIS 38192393Syz155240 err = COPYIN(src, (caddr_t)&ca, sizeof(ca)); 38202393Syz155240 if (err != 0) 38212393Syz155240 return err; 38222393Syz155240 # else 38232393Syz155240 bcopy(src, (caddr_t)&ca, sizeof(ca)); 38242393Syz155240 # endif 38252393Syz155240 err = COPYIN(ca, dst, size); 38262393Syz155240 return err; 38272393Syz155240 } 38282393Syz155240 38292393Syz155240 38302393Syz155240 /* ------------------------------------------------------------------------ */ 38312393Syz155240 /* Function: copyoutptr */ 38322393Syz155240 /* Returns: int - 0 = success, else failure */ 38332393Syz155240 /* Parameters: src(I) - pointer to the source address */ 38342393Syz155240 /* dst(I) - destination address */ 38352393Syz155240 /* size(I) - number of bytes to copy */ 38362393Syz155240 /* */ 38372393Syz155240 /* Copy a block of data out to user space, given a pointer to the pointer */ 38382393Syz155240 /* to start copying from (src) and a pointer to where to store it (dst). */ 38392393Syz155240 /* NB: src - kernel space pointer, dst - pointer to user space pointer. */ 38402393Syz155240 /* ------------------------------------------------------------------------ */ 38412393Syz155240 int copyoutptr(src, dst, size) 38422393Syz155240 void *src, *dst; 38432393Syz155240 size_t size; 38442393Syz155240 { 38452393Syz155240 caddr_t ca; 38462393Syz155240 int err; 38472393Syz155240 38482393Syz155240 # if SOLARIS 38492393Syz155240 err = COPYIN(dst, (caddr_t)&ca, sizeof(ca)); 38502393Syz155240 if (err != 0) 38512393Syz155240 return err; 38522393Syz155240 # else 38532393Syz155240 bcopy(dst, (caddr_t)&ca, sizeof(ca)); 38542393Syz155240 # endif 38552393Syz155240 err = COPYOUT(src, ca, size); 38562393Syz155240 return err; 38572393Syz155240 } 38582393Syz155240 #endif 38592393Syz155240 38602393Syz155240 38612393Syz155240 /* ------------------------------------------------------------------------ */ 38622393Syz155240 /* Function: fr_lock */ 38632393Syz155240 /* Returns: (void) */ 38642393Syz155240 /* Parameters: data(I) - pointer to lock value to set */ 38652393Syz155240 /* lockp(O) - pointer to location to store old lock value */ 38662393Syz155240 /* */ 38672393Syz155240 /* Get the new value for the lock integer, set it and return the old value */ 38682393Syz155240 /* in *lockp. */ 38692393Syz155240 /* ------------------------------------------------------------------------ */ 38702393Syz155240 void fr_lock(data, lockp) 38712393Syz155240 caddr_t data; 38722393Syz155240 int *lockp; 38732393Syz155240 { 38742393Syz155240 int arg; 38752393Syz155240 38762393Syz155240 BCOPYIN(data, (caddr_t)&arg, sizeof(arg)); 38772393Syz155240 BCOPYOUT((caddr_t)lockp, data, sizeof(*lockp)); 38782393Syz155240 *lockp = arg; 38792393Syz155240 } 38802393Syz155240 38812393Syz155240 38822393Syz155240 /* ------------------------------------------------------------------------ */ 38832393Syz155240 /* Function: fr_getstat */ 38842393Syz155240 /* Returns: Nil */ 38852393Syz155240 /* Parameters: fiop(I) - pointer to ipfilter stats structure */ 38862393Syz155240 /* */ 38872393Syz155240 /* Stores a copy of current pointers, counters, etc, in the friostat */ 38882393Syz155240 /* structure. */ 38892393Syz155240 /* ------------------------------------------------------------------------ */ 38903448Sdh155122 void fr_getstat(fiop, ifs) 38912393Syz155240 friostat_t *fiop; 38923448Sdh155122 ipf_stack_t *ifs; 38932393Syz155240 { 38942393Syz155240 int i, j; 38952393Syz155240 38963448Sdh155122 bcopy((char *)&ifs->ifs_frstats, (char *)fiop->f_st, 38973448Sdh155122 sizeof(filterstats_t) * 2); 38983448Sdh155122 fiop->f_locks[IPL_LOGSTATE] = ifs->ifs_fr_state_lock; 38993448Sdh155122 fiop->f_locks[IPL_LOGNAT] = ifs->ifs_fr_nat_lock; 39003448Sdh155122 fiop->f_locks[IPL_LOGIPF] = ifs->ifs_fr_frag_lock; 39013448Sdh155122 fiop->f_locks[IPL_LOGAUTH] = ifs->ifs_fr_auth_lock; 39022393Syz155240 39032393Syz155240 for (i = 0; i < 2; i++) 39042393Syz155240 for (j = 0; j < 2; j++) { 39053448Sdh155122 fiop->f_ipf[i][j] = ifs->ifs_ipfilter[i][j]; 39063448Sdh155122 fiop->f_acct[i][j] = ifs->ifs_ipacct[i][j]; 39073448Sdh155122 fiop->f_ipf6[i][j] = ifs->ifs_ipfilter6[i][j]; 39083448Sdh155122 fiop->f_acct6[i][j] = ifs->ifs_ipacct6[i][j]; 39092393Syz155240 } 39102393Syz155240 39113448Sdh155122 fiop->f_ticks = ifs->ifs_fr_ticks; 39123448Sdh155122 fiop->f_active = ifs->ifs_fr_active; 39133448Sdh155122 fiop->f_froute[0] = ifs->ifs_fr_frouteok[0]; 39143448Sdh155122 fiop->f_froute[1] = ifs->ifs_fr_frouteok[1]; 39153448Sdh155122 39163448Sdh155122 fiop->f_running = ifs->ifs_fr_running; 39172393Syz155240 for (i = 0; i < IPL_LOGSIZE; i++) { 39183448Sdh155122 fiop->f_groups[i][0] = ifs->ifs_ipfgroups[i][0]; 39193448Sdh155122 fiop->f_groups[i][1] = ifs->ifs_ipfgroups[i][1]; 39202393Syz155240 } 39212393Syz155240 #ifdef IPFILTER_LOG 39222393Syz155240 fiop->f_logging = 1; 39232393Syz155240 #else 39242393Syz155240 fiop->f_logging = 0; 39252393Syz155240 #endif 39263448Sdh155122 fiop->f_defpass = ifs->ifs_fr_pass; 39272393Syz155240 fiop->f_features = fr_features; 39282393Syz155240 (void) strncpy(fiop->f_version, ipfilter_version, 39292393Syz155240 sizeof(fiop->f_version)); 39302393Syz155240 } 39312393Syz155240 39322393Syz155240 39332393Syz155240 #ifdef USE_INET6 39342393Syz155240 int icmptoicmp6types[ICMP_MAXTYPE+1] = { 39352393Syz155240 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */ 39362393Syz155240 -1, /* 1: UNUSED */ 39372393Syz155240 -1, /* 2: UNUSED */ 39382393Syz155240 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */ 39392393Syz155240 -1, /* 4: ICMP_SOURCEQUENCH */ 39402393Syz155240 ND_REDIRECT, /* 5: ICMP_REDIRECT */ 39412393Syz155240 -1, /* 6: UNUSED */ 39422393Syz155240 -1, /* 7: UNUSED */ 39432393Syz155240 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */ 39442393Syz155240 -1, /* 9: UNUSED */ 39452393Syz155240 -1, /* 10: UNUSED */ 39462393Syz155240 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */ 39472393Syz155240 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */ 39482393Syz155240 -1, /* 13: ICMP_TSTAMP */ 39492393Syz155240 -1, /* 14: ICMP_TSTAMPREPLY */ 39502393Syz155240 -1, /* 15: ICMP_IREQ */ 39512393Syz155240 -1, /* 16: ICMP_IREQREPLY */ 39522393Syz155240 -1, /* 17: ICMP_MASKREQ */ 39532393Syz155240 -1, /* 18: ICMP_MASKREPLY */ 39542393Syz155240 }; 39552393Syz155240 39562393Syz155240 39572393Syz155240 int icmptoicmp6unreach[ICMP_MAX_UNREACH] = { 39582393Syz155240 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */ 39592393Syz155240 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */ 39602393Syz155240 -1, /* 2: ICMP_UNREACH_PROTOCOL */ 39612393Syz155240 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */ 39622393Syz155240 -1, /* 4: ICMP_UNREACH_NEEDFRAG */ 39632393Syz155240 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */ 39642393Syz155240 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */ 39652393Syz155240 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */ 39662393Syz155240 -1, /* 8: ICMP_UNREACH_ISOLATED */ 39672393Syz155240 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */ 39682393Syz155240 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */ 39692393Syz155240 -1, /* 11: ICMP_UNREACH_TOSNET */ 39702393Syz155240 -1, /* 12: ICMP_UNREACH_TOSHOST */ 39712393Syz155240 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */ 39722393Syz155240 }; 39732393Syz155240 int icmpreplytype6[ICMP6_MAXTYPE + 1]; 39742393Syz155240 #endif 39752393Syz155240 39762393Syz155240 int icmpreplytype4[ICMP_MAXTYPE + 1]; 39772393Syz155240 39782393Syz155240 39792393Syz155240 /* ------------------------------------------------------------------------ */ 39802393Syz155240 /* Function: fr_matchicmpqueryreply */ 39812393Syz155240 /* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */ 39822393Syz155240 /* Parameters: v(I) - IP protocol version (4 or 6) */ 39832393Syz155240 /* ic(I) - ICMP information */ 39842393Syz155240 /* icmp(I) - ICMP packet header */ 39852393Syz155240 /* rev(I) - direction (0 = forward/1 = reverse) of packet */ 39862393Syz155240 /* */ 39872393Syz155240 /* Check if the ICMP packet defined by the header pointed to by icmp is a */ 39882393Syz155240 /* reply to one as described by what's in ic. If it is a match, return 1, */ 39892393Syz155240 /* else return 0 for no match. */ 39902393Syz155240 /* ------------------------------------------------------------------------ */ 39912393Syz155240 int fr_matchicmpqueryreply(v, ic, icmp, rev) 39922393Syz155240 int v; 39932393Syz155240 icmpinfo_t *ic; 39942393Syz155240 icmphdr_t *icmp; 39952393Syz155240 int rev; 39962393Syz155240 { 39972393Syz155240 int ictype; 39982393Syz155240 39992393Syz155240 ictype = ic->ici_type; 40002393Syz155240 40012393Syz155240 if (v == 4) { 40022393Syz155240 /* 40032393Syz155240 * If we matched its type on the way in, then when going out 40042393Syz155240 * it will still be the same type. 40052393Syz155240 */ 40062393Syz155240 if ((!rev && (icmp->icmp_type == ictype)) || 40072393Syz155240 (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) { 40082393Syz155240 if (icmp->icmp_type != ICMP_ECHOREPLY) 40092393Syz155240 return 1; 40102393Syz155240 if (icmp->icmp_id == ic->ici_id) 40112393Syz155240 return 1; 40122393Syz155240 } 40132393Syz155240 } 40142393Syz155240 #ifdef USE_INET6 40152393Syz155240 else if (v == 6) { 40162393Syz155240 if ((!rev && (icmp->icmp_type == ictype)) || 40172393Syz155240 (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) { 40182393Syz155240 if (icmp->icmp_type != ICMP6_ECHO_REPLY) 40192393Syz155240 return 1; 40202393Syz155240 if (icmp->icmp_id == ic->ici_id) 40212393Syz155240 return 1; 40222393Syz155240 } 40232393Syz155240 } 40242393Syz155240 #endif 40252393Syz155240 return 0; 40262393Syz155240 } 40272393Syz155240 40282393Syz155240 40292393Syz155240 #ifdef IPFILTER_LOOKUP 40302393Syz155240 /* ------------------------------------------------------------------------ */ 40312393Syz155240 /* Function: fr_resolvelookup */ 40322393Syz155240 /* Returns: void * - NULL = failure, else success. */ 40332393Syz155240 /* Parameters: type(I) - type of lookup these parameters are for. */ 40342393Syz155240 /* number(I) - table number to use when searching */ 40352393Syz155240 /* funcptr(IO) - pointer to pointer for storing IP address */ 40362393Syz155240 /* searching function. */ 40372393Syz155240 /* */ 40382393Syz155240 /* Search for the "table" number passed in amongst those configured for */ 40392393Syz155240 /* that particular type. If the type is recognised then the function to */ 40402393Syz155240 /* call to do the IP address search will be change, regardless of whether */ 40412393Syz155240 /* or not the "table" number exists. */ 40422393Syz155240 /* ------------------------------------------------------------------------ */ 40433448Sdh155122 static void *fr_resolvelookup(type, number, funcptr, ifs) 40442393Syz155240 u_int type, number; 40452393Syz155240 lookupfunc_t *funcptr; 40463448Sdh155122 ipf_stack_t *ifs; 40472393Syz155240 { 40482393Syz155240 char name[FR_GROUPLEN]; 40492393Syz155240 iphtable_t *iph; 40502393Syz155240 ip_pool_t *ipo; 40512393Syz155240 void *ptr; 40522393Syz155240 40532393Syz155240 #if defined(SNPRINTF) && defined(_KERNEL) 40542393Syz155240 (void) SNPRINTF(name, sizeof(name), "%u", number); 40552393Syz155240 #else 40562393Syz155240 (void) sprintf(name, "%u", number); 40572393Syz155240 #endif 40582393Syz155240 40593448Sdh155122 READ_ENTER(&ifs->ifs_ip_poolrw); 40602393Syz155240 40612393Syz155240 switch (type) 40622393Syz155240 { 40632393Syz155240 case IPLT_POOL : 40642393Syz155240 # if (defined(__osf__) && defined(_KERNEL)) 40652393Syz155240 ptr = NULL; 40662393Syz155240 *funcptr = NULL; 40672393Syz155240 # else 40683448Sdh155122 ipo = ip_pool_find(IPL_LOGIPF, name, ifs); 40692393Syz155240 ptr = ipo; 40702393Syz155240 if (ipo != NULL) { 40712393Syz155240 ATOMIC_INC32(ipo->ipo_ref); 40722393Syz155240 } 40732393Syz155240 *funcptr = ip_pool_search; 40742393Syz155240 # endif 40752393Syz155240 break; 40762393Syz155240 case IPLT_HASH : 40773448Sdh155122 iph = fr_findhtable(IPL_LOGIPF, name, ifs); 40782393Syz155240 ptr = iph; 40792393Syz155240 if (iph != NULL) { 40802393Syz155240 ATOMIC_INC32(iph->iph_ref); 40812393Syz155240 } 40822393Syz155240 *funcptr = fr_iphmfindip; 40832393Syz155240 break; 40842393Syz155240 default: 40852393Syz155240 ptr = NULL; 40862393Syz155240 *funcptr = NULL; 40872393Syz155240 break; 40882393Syz155240 } 40893448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ip_poolrw); 40902393Syz155240 40912393Syz155240 return ptr; 40922393Syz155240 } 40932393Syz155240 #endif 40942393Syz155240 40952393Syz155240 40962393Syz155240 /* ------------------------------------------------------------------------ */ 40972393Syz155240 /* Function: frrequest */ 40982393Syz155240 /* Returns: int - 0 == success, > 0 == errno value */ 40992393Syz155240 /* Parameters: unit(I) - device for which this is for */ 41002393Syz155240 /* req(I) - ioctl command (SIOC*) */ 41012393Syz155240 /* data(I) - pointr to ioctl data */ 41022393Syz155240 /* set(I) - 1 or 0 (filter set) */ 41032393Syz155240 /* makecopy(I) - flag indicating whether data points to a rule */ 41042393Syz155240 /* in kernel space & hence doesn't need copying. */ 41052393Syz155240 /* */ 41062393Syz155240 /* This function handles all the requests which operate on the list of */ 41072393Syz155240 /* filter rules. This includes adding, deleting, insertion. It is also */ 41082393Syz155240 /* responsible for creating groups when a "head" rule is loaded. Interface */ 41092393Syz155240 /* names are resolved here and other sanity checks are made on the content */ 41102393Syz155240 /* of the rule structure being loaded. If a rule has user defined timeouts */ 41112393Syz155240 /* then make sure they are created and initialised before exiting. */ 41122393Syz155240 /* ------------------------------------------------------------------------ */ 41133448Sdh155122 int frrequest(unit, req, data, set, makecopy, ifs) 41142393Syz155240 int unit; 41152393Syz155240 ioctlcmd_t req; 41162393Syz155240 int set, makecopy; 41172393Syz155240 caddr_t data; 41183448Sdh155122 ipf_stack_t *ifs; 41192393Syz155240 { 41202393Syz155240 frentry_t frd, *fp, *f, **fprev, **ftail; 41212393Syz155240 int error = 0, in, v; 41222393Syz155240 void *ptr, *uptr; 41232393Syz155240 u_int *p, *pp; 41242393Syz155240 frgroup_t *fg; 41252393Syz155240 char *group; 41262393Syz155240 41272393Syz155240 fg = NULL; 41282393Syz155240 fp = &frd; 41292393Syz155240 if (makecopy != 0) { 41302393Syz155240 error = fr_inobj(data, fp, IPFOBJ_FRENTRY); 41312393Syz155240 if (error) 41322393Syz155240 return EFAULT; 41332393Syz155240 if ((fp->fr_flags & FR_T_BUILTIN) != 0) 41342393Syz155240 return EINVAL; 41352393Syz155240 fp->fr_ref = 0; 41362393Syz155240 fp->fr_flags |= FR_COPIED; 41372393Syz155240 } else { 41382393Syz155240 fp = (frentry_t *)data; 41392393Syz155240 if ((fp->fr_type & FR_T_BUILTIN) == 0) 41402393Syz155240 return EINVAL; 41412393Syz155240 fp->fr_flags &= ~FR_COPIED; 41422393Syz155240 } 41432393Syz155240 41442393Syz155240 if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) || 41452393Syz155240 ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) 41462393Syz155240 return EINVAL; 41472393Syz155240 41482393Syz155240 v = fp->fr_v; 41492393Syz155240 uptr = fp->fr_data; 41502393Syz155240 41512393Syz155240 /* 41522393Syz155240 * Only filter rules for IPv4 or IPv6 are accepted. 41532393Syz155240 */ 41542393Syz155240 if (v == 4) 41552393Syz155240 /*EMPTY*/; 41562393Syz155240 #ifdef USE_INET6 41572393Syz155240 else if (v == 6) 41582393Syz155240 /*EMPTY*/; 41592393Syz155240 #endif 41602393Syz155240 else { 41612393Syz155240 return EINVAL; 41622393Syz155240 } 41632393Syz155240 41642393Syz155240 /* 41652393Syz155240 * If the rule is being loaded from user space, i.e. we had to copy it 41662393Syz155240 * into kernel space, then do not trust the function pointer in the 41672393Syz155240 * rule. 41682393Syz155240 */ 41692393Syz155240 if ((makecopy == 1) && (fp->fr_func != NULL)) { 41702393Syz155240 if (fr_findfunc(fp->fr_func) == NULL) 41712393Syz155240 return ESRCH; 41723448Sdh155122 error = fr_funcinit(fp, ifs); 41732393Syz155240 if (error != 0) 41742393Syz155240 return error; 41752393Syz155240 } 41762393Syz155240 41772393Syz155240 ptr = NULL; 41782393Syz155240 /* 41792393Syz155240 * Check that the group number does exist and that its use (in/out) 41802393Syz155240 * matches what the rule is. 41812393Syz155240 */ 41822393Syz155240 if (!strncmp(fp->fr_grhead, "0", FR_GROUPLEN)) 41832393Syz155240 *fp->fr_grhead = '\0'; 41842393Syz155240 group = fp->fr_group; 41852393Syz155240 if (!strncmp(group, "0", FR_GROUPLEN)) 41862393Syz155240 *group = '\0'; 41872393Syz155240 41882393Syz155240 if (FR_ISACCOUNT(fp->fr_flags)) 41892393Syz155240 unit = IPL_LOGCOUNT; 41902393Syz155240 41912393Syz155240 if ((req != (int)SIOCZRLST) && (*group != '\0')) { 41923448Sdh155122 fg = fr_findgroup(group, unit, set, NULL, ifs); 41932393Syz155240 if (fg == NULL) 41942393Syz155240 return ESRCH; 41952393Syz155240 if (fg->fg_flags == 0) 41962393Syz155240 fg->fg_flags = fp->fr_flags & FR_INOUT; 41972393Syz155240 else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) 41982393Syz155240 return ESRCH; 41992393Syz155240 } 42002393Syz155240 42012393Syz155240 in = (fp->fr_flags & FR_INQUE) ? 0 : 1; 42022393Syz155240 42032393Syz155240 /* 42042393Syz155240 * Work out which rule list this change is being applied to. 42052393Syz155240 */ 42062393Syz155240 ftail = NULL; 42072393Syz155240 fprev = NULL; 42082393Syz155240 if (unit == IPL_LOGAUTH) 42093448Sdh155122 fprev = &ifs->ifs_ipauth; 42102393Syz155240 else if (v == 4) { 42112393Syz155240 if (FR_ISACCOUNT(fp->fr_flags)) 42123448Sdh155122 fprev = &ifs->ifs_ipacct[in][set]; 42132393Syz155240 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 42143448Sdh155122 fprev = &ifs->ifs_ipfilter[in][set]; 42152393Syz155240 } else if (v == 6) { 42162393Syz155240 if (FR_ISACCOUNT(fp->fr_flags)) 42173448Sdh155122 fprev = &ifs->ifs_ipacct6[in][set]; 42182393Syz155240 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0) 42193448Sdh155122 fprev = &ifs->ifs_ipfilter6[in][set]; 42202393Syz155240 } 42212393Syz155240 if (fprev == NULL) 42222393Syz155240 return ESRCH; 42232393Syz155240 42242393Syz155240 if (*group != '\0') { 42253448Sdh155122 if (!fg && !(fg = fr_findgroup(group, unit, set, NULL, ifs))) 42262393Syz155240 return ESRCH; 42272393Syz155240 fprev = &fg->fg_start; 42282393Syz155240 } 42292393Syz155240 42302393Syz155240 ftail = fprev; 42312393Syz155240 for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) { 42322393Syz155240 if (fp->fr_collect <= f->fr_collect) { 42332393Syz155240 ftail = fprev; 42342393Syz155240 f = NULL; 42352393Syz155240 break; 42362393Syz155240 } 42372393Syz155240 fprev = ftail; 42382393Syz155240 } 42392393Syz155240 42402393Syz155240 /* 42412393Syz155240 * Copy in extra data for the rule. 42422393Syz155240 */ 42432393Syz155240 if (fp->fr_dsize != 0) { 42442393Syz155240 if (makecopy != 0) { 42452393Syz155240 KMALLOCS(ptr, void *, fp->fr_dsize); 42462393Syz155240 if (!ptr) 42472393Syz155240 return ENOMEM; 42482393Syz155240 error = COPYIN(uptr, ptr, fp->fr_dsize); 42492393Syz155240 } else { 42502393Syz155240 ptr = uptr; 42512393Syz155240 error = 0; 42522393Syz155240 } 42532393Syz155240 if (error != 0) { 42542393Syz155240 KFREES(ptr, fp->fr_dsize); 42552393Syz155240 return ENOMEM; 42562393Syz155240 } 42572393Syz155240 fp->fr_data = ptr; 42582393Syz155240 } else 42592393Syz155240 fp->fr_data = NULL; 42602393Syz155240 42612393Syz155240 /* 42622393Syz155240 * Perform per-rule type sanity checks of their members. 42632393Syz155240 */ 42642393Syz155240 switch (fp->fr_type & ~FR_T_BUILTIN) 42652393Syz155240 { 42662393Syz155240 #if defined(IPFILTER_BPF) 42672393Syz155240 case FR_T_BPFOPC : 42682393Syz155240 if (fp->fr_dsize == 0) 42692393Syz155240 return EINVAL; 42702393Syz155240 if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) { 42712393Syz155240 if (makecopy && fp->fr_data != NULL) { 42722393Syz155240 KFREES(fp->fr_data, fp->fr_dsize); 42732393Syz155240 } 42742393Syz155240 return EINVAL; 42752393Syz155240 } 42762393Syz155240 break; 42772393Syz155240 #endif 42782393Syz155240 case FR_T_IPF : 42792393Syz155240 if (fp->fr_dsize != sizeof(fripf_t)) 42802393Syz155240 return EINVAL; 42812393Syz155240 42822393Syz155240 /* 42832393Syz155240 * Allowing a rule with both "keep state" and "with oow" is 42842393Syz155240 * pointless because adding a state entry to the table will 42852393Syz155240 * fail with the out of window (oow) flag set. 42862393Syz155240 */ 42872393Syz155240 if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) 42882393Syz155240 return EINVAL; 42892393Syz155240 42902393Syz155240 switch (fp->fr_satype) 42912393Syz155240 { 42922393Syz155240 case FRI_BROADCAST : 42932393Syz155240 case FRI_DYNAMIC : 42942393Syz155240 case FRI_NETWORK : 42952393Syz155240 case FRI_NETMASKED : 42962393Syz155240 case FRI_PEERADDR : 42972393Syz155240 if (fp->fr_sifpidx < 0 || fp->fr_sifpidx > 3) { 42982393Syz155240 if (makecopy && fp->fr_data != NULL) { 42992393Syz155240 KFREES(fp->fr_data, fp->fr_dsize); 43002393Syz155240 } 43012393Syz155240 return EINVAL; 43022393Syz155240 } 43032393Syz155240 break; 43042393Syz155240 #ifdef IPFILTER_LOOKUP 43052393Syz155240 case FRI_LOOKUP : 43062393Syz155240 fp->fr_srcptr = fr_resolvelookup(fp->fr_srctype, 43072393Syz155240 fp->fr_srcnum, 43083448Sdh155122 &fp->fr_srcfunc, ifs); 43092393Syz155240 break; 43102393Syz155240 #endif 43112393Syz155240 default : 43122393Syz155240 break; 43132393Syz155240 } 43142393Syz155240 43152393Syz155240 switch (fp->fr_datype) 43162393Syz155240 { 43172393Syz155240 case FRI_BROADCAST : 43182393Syz155240 case FRI_DYNAMIC : 43192393Syz155240 case FRI_NETWORK : 43202393Syz155240 case FRI_NETMASKED : 43212393Syz155240 case FRI_PEERADDR : 43222393Syz155240 if (fp->fr_difpidx < 0 || fp->fr_difpidx > 3) { 43232393Syz155240 if (makecopy && fp->fr_data != NULL) { 43242393Syz155240 KFREES(fp->fr_data, fp->fr_dsize); 43252393Syz155240 } 43262393Syz155240 return EINVAL; 43272393Syz155240 } 43282393Syz155240 break; 43292393Syz155240 #ifdef IPFILTER_LOOKUP 43302393Syz155240 case FRI_LOOKUP : 43312393Syz155240 fp->fr_dstptr = fr_resolvelookup(fp->fr_dsttype, 43322393Syz155240 fp->fr_dstnum, 43333448Sdh155122 &fp->fr_dstfunc, ifs); 43342393Syz155240 break; 43352393Syz155240 #endif 43362393Syz155240 default : 43372393Syz155240 break; 43382393Syz155240 } 43392393Syz155240 break; 43402393Syz155240 case FR_T_NONE : 43412393Syz155240 break; 43422393Syz155240 case FR_T_CALLFUNC : 43432393Syz155240 break; 43442393Syz155240 case FR_T_COMPIPF : 43452393Syz155240 break; 43462393Syz155240 default : 43472393Syz155240 if (makecopy && fp->fr_data != NULL) { 43482393Syz155240 KFREES(fp->fr_data, fp->fr_dsize); 43492393Syz155240 } 43502393Syz155240 return EINVAL; 43512393Syz155240 } 43522393Syz155240 43532393Syz155240 /* 43542393Syz155240 * Lookup all the interface names that are part of the rule. 43552393Syz155240 */ 43563448Sdh155122 frsynclist(0, 0, NULL, NULL, fp, ifs); 43572393Syz155240 fp->fr_statecnt = 0; 43582393Syz155240 43592393Syz155240 /* 43602393Syz155240 * Look for an existing matching filter rule, but don't include the 43612393Syz155240 * next or interface pointer in the comparison (fr_next, fr_ifa). 43622393Syz155240 * This elminates rules which are indentical being loaded. Checksum 43632393Syz155240 * the constant part of the filter rule to make comparisons quicker 43642393Syz155240 * (this meaning no pointers are included). 43652393Syz155240 */ 43662393Syz155240 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum; 43672393Syz155240 p < pp; p++) 43682393Syz155240 fp->fr_cksum += *p; 43692393Syz155240 pp = (u_int *)(fp->fr_caddr + fp->fr_dsize); 43702393Syz155240 for (p = (u_int *)fp->fr_data; p < pp; p++) 43712393Syz155240 fp->fr_cksum += *p; 43722393Syz155240 43733448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_mutex); 43743448Sdh155122 bzero((char *)&ifs->ifs_frcache, sizeof (ifs->ifs_frcache)); 43752393Syz155240 43762393Syz155240 for (; (f = *ftail) != NULL; ftail = &f->fr_next) { 43772393Syz155240 if ((fp->fr_cksum != f->fr_cksum) || 43782393Syz155240 (f->fr_dsize != fp->fr_dsize)) 43792393Syz155240 continue; 43802393Syz155240 if (bcmp((char *)&f->fr_func, (char *)&fp->fr_func, FR_CMPSIZ)) 43812393Syz155240 continue; 43822393Syz155240 if ((!ptr && !f->fr_data) || 43832393Syz155240 (ptr && f->fr_data && 43842393Syz155240 !bcmp((char *)ptr, (char *)f->fr_data, f->fr_dsize))) 43852393Syz155240 break; 43862393Syz155240 } 43872393Syz155240 43882393Syz155240 /* 43892393Syz155240 * If zero'ing statistics, copy current to caller and zero. 43902393Syz155240 */ 43912393Syz155240 if (req == (ioctlcmd_t)SIOCZRLST) { 43922393Syz155240 if (f == NULL) 43932393Syz155240 error = ESRCH; 43942393Syz155240 else { 43952393Syz155240 /* 43962393Syz155240 * Copy and reduce lock because of impending copyout. 43972393Syz155240 * Well we should, but if we do then the atomicity of 43982393Syz155240 * this call and the correctness of fr_hits and 43992393Syz155240 * fr_bytes cannot be guaranteed. As it is, this code 44002393Syz155240 * only resets them to 0 if they are successfully 44012393Syz155240 * copied out into user space. 44022393Syz155240 */ 44032393Syz155240 bcopy((char *)f, (char *)fp, sizeof(*f)); 44042393Syz155240 /* MUTEX_DOWNGRADE(&ipf_mutex); */ 44052393Syz155240 44062393Syz155240 /* 44072393Syz155240 * When we copy this rule back out, set the data 44082393Syz155240 * pointer to be what it was in user space. 44092393Syz155240 */ 44102393Syz155240 fp->fr_data = uptr; 44112393Syz155240 error = fr_outobj(data, fp, IPFOBJ_FRENTRY); 44122393Syz155240 44132393Syz155240 if (error == 0) { 44142393Syz155240 if ((f->fr_dsize != 0) && (uptr != NULL)) 44152393Syz155240 error = COPYOUT(f->fr_data, uptr, 44162393Syz155240 f->fr_dsize); 44172393Syz155240 if (error == 0) { 44182393Syz155240 f->fr_hits = 0; 44192393Syz155240 f->fr_bytes = 0; 44202393Syz155240 } 44212393Syz155240 } 44222393Syz155240 } 44232393Syz155240 44242393Syz155240 if ((ptr != NULL) && (makecopy != 0)) { 44252393Syz155240 KFREES(ptr, fp->fr_dsize); 44262393Syz155240 } 44273448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 44282393Syz155240 return error; 44292393Syz155240 } 44302393Syz155240 44312393Syz155240 if (!f) { 44322393Syz155240 /* 44332393Syz155240 * At the end of this, ftail must point to the place where the 44342393Syz155240 * new rule is to be saved/inserted/added. 44352393Syz155240 * For SIOCAD*FR, this should be the last rule in the group of 44362393Syz155240 * rules that have equal fr_collect fields. 44372393Syz155240 * For SIOCIN*FR, ... 44382393Syz155240 */ 44392393Syz155240 if (req == (ioctlcmd_t)SIOCADAFR || 44402393Syz155240 req == (ioctlcmd_t)SIOCADIFR) { 44412393Syz155240 44422393Syz155240 for (ftail = fprev; (f = *ftail) != NULL; ) { 44432393Syz155240 if (f->fr_collect > fp->fr_collect) 44442393Syz155240 break; 44452393Syz155240 ftail = &f->fr_next; 44462393Syz155240 } 44472393Syz155240 f = NULL; 44482393Syz155240 ptr = NULL; 44492393Syz155240 error = 0; 44502393Syz155240 } else if (req == (ioctlcmd_t)SIOCINAFR || 44512393Syz155240 req == (ioctlcmd_t)SIOCINIFR) { 44522393Syz155240 while ((f = *fprev) != NULL) { 44532393Syz155240 if (f->fr_collect >= fp->fr_collect) 44542393Syz155240 break; 44552393Syz155240 fprev = &f->fr_next; 44562393Syz155240 } 44572393Syz155240 ftail = fprev; 44582393Syz155240 if (fp->fr_hits != 0) { 44592393Syz155240 while (fp->fr_hits && (f = *ftail)) { 44602393Syz155240 if (f->fr_collect != fp->fr_collect) 44612393Syz155240 break; 44622393Syz155240 fprev = ftail; 44632393Syz155240 ftail = &f->fr_next; 44642393Syz155240 fp->fr_hits--; 44652393Syz155240 } 44662393Syz155240 } 44672393Syz155240 f = NULL; 44682393Syz155240 ptr = NULL; 44692393Syz155240 error = 0; 44702393Syz155240 } 44712393Syz155240 } 44722393Syz155240 44732393Syz155240 /* 44742393Syz155240 * Request to remove a rule. 44752393Syz155240 */ 44762393Syz155240 if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) { 44772393Syz155240 if (!f) 44782393Syz155240 error = ESRCH; 44792393Syz155240 else { 44802393Syz155240 /* 44812393Syz155240 * Do not allow activity from user space to interfere 44822393Syz155240 * with rules not loaded that way. 44832393Syz155240 */ 44842393Syz155240 if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) { 44852393Syz155240 error = EPERM; 44862393Syz155240 goto done; 44872393Syz155240 } 44882393Syz155240 44892393Syz155240 /* 44902393Syz155240 * Return EBUSY if the rule is being reference by 44912393Syz155240 * something else (eg state information. 44922393Syz155240 */ 44932393Syz155240 if (f->fr_ref > 1) { 44942393Syz155240 error = EBUSY; 44952393Syz155240 goto done; 44962393Syz155240 } 44972393Syz155240 #ifdef IPFILTER_SCAN 44982393Syz155240 if (f->fr_isctag[0] != '\0' && 44992393Syz155240 (f->fr_isc != (struct ipscan *)-1)) 45002393Syz155240 ipsc_detachfr(f); 45012393Syz155240 #endif 45022393Syz155240 if ((fg != NULL) && (fg->fg_head != NULL)) 45032393Syz155240 fg->fg_head->fr_ref--; 45042393Syz155240 if (unit == IPL_LOGAUTH) { 45053448Sdh155122 error = fr_preauthcmd(req, f, ftail, ifs); 45062393Syz155240 goto done; 45072393Syz155240 } 45082393Syz155240 if (*f->fr_grhead != '\0') 45093448Sdh155122 fr_delgroup(f->fr_grhead, unit, set, ifs); 45102393Syz155240 fr_fixskip(ftail, f, -1); 45112393Syz155240 *ftail = f->fr_next; 45122393Syz155240 f->fr_next = NULL; 45133448Sdh155122 (void)fr_derefrule(&f, ifs); 45142393Syz155240 } 45152393Syz155240 } else { 45162393Syz155240 /* 45172393Syz155240 * Not removing, so we must be adding/inserting a rule. 45182393Syz155240 */ 45192393Syz155240 if (f) 45202393Syz155240 error = EEXIST; 45212393Syz155240 else { 45222393Syz155240 if (unit == IPL_LOGAUTH) { 45233448Sdh155122 error = fr_preauthcmd(req, fp, ftail, ifs); 45242393Syz155240 goto done; 45252393Syz155240 } 45262393Syz155240 if (makecopy) { 45272393Syz155240 KMALLOC(f, frentry_t *); 45282393Syz155240 } else 45292393Syz155240 f = fp; 45302393Syz155240 if (f != NULL) { 45312393Syz155240 if (fg != NULL && fg->fg_head != NULL) 45322393Syz155240 fg->fg_head->fr_ref++; 45332393Syz155240 if (fp != f) 45342393Syz155240 bcopy((char *)fp, (char *)f, 45352393Syz155240 sizeof(*f)); 45362393Syz155240 MUTEX_NUKE(&f->fr_lock); 45372393Syz155240 MUTEX_INIT(&f->fr_lock, "filter rule lock"); 45382393Syz155240 #ifdef IPFILTER_SCAN 45392393Syz155240 if (f->fr_isctag[0] != '\0' && 45402393Syz155240 ipsc_attachfr(f)) 45412393Syz155240 f->fr_isc = (struct ipscan *)-1; 45422393Syz155240 #endif 45432393Syz155240 f->fr_hits = 0; 45442393Syz155240 if (makecopy != 0) 45452393Syz155240 f->fr_ref = 1; 45462393Syz155240 f->fr_next = *ftail; 45472393Syz155240 *ftail = f; 45482393Syz155240 if (req == (ioctlcmd_t)SIOCINIFR || 45492393Syz155240 req == (ioctlcmd_t)SIOCINAFR) 45502393Syz155240 fr_fixskip(ftail, f, 1); 45512393Syz155240 f->fr_grp = NULL; 45522393Syz155240 group = f->fr_grhead; 45532393Syz155240 if (*group != '\0') { 45542393Syz155240 fg = fr_addgroup(group, f, f->fr_flags, 45553448Sdh155122 unit, set, ifs); 45562393Syz155240 if (fg != NULL) 45572393Syz155240 f->fr_grp = &fg->fg_start; 45582393Syz155240 } 45592393Syz155240 } else 45602393Syz155240 error = ENOMEM; 45612393Syz155240 } 45622393Syz155240 } 45632393Syz155240 done: 45643448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 45652393Syz155240 if ((ptr != NULL) && (error != 0) && (makecopy != 0)) { 45662393Syz155240 KFREES(ptr, fp->fr_dsize); 45672393Syz155240 } 45682393Syz155240 return (error); 45692393Syz155240 } 45702393Syz155240 45712393Syz155240 45722393Syz155240 /* ------------------------------------------------------------------------ */ 45732393Syz155240 /* Function: fr_funcinit */ 45742393Syz155240 /* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */ 45752393Syz155240 /* Parameters: fr(I) - pointer to filter rule */ 45762393Syz155240 /* */ 45772393Syz155240 /* If a rule is a call rule, then check if the function it points to needs */ 45782393Syz155240 /* an init function to be called now the rule has been loaded. */ 45792393Syz155240 /* ------------------------------------------------------------------------ */ 45803448Sdh155122 static int fr_funcinit(fr, ifs) 45812393Syz155240 frentry_t *fr; 45823448Sdh155122 ipf_stack_t *ifs; 45832393Syz155240 { 45842393Syz155240 ipfunc_resolve_t *ft; 45852393Syz155240 int err; 45862393Syz155240 45872393Syz155240 err = ESRCH; 45882393Syz155240 45892393Syz155240 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 45902393Syz155240 if (ft->ipfu_addr == fr->fr_func) { 45912393Syz155240 err = 0; 45922393Syz155240 if (ft->ipfu_init != NULL) 45933448Sdh155122 err = (*ft->ipfu_init)(fr, ifs); 45942393Syz155240 break; 45952393Syz155240 } 45962393Syz155240 return err; 45972393Syz155240 } 45982393Syz155240 45992393Syz155240 46002393Syz155240 /* ------------------------------------------------------------------------ */ 46012393Syz155240 /* Function: fr_findfunc */ 46022393Syz155240 /* Returns: ipfunc_t - pointer to function if found, else NULL */ 46032393Syz155240 /* Parameters: funcptr(I) - function pointer to lookup */ 46042393Syz155240 /* */ 46052393Syz155240 /* Look for a function in the table of known functions. */ 46062393Syz155240 /* ------------------------------------------------------------------------ */ 46072393Syz155240 static ipfunc_t fr_findfunc(funcptr) 46082393Syz155240 ipfunc_t funcptr; 46092393Syz155240 { 46102393Syz155240 ipfunc_resolve_t *ft; 46112393Syz155240 46122393Syz155240 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 46132393Syz155240 if (ft->ipfu_addr == funcptr) 46142393Syz155240 return funcptr; 46152393Syz155240 return NULL; 46162393Syz155240 } 46172393Syz155240 46182393Syz155240 46192393Syz155240 /* ------------------------------------------------------------------------ */ 46202393Syz155240 /* Function: fr_resolvefunc */ 46212393Syz155240 /* Returns: int - 0 == success, else error */ 46222393Syz155240 /* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */ 46232393Syz155240 /* */ 46242393Syz155240 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */ 46252393Syz155240 /* This will either be the function name (if the pointer is set) or the */ 46262393Syz155240 /* function pointer if the name is set. When found, fill in the other one */ 46272393Syz155240 /* so that the entire, complete, structure can be copied back to user space.*/ 46282393Syz155240 /* ------------------------------------------------------------------------ */ 46292393Syz155240 int fr_resolvefunc(data) 46302393Syz155240 void *data; 46312393Syz155240 { 46322393Syz155240 ipfunc_resolve_t res, *ft; 46332393Syz155240 46342393Syz155240 BCOPYIN(data, &res, sizeof(res)); 46352393Syz155240 46362393Syz155240 if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') { 46372393Syz155240 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 46382393Syz155240 if (strncmp(res.ipfu_name, ft->ipfu_name, 46392393Syz155240 sizeof(res.ipfu_name)) == 0) { 46402393Syz155240 res.ipfu_addr = ft->ipfu_addr; 46412393Syz155240 res.ipfu_init = ft->ipfu_init; 46422393Syz155240 if (COPYOUT(&res, data, sizeof(res)) != 0) 46432393Syz155240 return EFAULT; 46442393Syz155240 return 0; 46452393Syz155240 } 46462393Syz155240 } 46472393Syz155240 if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') { 46482393Syz155240 for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++) 46492393Syz155240 if (ft->ipfu_addr == res.ipfu_addr) { 46502393Syz155240 (void) strncpy(res.ipfu_name, ft->ipfu_name, 46512393Syz155240 sizeof(res.ipfu_name)); 46522393Syz155240 res.ipfu_init = ft->ipfu_init; 46532393Syz155240 if (COPYOUT(&res, data, sizeof(res)) != 0) 46542393Syz155240 return EFAULT; 46552393Syz155240 return 0; 46562393Syz155240 } 46572393Syz155240 } 46582393Syz155240 return ESRCH; 46592393Syz155240 } 46602393Syz155240 46612393Syz155240 46622393Syz155240 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \ 46632393Syz155240 (defined(__FreeBSD__) && (__FreeBSD_version < 490000)) || \ 46642393Syz155240 (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \ 46652393Syz155240 (defined(__OpenBSD__) && (OpenBSD < 200006)) 46662393Syz155240 /* 46672393Syz155240 * From: NetBSD 46682393Syz155240 * ppsratecheck(): packets (or events) per second limitation. 46692393Syz155240 */ 46702393Syz155240 int 46712393Syz155240 ppsratecheck(lasttime, curpps, maxpps) 46722393Syz155240 struct timeval *lasttime; 46732393Syz155240 int *curpps; 46742393Syz155240 int maxpps; /* maximum pps allowed */ 46752393Syz155240 { 46762393Syz155240 struct timeval tv, delta; 46772393Syz155240 int rv; 46782393Syz155240 46792393Syz155240 GETKTIME(&tv); 46802393Syz155240 46812393Syz155240 delta.tv_sec = tv.tv_sec - lasttime->tv_sec; 46822393Syz155240 delta.tv_usec = tv.tv_usec - lasttime->tv_usec; 46832393Syz155240 if (delta.tv_usec < 0) { 46842393Syz155240 delta.tv_sec--; 46852393Syz155240 delta.tv_usec += 1000000; 46862393Syz155240 } 46872393Syz155240 46882393Syz155240 /* 46892393Syz155240 * check for 0,0 is so that the message will be seen at least once. 46902393Syz155240 * if more than one second have passed since the last update of 46912393Syz155240 * lasttime, reset the counter. 46922393Syz155240 * 46932393Syz155240 * we do increment *curpps even in *curpps < maxpps case, as some may 46942393Syz155240 * try to use *curpps for stat purposes as well. 46952393Syz155240 */ 46962393Syz155240 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 46972393Syz155240 delta.tv_sec >= 1) { 46982393Syz155240 *lasttime = tv; 46992393Syz155240 *curpps = 0; 47002393Syz155240 rv = 1; 47012393Syz155240 } else if (maxpps < 0) 47022393Syz155240 rv = 1; 47032393Syz155240 else if (*curpps < maxpps) 47042393Syz155240 rv = 1; 47052393Syz155240 else 47062393Syz155240 rv = 0; 47072393Syz155240 *curpps = *curpps + 1; 47082393Syz155240 47092393Syz155240 return (rv); 47102393Syz155240 } 47112393Syz155240 #endif 47122393Syz155240 47132393Syz155240 47142393Syz155240 /* ------------------------------------------------------------------------ */ 47152393Syz155240 /* Function: fr_derefrule */ 47162393Syz155240 /* Returns: int - 0 == rule freed up, else rule not freed */ 47172393Syz155240 /* Parameters: fr(I) - pointer to filter rule */ 47182393Syz155240 /* */ 47192393Syz155240 /* Decrement the reference counter to a rule by one. If it reaches zero, */ 47202393Syz155240 /* free it and any associated storage space being used by it. */ 47212393Syz155240 /* ------------------------------------------------------------------------ */ 47223448Sdh155122 int fr_derefrule(frp, ifs) 47232393Syz155240 frentry_t **frp; 47243448Sdh155122 ipf_stack_t *ifs; 47252393Syz155240 { 47262393Syz155240 frentry_t *fr; 47272393Syz155240 47282393Syz155240 fr = *frp; 47292393Syz155240 47302393Syz155240 MUTEX_ENTER(&fr->fr_lock); 47312393Syz155240 fr->fr_ref--; 47322393Syz155240 if (fr->fr_ref == 0) { 47332393Syz155240 MUTEX_EXIT(&fr->fr_lock); 47342393Syz155240 MUTEX_DESTROY(&fr->fr_lock); 47352393Syz155240 47362393Syz155240 #ifdef IPFILTER_LOOKUP 47372393Syz155240 if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP) 47383448Sdh155122 ip_lookup_deref(fr->fr_srctype, fr->fr_srcptr, ifs); 47392393Syz155240 if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP) 47403448Sdh155122 ip_lookup_deref(fr->fr_dsttype, fr->fr_dstptr, ifs); 47412393Syz155240 #endif 47422393Syz155240 47432393Syz155240 if (fr->fr_dsize) { 47442393Syz155240 KFREES(fr->fr_data, fr->fr_dsize); 47452393Syz155240 } 47462393Syz155240 if ((fr->fr_flags & FR_COPIED) != 0) { 47472393Syz155240 KFREE(fr); 47482393Syz155240 return 0; 47492393Syz155240 } 47502393Syz155240 return 1; 47512393Syz155240 } else { 47522393Syz155240 MUTEX_EXIT(&fr->fr_lock); 47532393Syz155240 } 47542393Syz155240 *frp = NULL; 47552393Syz155240 return -1; 47562393Syz155240 } 47572393Syz155240 47582393Syz155240 47592393Syz155240 #ifdef IPFILTER_LOOKUP 47602393Syz155240 /* ------------------------------------------------------------------------ */ 47612393Syz155240 /* Function: fr_grpmapinit */ 47622393Syz155240 /* Returns: int - 0 == success, else ESRCH because table entry not found*/ 47632393Syz155240 /* Parameters: fr(I) - pointer to rule to find hash table for */ 47642393Syz155240 /* */ 47652393Syz155240 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */ 47662393Syz155240 /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap. */ 47672393Syz155240 /* ------------------------------------------------------------------------ */ 47683448Sdh155122 static int fr_grpmapinit(fr, ifs) 47692393Syz155240 frentry_t *fr; 47703448Sdh155122 ipf_stack_t *ifs; 47712393Syz155240 { 47722393Syz155240 char name[FR_GROUPLEN]; 47732393Syz155240 iphtable_t *iph; 47742393Syz155240 47752393Syz155240 #if defined(SNPRINTF) && defined(_KERNEL) 47762393Syz155240 (void) SNPRINTF(name, sizeof(name), "%d", fr->fr_arg); 47772393Syz155240 #else 47782393Syz155240 (void) sprintf(name, "%d", fr->fr_arg); 47792393Syz155240 #endif 47803448Sdh155122 iph = fr_findhtable(IPL_LOGIPF, name, ifs); 47812393Syz155240 if (iph == NULL) 47822393Syz155240 return ESRCH; 47832393Syz155240 if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) 47842393Syz155240 return ESRCH; 47852393Syz155240 fr->fr_ptr = iph; 47862393Syz155240 return 0; 47872393Syz155240 } 47882393Syz155240 47892393Syz155240 47902393Syz155240 /* ------------------------------------------------------------------------ */ 47912393Syz155240 /* Function: fr_srcgrpmap */ 47922393Syz155240 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 47932393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 47942393Syz155240 /* passp(IO) - pointer to current/new filter decision (unused) */ 47952393Syz155240 /* */ 47962393Syz155240 /* Look for a rule group head in a hash table, using the source address as */ 47972393Syz155240 /* the key, and descend into that group and continue matching rules against */ 47982393Syz155240 /* the packet. */ 47992393Syz155240 /* ------------------------------------------------------------------------ */ 48002393Syz155240 frentry_t *fr_srcgrpmap(fin, passp) 48012393Syz155240 fr_info_t *fin; 48022393Syz155240 u_32_t *passp; 48032393Syz155240 { 48042393Syz155240 frgroup_t *fg; 48052393Syz155240 void *rval; 48063448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 48073448Sdh155122 48083448Sdh155122 rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_src, ifs); 48092393Syz155240 if (rval == NULL) 48102393Syz155240 return NULL; 48112393Syz155240 48122393Syz155240 fg = rval; 48132393Syz155240 fin->fin_fr = fg->fg_start; 48142393Syz155240 (void) fr_scanlist(fin, *passp); 48152393Syz155240 return fin->fin_fr; 48162393Syz155240 } 48172393Syz155240 48182393Syz155240 48192393Syz155240 /* ------------------------------------------------------------------------ */ 48202393Syz155240 /* Function: fr_dstgrpmap */ 48212393Syz155240 /* Returns: frentry_t * - pointer to "new last matching" rule or NULL */ 48222393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 48232393Syz155240 /* passp(IO) - pointer to current/new filter decision (unused) */ 48242393Syz155240 /* */ 48252393Syz155240 /* Look for a rule group head in a hash table, using the destination */ 48262393Syz155240 /* address as the key, and descend into that group and continue matching */ 48272393Syz155240 /* rules against the packet. */ 48282393Syz155240 /* ------------------------------------------------------------------------ */ 48292393Syz155240 frentry_t *fr_dstgrpmap(fin, passp) 48302393Syz155240 fr_info_t *fin; 48312393Syz155240 u_32_t *passp; 48322393Syz155240 { 48332393Syz155240 frgroup_t *fg; 48342393Syz155240 void *rval; 48353448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 48363448Sdh155122 48373448Sdh155122 rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_dst, ifs); 48382393Syz155240 if (rval == NULL) 48392393Syz155240 return NULL; 48402393Syz155240 48412393Syz155240 fg = rval; 48422393Syz155240 fin->fin_fr = fg->fg_start; 48432393Syz155240 (void) fr_scanlist(fin, *passp); 48442393Syz155240 return fin->fin_fr; 48452393Syz155240 } 48462393Syz155240 #endif /* IPFILTER_LOOKUP */ 48472393Syz155240 48482393Syz155240 /* 48492393Syz155240 * Queue functions 48502393Syz155240 * =============== 48512393Syz155240 * These functions manage objects on queues for efficient timeouts. There are 48522393Syz155240 * a number of system defined queues as well as user defined timeouts. It is 48532393Syz155240 * expected that a lock is held in the domain in which the queue belongs 48542393Syz155240 * (i.e. either state or NAT) when calling any of these functions that prevents 48552393Syz155240 * fr_freetimeoutqueue() from being called at the same time as any other. 48562393Syz155240 */ 48572393Syz155240 48582393Syz155240 48592393Syz155240 /* ------------------------------------------------------------------------ */ 48602393Syz155240 /* Function: fr_addtimeoutqueue */ 48612393Syz155240 /* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */ 48622393Syz155240 /* timeout queue with given interval. */ 48632393Syz155240 /* Parameters: parent(I) - pointer to pointer to parent node of this list */ 48642393Syz155240 /* of interface queues. */ 48652393Syz155240 /* seconds(I) - timeout value in seconds for this queue. */ 48662393Syz155240 /* */ 48672393Syz155240 /* This routine first looks for a timeout queue that matches the interval */ 48682393Syz155240 /* being requested. If it finds one, increments the reference counter and */ 48692393Syz155240 /* returns a pointer to it. If none are found, it allocates a new one and */ 48702393Syz155240 /* inserts it at the top of the list. */ 48712393Syz155240 /* */ 48722393Syz155240 /* Locking. */ 48732393Syz155240 /* It is assumed that the caller of this function has an appropriate lock */ 48742393Syz155240 /* held (exclusively) in the domain that encompases 'parent'. */ 48752393Syz155240 /* ------------------------------------------------------------------------ */ 48763448Sdh155122 ipftq_t *fr_addtimeoutqueue(parent, seconds, ifs) 48772393Syz155240 ipftq_t **parent; 48782393Syz155240 u_int seconds; 48793448Sdh155122 ipf_stack_t *ifs; 48802393Syz155240 { 48812393Syz155240 ipftq_t *ifq; 48822393Syz155240 u_int period; 48832393Syz155240 48842393Syz155240 period = seconds * IPF_HZ_DIVIDE; 48852393Syz155240 48863448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipf_timeoutlock); 48872393Syz155240 for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) { 48882393Syz155240 if (ifq->ifq_ttl == period) { 48892393Syz155240 /* 48902393Syz155240 * Reset the delete flag, if set, so the structure 48912393Syz155240 * gets reused rather than freed and reallocated. 48922393Syz155240 */ 48932393Syz155240 MUTEX_ENTER(&ifq->ifq_lock); 48942393Syz155240 ifq->ifq_flags &= ~IFQF_DELETE; 48952393Syz155240 ifq->ifq_ref++; 48962393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 48973448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock); 48982393Syz155240 48992393Syz155240 return ifq; 49002393Syz155240 } 49012393Syz155240 } 49022393Syz155240 49032393Syz155240 KMALLOC(ifq, ipftq_t *); 49042393Syz155240 if (ifq != NULL) { 49052393Syz155240 ifq->ifq_ttl = period; 49062393Syz155240 ifq->ifq_head = NULL; 49072393Syz155240 ifq->ifq_tail = &ifq->ifq_head; 49082393Syz155240 ifq->ifq_next = *parent; 49092393Syz155240 ifq->ifq_pnext = parent; 49102393Syz155240 ifq->ifq_ref = 1; 49112393Syz155240 ifq->ifq_flags = IFQF_USER; 49122393Syz155240 *parent = ifq; 49133448Sdh155122 ifs->ifs_fr_userifqs++; 49142393Syz155240 MUTEX_NUKE(&ifq->ifq_lock); 49152393Syz155240 MUTEX_INIT(&ifq->ifq_lock, "ipftq mutex"); 49162393Syz155240 } 49173448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock); 49182393Syz155240 return ifq; 49192393Syz155240 } 49202393Syz155240 49212393Syz155240 49222393Syz155240 /* ------------------------------------------------------------------------ */ 49232393Syz155240 /* Function: fr_deletetimeoutqueue */ 49242393Syz155240 /* Returns: int - new reference count value of the timeout queue */ 49252393Syz155240 /* Parameters: ifq(I) - timeout queue which is losing a reference. */ 49262393Syz155240 /* Locks: ifq->ifq_lock */ 49272393Syz155240 /* */ 49282393Syz155240 /* This routine must be called when we're discarding a pointer to a timeout */ 49292393Syz155240 /* queue object, taking care of the reference counter. */ 49302393Syz155240 /* */ 49312393Syz155240 /* Now that this just sets a DELETE flag, it requires the expire code to */ 49322393Syz155240 /* check the list of user defined timeout queues and call the free function */ 49332393Syz155240 /* below (currently commented out) to stop memory leaking. It is done this */ 49342393Syz155240 /* way because the locking may not be sufficient to safely do a free when */ 49352393Syz155240 /* this function is called. */ 49362393Syz155240 /* ------------------------------------------------------------------------ */ 49372393Syz155240 int fr_deletetimeoutqueue(ifq) 49382393Syz155240 ipftq_t *ifq; 49392393Syz155240 { 49402393Syz155240 49412393Syz155240 ifq->ifq_ref--; 49422393Syz155240 if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) { 49432393Syz155240 ifq->ifq_flags |= IFQF_DELETE; 49442393Syz155240 } 49452393Syz155240 49462393Syz155240 return ifq->ifq_ref; 49472393Syz155240 } 49482393Syz155240 49492393Syz155240 49502393Syz155240 /* ------------------------------------------------------------------------ */ 49512393Syz155240 /* Function: fr_freetimeoutqueue */ 49522393Syz155240 /* Parameters: ifq(I) - timeout queue which is losing a reference. */ 49532393Syz155240 /* Returns: Nil */ 49542393Syz155240 /* */ 49552393Syz155240 /* Locking: */ 49562393Syz155240 /* It is assumed that the caller of this function has an appropriate lock */ 49572393Syz155240 /* held (exclusively) in the domain that encompases the callers "domain". */ 49582393Syz155240 /* The ifq_lock for this structure should not be held. */ 49592393Syz155240 /* */ 49602393Syz155240 /* Remove a user definde timeout queue from the list of queues it is in and */ 49612393Syz155240 /* tidy up after this is done. */ 49622393Syz155240 /* ------------------------------------------------------------------------ */ 49633448Sdh155122 void fr_freetimeoutqueue(ifq, ifs) 49642393Syz155240 ipftq_t *ifq; 49653448Sdh155122 ipf_stack_t *ifs; 49662393Syz155240 { 49672393Syz155240 49682393Syz155240 49692393Syz155240 if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) || 49702393Syz155240 ((ifq->ifq_flags & IFQF_USER) == 0)) { 49712393Syz155240 printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n", 49722393Syz155240 (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl, 49732393Syz155240 ifq->ifq_ref); 49742393Syz155240 return; 49752393Syz155240 } 49762393Syz155240 49772393Syz155240 /* 49782393Syz155240 * Remove from its position in the list. 49792393Syz155240 */ 49802393Syz155240 *ifq->ifq_pnext = ifq->ifq_next; 49812393Syz155240 if (ifq->ifq_next != NULL) 49822393Syz155240 ifq->ifq_next->ifq_pnext = ifq->ifq_pnext; 49832393Syz155240 49842393Syz155240 MUTEX_DESTROY(&ifq->ifq_lock); 49853448Sdh155122 ifs->ifs_fr_userifqs--; 49862393Syz155240 KFREE(ifq); 49872393Syz155240 } 49882393Syz155240 49892393Syz155240 49902393Syz155240 /* ------------------------------------------------------------------------ */ 49912393Syz155240 /* Function: fr_deletequeueentry */ 49922393Syz155240 /* Returns: Nil */ 49932393Syz155240 /* Parameters: tqe(I) - timeout queue entry to delete */ 49942393Syz155240 /* ifq(I) - timeout queue to remove entry from */ 49952393Syz155240 /* */ 49962393Syz155240 /* Remove a tail queue entry from its queue and make it an orphan. */ 49972393Syz155240 /* fr_deletetimeoutqueue is called to make sure the reference count on the */ 49982393Syz155240 /* queue is correct. We can't, however, call fr_freetimeoutqueue because */ 49992393Syz155240 /* the correct lock(s) may not be held that would make it safe to do so. */ 50002393Syz155240 /* ------------------------------------------------------------------------ */ 50012393Syz155240 void fr_deletequeueentry(tqe) 50022393Syz155240 ipftqent_t *tqe; 50032393Syz155240 { 50042393Syz155240 ipftq_t *ifq; 50052393Syz155240 50062393Syz155240 ifq = tqe->tqe_ifq; 50072393Syz155240 if (ifq == NULL) 50082393Syz155240 return; 50092393Syz155240 50102393Syz155240 MUTEX_ENTER(&ifq->ifq_lock); 50112393Syz155240 50122393Syz155240 if (tqe->tqe_pnext != NULL) { 50132393Syz155240 *tqe->tqe_pnext = tqe->tqe_next; 50142393Syz155240 if (tqe->tqe_next != NULL) 50152393Syz155240 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 50162393Syz155240 else /* we must be the tail anyway */ 50172393Syz155240 ifq->ifq_tail = tqe->tqe_pnext; 50182393Syz155240 50192393Syz155240 tqe->tqe_pnext = NULL; 50202393Syz155240 tqe->tqe_ifq = NULL; 50212393Syz155240 } 50222393Syz155240 50232393Syz155240 (void) fr_deletetimeoutqueue(ifq); 50242393Syz155240 50252393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 50262393Syz155240 } 50272393Syz155240 50282393Syz155240 50292393Syz155240 /* ------------------------------------------------------------------------ */ 50302393Syz155240 /* Function: fr_queuefront */ 50312393Syz155240 /* Returns: Nil */ 50322393Syz155240 /* Parameters: tqe(I) - pointer to timeout queue entry */ 50332393Syz155240 /* */ 50342393Syz155240 /* Move a queue entry to the front of the queue, if it isn't already there. */ 50352393Syz155240 /* ------------------------------------------------------------------------ */ 50362393Syz155240 void fr_queuefront(tqe) 50372393Syz155240 ipftqent_t *tqe; 50382393Syz155240 { 50392393Syz155240 ipftq_t *ifq; 50402393Syz155240 50412393Syz155240 ifq = tqe->tqe_ifq; 50422393Syz155240 if (ifq == NULL) 50432393Syz155240 return; 50442393Syz155240 50452393Syz155240 MUTEX_ENTER(&ifq->ifq_lock); 50462393Syz155240 if (ifq->ifq_head != tqe) { 50472393Syz155240 *tqe->tqe_pnext = tqe->tqe_next; 50482393Syz155240 if (tqe->tqe_next) 50492393Syz155240 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 50502393Syz155240 else 50512393Syz155240 ifq->ifq_tail = tqe->tqe_pnext; 50522393Syz155240 50532393Syz155240 tqe->tqe_next = ifq->ifq_head; 50542393Syz155240 ifq->ifq_head->tqe_pnext = &tqe->tqe_next; 50552393Syz155240 ifq->ifq_head = tqe; 50562393Syz155240 tqe->tqe_pnext = &ifq->ifq_head; 50572393Syz155240 } 50582393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 50592393Syz155240 } 50602393Syz155240 50612393Syz155240 50622393Syz155240 /* ------------------------------------------------------------------------ */ 50632393Syz155240 /* Function: fr_queueback */ 50642393Syz155240 /* Returns: Nil */ 50652393Syz155240 /* Parameters: tqe(I) - pointer to timeout queue entry */ 50662393Syz155240 /* */ 50672393Syz155240 /* Move a queue entry to the back of the queue, if it isn't already there. */ 50682393Syz155240 /* ------------------------------------------------------------------------ */ 50693448Sdh155122 void fr_queueback(tqe, ifs) 50702393Syz155240 ipftqent_t *tqe; 50713448Sdh155122 ipf_stack_t *ifs; 50722393Syz155240 { 50732393Syz155240 ipftq_t *ifq; 50742393Syz155240 50752393Syz155240 ifq = tqe->tqe_ifq; 50762393Syz155240 if (ifq == NULL) 50772393Syz155240 return; 50783448Sdh155122 tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl; 50792393Syz155240 50802393Syz155240 MUTEX_ENTER(&ifq->ifq_lock); 50812393Syz155240 if (tqe->tqe_next == NULL) { /* at the end already ? */ 50822393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 50832393Syz155240 return; 50842393Syz155240 } 50852393Syz155240 50862393Syz155240 /* 50872393Syz155240 * Remove from list 50882393Syz155240 */ 50892393Syz155240 *tqe->tqe_pnext = tqe->tqe_next; 50902393Syz155240 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 50912393Syz155240 50922393Syz155240 /* 50932393Syz155240 * Make it the last entry. 50942393Syz155240 */ 50952393Syz155240 tqe->tqe_next = NULL; 50962393Syz155240 tqe->tqe_pnext = ifq->ifq_tail; 50972393Syz155240 *ifq->ifq_tail = tqe; 50982393Syz155240 ifq->ifq_tail = &tqe->tqe_next; 50992393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 51002393Syz155240 } 51012393Syz155240 51022393Syz155240 51032393Syz155240 /* ------------------------------------------------------------------------ */ 51042393Syz155240 /* Function: fr_queueappend */ 51052393Syz155240 /* Returns: Nil */ 51062393Syz155240 /* Parameters: tqe(I) - pointer to timeout queue entry */ 51072393Syz155240 /* ifq(I) - pointer to timeout queue */ 51082393Syz155240 /* parent(I) - owing object pointer */ 51092393Syz155240 /* */ 51102393Syz155240 /* Add a new item to this queue and put it on the very end. */ 51112393Syz155240 /* ------------------------------------------------------------------------ */ 51123448Sdh155122 void fr_queueappend(tqe, ifq, parent, ifs) 51132393Syz155240 ipftqent_t *tqe; 51142393Syz155240 ipftq_t *ifq; 51152393Syz155240 void *parent; 51163448Sdh155122 ipf_stack_t *ifs; 51172393Syz155240 { 51182393Syz155240 51192393Syz155240 MUTEX_ENTER(&ifq->ifq_lock); 51202393Syz155240 tqe->tqe_parent = parent; 51212393Syz155240 tqe->tqe_pnext = ifq->ifq_tail; 51222393Syz155240 *ifq->ifq_tail = tqe; 51232393Syz155240 ifq->ifq_tail = &tqe->tqe_next; 51242393Syz155240 tqe->tqe_next = NULL; 51252393Syz155240 tqe->tqe_ifq = ifq; 51263448Sdh155122 tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl; 51272393Syz155240 ifq->ifq_ref++; 51282393Syz155240 MUTEX_EXIT(&ifq->ifq_lock); 51292393Syz155240 } 51302393Syz155240 51312393Syz155240 51322393Syz155240 /* ------------------------------------------------------------------------ */ 51332393Syz155240 /* Function: fr_movequeue */ 51342393Syz155240 /* Returns: Nil */ 51352393Syz155240 /* Parameters: tq(I) - pointer to timeout queue information */ 51362393Syz155240 /* oifp(I) - old timeout queue entry was on */ 51372393Syz155240 /* nifp(I) - new timeout queue to put entry on */ 51382393Syz155240 /* */ 51392393Syz155240 /* Move a queue entry from one timeout queue to another timeout queue. */ 51402393Syz155240 /* If it notices that the current entry is already last and does not need */ 51412393Syz155240 /* to move queue, the return. */ 51422393Syz155240 /* ------------------------------------------------------------------------ */ 51433448Sdh155122 void fr_movequeue(tqe, oifq, nifq, ifs) 51442393Syz155240 ipftqent_t *tqe; 51452393Syz155240 ipftq_t *oifq, *nifq; 51463448Sdh155122 ipf_stack_t *ifs; 51472393Syz155240 { 51482393Syz155240 /* 51492393Syz155240 * Is the operation here going to be a no-op ? 51502393Syz155240 */ 51512393Syz155240 MUTEX_ENTER(&oifq->ifq_lock); 51522393Syz155240 if (oifq == nifq && *oifq->ifq_tail == tqe) { 51532393Syz155240 MUTEX_EXIT(&oifq->ifq_lock); 51542393Syz155240 return; 51552393Syz155240 } 51562393Syz155240 51572393Syz155240 /* 51582393Syz155240 * Remove from the old queue 51592393Syz155240 */ 51602393Syz155240 *tqe->tqe_pnext = tqe->tqe_next; 51612393Syz155240 if (tqe->tqe_next) 51622393Syz155240 tqe->tqe_next->tqe_pnext = tqe->tqe_pnext; 51632393Syz155240 else 51642393Syz155240 oifq->ifq_tail = tqe->tqe_pnext; 51652393Syz155240 tqe->tqe_next = NULL; 51662393Syz155240 51672393Syz155240 /* 51682393Syz155240 * If we're moving from one queue to another, release the lock on the 51692393Syz155240 * old queue and get a lock on the new queue. For user defined queues, 51702393Syz155240 * if we're moving off it, call delete in case it can now be freed. 51712393Syz155240 */ 51722393Syz155240 if (oifq != nifq) { 51732393Syz155240 tqe->tqe_ifq = NULL; 51742393Syz155240 51752393Syz155240 (void) fr_deletetimeoutqueue(oifq); 51762393Syz155240 51772393Syz155240 MUTEX_EXIT(&oifq->ifq_lock); 51782393Syz155240 51792393Syz155240 MUTEX_ENTER(&nifq->ifq_lock); 51802393Syz155240 51812393Syz155240 tqe->tqe_ifq = nifq; 51822393Syz155240 nifq->ifq_ref++; 51832393Syz155240 } 51842393Syz155240 51852393Syz155240 /* 51862393Syz155240 * Add to the bottom of the new queue 51872393Syz155240 */ 51883448Sdh155122 tqe->tqe_die = ifs->ifs_fr_ticks + nifq->ifq_ttl; 51892393Syz155240 tqe->tqe_pnext = nifq->ifq_tail; 51902393Syz155240 *nifq->ifq_tail = tqe; 51912393Syz155240 nifq->ifq_tail = &tqe->tqe_next; 51922393Syz155240 MUTEX_EXIT(&nifq->ifq_lock); 51932393Syz155240 } 51942393Syz155240 51952393Syz155240 51962393Syz155240 /* ------------------------------------------------------------------------ */ 51972393Syz155240 /* Function: fr_updateipid */ 51982393Syz155240 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */ 51992393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 52002393Syz155240 /* */ 52012393Syz155240 /* When we are doing NAT, change the IP of every packet to represent a */ 52022393Syz155240 /* single sequence of packets coming from the host, hiding any host */ 52032393Syz155240 /* specific sequencing that might otherwise be revealed. If the packet is */ 52042393Syz155240 /* a fragment, then store the 'new' IPid in the fragment cache and look up */ 52052393Syz155240 /* the fragment cache for non-leading fragments. If a non-leading fragment */ 52062393Syz155240 /* has no match in the cache, return an error. */ 52072393Syz155240 /* ------------------------------------------------------------------------ */ 52082393Syz155240 static INLINE int fr_updateipid(fin) 52092393Syz155240 fr_info_t *fin; 52102393Syz155240 { 52112393Syz155240 u_short id, ido, sums; 52122393Syz155240 u_32_t sumd, sum; 52132393Syz155240 ip_t *ip; 52142393Syz155240 52152393Syz155240 if (fin->fin_off != 0) { 52162393Syz155240 sum = fr_ipid_knownfrag(fin); 52172393Syz155240 if (sum == 0xffffffff) 52182393Syz155240 return -1; 52192393Syz155240 sum &= 0xffff; 52202393Syz155240 id = (u_short)sum; 52212393Syz155240 } else { 52222393Syz155240 id = fr_nextipid(fin); 52232393Syz155240 if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0) 52242393Syz155240 (void) fr_ipid_newfrag(fin, (u_32_t)id); 52252393Syz155240 } 52262393Syz155240 52272393Syz155240 ip = fin->fin_ip; 52282393Syz155240 ido = ntohs(ip->ip_id); 52292393Syz155240 if (id == ido) 52302393Syz155240 return 0; 52312393Syz155240 ip->ip_id = htons(id); 52322393Syz155240 CALC_SUMD(ido, id, sumd); /* DESTRUCTIVE MACRO! id,ido change */ 52332393Syz155240 sum = (~ntohs(ip->ip_sum)) & 0xffff; 52342393Syz155240 sum += sumd; 52352393Syz155240 sum = (sum >> 16) + (sum & 0xffff); 52362393Syz155240 sum = (sum >> 16) + (sum & 0xffff); 52372393Syz155240 sums = ~(u_short)sum; 52382393Syz155240 ip->ip_sum = htons(sums); 52392393Syz155240 return 0; 52402393Syz155240 } 52412393Syz155240 52422393Syz155240 52432393Syz155240 #ifdef NEED_FRGETIFNAME 52442393Syz155240 /* ------------------------------------------------------------------------ */ 52452393Syz155240 /* Function: fr_getifname */ 52462393Syz155240 /* Returns: char * - pointer to interface name */ 52472393Syz155240 /* Parameters: ifp(I) - pointer to network interface */ 52482393Syz155240 /* buffer(O) - pointer to where to store interface name */ 52492393Syz155240 /* */ 52502393Syz155240 /* Constructs an interface name in the buffer passed. The buffer passed is */ 52512393Syz155240 /* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */ 52522393Syz155240 /* as a NULL pointer then return a pointer to a static array. */ 52532393Syz155240 /* ------------------------------------------------------------------------ */ 52542393Syz155240 char *fr_getifname(ifp, buffer) 52552393Syz155240 struct ifnet *ifp; 52562393Syz155240 char *buffer; 52572393Syz155240 { 52582393Syz155240 static char namebuf[LIFNAMSIZ]; 52592393Syz155240 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 52602393Syz155240 defined(__sgi) || defined(linux) || defined(_AIX51) || \ 52612393Syz155240 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 52622393Syz155240 int unit, space; 52632393Syz155240 char temp[20]; 52642393Syz155240 char *s; 52652393Syz155240 # endif 52662393Syz155240 52673448Sdh155122 ASSERT(buffer != NULL); 52683448Sdh155122 #ifdef notdef 52692393Syz155240 if (buffer == NULL) 52702393Syz155240 buffer = namebuf; 52713448Sdh155122 #endif 52722393Syz155240 (void) strncpy(buffer, ifp->if_name, LIFNAMSIZ); 52732393Syz155240 buffer[LIFNAMSIZ - 1] = '\0'; 52742393Syz155240 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \ 52752393Syz155240 defined(__sgi) || defined(_AIX51) || \ 52762393Syz155240 (defined(sun) && !defined(__SVR4) && !defined(__svr4__)) 52772393Syz155240 for (s = buffer; *s; s++) 52782393Syz155240 ; 52792393Syz155240 unit = ifp->if_unit; 52802393Syz155240 space = LIFNAMSIZ - (s - buffer); 52812393Syz155240 if (space > 0) { 52822393Syz155240 # if defined(SNPRINTF) && defined(_KERNEL) 52832393Syz155240 (void) SNPRINTF(temp, sizeof(temp), "%d", unit); 52842393Syz155240 # else 52852393Syz155240 (void) sprintf(temp, "%d", unit); 52862393Syz155240 # endif 52872393Syz155240 (void) strncpy(s, temp, space); 52882393Syz155240 } 52892393Syz155240 # endif 52902393Syz155240 return buffer; 52912393Syz155240 } 52922393Syz155240 #endif 52932393Syz155240 52942393Syz155240 52952393Syz155240 /* ------------------------------------------------------------------------ */ 52962393Syz155240 /* Function: fr_ioctlswitch */ 52972393Syz155240 /* Returns: int - -1 continue processing, else ioctl return value */ 52982393Syz155240 /* Parameters: unit(I) - device unit opened */ 52992393Syz155240 /* data(I) - pointer to ioctl data */ 53002393Syz155240 /* cmd(I) - ioctl command */ 53012393Syz155240 /* mode(I) - mode value */ 53022393Syz155240 /* */ 53032393Syz155240 /* Based on the value of unit, call the appropriate ioctl handler or return */ 53042393Syz155240 /* EIO if ipfilter is not running. Also checks if write perms are req'd */ 53052393Syz155240 /* for the device in order to execute the ioctl. */ 53062393Syz155240 /* ------------------------------------------------------------------------ */ 53073448Sdh155122 INLINE int fr_ioctlswitch(unit, data, cmd, mode, uid, ctx, ifs) 53083448Sdh155122 int unit, mode, uid; 53092393Syz155240 ioctlcmd_t cmd; 53103448Sdh155122 void *data, *ctx; 53113448Sdh155122 ipf_stack_t *ifs; 53122393Syz155240 { 53132393Syz155240 int error = 0; 53142393Syz155240 53152393Syz155240 switch (unit) 53162393Syz155240 { 53172393Syz155240 case IPL_LOGIPF : 53182393Syz155240 error = -1; 53192393Syz155240 break; 53202393Syz155240 case IPL_LOGNAT : 53213448Sdh155122 if (ifs->ifs_fr_running > 0) 53223448Sdh155122 error = fr_nat_ioctl(data, cmd, mode, uid, ctx, ifs); 53232393Syz155240 else 53242393Syz155240 error = EIO; 53252393Syz155240 break; 53262393Syz155240 case IPL_LOGSTATE : 53273448Sdh155122 if (ifs->ifs_fr_running > 0) 53283448Sdh155122 error = fr_state_ioctl(data, cmd, mode, uid, ctx, ifs); 53292393Syz155240 else 53302393Syz155240 error = EIO; 53312393Syz155240 break; 53322393Syz155240 case IPL_LOGAUTH : 53333448Sdh155122 if (ifs->ifs_fr_running > 0) { 53342393Syz155240 if ((cmd == (ioctlcmd_t)SIOCADAFR) || 53352393Syz155240 (cmd == (ioctlcmd_t)SIOCRMAFR)) { 53362393Syz155240 if (!(mode & FWRITE)) { 53372393Syz155240 error = EPERM; 53382393Syz155240 } else { 53392393Syz155240 error = frrequest(unit, cmd, data, 53403448Sdh155122 ifs->ifs_fr_active, 1, ifs); 53412393Syz155240 } 53422393Syz155240 } else { 53433448Sdh155122 error = fr_auth_ioctl(data, cmd, mode, uid, ctx, ifs); 53442393Syz155240 } 53452393Syz155240 } else 53462393Syz155240 error = EIO; 53472393Syz155240 break; 53482393Syz155240 case IPL_LOGSYNC : 53492393Syz155240 #ifdef IPFILTER_SYNC 53503448Sdh155122 if (ifs->ifs_fr_running > 0) 53513448Sdh155122 error = fr_sync_ioctl(data, cmd, mode, ifs); 53522393Syz155240 else 53532393Syz155240 #endif 53542393Syz155240 error = EIO; 53552393Syz155240 break; 53562393Syz155240 case IPL_LOGSCAN : 53572393Syz155240 #ifdef IPFILTER_SCAN 53583448Sdh155122 if (ifs->ifs_fr_running > 0) 53593448Sdh155122 error = fr_scan_ioctl(data, cmd, mode, ifs); 53602393Syz155240 else 53612393Syz155240 #endif 53622393Syz155240 error = EIO; 53632393Syz155240 break; 53642393Syz155240 case IPL_LOGLOOKUP : 53652393Syz155240 #ifdef IPFILTER_LOOKUP 53663448Sdh155122 if (ifs->ifs_fr_running > 0) 53673448Sdh155122 error = ip_lookup_ioctl(data, cmd, mode, uid, ctx, ifs); 53682393Syz155240 else 53692393Syz155240 #endif 53702393Syz155240 error = EIO; 53712393Syz155240 break; 53722393Syz155240 default : 53732393Syz155240 error = EIO; 53742393Syz155240 break; 53752393Syz155240 } 53762393Syz155240 53772393Syz155240 return error; 53782393Syz155240 } 53792393Syz155240 53802393Syz155240 53812393Syz155240 /* 53822393Syz155240 * This array defines the expected size of objects coming into the kernel 53832393Syz155240 * for the various recognised object types. 53842393Syz155240 */ 53853448Sdh155122 #define NUM_OBJ_TYPES 19 53862393Syz155240 53872393Syz155240 static int fr_objbytes[NUM_OBJ_TYPES][2] = { 53882393Syz155240 { 1, sizeof(struct frentry) }, /* frentry */ 53892393Syz155240 { 0, sizeof(struct friostat) }, 53902393Syz155240 { 0, sizeof(struct fr_info) }, 53912393Syz155240 { 0, sizeof(struct fr_authstat) }, 53922393Syz155240 { 0, sizeof(struct ipfrstat) }, 53932393Syz155240 { 0, sizeof(struct ipnat) }, 53942393Syz155240 { 0, sizeof(struct natstat) }, 53952393Syz155240 { 0, sizeof(struct ipstate_save) }, 53962393Syz155240 { 1, sizeof(struct nat_save) }, /* nat_save */ 53972393Syz155240 { 0, sizeof(struct natlookup) }, 53982393Syz155240 { 1, sizeof(struct ipstate) }, /* ipstate */ 53992393Syz155240 { 0, sizeof(struct ips_stat) }, 54002393Syz155240 { 0, sizeof(struct frauth) }, 54013448Sdh155122 { 0, sizeof(struct ipftune) }, 54023448Sdh155122 { 0, sizeof(struct nat) }, /* nat_t */ 54033448Sdh155122 { 0, sizeof(struct ipfruleiter) }, 54043448Sdh155122 { 0, sizeof(struct ipfgeniter) }, 54053448Sdh155122 { 0, sizeof(struct ipftable) }, 54063448Sdh155122 { 0, sizeof(struct ipflookupiter) } 54072393Syz155240 }; 54082393Syz155240 54092393Syz155240 54102393Syz155240 /* ------------------------------------------------------------------------ */ 54112393Syz155240 /* Function: fr_inobj */ 54122393Syz155240 /* Returns: int - 0 = success, else failure */ 54132393Syz155240 /* Parameters: data(I) - pointer to ioctl data */ 54142393Syz155240 /* ptr(I) - pointer to store real data in */ 54152393Syz155240 /* type(I) - type of structure being moved */ 54162393Syz155240 /* */ 54172393Syz155240 /* Copy in the contents of what the ipfobj_t points to. In future, we */ 54182393Syz155240 /* add things to check for version numbers, sizes, etc, to make it backward */ 54192393Syz155240 /* compatible at the ABI for user land. */ 54202393Syz155240 /* ------------------------------------------------------------------------ */ 54212393Syz155240 int fr_inobj(data, ptr, type) 54222393Syz155240 void *data; 54232393Syz155240 void *ptr; 54242393Syz155240 int type; 54252393Syz155240 { 54262393Syz155240 ipfobj_t obj; 54272393Syz155240 int error = 0; 54282393Syz155240 54292393Syz155240 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 54302393Syz155240 return EINVAL; 54312393Syz155240 54322393Syz155240 BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 54332393Syz155240 54342393Syz155240 if (obj.ipfo_type != type) 54352393Syz155240 return EINVAL; 54362393Syz155240 54372393Syz155240 #ifndef IPFILTER_COMPAT 54382393Syz155240 if ((fr_objbytes[type][0] & 1) != 0) { 54392393Syz155240 if (obj.ipfo_size < fr_objbytes[type][1]) 54402393Syz155240 return EINVAL; 54412393Syz155240 } else if (obj.ipfo_size != fr_objbytes[type][1]) 54422393Syz155240 return EINVAL; 54432393Syz155240 #else 54442393Syz155240 if (obj.ipfo_rev != IPFILTER_VERSION) 54452393Syz155240 /* XXX compatibility hook here */ 54462393Syz155240 ; 54472393Syz155240 if ((fr_objbytes[type][0] & 1) != 0) { 54482393Syz155240 if (obj.ipfo_size < fr_objbytes[type][1]) 54492393Syz155240 /* XXX compatibility hook here */ 54502393Syz155240 return EINVAL; 54512393Syz155240 } else if (obj.ipfo_size != fr_objbytes[type][1]) 54522393Syz155240 /* XXX compatibility hook here */ 54532393Syz155240 return EINVAL; 54542393Syz155240 #endif 54552393Syz155240 54562393Syz155240 if ((fr_objbytes[type][0] & 1) != 0) { 54572393Syz155240 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, 54582393Syz155240 fr_objbytes[type][1]); 54592393Syz155240 } else { 54602393Syz155240 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, 54612393Syz155240 obj.ipfo_size); 54622393Syz155240 } 54632393Syz155240 return error; 54642393Syz155240 } 54652393Syz155240 54662393Syz155240 54672393Syz155240 /* ------------------------------------------------------------------------ */ 54682393Syz155240 /* Function: fr_inobjsz */ 54692393Syz155240 /* Returns: int - 0 = success, else failure */ 54702393Syz155240 /* Parameters: data(I) - pointer to ioctl data */ 54712393Syz155240 /* ptr(I) - pointer to store real data in */ 54722393Syz155240 /* type(I) - type of structure being moved */ 54732393Syz155240 /* sz(I) - size of data to copy */ 54742393Syz155240 /* */ 54752393Syz155240 /* As per fr_inobj, except the size of the object to copy in is passed in */ 54762393Syz155240 /* but it must not be smaller than the size defined for the type and the */ 54772393Syz155240 /* type must allow for varied sized objects. The extra requirement here is */ 54782393Syz155240 /* that sz must match the size of the object being passed in - this is not */ 54792393Syz155240 /* not possible nor required in fr_inobj(). */ 54802393Syz155240 /* ------------------------------------------------------------------------ */ 54812393Syz155240 int fr_inobjsz(data, ptr, type, sz) 54822393Syz155240 void *data; 54832393Syz155240 void *ptr; 54842393Syz155240 int type, sz; 54852393Syz155240 { 54862393Syz155240 ipfobj_t obj; 54872393Syz155240 int error; 54882393Syz155240 54892393Syz155240 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 54902393Syz155240 return EINVAL; 54912393Syz155240 if (((fr_objbytes[type][0] & 1) == 0) || (sz < fr_objbytes[type][1])) 54922393Syz155240 return EINVAL; 54932393Syz155240 54942393Syz155240 BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 54952393Syz155240 54962393Syz155240 if (obj.ipfo_type != type) 54972393Syz155240 return EINVAL; 54982393Syz155240 54992393Syz155240 #ifndef IPFILTER_COMPAT 55002393Syz155240 if (obj.ipfo_size != sz) 55012393Syz155240 return EINVAL; 55022393Syz155240 #else 55032393Syz155240 if (obj.ipfo_rev != IPFILTER_VERSION) 55042393Syz155240 /* XXX compatibility hook here */ 55052393Syz155240 ; 55062393Syz155240 if (obj.ipfo_size != sz) 55072393Syz155240 /* XXX compatibility hook here */ 55082393Syz155240 return EINVAL; 55092393Syz155240 #endif 55102393Syz155240 55112393Syz155240 error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, sz); 55122393Syz155240 return error; 55132393Syz155240 } 55142393Syz155240 55152393Syz155240 55162393Syz155240 /* ------------------------------------------------------------------------ */ 55172393Syz155240 /* Function: fr_outobjsz */ 55182393Syz155240 /* Returns: int - 0 = success, else failure */ 55192393Syz155240 /* Parameters: data(I) - pointer to ioctl data */ 55202393Syz155240 /* ptr(I) - pointer to store real data in */ 55212393Syz155240 /* type(I) - type of structure being moved */ 55222393Syz155240 /* sz(I) - size of data to copy */ 55232393Syz155240 /* */ 55242393Syz155240 /* As per fr_outobj, except the size of the object to copy out is passed in */ 55252393Syz155240 /* but it must not be smaller than the size defined for the type and the */ 55262393Syz155240 /* type must allow for varied sized objects. The extra requirement here is */ 55272393Syz155240 /* that sz must match the size of the object being passed in - this is not */ 55282393Syz155240 /* not possible nor required in fr_outobj(). */ 55292393Syz155240 /* ------------------------------------------------------------------------ */ 55302393Syz155240 int fr_outobjsz(data, ptr, type, sz) 55312393Syz155240 void *data; 55322393Syz155240 void *ptr; 55332393Syz155240 int type, sz; 55342393Syz155240 { 55352393Syz155240 ipfobj_t obj; 55362393Syz155240 int error; 55372393Syz155240 55382393Syz155240 if ((type < 0) || (type > NUM_OBJ_TYPES-1) || 55392393Syz155240 ((fr_objbytes[type][0] & 1) == 0) || 55402393Syz155240 (sz < fr_objbytes[type][1])) 55412393Syz155240 return EINVAL; 55422393Syz155240 55432393Syz155240 BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 55442393Syz155240 55452393Syz155240 if (obj.ipfo_type != type) 55462393Syz155240 return EINVAL; 55472393Syz155240 55482393Syz155240 #ifndef IPFILTER_COMPAT 55492393Syz155240 if (obj.ipfo_size != sz) 55502393Syz155240 return EINVAL; 55512393Syz155240 #else 55522393Syz155240 if (obj.ipfo_rev != IPFILTER_VERSION) 55532393Syz155240 /* XXX compatibility hook here */ 55542393Syz155240 ; 55552393Syz155240 if (obj.ipfo_size != sz) 55562393Syz155240 /* XXX compatibility hook here */ 55572393Syz155240 return EINVAL; 55582393Syz155240 #endif 55592393Syz155240 55602393Syz155240 error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, sz); 55612393Syz155240 return error; 55622393Syz155240 } 55632393Syz155240 55642393Syz155240 55652393Syz155240 /* ------------------------------------------------------------------------ */ 55662393Syz155240 /* Function: fr_outobj */ 55672393Syz155240 /* Returns: int - 0 = success, else failure */ 55682393Syz155240 /* Parameters: data(I) - pointer to ioctl data */ 55692393Syz155240 /* ptr(I) - pointer to store real data in */ 55702393Syz155240 /* type(I) - type of structure being moved */ 55712393Syz155240 /* */ 55722393Syz155240 /* Copy out the contents of what ptr is to where ipfobj points to. In */ 55732393Syz155240 /* future, we add things to check for version numbers, sizes, etc, to make */ 55742393Syz155240 /* it backward compatible at the ABI for user land. */ 55752393Syz155240 /* ------------------------------------------------------------------------ */ 55762393Syz155240 int fr_outobj(data, ptr, type) 55772393Syz155240 void *data; 55782393Syz155240 void *ptr; 55792393Syz155240 int type; 55802393Syz155240 { 55812393Syz155240 ipfobj_t obj; 55822393Syz155240 int error; 55832393Syz155240 55842393Syz155240 if ((type < 0) || (type > NUM_OBJ_TYPES-1)) 55852393Syz155240 return EINVAL; 55862393Syz155240 55872393Syz155240 BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj)); 55882393Syz155240 55892393Syz155240 if (obj.ipfo_type != type) 55902393Syz155240 return EINVAL; 55912393Syz155240 55922393Syz155240 #ifndef IPFILTER_COMPAT 55932393Syz155240 if ((fr_objbytes[type][0] & 1) != 0) { 55942393Syz155240 if (obj.ipfo_size < fr_objbytes[type][1]) 55952393Syz155240 return EINVAL; 55962393Syz155240 } else if (obj.ipfo_size != fr_objbytes[type][1]) 55972393Syz155240 return EINVAL; 55982393Syz155240 #else 55992393Syz155240 if (obj.ipfo_rev != IPFILTER_VERSION) 56002393Syz155240 /* XXX compatibility hook here */ 56012393Syz155240 ; 56022393Syz155240 if ((fr_objbytes[type][0] & 1) != 0) { 56032393Syz155240 if (obj.ipfo_size < fr_objbytes[type][1]) 56042393Syz155240 /* XXX compatibility hook here */ 56052393Syz155240 return EINVAL; 56062393Syz155240 } else if (obj.ipfo_size != fr_objbytes[type][1]) 56072393Syz155240 /* XXX compatibility hook here */ 56082393Syz155240 return EINVAL; 56092393Syz155240 #endif 56102393Syz155240 56112393Syz155240 error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, obj.ipfo_size); 56122393Syz155240 return error; 56132393Syz155240 } 56142393Syz155240 56152393Syz155240 56162393Syz155240 /* ------------------------------------------------------------------------ */ 56172393Syz155240 /* Function: fr_checkl4sum */ 56182393Syz155240 /* Returns: int - 0 = good, -1 = bad, 1 = cannot check */ 56192393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 56202393Syz155240 /* */ 56212393Syz155240 /* If possible, calculate the layer 4 checksum for the packet. If this is */ 56222393Syz155240 /* not possible, return without indicating a failure or success but in a */ 56232393Syz155240 /* way that is ditinguishable. */ 56242393Syz155240 /* ------------------------------------------------------------------------ */ 56252393Syz155240 int fr_checkl4sum(fin) 56262393Syz155240 fr_info_t *fin; 56272393Syz155240 { 56282393Syz155240 u_short sum, hdrsum, *csump; 56292393Syz155240 udphdr_t *udp; 56302393Syz155240 int dosum; 56313448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 56322393Syz155240 56332958Sdr146992 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 56342958Sdr146992 net_data_t net_data_p; 56352958Sdr146992 if (fin->fin_v == 4) 56363448Sdh155122 net_data_p = ifs->ifs_ipf_ipv4; 56372958Sdr146992 else 56383448Sdh155122 net_data_p = ifs->ifs_ipf_ipv6; 56392958Sdr146992 #endif 56402958Sdr146992 56412393Syz155240 if ((fin->fin_flx & FI_NOCKSUM) != 0) 56422393Syz155240 return 0; 56432393Syz155240 56442393Syz155240 /* 56452393Syz155240 * If the TCP packet isn't a fragment, isn't too short and otherwise 56462393Syz155240 * isn't already considered "bad", then validate the checksum. If 56472393Syz155240 * this check fails then considered the packet to be "bad". 56482393Syz155240 */ 56492393Syz155240 if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0) 56502393Syz155240 return 1; 56512393Syz155240 56522393Syz155240 csump = NULL; 56532393Syz155240 hdrsum = 0; 56542393Syz155240 dosum = 0; 56552393Syz155240 sum = 0; 56562393Syz155240 56572958Sdr146992 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 56582958Sdr146992 ASSERT(fin->fin_m != NULL); 56592958Sdr146992 if (NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) || 56602958Sdr146992 NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) { 56612958Sdr146992 hdrsum = 0; 56622958Sdr146992 sum = 0; 56632393Syz155240 } else { 56642393Syz155240 #endif 56652393Syz155240 switch (fin->fin_p) 56662393Syz155240 { 56672393Syz155240 case IPPROTO_TCP : 56682393Syz155240 csump = &((tcphdr_t *)fin->fin_dp)->th_sum; 56692393Syz155240 dosum = 1; 56702393Syz155240 break; 56712393Syz155240 56722393Syz155240 case IPPROTO_UDP : 56732393Syz155240 udp = fin->fin_dp; 56742393Syz155240 if (udp->uh_sum != 0) { 56752393Syz155240 csump = &udp->uh_sum; 56762393Syz155240 dosum = 1; 56772393Syz155240 } 56782393Syz155240 break; 56792393Syz155240 56802393Syz155240 case IPPROTO_ICMP : 56812393Syz155240 csump = &((struct icmp *)fin->fin_dp)->icmp_cksum; 56822393Syz155240 dosum = 1; 56832393Syz155240 break; 56842393Syz155240 56852393Syz155240 default : 56862393Syz155240 return 1; 56872393Syz155240 /*NOTREACHED*/ 56882393Syz155240 } 56892393Syz155240 56902393Syz155240 if (csump != NULL) 56912393Syz155240 hdrsum = *csump; 56922393Syz155240 56932393Syz155240 if (dosum) 56942393Syz155240 sum = fr_cksum(fin->fin_m, fin->fin_ip, 56952393Syz155240 fin->fin_p, fin->fin_dp); 56962958Sdr146992 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 56972393Syz155240 } 56982393Syz155240 #endif 56992393Syz155240 #if !defined(_KERNEL) 57002393Syz155240 if (sum == hdrsum) { 57012393Syz155240 FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum)); 57022393Syz155240 } else { 57032393Syz155240 FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum)); 57042393Syz155240 } 57052393Syz155240 #endif 57062393Syz155240 if (hdrsum == sum) 57072393Syz155240 return 0; 57082393Syz155240 return -1; 57092393Syz155240 } 57102393Syz155240 57112393Syz155240 57122393Syz155240 /* ------------------------------------------------------------------------ */ 57132393Syz155240 /* Function: fr_ifpfillv4addr */ 57142393Syz155240 /* Returns: int - 0 = address update, -1 = address not updated */ 57152393Syz155240 /* Parameters: atype(I) - type of network address update to perform */ 57162393Syz155240 /* sin(I) - pointer to source of address information */ 57172393Syz155240 /* mask(I) - pointer to source of netmask information */ 57182393Syz155240 /* inp(I) - pointer to destination address store */ 57192393Syz155240 /* inpmask(I) - pointer to destination netmask store */ 57202393Syz155240 /* */ 57212393Syz155240 /* Given a type of network address update (atype) to perform, copy */ 57222393Syz155240 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 57232393Syz155240 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 57242393Syz155240 /* which case the operation fails. For all values of atype other than */ 57252393Syz155240 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 57262393Syz155240 /* value. */ 57272393Syz155240 /* ------------------------------------------------------------------------ */ 57282393Syz155240 int fr_ifpfillv4addr(atype, sin, mask, inp, inpmask) 57292393Syz155240 int atype; 57302393Syz155240 struct sockaddr_in *sin, *mask; 57312393Syz155240 struct in_addr *inp, *inpmask; 57322393Syz155240 { 57332393Syz155240 if (inpmask != NULL && atype != FRI_NETMASKED) 57342393Syz155240 inpmask->s_addr = 0xffffffff; 57352393Syz155240 57362393Syz155240 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 57372393Syz155240 if (atype == FRI_NETMASKED) { 57382393Syz155240 if (inpmask == NULL) 57392393Syz155240 return -1; 57402393Syz155240 inpmask->s_addr = mask->sin_addr.s_addr; 57412393Syz155240 } 57422393Syz155240 inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr; 57432393Syz155240 } else { 57442393Syz155240 inp->s_addr = sin->sin_addr.s_addr; 57452393Syz155240 } 57462393Syz155240 return 0; 57472393Syz155240 } 57482393Syz155240 57492393Syz155240 57502393Syz155240 #ifdef USE_INET6 57512393Syz155240 /* ------------------------------------------------------------------------ */ 57522393Syz155240 /* Function: fr_ifpfillv6addr */ 57532393Syz155240 /* Returns: int - 0 = address update, -1 = address not updated */ 57542393Syz155240 /* Parameters: atype(I) - type of network address update to perform */ 57552393Syz155240 /* sin(I) - pointer to source of address information */ 57562393Syz155240 /* mask(I) - pointer to source of netmask information */ 57572393Syz155240 /* inp(I) - pointer to destination address store */ 57582393Syz155240 /* inpmask(I) - pointer to destination netmask store */ 57592393Syz155240 /* */ 57602393Syz155240 /* Given a type of network address update (atype) to perform, copy */ 57612393Syz155240 /* information from sin/mask into inp/inpmask. If ipnmask is NULL then no */ 57622393Syz155240 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in */ 57632393Syz155240 /* which case the operation fails. For all values of atype other than */ 57642393Syz155240 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */ 57652393Syz155240 /* value. */ 57662393Syz155240 /* ------------------------------------------------------------------------ */ 57672393Syz155240 int fr_ifpfillv6addr(atype, sin, mask, inp, inpmask) 57682393Syz155240 int atype; 57692393Syz155240 struct sockaddr_in6 *sin, *mask; 57702393Syz155240 struct in_addr *inp, *inpmask; 57712393Syz155240 { 57722393Syz155240 i6addr_t *src, *dst, *and, *dmask; 57732393Syz155240 57742393Syz155240 src = (i6addr_t *)&sin->sin6_addr; 57752393Syz155240 and = (i6addr_t *)&mask->sin6_addr; 57762393Syz155240 dst = (i6addr_t *)inp; 57772393Syz155240 dmask = (i6addr_t *)inpmask; 57782393Syz155240 57792393Syz155240 if (inpmask != NULL && atype != FRI_NETMASKED) { 57802393Syz155240 dmask->i6[0] = 0xffffffff; 57812393Syz155240 dmask->i6[1] = 0xffffffff; 57822393Syz155240 dmask->i6[2] = 0xffffffff; 57832393Syz155240 dmask->i6[3] = 0xffffffff; 57842393Syz155240 } 57852393Syz155240 57862393Syz155240 if (atype == FRI_NETWORK || atype == FRI_NETMASKED) { 57872393Syz155240 if (atype == FRI_NETMASKED) { 57882393Syz155240 if (inpmask == NULL) 57892393Syz155240 return -1; 57902393Syz155240 dmask->i6[0] = and->i6[0]; 57912393Syz155240 dmask->i6[1] = and->i6[1]; 57922393Syz155240 dmask->i6[2] = and->i6[2]; 57932393Syz155240 dmask->i6[3] = and->i6[3]; 57942393Syz155240 } 57952393Syz155240 57962393Syz155240 dst->i6[0] = src->i6[0] & and->i6[0]; 57972393Syz155240 dst->i6[1] = src->i6[1] & and->i6[1]; 57982393Syz155240 dst->i6[2] = src->i6[2] & and->i6[2]; 57992393Syz155240 dst->i6[3] = src->i6[3] & and->i6[3]; 58002393Syz155240 } else { 58012393Syz155240 dst->i6[0] = src->i6[0]; 58022393Syz155240 dst->i6[1] = src->i6[1]; 58032393Syz155240 dst->i6[2] = src->i6[2]; 58042393Syz155240 dst->i6[3] = src->i6[3]; 58052393Syz155240 } 58062393Syz155240 return 0; 58072393Syz155240 } 58082393Syz155240 #endif 58092393Syz155240 58102393Syz155240 58112393Syz155240 /* ------------------------------------------------------------------------ */ 58122393Syz155240 /* Function: fr_matchtag */ 58132393Syz155240 /* Returns: 0 == mismatch, 1 == match. */ 58142393Syz155240 /* Parameters: tag1(I) - pointer to first tag to compare */ 58152393Syz155240 /* tag2(I) - pointer to second tag to compare */ 58162393Syz155240 /* */ 58172393Syz155240 /* Returns true (non-zero) or false(0) if the two tag structures can be */ 58182393Syz155240 /* considered to be a match or not match, respectively. The tag is 16 */ 58192393Syz155240 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so */ 58202393Syz155240 /* compare the ints instead, for speed. tag1 is the master of the */ 58212393Syz155240 /* comparison. This function should only be called with both tag1 and tag2 */ 58222393Syz155240 /* as non-NULL pointers. */ 58232393Syz155240 /* ------------------------------------------------------------------------ */ 58242393Syz155240 int fr_matchtag(tag1, tag2) 58252393Syz155240 ipftag_t *tag1, *tag2; 58262393Syz155240 { 58272393Syz155240 if (tag1 == tag2) 58282393Syz155240 return 1; 58292393Syz155240 58302393Syz155240 if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0)) 58312393Syz155240 return 1; 58322393Syz155240 58332393Syz155240 if ((tag1->ipt_num[0] == tag2->ipt_num[0]) && 58342393Syz155240 (tag1->ipt_num[1] == tag2->ipt_num[1]) && 58352393Syz155240 (tag1->ipt_num[2] == tag2->ipt_num[2]) && 58362393Syz155240 (tag1->ipt_num[3] == tag2->ipt_num[3])) 58372393Syz155240 return 1; 58382393Syz155240 return 0; 58392393Syz155240 } 58402393Syz155240 58412393Syz155240 58422393Syz155240 /* ------------------------------------------------------------------------ */ 58432393Syz155240 /* Function: fr_coalesce */ 58442393Syz155240 /* Returns: 1 == success, -1 == failure, 0 == no change */ 58452393Syz155240 /* Parameters: fin(I) - pointer to packet information */ 58462393Syz155240 /* */ 58472393Syz155240 /* Attempt to get all of the packet data into a single, contiguous buffer. */ 58482393Syz155240 /* If this call returns a failure then the buffers have also been freed. */ 58492393Syz155240 /* ------------------------------------------------------------------------ */ 58502393Syz155240 int fr_coalesce(fin) 58512393Syz155240 fr_info_t *fin; 58522393Syz155240 { 58533448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 58542393Syz155240 if ((fin->fin_flx & FI_COALESCE) != 0) 58552393Syz155240 return 1; 58562393Syz155240 58572393Syz155240 /* 58582393Syz155240 * If the mbuf pointers indicate that there is no mbuf to work with, 58592393Syz155240 * return but do not indicate success or failure. 58602393Syz155240 */ 58612393Syz155240 if (fin->fin_m == NULL || fin->fin_mp == NULL) 58622393Syz155240 return 0; 58632393Syz155240 58642393Syz155240 #if defined(_KERNEL) 58652393Syz155240 if (fr_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) { 58663448Sdh155122 ATOMIC_INCL(ifs->ifs_fr_badcoalesces[fin->fin_out]); 58672393Syz155240 # ifdef MENTAT 58682393Syz155240 FREE_MB_T(*fin->fin_mp); 58692393Syz155240 # endif 58702393Syz155240 *fin->fin_mp = NULL; 58712393Syz155240 fin->fin_m = NULL; 58722393Syz155240 return -1; 58732393Syz155240 } 58742393Syz155240 #else 58752393Syz155240 fin = fin; /* LINT */ 58762393Syz155240 #endif 58772393Syz155240 return 1; 58782393Syz155240 } 58792393Syz155240 58802393Syz155240 58812393Syz155240 /* 58822393Syz155240 * The following table lists all of the tunable variables that can be 58833448Sdh155122 * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT. The format of each row 58842393Syz155240 * in the table below is as follows: 58852393Syz155240 * 58862393Syz155240 * pointer to value, name of value, minimum, maximum, size of the value's 58872393Syz155240 * container, value attribute flags 58882393Syz155240 * 58892393Syz155240 * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED 58902393Syz155240 * means the value can only be written to when IPFilter is loaded but disabled. 58912393Syz155240 * The obvious implication is if neither of these are set then the value can be 58922393Syz155240 * changed at any time without harm. 58932393Syz155240 */ 58943448Sdh155122 ipftuneable_t lcl_ipf_tuneables[] = { 58952393Syz155240 /* filtering */ 58963448Sdh155122 { { NULL }, "fr_flags", 0, 0xffffffff, 58973448Sdh155122 0, 0 }, 58983448Sdh155122 { { NULL }, "fr_active", 0, 0, 58993448Sdh155122 0, IPFT_RDONLY }, 59003448Sdh155122 { { NULL }, "fr_control_forwarding", 0, 1, 59013448Sdh155122 0, 0 }, 59023448Sdh155122 { { NULL }, "fr_update_ipid", 0, 1, 59033448Sdh155122 0, 0 }, 59043448Sdh155122 { { NULL }, "fr_chksrc", 0, 1, 59053448Sdh155122 0, 0 }, 59063448Sdh155122 { { NULL }, "fr_minttl", 0, 1, 59073448Sdh155122 0, 0 }, 59083448Sdh155122 { { NULL }, "fr_icmpminfragmtu", 0, 1, 59093448Sdh155122 0, 0 }, 59103448Sdh155122 { { NULL }, "fr_pass", 0, 0xffffffff, 59113448Sdh155122 0, 0 }, 59122958Sdr146992 #if SOLARIS2 >= 10 59133448Sdh155122 { { NULL }, "ipf_loopback", 0, 1, 59143448Sdh155122 0, IPFT_WRDISABLED }, 59152958Sdr146992 #endif 59162393Syz155240 /* state */ 59173448Sdh155122 { { NULL }, "fr_tcpidletimeout", 1, 0x7fffffff, 59183448Sdh155122 0, IPFT_WRDISABLED }, 59193448Sdh155122 { { NULL }, "fr_tcpclosewait", 1, 0x7fffffff, 59203448Sdh155122 0, IPFT_WRDISABLED }, 59213448Sdh155122 { { NULL }, "fr_tcplastack", 1, 0x7fffffff, 59223448Sdh155122 0, IPFT_WRDISABLED }, 59233448Sdh155122 { { NULL }, "fr_tcptimeout", 1, 0x7fffffff, 59243448Sdh155122 0, IPFT_WRDISABLED }, 59253448Sdh155122 { { NULL }, "fr_tcpclosed", 1, 0x7fffffff, 59263448Sdh155122 0, IPFT_WRDISABLED }, 59273448Sdh155122 { { NULL }, "fr_tcphalfclosed", 1, 0x7fffffff, 59283448Sdh155122 0, IPFT_WRDISABLED }, 59293448Sdh155122 { { NULL }, "fr_udptimeout", 1, 0x7fffffff, 59303448Sdh155122 0, IPFT_WRDISABLED }, 59313448Sdh155122 { { NULL }, "fr_udpacktimeout", 1, 0x7fffffff, 59323448Sdh155122 0, IPFT_WRDISABLED }, 59333448Sdh155122 { { NULL }, "fr_icmptimeout", 1, 0x7fffffff, 59343448Sdh155122 0, IPFT_WRDISABLED }, 59353448Sdh155122 { { NULL }, "fr_icmpacktimeout", 1, 0x7fffffff, 59363448Sdh155122 0, IPFT_WRDISABLED }, 59373448Sdh155122 { { NULL }, "fr_iptimeout", 1, 0x7fffffff, 59383448Sdh155122 0, IPFT_WRDISABLED }, 59393448Sdh155122 { { NULL }, "fr_statemax", 1, 0x7fffffff, 59403448Sdh155122 0, 0 }, 59413448Sdh155122 { { NULL }, "fr_statesize", 1, 0x7fffffff, 59423448Sdh155122 0, IPFT_WRDISABLED }, 59433448Sdh155122 { { NULL }, "fr_state_lock", 0, 1, 59443448Sdh155122 0, IPFT_RDONLY }, 59453448Sdh155122 { { NULL }, "fr_state_maxbucket", 1, 0x7fffffff, 59463448Sdh155122 0, IPFT_WRDISABLED }, 59473448Sdh155122 { { NULL }, "fr_state_maxbucket_reset", 0, 1, 59483448Sdh155122 0, IPFT_WRDISABLED }, 59493448Sdh155122 { { NULL }, "ipstate_logging", 0, 1, 59503448Sdh155122 0, 0 }, 59512393Syz155240 /* nat */ 59523448Sdh155122 { { NULL }, "fr_nat_lock", 0, 1, 59533448Sdh155122 0, IPFT_RDONLY }, 59543448Sdh155122 { { NULL }, "ipf_nattable_sz", 1, 0x7fffffff, 59553448Sdh155122 0, IPFT_WRDISABLED }, 59563448Sdh155122 { { NULL }, "ipf_nattable_max", 1, 0x7fffffff, 59573448Sdh155122 0, 0 }, 59583448Sdh155122 { { NULL }, "ipf_natrules_sz", 1, 0x7fffffff, 59593448Sdh155122 0, IPFT_WRDISABLED }, 59603448Sdh155122 { { NULL }, "ipf_rdrrules_sz", 1, 0x7fffffff, 59613448Sdh155122 0, IPFT_WRDISABLED }, 59623448Sdh155122 { { NULL }, "ipf_hostmap_sz", 1, 0x7fffffff, 59633448Sdh155122 0, IPFT_WRDISABLED }, 59643448Sdh155122 { { NULL }, "fr_nat_maxbucket", 1, 0x7fffffff, 59653448Sdh155122 0, IPFT_WRDISABLED }, 59663448Sdh155122 { { NULL }, "fr_nat_maxbucket_reset", 0, 1, 59673448Sdh155122 0, IPFT_WRDISABLED }, 59683448Sdh155122 { { NULL }, "nat_logging", 0, 1, 59693448Sdh155122 0, 0 }, 59703448Sdh155122 { { NULL }, "fr_defnatage", 1, 0x7fffffff, 59713448Sdh155122 0, IPFT_WRDISABLED }, 59723448Sdh155122 { { NULL }, "fr_defnatipage", 1, 0x7fffffff, 59733448Sdh155122 0, IPFT_WRDISABLED }, 59743448Sdh155122 { { NULL }, "fr_defnaticmpage", 1, 0x7fffffff, 59753448Sdh155122 0, IPFT_WRDISABLED }, 59762393Syz155240 /* frag */ 59773448Sdh155122 { { NULL }, "ipfr_size", 1, 0x7fffffff, 59783448Sdh155122 0, IPFT_WRDISABLED }, 59793448Sdh155122 { { NULL }, "fr_ipfrttl", 1, 0x7fffffff, 59803448Sdh155122 0, IPFT_WRDISABLED }, 59812393Syz155240 #ifdef IPFILTER_LOG 59822393Syz155240 /* log */ 59833448Sdh155122 { { NULL }, "ipl_suppress", 0, 1, 59843448Sdh155122 0, 0 }, 59853448Sdh155122 { { NULL }, "ipl_buffer_sz", 0, 0, 59863448Sdh155122 0, IPFT_RDONLY }, 59873448Sdh155122 { { NULL }, "ipl_logmax", 0, 0x7fffffff, 59883448Sdh155122 0, IPFT_WRDISABLED }, 59893448Sdh155122 { { NULL }, "ipl_logall", 0, 1, 59903448Sdh155122 0, 0 }, 59913448Sdh155122 { { NULL }, "ipl_logsize", 0, 0x80000, 59923448Sdh155122 0, 0 }, 59932393Syz155240 #endif 59942393Syz155240 { { NULL }, NULL, 0, 0 } 59952393Syz155240 }; 59962393Syz155240 59973448Sdh155122 static ipftuneable_t * 59983448Sdh155122 tune_lookup(ipf_stack_t *ifs, char *name) 59993448Sdh155122 { 60003448Sdh155122 int i; 60013448Sdh155122 60023448Sdh155122 for (i = 0; ifs->ifs_ipf_tuneables[i].ipft_name != NULL; i++) { 60033448Sdh155122 if (strcmp(ifs->ifs_ipf_tuneables[i].ipft_name, name) == 0) 60043448Sdh155122 return (&ifs->ifs_ipf_tuneables[i]); 60053448Sdh155122 } 60063448Sdh155122 return (NULL); 60073448Sdh155122 } 60083448Sdh155122 60093448Sdh155122 #ifdef _KERNEL 60103448Sdh155122 extern dev_info_t *ipf_dev_info; 60113448Sdh155122 extern int ipf_property_update __P((dev_info_t *, ipf_stack_t *)); 60123448Sdh155122 #endif 60133448Sdh155122 /* 60143448Sdh155122 * Allocate a per-stack tuneable and copy in the names. Then 60153448Sdh155122 * set it to point to each of the per-stack tunables. 60163448Sdh155122 */ 60173448Sdh155122 void 60183448Sdh155122 ipftuneable_alloc(ipf_stack_t *ifs) 60193448Sdh155122 { 60203448Sdh155122 ipftuneable_t *item; 60213448Sdh155122 60223448Sdh155122 KMALLOCS(ifs->ifs_ipf_tuneables, ipftuneable_t *, 60233448Sdh155122 sizeof (lcl_ipf_tuneables)); 60243448Sdh155122 bcopy(lcl_ipf_tuneables, ifs->ifs_ipf_tuneables, 60253448Sdh155122 sizeof (lcl_ipf_tuneables)); 60263448Sdh155122 60273448Sdh155122 #define TUNE_SET(_ifs, _name, _field) \ 60283448Sdh155122 item = tune_lookup((_ifs), (_name)); \ 60293448Sdh155122 if (item != NULL) { \ 60303448Sdh155122 item->ipft_una.ipftp_int = (unsigned int *)&((_ifs)->_field); \ 60313448Sdh155122 item->ipft_sz = sizeof ((_ifs)->_field); \ 60323448Sdh155122 } 60333448Sdh155122 60343448Sdh155122 TUNE_SET(ifs, "fr_flags", ifs_fr_flags); 60353448Sdh155122 TUNE_SET(ifs, "fr_active", ifs_fr_active); 60363448Sdh155122 TUNE_SET(ifs, "fr_control_forwarding", ifs_fr_control_forwarding); 60373448Sdh155122 TUNE_SET(ifs, "fr_update_ipid", ifs_fr_update_ipid); 60383448Sdh155122 TUNE_SET(ifs, "fr_chksrc", ifs_fr_chksrc); 60393448Sdh155122 TUNE_SET(ifs, "fr_minttl", ifs_fr_minttl); 60403448Sdh155122 TUNE_SET(ifs, "fr_icmpminfragmtu", ifs_fr_icmpminfragmtu); 60413448Sdh155122 TUNE_SET(ifs, "fr_pass", ifs_fr_pass); 60423448Sdh155122 TUNE_SET(ifs, "fr_tcpidletimeout", ifs_fr_tcpidletimeout); 60433448Sdh155122 TUNE_SET(ifs, "fr_tcpclosewait", ifs_fr_tcpclosewait); 60443448Sdh155122 TUNE_SET(ifs, "fr_tcplastack", ifs_fr_tcplastack); 60453448Sdh155122 TUNE_SET(ifs, "fr_tcptimeout", ifs_fr_tcptimeout); 60463448Sdh155122 TUNE_SET(ifs, "fr_tcpclosed", ifs_fr_tcpclosed); 60473448Sdh155122 TUNE_SET(ifs, "fr_tcphalfclosed", ifs_fr_tcphalfclosed); 60483448Sdh155122 TUNE_SET(ifs, "fr_udptimeout", ifs_fr_udptimeout); 60493448Sdh155122 TUNE_SET(ifs, "fr_udpacktimeout", ifs_fr_udpacktimeout); 60503448Sdh155122 TUNE_SET(ifs, "fr_icmptimeout", ifs_fr_icmptimeout); 60513448Sdh155122 TUNE_SET(ifs, "fr_icmpacktimeout", ifs_fr_icmpacktimeout); 60523448Sdh155122 TUNE_SET(ifs, "fr_iptimeout", ifs_fr_iptimeout); 60533448Sdh155122 TUNE_SET(ifs, "fr_statemax", ifs_fr_statemax); 60543448Sdh155122 TUNE_SET(ifs, "fr_statesize", ifs_fr_statesize); 60553448Sdh155122 TUNE_SET(ifs, "fr_state_lock", ifs_fr_state_lock); 60563448Sdh155122 TUNE_SET(ifs, "fr_state_maxbucket", ifs_fr_state_maxbucket); 60573448Sdh155122 TUNE_SET(ifs, "fr_state_maxbucket_reset", ifs_fr_state_maxbucket_reset); 60583448Sdh155122 TUNE_SET(ifs, "ipstate_logging", ifs_ipstate_logging); 60593448Sdh155122 TUNE_SET(ifs, "fr_nat_lock", ifs_fr_nat_lock); 60603448Sdh155122 TUNE_SET(ifs, "ipf_nattable_sz", ifs_ipf_nattable_sz); 60613448Sdh155122 TUNE_SET(ifs, "ipf_nattable_max", ifs_ipf_nattable_max); 60623448Sdh155122 TUNE_SET(ifs, "ipf_natrules_sz", ifs_ipf_natrules_sz); 60633448Sdh155122 TUNE_SET(ifs, "ipf_rdrrules_sz", ifs_ipf_rdrrules_sz); 60643448Sdh155122 TUNE_SET(ifs, "ipf_hostmap_sz", ifs_ipf_hostmap_sz); 60653448Sdh155122 TUNE_SET(ifs, "fr_nat_maxbucket", ifs_fr_nat_maxbucket); 60663448Sdh155122 TUNE_SET(ifs, "fr_nat_maxbucket_reset", ifs_fr_nat_maxbucket_reset); 60673448Sdh155122 TUNE_SET(ifs, "nat_logging", ifs_nat_logging); 60683448Sdh155122 TUNE_SET(ifs, "fr_defnatage", ifs_fr_defnatage); 60693448Sdh155122 TUNE_SET(ifs, "fr_defnaticmpage", ifs_fr_defnaticmpage); 60703448Sdh155122 TUNE_SET(ifs, "ipfr_size", ifs_ipfr_size); 60713448Sdh155122 TUNE_SET(ifs, "fr_ipfrttl", ifs_fr_ipfrttl); 60723448Sdh155122 #ifdef IPFILTER_LOG 60733448Sdh155122 TUNE_SET(ifs, "ipl_suppress", ifs_ipl_suppress); 60743448Sdh155122 TUNE_SET(ifs, "ipl_buffer_sz", ifs_ipl_buffer_sz); 60753448Sdh155122 TUNE_SET(ifs, "ipl_logmax", ifs_ipl_logmax); 60763448Sdh155122 TUNE_SET(ifs, "ipl_logall", ifs_ipl_logall); 60773448Sdh155122 TUNE_SET(ifs, "ipl_logsize", ifs_ipl_logsize); 60783448Sdh155122 #endif 60793448Sdh155122 #undef TUNE_SET 60803448Sdh155122 60813448Sdh155122 #ifdef _KERNEL 60823448Sdh155122 (void) ipf_property_update(ipf_dev_info, ifs); 60833448Sdh155122 #endif 60843448Sdh155122 } 60853448Sdh155122 60863448Sdh155122 void 60873448Sdh155122 ipftuneable_free(ipf_stack_t *ifs) 60883448Sdh155122 { 60893448Sdh155122 KFREES(ifs->ifs_ipf_tuneables, sizeof (lcl_ipf_tuneables)); 60903448Sdh155122 ifs->ifs_ipf_tuneables = NULL; 60913448Sdh155122 } 60922393Syz155240 60932393Syz155240 /* ------------------------------------------------------------------------ */ 60942393Syz155240 /* Function: fr_findtunebycookie */ 60952393Syz155240 /* Returns: NULL = search failed, else pointer to tune struct */ 60962393Syz155240 /* Parameters: cookie(I) - cookie value to search for amongst tuneables */ 60972393Syz155240 /* next(O) - pointer to place to store the cookie for the */ 60982393Syz155240 /* "next" tuneable, if it is desired. */ 60992393Syz155240 /* */ 61002393Syz155240 /* This function is used to walk through all of the existing tunables with */ 61012393Syz155240 /* successive calls. It searches the known tunables for the one which has */ 61022393Syz155240 /* a matching value for "cookie" - ie its address. When returning a match, */ 61032393Syz155240 /* the next one to be found may be returned inside next. */ 61042393Syz155240 /* ------------------------------------------------------------------------ */ 61053448Sdh155122 static ipftuneable_t *fr_findtunebycookie(cookie, next, ifs) 61062393Syz155240 void *cookie, **next; 61073448Sdh155122 ipf_stack_t * ifs; 61082393Syz155240 { 61092393Syz155240 ipftuneable_t *ta, **tap; 61102393Syz155240 61113448Sdh155122 for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++) 61122393Syz155240 if (ta == cookie) { 61132393Syz155240 if (next != NULL) { 61142393Syz155240 /* 61152393Syz155240 * If the next entry in the array has a name 61162393Syz155240 * present, then return a pointer to it for 61172393Syz155240 * where to go next, else return a pointer to 61182393Syz155240 * the dynaminc list as a key to search there 61192393Syz155240 * next. This facilitates a weak linking of 61202393Syz155240 * the two "lists" together. 61212393Syz155240 */ 61222393Syz155240 if ((ta + 1)->ipft_name != NULL) 61232393Syz155240 *next = ta + 1; 61242393Syz155240 else 61253448Sdh155122 *next = &ifs->ifs_ipf_tunelist; 61262393Syz155240 } 61272393Syz155240 return ta; 61282393Syz155240 } 61292393Syz155240 61303448Sdh155122 for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next) 61312393Syz155240 if (tap == cookie) { 61322393Syz155240 if (next != NULL) 61332393Syz155240 *next = &ta->ipft_next; 61342393Syz155240 return ta; 61352393Syz155240 } 61362393Syz155240 61372393Syz155240 if (next != NULL) 61382393Syz155240 *next = NULL; 61392393Syz155240 return NULL; 61402393Syz155240 } 61412393Syz155240 61422393Syz155240 61432393Syz155240 /* ------------------------------------------------------------------------ */ 61442393Syz155240 /* Function: fr_findtunebyname */ 61452393Syz155240 /* Returns: NULL = search failed, else pointer to tune struct */ 61462393Syz155240 /* Parameters: name(I) - name of the tuneable entry to find. */ 61472393Syz155240 /* */ 61482393Syz155240 /* Search the static array of tuneables and the list of dynamic tuneables */ 61492393Syz155240 /* for an entry with a matching name. If we can find one, return a pointer */ 61502393Syz155240 /* to the matching structure. */ 61512393Syz155240 /* ------------------------------------------------------------------------ */ 61523448Sdh155122 static ipftuneable_t *fr_findtunebyname(name, ifs) 61532393Syz155240 const char *name; 61543448Sdh155122 ipf_stack_t *ifs; 61552393Syz155240 { 61562393Syz155240 ipftuneable_t *ta; 61572393Syz155240 61583448Sdh155122 for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++) 61592393Syz155240 if (!strcmp(ta->ipft_name, name)) { 61602393Syz155240 return ta; 61612393Syz155240 } 61622393Syz155240 61633448Sdh155122 for (ta = ifs->ifs_ipf_tunelist; ta != NULL; ta = ta->ipft_next) 61642393Syz155240 if (!strcmp(ta->ipft_name, name)) { 61652393Syz155240 return ta; 61662393Syz155240 } 61672393Syz155240 61682393Syz155240 return NULL; 61692393Syz155240 } 61702393Syz155240 61712393Syz155240 61722393Syz155240 /* ------------------------------------------------------------------------ */ 61732393Syz155240 /* Function: fr_addipftune */ 61742393Syz155240 /* Returns: int - 0 == success, else failure */ 61752393Syz155240 /* Parameters: newtune - pointer to new tune struct to add to tuneables */ 61762393Syz155240 /* */ 61772393Syz155240 /* Appends the tune structure pointer to by "newtune" to the end of the */ 61782393Syz155240 /* current list of "dynamic" tuneable parameters. Once added, the owner */ 61792393Syz155240 /* of the object is not expected to ever change "ipft_next". */ 61802393Syz155240 /* ------------------------------------------------------------------------ */ 61813448Sdh155122 int fr_addipftune(newtune, ifs) 61822393Syz155240 ipftuneable_t *newtune; 61833448Sdh155122 ipf_stack_t *ifs; 61842393Syz155240 { 61852393Syz155240 ipftuneable_t *ta, **tap; 61862393Syz155240 61873448Sdh155122 ta = fr_findtunebyname(newtune->ipft_name, ifs); 61882393Syz155240 if (ta != NULL) 61892393Syz155240 return EEXIST; 61902393Syz155240 61913448Sdh155122 for (tap = &ifs->ifs_ipf_tunelist; *tap != NULL; tap = &(*tap)->ipft_next) 61922393Syz155240 ; 61932393Syz155240 61942393Syz155240 newtune->ipft_next = NULL; 61952393Syz155240 *tap = newtune; 61962393Syz155240 return 0; 61972393Syz155240 } 61982393Syz155240 61992393Syz155240 62002393Syz155240 /* ------------------------------------------------------------------------ */ 62012393Syz155240 /* Function: fr_delipftune */ 62022393Syz155240 /* Returns: int - 0 == success, else failure */ 62032393Syz155240 /* Parameters: oldtune - pointer to tune struct to remove from the list of */ 62042393Syz155240 /* current dynamic tuneables */ 62052393Syz155240 /* */ 62062393Syz155240 /* Search for the tune structure, by pointer, in the list of those that are */ 62072393Syz155240 /* dynamically added at run time. If found, adjust the list so that this */ 62082393Syz155240 /* structure is no longer part of it. */ 62092393Syz155240 /* ------------------------------------------------------------------------ */ 62103448Sdh155122 int fr_delipftune(oldtune, ifs) 62112393Syz155240 ipftuneable_t *oldtune; 62123448Sdh155122 ipf_stack_t *ifs; 62132393Syz155240 { 62142393Syz155240 ipftuneable_t *ta, **tap; 62152393Syz155240 62163448Sdh155122 for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next) 62172393Syz155240 if (ta == oldtune) { 62182393Syz155240 *tap = oldtune->ipft_next; 62192393Syz155240 oldtune->ipft_next = NULL; 62202393Syz155240 return 0; 62212393Syz155240 } 62222393Syz155240 62232393Syz155240 return ESRCH; 62242393Syz155240 } 62252393Syz155240 62262393Syz155240 62272393Syz155240 /* ------------------------------------------------------------------------ */ 62282393Syz155240 /* Function: fr_ipftune */ 62292393Syz155240 /* Returns: int - 0 == success, else failure */ 62302393Syz155240 /* Parameters: cmd(I) - ioctl command number */ 62312393Syz155240 /* data(I) - pointer to ioctl data structure */ 62322393Syz155240 /* */ 62332393Syz155240 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */ 62342393Syz155240 /* three ioctls provide the means to access and control global variables */ 62352393Syz155240 /* within IPFilter, allowing (for example) timeouts and table sizes to be */ 62362393Syz155240 /* changed without rebooting, reloading or recompiling. The initialisation */ 62372393Syz155240 /* and 'destruction' routines of the various components of ipfilter are all */ 62382393Syz155240 /* each responsible for handling their own values being too big. */ 62392393Syz155240 /* ------------------------------------------------------------------------ */ 62403448Sdh155122 int fr_ipftune(cmd, data, ifs) 62412393Syz155240 ioctlcmd_t cmd; 62422393Syz155240 void *data; 62433448Sdh155122 ipf_stack_t *ifs; 62442393Syz155240 { 62452393Syz155240 ipftuneable_t *ta; 62462393Syz155240 ipftune_t tu; 62472393Syz155240 void *cookie; 62482393Syz155240 int error; 62492393Syz155240 62502393Syz155240 error = fr_inobj(data, &tu, IPFOBJ_TUNEABLE); 62512393Syz155240 if (error != 0) 62522393Syz155240 return error; 62532393Syz155240 62542393Syz155240 tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 62552393Syz155240 cookie = tu.ipft_cookie; 62562393Syz155240 ta = NULL; 62572393Syz155240 62582393Syz155240 switch (cmd) 62592393Syz155240 { 62602393Syz155240 case SIOCIPFGETNEXT : 62612393Syz155240 /* 62622393Syz155240 * If cookie is non-NULL, assume it to be a pointer to the last 62632393Syz155240 * entry we looked at, so find it (if possible) and return a 62642393Syz155240 * pointer to the next one after it. The last entry in the 62652393Syz155240 * the table is a NULL entry, so when we get to it, set cookie 62662393Syz155240 * to NULL and return that, indicating end of list, erstwhile 62672393Syz155240 * if we come in with cookie set to NULL, we are starting anew 62682393Syz155240 * at the front of the list. 62692393Syz155240 */ 62702393Syz155240 if (cookie != NULL) { 62713448Sdh155122 ta = fr_findtunebycookie(cookie, &tu.ipft_cookie, ifs); 62722393Syz155240 } else { 62733448Sdh155122 ta = ifs->ifs_ipf_tuneables; 62742393Syz155240 tu.ipft_cookie = ta + 1; 62752393Syz155240 } 62762393Syz155240 if (ta != NULL) { 62772393Syz155240 /* 62782393Syz155240 * Entry found, but does the data pointed to by that 62792393Syz155240 * row fit in what we can return? 62802393Syz155240 */ 62812393Syz155240 if (ta->ipft_sz > sizeof(tu.ipft_un)) 62822393Syz155240 return EINVAL; 62832393Syz155240 62842393Syz155240 tu.ipft_vlong = 0; 62852393Syz155240 if (ta->ipft_sz == sizeof(u_long)) 62862393Syz155240 tu.ipft_vlong = *ta->ipft_plong; 62872393Syz155240 else if (ta->ipft_sz == sizeof(u_int)) 62882393Syz155240 tu.ipft_vint = *ta->ipft_pint; 62892393Syz155240 else if (ta->ipft_sz == sizeof(u_short)) 62902393Syz155240 tu.ipft_vshort = *ta->ipft_pshort; 62912393Syz155240 else if (ta->ipft_sz == sizeof(u_char)) 62922393Syz155240 tu.ipft_vchar = *ta->ipft_pchar; 62932393Syz155240 62942393Syz155240 tu.ipft_sz = ta->ipft_sz; 62952393Syz155240 tu.ipft_min = ta->ipft_min; 62962393Syz155240 tu.ipft_max = ta->ipft_max; 62972393Syz155240 tu.ipft_flags = ta->ipft_flags; 62982393Syz155240 bcopy(ta->ipft_name, tu.ipft_name, 62992393Syz155240 MIN(sizeof(tu.ipft_name), 63002393Syz155240 strlen(ta->ipft_name) + 1)); 63012393Syz155240 } 63022393Syz155240 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 63032393Syz155240 break; 63042393Syz155240 63052393Syz155240 case SIOCIPFGET : 63062393Syz155240 case SIOCIPFSET : 63072393Syz155240 /* 63082393Syz155240 * Search by name or by cookie value for a particular entry 63092393Syz155240 * in the tuning paramter table. 63102393Syz155240 */ 63112393Syz155240 error = ESRCH; 63122393Syz155240 if (cookie != NULL) { 63133448Sdh155122 ta = fr_findtunebycookie(cookie, NULL, ifs); 63142393Syz155240 if (ta != NULL) 63152393Syz155240 error = 0; 63162393Syz155240 } else if (tu.ipft_name[0] != '\0') { 63173448Sdh155122 ta = fr_findtunebyname(tu.ipft_name, ifs); 63182393Syz155240 if (ta != NULL) 63192393Syz155240 error = 0; 63202393Syz155240 } 63212393Syz155240 if (error != 0) 63222393Syz155240 break; 63232393Syz155240 63242393Syz155240 if (cmd == (ioctlcmd_t)SIOCIPFGET) { 63252393Syz155240 /* 63262393Syz155240 * Fetch the tuning parameters for a particular value 63272393Syz155240 */ 63282393Syz155240 tu.ipft_vlong = 0; 63292393Syz155240 if (ta->ipft_sz == sizeof(u_long)) 63302393Syz155240 tu.ipft_vlong = *ta->ipft_plong; 63312393Syz155240 else if (ta->ipft_sz == sizeof(u_int)) 63322393Syz155240 tu.ipft_vint = *ta->ipft_pint; 63332393Syz155240 else if (ta->ipft_sz == sizeof(u_short)) 63342393Syz155240 tu.ipft_vshort = *ta->ipft_pshort; 63352393Syz155240 else if (ta->ipft_sz == sizeof(u_char)) 63362393Syz155240 tu.ipft_vchar = *ta->ipft_pchar; 63372393Syz155240 tu.ipft_cookie = ta; 63382393Syz155240 tu.ipft_sz = ta->ipft_sz; 63392393Syz155240 tu.ipft_min = ta->ipft_min; 63402393Syz155240 tu.ipft_max = ta->ipft_max; 63412393Syz155240 tu.ipft_flags = ta->ipft_flags; 63422393Syz155240 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 63432393Syz155240 63442393Syz155240 } else if (cmd == (ioctlcmd_t)SIOCIPFSET) { 63452393Syz155240 /* 63462393Syz155240 * Set an internal parameter. The hard part here is 63472393Syz155240 * getting the new value safely and correctly out of 63482393Syz155240 * the kernel (given we only know its size, not type.) 63492393Syz155240 */ 63502393Syz155240 u_long in; 63512393Syz155240 63522393Syz155240 if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) && 63533448Sdh155122 (ifs->ifs_fr_running > 0)) { 63542393Syz155240 error = EBUSY; 63552393Syz155240 break; 63562393Syz155240 } 63572393Syz155240 63582393Syz155240 in = tu.ipft_vlong; 63592393Syz155240 if (in < ta->ipft_min || in > ta->ipft_max) { 63602393Syz155240 error = EINVAL; 63612393Syz155240 break; 63622393Syz155240 } 63632393Syz155240 63642393Syz155240 if (ta->ipft_sz == sizeof(u_long)) { 63652393Syz155240 tu.ipft_vlong = *ta->ipft_plong; 63662393Syz155240 *ta->ipft_plong = in; 63672393Syz155240 } else if (ta->ipft_sz == sizeof(u_int)) { 63682393Syz155240 tu.ipft_vint = *ta->ipft_pint; 63692393Syz155240 *ta->ipft_pint = (u_int)(in & 0xffffffff); 63702393Syz155240 } else if (ta->ipft_sz == sizeof(u_short)) { 63712393Syz155240 tu.ipft_vshort = *ta->ipft_pshort; 63722393Syz155240 *ta->ipft_pshort = (u_short)(in & 0xffff); 63732393Syz155240 } else if (ta->ipft_sz == sizeof(u_char)) { 63742393Syz155240 tu.ipft_vchar = *ta->ipft_pchar; 63752393Syz155240 *ta->ipft_pchar = (u_char)(in & 0xff); 63762393Syz155240 } 63772393Syz155240 error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE); 63782393Syz155240 } 63792393Syz155240 break; 63802393Syz155240 63812393Syz155240 default : 63822393Syz155240 error = EINVAL; 63832393Syz155240 break; 63842393Syz155240 } 63852393Syz155240 63862393Syz155240 return error; 63872393Syz155240 } 63882393Syz155240 63892393Syz155240 63902393Syz155240 /* ------------------------------------------------------------------------ */ 63912393Syz155240 /* Function: fr_initialise */ 63922393Syz155240 /* Returns: int - 0 == success, < 0 == failure */ 63932393Syz155240 /* Parameters: None. */ 63942393Syz155240 /* */ 63952393Syz155240 /* Call of the initialise functions for all the various subsystems inside */ 63962393Syz155240 /* of IPFilter. If any of them should fail, return immeadiately a failure */ 63972393Syz155240 /* BUT do not try to recover from the error here. */ 63982393Syz155240 /* ------------------------------------------------------------------------ */ 63993448Sdh155122 int fr_initialise(ifs) 64003448Sdh155122 ipf_stack_t *ifs; 64012393Syz155240 { 64022393Syz155240 int i; 64032393Syz155240 64042393Syz155240 #ifdef IPFILTER_LOG 64053448Sdh155122 i = fr_loginit(ifs); 64062393Syz155240 if (i < 0) 64072393Syz155240 return -10 + i; 64082393Syz155240 #endif 64093448Sdh155122 i = fr_natinit(ifs); 64102393Syz155240 if (i < 0) 64112393Syz155240 return -20 + i; 64122393Syz155240 64133448Sdh155122 i = fr_stateinit(ifs); 64142393Syz155240 if (i < 0) 64152393Syz155240 return -30 + i; 64162393Syz155240 64173448Sdh155122 i = fr_authinit(ifs); 64182393Syz155240 if (i < 0) 64192393Syz155240 return -40 + i; 64202393Syz155240 64213448Sdh155122 i = fr_fraginit(ifs); 64222393Syz155240 if (i < 0) 64232393Syz155240 return -50 + i; 64242393Syz155240 64253448Sdh155122 i = appr_init(ifs); 64262393Syz155240 if (i < 0) 64272393Syz155240 return -60 + i; 64282393Syz155240 64292393Syz155240 #ifdef IPFILTER_SYNC 64303448Sdh155122 i = ipfsync_init(ifs); 64312393Syz155240 if (i < 0) 64322393Syz155240 return -70 + i; 64332393Syz155240 #endif 64342393Syz155240 #ifdef IPFILTER_SCAN 64353448Sdh155122 i = ipsc_init(ifs); 64362393Syz155240 if (i < 0) 64372393Syz155240 return -80 + i; 64382393Syz155240 #endif 64392393Syz155240 #ifdef IPFILTER_LOOKUP 64403448Sdh155122 i = ip_lookup_init(ifs); 64412393Syz155240 if (i < 0) 64422393Syz155240 return -90 + i; 64432393Syz155240 #endif 64442393Syz155240 #ifdef IPFILTER_COMPILED 64453448Sdh155122 ipfrule_add(ifs); 64462393Syz155240 #endif 64472393Syz155240 return 0; 64482393Syz155240 } 64492393Syz155240 64502393Syz155240 64512393Syz155240 /* ------------------------------------------------------------------------ */ 64522393Syz155240 /* Function: fr_deinitialise */ 64532393Syz155240 /* Returns: None. */ 64542393Syz155240 /* Parameters: None. */ 64552393Syz155240 /* */ 64562393Syz155240 /* Call all the various subsystem cleanup routines to deallocate memory or */ 64572393Syz155240 /* destroy locks or whatever they've done that they need to now undo. */ 64582393Syz155240 /* The order here IS important as there are some cross references of */ 64592393Syz155240 /* internal data structures. */ 64602393Syz155240 /* ------------------------------------------------------------------------ */ 64613448Sdh155122 void fr_deinitialise(ifs) 64623448Sdh155122 ipf_stack_t *ifs; 64632393Syz155240 { 64643448Sdh155122 fr_fragunload(ifs); 64653448Sdh155122 fr_authunload(ifs); 64663448Sdh155122 fr_natunload(ifs); 64673448Sdh155122 fr_stateunload(ifs); 64682393Syz155240 #ifdef IPFILTER_SCAN 64693448Sdh155122 fr_scanunload(ifs); 64702393Syz155240 #endif 64713448Sdh155122 appr_unload(ifs); 64722393Syz155240 64732393Syz155240 #ifdef IPFILTER_COMPILED 64743448Sdh155122 ipfrule_remove(ifs); 64752393Syz155240 #endif 64762393Syz155240 64773448Sdh155122 (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs); 64783448Sdh155122 (void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE, ifs); 64793448Sdh155122 (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs); 64803448Sdh155122 (void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE, ifs); 64812393Syz155240 64822393Syz155240 #ifdef IPFILTER_LOOKUP 64833448Sdh155122 ip_lookup_unload(ifs); 64842393Syz155240 #endif 64852393Syz155240 64862393Syz155240 #ifdef IPFILTER_LOG 64873448Sdh155122 fr_logunload(ifs); 64882393Syz155240 #endif 64892393Syz155240 } 64902393Syz155240 64912393Syz155240 64922393Syz155240 /* ------------------------------------------------------------------------ */ 64932393Syz155240 /* Function: fr_zerostats */ 64942393Syz155240 /* Returns: int - 0 = success, else failure */ 64952393Syz155240 /* Parameters: data(O) - pointer to pointer for copying data back to */ 64962393Syz155240 /* */ 64972393Syz155240 /* Copies the current statistics out to userspace and then zero's the */ 64982393Syz155240 /* current ones in the kernel. The lock is only held across the bzero() as */ 64992393Syz155240 /* the copyout may result in paging (ie network activity.) */ 65002393Syz155240 /* ------------------------------------------------------------------------ */ 65013448Sdh155122 int fr_zerostats(data, ifs) 65022393Syz155240 caddr_t data; 65033448Sdh155122 ipf_stack_t *ifs; 65042393Syz155240 { 65052393Syz155240 friostat_t fio; 65062393Syz155240 int error; 65072393Syz155240 65083448Sdh155122 fr_getstat(&fio, ifs); 65092393Syz155240 error = copyoutptr(&fio, data, sizeof(fio)); 65102393Syz155240 if (error) 65112393Syz155240 return EFAULT; 65122393Syz155240 65133448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_mutex); 65143448Sdh155122 bzero((char *)ifs->ifs_frstats, sizeof(*ifs->ifs_frstats) * 2); 65153448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 65162393Syz155240 65172393Syz155240 return 0; 65182393Syz155240 } 65192393Syz155240 65202393Syz155240 65212393Syz155240 #ifdef _KERNEL 65222393Syz155240 /* ------------------------------------------------------------------------ */ 65232393Syz155240 /* Function: fr_resolvedest */ 65242393Syz155240 /* Returns: Nil */ 65252393Syz155240 /* Parameters: fdp(IO) - pointer to destination information to resolve */ 65262393Syz155240 /* v(I) - IP protocol version to match */ 65272393Syz155240 /* */ 65282393Syz155240 /* Looks up an interface name in the frdest structure pointed to by fdp and */ 65292393Syz155240 /* if a matching name can be found for the particular IP protocol version */ 65302393Syz155240 /* then store the interface pointer in the frdest struct. If no match is */ 65312393Syz155240 /* found, then set the interface pointer to be -1 as NULL is considered to */ 65322393Syz155240 /* indicate there is no information at all in the structure. */ 65332393Syz155240 /* ------------------------------------------------------------------------ */ 65343448Sdh155122 void fr_resolvedest(fdp, v, ifs) 65352393Syz155240 frdest_t *fdp; 65362393Syz155240 int v; 65373448Sdh155122 ipf_stack_t *ifs; 65382393Syz155240 { 65392958Sdr146992 fdp->fd_ifp = NULL; 65402958Sdr146992 65412958Sdr146992 if (*fdp->fd_ifname != '\0') { 65423448Sdh155122 fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs); 65432958Sdr146992 if (fdp->fd_ifp == NULL) 65442958Sdr146992 fdp->fd_ifp = (void *)-1; 65452393Syz155240 } 65462393Syz155240 } 65472393Syz155240 #endif /* _KERNEL */ 65482393Syz155240 65492393Syz155240 65502393Syz155240 /* ------------------------------------------------------------------------ */ 65512393Syz155240 /* Function: fr_resolvenic */ 65522393Syz155240 /* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */ 65532393Syz155240 /* pointer to interface structure for NIC */ 65542393Syz155240 /* Parameters: name(I) - complete interface name */ 65552393Syz155240 /* v(I) - IP protocol version */ 65562393Syz155240 /* */ 65572393Syz155240 /* Look for a network interface structure that firstly has a matching name */ 65582393Syz155240 /* to that passed in and that is also being used for that IP protocol */ 65592393Syz155240 /* version (necessary on some platforms where there are separate listings */ 65602393Syz155240 /* for both IPv4 and IPv6 on the same physical NIC. */ 65612393Syz155240 /* */ 65622393Syz155240 /* One might wonder why name gets terminated with a \0 byte in here. The */ 65632393Syz155240 /* reason is an interface name could get into the kernel structures of ipf */ 65642393Syz155240 /* in any number of ways and so long as they all use the same sized array */ 65652393Syz155240 /* to put the name in, it makes sense to ensure it gets null terminated */ 65662393Syz155240 /* before it is used for its intended purpose - finding its match in the */ 65672393Syz155240 /* kernel's list of configured interfaces. */ 65682393Syz155240 /* */ 65692393Syz155240 /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an */ 65702393Syz155240 /* array for the name that is LIFNAMSIZ bytes (at least) in length. */ 65712393Syz155240 /* ------------------------------------------------------------------------ */ 65723448Sdh155122 void *fr_resolvenic(name, v, ifs) 65732393Syz155240 char *name; 65742393Syz155240 int v; 65753448Sdh155122 ipf_stack_t *ifs; 65762393Syz155240 { 65772393Syz155240 void *nic; 65782393Syz155240 65792393Syz155240 if (name[0] == '\0') 65802393Syz155240 return NULL; 65812393Syz155240 65822393Syz155240 if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) { 65832393Syz155240 return NULL; 65842393Syz155240 } 65852393Syz155240 65862393Syz155240 name[LIFNAMSIZ - 1] = '\0'; 65872393Syz155240 65883448Sdh155122 nic = GETIFP(name, v, ifs); 65892393Syz155240 if (nic == NULL) 65902393Syz155240 nic = (void *)-1; 65912393Syz155240 return nic; 65922393Syz155240 } 65933448Sdh155122 65943448Sdh155122 void ipf_expiretokens(ifs) 65953448Sdh155122 ipf_stack_t *ifs; 65963448Sdh155122 { 65973448Sdh155122 ipftoken_t *it; 65983448Sdh155122 65993448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_tokens); 66003448Sdh155122 while ((it = ifs->ifs_ipftokenhead) != NULL) { 66013448Sdh155122 if (it->ipt_die > ifs->ifs_fr_ticks) 66023448Sdh155122 break; 66033448Sdh155122 66043448Sdh155122 ipf_freetoken(it, ifs); 66053448Sdh155122 } 66063448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 66073448Sdh155122 } 66083448Sdh155122 66093448Sdh155122 66103448Sdh155122 int ipf_deltoken(type, uid, ptr, ifs) 66113448Sdh155122 int type, uid; 66123448Sdh155122 void *ptr; 66133448Sdh155122 ipf_stack_t *ifs; 66143448Sdh155122 { 66153448Sdh155122 ipftoken_t *it; 66163448Sdh155122 int error = ESRCH; 66173448Sdh155122 66183448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_tokens); 66193448Sdh155122 for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) 66203448Sdh155122 if (ptr == it->ipt_ctx && type == it->ipt_type && 66213448Sdh155122 uid == it->ipt_uid) { 66223448Sdh155122 ipf_freetoken(it, ifs); 66233448Sdh155122 error = 0; 66243448Sdh155122 break; 66253448Sdh155122 } 66263448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 66273448Sdh155122 66283448Sdh155122 return error; 66293448Sdh155122 } 66303448Sdh155122 66313796Szf203873 void ipf_unlinktoken(token, ifs) 66323448Sdh155122 ipftoken_t *token; 66333448Sdh155122 ipf_stack_t *ifs; 66343448Sdh155122 { 66353448Sdh155122 66363448Sdh155122 if (ifs->ifs_ipftokentail == &token->ipt_next) 66373448Sdh155122 ifs->ifs_ipftokentail = token->ipt_pnext; 66383448Sdh155122 66393448Sdh155122 *token->ipt_pnext = token->ipt_next; 66403448Sdh155122 if (token->ipt_next != NULL) 66413448Sdh155122 token->ipt_next->ipt_pnext = token->ipt_pnext; 66423448Sdh155122 } 66433448Sdh155122 66443448Sdh155122 66453448Sdh155122 66463448Sdh155122 ipftoken_t *ipf_findtoken(type, uid, ptr, ifs) 66473448Sdh155122 int type, uid; 66483448Sdh155122 void *ptr; 66493448Sdh155122 ipf_stack_t *ifs; 66503448Sdh155122 { 66513448Sdh155122 ipftoken_t *it, *new; 66523448Sdh155122 66533448Sdh155122 KMALLOC(new, ipftoken_t *); 66543448Sdh155122 66553448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_tokens); 66563448Sdh155122 for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) { 66573448Sdh155122 if (it->ipt_alive == 0) 66583448Sdh155122 continue; 66593448Sdh155122 if (ptr == it->ipt_ctx && type == it->ipt_type && 66603448Sdh155122 uid == it->ipt_uid) 66613448Sdh155122 break; 66623448Sdh155122 } 66633448Sdh155122 66643448Sdh155122 if (it == NULL) { 66653448Sdh155122 it = new; 66663448Sdh155122 new = NULL; 66673448Sdh155122 if (it == NULL) 66683448Sdh155122 return NULL; 66693448Sdh155122 it->ipt_data = NULL; 66703448Sdh155122 it->ipt_ctx = ptr; 66713448Sdh155122 it->ipt_uid = uid; 66723448Sdh155122 it->ipt_type = type; 66733448Sdh155122 it->ipt_next = NULL; 66743448Sdh155122 it->ipt_alive = 1; 66753448Sdh155122 } else { 66763448Sdh155122 if (new != NULL) { 66773448Sdh155122 KFREE(new); 66783448Sdh155122 new = NULL; 66793448Sdh155122 } 66803448Sdh155122 66813448Sdh155122 ipf_unlinktoken(it, ifs); 66823448Sdh155122 } 66833448Sdh155122 it->ipt_pnext = ifs->ifs_ipftokentail; 66843448Sdh155122 *ifs->ifs_ipftokentail = it; 66853448Sdh155122 ifs->ifs_ipftokentail = &it->ipt_next; 66863448Sdh155122 it->ipt_next = NULL; 66873448Sdh155122 66883448Sdh155122 /* XXX: more needed */ 66893448Sdh155122 it->ipt_die = ifs->ifs_fr_ticks + 2; 66903448Sdh155122 66913448Sdh155122 MUTEX_DOWNGRADE(&ifs->ifs_ipf_tokens); 66923448Sdh155122 66933448Sdh155122 return it; 66943448Sdh155122 } 66953448Sdh155122 66963448Sdh155122 66973448Sdh155122 void ipf_freetoken(token, ifs) 66983448Sdh155122 ipftoken_t *token; 66993448Sdh155122 ipf_stack_t *ifs; 67003448Sdh155122 { 67013448Sdh155122 void *data; 67023448Sdh155122 67033448Sdh155122 ipf_unlinktoken(token, ifs); 67043448Sdh155122 67053448Sdh155122 data = token->ipt_data; 67063448Sdh155122 67073448Sdh155122 if ((data != NULL) && (data != (void *)-1)) { 67083448Sdh155122 switch (token->ipt_type) 67093448Sdh155122 { 67103448Sdh155122 case IPFGENITER_IPF : 67113448Sdh155122 (void)fr_derefrule((frentry_t **)&data, ifs); 67123448Sdh155122 break; 67133448Sdh155122 case IPFGENITER_IPNAT : 67143448Sdh155122 WRITE_ENTER(&ifs->ifs_ipf_nat); 67153448Sdh155122 fr_ipnatderef((ipnat_t **)&data, ifs); 67163448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_nat); 67173448Sdh155122 break; 67183448Sdh155122 case IPFGENITER_NAT : 67193448Sdh155122 fr_natderef((nat_t **)&data, ifs); 67203448Sdh155122 break; 67213448Sdh155122 case IPFGENITER_STATE : 67223448Sdh155122 fr_statederef(NULL, (ipstate_t **)&data, ifs); 67233448Sdh155122 break; 67243448Sdh155122 case IPFGENITER_FRAG : 67253448Sdh155122 fr_fragderef((ipfr_t **)&data, &ifs->ifs_ipf_frag, ifs); 67263448Sdh155122 break; 67273448Sdh155122 case IPFGENITER_NATFRAG : 67283448Sdh155122 fr_fragderef((ipfr_t **)&data, &ifs->ifs_ipf_natfrag, ifs); 67293448Sdh155122 break; 67303448Sdh155122 case IPFGENITER_HOSTMAP : 67313448Sdh155122 fr_hostmapderef((hostmap_t **)&data); 67323448Sdh155122 break; 67333448Sdh155122 default : 67343448Sdh155122 (void) ip_lookup_iterderef(token->ipt_type, data, ifs); 67353448Sdh155122 break; 67363448Sdh155122 } 67373448Sdh155122 } 67383448Sdh155122 67393448Sdh155122 KFREE(token); 67403448Sdh155122 } 67413448Sdh155122 67423448Sdh155122 int ipf_getnextrule(ipftoken_t *t, void *ptr, ipf_stack_t *ifs) 67433448Sdh155122 { 67443448Sdh155122 frentry_t *fr, *next, zero; 67453448Sdh155122 ipfruleiter_t it; 67463448Sdh155122 frgroup_t *fg; 67473448Sdh155122 int error; 67483448Sdh155122 67493448Sdh155122 if (t == NULL || ptr == NULL) 67503448Sdh155122 return EFAULT; 67513448Sdh155122 error = fr_inobj(ptr, &it, IPFOBJ_IPFITER); 67523448Sdh155122 if (error != 0) 67533448Sdh155122 return error; 67543448Sdh155122 if ((it.iri_ver != AF_INET) && (it.iri_ver != AF_INET6)) 67553448Sdh155122 return EINVAL; 67563448Sdh155122 if ((it.iri_inout != 0) && (it.iri_inout != 1)) 67573448Sdh155122 return EINVAL; 67583448Sdh155122 if ((it.iri_active != 0) && (it.iri_active != 1)) 67593448Sdh155122 return EINVAL; 67603448Sdh155122 if (it.iri_rule == NULL) 67613448Sdh155122 return EFAULT; 67623448Sdh155122 67633448Sdh155122 fr = t->ipt_data; 67643448Sdh155122 READ_ENTER(&ifs->ifs_ipf_mutex); 67653448Sdh155122 if (fr == NULL) { 67663448Sdh155122 if (*it.iri_group == '\0') { 67673448Sdh155122 if (it.iri_ver == AF_INET) 67683448Sdh155122 next = ifs->ifs_ipfilter 67693448Sdh155122 [it.iri_inout][it.iri_active]; 67703448Sdh155122 else 67713448Sdh155122 next = ifs->ifs_ipfilter6 67723448Sdh155122 [it.iri_inout][it.iri_active]; 67733448Sdh155122 } else { 67743448Sdh155122 fg = fr_findgroup(it.iri_group, IPL_LOGIPF, 67753448Sdh155122 it.iri_active, NULL, ifs); 67763448Sdh155122 if (fg != NULL) 67773448Sdh155122 next = fg->fg_start; 67783448Sdh155122 else 67793448Sdh155122 next = NULL; 67803448Sdh155122 } 67813448Sdh155122 } else { 67823448Sdh155122 next = fr->fr_next; 67833448Sdh155122 } 67843448Sdh155122 67853448Sdh155122 if (next != NULL) { 67863448Sdh155122 if (next->fr_next == NULL) { 67873448Sdh155122 t->ipt_alive = 0; 67883796Szf203873 ipf_unlinktoken(t, ifs); 67893796Szf203873 KFREE(t); 67903448Sdh155122 } else { 67913448Sdh155122 MUTEX_ENTER(&next->fr_lock); 67923448Sdh155122 next->fr_ref++; 67933448Sdh155122 MUTEX_EXIT(&next->fr_lock); 67943796Szf203873 t->ipt_data = next; 67953448Sdh155122 } 67963448Sdh155122 } else { 67973448Sdh155122 bzero(&zero, sizeof(zero)); 67983448Sdh155122 next = &zero; 67993448Sdh155122 ipf_freetoken(t, ifs); 68003448Sdh155122 fr = NULL; 68013448Sdh155122 } 68023448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_mutex); 68033448Sdh155122 68043448Sdh155122 if (fr != NULL) { 68053448Sdh155122 (void)fr_derefrule(&fr, ifs); 68063448Sdh155122 } 68073448Sdh155122 68083448Sdh155122 error = COPYOUT(next, it.iri_rule, sizeof(*next)); 68093448Sdh155122 if (error != 0) 68103448Sdh155122 return EFAULT; 68113448Sdh155122 68123448Sdh155122 if (next->fr_data != NULL) { 68133448Sdh155122 error = COPYOUT(next->fr_data, 68143448Sdh155122 (char *)it.iri_rule + sizeof(*next), 68153448Sdh155122 next->fr_dsize); 68163448Sdh155122 if (error != 0) 68173448Sdh155122 error = EFAULT; 68183448Sdh155122 } 68193448Sdh155122 68203448Sdh155122 return error; 68213448Sdh155122 } 68223448Sdh155122 68233448Sdh155122 68243448Sdh155122 int ipf_frruleiter(data, uid, ctx, ifs) 68253448Sdh155122 void *data, *ctx; 68263448Sdh155122 int uid; 68273448Sdh155122 ipf_stack_t *ifs; 68283448Sdh155122 { 68293448Sdh155122 ipftoken_t *token; 68303448Sdh155122 int error; 68313448Sdh155122 68323448Sdh155122 token = ipf_findtoken(IPFGENITER_IPF, uid, ctx, ifs); 68333448Sdh155122 if (token != NULL) 68343448Sdh155122 error = ipf_getnextrule(token, data, ifs); 68353448Sdh155122 else 68363448Sdh155122 error = EFAULT; 68373448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 68383448Sdh155122 68393448Sdh155122 return error; 68403448Sdh155122 } 68413448Sdh155122 68423448Sdh155122 68433448Sdh155122 int ipf_geniter(token, itp, ifs) 68443448Sdh155122 ipftoken_t *token; 68453448Sdh155122 ipfgeniter_t *itp; 68463448Sdh155122 ipf_stack_t *ifs; 68473448Sdh155122 { 68483448Sdh155122 int error; 68493448Sdh155122 68503448Sdh155122 switch (itp->igi_type) 68513448Sdh155122 { 68523448Sdh155122 case IPFGENITER_FRAG : 68533448Sdh155122 error = fr_nextfrag(token, itp, &ifs->ifs_ipfr_list, 68543448Sdh155122 &ifs->ifs_ipfr_tail, &ifs->ifs_ipf_frag, ifs); 68553448Sdh155122 break; 68563448Sdh155122 default : 68573448Sdh155122 error = EINVAL; 68583448Sdh155122 break; 68593448Sdh155122 } 68603448Sdh155122 68613448Sdh155122 return error; 68623448Sdh155122 } 68633448Sdh155122 68643448Sdh155122 68653448Sdh155122 int ipf_genericiter(data, uid, ctx, ifs) 68663448Sdh155122 void *data, *ctx; 68673448Sdh155122 int uid; 68683448Sdh155122 ipf_stack_t *ifs; 68693448Sdh155122 { 68703448Sdh155122 ipftoken_t *token; 68713448Sdh155122 ipfgeniter_t iter; 68723448Sdh155122 int error; 68733448Sdh155122 68743448Sdh155122 error = fr_inobj(data, &iter, IPFOBJ_GENITER); 68753448Sdh155122 if (error != 0) 68763448Sdh155122 return error; 68773448Sdh155122 68783448Sdh155122 token = ipf_findtoken(iter.igi_type, uid, ctx, ifs); 68793448Sdh155122 if (token != NULL) { 68803448Sdh155122 token->ipt_subtype = iter.igi_type; 68813448Sdh155122 error = ipf_geniter(token, &iter, ifs); 68823448Sdh155122 } else 68833448Sdh155122 error = EFAULT; 68843448Sdh155122 RWLOCK_EXIT(&ifs->ifs_ipf_tokens); 68853448Sdh155122 68863448Sdh155122 return error; 68873448Sdh155122 } 6888