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