1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * Copyright (C) 2013 Smoothwall Ltd. <vytas.dauksa@smoothwall.net>
6 /* Kernel module implementing an IP set type: the hash:ip,mark type */
8 #include <linux/jhash.h>
9 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
16 #include <net/netlink.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter/ipset/pfxlen.h>
21 #include <linux/netfilter/ipset/ip_set.h>
22 #include <linux/netfilter/ipset/ip_set_hash.h>
24 #define IPSET_TYPE_REV_MIN 0
25 /* 1 Forceadd support */
26 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support */
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
30 IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:ip,mark");
33 /* Type specific function prefix */
34 #define HTYPE hash_ipmark
35 #define IP_SET_HASH_WITH_MARKMASK
40 struct hash_ipmark4_elem {
45 /* Common functions */
48 hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
49 const struct hash_ipmark4_elem *ip2,
52 return ip1->ip == ip2->ip &&
53 ip1->mark == ip2->mark;
57 hash_ipmark4_data_list(struct sk_buff *skb,
58 const struct hash_ipmark4_elem *data)
60 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
61 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
70 hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
71 const struct hash_ipmark4_elem *d)
76 #define MTYPE hash_ipmark4
78 #include "ip_set_hash_gen.h"
81 hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
82 const struct xt_action_param *par,
83 enum ipset_adt adt, struct ip_set_adt_opt *opt)
85 const struct hash_ipmark4 *h = set->data;
86 ipset_adtfn adtfn = set->variant->adt[adt];
87 struct hash_ipmark4_elem e = { };
88 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
91 e.mark &= h->markmask;
93 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
94 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
98 hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
99 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
101 const struct hash_ipmark4 *h = set->data;
102 ipset_adtfn adtfn = set->variant->adt[adt];
103 struct hash_ipmark4_elem e = { };
104 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
108 if (tb[IPSET_ATTR_LINENO])
109 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
111 if (unlikely(!tb[IPSET_ATTR_IP] ||
112 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
113 return -IPSET_ERR_PROTOCOL;
115 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
119 ret = ip_set_get_extensions(set, tb, &ext);
123 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
124 e.mark &= h->markmask;
126 if (adt == IPSET_TEST ||
127 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
128 ret = adtfn(set, &e, &ext, &ext, flags);
129 return ip_set_eexist(ret, flags) ? 0 : ret;
132 ip_to = ip = ntohl(e.ip);
133 if (tb[IPSET_ATTR_IP_TO]) {
134 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
139 } else if (tb[IPSET_ATTR_CIDR]) {
140 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
142 if (!cidr || cidr > HOST_MASK)
143 return -IPSET_ERR_INVALID_CIDR;
144 ip_set_mask_from_to(ip, ip_to, cidr);
148 ip = ntohl(h->next.ip);
149 for (; ip <= ip_to; ip++) {
151 ret = adtfn(set, &e, &ext, &ext, flags);
153 if (ret && !ip_set_eexist(ret, flags))
163 struct hash_ipmark6_elem {
164 union nf_inet_addr ip;
168 /* Common functions */
171 hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
172 const struct hash_ipmark6_elem *ip2,
175 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
176 ip1->mark == ip2->mark;
180 hash_ipmark6_data_list(struct sk_buff *skb,
181 const struct hash_ipmark6_elem *data)
183 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
184 nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
185 goto nla_put_failure;
193 hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
194 const struct hash_ipmark6_elem *d)
201 #define MTYPE hash_ipmark6
202 #define HOST_MASK 128
203 #define IP_SET_EMIT_CREATE
204 #include "ip_set_hash_gen.h"
207 hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
208 const struct xt_action_param *par,
209 enum ipset_adt adt, struct ip_set_adt_opt *opt)
211 const struct hash_ipmark6 *h = set->data;
212 ipset_adtfn adtfn = set->variant->adt[adt];
213 struct hash_ipmark6_elem e = { };
214 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
217 e.mark &= h->markmask;
219 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
220 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
224 hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
225 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
227 const struct hash_ipmark6 *h = set->data;
228 ipset_adtfn adtfn = set->variant->adt[adt];
229 struct hash_ipmark6_elem e = { };
230 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
233 if (tb[IPSET_ATTR_LINENO])
234 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
236 if (unlikely(!tb[IPSET_ATTR_IP] ||
237 !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
238 return -IPSET_ERR_PROTOCOL;
239 if (unlikely(tb[IPSET_ATTR_IP_TO]))
240 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
241 if (unlikely(tb[IPSET_ATTR_CIDR])) {
242 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
244 if (cidr != HOST_MASK)
245 return -IPSET_ERR_INVALID_CIDR;
248 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
252 ret = ip_set_get_extensions(set, tb, &ext);
256 e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
257 e.mark &= h->markmask;
259 if (adt == IPSET_TEST) {
260 ret = adtfn(set, &e, &ext, &ext, flags);
261 return ip_set_eexist(ret, flags) ? 0 : ret;
264 ret = adtfn(set, &e, &ext, &ext, flags);
265 if (ret && !ip_set_eexist(ret, flags))
271 static struct ip_set_type hash_ipmark_type __read_mostly = {
272 .name = "hash:ip,mark",
273 .protocol = IPSET_PROTOCOL,
274 .features = IPSET_TYPE_IP | IPSET_TYPE_MARK,
275 .dimension = IPSET_DIM_TWO,
276 .family = NFPROTO_UNSPEC,
277 .revision_min = IPSET_TYPE_REV_MIN,
278 .revision_max = IPSET_TYPE_REV_MAX,
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_PROBES] = { .type = NLA_U8 },
285 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
286 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
287 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
290 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
291 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
292 [IPSET_ATTR_MARK] = { .type = NLA_U32 },
293 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
294 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
295 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
296 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
297 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
298 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
299 .len = IPSET_MAX_COMMENT_SIZE },
300 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
301 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
302 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
308 hash_ipmark_init(void)
310 return ip_set_type_register(&hash_ipmark_type);
314 hash_ipmark_fini(void)
317 ip_set_type_unregister(&hash_ipmark_type);
320 module_init(hash_ipmark_init);
321 module_exit(hash_ipmark_fini);