54df48b5c4558c9e7229abe6d82069af502e2562
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipset / ip_set_hash_ip.c
1 /* Copyright (C) 2003-2013 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:ip 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 #include <net/tcp.h>
20
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/ipset/pfxlen.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 #define IPSET_TYPE_REV_MIN      0
27 /*                              1          Counters support */
28 /*                              2          Comments support */
29 /*                              3          Forceadd support */
30 #define IPSET_TYPE_REV_MAX      4       /* skbinfo support  */
31
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:ip");
36
37 /* Type specific function prefix */
38 #define HTYPE           hash_ip
39 #define IP_SET_HASH_WITH_NETMASK
40
41 /* IPv4 variant */
42
43 /* Member elements */
44 struct hash_ip4_elem {
45         /* Zero valued IP addresses cannot be stored */
46         __be32 ip;
47 };
48
49 /* Common functions */
50
51 static inline bool
52 hash_ip4_data_equal(const struct hash_ip4_elem *e1,
53                     const struct hash_ip4_elem *e2,
54                     u32 *multi)
55 {
56         return e1->ip == e2->ip;
57 }
58
59 static bool
60 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
61 {
62         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
63                 goto nla_put_failure;
64         return false;
65
66 nla_put_failure:
67         return true;
68 }
69
70 static inline void
71 hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
72 {
73         next->ip = e->ip;
74 }
75
76 #define MTYPE           hash_ip4
77 #define HOST_MASK       32
78 #include "ip_set_hash_gen.h"
79
80 static int
81 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
82               const struct xt_action_param *par,
83               enum ipset_adt adt, struct ip_set_adt_opt *opt)
84 {
85         const struct hash_ip *h = set->data;
86         ipset_adtfn adtfn = set->variant->adt[adt];
87         struct hash_ip4_elem e = { 0 };
88         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
89         __be32 ip;
90
91         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
92         ip &= ip_set_netmask(h->netmask);
93         if (ip == 0)
94                 return -EINVAL;
95
96         e.ip = ip;
97         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
98 }
99
100 static int
101 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
102               enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
103 {
104         const struct hash_ip *h = set->data;
105         ipset_adtfn adtfn = set->variant->adt[adt];
106         struct hash_ip4_elem e = { 0 };
107         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
108         u32 ip = 0, ip_to = 0, hosts;
109         int ret = 0;
110
111         if (unlikely(!tb[IPSET_ATTR_IP] ||
112                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
113                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
114                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)   ||
115                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
116                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
117                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
118                 return -IPSET_ERR_PROTOCOL;
119
120         if (tb[IPSET_ATTR_LINENO])
121                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
122
123         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
124         if (ret)
125                 return ret;
126
127         ret = ip_set_get_extensions(set, tb, &ext);
128         if (ret)
129                 return ret;
130
131         ip &= ip_set_hostmask(h->netmask);
132
133         if (adt == IPSET_TEST) {
134                 e.ip = htonl(ip);
135                 if (e.ip == 0)
136                         return -IPSET_ERR_HASH_ELEM;
137                 return adtfn(set, &e, &ext, &ext, flags);
138         }
139
140         ip_to = ip;
141         if (tb[IPSET_ATTR_IP_TO]) {
142                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
143                 if (ret)
144                         return ret;
145                 if (ip > ip_to)
146                         swap(ip, ip_to);
147         } else if (tb[IPSET_ATTR_CIDR]) {
148                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
149
150                 if (!cidr || cidr > HOST_MASK)
151                         return -IPSET_ERR_INVALID_CIDR;
152                 ip_set_mask_from_to(ip, ip_to, cidr);
153         }
154
155         hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
156
157         if (retried)
158                 ip = ntohl(h->next.ip);
159         for (; !before(ip_to, ip); ip += hosts) {
160                 e.ip = htonl(ip);
161                 if (e.ip == 0)
162                         return -IPSET_ERR_HASH_ELEM;
163                 ret = adtfn(set, &e, &ext, &ext, flags);
164
165                 if (ret && !ip_set_eexist(ret, flags))
166                         return ret;
167                 else
168                         ret = 0;
169         }
170         return ret;
171 }
172
173 /* IPv6 variant */
174
175 /* Member elements */
176 struct hash_ip6_elem {
177         union nf_inet_addr ip;
178 };
179
180 /* Common functions */
181
182 static inline bool
183 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
184                     const struct hash_ip6_elem *ip2,
185                     u32 *multi)
186 {
187         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
188 }
189
190 static inline void
191 hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
192 {
193         ip6_netmask(ip, prefix);
194 }
195
196 static bool
197 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
198 {
199         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
200                 goto nla_put_failure;
201         return false;
202
203 nla_put_failure:
204         return true;
205 }
206
207 static inline void
208 hash_ip6_data_next(struct hash_ip4_elem *next, const struct hash_ip6_elem *e)
209 {
210 }
211
212 #undef MTYPE
213 #undef HOST_MASK
214
215 #define MTYPE           hash_ip6
216 #define HOST_MASK       128
217
218 #define IP_SET_EMIT_CREATE
219 #include "ip_set_hash_gen.h"
220
221 static int
222 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
223               const struct xt_action_param *par,
224               enum ipset_adt adt, struct ip_set_adt_opt *opt)
225 {
226         const struct hash_ip *h = set->data;
227         ipset_adtfn adtfn = set->variant->adt[adt];
228         struct hash_ip6_elem e = { { .all = { 0 } } };
229         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
230
231         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
232         hash_ip6_netmask(&e.ip, h->netmask);
233         if (ipv6_addr_any(&e.ip.in6))
234                 return -EINVAL;
235
236         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
237 }
238
239 static int
240 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
241               enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
242 {
243         const struct hash_ip *h = set->data;
244         ipset_adtfn adtfn = set->variant->adt[adt];
245         struct hash_ip6_elem e = { { .all = { 0 } } };
246         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
247         int ret;
248
249         if (unlikely(!tb[IPSET_ATTR_IP] ||
250                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
251                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
252                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
253                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
254                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
255                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE) ||
256                      tb[IPSET_ATTR_IP_TO] ||
257                      tb[IPSET_ATTR_CIDR]))
258                 return -IPSET_ERR_PROTOCOL;
259
260         if (tb[IPSET_ATTR_LINENO])
261                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
262
263         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
264         if (ret)
265                 return ret;
266
267         ret = ip_set_get_extensions(set, tb, &ext);
268         if (ret)
269                 return ret;
270
271         hash_ip6_netmask(&e.ip, h->netmask);
272         if (ipv6_addr_any(&e.ip.in6))
273                 return -IPSET_ERR_HASH_ELEM;
274
275         ret = adtfn(set, &e, &ext, &ext, flags);
276
277         return ip_set_eexist(ret, flags) ? 0 : ret;
278 }
279
280 static struct ip_set_type hash_ip_type __read_mostly = {
281         .name           = "hash:ip",
282         .protocol       = IPSET_PROTOCOL,
283         .features       = IPSET_TYPE_IP,
284         .dimension      = IPSET_DIM_ONE,
285         .family         = NFPROTO_UNSPEC,
286         .revision_min   = IPSET_TYPE_REV_MIN,
287         .revision_max   = IPSET_TYPE_REV_MAX,
288         .create         = hash_ip_create,
289         .create_policy  = {
290                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
291                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
292                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
293                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
294                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
295                 [IPSET_ATTR_NETMASK]    = { .type = NLA_U8  },
296                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
297         },
298         .adt_policy     = {
299                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
300                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
301                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
302                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
303                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
304                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
305                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
306                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
307                                             .len  = IPSET_MAX_COMMENT_SIZE },
308                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
309                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
310                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
311         },
312         .me             = THIS_MODULE,
313 };
314
315 static int __init
316 hash_ip_init(void)
317 {
318         return ip_set_type_register(&hash_ip_type);
319 }
320
321 static void __exit
322 hash_ip_fini(void)
323 {
324         ip_set_type_unregister(&hash_ip_type);
325 }
326
327 module_init(hash_ip_init);
328 module_exit(hash_ip_fini);