em_ipset.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * net/sched/em_ipset.c ipset ematch
  3. *
  4. * Copyright (c) 2012 Florian Westphal <fw@strlen.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/gfp.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/netfilter/xt_set.h>
  17. #include <linux/ipv6.h>
  18. #include <net/ip.h>
  19. #include <net/pkt_cls.h>
  20. static int em_ipset_change(struct tcf_proto *tp, void *data, int data_len,
  21. struct tcf_ematch *em)
  22. {
  23. struct xt_set_info *set = data;
  24. ip_set_id_t index;
  25. struct net *net = qdisc_dev(tp->q)->nd_net;
  26. if (data_len != sizeof(*set))
  27. return -EINVAL;
  28. index = ip_set_nfnl_get_byindex(net, set->index);
  29. if (index == IPSET_INVALID_ID)
  30. return -ENOENT;
  31. em->datalen = sizeof(*set);
  32. em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
  33. if (em->data)
  34. return 0;
  35. ip_set_nfnl_put(net, index);
  36. return -ENOMEM;
  37. }
  38. static void em_ipset_destroy(struct tcf_proto *p, struct tcf_ematch *em)
  39. {
  40. const struct xt_set_info *set = (const void *) em->data;
  41. if (set) {
  42. ip_set_nfnl_put(qdisc_dev(p->q)->nd_net, set->index);
  43. kfree((void *) em->data);
  44. }
  45. }
  46. static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em,
  47. struct tcf_pkt_info *info)
  48. {
  49. struct ip_set_adt_opt opt;
  50. struct xt_action_param acpar;
  51. const struct xt_set_info *set = (const void *) em->data;
  52. struct net_device *dev, *indev = NULL;
  53. int ret, network_offset;
  54. switch (skb->protocol) {
  55. case htons(ETH_P_IP):
  56. acpar.family = NFPROTO_IPV4;
  57. if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
  58. return 0;
  59. acpar.thoff = ip_hdrlen(skb);
  60. break;
  61. case htons(ETH_P_IPV6):
  62. acpar.family = NFPROTO_IPV6;
  63. if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
  64. return 0;
  65. /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */
  66. acpar.thoff = sizeof(struct ipv6hdr);
  67. break;
  68. default:
  69. return 0;
  70. }
  71. acpar.hooknum = 0;
  72. opt.family = acpar.family;
  73. opt.dim = set->dim;
  74. opt.flags = set->flags;
  75. opt.cmdflags = 0;
  76. opt.ext.timeout = ~0u;
  77. network_offset = skb_network_offset(skb);
  78. skb_pull(skb, network_offset);
  79. dev = skb->dev;
  80. rcu_read_lock();
  81. if (dev && skb->skb_iif)
  82. indev = dev_get_by_index_rcu(dev_net(dev), skb->skb_iif);
  83. acpar.in = indev ? indev : dev;
  84. acpar.out = dev;
  85. ret = ip_set_test(set->index, skb, &acpar, &opt);
  86. rcu_read_unlock();
  87. skb_push(skb, network_offset);
  88. return ret;
  89. }
  90. static struct tcf_ematch_ops em_ipset_ops = {
  91. .kind = TCF_EM_IPSET,
  92. .change = em_ipset_change,
  93. .destroy = em_ipset_destroy,
  94. .match = em_ipset_match,
  95. .owner = THIS_MODULE,
  96. .link = LIST_HEAD_INIT(em_ipset_ops.link)
  97. };
  98. static int __init init_em_ipset(void)
  99. {
  100. return tcf_em_register(&em_ipset_ops);
  101. }
  102. static void __exit exit_em_ipset(void)
  103. {
  104. tcf_em_unregister(&em_ipset_ops);
  105. }
  106. MODULE_LICENSE("GPL");
  107. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  108. MODULE_DESCRIPTION("TC extended match for IP sets");
  109. module_init(init_em_ipset);
  110. module_exit(exit_em_ipset);
  111. MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET);