1 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the hash:ip,port,net type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
18 #include <net/netlink.h>
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>
27 #define IPSET_TYPE_REV_MIN 0
28 /* 0 Comments support added */
29 /* 1 Forceadd support added */
30 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
34 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
35 MODULE_ALIAS("ip_set_hash:net,port,net");
37 /* Type specific function prefix */
38 #define HTYPE hash_netportnet
39 #define IP_SET_HASH_WITH_PROTO
40 #define IP_SET_HASH_WITH_NETS
41 #define IPSET_NET_COUNT 2
46 struct hash_netportnet4_elem
{
61 /* Common functions */
64 hash_netportnet4_data_equal(const struct hash_netportnet4_elem
*ip1
,
65 const struct hash_netportnet4_elem
*ip2
,
68 return ip1
->ipcmp
== ip2
->ipcmp
&&
69 ip1
->ccmp
== ip2
->ccmp
&&
70 ip1
->port
== ip2
->port
&&
71 ip1
->proto
== ip2
->proto
;
75 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem
*elem
)
77 return elem
->nomatch
? -ENOTEMPTY
: 1;
81 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem
*elem
, u32 flags
)
83 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
87 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem
*elem
, u8
*flags
)
89 swap(*flags
, elem
->nomatch
);
93 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem
*elem
,
94 struct hash_netportnet4_elem
*orig
)
96 elem
->ip
[1] = orig
->ip
[1];
100 hash_netportnet4_data_netmask(struct hash_netportnet4_elem
*elem
,
104 elem
->ip
[1] &= ip_set_netmask(cidr
);
105 elem
->cidr
[1] = cidr
;
107 elem
->ip
[0] &= ip_set_netmask(cidr
);
108 elem
->cidr
[0] = cidr
;
113 hash_netportnet4_data_list(struct sk_buff
*skb
,
114 const struct hash_netportnet4_elem
*data
)
116 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
118 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
[0]) ||
119 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, data
->ip
[1]) ||
120 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
121 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
[0]) ||
122 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
[1]) ||
123 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
125 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
126 goto nla_put_failure
;
134 hash_netportnet4_data_next(struct hash_netportnet4_elem
*next
,
135 const struct hash_netportnet4_elem
*d
)
137 next
->ipcmp
= d
->ipcmp
;
138 next
->port
= d
->port
;
141 #define MTYPE hash_netportnet4
143 #include "ip_set_hash_gen.h"
146 hash_netportnet4_init(struct hash_netportnet4_elem
*e
)
148 e
->cidr
[0] = HOST_MASK
;
149 e
->cidr
[1] = HOST_MASK
;
153 hash_netportnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
154 const struct xt_action_param
*par
,
155 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
157 const struct hash_netportnet
*h
= set
->data
;
158 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
159 struct hash_netportnet4_elem e
= { };
160 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
162 e
.cidr
[0] = INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
);
163 e
.cidr
[1] = INIT_CIDR(h
->nets
[0].cidr
[1], HOST_MASK
);
164 if (adt
== IPSET_TEST
)
165 e
.ccmp
= (HOST_MASK
<< (sizeof(e
.cidr
[0]) * 8)) | HOST_MASK
;
167 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
171 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
[0]);
172 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip
[1]);
173 e
.ip
[0] &= ip_set_netmask(e
.cidr
[0]);
174 e
.ip
[1] &= ip_set_netmask(e
.cidr
[1]);
176 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
180 hash_netportnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
181 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
183 const struct hash_netportnet
*h
= set
->data
;
184 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
185 struct hash_netportnet4_elem e
= { };
186 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
187 u32 ip
= 0, ip_to
= 0, ip_last
, p
= 0, port
, port_to
;
188 u32 ip2_from
= 0, ip2_to
= 0, ip2_last
, ip2
;
189 bool with_ports
= false;
192 if (tb
[IPSET_ATTR_LINENO
])
193 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
195 hash_netportnet4_init(&e
);
196 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
197 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
198 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
199 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
200 return -IPSET_ERR_PROTOCOL
;
202 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
206 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2
], &ip2_from
);
210 ret
= ip_set_get_extensions(set
, tb
, &ext
);
214 if (tb
[IPSET_ATTR_CIDR
]) {
215 e
.cidr
[0] = nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
216 if (!e
.cidr
[0] || e
.cidr
[0] > HOST_MASK
)
217 return -IPSET_ERR_INVALID_CIDR
;
220 if (tb
[IPSET_ATTR_CIDR2
]) {
221 e
.cidr
[1] = nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
222 if (!e
.cidr
[1] || e
.cidr
[1] > HOST_MASK
)
223 return -IPSET_ERR_INVALID_CIDR
;
226 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
228 if (tb
[IPSET_ATTR_PROTO
]) {
229 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
230 with_ports
= ip_set_proto_with_ports(e
.proto
);
233 return -IPSET_ERR_INVALID_PROTO
;
235 return -IPSET_ERR_MISSING_PROTO
;
238 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
241 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
242 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
244 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
245 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
248 with_ports
= with_ports
&& tb
[IPSET_ATTR_PORT_TO
];
249 if (adt
== IPSET_TEST
||
250 !(tb
[IPSET_ATTR_IP_TO
] || with_ports
|| tb
[IPSET_ATTR_IP2_TO
])) {
251 e
.ip
[0] = htonl(ip
& ip_set_hostmask(e
.cidr
[0]));
252 e
.ip
[1] = htonl(ip2_from
& ip_set_hostmask(e
.cidr
[1]));
253 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
254 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
255 ip_set_eexist(ret
, flags
) ? 0 : ret
;
259 if (tb
[IPSET_ATTR_IP_TO
]) {
260 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
265 if (unlikely(ip
+ UINT_MAX
== ip_to
))
266 return -IPSET_ERR_HASH_RANGE
;
268 ip_set_mask_from_to(ip
, ip_to
, e
.cidr
[0]);
271 port_to
= port
= ntohs(e
.port
);
272 if (tb
[IPSET_ATTR_PORT_TO
]) {
273 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
279 if (tb
[IPSET_ATTR_IP2_TO
]) {
280 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP2_TO
], &ip2_to
);
283 if (ip2_from
> ip2_to
)
284 swap(ip2_from
, ip2_to
);
285 if (unlikely(ip2_from
+ UINT_MAX
== ip2_to
))
286 return -IPSET_ERR_HASH_RANGE
;
288 ip_set_mask_from_to(ip2_from
, ip2_to
, e
.cidr
[1]);
292 ip
= ntohl(h
->next
.ip
[0]);
294 while (!after(ip
, ip_to
)) {
296 ip_last
= ip_set_range_to_cidr(ip
, ip_to
, &e
.cidr
[0]);
297 p
= retried
&& ip
== ntohl(h
->next
.ip
[0]) ? ntohs(h
->next
.port
)
299 for (; p
<= port_to
; p
++) {
301 ip2
= (retried
&& ip
== ntohl(h
->next
.ip
[0]) &&
302 p
== ntohs(h
->next
.port
)) ? ntohl(h
->next
.ip
[1])
304 while (!after(ip2
, ip2_to
)) {
305 e
.ip
[1] = htonl(ip2
);
306 ip2_last
= ip_set_range_to_cidr(ip2
, ip2_to
,
308 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
309 if (ret
&& !ip_set_eexist(ret
, flags
))
323 struct hash_netportnet6_elem
{
324 union nf_inet_addr ip
[2];
335 /* Common functions */
338 hash_netportnet6_data_equal(const struct hash_netportnet6_elem
*ip1
,
339 const struct hash_netportnet6_elem
*ip2
,
342 return ipv6_addr_equal(&ip1
->ip
[0].in6
, &ip2
->ip
[0].in6
) &&
343 ipv6_addr_equal(&ip1
->ip
[1].in6
, &ip2
->ip
[1].in6
) &&
344 ip1
->ccmp
== ip2
->ccmp
&&
345 ip1
->port
== ip2
->port
&&
346 ip1
->proto
== ip2
->proto
;
350 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem
*elem
)
352 return elem
->nomatch
? -ENOTEMPTY
: 1;
356 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem
*elem
, u32 flags
)
358 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
362 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem
*elem
, u8
*flags
)
364 swap(*flags
, elem
->nomatch
);
368 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem
*elem
,
369 struct hash_netportnet6_elem
*orig
)
371 elem
->ip
[1] = orig
->ip
[1];
375 hash_netportnet6_data_netmask(struct hash_netportnet6_elem
*elem
,
379 ip6_netmask(&elem
->ip
[1], cidr
);
380 elem
->cidr
[1] = cidr
;
382 ip6_netmask(&elem
->ip
[0], cidr
);
383 elem
->cidr
[0] = cidr
;
388 hash_netportnet6_data_list(struct sk_buff
*skb
,
389 const struct hash_netportnet6_elem
*data
)
391 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
393 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
[0].in6
) ||
394 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip
[1].in6
) ||
395 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
396 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
[0]) ||
397 nla_put_u8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
[1]) ||
398 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
400 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
401 goto nla_put_failure
;
409 hash_netportnet6_data_next(struct hash_netportnet4_elem
*next
,
410 const struct hash_netportnet6_elem
*d
)
412 next
->port
= d
->port
;
418 #define MTYPE hash_netportnet6
419 #define HOST_MASK 128
420 #define IP_SET_EMIT_CREATE
421 #include "ip_set_hash_gen.h"
424 hash_netportnet6_init(struct hash_netportnet6_elem
*e
)
426 e
->cidr
[0] = HOST_MASK
;
427 e
->cidr
[1] = HOST_MASK
;
431 hash_netportnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
432 const struct xt_action_param
*par
,
433 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
435 const struct hash_netportnet
*h
= set
->data
;
436 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
437 struct hash_netportnet6_elem e
= { };
438 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
440 e
.cidr
[0] = INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
);
441 e
.cidr
[1] = INIT_CIDR(h
->nets
[0].cidr
[1], HOST_MASK
);
442 if (adt
== IPSET_TEST
)
443 e
.ccmp
= (HOST_MASK
<< (sizeof(u8
) * 8)) | HOST_MASK
;
445 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
449 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
[0].in6
);
450 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &e
.ip
[1].in6
);
451 ip6_netmask(&e
.ip
[0], e
.cidr
[0]);
452 ip6_netmask(&e
.ip
[1], e
.cidr
[1]);
454 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
458 hash_netportnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
459 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
461 const struct hash_netportnet
*h
= set
->data
;
462 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
463 struct hash_netportnet6_elem e
= { };
464 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
466 bool with_ports
= false;
469 if (tb
[IPSET_ATTR_LINENO
])
470 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
472 hash_netportnet6_init(&e
);
473 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
474 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
475 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
476 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
477 return -IPSET_ERR_PROTOCOL
;
478 if (unlikely(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_IP2_TO
]))
479 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
481 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
[0]);
485 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &e
.ip
[1]);
489 ret
= ip_set_get_extensions(set
, tb
, &ext
);
493 if (tb
[IPSET_ATTR_CIDR
]) {
494 e
.cidr
[0] = nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
495 if (!e
.cidr
[0] || e
.cidr
[0] > HOST_MASK
)
496 return -IPSET_ERR_INVALID_CIDR
;
499 if (tb
[IPSET_ATTR_CIDR2
]) {
500 e
.cidr
[1] = nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
501 if (!e
.cidr
[1] || e
.cidr
[1] > HOST_MASK
)
502 return -IPSET_ERR_INVALID_CIDR
;
505 ip6_netmask(&e
.ip
[0], e
.cidr
[0]);
506 ip6_netmask(&e
.ip
[1], e
.cidr
[1]);
508 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
510 if (tb
[IPSET_ATTR_PROTO
]) {
511 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
512 with_ports
= ip_set_proto_with_ports(e
.proto
);
515 return -IPSET_ERR_INVALID_PROTO
;
517 return -IPSET_ERR_MISSING_PROTO
;
520 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
523 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
524 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
526 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
527 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
530 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
531 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
532 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
533 ip_set_eexist(ret
, flags
) ? 0 : ret
;
536 port
= ntohs(e
.port
);
537 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
542 port
= ntohs(h
->next
.port
);
543 for (; port
<= port_to
; port
++) {
544 e
.port
= htons(port
);
545 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
547 if (ret
&& !ip_set_eexist(ret
, flags
))
555 static struct ip_set_type hash_netportnet_type __read_mostly
= {
556 .name
= "hash:net,port,net",
557 .protocol
= IPSET_PROTOCOL
,
558 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
|
560 .dimension
= IPSET_DIM_THREE
,
561 .family
= NFPROTO_UNSPEC
,
562 .revision_min
= IPSET_TYPE_REV_MIN
,
563 .revision_max
= IPSET_TYPE_REV_MAX
,
564 .create
= hash_netportnet_create
,
566 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
567 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
568 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
569 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
570 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
571 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
574 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
575 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
576 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
577 [IPSET_ATTR_IP2_TO
] = { .type
= NLA_NESTED
},
578 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
579 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
580 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
581 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
582 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
583 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
584 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
585 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
586 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
587 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
588 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
589 .len
= IPSET_MAX_COMMENT_SIZE
},
590 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
591 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
592 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
598 hash_netportnet_init(void)
600 return ip_set_type_register(&hash_netportnet_type
);
604 hash_netportnet_fini(void)
607 ip_set_type_unregister(&hash_netportnet_type
);
610 module_init(hash_netportnet_init
);
611 module_exit(hash_netportnet_fini
);