1 /* Copyright (C) 2003-2011 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,ip 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_timeout.h>
25 #include <linux/netfilter/ipset/ip_set_getport.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
28 #define REVISION_MIN 0
29 #define REVISION_MAX 1 /* SCTP and UDPLITE support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("hash:ip,port,ip", REVISION_MIN
, REVISION_MAX
);
34 MODULE_ALIAS("ip_set_hash:ip,port,ip");
36 /* Type specific function prefix */
37 #define TYPE hash_ipportip
40 hash_ipportip_same_set(const struct ip_set
*a
, const struct ip_set
*b
);
42 #define hash_ipportip4_same_set hash_ipportip_same_set
43 #define hash_ipportip6_same_set hash_ipportip_same_set
45 /* The type variant functions: IPv4 */
47 /* Member elements without timeout */
48 struct hash_ipportip4_elem
{
56 /* Member elements with timeout support */
57 struct hash_ipportip4_telem
{
63 unsigned long timeout
;
67 hash_ipportip4_data_equal(const struct hash_ipportip4_elem
*ip1
,
68 const struct hash_ipportip4_elem
*ip2
,
71 return ip1
->ip
== ip2
->ip
&&
72 ip1
->ip2
== ip2
->ip2
&&
73 ip1
->port
== ip2
->port
&&
74 ip1
->proto
== ip2
->proto
;
78 hash_ipportip4_data_isnull(const struct hash_ipportip4_elem
*elem
)
80 return elem
->proto
== 0;
84 hash_ipportip4_data_copy(struct hash_ipportip4_elem
*dst
,
85 const struct hash_ipportip4_elem
*src
)
87 memcpy(dst
, src
, sizeof(*dst
));
91 hash_ipportip4_data_zero_out(struct hash_ipportip4_elem
*elem
)
97 hash_ipportip4_data_list(struct sk_buff
*skb
,
98 const struct hash_ipportip4_elem
*data
)
100 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
101 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, data
->ip2
) ||
102 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
103 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
))
104 goto nla_put_failure
;
112 hash_ipportip4_data_tlist(struct sk_buff
*skb
,
113 const struct hash_ipportip4_elem
*data
)
115 const struct hash_ipportip4_telem
*tdata
=
116 (const struct hash_ipportip4_telem
*)data
;
118 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, tdata
->ip
) ||
119 nla_put_ipaddr4(skb
, IPSET_ATTR_IP2
, tdata
->ip2
) ||
120 nla_put_net16(skb
, IPSET_ATTR_PORT
, tdata
->port
) ||
121 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
122 nla_put_net32(skb
, IPSET_ATTR_TIMEOUT
,
123 htonl(ip_set_timeout_get(tdata
->timeout
))))
124 goto nla_put_failure
;
133 #include <linux/netfilter/ipset/ip_set_ahash.h>
136 hash_ipportip4_data_next(struct ip_set_hash
*h
,
137 const struct hash_ipportip4_elem
*d
)
140 h
->next
.port
= d
->port
;
144 hash_ipportip4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
145 const struct xt_action_param
*par
,
146 enum ipset_adt adt
, const struct ip_set_adt_opt
*opt
)
148 const struct ip_set_hash
*h
= set
->data
;
149 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
150 struct hash_ipportip4_elem data
= { };
152 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
153 &data
.port
, &data
.proto
))
156 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &data
.ip
);
157 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
);
159 return adtfn(set
, &data
, opt_timeout(opt
, h
), opt
->cmdflags
);
163 hash_ipportip4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
164 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
166 const struct ip_set_hash
*h
= set
->data
;
167 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
168 struct hash_ipportip4_elem data
= { };
169 u32 ip
, ip_to
, p
= 0, port
, port_to
;
170 u32 timeout
= h
->timeout
;
171 bool with_ports
= false;
174 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
175 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
176 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
177 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
)))
178 return -IPSET_ERR_PROTOCOL
;
180 if (tb
[IPSET_ATTR_LINENO
])
181 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
183 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP
], &data
.ip
);
187 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
191 if (tb
[IPSET_ATTR_PORT
])
192 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
194 return -IPSET_ERR_PROTOCOL
;
196 if (tb
[IPSET_ATTR_PROTO
]) {
197 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
198 with_ports
= ip_set_proto_with_ports(data
.proto
);
201 return -IPSET_ERR_INVALID_PROTO
;
203 return -IPSET_ERR_MISSING_PROTO
;
205 if (!(with_ports
|| data
.proto
== IPPROTO_ICMP
))
208 if (tb
[IPSET_ATTR_TIMEOUT
]) {
209 if (!with_timeout(h
->timeout
))
210 return -IPSET_ERR_TIMEOUT
;
211 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
214 if (adt
== IPSET_TEST
||
215 !(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_CIDR
] ||
216 tb
[IPSET_ATTR_PORT_TO
])) {
217 ret
= adtfn(set
, &data
, timeout
, flags
);
218 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
221 ip_to
= ip
= ntohl(data
.ip
);
222 if (tb
[IPSET_ATTR_IP_TO
]) {
223 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
228 } else if (tb
[IPSET_ATTR_CIDR
]) {
229 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
231 if (!cidr
|| cidr
> 32)
232 return -IPSET_ERR_INVALID_CIDR
;
233 ip_set_mask_from_to(ip
, ip_to
, cidr
);
236 port_to
= port
= ntohs(data
.port
);
237 if (with_ports
&& tb
[IPSET_ATTR_PORT_TO
]) {
238 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
244 ip
= ntohl(h
->next
.ip
);
245 for (; !before(ip_to
, ip
); ip
++) {
246 p
= retried
&& ip
== ntohl(h
->next
.ip
) ? ntohs(h
->next
.port
)
248 for (; p
<= port_to
; p
++) {
250 data
.port
= htons(p
);
251 ret
= adtfn(set
, &data
, timeout
, flags
);
253 if (ret
&& !ip_set_eexist(ret
, flags
))
263 hash_ipportip_same_set(const struct ip_set
*a
, const struct ip_set
*b
)
265 const struct ip_set_hash
*x
= a
->data
;
266 const struct ip_set_hash
*y
= b
->data
;
268 /* Resizing changes htable_bits, so we ignore it */
269 return x
->maxelem
== y
->maxelem
&&
270 x
->timeout
== y
->timeout
;
273 /* The type variant functions: IPv6 */
275 struct hash_ipportip6_elem
{
276 union nf_inet_addr ip
;
277 union nf_inet_addr ip2
;
283 struct hash_ipportip6_telem
{
284 union nf_inet_addr ip
;
285 union nf_inet_addr ip2
;
289 unsigned long timeout
;
293 hash_ipportip6_data_equal(const struct hash_ipportip6_elem
*ip1
,
294 const struct hash_ipportip6_elem
*ip2
,
297 return ipv6_addr_cmp(&ip1
->ip
.in6
, &ip2
->ip
.in6
) == 0 &&
298 ipv6_addr_cmp(&ip1
->ip2
.in6
, &ip2
->ip2
.in6
) == 0 &&
299 ip1
->port
== ip2
->port
&&
300 ip1
->proto
== ip2
->proto
;
304 hash_ipportip6_data_isnull(const struct hash_ipportip6_elem
*elem
)
306 return elem
->proto
== 0;
310 hash_ipportip6_data_copy(struct hash_ipportip6_elem
*dst
,
311 const struct hash_ipportip6_elem
*src
)
313 memcpy(dst
, src
, sizeof(*dst
));
317 hash_ipportip6_data_zero_out(struct hash_ipportip6_elem
*elem
)
323 hash_ipportip6_data_list(struct sk_buff
*skb
,
324 const struct hash_ipportip6_elem
*data
)
326 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
327 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip2
.in6
) ||
328 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
329 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
))
330 goto nla_put_failure
;
338 hash_ipportip6_data_tlist(struct sk_buff
*skb
,
339 const struct hash_ipportip6_elem
*data
)
341 const struct hash_ipportip6_telem
*e
=
342 (const struct hash_ipportip6_telem
*)data
;
344 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &e
->ip
.in6
) ||
345 nla_put_ipaddr6(skb
, IPSET_ATTR_IP2
, &data
->ip2
.in6
) ||
346 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
347 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
348 nla_put_net32(skb
, IPSET_ATTR_TIMEOUT
,
349 htonl(ip_set_timeout_get(e
->timeout
))))
350 goto nla_put_failure
;
361 #define HOST_MASK 128
362 #include <linux/netfilter/ipset/ip_set_ahash.h>
365 hash_ipportip6_data_next(struct ip_set_hash
*h
,
366 const struct hash_ipportip6_elem
*d
)
368 h
->next
.port
= d
->port
;
372 hash_ipportip6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
373 const struct xt_action_param
*par
,
374 enum ipset_adt adt
, const struct ip_set_adt_opt
*opt
)
376 const struct ip_set_hash
*h
= set
->data
;
377 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
378 struct hash_ipportip6_elem data
= { };
380 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
381 &data
.port
, &data
.proto
))
384 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &data
.ip
.in6
);
385 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
.in6
);
387 return adtfn(set
, &data
, opt_timeout(opt
, h
), opt
->cmdflags
);
391 hash_ipportip6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
392 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
394 const struct ip_set_hash
*h
= set
->data
;
395 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
396 struct hash_ipportip6_elem data
= { };
398 u32 timeout
= h
->timeout
;
399 bool with_ports
= false;
402 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
403 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
404 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
405 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
406 tb
[IPSET_ATTR_IP_TO
] ||
407 tb
[IPSET_ATTR_CIDR
]))
408 return -IPSET_ERR_PROTOCOL
;
410 if (tb
[IPSET_ATTR_LINENO
])
411 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
413 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &data
.ip
);
417 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
421 if (tb
[IPSET_ATTR_PORT
])
422 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
424 return -IPSET_ERR_PROTOCOL
;
426 if (tb
[IPSET_ATTR_PROTO
]) {
427 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
428 with_ports
= ip_set_proto_with_ports(data
.proto
);
431 return -IPSET_ERR_INVALID_PROTO
;
433 return -IPSET_ERR_MISSING_PROTO
;
435 if (!(with_ports
|| data
.proto
== IPPROTO_ICMPV6
))
438 if (tb
[IPSET_ATTR_TIMEOUT
]) {
439 if (!with_timeout(h
->timeout
))
440 return -IPSET_ERR_TIMEOUT
;
441 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
444 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
445 ret
= adtfn(set
, &data
, timeout
, flags
);
446 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
449 port
= ntohs(data
.port
);
450 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
455 port
= ntohs(h
->next
.port
);
456 for (; port
<= port_to
; port
++) {
457 data
.port
= htons(port
);
458 ret
= adtfn(set
, &data
, timeout
, flags
);
460 if (ret
&& !ip_set_eexist(ret
, flags
))
468 /* Create hash:ip type of sets */
471 hash_ipportip_create(struct ip_set
*set
, struct nlattr
*tb
[], u32 flags
)
473 struct ip_set_hash
*h
;
474 u32 hashsize
= IPSET_DEFAULT_HASHSIZE
, maxelem
= IPSET_DEFAULT_MAXELEM
;
478 if (!(set
->family
== NFPROTO_IPV4
|| set
->family
== NFPROTO_IPV6
))
479 return -IPSET_ERR_INVALID_FAMILY
;
481 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_HASHSIZE
) ||
482 !ip_set_optattr_netorder(tb
, IPSET_ATTR_MAXELEM
) ||
483 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
)))
484 return -IPSET_ERR_PROTOCOL
;
486 if (tb
[IPSET_ATTR_HASHSIZE
]) {
487 hashsize
= ip_set_get_h32(tb
[IPSET_ATTR_HASHSIZE
]);
488 if (hashsize
< IPSET_MIMINAL_HASHSIZE
)
489 hashsize
= IPSET_MIMINAL_HASHSIZE
;
492 if (tb
[IPSET_ATTR_MAXELEM
])
493 maxelem
= ip_set_get_h32(tb
[IPSET_ATTR_MAXELEM
]);
495 h
= kzalloc(sizeof(*h
), GFP_KERNEL
);
499 h
->maxelem
= maxelem
;
500 get_random_bytes(&h
->initval
, sizeof(h
->initval
));
501 h
->timeout
= IPSET_NO_TIMEOUT
;
503 hbits
= htable_bits(hashsize
);
504 hsize
= htable_size(hbits
);
509 h
->table
= ip_set_alloc(hsize
);
514 h
->table
->htable_bits
= hbits
;
518 if (tb
[IPSET_ATTR_TIMEOUT
]) {
519 h
->timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
521 set
->variant
= set
->family
== NFPROTO_IPV4
522 ? &hash_ipportip4_tvariant
: &hash_ipportip6_tvariant
;
524 if (set
->family
== NFPROTO_IPV4
)
525 hash_ipportip4_gc_init(set
);
527 hash_ipportip6_gc_init(set
);
529 set
->variant
= set
->family
== NFPROTO_IPV4
530 ? &hash_ipportip4_variant
: &hash_ipportip6_variant
;
533 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
534 set
->name
, jhash_size(h
->table
->htable_bits
),
535 h
->table
->htable_bits
, h
->maxelem
, set
->data
, h
->table
);
540 static struct ip_set_type hash_ipportip_type __read_mostly
= {
541 .name
= "hash:ip,port,ip",
542 .protocol
= IPSET_PROTOCOL
,
543 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
,
544 .dimension
= IPSET_DIM_THREE
,
545 .family
= NFPROTO_UNSPEC
,
546 .revision_min
= REVISION_MIN
,
547 .revision_max
= REVISION_MAX
,
548 .create
= hash_ipportip_create
,
550 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
551 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
552 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
553 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
554 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
557 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
558 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
559 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
560 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
561 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
562 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
563 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
564 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
565 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
571 hash_ipportip_init(void)
573 return ip_set_type_register(&hash_ipportip_type
);
577 hash_ipportip_fini(void)
579 ip_set_type_unregister(&hash_ipportip_type
);
582 module_init(hash_ipportip_init
);
583 module_exit(hash_ipportip_fini
);