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 /* 5 skbinfo support added */
29 #define IPSET_TYPE_REV_MAX 6 /* bucketsize, initval support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
33 IP_SET_MODULE_DESC("hash:ip,port,ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,port,ip");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipportip
42 struct hash_ipportip4_elem {
51 hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
52 const struct hash_ipportip4_elem *ip2,
55 return ip1->ip == ip2->ip &&
56 ip1->ip2 == ip2->ip2 &&
57 ip1->port == ip2->port &&
58 ip1->proto == ip2->proto;
62 hash_ipportip4_data_list(struct sk_buff *skb,
63 const struct hash_ipportip4_elem *data)
65 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
66 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
67 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
68 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
77 hash_ipportip4_data_next(struct hash_ipportip4_elem *next,
78 const struct hash_ipportip4_elem *d)
84 /* Common functions */
85 #define MTYPE hash_ipportip4
87 #include "ip_set_hash_gen.h"
90 hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
91 const struct xt_action_param *par,
92 enum ipset_adt adt, struct ip_set_adt_opt *opt)
94 ipset_adtfn adtfn = set->variant->adt[adt];
95 struct hash_ipportip4_elem e = { .ip = 0 };
96 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
98 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
102 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
103 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
104 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
108 hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
109 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
111 struct hash_ipportip4 *h = set->data;
112 ipset_adtfn adtfn = set->variant->adt[adt];
113 struct hash_ipportip4_elem e = { .ip = 0 };
114 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
115 u32 ip, ip_to = 0, p = 0, port, port_to, i = 0;
116 bool with_ports = false;
119 if (tb[IPSET_ATTR_LINENO])
120 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
122 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
123 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
124 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
125 return -IPSET_ERR_PROTOCOL;
127 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
131 ret = ip_set_get_extensions(set, tb, &ext);
135 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP2], &e.ip2);
139 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
141 if (tb[IPSET_ATTR_PROTO]) {
142 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
143 with_ports = ip_set_proto_with_ports(e.proto);
146 return -IPSET_ERR_INVALID_PROTO;
148 return -IPSET_ERR_MISSING_PROTO;
151 if (!(with_ports || e.proto == IPPROTO_ICMP))
154 if (adt == IPSET_TEST ||
155 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
156 tb[IPSET_ATTR_PORT_TO])) {
157 ret = adtfn(set, &e, &ext, &ext, flags);
158 return ip_set_eexist(ret, flags) ? 0 : ret;
161 ip_to = ip = ntohl(e.ip);
162 if (tb[IPSET_ATTR_IP_TO]) {
163 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
168 } else if (tb[IPSET_ATTR_CIDR]) {
169 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
171 if (!cidr || cidr > HOST_MASK)
172 return -IPSET_ERR_INVALID_CIDR;
173 ip_set_mask_from_to(ip, ip_to, cidr);
176 port_to = port = ntohs(e.port);
177 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
178 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
184 ip = ntohl(h->next.ip);
185 for (; ip <= ip_to; ip++) {
186 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
188 for (; p <= port_to; p++, i++) {
191 if (i > IPSET_MAX_RANGE) {
192 hash_ipportip4_data_next(&h->next, &e);
195 ret = adtfn(set, &e, &ext, &ext, flags);
197 if (ret && !ip_set_eexist(ret, flags))
208 struct hash_ipportip6_elem {
209 union nf_inet_addr ip;
210 union nf_inet_addr ip2;
216 /* Common functions */
219 hash_ipportip6_data_equal(const struct hash_ipportip6_elem *ip1,
220 const struct hash_ipportip6_elem *ip2,
223 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
224 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
225 ip1->port == ip2->port &&
226 ip1->proto == ip2->proto;
230 hash_ipportip6_data_list(struct sk_buff *skb,
231 const struct hash_ipportip6_elem *data)
233 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
234 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
235 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
236 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
237 goto nla_put_failure;
245 hash_ipportip6_data_next(struct hash_ipportip6_elem *next,
246 const struct hash_ipportip6_elem *d)
248 next->port = d->port;
254 #define MTYPE hash_ipportip6
255 #define HOST_MASK 128
256 #define IP_SET_EMIT_CREATE
257 #include "ip_set_hash_gen.h"
260 hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
261 const struct xt_action_param *par,
262 enum ipset_adt adt, struct ip_set_adt_opt *opt)
264 ipset_adtfn adtfn = set->variant->adt[adt];
265 struct hash_ipportip6_elem e = { .ip = { .all = { 0 } } };
266 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
268 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
272 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
273 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
274 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
278 hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
279 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
281 const struct hash_ipportip6 *h = set->data;
282 ipset_adtfn adtfn = set->variant->adt[adt];
283 struct hash_ipportip6_elem e = { .ip = { .all = { 0 } } };
284 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
286 bool with_ports = false;
289 if (tb[IPSET_ATTR_LINENO])
290 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
292 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
293 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
294 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
295 return -IPSET_ERR_PROTOCOL;
296 if (unlikely(tb[IPSET_ATTR_IP_TO]))
297 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
298 if (unlikely(tb[IPSET_ATTR_CIDR])) {
299 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
301 if (cidr != HOST_MASK)
302 return -IPSET_ERR_INVALID_CIDR;
305 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
309 ret = ip_set_get_extensions(set, tb, &ext);
313 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
317 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
319 if (tb[IPSET_ATTR_PROTO]) {
320 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
321 with_ports = ip_set_proto_with_ports(e.proto);
324 return -IPSET_ERR_INVALID_PROTO;
326 return -IPSET_ERR_MISSING_PROTO;
329 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
332 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
333 ret = adtfn(set, &e, &ext, &ext, flags);
334 return ip_set_eexist(ret, flags) ? 0 : ret;
337 port = ntohs(e.port);
338 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
343 port = ntohs(h->next.port);
344 for (; port <= port_to; port++) {
345 e.port = htons(port);
346 ret = adtfn(set, &e, &ext, &ext, flags);
348 if (ret && !ip_set_eexist(ret, flags))
356 static struct ip_set_type hash_ipportip_type __read_mostly = {
357 .name = "hash:ip,port,ip",
358 .protocol = IPSET_PROTOCOL,
359 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
360 .dimension = IPSET_DIM_THREE,
361 .family = NFPROTO_UNSPEC,
362 .revision_min = IPSET_TYPE_REV_MIN,
363 .revision_max = IPSET_TYPE_REV_MAX,
364 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
365 .create = hash_ipportip_create,
367 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
368 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
369 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
370 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
371 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
372 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
373 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
376 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
377 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
378 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
379 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
380 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
381 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
382 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
383 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
384 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
385 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
386 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
387 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
388 .len = IPSET_MAX_COMMENT_SIZE },
389 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
390 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
391 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
397 hash_ipportip_init(void)
399 return ip_set_type_register(&hash_ipportip_type);
403 hash_ipportip_fini(void)
406 ip_set_type_unregister(&hash_ipportip_type);
409 module_init(hash_ipportip_init);
410 module_exit(hash_ipportip_fini);