]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_encap_tlv.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / bgpd / bgp_encap_tlv.c
index abf2032799e4f9ba15d873fc10902c034ef1c67a..fde11970ceabc91242f8fd286488218c553b3d38 100644 (file)
@@ -1,20 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright 2015, LabN Consulting, L.L.C.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
- *
  */
 
 #include <zebra.h>
@@ -23,6 +9,7 @@
 #include "memory.h"
 #include "prefix.h"
 #include "filter.h"
+#include "stream.h"
 
 #include "bgpd.h"
 #include "bgp_attr.h"
@@ -47,7 +34,7 @@ static struct bgp_attr_encap_subtlv *subtlv_encode_encap_l2tpv3_over_ip(
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
        new->length = total;
@@ -72,7 +59,7 @@ subtlv_encode_encap_gre(struct bgp_tea_subtlv_encap_gre_key *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
        new->length = total;
@@ -95,7 +82,7 @@ subtlv_encode_encap_pbb(struct bgp_tea_subtlv_encap_pbb *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
        new->length = total;
@@ -128,7 +115,7 @@ subtlv_encode_proto_type(struct bgp_tea_subtlv_proto_type *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE;
        new->length = total;
@@ -150,7 +137,7 @@ subtlv_encode_color(struct bgp_tea_subtlv_color *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_COLOR;
        new->length = total;
@@ -182,7 +169,7 @@ subtlv_encode_ipsec_ta(struct bgp_tea_subtlv_ipsec_ta *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA;
        new->length = total;
@@ -206,18 +193,18 @@ subtlv_encode_remote_endpoint(struct bgp_tea_subtlv_remote_endpoint *st)
        assert(total <= 0xff);
 
        new = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+                     sizeof(struct bgp_attr_encap_subtlv) + total);
        assert(new);
        new->type = BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT;
        new->length = total;
        p = new->value;
        if (st->family == AF_INET) {
-               memcpy(p, &(st->ip_address.v4.s_addr), 4);
-               p += 4;
+               memcpy(p, &(st->ip_address.v4.s_addr), IPV4_MAX_BYTELEN);
+               p += IPV4_MAX_BYTELEN;
        } else {
                assert(st->family == AF_INET6);
-               memcpy(p, &(st->ip_address.v6.s6_addr), 16);
-               p += 16;
+               memcpy(p, &(st->ip_address.v6.s6_addr), IPV6_MAX_BYTELEN);
+               p += IPV6_MAX_BYTELEN;
        }
        memcpy(p, &(st->as4), 4);
        return new;
@@ -238,7 +225,7 @@ subtlv_encode_remote_endpoint(struct bgp_tea_subtlv_remote_endpoint *st)
                        if (last) {                                            \
                                last->next = new;                              \
                        } else {                                               \
-                               extra->encap_subtlvs = new;                    \
+                               attr->encap_subtlvs = new;                     \
                        }                                                      \
                        last = new;                                            \
                }                                                              \
@@ -248,14 +235,13 @@ void bgp_encap_type_l2tpv3overip_to_tlv(
        struct bgp_encap_type_l2tpv3_over_ip *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_L2TPV3_OVER_IP;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_L2TPV3_OVER_IP;
 
        assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
 
@@ -272,14 +258,13 @@ void bgp_encap_type_gre_to_tlv(
        struct bgp_encap_type_gre *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_GRE;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_GRE;
 
        ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_gre, st_encap);
        ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type,
@@ -293,14 +278,13 @@ void bgp_encap_type_ip_in_ip_to_tlv(
        struct bgp_encap_type_ip_in_ip *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_IP_IN_IP;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_IP_IN_IP;
 
        ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type,
                   st_proto);
@@ -314,14 +298,13 @@ void bgp_encap_type_transmit_tunnel_endpoint(
                *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_TRANSMIT_TUNNEL_ENDPOINT;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_TRANSMIT_TUNNEL_ENDPOINT;
 
        /* no subtlvs for this type */
 }
@@ -330,14 +313,13 @@ void bgp_encap_type_ipsec_in_tunnel_mode_to_tlv(
        struct bgp_encap_type_ipsec_in_tunnel_mode *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_IPSEC_IN_TUNNEL_MODE;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_IPSEC_IN_TUNNEL_MODE;
 
        ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
                   st_ipsec_ta);
@@ -348,14 +330,13 @@ void bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
                *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype =
+       attr->encap_tunneltype =
                BGP_ENCAP_TYPE_IP_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
 
        ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
@@ -367,14 +348,13 @@ void bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
                *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype =
+       attr->encap_tunneltype =
                BGP_ENCAP_TYPE_MPLS_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
 
        ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
@@ -385,14 +365,13 @@ void bgp_encap_type_pbb_to_tlv(
        struct bgp_encap_type_pbb *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *last;
 
        /* advance to last subtlv */
-       for (last = extra->encap_subtlvs; last && last->next; last = last->next)
+       for (last = attr->encap_subtlvs; last && last->next; last = last->next)
                ;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_PBB;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_PBB;
 
        assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
        ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_pbb, st_encap);
@@ -402,18 +381,16 @@ void bgp_encap_type_vxlan_to_tlv(
        struct bgp_encap_type_vxlan *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
        struct bgp_attr_encap_subtlv *tlv;
        uint32_t vnid;
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN;
 
        if (bet == NULL || !bet->vnid)
                return;
-       if (extra->encap_subtlvs)
-               XFREE(MTYPE_ENCAP_TLV, extra->encap_subtlvs);
+       XFREE(MTYPE_ENCAP_TLV, attr->encap_subtlvs);
        tlv = XCALLOC(MTYPE_ENCAP_TLV,
-                     sizeof(struct bgp_attr_encap_subtlv) - 1 + 12);
+                     sizeof(struct bgp_attr_encap_subtlv) + 12);
        tlv->type = 1; /* encapsulation type */
        tlv->length = 12;
        if (bet->vnid) {
@@ -424,7 +401,7 @@ void bgp_encap_type_vxlan_to_tlv(
                char *ptr = (char *)&tlv->value + 4;
                memcpy(ptr, bet->mac_address, 6);
        }
-       extra->encap_subtlvs = tlv;
+       attr->encap_subtlvs = tlv;
        return;
 }
 
@@ -432,9 +409,7 @@ void bgp_encap_type_nvgre_to_tlv(
        struct bgp_encap_type_nvgre *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
-
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_NVGRE;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_NVGRE;
 }
 
 void bgp_encap_type_mpls_to_tlv(
@@ -448,27 +423,23 @@ void bgp_encap_type_mpls_in_gre_to_tlv(
        struct bgp_encap_type_mpls_in_gre *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
-
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_GRE;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_GRE;
 }
 
 void bgp_encap_type_vxlan_gpe_to_tlv(
        struct bgp_encap_type_vxlan_gpe *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN_GPE;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN_GPE;
 }
 
 void bgp_encap_type_mpls_in_udp_to_tlv(
        struct bgp_encap_type_mpls_in_udp *bet, /* input structure */
        struct attr *attr)
 {
-       struct attr_extra *extra = bgp_attr_extra_get(attr);
 
-       extra->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_UDP;
+       attr->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_UDP;
 }
 
 
@@ -486,8 +457,7 @@ static int subtlv_decode_encap_l2tpv3_over_ip(
                return -1;
        }
 
-       st->sessionid = (subtlv->value[0] << 24) | (subtlv->value[1] << 16)
-                       | (subtlv->value[2] << 8) | subtlv->value[3];
+       ptr_get_be32(subtlv->value, &st->sessionid);
        st->cookie_length = subtlv->length - 4;
        if (st->cookie_length > sizeof(st->cookie)) {
                zlog_debug("%s, subtlv length %d is greater than %d", __func__,
@@ -507,8 +477,7 @@ static int subtlv_decode_encap_gre(struct bgp_attr_encap_subtlv *subtlv,
                           subtlv->length);
                return -1;
        }
-       st->gre_key = (subtlv->value[0] << 24) | (subtlv->value[1] << 16)
-                     | (subtlv->value[2] << 8) | subtlv->value[3];
+       ptr_get_be32(subtlv->value, &st->gre_key);
        return 0;
 }
 
@@ -561,8 +530,7 @@ static int subtlv_decode_color(struct bgp_attr_encap_subtlv *subtlv,
                           __func__);
                return -1;
        }
-       st->color = (subtlv->value[4] << 24) | (subtlv->value[5] << 16)
-                   | (subtlv->value[6] << 8) | subtlv->value[7];
+       ptr_get_be32(subtlv->value + 4, &st->color);
        return 0;
 }
 
@@ -596,16 +564,15 @@ subtlv_decode_remote_endpoint(struct bgp_attr_encap_subtlv *subtlv,
        }
        if (subtlv->length == 8) {
                st->family = AF_INET;
-               st->ip_address.v4.s_addr =
-                       ((subtlv->value[0] << 24) | (subtlv->value[1] << 16)
-                        | (subtlv->value[2] << 8) | subtlv->value[3]);
+               memcpy(&st->ip_address.v4.s_addr, subtlv->value,
+                      IPV4_MAX_BYTELEN);
        } else {
                st->family = AF_INET6;
-               memcpy(&(st->ip_address.v6.s6_addr), subtlv->value, 16);
+               memcpy(&(st->ip_address.v6.s6_addr), subtlv->value,
+                      IPV6_MAX_BYTELEN);
        }
        i = subtlv->length - 4;
-       st->as4 = ((subtlv->value[i] << 24) | (subtlv->value[i + 1] << 16)
-                  | (subtlv->value[i + 2] << 8) | subtlv->value[i + 3]);
+       ptr_get_be32(subtlv->value + i, &st->as4);
        return 0;
 }