Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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 inline 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 0;
65
66 nla_put_failure:
67         return 1;
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 PF              4
78 #define HOST_MASK       32
79 #include "ip_set_hash_gen.h"
80
81 static int
82 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
83               const struct xt_action_param *par,
84               enum ipset_adt adt, struct ip_set_adt_opt *opt)
85 {
86         const struct hash_ip *h = set->data;
87         ipset_adtfn adtfn = set->variant->adt[adt];
88         struct hash_ip4_elem e = { 0 };
89         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
90         __be32 ip;
91
92         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
93         ip &= ip_set_netmask(h->netmask);
94         if (ip == 0)
95                 return -EINVAL;
96
97         e.ip = ip;
98         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
99 }
100
101 static int
102 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
103               enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
104 {
105         const struct hash_ip *h = set->data;
106         ipset_adtfn adtfn = set->variant->adt[adt];
107         struct hash_ip4_elem e = { 0 };
108         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
109         u32 ip = 0, ip_to = 0, hosts;
110         int ret = 0;
111
112         if (unlikely(!tb[IPSET_ATTR_IP] ||
113                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
114                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
115                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)   ||
116                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
117                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
118                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
119                 return -IPSET_ERR_PROTOCOL;
120
121         if (tb[IPSET_ATTR_LINENO])
122                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
123
124         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
125               ip_set_get_extensions(set, tb, &ext);
126         if (ret)
127                 return ret;
128
129         ip &= ip_set_hostmask(h->netmask);
130
131         if (adt == IPSET_TEST) {
132                 e.ip = htonl(ip);
133                 if (e.ip == 0)
134                         return -IPSET_ERR_HASH_ELEM;
135                 return adtfn(set, &e, &ext, &ext, flags);
136         }
137
138         ip_to = ip;
139         if (tb[IPSET_ATTR_IP_TO]) {
140                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
141                 if (ret)
142                         return ret;
143                 if (ip > ip_to)
144                         swap(ip, ip_to);
145         } else if (tb[IPSET_ATTR_CIDR]) {
146                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
147
148                 if (!cidr || cidr > 32)
149                         return -IPSET_ERR_INVALID_CIDR;
150                 ip_set_mask_from_to(ip, ip_to, cidr);
151         }
152
153         hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
154
155         if (retried)
156                 ip = ntohl(h->next.ip);
157         for (; !before(ip_to, ip); ip += hosts) {
158                 e.ip = htonl(ip);
159                 if (e.ip == 0)
160                         return -IPSET_ERR_HASH_ELEM;
161                 ret = adtfn(set, &e, &ext, &ext, flags);
162
163                 if (ret && !ip_set_eexist(ret, flags))
164                         return ret;
165                 else
166                         ret = 0;
167         }
168         return ret;
169 }
170
171 /* IPv6 variant */
172
173 /* Member elements */
174 struct hash_ip6_elem {
175         union nf_inet_addr ip;
176 };
177
178 /* Common functions */
179
180 static inline bool
181 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
182                     const struct hash_ip6_elem *ip2,
183                     u32 *multi)
184 {
185         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
186 }
187
188 static inline void
189 hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
190 {
191         ip6_netmask(ip, prefix);
192 }
193
194 static bool
195 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
196 {
197         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
198                 goto nla_put_failure;
199         return 0;
200
201 nla_put_failure:
202         return 1;
203 }
204
205 static inline void
206 hash_ip6_data_next(struct hash_ip4_elem *next, const struct hash_ip6_elem *e)
207 {
208 }
209
210 #undef MTYPE
211 #undef PF
212 #undef HOST_MASK
213 #undef HKEY_DATALEN
214
215 #define MTYPE           hash_ip6
216 #define PF              6
217 #define HOST_MASK       128
218
219 #define IP_SET_EMIT_CREATE
220 #include "ip_set_hash_gen.h"
221
222 static int
223 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
224               const struct xt_action_param *par,
225               enum ipset_adt adt, struct ip_set_adt_opt *opt)
226 {
227         const struct hash_ip *h = set->data;
228         ipset_adtfn adtfn = set->variant->adt[adt];
229         struct hash_ip6_elem e = { { .all = { 0 } } };
230         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
231
232         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
233         hash_ip6_netmask(&e.ip, h->netmask);
234         if (ipv6_addr_any(&e.ip.in6))
235                 return -EINVAL;
236
237         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
238 }
239
240 static int
241 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
242               enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
243 {
244         const struct hash_ip *h = set->data;
245         ipset_adtfn adtfn = set->variant->adt[adt];
246         struct hash_ip6_elem e = { { .all = { 0 } } };
247         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
248         int ret;
249
250         if (unlikely(!tb[IPSET_ATTR_IP] ||
251                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
252                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
253                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
254                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
255                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
256                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE) ||
257                      tb[IPSET_ATTR_IP_TO] ||
258                      tb[IPSET_ATTR_CIDR]))
259                 return -IPSET_ERR_PROTOCOL;
260
261         if (tb[IPSET_ATTR_LINENO])
262                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
263
264         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
265               ip_set_get_extensions(set, tb, &ext);
266         if (ret)
267                 return ret;
268
269         hash_ip6_netmask(&e.ip, h->netmask);
270         if (ipv6_addr_any(&e.ip.in6))
271                 return -IPSET_ERR_HASH_ELEM;
272
273         ret = adtfn(set, &e, &ext, &ext, flags);
274
275         return ip_set_eexist(ret, flags) ? 0 : ret;
276 }
277
278 static struct ip_set_type hash_ip_type __read_mostly = {
279         .name           = "hash:ip",
280         .protocol       = IPSET_PROTOCOL,
281         .features       = IPSET_TYPE_IP,
282         .dimension      = IPSET_DIM_ONE,
283         .family         = NFPROTO_UNSPEC,
284         .revision_min   = IPSET_TYPE_REV_MIN,
285         .revision_max   = IPSET_TYPE_REV_MAX,
286         .create         = hash_ip_create,
287         .create_policy  = {
288                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
289                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
290                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
291                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
292                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
293                 [IPSET_ATTR_NETMASK]    = { .type = NLA_U8  },
294                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
295         },
296         .adt_policy     = {
297                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
298                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
299                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
300                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
301                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
302                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
303                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
304                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
305                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
306                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
307                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
308         },
309         .me             = THIS_MODULE,
310 };
311
312 static int __init
313 hash_ip_init(void)
314 {
315         return ip_set_type_register(&hash_ip_type);
316 }
317
318 static void __exit
319 hash_ip_fini(void)
320 {
321         ip_set_type_unregister(&hash_ip_type);
322 }
323
324 module_init(hash_ip_init);
325 module_exit(hash_ip_fini);