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