2008-01-15 07:40:53 +00:00
|
|
|
/*
|
|
|
|
* xt_conntrack - Netfilter module to match connection tracking
|
|
|
|
* information. (Superset of Rusty's minimalistic state match.)
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-15 07:40:53 +00:00
|
|
|
* (C) 2001 Marc Boucher (marc@mbsi.ca).
|
|
|
|
* Copyright © CC Computer Consultants GmbH, 2007 - 2008
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-15 07:40:53 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
2008-01-15 07:40:53 +00:00
|
|
|
#include <net/ipv6.h>
|
[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables
This monster-patch tries to do the best job for unifying the data
structures and backend interfaces for the three evil clones ip_tables,
ip6_tables and arp_tables. In an ideal world we would never have
allowed this kind of copy+paste programming... but well, our world
isn't (yet?) ideal.
o introduce a new x_tables module
o {ip,arp,ip6}_tables depend on this x_tables module
o registration functions for tables, matches and targets are only
wrappers around x_tables provided functions
o all matches/targets that are used from ip_tables and ip6_tables
are now implemented as xt_FOOBAR.c files and provide module aliases
to ipt_FOOBAR and ip6t_FOOBAR
o header files for xt_matches are in include/linux/netfilter/,
include/linux/netfilter_{ipv4,ipv6} contains compatibility wrappers
around the xt_FOOBAR.h headers
Based on this patchset we're going to further unify the code,
gradually getting rid of all the layer 3 specific assumptions.
Signed-off-by: Harald Welte <laforge@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-01-12 21:30:04 +00:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_conntrack.h>
|
2007-03-14 23:37:25 +00:00
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
|
2009-06-12 16:56:14 +00:00
|
|
|
MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: connection tracking state match");
|
[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables
This monster-patch tries to do the best job for unifying the data
structures and backend interfaces for the three evil clones ip_tables,
ip6_tables and arp_tables. In an ideal world we would never have
allowed this kind of copy+paste programming... but well, our world
isn't (yet?) ideal.
o introduce a new x_tables module
o {ip,arp,ip6}_tables depend on this x_tables module
o registration functions for tables, matches and targets are only
wrappers around x_tables provided functions
o all matches/targets that are used from ip_tables and ip6_tables
are now implemented as xt_FOOBAR.c files and provide module aliases
to ipt_FOOBAR and ip6t_FOOBAR
o header files for xt_matches are in include/linux/netfilter/,
include/linux/netfilter_{ipv4,ipv6} contains compatibility wrappers
around the xt_FOOBAR.h headers
Based on this patchset we're going to further unify the code,
gradually getting rid of all the layer 3 specific assumptions.
Signed-off-by: Harald Welte <laforge@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-01-12 21:30:04 +00:00
|
|
|
MODULE_ALIAS("ipt_conntrack");
|
2008-01-15 07:40:53 +00:00
|
|
|
MODULE_ALIAS("ip6t_conntrack");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-15 07:40:53 +00:00
|
|
|
static bool
|
|
|
|
conntrack_addrcmp(const union nf_inet_addr *kaddr,
|
|
|
|
const union nf_inet_addr *uaddr,
|
|
|
|
const union nf_inet_addr *umask, unsigned int l3proto)
|
|
|
|
{
|
2008-10-08 09:35:01 +00:00
|
|
|
if (l3proto == NFPROTO_IPV4)
|
2008-02-27 20:20:41 +00:00
|
|
|
return ((kaddr->ip ^ uaddr->ip) & umask->ip) == 0;
|
2008-10-08 09:35:01 +00:00
|
|
|
else if (l3proto == NFPROTO_IPV6)
|
2008-01-15 07:40:53 +00:00
|
|
|
return ipv6_masked_addr_cmp(&kaddr->in6, &umask->in6,
|
|
|
|
&uaddr->in6) == 0;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
conntrack_mt_origsrc(const struct nf_conn *ct,
|
2009-06-29 12:31:46 +00:00
|
|
|
const struct xt_conntrack_mtinfo2 *info,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t family)
|
2008-01-15 07:40:53 +00:00
|
|
|
{
|
|
|
|
return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3,
|
|
|
|
&info->origsrc_addr, &info->origsrc_mask, family);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
conntrack_mt_origdst(const struct nf_conn *ct,
|
2009-06-29 12:31:46 +00:00
|
|
|
const struct xt_conntrack_mtinfo2 *info,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t family)
|
2008-01-15 07:40:53 +00:00
|
|
|
{
|
|
|
|
return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3,
|
|
|
|
&info->origdst_addr, &info->origdst_mask, family);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
conntrack_mt_replsrc(const struct nf_conn *ct,
|
2009-06-29 12:31:46 +00:00
|
|
|
const struct xt_conntrack_mtinfo2 *info,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t family)
|
2008-01-15 07:40:53 +00:00
|
|
|
{
|
|
|
|
return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3,
|
|
|
|
&info->replsrc_addr, &info->replsrc_mask, family);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
conntrack_mt_repldst(const struct nf_conn *ct,
|
2009-06-29 12:31:46 +00:00
|
|
|
const struct xt_conntrack_mtinfo2 *info,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t family)
|
2008-01-15 07:40:53 +00:00
|
|
|
{
|
|
|
|
return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3,
|
|
|
|
&info->repldst_addr, &info->repldst_mask, family);
|
|
|
|
}
|
|
|
|
|
2008-01-31 11:58:24 +00:00
|
|
|
static inline bool
|
2009-06-29 12:31:46 +00:00
|
|
|
ct_proto_port_check(const struct xt_conntrack_mtinfo2 *info,
|
2008-01-31 11:58:24 +00:00
|
|
|
const struct nf_conn *ct)
|
|
|
|
{
|
|
|
|
const struct nf_conntrack_tuple *tuple;
|
|
|
|
|
|
|
|
tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
|
|
|
|
if ((info->match_flags & XT_CONNTRACK_PROTO) &&
|
2008-04-14 09:15:52 +00:00
|
|
|
(nf_ct_protonum(ct) == info->l4proto) ^
|
2008-01-31 11:58:24 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_PROTO))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Shortcut to match all recognized protocols by using ->src.all. */
|
|
|
|
if ((info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) &&
|
|
|
|
(tuple->src.u.all == info->origsrc_port) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((info->match_flags & XT_CONNTRACK_ORIGDST_PORT) &&
|
|
|
|
(tuple->dst.u.all == info->origdst_port) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_ORIGDST_PORT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
|
|
|
|
|
|
|
|
if ((info->match_flags & XT_CONNTRACK_REPLSRC_PORT) &&
|
|
|
|
(tuple->src.u.all == info->replsrc_port) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_REPLSRC_PORT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((info->match_flags & XT_CONNTRACK_REPLDST_PORT) &&
|
|
|
|
(tuple->dst.u.all == info->repldst_port) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_REPLDST_PORT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-15 07:40:53 +00:00
|
|
|
static bool
|
2008-10-08 09:35:18 +00:00
|
|
|
conntrack_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2008-01-15 07:40:53 +00:00
|
|
|
{
|
2009-06-29 12:31:46 +00:00
|
|
|
const struct xt_conntrack_mtinfo2 *info = par->matchinfo;
|
2008-01-15 07:40:53 +00:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
const struct nf_conn *ct;
|
|
|
|
unsigned int statebit;
|
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
|
|
|
|
if (ct == &nf_conntrack_untracked)
|
|
|
|
statebit = XT_CONNTRACK_STATE_UNTRACKED;
|
|
|
|
else if (ct != NULL)
|
|
|
|
statebit = XT_CONNTRACK_STATE_BIT(ctinfo);
|
|
|
|
else
|
|
|
|
statebit = XT_CONNTRACK_STATE_INVALID;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_STATE) {
|
|
|
|
if (ct != NULL) {
|
|
|
|
if (test_bit(IPS_SRC_NAT_BIT, &ct->status))
|
|
|
|
statebit |= XT_CONNTRACK_STATE_SNAT;
|
|
|
|
if (test_bit(IPS_DST_NAT_BIT, &ct->status))
|
|
|
|
statebit |= XT_CONNTRACK_STATE_DNAT;
|
|
|
|
}
|
2008-02-27 20:09:05 +00:00
|
|
|
if (!!(info->state_mask & statebit) ^
|
2008-01-15 07:40:53 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_STATE))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ct == NULL)
|
|
|
|
return info->match_flags & XT_CONNTRACK_STATE;
|
2008-01-31 11:58:24 +00:00
|
|
|
if ((info->match_flags & XT_CONNTRACK_DIRECTION) &&
|
|
|
|
(CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) ^
|
|
|
|
!!(info->invert_flags & XT_CONNTRACK_DIRECTION))
|
2008-01-15 07:40:53 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_ORIGSRC)
|
2008-10-08 09:35:20 +00:00
|
|
|
if (conntrack_mt_origsrc(ct, info, par->family) ^
|
2008-01-15 07:40:53 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_ORIGSRC))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_ORIGDST)
|
2008-10-08 09:35:20 +00:00
|
|
|
if (conntrack_mt_origdst(ct, info, par->family) ^
|
2008-01-15 07:40:53 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_ORIGDST))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_REPLSRC)
|
2008-10-08 09:35:20 +00:00
|
|
|
if (conntrack_mt_replsrc(ct, info, par->family) ^
|
2008-01-15 07:40:53 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_REPLSRC))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_REPLDST)
|
2008-10-08 09:35:20 +00:00
|
|
|
if (conntrack_mt_repldst(ct, info, par->family) ^
|
2008-01-15 07:40:53 +00:00
|
|
|
!(info->invert_flags & XT_CONNTRACK_REPLDST))
|
|
|
|
return false;
|
|
|
|
|
2008-01-31 11:58:24 +00:00
|
|
|
if (!ct_proto_port_check(info, ct))
|
|
|
|
return false;
|
|
|
|
|
2008-01-15 07:40:53 +00:00
|
|
|
if ((info->match_flags & XT_CONNTRACK_STATUS) &&
|
|
|
|
(!!(info->status_mask & ct->status) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_STATUS)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->match_flags & XT_CONNTRACK_EXPIRES) {
|
|
|
|
unsigned long expires = 0;
|
|
|
|
|
|
|
|
if (timer_pending(&ct->timeout))
|
|
|
|
expires = (ct->timeout.expires - jiffies) / HZ;
|
|
|
|
if ((expires >= info->expires_min &&
|
|
|
|
expires <= info->expires_max) ^
|
|
|
|
!(info->invert_flags & XT_CONNTRACK_EXPIRES))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-29 12:31:46 +00:00
|
|
|
static bool
|
|
|
|
conntrack_mt_v1(const struct sk_buff *skb, const struct xt_match_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_conntrack_mtinfo2 *const *info = par->matchinfo;
|
|
|
|
struct xt_match_param newpar = *par;
|
|
|
|
|
|
|
|
newpar.matchinfo = *info;
|
|
|
|
return conntrack_mt(skb, &newpar);
|
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:18 +00:00
|
|
|
static bool conntrack_mt_check(const struct xt_mtchk_param *par)
|
2006-03-22 21:56:08 +00:00
|
|
|
{
|
2008-10-08 09:35:20 +00:00
|
|
|
if (nf_ct_l3proto_try_module_get(par->family) < 0) {
|
2006-12-12 08:28:40 +00:00
|
|
|
printk(KERN_WARNING "can't load conntrack support for "
|
2008-10-08 09:35:20 +00:00
|
|
|
"proto=%u\n", par->family);
|
2007-07-08 05:16:00 +00:00
|
|
|
return false;
|
2006-03-22 21:56:08 +00:00
|
|
|
}
|
2007-07-08 05:16:00 +00:00
|
|
|
return true;
|
2006-03-22 21:56:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 12:31:46 +00:00
|
|
|
static bool conntrack_mt_check_v1(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
struct xt_conntrack_mtinfo1 *info = par->matchinfo;
|
|
|
|
struct xt_conntrack_mtinfo2 *up;
|
|
|
|
int ret = conntrack_mt_check(par);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
up = kmalloc(sizeof(*up), GFP_KERNEL);
|
|
|
|
if (up == NULL) {
|
|
|
|
nf_ct_l3proto_module_put(par->family);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The strategy here is to minimize the overhead of v1 matching,
|
|
|
|
* by prebuilding a v2 struct and putting the pointer into the
|
|
|
|
* v1 dataspace.
|
|
|
|
*/
|
|
|
|
memcpy(up, info, offsetof(typeof(*info), state_mask));
|
|
|
|
up->state_mask = info->state_mask;
|
|
|
|
up->status_mask = info->status_mask;
|
|
|
|
*(void **)info = up;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:19 +00:00
|
|
|
static void conntrack_mt_destroy(const struct xt_mtdtor_param *par)
|
2006-03-22 21:56:08 +00:00
|
|
|
{
|
2008-10-08 09:35:20 +00:00
|
|
|
nf_ct_l3proto_module_put(par->family);
|
2006-03-22 21:56:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 12:31:46 +00:00
|
|
|
static void conntrack_mt_destroy_v1(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
struct xt_conntrack_mtinfo2 **info = par->matchinfo;
|
|
|
|
kfree(*info);
|
|
|
|
conntrack_mt_destroy(par);
|
|
|
|
}
|
|
|
|
|
2008-01-15 07:40:53 +00:00
|
|
|
static struct xt_match conntrack_mt_reg[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "conntrack",
|
|
|
|
.revision = 1,
|
2008-10-08 09:35:20 +00:00
|
|
|
.family = NFPROTO_UNSPEC,
|
2008-01-15 07:40:53 +00:00
|
|
|
.matchsize = sizeof(struct xt_conntrack_mtinfo1),
|
2009-06-29 12:31:46 +00:00
|
|
|
.match = conntrack_mt_v1,
|
|
|
|
.checkentry = conntrack_mt_check_v1,
|
|
|
|
.destroy = conntrack_mt_destroy_v1,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "conntrack",
|
|
|
|
.revision = 2,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.matchsize = sizeof(struct xt_conntrack_mtinfo2),
|
2008-01-15 07:40:53 +00:00
|
|
|
.match = conntrack_mt,
|
|
|
|
.checkentry = conntrack_mt_check,
|
|
|
|
.destroy = conntrack_mt_destroy,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init conntrack_mt_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-15 07:40:53 +00:00
|
|
|
return xt_register_matches(conntrack_mt_reg,
|
|
|
|
ARRAY_SIZE(conntrack_mt_reg));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit conntrack_mt_exit(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-15 07:40:53 +00:00
|
|
|
xt_unregister_matches(conntrack_mt_reg, ARRAY_SIZE(conntrack_mt_reg));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(conntrack_mt_init);
|
|
|
|
module_exit(conntrack_mt_exit);
|