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