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,net type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <asm/uaccess.h>
16 #include <asm/bitops.h>
17 #include <linux/spinlock.h>
18 #include <linux/random.h>
21 #include <net/netlink.h>
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/ipset/pfxlen.h>
26 #include <linux/netfilter/ipset/ip_set.h>
27 #include <linux/netfilter/ipset/ip_set_timeout.h>
28 #include <linux/netfilter/ipset/ip_set_getport.h>
29 #include <linux/netfilter/ipset/ip_set_hash.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 MODULE_DESCRIPTION("hash:ip,port,net type of IP sets");
34 MODULE_ALIAS("ip_set_hash:ip,port,net");
36 /* Type specific function prefix */
37 #define TYPE hash_ipportnet
40 hash_ipportnet_same_set(const struct ip_set
*a
, const struct ip_set
*b
);
42 #define hash_ipportnet4_same_set hash_ipportnet_same_set
43 #define hash_ipportnet6_same_set hash_ipportnet_same_set
45 /* The type variant functions: IPv4 */
47 /* Member elements without timeout */
48 struct hash_ipportnet4_elem
{
56 /* Member elements with timeout support */
57 struct hash_ipportnet4_telem
{
63 unsigned long timeout
;
67 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem
*ip1
,
68 const struct hash_ipportnet4_elem
*ip2
)
70 return ip1
->ip
== ip2
->ip
&&
71 ip1
->ip2
== ip2
->ip2
&&
72 ip1
->cidr
== ip2
->cidr
&&
73 ip1
->port
== ip2
->port
&&
74 ip1
->proto
== ip2
->proto
;
78 hash_ipportnet4_data_isnull(const struct hash_ipportnet4_elem
*elem
)
80 return elem
->proto
== 0;
84 hash_ipportnet4_data_copy(struct hash_ipportnet4_elem
*dst
,
85 const struct hash_ipportnet4_elem
*src
)
87 memcpy(dst
, src
, sizeof(*dst
));
91 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem
*elem
, u8 cidr
)
93 elem
->ip2
&= ip_set_netmask(cidr
);
98 hash_ipportnet4_data_zero_out(struct hash_ipportnet4_elem
*elem
)
104 hash_ipportnet4_data_list(struct sk_buff
*skb
,
105 const struct hash_ipportnet4_elem
*data
)
107 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP
, data
->ip
);
108 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP2
, data
->ip2
);
109 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
110 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
111 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
119 hash_ipportnet4_data_tlist(struct sk_buff
*skb
,
120 const struct hash_ipportnet4_elem
*data
)
122 const struct hash_ipportnet4_telem
*tdata
=
123 (const struct hash_ipportnet4_telem
*)data
;
125 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP
, tdata
->ip
);
126 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP2
, tdata
->ip2
);
127 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, tdata
->port
);
128 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
129 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
130 NLA_PUT_NET32(skb
, IPSET_ATTR_TIMEOUT
,
131 htonl(ip_set_timeout_get(tdata
->timeout
)));
139 #define IP_SET_HASH_WITH_PROTO
140 #define IP_SET_HASH_WITH_NETS
144 #include <linux/netfilter/ipset/ip_set_ahash.h>
147 hash_ipportnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
148 enum ipset_adt adt
, u8 pf
, u8 dim
, u8 flags
)
150 const struct ip_set_hash
*h
= set
->data
;
151 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
152 struct hash_ipportnet4_elem data
=
153 { .cidr
= h
->nets
[0].cidr
|| HOST_MASK
};
157 if (adt
== IPSET_TEST
)
158 data
.cidr
= HOST_MASK
;
160 if (!ip_set_get_ip4_port(skb
, flags
& IPSET_DIM_TWO_SRC
,
161 &data
.port
, &data
.proto
))
164 ip4addrptr(skb
, flags
& IPSET_DIM_ONE_SRC
, &data
.ip
);
165 ip4addrptr(skb
, flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
);
166 data
.ip2
&= ip_set_netmask(data
.cidr
);
168 return adtfn(set
, &data
, h
->timeout
);
172 hash_ipportnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
173 enum ipset_adt adt
, u32
*lineno
, u32 flags
)
175 const struct ip_set_hash
*h
= set
->data
;
176 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
177 struct hash_ipportnet4_elem data
= { .cidr
= HOST_MASK
};
178 u32 ip
, ip_to
, p
, port
, port_to
;
179 u32 timeout
= h
->timeout
;
182 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
183 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
184 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
185 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
)))
186 return -IPSET_ERR_PROTOCOL
;
188 if (tb
[IPSET_ATTR_LINENO
])
189 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
191 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP
], &data
.ip
);
195 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
199 if (tb
[IPSET_ATTR_CIDR2
])
200 data
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
203 return -IPSET_ERR_INVALID_CIDR
;
205 data
.ip2
&= ip_set_netmask(data
.cidr
);
207 if (tb
[IPSET_ATTR_PORT
])
208 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
210 return -IPSET_ERR_PROTOCOL
;
212 if (tb
[IPSET_ATTR_PROTO
]) {
213 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
216 return -IPSET_ERR_INVALID_PROTO
;
218 return -IPSET_ERR_MISSING_PROTO
;
220 switch (data
.proto
) {
230 if (tb
[IPSET_ATTR_TIMEOUT
]) {
231 if (!with_timeout(h
->timeout
))
232 return -IPSET_ERR_TIMEOUT
;
233 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
236 if (adt
== IPSET_TEST
||
237 !(data
.proto
== IPPROTO_TCP
|| data
.proto
== IPPROTO_UDP
) ||
238 !(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_CIDR
] ||
239 tb
[IPSET_ATTR_PORT_TO
])) {
240 ret
= adtfn(set
, &data
, timeout
);
241 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
245 if (tb
[IPSET_ATTR_IP_TO
]) {
246 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
251 } else if (tb
[IPSET_ATTR_CIDR
]) {
252 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
255 return -IPSET_ERR_INVALID_CIDR
;
256 ip
&= ip_set_hostmask(cidr
);
257 ip_to
= ip
| ~ip_set_hostmask(cidr
);
261 port
= ntohs(data
.port
);
262 if (tb
[IPSET_ATTR_PORT_TO
]) {
263 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
269 for (; !before(ip_to
, ip
); ip
++)
270 for (p
= port
; p
<= port_to
; p
++) {
272 data
.port
= htons(p
);
273 ret
= adtfn(set
, &data
, timeout
);
275 if (ret
&& !ip_set_eexist(ret
, flags
))
284 hash_ipportnet_same_set(const struct ip_set
*a
, const struct ip_set
*b
)
286 const struct ip_set_hash
*x
= a
->data
;
287 const struct ip_set_hash
*y
= b
->data
;
289 /* Resizing changes htable_bits, so we ignore it */
290 return x
->maxelem
== y
->maxelem
&&
291 x
->timeout
== y
->timeout
;
294 /* The type variant functions: IPv6 */
296 struct hash_ipportnet6_elem
{
297 union nf_inet_addr ip
;
298 union nf_inet_addr ip2
;
304 struct hash_ipportnet6_telem
{
305 union nf_inet_addr ip
;
306 union nf_inet_addr ip2
;
310 unsigned long timeout
;
314 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem
*ip1
,
315 const struct hash_ipportnet6_elem
*ip2
)
317 return ipv6_addr_cmp(&ip1
->ip
.in6
, &ip2
->ip
.in6
) == 0 &&
318 ipv6_addr_cmp(&ip1
->ip2
.in6
, &ip2
->ip2
.in6
) == 0 &&
319 ip1
->cidr
== ip2
->cidr
&&
320 ip1
->port
== ip2
->port
&&
321 ip1
->proto
== ip2
->proto
;
325 hash_ipportnet6_data_isnull(const struct hash_ipportnet6_elem
*elem
)
327 return elem
->proto
== 0;
331 hash_ipportnet6_data_copy(struct hash_ipportnet6_elem
*dst
,
332 const struct hash_ipportnet6_elem
*src
)
334 memcpy(dst
, src
, sizeof(*dst
));
338 hash_ipportnet6_data_zero_out(struct hash_ipportnet6_elem
*elem
)
344 ip6_netmask(union nf_inet_addr
*ip
, u8 prefix
)
346 ip
->ip6
[0] &= ip_set_netmask6(prefix
)[0];
347 ip
->ip6
[1] &= ip_set_netmask6(prefix
)[1];
348 ip
->ip6
[2] &= ip_set_netmask6(prefix
)[2];
349 ip
->ip6
[3] &= ip_set_netmask6(prefix
)[3];
353 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem
*elem
, u8 cidr
)
355 ip6_netmask(&elem
->ip2
, cidr
);
360 hash_ipportnet6_data_list(struct sk_buff
*skb
,
361 const struct hash_ipportnet6_elem
*data
)
363 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP
, &data
->ip
);
364 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP2
, &data
->ip2
);
365 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
366 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
367 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
375 hash_ipportnet6_data_tlist(struct sk_buff
*skb
,
376 const struct hash_ipportnet6_elem
*data
)
378 const struct hash_ipportnet6_telem
*e
=
379 (const struct hash_ipportnet6_telem
*)data
;
381 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP
, &e
->ip
);
382 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP2
, &data
->ip2
);
383 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
384 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
385 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
386 NLA_PUT_NET32(skb
, IPSET_ATTR_TIMEOUT
,
387 htonl(ip_set_timeout_get(e
->timeout
)));
398 #define HOST_MASK 128
399 #include <linux/netfilter/ipset/ip_set_ahash.h>
402 hash_ipportnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
403 enum ipset_adt adt
, u8 pf
, u8 dim
, u8 flags
)
405 const struct ip_set_hash
*h
= set
->data
;
406 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
407 struct hash_ipportnet6_elem data
=
408 { .cidr
= h
->nets
[0].cidr
|| HOST_MASK
};
412 if (adt
== IPSET_TEST
)
413 data
.cidr
= HOST_MASK
;
415 if (!ip_set_get_ip6_port(skb
, flags
& IPSET_DIM_TWO_SRC
,
416 &data
.port
, &data
.proto
))
419 ip6addrptr(skb
, flags
& IPSET_DIM_ONE_SRC
, &data
.ip
.in6
);
420 ip6addrptr(skb
, flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
.in6
);
421 ip6_netmask(&data
.ip2
, data
.cidr
);
423 return adtfn(set
, &data
, h
->timeout
);
427 hash_ipportnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
428 enum ipset_adt adt
, u32
*lineno
, u32 flags
)
430 const struct ip_set_hash
*h
= set
->data
;
431 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
432 struct hash_ipportnet6_elem data
= { .cidr
= HOST_MASK
};
434 u32 timeout
= h
->timeout
;
437 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
438 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
439 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
440 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
441 tb
[IPSET_ATTR_IP_TO
] ||
442 tb
[IPSET_ATTR_CIDR
]))
443 return -IPSET_ERR_PROTOCOL
;
445 if (tb
[IPSET_ATTR_LINENO
])
446 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
448 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &data
.ip
);
452 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
456 if (tb
[IPSET_ATTR_CIDR2
])
457 data
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
460 return -IPSET_ERR_INVALID_CIDR
;
462 ip6_netmask(&data
.ip2
, data
.cidr
);
464 if (tb
[IPSET_ATTR_PORT
])
465 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
467 return -IPSET_ERR_PROTOCOL
;
469 if (tb
[IPSET_ATTR_PROTO
]) {
470 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
473 return -IPSET_ERR_INVALID_PROTO
;
475 return -IPSET_ERR_MISSING_PROTO
;
477 switch (data
.proto
) {
487 if (tb
[IPSET_ATTR_TIMEOUT
]) {
488 if (!with_timeout(h
->timeout
))
489 return -IPSET_ERR_TIMEOUT
;
490 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
493 if (adt
== IPSET_TEST
||
494 !(data
.proto
== IPPROTO_TCP
|| data
.proto
== IPPROTO_UDP
) ||
495 !tb
[IPSET_ATTR_PORT_TO
]) {
496 ret
= adtfn(set
, &data
, timeout
);
497 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
500 port
= ntohs(data
.port
);
501 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
505 for (; port
<= port_to
; port
++) {
506 data
.port
= htons(port
);
507 ret
= adtfn(set
, &data
, timeout
);
509 if (ret
&& !ip_set_eexist(ret
, flags
))
517 /* Create hash:ip type of sets */
520 hash_ipportnet_create(struct ip_set
*set
, struct nlattr
*tb
[], u32 flags
)
522 struct ip_set_hash
*h
;
523 u32 hashsize
= IPSET_DEFAULT_HASHSIZE
, maxelem
= IPSET_DEFAULT_MAXELEM
;
526 if (!(set
->family
== AF_INET
|| set
->family
== AF_INET6
))
527 return -IPSET_ERR_INVALID_FAMILY
;
529 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_HASHSIZE
) ||
530 !ip_set_optattr_netorder(tb
, IPSET_ATTR_MAXELEM
) ||
531 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
)))
532 return -IPSET_ERR_PROTOCOL
;
534 if (tb
[IPSET_ATTR_HASHSIZE
]) {
535 hashsize
= ip_set_get_h32(tb
[IPSET_ATTR_HASHSIZE
]);
536 if (hashsize
< IPSET_MIMINAL_HASHSIZE
)
537 hashsize
= IPSET_MIMINAL_HASHSIZE
;
540 if (tb
[IPSET_ATTR_MAXELEM
])
541 maxelem
= ip_set_get_h32(tb
[IPSET_ATTR_MAXELEM
]);
543 h
= kzalloc(sizeof(*h
)
544 + sizeof(struct ip_set_hash_nets
)
545 * (set
->family
== AF_INET
? 32 : 128), GFP_KERNEL
);
549 h
->maxelem
= maxelem
;
550 get_random_bytes(&h
->initval
, sizeof(h
->initval
));
551 h
->timeout
= IPSET_NO_TIMEOUT
;
553 hbits
= htable_bits(hashsize
);
554 h
->table
= ip_set_alloc(
555 sizeof(struct htable
)
556 + jhash_size(hbits
) * sizeof(struct hbucket
));
561 h
->table
->htable_bits
= hbits
;
565 if (tb
[IPSET_ATTR_TIMEOUT
]) {
566 h
->timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
568 set
->variant
= set
->family
== AF_INET
569 ? &hash_ipportnet4_tvariant
570 : &hash_ipportnet6_tvariant
;
572 if (set
->family
== AF_INET
)
573 hash_ipportnet4_gc_init(set
);
575 hash_ipportnet6_gc_init(set
);
577 set
->variant
= set
->family
== AF_INET
578 ? &hash_ipportnet4_variant
: &hash_ipportnet6_variant
;
581 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
582 set
->name
, jhash_size(h
->table
->htable_bits
),
583 h
->table
->htable_bits
, h
->maxelem
, set
->data
, h
->table
);
588 static struct ip_set_type hash_ipportnet_type __read_mostly
= {
589 .name
= "hash:ip,port,net",
590 .protocol
= IPSET_PROTOCOL
,
591 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
,
592 .dimension
= IPSET_DIM_THREE
,
595 .create
= hash_ipportnet_create
,
597 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
598 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
599 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
600 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
601 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
604 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
605 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
606 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
607 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
608 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
609 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
610 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
611 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
612 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
613 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
619 hash_ipportnet_init(void)
621 return ip_set_type_register(&hash_ipportnet_type
);
625 hash_ipportnet_fini(void)
627 ip_set_type_unregister(&hash_ipportnet_type
);
630 module_init(hash_ipportnet_init
);
631 module_exit(hash_ipportnet_fini
);