Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / net / netfilter / ipset / ip_set_hash_ipport.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,port 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_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
26
27 #define IPSET_TYPE_REV_MIN      0
28 /*                              1    SCTP and UDPLITE support added */
29 /*                              2    Counters support added */
30 /*                              3    Comments support added */
31 #define IPSET_TYPE_REV_MAX      4 /* Forceadd support added */
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
36 MODULE_ALIAS("ip_set_hash:ip,port");
37
38 /* Type specific function prefix */
39 #define HTYPE           hash_ipport
40
41 /* IPv4 variant */
42
43 /* Member elements */
44 struct hash_ipport4_elem {
45         __be32 ip;
46         __be16 port;
47         u8 proto;
48         u8 padding;
49 };
50
51 /* Common functions */
52
53 static inline bool
54 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
55                         const struct hash_ipport4_elem *ip2,
56                         u32 *multi)
57 {
58         return ip1->ip == ip2->ip &&
59                ip1->port == ip2->port &&
60                ip1->proto == ip2->proto;
61 }
62
63 static bool
64 hash_ipport4_data_list(struct sk_buff *skb,
65                        const struct hash_ipport4_elem *data)
66 {
67         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
68             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
69             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
70                 goto nla_put_failure;
71         return 0;
72
73 nla_put_failure:
74         return 1;
75 }
76
77 static inline void
78 hash_ipport4_data_next(struct hash_ipport4_elem *next,
79                        const struct hash_ipport4_elem *d)
80 {
81         next->ip = d->ip;
82         next->port = d->port;
83 }
84
85 #define MTYPE           hash_ipport4
86 #define PF              4
87 #define HOST_MASK       32
88 #define HKEY_DATALEN    sizeof(struct hash_ipport4_elem)
89 #include "ip_set_hash_gen.h"
90
91 static int
92 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
93                   const struct xt_action_param *par,
94                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
95 {
96         ipset_adtfn adtfn = set->variant->adt[adt];
97         struct hash_ipport4_elem e = { };
98         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
99
100         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
101                                  &e.port, &e.proto))
102                 return -EINVAL;
103
104         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
105         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
106 }
107
108 static int
109 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
110                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
111 {
112         const struct hash_ipport *h = set->data;
113         ipset_adtfn adtfn = set->variant->adt[adt];
114         struct hash_ipport4_elem e = { };
115         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
116         u32 ip, ip_to = 0, p = 0, port, port_to;
117         bool with_ports = false;
118         int ret;
119
120         if (unlikely(!tb[IPSET_ATTR_IP] ||
121                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
122                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
123                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
124                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
125                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
126                 return -IPSET_ERR_PROTOCOL;
127
128         if (tb[IPSET_ATTR_LINENO])
129                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
130
131         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip) ||
132               ip_set_get_extensions(set, tb, &ext);
133         if (ret)
134                 return ret;
135
136         if (tb[IPSET_ATTR_PORT])
137                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
138         else
139                 return -IPSET_ERR_PROTOCOL;
140
141         if (tb[IPSET_ATTR_PROTO]) {
142                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
143                 with_ports = ip_set_proto_with_ports(e.proto);
144
145                 if (e.proto == 0)
146                         return -IPSET_ERR_INVALID_PROTO;
147         } else
148                 return -IPSET_ERR_MISSING_PROTO;
149
150         if (!(with_ports || e.proto == IPPROTO_ICMP))
151                 e.port = 0;
152
153         if (adt == IPSET_TEST ||
154             !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
155               tb[IPSET_ATTR_PORT_TO])) {
156                 ret = adtfn(set, &e, &ext, &ext, flags);
157                 return ip_set_eexist(ret, flags) ? 0 : ret;
158         }
159
160         ip_to = ip = ntohl(e.ip);
161         if (tb[IPSET_ATTR_IP_TO]) {
162                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
163                 if (ret)
164                         return ret;
165                 if (ip > ip_to)
166                         swap(ip, ip_to);
167         } else if (tb[IPSET_ATTR_CIDR]) {
168                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
169
170                 if (!cidr || cidr > 32)
171                         return -IPSET_ERR_INVALID_CIDR;
172                 ip_set_mask_from_to(ip, ip_to, cidr);
173         }
174
175         port_to = port = ntohs(e.port);
176         if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
177                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
178                 if (port > port_to)
179                         swap(port, port_to);
180         }
181
182         if (retried)
183                 ip = ntohl(h->next.ip);
184         for (; !before(ip_to, ip); ip++) {
185                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
186                                                        : port;
187                 for (; p <= port_to; p++) {
188                         e.ip = htonl(ip);
189                         e.port = htons(p);
190                         ret = adtfn(set, &e, &ext, &ext, flags);
191
192                         if (ret && !ip_set_eexist(ret, flags))
193                                 return ret;
194                         else
195                                 ret = 0;
196                 }
197         }
198         return ret;
199 }
200
201 /* IPv6 variant */
202
203 struct hash_ipport6_elem {
204         union nf_inet_addr ip;
205         __be16 port;
206         u8 proto;
207         u8 padding;
208 };
209
210 /* Common functions */
211
212 static inline bool
213 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
214                         const struct hash_ipport6_elem *ip2,
215                         u32 *multi)
216 {
217         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
218                ip1->port == ip2->port &&
219                ip1->proto == ip2->proto;
220 }
221
222 static bool
223 hash_ipport6_data_list(struct sk_buff *skb,
224                        const struct hash_ipport6_elem *data)
225 {
226         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
227             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
228             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
229                 goto nla_put_failure;
230         return 0;
231
232 nla_put_failure:
233         return 1;
234 }
235
236 static inline void
237 hash_ipport6_data_next(struct hash_ipport4_elem *next,
238                        const struct hash_ipport6_elem *d)
239 {
240         next->port = d->port;
241 }
242
243 #undef MTYPE
244 #undef PF
245 #undef HOST_MASK
246 #undef HKEY_DATALEN
247
248 #define MTYPE           hash_ipport6
249 #define PF              6
250 #define HOST_MASK       128
251 #define HKEY_DATALEN    sizeof(struct hash_ipport6_elem)
252 #define IP_SET_EMIT_CREATE
253 #include "ip_set_hash_gen.h"
254
255 static int
256 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
257                   const struct xt_action_param *par,
258                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
259 {
260         ipset_adtfn adtfn = set->variant->adt[adt];
261         struct hash_ipport6_elem e = { };
262         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
263
264         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
265                                  &e.port, &e.proto))
266                 return -EINVAL;
267
268         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
269         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
270 }
271
272 static int
273 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
274                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
275 {
276         const struct hash_ipport *h = set->data;
277         ipset_adtfn adtfn = set->variant->adt[adt];
278         struct hash_ipport6_elem e = { };
279         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
280         u32 port, port_to;
281         bool with_ports = false;
282         int ret;
283
284         if (unlikely(!tb[IPSET_ATTR_IP] ||
285                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
286                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
287                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
288                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
289                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
290                      tb[IPSET_ATTR_IP_TO] ||
291                      tb[IPSET_ATTR_CIDR]))
292                 return -IPSET_ERR_PROTOCOL;
293
294         if (tb[IPSET_ATTR_LINENO])
295                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
296
297         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
298               ip_set_get_extensions(set, tb, &ext);
299         if (ret)
300                 return ret;
301
302         if (tb[IPSET_ATTR_PORT])
303                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
304         else
305                 return -IPSET_ERR_PROTOCOL;
306
307         if (tb[IPSET_ATTR_PROTO]) {
308                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
309                 with_ports = ip_set_proto_with_ports(e.proto);
310
311                 if (e.proto == 0)
312                         return -IPSET_ERR_INVALID_PROTO;
313         } else
314                 return -IPSET_ERR_MISSING_PROTO;
315
316         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
317                 e.port = 0;
318
319         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
320                 ret = adtfn(set, &e, &ext, &ext, flags);
321                 return ip_set_eexist(ret, flags) ? 0 : ret;
322         }
323
324         port = ntohs(e.port);
325         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
326         if (port > port_to)
327                 swap(port, port_to);
328
329         if (retried)
330                 port = ntohs(h->next.port);
331         for (; port <= port_to; port++) {
332                 e.port = htons(port);
333                 ret = adtfn(set, &e, &ext, &ext, flags);
334
335                 if (ret && !ip_set_eexist(ret, flags))
336                         return ret;
337                 else
338                         ret = 0;
339         }
340         return ret;
341 }
342
343 static struct ip_set_type hash_ipport_type __read_mostly = {
344         .name           = "hash:ip,port",
345         .protocol       = IPSET_PROTOCOL,
346         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT,
347         .dimension      = IPSET_DIM_TWO,
348         .family         = NFPROTO_UNSPEC,
349         .revision_min   = IPSET_TYPE_REV_MIN,
350         .revision_max   = IPSET_TYPE_REV_MAX,
351         .create         = hash_ipport_create,
352         .create_policy  = {
353                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
354                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
355                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
356                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
357                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
358                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
359                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
360         },
361         .adt_policy     = {
362                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
363                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
364                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
365                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
366                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
367                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
368                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
369                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
370                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
371                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
372                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
373         },
374         .me             = THIS_MODULE,
375 };
376
377 static int __init
378 hash_ipport_init(void)
379 {
380         return ip_set_type_register(&hash_ipport_type);
381 }
382
383 static void __exit
384 hash_ipport_fini(void)
385 {
386         ip_set_type_unregister(&hash_ipport_type);
387 }
388
389 module_init(hash_ipport_init);
390 module_exit(hash_ipport_fini);