xt_cluster.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * (C) 2008-2009 Pablo Neira Ayuso <pablo@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/jhash.h>
  12. #include <linux/ip.h>
  13. #include <net/ipv6.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <net/netfilter/nf_conntrack.h>
  16. #include <linux/netfilter/xt_cluster.h>
  17. static inline u32 nf_ct_orig_ipv4_src(const struct nf_conn *ct)
  18. {
  19. return (__force u32)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
  20. }
  21. static inline const u32 *nf_ct_orig_ipv6_src(const struct nf_conn *ct)
  22. {
  23. return (__force u32 *)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6;
  24. }
  25. static inline u_int32_t
  26. xt_cluster_hash_ipv4(u_int32_t ip, const struct xt_cluster_match_info *info)
  27. {
  28. return jhash_1word(ip, info->hash_seed);
  29. }
  30. static inline u_int32_t
  31. xt_cluster_hash_ipv6(const void *ip, const struct xt_cluster_match_info *info)
  32. {
  33. return jhash2(ip, NF_CT_TUPLE_L3SIZE / sizeof(__u32), info->hash_seed);
  34. }
  35. static inline u_int32_t
  36. xt_cluster_hash(const struct nf_conn *ct,
  37. const struct xt_cluster_match_info *info)
  38. {
  39. u_int32_t hash = 0;
  40. switch(nf_ct_l3num(ct)) {
  41. case AF_INET:
  42. hash = xt_cluster_hash_ipv4(nf_ct_orig_ipv4_src(ct), info);
  43. break;
  44. case AF_INET6:
  45. hash = xt_cluster_hash_ipv6(nf_ct_orig_ipv6_src(ct), info);
  46. break;
  47. default:
  48. WARN_ON(1);
  49. break;
  50. }
  51. return reciprocal_scale(hash, info->total_nodes);
  52. }
  53. static inline bool
  54. xt_cluster_ipv6_is_multicast(const struct in6_addr *addr)
  55. {
  56. __be32 st = addr->s6_addr32[0];
  57. return ((st & htonl(0xFF000000)) == htonl(0xFF000000));
  58. }
  59. static inline bool
  60. xt_cluster_is_multicast_addr(const struct sk_buff *skb, u_int8_t family)
  61. {
  62. bool is_multicast = false;
  63. switch(family) {
  64. case NFPROTO_IPV4:
  65. is_multicast = ipv4_is_multicast(ip_hdr(skb)->daddr);
  66. break;
  67. case NFPROTO_IPV6:
  68. is_multicast =
  69. xt_cluster_ipv6_is_multicast(&ipv6_hdr(skb)->daddr);
  70. break;
  71. default:
  72. WARN_ON(1);
  73. break;
  74. }
  75. return is_multicast;
  76. }
  77. static bool
  78. xt_cluster_mt(const struct sk_buff *skb, struct xt_action_param *par)
  79. {
  80. struct sk_buff *pskb = (struct sk_buff *)skb;
  81. const struct xt_cluster_match_info *info = par->matchinfo;
  82. const struct nf_conn *ct;
  83. enum ip_conntrack_info ctinfo;
  84. unsigned long hash;
  85. /* This match assumes that all nodes see the same packets. This can be
  86. * achieved if the switch that connects the cluster nodes support some
  87. * sort of 'port mirroring'. However, if your switch does not support
  88. * this, your cluster nodes can reply ARP request using a multicast MAC
  89. * address. Thus, your switch will flood the same packets to the
  90. * cluster nodes with the same multicast MAC address. Using a multicast
  91. * link address is a RFC 1812 (section 3.3.2) violation, but this works
  92. * fine in practise.
  93. *
  94. * Unfortunately, if you use the multicast MAC address, the link layer
  95. * sets skbuff's pkt_type to PACKET_MULTICAST, which is not accepted
  96. * by TCP and others for packets coming to this node. For that reason,
  97. * this match mangles skbuff's pkt_type if it detects a packet
  98. * addressed to a unicast address but using PACKET_MULTICAST. Yes, I
  99. * know, matches should not alter packets, but we are doing this here
  100. * because we would need to add a PKTTYPE target for this sole purpose.
  101. */
  102. if (!xt_cluster_is_multicast_addr(skb, par->family) &&
  103. skb->pkt_type == PACKET_MULTICAST) {
  104. pskb->pkt_type = PACKET_HOST;
  105. }
  106. ct = nf_ct_get(skb, &ctinfo);
  107. if (ct == NULL)
  108. return false;
  109. if (nf_ct_is_untracked(ct))
  110. return false;
  111. if (ct->master)
  112. hash = xt_cluster_hash(ct->master, info);
  113. else
  114. hash = xt_cluster_hash(ct, info);
  115. return !!((1 << hash) & info->node_mask) ^
  116. !!(info->flags & XT_CLUSTER_F_INV);
  117. }
  118. static int xt_cluster_mt_checkentry(const struct xt_mtchk_param *par)
  119. {
  120. struct xt_cluster_match_info *info = par->matchinfo;
  121. if (info->total_nodes > XT_CLUSTER_NODES_MAX) {
  122. pr_info("you have exceeded the maximum "
  123. "number of cluster nodes (%u > %u)\n",
  124. info->total_nodes, XT_CLUSTER_NODES_MAX);
  125. return -EINVAL;
  126. }
  127. if (info->node_mask >= (1ULL << info->total_nodes)) {
  128. pr_info("this node mask cannot be "
  129. "higher than the total number of nodes\n");
  130. return -EDOM;
  131. }
  132. return 0;
  133. }
  134. static struct xt_match xt_cluster_match __read_mostly = {
  135. .name = "cluster",
  136. .family = NFPROTO_UNSPEC,
  137. .match = xt_cluster_mt,
  138. .checkentry = xt_cluster_mt_checkentry,
  139. .matchsize = sizeof(struct xt_cluster_match_info),
  140. .me = THIS_MODULE,
  141. };
  142. static int __init xt_cluster_mt_init(void)
  143. {
  144. return xt_register_match(&xt_cluster_match);
  145. }
  146. static void __exit xt_cluster_mt_fini(void)
  147. {
  148. xt_unregister_match(&xt_cluster_match);
  149. }
  150. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  151. MODULE_LICENSE("GPL");
  152. MODULE_DESCRIPTION("Xtables: hash-based cluster match");
  153. MODULE_ALIAS("ipt_cluster");
  154. MODULE_ALIAS("ip6t_cluster");
  155. module_init(xt_cluster_mt_init);
  156. module_exit(xt_cluster_mt_fini);