1
2
3
4
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
16#include <linux/netfilter.h>
17#include <linux/netfilter/ipset/pfxlen.h>
18#include <linux/netfilter/ipset/ip_set.h>
19#include <linux/netfilter/ipset/ip_set_getport.h>
20#include <linux/netfilter/ipset/ip_set_hash.h>
21
22#define IPSET_TYPE_REV_MIN 0
23
24
25
26
27
28
29
30#define IPSET_TYPE_REV_MAX 8
31
32MODULE_LICENSE("GPL");
33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35MODULE_ALIAS("ip_set_hash:net,port");
36
37
38#define HTYPE hash_netport
39#define IP_SET_HASH_WITH_PROTO
40#define IP_SET_HASH_WITH_NETS
41
42
43
44
45
46#define IP_SET_HASH_WITH_NETS_PACKED
47
48
49
50
51struct hash_netport4_elem {
52 __be32 ip;
53 __be16 port;
54 u8 proto;
55 u8 cidr:7;
56 u8 nomatch:1;
57};
58
59
60
61static bool
62hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
63 const struct hash_netport4_elem *ip2,
64 u32 *multi)
65{
66 return ip1->ip == ip2->ip &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto &&
69 ip1->cidr == ip2->cidr;
70}
71
72static int
73hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
74{
75 return elem->nomatch ? -ENOTEMPTY : 1;
76}
77
78static void
79hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
80{
81 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
82}
83
84static void
85hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
86{
87 swap(*flags, elem->nomatch);
88}
89
90static void
91hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
92{
93 elem->ip &= ip_set_netmask(cidr);
94 elem->cidr = cidr - 1;
95}
96
97static bool
98hash_netport4_data_list(struct sk_buff *skb,
99 const struct hash_netport4_elem *data)
100{
101 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
102
103 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
104 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
105 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
106 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
107 (flags &&
108 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
109 goto nla_put_failure;
110 return false;
111
112nla_put_failure:
113 return true;
114}
115
116static void
117hash_netport4_data_next(struct hash_netport4_elem *next,
118 const struct hash_netport4_elem *d)
119{
120 next->ip = d->ip;
121 next->port = d->port;
122}
123
124#define MTYPE hash_netport4
125#define HOST_MASK 32
126#include "ip_set_hash_gen.h"
127
128static int
129hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
130 const struct xt_action_param *par,
131 enum ipset_adt adt, struct ip_set_adt_opt *opt)
132{
133 const struct hash_netport4 *h = set->data;
134 ipset_adtfn adtfn = set->variant->adt[adt];
135 struct hash_netport4_elem e = {
136 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
137 };
138 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
139
140 if (adt == IPSET_TEST)
141 e.cidr = HOST_MASK - 1;
142
143 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
144 &e.port, &e.proto))
145 return -EINVAL;
146
147 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
148 e.ip &= ip_set_netmask(e.cidr + 1);
149
150 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
151}
152
153static int
154hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
155 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
156{
157 const struct hash_netport4 *h = set->data;
158 ipset_adtfn adtfn = set->variant->adt[adt];
159 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
160 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
161 u32 port, port_to, p = 0, ip = 0, ip_to = 0, ipn;
162 u64 n = 0;
163 bool with_ports = false;
164 u8 cidr;
165 int ret;
166
167 if (tb[IPSET_ATTR_LINENO])
168 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
169
170 if (unlikely(!tb[IPSET_ATTR_IP] ||
171 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
172 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
174 return -IPSET_ERR_PROTOCOL;
175
176 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
177 if (ret)
178 return ret;
179
180 ret = ip_set_get_extensions(set, tb, &ext);
181 if (ret)
182 return ret;
183
184 if (tb[IPSET_ATTR_CIDR]) {
185 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
186 if (!cidr || cidr > HOST_MASK)
187 return -IPSET_ERR_INVALID_CIDR;
188 e.cidr = cidr - 1;
189 }
190
191 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
192
193 if (tb[IPSET_ATTR_PROTO]) {
194 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
195 with_ports = ip_set_proto_with_ports(e.proto);
196
197 if (e.proto == 0)
198 return -IPSET_ERR_INVALID_PROTO;
199 } else {
200 return -IPSET_ERR_MISSING_PROTO;
201 }
202
203 if (!(with_ports || e.proto == IPPROTO_ICMP))
204 e.port = 0;
205
206 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
207
208 if (tb[IPSET_ATTR_CADT_FLAGS]) {
209 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
210
211 if (cadt_flags & IPSET_FLAG_NOMATCH)
212 flags |= (IPSET_FLAG_NOMATCH << 16);
213 }
214
215 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
216 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
217 ret = adtfn(set, &e, &ext, &ext, flags);
218 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
219 ip_set_eexist(ret, flags) ? 0 : ret;
220 }
221
222 port = port_to = ntohs(e.port);
223 if (tb[IPSET_ATTR_PORT_TO]) {
224 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
225 if (port_to < port)
226 swap(port, port_to);
227 }
228 if (tb[IPSET_ATTR_IP_TO]) {
229 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
230 if (ret)
231 return ret;
232 if (ip_to < ip)
233 swap(ip, ip_to);
234 if (ip + UINT_MAX == ip_to)
235 return -IPSET_ERR_HASH_RANGE;
236 } else {
237 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
238 }
239 ipn = ip;
240 do {
241 ipn = ip_set_range_to_cidr(ipn, ip_to, &cidr);
242 n++;
243 } while (ipn++ < ip_to);
244
245 if (n*(port_to - port + 1) > IPSET_MAX_RANGE)
246 return -ERANGE;
247
248 if (retried) {
249 ip = ntohl(h->next.ip);
250 p = ntohs(h->next.port);
251 } else {
252 p = port;
253 }
254 do {
255 e.ip = htonl(ip);
256 ip = ip_set_range_to_cidr(ip, ip_to, &cidr);
257 e.cidr = cidr - 1;
258 for (; p <= port_to; p++) {
259 e.port = htons(p);
260 ret = adtfn(set, &e, &ext, &ext, flags);
261 if (ret && !ip_set_eexist(ret, flags))
262 return ret;
263
264 ret = 0;
265 }
266 p = port;
267 } while (ip++ < ip_to);
268 return ret;
269}
270
271
272
273struct hash_netport6_elem {
274 union nf_inet_addr ip;
275 __be16 port;
276 u8 proto;
277 u8 cidr:7;
278 u8 nomatch:1;
279};
280
281
282
283static bool
284hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
285 const struct hash_netport6_elem *ip2,
286 u32 *multi)
287{
288 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
289 ip1->port == ip2->port &&
290 ip1->proto == ip2->proto &&
291 ip1->cidr == ip2->cidr;
292}
293
294static int
295hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
296{
297 return elem->nomatch ? -ENOTEMPTY : 1;
298}
299
300static void
301hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
302{
303 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
304}
305
306static void
307hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
308{
309 swap(*flags, elem->nomatch);
310}
311
312static void
313hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
314{
315 ip6_netmask(&elem->ip, cidr);
316 elem->cidr = cidr - 1;
317}
318
319static bool
320hash_netport6_data_list(struct sk_buff *skb,
321 const struct hash_netport6_elem *data)
322{
323 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
324
325 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
326 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
327 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
328 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
329 (flags &&
330 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
331 goto nla_put_failure;
332 return false;
333
334nla_put_failure:
335 return true;
336}
337
338static void
339hash_netport6_data_next(struct hash_netport6_elem *next,
340 const struct hash_netport6_elem *d)
341{
342 next->port = d->port;
343}
344
345#undef MTYPE
346#undef HOST_MASK
347
348#define MTYPE hash_netport6
349#define HOST_MASK 128
350#define IP_SET_EMIT_CREATE
351#include "ip_set_hash_gen.h"
352
353static int
354hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
355 const struct xt_action_param *par,
356 enum ipset_adt adt, struct ip_set_adt_opt *opt)
357{
358 const struct hash_netport6 *h = set->data;
359 ipset_adtfn adtfn = set->variant->adt[adt];
360 struct hash_netport6_elem e = {
361 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
362 };
363 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
364
365 if (adt == IPSET_TEST)
366 e.cidr = HOST_MASK - 1;
367
368 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
369 &e.port, &e.proto))
370 return -EINVAL;
371
372 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
373 ip6_netmask(&e.ip, e.cidr + 1);
374
375 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
376}
377
378static int
379hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
380 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
381{
382 const struct hash_netport6 *h = set->data;
383 ipset_adtfn adtfn = set->variant->adt[adt];
384 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
385 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
386 u32 port, port_to;
387 bool with_ports = false;
388 u8 cidr;
389 int ret;
390
391 if (tb[IPSET_ATTR_LINENO])
392 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
393
394 if (unlikely(!tb[IPSET_ATTR_IP] ||
395 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
396 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
397 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
398 return -IPSET_ERR_PROTOCOL;
399 if (unlikely(tb[IPSET_ATTR_IP_TO]))
400 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
401
402 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
403 if (ret)
404 return ret;
405
406 ret = ip_set_get_extensions(set, tb, &ext);
407 if (ret)
408 return ret;
409
410 if (tb[IPSET_ATTR_CIDR]) {
411 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
412 if (!cidr || cidr > HOST_MASK)
413 return -IPSET_ERR_INVALID_CIDR;
414 e.cidr = cidr - 1;
415 }
416 ip6_netmask(&e.ip, e.cidr + 1);
417
418 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
419
420 if (tb[IPSET_ATTR_PROTO]) {
421 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
422 with_ports = ip_set_proto_with_ports(e.proto);
423
424 if (e.proto == 0)
425 return -IPSET_ERR_INVALID_PROTO;
426 } else {
427 return -IPSET_ERR_MISSING_PROTO;
428 }
429
430 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
431 e.port = 0;
432
433 if (tb[IPSET_ATTR_CADT_FLAGS]) {
434 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
435
436 if (cadt_flags & IPSET_FLAG_NOMATCH)
437 flags |= (IPSET_FLAG_NOMATCH << 16);
438 }
439
440 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
441 ret = adtfn(set, &e, &ext, &ext, flags);
442 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
443 ip_set_eexist(ret, flags) ? 0 : ret;
444 }
445
446 port = ntohs(e.port);
447 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
448 if (port > port_to)
449 swap(port, port_to);
450
451 if (retried)
452 port = ntohs(h->next.port);
453 for (; port <= port_to; port++) {
454 e.port = htons(port);
455 ret = adtfn(set, &e, &ext, &ext, flags);
456
457 if (ret && !ip_set_eexist(ret, flags))
458 return ret;
459
460 ret = 0;
461 }
462 return ret;
463}
464
465static struct ip_set_type hash_netport_type __read_mostly = {
466 .name = "hash:net,port",
467 .protocol = IPSET_PROTOCOL,
468 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
469 .dimension = IPSET_DIM_TWO,
470 .family = NFPROTO_UNSPEC,
471 .revision_min = IPSET_TYPE_REV_MIN,
472 .revision_max = IPSET_TYPE_REV_MAX,
473 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
474 .create = hash_netport_create,
475 .create_policy = {
476 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
477 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
478 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
479 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
480 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
481 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
482 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
483 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
484 },
485 .adt_policy = {
486 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
487 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
488 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
489 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
490 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
491 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
492 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
493 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
494 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
495 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
496 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
497 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
498 .len = IPSET_MAX_COMMENT_SIZE },
499 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
500 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
501 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
502 },
503 .me = THIS_MODULE,
504};
505
506static int __init
507hash_netport_init(void)
508{
509 return ip_set_type_register(&hash_netport_type);
510}
511
512static void __exit
513hash_netport_fini(void)
514{
515 rcu_barrier();
516 ip_set_type_unregister(&hash_netport_type);
517}
518
519module_init(hash_netport_init);
520module_exit(hash_netport_fini);
521