Commit 59c08c69 authored by Kristian Evensen's avatar Kristian Evensen Committed by Pablo Neira Ayuso

netfilter: ctnetlink: Support L3 protocol-filter on flush

The same connection mark can be set on flows belonging to different
address families. This commit adds support for filtering on the L3
protocol when flushing connection track entries. If no protocol is
specified, then all L3 protocols match.

In order to avoid code duplication and a redundant check, the protocol
comparison in ctnetlink_dump_table() has been removed. Instead, a filter
is created if the GET-message triggering the dump contains an address
family. ctnetlink_filter_match() is then used to compare the L3
protocols.
Signed-off-by: default avatarKristian Evensen <kristian.evensen@gmail.com>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 6c472602
...@@ -821,6 +821,7 @@ static int ctnetlink_done(struct netlink_callback *cb) ...@@ -821,6 +821,7 @@ static int ctnetlink_done(struct netlink_callback *cb)
} }
struct ctnetlink_filter { struct ctnetlink_filter {
u8 family;
struct { struct {
u_int32_t val; u_int32_t val;
u_int32_t mask; u_int32_t mask;
...@@ -828,31 +829,32 @@ struct ctnetlink_filter { ...@@ -828,31 +829,32 @@ struct ctnetlink_filter {
}; };
static struct ctnetlink_filter * static struct ctnetlink_filter *
ctnetlink_alloc_filter(const struct nlattr * const cda[]) ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
{ {
#ifdef CONFIG_NF_CONNTRACK_MARK
struct ctnetlink_filter *filter; struct ctnetlink_filter *filter;
filter = kzalloc(sizeof(*filter), GFP_KERNEL); filter = kzalloc(sizeof(*filter), GFP_KERNEL);
if (filter == NULL) if (filter == NULL)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
filter->family = family;
#ifdef CONFIG_NF_CONNTRACK_MARK
filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK])); filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
return filter;
#else
return ERR_PTR(-EOPNOTSUPP);
#endif #endif
return filter;
} }
static int ctnetlink_start(struct netlink_callback *cb) static int ctnetlink_start(struct netlink_callback *cb)
{ {
const struct nlattr * const *cda = cb->data; const struct nlattr * const *cda = cb->data;
struct ctnetlink_filter *filter = NULL; struct ctnetlink_filter *filter = NULL;
struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
u8 family = nfmsg->nfgen_family;
if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
filter = ctnetlink_alloc_filter(cda); filter = ctnetlink_alloc_filter(cda, family);
if (IS_ERR(filter)) if (IS_ERR(filter))
return PTR_ERR(filter); return PTR_ERR(filter);
} }
...@@ -866,13 +868,24 @@ static int ctnetlink_filter_match(struct nf_conn *ct, void *data) ...@@ -866,13 +868,24 @@ static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
struct ctnetlink_filter *filter = data; struct ctnetlink_filter *filter = data;
if (filter == NULL) if (filter == NULL)
return 1; goto out;
/* Match entries of a given L3 protocol number.
* If it is not specified, ie. l3proto == 0,
* then match everything.
*/
if (filter->family && nf_ct_l3num(ct) != filter->family)
goto ignore_entry;
#ifdef CONFIG_NF_CONNTRACK_MARK #ifdef CONFIG_NF_CONNTRACK_MARK
if ((ct->mark & filter->mark.mask) == filter->mark.val) if ((ct->mark & filter->mark.mask) != filter->mark.val)
return 1; goto ignore_entry;
#endif #endif
out:
return 1;
ignore_entry:
return 0; return 0;
} }
...@@ -883,8 +896,6 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) ...@@ -883,8 +896,6 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
struct nf_conn *ct, *last; struct nf_conn *ct, *last;
struct nf_conntrack_tuple_hash *h; struct nf_conntrack_tuple_hash *h;
struct hlist_nulls_node *n; struct hlist_nulls_node *n;
struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
u_int8_t l3proto = nfmsg->nfgen_family;
struct nf_conn *nf_ct_evict[8]; struct nf_conn *nf_ct_evict[8];
int res, i; int res, i;
spinlock_t *lockp; spinlock_t *lockp;
...@@ -923,11 +934,6 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) ...@@ -923,11 +934,6 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
if (!net_eq(net, nf_ct_net(ct))) if (!net_eq(net, nf_ct_net(ct)))
continue; continue;
/* Dump entries of a given L3 protocol number.
* If it is not specified, ie. l3proto == 0,
* then dump everything. */
if (l3proto && nf_ct_l3num(ct) != l3proto)
continue;
if (cb->args[1]) { if (cb->args[1]) {
if (ct != last) if (ct != last)
continue; continue;
...@@ -1213,12 +1219,12 @@ static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data) ...@@ -1213,12 +1219,12 @@ static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
static int ctnetlink_flush_conntrack(struct net *net, static int ctnetlink_flush_conntrack(struct net *net,
const struct nlattr * const cda[], const struct nlattr * const cda[],
u32 portid, int report) u32 portid, int report, u8 family)
{ {
struct ctnetlink_filter *filter = NULL; struct ctnetlink_filter *filter = NULL;
if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
filter = ctnetlink_alloc_filter(cda); filter = ctnetlink_alloc_filter(cda, family);
if (IS_ERR(filter)) if (IS_ERR(filter))
return PTR_ERR(filter); return PTR_ERR(filter);
} }
...@@ -1257,7 +1263,7 @@ static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl, ...@@ -1257,7 +1263,7 @@ static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
else { else {
return ctnetlink_flush_conntrack(net, cda, return ctnetlink_flush_conntrack(net, cda,
NETLINK_CB(skb).portid, NETLINK_CB(skb).portid,
nlmsg_report(nlh)); nlmsg_report(nlh), u3);
} }
if (err < 0) if (err < 0)
......
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