1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
3
4/* Kernel module implementing an IP set type: the hash:ip type */
5
6#include <linux/jhash.h>
7#include <linux/module.h>
8#include <linux/ip.h>
9#include <linux/skbuff.h>
10#include <linux/errno.h>
11#include <linux/random.h>
12#include <net/ip.h>
13#include <net/ipv6.h>
14#include <net/netlink.h>
15#include <net/tcp.h>
16
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>
21
22#define IPSET_TYPE_REV_MIN	0
23/*				1	   Counters support */
24/*				2	   Comments support */
25/*				3	   Forceadd support */
26/*				4	   skbinfo support */
27/*				5	   bucketsize, initval support  */
28#define IPSET_TYPE_REV_MAX	6	/* bitmask support  */
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33MODULE_ALIAS("ip_set_hash:ip");
34
35/* Type specific function prefix */
36#define HTYPE		hash_ip
37#define IP_SET_HASH_WITH_NETMASK
38#define IP_SET_HASH_WITH_BITMASK
39
40/* IPv4 variant */
41
42/* Member elements */
43struct hash_ip4_elem {
44	/* Zero valued IP addresses cannot be stored */
45	__be32 ip;
46};
47
48/* Common functions */
49
50static bool
51hash_ip4_data_equal(const struct hash_ip4_elem *e1,
52		    const struct hash_ip4_elem *e2,
53		    u32 *multi)
54{
55	return e1->ip == e2->ip;
56}
57
58static bool
59hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
60{
61	if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
62		goto nla_put_failure;
63	return false;
64
65nla_put_failure:
66	return true;
67}
68
69static void
70hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
71{
72	next->ip = e->ip;
73}
74
75#define MTYPE		hash_ip4
76#define HOST_MASK	32
77#include "ip_set_hash_gen.h"
78
79static int
80hash_ip4_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)
83{
84	const struct hash_ip4 *h = set->data;
85	ipset_adtfn adtfn = set->variant->adt[adt];
86	struct hash_ip4_elem e = { 0 };
87	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
88	__be32 ip;
89
90	ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
91	ip &= h->bitmask.ip;
92	if (ip == 0)
93		return -EINVAL;
94
95	e.ip = ip;
96	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
97}
98
99static int
100hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
101	      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
102{
103	struct hash_ip4 *h = set->data;
104	ipset_adtfn adtfn = set->variant->adt[adt];
105	struct hash_ip4_elem e = { 0 };
106	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
107	u32 ip = 0, ip_to = 0, hosts, i = 0;
108	int ret = 0;
109
110	if (tb[IPSET_ATTR_LINENO])
111		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
112
113	if (unlikely(!tb[IPSET_ATTR_IP]))
114		return -IPSET_ERR_PROTOCOL;
115
116	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
117	if (ret)
118		return ret;
119
120	ret = ip_set_get_extensions(set, tb, &ext);
121	if (ret)
122		return ret;
123
124	ip &= ntohl(h->bitmask.ip);
125	e.ip = htonl(ip);
126	if (e.ip == 0)
127		return -IPSET_ERR_HASH_ELEM;
128
129	if (adt == IPSET_TEST)
130		return adtfn(set, &e, &ext, &ext, flags);
131
132	ip_to = ip;
133	if (tb[IPSET_ATTR_IP_TO]) {
134		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
135		if (ret)
136			return ret;
137		if (ip > ip_to) {
138			if (ip_to == 0)
139				return -IPSET_ERR_HASH_ELEM;
140			swap(ip, ip_to);
141		}
142	} else if (tb[IPSET_ATTR_CIDR]) {
143		u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
144
145		if (!cidr || cidr > HOST_MASK)
146			return -IPSET_ERR_INVALID_CIDR;
147		ip_set_mask_from_to(ip, ip_to, cidr);
148	}
149
150	hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
151
152	if (retried)
153		ip = ntohl(h->next.ip);
154	for (; ip <= ip_to; i++) {
155		e.ip = htonl(ip);
156		if (i > IPSET_MAX_RANGE) {
157			hash_ip4_data_next(&h->next, &e);
158			return -ERANGE;
159		}
160		ret = adtfn(set, &e, &ext, &ext, flags);
161		if (ret && !ip_set_eexist(ret, flags))
162			return ret;
163
164		ip += hosts;
165		if (ip == 0)
166			return 0;
167
168		ret = 0;
169	}
170	return ret;
171}
172
173/* IPv6 variant */
174
175/* Member elements */
176struct hash_ip6_elem {
177	union nf_inet_addr ip;
178};
179
180/* Common functions */
181
182static bool
183hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
184		    const struct hash_ip6_elem *ip2,
185		    u32 *multi)
186{
187	return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
188}
189
190static bool
191hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
192{
193	if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
194		goto nla_put_failure;
195	return false;
196
197nla_put_failure:
198	return true;
199}
200
201static void
202hash_ip6_data_next(struct hash_ip6_elem *next, const struct hash_ip6_elem *e)
203{
204}
205
206#undef MTYPE
207#undef HOST_MASK
208
209#define MTYPE		hash_ip6
210#define HOST_MASK	128
211
212#define IP_SET_EMIT_CREATE
213#include "ip_set_hash_gen.h"
214
215static int
216hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
217	      const struct xt_action_param *par,
218	      enum ipset_adt adt, struct ip_set_adt_opt *opt)
219{
220	const struct hash_ip6 *h = set->data;
221	ipset_adtfn adtfn = set->variant->adt[adt];
222	struct hash_ip6_elem e = { { .all = { 0 } } };
223	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
224
225	ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
226	nf_inet_addr_mask_inplace(&e.ip, &h->bitmask);
227	if (ipv6_addr_any(&e.ip.in6))
228		return -EINVAL;
229
230	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
231}
232
233static int
234hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
235	      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
236{
237	const struct hash_ip6 *h = set->data;
238	ipset_adtfn adtfn = set->variant->adt[adt];
239	struct hash_ip6_elem e = { { .all = { 0 } } };
240	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
241	int ret;
242
243	if (tb[IPSET_ATTR_LINENO])
244		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
245
246	if (unlikely(!tb[IPSET_ATTR_IP]))
247		return -IPSET_ERR_PROTOCOL;
248	if (unlikely(tb[IPSET_ATTR_IP_TO]))
249		return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
250	if (unlikely(tb[IPSET_ATTR_CIDR])) {
251		u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
252
253		if (cidr != HOST_MASK)
254			return -IPSET_ERR_INVALID_CIDR;
255	}
256
257	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
258	if (ret)
259		return ret;
260
261	ret = ip_set_get_extensions(set, tb, &ext);
262	if (ret)
263		return ret;
264
265	nf_inet_addr_mask_inplace(&e.ip, &h->bitmask);
266	if (ipv6_addr_any(&e.ip.in6))
267		return -IPSET_ERR_HASH_ELEM;
268
269	ret = adtfn(set, &e, &ext, &ext, flags);
270
271	return ip_set_eexist(ret, flags) ? 0 : ret;
272}
273
274static struct ip_set_type hash_ip_type __read_mostly = {
275	.name		= "hash:ip",
276	.protocol	= IPSET_PROTOCOL,
277	.features	= IPSET_TYPE_IP,
278	.dimension	= IPSET_DIM_ONE,
279	.family		= NFPROTO_UNSPEC,
280	.revision_min	= IPSET_TYPE_REV_MIN,
281	.revision_max	= IPSET_TYPE_REV_MAX,
282	.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
283	.create		= hash_ip_create,
284	.create_policy	= {
285		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
286		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
287		[IPSET_ATTR_INITVAL]	= { .type = NLA_U32 },
288		[IPSET_ATTR_BUCKETSIZE]	= { .type = NLA_U8 },
289		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
290		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
291		[IPSET_ATTR_NETMASK]	= { .type = NLA_U8  },
292		[IPSET_ATTR_BITMASK]	= { .type = NLA_NESTED },
293		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
294	},
295	.adt_policy	= {
296		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
297		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
298		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
299		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
300		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
301		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
302		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
303		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
304					    .len  = IPSET_MAX_COMMENT_SIZE },
305		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
306		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
307		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
308	},
309	.me		= THIS_MODULE,
310};
311
312static int __init
313hash_ip_init(void)
314{
315	return ip_set_type_register(&hash_ip_type);
316}
317
318static void __exit
319hash_ip_fini(void)
320{
321	rcu_barrier();
322	ip_set_type_unregister(&hash_ip_type);
323}
324
325module_init(hash_ip_init);
326module_exit(hash_ip_fini);
327