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