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:net,port 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>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/ipset/pfxlen.h>
18 #include <linux/netfilter/ipset/ip_set.h>
19 #include <linux/netfilter/ipset/ip_set_getport.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 SCTP and UDPLITE support added */
24 /* 2 Range as input support for IPv4 added */
25 /* 3 nomatch flag support added */
26 /* 4 Counters support added */
27 /* 5 Comments support added */
28 /* 6 Forceadd support added */
29 /* 7 skbinfo support added */
30 #define IPSET_TYPE_REV_MAX 8 /* bucketsize, initval support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
35 MODULE_ALIAS("ip_set_hash:net,port");
37 /* Type specific function prefix */
38 #define HTYPE hash_netport
39 #define IP_SET_HASH_WITH_PROTO
40 #define IP_SET_HASH_WITH_NETS
42 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
43 * However this way we have to store internally cidr - 1,
44 * dancing back and forth.
46 #define IP_SET_HASH_WITH_NETS_PACKED
51 struct hash_netport4_elem
{
59 /* Common functions */
62 hash_netport4_data_equal(const struct hash_netport4_elem
*ip1
,
63 const struct hash_netport4_elem
*ip2
,
66 return ip1
->ip
== ip2
->ip
&&
67 ip1
->port
== ip2
->port
&&
68 ip1
->proto
== ip2
->proto
&&
69 ip1
->cidr
== ip2
->cidr
;
73 hash_netport4_do_data_match(const struct hash_netport4_elem
*elem
)
75 return elem
->nomatch
? -ENOTEMPTY
: 1;
79 hash_netport4_data_set_flags(struct hash_netport4_elem
*elem
, u32 flags
)
81 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
85 hash_netport4_data_reset_flags(struct hash_netport4_elem
*elem
, u8
*flags
)
87 swap(*flags
, elem
->nomatch
);
91 hash_netport4_data_netmask(struct hash_netport4_elem
*elem
, u8 cidr
)
93 elem
->ip
&= ip_set_netmask(cidr
);
94 elem
->cidr
= cidr
- 1;
98 hash_netport4_data_list(struct sk_buff
*skb
,
99 const struct hash_netport4_elem
*data
)
101 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
103 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
104 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
105 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
+ 1) ||
106 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
108 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
109 goto nla_put_failure
;
117 hash_netport4_data_next(struct hash_netport4_elem
*next
,
118 const struct hash_netport4_elem
*d
)
121 next
->port
= d
->port
;
124 #define MTYPE hash_netport4
126 #include "ip_set_hash_gen.h"
129 hash_netport4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
130 const struct xt_action_param
*par
,
131 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
133 const struct hash_netport4
*h
= set
->data
;
134 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
135 struct hash_netport4_elem e
= {
136 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
138 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
140 if (adt
== IPSET_TEST
)
141 e
.cidr
= HOST_MASK
- 1;
143 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
147 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
148 e
.ip
&= ip_set_netmask(e
.cidr
+ 1);
150 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
154 hash_netport4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
155 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
157 const struct hash_netport4
*h
= set
->data
;
158 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
159 struct hash_netport4_elem e
= { .cidr
= HOST_MASK
- 1 };
160 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
161 u32 port
, port_to
, p
= 0, ip
= 0, ip_to
= 0, ipn
;
163 bool with_ports
= false;
167 if (tb
[IPSET_ATTR_LINENO
])
168 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
170 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
171 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
172 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
173 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
174 return -IPSET_ERR_PROTOCOL
;
176 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
180 ret
= ip_set_get_extensions(set
, tb
, &ext
);
184 if (tb
[IPSET_ATTR_CIDR
]) {
185 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
186 if (!cidr
|| cidr
> HOST_MASK
)
187 return -IPSET_ERR_INVALID_CIDR
;
191 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
193 if (tb
[IPSET_ATTR_PROTO
]) {
194 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
195 with_ports
= ip_set_proto_with_ports(e
.proto
);
198 return -IPSET_ERR_INVALID_PROTO
;
200 return -IPSET_ERR_MISSING_PROTO
;
203 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
206 with_ports
= with_ports
&& tb
[IPSET_ATTR_PORT_TO
];
208 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
209 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
211 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
212 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
215 if (adt
== IPSET_TEST
|| !(with_ports
|| tb
[IPSET_ATTR_IP_TO
])) {
216 e
.ip
= htonl(ip
& ip_set_hostmask(e
.cidr
+ 1));
217 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
218 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
219 ip_set_eexist(ret
, flags
) ? 0 : ret
;
222 port
= port_to
= ntohs(e
.port
);
223 if (tb
[IPSET_ATTR_PORT_TO
]) {
224 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
228 if (tb
[IPSET_ATTR_IP_TO
]) {
229 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
234 if (ip
+ UINT_MAX
== ip_to
)
235 return -IPSET_ERR_HASH_RANGE
;
237 ip_set_mask_from_to(ip
, ip_to
, e
.cidr
+ 1);
241 ipn
= ip_set_range_to_cidr(ipn
, ip_to
, &cidr
);
243 } while (ipn
++ < ip_to
);
245 if (n
*(port_to
- port
+ 1) > IPSET_MAX_RANGE
)
249 ip
= ntohl(h
->next
.ip
);
250 p
= ntohs(h
->next
.port
);
256 ip
= ip_set_range_to_cidr(ip
, ip_to
, &cidr
);
258 for (; p
<= port_to
; p
++) {
260 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
261 if (ret
&& !ip_set_eexist(ret
, flags
))
267 } while (ip
++ < ip_to
);
273 struct hash_netport6_elem
{
274 union nf_inet_addr ip
;
281 /* Common functions */
284 hash_netport6_data_equal(const struct hash_netport6_elem
*ip1
,
285 const struct hash_netport6_elem
*ip2
,
288 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
289 ip1
->port
== ip2
->port
&&
290 ip1
->proto
== ip2
->proto
&&
291 ip1
->cidr
== ip2
->cidr
;
295 hash_netport6_do_data_match(const struct hash_netport6_elem
*elem
)
297 return elem
->nomatch
? -ENOTEMPTY
: 1;
301 hash_netport6_data_set_flags(struct hash_netport6_elem
*elem
, u32 flags
)
303 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
307 hash_netport6_data_reset_flags(struct hash_netport6_elem
*elem
, u8
*flags
)
309 swap(*flags
, elem
->nomatch
);
313 hash_netport6_data_netmask(struct hash_netport6_elem
*elem
, u8 cidr
)
315 ip6_netmask(&elem
->ip
, cidr
);
316 elem
->cidr
= cidr
- 1;
320 hash_netport6_data_list(struct sk_buff
*skb
,
321 const struct hash_netport6_elem
*data
)
323 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
325 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
326 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
327 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
+ 1) ||
328 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
330 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
331 goto nla_put_failure
;
339 hash_netport6_data_next(struct hash_netport6_elem
*next
,
340 const struct hash_netport6_elem
*d
)
342 next
->port
= d
->port
;
348 #define MTYPE hash_netport6
349 #define HOST_MASK 128
350 #define IP_SET_EMIT_CREATE
351 #include "ip_set_hash_gen.h"
354 hash_netport6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
355 const struct xt_action_param
*par
,
356 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
358 const struct hash_netport6
*h
= set
->data
;
359 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
360 struct hash_netport6_elem e
= {
361 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
363 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
365 if (adt
== IPSET_TEST
)
366 e
.cidr
= HOST_MASK
- 1;
368 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
372 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
373 ip6_netmask(&e
.ip
, e
.cidr
+ 1);
375 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
379 hash_netport6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
380 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
382 const struct hash_netport6
*h
= set
->data
;
383 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
384 struct hash_netport6_elem e
= { .cidr
= HOST_MASK
- 1 };
385 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
387 bool with_ports
= false;
391 if (tb
[IPSET_ATTR_LINENO
])
392 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
394 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
395 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
396 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
397 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
398 return -IPSET_ERR_PROTOCOL
;
399 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
400 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
402 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
406 ret
= ip_set_get_extensions(set
, tb
, &ext
);
410 if (tb
[IPSET_ATTR_CIDR
]) {
411 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
412 if (!cidr
|| cidr
> HOST_MASK
)
413 return -IPSET_ERR_INVALID_CIDR
;
416 ip6_netmask(&e
.ip
, e
.cidr
+ 1);
418 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
420 if (tb
[IPSET_ATTR_PROTO
]) {
421 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
422 with_ports
= ip_set_proto_with_ports(e
.proto
);
425 return -IPSET_ERR_INVALID_PROTO
;
427 return -IPSET_ERR_MISSING_PROTO
;
430 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
433 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
434 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
436 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
437 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
440 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
441 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
442 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
443 ip_set_eexist(ret
, flags
) ? 0 : ret
;
446 port
= ntohs(e
.port
);
447 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
452 port
= ntohs(h
->next
.port
);
453 for (; port
<= port_to
; port
++) {
454 e
.port
= htons(port
);
455 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
457 if (ret
&& !ip_set_eexist(ret
, flags
))
465 static struct ip_set_type hash_netport_type __read_mostly
= {
466 .name
= "hash:net,port",
467 .protocol
= IPSET_PROTOCOL
,
468 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_NOMATCH
,
469 .dimension
= IPSET_DIM_TWO
,
470 .family
= NFPROTO_UNSPEC
,
471 .revision_min
= IPSET_TYPE_REV_MIN
,
472 .revision_max
= IPSET_TYPE_REV_MAX
,
473 .create_flags
[IPSET_TYPE_REV_MAX
] = IPSET_CREATE_FLAG_BUCKETSIZE
,
474 .create
= hash_netport_create
,
476 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
477 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
478 [IPSET_ATTR_INITVAL
] = { .type
= NLA_U32
},
479 [IPSET_ATTR_BUCKETSIZE
] = { .type
= NLA_U8
},
480 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
481 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
482 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
483 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
486 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
487 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
488 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
489 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
490 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
491 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
492 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
493 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
494 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
495 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
496 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
497 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
498 .len
= IPSET_MAX_COMMENT_SIZE
},
499 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
500 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
501 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
507 hash_netport_init(void)
509 return ip_set_type_register(&hash_netport_type
);
513 hash_netport_fini(void)
516 ip_set_type_unregister(&hash_netport_type
);
519 module_init(hash_netport_init
);
520 module_exit(hash_netport_fini
);