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