From b0696618f824100c0f5b1c5b8ed37cb05c0a77ec Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 3 Jan 2011 12:51:53 -0800 Subject: Fix check for broadcast address Bug 6625 The validate address function was not accounting for prefix length. (cherry picked from commit d0312e91bad0d9d3741d61791928ac1b663a7f29) --- src/valid_address.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/valid_address.c b/src/valid_address.c index 2f93c257..1053693e 100644 --- a/src/valid_address.c +++ b/src/valid_address.c @@ -62,20 +62,23 @@ static int valid_ipv4(const char *str) return 0; } - if (~addr == 0) { - fprintf(stderr, - "Can not assign broadcast address as IP address\n"); - return 0; - } - if (plen < 31) { uint32_t net_mask = ~0 << (32 - plen); + uint32_t broadcast = (addr & net_mask) | (~0 &~ net_mask); + if ((addr & net_mask) == addr) { fprintf(stderr, "Can not assign network address as IP address\n"); return 0; } + + if (addr == broadcast) { + fprintf(stderr, + "Can not assign broadcast address as IP address\n"); + return 0; + } } + return 1; bad_addr: -- cgit v1.2.3