Commit 017b1b6d28c479f1ad9a7a41f775545a3e1cba35

Authored by Phil Turnbull
Committed by Pablo Neira Ayuso
1 parent 472681d57a

netfilter: nfnetlink_acct: validate NFACCT_FILTER parameters

nfacct_filter_alloc doesn't validate the NFACCT_FILTER_MASK and
NFACCT_FILTER_VALUE parameters which can trigger a NULL pointer
dereference. CAP_NET_ADMIN is required to trigger the bug.

Signed-off-by: Phil Turnbull <phil.turnbull@oracle.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

Showing 1 changed file with 3 additions and 0 deletions Inline Diff

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