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 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_hash.h>
25 #define IPSET_TYPE_REV_MIN 0
26 /* 1 Range as input support for IPv4 added */
27 /* 2 nomatch flag support added */
28 /* 3 Counters support added */
29 /* 4 Comments support added */
30 /* 5 Forceadd support added */
31 #define IPSET_TYPE_REV_MAX 6 /* skbinfo mapping support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
36 MODULE_ALIAS("ip_set_hash:net");
38 /* Type specific function prefix */
39 #define HTYPE hash_net
40 #define IP_SET_HASH_WITH_NETS
45 struct hash_net4_elem
{
52 /* Common functions */
55 hash_net4_data_equal(const struct hash_net4_elem
*ip1
,
56 const struct hash_net4_elem
*ip2
,
59 return ip1
->ip
== ip2
->ip
&&
60 ip1
->cidr
== ip2
->cidr
;
64 hash_net4_do_data_match(const struct hash_net4_elem
*elem
)
66 return elem
->nomatch
? -ENOTEMPTY
: 1;
70 hash_net4_data_set_flags(struct hash_net4_elem
*elem
, u32 flags
)
72 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
76 hash_net4_data_reset_flags(struct hash_net4_elem
*elem
, u8
*flags
)
78 swap(*flags
, elem
->nomatch
);
82 hash_net4_data_netmask(struct hash_net4_elem
*elem
, u8 cidr
)
84 elem
->ip
&= ip_set_netmask(cidr
);
89 hash_net4_data_list(struct sk_buff
*skb
, const struct hash_net4_elem
*data
)
91 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
93 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
94 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
96 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
105 hash_net4_data_next(struct hash_net4_elem
*next
,
106 const struct hash_net4_elem
*d
)
111 #define MTYPE hash_net4
113 #include "ip_set_hash_gen.h"
116 hash_net4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
117 const struct xt_action_param
*par
,
118 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
120 const struct hash_net
*h
= set
->data
;
121 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
122 struct hash_net4_elem e
= {
123 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
125 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
129 if (adt
== IPSET_TEST
)
132 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
133 e
.ip
&= ip_set_netmask(e
.cidr
);
135 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
139 hash_net4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
140 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
142 const struct hash_net
*h
= set
->data
;
143 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
144 struct hash_net4_elem e
= { .cidr
= HOST_MASK
};
145 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
146 u32 ip
= 0, ip_to
= 0, last
;
149 if (tb
[IPSET_ATTR_LINENO
])
150 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
152 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
153 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
154 return -IPSET_ERR_PROTOCOL
;
156 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
160 ret
= ip_set_get_extensions(set
, tb
, &ext
);
164 if (tb
[IPSET_ATTR_CIDR
]) {
165 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
166 if (!e
.cidr
|| e
.cidr
> HOST_MASK
)
167 return -IPSET_ERR_INVALID_CIDR
;
170 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
171 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
173 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
174 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
177 if (adt
== IPSET_TEST
|| !tb
[IPSET_ATTR_IP_TO
]) {
178 e
.ip
= htonl(ip
& ip_set_hostmask(e
.cidr
));
179 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
180 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
181 ip_set_eexist(ret
, flags
) ? 0 : ret
;
185 if (tb
[IPSET_ATTR_IP_TO
]) {
186 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
191 if (ip
+ UINT_MAX
== ip_to
)
192 return -IPSET_ERR_HASH_RANGE
;
195 ip
= ntohl(h
->next
.ip
);
196 while (!after(ip
, ip_to
)) {
198 last
= ip_set_range_to_cidr(ip
, ip_to
, &e
.cidr
);
199 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
200 if (ret
&& !ip_set_eexist(ret
, flags
))
211 struct hash_net6_elem
{
212 union nf_inet_addr ip
;
218 /* Common functions */
221 hash_net6_data_equal(const struct hash_net6_elem
*ip1
,
222 const struct hash_net6_elem
*ip2
,
225 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
226 ip1
->cidr
== ip2
->cidr
;
230 hash_net6_do_data_match(const struct hash_net6_elem
*elem
)
232 return elem
->nomatch
? -ENOTEMPTY
: 1;
236 hash_net6_data_set_flags(struct hash_net6_elem
*elem
, u32 flags
)
238 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
242 hash_net6_data_reset_flags(struct hash_net6_elem
*elem
, u8
*flags
)
244 swap(*flags
, elem
->nomatch
);
248 hash_net6_data_netmask(struct hash_net6_elem
*elem
, u8 cidr
)
250 ip6_netmask(&elem
->ip
, cidr
);
255 hash_net6_data_list(struct sk_buff
*skb
, const struct hash_net6_elem
*data
)
257 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
259 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
260 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
262 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
263 goto nla_put_failure
;
271 hash_net6_data_next(struct hash_net4_elem
*next
,
272 const struct hash_net6_elem
*d
)
279 #define MTYPE hash_net6
280 #define HOST_MASK 128
281 #define IP_SET_EMIT_CREATE
282 #include "ip_set_hash_gen.h"
285 hash_net6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
286 const struct xt_action_param
*par
,
287 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
289 const struct hash_net
*h
= set
->data
;
290 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
291 struct hash_net6_elem e
= {
292 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
294 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
298 if (adt
== IPSET_TEST
)
301 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
302 ip6_netmask(&e
.ip
, e
.cidr
);
304 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
308 hash_net6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
309 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
311 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
312 struct hash_net6_elem e
= { .cidr
= HOST_MASK
};
313 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
316 if (tb
[IPSET_ATTR_LINENO
])
317 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
319 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
320 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
321 return -IPSET_ERR_PROTOCOL
;
322 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
323 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
325 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
329 ret
= ip_set_get_extensions(set
, tb
, &ext
);
333 if (tb
[IPSET_ATTR_CIDR
]) {
334 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
335 if (!e
.cidr
|| e
.cidr
> HOST_MASK
)
336 return -IPSET_ERR_INVALID_CIDR
;
339 ip6_netmask(&e
.ip
, e
.cidr
);
341 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
342 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
344 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
345 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
348 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
350 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
351 ip_set_eexist(ret
, flags
) ? 0 : ret
;
354 static struct ip_set_type hash_net_type __read_mostly
= {
356 .protocol
= IPSET_PROTOCOL
,
357 .features
= IPSET_TYPE_IP
| IPSET_TYPE_NOMATCH
,
358 .dimension
= IPSET_DIM_ONE
,
359 .family
= NFPROTO_UNSPEC
,
360 .revision_min
= IPSET_TYPE_REV_MIN
,
361 .revision_max
= IPSET_TYPE_REV_MAX
,
362 .create
= hash_net_create
,
364 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
365 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
366 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
367 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
368 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
369 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
372 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
373 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
374 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
375 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
376 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
377 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
378 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
379 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
380 .len
= IPSET_MAX_COMMENT_SIZE
},
381 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
382 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
383 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
391 return ip_set_type_register(&hash_net_type
);
398 ip_set_type_unregister(&hash_net_type
);
401 module_init(hash_net_init
);
402 module_exit(hash_net_fini
);