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,ip 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 Counters support added */
26 /* 3 Comments support added */
27 /* 4 Forceadd support added */
28 #define IPSET_TYPE_REV_MAX 5 /* skbinfo support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32 IP_SET_MODULE_DESC("hash:ip,port,ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_hash:ip,port,ip");
35 /* Type specific function prefix */
36 #define HTYPE hash_ipportip
41 struct hash_ipportip4_elem {
50 hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
51 const struct hash_ipportip4_elem *ip2,
54 return ip1->ip == ip2->ip &&
55 ip1->ip2 == ip2->ip2 &&
56 ip1->port == ip2->port &&
57 ip1->proto == ip2->proto;
61 hash_ipportip4_data_list(struct sk_buff *skb,
62 const struct hash_ipportip4_elem *data)
64 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
65 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
66 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
67 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
76 hash_ipportip4_data_next(struct hash_ipportip4_elem *next,
77 const struct hash_ipportip4_elem *d)
83 /* Common functions */
84 #define MTYPE hash_ipportip4
86 #include "ip_set_hash_gen.h"
89 hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
90 const struct xt_action_param *par,
91 enum ipset_adt adt, struct ip_set_adt_opt *opt)
93 ipset_adtfn adtfn = set->variant->adt[adt];
94 struct hash_ipportip4_elem e = { .ip = 0 };
95 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
97 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
101 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
102 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
103 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
107 hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
108 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
110 struct hash_ipportip4 *h = set->data;
111 ipset_adtfn adtfn = set->variant->adt[adt];
112 struct hash_ipportip4_elem e = { .ip = 0 };
113 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
114 u32 ip, ip_to = 0, p = 0, port, port_to, i = 0;
115 bool with_ports = false;
118 if (tb[IPSET_ATTR_LINENO])
119 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
121 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
122 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
123 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
124 return -IPSET_ERR_PROTOCOL;
126 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
130 ret = ip_set_get_extensions(set, tb, &ext);
134 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP2], &e.ip2);
138 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
140 if (tb[IPSET_ATTR_PROTO]) {
141 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
142 with_ports = ip_set_proto_with_ports(e.proto);
145 return -IPSET_ERR_INVALID_PROTO;
147 return -IPSET_ERR_MISSING_PROTO;
150 if (!(with_ports || e.proto == IPPROTO_ICMP))
153 if (adt == IPSET_TEST ||
154 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
155 tb[IPSET_ATTR_PORT_TO])) {
156 ret = adtfn(set, &e, &ext, &ext, flags);
157 return ip_set_eexist(ret, flags) ? 0 : ret;
160 ip_to = ip = ntohl(e.ip);
161 if (tb[IPSET_ATTR_IP_TO]) {
162 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
167 } else if (tb[IPSET_ATTR_CIDR]) {
168 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
170 if (!cidr || cidr > HOST_MASK)
171 return -IPSET_ERR_INVALID_CIDR;
172 ip_set_mask_from_to(ip, ip_to, cidr);
175 port_to = port = ntohs(e.port);
176 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
177 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
183 ip = ntohl(h->next.ip);
184 for (; ip <= ip_to; ip++) {
185 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
187 for (; p <= port_to; p++, i++) {
190 if (i > IPSET_MAX_RANGE) {
191 hash_ipportip4_data_next(&h->next, &e);
194 ret = adtfn(set, &e, &ext, &ext, flags);
196 if (ret && !ip_set_eexist(ret, flags))
207 struct hash_ipportip6_elem {
208 union nf_inet_addr ip;
209 union nf_inet_addr ip2;
215 /* Common functions */
218 hash_ipportip6_data_equal(const struct hash_ipportip6_elem *ip1,
219 const struct hash_ipportip6_elem *ip2,
222 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
223 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
224 ip1->port == ip2->port &&
225 ip1->proto == ip2->proto;
229 hash_ipportip6_data_list(struct sk_buff *skb,
230 const struct hash_ipportip6_elem *data)
232 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
233 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
234 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
235 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
236 goto nla_put_failure;
244 hash_ipportip6_data_next(struct hash_ipportip6_elem *next,
245 const struct hash_ipportip6_elem *d)
247 next->port = d->port;
253 #define MTYPE hash_ipportip6
254 #define HOST_MASK 128
255 #define IP_SET_EMIT_CREATE
256 #include "ip_set_hash_gen.h"
259 hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
260 const struct xt_action_param *par,
261 enum ipset_adt adt, struct ip_set_adt_opt *opt)
263 ipset_adtfn adtfn = set->variant->adt[adt];
264 struct hash_ipportip6_elem e = { .ip = { .all = { 0 } } };
265 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
267 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
271 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
272 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
273 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
277 hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
278 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
280 const struct hash_ipportip6 *h = set->data;
281 ipset_adtfn adtfn = set->variant->adt[adt];
282 struct hash_ipportip6_elem e = { .ip = { .all = { 0 } } };
283 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
285 bool with_ports = false;
288 if (tb[IPSET_ATTR_LINENO])
289 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
291 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
292 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
293 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
294 return -IPSET_ERR_PROTOCOL;
295 if (unlikely(tb[IPSET_ATTR_IP_TO]))
296 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
297 if (unlikely(tb[IPSET_ATTR_CIDR])) {
298 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
300 if (cidr != HOST_MASK)
301 return -IPSET_ERR_INVALID_CIDR;
304 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
308 ret = ip_set_get_extensions(set, tb, &ext);
312 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
316 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
318 if (tb[IPSET_ATTR_PROTO]) {
319 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
320 with_ports = ip_set_proto_with_ports(e.proto);
323 return -IPSET_ERR_INVALID_PROTO;
325 return -IPSET_ERR_MISSING_PROTO;
328 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
331 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
332 ret = adtfn(set, &e, &ext, &ext, flags);
333 return ip_set_eexist(ret, flags) ? 0 : ret;
336 port = ntohs(e.port);
337 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
342 port = ntohs(h->next.port);
343 for (; port <= port_to; port++) {
344 e.port = htons(port);
345 ret = adtfn(set, &e, &ext, &ext, flags);
347 if (ret && !ip_set_eexist(ret, flags))
355 static struct ip_set_type hash_ipportip_type __read_mostly = {
356 .name = "hash:ip,port,ip",
357 .protocol = IPSET_PROTOCOL,
358 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
359 .dimension = IPSET_DIM_THREE,
360 .family = NFPROTO_UNSPEC,
361 .revision_min = IPSET_TYPE_REV_MIN,
362 .revision_max = IPSET_TYPE_REV_MAX,
363 .create = hash_ipportip_create,
365 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
366 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
367 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
368 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
369 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
370 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
373 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
374 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
375 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
376 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
377 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
378 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
379 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
380 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
381 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
382 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
383 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
384 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
385 .len = IPSET_MAX_COMMENT_SIZE },
386 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
387 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
388 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
394 hash_ipportip_init(void)
396 return ip_set_type_register(&hash_ipportip_type);
400 hash_ipportip_fini(void)
403 ip_set_type_unregister(&hash_ipportip_type);
406 module_init(hash_ipportip_init);
407 module_exit(hash_ipportip_fini);