]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib,ospfd,ospf6d: remove duplicated function
authorRafael Zalamena <rzalamena@opensourcerouting.org>
Thu, 8 Jul 2021 17:09:20 +0000 (14:09 -0300)
committerRafael Zalamena <rzalamena@opensourcerouting.org>
Fri, 9 Jul 2021 10:55:25 +0000 (07:55 -0300)
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>
lib/prefix.h
ospf6d/ospf6_zebra.c
ospfd/ospf_asbr.c
ospfd/ospf_flood.c
ospfd/ospf_lsa.c
ospfd/ospf_lsa.h
ospfd/ospf_vty.c
ospfd/ospf_zebra.c

index bc4cb7f441c2007c746d86c3d90ecb9ab30c12bd..944c94f57f8a2a6a93d4ac4772befdadf0694812 100644 (file)
@@ -537,20 +537,32 @@ static inline int ipv4_martian(struct in_addr *addr)
        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 0;
+       return p && p->family == AF_INET && p->prefixlen == 0
+              && p->prefix.s_addr == INADDR_ANY;
+}
 
-       if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY)
-           && (p->prefixlen == 0))
-               return 1;
+static inline bool is_default_prefix6(const struct prefix_ipv6 *p)
+{
+       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)
-           && (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr))))
-               return 1;
+static inline bool is_default_prefix(const struct prefix *p)
+{
+       if (p == NULL)
+               return false;
+
+       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 0;
+       return false;
 }
 
 static inline int is_host_route(const struct prefix *p)
index a7e15c68ae81f35b6fba3a446de1acdd38804d2a..72bc3a2f3ac052d6f2d8d01d877854bd54e9bff4 100644 (file)
@@ -194,16 +194,6 @@ static int ospf6_zebra_if_address_update_delete(ZAPI_CALLBACK_ARGS)
        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)
 {
        struct zapi_route api;
@@ -239,7 +229,7 @@ static int ospf6_zebra_read_route(ZAPI_CALLBACK_ARGS)
                        ifindex, api.tag);
 
        memcpy(&p, &api.prefix, sizeof(p));
-       if (is_prefix_default(&p))
+       if (is_default_prefix6(&p))
                api.type = DEFAULT_ROUTE;
 
        if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
index 192dbe4fc8c0d8bffef6280fc148359a1088c946..53d2ec538ce8ab81311bfef50f6f60ad4f667e6e 100644 (file)
@@ -334,7 +334,7 @@ void ospf_redistribute_withdraw(struct ospf *ospf, uint8_t type,
 
                struct ospf_external_aggr_rt *aggr;
 
-               if (is_prefix_default(&ei->p)
+               if (is_default_prefix4(&ei->p)
                    && ospf->default_originate != DEFAULT_ORIGINATE_NONE)
                        continue;
 
@@ -862,7 +862,7 @@ static void ospf_handle_external_aggr_add(struct ospf *ospf)
                                        continue;
 
                                ei = rn->info;
-                               if (is_prefix_default(&ei->p))
+                               if (is_default_prefix4(&ei->p))
                                        continue;
 
                                /* Check the AS-external-LSA
index 6c1ac6761af8f525bb9035015be23a2e37302251..55bcaebd6ef95f8614b133df6aee611b57a19dfa 100644 (file)
@@ -98,14 +98,14 @@ struct external_info *ospf_external_info_check(struct ospf *ospf,
                int redist_on = 0;
 
                redist_on =
-                       is_prefix_default(&p)
+                       is_default_prefix4(&p)
                                ? vrf_bitmap_check(
-                                         zclient->default_information[AFI_IP],
-                                         ospf->vrf_id)
+                                       zclient->default_information[AFI_IP],
+                                       ospf->vrf_id)
                                : (zclient->mi_redist[AFI_IP][type].enabled
                                   || vrf_bitmap_check(
-                                             zclient->redist[AFI_IP][type],
-                                             ospf->vrf_id));
+                                          zclient->redist[AFI_IP][type],
+                                          ospf->vrf_id));
                // Pending: check for MI above.
                if (redist_on) {
                        ext_list = ospf->external[type];
@@ -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];
 
                for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
index c850df55bb5892a4799f601d5bd619810ed22a1b..c7c9fa2d6cbd351c945aa24734178aaeb47023de 100644 (file)
@@ -1551,8 +1551,8 @@ static void ospf_external_lsa_body_set(struct stream *s,
        stream_put_ipv4(s, mask.s_addr);
 
        /* If prefix is default, specify DEFAULT_ROUTE. */
-       type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
-       instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
+       type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
+       instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
 
        mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
                        ? ROUTEMAP_METRIC_TYPE(ei)
@@ -1947,17 +1947,6 @@ struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
        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. */
 struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
                                             struct external_info *ei)
@@ -2113,8 +2102,7 @@ void ospf_external_lsa_rid_change(struct ospf *ospf)
                                if (!ei)
                                        continue;
 
-                               if (is_prefix_default(
-                                           (struct prefix_ipv4 *)&ei->p))
+                               if (is_default_prefix4(&ei->p))
                                        continue;
 
                                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)) {
                        ei = rn->info;
                        if (ei) {
-                               if (!is_prefix_default(&ei->p)) {
+                               if (!is_default_prefix4(&ei->p)) {
                                        struct ospf_lsa *lsa;
                                        struct ospf_external_aggr_rt *aggr;
 
index 3808700cccc9e17b0b87ef55f18b8ee57477429b..d01dc720ba484daf5cd4c309547eff32d0445cc4 100644 (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 *,
                                 struct ospf_lsa *);
-extern int is_prefix_default(struct prefix_ipv4 *);
 
 extern int metric_type(struct ospf *, uint8_t, unsigned short);
 extern int metric_value(struct ospf *, uint8_t, unsigned short);
index db8e961b8b8b4436d38b1c3b4abf0bcbd1c357fc..946d19cfd4605f601d84bd1379535c8149457ed0 100644 (file)
@@ -10027,7 +10027,7 @@ DEFUN (ospf_external_route_aggregation,
 
        str2prefix_ipv4(argv[idx]->arg, &p);
 
-       if (is_prefix_default(&p)) {
+       if (is_default_prefix4(&p)) {
                vty_out(vty,
                        "Default address shouldn't be configured as summary address.\n");
                return CMD_SUCCESS;
@@ -10068,7 +10068,7 @@ DEFUN (no_ospf_external_route_aggregation,
 
        str2prefix_ipv4(argv[idx]->arg, &p);
 
-       if (is_prefix_default(&p)) {
+       if (is_default_prefix4(&p)) {
                vty_out(vty,
                        "Default address shouldn't be configured as summary address.\n");
                return CMD_SUCCESS;
@@ -10361,7 +10361,7 @@ DEFUN (ospf_external_route_aggregation_no_adrvertise,
 
        str2prefix_ipv4(argv[idx]->arg, &p);
 
-       if (is_prefix_default(&p)) {
+       if (is_default_prefix4(&p)) {
                vty_out(vty,
                        "Default address shouldn't be configured as summary address.\n");
                return CMD_SUCCESS;
@@ -10397,7 +10397,7 @@ DEFUN (no_ospf_external_route_aggregation_no_adrvertise,
 
        str2prefix_ipv4(argv[idx]->arg, &p);
 
-       if (is_prefix_default(&p)) {
+       if (is_default_prefix4(&p)) {
                vty_out(vty,
                        "Default address shouldn't be configured as summary address.\n");
                return CMD_SUCCESS;
index 017915e0ee3bbb57556388303d147fa94cb96e31..387bbc0ce9b276c86debadb7b71bc596f0cab046 100644 (file)
@@ -933,7 +933,7 @@ static int ospf_external_lsa_originate_check(struct ospf *ospf,
        }
 
        /* Take care of default-originate. */
-       if (is_prefix_default(&ei->p))
+       if (is_default_prefix4(&ei->p))
                if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) {
                        zlog_info(
                                "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 prefix_ipv4 *p = &ei->p;
        struct ospf_redist *red;
-       uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
-       unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
+       uint8_t type = is_default_prefix4(&ei->p) ? DEFAULT_ROUTE : ei->type;
+       unsigned short instance = is_default_prefix4(&ei->p) ? 0 : ei->instance;
        route_tag_t saved_tag = 0;
 
        /* 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.
         * Resolved this inconsistency by maintaining same route type.
         */
-       if (is_prefix_default(&p))
+       if (is_default_prefix4(&p))
                rt_type = DEFAULT_ROUTE;
 
        if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE))
@@ -1252,7 +1252,7 @@ static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS)
                        return 0;
                }
                if (ospf->router_id.s_addr != INADDR_ANY) {
-                       if (is_prefix_default(&p))
+                       if (is_default_prefix4(&p))
                                ospf_external_lsa_refresh_default(ospf);
                        else {
                                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,
                                                  p);
 
-                       if (is_prefix_default(&p))
+                       if (is_default_prefix4(&p))
                                ospf_external_lsa_refresh_default(ospf);
                        else
                                ospf_external_lsa_flush(ospf, rt_type, &p,
@@ -1471,7 +1471,7 @@ static int ospf_distribute_list_update_timer(struct thread *thread)
                                if (!ei)
                                        continue;
 
-                               if (is_prefix_default(&ei->p))
+                               if (is_default_prefix4(&ei->p))
                                        default_refresh = 1;
                                else {
                                        struct ospf_external_aggr_rt *aggr;