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