em_ipset.c 3.0 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.in = indev ? indev : dev;
  83. acpar.out = dev;
  84. ret = ip_set_test(set->index, skb, &acpar, &opt);
  85. rcu_read_unlock();
  86. skb_push(skb, network_offset);
  87. return ret;
  88. }
  89. static struct tcf_ematch_ops em_ipset_ops = {
  90. .kind = TCF_EM_IPSET,
  91. .change = em_ipset_change,
  92. .destroy = em_ipset_destroy,
  93. .match = em_ipset_match,
  94. .owner = THIS_MODULE,
  95. .link = LIST_HEAD_INIT(em_ipset_ops.link)
  96. };
  97. static int __init init_em_ipset(void)
  98. {
  99. return tcf_em_register(&em_ipset_ops);
  100. }
  101. static void __exit exit_em_ipset(void)
  102. {
  103. tcf_em_unregister(&em_ipset_ops);
  104. }
  105. MODULE_LICENSE("GPL");
  106. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  107. MODULE_DESCRIPTION("TC extended match for IP sets");
  108. module_init(init_em_ipset);
  109. module_exit(exit_em_ipset);
  110. MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET);