diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2005-06-16 18:43:08 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2005-06-16 18:43:08 +0000 |
commit | c199f848fd9ee5bdebd8c68028cab2d80a10deff (patch) | |
tree | 3149a4281d6e595cbb23b1aba972bc50b7bfc339 | |
parent | 332823dcdc4b0731c36d6f4a6ea4740ca1f77ae7 (diff) |
use prefixlen2mask() instead of doing the bitshifting by hand; fixes the
zero prefixlen case (32 bit shift on 32 bit val -> undefined result)
spotted by Moritz Grimm and otto
-rw-r--r-- | usr.sbin/bgpctl/parser.c | 14 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_update.c | 6 | ||||
-rw-r--r-- | usr.sbin/bgpd/session.c | 4 |
3 files changed, 17 insertions, 7 deletions
diff --git a/usr.sbin/bgpctl/parser.c b/usr.sbin/bgpctl/parser.c index 7f78ebe7618..46de769f9c1 100644 --- a/usr.sbin/bgpctl/parser.c +++ b/usr.sbin/bgpctl/parser.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parser.c,v 1.16 2005/06/07 17:43:06 claudio Exp $ */ +/* $OpenBSD: parser.c,v 1.17 2005/06/16 18:43:00 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -517,7 +517,7 @@ parse_prefix(const char *word, struct bgpd_addr *addr, u_int8_t *prefixlen) &ina, sizeof(ina))) == -1) return (0); addr->af = AF_INET; - addr->v4.s_addr = ina.s_addr & htonl(0xffffffff << (32 - bits)); + addr->v4.s_addr = ina.s_addr & htonl(prefixlen2mask(bits)); *prefixlen = bits; return (1); } else { @@ -726,3 +726,13 @@ parse_nexthop(const char *word, struct parse_result *r) SIMPLEQ_INSERT_TAIL(&r->set, fs, entry); return (1); } + +/* XXX local copy from kroute.c, should go to a shared file */ +in_addr_t +prefixlen2mask(u_int8_t prefixlen) +{ + if (prefixlen == 0) + return (0); + + return (0xffffffff << (32 - prefixlen)); +} diff --git a/usr.sbin/bgpd/rde_update.c b/usr.sbin/bgpd/rde_update.c index 5fd755b298e..24a256aec57 100644 --- a/usr.sbin/bgpd/rde_update.c +++ b/usr.sbin/bgpd/rde_update.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_update.c,v 1.35 2005/03/11 12:54:20 claudio Exp $ */ +/* $OpenBSD: rde_update.c,v 1.36 2005/06/16 18:43:07 henning Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -620,8 +620,8 @@ up_generate_attr(struct rde_peer *peer, struct update_attr *upa, /* ebgp directly connected */ if (a->nexthop != NULL && a->nexthop->flags & NEXTHOP_CONNECTED) { - mask = 0xffffffff << (32 - a->nexthop->nexthop_netlen); - mask = htonl(mask); + mask = htonl( + prefixlen2mask(a->nexthop->nexthop_netlen)); if ((peer->remote_addr.v4.s_addr & mask) == (a->nexthop->nexthop_net.v4.s_addr & mask)) /* nexthop and peer are in the same net */ diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index 80fed08f770..54208f3a99e 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.227 2005/06/09 12:31:38 claudio Exp $ */ +/* $OpenBSD: session.c,v 1.228 2005/06/16 18:43:07 henning Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org> @@ -2518,7 +2518,7 @@ session_match_mask(struct peer *p, struct sockaddr *ip) struct in6_addr mask; if (p->conf.remote_addr.af == AF_INET) { - v4mask = htonl(0xffffffff << (32 - p->conf.remote_masklen)); + v4mask = htonl(prefixlen2mask(p->conf.remote_masklen)); if (p->conf.remote_addr.v4.s_addr == ((((struct sockaddr_in *)ip)->sin_addr.s_addr) & v4mask)) return (1); |