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 net *net, 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. if (data_len != sizeof(*set))
  26. return -EINVAL;
  27. index = ip_set_nfnl_get_byindex(net, set->index);
  28. if (index == IPSET_INVALID_ID)
  29. return -ENOENT;
  30. em->datalen = sizeof(*set);
  31. em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
  32. if (em->data)
  33. return 0;
  34. ip_set_nfnl_put(net, index);
  35. return -ENOMEM;
  36. }
  37. static void em_ipset_destroy(struct tcf_ematch *em)
  38. {
  39. const struct xt_set_info *set = (const void *) em->data;
  40. if (set) {
  41. ip_set_nfnl_put(em->net, set->index);
  42. kfree((void *) em->data);
  43. }
  44. }
  45. static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em,
  46. struct tcf_pkt_info *info)
  47. {
  48. struct ip_set_adt_opt opt;
  49. struct xt_action_param acpar;
  50. const struct xt_set_info *set = (const void *) em->data;
  51. struct net_device *dev, *indev = NULL;
  52. int ret, network_offset;
  53. switch (tc_skb_protocol(skb)) {
  54. case htons(ETH_P_IP):
  55. acpar.family = NFPROTO_IPV4;
  56. if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
  57. return 0;
  58. acpar.thoff = ip_hdrlen(skb);
  59. break;
  60. case htons(ETH_P_IPV6):
  61. acpar.family = NFPROTO_IPV6;
  62. if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
  63. return 0;
  64. /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */
  65. acpar.thoff = sizeof(struct ipv6hdr);
  66. break;
  67. default:
  68. return 0;
  69. }
  70. acpar.hooknum = 0;
  71. opt.family = acpar.family;
  72. opt.dim = set->dim;
  73. opt.flags = set->flags;
  74. opt.cmdflags = 0;
  75. opt.ext.timeout = ~0u;
  76. network_offset = skb_network_offset(skb);
  77. skb_pull(skb, network_offset);
  78. dev = skb->dev;
  79. rcu_read_lock();
  80. if (skb->skb_iif)
  81. indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
  82. acpar.net = em->net;
  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);