Skip to contentSkip to conversation

netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

PATCH In review Harout Hedeshian 2 participants 6 messages Jun 12, 2015 – Jun 16, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

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 match sockets based on the socket mark.

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 --mark 10 -J act1 iptables -t mangle -I PREROUTING -m socket --transparent --mark 11 -J act2

The mark field also takes an optional mask and is invertable.

---
 include/uapi/linux/netfilter/xt_socket.h |  10 +++
 net/netfilter/xt_socket.c                | 108 +++++++++++++++++++++++++++++--
 2 files changed, 112 insertions(+), 6 deletions(-)
include/uapi/linux/netfilter/xt_socket.h +10 -0
index 6315e2a..3a7304e 100644
@@ -6,6 +6,7 @@
 enum {
 	XT_SOCKET_TRANSPARENT = 1 << 0,
 	XT_SOCKET_NOWILDCARD = 1 << 1,
+	XT_SOCKET_MATCHSOCKMARK = 1 << 2,
 };
 
 struct xt_socket_mtinfo1 {
@@ -18,4 +19,13 @@ struct xt_socket_mtinfo2 {
 };
 #define XT_SOCKET_FLAGS_V2 (XT_SOCKET_TRANSPARENT | XT_SOCKET_NOWILDCARD)
 
+struct xt_socket_mtinfo3 {
+	__u8 flags;
+	__u32 mark, mask;
+	__u8 invert_mark;
+};
+#define XT_SOCKET_FLAGS_V3 (XT_SOCKET_TRANSPARENT \
+			   | XT_SOCKET_NOWILDCARD \
+			   | XT_SOCKET_MATCHSOCKMARK)
+
 #endif /* _XT_SOCKET_H */
net/netfilter/xt_socket.c +102 -6

181 lines not on this page · raw message

Signature · 2 lines
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
#1 lore raw
Quoted 8 lines · Harout Hedeshian

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.

Quoted 7 lines

Add the ability to match sockets based on the socket mark.

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 --mark 10 -J act1 iptables -t mangle -I PREROUTING -m socket --transparent --mark 11 -J act2

Wouldn't it be better to restore the sk_mark to skb->mark? I mean:

        iptables -t mangle -I PREROUTING \
                -m socket --transparent --restore-skmark -j myskchain

Thus, you can use -m mark from the 'myskchain' without having to fetch and inspect the sk over and over again as it happens with this extension.

#2 lore raw
Quoted 28 lines

-----Original Message-----
From: Pablo Neira Ayuso [mailto:[email protected]]
Sent: Monday, June 15, 2015 10:23 AM
To: Harout Hedeshian
Cc: [email protected]; Lorenzo Colitti
Subject: Re: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 8 lines · Harout Hedeshian

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.

Quoted 8 lines

Add the ability to match sockets based on the socket mark.

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 --mark 10 -J act1 iptables -t mangle -I PREROUTING -m socket --transparent --mark 11 -J act2

Quoted 8 lines

Wouldn't it be better to restore the sk_mark to skb->mark? I mean:

        iptables -t mangle -I PREROUTING \
                -m socket --transparent --restore-skmark -j myskchain

Thus, you can use -m mark from the 'myskchain' without having to fetch and inspect the sk over and over again as it happens with this extension.

Yes, I think that can work. Let me try that out and I'll submit a new patch.

#3 lore raw
In reply to Pablo Neira Ayuso
Quoted 45 lines

-----Original Message-----
From: Harout Hedeshian [mailto:[email protected]]
Sent: Monday, June 15, 2015 11:42 AM
To: 'Pablo Neira Ayuso'
Cc: '[email protected]'; 'Lorenzo Colitti'
Subject: RE: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 8 lines

-----Original Message-----
From: Pablo Neira Ayuso [mailto:[email protected]]
Sent: Monday, June 15, 2015 10:23 AM
To: Harout Hedeshian
Cc: [email protected]; Lorenzo Colitti
Subject: Re: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 8 lines
Quoted 7 lines · Harout Hedeshian

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.

Quoted 16 lines
Quoted 10 lines

Add the ability to match sockets based on the socket mark.

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 --mark 10 -J
act1 iptables -t mangle -I PREROUTING -m socket --transparent --mark 11 -J act2

Wouldn't it be better to restore the sk_mark to skb->mark? I mean:

        iptables -t mangle -I PREROUTING \
                -m socket --transparent --restore-skmark -j myskchain
Quoted 6 lines
Quoted 3 lines

Thus, you can use -m mark from the 'myskchain' without having to fetch and inspect the sk over and over again as it happens with this extension.

Yes, I think that can work. Let me try that out and I'll submit a new patch.

Actually, on second thought, I'm not so sure. This socket lookup is happening as part of a match operation in xt_socket.c.

Quoted 1 line

From x_tables.h, I can see that match functions are not supposed to modify

the skb:

struct xt_match{
...
bool (*match)(const struct sk_buff *skb, struct xt_action_param *);

In that case, would this even be valid?
-m socket --transparent --restore-skmark

Keeping in mind --restore-skmark is happening as part of -m socket

I would think we would need a whole new target to handle something like this:
iptables -t mangle -I PREROUTING -m socket --transparent --no-wildcard -j SOCKET --restore-skmark

Since the target invocation would be separate, we would need a second socket lookup? Seems perhaps a little overkill...

Harout

#4 lore raw
Quoted 66 lines · Harout Hedeshian
Quoted 45 lines

-----Original Message-----
From: Harout Hedeshian [mailto:[email protected]]
Sent: Monday, June 15, 2015 11:42 AM
To: 'Pablo Neira Ayuso'
Cc: '[email protected]'; 'Lorenzo Colitti'
Subject: RE: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 8 lines

-----Original Message-----
From: Pablo Neira Ayuso [mailto:[email protected]]
Sent: Monday, June 15, 2015 10:23 AM
To: Harout Hedeshian
Cc: [email protected]; Lorenzo Colitti
Subject: Re: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 8 lines
Quoted 7 lines · Harout Hedeshian

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.

Quoted 16 lines
Quoted 10 lines

Add the ability to match sockets based on the socket mark.

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 --mark 10 -J
act1 iptables -t mangle -I PREROUTING -m socket --transparent --mark 11 -J act2

Wouldn't it be better to restore the sk_mark to skb->mark? I mean:

        iptables -t mangle -I PREROUTING \
                -m socket --transparent --restore-skmark -j myskchain
Quoted 6 lines
Quoted 3 lines

Thus, you can use -m mark from the 'myskchain' without having to fetch and inspect the sk over and over again as it happens with this extension.

Yes, I think that can work. Let me try that out and I'll submit a new patch.

Actually, on second thought, I'm not so sure. This socket lookup is happening as part of a match operation in xt_socket.c.
From x_tables.h, I can see that match functions are not supposed to modify the skb:

struct xt_match{
...
bool (*match)(const struct sk_buff *skb, struct xt_action_param *);

In that case, would this even be valid?
-m socket --transparent --restore-skmark

Keeping in mind --restore-skmark is happening as part of -m socket

Quoted 7 lines

I would think we would need a whole new target to handle something like this:
iptables -t mangle -I PREROUTING -m socket --transparent --no-wildcard -j SOCKET --restore-skmark

Since the target invocation would be separate, we would need a second socket lookup? Seems perhaps a little overkill...

Yes, it's simply overkill to add a new target to add this.

So just cast it:

        struct sk_buff *pskb = (struct sk_buff *)skb;

iptables matches were originally design not to modify anything, but it can lead us to situations like this. That's one of the reasons why we have no distinction between matches and targets in nftables anymore.

Thanks.

#5 lore raw
Quoted 30 lines

-----Original Message-----
From: Pablo Neira Ayuso [mailto:[email protected]]
Sent: Monday, June 15, 2015 2:58 PM
To: Harout Hedeshian
Cc: [email protected]; 'Lorenzo Colitti'
Subject: Re: [PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_MATCHSOCKMARK flag and mark fields

Quoted 21 lines

Actually, on second thought, I'm not so sure. This socket lookup is happening as part of a match operation in xt_socket.c. From x_tables.h, I can see that match functions are not supposed to modify the skb:

struct xt_match{
...
bool (*match)(const struct sk_buff *skb, struct xt_action_param *);

In that case, would this even be valid?
-m socket --transparent --restore-skmark

Keeping in mind --restore-skmark is happening as part of -m socket

I would think we would need a whole new target to handle something like
this:
iptables -t mangle -I PREROUTING -m socket --transparent --no-wildcard -j SOCKET --restore-skmark

Quoted 6 lines
Quoted 2 lines

Since the target invocation would be separate, we would need a second socket lookup? Seems perhaps a little overkill...

Yes, it's simply overkill to add a new target to add this.

So just cast it:

Yuck...

Quoted 5 lines
        struct sk_buff *pskb = (struct sk_buff *)skb;

iptables matches were originally design not to modify anything, but it can lead us to situations like this. That's one of the reasons why we have no distinction between matches and targets in nftables anymore.

That makes sense.

Quoted 1 line

Thanks.

These 2 MATCHSOCKMARK patches should be replaced with:
[PATCH nf-next] netfilter: xt_socket: add XT_SOCKET_RESTORESKMARK flag [PATCH iptables] xt_socket: add --restore-skmark option

Thanks,
Harout