Change the logic and name of bgpd_filternexthop(). This function applies the 'nexthop qualify via' config setting. Instead of telling if the route is filtered (true) or not (false) flip the logic around and rename the function to bgpd_oknexthop(). Also flip the internal logic around to simplify the logic in bgpd_oknexthop().
Adjust the kroute_match() code accordingly (which makes that code easier to understand). This is just a refactor and should not change behaviour. -- :wq Claudio Index: bgpd.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpd/bgpd.c,v retrieving revision 1.251 diff -u -p -r1.251 bgpd.c --- bgpd.c 22 Jul 2022 17:26:58 -0000 1.251 +++ bgpd.c 23 Jul 2022 08:53:58 -0000 @@ -1115,23 +1115,27 @@ send_network(int type, struct network_co 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 (cflags & BGPD_FLAG_NEXTHOP_BGP) { - if (kf->flags & F_BGPD) + if (kf->flags & F_BGPD) { + if (cflags & BGPD_FLAG_NEXTHOP_BGP) + return (1); + else return (0); } - if (cflags & BGPD_FLAG_NEXTHOP_DEFAULT) { - if (kf->prefixlen == 0) + if (kf->prefixlen == 0) { + if (cflags & BGPD_FLAG_NEXTHOP_DEFAULT) + return (1); + else return (0); } + /* any other route is fine */ return (1); } Index: bgpd.h =================================================================== RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v retrieving revision 1.444 diff -u -p -r1.444 bgpd.h --- bgpd.h 22 Jul 2022 17:26:58 -0000 1.444 +++ bgpd.h 23 Jul 2022 08:54:09 -0000 @@ -1265,7 +1265,7 @@ void send_nexthop_update(struct kroute 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 *); Index: kroute.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpd/kroute.c,v retrieving revision 1.278 diff -u -p -r1.278 kroute.c --- kroute.c 22 Jul 2022 17:26:58 -0000 1.278 +++ kroute.c 23 Jul 2022 08:55:27 -0000 @@ -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 b 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 b } 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 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); }