Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org> |
| 3 | * (C) 2011 Intra2net AG <http://www.intra2net.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation (or any later at your option). |
| 8 | */ |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/skbuff.h> |
Andrew Morton | 6523cf9 | 2012-03-28 12:10:57 +0000 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 14 | #include <linux/netlink.h> |
| 15 | #include <linux/rculist.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <net/netlink.h> |
| 20 | #include <net/sock.h> |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 21 | |
| 22 | #include <linux/netfilter.h> |
| 23 | #include <linux/netfilter/nfnetlink.h> |
| 24 | #include <linux/netfilter/nfnetlink_acct.h> |
| 25 | |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 28 | MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure"); |
| 29 | |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 30 | struct nf_acct { |
| 31 | atomic64_t pkts; |
| 32 | atomic64_t bytes; |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 33 | unsigned long flags; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 34 | struct list_head head; |
| 35 | atomic_t refcnt; |
| 36 | char name[NFACCT_NAME_MAX]; |
| 37 | struct rcu_head rcu_head; |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 38 | char data[0]; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 41 | struct nfacct_filter { |
| 42 | u32 value; |
| 43 | u32 mask; |
| 44 | }; |
| 45 | |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 46 | #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES) |
Alexey Perevalov | b6d0468 | 2014-07-31 17:14:05 +0400 | [diff] [blame] | 47 | #define NFACCT_OVERQUOTA_BIT 2 /* NFACCT_F_OVERQUOTA */ |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 48 | |
Pablo Neira Ayuso | 7b8002a | 2015-12-15 18:41:56 +0100 | [diff] [blame] | 49 | static int nfnl_acct_new(struct net *net, struct sock *nfnl, |
| 50 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
| 51 | const struct nlattr * const tb[]) |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 52 | { |
| 53 | struct nf_acct *nfacct, *matching = NULL; |
| 54 | char *acct_name; |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 55 | unsigned int size = 0; |
| 56 | u32 flags = 0; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 57 | |
| 58 | if (!tb[NFACCT_NAME]) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | acct_name = nla_data(tb[NFACCT_NAME]); |
Pablo Neira Ayuso | deadcfc | 2013-03-23 16:57:59 +0100 | [diff] [blame] | 62 | if (strlen(acct_name) == 0) |
| 63 | return -EINVAL; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 64 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 65 | list_for_each_entry(nfacct, &net->nfnl_acct_list, head) { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 66 | if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0) |
| 67 | continue; |
| 68 | |
| 69 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 70 | return -EEXIST; |
| 71 | |
| 72 | matching = nfacct; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | if (matching) { |
| 77 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
| 78 | /* reset counters if you request a replacement. */ |
| 79 | atomic64_set(&matching->pkts, 0); |
| 80 | atomic64_set(&matching->bytes, 0); |
Linus Torvalds | f9da455 | 2014-06-12 14:27:40 -0700 | [diff] [blame] | 81 | smp_mb__before_atomic(); |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 82 | /* reset overquota flag if quota is enabled. */ |
| 83 | if ((matching->flags & NFACCT_F_QUOTA)) |
Alexey Perevalov | b6d0468 | 2014-07-31 17:14:05 +0400 | [diff] [blame] | 84 | clear_bit(NFACCT_OVERQUOTA_BIT, |
| 85 | &matching->flags); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | return -EBUSY; |
| 89 | } |
| 90 | |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 91 | if (tb[NFACCT_FLAGS]) { |
| 92 | flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS])); |
| 93 | if (flags & ~NFACCT_F_QUOTA) |
| 94 | return -EOPNOTSUPP; |
| 95 | if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA) |
| 96 | return -EINVAL; |
| 97 | if (flags & NFACCT_F_OVERQUOTA) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | size += sizeof(u64); |
| 101 | } |
| 102 | |
| 103 | nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 104 | if (nfacct == NULL) |
| 105 | return -ENOMEM; |
| 106 | |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 107 | if (flags & NFACCT_F_QUOTA) { |
| 108 | u64 *quota = (u64 *)nfacct->data; |
| 109 | |
| 110 | *quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA])); |
| 111 | nfacct->flags = flags; |
| 112 | } |
| 113 | |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 114 | strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX); |
| 115 | |
| 116 | if (tb[NFACCT_BYTES]) { |
| 117 | atomic64_set(&nfacct->bytes, |
Patrick McHardy | fe31d1a | 2012-08-19 10:16:09 +0000 | [diff] [blame] | 118 | be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES]))); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 119 | } |
| 120 | if (tb[NFACCT_PKTS]) { |
| 121 | atomic64_set(&nfacct->pkts, |
Patrick McHardy | fe31d1a | 2012-08-19 10:16:09 +0000 | [diff] [blame] | 122 | be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS]))); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 123 | } |
| 124 | atomic_set(&nfacct->refcnt, 1); |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 125 | list_add_tail_rcu(&nfacct->head, &net->nfnl_acct_list); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 130 | nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 131 | int event, struct nf_acct *acct) |
| 132 | { |
| 133 | struct nlmsghdr *nlh; |
| 134 | struct nfgenmsg *nfmsg; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 135 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 136 | u64 pkts, bytes; |
Alexey Perevalov | d24675c | 2014-07-30 19:17:55 +0400 | [diff] [blame] | 137 | u32 old_flags; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 138 | |
| 139 | event |= NFNL_SUBSYS_ACCT << 8; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 140 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 141 | if (nlh == NULL) |
| 142 | goto nlmsg_failure; |
| 143 | |
| 144 | nfmsg = nlmsg_data(nlh); |
| 145 | nfmsg->nfgen_family = AF_UNSPEC; |
| 146 | nfmsg->version = NFNETLINK_V0; |
| 147 | nfmsg->res_id = 0; |
| 148 | |
David S. Miller | 7c80118 | 2012-04-01 18:46:29 -0400 | [diff] [blame] | 149 | if (nla_put_string(skb, NFACCT_NAME, acct->name)) |
| 150 | goto nla_put_failure; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 151 | |
Alexey Perevalov | d24675c | 2014-07-30 19:17:55 +0400 | [diff] [blame] | 152 | old_flags = acct->flags; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 153 | if (type == NFNL_MSG_ACCT_GET_CTRZERO) { |
| 154 | pkts = atomic64_xchg(&acct->pkts, 0); |
| 155 | bytes = atomic64_xchg(&acct->bytes, 0); |
Linus Torvalds | f9da455 | 2014-06-12 14:27:40 -0700 | [diff] [blame] | 156 | smp_mb__before_atomic(); |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 157 | if (acct->flags & NFACCT_F_QUOTA) |
Alexey Perevalov | b6d0468 | 2014-07-31 17:14:05 +0400 | [diff] [blame] | 158 | clear_bit(NFACCT_OVERQUOTA_BIT, &acct->flags); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 159 | } else { |
| 160 | pkts = atomic64_read(&acct->pkts); |
| 161 | bytes = atomic64_read(&acct->bytes); |
| 162 | } |
Nicolas Dichtel | b46f6de | 2016-04-22 17:31:18 +0200 | [diff] [blame] | 163 | if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts), |
| 164 | NFACCT_PAD) || |
| 165 | nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes), |
| 166 | NFACCT_PAD) || |
David S. Miller | 7c80118 | 2012-04-01 18:46:29 -0400 | [diff] [blame] | 167 | nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt)))) |
| 168 | goto nla_put_failure; |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 169 | if (acct->flags & NFACCT_F_QUOTA) { |
| 170 | u64 *quota = (u64 *)acct->data; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 171 | |
Alexey Perevalov | d24675c | 2014-07-30 19:17:55 +0400 | [diff] [blame] | 172 | if (nla_put_be32(skb, NFACCT_FLAGS, htonl(old_flags)) || |
Nicolas Dichtel | b46f6de | 2016-04-22 17:31:18 +0200 | [diff] [blame] | 173 | nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota), |
| 174 | NFACCT_PAD)) |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 175 | goto nla_put_failure; |
| 176 | } |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 177 | nlmsg_end(skb, nlh); |
| 178 | return skb->len; |
| 179 | |
| 180 | nlmsg_failure: |
| 181 | nla_put_failure: |
| 182 | nlmsg_cancel(skb, nlh); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | static int |
| 187 | nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 188 | { |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 189 | struct net *net = sock_net(skb->sk); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 190 | struct nf_acct *cur, *last; |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 191 | const struct nfacct_filter *filter = cb->data; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 192 | |
| 193 | if (cb->args[2]) |
| 194 | return 0; |
| 195 | |
| 196 | last = (struct nf_acct *)cb->args[1]; |
| 197 | if (cb->args[1]) |
| 198 | cb->args[1] = 0; |
| 199 | |
| 200 | rcu_read_lock(); |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 201 | list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) { |
Pablo Neira Ayuso | 991a6b7 | 2013-06-01 15:31:40 +0200 | [diff] [blame] | 202 | if (last) { |
| 203 | if (cur != last) |
| 204 | continue; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 205 | |
Pablo Neira Ayuso | 991a6b7 | 2013-06-01 15:31:40 +0200 | [diff] [blame] | 206 | last = NULL; |
| 207 | } |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 208 | |
| 209 | if (filter && (cur->flags & filter->mask) != filter->value) |
| 210 | continue; |
| 211 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 212 | if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid, |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 213 | cb->nlh->nlmsg_seq, |
| 214 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), |
| 215 | NFNL_MSG_ACCT_NEW, cur) < 0) { |
| 216 | cb->args[1] = (unsigned long)cur; |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | if (!cb->args[1]) |
| 221 | cb->args[2] = 1; |
| 222 | rcu_read_unlock(); |
| 223 | return skb->len; |
| 224 | } |
| 225 | |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 226 | static int nfnl_acct_done(struct netlink_callback *cb) |
| 227 | { |
| 228 | kfree(cb->data); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static const struct nla_policy filter_policy[NFACCT_FILTER_MAX + 1] = { |
| 233 | [NFACCT_FILTER_MASK] = { .type = NLA_U32 }, |
| 234 | [NFACCT_FILTER_VALUE] = { .type = NLA_U32 }, |
| 235 | }; |
| 236 | |
| 237 | static struct nfacct_filter * |
| 238 | nfacct_filter_alloc(const struct nlattr * const attr) |
| 239 | { |
| 240 | struct nfacct_filter *filter; |
| 241 | struct nlattr *tb[NFACCT_FILTER_MAX + 1]; |
| 242 | int err; |
| 243 | |
| 244 | err = nla_parse_nested(tb, NFACCT_FILTER_MAX, attr, filter_policy); |
| 245 | if (err < 0) |
| 246 | return ERR_PTR(err); |
| 247 | |
Phil Turnbull | 017b1b6 | 2016-02-24 15:34:43 -0500 | [diff] [blame] | 248 | if (!tb[NFACCT_FILTER_MASK] || !tb[NFACCT_FILTER_VALUE]) |
| 249 | return ERR_PTR(-EINVAL); |
| 250 | |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 251 | filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL); |
| 252 | if (!filter) |
| 253 | return ERR_PTR(-ENOMEM); |
| 254 | |
| 255 | filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK])); |
| 256 | filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE])); |
| 257 | |
| 258 | return filter; |
| 259 | } |
| 260 | |
Pablo Neira Ayuso | 7b8002a | 2015-12-15 18:41:56 +0100 | [diff] [blame] | 261 | static int nfnl_acct_get(struct net *net, struct sock *nfnl, |
| 262 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
| 263 | const struct nlattr * const tb[]) |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 264 | { |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 265 | int ret = -ENOENT; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 266 | struct nf_acct *cur; |
| 267 | char *acct_name; |
| 268 | |
| 269 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 270 | struct netlink_dump_control c = { |
| 271 | .dump = nfnl_acct_dump, |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 272 | .done = nfnl_acct_done, |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 273 | }; |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 274 | |
| 275 | if (tb[NFACCT_FILTER]) { |
| 276 | struct nfacct_filter *filter; |
| 277 | |
| 278 | filter = nfacct_filter_alloc(tb[NFACCT_FILTER]); |
| 279 | if (IS_ERR(filter)) |
| 280 | return PTR_ERR(filter); |
| 281 | |
| 282 | c.data = filter; |
| 283 | } |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 284 | return netlink_dump_start(nfnl, skb, nlh, &c); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (!tb[NFACCT_NAME]) |
| 288 | return -EINVAL; |
| 289 | acct_name = nla_data(tb[NFACCT_NAME]); |
| 290 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 291 | list_for_each_entry(cur, &net->nfnl_acct_list, head) { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 292 | struct sk_buff *skb2; |
| 293 | |
| 294 | if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0) |
| 295 | continue; |
| 296 | |
| 297 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 298 | if (skb2 == NULL) { |
| 299 | ret = -ENOMEM; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 300 | break; |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 301 | } |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 302 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 303 | ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid, |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 304 | nlh->nlmsg_seq, |
| 305 | NFNL_MSG_TYPE(nlh->nlmsg_type), |
| 306 | NFNL_MSG_ACCT_NEW, cur); |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 307 | if (ret <= 0) { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 308 | kfree_skb(skb2); |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 309 | break; |
| 310 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 311 | ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid, |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 312 | MSG_DONTWAIT); |
| 313 | if (ret > 0) |
| 314 | ret = 0; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 315 | |
Pablo Neira Ayuso | 3ab0b24 | 2011-12-30 20:32:26 +0100 | [diff] [blame] | 316 | /* this avoids a loop in nfnetlink. */ |
| 317 | return ret == -EAGAIN ? -ENOBUFS : ret; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 318 | } |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* try to delete object, fail if it is still in use. */ |
| 323 | static int nfnl_acct_try_del(struct nf_acct *cur) |
| 324 | { |
| 325 | int ret = 0; |
| 326 | |
| 327 | /* we want to avoid races with nfnl_acct_find_get. */ |
| 328 | if (atomic_dec_and_test(&cur->refcnt)) { |
| 329 | /* We are protected by nfnl mutex. */ |
| 330 | list_del_rcu(&cur->head); |
| 331 | kfree_rcu(cur, rcu_head); |
| 332 | } else { |
| 333 | /* still in use, restore reference counter. */ |
| 334 | atomic_inc(&cur->refcnt); |
| 335 | ret = -EBUSY; |
| 336 | } |
| 337 | return ret; |
| 338 | } |
| 339 | |
Pablo Neira Ayuso | 7b8002a | 2015-12-15 18:41:56 +0100 | [diff] [blame] | 340 | static int nfnl_acct_del(struct net *net, struct sock *nfnl, |
| 341 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
| 342 | const struct nlattr * const tb[]) |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 343 | { |
| 344 | char *acct_name; |
| 345 | struct nf_acct *cur; |
| 346 | int ret = -ENOENT; |
| 347 | |
| 348 | if (!tb[NFACCT_NAME]) { |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 349 | list_for_each_entry(cur, &net->nfnl_acct_list, head) |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 350 | nfnl_acct_try_del(cur); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | acct_name = nla_data(tb[NFACCT_NAME]); |
| 355 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 356 | list_for_each_entry(cur, &net->nfnl_acct_list, head) { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 357 | if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0) |
| 358 | continue; |
| 359 | |
| 360 | ret = nfnl_acct_try_del(cur); |
| 361 | if (ret < 0) |
| 362 | return ret; |
| 363 | |
| 364 | break; |
| 365 | } |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = { |
| 370 | [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 }, |
| 371 | [NFACCT_BYTES] = { .type = NLA_U64 }, |
| 372 | [NFACCT_PKTS] = { .type = NLA_U64 }, |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 373 | [NFACCT_FLAGS] = { .type = NLA_U32 }, |
| 374 | [NFACCT_QUOTA] = { .type = NLA_U64 }, |
Alexey Perevalov | f111f78 | 2014-08-20 22:03:18 +0400 | [diff] [blame] | 375 | [NFACCT_FILTER] = {.type = NLA_NESTED }, |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = { |
| 379 | [NFNL_MSG_ACCT_NEW] = { .call = nfnl_acct_new, |
| 380 | .attr_count = NFACCT_MAX, |
| 381 | .policy = nfnl_acct_policy }, |
| 382 | [NFNL_MSG_ACCT_GET] = { .call = nfnl_acct_get, |
| 383 | .attr_count = NFACCT_MAX, |
| 384 | .policy = nfnl_acct_policy }, |
| 385 | [NFNL_MSG_ACCT_GET_CTRZERO] = { .call = nfnl_acct_get, |
| 386 | .attr_count = NFACCT_MAX, |
| 387 | .policy = nfnl_acct_policy }, |
| 388 | [NFNL_MSG_ACCT_DEL] = { .call = nfnl_acct_del, |
| 389 | .attr_count = NFACCT_MAX, |
| 390 | .policy = nfnl_acct_policy }, |
| 391 | }; |
| 392 | |
| 393 | static const struct nfnetlink_subsystem nfnl_acct_subsys = { |
| 394 | .name = "acct", |
| 395 | .subsys_id = NFNL_SUBSYS_ACCT, |
| 396 | .cb_count = NFNL_MSG_ACCT_MAX, |
| 397 | .cb = nfnl_acct_cb, |
| 398 | }; |
| 399 | |
| 400 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT); |
| 401 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 402 | struct nf_acct *nfnl_acct_find_get(struct net *net, const char *acct_name) |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 403 | { |
| 404 | struct nf_acct *cur, *acct = NULL; |
| 405 | |
| 406 | rcu_read_lock(); |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 407 | list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 408 | if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0) |
| 409 | continue; |
| 410 | |
| 411 | if (!try_module_get(THIS_MODULE)) |
| 412 | goto err; |
| 413 | |
| 414 | if (!atomic_inc_not_zero(&cur->refcnt)) { |
| 415 | module_put(THIS_MODULE); |
| 416 | goto err; |
| 417 | } |
| 418 | |
| 419 | acct = cur; |
| 420 | break; |
| 421 | } |
| 422 | err: |
| 423 | rcu_read_unlock(); |
| 424 | return acct; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(nfnl_acct_find_get); |
| 427 | |
| 428 | void nfnl_acct_put(struct nf_acct *acct) |
| 429 | { |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 430 | if (atomic_dec_and_test(&acct->refcnt)) |
| 431 | kfree_rcu(acct, rcu_head); |
| 432 | |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 433 | module_put(THIS_MODULE); |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(nfnl_acct_put); |
| 436 | |
| 437 | void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct) |
| 438 | { |
| 439 | atomic64_inc(&nfacct->pkts); |
| 440 | atomic64_add(skb->len, &nfacct->bytes); |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(nfnl_acct_update); |
| 443 | |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 444 | static void nfnl_overquota_report(struct nf_acct *nfacct) |
| 445 | { |
| 446 | int ret; |
| 447 | struct sk_buff *skb; |
| 448 | |
| 449 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 450 | if (skb == NULL) |
| 451 | return; |
| 452 | |
| 453 | ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0, |
| 454 | nfacct); |
| 455 | if (ret <= 0) { |
| 456 | kfree_skb(skb); |
| 457 | return; |
| 458 | } |
| 459 | netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA, |
| 460 | GFP_ATOMIC); |
| 461 | } |
| 462 | |
| 463 | int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) |
| 464 | { |
| 465 | u64 now; |
| 466 | u64 *quota; |
| 467 | int ret = NFACCT_UNDERQUOTA; |
| 468 | |
| 469 | /* no place here if we don't have a quota */ |
| 470 | if (!(nfacct->flags & NFACCT_F_QUOTA)) |
| 471 | return NFACCT_NO_QUOTA; |
| 472 | |
| 473 | quota = (u64 *)nfacct->data; |
| 474 | now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ? |
| 475 | atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes); |
| 476 | |
| 477 | ret = now > *quota; |
| 478 | |
| 479 | if (now >= *quota && |
Alexey Perevalov | b6d0468 | 2014-07-31 17:14:05 +0400 | [diff] [blame] | 480 | !test_and_set_bit(NFACCT_OVERQUOTA_BIT, &nfacct->flags)) { |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 481 | nfnl_overquota_report(nfacct); |
| 482 | } |
| 483 | |
| 484 | return ret; |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(nfnl_acct_overquota); |
| 487 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 488 | static int __net_init nfnl_acct_net_init(struct net *net) |
| 489 | { |
| 490 | INIT_LIST_HEAD(&net->nfnl_acct_list); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static void __net_exit nfnl_acct_net_exit(struct net *net) |
| 496 | { |
| 497 | struct nf_acct *cur, *tmp; |
| 498 | |
| 499 | list_for_each_entry_safe(cur, tmp, &net->nfnl_acct_list, head) { |
| 500 | list_del_rcu(&cur->head); |
| 501 | |
| 502 | if (atomic_dec_and_test(&cur->refcnt)) |
| 503 | kfree_rcu(cur, rcu_head); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | static struct pernet_operations nfnl_acct_ops = { |
| 508 | .init = nfnl_acct_net_init, |
| 509 | .exit = nfnl_acct_net_exit, |
| 510 | }; |
| 511 | |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 512 | static int __init nfnl_acct_init(void) |
| 513 | { |
| 514 | int ret; |
| 515 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 516 | ret = register_pernet_subsys(&nfnl_acct_ops); |
| 517 | if (ret < 0) { |
| 518 | pr_err("nfnl_acct_init: failed to register pernet ops\n"); |
| 519 | goto err_out; |
| 520 | } |
| 521 | |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 522 | pr_info("nfnl_acct: registering with nfnetlink.\n"); |
| 523 | ret = nfnetlink_subsys_register(&nfnl_acct_subsys); |
| 524 | if (ret < 0) { |
| 525 | pr_err("nfnl_acct_init: cannot register with nfnetlink.\n"); |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 526 | goto cleanup_pernet; |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 527 | } |
| 528 | return 0; |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 529 | |
| 530 | cleanup_pernet: |
| 531 | unregister_pernet_subsys(&nfnl_acct_ops); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 532 | err_out: |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static void __exit nfnl_acct_exit(void) |
| 537 | { |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 538 | pr_info("nfnl_acct: unregistering from nfnetlink.\n"); |
| 539 | nfnetlink_subsys_unregister(&nfnl_acct_subsys); |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 540 | unregister_pernet_subsys(&nfnl_acct_ops); |
Pablo Neira Ayuso | 9413902 | 2011-12-23 14:19:50 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | module_init(nfnl_acct_init); |
| 544 | module_exit(nfnl_acct_exit); |