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