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