Commit 90d0e08e authored by David S. Miller's avatar David S. Miller

Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next

Pablo Neira Ayuso says:

====================
Netfilter/IPVS updates for net-next

This small patchset contains three accumulated Netfilter/IPVS updates,
they are:

1) Refactorize common NAT code by encapsulating it into a helper
   function, similarly to what we do in other conntrack extensions,
   from Florian Westphal.

2) A minor format string mismatch fix for IPVS, from Masanari Iida.

3) Add quota support to the netfilter accounting infrastructure, now
   you can add quotas to accounting objects via the nfnetlink interface
   and use them from iptables. You can also listen to quota
   notifications from userspace. This enhancement from Mathieu Poirier.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 648d4feb f768e5bd
...@@ -3,11 +3,17 @@ ...@@ -3,11 +3,17 @@
#include <uapi/linux/netfilter/nfnetlink_acct.h> #include <uapi/linux/netfilter/nfnetlink_acct.h>
enum {
NFACCT_NO_QUOTA = -1,
NFACCT_UNDERQUOTA,
NFACCT_OVERQUOTA,
};
struct nf_acct; struct nf_acct;
struct nf_acct *nfnl_acct_find_get(const char *filter_name); struct nf_acct *nfnl_acct_find_get(const char *filter_name);
void nfnl_acct_put(struct nf_acct *acct); void nfnl_acct_put(struct nf_acct *acct);
void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct); void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct);
extern int nfnl_acct_overquota(const struct sk_buff *skb,
struct nf_acct *nfacct);
#endif /* _NFNL_ACCT_H */ #endif /* _NFNL_ACCT_H */
...@@ -48,6 +48,8 @@ unsigned int nf_nat_setup_info(struct nf_conn *ct, ...@@ -48,6 +48,8 @@ unsigned int nf_nat_setup_info(struct nf_conn *ct,
extern unsigned int nf_nat_alloc_null_binding(struct nf_conn *ct, extern unsigned int nf_nat_alloc_null_binding(struct nf_conn *ct,
unsigned int hooknum); unsigned int hooknum);
struct nf_conn_nat *nf_ct_nat_ext_add(struct nf_conn *ct);
/* Is this tuple already taken? (not by us)*/ /* Is this tuple already taken? (not by us)*/
int nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple, int nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple,
const struct nf_conn *ignored_conntrack); const struct nf_conn *ignored_conntrack);
......
...@@ -20,6 +20,8 @@ enum nfnetlink_groups { ...@@ -20,6 +20,8 @@ enum nfnetlink_groups {
#define NFNLGRP_CONNTRACK_EXP_DESTROY NFNLGRP_CONNTRACK_EXP_DESTROY #define NFNLGRP_CONNTRACK_EXP_DESTROY NFNLGRP_CONNTRACK_EXP_DESTROY
NFNLGRP_NFTABLES, NFNLGRP_NFTABLES,
#define NFNLGRP_NFTABLES NFNLGRP_NFTABLES #define NFNLGRP_NFTABLES NFNLGRP_NFTABLES
NFNLGRP_ACCT_QUOTA,
#define NFNLGRP_ACCT_QUOTA NFNLGRP_ACCT_QUOTA
__NFNLGRP_MAX, __NFNLGRP_MAX,
}; };
#define NFNLGRP_MAX (__NFNLGRP_MAX - 1) #define NFNLGRP_MAX (__NFNLGRP_MAX - 1)
......
...@@ -10,15 +10,24 @@ enum nfnl_acct_msg_types { ...@@ -10,15 +10,24 @@ enum nfnl_acct_msg_types {
NFNL_MSG_ACCT_GET, NFNL_MSG_ACCT_GET,
NFNL_MSG_ACCT_GET_CTRZERO, NFNL_MSG_ACCT_GET_CTRZERO,
NFNL_MSG_ACCT_DEL, NFNL_MSG_ACCT_DEL,
NFNL_MSG_ACCT_OVERQUOTA,
NFNL_MSG_ACCT_MAX NFNL_MSG_ACCT_MAX
}; };
enum nfnl_acct_flags {
NFACCT_F_QUOTA_PKTS = (1 << 0),
NFACCT_F_QUOTA_BYTES = (1 << 1),
NFACCT_F_OVERQUOTA = (1 << 2), /* can't be set from userspace */
};
enum nfnl_acct_type { enum nfnl_acct_type {
NFACCT_UNSPEC, NFACCT_UNSPEC,
NFACCT_NAME, NFACCT_NAME,
NFACCT_PKTS, NFACCT_PKTS,
NFACCT_BYTES, NFACCT_BYTES,
NFACCT_USE, NFACCT_USE,
NFACCT_FLAGS,
NFACCT_QUOTA,
__NFACCT_MAX __NFACCT_MAX
}; };
#define NFACCT_MAX (__NFACCT_MAX - 1) #define NFACCT_MAX (__NFACCT_MAX - 1)
......
...@@ -91,17 +91,9 @@ nf_nat_ipv4_fn(const struct nf_hook_ops *ops, ...@@ -91,17 +91,9 @@ nf_nat_ipv4_fn(const struct nf_hook_ops *ops,
if (nf_ct_is_untracked(ct)) if (nf_ct_is_untracked(ct))
return NF_ACCEPT; return NF_ACCEPT;
nat = nfct_nat(ct); nat = nf_ct_nat_ext_add(ct);
if (!nat) { if (nat == NULL)
/* NAT module was loaded late. */ return NF_ACCEPT;
if (nf_ct_is_confirmed(ct))
return NF_ACCEPT;
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
if (nat == NULL) {
pr_debug("failed to add NAT extension\n");
return NF_ACCEPT;
}
}
switch (ctinfo) { switch (ctinfo) {
case IP_CT_RELATED: case IP_CT_RELATED:
......
...@@ -48,15 +48,9 @@ static unsigned int nf_nat_fn(const struct nf_hook_ops *ops, ...@@ -48,15 +48,9 @@ static unsigned int nf_nat_fn(const struct nf_hook_ops *ops,
NF_CT_ASSERT(!(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET))); NF_CT_ASSERT(!(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)));
nat = nfct_nat(ct); nat = nf_ct_nat_ext_add(ct);
if (nat == NULL) { if (nat == NULL)
/* Conntrack module was loaded late, can't add extension. */ return NF_ACCEPT;
if (nf_ct_is_confirmed(ct))
return NF_ACCEPT;
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
if (nat == NULL)
return NF_ACCEPT;
}
switch (ctinfo) { switch (ctinfo) {
case IP_CT_RELATED: case IP_CT_RELATED:
......
...@@ -90,17 +90,9 @@ nf_nat_ipv6_fn(const struct nf_hook_ops *ops, ...@@ -90,17 +90,9 @@ nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
if (nf_ct_is_untracked(ct)) if (nf_ct_is_untracked(ct))
return NF_ACCEPT; return NF_ACCEPT;
nat = nfct_nat(ct); nat = nf_ct_nat_ext_add(ct);
if (!nat) { if (nat == NULL)
/* NAT module was loaded late. */ return NF_ACCEPT;
if (nf_ct_is_confirmed(ct))
return NF_ACCEPT;
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
if (nat == NULL) {
pr_debug("failed to add NAT extension\n");
return NF_ACCEPT;
}
}
switch (ctinfo) { switch (ctinfo) {
case IP_CT_RELATED: case IP_CT_RELATED:
......
...@@ -47,15 +47,9 @@ static unsigned int nf_nat_ipv6_fn(const struct nf_hook_ops *ops, ...@@ -47,15 +47,9 @@ static unsigned int nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
if (ct == NULL || nf_ct_is_untracked(ct)) if (ct == NULL || nf_ct_is_untracked(ct))
return NF_ACCEPT; return NF_ACCEPT;
nat = nfct_nat(ct); nat = nf_ct_nat_ext_add(ct);
if (nat == NULL) { if (nat == NULL)
/* Conntrack module was loaded late, can't add extension. */ return NF_ACCEPT;
if (nf_ct_is_confirmed(ct))
return NF_ACCEPT;
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
if (nat == NULL)
return NF_ACCEPT;
}
switch (ctinfo) { switch (ctinfo) {
case IP_CT_RELATED: case IP_CT_RELATED:
......
...@@ -97,7 +97,7 @@ const char *ip_vs_proto_name(unsigned int proto) ...@@ -97,7 +97,7 @@ const char *ip_vs_proto_name(unsigned int proto)
return "ICMPv6"; return "ICMPv6";
#endif #endif
default: default:
sprintf(buf, "IP_%d", proto); sprintf(buf, "IP_%u", proto);
return buf; return buf;
} }
} }
......
...@@ -358,6 +358,19 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple, ...@@ -358,6 +358,19 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,
rcu_read_unlock(); rcu_read_unlock();
} }
struct nf_conn_nat *nf_ct_nat_ext_add(struct nf_conn *ct)
{
struct nf_conn_nat *nat = nfct_nat(ct);
if (nat)
return nat;
if (!nf_ct_is_confirmed(ct))
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
return nat;
}
EXPORT_SYMBOL_GPL(nf_ct_nat_ext_add);
unsigned int unsigned int
nf_nat_setup_info(struct nf_conn *ct, nf_nat_setup_info(struct nf_conn *ct,
const struct nf_nat_range *range, const struct nf_nat_range *range,
...@@ -368,14 +381,9 @@ nf_nat_setup_info(struct nf_conn *ct, ...@@ -368,14 +381,9 @@ nf_nat_setup_info(struct nf_conn *ct,
struct nf_conn_nat *nat; struct nf_conn_nat *nat;
/* nat helper or nfctnetlink also setup binding */ /* nat helper or nfctnetlink also setup binding */
nat = nfct_nat(ct); nat = nf_ct_nat_ext_add(ct);
if (!nat) { if (nat == NULL)
nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC); return NF_ACCEPT;
if (nat == NULL) {
pr_debug("failed to add NAT extension\n");
return NF_ACCEPT;
}
}
NF_CT_ASSERT(maniptype == NF_NAT_MANIP_SRC || NF_CT_ASSERT(maniptype == NF_NAT_MANIP_SRC ||
maniptype == NF_NAT_MANIP_DST); maniptype == NF_NAT_MANIP_DST);
......
...@@ -32,18 +32,24 @@ static LIST_HEAD(nfnl_acct_list); ...@@ -32,18 +32,24 @@ static LIST_HEAD(nfnl_acct_list);
struct nf_acct { struct nf_acct {
atomic64_t pkts; atomic64_t pkts;
atomic64_t bytes; atomic64_t bytes;
unsigned long flags;
struct list_head head; struct list_head head;
atomic_t refcnt; atomic_t refcnt;
char name[NFACCT_NAME_MAX]; char name[NFACCT_NAME_MAX];
struct rcu_head rcu_head; struct rcu_head rcu_head;
char data[0];
}; };
#define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
static int static int
nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb, nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
const struct nlmsghdr *nlh, const struct nlattr * const tb[]) const struct nlmsghdr *nlh, const struct nlattr * const tb[])
{ {
struct nf_acct *nfacct, *matching = NULL; struct nf_acct *nfacct, *matching = NULL;
char *acct_name; char *acct_name;
unsigned int size = 0;
u32 flags = 0;
if (!tb[NFACCT_NAME]) if (!tb[NFACCT_NAME])
return -EINVAL; return -EINVAL;
...@@ -68,15 +74,39 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb, ...@@ -68,15 +74,39 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
/* reset counters if you request a replacement. */ /* reset counters if you request a replacement. */
atomic64_set(&matching->pkts, 0); atomic64_set(&matching->pkts, 0);
atomic64_set(&matching->bytes, 0); atomic64_set(&matching->bytes, 0);
smp_mb__before_clear_bit();
/* reset overquota flag if quota is enabled. */
if ((matching->flags & NFACCT_F_QUOTA))
clear_bit(NFACCT_F_OVERQUOTA, &matching->flags);
return 0; return 0;
} }
return -EBUSY; return -EBUSY;
} }
nfacct = kzalloc(sizeof(struct nf_acct), GFP_KERNEL); nfacct = kzalloc(sizeof(struct nf_acct), GFP_KERNEL);
if (tb[NFACCT_FLAGS]) {
flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
if (flags & ~NFACCT_F_QUOTA)
return -EOPNOTSUPP;
if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
return -EINVAL;
if (flags & NFACCT_F_OVERQUOTA)
return -EINVAL;
size += sizeof(u64);
}
nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
if (nfacct == NULL) if (nfacct == NULL)
return -ENOMEM; return -ENOMEM;
if (flags & NFACCT_F_QUOTA) {
u64 *quota = (u64 *)nfacct->data;
*quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
nfacct->flags = flags;
}
strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX); strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX);
if (tb[NFACCT_BYTES]) { if (tb[NFACCT_BYTES]) {
...@@ -117,6 +147,9 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, ...@@ -117,6 +147,9 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
if (type == NFNL_MSG_ACCT_GET_CTRZERO) { if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
pkts = atomic64_xchg(&acct->pkts, 0); pkts = atomic64_xchg(&acct->pkts, 0);
bytes = atomic64_xchg(&acct->bytes, 0); bytes = atomic64_xchg(&acct->bytes, 0);
smp_mb__before_clear_bit();
if (acct->flags & NFACCT_F_QUOTA)
clear_bit(NFACCT_F_OVERQUOTA, &acct->flags);
} else { } else {
pkts = atomic64_read(&acct->pkts); pkts = atomic64_read(&acct->pkts);
bytes = atomic64_read(&acct->bytes); bytes = atomic64_read(&acct->bytes);
...@@ -125,7 +158,13 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, ...@@ -125,7 +158,13 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) || nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) ||
nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt)))) nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt))))
goto nla_put_failure; goto nla_put_failure;
if (acct->flags & NFACCT_F_QUOTA) {
u64 *quota = (u64 *)acct->data;
if (nla_put_be32(skb, NFACCT_FLAGS, htonl(acct->flags)) ||
nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota)))
goto nla_put_failure;
}
nlmsg_end(skb, nlh); nlmsg_end(skb, nlh);
return skb->len; return skb->len;
...@@ -270,6 +309,8 @@ static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = { ...@@ -270,6 +309,8 @@ static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
[NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 }, [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
[NFACCT_BYTES] = { .type = NLA_U64 }, [NFACCT_BYTES] = { .type = NLA_U64 },
[NFACCT_PKTS] = { .type = NLA_U64 }, [NFACCT_PKTS] = { .type = NLA_U64 },
[NFACCT_FLAGS] = { .type = NLA_U32 },
[NFACCT_QUOTA] = { .type = NLA_U64 },
}; };
static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = { static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
...@@ -336,6 +377,50 @@ void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct) ...@@ -336,6 +377,50 @@ void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
} }
EXPORT_SYMBOL_GPL(nfnl_acct_update); EXPORT_SYMBOL_GPL(nfnl_acct_update);
static void nfnl_overquota_report(struct nf_acct *nfacct)
{
int ret;
struct sk_buff *skb;
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
if (skb == NULL)
return;
ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
nfacct);
if (ret <= 0) {
kfree_skb(skb);
return;
}
netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
GFP_ATOMIC);
}
int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
{
u64 now;
u64 *quota;
int ret = NFACCT_UNDERQUOTA;
/* no place here if we don't have a quota */
if (!(nfacct->flags & NFACCT_F_QUOTA))
return NFACCT_NO_QUOTA;
quota = (u64 *)nfacct->data;
now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
ret = now > *quota;
if (now >= *quota &&
!test_and_set_bit(NFACCT_F_OVERQUOTA, &nfacct->flags)) {
nfnl_overquota_report(nfacct);
}
return ret;
}
EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
static int __init nfnl_acct_init(void) static int __init nfnl_acct_init(void)
{ {
int ret; int ret;
......
...@@ -21,11 +21,14 @@ MODULE_ALIAS("ip6t_nfacct"); ...@@ -21,11 +21,14 @@ MODULE_ALIAS("ip6t_nfacct");
static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par) static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par)
{ {
int overquota;
const struct xt_nfacct_match_info *info = par->targinfo; const struct xt_nfacct_match_info *info = par->targinfo;
nfnl_acct_update(skb, info->nfacct); nfnl_acct_update(skb, info->nfacct);
return true; overquota = nfnl_acct_overquota(skb, info->nfacct);
return overquota == NFACCT_UNDERQUOTA ? false : true;
} }
static int static int
......
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