1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
4 /* Kernel module implementing an IP set type: the hash:ip,port,net type */
6 #include <linux/jhash.h>
7 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
11 #include <linux/random.h>
14 #include <net/netlink.h>
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>
23 #define IPSET_TYPE_REV_MIN 0
24 /* 1 SCTP and UDPLITE support added */
25 /* 2 Range as input support for IPv4 added */
26 /* 3 nomatch flag support added */
27 /* 4 Counters support added */
28 /* 5 Comments support added */
29 /* 6 Forceadd support added */
30 #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
35 MODULE_ALIAS("ip_set_hash:ip,port,net");
37 /* Type specific function prefix */
38 #define HTYPE hash_ipportnet
40 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
41 * However this way we have to store internally cidr - 1,
42 * dancing back and forth.
44 #define IP_SET_HASH_WITH_NETS_PACKED
45 #define IP_SET_HASH_WITH_PROTO
46 #define IP_SET_HASH_WITH_NETS
51 struct hash_ipportnet4_elem
{
60 /* Common functions */
63 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem
*ip1
,
64 const struct hash_ipportnet4_elem
*ip2
,
67 return ip1
->ip
== ip2
->ip
&&
68 ip1
->ip2
== ip2
->ip2
&&
69 ip1
->cidr
== ip2
->cidr
&&
70 ip1
->port
== ip2
->port
&&
71 ip1
->proto
== ip2
->proto
;
75 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem
*elem
)
77 return elem
->nomatch
? -ENOTEMPTY
: 1;
81 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem
*elem
, u32 flags
)
83 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
87 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem
*elem
, u8
*flags
)
89 swap(*flags
, elem
->nomatch
);
93 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem
*elem
, u8 cidr
)
95 elem
->ip2
&= ip_set_netmask(cidr
);
96 elem
->cidr
= cidr
- 1;
100 hash_ipportnet4_data_list(struct sk_buff
*skb
,
101 const struct hash_ipportnet4_elem
*data
)
103 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
105 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
106 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, data
->ip2
) ||
107 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
108 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
+ 1) ||
109 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
111 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
112 goto nla_put_failure
;
120 hash_ipportnet4_data_next(struct hash_ipportnet4_elem
*next
,
121 const struct hash_ipportnet4_elem
*d
)
124 next
->port
= d
->port
;
128 #define MTYPE hash_ipportnet4
130 #include "ip_set_hash_gen.h"
133 hash_ipportnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
134 const struct xt_action_param
*par
,
135 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
137 const struct hash_ipportnet4
*h
= set
->data
;
138 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
139 struct hash_ipportnet4_elem e
= {
140 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
142 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
144 if (adt
== IPSET_TEST
)
145 e
.cidr
= HOST_MASK
- 1;
147 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
151 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
152 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip2
);
153 e
.ip2
&= ip_set_netmask(e
.cidr
+ 1);
155 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
159 hash_ipportnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
160 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
162 const struct hash_ipportnet4
*h
= set
->data
;
163 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
164 struct hash_ipportnet4_elem e
= { .cidr
= HOST_MASK
- 1 };
165 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
166 u32 ip
= 0, ip_to
= 0, p
= 0, port
, port_to
;
167 u32 ip2_from
= 0, ip2_to
= 0, ip2
;
168 bool with_ports
= false;
172 if (tb
[IPSET_ATTR_LINENO
])
173 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
175 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
176 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
177 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
178 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
179 return -IPSET_ERR_PROTOCOL
;
181 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
185 ret
= ip_set_get_extensions(set
, tb
, &ext
);
189 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2
], &ip2_from
);
193 if (tb
[IPSET_ATTR_CIDR2
]) {
194 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
195 if (!cidr
|| cidr
> HOST_MASK
)
196 return -IPSET_ERR_INVALID_CIDR
;
200 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
202 if (tb
[IPSET_ATTR_PROTO
]) {
203 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
204 with_ports
= ip_set_proto_with_ports(e
.proto
);
207 return -IPSET_ERR_INVALID_PROTO
;
209 return -IPSET_ERR_MISSING_PROTO
;
212 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
215 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
216 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
218 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
219 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
222 with_ports
= with_ports
&& tb
[IPSET_ATTR_PORT_TO
];
223 if (adt
== IPSET_TEST
||
224 !(tb
[IPSET_ATTR_CIDR
] || tb
[IPSET_ATTR_IP_TO
] || with_ports
||
225 tb
[IPSET_ATTR_IP2_TO
])) {
227 e
.ip2
= htonl(ip2_from
& ip_set_hostmask(e
.cidr
+ 1));
228 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
229 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
230 ip_set_eexist(ret
, flags
) ? 0 : ret
;
234 if (tb
[IPSET_ATTR_IP_TO
]) {
235 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
240 } else if (tb
[IPSET_ATTR_CIDR
]) {
241 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
243 if (!cidr
|| cidr
> HOST_MASK
)
244 return -IPSET_ERR_INVALID_CIDR
;
245 ip_set_mask_from_to(ip
, ip_to
, cidr
);
248 port_to
= port
= ntohs(e
.port
);
249 if (tb
[IPSET_ATTR_PORT_TO
]) {
250 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
256 if (tb
[IPSET_ATTR_IP2_TO
]) {
257 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2_TO
], &ip2_to
);
260 if (ip2_from
> ip2_to
)
261 swap(ip2_from
, ip2_to
);
262 if (ip2_from
+ UINT_MAX
== ip2_to
)
263 return -IPSET_ERR_HASH_RANGE
;
265 ip_set_mask_from_to(ip2_from
, ip2_to
, e
.cidr
+ 1);
269 ip
= ntohl(h
->next
.ip
);
270 p
= ntohs(h
->next
.port
);
271 ip2
= ntohl(h
->next
.ip2
);
276 for (; ip
<= ip_to
; ip
++) {
278 for (; p
<= port_to
; p
++) {
282 ip2
= ip_set_range_to_cidr(ip2
, ip2_to
, &cidr
);
284 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
286 if (ret
&& !ip_set_eexist(ret
, flags
))
290 } while (ip2
++ < ip2_to
);
300 struct hash_ipportnet6_elem
{
301 union nf_inet_addr ip
;
302 union nf_inet_addr ip2
;
309 /* Common functions */
312 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem
*ip1
,
313 const struct hash_ipportnet6_elem
*ip2
,
316 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
317 ipv6_addr_equal(&ip1
->ip2
.in6
, &ip2
->ip2
.in6
) &&
318 ip1
->cidr
== ip2
->cidr
&&
319 ip1
->port
== ip2
->port
&&
320 ip1
->proto
== ip2
->proto
;
324 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem
*elem
)
326 return elem
->nomatch
? -ENOTEMPTY
: 1;
330 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem
*elem
, u32 flags
)
332 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
336 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem
*elem
, u8
*flags
)
338 swap(*flags
, elem
->nomatch
);
342 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem
*elem
, u8 cidr
)
344 ip6_netmask(&elem
->ip2
, cidr
);
345 elem
->cidr
= cidr
- 1;
349 hash_ipportnet6_data_list(struct sk_buff
*skb
,
350 const struct hash_ipportnet6_elem
*data
)
352 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
354 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
355 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip2
.in6
) ||
356 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
357 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
+ 1) ||
358 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
360 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
361 goto nla_put_failure
;
369 hash_ipportnet6_data_next(struct hash_ipportnet6_elem
*next
,
370 const struct hash_ipportnet6_elem
*d
)
372 next
->port
= d
->port
;
378 #define MTYPE hash_ipportnet6
379 #define HOST_MASK 128
380 #define IP_SET_EMIT_CREATE
381 #include "ip_set_hash_gen.h"
384 hash_ipportnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
385 const struct xt_action_param
*par
,
386 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
388 const struct hash_ipportnet6
*h
= set
->data
;
389 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
390 struct hash_ipportnet6_elem e
= {
391 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
393 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
395 if (adt
== IPSET_TEST
)
396 e
.cidr
= HOST_MASK
- 1;
398 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
402 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
403 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip2
.in6
);
404 ip6_netmask(&e
.ip2
, e
.cidr
+ 1);
406 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
410 hash_ipportnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
411 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
413 const struct hash_ipportnet6
*h
= set
->data
;
414 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
415 struct hash_ipportnet6_elem e
= { .cidr
= HOST_MASK
- 1 };
416 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
418 bool with_ports
= false;
422 if (tb
[IPSET_ATTR_LINENO
])
423 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
425 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
426 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
427 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
428 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
429 return -IPSET_ERR_PROTOCOL
;
430 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
431 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
432 if (unlikely(tb
[IPSET_ATTR_CIDR
])) {
433 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
435 if (cidr
!= HOST_MASK
)
436 return -IPSET_ERR_INVALID_CIDR
;
439 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
443 ret
= ip_set_get_extensions(set
, tb
, &ext
);
447 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &e
.ip2
);
451 if (tb
[IPSET_ATTR_CIDR2
]) {
452 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
453 if (!cidr
|| cidr
> HOST_MASK
)
454 return -IPSET_ERR_INVALID_CIDR
;
458 ip6_netmask(&e
.ip2
, e
.cidr
+ 1);
460 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
462 if (tb
[IPSET_ATTR_PROTO
]) {
463 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
464 with_ports
= ip_set_proto_with_ports(e
.proto
);
467 return -IPSET_ERR_INVALID_PROTO
;
469 return -IPSET_ERR_MISSING_PROTO
;
472 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
475 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
476 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
478 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
479 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
482 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
483 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
484 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
485 ip_set_eexist(ret
, flags
) ? 0 : ret
;
488 port
= ntohs(e
.port
);
489 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
494 port
= ntohs(h
->next
.port
);
495 for (; port
<= port_to
; port
++) {
496 e
.port
= htons(port
);
497 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
499 if (ret
&& !ip_set_eexist(ret
, flags
))
507 static struct ip_set_type hash_ipportnet_type __read_mostly
= {
508 .name
= "hash:ip,port,net",
509 .protocol
= IPSET_PROTOCOL
,
510 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
|
512 .dimension
= IPSET_DIM_THREE
,
513 .family
= NFPROTO_UNSPEC
,
514 .revision_min
= IPSET_TYPE_REV_MIN
,
515 .revision_max
= IPSET_TYPE_REV_MAX
,
516 .create
= hash_ipportnet_create
,
518 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
519 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
520 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
521 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
522 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
523 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
526 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
527 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
528 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
529 [IPSET_ATTR_IP2_TO
] = { .type
= NLA_NESTED
},
530 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
531 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
532 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
533 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
534 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
535 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
536 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
537 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
538 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
539 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
540 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
541 .len
= IPSET_MAX_COMMENT_SIZE
},
542 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
543 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
544 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
550 hash_ipportnet_init(void)
552 return ip_set_type_register(&hash_ipportnet_type
);
556 hash_ipportnet_fini(void)
559 ip_set_type_unregister(&hash_ipportnet_type
);
562 module_init(hash_ipportnet_init
);
563 module_exit(hash_ipportnet_fini
);