提交 01555e74 编写于 作者: H Harout Hedeshian 提交者: Pablo Neira Ayuso

netfilter: xt_socket: add XT_SOCKET_RESTORESKMARK flag

xt_socket is useful for matching sockets with IP_TRANSPARENT and
taking some action on the matching packets. However, it lacks the
ability to match only a small subset of transparent sockets.

Suppose there are 2 applications, each with its own set of transparent
sockets. The first application wants all matching packets dropped,
while the second application wants them forwarded somewhere else.

Add the ability to retore the skb->mark from the sk_mark. The mark
is only restored if a matching socket is found and the transparent /
nowildcard conditions are satisfied.

Now the 2 hypothetical applications can differentiate their sockets
based on a mark value set with SO_MARK.

iptables -t mangle -I PREROUTING -m socket --transparent \
                                           --restore-skmark -j action
iptables -t mangle -A action -m mark --mark 10 -j action2
iptables -t mangle -A action -m mark --mark 11 -j action3
Signed-off-by: NHarout Hedeshian <harouth@codeaurora.org>
Signed-off-by: NPablo Neira Ayuso <pablo@netfilter.org>
上级 ef493bd9
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
enum { enum {
XT_SOCKET_TRANSPARENT = 1 << 0, XT_SOCKET_TRANSPARENT = 1 << 0,
XT_SOCKET_NOWILDCARD = 1 << 1, XT_SOCKET_NOWILDCARD = 1 << 1,
XT_SOCKET_RESTORESKMARK = 1 << 2,
}; };
struct xt_socket_mtinfo1 { struct xt_socket_mtinfo1 {
...@@ -18,4 +19,11 @@ struct xt_socket_mtinfo2 { ...@@ -18,4 +19,11 @@ struct xt_socket_mtinfo2 {
}; };
#define XT_SOCKET_FLAGS_V2 (XT_SOCKET_TRANSPARENT | XT_SOCKET_NOWILDCARD) #define XT_SOCKET_FLAGS_V2 (XT_SOCKET_TRANSPARENT | XT_SOCKET_NOWILDCARD)
struct xt_socket_mtinfo3 {
__u8 flags;
};
#define XT_SOCKET_FLAGS_V3 (XT_SOCKET_TRANSPARENT \
| XT_SOCKET_NOWILDCARD \
| XT_SOCKET_RESTORESKMARK)
#endif /* _XT_SOCKET_H */ #endif /* _XT_SOCKET_H */
...@@ -205,6 +205,7 @@ static bool ...@@ -205,6 +205,7 @@ static bool
socket_match(const struct sk_buff *skb, struct xt_action_param *par, socket_match(const struct sk_buff *skb, struct xt_action_param *par,
const struct xt_socket_mtinfo1 *info) const struct xt_socket_mtinfo1 *info)
{ {
struct sk_buff *pskb = (struct sk_buff *)skb;
struct sock *sk = skb->sk; struct sock *sk = skb->sk;
if (!sk) if (!sk)
...@@ -226,6 +227,10 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par, ...@@ -226,6 +227,10 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
if (info->flags & XT_SOCKET_TRANSPARENT) if (info->flags & XT_SOCKET_TRANSPARENT)
transparent = xt_socket_sk_is_transparent(sk); transparent = xt_socket_sk_is_transparent(sk);
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
transparent)
pskb->mark = sk->sk_mark;
if (sk != skb->sk) if (sk != skb->sk)
sock_gen_put(sk); sock_gen_put(sk);
...@@ -247,7 +252,7 @@ socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par) ...@@ -247,7 +252,7 @@ socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
} }
static bool static bool
socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
{ {
return socket_match(skb, par, par->matchinfo); return socket_match(skb, par, par->matchinfo);
} }
...@@ -371,9 +376,10 @@ static struct sock *xt_socket_lookup_slow_v6(const struct sk_buff *skb, ...@@ -371,9 +376,10 @@ static struct sock *xt_socket_lookup_slow_v6(const struct sk_buff *skb,
} }
static bool static bool
socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
{ {
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo; const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
struct sk_buff *pskb = (struct sk_buff *)skb;
struct sock *sk = skb->sk; struct sock *sk = skb->sk;
if (!sk) if (!sk)
...@@ -395,6 +401,10 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) ...@@ -395,6 +401,10 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
if (info->flags & XT_SOCKET_TRANSPARENT) if (info->flags & XT_SOCKET_TRANSPARENT)
transparent = xt_socket_sk_is_transparent(sk); transparent = xt_socket_sk_is_transparent(sk);
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
transparent)
pskb->mark = sk->sk_mark;
if (sk != skb->sk) if (sk != skb->sk)
sock_gen_put(sk); sock_gen_put(sk);
...@@ -428,6 +438,19 @@ static int socket_mt_v2_check(const struct xt_mtchk_param *par) ...@@ -428,6 +438,19 @@ static int socket_mt_v2_check(const struct xt_mtchk_param *par)
return 0; return 0;
} }
static int socket_mt_v3_check(const struct xt_mtchk_param *par)
{
const struct xt_socket_mtinfo3 *info =
(struct xt_socket_mtinfo3 *)par->matchinfo;
if (info->flags & ~XT_SOCKET_FLAGS_V3) {
pr_info("unknown flags 0x%x\n",
info->flags & ~XT_SOCKET_FLAGS_V3);
return -EINVAL;
}
return 0;
}
static struct xt_match socket_mt_reg[] __read_mostly = { static struct xt_match socket_mt_reg[] __read_mostly = {
{ {
.name = "socket", .name = "socket",
...@@ -442,7 +465,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = { ...@@ -442,7 +465,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = {
.name = "socket", .name = "socket",
.revision = 1, .revision = 1,
.family = NFPROTO_IPV4, .family = NFPROTO_IPV4,
.match = socket_mt4_v1_v2, .match = socket_mt4_v1_v2_v3,
.checkentry = socket_mt_v1_check, .checkentry = socket_mt_v1_check,
.matchsize = sizeof(struct xt_socket_mtinfo1), .matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) | .hooks = (1 << NF_INET_PRE_ROUTING) |
...@@ -454,7 +477,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = { ...@@ -454,7 +477,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = {
.name = "socket", .name = "socket",
.revision = 1, .revision = 1,
.family = NFPROTO_IPV6, .family = NFPROTO_IPV6,
.match = socket_mt6_v1_v2, .match = socket_mt6_v1_v2_v3,
.checkentry = socket_mt_v1_check, .checkentry = socket_mt_v1_check,
.matchsize = sizeof(struct xt_socket_mtinfo1), .matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) | .hooks = (1 << NF_INET_PRE_ROUTING) |
...@@ -466,7 +489,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = { ...@@ -466,7 +489,7 @@ static struct xt_match socket_mt_reg[] __read_mostly = {
.name = "socket", .name = "socket",
.revision = 2, .revision = 2,
.family = NFPROTO_IPV4, .family = NFPROTO_IPV4,
.match = socket_mt4_v1_v2, .match = socket_mt4_v1_v2_v3,
.checkentry = socket_mt_v2_check, .checkentry = socket_mt_v2_check,
.matchsize = sizeof(struct xt_socket_mtinfo1), .matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) | .hooks = (1 << NF_INET_PRE_ROUTING) |
...@@ -478,13 +501,37 @@ static struct xt_match socket_mt_reg[] __read_mostly = { ...@@ -478,13 +501,37 @@ static struct xt_match socket_mt_reg[] __read_mostly = {
.name = "socket", .name = "socket",
.revision = 2, .revision = 2,
.family = NFPROTO_IPV6, .family = NFPROTO_IPV6,
.match = socket_mt6_v1_v2, .match = socket_mt6_v1_v2_v3,
.checkentry = socket_mt_v2_check, .checkentry = socket_mt_v2_check,
.matchsize = sizeof(struct xt_socket_mtinfo1), .matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) | .hooks = (1 << NF_INET_PRE_ROUTING) |
(1 << NF_INET_LOCAL_IN), (1 << NF_INET_LOCAL_IN),
.me = THIS_MODULE, .me = THIS_MODULE,
}, },
#endif
{
.name = "socket",
.revision = 3,
.family = NFPROTO_IPV4,
.match = socket_mt4_v1_v2_v3,
.checkentry = socket_mt_v3_check,
.matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) |
(1 << NF_INET_LOCAL_IN),
.me = THIS_MODULE,
},
#ifdef XT_SOCKET_HAVE_IPV6
{
.name = "socket",
.revision = 3,
.family = NFPROTO_IPV6,
.match = socket_mt6_v1_v2_v3,
.checkentry = socket_mt_v3_check,
.matchsize = sizeof(struct xt_socket_mtinfo1),
.hooks = (1 << NF_INET_PRE_ROUTING) |
(1 << NF_INET_LOCAL_IN),
.me = THIS_MODULE,
},
#endif #endif
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册