Skip to content

Commit cb8aa9a

Browse files
inemajoummakynes
authored andcommitted
netfilter: ctnetlink: add kernel side filtering for dump
Conntrack dump does not support kernel side filtering (only get exists, but it returns only one entry. And user has to give a full valid tuple) It means that userspace has to implement filtering after receiving many irrelevant entries, consuming resources (conntrack table is sometimes very huge, much more than a routing table for example). This patch adds filtering in kernel side. To achieve this goal, we: * Add a new CTA_FILTER netlink attributes, actually a flag list to parametize filtering * Convert some *nlattr_to_tuple() functions, to allow a partial parsing of CTA_TUPLE_ORIG and CTA_TUPLE_REPLY (so nf_conntrack_tuple it not fully set) Filtering is now possible on: * IP SRC/DST values * Ports for TCP and UDP flows * IMCP(v6) codes types and IDs Filtering is done as an "AND" operator. For example, when flags PROTO_SRC_PORT, PROTO_NUM and IP_SRC are sets, only entries matching all values are dumped. Changes since v1: Set NLM_F_DUMP_FILTERED in nlm flags if entries are filtered Changes since v2: Move several constants to nf_internals.h Move a fix on netlink values check in a separate patch Add a check on not-supported flags Return EOPNOTSUPP if CDA_FILTER is set in ctnetlink_flush_conntrack (not yet implemented) Code style issues Changes since v3: Fix compilation warning reported by kbuild test robot Changes since v4: Fix a regression introduced in v3 (returned EINVAL for valid netlink messages without CTA_MARK) Changes since v5: Change definition of CTA_FILTER_F_ALL Fix a regression when CTA_TUPLE_ZONE is not set Signed-off-by: Romain Bellan <romain.bellan@wifirst.fr> Signed-off-by: Florent Fourcot <florent.fourcot@wifirst.fr> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 626a832 commit cb8aa9a

7 files changed

Lines changed: 394 additions & 73 deletions

File tree

‎include/net/netfilter/nf_conntrack_l4proto.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct nf_conntrack_l4proto {
4242
/* Calculate tuple nlattr size */
4343
unsigned int (*nlattr_tuple_size)(void);
4444
int (*nlattr_to_tuple)(struct nlattr *tb[],
45-
struct nf_conntrack_tuple *t);
45+
struct nf_conntrack_tuple *t,
46+
u_int32_t flags);
4647
const struct nla_policy *nla_policy;
4748

4849
struct {
@@ -152,7 +153,8 @@ const struct nf_conntrack_l4proto *nf_ct_l4proto_find(u8 l4proto);
152153
int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
153154
const struct nf_conntrack_tuple *tuple);
154155
int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
155-
struct nf_conntrack_tuple *t);
156+
struct nf_conntrack_tuple *t,
157+
u_int32_t flags);
156158
unsigned int nf_ct_port_nlattr_tuple_size(void);
157159
extern const struct nla_policy nf_ct_port_nla_policy[];
158160

‎include/uapi/linux/netfilter/nfnetlink_conntrack.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ enum ctattr_type {
5555
CTA_LABELS,
5656
CTA_LABELS_MASK,
5757
CTA_SYNPROXY,
58+
CTA_FILTER,
5859
__CTA_MAX
5960
};
6061
#define CTA_MAX (__CTA_MAX - 1)
@@ -276,4 +277,12 @@ enum ctattr_expect_stats {
276277
};
277278
#define CTA_STATS_EXP_MAX (__CTA_STATS_EXP_MAX - 1)
278279

280+
enum ctattr_filter {
281+
CTA_FILTER_UNSPEC,
282+
CTA_FILTER_ORIG_FLAGS,
283+
CTA_FILTER_REPLY_FLAGS,
284+
__CTA_FILTER_MAX
285+
};
286+
#define CTA_FILTER_MAX (__CTA_FILTER_MAX - 1)
287+
279288
#endif /* _IPCONNTRACK_NETLINK_H */

‎net/netfilter/nf_conntrack_core.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,13 +1974,22 @@ const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
19741974
EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
19751975

19761976
int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
1977-
struct nf_conntrack_tuple *t)
1977+
struct nf_conntrack_tuple *t,
1978+
u_int32_t flags)
19781979
{
1979-
if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
1980-
return -EINVAL;
1980+
if (flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) {
1981+
if (!tb[CTA_PROTO_SRC_PORT])
1982+
return -EINVAL;
1983+
1984+
t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1985+
}
19811986

1982-
t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1983-
t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1987+
if (flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) {
1988+
if (!tb[CTA_PROTO_DST_PORT])
1989+
return -EINVAL;
1990+
1991+
t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1992+
}
19841993

19851994
return 0;
19861995
}

0 commit comments

Comments
 (0)