]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/ipset/ip_set_hash_netportnet.c
netfilter: ipset: Limit the maximal range of consecutive elements to add/delete
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / ipset / ip_set_hash_netportnet.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1c5ba67d 2/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
7c3ad056
OS
3
4/* Kernel module implementing an IP set type: the hash:ip,port,net type */
5
6#include <linux/jhash.h>
7#include <linux/module.h>
8#include <linux/ip.h>
9#include <linux/skbuff.h>
10#include <linux/errno.h>
11#include <linux/random.h>
12#include <net/ip.h>
13#include <net/ipv6.h>
14#include <net/netlink.h>
15#include <net/tcp.h>
16
17#include <linux/netfilter.h>
18#include <linux/netfilter/ipset/pfxlen.h>
19#include <linux/netfilter/ipset/ip_set.h>
20#include <linux/netfilter/ipset/ip_set_getport.h>
21#include <linux/netfilter/ipset/ip_set_hash.h>
22
23#define IPSET_TYPE_REV_MIN 0
07cf8f5a 24/* 0 Comments support added */
af331419 25/* 1 Forceadd support added */
ccf0a4b7 26/* 2 skbinfo support added */
3976ca10 27#define IPSET_TYPE_REV_MAX 3 /* bucketsize, initval support added */
7c3ad056
OS
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
31IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32MODULE_ALIAS("ip_set_hash:net,port,net");
33
34/* Type specific function prefix */
35#define HTYPE hash_netportnet
36#define IP_SET_HASH_WITH_PROTO
37#define IP_SET_HASH_WITH_NETS
38#define IPSET_NET_COUNT 2
39
40/* IPv4 variant */
41
42/* Member elements */
43struct hash_netportnet4_elem {
44 union {
45 __be32 ip[2];
46 __be64 ipcmp;
47 };
48 __be16 port;
49 union {
50 u8 cidr[2];
51 u16 ccmp;
52 };
cac37639 53 u16 padding;
2b67d6e0 54 u8 nomatch;
7c3ad056
OS
55 u8 proto;
56};
57
58/* Common functions */
59
8dea982a 60static bool
7c3ad056 61hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
ca0f6a5c
JK
62 const struct hash_netportnet4_elem *ip2,
63 u32 *multi)
7c3ad056
OS
64{
65 return ip1->ipcmp == ip2->ipcmp &&
66 ip1->ccmp == ip2->ccmp &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto;
69}
70
8dea982a 71static int
7c3ad056
OS
72hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
73{
74 return elem->nomatch ? -ENOTEMPTY : 1;
75}
76
8dea982a 77static void
7c3ad056
OS
78hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
79{
80 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
81}
82
8dea982a 83static void
7c3ad056
OS
84hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
85{
86 swap(*flags, elem->nomatch);
87}
88
8dea982a 89static void
7c3ad056 90hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
ca0f6a5c 91 struct hash_netportnet4_elem *orig)
7c3ad056
OS
92{
93 elem->ip[1] = orig->ip[1];
94}
95
8dea982a 96static void
7c3ad056
OS
97hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
98 u8 cidr, bool inner)
99{
100 if (inner) {
101 elem->ip[1] &= ip_set_netmask(cidr);
102 elem->cidr[1] = cidr;
103 } else {
104 elem->ip[0] &= ip_set_netmask(cidr);
105 elem->cidr[0] = cidr;
106 }
107}
108
109static bool
110hash_netportnet4_data_list(struct sk_buff *skb,
ca0f6a5c 111 const struct hash_netportnet4_elem *data)
7c3ad056
OS
112{
113 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
114
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
116 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
117 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
118 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
119 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
120 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
121 (flags &&
122 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
123 goto nla_put_failure;
728a7e69 124 return false;
7c3ad056
OS
125
126nla_put_failure:
728a7e69 127 return true;
7c3ad056
OS
128}
129
8dea982a 130static void
7c3ad056 131hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
ca0f6a5c 132 const struct hash_netportnet4_elem *d)
7c3ad056
OS
133{
134 next->ipcmp = d->ipcmp;
135 next->port = d->port;
136}
137
138#define MTYPE hash_netportnet4
7c3ad056
OS
139#define HOST_MASK 32
140#include "ip_set_hash_gen.h"
141
96be5f28
ER
142static void
143hash_netportnet4_init(struct hash_netportnet4_elem *e)
144{
145 e->cidr[0] = HOST_MASK;
146 e->cidr[1] = HOST_MASK;
147}
148
7c3ad056
OS
149static int
150hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
ca0f6a5c
JK
151 const struct xt_action_param *par,
152 enum ipset_adt adt, struct ip_set_adt_opt *opt)
7c3ad056 153{
21956ab2 154 const struct hash_netportnet4 *h = set->data;
7c3ad056 155 ipset_adtfn adtfn = set->variant->adt[adt];
1a869205 156 struct hash_netportnet4_elem e = { };
7c3ad056
OS
157 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
158
f690cbae
JK
159 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
160 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
7c3ad056
OS
161 if (adt == IPSET_TEST)
162 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
163
164 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
165 &e.port, &e.proto))
166 return -EINVAL;
167
168 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
169 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
170 e.ip[0] &= ip_set_netmask(e.cidr[0]);
171 e.ip[1] &= ip_set_netmask(e.cidr[1]);
172
173 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
174}
175
176static int
177hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
ca0f6a5c 178 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
7c3ad056 179{
21956ab2 180 const struct hash_netportnet4 *h = set->data;
7c3ad056 181 ipset_adtfn adtfn = set->variant->adt[adt];
96be5f28 182 struct hash_netportnet4_elem e = { };
7c3ad056 183 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
0b8d9073 184 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
aa67f109
JK
185 u32 ip2_from = 0, ip2_to = 0, ip2, ipn;
186 u64 n = 0, m = 0;
7c3ad056 187 bool with_ports = false;
7c3ad056
OS
188 int ret;
189
a212e08e
SP
190 if (tb[IPSET_ATTR_LINENO])
191 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
192
96be5f28 193 hash_netportnet4_init(&e);
7c3ad056
OS
194 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
195 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
196 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
7dd37bc8 197 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
7c3ad056
OS
198 return -IPSET_ERR_PROTOCOL;
199
8e55d2e5
SP
200 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
201 if (ret)
202 return ret;
203
204 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
205 if (ret)
206 return ret;
207
208 ret = ip_set_get_extensions(set, tb, &ext);
7c3ad056
OS
209 if (ret)
210 return ret;
211
212 if (tb[IPSET_ATTR_CIDR]) {
aff22758 213 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
886503f3 214 if (e.cidr[0] > HOST_MASK)
7c3ad056 215 return -IPSET_ERR_INVALID_CIDR;
7c3ad056
OS
216 }
217
218 if (tb[IPSET_ATTR_CIDR2]) {
aff22758 219 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
886503f3 220 if (e.cidr[1] > HOST_MASK)
7c3ad056 221 return -IPSET_ERR_INVALID_CIDR;
7c3ad056
OS
222 }
223
d25472e4 224 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
7c3ad056
OS
225
226 if (tb[IPSET_ATTR_PROTO]) {
227 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
228 with_ports = ip_set_proto_with_ports(e.proto);
229
230 if (e.proto == 0)
231 return -IPSET_ERR_INVALID_PROTO;
ca0f6a5c 232 } else {
7c3ad056 233 return -IPSET_ERR_MISSING_PROTO;
ca0f6a5c 234 }
7c3ad056
OS
235
236 if (!(with_ports || e.proto == IPPROTO_ICMP))
237 e.port = 0;
238
239 if (tb[IPSET_ATTR_CADT_FLAGS]) {
240 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
ca0f6a5c 241
7c3ad056
OS
242 if (cadt_flags & IPSET_FLAG_NOMATCH)
243 flags |= (IPSET_FLAG_NOMATCH << 16);
244 }
245
246 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
247 if (adt == IPSET_TEST ||
248 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
249 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
250 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
251 ret = adtfn(set, &e, &ext, &ext, flags);
252 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
253 ip_set_eexist(ret, flags) ? 0 : ret;
254 }
255
256 ip_to = ip;
257 if (tb[IPSET_ATTR_IP_TO]) {
258 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
259 if (ret)
260 return ret;
261 if (ip > ip_to)
262 swap(ip, ip_to);
263 if (unlikely(ip + UINT_MAX == ip_to))
264 return -IPSET_ERR_HASH_RANGE;
ca0f6a5c 265 } else {
6e41ee68 266 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
ca0f6a5c 267 }
7c3ad056
OS
268
269 port_to = port = ntohs(e.port);
270 if (tb[IPSET_ATTR_PORT_TO]) {
271 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
272 if (port > port_to)
273 swap(port, port_to);
274 }
275
276 ip2_to = ip2_from;
277 if (tb[IPSET_ATTR_IP2_TO]) {
278 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
279 if (ret)
280 return ret;
281 if (ip2_from > ip2_to)
282 swap(ip2_from, ip2_to);
283 if (unlikely(ip2_from + UINT_MAX == ip2_to))
284 return -IPSET_ERR_HASH_RANGE;
ca0f6a5c 285 } else {
6e41ee68 286 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
ca0f6a5c 287 }
aa67f109
JK
288 ipn = ip;
289 do {
290 ipn = ip_set_range_to_cidr(ipn, ip_to, &e.cidr[0]);
291 n++;
292 } while (ipn++ < ip_to);
293 ipn = ip2_from;
294 do {
295 ipn = ip_set_range_to_cidr(ipn, ip2_to, &e.cidr[1]);
296 m++;
297 } while (ipn++ < ip2_to);
298
299 if (n*m*(port_to - port + 1) > IPSET_MAX_RANGE)
300 return -ERANGE;
7c3ad056 301
0b8d9073 302 if (retried) {
7c3ad056 303 ip = ntohl(h->next.ip[0]);
0b8d9073
JK
304 p = ntohs(h->next.port);
305 ip2 = ntohl(h->next.ip[1]);
306 } else {
307 p = port;
308 ip2 = ip2_from;
309 }
7c3ad056 310
0b8d9073 311 do {
7c3ad056 312 e.ip[0] = htonl(ip);
0b8d9073 313 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
7c3ad056
OS
314 for (; p <= port_to; p++) {
315 e.port = htons(p);
0b8d9073 316 do {
7c3ad056 317 e.ip[1] = htonl(ip2);
0b8d9073
JK
318 ip2 = ip_set_range_to_cidr(ip2, ip2_to,
319 &e.cidr[1]);
7c3ad056
OS
320 ret = adtfn(set, &e, &ext, &ext, flags);
321 if (ret && !ip_set_eexist(ret, flags))
322 return ret;
ca0f6a5c
JK
323
324 ret = 0;
0b8d9073
JK
325 } while (ip2++ < ip2_to);
326 ip2 = ip2_from;
7c3ad056 327 }
0b8d9073
JK
328 p = port;
329 } while (ip++ < ip_to);
7c3ad056
OS
330 return ret;
331}
332
333/* IPv6 variant */
334
335struct hash_netportnet6_elem {
336 union nf_inet_addr ip[2];
337 __be16 port;
338 union {
339 u8 cidr[2];
340 u16 ccmp;
341 };
cac37639 342 u16 padding;
2b67d6e0 343 u8 nomatch;
7c3ad056
OS
344 u8 proto;
345};
346
347/* Common functions */
348
8dea982a 349static bool
7c3ad056 350hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
ca0f6a5c
JK
351 const struct hash_netportnet6_elem *ip2,
352 u32 *multi)
7c3ad056
OS
353{
354 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
355 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
356 ip1->ccmp == ip2->ccmp &&
357 ip1->port == ip2->port &&
358 ip1->proto == ip2->proto;
359}
360
8dea982a 361static int
7c3ad056
OS
362hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
363{
364 return elem->nomatch ? -ENOTEMPTY : 1;
365}
366
8dea982a 367static void
7c3ad056
OS
368hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
369{
370 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
371}
372
8dea982a 373static void
7c3ad056
OS
374hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
375{
376 swap(*flags, elem->nomatch);
377}
378
8dea982a 379static void
7c3ad056 380hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
ca0f6a5c 381 struct hash_netportnet6_elem *orig)
7c3ad056
OS
382{
383 elem->ip[1] = orig->ip[1];
384}
385
8dea982a 386static void
7c3ad056
OS
387hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
388 u8 cidr, bool inner)
389{
390 if (inner) {
391 ip6_netmask(&elem->ip[1], cidr);
392 elem->cidr[1] = cidr;
393 } else {
394 ip6_netmask(&elem->ip[0], cidr);
395 elem->cidr[0] = cidr;
396 }
397}
398
399static bool
400hash_netportnet6_data_list(struct sk_buff *skb,
ca0f6a5c 401 const struct hash_netportnet6_elem *data)
7c3ad056
OS
402{
403 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
404
405 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
406 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
407 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
408 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
409 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
410 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
411 (flags &&
412 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
413 goto nla_put_failure;
728a7e69 414 return false;
7c3ad056
OS
415
416nla_put_failure:
728a7e69 417 return true;
7c3ad056
OS
418}
419
8dea982a 420static void
21956ab2 421hash_netportnet6_data_next(struct hash_netportnet6_elem *next,
ca0f6a5c 422 const struct hash_netportnet6_elem *d)
7c3ad056
OS
423{
424 next->port = d->port;
425}
426
427#undef MTYPE
7c3ad056
OS
428#undef HOST_MASK
429
430#define MTYPE hash_netportnet6
7c3ad056
OS
431#define HOST_MASK 128
432#define IP_SET_EMIT_CREATE
433#include "ip_set_hash_gen.h"
434
96be5f28
ER
435static void
436hash_netportnet6_init(struct hash_netportnet6_elem *e)
437{
438 e->cidr[0] = HOST_MASK;
439 e->cidr[1] = HOST_MASK;
440}
441
7c3ad056
OS
442static int
443hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
ca0f6a5c
JK
444 const struct xt_action_param *par,
445 enum ipset_adt adt, struct ip_set_adt_opt *opt)
7c3ad056 446{
21956ab2 447 const struct hash_netportnet6 *h = set->data;
7c3ad056 448 ipset_adtfn adtfn = set->variant->adt[adt];
1a869205 449 struct hash_netportnet6_elem e = { };
7c3ad056
OS
450 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
451
f690cbae
JK
452 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
453 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
7c3ad056
OS
454 if (adt == IPSET_TEST)
455 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
456
457 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
458 &e.port, &e.proto))
459 return -EINVAL;
460
461 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
462 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
463 ip6_netmask(&e.ip[0], e.cidr[0]);
464 ip6_netmask(&e.ip[1], e.cidr[1]);
465
466 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
467}
468
469static int
470hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
ca0f6a5c 471 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
7c3ad056 472{
21956ab2 473 const struct hash_netportnet6 *h = set->data;
7c3ad056 474 ipset_adtfn adtfn = set->variant->adt[adt];
96be5f28 475 struct hash_netportnet6_elem e = { };
7c3ad056
OS
476 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
477 u32 port, port_to;
478 bool with_ports = false;
479 int ret;
480
a212e08e
SP
481 if (tb[IPSET_ATTR_LINENO])
482 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
483
96be5f28 484 hash_netportnet6_init(&e);
7c3ad056
OS
485 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
486 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
487 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
7dd37bc8 488 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
7c3ad056
OS
489 return -IPSET_ERR_PROTOCOL;
490 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
491 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
492
8e55d2e5
SP
493 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
494 if (ret)
495 return ret;
496
497 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
498 if (ret)
499 return ret;
500
501 ret = ip_set_get_extensions(set, tb, &ext);
7c3ad056
OS
502 if (ret)
503 return ret;
504
aff22758 505 if (tb[IPSET_ATTR_CIDR]) {
7c3ad056 506 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
886503f3 507 if (e.cidr[0] > HOST_MASK)
aff22758
SP
508 return -IPSET_ERR_INVALID_CIDR;
509 }
7c3ad056 510
aff22758 511 if (tb[IPSET_ATTR_CIDR2]) {
7c3ad056 512 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
886503f3 513 if (e.cidr[1] > HOST_MASK)
aff22758
SP
514 return -IPSET_ERR_INVALID_CIDR;
515 }
7c3ad056
OS
516
517 ip6_netmask(&e.ip[0], e.cidr[0]);
518 ip6_netmask(&e.ip[1], e.cidr[1]);
519
d25472e4 520 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
7c3ad056
OS
521
522 if (tb[IPSET_ATTR_PROTO]) {
523 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
524 with_ports = ip_set_proto_with_ports(e.proto);
525
526 if (e.proto == 0)
527 return -IPSET_ERR_INVALID_PROTO;
ca0f6a5c 528 } else {
7c3ad056 529 return -IPSET_ERR_MISSING_PROTO;
ca0f6a5c 530 }
7c3ad056
OS
531
532 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
533 e.port = 0;
534
535 if (tb[IPSET_ATTR_CADT_FLAGS]) {
536 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
ca0f6a5c 537
7c3ad056
OS
538 if (cadt_flags & IPSET_FLAG_NOMATCH)
539 flags |= (IPSET_FLAG_NOMATCH << 16);
540 }
541
542 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
543 ret = adtfn(set, &e, &ext, &ext, flags);
544 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
545 ip_set_eexist(ret, flags) ? 0 : ret;
546 }
547
548 port = ntohs(e.port);
549 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
550 if (port > port_to)
551 swap(port, port_to);
552
553 if (retried)
554 port = ntohs(h->next.port);
555 for (; port <= port_to; port++) {
556 e.port = htons(port);
557 ret = adtfn(set, &e, &ext, &ext, flags);
558
559 if (ret && !ip_set_eexist(ret, flags))
560 return ret;
ca0f6a5c
JK
561
562 ret = 0;
7c3ad056
OS
563 }
564 return ret;
565}
566
567static struct ip_set_type hash_netportnet_type __read_mostly = {
568 .name = "hash:net,port,net",
569 .protocol = IPSET_PROTOCOL,
570 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
571 IPSET_TYPE_NOMATCH,
572 .dimension = IPSET_DIM_THREE,
573 .family = NFPROTO_UNSPEC,
574 .revision_min = IPSET_TYPE_REV_MIN,
575 .revision_max = IPSET_TYPE_REV_MAX,
ccf0a4b7 576 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
7c3ad056
OS
577 .create = hash_netportnet_create,
578 .create_policy = {
579 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
580 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
3976ca10 581 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
ccf0a4b7 582 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
7c3ad056
OS
583 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
584 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
585 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
586 },
587 .adt_policy = {
588 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
589 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
590 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
591 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
592 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
593 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
594 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
595 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
596 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
597 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
598 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
599 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
600 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
601 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
03726186
SP
602 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
603 .len = IPSET_MAX_COMMENT_SIZE },
af331419
AD
604 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
605 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
606 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
7c3ad056
OS
607 },
608 .me = THIS_MODULE,
609};
610
611static int __init
612hash_netportnet_init(void)
613{
614 return ip_set_type_register(&hash_netportnet_type);
615}
616
617static void __exit
618hash_netportnet_fini(void)
619{
18f84d41 620 rcu_barrier();
7c3ad056
OS
621 ip_set_type_unregister(&hash_netportnet_type);
622}
623
624module_init(hash_netportnet_init);
625module_exit(hash_netportnet_fini);