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