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 /* 7 skbinfo support added */
31 #define IPSET_TYPE_REV_MAX 8 /* bucketsize, initval support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
35 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
36 MODULE_ALIAS("ip_set_hash:ip,port,net");
38 /* Type specific function prefix */
39 #define HTYPE hash_ipportnet
41 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
42 * However this way we have to store internally cidr - 1,
43 * dancing back and forth.
45 #define IP_SET_HASH_WITH_NETS_PACKED
46 #define IP_SET_HASH_WITH_PROTO
47 #define IP_SET_HASH_WITH_NETS
52 struct hash_ipportnet4_elem
{
61 /* Common functions */
64 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem
*ip1
,
65 const struct hash_ipportnet4_elem
*ip2
,
68 return ip1
->ip
== ip2
->ip
&&
69 ip1
->ip2
== ip2
->ip2
&&
70 ip1
->cidr
== ip2
->cidr
&&
71 ip1
->port
== ip2
->port
&&
72 ip1
->proto
== ip2
->proto
;
76 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem
*elem
)
78 return elem
->nomatch
? -ENOTEMPTY
: 1;
82 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem
*elem
, u32 flags
)
84 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
88 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem
*elem
, u8
*flags
)
90 swap(*flags
, elem
->nomatch
);
94 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem
*elem
, u8 cidr
)
96 elem
->ip2
&= ip_set_netmask(cidr
);
97 elem
->cidr
= cidr
- 1;
101 hash_ipportnet4_data_list(struct sk_buff
*skb
,
102 const struct hash_ipportnet4_elem
*data
)
104 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
106 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
107 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, data
->ip2
) ||
108 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
109 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
+ 1) ||
110 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
112 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
113 goto nla_put_failure
;
121 hash_ipportnet4_data_next(struct hash_ipportnet4_elem
*next
,
122 const struct hash_ipportnet4_elem
*d
)
125 next
->port
= d
->port
;
129 #define MTYPE hash_ipportnet4
131 #include "ip_set_hash_gen.h"
134 hash_ipportnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
135 const struct xt_action_param
*par
,
136 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
138 const struct hash_ipportnet4
*h
= set
->data
;
139 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
140 struct hash_ipportnet4_elem e
= {
141 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
143 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
145 if (adt
== IPSET_TEST
)
146 e
.cidr
= HOST_MASK
- 1;
148 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
152 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
153 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip2
);
154 e
.ip2
&= ip_set_netmask(e
.cidr
+ 1);
156 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
160 hash_ipportnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
161 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
163 const struct hash_ipportnet4
*h
= set
->data
;
164 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
165 struct hash_ipportnet4_elem e
= { .cidr
= HOST_MASK
- 1 };
166 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
167 u32 ip
= 0, ip_to
= 0, p
= 0, port
, port_to
;
168 u32 ip2_from
= 0, ip2_to
= 0, ip2
;
169 bool with_ports
= false;
173 if (tb
[IPSET_ATTR_LINENO
])
174 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
176 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
177 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
178 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
179 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
180 return -IPSET_ERR_PROTOCOL
;
182 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
186 ret
= ip_set_get_extensions(set
, tb
, &ext
);
190 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2
], &ip2_from
);
194 if (tb
[IPSET_ATTR_CIDR2
]) {
195 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
196 if (!cidr
|| cidr
> HOST_MASK
)
197 return -IPSET_ERR_INVALID_CIDR
;
201 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
203 if (tb
[IPSET_ATTR_PROTO
]) {
204 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
205 with_ports
= ip_set_proto_with_ports(e
.proto
);
208 return -IPSET_ERR_INVALID_PROTO
;
210 return -IPSET_ERR_MISSING_PROTO
;
213 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
216 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
217 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
219 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
220 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
223 with_ports
= with_ports
&& tb
[IPSET_ATTR_PORT_TO
];
224 if (adt
== IPSET_TEST
||
225 !(tb
[IPSET_ATTR_CIDR
] || tb
[IPSET_ATTR_IP_TO
] || with_ports
||
226 tb
[IPSET_ATTR_IP2_TO
])) {
228 e
.ip2
= htonl(ip2_from
& ip_set_hostmask(e
.cidr
+ 1));
229 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
230 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
231 ip_set_eexist(ret
, flags
) ? 0 : ret
;
235 if (tb
[IPSET_ATTR_IP_TO
]) {
236 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
241 } else if (tb
[IPSET_ATTR_CIDR
]) {
242 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
244 if (!cidr
|| cidr
> HOST_MASK
)
245 return -IPSET_ERR_INVALID_CIDR
;
246 ip_set_mask_from_to(ip
, ip_to
, cidr
);
249 port_to
= port
= ntohs(e
.port
);
250 if (tb
[IPSET_ATTR_PORT_TO
]) {
251 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
257 if (tb
[IPSET_ATTR_IP2_TO
]) {
258 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2_TO
], &ip2_to
);
261 if (ip2_from
> ip2_to
)
262 swap(ip2_from
, ip2_to
);
263 if (ip2_from
+ UINT_MAX
== ip2_to
)
264 return -IPSET_ERR_HASH_RANGE
;
266 ip_set_mask_from_to(ip2_from
, ip2_to
, e
.cidr
+ 1);
270 ip
= ntohl(h
->next
.ip
);
271 p
= ntohs(h
->next
.port
);
272 ip2
= ntohl(h
->next
.ip2
);
277 for (; ip
<= ip_to
; ip
++) {
279 for (; p
<= port_to
; p
++) {
283 ip2
= ip_set_range_to_cidr(ip2
, ip2_to
, &cidr
);
285 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
287 if (ret
&& !ip_set_eexist(ret
, flags
))
291 } while (ip2
++ < ip2_to
);
301 struct hash_ipportnet6_elem
{
302 union nf_inet_addr ip
;
303 union nf_inet_addr ip2
;
310 /* Common functions */
313 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem
*ip1
,
314 const struct hash_ipportnet6_elem
*ip2
,
317 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
318 ipv6_addr_equal(&ip1
->ip2
.in6
, &ip2
->ip2
.in6
) &&
319 ip1
->cidr
== ip2
->cidr
&&
320 ip1
->port
== ip2
->port
&&
321 ip1
->proto
== ip2
->proto
;
325 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem
*elem
)
327 return elem
->nomatch
? -ENOTEMPTY
: 1;
331 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem
*elem
, u32 flags
)
333 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
337 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem
*elem
, u8
*flags
)
339 swap(*flags
, elem
->nomatch
);
343 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem
*elem
, u8 cidr
)
345 ip6_netmask(&elem
->ip2
, cidr
);
346 elem
->cidr
= cidr
- 1;
350 hash_ipportnet6_data_list(struct sk_buff
*skb
,
351 const struct hash_ipportnet6_elem
*data
)
353 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
355 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
356 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip2
.in6
) ||
357 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
358 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
+ 1) ||
359 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
361 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
362 goto nla_put_failure
;
370 hash_ipportnet6_data_next(struct hash_ipportnet6_elem
*next
,
371 const struct hash_ipportnet6_elem
*d
)
373 next
->port
= d
->port
;
379 #define MTYPE hash_ipportnet6
380 #define HOST_MASK 128
381 #define IP_SET_EMIT_CREATE
382 #include "ip_set_hash_gen.h"
385 hash_ipportnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
386 const struct xt_action_param
*par
,
387 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
389 const struct hash_ipportnet6
*h
= set
->data
;
390 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
391 struct hash_ipportnet6_elem e
= {
392 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
394 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
396 if (adt
== IPSET_TEST
)
397 e
.cidr
= HOST_MASK
- 1;
399 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
403 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
404 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip2
.in6
);
405 ip6_netmask(&e
.ip2
, e
.cidr
+ 1);
407 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
411 hash_ipportnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
412 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
414 const struct hash_ipportnet6
*h
= set
->data
;
415 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
416 struct hash_ipportnet6_elem e
= { .cidr
= HOST_MASK
- 1 };
417 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
419 bool with_ports
= false;
423 if (tb
[IPSET_ATTR_LINENO
])
424 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
426 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
427 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
428 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
429 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
430 return -IPSET_ERR_PROTOCOL
;
431 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
432 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
433 if (unlikely(tb
[IPSET_ATTR_CIDR
])) {
434 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
436 if (cidr
!= HOST_MASK
)
437 return -IPSET_ERR_INVALID_CIDR
;
440 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
444 ret
= ip_set_get_extensions(set
, tb
, &ext
);
448 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &e
.ip2
);
452 if (tb
[IPSET_ATTR_CIDR2
]) {
453 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
454 if (!cidr
|| cidr
> HOST_MASK
)
455 return -IPSET_ERR_INVALID_CIDR
;
459 ip6_netmask(&e
.ip2
, e
.cidr
+ 1);
461 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
463 if (tb
[IPSET_ATTR_PROTO
]) {
464 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
465 with_ports
= ip_set_proto_with_ports(e
.proto
);
468 return -IPSET_ERR_INVALID_PROTO
;
470 return -IPSET_ERR_MISSING_PROTO
;
473 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
476 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
477 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
479 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
480 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
483 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
484 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
485 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
486 ip_set_eexist(ret
, flags
) ? 0 : ret
;
489 port
= ntohs(e
.port
);
490 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
495 port
= ntohs(h
->next
.port
);
496 for (; port
<= port_to
; port
++) {
497 e
.port
= htons(port
);
498 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
500 if (ret
&& !ip_set_eexist(ret
, flags
))
508 static struct ip_set_type hash_ipportnet_type __read_mostly
= {
509 .name
= "hash:ip,port,net",
510 .protocol
= IPSET_PROTOCOL
,
511 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
|
513 .dimension
= IPSET_DIM_THREE
,
514 .family
= NFPROTO_UNSPEC
,
515 .revision_min
= IPSET_TYPE_REV_MIN
,
516 .revision_max
= IPSET_TYPE_REV_MAX
,
517 .create_flags
[IPSET_TYPE_REV_MAX
] = IPSET_CREATE_FLAG_BUCKETSIZE
,
518 .create
= hash_ipportnet_create
,
520 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
521 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
522 [IPSET_ATTR_INITVAL
] = { .type
= NLA_U32
},
523 [IPSET_ATTR_BUCKETSIZE
] = { .type
= NLA_U8
},
524 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
525 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
526 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
529 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
530 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
531 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
532 [IPSET_ATTR_IP2_TO
] = { .type
= NLA_NESTED
},
533 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
534 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
535 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
536 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
537 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
538 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
539 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
540 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
541 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
542 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
543 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
544 .len
= IPSET_MAX_COMMENT_SIZE
},
545 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
546 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
547 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
553 hash_ipportnet_init(void)
555 return ip_set_type_register(&hash_ipportnet_type
);
559 hash_ipportnet_fini(void)
562 ip_set_type_unregister(&hash_ipportnet_type
);
565 module_init(hash_ipportnet_init
);
566 module_exit(hash_ipportnet_fini
);