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:net,port 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>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_getport.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
26 #define IPSET_TYPE_REV_MIN 0
27 /* 1 SCTP and UDPLITE support added */
28 /* 2 Range as input support for IPv4 added */
29 /* 3 nomatch flag support added */
30 /* 4 Counters support added */
31 #define IPSET_TYPE_REV_MAX 5 /* Comments support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
36 MODULE_ALIAS("ip_set_hash:net,port");
38 /* Type specific function prefix */
39 #define HTYPE hash_netport
40 #define IP_SET_HASH_WITH_PROTO
41 #define IP_SET_HASH_WITH_NETS
43 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
44 * However this way we have to store internally cidr - 1,
45 * dancing back and forth.
47 #define IP_SET_HASH_WITH_NETS_PACKED
52 struct hash_netport4_elem
{
60 /* Common functions */
63 hash_netport4_data_equal(const struct hash_netport4_elem
*ip1
,
64 const struct hash_netport4_elem
*ip2
,
67 return ip1
->ip
== ip2
->ip
&&
68 ip1
->port
== ip2
->port
&&
69 ip1
->proto
== ip2
->proto
&&
70 ip1
->cidr
== ip2
->cidr
;
74 hash_netport4_do_data_match(const struct hash_netport4_elem
*elem
)
76 return elem
->nomatch
? -ENOTEMPTY
: 1;
80 hash_netport4_data_set_flags(struct hash_netport4_elem
*elem
, u32 flags
)
82 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
86 hash_netport4_data_reset_flags(struct hash_netport4_elem
*elem
, u8
*flags
)
88 swap(*flags
, elem
->nomatch
);
92 hash_netport4_data_netmask(struct hash_netport4_elem
*elem
, u8 cidr
)
94 elem
->ip
&= ip_set_netmask(cidr
);
95 elem
->cidr
= cidr
- 1;
99 hash_netport4_data_list(struct sk_buff
*skb
,
100 const struct hash_netport4_elem
*data
)
102 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
104 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
105 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
106 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
+ 1) ||
107 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
109 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
110 goto nla_put_failure
;
118 hash_netport4_data_next(struct hash_netport4_elem
*next
,
119 const struct hash_netport4_elem
*d
)
122 next
->port
= d
->port
;
125 #define MTYPE hash_netport4
128 #include "ip_set_hash_gen.h"
131 hash_netport4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
132 const struct xt_action_param
*par
,
133 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
135 const struct hash_netport
*h
= set
->data
;
136 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
137 struct hash_netport4_elem e
= {
138 .cidr
= IP_SET_INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
) - 1,
140 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
142 if (adt
== IPSET_TEST
)
143 e
.cidr
= HOST_MASK
- 1;
145 if (!ip_set_get_ip4_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
149 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
150 e
.ip
&= ip_set_netmask(e
.cidr
+ 1);
152 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
156 hash_netport4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
157 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
159 const struct hash_netport
*h
= set
->data
;
160 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
161 struct hash_netport4_elem e
= { .cidr
= HOST_MASK
- 1 };
162 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
163 u32 port
, port_to
, p
= 0, ip
= 0, ip_to
= 0, last
;
164 bool with_ports
= false;
168 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
169 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
170 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
171 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
172 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
) ||
173 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
174 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
)))
175 return -IPSET_ERR_PROTOCOL
;
177 if (tb
[IPSET_ATTR_LINENO
])
178 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
180 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
) ||
181 ip_set_get_extensions(set
, tb
, &ext
);
185 if (tb
[IPSET_ATTR_CIDR
]) {
186 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
187 if (!cidr
|| cidr
> HOST_MASK
)
188 return -IPSET_ERR_INVALID_CIDR
;
192 if (tb
[IPSET_ATTR_PORT
])
193 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
195 return -IPSET_ERR_PROTOCOL
;
197 if (tb
[IPSET_ATTR_PROTO
]) {
198 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
199 with_ports
= ip_set_proto_with_ports(e
.proto
);
202 return -IPSET_ERR_INVALID_PROTO
;
204 return -IPSET_ERR_MISSING_PROTO
;
206 if (!(with_ports
|| e
.proto
== IPPROTO_ICMP
))
209 with_ports
= with_ports
&& tb
[IPSET_ATTR_PORT_TO
];
211 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
212 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
213 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
214 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
217 if (adt
== IPSET_TEST
|| !(with_ports
|| tb
[IPSET_ATTR_IP_TO
])) {
218 e
.ip
= htonl(ip
& ip_set_hostmask(e
.cidr
+ 1));
219 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
220 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
221 ip_set_eexist(ret
, flags
) ? 0 : ret
;
224 port
= port_to
= ntohs(e
.port
);
225 if (tb
[IPSET_ATTR_PORT_TO
]) {
226 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
230 if (tb
[IPSET_ATTR_IP_TO
]) {
231 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
236 if (ip
+ UINT_MAX
== ip_to
)
237 return -IPSET_ERR_HASH_RANGE
;
239 ip_set_mask_from_to(ip
, ip_to
, e
.cidr
+ 1);
242 ip
= ntohl(h
->next
.ip
);
243 while (!after(ip
, ip_to
)) {
245 last
= ip_set_range_to_cidr(ip
, ip_to
, &cidr
);
247 p
= retried
&& ip
== ntohl(h
->next
.ip
) ? ntohs(h
->next
.port
)
249 for (; p
<= port_to
; p
++) {
251 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
253 if (ret
&& !ip_set_eexist(ret
, flags
))
265 struct hash_netport6_elem
{
266 union nf_inet_addr ip
;
273 /* Common functions */
276 hash_netport6_data_equal(const struct hash_netport6_elem
*ip1
,
277 const struct hash_netport6_elem
*ip2
,
280 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
281 ip1
->port
== ip2
->port
&&
282 ip1
->proto
== ip2
->proto
&&
283 ip1
->cidr
== ip2
->cidr
;
287 hash_netport6_do_data_match(const struct hash_netport6_elem
*elem
)
289 return elem
->nomatch
? -ENOTEMPTY
: 1;
293 hash_netport6_data_set_flags(struct hash_netport6_elem
*elem
, u32 flags
)
295 elem
->nomatch
= !!((flags
>> 16) & IPSET_FLAG_NOMATCH
);
299 hash_netport6_data_reset_flags(struct hash_netport6_elem
*elem
, u8
*flags
)
301 swap(*flags
, elem
->nomatch
);
305 hash_netport6_data_netmask(struct hash_netport6_elem
*elem
, u8 cidr
)
307 ip6_netmask(&elem
->ip
, cidr
);
308 elem
->cidr
= cidr
- 1;
312 hash_netport6_data_list(struct sk_buff
*skb
,
313 const struct hash_netport6_elem
*data
)
315 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
317 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
318 nla_put_net16(skb
, IPSET_ATTR_PORT
, data
->port
) ||
319 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
+ 1) ||
320 nla_put_u8(skb
, IPSET_ATTR_PROTO
, data
->proto
) ||
322 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
323 goto nla_put_failure
;
331 hash_netport6_data_next(struct hash_netport4_elem
*next
,
332 const struct hash_netport6_elem
*d
)
334 next
->port
= d
->port
;
341 #define MTYPE hash_netport6
343 #define HOST_MASK 128
344 #define IP_SET_EMIT_CREATE
345 #include "ip_set_hash_gen.h"
348 hash_netport6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
349 const struct xt_action_param
*par
,
350 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
352 const struct hash_netport
*h
= set
->data
;
353 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
354 struct hash_netport6_elem e
= {
355 .cidr
= IP_SET_INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
) - 1,
357 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
359 if (adt
== IPSET_TEST
)
360 e
.cidr
= HOST_MASK
- 1;
362 if (!ip_set_get_ip6_port(skb
, opt
->flags
& IPSET_DIM_TWO_SRC
,
366 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
367 ip6_netmask(&e
.ip
, e
.cidr
+ 1);
369 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
373 hash_netport6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
374 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
376 const struct hash_netport
*h
= set
->data
;
377 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
378 struct hash_netport6_elem e
= { .cidr
= HOST_MASK
- 1 };
379 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
381 bool with_ports
= false;
385 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
386 !ip_set_attr_netorder(tb
, IPSET_ATTR_PORT
) ||
387 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PORT_TO
) ||
388 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
389 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
) ||
390 !ip_set_optattr_netorder(tb
, IPSET_ATTR_PACKETS
) ||
391 !ip_set_optattr_netorder(tb
, IPSET_ATTR_BYTES
)))
392 return -IPSET_ERR_PROTOCOL
;
393 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
394 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
396 if (tb
[IPSET_ATTR_LINENO
])
397 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
399 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
) ||
400 ip_set_get_extensions(set
, tb
, &ext
);
404 if (tb
[IPSET_ATTR_CIDR
]) {
405 cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
406 if (!cidr
|| cidr
> HOST_MASK
)
407 return -IPSET_ERR_INVALID_CIDR
;
410 ip6_netmask(&e
.ip
, e
.cidr
+ 1);
412 if (tb
[IPSET_ATTR_PORT
])
413 e
.port
= nla_get_be16(tb
[IPSET_ATTR_PORT
]);
415 return -IPSET_ERR_PROTOCOL
;
417 if (tb
[IPSET_ATTR_PROTO
]) {
418 e
.proto
= nla_get_u8(tb
[IPSET_ATTR_PROTO
]);
419 with_ports
= ip_set_proto_with_ports(e
.proto
);
422 return -IPSET_ERR_INVALID_PROTO
;
424 return -IPSET_ERR_MISSING_PROTO
;
426 if (!(with_ports
|| e
.proto
== IPPROTO_ICMPV6
))
429 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
430 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
431 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
432 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
435 if (adt
== IPSET_TEST
|| !with_ports
|| !tb
[IPSET_ATTR_PORT_TO
]) {
436 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
437 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
438 ip_set_eexist(ret
, flags
) ? 0 : ret
;
441 port
= ntohs(e
.port
);
442 port_to
= ip_set_get_h16(tb
[IPSET_ATTR_PORT_TO
]);
447 port
= ntohs(h
->next
.port
);
448 for (; port
<= port_to
; port
++) {
449 e
.port
= htons(port
);
450 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
452 if (ret
&& !ip_set_eexist(ret
, flags
))
460 static struct ip_set_type hash_netport_type __read_mostly
= {
461 .name
= "hash:net,port",
462 .protocol
= IPSET_PROTOCOL
,
463 .features
= IPSET_TYPE_IP
| IPSET_TYPE_PORT
| IPSET_TYPE_NOMATCH
,
464 .dimension
= IPSET_DIM_TWO
,
465 .family
= NFPROTO_UNSPEC
,
466 .revision_min
= IPSET_TYPE_REV_MIN
,
467 .revision_max
= IPSET_TYPE_REV_MAX
,
468 .create
= hash_netport_create
,
470 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
471 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
472 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
473 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
474 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
475 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
476 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
479 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
480 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
481 [IPSET_ATTR_PORT
] = { .type
= NLA_U16
},
482 [IPSET_ATTR_PORT_TO
] = { .type
= NLA_U16
},
483 [IPSET_ATTR_PROTO
] = { .type
= NLA_U8
},
484 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
485 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
486 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
487 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
488 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
489 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
490 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
},
496 hash_netport_init(void)
498 return ip_set_type_register(&hash_netport_type
);
502 hash_netport_fini(void)
504 ip_set_type_unregister(&hash_netport_type
);
507 module_init(hash_netport_init
);
508 module_exit(hash_netport_fini
);