From b369529143346058dce414ee6b5b55fad814c4fc Mon Sep 17 00:00:00 2001 From: claudio Date: Sat, 23 Jul 2022 10:24:00 +0000 Subject: [PATCH] Refactor and rename bgpd_filternexthop() to bgpd_oknexthop() Simplify the logic and adjust kroute_match() which makes the code easier to understand. OK tb@ --- usr.sbin/bgpd/bgpd.c | 24 ++++++++++-------------- usr.sbin/bgpd/bgpd.h | 4 ++-- usr.sbin/bgpd/kroute.c | 12 +++++------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/usr.sbin/bgpd/bgpd.c b/usr.sbin/bgpd/bgpd.c index 600e1f89795..2a2b515fdc6 100644 --- a/usr.sbin/bgpd/bgpd.c +++ b/usr.sbin/bgpd/bgpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.c,v 1.251 2022/07/22 17:26:58 claudio Exp $ */ +/* $OpenBSD: bgpd.c,v 1.252 2022/07/23 10:24:00 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -1115,23 +1115,19 @@ send_network(int type, struct network_config *net, struct filter_set_head *h) return (0); } +/* + * Return true if a route can be used for nexthop resolution. + */ int -bgpd_filternexthop(struct kroute_full *kf) +bgpd_oknexthop(struct kroute_full *kf) { - /* kernel routes are never filtered */ - if (kf->flags & F_KERNEL && kf->prefixlen != 0) - return (0); + if (kf->flags & F_BGPD) + return ((cflags & BGPD_FLAG_NEXTHOP_BGP) != 0); - if (cflags & BGPD_FLAG_NEXTHOP_BGP) { - if (kf->flags & F_BGPD) - return (0); - } - - if (cflags & BGPD_FLAG_NEXTHOP_DEFAULT) { - if (kf->prefixlen == 0) - return (0); - } + if (kf->prefixlen == 0) + return ((cflags & BGPD_FLAG_NEXTHOP_DEFAULT) != 0); + /* any other route is fine */ return (1); } diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index ef75e651206..ce50f2bc7d9 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.444 2022/07/22 17:26:58 claudio Exp $ */ +/* $OpenBSD: bgpd.h,v 1.445 2022/07/23 10:24:01 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -1265,7 +1265,7 @@ void send_nexthop_update(struct kroute_nexthop *); void send_imsg_session(int, pid_t, void *, uint16_t); int send_network(int, struct network_config *, struct filter_set_head *); -int bgpd_filternexthop(struct kroute_full *); +int bgpd_oknexthop(struct kroute_full *); void set_pollfd(struct pollfd *, struct imsgbuf *); int handle_pollfd(struct pollfd *, struct imsgbuf *); diff --git a/usr.sbin/bgpd/kroute.c b/usr.sbin/bgpd/kroute.c index d261d4220e0..49a73201822 100644 --- a/usr.sbin/bgpd/kroute.c +++ b/usr.sbin/bgpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.278 2022/07/22 17:26:58 claudio Exp $ */ +/* $OpenBSD: kroute.c,v 1.279 2022/07/23 10:24:01 claudio Exp $ */ /* * Copyright (c) 2022 Claudio Jeker @@ -2444,7 +2444,7 @@ knexthop_send_update(struct knexthop *kn) } struct kroute * -kroute_match(struct ktable *kt, struct bgpd_addr *key, int matchall) +kroute_match(struct ktable *kt, struct bgpd_addr *key, int matchany) { int i; struct kroute *kr; @@ -2453,8 +2453,7 @@ kroute_match(struct ktable *kt, struct bgpd_addr *key, int matchall) for (i = 32; i >= 0; i--) { applymask(&masked, key, i); if ((kr = kroute_find(kt, &masked, i, RTP_ANY)) != NULL) - if (matchall || - bgpd_filternexthop(kr_tofull(kr)) == 0) + if (matchany || bgpd_oknexthop(kr_tofull(kr))) return (kr); } @@ -2462,7 +2461,7 @@ kroute_match(struct ktable *kt, struct bgpd_addr *key, int matchall) } struct kroute6 * -kroute6_match(struct ktable *kt, struct bgpd_addr *key, int matchall) +kroute6_match(struct ktable *kt, struct bgpd_addr *key, int matchany) { int i; struct kroute6 *kr6; @@ -2471,8 +2470,7 @@ kroute6_match(struct ktable *kt, struct bgpd_addr *key, int matchall) for (i = 128; i >= 0; i--) { applymask(&masked, key, i); if ((kr6 = kroute6_find(kt, &masked, i, RTP_ANY)) != NULL) - if (matchall || - bgpd_filternexthop(kr6_tofull(kr6)) == 0) + if (matchany || bgpd_oknexthop(kr6_tofull(kr6))) return (kr6); } -- 2.20.1