fil.c revision bb1d9de55b0c95d4bd8ff7f06a294587a25a6c2e
/*
* Copyright (C) 1993-2003 by Darren Reed.
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
# define KERNEL 1
# define _KERNEL 1
#endif
#if defined(__NetBSD__)
# include "opt_ipfilter_log.h"
# endif
#endif
#if defined(_KERNEL) && defined(__FreeBSD_version) && \
(__FreeBSD_version >= 220000)
# if (__FreeBSD_version >= 400000)
# if !defined(IPFILTER_LKM)
# include "opt_inet6.h"
# endif
# if (__FreeBSD_version == 400019)
# define CSUM_DELAY_DATA
# endif
# endif
#else
#endif
#if !defined(_AIX51)
#endif
#if defined(_KERNEL)
#else
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <stddef.h>
# define _KERNEL
# ifdef __OpenBSD__
struct file;
# endif
#endif
!defined(linux)
#else
# if !defined(linux)
# include <sys/byteorder.h>
# endif
# include <sys/dditypes.h>
# endif
#endif
#ifdef __hpux
# define _NET_ROUTE_INCLUDED
#endif
#if !defined(linux)
#endif
#ifdef sun
#endif
#if !defined(_KERNEL) && defined(__FreeBSD__)
# include "radix_ipf.h"
#endif
#include <netinet/in_systm.h>
#if !defined(linux)
#endif
#endif
#endif
#ifdef __hpux
#endif
#include "netinet/ip_compat.h"
#ifdef USE_INET6
# endif
#endif
#include "netinet/ip_state.h"
#include "netinet/ip_proxy.h"
#include "netinet/ipf_stack.h"
#ifdef IPFILTER_SCAN
#endif
#ifdef IPFILTER_SYNC
#endif
#include "netinet/ip_htable.h"
#ifdef IPFILTER_COMPILED
# include "netinet/ip_rules.h"
#endif
#if defined(IPFILTER_BPF) && defined(_KERNEL)
#endif
# if defined(_KERNEL) && !defined(IPFILTER_LKM)
# include "opt_ipfilter.h"
# endif
#endif
/* END OF INCLUDES */
#if !defined(lint)
#endif
#ifndef _KERNEL
# include "ipf.h"
# include "ipt.h"
# include "bpf-ipf.h"
extern int opts;
#else /* #ifndef _KERNEL */
# define FR_VERBOSE(verb_pr)
#endif /* _KERNEL */
char ipfilter_version[] = IPL_VERSION;
int fr_features = 0
#ifdef IPFILTER_LKM
#endif
#ifdef IPFILTER_LOG
#endif
#ifdef IPFILTER_LOOKUP
#endif
#ifdef IPFILTER_BPF
#endif
#ifdef IPFILTER_COMPILED
#endif
#ifdef IPFILTER_CKSUM
#endif
#ifdef IPFILTER_SYNC
#endif
#ifdef IPFILTER_SCAN
#endif
#ifdef USE_INET6
#endif
;
#define IPF_BUMP(x) (x)++
ipf_stack_t *));
#ifdef IPFILTER_LOOKUP
ipf_stack_t *));
#endif
ipf_stack_t *));
void *, void *, ipf_stack_t *));
/*
* bit values for identifying presence of individual IP options
* All of these tables should be ordered by increasing key value on the left
* hand side to allow for binary searching of the array and include a trailer
* with a 0 for the bitmask for linear searches to easily find the end with.
*/
{ IPOPT_NOP, 0x000001 },
{ IPOPT_RR, 0x000002 },
{ IPOPT_ZSU, 0x000004 },
{ IPOPT_MTUP, 0x000008 },
{ IPOPT_MTUR, 0x000010 },
{ IPOPT_ENCODE, 0x000020 },
{ IPOPT_TS, 0x000040 },
{ IPOPT_TR, 0x000080 },
{ IPOPT_SECURITY, 0x000100 },
{ IPOPT_LSRR, 0x000200 },
{ IPOPT_E_SEC, 0x000400 },
{ IPOPT_CIPSO, 0x000800 },
{ IPOPT_SATID, 0x001000 },
{ IPOPT_SSRR, 0x002000 },
{ IPOPT_ADDEXT, 0x004000 },
{ IPOPT_VISA, 0x008000 },
{ IPOPT_IMITD, 0x010000 },
{ IPOPT_EIP, 0x020000 },
{ IPOPT_FINN, 0x040000 },
{ 0, 0x000000 }
};
#ifdef USE_INET6
{ IPPROTO_HOPOPTS, 0x000001 },
{ IPPROTO_IPV6, 0x000002 },
{ IPPROTO_ROUTING, 0x000004 },
{ IPPROTO_FRAGMENT, 0x000008 },
{ IPPROTO_ESP, 0x000010 },
{ IPPROTO_AH, 0x000020 },
{ IPPROTO_NONE, 0x000040 },
{ IPPROTO_DSTOPTS, 0x000080 },
{ 0, 0 }
};
#endif
{ TCPOPT_NOP, 0x000001 },
{ TCPOPT_MAXSEG, 0x000002 },
{ TCPOPT_WINDOW, 0x000004 },
{ TCPOPT_SACK_PERMITTED, 0x000008 },
{ TCPOPT_SACK, 0x000010 },
{ TCPOPT_TIMESTAMP, 0x000020 },
{ 0, 0x000000 }
};
/*
* bit values for identifying presence of individual IP security options
*/
{ IPSO_CLASS_RES4, 0x01 },
{ IPSO_CLASS_TOPS, 0x02 },
{ IPSO_CLASS_SECR, 0x04 },
{ IPSO_CLASS_RES3, 0x08 },
{ IPSO_CLASS_CONF, 0x10 },
{ IPSO_CLASS_UNCL, 0x20 },
{ IPSO_CLASS_RES2, 0x40 },
{ IPSO_CLASS_RES1, 0x80 }
};
/*
* Table of functions available for use with call rules.
*/
static ipfunc_resolve_t fr_availfuncs[] = {
#ifdef IPFILTER_LOOKUP
#endif
{ "", NULL }
};
/*
* The next section of code is a a collection of small routines that set
* fields in the fr_info_t structure passed based on properties of the
* current packet. There are different routines for the same protocol
* for each of IPv4 and IPv6. Adding a new protocol, for which there
* will "special" inspection for setup, is now more easily done by adding
* a new routine and expanding the frpr_ipinit*() function rather than by
* adding more code to a growing switch statement.
*/
#ifdef USE_INET6
/* ------------------------------------------------------------------------ */
/* Function: frpr_short6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function enforces the 'is a packet too short to be legit' rule */
/* for IPv6 and marks the packet with FI_SHORT if so. See function comment */
/* for frpr_short() for more details. */
/* ------------------------------------------------------------------------ */
int xmin;
{
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_ipv6hdr */
/* Returns: int */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Copy values from the IPv6 header into the fr_info_t struct and call the */
/* per-protocol analyzer if it exists. */
/* ------------------------------------------------------------------------ */
{
hdrcount = 0;
switch (p)
{
case IPPROTO_UDP :
go = 0;
break;
case IPPROTO_TCP :
go = 0;
break;
case IPPROTO_ICMPV6 :
go = 0;
break;
case IPPROTO_GRE :
go = 0;
break;
case IPPROTO_HOPOPTS :
/*
* hop by hop ext header is only allowed
* right after IPv6 header.
*/
if (hdrcount != 0) {
p = IPPROTO_NONE;
} else {
p = frpr_hopopts6(fin);
}
break;
case IPPROTO_DSTOPTS :
p = frpr_dstopts6(fin);
break;
case IPPROTO_ROUTING :
p = frpr_routing6(fin);
break;
case IPPROTO_AH :
break;
case IPPROTO_ESP :
go = 0;
break;
case IPPROTO_IPV6 :
break;
}
go = 0;
break;
case IPPROTO_NONE :
go = 0;
break;
case IPPROTO_FRAGMENT :
p = frpr_fragment6(fin);
go = 0;
break;
default :
go = 0;
break;
}
hdrcount++;
/*
* It is important to note that at this point, for the
* extension headers (go != 0), the entire header may not have
* been pulled up when the code gets to this point. This is
* only done for "go != 0" because the other header handlers
* will all pullup their complete header. The other indicator
* of an incomplete packet is that this was just an extension
* header.
*/
if ((go != 0) && (p != IPPROTO_NONE) &&
p = IPPROTO_NONE;
go = 0;
}
}
return -1;
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_ipv6exthdr */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* of this extension header are allowed. */
/* proto(I) - protocol number for this extension header */
/* */
/* IPv6 Only */
/* ------------------------------------------------------------------------ */
{
int i;
/* 8 is default length of extension hdr */
return IPPROTO_NONE;
}
return IPPROTO_NONE;
return IPPROTO_NONE;
}
/*
* Most IPv6 extension headers are only allowed once.
*/
if ((multiple == 0) &&
else
break;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_hopopts6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks pending hop by hop options extension header */
/* ------------------------------------------------------------------------ */
{
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_routing6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This is function checks pending routing extension header */
/* ------------------------------------------------------------------------ */
{
int shift;
return IPPROTO_NONE;
/*
* Nasty extension header length?
*/
/*
* Compensate for the changes made in frpr_ipv6exthdr()
*/
return IPPROTO_NONE;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_fragment6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Examine the IPv6 fragment header and extract fragment offset information.*/
/* */
/* We don't know where the transport layer header (or whatever is next is), */
/* as it could be behind destination options (amongst others). Because */
/* there is no fragment cache, there is no knowledge about whether or not an*/
/* upper layer header has been seen (or where it ends) and thus we are not */
/* able to continue processing beyond this header with any confidence. */
/* ------------------------------------------------------------------------ */
{
int dlen;
return IPPROTO_NONE;
return IPPROTO_NONE;
return IPPROTO_NONE;
/*
* Fragment but no fragmentation info set? Bad packet...
*/
if (frag->ip6f_offlg == 0) {
return IPPROTO_NONE;
}
/* length of hdrs(after frag hdr) + data */
/*
* If the frag is not the last one and the payload length
* is not multiple of 8, it must be dropped.
*/
return IPPROTO_NONE;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_dstopts6 */
/* Returns: int - value of the next header or IPPROTO_NONE if error */
/* Parameters: fin(I) - pointer to packet information */
/* nextheader(I) - stores next header value */
/* */
/* IPv6 Only */
/* This is function checks pending destination options extension header */
/* ------------------------------------------------------------------------ */
{
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_icmp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* This routine is mainly concerned with determining the minimum valid size */
/* for an ICMPv6 packet. */
/* ------------------------------------------------------------------------ */
{
return;
switch (icmp6->icmp6_type)
{
case ICMP6_ECHO_REPLY :
case ICMP6_ECHO_REQUEST :
break;
case ICMP6_DST_UNREACH :
case ICMP6_PACKET_TOO_BIG :
case ICMP6_TIME_EXCEEDED :
case ICMP6_PARAM_PROB :
return;
}
break;
default :
break;
}
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_udp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Is not expected to be called for fragmented packets. */
/* ------------------------------------------------------------------------ */
{
return;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_tcp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Is not expected to be called for fragmented packets. */
/* ------------------------------------------------------------------------ */
{
return;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_esp6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Analyse the packet for ESP properties. */
/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
/* even though the newer ESP packets must also have a sequence number that */
/* is 32bits as well, it is not possible(?) to determine the version from a */
/* simple packet header. */
/* ------------------------------------------------------------------------ */
{
int i;
break;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_ah6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv6 Only */
/* Analyse the packet for AH properties. */
/* The minimum length is taken to be the combination of all fields in the */
/* header being present and no authentication data (null algorithm used.) */
/* ------------------------------------------------------------------------ */
{
int i, shift;
return IPPROTO_NONE;
break;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_gre6 */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Analyse the packet for GRE properties. */
/* ------------------------------------------------------------------------ */
{
return;
}
#endif /* USE_INET6 */
/* ------------------------------------------------------------------------ */
/* Function: frpr_pullup */
/* Returns: int - 0 == pullup succeeded, -1 == failure */
/* Parameters: fin(I) - pointer to packet information */
/* plen(I) - length (excluding L3 header) to pullup */
/* */
/* Short inline function to cut down on code duplication to perform a call */
/* to fr_pullup to ensure there is the required amount of data, */
/* consecutively in the packet buffer. */
/* ------------------------------------------------------------------------ */
int plen;
{
#if defined(_KERNEL)
return -1;
}
}
#endif
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_short */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* xmin(I) - minimum header size */
/* */
/* Check if a packet is "short" as defined by xmin. The rule we are */
/* applying here is that the packet must not be fragmented within the layer */
/* 4 header. That is, it must not be a fragment that has its offset set to */
/* start within the layer 4 header (hdrmin) or if it is at offset 0, the */
/* entire layer 4 header must be present (min). */
/* ------------------------------------------------------------------------ */
int xmin;
{
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_icmp */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv4 Only */
/* Do a sanity check on the packet for ICMP (v4). In nearly all cases, */
/* except extrememly bad packets, both type and code will be present. */
/* The expected minimum size of an ICMP packet is very much dependent on */
/* the type of it. */
/* */
/* XXX - other ICMP sanity checks? */
/* ------------------------------------------------------------------------ */
{
return;
}
return;
{
case ICMP_ECHOREPLY :
case ICMP_ECHO :
/* Router discovery messaes - RFC 1256 */
case ICMP_ROUTERADVERT :
case ICMP_ROUTERSOLICIT :
break;
/*
* type(1) + code(1) + cksum(2) + id(2) seq(2) +
* 3 * timestamp(3 * 4)
*/
case ICMP_TSTAMP :
case ICMP_TSTAMPREPLY :
minicmpsz = 20;
break;
/*
* type(1) + code(1) + cksum(2) + id(2) seq(2) +
* mask(4)
*/
case ICMP_MASKREQ :
case ICMP_MASKREPLY :
minicmpsz = 12;
break;
/*
* type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
*/
case ICMP_UNREACH :
}
/* FALLTHRU */
case ICMP_SOURCEQUENCH :
case ICMP_REDIRECT :
case ICMP_TIMXCEED :
case ICMP_PARAMPROB :
return;
/*
* ICMP error packets should not be generated for IP
* packets that are a fragment that isn't the first
* fragment.
*/
break;
default :
break;
}
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_tcpcommon */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* TCP header sanity checking. Look for bad combinations of TCP flags, */
/* and make some checks with how they interact with other fields. */
/* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is */
/* valid and mark the packet as bad if not. */
/* ------------------------------------------------------------------------ */
{
return;
return;
}
return;
/*
* Use of the TCP data offset *must* result in a value that is at
* least the same size as the TCP header.
*/
return;
}
/*
* If the urgent flag is set, then the urgent pointer must
* also be set and vice versa. Good TCP packets do not have
* just one of these set.
*/
/* Ignore this case, it shows up in "real" traffic with */
/* bogus values in the urgent pointer field. */
/* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
/*
* If the ack bit isn't set, then either the SYN or
* RST bit must be set. If the SYN bit is set, then
* we expect the ACK field to be 0. If the ACK is
* not set and if URG, PSH or FIN are set, consdier
* that to indicate a bad TCP packet.
*/
/*
* Cisco PIX sets the ACK field to a random value.
* In light of this, do not set FI_BAD until a patch
* is available from Cisco to ensure that
* interoperability between existing systems is
* achieved.
*/
/*fin->fin_flx |= FI_BAD*/;
}
}
/*
* At this point, it's not exactly clear what is to be gained by
* marking up which TCP options are and are not present. The one we
* are most interested in is the TCP window scale. This is only in
* a SYN packet [RFC1323] so we don't need this here...?
* Now if we were to analyse the header for passive fingerprinting,
* then that might add some weight to adding this...
*/
return;
return;
#if 0
# ifdef _KERNEL
return;
}
# endif
opt = *s;
if (opt == '\0')
break;
else if (opt == TCPOPT_NOP)
ol = 1;
else {
if (tlen < 2)
break;
ol = (int)*(s + 1);
break;
}
break;
}
}
s += ol;
}
#endif /* 0 */
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_udpcommon */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Extract the UDP source and destination ports, if present. If compiled */
/* with IPFILTER_CKSUM, check to see if the UDP checksum is valid. */
/* ------------------------------------------------------------------------ */
{
return;
}
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_tcp */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv4 Only */
/* ------------------------------------------------------------------------ */
{
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_udp */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv4 Only */
/* ------------------------------------------------------------------------ */
{
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_esp */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Analyse the packet for ESP properties. */
/* The minimum length is taken to be the SPI (32bits) plus a tail (32bits) */
/* even though the newer ESP packets must also have a sequence number that */
/* is 32bits as well, it is not possible(?) to determine the version from a */
/* simple packet header. */
/* ------------------------------------------------------------------------ */
{
return;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_ah */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Analyse the packet for AH properties. */
/* The minimum length is taken to be the combination of all fields in the */
/* header being present and no authentication data (null algorithm used.) */
/* ------------------------------------------------------------------------ */
{
int len;
return;
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_gre */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Analyse the packet for GRE properties. */
/* ------------------------------------------------------------------------ */
{
return;
}
}
/* ------------------------------------------------------------------------ */
/* Function: frpr_ipv4hdr */
/* Returns: void */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* IPv4 Only */
/* Analyze the IPv4 header and set fields in the fr_info_t structure. */
/* Check all options present and flag their presence if any exist. */
/* ------------------------------------------------------------------------ */
{
/* Get both TTL and protocol */
#if 0
#endif
/* Zero out bits not used in IPv6 address */
/*
* set packet attribute flags based on the offset and
* calculate the byte offset that it represents.
*/
if (off != 0) {
off &= IP_OFFMASK;
if (off != 0) {
off <<= 3;
/*
* The length of the packet, starting at its
* offset cannot exceed 65535 (0xffff) as the
* length of an IP packet is only 16 bits.
*
* Any fragment that isn't the last fragment
* must have a length greater than 0 and it
* must be an even multiple of 8.
*/
}
}
}
/*
* Call per-protocol setup and checking
*/
switch (p)
{
case IPPROTO_UDP :
break;
case IPPROTO_TCP :
break;
case IPPROTO_ICMP :
break;
case IPPROTO_AH :
break;
case IPPROTO_ESP :
break;
case IPPROTO_GRE :
break;
}
return;
/*
* If it is a standard IP header (no options), set the flag fields
* which relate to options to 0.
*/
return;
}
/*
* So the IP header has some IP options attached. Walk the entire
* list of options present with this packet and set flags to indicate
* which ones are here and which ones are not. For the somewhat out
* of date and obscure security classification options, set a flag to
* represent which classification is present.
*/
opt = *s;
if (opt == '\0')
break;
ol = 1;
else {
if (hlen < 2)
break;
ol = (int)*(s + 1);
break;
}
if (opt == IPOPT_SECURITY) {
int j, m;
for (j = 3, m = 2; m >= 0; ) {
auth = *(s + 3);
auth *= 256;
auth += *(s + 4);
break;
}
j -= m;
else
j += m;
m--;
}
}
break;
}
i -= mv;
else
i += mv;
mv--;
}
s += ol;
}
/*
*
*/
auth &= 0xff00;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_makefrip */
/* Returns: int - 1 == hdr checking error, 0 == OK */
/* Parameters: hlen(I) - length of IP packet header */
/* ip(I) - pointer to the IP header */
/* fin(IO) - pointer to packet information */
/* */
/* Compact the IP header into a structure which contains just the info. */
/* which is useful for comparing IP headers with and store this information */
/* in the fr_info_t structure pointer to by fin. At present, it is assumed */
/* this function will be called with either an IPv4 or IPv6 packet. */
/* ------------------------------------------------------------------------ */
int hlen;
{
int v;
if (v == 4)
#ifdef USE_INET6
else if (v == 6) {
return -1;
}
#endif
return -1;
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_portcheck */
/* Returns: int - 1 == port matched, 0 == port match failed */
/* Parameters: frp(I) - pointer to port check `expression' */
/* pop(I) - pointer to port number to evaluate */
/* */
/* Perform a comparison of a port number against some other(s), using a */
/* structure with compare information stored in it. */
/* ------------------------------------------------------------------------ */
{
int err = 1;
/*
* Do opposite test to that required and continue if that succeeds.
*/
{
case FR_EQUAL :
err = 0;
break;
case FR_NEQUAL :
err = 0;
break;
case FR_LESST :
err = 0;
break;
case FR_GREATERT :
err = 0;
break;
case FR_LESSTE :
err = 0;
break;
case FR_GREATERTE :
err = 0;
break;
case FR_OUTRANGE :
err = 0;
break;
case FR_INRANGE :
err = 0;
break;
case FR_INCRANGE :
err = 0;
break;
default :
break;
}
return err;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_tcpudpchk */
/* Returns: int - 1 == protocol matched, 0 == check failed */
/* Parameters: fin(I) - pointer to packet information */
/* ft(I) - pointer to structure with comparison data */
/* */
/* structure containing information that we want to match against. */
/* ------------------------------------------------------------------------ */
{
int err = 1;
/*
* Both ports should *always* be in the first fragment.
* So far, I cannot find any cases where they can not be.
*
* compare destination ports
*/
/*
* compare source ports
*/
/*
* expect to do any sort of match on it ? If we were looking for
* TCP flags, then NO match. If not, then match (which should
* satisfy the "short" class too).
*/
/*
* Match the flags ? If not, abort this match.
*/
err = 0;
}
}
return err;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_ipfcheck */
/* Returns: int - 0 == match, 1 == no match */
/* Parameters: fin(I) - pointer to packet information */
/* fr(I) - pointer to filter rule */
/* portcmp(I) - flag indicating whether to attempt matching on */
/* */
/* Check to see if a packet matches an IPFilter rule. Checks of addresses, */
/* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
/* this function. */
/* ------------------------------------------------------------------------ */
int portcmp;
{
int i;
/*
* first 32 bits to check coversion:
* IP version, TOS, TTL, protocol
*/
FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
if (i)
return 1;
/*
* Next 32 bits is a constructed bitmask indicating which IP options
* are present (if any) in this packet.
*/
FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
if (i)
return 1;
/*
* Unrolled loops (4 each, for 32 bits) for address checks.
*/
/*
* Check the source address.
*/
#ifdef IPFILTER_LOOKUP
if (i == -1)
return 1;
lip += 3;
lm += 3;
ld += 3;
} else {
#endif
FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
} else {
lip += 3;
lm += 3;
ld += 3;
}
#ifdef IPFILTER_LOOKUP
}
#endif
if (i)
return 1;
/*
* Check the destination address.
*/
#ifdef IPFILTER_LOOKUP
if (i == -1)
return 1;
lip += 3;
lm += 3;
ld += 3;
} else {
#endif
FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
} else {
lip += 3;
lm += 3;
ld += 3;
}
#ifdef IPFILTER_LOOKUP
}
#endif
if (i)
return 1;
/*
* IP addresses matched. The next 32bits contains:
* mast of old IP header security & authentication bits.
*/
FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
/*
* Next we have 32 bits of packet flags.
*/
FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
if (i == 0) {
/*
* If a fragment, then only the first has what we're
* looking for here...
*/
if (portcmp) {
i = 1;
} else {
i = 1;
i = 1;
FR_DEBUG(("i. %#x & %#x != %#x\n",
i = 1;
}
}
}
}
return i;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_scanlist */
/* Returns: int - result flags of scanning filter list */
/* Parameters: fin(I) - pointer to packet information */
/* pass(I) - default result to return for filtering */
/* */
/* If a match is found, the value of fr_flags from the rule becomes the */
/* return value and fin->fin_fr points to the matched rule. */
/* */
/* This function may be called recusively upto 16 times (limit inbuilt.) */
/* When unwinding, it should finish up with fin_depth as 0. */
/* */
/* Could be per interface, but this gets real nasty when you don't have, */
/* or can't easily change, the kernel source code to . */
/* ------------------------------------------------------------------------ */
{
/*
* Do not allow nesting deeper than 16 levels.
*/
return pass;
/*
* If there are no rules in this list, return now.
*/
return pass;
skip = 0;
logged = 0;
portcmp = 0;
portcmp = 1;
if (skip != 0) {
skip--;
continue;
}
/*
* In all checks below, a null (zero) value in the
* filter struture is taken to mean a wildcard.
*
* check that we are working for the right interface
*/
#ifdef _KERNEL
continue;
#else
printf("\n");
continue;
FR_VERBOSE((":i"));
#endif
{
case FR_T_IPF :
case FR_T_IPF|FR_T_BUILTIN :
continue;
break;
#if defined(IPFILTER_BPF)
case FR_T_BPFOPC :
case FR_T_BPFOPC|FR_T_BUILTIN :
{
continue;
continue;
continue;
break;
}
#endif
case FR_T_CALLFUNC|FR_T_BUILTIN :
{
frentry_t *f;
if (f != NULL)
fr = f;
else
continue;
break;
}
default :
break;
}
continue;
continue;
}
/*
* Allowing a rule with the "keep state" flag set to match
* packets that have been tagged "out of window" by the TCP
* state tracking is foolish as the attempt to add a new
* state entry to the table will fail.
*/
continue;
/*
* If the rule is a "call now" rule, then call the function
* in the rule, if it exists and use the results from that.
* If the function pointer is bad, just make like we ignore
* it, except for increasing the hit counter.
*/
if ((passt & FR_CALLNOW) != 0) {
continue;
}
}
} else {
}
#ifdef IPFILTER_LOG
/*
* Just log this packet...
*/
if (passt & FR_LOGORBLOCK) {
passt &= ~FR_CMDMASK;
}
}
logged = 1;
}
#endif /* IPFILTER_LOG */
}
logged = 1;
}
/*
* Finally, if we've asked to track state for this
* packet, set it up. Add state for "quick" rules
* here so that if the action fails we can consider
* the rule to "not match" and keep on processing
* filter rules.
*/
if ((pass & FR_KEEPSTATE) &&
} else {
continue;
}
}
break;
}
}
if (logged)
return pass;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_acctpkt */
/* Returns: frentry_t* - always returns NULL */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Checks a packet against accounting rules, if there are any for the given */
/* IP protocol version. */
/* */
/* N.B.: this function returns NULL to match the prototype used by other */
/* functions called from the IPFilter "mainline" in fr_check(). */
/* ------------------------------------------------------------------------ */
{
char group[FR_GROUPLEN];
#ifdef USE_INET6
else
#endif
if (FR_ISACCOUNT(pass)) {
}
}
return NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_firewall */
/* Returns: frentry_t* - returns pointer to matched rule, if no matches */
/* were found, returns NULL. */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Applies an appropriate set of firewall rules to the packet, to see if */
/* there are any matches. The first check is to see if a match can be seen */
/* in the cache. If not, then search an appropriate list of rules. Once a */
/* matching rule is found, take any appropriate actions as defined by the */
/* rule - except logging. */
/* ------------------------------------------------------------------------ */
{
int out;
#ifdef USE_INET6
else
#endif
if ((pass & FR_NOMATCH)) {
}
/*
* Apply packets per second rate-limiting to a rule as required.
*/
}
/*
* If we fail to add a packet to the authorization queue, then we
* drop the packet later. However, if it was added then pretend
* we've dropped it already.
*/
#ifdef _KERNEL
#else
;
#endif
} else
}
/*
* If a rule is a pre-auth rule, check again in the list of rules
* loaded for authenticated use. It does not particulary matter
* if this search fails because a "preauth" result, from a rule,
* is treated as "not a pass", hence the packet is blocked.
*/
if (FR_ISPREAUTH(pass)) {
}
/*
* If the rule has "keep frag" and the packet is actually a fragment,
* then create a fragment state entry.
*/
} else {
}
} else {
}
}
/*
* Finally, if we've asked to track state for this packet, set it up.
*/
} else {
pass &= ~FR_CMDMASK;
}
}
}
return fr;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_check */
/* Returns: int - 0 == packet allowed through, */
/* User space: */
/* -1 == packet blocked */
/* 1 == packet not matched */
/* -2 == requires authentication */
/* Kernel: */
/* > 0 == filter error # for packet */
/* Parameters: ip(I) - pointer to start of IPv4/6 packet */
/* hlen(I) - length of header */
/* ifp(I) - pointer to interface this packet is on */
/* out(I) - 0 == packet going in, 1 == packet going out */
/* mp(IO) - pointer to caller's buffer pointer that holds this */
/* IP packet. */
/* Solaris & HP-UX ONLY : */
/* qpi(I) - pointer to STREAMS queue information for this */
/* interface & direction. */
/* */
/* fr_check() is the master function for all IPFilter packet processing. */
/* It orchestrates: Network Address Translation (NAT), checking for packet */
/* authorisation (or pre-authorisation), presence of related state info., */
/* generating log entries, IP packet accounting, routing of packets as */
/* directed by firewall rules and of course whether or not to allow the */
/* packet to be further processed by the kernel. */
/* */
/* For packets blocked, the contents of "mp" will be NULL'd and the buffer */
/* freed. Packets passed may be returned with the pointer pointed to by */
/* by "mp" changed to a new buffer. */
/* ------------------------------------------------------------------------ */
void *qif;
#else
#endif
int hlen;
void *ifp;
int out;
{
/*
* The above really sucks, but short of writing a diff
*/
mb_t *m;
#ifdef USE_INET6
#endif
#ifdef _KERNEL
# ifdef MENTAT
#endif
#endif
SPL_INT(s);
/*
* The first part of fr_check() deals with making sure that what goes
* into the filtering engine makes some sense. Information about the
* the packet is distilled, collected into a fr_info_t structure and
* the an attempt to ensure the buffer the packet is in is big enough
* to hold all the required packet headers.
*/
#ifdef _KERNEL
# ifdef MENTAT
return 2;
# endif
if (ifs->ifs_fr_running <= 0) {
return 0;
}
# ifdef MENTAT
# else /* MENTAT */
m = *mp;
# if defined(M_MCAST)
# endif
# if defined(M_MLOOP)
# endif
# if defined(M_BCAST)
# endif
# ifdef M_CANFASTFWD
/*
* XXX For now, IP Filter and fast-forwarding of cached flows
* XXX are mutually exclusive. Eventually, IP Filter should
* XXX get a "can-fast-forward" filter rule.
*/
m->m_flags &= ~M_CANFASTFWD;
# endif /* M_CANFASTFWD */
# ifdef CSUM_DELAY_DATA
/*
* disable delayed checksums.
*/
in_delayed_cksum(m);
}
# endif /* CSUM_DELAY_DATA */
# endif /* MENTAT */
#else
m = *mp;
#endif /* _KERNEL */
SPL_NET(s);
#ifdef USE_INET6
if (v == 6) {
/*
* Jumbo grams are quite likely too big for internal buffer
* structures to handle comfortably, for now, so just drop
* them.
*/
goto filtered;
}
} else
#endif
{
#endif
}
goto filtered;
}
/*
* For at least IPv6 packets, if a m_pullup() fails then this pointer
* becomes NULL and so we have no packet to free.
*/
goto finished;
if (!out) {
if (v == 4) {
#ifdef _KERNEL
}
#endif
}
}
#ifdef USE_INET6
else if (v == 6) {
#ifdef _KERNEL
}
#endif
}
}
#endif
}
}
/*
* Check auth now. This, combined with the check below to see if apass
* is 0 is to ensure that we don't count the packet twice, which can
* otherwise occur when we reprocess it. As it is, we only count it
* after it has no auth. table matchup. This also stops NAT from
* occuring until after the packet has been auth'd.
*/
if (!out) {
{
case 4 :
goto finished;
}
break;
#ifdef USE_INET6
case 6 :
goto finished;
}
break;
#endif
default :
break;
}
}
if (!out)
/*
* interface.
*/
{
case 4 :
goto finished;
}
break;
#ifdef USE_INET6
case 6 :
goto finished;
}
break;
#endif
default :
break;
}
pass &= ~FR_CMDMASK;
} else {
}
}
}
#ifdef IPFILTER_LOG
}
#endif
} else {
}
}
/*
* Only allow FR_DUP to work if a rule matched - it makes no sense to
* set FR_DUP as a "default" as there are no instructions about where
* to send the packet. Use fin_m here because it may have changed
* (without an update of 'm') in prior processing.
*/
}
/*
* Should we return an ICMP packet to indicate error
* status passing through the packet filter ?
* WARNING: ICMP error packets AND TCP RST packets should
* ONLY be sent in repsonse to incoming packets. Sending them
* in response to outbound packets can result in a panic on
* some operating systems.
*/
if (!out) {
if (pass & FR_RETICMP) {
int dst;
dst = 1;
else
dst = 0;
if (fr_send_reset(fin) == 0) {
}
}
} else {
}
}
/*
* If we didn't drop off the bottom of the list of rules (and thus
* the 'current' rule fr is not NULL), then we may have some extra
* instructions about what to do with a packet.
* Once we're finished return to our caller, freeing the packet if
* we are dropping it (* BSD ONLY *).
* Reassign m from fin_m as we may have a new buffer, now.
*/
/*
* For fastroute rule, no destioation interface defined
* so pass NULL as the frdest_t parameter
*/
/* this is for to rules: */
}
/*
* Generate a duplicated packet.
*/
}
/*
* This late because the likes of fr_fastroute() use fin_fr.
*/
}
} else {
}
#endif
}
SPL_X(s);
#ifdef _KERNEL
# if OpenBSD >= 200311
}
# endif
#else /* _KERNEL */
if ((pass & FR_NOMATCH) != 0)
return 1;
if ((pass & FR_RETMASK) != 0)
switch (pass & FR_RETMASK)
{
case FR_RETRST :
return 3;
case FR_RETICMP :
return 4;
case FR_FAKEICMP :
return 5;
}
switch (pass & FR_CMDMASK)
{
case FR_PASS :
return 0;
case FR_BLOCK :
return -1;
case FR_AUTH :
return -2;
case FR_ACCOUNT :
return -3;
case FR_PREAUTH :
return -4;
}
return 2;
#endif /* _KERNEL */
}
#ifdef IPFILTER_LOG
/* ------------------------------------------------------------------------ */
/* Function: fr_dolog */
/* Returns: frentry_t* - returns contents of fin_fr (no change made) */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Checks flags set to see how a packet should be logged, if it is to be */
/* logged. Adjust statistics based on its success or not. */
/* ------------------------------------------------------------------------ */
{
int out;
pass |= FF_LOGNOMATCH;
goto logit;
pass |= FF_LOGPASS;
goto logit;
pass |= FF_LOGBLOCK;
/*
* If the "or-block" option has been used then
* block the packet if we failed to log it.
*/
if ((pass & FR_LOGORBLOCK) &&
pass &= ~FR_CMDMASK;
}
}
}
}
#endif /* IPFILTER_LOG */
/* ------------------------------------------------------------------------ */
/* Function: ipf_cksum */
/* Returns: u_short - IP header checksum */
/* Parameters: addr(I) - pointer to start of buffer to checksum */
/* len(I) - length of buffer in bytes */
/* */
/* Calculate the two's complement 16 bit checksum of the buffer passed. */
/* */
/* N.B.: addr should be 16bit aligned. */
/* ------------------------------------------------------------------------ */
int len;
{
/* mop up an odd byte, if necessary */
if (len == 1)
/*
* add back carry outs from top 16 bits to low 16 bits
*/
}
/* ------------------------------------------------------------------------ */
/* Function: fr_cksum */
/* Returns: u_short - layer 4 checksum */
/* Parameters: m(I ) - pointer to buffer holding packet */
/* ip(I) - pointer to IP header */
/* l4proto(I) - protocol to caclulate checksum for */
/* l4hdr(I) - pointer to layer 4 header */
/* */
/* Calculates the TCP checksum for the packet held in "m", using the data */
/* in the IP header "ip" to seed it. */
/* */
/* NB: This function assumes we've pullup'd enough for all of the IP header */
/* and the TCP header. We also assume that data blocks aren't allocated in */
/* odd sizes. */
/* */
/* Expects ip_len to be in host byte order when called. */
/* ------------------------------------------------------------------------ */
mb_t *m;
int l4proto;
void *l4hdr;
{
int hlen;
#ifdef USE_INET6
#endif
sumsave = 0;
l4hlen = 0;
slen = 0;
hlen = 0;
sum = 0;
/*
* Add up IP Header portion
*/
#ifdef USE_INET6
#endif
#ifdef USE_INET6
}
#endif
switch (l4proto)
{
case IPPROTO_UDP :
break;
case IPPROTO_TCP :
break;
case IPPROTO_ICMP :
l4hlen = 4;
sum = 0;
break;
default :
break;
}
*csump = 0;
}
#ifdef _KERNEL
# ifdef MENTAT
{
}
# else /* MENTAT */
# if BSD >= 199103
# else
# endif
# if BSD >= 199103
# else
# endif
/*
* Both sum and sum2 are partial sums, so combine them together.
*/
while (sum > 0xffff)
# else /* defined(BSD) || defined(sun) */
{
union {
u_char c[2];
u_short s;
} bytes;
# if defined(__sgi)
int add;
# endif
/*
* Add up IP Header portion
*/
switch (l4proto)
{
case IPPROTO_UDP :
break;
case IPPROTO_TCP :
break;
case IPPROTO_ICMP :
break;
}
# ifdef __sgi
/*
* In case we had to copy the IP & TCP header out of mbufs,
* skip over the mbuf bits which are the header
*/
while (hlen) {
m = m->m_next;
if (!hlen) {
if (!m)
break;
}
PANIC((!m),("fr_cksum(1): not enough data"));
}
}
}
# endif
if (len <= 0)
goto nodata;
while (len > 1) {
m = m->m_next;
PANIC((!m),("fr_cksum(2): not enough data"));
}
m = m->m_next;
PANIC((!m),("fr_cksum(3): not enough data"));
}
} else
len -= 2;
}
if (len != 0)
while (sum > 0xffff)
}
# endif /* defined(BSD) || defined(sun) */
# endif /* MENTAT */
#else /* _KERNEL */
if (slen)
while (sum > 0xffff)
#endif /* _KERNEL */
return sum2;
}
/*
* Copyright (c) 1982, 1986, 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
* $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $
*/
/*
* Copy data from an mbuf chain starting "off" bytes from the beginning,
* continuing for "len" bytes, into the indicated buffer.
*/
void
mb_t *m;
int off;
int len;
{
unsigned count;
panic("m_copydata");
while (off > 0) {
if (m == 0)
panic("m_copydata");
break;
m = m->m_next;
}
while (len > 0) {
if (m == 0)
panic("m_copydata");
off = 0;
m = m->m_next;
}
}
/*
* Copy data from a buffer back into the indicated mbuf chain,
* starting "off" bytes from the beginning, extending the mbuf
* chain if necessary.
*/
void
int off;
int len;
{
int mlen;
int totlen = 0;
if (m0 == 0)
return;
if (m->m_next == 0) {
if (n == 0)
goto out;
m->m_next = n;
}
m = m->m_next;
}
while (len > 0) {
off = 0;
if (len == 0)
break;
if (m->m_next == 0) {
if (n == 0)
break;
m->m_next = n;
}
m = m->m_next;
}
out:
#if 0
#endif
return;
}
#endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
/* ------------------------------------------------------------------------ */
/* Function: fr_findgroup */
/* Returns: frgroup_t * - NULL = group not found, else pointer to group */
/* Parameters: group(I) - group name to search for */
/* unit(I) - device to which this group belongs */
/* fgpp(O) - pointer to place to store pointer to the pointer */
/* to where to add the next (last) group or where */
/* to delete group from. */
/* */
/* Search amongst the defined groups for a particular group number. */
/* ------------------------------------------------------------------------ */
char *group;
int set;
{
/*
* Which list of groups to search in is dependent on which list of
* rules are being operated on.
*/
break;
else
}
return fg;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_addgroup */
/* Returns: frgroup_t * - NULL == did not create group, */
/* != NULL == pointer to the group */
/* Parameters: num(I) - group number to add */
/* head(I) - rule pointer that is using this as the head */
/* flags(I) - rule flags which describe the type of rule it is */
/* unit(I) - device to which this group will belong to */
/* Write Locks: ipf_mutex */
/* */
/* Add a new group head, or if it already exists, increase the reference */
/* count to it. */
/* ------------------------------------------------------------------------ */
char *group;
void *head;
int set;
{
return NULL;
return NULL;
return NULL;
return fg;
}
}
return fg;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_delgroup */
/* Returns: Nil */
/* Parameters: group(I) - group name to delete */
/* unit(I) - device to which this group belongs */
/* Write Locks: ipf_mutex */
/* */
/* Attempt to delete a group head. */
/* Only do this when its reference count reaches 0. */
/* ------------------------------------------------------------------------ */
char *group;
int set;
{
return;
}
}
/* ------------------------------------------------------------------------ */
/* Function: fr_getrulen */
/* Returns: frentry_t * - NULL == not found, else pointer to rule n */
/* Parameters: unit(I) - device for which to count the rule's number */
/* flags(I) - which set of rules to find the rule in */
/* group(I) - group name */
/* n(I) - rule number to find */
/* */
/* Find rule # n in group # g and return a pointer to it. Return NULl if */
/* group # g doesn't exist or there are less than n rules in the group. */
/* ------------------------------------------------------------------------ */
int unit;
char *group;
u_32_t n;
{
return NULL;
;
if (n != 0)
return NULL;
return fr;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_rulen */
/* Returns: int - >= 0 - rule number, -1 == search failed */
/* Parameters: unit(I) - device for which to count the rule's number */
/* fr(I) - pointer to rule to match */
/* */
/* Return the number for a rule on a specific filtering device. */
/* ------------------------------------------------------------------------ */
int unit;
{
u_32_t n = 0;
return -1;
return -1;
break;
return -1;
return n;
}
/* ------------------------------------------------------------------------ */
/* Function: frflushlist */
/* Returns: int - >= 0 - number of flushed rules */
/* unit(I) - device for which to flush rules */
/* flags(I) - which set of rules to flush */
/* nfreedp(O) - pointer to int where flush count is stored */
/* listp(I) - pointer to list to flush pointer */
/* Write Locks: ipf_mutex */
/* */
/* Recursively flush rules from the list, descending groups as they are */
/* encountered. if a rule is the head of a group and it has lost all its */
/* group members, then also delete the group reference. nfreedp is needed */
/* to store the accumulating count of rules removed, whereas the returned */
/* value is just the number removed from the current list. The latter is */
/* needed to correctly adjust reference counts on rules that define groups. */
/* */
/* NOTE: Rules not loaded from user space cannot be flushed. */
/* ------------------------------------------------------------------------ */
int set;
int *nfreedp;
{
int freed = 0;
continue;
}
}
}
freed++;
}
return freed;
}
/* ------------------------------------------------------------------------ */
/* Function: frflush */
/* Returns: int - >= 0 - number of flushed rules */
/* Parameters: unit(I) - device for which to flush rules */
/* flags(I) - which set of rules to flush */
/* */
/* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
/* and IPv6) as defined by the value of flags. */
/* ------------------------------------------------------------------------ */
{
}
}
}
}
}
}
if (unit == IPL_LOGIPF) {
int tmp;
if (tmp >= 0)
}
return flushed;
}
/* ------------------------------------------------------------------------ */
/* Function: memstr */
/* Returns: char * - NULL if failed, != NULL pointer to matching bytes */
/* Parameters: src(I) - pointer to byte sequence to match */
/* dst(I) - pointer to byte sequence to search */
/* slen(I) - match length */
/* dlen(I) - length available to search in */
/* */
/* Search dst for a sequence of bytes matching those at src and extend for */
/* slen bytes. */
/* ------------------------------------------------------------------------ */
{
char *s = NULL;
s = dst;
break;
}
dst++;
dlen--;
}
return s;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_fixskip */
/* Returns: Nil */
/* Parameters: listp(IO) - pointer to start of list with skip rule */
/* addremove(I) - adjustment (-1/+1) to make to skip count, */
/* depending on whether a rule was just added */
/* or removed. */
/* */
/* Adjust all the rules in a list which would have skip'd past the position */
/* where we are inserting to skip to the right place given the change. */
/* ------------------------------------------------------------------------ */
int addremove;
{
rules = 0;
rules++;
if (!fp)
return;
}
#ifdef _KERNEL
/* ------------------------------------------------------------------------ */
/* Function: count4bits */
/* Returns: int - >= 0 - number of consecutive bits in input */
/* Parameters: ip(I) - 32bit IP address */
/* */
/* IPv4 ONLY */
/* count consecutive 1's in bit mask. If the mask generated by counting */
/* consecutive 1's is different to that passed, return -1, else return # */
/* of bits. */
/* ------------------------------------------------------------------------ */
int count4bits(ip)
{
int cnt = 0, i, j;
if (ipn & 0x80000000)
cnt++;
else
break;
ipn = 0;
for (i = 32, j = cnt; i; i--, j--) {
ipn *= 2;
if (j > 0)
ipn++;
}
return cnt;
return -1;
}
#ifdef USE_INET6
/* ------------------------------------------------------------------------ */
/* Function: count6bits */
/* Returns: int - >= 0 - number of consecutive bits in input */
/* Parameters: msk(I) - pointer to start of IPv6 bitmask */
/* */
/* IPv6 ONLY */
/* count consecutive 1's in bit mask. */
/* ------------------------------------------------------------------------ */
int count6bits(msk)
{
int i = 0, k;
u_32_t j;
for (k = 3; k >= 0; k--)
if (msk[k] == 0xffffffff)
i += 32;
else {
for (j = msk[k]; j; j <<= 1)
if (j & 0x80000000)
i++;
}
return i;
}
# endif
#endif /* _KERNEL */
/* ------------------------------------------------------------------------ */
/* Function: fr_ifsync */
/* Returns: void * - new interface identifier */
/* Parameters: action(I) - type of synchronisation to do */
/* v(I) - IP version being sync'd (v4 or v6) */
/* newifp(I) - interface identifier being introduced/removed */
/* oldifp(I) - interface identifier in a filter rule */
/* newname(I) - name associated with oldifp interface */
/* oldname(I) - name associated with newifp interface */
/* */
/* This function returns what the new value for "oldifp" should be for its */
/* caller. In some cases it will not change, in some it will. */
/* action == IPFSYNC_RESYNC */
/* a new value for oldifp will always be looked up, according to oldname, */
/* the values of newname and newifp are ignored. */
/* action == IPFSYNC_NEWIFP */
/* if oldname matches newname then we are doing a sync for the matching */
/* interface, so we return newifp to be used in place of oldifp. If the */
/* the names don't match, just return oldifp. */
/* action == IPFSYNC_OLDIFP */
/* if oldifp matches newifp then we are are doing a sync to remove any */
/* references to oldifp, so we return "-1". */
/* ------------------------------------------------------------------------ */
int action, v;
{
switch (action)
{
case IPFSYNC_RESYNC :
if (oldname[0] != '\0') {
}
break;
case IPFSYNC_NEWIFP :
break;
case IPFSYNC_OLDIFP :
break;
}
return rval;
}
/* ------------------------------------------------------------------------ */
/* Function: frsynclist */
/* Returns: void */
/* Parameters: action(I) - type of synchronisation to do */
/* v(I) - IP version being sync'd (v4 or v6) */
/* ifp(I) - interface identifier associated with action */
/* name(I) - name associated with ifp parameter */
/* Write Locks: ipf_mutex */
/* */
/* Walk through a list of filter rules and resolve any interface names into */
/* pointers. Where dynamic addresses are used, also update the IP address */
/* used in the rule. The interface pointer is used to limit the lookups to */
/* a specific set of matching names if it is non-NULL. */
/* ------------------------------------------------------------------------ */
int action, v;
void *ifp;
char *ifname;
{
int rv, i;
if (v != 0 && v != rv)
continue;
/*
* Lookup all the interface names that are part of the rule.
*/
for (i = 0; i < 4; i++) {
fr->fr_ifnames[i],
ifs);
}
if (action != IPFSYNC_RESYNC)
continue;
ifs);
}
ifs);
}
}
#ifdef IPFILTER_LOOKUP
}
}
#endif
}
}
#ifdef _KERNEL
/* ------------------------------------------------------------------------ */
/* Function: frsync */
/* Returns: void */
/* Parameters: action(I) - type of synchronisation to do */
/* v(I) - IP version being sync'd (v4 or v6) */
/* ifp(I) - interface identifier associated with action */
/* name(I) - name associated with ifp parameter */
/* */
/* frsync() is called when we suspect that the interface list or */
/* information about interfaces (like IP#) has changed. Go through all */
/* filter rules, NAT entries and the state table and check if anything */
/* With the filtering hooks added to Solaris, we needed to change the manner*/
/* in which this was done to support three different types of sync: */
/* - complete resync of all interface name/identifiers */
/* - new interface being announced with its name and identifier */
/* - interface removal being announced by only its identifier */
/* ------------------------------------------------------------------------ */
int action, v;
void *ifp;
char *name;
{
int i;
for (i = 0; i < IPL_LOGSIZE; i++) {
frgroup_t *g;
}
}
/*
* In the functions below, bcopy() is called because the pointer being
* copied _from_ in this instance is a pointer to a char buf (which could
* end up being unaligned) and on the kernel's local stack.
*/
/* ------------------------------------------------------------------------ */
/* Function: copyinptr */
/* Returns: int - 0 = success, else failure */
/* Parameters: src(I) - pointer to the source address */
/* dst(I) - destination address */
/* size(I) - number of bytes to copy */
/* */
/* Copy a block of data in from user space, given a pointer to the pointer */
/* to start copying from (src) and a pointer to where to store it (dst). */
/* NB: src - pointer to user space pointer, dst - kernel space pointer */
/* ------------------------------------------------------------------------ */
{
int err;
# if SOLARIS
if (err != 0)
return err;
# else
# endif
return err;
}
/* ------------------------------------------------------------------------ */
/* Function: copyoutptr */
/* Returns: int - 0 = success, else failure */
/* Parameters: src(I) - pointer to the source address */
/* dst(I) - destination address */
/* size(I) - number of bytes to copy */
/* */
/* Copy a block of data out to user space, given a pointer to the pointer */
/* to start copying from (src) and a pointer to where to store it (dst). */
/* NB: src - kernel space pointer, dst - pointer to user space pointer. */
/* ------------------------------------------------------------------------ */
{
int err;
# if SOLARIS
if (err != 0)
return err;
# else
# endif
return err;
}
#endif
/* ------------------------------------------------------------------------ */
/* Function: fr_lock */
/* Returns: int - 0 = success, else error */
/* Parameters: data(I) - pointer to lock value to set */
/* lockp(O) - pointer to location to store old lock value */
/* */
/* Get the new value for the lock integer, set it and return the old value */
/* in *lockp. */
/* ------------------------------------------------------------------------ */
int *lockp;
{
if (err != 0)
return (EFAULT);
if (err != 0)
return (EFAULT);
return (0);
}
/* ------------------------------------------------------------------------ */
/* Function: fr_getstat */
/* Returns: Nil */
/* Parameters: fiop(I) - pointer to ipfilter stats structure */
/* */
/* Stores a copy of current pointers, counters, etc, in the friostat */
/* structure. */
/* ------------------------------------------------------------------------ */
{
int i, j;
sizeof(filterstats_t) * 2);
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++) {
}
for (i = 0; i < IPL_LOGSIZE; i++) {
}
#ifdef IPFILTER_LOG
#else
#endif
}
#ifdef USE_INET6
ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */
-1, /* 1: UNUSED */
-1, /* 2: UNUSED */
ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */
-1, /* 4: ICMP_SOURCEQUENCH */
ND_REDIRECT, /* 5: ICMP_REDIRECT */
-1, /* 6: UNUSED */
-1, /* 7: UNUSED */
ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */
-1, /* 9: UNUSED */
-1, /* 10: UNUSED */
ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */
ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */
-1, /* 13: ICMP_TSTAMP */
-1, /* 14: ICMP_TSTAMPREPLY */
-1, /* 15: ICMP_IREQ */
-1, /* 16: ICMP_IREQREPLY */
-1, /* 17: ICMP_MASKREQ */
-1, /* 18: ICMP_MASKREPLY */
};
int icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */
ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */
-1, /* 2: ICMP_UNREACH_PROTOCOL */
ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */
-1, /* 4: ICMP_UNREACH_NEEDFRAG */
ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */
ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */
ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
-1, /* 8: ICMP_UNREACH_ISOLATED */
ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */
ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */
-1, /* 11: ICMP_UNREACH_TOSNET */
-1, /* 12: ICMP_UNREACH_TOSHOST */
ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
};
#endif
/* ------------------------------------------------------------------------ */
/* Function: fr_matchicmpqueryreply */
/* Returns: int - 1 if "icmp" is a valid reply to "ic" else 0. */
/* Parameters: v(I) - IP protocol version (4 or 6) */
/* ic(I) - ICMP information */
/* icmp(I) - ICMP packet header */
/* rev(I) - direction (0 = forward/1 = reverse) of packet */
/* */
/* Check if the ICMP packet defined by the header pointed to by icmp is a */
/* reply to one as described by what's in ic. If it is a match, return 1, */
/* else return 0 for no match. */
/* ------------------------------------------------------------------------ */
int v;
icmpinfo_t *ic;
int rev;
{
int ictype;
if (v == 4) {
/*
* If we matched its type on the way in, then when going out
* it will still be the same type.
*/
return 1;
return 1;
}
}
#ifdef USE_INET6
else if (v == 6) {
return 1;
return 1;
}
}
#endif
return 0;
}
#ifdef IPFILTER_LOOKUP
/* ------------------------------------------------------------------------ */
/* Function: fr_resolvelookup */
/* Returns: void * - NULL = failure, else success. */
/* Parameters: type(I) - type of lookup these parameters are for. */
/* number(I) - table number to use when searching */
/* funcptr(IO) - pointer to pointer for storing IP address */
/* searching function. */
/* */
/* Search for the "table" number passed in amongst those configured for */
/* that particular type. If the type is recognised then the function to */
/* call to do the IP address search will be change, regardless of whether */
/* or not the "table" number exists. */
/* ------------------------------------------------------------------------ */
{
char name[FR_GROUPLEN];
void *ptr;
#else
#endif
switch (type)
{
case IPLT_POOL :
# else
}
# endif
break;
case IPLT_HASH :
}
*funcptr = fr_iphmfindip;
break;
default:
break;
}
return ptr;
}
#endif
/* ------------------------------------------------------------------------ */
/* Function: frrequest */
/* Returns: int - 0 == success, > 0 == errno value */
/* Parameters: unit(I) - device for which this is for */
/* req(I) - ioctl command (SIOC*) */
/* data(I) - pointr to ioctl data */
/* set(I) - 1 or 0 (filter set) */
/* makecopy(I) - flag indicating whether data points to a rule */
/* in kernel space & hence doesn't need copying. */
/* */
/* This function handles all the requests which operate on the list of */
/* filter rules. This includes adding, deleting, insertion. It is also */
/* responsible for creating groups when a "head" rule is loaded. Interface */
/* names are resolved here and other sanity checks are made on the content */
/* of the rule structure being loaded. If a rule has user defined timeouts */
/* then make sure they are created and initialised before exiting. */
/* ------------------------------------------------------------------------ */
int unit;
{
char *group;
if (makecopy != 0) {
if (error)
return EFAULT;
return EINVAL;
} else {
return EINVAL;
}
return EINVAL;
/*
* Only filter rules for IPv4 or IPv6 are accepted.
*/
if (v == 4)
/*EMPTY*/;
#ifdef USE_INET6
else if (v == 6)
/*EMPTY*/;
#endif
else {
return EINVAL;
}
/*
* If the rule is being loaded from user space, i.e. we had to copy it
* into kernel space, then do not trust the function pointer in the
* rule.
*/
return ESRCH;
if (error != 0)
return error;
}
/*
* matches what the rule is.
*/
*group = '\0';
unit = IPL_LOGCOUNT;
return ESRCH;
return ESRCH;
}
/*
* Work out which rule list this change is being applied to.
*/
if (unit == IPL_LOGAUTH)
else if (v == 4) {
} else if (v == 6) {
}
return ESRCH;
if (*group != '\0') {
return ESRCH;
}
f = NULL;
break;
}
}
/*
* Copy in extra data for the rule.
*/
if (makecopy != 0) {
if (!ptr)
return ENOMEM;
} else {
error = 0;
}
if (error != 0) {
return ENOMEM;
}
} else
/*
* Perform per-rule type sanity checks of their members.
*/
{
#if defined(IPFILTER_BPF)
case FR_T_BPFOPC :
return EINVAL;
}
return EINVAL;
}
break;
#endif
case FR_T_IPF :
return EINVAL;
/*
* Allowing a rule with both "keep state" and "with oow" is
* pointless because adding a state entry to the table will
* fail with the out of window (oow) flag set.
*/
return EINVAL;
{
case FRI_BROADCAST :
case FRI_DYNAMIC :
case FRI_NETWORK :
case FRI_NETMASKED :
case FRI_PEERADDR :
}
return EINVAL;
}
break;
#ifdef IPFILTER_LOOKUP
case FRI_LOOKUP :
break;
#endif
default :
break;
}
{
case FRI_BROADCAST :
case FRI_DYNAMIC :
case FRI_NETWORK :
case FRI_NETMASKED :
case FRI_PEERADDR :
}
return EINVAL;
}
break;
#ifdef IPFILTER_LOOKUP
case FRI_LOOKUP :
break;
#endif
default :
break;
}
break;
case FR_T_NONE :
break;
case FR_T_CALLFUNC :
break;
case FR_T_COMPIPF :
break;
default :
}
return EINVAL;
}
/*
* Lookup all the interface names that are part of the rule.
*/
fp->fr_statecnt = 0;
/*
* Look for an existing matching filter rule, but don't include the
* next or interface pointer in the comparison (fr_next, fr_ifa).
* This elminates rules which are indentical being loaded. Checksum
* the constant part of the filter rule to make comparisons quicker
* (this meaning no pointers are included).
*/
p < pp; p++)
continue;
continue;
break;
}
/*
* If zero'ing statistics, copy current to caller and zero.
*/
if (f == NULL)
else {
/*
* Copy and reduce lock because of impending copyout.
* Well we should, but if we do then the atomicity of
* this call and the correctness of fr_hits and
* fr_bytes cannot be guaranteed. As it is, this code
* only resets them to 0 if they are successfully
* copied out into user space.
*/
/*
* When we copy this rule back out, set the data
* pointer to be what it was in user space.
*/
if (error == 0) {
f->fr_dsize);
if (error == 0) {
f->fr_hits = 0;
f->fr_bytes = 0;
}
}
}
}
return error;
}
if (!f) {
/*
* At the end of this, ftail must point to the place where the
* For SIOCAD*FR, this should be the last rule in the group of
* rules that have equal fr_collect fields.
* For SIOCIN*FR, ...
*/
break;
}
f = NULL;
error = 0;
break;
}
break;
}
}
f = NULL;
error = 0;
}
}
/*
* Request to remove a rule.
*/
if (!f)
else {
/*
* Do not allow activity from user space to interfere
* with rules not loaded that way.
*/
goto done;
}
/*
* Return EBUSY if the rule is being reference by
* something else (eg state information.
*/
if (f->fr_ref > 1) {
goto done;
}
#ifdef IPFILTER_SCAN
if (f->fr_isctag[0] != '\0' &&
ipsc_detachfr(f);
#endif
if (unit == IPL_LOGAUTH) {
goto done;
}
if (*f->fr_grhead != '\0')
(void)fr_derefrule(&f, ifs);
}
} else {
/*
*/
if (f)
else {
if (unit == IPL_LOGAUTH) {
goto done;
}
if (makecopy) {
} else
f = fp;
if (f != NULL) {
if (fp != f)
sizeof(*f));
MUTEX_NUKE(&f->fr_lock);
#ifdef IPFILTER_SCAN
if (f->fr_isctag[0] != '\0' &&
ipsc_attachfr(f))
#endif
f->fr_hits = 0;
if (makecopy != 0)
f->fr_ref = 1;
*ftail = f;
if (*group != '\0') {
}
} else
}
}
done:
}
return (error);
}
/* ------------------------------------------------------------------------ */
/* Function: fr_funcinit */
/* Returns: int - 0 == success, else ESRCH: cannot resolve rule details */
/* Parameters: fr(I) - pointer to filter rule */
/* */
/* If a rule is a call rule, then check if the function it points to needs */
/* an init function to be called now the rule has been loaded. */
/* ------------------------------------------------------------------------ */
{
int err;
err = 0;
break;
}
return err;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_findfunc */
/* Returns: ipfunc_t - pointer to function if found, else NULL */
/* Parameters: funcptr(I) - function pointer to lookup */
/* */
/* Look for a function in the table of known functions. */
/* ------------------------------------------------------------------------ */
{
return funcptr;
return NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_resolvefunc */
/* Returns: int - 0 == success, else error */
/* Parameters: data(IO) - ioctl data pointer to ipfunc_resolve_t struct */
/* */
/* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
/* This will either be the function name (if the pointer is set) or the */
/* function pointer if the name is set. When found, fill in the other one */
/* so that the entire, complete, structure can be copied back to user space.*/
/* ------------------------------------------------------------------------ */
int fr_resolvefunc(data)
void *data;
{
int err;
if (err != 0)
return EFAULT;
return EFAULT;
return 0;
}
}
return EFAULT;
return 0;
}
}
return ESRCH;
}
#if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \
/*
* From: NetBSD
* ppsratecheck(): packets (or events) per second limitation.
*/
int
int *curpps;
int maxpps; /* maximum pps allowed */
{
int rv;
}
/*
* check for 0,0 is so that the message will be seen at least once.
* if more than one second have passed since the last update of
* lasttime, reset the counter.
*
* we do increment *curpps even in *curpps < maxpps case, as some may
* try to use *curpps for stat purposes as well.
*/
*curpps = 0;
rv = 1;
} else if (maxpps < 0)
rv = 1;
rv = 1;
else
rv = 0;
return (rv);
}
#endif
/* ------------------------------------------------------------------------ */
/* Function: fr_derefrule */
/* Returns: int - 0 == rule freed up, else rule not freed */
/* Parameters: fr(I) - pointer to filter rule */
/* */
/* Decrement the reference counter to a rule by one. If it reaches zero, */
/* free it and any associated storage space being used by it. */
/* ------------------------------------------------------------------------ */
{
#ifdef IPFILTER_LOOKUP
#endif
}
return 0;
}
return 1;
} else {
}
return -1;
}
#ifdef IPFILTER_LOOKUP
/* ------------------------------------------------------------------------ */
/* Function: fr_grpmapinit */
/* Returns: int - 0 == success, else ESRCH because table entry not found*/
/* Parameters: fr(I) - pointer to rule to find hash table for */
/* */
/* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr. */
/* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap. */
/* ------------------------------------------------------------------------ */
{
char name[FR_GROUPLEN];
#else
#endif
return ESRCH;
return ESRCH;
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_srcgrpmap */
/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Look for a rule group head in a hash table, using the source address as */
/* the key, and descend into that group and continue matching rules against */
/* the packet. */
/* ------------------------------------------------------------------------ */
{
void *rval;
return NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_dstgrpmap */
/* Returns: frentry_t * - pointer to "new last matching" rule or NULL */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Look for a rule group head in a hash table, using the destination */
/* address as the key, and descend into that group and continue matching */
/* rules against the packet. */
/* ------------------------------------------------------------------------ */
{
void *rval;
return NULL;
}
#endif /* IPFILTER_LOOKUP */
/*
* Queue functions
* ===============
* These functions manage objects on queues for efficient timeouts. There are
* a number of system defined queues as well as user defined timeouts. It is
* expected that a lock is held in the domain in which the queue belongs
* (i.e. either state or NAT) when calling any of these functions that prevents
* fr_freetimeoutqueue() from being called at the same time as any other.
*/
/* ------------------------------------------------------------------------ */
/* Function: fr_addtimeoutqueue */
/* Returns: struct ifqtq * - NULL if malloc fails, else pointer to */
/* timeout queue with given interval. */
/* Parameters: parent(I) - pointer to pointer to parent node of this list */
/* of interface queues. */
/* seconds(I) - timeout value in seconds for this queue. */
/* */
/* This routine first looks for a timeout queue that matches the interval */
/* being requested. If it finds one, increments the reference counter and */
/* returns a pointer to it. If none are found, it allocates a new one and */
/* inserts it at the top of the list. */
/* */
/* Locking. */
/* It is assumed that the caller of this function has an appropriate lock */
/* held (exclusively) in the domain that encompases 'parent'. */
/* ------------------------------------------------------------------------ */
{
/*
* Reset the delete flag, if set, so the structure
* gets reused rather than freed and reallocated.
*/
return ifq;
}
}
ifs->ifs_fr_userifqs++;
}
return ifq;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_deletetimeoutqueue */
/* Returns: int - new reference count value of the timeout queue */
/* Parameters: ifq(I) - timeout queue which is losing a reference. */
/* Locks: ifq->ifq_lock */
/* */
/* This routine must be called when we're discarding a pointer to a timeout */
/* queue object, taking care of the reference counter. */
/* */
/* Now that this just sets a DELETE flag, it requires the expire code to */
/* check the list of user defined timeout queues and call the free function */
/* below (currently commented out) to stop memory leaking. It is done this */
/* way because the locking may not be sufficient to safely do a free when */
/* this function is called. */
/* ------------------------------------------------------------------------ */
int fr_deletetimeoutqueue(ifq)
{
}
}
/* ------------------------------------------------------------------------ */
/* Function: fr_freetimeoutqueue */
/* Parameters: ifq(I) - timeout queue which is losing a reference. */
/* Returns: Nil */
/* */
/* Locking: */
/* It is assumed that the caller of this function has an appropriate lock */
/* held (exclusively) in the domain that encompases the callers "domain". */
/* The ifq_lock for this structure should not be held. */
/* */
/* Remove a user definde timeout queue from the list of queues it is in and */
/* tidy up after this is done. */
/* ------------------------------------------------------------------------ */
{
printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
return;
}
/*
* Remove from its position in the list.
*/
ifs->ifs_fr_userifqs--;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_deletequeueentry */
/* Returns: Nil */
/* Parameters: tqe(I) - timeout queue entry to delete */
/* ifq(I) - timeout queue to remove entry from */
/* */
/* Remove a tail queue entry from its queue and make it an orphan. */
/* fr_deletetimeoutqueue is called to make sure the reference count on the */
/* queue is correct. We can't, however, call fr_freetimeoutqueue because */
/* the correct lock(s) may not be held that would make it safe to do so. */
/* ------------------------------------------------------------------------ */
void fr_deletequeueentry(tqe)
{
return;
else /* we must be the tail anyway */
}
(void) fr_deletetimeoutqueue(ifq);
}
/* ------------------------------------------------------------------------ */
/* Function: fr_queuefront */
/* Returns: Nil */
/* Parameters: tqe(I) - pointer to timeout queue entry */
/* */
/* Move a queue entry to the front of the queue, if it isn't already there. */
/* ------------------------------------------------------------------------ */
void fr_queuefront(tqe)
{
return;
else
}
}
/* ------------------------------------------------------------------------ */
/* Function: fr_queueback */
/* Returns: Nil */
/* Parameters: tqe(I) - pointer to timeout queue entry */
/* */
/* Move a queue entry to the back of the queue, if it isn't already there. */
/* ------------------------------------------------------------------------ */
{
return;
return;
}
/*
* Remove from list
*/
/*
* Make it the last entry.
*/
}
/* ------------------------------------------------------------------------ */
/* Function: fr_queueappend */
/* Returns: Nil */
/* Parameters: tqe(I) - pointer to timeout queue entry */
/* ifq(I) - pointer to timeout queue */
/* parent(I) - owing object pointer */
/* */
/* Add a new item to this queue and put it on the very end. */
/* ------------------------------------------------------------------------ */
void *parent;
{
}
/* ------------------------------------------------------------------------ */
/* Function: fr_movequeue */
/* Returns: Nil */
/* Parameters: tq(I) - pointer to timeout queue information */
/* oifp(I) - old timeout queue entry was on */
/* nifp(I) - new timeout queue to put entry on */
/* */
/* Move a queue entry from one timeout queue to another timeout queue. */
/* If it notices that the current entry is already last and does not need */
/* to move queue, the return. */
/* ------------------------------------------------------------------------ */
{
/*
* Is the operation here going to be a no-op ?
*/
return;
return;
}
/*
* Remove from the old queue
*/
else
/*
* If we're moving from one queue to another, release the lock on the
* old queue and get a lock on the new queue. For user defined queues,
* if we're moving off it, call delete in case it can now be freed.
*/
(void) fr_deletetimeoutqueue(oifq);
}
/*
* Add to the bottom of the new queue
*/
}
/* ------------------------------------------------------------------------ */
/* Function: fr_updateipid */
/* Returns: int - 0 == success, -1 == error (packet should be droppped) */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* When we are doing NAT, change the IP of every packet to represent a */
/* single sequence of packets coming from the host, hiding any host */
/* specific sequencing that might otherwise be revealed. If the packet is */
/* a fragment, then store the 'new' IPid in the fragment cache and look up */
/* the fragment cache for non-leading fragments. If a non-leading fragment */
/* has no match in the cache, return an error. */
/* ------------------------------------------------------------------------ */
{
if (sum == 0xffffffff)
return -1;
sum &= 0xffff;
} else {
}
return 0;
return 0;
}
#ifdef NEED_FRGETIFNAME
/* ------------------------------------------------------------------------ */
/* Function: fr_getifname */
/* Returns: char * - pointer to interface name */
/* Parameters: ifp(I) - pointer to network interface */
/* buffer(O) - pointer to where to store interface name */
/* */
/* Constructs an interface name in the buffer passed. The buffer passed is */
/* expected to be at least LIFNAMSIZ in bytes big. If buffer is passed in */
/* as a NULL pointer then return a pointer to a static array. */
/* ------------------------------------------------------------------------ */
char *buffer;
{
char temp[20];
char *s;
# endif
#ifdef notdef
#endif
for (s = buffer; *s; s++)
;
if (space > 0) {
# else
# endif
}
# endif
return buffer;
}
#endif
/* ------------------------------------------------------------------------ */
/* Function: fr_ioctlswitch */
/* Returns: int - -1 continue processing, else ioctl return value */
/* Parameters: unit(I) - device unit opened */
/* data(I) - pointer to ioctl data */
/* cmd(I) - ioctl command */
/* mode(I) - mode value */
/* */
/* Based on the value of unit, call the appropriate ioctl handler or return */
/* EIO if ipfilter is not running. Also checks if write perms are req'd */
/* for the device in order to execute the ioctl. */
/* ------------------------------------------------------------------------ */
{
int error = 0;
switch (unit)
{
case IPL_LOGIPF :
error = -1;
break;
case IPL_LOGNAT :
if (ifs->ifs_fr_running > 0)
else
break;
case IPL_LOGSTATE :
if (ifs->ifs_fr_running > 0)
else
break;
case IPL_LOGAUTH :
if (ifs->ifs_fr_running > 0) {
} else {
}
} else {
}
} else
break;
case IPL_LOGSYNC :
#ifdef IPFILTER_SYNC
if (ifs->ifs_fr_running > 0)
else
#endif
break;
case IPL_LOGSCAN :
#ifdef IPFILTER_SCAN
if (ifs->ifs_fr_running > 0)
else
#endif
break;
case IPL_LOGLOOKUP :
#ifdef IPFILTER_LOOKUP
if (ifs->ifs_fr_running > 0)
else
#endif
break;
default :
break;
}
return error;
}
/*
* This array defines the expected size of objects coming into the kernel
* for the various recognised object types.
*/
#define NUM_OBJ_TYPES 19
{ 0, sizeof(struct friostat) },
{ 0, sizeof(struct fr_info) },
{ 0, sizeof(struct fr_authstat) },
{ 0, sizeof(struct ipfrstat) },
{ 0, sizeof(struct ipnat) },
{ 0, sizeof(struct natstat) },
{ 0, sizeof(struct ipstate_save) },
{ 0, sizeof(struct natlookup) },
{ 0, sizeof(struct ips_stat) },
{ 0, sizeof(struct frauth) },
{ 0, sizeof(struct ipftune) },
{ 0, sizeof(struct nat) }, /* nat_t */
{ 0, sizeof(struct ipfruleiter) },
{ 0, sizeof(struct ipfgeniter) },
{ 0, sizeof(struct ipftable) },
{ 0, sizeof(struct ipflookupiter) }
};
/* ------------------------------------------------------------------------ */
/* Function: fr_inobj */
/* Returns: int - 0 = success, else failure */
/* Parameters: data(I) - pointer to ioctl data */
/* ptr(I) - pointer to store real data in */
/* type(I) - type of structure being moved */
/* */
/* Copy in the contents of what the ipfobj_t points to. In future, we */
/* add things to check for version numbers, sizes, etc, to make it backward */
/* compatible at the ABI for user land. */
/* ------------------------------------------------------------------------ */
void *data;
void *ptr;
int type;
{
int error = 0;
return EINVAL;
if (error != 0)
return EFAULT;
return EINVAL;
#ifndef IPFILTER_COMPAT
return EINVAL;
return EINVAL;
#else
return error;
}
return EINVAL;
#endif
} else {
}
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_inobjsz */
/* Returns: int - 0 = success, else failure */
/* Parameters: data(I) - pointer to ioctl data */
/* ptr(I) - pointer to store real data in */
/* type(I) - type of structure being moved */
/* sz(I) - size of data to copy */
/* */
/* As per fr_inobj, except the size of the object to copy in is passed in */
/* but it must not be smaller than the size defined for the type and the */
/* type must allow for varied sized objects. The extra requirement here is */
/* that sz must match the size of the object being passed in - this is not */
/* not possible nor required in fr_inobj(). */
/* ------------------------------------------------------------------------ */
void *data;
void *ptr;
{
int error;
return EINVAL;
return EINVAL;
if (error != 0)
return EFAULT;
return EINVAL;
#ifndef IPFILTER_COMPAT
return EINVAL;
#else
/*XXX compatibility hook here */
/*EMPTY*/;
/* XXX compatibility hook here */
return EINVAL;
#endif
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_outobjsz */
/* Returns: int - 0 = success, else failure */
/* Parameters: data(I) - pointer to ioctl data */
/* ptr(I) - pointer to store real data in */
/* type(I) - type of structure being moved */
/* sz(I) - size of data to copy */
/* */
/* As per fr_outobj, except the size of the object to copy out is passed in */
/* but it must not be smaller than the size defined for the type and the */
/* type must allow for varied sized objects. The extra requirement here is */
/* that sz must match the size of the object being passed in - this is not */
/* not possible nor required in fr_outobj(). */
/* ------------------------------------------------------------------------ */
void *data;
void *ptr;
{
int error;
return EINVAL;
if (error != 0)
return EFAULT;
return EINVAL;
#ifndef IPFILTER_COMPAT
return EINVAL;
#else
/* XXX compatibility hook here */
/*EMPTY*/;
/* XXX compatibility hook here */
return EINVAL;
#endif
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_outobj */
/* Returns: int - 0 = success, else failure */
/* Parameters: data(I) - pointer to ioctl data */
/* ptr(I) - pointer to store real data in */
/* type(I) - type of structure being moved */
/* */
/* Copy out the contents of what ptr is to where ipfobj points to. In */
/* future, we add things to check for version numbers, sizes, etc, to make */
/* it backward compatible at the ABI for user land. */
/* ------------------------------------------------------------------------ */
void *data;
void *ptr;
int type;
{
int error;
return EINVAL;
if (error != 0)
return EFAULT;
return EINVAL;
#ifndef IPFILTER_COMPAT
return EINVAL;
return EINVAL;
#else
return error;
}
return EINVAL;
#endif
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_checkl4sum */
/* Returns: int - 0 = good, -1 = bad, 1 = cannot check */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* If possible, calculate the layer 4 checksum for the packet. If this is */
/* not possible, return without indicating a failure or success but in a */
/* way that is ditinguishable. */
/* ------------------------------------------------------------------------ */
int fr_checkl4sum(fin)
{
int dosum;
else
#endif
return 0;
/*
* If the TCP packet isn't a fragment, isn't too short and otherwise
* isn't already considered "bad", then validate the checksum. If
* this check fails then considered the packet to be "bad".
*/
return 1;
hdrsum = 0;
dosum = 0;
sum = 0;
hdrsum = 0;
sum = 0;
} else {
#endif
{
case IPPROTO_TCP :
dosum = 1;
break;
case IPPROTO_UDP :
dosum = 1;
}
break;
case IPPROTO_ICMP :
dosum = 1;
break;
default :
return 1;
/*NOTREACHED*/
}
if (dosum)
}
#endif
#if !defined(_KERNEL)
} else {
}
#endif
return 0;
return -1;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_ifpfillv4addr */
/* Returns: int - 0 = address update, -1 = address not updated */
/* Parameters: atype(I) - type of network address update to perform */
/* sin(I) - pointer to source of address information */
/* mask(I) - pointer to source of netmask information */
/* inp(I) - pointer to destination address store */
/* inpmask(I) - pointer to destination netmask store */
/* */
/* Given a type of network address update (atype) to perform, copy */
/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
/* which case the operation fails. For all values of atype other than */
/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
/* value. */
/* ------------------------------------------------------------------------ */
int atype;
{
if (atype == FRI_NETMASKED) {
return -1;
}
} else {
}
return 0;
}
#ifdef USE_INET6
/* ------------------------------------------------------------------------ */
/* Function: fr_ifpfillv6addr */
/* Returns: int - 0 = address update, -1 = address not updated */
/* Parameters: atype(I) - type of network address update to perform */
/* sin(I) - pointer to source of address information */
/* mask(I) - pointer to source of netmask information */
/* inp(I) - pointer to destination address store */
/* inpmask(I) - pointer to destination netmask store */
/* */
/* Given a type of network address update (atype) to perform, copy */
/* netmask update is performed unless FRI_NETMASKED is passed as atype, in */
/* which case the operation fails. For all values of atype other than */
/* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s */
/* value. */
/* ------------------------------------------------------------------------ */
int atype;
{
}
if (atype == FRI_NETMASKED) {
return -1;
}
} else {
}
return 0;
}
#endif
/* ------------------------------------------------------------------------ */
/* Function: fr_matchtag */
/* Returns: 0 == mismatch, 1 == match. */
/* Parameters: tag1(I) - pointer to first tag to compare */
/* tag2(I) - pointer to second tag to compare */
/* */
/* Returns true (non-zero) or false(0) if the two tag structures can be */
/* considered to be a match or not match, respectively. The tag is 16 */
/* bytes long (16 characters) but that is overlayed with 4 32bit ints so */
/* compare the ints instead, for speed. tag1 is the master of the */
/* comparison. This function should only be called with both tag1 and tag2 */
/* as non-NULL pointers. */
/* ------------------------------------------------------------------------ */
{
return 1;
return 1;
return 1;
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_coalesce */
/* Returns: 1 == success, -1 == failure, 0 == no change */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Attempt to get all of the packet data into a single, contiguous buffer. */
/* If this call returns a failure then the buffers have also been freed. */
/* ------------------------------------------------------------------------ */
int fr_coalesce(fin)
{
return 1;
/*
* If the mbuf pointers indicate that there is no mbuf to work with,
* return but do not indicate success or failure.
*/
return 0;
#if defined(_KERNEL)
# ifdef MENTAT
# endif
return -1;
}
#else
#endif
return 1;
}
/*
* The following table lists all of the tunable variables that can be
* accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT. The format of each row
* in the table below is as follows:
*
* pointer to value, name of value, minimum, maximum, size of the value's
* container, value attribute flags
*
* For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
* means the value can only be written to when IPFilter is loaded but disabled.
* The obvious implication is if neither of these are set then the value can be
* changed at any time without harm.
*/
/* filtering */
0, 0 },
{ { NULL }, "fr_active", 0, 0,
0, IPFT_RDONLY },
0, 0 },
0, 0 },
0, 0 },
0, 0 },
0, 0 },
0, 0 },
#if SOLARIS2 >= 10
0, IPFT_WRDISABLED },
#endif
/* state */
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, 0 },
0, IPFT_WRDISABLED },
0, IPFT_RDONLY },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, 0 },
/* nat */
0, IPFT_RDONLY },
0, IPFT_WRDISABLED },
0, 0 },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, 0 },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
0, 0 },
0, 0 },
/* frag */
0, IPFT_WRDISABLED },
0, IPFT_WRDISABLED },
#ifdef IPFILTER_LOG
/* log */
0, 0 },
{ { NULL }, "ipl_buffer_sz", 0, 0,
0, IPFT_RDONLY },
0, IPFT_WRDISABLED },
0, 0 },
0, 0 },
#endif
};
static ipftuneable_t *
{
int i;
return (&ifs->ifs_ipf_tuneables[i]);
}
return (NULL);
}
#ifdef _KERNEL
extern dev_info_t *ipf_dev_info;
#endif
/* -------------------------------------------------------------------- */
/* Function: ipftuneable_setdefs() */
/* Returns: void */
/* Parameters: ifs - pointer to newly allocated IPF instance */
/* assigned to IP instance */
/* */
/* Function initializes IPF instance variables. Function is invoked */
/* from ipftuneable_alloc(). ipftuneable_alloc() is called only one */
/* time during IP instance lifetime - at the time of IP instance */
/* creation. Anytime IP instance is being created new private IPF */
/* instance is allocated and assigned to it. The moment of IP */
/* instance creation is the right time to initialize those IPF */
/* variables. */
/* */
/* -------------------------------------------------------------------- */
{
/* it comes from fr_authinit() in IPF auth */
/* it comes from fr_stateinit() in IPF state */
/* it comes from fr_natinit() in ipnat */
#ifdef IPFILTER_LOG
/* it comes from fr_loginit() in IPF log */
/* from fr_natinit() */
/* from fr_stateinit() */
#else
/* from fr_natinit() */
ifs->ifs_nat_logging = 0;
/* from fr_stateinit() */
ifs->ifs_ipstate_logging = 0;
#endif
ifs->ifs_ipf_loopback = 0;
}
/*
* Allocate a per-stack tuneable and copy in the names. Then
* set it to point to each of the per-stack tunables.
*/
void
{
sizeof (lcl_ipf_tuneables));
sizeof (lcl_ipf_tuneables));
}
#ifdef IPFILTER_LOG
#endif
#ifdef _KERNEL
#endif
}
void
{
}
/* ------------------------------------------------------------------------ */
/* Function: fr_findtunebycookie */
/* Returns: NULL = search failed, else pointer to tune struct */
/* Parameters: cookie(I) - cookie value to search for amongst tuneables */
/* next(O) - pointer to place to store the cookie for the */
/* "next" tuneable, if it is desired. */
/* */
/* This function is used to walk through all of the existing tunables with */
/* successive calls. It searches the known tunables for the one which has */
/* a matching value for "cookie" - ie its address. When returning a match, */
/* the next one to be found may be returned inside next. */
/* ------------------------------------------------------------------------ */
ipf_stack_t * ifs;
{
/*
* If the next entry in the array has a name
* present, then return a pointer to it for
* where to go next, else return a pointer to
* the dynaminc list as a key to search there
* next. This facilitates a weak linking of
* the two "lists" together.
*/
else
}
return ta;
}
return ta;
}
return NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_findtunebyname */
/* Returns: NULL = search failed, else pointer to tune struct */
/* Parameters: name(I) - name of the tuneable entry to find. */
/* */
/* Search the static array of tuneables and the list of dynamic tuneables */
/* for an entry with a matching name. If we can find one, return a pointer */
/* to the matching structure. */
/* ------------------------------------------------------------------------ */
const char *name;
{
return ta;
}
return ta;
}
return NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_addipftune */
/* Returns: int - 0 == success, else failure */
/* Parameters: newtune - pointer to new tune struct to add to tuneables */
/* */
/* Appends the tune structure pointer to by "newtune" to the end of the */
/* current list of "dynamic" tuneable parameters. Once added, the owner */
/* of the object is not expected to ever change "ipft_next". */
/* ------------------------------------------------------------------------ */
{
return EEXIST;
;
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_delipftune */
/* Returns: int - 0 == success, else failure */
/* Parameters: oldtune - pointer to tune struct to remove from the list of */
/* current dynamic tuneables */
/* */
/* Search for the tune structure, by pointer, in the list of those that are */
/* dynamically added at run time. If found, adjust the list so that this */
/* structure is no longer part of it. */
/* ------------------------------------------------------------------------ */
{
return 0;
}
return ESRCH;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_ipftune */
/* Returns: int - 0 == success, else failure */
/* Parameters: cmd(I) - ioctl command number */
/* data(I) - pointer to ioctl data structure */
/* */
/* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET. These */
/* three ioctls provide the means to access and control global variables */
/* within IPFilter, allowing (for example) timeouts and table sizes to be */
/* changed without rebooting, reloading or recompiling. The initialisation */
/* and 'destruction' routines of the various components of ipfilter are all */
/* each responsible for handling their own values being too big. */
/* ------------------------------------------------------------------------ */
void *data;
{
void *cookie;
int error;
if (error != 0)
return error;
switch (cmd)
{
case SIOCIPFGETNEXT :
/*
* If cookie is non-NULL, assume it to be a pointer to the last
* entry we looked at, so find it (if possible) and return a
* pointer to the next one after it. The last entry in the
* the table is a NULL entry, so when we get to it, set cookie
* to NULL and return that, indicating end of list, erstwhile
* if we come in with cookie set to NULL, we are starting anew
* at the front of the list.
*/
} else {
}
/*
* Entry found, but does the data pointed to by that
* row fit in what we can return?
*/
return EINVAL;
tu.ipft_vlong = 0;
}
break;
case SIOCIPFGET :
case SIOCIPFSET :
/*
* Search by name or by cookie value for a particular entry
* in the tuning paramter table.
*/
error = 0;
error = 0;
}
if (error != 0)
break;
/*
* Fetch the tuning parameters for a particular value
*/
tu.ipft_vlong = 0;
/*
* Set an internal parameter. The hard part here is
* getting the new value safely and correctly out of
* the kernel (given we only know its size, not type.)
*/
(ifs->ifs_fr_running > 0)) {
break;
}
break;
}
}
}
break;
default :
break;
}
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_initialise */
/* Returns: int - 0 == success, < 0 == failure */
/* Parameters: None. */
/* */
/* Call of the initialise functions for all the various subsystems inside */
/* of IPFilter. If any of them should fail, return immeadiately a failure */
/* BUT do not try to recover from the error here. */
/* ------------------------------------------------------------------------ */
int fr_initialise(ifs)
{
int i;
#ifdef IPFILTER_LOG
i = fr_loginit(ifs);
if (i < 0)
return -10 + i;
#endif
i = fr_natinit(ifs);
if (i < 0)
return -20 + i;
i = fr_stateinit(ifs);
if (i < 0)
return -30 + i;
i = fr_authinit(ifs);
if (i < 0)
return -40 + i;
i = fr_fraginit(ifs);
if (i < 0)
return -50 + i;
if (i < 0)
return -60 + i;
#ifdef IPFILTER_SYNC
i = ipfsync_init(ifs);
if (i < 0)
return -70 + i;
#endif
#ifdef IPFILTER_SCAN
if (i < 0)
return -80 + i;
#endif
#ifdef IPFILTER_LOOKUP
i = ip_lookup_init(ifs);
if (i < 0)
return -90 + i;
#endif
#ifdef IPFILTER_COMPILED
#endif
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_deinitialise */
/* Returns: None. */
/* Parameters: None. */
/* */
/* Call all the various subsystem cleanup routines to deallocate memory or */
/* destroy locks or whatever they've done that they need to now undo. */
/* The order here IS important as there are some cross references of */
/* internal data structures. */
/* ------------------------------------------------------------------------ */
void fr_deinitialise(ifs)
{
#ifdef IPFILTER_SCAN
#endif
#ifdef IPFILTER_COMPILED
#endif
#ifdef IPFILTER_LOOKUP
#endif
#ifdef IPFILTER_LOG
#endif
}
/* ------------------------------------------------------------------------ */
/* Function: fr_zerostats */
/* Returns: int - 0 = success, else failure */
/* Parameters: data(O) - pointer to pointer for copying data back to */
/* */
/* Copies the current statistics out to userspace and then zero's the */
/* current ones in the kernel. The lock is only held across the bzero() as */
/* the copyout may result in paging (ie network activity.) */
/* ------------------------------------------------------------------------ */
{
int error;
if (error)
return EFAULT;
return 0;
}
#ifdef _KERNEL
/* ------------------------------------------------------------------------ */
/* Function: fr_resolvedest */
/* Returns: Nil */
/* Parameters: fdp(IO) - pointer to destination information to resolve */
/* v(I) - IP protocol version to match */
/* */
/* Looks up an interface name in the frdest structure pointed to by fdp and */
/* if a matching name can be found for the particular IP protocol version */
/* then store the interface pointer in the frdest struct. If no match is */
/* found, then set the interface pointer to be -1 as NULL is considered to */
/* indicate there is no information at all in the structure. */
/* ------------------------------------------------------------------------ */
int v;
{
}
}
#endif /* _KERNEL */
/* ------------------------------------------------------------------------ */
/* Function: fr_resolvenic */
/* Returns: void* - NULL = wildcard name, -1 = failed to find NIC, else */
/* pointer to interface structure for NIC */
/* Parameters: name(I) - complete interface name */
/* v(I) - IP protocol version */
/* */
/* Look for a network interface structure that firstly has a matching name */
/* to that passed in and that is also being used for that IP protocol */
/* version (necessary on some platforms where there are separate listings */
/* for both IPv4 and IPv6 on the same physical NIC. */
/* */
/* One might wonder why name gets terminated with a \0 byte in here. The */
/* reason is an interface name could get into the kernel structures of ipf */
/* in any number of ways and so long as they all use the same sized array */
/* to put the name in, it makes sense to ensure it gets null terminated */
/* before it is used for its intended purpose - finding its match in the */
/* kernel's list of configured interfaces. */
/* */
/* NOTE: This SHOULD ONLY be used with IPFilter structures that have an */
/* array for the name that is LIFNAMSIZ bytes (at least) in length. */
/* ------------------------------------------------------------------------ */
char *name;
int v;
{
void *nic;
if (name[0] == '\0')
return NULL;
return NULL;
}
nic = (void *)-1;
return nic;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_expiretokens */
/* Returns: None. */
/* Parameters: ifs - ipf stack instance */
/* */
/* This function is run every ipf tick to see if there are any tokens that */
/* have been held for too long and need to be freed up. */
/* ------------------------------------------------------------------------ */
void ipf_expiretokens(ifs)
{
ipftoken_t *it;
break;
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_deltoken */
/* Returns: int - 0 = success, else error */
/* Parameters: type(I) - the token type to match */
/* uid(I) - uid owning the token */
/* ptr(I) - context pointer for the token */
/* ifs - ipf stack instance */
/* */
/* This function looks for a a token in the current list that matches up */
/* the fields (type, uid, ptr). If none is found, ESRCH is returned, else */
/* call ipf_freetoken() to remove it from the list. */
/* ------------------------------------------------------------------------ */
void *ptr;
{
ipftoken_t *it;
error = 0;
break;
}
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_unlinktoken */
/* Returns: None. */
/* Parameters: token(I) - pointer to token structure */
/* ifs - ipf stack instance */
/* */
/* This function unlinks a token structure from the linked list of tokens */
/* that it belongs to. The head pointer never needs to be explicitly */
/* adjusted, but the tail does due to the linked list implementation. */
/* ------------------------------------------------------------------------ */
{
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_findtoken */
/* Returns: ipftoken_t * - NULL if no memory, else pointer to token */
/* Parameters: type(I) - the token type to match */
/* uid(I) - uid owning the token */
/* ptr(I) - context pointer for the token */
/* ifs - ipf stack instance */
/* */
/* This function looks for a live token in the list of current tokens that */
/* matches the tuple (type, uid, ptr). If one cannot be found then one is */
/* allocated. If one is found then it is moved to the top of the list of */
/* currently active tokens. */
/* */
/* NOTE: It is by design that this function returns holding a read lock on */
/* ipf_tokens. Callers must make sure they release it! */
/* ------------------------------------------------------------------------ */
void *ptr;
{
continue;
break;
}
return NULL;
} else {
}
}
return it;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_freetoken */
/* Returns: None. */
/* Parameters: token(I) - pointer to token structure */
/* ifs - ipf stack instance */
/* */
/* This function unlinks a token from the linked list and on the path to */
/* free'ing the data, it calls the dereference function that is associated */
/* with the type of data pointed to by the token as it is considered to */
/* hold a reference to it. */
/* ------------------------------------------------------------------------ */
{
{
case IPFGENITER_IPF :
break;
case IPFGENITER_IPNAT :
break;
case IPFGENITER_NAT :
break;
case IPFGENITER_STATE :
break;
case IPFGENITER_FRAG :
break;
case IPFGENITER_NATFRAG :
break;
case IPFGENITER_HOSTMAP :
break;
default :
break;
}
}
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_getnextrule */
/* Returns: int - 0 = success, else error */
/* Parameters: t(I) - pointer to destination information to resolve */
/* ptr(I) - pointer to ipfobj_t to copyin from user space */
/* ifs - ipf stack instance */
/* */
/* This function's first job is to bring in the ipfruleiter_t structure via */
/* the ipfobj_t structure to determine what should be the next rule to */
/* return. Once the ipfruleiter_t has been brought in, it then tries to */
/* find the 'next rule'. This may include searching rule group lists or */
/* just be as simple as looking at the 'next' field in the rule structure. */
/* When we have found the rule to return, increase its reference count and */
/* if we used an existing rule to get here, decrease its reference count. */
/* ------------------------------------------------------------------------ */
ipftoken_t *t;
void *ptr;
{
char *dst;
return EFAULT;
if (error != 0)
return error;
return EINVAL;
return EINVAL;
if (it.iri_nrules == 0)
return EINVAL;
return EINVAL;
return EFAULT;
/*
* Use bitmask on it.iri_inout to determine direction.
* F_OUT (1) and F_ACOUT (3) mask to out = 1, while
* F_IN (0) and F_ACIN (2) mask to out = 0.
*/
/*
* Retrieve "previous" entry from token and find the next entry.
*/
/*
* Use bitmask again to determine accounting or not.
* F_ACIN will mask to accounting cases F_ACIN (2)
* or F_ACOUT (3), but not F_IN or F_OUT.
*/
else
} else {
else
}
} else {
else
}
} else {
}
/*
* The ipfruleiter may ask for more than 1 rule at a time to be
* copied out, so long as that many exist in the list to start with!
*/
/*
* If we found an entry, add reference to it and update token.
* Otherwise, zero out data to be returned and NULL out token.
*/
} else {
}
/*
* Now that we have ref, it's save to give up lock.
*/
/*
* Copy out data and clean up references and token as needed.
*/
if (error != 0)
ipf_freetoken(t, ifs);
break;
} else {
if (error != 0)
else
}
ipf_freetoken(t, ifs);
break;
}
}
break;
}
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_frruleiter */
/* Returns: int - 0 = success, else error */
/* Parameters: data(I) - the token type to match */
/* uid(I) - uid owning the token */
/* ptr(I) - context pointer for the token */
/* ifs - ipf stack instance */
/* */
/* This function serves as a stepping stone between fr_ipf_ioctl and */
/* ipf_getnextrule. It's role is to find the right token in the kernel for */
/* the process doing the ioctl and use that to ask for the next rule. */
/* ------------------------------------------------------------------------ */
int uid;
{
int error;
else
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_geniter */
/* Returns: int - 0 = success, else error */
/* Parameters: token(I) - pointer to ipftoken structure */
/* itp(I) - pointer to ipfgeniter structure */
/* ifs - ipf stack instance */
/* */
/* Generic iterator called from ipf_genericiter. Currently only used for */
/* walking through list of fragments. */
/* ------------------------------------------------------------------------ */
{
int error;
{
case IPFGENITER_FRAG :
ifs);
break;
default :
break;
}
return error;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_genericiter */
/* Returns: int - 0 = success, else error */
/* Parameters: data(I) - the token type to match */
/* uid(I) - uid owning the token */
/* ptr(I) - context pointer for the token */
/* ifs - ipf stack instance */
/* */
/* This function serves as a stepping stone between fr_ipf_ioctl and */
/* ipf_geniter when handling SIOCGENITER. It's role is to find the right */
/* token in the kernel for the process using the ioctl, and to use that */
/* token when calling ipf_geniter. */
/* ------------------------------------------------------------------------ */
int uid;
{
int error;
if (error != 0)
return error;
} else
return error;
}