lib,ospfd,ospf6d: remove duplicated function

Move `is_default_prefix` variations to `lib/prefix.h` and make the code
use the library version instead of implementing it again.

NOTE
----

The function was split into per family versions to cover all types.
Using `union prefixconstptr` is not possible due to static analyzer
warnings which cause CI to fail.

The specific cases that would cause this failure were:
 - Caller used `struct prefix_ipv4` and called the generic function.
 - `is_default_prefix` with signature using `const struct prefix *` or
   `union prefixconstptr`.

The compiler would complain about reading bytes outside of the memory
bounds even though it did not take into account the `prefix->family`
part.

Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
This commit is contained in:
Rafael Zalamena 2021-07-08 14:09:20 -03:00
parent f45f6a22b0
commit 1fe59b44fc
8 changed files with 46 additions and 57 deletions

View File

@ -537,20 +537,32 @@ static inline int ipv4_martian(struct in_addr *addr)
return 0; return 0;
} }
static inline int is_default_prefix(const struct prefix *p) static inline bool is_default_prefix4(const struct prefix_ipv4 *p)
{ {
if (!p) return p && p->family == AF_INET && p->prefixlen == 0
return 0; && p->prefix.s_addr == INADDR_ANY;
}
if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY) static inline bool is_default_prefix6(const struct prefix_ipv6 *p)
&& (p->prefixlen == 0)) {
return 1; return p && p->family == AF_INET6 && p->prefixlen == 0
&& memcmp(&p->prefix, &in6addr_any, sizeof(struct in6_addr))
== 0;
}
if ((p->family == AF_INET6) && (p->prefixlen == 0) static inline bool is_default_prefix(const struct prefix *p)
&& (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr)))) {
return 1; if (p == NULL)
return false;
return 0; switch (p->family) {
case AF_INET:
return is_default_prefix4((const struct prefix_ipv4 *)p);
case AF_INET6:
return is_default_prefix6((const struct prefix_ipv6 *)p);
}
return false;
} }
static inline int is_host_route(const struct prefix *p) static inline int is_host_route(const struct prefix *p)

View File

@ -194,16 +194,6 @@ static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS)
return 0; return 0;
} }
static int is_prefix_default(struct prefix_ipv6 *p)
{
struct prefix_ipv6 q = {};
q.family = AF_INET6;
q.prefixlen = 0;
return prefix_same((struct prefix *)p, (struct prefix *)&q);
}
static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS) static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
{ {
struct zapi_route api; struct zapi_route api;
@ -239,7 +229,7 @@ static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
ifindex, api.tag); ifindex, api.tag);
memcpy(&p, &api.prefix, sizeof(p)); memcpy(&p, &api.prefix, sizeof(p));
if (is_prefix_default(&p)) if (is_default_prefix6(&p))
api.type = DEFAULT_ROUTE; api.type = DEFAULT_ROUTE;
if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)

View File

@ -334,7 +334,7 @@ void ospf_redistribute_withdraw(struct ospf *ospf, uint8_t type,
struct ospf_external_aggr_rt *aggr; struct ospf_external_aggr_rt *aggr;
if (is_prefix_default(&ei->p) if (is_default_prefix4(&ei->p)
&& ospf->default_originate != DEFAULT_ORIGINATE_NONE) && ospf->default_originate != DEFAULT_ORIGINATE_NONE)
continue; continue;
@ -862,7 +862,7 @@ static void ospf_handle_external_aggr_add(struct ospf *ospf)
continue; continue;
ei = rn->info; ei = rn->info;
if (is_prefix_default(&ei->p)) if (is_default_prefix4(&ei->p))
continue; continue;
/* Check the AS-external-LSA /* Check the AS-external-LSA

View File

@ -98,7 +98,7 @@ struct external_info *ospf_external_info_check(struct ospf *ospf,
int redist_on = 0; int redist_on = 0;
redist_on = redist_on =
is_prefix_default(&p) is_default_prefix4(&p)
? vrf_bitmap_check( ? vrf_bitmap_check(
zclient->default_information[AFI_IP], zclient->default_information[AFI_IP],
ospf->vrf_id) ospf->vrf_id)
@ -128,7 +128,7 @@ struct external_info *ospf_external_info_check(struct ospf *ospf,
} }
} }
if (is_prefix_default(&p) && ospf->external[DEFAULT_ROUTE]) { if (is_default_prefix4(&p) && ospf->external[DEFAULT_ROUTE]) {
ext_list = ospf->external[DEFAULT_ROUTE]; ext_list = ospf->external[DEFAULT_ROUTE];
for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) { for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {

View File

@ -1551,8 +1551,8 @@ static void ospf_external_lsa_body_set(struct stream *s,
stream_put_ipv4(s, mask.s_addr); stream_put_ipv4(s, mask.s_addr);
/* If prefix is default, specify DEFAULT_ROUTE. */ /* If prefix is default, specify DEFAULT_ROUTE. */
type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type; type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
instance = is_prefix_default(&ei->p) ? 0 : ei->instance; instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1) mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
? ROUTEMAP_METRIC_TYPE(ei) ? ROUTEMAP_METRIC_TYPE(ei)
@ -1947,17 +1947,6 @@ struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
return new; return new;
} }
int is_prefix_default(struct prefix_ipv4 *p)
{
struct prefix_ipv4 q;
q.family = AF_INET;
q.prefix.s_addr = INADDR_ANY;
q.prefixlen = 0;
return prefix_same((struct prefix *)p, (struct prefix *)&q);
}
/* Originate an AS-external-LSA, install and flood. */ /* Originate an AS-external-LSA, install and flood. */
struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf, struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
struct external_info *ei) struct external_info *ei)
@ -2113,8 +2102,7 @@ void ospf_external_lsa_rid_change(struct ospf *ospf)
if (!ei) if (!ei)
continue; continue;
if (is_prefix_default( if (is_default_prefix4(&ei->p))
(struct prefix_ipv4 *)&ei->p))
continue; continue;
lsa = ospf_external_info_find_lsa(ospf, &ei->p); lsa = ospf_external_info_find_lsa(ospf, &ei->p);
@ -2284,7 +2272,7 @@ void ospf_external_lsa_refresh_type(struct ospf *ospf, uint8_t type,
rn = route_next(rn)) { rn = route_next(rn)) {
ei = rn->info; ei = rn->info;
if (ei) { if (ei) {
if (!is_prefix_default(&ei->p)) { if (!is_default_prefix4(&ei->p)) {
struct ospf_lsa *lsa; struct ospf_lsa *lsa;
struct ospf_external_aggr_rt *aggr; struct ospf_external_aggr_rt *aggr;

View File

@ -331,7 +331,6 @@ extern void ospf_lsa_maxage_delete(struct ospf *, struct ospf_lsa *);
extern void ospf_discard_from_db(struct ospf *, struct ospf_lsdb *, extern void ospf_discard_from_db(struct ospf *, struct ospf_lsdb *,
struct ospf_lsa *); struct ospf_lsa *);
extern int is_prefix_default(struct prefix_ipv4 *);
extern int metric_type(struct ospf *, uint8_t, unsigned short); extern int metric_type(struct ospf *, uint8_t, unsigned short);
extern int metric_value(struct ospf *, uint8_t, unsigned short); extern int metric_value(struct ospf *, uint8_t, unsigned short);

View File

@ -10027,7 +10027,7 @@ DEFUN (ospf_external_route_aggregation,
str2prefix_ipv4(argv[idx]->arg, &p); str2prefix_ipv4(argv[idx]->arg, &p);
if (is_prefix_default(&p)) { if (is_default_prefix4(&p)) {
vty_out(vty, vty_out(vty,
"Default address shouldn't be configured as summary address.\n"); "Default address shouldn't be configured as summary address.\n");
return CMD_SUCCESS; return CMD_SUCCESS;
@ -10068,7 +10068,7 @@ DEFUN (no_ospf_external_route_aggregation,
str2prefix_ipv4(argv[idx]->arg, &p); str2prefix_ipv4(argv[idx]->arg, &p);
if (is_prefix_default(&p)) { if (is_default_prefix4(&p)) {
vty_out(vty, vty_out(vty,
"Default address shouldn't be configured as summary address.\n"); "Default address shouldn't be configured as summary address.\n");
return CMD_SUCCESS; return CMD_SUCCESS;
@ -10361,7 +10361,7 @@ DEFUN (ospf_external_route_aggregation_no_adrvertise,
str2prefix_ipv4(argv[idx]->arg, &p); str2prefix_ipv4(argv[idx]->arg, &p);
if (is_prefix_default(&p)) { if (is_default_prefix4(&p)) {
vty_out(vty, vty_out(vty,
"Default address shouldn't be configured as summary address.\n"); "Default address shouldn't be configured as summary address.\n");
return CMD_SUCCESS; return CMD_SUCCESS;
@ -10397,7 +10397,7 @@ DEFUN (no_ospf_external_route_aggregation_no_adrvertise,
str2prefix_ipv4(argv[idx]->arg, &p); str2prefix_ipv4(argv[idx]->arg, &p);
if (is_prefix_default(&p)) { if (is_default_prefix4(&p)) {
vty_out(vty, vty_out(vty,
"Default address shouldn't be configured as summary address.\n"); "Default address shouldn't be configured as summary address.\n");
return CMD_SUCCESS; return CMD_SUCCESS;

View File

@ -933,7 +933,7 @@ static int ospf_external_lsa_originate_check(struct ospf *ospf,
} }
/* Take care of default-originate. */ /* Take care of default-originate. */
if (is_prefix_default(&ei->p)) if (is_default_prefix4(&ei->p))
if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) { if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
zlog_info( zlog_info(
"LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default"); "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA for default");
@ -1089,8 +1089,8 @@ int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei,
struct route_map_set_values save_values; struct route_map_set_values save_values;
struct prefix_ipv4 *p = &ei->p; struct prefix_ipv4 *p = &ei->p;
struct ospf_redist *red; struct ospf_redist *red;
uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type; uint8_t type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance; unsigned short instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
route_tag_t saved_tag = 0; route_tag_t saved_tag = 0;
/* Default is handled differently. */ /* Default is handled differently. */
@ -1213,7 +1213,7 @@ static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
* originate)ZEBRA_ROUTE_MAX is used to delete the ex-info. * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info.
* Resolved this inconsistency by maintaining same route type. * Resolved this inconsistency by maintaining same route type.
*/ */
if (is_prefix_default(&p)) if (is_default_prefix4(&p))
rt_type = DEFAULT_ROUTE; rt_type = DEFAULT_ROUTE;
if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
@ -1252,7 +1252,7 @@ static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
return 0; return 0;
} }
if (ospf->router_id.s_addr != INADDR_ANY) { if (ospf->router_id.s_addr != INADDR_ANY) {
if (is_prefix_default(&p)) if (is_default_prefix4(&p))
ospf_external_lsa_refresh_default(ospf); ospf_external_lsa_refresh_default(ospf);
else { else {
struct ospf_external_aggr_rt *aggr; struct ospf_external_aggr_rt *aggr;
@ -1374,7 +1374,7 @@ static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
ospf_external_info_delete(ospf, rt_type, api.instance, ospf_external_info_delete(ospf, rt_type, api.instance,
p); p);
if (is_prefix_default(&p)) if (is_default_prefix4(&p))
ospf_external_lsa_refresh_default(ospf); ospf_external_lsa_refresh_default(ospf);
else else
ospf_external_lsa_flush(ospf, rt_type, &p, ospf_external_lsa_flush(ospf, rt_type, &p,
@ -1471,7 +1471,7 @@ static int ospf_distribute_list_update_timer(struct thread *thread)
if (!ei) if (!ei)
continue; continue;
if (is_prefix_default(&ei->p)) if (is_default_prefix4(&ei->p))
default_refresh = 1; default_refresh = 1;
else { else {
struct ospf_external_aggr_rt *aggr; struct ospf_external_aggr_rt *aggr;