From 06b9f471ab94bea8ef8423e92beeb4c58ba488e2 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 11 Dec 2017 12:38:26 -0500 Subject: [PATCH] bgpd: Fixup buffer sizes for prefix_rd2str The prefix_rd2str uses a buffer size of RD_ADDRSTRLEN. Modify the code to consistently use this for all of BGP. Signed-off-by: Donald Sharp --- bgpd/bgp_evpn_vty.c | 16 +++++----- bgpd/bgp_route.c | 27 ++++++++-------- bgpd/rfapi/bgp_rfapi_cfg.c | 63 +++++++++++--------------------------- bgpd/rfapi/rfapi.c | 16 +++++----- bgpd/rfapi/rfapi_import.c | 51 +++++++++++++++--------------- bgpd/rfapi/rfapi_rib.c | 20 ++++++------ bgpd/rfapi/rfapi_vty.c | 4 +-- 7 files changed, 83 insertions(+), 114 deletions(-) diff --git a/bgpd/bgp_evpn_vty.c b/bgpd/bgp_evpn_vty.c index 70f7e78976..f473b4604a 100644 --- a/bgpd/bgp_evpn_vty.c +++ b/bgpd/bgp_evpn_vty.c @@ -226,7 +226,7 @@ static void bgp_evpn_show_route_header(struct vty *vty, struct bgp *bgp, static void display_vni(struct vty *vty, struct bgpevpn *vpn, json_object *json) { - char buf1[INET6_ADDRSTRLEN]; + char buf1[RD_ADDRSTRLEN]; char *ecom_str; struct listnode *node, *nnode; struct ecommunity *ecom; @@ -241,7 +241,7 @@ static void display_vni(struct vty *vty, struct bgpevpn *vpn, json_object *json) is_vni_live(vpn) ? "Yes" : "No"); json_object_string_add( json, "rd", - prefix_rd2str(&vpn->prd, buf1, RD_ADDRSTRLEN)); + prefix_rd2str(&vpn->prd, buf1, sizeof(buf1))); json_object_string_add(json, "originatorIp", inet_ntoa(vpn->originator_ip)); json_object_string_add(json, "advertiseGatewayMacip", @@ -253,7 +253,7 @@ static void display_vni(struct vty *vty, struct bgpevpn *vpn, json_object *json) vty_out(vty, "\n"); vty_out(vty, " RD: %s\n", - prefix_rd2str(&vpn->prd, buf1, RD_ADDRSTRLEN)); + prefix_rd2str(&vpn->prd, buf1, sizeof(buf1))); vty_out(vty, " Originator IP: %s\n", inet_ntoa(vpn->originator_ip)); vty_out(vty, " Advertise-gw-macip : %s\n", @@ -419,7 +419,7 @@ static void show_vni_entry(struct hash_backet *backet, void *args[]) json_object *json_export_rtl; struct bgpevpn *vpn = (struct bgpevpn *)backet->data; char buf1[10]; - char buf2[INET6_ADDRSTRLEN]; + char buf2[RD_ADDRSTRLEN]; char rt_buf[25]; char *ecom_str; struct listnode *node, *nnode; @@ -446,11 +446,11 @@ static void show_vni_entry(struct hash_backet *backet, void *args[]) inet_ntoa(vpn->originator_ip)); json_object_string_add( json_vni, "rd", - prefix_rd2str(&vpn->prd, buf2, RD_ADDRSTRLEN)); + prefix_rd2str(&vpn->prd, buf2, sizeof(buf2))); } else { vty_out(vty, "%-1s %-10u %-15s %-21s", buf1, vpn->vni, inet_ntoa(vpn->originator_ip), - prefix_rd2str(&vpn->prd, buf2, RD_ADDRSTRLEN)); + prefix_rd2str(&vpn->prd, buf2, sizeof(buf2))); } for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) { @@ -2093,7 +2093,7 @@ static void evpn_unset_advertise_all_vni(struct bgp *bgp) static void write_vni_config(struct vty *vty, struct bgpevpn *vpn) { - char buf1[INET6_ADDRSTRLEN]; + char buf1[RD_ADDRSTRLEN]; char *ecom_str; struct listnode *node, *nnode; struct ecommunity *ecom; @@ -2102,7 +2102,7 @@ static void write_vni_config(struct vty *vty, struct bgpevpn *vpn) vty_out(vty, " vni %d\n", vpn->vni); if (is_rd_configured(vpn)) vty_out(vty, " rd %s\n", - prefix_rd2str(&vpn->prd, buf1, RD_ADDRSTRLEN)); + prefix_rd2str(&vpn->prd, buf1, sizeof(buf1))); if (is_import_rt_configured(vpn)) { for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 8774e3e806..2323572488 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -7388,7 +7388,7 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct prefix *p, vty_out(vty, " Imported from %s:%s\n", prefix_rd2str( (struct prefix_rd *)&prn->p, - buf1, RD_ADDRSTRLEN), + buf1, sizeof(buf1)), buf2); } } @@ -7601,7 +7601,7 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct prefix *p, json_peer, "routerId", inet_ntop(AF_INET, &binfo->peer->remote_id, buf1, - BUFSIZ)); + sizeof(buf1))); if (binfo->peer->hostname) json_object_string_add( @@ -7655,7 +7655,7 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct prefix *p, inet_ntop( AF_INET, &binfo->peer->remote_id, - buf1, BUFSIZ)); + buf1, sizeof(buf1))); } } @@ -8447,13 +8447,10 @@ int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi, continue; if (rn->info != NULL) { struct prefix_rd prd; - char rd[BUFSIZ]; + char rd[RD_ADDRSTRLEN]; memcpy(&prd, &(rn->p), sizeof(struct prefix_rd)); - if (prefix_rd2str(&prd, rd, BUFSIZ) == NULL) - sprintf(rd, - "Unknown Type: %u", - decode_rd_type(prd.val)); + prefix_rd2str(&prd, rd, sizeof(rd)); bgp_show_table(vty, bgp, safi, rn->info, type, output_arg, use_json, rd, next == NULL, @@ -8539,7 +8536,7 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp, struct prefix *p; struct peer *peer; struct listnode *node, *nnode; - char buf1[INET6_ADDRSTRLEN]; + char buf1[RD_ADDRSTRLEN]; char buf2[INET6_ADDRSTRLEN]; #if defined(HAVE_CUMULUS) char buf3[EVPN_ROUTE_STRLEN]; @@ -8573,7 +8570,7 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp, #if defined(HAVE_CUMULUS) if (safi == SAFI_EVPN) vty_out(vty, "BGP routing table entry for %s%s%s\n", - prd ? prefix_rd2str(prd, buf1, RD_ADDRSTRLEN) + prd ? prefix_rd2str(prd, buf1, sizeof(buf1)) : "", prd ? ":" : "", bgp_evpn_route2str((struct prefix_evpn *)p, @@ -8582,7 +8579,7 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp, vty_out(vty, "BGP routing table entry for %s%s%s/%d\n", ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP) ? prefix_rd2str(prd, buf1, - RD_ADDRSTRLEN) + sizeof(buf1)) : ""), safi == SAFI_MPLS_VPN ? ":" : "", inet_ntop(p->family, &p->u.prefix, buf2, @@ -8597,8 +8594,8 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp, vty_out(vty, "BGP routing table entry for %s%s%s/%d\n", ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) - ? prefix_rd2str(prd, buf1, RD_ADDRSTRLEN) - : ""), + ? prefix_rd2str(prd, buf1, sizeof(buf1)) + : ""), ((safi == SAFI_MPLS_VPN) || (safi == SAFI_EVPN)) ? ":" : "", buf2, p->prefixlen); @@ -11144,7 +11141,7 @@ static void bgp_config_write_network_vpn(struct vty *vty, struct bgp *bgp, prd = (struct prefix_rd *)&prn->p; /* "network" configuration display. */ - prefix_rd2str(prd, rdbuf, RD_ADDRSTRLEN); + prefix_rd2str(prd, rdbuf, sizeof(rdbuf)); label = decode_label(&bgp_static->label); vty_out(vty, " network %s/%d rd %s", @@ -11201,7 +11198,7 @@ static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp, prd = (struct prefix_rd *)&prn->p; /* "network" configuration display. */ - prefix_rd2str(prd, rdbuf, RD_ADDRSTRLEN); + prefix_rd2str(prd, rdbuf, sizeof(rdbuf)); if (p->u.prefix_evpn.route_type == 5) { char local_buf[PREFIX_STRLEN]; uint8_t family = IS_EVPN_PREFIX_IPADDR_V4((struct prefix_evpn *)p) diff --git a/bgpd/rfapi/bgp_rfapi_cfg.c b/bgpd/rfapi/bgp_rfapi_cfg.c index 5eb4b78341..3fbba66774 100644 --- a/bgpd/rfapi/bgp_rfapi_cfg.c +++ b/bgpd/rfapi/bgp_rfapi_cfg.c @@ -3884,8 +3884,7 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) } if (rfg->rd.prefixlen) { - char buf[BUFSIZ]; - buf[0] = buf[BUFSIZ - 1] = 0; + char buf[RD_ADDRSTRLEN]; if (AF_UNIX == rfg->rd.family) { @@ -3898,18 +3897,10 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) vty_out(vty, " rd auto:nh:%d\n", value); - } else { - - if (!prefix_rd2str(&rfg->rd, buf, - BUFSIZ) - || !buf[0] || buf[BUFSIZ - 1]) { - - vty_out(vty, - "!Error: Can't convert rd\n"); - } else { - vty_out(vty, " rd %s\n", buf); - } - } + } else + vty_out(vty, " rd %s\n", + prefix_rd2str(&rfg->rd, buf, + sizeof(buf))); } if (rfg->rt_import_list && rfg->rt_export_list @@ -4107,8 +4098,7 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) vty_out(vty, " vnc defaults\n"); if (hc->default_rd.prefixlen) { - char buf[BUFSIZ]; - buf[0] = buf[BUFSIZ - 1] = 0; + char buf[RD_ADDRSTRLEN]; if (AF_UNIX == hc->default_rd.family) { uint16_t value = 0; @@ -4121,18 +4111,11 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) vty_out(vty, " rd auto:vn:%d\n", value); - } else { - - if (!prefix_rd2str(&hc->default_rd, buf, - BUFSIZ) - || !buf[0] || buf[BUFSIZ - 1]) { - - vty_out(vty, - "!Error: Can't convert rd\n"); - } else { - vty_out(vty, " rd %s\n", buf); - } - } + } else + vty_out(vty, " rd %s\n", + prefix_rd2str(&hc->default_rd, + buf, + sizeof(buf))); } if (hc->default_response_lifetime) { vty_out(vty, " response-lifetime "); @@ -4206,8 +4189,7 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) if (rfg->rd.prefixlen) { - char buf[BUFSIZ]; - buf[0] = buf[BUFSIZ - 1] = 0; + char buf[RD_ADDRSTRLEN]; if (AF_UNIX == rfg->rd.family) { @@ -4222,21 +4204,12 @@ int bgp_rfapi_cfg_write(struct vty *vty, struct bgp *bgp) " rd auto:vn:%d\n", value); - } else { - - if (!prefix_rd2str(&rfg->rd, - buf, BUFSIZ) - || !buf[0] - || buf[BUFSIZ - 1]) { - - vty_out(vty, - "!Error: Can't convert rd\n"); - } else { - vty_out(vty, - " rd %s\n", - buf); - } - } + } else + vty_out(vty, + " rd %s\n", + prefix_rd2str(&rfg->rd, + buf, + sizeof(buf))); } if (rfg->flags & RFAPI_RFG_RESPONSE_LIFETIME) { vty_out(vty, " response-lifetime "); diff --git a/bgpd/rfapi/rfapi.c b/bgpd/rfapi/rfapi.c index b1b23ca068..5ba7a96a8f 100644 --- a/bgpd/rfapi/rfapi.c +++ b/bgpd/rfapi/rfapi.c @@ -364,11 +364,10 @@ void del_vnc_route(struct rfapi_descriptor *rfd, struct bgp_node *bn; struct bgp_info *bi; char buf[PREFIX_STRLEN]; - char buf2[BUFSIZ]; + char buf2[RD_ADDRSTRLEN]; struct prefix_rd prd0; prefix2str(p, buf, sizeof(buf)); - prefix_rd2str(prd, buf2, sizeof(buf2)); afi = family2afi(p->family); assert(afi == AFI_IP || afi == AFI_IP6); @@ -383,7 +382,8 @@ void del_vnc_route(struct rfapi_descriptor *rfd, vnc_zlog_debug_verbose( "%s: peer=%p, prefix=%s, prd=%s afi=%d, safi=%d bn=%p, bn->info=%p", - __func__, peer, buf, buf2, afi, safi, bn, + __func__, peer, buf, + prefix_rd2str(prd, buf2, sizeof(buf2)), afi, safi, bn, (bn ? bn->info : NULL)); for (bi = (bn ? bn->info : NULL); bi; bi = bi->next) { @@ -589,7 +589,7 @@ void add_vnc_route(struct rfapi_descriptor *rfd, /* cookie, VPN UN addr, peer */ struct bgp_attr_encap_subtlv *encaptlv; char buf[PREFIX_STRLEN]; - char buf2[BUFSIZ]; + char buf2[RD_ADDRSTRLEN]; #if 0 /* unused? */ struct prefix pfx_buf; #endif @@ -3831,12 +3831,10 @@ int rfapi_set_autord_from_vn(struct prefix_rd *rd, struct rfapi_ip_addr *vn) 4); /* low order 4 bytes */ } { - char buf[BUFSIZ]; - buf[0] = 0; - prefix_rd2str(rd, buf, BUFSIZ); - buf[BUFSIZ - 1] = 0; + char buf[RD_ADDRSTRLEN]; + vnc_zlog_debug_verbose("%s: auto-RD is set to %s", __func__, - buf); + prefix_rd2str(rd, buf, sizeof(buf))); } return 0; } diff --git a/bgpd/rfapi/rfapi_import.c b/bgpd/rfapi/rfapi_import.c index 00bcc81fe0..8727c5d5ea 100644 --- a/bgpd/rfapi/rfapi_import.c +++ b/bgpd/rfapi/rfapi_import.c @@ -41,6 +41,7 @@ #include "bgpd/bgp_route.h" #include "bgpd/bgp_mplsvpn.h" /* prefix_rd2str() */ #include "bgpd/bgp_vnc_types.h" +#include "bgpd/bgp_rd.h" #include "bgpd/rfapi/rfapi.h" #include "bgpd/rfapi/bgp_rfapi_cfg.h" @@ -2132,10 +2133,12 @@ static void rfapiItBiIndexAdd(struct route_node *rn, /* Import table VPN node */ assert(bi->extra); { - char buf[BUFSIZ]; - prefix_rd2str(&bi->extra->vnc.import.rd, buf, BUFSIZ); + char buf[RD_ADDRSTRLEN]; + vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__, - bi, bi->peer, buf); + bi, bi->peer, + prefix_rd2str(&bi->extra->vnc.import.rd, + buf, sizeof(buf))); } sl = RFAPI_RDINDEX_W_ALLOC(rn); @@ -2169,18 +2172,15 @@ static void rfapiItBiIndexDump(struct route_node *rn) for (rc = skiplist_next(sl, (void **)&k, (void **)&v, &cursor); !rc; rc = skiplist_next(sl, (void **)&k, (void **)&v, &cursor)) { - char buf[BUFSIZ]; + char buf[RD_ADDRSTRLEN]; char buf_aux_pfx[PREFIX_STRLEN]; - prefix_rd2str(&k->extra->vnc.import.rd, buf, BUFSIZ); - buf_aux_pfx[0] = 0; + prefix_rd2str(&k->extra->vnc.import.rd, buf, sizeof(buf)); if (k->extra->vnc.import.aux_prefix.family) { prefix2str(&k->extra->vnc.import.aux_prefix, - buf_aux_pfx, sizeof(buf)); - } else { + buf_aux_pfx, sizeof(buf_aux_pfx)); + } else strncpy(buf_aux_pfx, "(none)", PREFIX_STRLEN); - buf_aux_pfx[PREFIX_STRLEN - 1] = 0; - } vnc_zlog_debug_verbose("bi %p, peer %p, rd %s, aux_prefix %s", k, k->peer, buf, buf_aux_pfx); @@ -2204,20 +2204,19 @@ static struct bgp_info *rfapiItBiIndexSearch( #if DEBUG_BI_SEARCH { - char buf[BUFSIZ]; + char buf[RD_ADDRSTRLEN]; char buf_aux_pfx[PREFIX_STRLEN]; - prefix_rd2str(prd, buf, BUFSIZ); if (aux_prefix) { prefix2str(aux_prefix, buf_aux_pfx, sizeof(buf_aux_pfx)); - } else { - strncpy(buf_aux_pfx, "(nil)", BUFSIZ - 1); - buf_aux_pfx[BUFSIZ - 1] = 0; - } + } else + strncpy(buf_aux_pfx, "(nil)", sizeof(buf_aux_pfx)); vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s", - __func__, buf, peer, buf_aux_pfx); + __func__, + prefix_rd2str(prd, buf, sizeof(buf)), + peer, buf_aux_pfx); rfapiItBiIndexDump(rn); } #endif @@ -2232,12 +2231,14 @@ static struct bgp_info *rfapiItBiIndexSearch( bi_result = bi_result->next) { #if DEBUG_BI_SEARCH { - char buf[BUFSIZ]; - prefix_rd2str(&bi_result->extra->vnc.import.rd, - buf, BUFSIZ); + char buf[RD_ADDRSTRLEN]; + vnc_zlog_debug_verbose( "%s: bi has prd=%s, peer=%p", __func__, - buf, bi_result->peer); + prefix_rd2str(&bi_result->extra->vnc.import.rd, + buf, + sizeof(buf)), + bi_result->peer); } #endif if (peer == bi_result->peer @@ -2300,10 +2301,12 @@ static void rfapiItBiIndexDel(struct route_node *rn, /* Import table VPN node */ int rc; { - char buf[BUFSIZ]; - prefix_rd2str(&bi->extra->vnc.import.rd, buf, BUFSIZ); + char buf[RD_ADDRSTRLEN]; + vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__, - bi, bi->peer, buf); + bi, bi->peer, + prefix_rd2str(&bi->extra->vnc.import.rd, + buf, sizeof(buf))); } sl = RFAPI_RDINDEX(rn); diff --git a/bgpd/rfapi/rfapi_rib.c b/bgpd/rfapi/rfapi_rib.c index 66bc6fb8f2..bd79518bfd 100644 --- a/bgpd/rfapi/rfapi_rib.c +++ b/bgpd/rfapi/rfapi_rib.c @@ -1123,7 +1123,7 @@ static void process_pending_node(struct bgp *bgp, struct rfapi_descriptor *rfd, } else { - char buf_rd[BUFSIZ]; + char buf_rd[RD_ADDRSTRLEN]; /* not found: add new route to RIB */ ori = rfapi_info_new(); @@ -1402,13 +1402,14 @@ callback: ri->last_sent_time = rfapi_time(NULL); #if DEBUG_RIB_SL_RD { - char buf_rd[BUFSIZ]; - prefix_rd2str(&ri->rk.rd, - buf_rd, - sizeof(buf_rd)); + char buf_rd[RD_ADDRSTRLEN]; + vnc_zlog_debug_verbose( "%s: move route to recently deleted list, rd=%s", - __func__, buf_rd); + __func__, + prefix_rd2str(&ri->rk.rd, + buf_rd, + sizeof(buf_rd))); } #endif @@ -2293,7 +2294,7 @@ static int print_rib_sl(int (*fp)(void *, const char *, ...), struct vty *vty, char str_lifetime[BUFSIZ]; char str_age[BUFSIZ]; char *p; - char str_rd[BUFSIZ]; + char str_rd[RD_ADDRSTRLEN]; ++routes_displayed; @@ -2323,11 +2324,10 @@ static int print_rib_sl(int (*fp)(void *, const char *, ...), struct vty *vty, str_rd[0] = 0; /* start empty */ #if DEBUG_RIB_SL_RD - str_rd[0] = ' '; - prefix_rd2str(&ri->rk.rd, str_rd + 1, BUFSIZ - 1); + prefix_rd2str(&ri->rk.rd, str_rd, sizeof(str_rd)); #endif - fp(out, " %c %-20s %-15s %-15s %-4u %-8s %-8s%s\n", + fp(out, " %c %-20s %-15s %-15s %-4u %-8s %-8s %s\n", deleted ? 'r' : ' ', *printedprefix ? "" : str_pfx, str_vn, str_un, ri->cost, str_lifetime, str_age, str_rd); diff --git a/bgpd/rfapi/rfapi_vty.c b/bgpd/rfapi/rfapi_vty.c index 922140f4e3..fb7d8beab3 100644 --- a/bgpd/rfapi/rfapi_vty.c +++ b/bgpd/rfapi/rfapi_vty.c @@ -1527,11 +1527,9 @@ void rfapiPrintRfapiIpPrefix(void *stream, struct rfapi_ip_prefix *p) void rfapiPrintRd(struct vty *vty, struct prefix_rd *prd) { - char buf[BUFSIZ]; + char buf[RD_ADDRSTRLEN]; - buf[0] = 0; prefix_rd2str(prd, buf, BUFSIZ); - buf[BUFSIZ - 1] = 0; vty_out(vty, "%s", buf); } -- 2.39.5