1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 /* Kernel module implementing an IP set type: the hash:net type */
7 #include <linux/jhash.h>
8 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/errno.h>
12 #include <linux/random.h>
15 #include <net/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 Range as input support for IPv4 added */
24 /* 2 nomatch flag support added */
25 /* 3 Counters support added */
26 /* 4 Comments support added */
27 /* 5 Forceadd support added */
28 #define IPSET_TYPE_REV_MAX 6 /* skbinfo mapping support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN
, IPSET_TYPE_REV_MAX
);
33 MODULE_ALIAS("ip_set_hash:net");
35 /* Type specific function prefix */
36 #define HTYPE hash_net
37 #define IP_SET_HASH_WITH_NETS
42 struct hash_net4_elem
{
49 /* Common functions */
52 hash_net4_data_equal(const struct hash_net4_elem
*ip1
,
53 const struct hash_net4_elem
*ip2
,
56 return ip1
->ip
== ip2
->ip
&&
57 ip1
->cidr
== ip2
->cidr
;
61 hash_net4_do_data_match(const struct hash_net4_elem
*elem
)
63 return elem
->nomatch
? -ENOTEMPTY
: 1;
67 hash_net4_data_set_flags(struct hash_net4_elem
*elem
, u32 flags
)
69 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
73 hash_net4_data_reset_flags(struct hash_net4_elem
*elem
, u8
*flags
)
75 swap(*flags
, elem
->nomatch
);
79 hash_net4_data_netmask(struct hash_net4_elem
*elem
, u8 cidr
)
81 elem
->ip
&= ip_set_netmask(cidr
);
86 hash_net4_data_list(struct sk_buff
*skb
, const struct hash_net4_elem
*data
)
88 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
90 if (nla_put_ipaddr4(skb
, IPSET_ATTR_IP
, data
->ip
) ||
91 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
93 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
102 hash_net4_data_next(struct hash_net4_elem
*next
,
103 const struct hash_net4_elem
*d
)
108 #define MTYPE hash_net4
110 #include "ip_set_hash_gen.h"
113 hash_net4_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
114 const struct xt_action_param
*par
,
115 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
117 const struct hash_net4
*h
= set
->data
;
118 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
119 struct hash_net4_elem e
= {
120 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
122 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
126 if (adt
== IPSET_TEST
)
129 ip4addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
);
130 e
.ip
&= ip_set_netmask(e
.cidr
);
132 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
136 hash_net4_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
137 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
139 const struct hash_net4
*h
= set
->data
;
140 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
141 struct hash_net4_elem e
= { .cidr
= HOST_MASK
};
142 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
143 u32 ip
= 0, ip_to
= 0;
146 if (tb
[IPSET_ATTR_LINENO
])
147 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
149 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
150 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
151 return -IPSET_ERR_PROTOCOL
;
153 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP
], &ip
);
157 ret
= ip_set_get_extensions(set
, tb
, &ext
);
161 if (tb
[IPSET_ATTR_CIDR
]) {
162 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
163 if (!e
.cidr
|| e
.cidr
> HOST_MASK
)
164 return -IPSET_ERR_INVALID_CIDR
;
167 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
168 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
170 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
171 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
174 if (adt
== IPSET_TEST
|| !tb
[IPSET_ATTR_IP_TO
]) {
175 e
.ip
= htonl(ip
& ip_set_hostmask(e
.cidr
));
176 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
177 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
178 ip_set_eexist(ret
, flags
) ? 0 : ret
;
182 if (tb
[IPSET_ATTR_IP_TO
]) {
183 ret
= ip_set_get_hostipaddr4(tb
[IPSET_ATTR_IP_TO
], &ip_to
);
188 if (ip
+ UINT_MAX
== ip_to
)
189 return -IPSET_ERR_HASH_RANGE
;
192 ip
= ntohl(h
->next
.ip
);
195 ip
= ip_set_range_to_cidr(ip
, ip_to
, &e
.cidr
);
196 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
197 if (ret
&& !ip_set_eexist(ret
, flags
))
201 } while (ip
++ < ip_to
);
207 struct hash_net6_elem
{
208 union nf_inet_addr ip
;
214 /* Common functions */
217 hash_net6_data_equal(const struct hash_net6_elem
*ip1
,
218 const struct hash_net6_elem
*ip2
,
221 return ipv6_addr_equal(&ip1
->ip
.in6
, &ip2
->ip
.in6
) &&
222 ip1
->cidr
== ip2
->cidr
;
226 hash_net6_do_data_match(const struct hash_net6_elem
*elem
)
228 return elem
->nomatch
? -ENOTEMPTY
: 1;
232 hash_net6_data_set_flags(struct hash_net6_elem
*elem
, u32 flags
)
234 elem
->nomatch
= (flags
>> 16) & IPSET_FLAG_NOMATCH
;
238 hash_net6_data_reset_flags(struct hash_net6_elem
*elem
, u8
*flags
)
240 swap(*flags
, elem
->nomatch
);
244 hash_net6_data_netmask(struct hash_net6_elem
*elem
, u8 cidr
)
246 ip6_netmask(&elem
->ip
, cidr
);
251 hash_net6_data_list(struct sk_buff
*skb
, const struct hash_net6_elem
*data
)
253 u32 flags
= data
->nomatch
? IPSET_FLAG_NOMATCH
: 0;
255 if (nla_put_ipaddr6(skb
, IPSET_ATTR_IP
, &data
->ip
.in6
) ||
256 nla_put_u8(skb
, IPSET_ATTR_CIDR
, data
->cidr
) ||
258 nla_put_net32(skb
, IPSET_ATTR_CADT_FLAGS
, htonl(flags
))))
259 goto nla_put_failure
;
267 hash_net6_data_next(struct hash_net6_elem
*next
,
268 const struct hash_net6_elem
*d
)
275 #define MTYPE hash_net6
276 #define HOST_MASK 128
277 #define IP_SET_EMIT_CREATE
278 #include "ip_set_hash_gen.h"
281 hash_net6_kadt(struct ip_set
*set
, const struct sk_buff
*skb
,
282 const struct xt_action_param
*par
,
283 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
)
285 const struct hash_net6
*h
= set
->data
;
286 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
287 struct hash_net6_elem e
= {
288 .cidr
= INIT_CIDR(h
->nets
[0].cidr
[0], HOST_MASK
),
290 struct ip_set_ext ext
= IP_SET_INIT_KEXT(skb
, opt
, set
);
294 if (adt
== IPSET_TEST
)
297 ip6addrptr(skb
, opt
->flags
& IPSET_DIM_ONE_SRC
, &e
.ip
.in6
);
298 ip6_netmask(&e
.ip
, e
.cidr
);
300 return adtfn(set
, &e
, &ext
, &opt
->ext
, opt
->cmdflags
);
304 hash_net6_uadt(struct ip_set
*set
, struct nlattr
*tb
[],
305 enum ipset_adt adt
, u32
*lineno
, u32 flags
, bool retried
)
307 ipset_adtfn adtfn
= set
->variant
->adt
[adt
];
308 struct hash_net6_elem e
= { .cidr
= HOST_MASK
};
309 struct ip_set_ext ext
= IP_SET_INIT_UEXT(set
);
312 if (tb
[IPSET_ATTR_LINENO
])
313 *lineno
= nla_get_u32(tb
[IPSET_ATTR_LINENO
]);
315 if (unlikely(!tb
[IPSET_ATTR_IP
] ||
316 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
317 return -IPSET_ERR_PROTOCOL
;
318 if (unlikely(tb
[IPSET_ATTR_IP_TO
]))
319 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED
;
321 ret
= ip_set_get_ipaddr6(tb
[IPSET_ATTR_IP
], &e
.ip
);
325 ret
= ip_set_get_extensions(set
, tb
, &ext
);
329 if (tb
[IPSET_ATTR_CIDR
]) {
330 e
.cidr
= nla_get_u8(tb
[IPSET_ATTR_CIDR
]);
331 if (!e
.cidr
|| e
.cidr
> HOST_MASK
)
332 return -IPSET_ERR_INVALID_CIDR
;
335 ip6_netmask(&e
.ip
, e
.cidr
);
337 if (tb
[IPSET_ATTR_CADT_FLAGS
]) {
338 u32 cadt_flags
= ip_set_get_h32(tb
[IPSET_ATTR_CADT_FLAGS
]);
340 if (cadt_flags
& IPSET_FLAG_NOMATCH
)
341 flags
|= (IPSET_FLAG_NOMATCH
<< 16);
344 ret
= adtfn(set
, &e
, &ext
, &ext
, flags
);
346 return ip_set_enomatch(ret
, flags
, adt
, set
) ? -ret
:
347 ip_set_eexist(ret
, flags
) ? 0 : ret
;
350 static struct ip_set_type hash_net_type __read_mostly
= {
352 .protocol
= IPSET_PROTOCOL
,
353 .features
= IPSET_TYPE_IP
| IPSET_TYPE_NOMATCH
,
354 .dimension
= IPSET_DIM_ONE
,
355 .family
= NFPROTO_UNSPEC
,
356 .revision_min
= IPSET_TYPE_REV_MIN
,
357 .revision_max
= IPSET_TYPE_REV_MAX
,
358 .create
= hash_net_create
,
360 [IPSET_ATTR_HASHSIZE
] = { .type
= NLA_U32
},
361 [IPSET_ATTR_MAXELEM
] = { .type
= NLA_U32
},
362 [IPSET_ATTR_PROBES
] = { .type
= NLA_U8
},
363 [IPSET_ATTR_RESIZE
] = { .type
= NLA_U8
},
364 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
365 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
368 [IPSET_ATTR_IP
] = { .type
= NLA_NESTED
},
369 [IPSET_ATTR_IP_TO
] = { .type
= NLA_NESTED
},
370 [IPSET_ATTR_CIDR
] = { .type
= NLA_U8
},
371 [IPSET_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
372 [IPSET_ATTR_CADT_FLAGS
] = { .type
= NLA_U32
},
373 [IPSET_ATTR_BYTES
] = { .type
= NLA_U64
},
374 [IPSET_ATTR_PACKETS
] = { .type
= NLA_U64
},
375 [IPSET_ATTR_COMMENT
] = { .type
= NLA_NUL_STRING
,
376 .len
= IPSET_MAX_COMMENT_SIZE
},
377 [IPSET_ATTR_SKBMARK
] = { .type
= NLA_U64
},
378 [IPSET_ATTR_SKBPRIO
] = { .type
= NLA_U32
},
379 [IPSET_ATTR_SKBQUEUE
] = { .type
= NLA_U16
},
387 return ip_set_type_register(&hash_net_type
);
394 ip_set_type_unregister(&hash_net_type
);
397 module_init(hash_net_init
);
398 module_exit(hash_net_fini
);