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,mark 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_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 Forceadd support */
24 /* 2 skbinfo support */
25 #define IPSET_TYPE_REV_MAX 3 /* bucketsize, initval support */
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
29 IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
30 MODULE_ALIAS("ip_set_hash:ip,mark");
32 /* Type specific function prefix */
33 #define HTYPE hash_ipmark
34 #define IP_SET_HASH_WITH_MARKMASK
39 struct hash_ipmark4_elem {
44 /* Common functions */
47 hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
48 const struct hash_ipmark4_elem *ip2,
51 return ip1->ip == ip2->ip &&
52 ip1->mark == ip2->mark;
56 hash_ipmark4_data_list(struct sk_buff *skb,
57 const struct hash_ipmark4_elem *data)
59 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
60 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
69 hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
70 const struct hash_ipmark4_elem *d)
75 #define MTYPE hash_ipmark4
77 #include "ip_set_hash_gen.h"
80 hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
81 const struct xt_action_param *par,
82 enum ipset_adt adt, struct ip_set_adt_opt *opt)
84 const struct hash_ipmark4 *h = set->data;
85 ipset_adtfn adtfn = set->variant->adt[adt];
86 struct hash_ipmark4_elem e = { };
87 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
90 e.mark &= h->markmask;
92 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
93 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
97 hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
98 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
100 const struct hash_ipmark4 *h = set->data;
101 ipset_adtfn adtfn = set->variant->adt[adt];
102 struct hash_ipmark4_elem e = { };
103 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
107 if (tb[IPSET_ATTR_LINENO])
108 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
110 if (unlikely(!tb[IPSET_ATTR_IP] ||
111 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
112 return -IPSET_ERR_PROTOCOL;
114 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
118 ret = ip_set_get_extensions(set, tb, &ext);
122 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
123 e.mark &= h->markmask;
125 if (adt == IPSET_TEST ||
126 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
127 ret = adtfn(set, &e, &ext, &ext, flags);
128 return ip_set_eexist(ret, flags) ? 0 : ret;
131 ip_to = ip = ntohl(e.ip);
132 if (tb[IPSET_ATTR_IP_TO]) {
133 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
138 } else if (tb[IPSET_ATTR_CIDR]) {
139 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
141 if (!cidr || cidr > HOST_MASK)
142 return -IPSET_ERR_INVALID_CIDR;
143 ip_set_mask_from_to(ip, ip_to, cidr);
147 ip = ntohl(h->next.ip);
148 for (; ip <= ip_to; ip++) {
150 ret = adtfn(set, &e, &ext, &ext, flags);
152 if (ret && !ip_set_eexist(ret, flags))
162 struct hash_ipmark6_elem {
163 union nf_inet_addr ip;
167 /* Common functions */
170 hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
171 const struct hash_ipmark6_elem *ip2,
174 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
175 ip1->mark == ip2->mark;
179 hash_ipmark6_data_list(struct sk_buff *skb,
180 const struct hash_ipmark6_elem *data)
182 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
183 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
184 goto nla_put_failure;
192 hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
193 const struct hash_ipmark6_elem *d)
200 #define MTYPE hash_ipmark6
201 #define HOST_MASK 128
202 #define IP_SET_EMIT_CREATE
203 #include "ip_set_hash_gen.h"
206 hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
207 const struct xt_action_param *par,
208 enum ipset_adt adt, struct ip_set_adt_opt *opt)
210 const struct hash_ipmark6 *h = set->data;
211 ipset_adtfn adtfn = set->variant->adt[adt];
212 struct hash_ipmark6_elem e = { };
213 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
216 e.mark &= h->markmask;
218 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
219 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
223 hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
224 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
226 const struct hash_ipmark6 *h = set->data;
227 ipset_adtfn adtfn = set->variant->adt[adt];
228 struct hash_ipmark6_elem e = { };
229 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
232 if (tb[IPSET_ATTR_LINENO])
233 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
235 if (unlikely(!tb[IPSET_ATTR_IP] ||
236 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
237 return -IPSET_ERR_PROTOCOL;
238 if (unlikely(tb[IPSET_ATTR_IP_TO]))
239 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
240 if (unlikely(tb[IPSET_ATTR_CIDR])) {
241 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
243 if (cidr != HOST_MASK)
244 return -IPSET_ERR_INVALID_CIDR;
247 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
251 ret = ip_set_get_extensions(set, tb, &ext);
255 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
256 e.mark &= h->markmask;
258 if (adt == IPSET_TEST) {
259 ret = adtfn(set, &e, &ext, &ext, flags);
260 return ip_set_eexist(ret, flags) ? 0 : ret;
263 ret = adtfn(set, &e, &ext, &ext, flags);
264 if (ret && !ip_set_eexist(ret, flags))
270 static struct ip_set_type hash_ipmark_type __read_mostly = {
271 .name = "hash:ip,mark",
272 .protocol = IPSET_PROTOCOL,
273 .features = IPSET_TYPE_IP | IPSET_TYPE_MARK,
274 .dimension = IPSET_DIM_TWO,
275 .family = NFPROTO_UNSPEC,
276 .revision_min = IPSET_TYPE_REV_MIN,
277 .revision_max = IPSET_TYPE_REV_MAX,
278 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
279 .create = hash_ipmark_create,
281 [IPSET_ATTR_MARKMASK] = { .type = NLA_U32 },
282 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
283 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
284 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
285 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
286 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
287 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
288 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
291 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
292 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
293 [IPSET_ATTR_MARK] = { .type = NLA_U32 },
294 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
295 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
296 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
297 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
298 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
299 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
300 .len = IPSET_MAX_COMMENT_SIZE },
301 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
302 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
303 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
309 hash_ipmark_init(void)
311 return ip_set_type_register(&hash_ipmark_type);
315 hash_ipmark_fini(void)
318 ip_set_type_unregister(&hash_ipmark_type);
321 module_init(hash_ipmark_init);
322 module_exit(hash_ipmark_fini);