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