1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 /* Kernel module implementing an IP set type: the hash:ip,port type */
7 #include <linux/jhash.h>
8 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/errno.h>
12 #include <linux/random.h>
15 #include <net/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/ipset/pfxlen.h>
20 #include <linux/netfilter/ipset/ip_set.h>
21 #include <linux/netfilter/ipset/ip_set_getport.h>
22 #include <linux/netfilter/ipset/ip_set_hash.h>
24 #define IPSET_TYPE_REV_MIN 0
25 /* 1 SCTP and UDPLITE support added */
26 /* 2 Counters support added */
27 /* 3 Comments support added */
28 /* 4 Forceadd support added */
29 #define IPSET_TYPE_REV_MAX 5 /* skbinfo support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,port");
36 /* Type specific function prefix */
37 #define HTYPE hash_ipport
42 struct hash_ipport4_elem {
49 /* Common functions */
52 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
53 const struct hash_ipport4_elem *ip2,
56 return ip1->ip == ip2->ip &&
57 ip1->port == ip2->port &&
58 ip1->proto == ip2->proto;
62 hash_ipport4_data_list(struct sk_buff *skb,
63 const struct hash_ipport4_elem *data)
65 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
66 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
67 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
76 hash_ipport4_data_next(struct hash_ipport4_elem *next,
77 const struct hash_ipport4_elem *d)
83 #define MTYPE hash_ipport4
85 #include "ip_set_hash_gen.h"
88 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
89 const struct xt_action_param *par,
90 enum ipset_adt adt, struct ip_set_adt_opt *opt)
92 ipset_adtfn adtfn = set->variant->adt[adt];
93 struct hash_ipport4_elem e = { .ip = 0 };
94 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
96 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
100 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
101 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
105 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
106 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
108 const struct hash_ipport4 *h = set->data;
109 ipset_adtfn adtfn = set->variant->adt[adt];
110 struct hash_ipport4_elem e = { .ip = 0 };
111 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
112 u32 ip, ip_to = 0, p = 0, port, port_to;
113 bool with_ports = false;
116 if (tb[IPSET_ATTR_LINENO])
117 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
119 if (unlikely(!tb[IPSET_ATTR_IP] ||
120 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
121 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
122 return -IPSET_ERR_PROTOCOL;
124 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
128 ret = ip_set_get_extensions(set, tb, &ext);
132 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
134 if (tb[IPSET_ATTR_PROTO]) {
135 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
136 with_ports = ip_set_proto_with_ports(e.proto);
139 return -IPSET_ERR_INVALID_PROTO;
141 return -IPSET_ERR_MISSING_PROTO;
144 if (!(with_ports || e.proto == IPPROTO_ICMP))
147 if (adt == IPSET_TEST ||
148 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
149 tb[IPSET_ATTR_PORT_TO])) {
150 ret = adtfn(set, &e, &ext, &ext, flags);
151 return ip_set_eexist(ret, flags) ? 0 : ret;
154 ip_to = ip = ntohl(e.ip);
155 if (tb[IPSET_ATTR_IP_TO]) {
156 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
161 } else if (tb[IPSET_ATTR_CIDR]) {
162 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
164 if (!cidr || cidr > HOST_MASK)
165 return -IPSET_ERR_INVALID_CIDR;
166 ip_set_mask_from_to(ip, ip_to, cidr);
169 port_to = port = ntohs(e.port);
170 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
171 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
177 ip = ntohl(h->next.ip);
178 for (; ip <= ip_to; ip++) {
179 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
181 for (; p <= port_to; p++) {
184 ret = adtfn(set, &e, &ext, &ext, flags);
186 if (ret && !ip_set_eexist(ret, flags))
197 struct hash_ipport6_elem {
198 union nf_inet_addr ip;
204 /* Common functions */
207 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
208 const struct hash_ipport6_elem *ip2,
211 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
212 ip1->port == ip2->port &&
213 ip1->proto == ip2->proto;
217 hash_ipport6_data_list(struct sk_buff *skb,
218 const struct hash_ipport6_elem *data)
220 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
221 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
222 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
223 goto nla_put_failure;
231 hash_ipport6_data_next(struct hash_ipport6_elem *next,
232 const struct hash_ipport6_elem *d)
234 next->port = d->port;
240 #define MTYPE hash_ipport6
241 #define HOST_MASK 128
242 #define IP_SET_EMIT_CREATE
243 #include "ip_set_hash_gen.h"
246 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
247 const struct xt_action_param *par,
248 enum ipset_adt adt, struct ip_set_adt_opt *opt)
250 ipset_adtfn adtfn = set->variant->adt[adt];
251 struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
252 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
254 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
258 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
259 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
263 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
264 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
266 const struct hash_ipport6 *h = set->data;
267 ipset_adtfn adtfn = set->variant->adt[adt];
268 struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
269 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
271 bool with_ports = false;
274 if (tb[IPSET_ATTR_LINENO])
275 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
277 if (unlikely(!tb[IPSET_ATTR_IP] ||
278 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
279 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
280 return -IPSET_ERR_PROTOCOL;
281 if (unlikely(tb[IPSET_ATTR_IP_TO]))
282 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
283 if (unlikely(tb[IPSET_ATTR_CIDR])) {
284 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
286 if (cidr != HOST_MASK)
287 return -IPSET_ERR_INVALID_CIDR;
290 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
294 ret = ip_set_get_extensions(set, tb, &ext);
298 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
300 if (tb[IPSET_ATTR_PROTO]) {
301 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
302 with_ports = ip_set_proto_with_ports(e.proto);
305 return -IPSET_ERR_INVALID_PROTO;
307 return -IPSET_ERR_MISSING_PROTO;
310 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
313 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
314 ret = adtfn(set, &e, &ext, &ext, flags);
315 return ip_set_eexist(ret, flags) ? 0 : ret;
318 port = ntohs(e.port);
319 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
324 port = ntohs(h->next.port);
325 for (; port <= port_to; port++) {
326 e.port = htons(port);
327 ret = adtfn(set, &e, &ext, &ext, flags);
329 if (ret && !ip_set_eexist(ret, flags))
337 static struct ip_set_type hash_ipport_type __read_mostly = {
338 .name = "hash:ip,port",
339 .protocol = IPSET_PROTOCOL,
340 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
341 .dimension = IPSET_DIM_TWO,
342 .family = NFPROTO_UNSPEC,
343 .revision_min = IPSET_TYPE_REV_MIN,
344 .revision_max = IPSET_TYPE_REV_MAX,
345 .create = hash_ipport_create,
347 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
348 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
349 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
350 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
351 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
352 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
353 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
356 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
357 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
358 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
359 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
360 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
361 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
362 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
363 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
364 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
365 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
366 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
367 .len = IPSET_MAX_COMMENT_SIZE },
368 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
369 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
370 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
376 hash_ipport_init(void)
378 return ip_set_type_register(&hash_ipport_type);
382 hash_ipport_fini(void)
385 ip_set_type_unregister(&hash_ipport_type);
388 module_init(hash_ipport_init);
389 module_exit(hash_ipport_fini);