nfnetlink_acct.c 13 KB

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