From 2b5c88fe9e6affc84d40c094ca695ea77e6ab5c2 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 4 Sep 2018 12:00:29 +0000 Subject: [PATCH] Introduce inet4applymask() which does the same as inet6applymask() and can be used instead of doing direct fiddling around with struct in_addr. Use it in a few cases where it makes the code more similar between INET and INET6 case. OK denis@ --- usr.sbin/bgpd/bgpd.h | 3 ++- usr.sbin/bgpd/rde_prefix.c | 14 +++++--------- usr.sbin/bgpd/session.c | 15 ++++++++------- usr.sbin/bgpd/util.c | 11 ++++++++++- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index 046a568e38f..71622512a4b 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.331 2018/08/29 19:47:47 claudio Exp $ */ +/* $OpenBSD: bgpd.h,v 1.332 2018/09/04 12:00:29 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -1174,6 +1174,7 @@ int nlri_get_vpn4(u_char *, u_int16_t, struct bgpd_addr *, int prefix_compare(const struct bgpd_addr *, const struct bgpd_addr *, int); in_addr_t prefixlen2mask(u_int8_t); +void inet4applymask(struct in_addr *, const struct in_addr *, int); void inet6applymask(struct in6_addr *, const struct in6_addr *, int); const char *aid2str(u_int8_t); diff --git a/usr.sbin/bgpd/rde_prefix.c b/usr.sbin/bgpd/rde_prefix.c index e1b64d96923..63927c95503 100644 --- a/usr.sbin/bgpd/rde_prefix.c +++ b/usr.sbin/bgpd/rde_prefix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_prefix.c,v 1.33 2017/01/24 04:22:42 benno Exp $ */ +/* $OpenBSD: rde_prefix.c,v 1.34 2018/09/04 12:00:29 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker @@ -101,7 +101,6 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) static struct pt_entry4 pte4; static struct pt_entry6 pte6; static struct pt_entry_vpn4 pte_vpn4; - in_addr_t addr_hbo; switch (prefix->aid) { case AID_INET: @@ -109,9 +108,7 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) pte4.aid = prefix->aid; if (prefixlen > 32) fatalx("pt_fill: bad IPv4 prefixlen"); - addr_hbo = ntohl(prefix->v4.s_addr); - pte4.prefix4.s_addr = htonl(addr_hbo & - prefixlen2mask(prefixlen)); + inet4applymask(&pte4.prefix4, &prefix->v4, prefixlen); pte4.prefixlen = prefixlen; return ((struct pt_entry *)&pte4); case AID_INET6: @@ -119,17 +116,16 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) pte6.aid = prefix->aid; if (prefixlen > 128) fatalx("pt_get: bad IPv6 prefixlen"); - pte6.prefixlen = prefixlen; inet6applymask(&pte6.prefix6, &prefix->v6, prefixlen); + pte6.prefixlen = prefixlen; return ((struct pt_entry *)&pte6); case AID_VPN_IPv4: bzero(&pte_vpn4, sizeof(pte_vpn4)); pte_vpn4.aid = prefix->aid; if (prefixlen > 32) fatalx("pt_fill: bad IPv4 prefixlen"); - addr_hbo = ntohl(prefix->vpn4.addr.s_addr); - pte_vpn4.prefix4.s_addr = htonl(addr_hbo & - prefixlen2mask(prefixlen)); + inet4applymask(&pte_vpn4.prefix4, &prefix->vpn4.addr, + prefixlen); pte_vpn4.prefixlen = prefixlen; pte_vpn4.rd = prefix->vpn4.rd; pte_vpn4.labellen = prefix->vpn4.labellen; diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index 1cd90aa3d2c..659d52157ac 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.365 2018/07/11 16:34:36 claudio Exp $ */ +/* $OpenBSD: session.c,v 1.366 2018/09/04 12:00:29 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer @@ -3150,19 +3150,20 @@ session_template_clone(struct peer *p, struct sockaddr *ip, u_int32_t id, int session_match_mask(struct peer *p, struct bgpd_addr *a) { - in_addr_t v4mask; - struct in6_addr masked; + struct in_addr v4masked; + struct in6_addr v6masked; switch (p->conf.remote_addr.aid) { case AID_INET: - v4mask = htonl(prefixlen2mask(p->conf.remote_masklen)); - if (p->conf.remote_addr.v4.s_addr == (a->v4.s_addr & v4mask)) + inet4applymask(&v4masked, &a->v4, p->conf.remote_masklen); + if (p->conf.remote_addr.v4.s_addr == v4masked.s_addr) return (1); return (0); case AID_INET6: - inet6applymask(&masked, &a->v6, p->conf.remote_masklen); + inet6applymask(&v6masked, &a->v6, p->conf.remote_masklen); - if (!memcmp(&masked, &p->conf.remote_addr.v6, sizeof(masked))) + if (memcmp(&v6masked, &p->conf.remote_addr.v6, + sizeof(v6masked)) == 0) return (1); return (0); } diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c index 2ef6330363d..3b35efc7a35 100644 --- a/usr.sbin/bgpd/util.c +++ b/usr.sbin/bgpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.31 2018/08/29 11:43:15 claudio Exp $ */ +/* $OpenBSD: util.c,v 1.32 2018/09/04 12:00:29 claudio Exp $ */ /* * Copyright (c) 2006 Claudio Jeker @@ -731,6 +731,15 @@ prefixlen2mask(u_int8_t prefixlen) return (0xffffffff << (32 - prefixlen)); } +void +inet4applymask(struct in_addr *dest, const struct in_addr *src, int prefixlen) +{ + struct in_addr mask; + + mask.s_addr = htonl(prefixlen2mask(prefixlen)); + dest->s_addr = src->s_addr & mask.s_addr; +} + void inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen) { -- 2.20.1