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 <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 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
30 MODULE_DESCRIPTION("hash:ip,port,net type of IP sets");
31 MODULE_ALIAS("ip_set_hash:ip,port,net");
33 /* Type specific function prefix */
34 #define TYPE hash_ipportnet
37 hash_ipportnet_same_set(const struct ip_set
*a
, const struct ip_set
*b
);
39 #define hash_ipportnet4_same_set hash_ipportnet_same_set
40 #define hash_ipportnet6_same_set hash_ipportnet_same_set
42 /* The type variant functions: IPv4 */
44 /* Member elements without timeout */
45 struct hash_ipportnet4_elem
{
53 /* Member elements with timeout support */
54 struct hash_ipportnet4_telem
{
60 unsigned long timeout
;
64 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem
*ip1
,
65 const struct hash_ipportnet4_elem
*ip2
)
67 return ip1
->ip
== ip2
->ip
&&
68 ip1
->ip2
== ip2
->ip2
&&
69 ip1
->cidr
== ip2
->cidr
&&
70 ip1
->port
== ip2
->port
&&
71 ip1
->proto
== ip2
->proto
;
75 hash_ipportnet4_data_isnull(const struct hash_ipportnet4_elem
*elem
)
77 return elem
->proto
== 0;
81 hash_ipportnet4_data_copy(struct hash_ipportnet4_elem
*dst
,
82 const struct hash_ipportnet4_elem
*src
)
84 memcpy(dst
, src
, sizeof(*dst
));
88 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem
*elem
, u8 cidr
)
90 elem
->ip2
&= ip_set_netmask(cidr
);
95 hash_ipportnet4_data_zero_out(struct hash_ipportnet4_elem
*elem
)
101 hash_ipportnet4_data_list(struct sk_buff
*skb
,
102 const struct hash_ipportnet4_elem
*data
)
104 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP
, data
->ip
);
105 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP2
, data
->ip2
);
106 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
107 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
108 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
116 hash_ipportnet4_data_tlist(struct sk_buff
*skb
,
117 const struct hash_ipportnet4_elem
*data
)
119 const struct hash_ipportnet4_telem
*tdata
=
120 (const struct hash_ipportnet4_telem
*)data
;
122 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP
, tdata
->ip
);
123 NLA_PUT_IPADDR4(skb
, IPSET_ATTR_IP2
, tdata
->ip2
);
124 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, tdata
->port
);
125 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
126 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
127 NLA_PUT_NET32(skb
, IPSET_ATTR_TIMEOUT
,
128 htonl(ip_set_timeout_get(tdata
->timeout
)));
136 #define IP_SET_HASH_WITH_PROTO
137 #define IP_SET_HASH_WITH_NETS
141 #include <linux/netfilter/ipset/ip_set_ahash.h>
144 hash_ipportnet4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
145 enum ipset_adt adt
, u8 pf
, u8 dim
, u8 flags
)
147 const struct ip_set_hash
*h
= set
->data
;
148 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
149 struct hash_ipportnet4_elem data
=
150 { .cidr
= h
->nets
[0].cidr
|| HOST_MASK
};
154 if (adt
== IPSET_TEST
)
155 data
.cidr
= HOST_MASK
;
157 if (!ip_set_get_ip4_port(skb
, flags
& IPSET_DIM_TWO_SRC
,
158 &data
.port
, &data
.proto
))
161 ip4addrptr(skb
, flags
& IPSET_DIM_ONE_SRC
, &data
.ip
);
162 ip4addrptr(skb
, flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
);
163 data
.ip2
&= ip_set_netmask(data
.cidr
);
165 return adtfn(set
, &data
, h
->timeout
);
169 hash_ipportnet4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
170 enum ipset_adt adt
, u32
*lineno
, u32 flags
)
172 const struct ip_set_hash
*h
= set
->data
;
173 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
174 struct hash_ipportnet4_elem data
= { .cidr
= HOST_MASK
};
175 u32 ip
, ip_to
, p
, port
, port_to
;
176 u32 timeout
= h
->timeout
;
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_TIMEOUT
)))
183 return -IPSET_ERR_PROTOCOL
;
185 if (tb
[IPSET_ATTR_LINENO
])
186 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
188 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP
], &data
.ip
);
192 ret
= ip_set_get_ipaddr4(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
196 if (tb
[IPSET_ATTR_CIDR2
])
197 data
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
200 return -IPSET_ERR_INVALID_CIDR
;
202 data
.ip2
&= ip_set_netmask(data
.cidr
);
204 if (tb
[IPSET_ATTR_PORT
])
205 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
207 return -IPSET_ERR_PROTOCOL
;
209 if (tb
[IPSET_ATTR_PROTO
]) {
210 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
213 return -IPSET_ERR_INVALID_PROTO
;
215 return -IPSET_ERR_MISSING_PROTO
;
217 switch (data
.proto
) {
227 if (tb
[IPSET_ATTR_TIMEOUT
]) {
228 if (!with_timeout(h
->timeout
))
229 return -IPSET_ERR_TIMEOUT
;
230 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
233 if (adt
== IPSET_TEST
||
234 !(data
.proto
== IPPROTO_TCP
|| data
.proto
== IPPROTO_UDP
) ||
235 !(tb
[IPSET_ATTR_IP_TO
] || tb
[IPSET_ATTR_CIDR
] ||
236 tb
[IPSET_ATTR_PORT_TO
])) {
237 ret
= adtfn(set
, &data
, timeout
);
238 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
242 if (tb
[IPSET_ATTR_IP_TO
]) {
243 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
248 } else if (tb
[IPSET_ATTR_CIDR
]) {
249 u8 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
252 return -IPSET_ERR_INVALID_CIDR
;
253 ip
&= ip_set_hostmask(cidr
);
254 ip_to
= ip
| ~ip_set_hostmask(cidr
);
258 port
= ntohs(data
.port
);
259 if (tb
[IPSET_ATTR_PORT_TO
]) {
260 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
266 for (; !before(ip_to
, ip
); ip
++)
267 for (p
= port
; p
<= port_to
; p
++) {
269 data
.port
= htons(p
);
270 ret
= adtfn(set
, &data
, timeout
);
272 if (ret
&& !ip_set_eexist(ret
, flags
))
281 hash_ipportnet_same_set(const struct ip_set
*a
, const struct ip_set
*b
)
283 const struct ip_set_hash
*x
= a
->data
;
284 const struct ip_set_hash
*y
= b
->data
;
286 /* Resizing changes htable_bits, so we ignore it */
287 return x
->maxelem
== y
->maxelem
&&
288 x
->timeout
== y
->timeout
;
291 /* The type variant functions: IPv6 */
293 struct hash_ipportnet6_elem
{
294 union nf_inet_addr ip
;
295 union nf_inet_addr ip2
;
301 struct hash_ipportnet6_telem
{
302 union nf_inet_addr ip
;
303 union nf_inet_addr ip2
;
307 unsigned long timeout
;
311 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem
*ip1
,
312 const struct hash_ipportnet6_elem
*ip2
)
314 return ipv6_addr_cmp(&ip1
->ip
.in6
, &ip2
->ip
.in6
) == 0 &&
315 ipv6_addr_cmp(&ip1
->ip2
.in6
, &ip2
->ip2
.in6
) == 0 &&
316 ip1
->cidr
== ip2
->cidr
&&
317 ip1
->port
== ip2
->port
&&
318 ip1
->proto
== ip2
->proto
;
322 hash_ipportnet6_data_isnull(const struct hash_ipportnet6_elem
*elem
)
324 return elem
->proto
== 0;
328 hash_ipportnet6_data_copy(struct hash_ipportnet6_elem
*dst
,
329 const struct hash_ipportnet6_elem
*src
)
331 memcpy(dst
, src
, sizeof(*dst
));
335 hash_ipportnet6_data_zero_out(struct hash_ipportnet6_elem
*elem
)
341 ip6_netmask(union nf_inet_addr
*ip
, u8 prefix
)
343 ip
->ip6
[0] &= ip_set_netmask6(prefix
)[0];
344 ip
->ip6
[1] &= ip_set_netmask6(prefix
)[1];
345 ip
->ip6
[2] &= ip_set_netmask6(prefix
)[2];
346 ip
->ip6
[3] &= ip_set_netmask6(prefix
)[3];
350 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem
*elem
, u8 cidr
)
352 ip6_netmask(&elem
->ip2
, cidr
);
357 hash_ipportnet6_data_list(struct sk_buff
*skb
,
358 const struct hash_ipportnet6_elem
*data
)
360 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP
, &data
->ip
);
361 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP2
, &data
->ip2
);
362 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
363 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
364 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
372 hash_ipportnet6_data_tlist(struct sk_buff
*skb
,
373 const struct hash_ipportnet6_elem
*data
)
375 const struct hash_ipportnet6_telem
*e
=
376 (const struct hash_ipportnet6_telem
*)data
;
378 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP
, &e
->ip
);
379 NLA_PUT_IPADDR6(skb
, IPSET_ATTR_IP2
, &data
->ip2
);
380 NLA_PUT_NET16(skb
, IPSET_ATTR_PORT
, data
->port
);
381 NLA_PUT_U8(skb
, IPSET_ATTR_CIDR2
, data
->cidr
);
382 NLA_PUT_U8(skb
, IPSET_ATTR_PROTO
, data
->proto
);
383 NLA_PUT_NET32(skb
, IPSET_ATTR_TIMEOUT
,
384 htonl(ip_set_timeout_get(e
->timeout
)));
395 #define HOST_MASK 128
396 #include <linux/netfilter/ipset/ip_set_ahash.h>
399 hash_ipportnet6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
400 enum ipset_adt adt
, u8 pf
, u8 dim
, u8 flags
)
402 const struct ip_set_hash
*h
= set
->data
;
403 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
404 struct hash_ipportnet6_elem data
=
405 { .cidr
= h
->nets
[0].cidr
|| HOST_MASK
};
409 if (adt
== IPSET_TEST
)
410 data
.cidr
= HOST_MASK
;
412 if (!ip_set_get_ip6_port(skb
, flags
& IPSET_DIM_TWO_SRC
,
413 &data
.port
, &data
.proto
))
416 ip6addrptr(skb
, flags
& IPSET_DIM_ONE_SRC
, &data
.ip
.in6
);
417 ip6addrptr(skb
, flags
& IPSET_DIM_THREE_SRC
, &data
.ip2
.in6
);
418 ip6_netmask(&data
.ip2
, data
.cidr
);
420 return adtfn(set
, &data
, h
->timeout
);
424 hash_ipportnet6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
425 enum ipset_adt adt
, u32
*lineno
, u32 flags
)
427 const struct ip_set_hash
*h
= set
->data
;
428 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
429 struct hash_ipportnet6_elem data
= { .cidr
= HOST_MASK
};
431 u32 timeout
= h
->timeout
;
434 if (unlikely(!tb
[IPSET_ATTR_IP
] || !tb
[IPSET_ATTR_IP2
] ||
435 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
436 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
437 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
438 tb
[IPSET_ATTR_IP_TO
] ||
439 tb
[IPSET_ATTR_CIDR
]))
440 return -IPSET_ERR_PROTOCOL
;
442 if (tb
[IPSET_ATTR_LINENO
])
443 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
445 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &data
.ip
);
449 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP2
], &data
.ip2
);
453 if (tb
[IPSET_ATTR_CIDR2
])
454 data
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR2
]);
457 return -IPSET_ERR_INVALID_CIDR
;
459 ip6_netmask(&data
.ip2
, data
.cidr
);
461 if (tb
[IPSET_ATTR_PORT
])
462 data
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
464 return -IPSET_ERR_PROTOCOL
;
466 if (tb
[IPSET_ATTR_PROTO
]) {
467 data
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
470 return -IPSET_ERR_INVALID_PROTO
;
472 return -IPSET_ERR_MISSING_PROTO
;
474 switch (data
.proto
) {
484 if (tb
[IPSET_ATTR_TIMEOUT
]) {
485 if (!with_timeout(h
->timeout
))
486 return -IPSET_ERR_TIMEOUT
;
487 timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
490 if (adt
== IPSET_TEST
||
491 !(data
.proto
== IPPROTO_TCP
|| data
.proto
== IPPROTO_UDP
) ||
492 !tb
[IPSET_ATTR_PORT_TO
]) {
493 ret
= adtfn(set
, &data
, timeout
);
494 return ip_set_eexist(ret
, flags
) ? 0 : ret
;
497 port
= ntohs(data
.port
);
498 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
502 for (; port
<= port_to
; port
++) {
503 data
.port
= htons(port
);
504 ret
= adtfn(set
, &data
, timeout
);
506 if (ret
&& !ip_set_eexist(ret
, flags
))
514 /* Create hash:ip type of sets */
517 hash_ipportnet_create(struct ip_set
*set
, struct nlattr
*tb
[], u32 flags
)
519 struct ip_set_hash
*h
;
520 u32 hashsize
= IPSET_DEFAULT_HASHSIZE
, maxelem
= IPSET_DEFAULT_MAXELEM
;
523 if (!(set
->family
== AF_INET
|| set
->family
== AF_INET6
))
524 return -IPSET_ERR_INVALID_FAMILY
;
526 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_HASHSIZE
) ||
527 !ip_set_optattr_netorder(tb
, IPSET_ATTR_MAXELEM
) ||
528 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
)))
529 return -IPSET_ERR_PROTOCOL
;
531 if (tb
[IPSET_ATTR_HASHSIZE
]) {
532 hashsize
= ip_set_get_h32(tb
[IPSET_ATTR_HASHSIZE
]);
533 if (hashsize
< IPSET_MIMINAL_HASHSIZE
)
534 hashsize
= IPSET_MIMINAL_HASHSIZE
;
537 if (tb
[IPSET_ATTR_MAXELEM
])
538 maxelem
= ip_set_get_h32(tb
[IPSET_ATTR_MAXELEM
]);
540 h
= kzalloc(sizeof(*h
)
541 + sizeof(struct ip_set_hash_nets
)
542 * (set
->family
== AF_INET
? 32 : 128), GFP_KERNEL
);
546 h
->maxelem
= maxelem
;
547 get_random_bytes(&h
->initval
, sizeof(h
->initval
));
548 h
->timeout
= IPSET_NO_TIMEOUT
;
550 hbits
= htable_bits(hashsize
);
551 h
->table
= ip_set_alloc(
552 sizeof(struct htable
)
553 + jhash_size(hbits
) * sizeof(struct hbucket
));
558 h
->table
->htable_bits
= hbits
;
562 if (tb
[IPSET_ATTR_TIMEOUT
]) {
563 h
->timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
565 set
->variant
= set
->family
== AF_INET
566 ? &hash_ipportnet4_tvariant
567 : &hash_ipportnet6_tvariant
;
569 if (set
->family
== AF_INET
)
570 hash_ipportnet4_gc_init(set
);
572 hash_ipportnet6_gc_init(set
);
574 set
->variant
= set
->family
== AF_INET
575 ? &hash_ipportnet4_variant
: &hash_ipportnet6_variant
;
578 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
579 set
->name
, jhash_size(h
->table
->htable_bits
),
580 h
->table
->htable_bits
, h
->maxelem
, set
->data
, h
->table
);
585 static struct ip_set_type hash_ipportnet_type __read_mostly
= {
586 .name
= "hash:ip,port,net",
587 .protocol
= IPSET_PROTOCOL
,
588 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_IP2
,
589 .dimension
= IPSET_DIM_THREE
,
592 .create
= hash_ipportnet_create
,
594 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
595 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
596 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
597 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
598 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
601 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
602 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
603 [IPSET_ATTR_IP2
] = { .type
= NLA_NESTED
},
604 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
605 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
606 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
607 [IPSET_ATTR_CIDR2
] = { .type
= NLA_U8
},
608 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
609 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
610 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
616 hash_ipportnet_init(void)
618 return ip_set_type_register(&hash_ipportnet_type
);
622 hash_ipportnet_fini(void)
624 ip_set_type_unregister(&hash_ipportnet_type
);
627 module_init(hash_ipportnet_init
);
628 module_exit(hash_ipportnet_fini
);