Skip to content

Commit 3e0304a

Browse files
author
Jozsef Kadlecsik
committed
netfilter: ipset: Support to match elements marked with "nomatch"
Exceptions can now be matched and we can branch according to the possible cases: a. match in the set if the element is not flagged as "nomatch" b. match in the set if the element is flagged with "nomatch" c. no match i.e. iptables ... -m set --match-set ... -j ... iptables ... -m set --match-set ... --nomatch-entries -j ... ... Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1 parent 3ace95c commit 3e0304a

7 files changed

Lines changed: 54 additions & 20 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ enum ip_set_dim {
190190
* If changed, new revision of iptables match/target is required.
191191
*/
192192
IPSET_DIM_MAX = 6,
193+
IPSET_BIT_RETURN_NOMATCH = 7,
193194
};
194195

195196
/* Option flags for kernel operations */
@@ -198,6 +199,7 @@ enum ip_set_kopt {
198199
IPSET_DIM_ONE_SRC = (1 << IPSET_DIM_ONE),
199200
IPSET_DIM_TWO_SRC = (1 << IPSET_DIM_TWO),
200201
IPSET_DIM_THREE_SRC = (1 << IPSET_DIM_THREE),
202+
IPSET_RETURN_NOMATCH = (1 << IPSET_BIT_RETURN_NOMATCH),
201203
};
202204

203205
#ifdef __KERNEL__
@@ -229,6 +231,8 @@ enum ip_set_feature {
229231
IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
230232
IPSET_TYPE_IFACE_FLAG = 5,
231233
IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
234+
IPSET_TYPE_NOMATCH_FLAG = 6,
235+
IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
232236
/* Strictly speaking not a feature, but a flag for dumping:
233237
* this settype must be dumped last */
234238
IPSET_DUMP_LAST_FLAG = 7,

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
370370
set->variant->kadt(set, skb, par, IPSET_ADD, opt);
371371
write_unlock_bh(&set->lock);
372372
ret = 1;
373+
} else {
374+
/* --return-nomatch: invert matched element */
375+
if ((opt->flags & IPSET_RETURN_NOMATCH) &&
376+
(set->type->features & IPSET_TYPE_NOMATCH) &&
377+
(ret > 0 || ret == -ENOTEMPTY))
378+
ret = -ret;
373379
}
374380

375381
/* Convert error codes to nomatch */

‎net/netfilter/ipset/ip_set_hash_ipportnet.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ hash_ipportnet4_data_flags(struct hash_ipportnet4_elem *dst, u32 flags)
104104
dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
105105
}
106106

107-
static inline bool
107+
static inline int
108108
hash_ipportnet4_data_match(const struct hash_ipportnet4_elem *elem)
109109
{
110-
return !elem->nomatch;
110+
return elem->nomatch ? -ENOTEMPTY : 1;
111111
}
112112

113113
static inline void
@@ -411,10 +411,10 @@ hash_ipportnet6_data_flags(struct hash_ipportnet6_elem *dst, u32 flags)
411411
dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
412412
}
413413

414-
static inline bool
414+
static inline int
415415
hash_ipportnet6_data_match(const struct hash_ipportnet6_elem *elem)
416416
{
417-
return !elem->nomatch;
417+
return elem->nomatch ? -ENOTEMPTY : 1;
418418
}
419419

420420
static inline void
@@ -697,7 +697,8 @@ hash_ipportnet_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
697697
static struct ip_set_type hash_ipportnet_type __read_mostly = {
698698
.name = "hash:ip,port,net",
699699
.protocol = IPSET_PROTOCOL,
700-
.features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
700+
.features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
701+
IPSET_TYPE_NOMATCH,
701702
.dimension = IPSET_DIM_THREE,
702703
.family = NFPROTO_UNSPEC,
703704
.revision_min = REVISION_MIN,

‎net/netfilter/ipset/ip_set_hash_net.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ hash_net4_data_flags(struct hash_net4_elem *dst, u32 flags)
9090
dst->nomatch = flags & IPSET_FLAG_NOMATCH;
9191
}
9292

93-
static inline bool
93+
static inline int
9494
hash_net4_data_match(const struct hash_net4_elem *elem)
9595
{
96-
return !elem->nomatch;
96+
return elem->nomatch ? -ENOTEMPTY : 1;
9797
}
9898

9999
static inline void
@@ -311,10 +311,10 @@ hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
311311
dst->nomatch = flags & IPSET_FLAG_NOMATCH;
312312
}
313313

314-
static inline bool
314+
static inline int
315315
hash_net6_data_match(const struct hash_net6_elem *elem)
316316
{
317-
return !elem->nomatch;
317+
return elem->nomatch ? -ENOTEMPTY : 1;
318318
}
319319

320320
static inline void
@@ -536,7 +536,7 @@ hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
536536
static struct ip_set_type hash_net_type __read_mostly = {
537537
.name = "hash:net",
538538
.protocol = IPSET_PROTOCOL,
539-
.features = IPSET_TYPE_IP,
539+
.features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
540540
.dimension = IPSET_DIM_ONE,
541541
.family = NFPROTO_UNSPEC,
542542
.revision_min = REVISION_MIN,

‎net/netfilter/ipset/ip_set_hash_netiface.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ hash_netiface4_data_flags(struct hash_netiface4_elem *dst, u32 flags)
201201
dst->nomatch = flags & IPSET_FLAG_NOMATCH;
202202
}
203203

204-
static inline bool
204+
static inline int
205205
hash_netiface4_data_match(const struct hash_netiface4_elem *elem)
206206
{
207-
return !elem->nomatch;
207+
return elem->nomatch ? -ENOTEMPTY : 1;
208208
}
209209

210210
static inline void
@@ -497,10 +497,10 @@ hash_netiface6_data_flags(struct hash_netiface6_elem *dst, u32 flags)
497497
dst->nomatch = flags & IPSET_FLAG_NOMATCH;
498498
}
499499

500-
static inline bool
500+
static inline int
501501
hash_netiface6_data_match(const struct hash_netiface6_elem *elem)
502502
{
503-
return !elem->nomatch;
503+
return elem->nomatch ? -ENOTEMPTY : 1;
504504
}
505505

506506
static inline void
@@ -774,7 +774,8 @@ hash_netiface_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
774774
static struct ip_set_type hash_netiface_type __read_mostly = {
775775
.name = "hash:net,iface",
776776
.protocol = IPSET_PROTOCOL,
777-
.features = IPSET_TYPE_IP | IPSET_TYPE_IFACE,
777+
.features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
778+
IPSET_TYPE_NOMATCH,
778779
.dimension = IPSET_DIM_TWO,
779780
.family = NFPROTO_UNSPEC,
780781
.revision_min = REVISION_MIN,

‎net/netfilter/ipset/ip_set_hash_netport.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ hash_netport4_data_flags(struct hash_netport4_elem *dst, u32 flags)
104104
dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
105105
}
106106

107-
static inline bool
107+
static inline int
108108
hash_netport4_data_match(const struct hash_netport4_elem *elem)
109109
{
110-
return !elem->nomatch;
110+
return elem->nomatch ? -ENOTEMPTY : 1;
111111
}
112112

113113
static inline void
@@ -375,10 +375,10 @@ hash_netport6_data_flags(struct hash_netport6_elem *dst, u32 flags)
375375
dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
376376
}
377377

378-
static inline bool
378+
static inline int
379379
hash_netport6_data_match(const struct hash_netport6_elem *elem)
380380
{
381-
return !elem->nomatch;
381+
return elem->nomatch ? -ENOTEMPTY : 1;
382382
}
383383

384384
static inline void
@@ -650,7 +650,7 @@ hash_netport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
650650
static struct ip_set_type hash_netport_type __read_mostly = {
651651
.name = "hash:net,port",
652652
.protocol = IPSET_PROTOCOL,
653-
.features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
653+
.features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
654654
.dimension = IPSET_DIM_TWO,
655655
.family = NFPROTO_UNSPEC,
656656
.revision_min = REVISION_MIN,

‎net/netfilter/xt_set.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,27 @@ static struct xt_match set_matches[] __read_mostly = {
356356
.destroy = set_match_v1_destroy,
357357
.me = THIS_MODULE
358358
},
359+
/* --return-nomatch flag support */
360+
{
361+
.name = "set",
362+
.family = NFPROTO_IPV4,
363+
.revision = 2,
364+
.match = set_match_v1,
365+
.matchsize = sizeof(struct xt_set_info_match_v1),
366+
.checkentry = set_match_v1_checkentry,
367+
.destroy = set_match_v1_destroy,
368+
.me = THIS_MODULE
369+
},
370+
{
371+
.name = "set",
372+
.family = NFPROTO_IPV6,
373+
.revision = 2,
374+
.match = set_match_v1,
375+
.matchsize = sizeof(struct xt_set_info_match_v1),
376+
.checkentry = set_match_v1_checkentry,
377+
.destroy = set_match_v1_destroy,
378+
.me = THIS_MODULE
379+
},
359380
};
360381

361382
static struct xt_target set_targets[] __read_mostly = {
@@ -389,6 +410,7 @@ static struct xt_target set_targets[] __read_mostly = {
389410
.destroy = set_target_v1_destroy,
390411
.me = THIS_MODULE
391412
},
413+
/* --timeout and --exist flags support */
392414
{
393415
.name = "SET",
394416
.revision = 2,

0 commit comments

Comments
 (0)