xt_quota2.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * xt_quota2 - enhanced xt_quota that can count upwards and in packets
  3. * as a minimal accounting match.
  4. * by Jan Engelhardt <jengelh@medozas.de>, 2008
  5. *
  6. * Originally based on xt_quota.c:
  7. * netfilter module to enforce network quotas
  8. * Sam Johnston <samj@samj.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License; either
  12. * version 2 of the License, as published by the Free Software Foundation.
  13. */
  14. #include <linux/list.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/spinlock.h>
  18. #include <asm/atomic.h>
  19. #include <linux/netfilter/x_tables.h>
  20. #include <linux/netfilter/xt_quota2.h>
  21. #ifdef CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG
  22. #include <linux/netfilter_ipv4/ipt_ULOG.h>
  23. #endif
  24. /**
  25. * @lock: lock to protect quota writers from each other
  26. */
  27. struct xt_quota_counter {
  28. u_int64_t quota;
  29. spinlock_t lock;
  30. struct list_head list;
  31. atomic_t ref;
  32. char name[sizeof(((struct xt_quota_mtinfo2 *)NULL)->name)];
  33. struct proc_dir_entry *procfs_entry;
  34. };
  35. #ifdef CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG
  36. /* Harald's favorite number +1 :D From ipt_ULOG.C */
  37. static int qlog_nl_event = 112;
  38. module_param_named(event_num, qlog_nl_event, uint, S_IRUGO | S_IWUSR);
  39. MODULE_PARM_DESC(event_num,
  40. "Event number for NETLINK_NFLOG message. 0 disables log."
  41. "111 is what ipt_ULOG uses.");
  42. static struct sock *nflognl;
  43. #endif
  44. static LIST_HEAD(counter_list);
  45. static DEFINE_SPINLOCK(counter_list_lock);
  46. static struct proc_dir_entry *proc_xt_quota;
  47. static unsigned int quota_list_perms = S_IRUGO | S_IWUSR;
  48. static unsigned int quota_list_uid = 0;
  49. static unsigned int quota_list_gid = 0;
  50. module_param_named(perms, quota_list_perms, uint, S_IRUGO | S_IWUSR);
  51. module_param_named(uid, quota_list_uid, uint, S_IRUGO | S_IWUSR);
  52. module_param_named(gid, quota_list_gid, uint, S_IRUGO | S_IWUSR);
  53. #ifdef CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG
  54. static void quota2_log(unsigned int hooknum,
  55. const struct sk_buff *skb,
  56. const struct net_device *in,
  57. const struct net_device *out,
  58. const char *prefix)
  59. {
  60. ulog_packet_msg_t *pm;
  61. struct sk_buff *log_skb;
  62. size_t size;
  63. struct nlmsghdr *nlh;
  64. if (!qlog_nl_event)
  65. return;
  66. size = NLMSG_SPACE(sizeof(*pm));
  67. size = max(size, (size_t)NLMSG_GOODSIZE);
  68. log_skb = alloc_skb(size, GFP_ATOMIC);
  69. if (!log_skb) {
  70. pr_err("xt_quota2: cannot alloc skb for logging\n");
  71. return;
  72. }
  73. /* NLMSG_PUT() uses "goto nlmsg_failure" */
  74. nlh = NLMSG_PUT(log_skb, /*pid*/0, /*seq*/0, qlog_nl_event,
  75. sizeof(*pm));
  76. pm = NLMSG_DATA(nlh);
  77. if (skb->tstamp.tv64 == 0)
  78. __net_timestamp((struct sk_buff *)skb);
  79. pm->data_len = 0;
  80. pm->hook = hooknum;
  81. if (prefix != NULL)
  82. strlcpy(pm->prefix, prefix, sizeof(pm->prefix));
  83. else
  84. *(pm->prefix) = '\0';
  85. if (in)
  86. strlcpy(pm->indev_name, in->name, sizeof(pm->indev_name));
  87. else
  88. pm->indev_name[0] = '\0';
  89. if (out)
  90. strlcpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
  91. else
  92. pm->outdev_name[0] = '\0';
  93. NETLINK_CB(log_skb).dst_group = 1;
  94. pr_debug("throwing 1 packets to netlink group 1\n");
  95. netlink_broadcast(nflognl, log_skb, 0, 1, GFP_ATOMIC);
  96. nlmsg_failure: /* Used within NLMSG_PUT() */
  97. pr_debug("xt_quota2: error during NLMSG_PUT\n");
  98. }
  99. #else
  100. static void quota2_log(unsigned int hooknum,
  101. const struct sk_buff *skb,
  102. const struct net_device *in,
  103. const struct net_device *out,
  104. const char *prefix)
  105. {
  106. }
  107. #endif /* if+else CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG */
  108. static int quota_proc_read(char *page, char **start, off_t offset,
  109. int count, int *eof, void *data)
  110. {
  111. struct xt_quota_counter *e = data;
  112. int ret;
  113. spin_lock_bh(&e->lock);
  114. ret = snprintf(page, PAGE_SIZE, "%llu\n", e->quota);
  115. spin_unlock_bh(&e->lock);
  116. return ret;
  117. }
  118. static int quota_proc_write(struct file *file, const char __user *input,
  119. unsigned long size, void *data)
  120. {
  121. struct xt_quota_counter *e = data;
  122. char buf[sizeof("18446744073709551616")];
  123. if (size > sizeof(buf))
  124. size = sizeof(buf);
  125. if (copy_from_user(buf, input, size) != 0)
  126. return -EFAULT;
  127. buf[sizeof(buf)-1] = '\0';
  128. spin_lock_bh(&e->lock);
  129. e->quota = simple_strtoull(buf, NULL, 0);
  130. spin_unlock_bh(&e->lock);
  131. return size;
  132. }
  133. static struct xt_quota_counter *
  134. q2_new_counter(const struct xt_quota_mtinfo2 *q, bool anon)
  135. {
  136. struct xt_quota_counter *e;
  137. unsigned int size;
  138. /* Do not need all the procfs things for anonymous counters. */
  139. size = anon ? offsetof(typeof(*e), list) : sizeof(*e);
  140. e = kmalloc(size, GFP_KERNEL);
  141. if (e == NULL)
  142. return NULL;
  143. e->quota = q->quota;
  144. spin_lock_init(&e->lock);
  145. if (!anon) {
  146. INIT_LIST_HEAD(&e->list);
  147. atomic_set(&e->ref, 1);
  148. strlcpy(e->name, q->name, sizeof(e->name));
  149. }
  150. return e;
  151. }
  152. /**
  153. * q2_get_counter - get ref to counter or create new
  154. * @name: name of counter
  155. */
  156. static struct xt_quota_counter *
  157. q2_get_counter(const struct xt_quota_mtinfo2 *q)
  158. {
  159. struct proc_dir_entry *p;
  160. struct xt_quota_counter *e = NULL;
  161. struct xt_quota_counter *new_e;
  162. if (*q->name == '\0')
  163. return q2_new_counter(q, true);
  164. /* No need to hold a lock while getting a new counter */
  165. new_e = q2_new_counter(q, false);
  166. if (new_e == NULL)
  167. goto out;
  168. spin_lock_bh(&counter_list_lock);
  169. list_for_each_entry(e, &counter_list, list)
  170. if (strcmp(e->name, q->name) == 0) {
  171. atomic_inc(&e->ref);
  172. spin_unlock_bh(&counter_list_lock);
  173. kfree(new_e);
  174. pr_debug("xt_quota2: old counter name=%s", e->name);
  175. return e;
  176. }
  177. e = new_e;
  178. pr_debug("xt_quota2: new_counter name=%s", e->name);
  179. list_add_tail(&e->list, &counter_list);
  180. /* The entry having a refcount of 1 is not directly destructible.
  181. * This func has not yet returned the new entry, thus iptables
  182. * has not references for destroying this entry.
  183. * For another rule to try to destroy it, it would 1st need for this
  184. * func* to be re-invoked, acquire a new ref for the same named quota.
  185. * Nobody will access the e->procfs_entry either.
  186. * So release the lock. */
  187. spin_unlock_bh(&counter_list_lock);
  188. /* create_proc_entry() is not spin_lock happy */
  189. p = e->procfs_entry = create_proc_entry(e->name, quota_list_perms,
  190. proc_xt_quota);
  191. if (IS_ERR_OR_NULL(p)) {
  192. spin_lock_bh(&counter_list_lock);
  193. list_del(&e->list);
  194. spin_unlock_bh(&counter_list_lock);
  195. goto out;
  196. }
  197. p->data = e;
  198. p->read_proc = quota_proc_read;
  199. p->write_proc = quota_proc_write;
  200. p->uid = quota_list_uid;
  201. p->gid = quota_list_gid;
  202. return e;
  203. out:
  204. kfree(e);
  205. return NULL;
  206. }
  207. static int quota_mt2_check(const struct xt_mtchk_param *par)
  208. {
  209. struct xt_quota_mtinfo2 *q = par->matchinfo;
  210. pr_debug("xt_quota2: check() flags=0x%04x", q->flags);
  211. if (q->flags & ~XT_QUOTA_MASK)
  212. return -EINVAL;
  213. q->name[sizeof(q->name)-1] = '\0';
  214. if (*q->name == '.' || strchr(q->name, '/') != NULL) {
  215. printk(KERN_ERR "xt_quota.3: illegal name\n");
  216. return -EINVAL;
  217. }
  218. q->master = q2_get_counter(q);
  219. if (q->master == NULL) {
  220. printk(KERN_ERR "xt_quota.3: memory alloc failure\n");
  221. return -ENOMEM;
  222. }
  223. return 0;
  224. }
  225. static void quota_mt2_destroy(const struct xt_mtdtor_param *par)
  226. {
  227. struct xt_quota_mtinfo2 *q = par->matchinfo;
  228. struct xt_quota_counter *e = q->master;
  229. if (*q->name == '\0') {
  230. kfree(e);
  231. return;
  232. }
  233. spin_lock_bh(&counter_list_lock);
  234. if (!atomic_dec_and_test(&e->ref)) {
  235. spin_unlock_bh(&counter_list_lock);
  236. return;
  237. }
  238. list_del(&e->list);
  239. remove_proc_entry(e->name, proc_xt_quota);
  240. spin_unlock_bh(&counter_list_lock);
  241. kfree(e);
  242. }
  243. static bool
  244. quota_mt2(const struct sk_buff *skb, struct xt_action_param *par)
  245. {
  246. struct xt_quota_mtinfo2 *q = (void *)par->matchinfo;
  247. struct xt_quota_counter *e = q->master;
  248. bool ret = q->flags & XT_QUOTA_INVERT;
  249. spin_lock_bh(&e->lock);
  250. if (q->flags & XT_QUOTA_GROW) {
  251. /*
  252. * While no_change is pointless in "grow" mode, we will
  253. * implement it here simply to have a consistent behavior.
  254. */
  255. if (!(q->flags & XT_QUOTA_NO_CHANGE)) {
  256. e->quota += (q->flags & XT_QUOTA_PACKET) ? 1 : skb->len;
  257. }
  258. ret = true;
  259. } else {
  260. if (e->quota >= skb->len) {
  261. if (!(q->flags & XT_QUOTA_NO_CHANGE))
  262. e->quota -= (q->flags & XT_QUOTA_PACKET) ? 1 : skb->len;
  263. ret = !ret;
  264. } else {
  265. /* We are transitioning, log that fact. */
  266. if (e->quota) {
  267. quota2_log(par->hooknum,
  268. skb,
  269. par->in,
  270. par->out,
  271. q->name);
  272. }
  273. /* we do not allow even small packets from now on */
  274. e->quota = 0;
  275. }
  276. }
  277. spin_unlock_bh(&e->lock);
  278. return ret;
  279. }
  280. static struct xt_match quota_mt2_reg[] __read_mostly = {
  281. {
  282. .name = "quota2",
  283. .revision = 3,
  284. .family = NFPROTO_IPV4,
  285. .checkentry = quota_mt2_check,
  286. .match = quota_mt2,
  287. .destroy = quota_mt2_destroy,
  288. .matchsize = sizeof(struct xt_quota_mtinfo2),
  289. .me = THIS_MODULE,
  290. },
  291. {
  292. .name = "quota2",
  293. .revision = 3,
  294. .family = NFPROTO_IPV6,
  295. .checkentry = quota_mt2_check,
  296. .match = quota_mt2,
  297. .destroy = quota_mt2_destroy,
  298. .matchsize = sizeof(struct xt_quota_mtinfo2),
  299. .me = THIS_MODULE,
  300. },
  301. };
  302. static int __init quota_mt2_init(void)
  303. {
  304. int ret;
  305. pr_debug("xt_quota2: init()");
  306. #ifdef CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG
  307. nflognl = netlink_kernel_create(&init_net,
  308. NETLINK_NFLOG, 1, NULL,
  309. NULL, THIS_MODULE);
  310. if (!nflognl)
  311. return -ENOMEM;
  312. #endif
  313. proc_xt_quota = proc_mkdir("xt_quota", init_net.proc_net);
  314. if (proc_xt_quota == NULL)
  315. return -EACCES;
  316. ret = xt_register_matches(quota_mt2_reg, ARRAY_SIZE(quota_mt2_reg));
  317. if (ret < 0)
  318. remove_proc_entry("xt_quota", init_net.proc_net);
  319. pr_debug("xt_quota2: init() %d", ret);
  320. return ret;
  321. }
  322. static void __exit quota_mt2_exit(void)
  323. {
  324. xt_unregister_matches(quota_mt2_reg, ARRAY_SIZE(quota_mt2_reg));
  325. remove_proc_entry("xt_quota", init_net.proc_net);
  326. }
  327. module_init(quota_mt2_init);
  328. module_exit(quota_mt2_exit);
  329. MODULE_DESCRIPTION("Xtables: countdown quota match; up counter");
  330. MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
  331. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  332. MODULE_LICENSE("GPL");
  333. MODULE_ALIAS("ipt_quota2");
  334. MODULE_ALIAS("ip6t_quota2");