Author: luigi
Date: Tue Dec 15 16:15:14 2009
New Revision: 200580
URL: http://svn.freebsd.org/changeset/base/200580

Log:
  Start splitting ip_fw2.c and ip_fw.h into smaller components.
  At this time we pull out from ip_fw2.c the logging functions, and
  support for dynamic rules, and move kernel-only stuff into
  netinet/ipfw/ip_fw_private.h
  
  No ABI change involved in this commit, unless I made some mistake.
  ip_fw.h has changed, though not in the userland-visible part.
  
  Files touched by this commit:
  
  conf/files
        now references the two new source files
  
  netinet/ip_fw.h
        remove kernel-only definitions gone into netinet/ipfw/ip_fw_private.h.
  
  netinet/ipfw/ip_fw_private.h
        new file with kernel-specific ipfw definitions
  
  netinet/ipfw/ip_fw_log.c
        ipfw_log and related functions
  
  netinet/ipfw/ip_fw_dynamic.c
        code related to dynamic rules
  
  netinet/ipfw/ip_fw2.c
        removed the pieces that goes in the new files
  
  netinet/ipfw/ip_fw_nat.c
        minor rearrangement to remove LOOKUP_NAT from the
        main headers. This require a new function pointer.
  
  A bunch of other kernel files that included netinet/ip_fw.h now
  require netinet/ipfw/ip_fw_private.h as well.
  Not 100% sure i caught all of them.
  
  MFC after:    1 month

Added:
  head/sys/netinet/ipfw/ip_fw_dynamic.c   (contents, props changed)
  head/sys/netinet/ipfw/ip_fw_log.c   (contents, props changed)
  head/sys/netinet/ipfw/ip_fw_private.h   (contents, props changed)
Modified:
  head/sys/conf/files
  head/sys/net/if_bridge.c
  head/sys/net/if_ethersubr.c
  head/sys/netinet/ip_divert.c
  head/sys/netinet/ip_fw.h
  head/sys/netinet/ipfw/ip_dummynet.c
  head/sys/netinet/ipfw/ip_fw2.c
  head/sys/netinet/ipfw/ip_fw_nat.c
  head/sys/netinet/ipfw/ip_fw_pfil.c

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Tue Dec 15 15:13:41 2009        (r200579)
+++ head/sys/conf/files Tue Dec 15 16:15:14 2009        (r200580)
@@ -2427,6 +2427,8 @@ netinet/ip_encap.c                optional inet | inet
 netinet/ip_fastfwd.c           optional inet
 netinet/ipfw/ip_fw2.c          optional inet ipfirewall \
        compile-with "${NORMAL_C} -I$S/contrib/pf"
+netinet/ipfw/ip_fw_dynamic.c   optional inet ipfirewall
+netinet/ipfw/ip_fw_log.c       optional inet ipfirewall
 netinet/ipfw/ip_fw_pfil.c      optional inet ipfirewall
 netinet/ipfw/ip_fw_nat.c       optional inet ipfirewall_nat
 netinet/ip_icmp.c              optional inet

Modified: head/sys/net/if_bridge.c
==============================================================================
--- head/sys/net/if_bridge.c    Tue Dec 15 15:13:41 2009        (r200579)
+++ head/sys/net/if_bridge.c    Tue Dec 15 16:15:14 2009        (r200580)
@@ -134,6 +134,7 @@ __FBSDID("$FreeBSD$");
 
 #include <net/route.h>
 #include <netinet/ip_fw.h>
+#include <netinet/ipfw/ip_fw_private.h>
 #include <netinet/ip_dummynet.h>
 
 /*

Modified: head/sys/net/if_ethersubr.c
==============================================================================
--- head/sys/net/if_ethersubr.c Tue Dec 15 15:13:41 2009        (r200579)
+++ head/sys/net/if_ethersubr.c Tue Dec 15 16:15:14 2009        (r200580)
@@ -71,6 +71,7 @@
 #include <netinet/in_var.h>
 #include <netinet/if_ether.h>
 #include <netinet/ip_fw.h>
+#include <netinet/ipfw/ip_fw_private.h>
 #include <netinet/ip_dummynet.h>
 #include <netinet/ip_var.h>
 #endif

Modified: head/sys/netinet/ip_divert.c
==============================================================================
--- head/sys/netinet/ip_divert.c        Tue Dec 15 15:13:41 2009        
(r200579)
+++ head/sys/netinet/ip_divert.c        Tue Dec 15 16:15:14 2009        
(r200580)
@@ -75,6 +75,7 @@ __FBSDID("$FreeBSD$");
 #include <netinet/ip_divert.h>
 #include <netinet/ip_var.h>
 #include <netinet/ip_fw.h>
+#include <netinet/ipfw/ip_fw_private.h>
 #ifdef SCTP
 #include <netinet/sctp_crc32.h>
 #endif

Modified: head/sys/netinet/ip_fw.h
==============================================================================
--- head/sys/netinet/ip_fw.h    Tue Dec 15 15:13:41 2009        (r200579)
+++ head/sys/netinet/ip_fw.h    Tue Dec 15 16:15:14 2009        (r200580)
@@ -571,133 +571,4 @@ typedef struct    _ipfw_table {
        ipfw_table_entry ent[0];        /* entries                      */
 } ipfw_table;
 
-/*
- * Main firewall chains definitions and global var's definitions.
- */
-#ifdef _KERNEL
-
-#define MTAG_IPFW      1148380143      /* IPFW-tagged cookie */
-
-/* Return values from ipfw_chk() */
-enum {
-       IP_FW_PASS = 0,
-       IP_FW_DENY,
-       IP_FW_DIVERT,
-       IP_FW_TEE,
-       IP_FW_DUMMYNET,
-       IP_FW_NETGRAPH,
-       IP_FW_NGTEE,
-       IP_FW_NAT,
-       IP_FW_REASS,
-};
-
-/* flags for divert mtag */
-#define        IP_FW_DIVERT_LOOPBACK_FLAG      0x00080000
-#define        IP_FW_DIVERT_OUTPUT_FLAG        0x00100000
-
-/*
- * Structure for collecting parameters to dummynet for ip6_output forwarding
- */
-struct _ip6dn_args {
-       struct ip6_pktopts *opt_or;
-       struct route_in6 ro_or;
-       int flags_or;
-       struct ip6_moptions *im6o_or;
-       struct ifnet *origifp_or;
-       struct ifnet *ifp_or;
-       struct sockaddr_in6 dst_or;
-       u_long mtu_or;
-       struct route_in6 ro_pmtu_or;
-};
-
-/*
- * Arguments for calling ipfw_chk() and dummynet_io(). We put them
- * all into a structure because this way it is easier and more
- * efficient to pass variables around and extend the interface.
- */
-struct ip_fw_args {
-       struct mbuf     *m;             /* the mbuf chain               */
-       struct ifnet    *oif;           /* output interface             */
-       struct sockaddr_in *next_hop;   /* forward address              */
-       struct ip_fw    *rule;          /* matching rule                */
-       uint32_t        rule_id;        /* matching rule id */
-       uint32_t        chain_id;       /* ruleset id */
-       struct ether_header *eh;        /* for bridged packets          */
-
-       struct ipfw_flow_id f_id;       /* grabbed from IP header       */
-       uint32_t        cookie;         /* a cookie depending on rule action */
-       struct inpcb    *inp;
-
-       struct _ip6dn_args      dummypar; /* dummynet->ip6_output */
-       struct sockaddr_in hopstore;    /* store here if cannot use a pointer */
-};
-
-/*
- * Function definitions.
- */
-
-/* Firewall hooks */
-struct sockopt;
-struct dn_flow_set;
-
-int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb 
*inp);
-int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb 
*inp);
-
-int ipfw_chk(struct ip_fw_args *);
-
-int ipfw_hook(void);
-int ipfw6_hook(void);
-int ipfw_unhook(void);
-int ipfw6_unhook(void);
-#ifdef NOTYET
-void ipfw_nat_destroy(void);
-#endif
-
-VNET_DECLARE(int, fw_one_pass);
-VNET_DECLARE(int, fw_enable);
-#define        V_fw_one_pass           VNET(fw_one_pass)
-#define        V_fw_enable             VNET(fw_enable)
-
-#ifdef INET6
-VNET_DECLARE(int, fw6_enable);
-#define        V_fw6_enable            VNET(fw6_enable)
-#endif
-
-struct ip_fw_chain {
-       struct ip_fw    *rules;         /* list of rules */
-       struct ip_fw    *reap;          /* list of rules to reap */
-       LIST_HEAD(, cfg_nat) nat;       /* list of nat entries */
-       struct radix_node_head *tables[IPFW_TABLES_MAX];
-       struct rwlock   rwmtx;
-       uint32_t        id;             /* ruleset id */
-};
-
-#ifdef IPFW_INTERNAL
-
-#define        IPFW_LOCK_INIT(_chain) \
-       rw_init(&(_chain)->rwmtx, "IPFW static rules")
-#define        IPFW_LOCK_DESTROY(_chain)       rw_destroy(&(_chain)->rwmtx)
-#define        IPFW_WLOCK_ASSERT(_chain)       rw_assert(&(_chain)->rwmtx, 
RA_WLOCKED)
-
-#define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
-#define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
-#define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
-#define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
-
-#define LOOKUP_NAT(l, i, p) do {                                       \
-               LIST_FOREACH((p), &(l.nat), _next) {                    \
-                       if ((p)->id == (i)) {                           \
-                               break;                                  \
-                       }                                               \
-               }                                                       \
-       } while (0)
-
-typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
-typedef int ipfw_nat_cfg_t(struct sockopt *);
-#endif
-
-VNET_DECLARE(struct ip_fw_chain, layer3_chain);
-#define        V_layer3_chain          VNET(layer3_chain)
-
-#endif /* _KERNEL */
 #endif /* _IPFW2_H */

Modified: head/sys/netinet/ipfw/ip_dummynet.c
==============================================================================
--- head/sys/netinet/ipfw/ip_dummynet.c Tue Dec 15 15:13:41 2009        
(r200579)
+++ head/sys/netinet/ipfw/ip_dummynet.c Tue Dec 15 16:15:14 2009        
(r200580)
@@ -76,6 +76,7 @@ __FBSDID("$FreeBSD$");
 #include <netinet/in.h>
 #include <netinet/ip.h>                /* ip_len, ip_off */
 #include <netinet/ip_fw.h>
+#include <netinet/ipfw/ip_fw_private.h>
 #include <netinet/ip_dummynet.h>
 #include <netinet/ip_var.h>    /* ip_output(), IP_FORWARDING */
 

Modified: head/sys/netinet/ipfw/ip_fw2.c
==============================================================================
--- head/sys/netinet/ipfw/ip_fw2.c      Tue Dec 15 15:13:41 2009        
(r200579)
+++ head/sys/netinet/ipfw/ip_fw2.c      Tue Dec 15 16:15:14 2009        
(r200580)
@@ -70,8 +70,6 @@ __FBSDID("$FreeBSD$");
 #include <net/pf_mtag.h>
 #include <net/vnet.h>
 
-#define        IPFW_INTERNAL   /* Access to protected data structures in 
ip_fw.h. */
-
 #include <netinet/in.h>
 #include <netinet/in_var.h>
 #include <netinet/in_pcb.h>
@@ -79,6 +77,7 @@ __FBSDID("$FreeBSD$");
 #include <netinet/ip_var.h>
 #include <netinet/ip_icmp.h>
 #include <netinet/ip_fw.h>
+#include <netinet/ipfw/ip_fw_private.h>
 #include <netinet/ip_divert.h>
 #include <netinet/ip_dummynet.h>
 #include <netinet/ip_carp.h>
@@ -114,13 +113,9 @@ static VNET_DEFINE(int, ipfw_vnet_ready)
  * Rules in set RESVD_SET can only be deleted explicitly.
  */
 static VNET_DEFINE(u_int32_t, set_disable);
-static VNET_DEFINE(int, fw_verbose);
-static VNET_DEFINE(struct callout, ipfw_timeout);
-static VNET_DEFINE(int, verbose_limit);
+VNET_DEFINE(int, fw_verbose);
 
 #define        V_set_disable                   VNET(set_disable)
-#define        V_fw_verbose                    VNET(fw_verbose)
-#define        V_ipfw_timeout                  VNET(ipfw_timeout)
 #define        V_verbose_limit                 VNET(verbose_limit)
 
 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
@@ -128,7 +123,6 @@ static int default_to_accept = 1;
 #else
 static int default_to_accept;
 #endif
-static uma_zone_t ipfw_dyn_rule_zone;
 
 struct ip_fw *ip_fw_default_rule;
 
@@ -141,6 +135,7 @@ MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "Ip
 MALLOC_DEFINE(M_IPFW_TBL, "ipfw_tbl", "IpFw tables");
 #define IPFW_NAT_LOADED (ipfw_nat_ptr != NULL)
 ipfw_nat_t *ipfw_nat_ptr = NULL;
+struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
@@ -157,6 +152,10 @@ static VNET_DEFINE(int, autoinc_step);
 static VNET_DEFINE(int, fw_deny_unknown_exthdrs);
 #define        V_fw_deny_unknown_exthdrs       VNET(fw_deny_unknown_exthdrs)
 
+static VNET_DEFINE(u_int32_t, static_count);   /* # of static rules */
+static VNET_DEFINE(u_int32_t, static_len);     /* bytes of static rules */
+#define        V_static_count                  VNET(static_count)
+#define        V_static_len                    VNET(static_len)
 extern int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
 
 #ifdef SYSCTL_NODE
@@ -198,146 +197,12 @@ SYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_A
     "Deny packets with unknown IPv6 Extension Headers");
 #endif /* INET6 */
 
-#endif /* SYSCTL_NODE */
-
-/*
- * Description of dynamic rules.
- *
- * Dynamic rules are stored in lists accessed through a hash table
- * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
- * be modified through the sysctl variable dyn_buckets which is
- * updated when the table becomes empty.
- *
- * XXX currently there is only one list, ipfw_dyn.
- *
- * When a packet is received, its address fields are first masked
- * with the mask defined for the rule, then hashed, then matched
- * against the entries in the corresponding list.
- * Dynamic rules can be used for different purposes:
- *  + stateful rules;
- *  + enforcing limits on the number of sessions;
- *  + in-kernel NAT (not implemented yet)
- *
- * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
- * measured in seconds and depending on the flags.
- *
- * The total number of dynamic rules is stored in dyn_count.
- * The max number of dynamic rules is dyn_max. When we reach
- * the maximum number of rules we do not create anymore. This is
- * done to avoid consuming too much memory, but also too much
- * time when searching on each packet (ideally, we should try instead
- * to put a limit on the length of the list on each bucket...).
- *
- * Each dynamic rule holds a pointer to the parent ipfw rule so
- * we know what action to perform. Dynamic rules are removed when
- * the parent rule is deleted. XXX we should make them survive.
- *
- * There are some limitations with dynamic rules -- we do not
- * obey the 'randomized match', and we do not do multiple
- * passes through the firewall. XXX check the latter!!!
- */
-static VNET_DEFINE(ipfw_dyn_rule **, ipfw_dyn_v);
-static VNET_DEFINE(u_int32_t, dyn_buckets);
-static VNET_DEFINE(u_int32_t, curr_dyn_buckets);
-
-#define        V_ipfw_dyn_v                    VNET(ipfw_dyn_v)
-#define        V_dyn_buckets                   VNET(dyn_buckets)
-#define        V_curr_dyn_buckets              VNET(curr_dyn_buckets)
-
-static struct mtx ipfw_dyn_mtx;                /* mutex guarding dynamic rules 
*/
-#define        IPFW_DYN_LOCK_INIT() \
-       mtx_init(&ipfw_dyn_mtx, "IPFW dynamic rules", NULL, MTX_DEF)
-#define        IPFW_DYN_LOCK_DESTROY() mtx_destroy(&ipfw_dyn_mtx)
-#define        IPFW_DYN_LOCK()         mtx_lock(&ipfw_dyn_mtx)
-#define        IPFW_DYN_UNLOCK()       mtx_unlock(&ipfw_dyn_mtx)
-#define        IPFW_DYN_LOCK_ASSERT()  mtx_assert(&ipfw_dyn_mtx, MA_OWNED)
-
-static struct mbuf *send_pkt(struct mbuf *, struct ipfw_flow_id *,
-    u_int32_t, u_int32_t, int);
-
-
-/*
- * Timeouts for various events in handing dynamic rules.
- */
-static VNET_DEFINE(u_int32_t, dyn_ack_lifetime);
-static VNET_DEFINE(u_int32_t, dyn_syn_lifetime);
-static VNET_DEFINE(u_int32_t, dyn_fin_lifetime);
-static VNET_DEFINE(u_int32_t, dyn_rst_lifetime);
-static VNET_DEFINE(u_int32_t, dyn_udp_lifetime);
-static VNET_DEFINE(u_int32_t, dyn_short_lifetime);
-
-#define        V_dyn_ack_lifetime              VNET(dyn_ack_lifetime)
-#define        V_dyn_syn_lifetime              VNET(dyn_syn_lifetime)
-#define        V_dyn_fin_lifetime              VNET(dyn_fin_lifetime)
-#define        V_dyn_rst_lifetime              VNET(dyn_rst_lifetime)
-#define        V_dyn_udp_lifetime              VNET(dyn_udp_lifetime)
-#define        V_dyn_short_lifetime            VNET(dyn_short_lifetime)
-
-/*
- * Keepalives are sent if dyn_keepalive is set. They are sent every
- * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
- * seconds of lifetime of a rule.
- * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
- * than dyn_keepalive_period.
- */
-
-static VNET_DEFINE(u_int32_t, dyn_keepalive_interval);
-static VNET_DEFINE(u_int32_t, dyn_keepalive_period);
-static VNET_DEFINE(u_int32_t, dyn_keepalive);
-
-#define        V_dyn_keepalive_interval        VNET(dyn_keepalive_interval)
-#define        V_dyn_keepalive_period          VNET(dyn_keepalive_period)
-#define        V_dyn_keepalive                 VNET(dyn_keepalive)
-
-static VNET_DEFINE(u_int32_t, static_count);   /* # of static rules */
-static VNET_DEFINE(u_int32_t, static_len);     /* bytes of static rules */
-static VNET_DEFINE(u_int32_t, dyn_count);      /* # of dynamic rules */
-static VNET_DEFINE(u_int32_t, dyn_max);                /* max # of dynamic 
rules */
-
-#define        V_static_count                  VNET(static_count)
-#define        V_static_len                    VNET(static_len)
-#define        V_dyn_count                     VNET(dyn_count)
-#define        V_dyn_max                       VNET(dyn_max)
-
-#ifdef SYSCTL_NODE
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
-    CTLFLAG_RW, &VNET_NAME(dyn_buckets), 0,
-    "Number of dyn. buckets");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
-    CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
-    "Current Number of dyn. buckets");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_count,
-    CTLFLAG_RD, &VNET_NAME(dyn_count), 0,
-    "Number of dyn. rules");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_max,
-    CTLFLAG_RW, &VNET_NAME(dyn_max), 0,
-    "Max number of dyn. rules");
 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count,
     CTLFLAG_RD, &VNET_NAME(static_count), 0,
     "Number of static rules");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0,
-    "Lifetime of dyn. rules for acks");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0,
-    "Lifetime of dyn. rules for syn");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0,
-    "Lifetime of dyn. rules for fin");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0,
-    "Lifetime of dyn. rules for rst");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0,
-    "Lifetime of dyn. rules for UDP");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime,
-    CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0,
-    "Lifetime of dyn. rules for other situations");
-SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive,
-    CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0,
-    "Enable keepalives for dyn. rules");
 #endif /* SYSCTL_NODE */
 
+
 /*
  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
  * Other macros just cast void * into the appropriate type
@@ -681,18 +546,6 @@ verify_path6(struct in6_addr *src, struc
        return 1;
 
 }
-static __inline int
-hash_packet6(struct ipfw_flow_id *id)
-{
-       u_int32_t i;
-       i = (id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
-           (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
-           (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
-           (id->src_ip6.__u6_addr.__u6_addr32[3]) ^
-           (id->dst_port) ^ (id->src_port);
-       return i;
-}
-
 static int
 is_icmp6_query(int icmp6_type)
 {
@@ -749,1072 +602,6 @@ send_reject6(struct ip_fw_args *args, in
 
 #endif /* INET6 */
 
-/* counter for ipfw_log(NULL...) */
-static VNET_DEFINE(u_int64_t, norule_counter);
-#define        V_norule_counter                VNET(norule_counter)
-
-#define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
-#define SNP(buf) buf, sizeof(buf)
-
-/*
- * We enter here when we have a rule with O_LOG.
- * XXX this function alone takes about 2Kbytes of code!
- */
-static void
-ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
-    struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
-    struct ip *ip)
-{
-       struct ether_header *eh = args->eh;
-       char *action;
-       int limit_reached = 0;
-       char action2[40], proto[128], fragment[32];
-
-       fragment[0] = '\0';
-       proto[0] = '\0';
-
-       if (f == NULL) {        /* bogus pkt */
-               if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
-                       return;
-               V_norule_counter++;
-               if (V_norule_counter == V_verbose_limit)
-                       limit_reached = V_verbose_limit;
-               action = "Refuse";
-       } else {        /* O_LOG is the first action, find the real one */
-               ipfw_insn *cmd = ACTION_PTR(f);
-               ipfw_insn_log *l = (ipfw_insn_log *)cmd;
-
-               if (l->max_log != 0 && l->log_left == 0)
-                       return;
-               l->log_left--;
-               if (l->log_left == 0)
-                       limit_reached = l->max_log;
-               cmd += F_LEN(cmd);      /* point to first action */
-               if (cmd->opcode == O_ALTQ) {
-                       ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
-
-                       snprintf(SNPARGS(action2, 0), "Altq %d",
-                               altq->qid);
-                       cmd += F_LEN(cmd);
-               }
-               if (cmd->opcode == O_PROB)
-                       cmd += F_LEN(cmd);
-
-               if (cmd->opcode == O_TAG)
-                       cmd += F_LEN(cmd);
-
-               action = action2;
-               switch (cmd->opcode) {
-               case O_DENY:
-                       action = "Deny";
-                       break;
-
-               case O_REJECT:
-                       if (cmd->arg1==ICMP_REJECT_RST)
-                               action = "Reset";
-                       else if (cmd->arg1==ICMP_UNREACH_HOST)
-                               action = "Reject";
-                       else
-                               snprintf(SNPARGS(action2, 0), "Unreach %d",
-                                       cmd->arg1);
-                       break;
-
-               case O_UNREACH6:
-                       if (cmd->arg1==ICMP6_UNREACH_RST)
-                               action = "Reset";
-                       else
-                               snprintf(SNPARGS(action2, 0), "Unreach %d",
-                                       cmd->arg1);
-                       break;
-
-               case O_ACCEPT:
-                       action = "Accept";
-                       break;
-               case O_COUNT:
-                       action = "Count";
-                       break;
-               case O_DIVERT:
-                       snprintf(SNPARGS(action2, 0), "Divert %d",
-                               cmd->arg1);
-                       break;
-               case O_TEE:
-                       snprintf(SNPARGS(action2, 0), "Tee %d",
-                               cmd->arg1);
-                       break;
-               case O_SETFIB:
-                       snprintf(SNPARGS(action2, 0), "SetFib %d",
-                               cmd->arg1);
-                       break;
-               case O_SKIPTO:
-                       snprintf(SNPARGS(action2, 0), "SkipTo %d",
-                               cmd->arg1);
-                       break;
-               case O_PIPE:
-                       snprintf(SNPARGS(action2, 0), "Pipe %d",
-                               cmd->arg1);
-                       break;
-               case O_QUEUE:
-                       snprintf(SNPARGS(action2, 0), "Queue %d",
-                               cmd->arg1);
-                       break;
-               case O_FORWARD_IP: {
-                       ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
-                       int len;
-                       struct in_addr dummyaddr;
-                       if (sa->sa.sin_addr.s_addr == INADDR_ANY)
-                               dummyaddr.s_addr = htonl(tablearg);
-                       else
-                               dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
-
-                       len = snprintf(SNPARGS(action2, 0), "Forward to %s",
-                               inet_ntoa(dummyaddr));
-
-                       if (sa->sa.sin_port)
-                               snprintf(SNPARGS(action2, len), ":%d",
-                                   sa->sa.sin_port);
-                       }
-                       break;
-               case O_NETGRAPH:
-                       snprintf(SNPARGS(action2, 0), "Netgraph %d",
-                               cmd->arg1);
-                       break;
-               case O_NGTEE:
-                       snprintf(SNPARGS(action2, 0), "Ngtee %d",
-                               cmd->arg1);
-                       break;
-               case O_NAT:
-                       action = "Nat";
-                       break;
-               case O_REASS:
-                       action = "Reass";
-                       break;
-               default:
-                       action = "UNKNOWN";
-                       break;
-               }
-       }
-
-       if (hlen == 0) {        /* non-ip */
-               snprintf(SNPARGS(proto, 0), "MAC");
-
-       } else {
-               int len;
-#ifdef INET6
-               char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
-#else
-               char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
-#endif
-               struct icmphdr *icmp;
-               struct tcphdr *tcp;
-               struct udphdr *udp;
-#ifdef INET6
-               struct ip6_hdr *ip6 = NULL;
-               struct icmp6_hdr *icmp6;
-#endif
-               src[0] = '\0';
-               dst[0] = '\0';
-#ifdef INET6
-               if (IS_IP6_FLOW_ID(&(args->f_id))) {
-                       char ip6buf[INET6_ADDRSTRLEN];
-                       snprintf(src, sizeof(src), "[%s]",
-                           ip6_sprintf(ip6buf, &args->f_id.src_ip6));
-                       snprintf(dst, sizeof(dst), "[%s]",
-                           ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
-
-                       ip6 = (struct ip6_hdr *)ip;
-                       tcp = (struct tcphdr *)(((char *)ip) + hlen);
-                       udp = (struct udphdr *)(((char *)ip) + hlen);
-               } else
-#endif
-               {
-                       tcp = L3HDR(struct tcphdr, ip);
-                       udp = L3HDR(struct udphdr, ip);
-
-                       inet_ntoa_r(ip->ip_src, src);
-                       inet_ntoa_r(ip->ip_dst, dst);
-               }
-
-               switch (args->f_id.proto) {
-               case IPPROTO_TCP:
-                       len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
-                       if (offset == 0)
-                               snprintf(SNPARGS(proto, len), ":%d %s:%d",
-                                   ntohs(tcp->th_sport),
-                                   dst,
-                                   ntohs(tcp->th_dport));
-                       else
-                               snprintf(SNPARGS(proto, len), " %s", dst);
-                       break;
-
-               case IPPROTO_UDP:
-                       len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
-                       if (offset == 0)
-                               snprintf(SNPARGS(proto, len), ":%d %s:%d",
-                                   ntohs(udp->uh_sport),
-                                   dst,
-                                   ntohs(udp->uh_dport));
-                       else
-                               snprintf(SNPARGS(proto, len), " %s", dst);
-                       break;
-
-               case IPPROTO_ICMP:
-                       icmp = L3HDR(struct icmphdr, ip);
-                       if (offset == 0)
-                               len = snprintf(SNPARGS(proto, 0),
-                                   "ICMP:%u.%u ",
-                                   icmp->icmp_type, icmp->icmp_code);
-                       else
-                               len = snprintf(SNPARGS(proto, 0), "ICMP ");
-                       len += snprintf(SNPARGS(proto, len), "%s", src);
-                       snprintf(SNPARGS(proto, len), " %s", dst);
-                       break;
-#ifdef INET6
-               case IPPROTO_ICMPV6:
-                       icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
-                       if (offset == 0)
-                               len = snprintf(SNPARGS(proto, 0),
-                                   "ICMPv6:%u.%u ",
-                                   icmp6->icmp6_type, icmp6->icmp6_code);
-                       else
-                               len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
-                       len += snprintf(SNPARGS(proto, len), "%s", src);
-                       snprintf(SNPARGS(proto, len), " %s", dst);
-                       break;
-#endif
-               default:
-                       len = snprintf(SNPARGS(proto, 0), "P:%d %s",
-                           args->f_id.proto, src);
-                       snprintf(SNPARGS(proto, len), " %s", dst);
-                       break;
-               }
-
-#ifdef INET6
-               if (IS_IP6_FLOW_ID(&(args->f_id))) {
-                       if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
-                               snprintf(SNPARGS(fragment, 0),
-                                   " (frag %08x:%...@%d%s)",
-                                   args->f_id.frag_id6,
-                                   ntohs(ip6->ip6_plen) - hlen,
-                                   ntohs(offset & IP6F_OFF_MASK) << 3,
-                                   (offset & IP6F_MORE_FRAG) ? "+" : "");
-               } else
-#endif
-               {
-                       int ip_off, ip_len;
-                       if (eh != NULL) { /* layer 2 packets are as on the wire 
*/
-                               ip_off = ntohs(ip->ip_off);
-                               ip_len = ntohs(ip->ip_len);
-                       } else {
-                               ip_off = ip->ip_off;
-                               ip_len = ip->ip_len;
-                       }
-                       if (ip_off & (IP_MF | IP_OFFMASK))
-                               snprintf(SNPARGS(fragment, 0),
-                                   " (frag %d:%...@%d%s)",
-                                   ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2),
-                                   offset << 3,
-                                   (ip_off & IP_MF) ? "+" : "");
-               }
-       }
-       if (oif || m->m_pkthdr.rcvif)
-               log(LOG_SECURITY | LOG_INFO,
-                   "ipfw: %d %s %s %s via %s%s\n",
-                   f ? f->rulenum : -1,
-                   action, proto, oif ? "out" : "in",
-                   oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
-                   fragment);
-       else
-               log(LOG_SECURITY | LOG_INFO,
-                   "ipfw: %d %s %s [no if info]%s\n",
-                   f ? f->rulenum : -1,
-                   action, proto, fragment);
-       if (limit_reached)
-               log(LOG_SECURITY | LOG_NOTICE,
-                   "ipfw: limit %d reached on entry %d\n",
-                   limit_reached, f ? f->rulenum : -1);
-}
-
-/*
- * IMPORTANT: the hash function for dynamic rules must be commutative
- * in source and destination (ip,port), because rules are bidirectional
- * and we want to find both in the same bucket.
- */
-static __inline int
-hash_packet(struct ipfw_flow_id *id)
-{
-       u_int32_t i;
-
-#ifdef INET6
-       if (IS_IP6_FLOW_ID(id)) 
-               i = hash_packet6(id);
-       else
-#endif /* INET6 */
-       i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
-       i &= (V_curr_dyn_buckets - 1);
-       return i;
-}
-
-static __inline void
-unlink_dyn_rule_print(struct ipfw_flow_id *id)
-{
-       struct in_addr da;
-#ifdef INET6
-       char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
-#else
-       char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
-#endif
-
-#ifdef INET6
-       if (IS_IP6_FLOW_ID(id)) {
-               ip6_sprintf(src, &id->src_ip6);
-               ip6_sprintf(dst, &id->dst_ip6);
-       } else
-#endif
-       {
-               da.s_addr = htonl(id->src_ip);
-               inet_ntoa_r(da, src);
-               da.s_addr = htonl(id->dst_ip);
-               inet_ntoa_r(da, dst);
-       }
-       printf("ipfw: unlink entry %s %d -> %s %d, %d left\n",
-           src, id->src_port, dst, id->dst_port, V_dyn_count - 1);
-}
-
-/**
- * unlink a dynamic rule from a chain. prev is a pointer to
- * the previous one, q is a pointer to the rule to delete,
- * head is a pointer to the head of the queue.
- * Modifies q and potentially also head.
- */
-#define UNLINK_DYN_RULE(prev, head, q) {                               \
-       ipfw_dyn_rule *old_q = q;                                       \
-                                                                       \
-       /* remove a refcount to the parent */                           \
-       if (q->dyn_type == O_LIMIT)                                     \
-               q->parent->count--;                                     \
-       DEB(unlink_dyn_rule_print(&q->id);)                             \
-       if (prev != NULL)                                               \
-               prev->next = q = q->next;                               \
-       else                                                            \
-               head = q = q->next;                                     \
-       V_dyn_count--;                                                  \
-       uma_zfree(ipfw_dyn_rule_zone, old_q); }
-
-#define TIME_LEQ(a,b)       ((int)((a)-(b)) <= 0)
-
-/**
- * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
- *
- * If keep_me == NULL, rules are deleted even if not expired,
- * otherwise only expired rules are removed.
- *
- * The value of the second parameter is also used to point to identify
- * a rule we absolutely do not want to remove (e.g. because we are
- * holding a reference to it -- this is the case with O_LIMIT_PARENT
- * rules). The pointer is only used for comparison, so any non-null
- * value will do.
- */
-static void
-remove_dyn_rule(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
-{
-       static u_int32_t last_remove = 0;
-
-#define FORCE (keep_me == NULL)
-
-       ipfw_dyn_rule *prev, *q;
-       int i, pass = 0, max_pass = 0;
-
-       IPFW_DYN_LOCK_ASSERT();
-
-       if (V_ipfw_dyn_v == NULL || V_dyn_count == 0)
-               return;
-       /* do not expire more than once per second, it is useless */
-       if (!FORCE && last_remove == time_uptime)
-               return;
-       last_remove = time_uptime;
-
-       /*
-        * because O_LIMIT refer to parent rules, during the first pass only
-        * remove child and mark any pending LIMIT_PARENT, and remove
-        * them in a second pass.
-        */
-next_pass:
-       for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
-               for (prev=NULL, q = V_ipfw_dyn_v[i] ; q ; ) {
-                       /*
-                        * Logic can become complex here, so we split tests.
-                        */
-                       if (q == keep_me)
-                               goto next;
-                       if (rule != NULL && rule != q->rule)
-                               goto next; /* not the one we are looking for */
-                       if (q->dyn_type == O_LIMIT_PARENT) {
-                               /*
-                                * handle parent in the second pass,
-                                * record we need one.
-                                */
-                               max_pass = 1;
-                               if (pass == 0)
-                                       goto next;
-                               if (FORCE && q->count != 0 ) {
-                                       /* XXX should not happen! */
-                                       printf("ipfw: OUCH! cannot remove rule,"
-                                            " count %d\n", q->count);
-                               }
-                       } else {
-                               if (!FORCE &&
-                                   !TIME_LEQ( q->expire, time_uptime ))
-                                       goto next;
-                       }
-             if (q->dyn_type != O_LIMIT_PARENT || !q->count) {
-                     UNLINK_DYN_RULE(prev, V_ipfw_dyn_v[i], q);
-                     continue;
-             }
-next:
-                       prev=q;
-                       q=q->next;
-               }
-       }
-       if (pass++ < max_pass)
-               goto next_pass;
-}
-
-
-/**
- * lookup a dynamic rule.
- */
-static ipfw_dyn_rule *
-lookup_dyn_rule_locked(struct ipfw_flow_id *pkt, int *match_direction,
-    struct tcphdr *tcp)
-{
-       /*
-        * stateful ipfw extensions.
-        * Lookup into dynamic session queue
-        */
-#define MATCH_REVERSE  0
-#define MATCH_FORWARD  1
-#define MATCH_NONE     2
-#define MATCH_UNKNOWN  3
-       int i, dir = MATCH_NONE;
-       ipfw_dyn_rule *prev, *q=NULL;
-
-       IPFW_DYN_LOCK_ASSERT();
-
-       if (V_ipfw_dyn_v == NULL)
-               goto done;      /* not found */
-       i = hash_packet( pkt );
-       for (prev=NULL, q = V_ipfw_dyn_v[i] ; q != NULL ; ) {
-               if (q->dyn_type == O_LIMIT_PARENT && q->count)
-                       goto next;
-               if (TIME_LEQ( q->expire, time_uptime)) { /* expire entry */
-                       UNLINK_DYN_RULE(prev, V_ipfw_dyn_v[i], q);
-                       continue;
-               }
-               if (pkt->proto == q->id.proto &&
-                   q->dyn_type != O_LIMIT_PARENT) {
-                       if (IS_IP6_FLOW_ID(pkt)) {
-                           if (IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6),
-                               &(q->id.src_ip6)) &&
-                           IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6),
-                               &(q->id.dst_ip6)) &&
-                           pkt->src_port == q->id.src_port &&
-                           pkt->dst_port == q->id.dst_port ) {
-                               dir = MATCH_FORWARD;
-                               break;
-                           }
-                           if (IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6),
-                                   &(q->id.dst_ip6)) &&
-                               IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6),
-                                   &(q->id.src_ip6)) &&
-                               pkt->src_port == q->id.dst_port &&
-                               pkt->dst_port == q->id.src_port ) {
-                                   dir = MATCH_REVERSE;
-                                   break;
-                           }
-                       } else {
-                           if (pkt->src_ip == q->id.src_ip &&
-                               pkt->dst_ip == q->id.dst_ip &&
-                               pkt->src_port == q->id.src_port &&
-                               pkt->dst_port == q->id.dst_port ) {
-                                   dir = MATCH_FORWARD;
-                                   break;
-                           }
-                           if (pkt->src_ip == q->id.dst_ip &&
-                               pkt->dst_ip == q->id.src_ip &&
-                               pkt->src_port == q->id.dst_port &&
-                               pkt->dst_port == q->id.src_port ) {
-                                   dir = MATCH_REVERSE;
-                                   break;
-                           }
-                       }
-               }
-next:
-               prev = q;
-               q = q->next;
-       }
-       if (q == NULL)
-               goto done; /* q = NULL, not found */
-
-       if ( prev != NULL) { /* found and not in front */
-               prev->next = q->next;
-               q->next = V_ipfw_dyn_v[i];
-               V_ipfw_dyn_v[i] = q;
-       }
-       if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
-               u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST);
-
-#define BOTH_SYN       (TH_SYN | (TH_SYN << 8))
-#define BOTH_FIN       (TH_FIN | (TH_FIN << 8))
-               q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8);
-               switch (q->state) {
-               case TH_SYN:                            /* opening */
-                       q->expire = time_uptime + V_dyn_syn_lifetime;
-                       break;
-
-               case BOTH_SYN:                  /* move to established */
-               case BOTH_SYN | TH_FIN :        /* one side tries to close */
-               case BOTH_SYN | (TH_FIN << 8) :
-                       if (tcp) {
-#define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
-                           u_int32_t ack = ntohl(tcp->th_ack);
-                           if (dir == MATCH_FORWARD) {
-                               if (q->ack_fwd == 0 || _SEQ_GE(ack, q->ack_fwd))
-                                   q->ack_fwd = ack;
-                               else { /* ignore out-of-sequence */
-                                   break;
-                               }
-                           } else {
-                               if (q->ack_rev == 0 || _SEQ_GE(ack, q->ack_rev))
-                                   q->ack_rev = ack;
-                               else { /* ignore out-of-sequence */
-                                   break;
-                               }
-                           }
-                       }
-                       q->expire = time_uptime + V_dyn_ack_lifetime;
-                       break;
-
-               case BOTH_SYN | BOTH_FIN:       /* both sides closed */
-                       if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
-                               V_dyn_fin_lifetime = V_dyn_keepalive_period - 1;
-                       q->expire = time_uptime + V_dyn_fin_lifetime;
-                       break;
-
-               default:
-#if 0
-                       /*
-                        * reset or some invalid combination, but can also
-                        * occur if we use keep-state the wrong way.
-                        */
-                       if ( (q->state & ((TH_RST << 8)|TH_RST)) == 0)
-                               printf("invalid state: 0x%x\n", q->state);
-#endif
-                       if (V_dyn_rst_lifetime >= V_dyn_keepalive_period)
-                               V_dyn_rst_lifetime = V_dyn_keepalive_period - 1;
-                       q->expire = time_uptime + V_dyn_rst_lifetime;
-                       break;
-               }
-       } else if (pkt->proto == IPPROTO_UDP) {
-               q->expire = time_uptime + V_dyn_udp_lifetime;
-       } else {
-               /* other protocols */
-               q->expire = time_uptime + V_dyn_short_lifetime;
-       }
-done:
-       if (match_direction)
-               *match_direction = dir;
-       return q;
-}
-
-static ipfw_dyn_rule *
-lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
-    struct tcphdr *tcp)
-{

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to