Skip to content

Commit e6146e8

Browse files
Jozsef Kadlecsikkaber
authored andcommitted
netfilter: ipset: use unified from/to address masking and check the usage
Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent f3dfd15 commit e6146e8

8 files changed

Lines changed: 15 additions & 17 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ ip_set_hostmask6(u8 pfxlen)
3535

3636
extern u32 ip_set_range_to_cidr(u32 from, u32 to, u8 *cidr);
3737

38+
#define ip_set_mask_from_to(from, to, cidr) \
39+
do { \
40+
from &= ip_set_hostmask(cidr); \
41+
to = from | ~ip_set_hostmask(cidr); \
42+
} while (0)
43+
3844
#endif /*_PFXLEN_H */

‎net/netfilter/ipset/ip_set_bitmap_ip.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],
283283

284284
if (cidr > 32)
285285
return -IPSET_ERR_INVALID_CIDR;
286-
ip &= ip_set_hostmask(cidr);
287-
ip_to = ip | ~ip_set_hostmask(cidr);
286+
ip_set_mask_from_to(ip, ip_to, cidr);
288287
} else
289288
ip_to = ip;
290289

@@ -478,8 +477,7 @@ bitmap_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
478477

479478
if (cidr >= 32)
480479
return -IPSET_ERR_INVALID_CIDR;
481-
first_ip &= ip_set_hostmask(cidr);
482-
last_ip = first_ip | ~ip_set_hostmask(cidr);
480+
ip_set_mask_from_to(first_ip, last_ip, cidr);
483481
} else
484482
return -IPSET_ERR_PROTOCOL;
485483

‎net/netfilter/ipset/ip_set_bitmap_ipmac.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
578578

579579
if (cidr >= 32)
580580
return -IPSET_ERR_INVALID_CIDR;
581-
last_ip = first_ip | ~ip_set_hostmask(cidr);
581+
ip_set_mask_from_to(first_ip, last_ip, cidr);
582582
} else
583583
return -IPSET_ERR_PROTOCOL;
584584

‎net/netfilter/ipset/ip_set_hash_ip.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
177177

178178
if (cidr > 32)
179179
return -IPSET_ERR_INVALID_CIDR;
180-
ip &= ip_set_hostmask(cidr);
181-
ip_to = ip | ~ip_set_hostmask(cidr);
180+
ip_set_mask_from_to(ip, ip_to, cidr);
182181
} else
183182
ip_to = ip;
184183

‎net/netfilter/ipset/ip_set_hash_ipport.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
216216

217217
if (cidr > 32)
218218
return -IPSET_ERR_INVALID_CIDR;
219-
ip &= ip_set_hostmask(cidr);
220-
ip_to = ip | ~ip_set_hostmask(cidr);
219+
ip_set_mask_from_to(ip, ip_to, cidr);
221220
} else
222221
ip_to = ip;
223222

‎net/netfilter/ipset/ip_set_hash_ipportip.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
224224

225225
if (cidr > 32)
226226
return -IPSET_ERR_INVALID_CIDR;
227-
ip &= ip_set_hostmask(cidr);
228-
ip_to = ip | ~ip_set_hostmask(cidr);
227+
ip_set_mask_from_to(ip, ip_to, cidr);
229228
} else
230229
ip_to = ip;
231230

‎net/netfilter/ipset/ip_set_hash_ipportnet.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
254254

255255
if (cidr > 32)
256256
return -IPSET_ERR_INVALID_CIDR;
257-
ip &= ip_set_hostmask(cidr);
258-
ip_to = ip | ~ip_set_hostmask(cidr);
257+
ip_set_mask_from_to(ip, ip_to, cidr);
259258
}
260259

261260
port_to = port = ntohs(data.port);
@@ -273,8 +272,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
273272
if (ip2_from + UINT_MAX == ip2_to)
274273
return -IPSET_ERR_HASH_RANGE;
275274
} else {
276-
ip2_from &= ip_set_hostmask(data.cidr);
277-
ip2_to = ip2_from | ~ip_set_hostmask(data.cidr);
275+
ip_set_mask_from_to(ip2_from, ip2_to, data.cidr);
278276
}
279277

280278
if (retried)

‎net/netfilter/ipset/ip_set_hash_netport.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
245245
if (ip + UINT_MAX == ip_to)
246246
return -IPSET_ERR_HASH_RANGE;
247247
} else {
248-
ip &= ip_set_hostmask(data.cidr);
249-
ip_to = ip | ~ip_set_hostmask(data.cidr);
248+
ip_set_mask_from_to(ip, ip_to, data.cidr);
250249
}
251250

252251
if (retried)

0 commit comments

Comments
 (0)