360cf5b3ddf6e5d00d4236c448bad2adbce0cb87
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipset / ip_set_hash_net.c
1 /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation.
6  */
7
8 /* Kernel module implementing an IP set type: the hash:net type */
9
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.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_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28 MODULE_DESCRIPTION("hash:net type of IP sets");
29 MODULE_ALIAS("ip_set_hash:net");
30
31 /* Type specific function prefix */
32 #define TYPE            hash_net
33
34 static bool
35 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
36
37 #define hash_net4_same_set      hash_net_same_set
38 #define hash_net6_same_set      hash_net_same_set
39
40 /* The type variant functions: IPv4 */
41
42 /* Member elements without timeout */
43 struct hash_net4_elem {
44         __be32 ip;
45         u16 padding0;
46         u8 padding1;
47         u8 cidr;
48 };
49
50 /* Member elements with timeout support */
51 struct hash_net4_telem {
52         __be32 ip;
53         u16 padding0;
54         u8 padding1;
55         u8 cidr;
56         unsigned long timeout;
57 };
58
59 static inline bool
60 hash_net4_data_equal(const struct hash_net4_elem *ip1,
61                     const struct hash_net4_elem *ip2)
62 {
63         return ip1->ip == ip2->ip && ip1->cidr == ip2->cidr;
64 }
65
66 static inline bool
67 hash_net4_data_isnull(const struct hash_net4_elem *elem)
68 {
69         return elem->cidr == 0;
70 }
71
72 static inline void
73 hash_net4_data_copy(struct hash_net4_elem *dst,
74                     const struct hash_net4_elem *src)
75 {
76         dst->ip = src->ip;
77         dst->cidr = src->cidr;
78 }
79
80 static inline void
81 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
82 {
83         elem->ip &= ip_set_netmask(cidr);
84         elem->cidr = cidr;
85 }
86
87 /* Zero CIDR values cannot be stored */
88 static inline void
89 hash_net4_data_zero_out(struct hash_net4_elem *elem)
90 {
91         elem->cidr = 0;
92 }
93
94 static bool
95 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
96 {
97         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
98         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
99         return 0;
100
101 nla_put_failure:
102         return 1;
103 }
104
105 static bool
106 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
107 {
108         const struct hash_net4_telem *tdata =
109                 (const struct hash_net4_telem *)data;
110
111         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
112         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
113         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
114                       htonl(ip_set_timeout_get(tdata->timeout)));
115
116         return 0;
117
118 nla_put_failure:
119         return 1;
120 }
121
122 #define IP_SET_HASH_WITH_NETS
123
124 #define PF              4
125 #define HOST_MASK       32
126 #include <linux/netfilter/ipset/ip_set_ahash.h>
127
128 static inline void
129 hash_net4_data_next(struct ip_set_hash *h,
130                     const struct hash_net4_elem *d)
131 {
132 }
133
134 static int
135 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
136                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
137 {
138         const struct ip_set_hash *h = set->data;
139         ipset_adtfn adtfn = set->variant->adt[adt];
140         struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
141
142         if (data.cidr == 0)
143                 return -EINVAL;
144         if (adt == IPSET_TEST)
145                 data.cidr = HOST_MASK;
146
147         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
148         data.ip &= ip_set_netmask(data.cidr);
149
150         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
151 }
152
153 static int
154 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
155                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
156 {
157         const struct ip_set_hash *h = set->data;
158         ipset_adtfn adtfn = set->variant->adt[adt];
159         struct hash_net4_elem data = { .cidr = HOST_MASK };
160         u32 timeout = h->timeout;
161         int ret;
162
163         if (unlikely(!tb[IPSET_ATTR_IP] ||
164                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
165                 return -IPSET_ERR_PROTOCOL;
166
167         if (tb[IPSET_ATTR_LINENO])
168                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
169
170         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
171         if (ret)
172                 return ret;
173
174         if (tb[IPSET_ATTR_CIDR])
175                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
176
177         if (!data.cidr)
178                 return -IPSET_ERR_INVALID_CIDR;
179
180         data.ip &= ip_set_netmask(data.cidr);
181
182         if (tb[IPSET_ATTR_TIMEOUT]) {
183                 if (!with_timeout(h->timeout))
184                         return -IPSET_ERR_TIMEOUT;
185                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
186         }
187
188         ret = adtfn(set, &data, timeout, flags);
189
190         return ip_set_eexist(ret, flags) ? 0 : ret;
191 }
192
193 static bool
194 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
195 {
196         const struct ip_set_hash *x = a->data;
197         const struct ip_set_hash *y = b->data;
198
199         /* Resizing changes htable_bits, so we ignore it */
200         return x->maxelem == y->maxelem &&
201                x->timeout == y->timeout;
202 }
203
204 /* The type variant functions: IPv6 */
205
206 struct hash_net6_elem {
207         union nf_inet_addr ip;
208         u16 padding0;
209         u8 padding1;
210         u8 cidr;
211 };
212
213 struct hash_net6_telem {
214         union nf_inet_addr ip;
215         u16 padding0;
216         u8 padding1;
217         u8 cidr;
218         unsigned long timeout;
219 };
220
221 static inline bool
222 hash_net6_data_equal(const struct hash_net6_elem *ip1,
223                      const struct hash_net6_elem *ip2)
224 {
225         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
226                ip1->cidr == ip2->cidr;
227 }
228
229 static inline bool
230 hash_net6_data_isnull(const struct hash_net6_elem *elem)
231 {
232         return elem->cidr == 0;
233 }
234
235 static inline void
236 hash_net6_data_copy(struct hash_net6_elem *dst,
237                     const struct hash_net6_elem *src)
238 {
239         ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
240         dst->cidr = src->cidr;
241 }
242
243 static inline void
244 hash_net6_data_zero_out(struct hash_net6_elem *elem)
245 {
246         elem->cidr = 0;
247 }
248
249 static inline void
250 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
251 {
252         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
253         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
254         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
255         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
256 }
257
258 static inline void
259 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
260 {
261         ip6_netmask(&elem->ip, cidr);
262         elem->cidr = cidr;
263 }
264
265 static bool
266 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
267 {
268         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
269         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
270         return 0;
271
272 nla_put_failure:
273         return 1;
274 }
275
276 static bool
277 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
278 {
279         const struct hash_net6_telem *e =
280                 (const struct hash_net6_telem *)data;
281
282         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
283         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
284         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
285                       htonl(ip_set_timeout_get(e->timeout)));
286         return 0;
287
288 nla_put_failure:
289         return 1;
290 }
291
292 #undef PF
293 #undef HOST_MASK
294
295 #define PF              6
296 #define HOST_MASK       128
297 #include <linux/netfilter/ipset/ip_set_ahash.h>
298
299 static inline void
300 hash_net6_data_next(struct ip_set_hash *h,
301                     const struct hash_net6_elem *d)
302 {
303 }
304
305 static int
306 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
307                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
308 {
309         const struct ip_set_hash *h = set->data;
310         ipset_adtfn adtfn = set->variant->adt[adt];
311         struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
312
313         if (data.cidr == 0)
314                 return -EINVAL;
315         if (adt == IPSET_TEST)
316                 data.cidr = HOST_MASK;
317
318         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
319         ip6_netmask(&data.ip, data.cidr);
320
321         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
322 }
323
324 static int
325 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
326                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
327 {
328         const struct ip_set_hash *h = set->data;
329         ipset_adtfn adtfn = set->variant->adt[adt];
330         struct hash_net6_elem data = { .cidr = HOST_MASK };
331         u32 timeout = h->timeout;
332         int ret;
333
334         if (unlikely(!tb[IPSET_ATTR_IP] ||
335                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
336                 return -IPSET_ERR_PROTOCOL;
337
338         if (tb[IPSET_ATTR_LINENO])
339                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
340
341         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
342         if (ret)
343                 return ret;
344
345         if (tb[IPSET_ATTR_CIDR])
346                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
347
348         if (!data.cidr)
349                 return -IPSET_ERR_INVALID_CIDR;
350
351         ip6_netmask(&data.ip, data.cidr);
352
353         if (tb[IPSET_ATTR_TIMEOUT]) {
354                 if (!with_timeout(h->timeout))
355                         return -IPSET_ERR_TIMEOUT;
356                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
357         }
358
359         ret = adtfn(set, &data, timeout, flags);
360
361         return ip_set_eexist(ret, flags) ? 0 : ret;
362 }
363
364 /* Create hash:ip type of sets */
365
366 static int
367 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
368 {
369         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
370         struct ip_set_hash *h;
371         u8 hbits;
372
373         if (!(set->family == AF_INET || set->family == AF_INET6))
374                 return -IPSET_ERR_INVALID_FAMILY;
375
376         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
377                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
378                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
379                 return -IPSET_ERR_PROTOCOL;
380
381         if (tb[IPSET_ATTR_HASHSIZE]) {
382                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
383                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
384                         hashsize = IPSET_MIMINAL_HASHSIZE;
385         }
386
387         if (tb[IPSET_ATTR_MAXELEM])
388                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
389
390         h = kzalloc(sizeof(*h)
391                     + sizeof(struct ip_set_hash_nets)
392                       * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
393         if (!h)
394                 return -ENOMEM;
395
396         h->maxelem = maxelem;
397         get_random_bytes(&h->initval, sizeof(h->initval));
398         h->timeout = IPSET_NO_TIMEOUT;
399
400         hbits = htable_bits(hashsize);
401         h->table = ip_set_alloc(
402                         sizeof(struct htable)
403                         + jhash_size(hbits) * sizeof(struct hbucket));
404         if (!h->table) {
405                 kfree(h);
406                 return -ENOMEM;
407         }
408         h->table->htable_bits = hbits;
409
410         set->data = h;
411
412         if (tb[IPSET_ATTR_TIMEOUT]) {
413                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
414
415                 set->variant = set->family == AF_INET
416                         ? &hash_net4_tvariant : &hash_net6_tvariant;
417
418                 if (set->family == AF_INET)
419                         hash_net4_gc_init(set);
420                 else
421                         hash_net6_gc_init(set);
422         } else {
423                 set->variant = set->family == AF_INET
424                         ? &hash_net4_variant : &hash_net6_variant;
425         }
426
427         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
428                  set->name, jhash_size(h->table->htable_bits),
429                  h->table->htable_bits, h->maxelem, set->data, h->table);
430
431         return 0;
432 }
433
434 static struct ip_set_type hash_net_type __read_mostly = {
435         .name           = "hash:net",
436         .protocol       = IPSET_PROTOCOL,
437         .features       = IPSET_TYPE_IP,
438         .dimension      = IPSET_DIM_ONE,
439         .family         = AF_UNSPEC,
440         .revision_min   = 0,
441         .revision_max   = 0,
442         .create         = hash_net_create,
443         .create_policy  = {
444                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
445                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
446                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
447                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
448                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
449         },
450         .adt_policy     = {
451                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
452                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
453                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
454         },
455         .me             = THIS_MODULE,
456 };
457
458 static int __init
459 hash_net_init(void)
460 {
461         return ip_set_type_register(&hash_net_type);
462 }
463
464 static void __exit
465 hash_net_fini(void)
466 {
467         ip_set_type_unregister(&hash_net_type);
468 }
469
470 module_init(hash_net_init);
471 module_exit(hash_net_fini);