Merge branch 'x86-topology-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / net / netfilter / ipset / ip_set_hash_ipmac.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2016 Tomasz Chilinski <tomasz.chilinski@chilan.com>
3  */
4
5 /* Kernel module implementing an IP set type: the hash:ip,mac type */
6
7 #include <linux/jhash.h>
8 #include <linux/module.h>
9 #include <linux/ip.h>
10 #include <linux/etherdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
14 #include <linux/if_ether.h>
15 #include <net/ip.h>
16 #include <net/ipv6.h>
17 #include <net/netlink.h>
18 #include <net/tcp.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_hash.h>
24
25 #define IPSET_TYPE_REV_MIN      0
26 #define IPSET_TYPE_REV_MAX      0
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
30 IP_SET_MODULE_DESC("hash:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:ip,mac");
32
33 /* Type specific function prefix */
34 #define HTYPE           hash_ipmac
35
36 /* IPv4 variant */
37
38 /* Member elements */
39 struct hash_ipmac4_elem {
40         /* Zero valued IP addresses cannot be stored */
41         __be32 ip;
42         union {
43                 unsigned char ether[ETH_ALEN];
44                 __be32 foo[2];
45         };
46 };
47
48 /* Common functions */
49
50 static inline bool
51 hash_ipmac4_data_equal(const struct hash_ipmac4_elem *e1,
52                        const struct hash_ipmac4_elem *e2,
53                        u32 *multi)
54 {
55         return e1->ip == e2->ip && ether_addr_equal(e1->ether, e2->ether);
56 }
57
58 static bool
59 hash_ipmac4_data_list(struct sk_buff *skb, const struct hash_ipmac4_elem *e)
60 {
61         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip) ||
62             nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
63                 goto nla_put_failure;
64         return false;
65
66 nla_put_failure:
67         return true;
68 }
69
70 static inline void
71 hash_ipmac4_data_next(struct hash_ipmac4_elem *next,
72                       const struct hash_ipmac4_elem *e)
73 {
74         next->ip = e->ip;
75 }
76
77 #define MTYPE           hash_ipmac4
78 #define PF              4
79 #define HOST_MASK       32
80 #define HKEY_DATALEN    sizeof(struct hash_ipmac4_elem)
81 #include "ip_set_hash_gen.h"
82
83 static int
84 hash_ipmac4_kadt(struct ip_set *set, const struct sk_buff *skb,
85                  const struct xt_action_param *par,
86                  enum ipset_adt adt, struct ip_set_adt_opt *opt)
87 {
88         ipset_adtfn adtfn = set->variant->adt[adt];
89         struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
90         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
91
92          /* MAC can be src only */
93         if (!(opt->flags & IPSET_DIM_TWO_SRC))
94                 return 0;
95
96         if (skb_mac_header(skb) < skb->head ||
97             (skb_mac_header(skb) + ETH_HLEN) > skb->data)
98                 return -EINVAL;
99
100         if (opt->flags & IPSET_DIM_ONE_SRC)
101                 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
102         else
103                 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
104
105         if (is_zero_ether_addr(e.ether))
106                 return -EINVAL;
107
108         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
109
110         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
111 }
112
113 static int
114 hash_ipmac4_uadt(struct ip_set *set, struct nlattr *tb[],
115                  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
116 {
117         ipset_adtfn adtfn = set->variant->adt[adt];
118         struct hash_ipmac4_elem e = { .ip = 0, { .foo[0] = 0, .foo[1] = 0 } };
119         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
120         int ret;
121
122         if (unlikely(!tb[IPSET_ATTR_IP] ||
123                      !tb[IPSET_ATTR_ETHER] ||
124                      nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
125                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
126                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
127                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)   ||
128                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
129                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
130                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
131                 return -IPSET_ERR_PROTOCOL;
132
133         if (tb[IPSET_ATTR_LINENO])
134                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
135
136         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
137                 ip_set_get_extensions(set, tb, &ext);
138         if (ret)
139                 return ret;
140         memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
141         if (is_zero_ether_addr(e.ether))
142                 return -IPSET_ERR_HASH_ELEM;
143
144         return adtfn(set, &e, &ext, &ext, flags);
145 }
146
147 /* IPv6 variant */
148
149 /* Member elements */
150 struct hash_ipmac6_elem {
151         /* Zero valued IP addresses cannot be stored */
152         union nf_inet_addr ip;
153         union {
154                 unsigned char ether[ETH_ALEN];
155                 __be32 foo[2];
156         };
157 };
158
159 /* Common functions */
160
161 static inline bool
162 hash_ipmac6_data_equal(const struct hash_ipmac6_elem *e1,
163                        const struct hash_ipmac6_elem *e2,
164                        u32 *multi)
165 {
166         return ipv6_addr_equal(&e1->ip.in6, &e2->ip.in6) &&
167                 ether_addr_equal(e1->ether, e2->ether);
168 }
169
170 static bool
171 hash_ipmac6_data_list(struct sk_buff *skb, const struct hash_ipmac6_elem *e)
172 {
173         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
174             nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, e->ether))
175                 goto nla_put_failure;
176         return false;
177
178 nla_put_failure:
179         return true;
180 }
181
182 static inline void
183 hash_ipmac6_data_next(struct hash_ipmac6_elem *next,
184                       const struct hash_ipmac6_elem *e)
185 {
186 }
187
188 #undef MTYPE
189 #undef PF
190 #undef HOST_MASK
191 #undef HKEY_DATALEN
192
193 #define MTYPE           hash_ipmac6
194 #define PF              6
195 #define HOST_MASK       128
196 #define HKEY_DATALEN    sizeof(struct hash_ipmac6_elem)
197 #define IP_SET_EMIT_CREATE
198 #include "ip_set_hash_gen.h"
199
200 static int
201 hash_ipmac6_kadt(struct ip_set *set, const struct sk_buff *skb,
202                  const struct xt_action_param *par,
203                  enum ipset_adt adt, struct ip_set_adt_opt *opt)
204 {
205         ipset_adtfn adtfn = set->variant->adt[adt];
206         struct hash_ipmac6_elem e = {
207                 { .all = { 0 } },
208                 { .foo[0] = 0, .foo[1] = 0 }
209         };
210         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
211
212         if (skb_mac_header(skb) < skb->head ||
213             (skb_mac_header(skb) + ETH_HLEN) > skb->data)
214                 return -EINVAL;
215
216         if (opt->flags & IPSET_DIM_ONE_SRC)
217                 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
218         else
219                 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
220
221         if (is_zero_ether_addr(e.ether))
222                 return -EINVAL;
223
224         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
225
226         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
227 }
228
229 static int
230 hash_ipmac6_uadt(struct ip_set *set, struct nlattr *tb[],
231                  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
232 {
233         ipset_adtfn adtfn = set->variant->adt[adt];
234         struct hash_ipmac6_elem e = {
235                 { .all = { 0 } },
236                 { .foo[0] = 0, .foo[1] = 0 }
237         };
238         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
239         int ret;
240
241         if (unlikely(!tb[IPSET_ATTR_IP] ||
242                      !tb[IPSET_ATTR_ETHER] ||
243                      nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN ||
244                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
245                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
246                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)   ||
247                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
248                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
249                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
250                 return -IPSET_ERR_PROTOCOL;
251
252         if (tb[IPSET_ATTR_LINENO])
253                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
254
255         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
256                 ip_set_get_extensions(set, tb, &ext);
257         if (ret)
258                 return ret;
259
260         memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
261         if (is_zero_ether_addr(e.ether))
262                 return -IPSET_ERR_HASH_ELEM;
263
264         return adtfn(set, &e, &ext, &ext, flags);
265 }
266
267 static struct ip_set_type hash_ipmac_type __read_mostly = {
268         .name           = "hash:ip,mac",
269         .protocol       = IPSET_PROTOCOL,
270         .features       = IPSET_TYPE_IP | IPSET_TYPE_MAC,
271         .dimension      = IPSET_DIM_TWO,
272         .family         = NFPROTO_UNSPEC,
273         .revision_min   = IPSET_TYPE_REV_MIN,
274         .revision_max   = IPSET_TYPE_REV_MAX,
275         .create         = hash_ipmac_create,
276         .create_policy  = {
277                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
278                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
279                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
280                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
281                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
282                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
283         },
284         .adt_policy     = {
285                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
286                 [IPSET_ATTR_ETHER]      = { .type = NLA_BINARY,
287                                 .len  = ETH_ALEN },
288                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
289                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
290                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
291                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
292                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
293                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
294                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
295                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
296         },
297         .me             = THIS_MODULE,
298 };
299
300 static int __init
301 hash_ipmac_init(void)
302 {
303         return ip_set_type_register(&hash_ipmac_type);
304 }
305
306 static void __exit
307 hash_ipmac_fini(void)
308 {
309         ip_set_type_unregister(&hash_ipmac_type);
310 }
311
312 module_init(hash_ipmac_init);
313 module_exit(hash_ipmac_fini);