Commit 472a73e0 authored by Jack Ma's avatar Jack Ma Committed by Pablo Neira Ayuso

netfilter: xt_conntrack: Support bit-shifting for CONNMARK & MARK targets.

This patch introduces a new feature that allows bitshifting (left
and right) operations to co-operate with existing iptables options.
Reviewed-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarJack Ma <jack.ma@alliedtelesis.co.nz>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent d72133e6
...@@ -19,11 +19,21 @@ enum { ...@@ -19,11 +19,21 @@ enum {
XT_CONNMARK_RESTORE XT_CONNMARK_RESTORE
}; };
enum {
D_SHIFT_LEFT = 0,
D_SHIFT_RIGHT,
};
struct xt_connmark_tginfo1 { struct xt_connmark_tginfo1 {
__u32 ctmark, ctmask, nfmask; __u32 ctmark, ctmask, nfmask;
__u8 mode; __u8 mode;
}; };
struct xt_connmark_tginfo2 {
__u32 ctmark, ctmask, nfmask;
__u8 shift_dir, shift_bits, mode;
};
struct xt_connmark_mtinfo1 { struct xt_connmark_mtinfo1 {
__u32 mark, mask; __u32 mark, mask;
__u8 invert; __u8 invert;
......
...@@ -36,9 +36,10 @@ MODULE_ALIAS("ipt_connmark"); ...@@ -36,9 +36,10 @@ MODULE_ALIAS("ipt_connmark");
MODULE_ALIAS("ip6t_connmark"); MODULE_ALIAS("ip6t_connmark");
static unsigned int static unsigned int
connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) connmark_tg_shift(struct sk_buff *skb,
const struct xt_connmark_tginfo1 *info,
u8 shift_bits, u8 shift_dir)
{ {
const struct xt_connmark_tginfo1 *info = par->targinfo;
enum ip_conntrack_info ctinfo; enum ip_conntrack_info ctinfo;
struct nf_conn *ct; struct nf_conn *ct;
u_int32_t newmark; u_int32_t newmark;
...@@ -50,6 +51,10 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) ...@@ -50,6 +51,10 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
switch (info->mode) { switch (info->mode) {
case XT_CONNMARK_SET: case XT_CONNMARK_SET:
newmark = (ct->mark & ~info->ctmask) ^ info->ctmark; newmark = (ct->mark & ~info->ctmask) ^ info->ctmark;
if (shift_dir == D_SHIFT_RIGHT)
newmark >>= shift_bits;
else
newmark <<= shift_bits;
if (ct->mark != newmark) { if (ct->mark != newmark) {
ct->mark = newmark; ct->mark = newmark;
nf_conntrack_event_cache(IPCT_MARK, ct); nf_conntrack_event_cache(IPCT_MARK, ct);
...@@ -58,6 +63,10 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) ...@@ -58,6 +63,10 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
case XT_CONNMARK_SAVE: case XT_CONNMARK_SAVE:
newmark = (ct->mark & ~info->ctmask) ^ newmark = (ct->mark & ~info->ctmask) ^
(skb->mark & info->nfmask); (skb->mark & info->nfmask);
if (shift_dir == D_SHIFT_RIGHT)
newmark >>= shift_bits;
else
newmark <<= shift_bits;
if (ct->mark != newmark) { if (ct->mark != newmark) {
ct->mark = newmark; ct->mark = newmark;
nf_conntrack_event_cache(IPCT_MARK, ct); nf_conntrack_event_cache(IPCT_MARK, ct);
...@@ -66,13 +75,33 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) ...@@ -66,13 +75,33 @@ connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
case XT_CONNMARK_RESTORE: case XT_CONNMARK_RESTORE:
newmark = (skb->mark & ~info->nfmask) ^ newmark = (skb->mark & ~info->nfmask) ^
(ct->mark & info->ctmask); (ct->mark & info->ctmask);
if (shift_dir == D_SHIFT_RIGHT)
newmark >>= shift_bits;
else
newmark <<= shift_bits;
skb->mark = newmark; skb->mark = newmark;
break; break;
} }
return XT_CONTINUE; return XT_CONTINUE;
} }
static unsigned int
connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct xt_connmark_tginfo1 *info = par->targinfo;
return connmark_tg_shift(skb, info, 0, 0);
}
static unsigned int
connmark_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct xt_connmark_tginfo2 *info = par->targinfo;
return connmark_tg_shift(skb, (const struct xt_connmark_tginfo1 *)info,
info->shift_bits, info->shift_dir);
}
static int connmark_tg_check(const struct xt_tgchk_param *par) static int connmark_tg_check(const struct xt_tgchk_param *par)
{ {
int ret; int ret;
...@@ -119,7 +148,8 @@ static void connmark_mt_destroy(const struct xt_mtdtor_param *par) ...@@ -119,7 +148,8 @@ static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
nf_ct_netns_put(par->net, par->family); nf_ct_netns_put(par->net, par->family);
} }
static struct xt_target connmark_tg_reg __read_mostly = { static struct xt_target connmark_tg_reg[] __read_mostly = {
{
.name = "CONNMARK", .name = "CONNMARK",
.revision = 1, .revision = 1,
.family = NFPROTO_UNSPEC, .family = NFPROTO_UNSPEC,
...@@ -128,6 +158,17 @@ static struct xt_target connmark_tg_reg __read_mostly = { ...@@ -128,6 +158,17 @@ static struct xt_target connmark_tg_reg __read_mostly = {
.targetsize = sizeof(struct xt_connmark_tginfo1), .targetsize = sizeof(struct xt_connmark_tginfo1),
.destroy = connmark_tg_destroy, .destroy = connmark_tg_destroy,
.me = THIS_MODULE, .me = THIS_MODULE,
},
{
.name = "CONNMARK",
.revision = 2,
.family = NFPROTO_UNSPEC,
.checkentry = connmark_tg_check,
.target = connmark_tg_v2,
.targetsize = sizeof(struct xt_connmark_tginfo2),
.destroy = connmark_tg_destroy,
.me = THIS_MODULE,
}
}; };
static struct xt_match connmark_mt_reg __read_mostly = { static struct xt_match connmark_mt_reg __read_mostly = {
...@@ -145,12 +186,14 @@ static int __init connmark_mt_init(void) ...@@ -145,12 +186,14 @@ static int __init connmark_mt_init(void)
{ {
int ret; int ret;
ret = xt_register_target(&connmark_tg_reg); ret = xt_register_targets(connmark_tg_reg,
ARRAY_SIZE(connmark_tg_reg));
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = xt_register_match(&connmark_mt_reg); ret = xt_register_match(&connmark_mt_reg);
if (ret < 0) { if (ret < 0) {
xt_unregister_target(&connmark_tg_reg); xt_unregister_targets(connmark_tg_reg,
ARRAY_SIZE(connmark_tg_reg));
return ret; return ret;
} }
return 0; return 0;
...@@ -159,7 +202,7 @@ static int __init connmark_mt_init(void) ...@@ -159,7 +202,7 @@ static int __init connmark_mt_init(void)
static void __exit connmark_mt_exit(void) static void __exit connmark_mt_exit(void)
{ {
xt_unregister_match(&connmark_mt_reg); xt_unregister_match(&connmark_mt_reg);
xt_unregister_target(&connmark_tg_reg); xt_unregister_target(connmark_tg_reg);
} }
module_init(connmark_mt_init); module_init(connmark_mt_init);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment