Skip to content

Commit ac8cc92

Browse files
Jozsef Kadlecsikkaber
authored andcommitted
netfilter: ipset: options and flags support added to the kernel API
The support makes possible to specify the timeout value for the SET target and a flag to reset the timeout for already existing entries. Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 483e9ea commit ac8cc92

16 files changed

Lines changed: 206 additions & 134 deletions

‎include/linux/netfilter/ipset/ip_set.h‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,23 @@ struct ip_set;
217217
typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
218218
u32 timeout, u32 flags);
219219

220+
/* Kernel API function options */
221+
struct ip_set_adt_opt {
222+
u8 family; /* Actual protocol family */
223+
u8 dim; /* Dimension of match/target */
224+
u8 flags; /* Direction and negation flags */
225+
u32 cmdflags; /* Command-like flags */
226+
u32 timeout; /* Timeout value */
227+
};
228+
220229
/* Set type, variant-specific part */
221230
struct ip_set_type_variant {
222231
/* Kernelspace: test/add/del entries
223232
* returns negative error code,
224233
* zero for no match/success to add/delete
225234
* positive for matching element */
226235
int (*kadt)(struct ip_set *set, const struct sk_buff * skb,
227-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags);
236+
enum ipset_adt adt, const struct ip_set_adt_opt *opt);
228237

229238
/* Userspace: test/add/del entries
230239
* returns negative error code,
@@ -314,12 +323,13 @@ extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
314323
extern void ip_set_nfnl_put(ip_set_id_t index);
315324

316325
/* API for iptables set match, and SET target */
326+
317327
extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
318-
u8 family, u8 dim, u8 flags);
328+
const struct ip_set_adt_opt *opt);
319329
extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
320-
u8 family, u8 dim, u8 flags);
330+
const struct ip_set_adt_opt *opt);
321331
extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
322-
u8 family, u8 dim, u8 flags);
332+
const struct ip_set_adt_opt *opt);
323333

324334
/* Utility functions */
325335
extern void * ip_set_alloc(size_t size);

‎include/linux/netfilter/ipset/ip_set_ahash.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ type_pf_list(const struct ip_set *set,
586586

587587
static int
588588
type_pf_kadt(struct ip_set *set, const struct sk_buff * skb,
589-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags);
589+
enum ipset_adt adt, const struct ip_set_adt_opt *opt);
590590
static int
591591
type_pf_uadt(struct ip_set *set, struct nlattr *tb[],
592592
enum ipset_adt adt, u32 *lineno, u32 flags);

‎include/linux/netfilter/ipset/ip_set_timeout.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
#define with_timeout(timeout) ((timeout) != IPSET_NO_TIMEOUT)
2424

25+
#define opt_timeout(opt, map) \
26+
(with_timeout((opt)->timeout) ? (opt)->timeout : (map)->timeout)
27+
2528
static inline unsigned int
2629
ip_set_timeout_uget(struct nlattr *tb)
2730
{

‎include/linux/netfilter/xt_set.h‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct xt_set_info_target_v0 {
3535
struct xt_set_info_v0 del_set;
3636
};
3737

38-
/* Revision 1: current interface to netfilter/iptables */
38+
/* Revision 1 match and target */
3939

4040
struct xt_set_info {
4141
ip_set_id_t index;
@@ -44,13 +44,22 @@ struct xt_set_info {
4444
};
4545

4646
/* match and target infos */
47-
struct xt_set_info_match {
47+
struct xt_set_info_match_v1 {
4848
struct xt_set_info match_set;
4949
};
5050

51-
struct xt_set_info_target {
51+
struct xt_set_info_target_v1 {
5252
struct xt_set_info add_set;
5353
struct xt_set_info del_set;
5454
};
5555

56+
/* Revision 2 target */
57+
58+
struct xt_set_info_target_v2 {
59+
struct xt_set_info add_set;
60+
struct xt_set_info del_set;
61+
u32 flags;
62+
u32 timeout;
63+
};
64+
5665
#endif /*_XT_SET_H*/

‎net/netfilter/ipset/ip_set_bitmap_ip.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,19 @@ bitmap_ip_tlist(const struct ip_set *set,
219219

220220
static int
221221
bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb,
222-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
222+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
223223
{
224224
struct bitmap_ip *map = set->data;
225225
ipset_adtfn adtfn = set->variant->adt[adt];
226226
u32 ip;
227227

228-
ip = ntohl(ip4addr(skb, flags & IPSET_DIM_ONE_SRC));
228+
ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
229229
if (ip < map->first_ip || ip > map->last_ip)
230230
return -IPSET_ERR_BITMAP_RANGE;
231231

232232
ip = ip_to_id(map, ip);
233233

234-
return adtfn(set, &ip, map->timeout, flags);
234+
return adtfn(set, &ip, opt_timeout(opt, map), opt->cmdflags);
235235
}
236236

237237
static int

‎net/netfilter/ipset/ip_set_bitmap_ipmac.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,17 @@ bitmap_ipmac_tlist(const struct ip_set *set,
338338

339339
static int
340340
bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
341-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
341+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
342342
{
343343
struct bitmap_ipmac *map = set->data;
344344
ipset_adtfn adtfn = set->variant->adt[adt];
345345
struct ipmac data;
346346

347347
/* MAC can be src only */
348-
if (!(flags & IPSET_DIM_TWO_SRC))
348+
if (!(opt->flags & IPSET_DIM_TWO_SRC))
349349
return 0;
350350

351-
data.id = ntohl(ip4addr(skb, flags & IPSET_DIM_ONE_SRC));
351+
data.id = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
352352
if (data.id < map->first_ip || data.id > map->last_ip)
353353
return -IPSET_ERR_BITMAP_RANGE;
354354

@@ -360,7 +360,7 @@ bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
360360
data.id -= map->first_ip;
361361
data.ether = eth_hdr(skb)->h_source;
362362

363-
return adtfn(set, &data, map->timeout, flags);
363+
return adtfn(set, &data, opt_timeout(opt, map), opt->cmdflags);
364364
}
365365

366366
static int

‎net/netfilter/ipset/ip_set_bitmap_port.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@ bitmap_port_tlist(const struct ip_set *set,
208208

209209
static int
210210
bitmap_port_kadt(struct ip_set *set, const struct sk_buff *skb,
211-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
211+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
212212
{
213213
struct bitmap_port *map = set->data;
214214
ipset_adtfn adtfn = set->variant->adt[adt];
215215
__be16 __port;
216216
u16 port = 0;
217217

218-
if (!ip_set_get_ip_port(skb, pf, flags & IPSET_DIM_ONE_SRC, &__port))
218+
if (!ip_set_get_ip_port(skb, opt->family,
219+
opt->flags & IPSET_DIM_ONE_SRC, &__port))
219220
return -EINVAL;
220221

221222
port = ntohs(__port);
@@ -225,7 +226,7 @@ bitmap_port_kadt(struct ip_set *set, const struct sk_buff *skb,
225226

226227
port -= map->first_port;
227228

228-
return adtfn(set, &port, map->timeout, flags);
229+
return adtfn(set, &port, opt_timeout(opt, map), opt->cmdflags);
229230
}
230231

231232
static int

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,27 +325,27 @@ __ip_set_put(ip_set_id_t index)
325325

326326
int
327327
ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
328-
u8 family, u8 dim, u8 flags)
328+
const struct ip_set_adt_opt *opt)
329329
{
330330
struct ip_set *set = ip_set_list[index];
331331
int ret = 0;
332332

333333
BUG_ON(set == NULL);
334334
pr_debug("set %s, index %u\n", set->name, index);
335335

336-
if (dim < set->type->dimension ||
337-
!(family == set->family || set->family == AF_UNSPEC))
336+
if (opt->dim < set->type->dimension ||
337+
!(opt->family == set->family || set->family == AF_UNSPEC))
338338
return 0;
339339

340340
read_lock_bh(&set->lock);
341-
ret = set->variant->kadt(set, skb, IPSET_TEST, family, dim, flags);
341+
ret = set->variant->kadt(set, skb, IPSET_TEST, opt);
342342
read_unlock_bh(&set->lock);
343343

344344
if (ret == -EAGAIN) {
345345
/* Type requests element to be completed */
346346
pr_debug("element must be competed, ADD is triggered\n");
347347
write_lock_bh(&set->lock);
348-
set->variant->kadt(set, skb, IPSET_ADD, family, dim, flags);
348+
set->variant->kadt(set, skb, IPSET_ADD, opt);
349349
write_unlock_bh(&set->lock);
350350
ret = 1;
351351
}
@@ -357,20 +357,20 @@ EXPORT_SYMBOL_GPL(ip_set_test);
357357

358358
int
359359
ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
360-
u8 family, u8 dim, u8 flags)
360+
const struct ip_set_adt_opt *opt)
361361
{
362362
struct ip_set *set = ip_set_list[index];
363363
int ret;
364364

365365
BUG_ON(set == NULL);
366366
pr_debug("set %s, index %u\n", set->name, index);
367367

368-
if (dim < set->type->dimension ||
369-
!(family == set->family || set->family == AF_UNSPEC))
368+
if (opt->dim < set->type->dimension ||
369+
!(opt->family == set->family || set->family == AF_UNSPEC))
370370
return 0;
371371

372372
write_lock_bh(&set->lock);
373-
ret = set->variant->kadt(set, skb, IPSET_ADD, family, dim, flags);
373+
ret = set->variant->kadt(set, skb, IPSET_ADD, opt);
374374
write_unlock_bh(&set->lock);
375375

376376
return ret;
@@ -379,20 +379,20 @@ EXPORT_SYMBOL_GPL(ip_set_add);
379379

380380
int
381381
ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
382-
u8 family, u8 dim, u8 flags)
382+
const struct ip_set_adt_opt *opt)
383383
{
384384
struct ip_set *set = ip_set_list[index];
385385
int ret = 0;
386386

387387
BUG_ON(set == NULL);
388388
pr_debug("set %s, index %u\n", set->name, index);
389389

390-
if (dim < set->type->dimension ||
391-
!(family == set->family || set->family == AF_UNSPEC))
390+
if (opt->dim < set->type->dimension ||
391+
!(opt->family == set->family || set->family == AF_UNSPEC))
392392
return 0;
393393

394394
write_lock_bh(&set->lock);
395-
ret = set->variant->kadt(set, skb, IPSET_DEL, family, dim, flags);
395+
ret = set->variant->kadt(set, skb, IPSET_DEL, opt);
396396
write_unlock_bh(&set->lock);
397397

398398
return ret;

‎net/netfilter/ipset/ip_set_hash_ip.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
110110

111111
static int
112112
hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
113-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
113+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
114114
{
115115
const struct ip_set_hash *h = set->data;
116116
ipset_adtfn adtfn = set->variant->adt[adt];
117117
__be32 ip;
118118

119-
ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &ip);
119+
ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
120120
ip &= ip_set_netmask(h->netmask);
121121
if (ip == 0)
122122
return -EINVAL;
123123

124-
return adtfn(set, &ip, h->timeout, flags);
124+
return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
125125
}
126126

127127
static int
@@ -283,18 +283,18 @@ hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
283283

284284
static int
285285
hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
286-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
286+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
287287
{
288288
const struct ip_set_hash *h = set->data;
289289
ipset_adtfn adtfn = set->variant->adt[adt];
290290
union nf_inet_addr ip;
291291

292-
ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &ip.in6);
292+
ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip.in6);
293293
ip6_netmask(&ip, h->netmask);
294294
if (ipv6_addr_any(&ip.in6))
295295
return -EINVAL;
296296

297-
return adtfn(set, &ip, h->timeout, flags);
297+
return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
298298
}
299299

300300
static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX + 1] = {

‎net/netfilter/ipset/ip_set_hash_ipport.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ hash_ipport4_data_tlist(struct sk_buff *skb,
126126

127127
static int
128128
hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
129-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
129+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
130130
{
131131
const struct ip_set_hash *h = set->data;
132132
ipset_adtfn adtfn = set->variant->adt[adt];
133133
struct hash_ipport4_elem data = { };
134134

135-
if (!ip_set_get_ip4_port(skb, flags & IPSET_DIM_TWO_SRC,
135+
if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
136136
&data.port, &data.proto))
137137
return -EINVAL;
138138

139-
ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
139+
ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
140140

141-
return adtfn(set, &data, h->timeout, flags);
141+
return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
142142
}
143143

144144
static int
@@ -330,19 +330,19 @@ hash_ipport6_data_tlist(struct sk_buff *skb,
330330

331331
static int
332332
hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
333-
enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
333+
enum ipset_adt adt, const struct ip_set_adt_opt *opt)
334334
{
335335
const struct ip_set_hash *h = set->data;
336336
ipset_adtfn adtfn = set->variant->adt[adt];
337337
struct hash_ipport6_elem data = { };
338338

339-
if (!ip_set_get_ip6_port(skb, flags & IPSET_DIM_TWO_SRC,
339+
if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
340340
&data.port, &data.proto))
341341
return -EINVAL;
342342

343-
ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
343+
ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
344344

345-
return adtfn(set, &data, h->timeout, flags);
345+
return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
346346
}
347347

348348
static int

0 commit comments

Comments
 (0)