]> git.puffer.fish Git - mirror/frr.git/commitdiff
eigrpd: Fix endianness issue in packets 4526/head
authorPawel Dembicki <p.dembicki@wb.com.pl>
Fri, 14 Jun 2019 10:28:00 +0000 (12:28 +0200)
committerPawel Dembicki <p.dembicki@wb.com.pl>
Fri, 14 Jun 2019 11:04:07 +0000 (13:04 +0200)
Net prefixes in eigrp update packets is created and read
without check host endianness.

This patch use ntohl and htonl to read and write ip prefix
from and to packet.

Tested: x86, powerpc

Signed-off-by: Pawel Dembicki <p.dembicki@wb.com.pl>
eigrpd/eigrp_packet.c
eigrpd/eigrp_structs.h

index bedaf15c4744f7d9f28a1730913b832e06f9a1b9..4efb91e4a033e2230592262c8bb92f5fd5be28a2 100644 (file)
@@ -1114,6 +1114,7 @@ static struct TLV_IPv4_Internal_type *eigrp_IPv4_InternalTLV_new(void)
 struct TLV_IPv4_Internal_type *eigrp_read_ipv4_tlv(struct stream *s)
 {
        struct TLV_IPv4_Internal_type *tlv;
+       uint32_t destination_tmp;
 
        tlv = eigrp_IPv4_InternalTLV_new();
 
@@ -1133,31 +1134,16 @@ struct TLV_IPv4_Internal_type *eigrp_read_ipv4_tlv(struct stream *s)
 
        tlv->prefix_length = stream_getc(s);
 
-       if (tlv->prefix_length <= 8) {
-               tlv->destination_part[0] = stream_getc(s);
-               tlv->destination.s_addr = (tlv->destination_part[0]);
-       } else if (tlv->prefix_length > 8 && tlv->prefix_length <= 16) {
-               tlv->destination_part[0] = stream_getc(s);
-               tlv->destination_part[1] = stream_getc(s);
-               tlv->destination.s_addr = ((tlv->destination_part[1] << 8)
-                                          + tlv->destination_part[0]);
-       } else if (tlv->prefix_length > 16 && tlv->prefix_length <= 24) {
-               tlv->destination_part[0] = stream_getc(s);
-               tlv->destination_part[1] = stream_getc(s);
-               tlv->destination_part[2] = stream_getc(s);
-               tlv->destination.s_addr = ((tlv->destination_part[2] << 16)
-                                          + (tlv->destination_part[1] << 8)
-                                          + tlv->destination_part[0]);
-       } else if (tlv->prefix_length > 24 && tlv->prefix_length <= 32) {
-               tlv->destination_part[0] = stream_getc(s);
-               tlv->destination_part[1] = stream_getc(s);
-               tlv->destination_part[2] = stream_getc(s);
-               tlv->destination_part[3] = stream_getc(s);
-               tlv->destination.s_addr = ((tlv->destination_part[3] << 24)
-                                          + (tlv->destination_part[2] << 16)
-                                          + (tlv->destination_part[1] << 8)
-                                          + tlv->destination_part[0]);
-       }
+       destination_tmp = stream_getc(s) << 24;
+       if (tlv->prefix_length > 8)
+               destination_tmp |= stream_getc(s) << 16;
+       if (tlv->prefix_length > 16)
+               destination_tmp |= stream_getc(s) << 8;
+       if (tlv->prefix_length > 24)
+               destination_tmp |= stream_getc(s);
+
+       tlv->destination.s_addr = htonl(destination_tmp);
+
        return tlv;
 }
 
@@ -1234,15 +1220,13 @@ uint16_t eigrp_add_internalTLV_to_stream(struct stream *s,
 
        stream_putc(s, pe->destination->prefixlen);
 
-       stream_putc(s, pe->destination->u.prefix4.s_addr & 0xFF);
+       stream_putc(s, (ntohl(pe->destination->u.prefix4.s_addr) >> 24) & 0xFF);
        if (pe->destination->prefixlen > 8)
-               stream_putc(s, (pe->destination->u.prefix4.s_addr >> 8) & 0xFF);
+               stream_putc(s, (ntohl(pe->destination->u.prefix4.s_addr) >> 16) & 0xFF);
        if (pe->destination->prefixlen > 16)
-               stream_putc(s,
-                           (pe->destination->u.prefix4.s_addr >> 16) & 0xFF);
+               stream_putc(s, (ntohl(pe->destination->u.prefix4.s_addr) >> 8) & 0xFF);
        if (pe->destination->prefixlen > 24)
-               stream_putc(s,
-                           (pe->destination->u.prefix4.s_addr >> 24) & 0xFF);
+               stream_putc(s, ntohl(pe->destination->u.prefix4.s_addr) & 0xFF);
 
        return length;
 }
index a78e5a53cf751918dab908df63a289bf01a4231c..1b9186f0114db654bf31f7f81b45b0be63282451 100644 (file)
@@ -409,7 +409,6 @@ struct TLV_IPv4_Internal_type {
 
        uint8_t prefix_length;
 
-       unsigned char destination_part[4];
        struct in_addr destination;
 } __attribute__((packed));