xt_cluster.c 4.9 KB

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