]> git.puffer.fish Git - mirror/frr.git/commitdiff
ospf6d: replace OSPF6_LSA_SIZE with ospf6_lsa_size 16055/head
authorAndrew Cooks <acooks.at.bda@gmail.com>
Tue, 28 May 2024 04:07:14 +0000 (14:07 +1000)
committerAndrew Cooks <acooks.at.bda@gmail.com>
Tue, 28 May 2024 04:07:14 +0000 (14:07 +1000)
Dropping the macro enables better compiler type checking.

The macro was not used consistently when reading the lsa size from the
header, so this change also aims to use the replacement inline function
consistently.

Keeping the inline function has (marginal) utility in that it ensures that
the endian conversion is consistently performed.

Signed-off-by: Andrew Cooks <acooks.at.bda@gmail.com>
ospf6d/ospf6_lsa.c
ospf6d/ospf6_lsa.h
ospf6d/ospf6_message.c
ospf6d/ospf6_snmp.c
ospf6d/ospf6_spf.c

index b907ccbc208e4b453bbe5f3d0923d304bfbb5eb8..017751825f6bd8b95518388400dbb1806af8b4d1 100644 (file)
@@ -201,10 +201,10 @@ int ospf6_lsa_is_differ(struct ospf6_lsa *lsa1, struct ospf6_lsa *lsa2)
                return 1;
 
        /* compare body */
-       if (ntohs(lsa1->header->length) != ntohs(lsa2->header->length))
+       if (ospf6_lsa_size(lsa1->header) != ospf6_lsa_size(lsa2->header))
                return 1;
 
-       len = ntohs(lsa1->header->length) - sizeof(struct ospf6_lsa_header);
+       len = ospf6_lsa_size(lsa1->header) - sizeof(struct ospf6_lsa_header);
        return memcmp(lsa1->header + 1, lsa2->header + 1, len);
 }
 
@@ -214,7 +214,7 @@ int ospf6_lsa_is_changed(struct ospf6_lsa *lsa1, struct ospf6_lsa *lsa2)
 
        if (OSPF6_LSA_IS_MAXAGE(lsa1) ^ OSPF6_LSA_IS_MAXAGE(lsa2))
                return 1;
-       if (ntohs(lsa1->header->length) != ntohs(lsa2->header->length))
+       if (ospf6_lsa_size(lsa1->header) != ospf6_lsa_size(lsa2->header))
                return 1;
        /* Going beyond LSA headers to compare the payload only makes sense,
         * when both LSAs aren't header-only. */
@@ -228,7 +228,7 @@ int ospf6_lsa_is_changed(struct ospf6_lsa *lsa1, struct ospf6_lsa *lsa2)
        if (CHECK_FLAG(lsa1->flag, OSPF6_LSA_HEADERONLY))
                return 0;
 
-       length = OSPF6_LSA_SIZE(lsa1->header) - sizeof(struct ospf6_lsa_header);
+       length = ospf6_lsa_size(lsa1->header) - sizeof(struct ospf6_lsa_header);
        /* Once upper layer verifies LSAs received, length underrun should
         * become a warning. */
        if (length <= 0)
@@ -613,7 +613,7 @@ void ospf6_lsa_show_internal(struct vty *vty, struct ospf6_lsa *lsa,
                json_object_int_add(json_obj, "checksum",
                                    ntohs(lsa->header->checksum));
                json_object_int_add(json_obj, "length",
-                                   ntohs(lsa->header->length));
+                                   ospf6_lsa_size(lsa->header));
                json_object_int_add(json_obj, "flag", lsa->flag);
                json_object_int_add(json_obj, "lock", lsa->lock);
                json_object_int_add(json_obj, "reTxCount", lsa->retrans_count);
@@ -630,7 +630,7 @@ void ospf6_lsa_show_internal(struct vty *vty, struct ospf6_lsa *lsa,
                        (unsigned long)ntohl(lsa->header->seqnum));
                vty_out(vty, "CheckSum: %#06hx Length: %hu\n",
                        ntohs(lsa->header->checksum),
-                       ntohs(lsa->header->length));
+                       ospf6_lsa_size(lsa->header));
                vty_out(vty, "Flag: %x \n", lsa->flag);
                vty_out(vty, "Lock: %d \n", lsa->lock);
                vty_out(vty, "ReTx Count: %d\n", lsa->retrans_count);
@@ -720,7 +720,7 @@ struct ospf6_lsa *ospf6_lsa_create(struct ospf6_lsa_header *header)
        uint16_t lsa_size = 0;
 
        /* size of the entire LSA */
-       lsa_size = ntohs(header->length); /* XXX vulnerable */
+       lsa_size = ospf6_lsa_size(header); /* XXX vulnerable */
 
        lsa = ospf6_lsa_alloc(lsa_size);
 
@@ -947,7 +947,7 @@ unsigned short ospf6_lsa_checksum(struct ospf6_lsa_header *lsa_header)
                buffer - (uint8_t *)&lsa_header->age; /* should be 2 */
 
        /* Skip the AGE field */
-       uint16_t len = ntohs(lsa_header->length) - type_offset;
+       uint16_t len = ospf6_lsa_size(lsa_header) - type_offset;
 
        /* Checksum offset starts from "type" field, not the beginning of the
           lsa_header struct. The offset is 14, rather than 16. */
@@ -963,7 +963,7 @@ int ospf6_lsa_checksum_valid(struct ospf6_lsa_header *lsa_header)
                buffer - (uint8_t *)&lsa_header->age; /* should be 2 */
 
        /* Skip the AGE field */
-       uint16_t len = ntohs(lsa_header->length) - type_offset;
+       uint16_t len = ospf6_lsa_size(lsa_header) - type_offset;
 
        return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
                == 0);
index 714cd5883d5e8bee941d9ae5ad0102c411e6ac71..4fc2f0dd18898ba62613e7749e13400075d5050b 100644 (file)
@@ -87,8 +87,6 @@ struct ospf6_lsa_header {
        uint16_t length;     /* LSA length */
 };
 
-#define OSPF6_LSA_SIZE(h) (ntohs(((struct ospf6_lsa_header *)(h))->length))
-
 static inline char *ospf6_lsa_header_end(struct ospf6_lsa_header *header)
 {
        return (char *)header + sizeof(struct ospf6_lsa_header);
@@ -99,6 +97,11 @@ static inline char *ospf6_lsa_end(struct ospf6_lsa_header *header)
        return (char *)header + ntohs(header->length);
 }
 
+static inline uint16_t ospf6_lsa_size(struct ospf6_lsa_header *header)
+{
+       return ntohs(header->length);
+}
+
 #define OSPF6_LSA_IS_TYPE(t, L)                                                \
        ((L)->header->type == htons(OSPF6_LSTYPE_##t) ? 1 : 0)
 #define OSPF6_LSA_IS_SAME(L1, L2)                                              \
index d13799c0e81d84f0df002131ce58a7fb874b8f16..a6ee8d8b0162ec11c6c907aff43b2ff1d9829f99 100644 (file)
@@ -221,12 +221,12 @@ void ospf6_lsupdate_print(struct ospf6_header *oh, int action)
             && action == OSPF6_ACTION_RECV)
            || (IS_OSPF6_DEBUG_MESSAGE(oh->type, SEND)
                && action == OSPF6_ACTION_SEND)) {
-
-               for (p = (char *)((caddr_t)lsupdate
-                                 + sizeof(struct ospf6_lsupdate));
-                    p < OSPF6_MESSAGE_END(oh)
-                    && p + OSPF6_LSA_SIZE(p) <= OSPF6_MESSAGE_END(oh);
-                    p += OSPF6_LSA_SIZE(p)) {
+               for (p = (char *)((caddr_t)lsupdate +
+                                 sizeof(struct ospf6_lsupdate));
+                    p < OSPF6_MESSAGE_END(oh) &&
+                    p + ospf6_lsa_size((struct ospf6_lsa_header *)p) <=
+                            OSPF6_MESSAGE_END(oh);
+                    p += ospf6_lsa_size((struct ospf6_lsa_header *)p)) {
                        ospf6_lsa_header_print_raw(
                                (struct ospf6_lsa_header *)p);
                }
@@ -1414,7 +1414,7 @@ ospf6_lsaseq_examin(struct ospf6_lsa_header *lsah, /* start of buffered data */
                        return MSG_NG;
                }
                /* save on ntohs() calls here and in the LSA validator */
-               lsalen = OSPF6_LSA_SIZE(lsah);
+               lsalen = ospf6_lsa_size(lsah);
                if (lsalen < OSPF6_LSA_HEADER_SIZE) {
                        zlog_warn(
                                "%s: malformed LSA header #%u, declared length is %u B",
@@ -1646,9 +1646,10 @@ static void ospf6_lsupdate_recv(struct in6_addr *src, struct in6_addr *dst,
 
        /* Process LSAs */
        for (p = (char *)((caddr_t)lsupdate + sizeof(struct ospf6_lsupdate));
-            p < OSPF6_MESSAGE_END(oh)
-            && p + OSPF6_LSA_SIZE(p) <= OSPF6_MESSAGE_END(oh);
-            p += OSPF6_LSA_SIZE(p)) {
+            p < OSPF6_MESSAGE_END(oh) &&
+            p + ospf6_lsa_size((struct ospf6_lsa_header *)p) <=
+                    OSPF6_MESSAGE_END(oh);
+            p += ospf6_lsa_size((struct ospf6_lsa_header *)p)) {
                ospf6_receive_lsa(on, (struct ospf6_lsa_header *)p);
        }
 
@@ -2702,7 +2703,7 @@ static uint16_t ospf6_make_lsupdate_list(struct ospf6_neighbor *on,
        stream_forward_endp((*op)->s, OSPF6_LS_UPD_MIN_SIZE);
 
        for (ALL_LSDB(on->lsupdate_list, lsa, lsanext)) {
-               if ((length + OSPF6_LSA_SIZE(lsa->header) + OSPF6_HEADER_SIZE) >
+               if ((length + ospf6_lsa_size(lsa->header) + OSPF6_HEADER_SIZE) >
                    ospf6_packet_max(on->ospf6_if)) {
                        ospf6_fill_header(on->ospf6_if, (*op)->s,
                                          length + OSPF6_HEADER_SIZE);
@@ -2719,9 +2720,9 @@ static uint16_t ospf6_make_lsupdate_list(struct ospf6_neighbor *on,
                        stream_forward_endp((*op)->s, OSPF6_LS_UPD_MIN_SIZE);
                }
                ospf6_lsa_age_update_to_send(lsa, on->ospf6_if->transdelay);
-               stream_put((*op)->s, lsa->header, OSPF6_LSA_SIZE(lsa->header));
+               stream_put((*op)->s, lsa->header, ospf6_lsa_size(lsa->header));
                (*lsa_cnt)++;
-               length += OSPF6_LSA_SIZE(lsa->header);
+               length += ospf6_lsa_size(lsa->header);
                assert(lsa->lock == 2);
                ospf6_lsdb_remove(lsa, on->lsupdate_list);
        }
@@ -2739,7 +2740,7 @@ static uint16_t ospf6_make_ls_retrans_list(struct ospf6_neighbor *on,
        stream_forward_endp((*op)->s, OSPF6_LS_UPD_MIN_SIZE);
 
        for (ALL_LSDB(on->retrans_list, lsa, lsanext)) {
-               if ((length + OSPF6_LSA_SIZE(lsa->header) + OSPF6_HEADER_SIZE) >
+               if ((length + ospf6_lsa_size(lsa->header) + OSPF6_HEADER_SIZE) >
                    ospf6_packet_max(on->ospf6_if)) {
                        ospf6_fill_header(on->ospf6_if, (*op)->s,
                                          length + OSPF6_HEADER_SIZE);
@@ -2763,9 +2764,9 @@ static uint16_t ospf6_make_ls_retrans_list(struct ospf6_neighbor *on,
                        stream_forward_endp((*op)->s, OSPF6_LS_UPD_MIN_SIZE);
                }
                ospf6_lsa_age_update_to_send(lsa, on->ospf6_if->transdelay);
-               stream_put((*op)->s, lsa->header, OSPF6_LSA_SIZE(lsa->header));
+               stream_put((*op)->s, lsa->header, ospf6_lsa_size(lsa->header));
                (*lsa_cnt)++;
-               length += OSPF6_LSA_SIZE(lsa->header);
+               length += ospf6_lsa_size(lsa->header);
        }
        return length;
 }
@@ -2849,9 +2850,9 @@ int ospf6_lsupdate_send_neighbor_now(struct ospf6_neighbor *on,
        /* skip over fixed header */
        stream_forward_endp(op->s, OSPF6_LS_UPD_MIN_SIZE);
        ospf6_lsa_age_update_to_send(lsa, on->ospf6_if->transdelay);
-       stream_put(op->s, lsa->header, OSPF6_LSA_SIZE(lsa->header));
-       length = OSPF6_HEADER_SIZE + OSPF6_LS_UPD_MIN_SIZE
-                + OSPF6_LSA_SIZE(lsa->header);
+       stream_put(op->s, lsa->header, ospf6_lsa_size(lsa->header));
+       length = OSPF6_HEADER_SIZE + OSPF6_LS_UPD_MIN_SIZE +
+                ospf6_lsa_size(lsa->header);
        ospf6_fill_header(on->ospf6_if, op->s, length);
        ospf6_fill_lsupdate_header(op->s, 1);
        op->length = length;
@@ -2877,7 +2878,7 @@ static uint16_t ospf6_make_lsupdate_interface(struct ospf6_interface *oi,
        stream_forward_endp((*op)->s, OSPF6_LS_UPD_MIN_SIZE);
 
        for (ALL_LSDB(oi->lsupdate_list, lsa, lsanext)) {
-               if (length + OSPF6_LSA_SIZE(lsa->header) + OSPF6_HEADER_SIZE >
+               if (length + ospf6_lsa_size(lsa->header) + OSPF6_HEADER_SIZE >
                    ospf6_packet_max(oi)) {
                        ospf6_fill_header(oi, (*op)->s,
                                          length + OSPF6_HEADER_SIZE);
@@ -2895,9 +2896,9 @@ static uint16_t ospf6_make_lsupdate_interface(struct ospf6_interface *oi,
                }
 
                ospf6_lsa_age_update_to_send(lsa, oi->transdelay);
-               stream_put((*op)->s, lsa->header, OSPF6_LSA_SIZE(lsa->header));
+               stream_put((*op)->s, lsa->header, ospf6_lsa_size(lsa->header));
                (*lsa_cnt)++;
-               length += OSPF6_LSA_SIZE(lsa->header);
+               length += ospf6_lsa_size(lsa->header);
 
                assert(lsa->lock == 2);
                ospf6_lsdb_remove(lsa, oi->lsupdate_list);
index 17cdcdae8ec4eb4d9292dce044cce991321e47a8..36864d2a7de7b9d7ca3f2edf4334162f4f99b904 100644 (file)
@@ -1017,7 +1017,7 @@ static uint8_t *ospfv3WwLsdbEntry(struct variable *v, oid *name, size_t *length,
        case OSPFv3WWLSDBCHECKSUM:
                return SNMP_INTEGER(ntohs(lsa->header->checksum));
        case OSPFv3WWLSDBADVERTISEMENT:
-               *var_len = ntohs(lsa->header->length);
+               *var_len = ospf6_lsa_size(lsa->header);
                return (uint8_t *)lsa->header;
        case OSPFv3WWLSDBTYPEKNOWN:
                return SNMP_INTEGER(OSPF6_LSA_IS_KNOWN(lsa->header->type)
index 7d6dcf8e36bb6022f8f9d0fcc5610791f1525b41..7879dae8d7cc8aa18adce941a9f52dcd07417354 100644 (file)
@@ -187,7 +187,7 @@ static struct ospf6_lsa *ospf6_lsdesc_lsa(caddr_t lsdesc,
                inet_ntop(AF_INET, &adv_router, abuf, sizeof(abuf));
                if (lsa)
                        zlog_debug("  Link to: %s len %u, V %s", lsa->name,
-                                  ntohs(lsa->header->length), v->name);
+                                  ospf6_lsa_size(lsa->header), v->name);
                else
                        zlog_debug("  Link to: [%s Id:%s Adv:%s] No LSA , V %s",
                                   ospf6_lstype_name(type), ibuf, abuf,
@@ -1011,7 +1011,7 @@ struct ospf6_lsa *ospf6_create_single_router_lsa(struct ospf6_area *area,
                        continue;
                }
                lsa_header = rtr_lsa->header;
-               total_lsa_length += (ntohs(lsa_header->length) - lsa_length);
+               total_lsa_length += (ospf6_lsa_size(lsa_header) - lsa_length);
                num_lsa++;
                rtr_lsa = ospf6_lsdb_next(end, rtr_lsa);
        }
@@ -1044,11 +1044,11 @@ struct ospf6_lsa *ospf6_create_single_router_lsa(struct ospf6_area *area,
        if (!OSPF6_LSA_IS_MAXAGE(rtr_lsa)) {
                /* Append first Link State ID LSA */
                lsa_header = rtr_lsa->header;
-               memcpy(new_header, lsa_header, ntohs(lsa_header->length));
+               memcpy(new_header, lsa_header, ospf6_lsa_size(lsa_header));
                /* Assign new lsa length as aggregated length. */
                ((struct ospf6_lsa_header *)new_header)->length =
                        htons(total_lsa_length);
-               new_header += ntohs(lsa_header->length);
+               new_header += ospf6_lsa_size(lsa_header);
                num_lsa--;
        }
 
@@ -1066,17 +1066,16 @@ struct ospf6_lsa *ospf6_create_single_router_lsa(struct ospf6_area *area,
                        lsd = ospf6_lsa_header_end(rtr_lsa->header) + 4;
                        interface_id = ROUTER_LSDESC_GET_IFID(lsd);
                        inet_ntop(AF_INET, &interface_id, ifbuf, sizeof(ifbuf));
-                       zlog_debug(
-                               "%s: Next Router LSA %s to aggreat with len %u interface_id %s",
-                               __func__, rtr_lsa->name,
-                               ntohs(lsa_header->length), ifbuf);
+                       zlog_debug("%s: Next Router LSA %s to aggreat with len %u interface_id %s",
+                                  __func__, rtr_lsa->name,
+                                  ospf6_lsa_size(lsa_header), ifbuf);
                }
 
                /* Append Next Link State ID LSA */
                lsa_header = rtr_lsa->header;
                memcpy(new_header, (ospf6_lsa_header_end(rtr_lsa->header) + 4),
-                      (ntohs(lsa_header->length) - lsa_length));
-               new_header += (ntohs(lsa_header->length) - lsa_length);
+                      (ospf6_lsa_size(lsa_header) - lsa_length));
+               new_header += (ospf6_lsa_size(lsa_header) - lsa_length);
                num_lsa--;
 
                rtr_lsa = ospf6_lsdb_next(end, rtr_lsa);
@@ -1091,8 +1090,8 @@ struct ospf6_lsa *ospf6_create_single_router_lsa(struct ospf6_area *area,
        if (IS_OSPF6_DEBUG_SPF(PROCESS))
                zlog_debug("%s: LSA %s id %u type 0%x len %u num_lsa %u",
                           __func__, lsa->name, ntohl(lsa->header->id),
-                          ntohs(lsa->header->type), ntohs(lsa->header->length),
-                          num_lsa);
+                          ntohs(lsa->header->type),
+                          ospf6_lsa_size(lsa->header), num_lsa);
 
        return lsa;
 }