提交 b4164998 编写于 作者: J Jan Engelhardt 提交者: David S. Miller

[NETFILTER]: xt_conntrack: add port and direction matching

Extend the xt_conntrack match revision 1 by port matching (all four
{orig,repl}{src,dst}) and by packet direction matching.
Signed-off-by: NJan Engelhardt <jengelh@computergmbh.de>
Signed-off-by: NPatrick McHardy <kaber@trash.net>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 41d0cded
...@@ -6,9 +6,6 @@ ...@@ -6,9 +6,6 @@
#define _XT_CONNTRACK_H #define _XT_CONNTRACK_H
#include <linux/netfilter/nf_conntrack_tuple_common.h> #include <linux/netfilter/nf_conntrack_tuple_common.h>
#ifdef __KERNEL__
# include <linux/in.h>
#endif
#define XT_CONNTRACK_STATE_BIT(ctinfo) (1 << ((ctinfo)%IP_CT_IS_REPLY+1)) #define XT_CONNTRACK_STATE_BIT(ctinfo) (1 << ((ctinfo)%IP_CT_IS_REPLY+1))
#define XT_CONNTRACK_STATE_INVALID (1 << 0) #define XT_CONNTRACK_STATE_INVALID (1 << 0)
...@@ -18,14 +15,21 @@ ...@@ -18,14 +15,21 @@
#define XT_CONNTRACK_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 3)) #define XT_CONNTRACK_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 3))
/* flags, invflags: */ /* flags, invflags: */
#define XT_CONNTRACK_STATE 0x01 enum {
#define XT_CONNTRACK_PROTO 0x02 XT_CONNTRACK_STATE = 1 << 0,
#define XT_CONNTRACK_ORIGSRC 0x04 XT_CONNTRACK_PROTO = 1 << 1,
#define XT_CONNTRACK_ORIGDST 0x08 XT_CONNTRACK_ORIGSRC = 1 << 2,
#define XT_CONNTRACK_REPLSRC 0x10 XT_CONNTRACK_ORIGDST = 1 << 3,
#define XT_CONNTRACK_REPLDST 0x20 XT_CONNTRACK_REPLSRC = 1 << 4,
#define XT_CONNTRACK_STATUS 0x40 XT_CONNTRACK_REPLDST = 1 << 5,
#define XT_CONNTRACK_EXPIRES 0x80 XT_CONNTRACK_STATUS = 1 << 6,
XT_CONNTRACK_EXPIRES = 1 << 7,
XT_CONNTRACK_ORIGSRC_PORT = 1 << 8,
XT_CONNTRACK_ORIGDST_PORT = 1 << 9,
XT_CONNTRACK_REPLSRC_PORT = 1 << 10,
XT_CONNTRACK_REPLDST_PORT = 1 << 11,
XT_CONNTRACK_DIRECTION = 1 << 12,
};
/* This is exposed to userspace, so remains frozen in time. */ /* This is exposed to userspace, so remains frozen in time. */
struct ip_conntrack_old_tuple struct ip_conntrack_old_tuple
...@@ -70,8 +74,10 @@ struct xt_conntrack_mtinfo1 { ...@@ -70,8 +74,10 @@ struct xt_conntrack_mtinfo1 {
union nf_inet_addr repldst_addr, repldst_mask; union nf_inet_addr repldst_addr, repldst_mask;
u_int32_t expires_min, expires_max; u_int32_t expires_min, expires_max;
u_int16_t l4proto; u_int16_t l4proto;
__be16 origsrc_port, origdst_port;
__be16 replsrc_port, repldst_port;
u_int16_t match_flags, invert_flags;
u_int8_t state_mask, status_mask; u_int8_t state_mask, status_mask;
u_int8_t match_flags, invert_flags;
}; };
#endif /*_XT_CONNTRACK_H*/ #endif /*_XT_CONNTRACK_H*/
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
* *
* (C) 2001 Marc Boucher (marc@mbsi.ca). * (C) 2001 Marc Boucher (marc@mbsi.ca).
* Copyright © CC Computer Consultants GmbH, 2007 - 2008 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
* Jan Engelhardt <jengelh@computergmbh.de>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License version 2 as
...@@ -20,6 +19,7 @@ ...@@ -20,6 +19,7 @@
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
MODULE_DESCRIPTION("Xtables: connection tracking state match"); MODULE_DESCRIPTION("Xtables: connection tracking state match");
MODULE_ALIAS("ipt_conntrack"); MODULE_ALIAS("ipt_conntrack");
MODULE_ALIAS("ip6t_conntrack"); MODULE_ALIAS("ip6t_conntrack");
...@@ -166,6 +166,44 @@ conntrack_mt_repldst(const struct nf_conn *ct, ...@@ -166,6 +166,44 @@ conntrack_mt_repldst(const struct nf_conn *ct,
&info->repldst_addr, &info->repldst_mask, family); &info->repldst_addr, &info->repldst_mask, family);
} }
static inline bool
ct_proto_port_check(const struct xt_conntrack_mtinfo1 *info,
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) &&
(tuple->dst.protonum == info->l4proto) ^
!(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;
}
static bool static bool
conntrack_mt(const struct sk_buff *skb, const struct net_device *in, conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
const struct net_device *out, const struct xt_match *match, const struct net_device *out, const struct xt_match *match,
...@@ -200,10 +238,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in, ...@@ -200,10 +238,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
if (ct == NULL) if (ct == NULL)
return info->match_flags & XT_CONNTRACK_STATE; return info->match_flags & XT_CONNTRACK_STATE;
if ((info->match_flags & XT_CONNTRACK_DIRECTION) &&
if ((info->match_flags & XT_CONNTRACK_PROTO) && (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) ^
((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum == !!(info->invert_flags & XT_CONNTRACK_DIRECTION))
info->l4proto) ^ !(info->invert_flags & XT_CONNTRACK_PROTO)))
return false; return false;
if (info->match_flags & XT_CONNTRACK_ORIGSRC) if (info->match_flags & XT_CONNTRACK_ORIGSRC)
...@@ -226,6 +263,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in, ...@@ -226,6 +263,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
!(info->invert_flags & XT_CONNTRACK_REPLDST)) !(info->invert_flags & XT_CONNTRACK_REPLDST))
return false; return false;
if (!ct_proto_port_check(info, ct))
return false;
if ((info->match_flags & XT_CONNTRACK_STATUS) && if ((info->match_flags & XT_CONNTRACK_STATUS) &&
(!!(info->status_mask & ct->status) ^ (!!(info->status_mask & ct->status) ^
!(info->invert_flags & XT_CONNTRACK_STATUS))) !(info->invert_flags & XT_CONNTRACK_STATUS)))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册