em_ipset.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct nf_hook_state state = {
  53. .net = em->net,
  54. };
  55. int ret, network_offset;
  56. switch (tc_skb_protocol(skb)) {
  57. case htons(ETH_P_IP):
  58. state.pf = NFPROTO_IPV4;
  59. if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
  60. return 0;
  61. acpar.thoff = ip_hdrlen(skb);
  62. break;
  63. case htons(ETH_P_IPV6):
  64. state.pf = NFPROTO_IPV6;
  65. if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
  66. return 0;
  67. /* doesn't call ipv6_find_hdr() because ipset doesn't use thoff, yet */
  68. acpar.thoff = sizeof(struct ipv6hdr);
  69. break;
  70. default:
  71. return 0;
  72. }
  73. opt.family = state.pf;
  74. opt.dim = set->dim;
  75. opt.flags = set->flags;
  76. opt.cmdflags = 0;
  77. opt.ext.timeout = ~0u;
  78. network_offset = skb_network_offset(skb);
  79. skb_pull(skb, network_offset);
  80. dev = skb->dev;
  81. rcu_read_lock();
  82. if (skb->skb_iif)
  83. indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
  84. state.in = indev ? indev : dev;
  85. state.out = dev;
  86. acpar.state = &state;
  87. ret = ip_set_test(set->index, skb, &acpar, &opt);
  88. rcu_read_unlock();
  89. skb_push(skb, network_offset);
  90. return ret;
  91. }
  92. static struct tcf_ematch_ops em_ipset_ops = {
  93. .kind = TCF_EM_IPSET,
  94. .change = em_ipset_change,
  95. .destroy = em_ipset_destroy,
  96. .match = em_ipset_match,
  97. .owner = THIS_MODULE,
  98. .link = LIST_HEAD_INIT(em_ipset_ops.link)
  99. };
  100. static int __init init_em_ipset(void)
  101. {
  102. return tcf_em_register(&em_ipset_ops);
  103. }
  104. static void __exit exit_em_ipset(void)
  105. {
  106. tcf_em_unregister(&em_ipset_ops);
  107. }
  108. MODULE_LICENSE("GPL");
  109. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  110. MODULE_DESCRIPTION("TC extended match for IP sets");
  111. module_init(init_em_ipset);
  112. module_exit(exit_em_ipset);
  113. MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPSET);