0001
0002
0003
0004
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006 #include <linux/module.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/jhash.h>
0009 #include <linux/ip.h>
0010 #include <net/ipv6.h>
0011
0012 #include <linux/netfilter/x_tables.h>
0013 #include <net/netfilter/nf_conntrack.h>
0014 #include <linux/netfilter/xt_cluster.h>
0015
0016 static inline u32 nf_ct_orig_ipv4_src(const struct nf_conn *ct)
0017 {
0018 return (__force u32)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
0019 }
0020
0021 static inline const u32 *nf_ct_orig_ipv6_src(const struct nf_conn *ct)
0022 {
0023 return (__force u32 *)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6;
0024 }
0025
0026 static inline u_int32_t
0027 xt_cluster_hash_ipv4(u_int32_t ip, const struct xt_cluster_match_info *info)
0028 {
0029 return jhash_1word(ip, info->hash_seed);
0030 }
0031
0032 static inline u_int32_t
0033 xt_cluster_hash_ipv6(const void *ip, const struct xt_cluster_match_info *info)
0034 {
0035 return jhash2(ip, NF_CT_TUPLE_L3SIZE / sizeof(__u32), info->hash_seed);
0036 }
0037
0038 static inline u_int32_t
0039 xt_cluster_hash(const struct nf_conn *ct,
0040 const struct xt_cluster_match_info *info)
0041 {
0042 u_int32_t hash = 0;
0043
0044 switch(nf_ct_l3num(ct)) {
0045 case AF_INET:
0046 hash = xt_cluster_hash_ipv4(nf_ct_orig_ipv4_src(ct), info);
0047 break;
0048 case AF_INET6:
0049 hash = xt_cluster_hash_ipv6(nf_ct_orig_ipv6_src(ct), info);
0050 break;
0051 default:
0052 WARN_ON(1);
0053 break;
0054 }
0055
0056 return reciprocal_scale(hash, info->total_nodes);
0057 }
0058
0059 static inline bool
0060 xt_cluster_is_multicast_addr(const struct sk_buff *skb, u_int8_t family)
0061 {
0062 bool is_multicast = false;
0063
0064 switch(family) {
0065 case NFPROTO_IPV4:
0066 is_multicast = ipv4_is_multicast(ip_hdr(skb)->daddr);
0067 break;
0068 case NFPROTO_IPV6:
0069 is_multicast = ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr);
0070 break;
0071 default:
0072 WARN_ON(1);
0073 break;
0074 }
0075 return is_multicast;
0076 }
0077
0078 static bool
0079 xt_cluster_mt(const struct sk_buff *skb, struct xt_action_param *par)
0080 {
0081 struct sk_buff *pskb = (struct sk_buff *)skb;
0082 const struct xt_cluster_match_info *info = par->matchinfo;
0083 const struct nf_conn *ct;
0084 enum ip_conntrack_info ctinfo;
0085 unsigned long hash;
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 if (!xt_cluster_is_multicast_addr(skb, xt_family(par)) &&
0105 skb->pkt_type == PACKET_MULTICAST) {
0106 pskb->pkt_type = PACKET_HOST;
0107 }
0108
0109 ct = nf_ct_get(skb, &ctinfo);
0110 if (ct == NULL)
0111 return false;
0112
0113 if (ct->master)
0114 hash = xt_cluster_hash(ct->master, info);
0115 else
0116 hash = xt_cluster_hash(ct, info);
0117
0118 return !!((1 << hash) & info->node_mask) ^
0119 !!(info->flags & XT_CLUSTER_F_INV);
0120 }
0121
0122 static int xt_cluster_mt_checkentry(const struct xt_mtchk_param *par)
0123 {
0124 struct xt_cluster_match_info *info = par->matchinfo;
0125 int ret;
0126
0127 if (info->total_nodes > XT_CLUSTER_NODES_MAX) {
0128 pr_info_ratelimited("you have exceeded the maximum number of cluster nodes (%u > %u)\n",
0129 info->total_nodes, XT_CLUSTER_NODES_MAX);
0130 return -EINVAL;
0131 }
0132 if (info->node_mask >= (1ULL << info->total_nodes)) {
0133 pr_info_ratelimited("node mask cannot exceed total number of nodes\n");
0134 return -EDOM;
0135 }
0136
0137 ret = nf_ct_netns_get(par->net, par->family);
0138 if (ret < 0)
0139 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
0140 par->family);
0141 return ret;
0142 }
0143
0144 static void xt_cluster_mt_destroy(const struct xt_mtdtor_param *par)
0145 {
0146 nf_ct_netns_put(par->net, par->family);
0147 }
0148
0149 static struct xt_match xt_cluster_match __read_mostly = {
0150 .name = "cluster",
0151 .family = NFPROTO_UNSPEC,
0152 .match = xt_cluster_mt,
0153 .checkentry = xt_cluster_mt_checkentry,
0154 .matchsize = sizeof(struct xt_cluster_match_info),
0155 .destroy = xt_cluster_mt_destroy,
0156 .me = THIS_MODULE,
0157 };
0158
0159 static int __init xt_cluster_mt_init(void)
0160 {
0161 return xt_register_match(&xt_cluster_match);
0162 }
0163
0164 static void __exit xt_cluster_mt_fini(void)
0165 {
0166 xt_unregister_match(&xt_cluster_match);
0167 }
0168
0169 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
0170 MODULE_LICENSE("GPL");
0171 MODULE_DESCRIPTION("Xtables: hash-based cluster match");
0172 MODULE_ALIAS("ipt_cluster");
0173 MODULE_ALIAS("ip6t_cluster");
0174 module_init(xt_cluster_mt_init);
0175 module_exit(xt_cluster_mt_fini);