ospfd: make reason string production safer

Use to-string functions for GR message codes instead of raw
string array indexing; the values used can come in packets
and are not validated.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
This commit is contained in:
Mark Stapp 2020-10-14 12:51:52 -04:00
parent 6431621e4e
commit d05d5280e6
3 changed files with 66 additions and 35 deletions

View File

@ -50,26 +50,26 @@
#include "ospfd/ospf_ism.h" #include "ospfd/ospf_ism.h"
#include "ospfd/ospf_gr_helper.h" #include "ospfd/ospf_gr_helper.h"
const char *ospf_exit_reason_desc[] = { static const char * const ospf_exit_reason_desc[] = {
"Unknown reason", "Unknown reason",
"Helper inprogress", "Helper inprogress",
"Topology Change", "Topology Change",
"Grace timer expairy", "Grace timer expiry",
"Successful graceful restart", "Successful graceful restart",
}; };
const char *ospf_restart_reason_desc[] = { static const char * const ospf_restart_reason_desc[] = {
"Unknown restart", "Unknown restart",
"Software restart", "Software restart",
"Software reload/upgrade", "Software reload/upgrade",
"Switch to redundant control processor", "Switch to redundant control processor",
}; };
const char *ospf_rejected_reason_desc[] = { static const char * const ospf_rejected_reason_desc[] = {
"Unknown reason", "Unknown reason",
"Helper support disabled", "Helper support disabled",
"Neighbour is not in FULL state", "Neighbour is not in FULL state",
"Supports only planned restart but received for unplanned", "Supports only planned restart but received unplanned",
"Topo change due to change in lsa rxmt list", "Topo change due to change in lsa rxmt list",
"LSA age is more than Grace interval", "LSA age is more than Grace interval",
}; };
@ -117,6 +117,39 @@ static void ospf_enable_rtr_hash_destroy(struct ospf *ospf)
ospf->enable_rtr_list = NULL; ospf->enable_rtr_list = NULL;
} }
/*
* GR exit reason strings
*/
const char *ospf_exit_reason2str(unsigned int reason)
{
if (reason < array_size(ospf_exit_reason_desc))
return(ospf_exit_reason_desc[reason]);
else
return "Invalid reason";
}
/*
* GR restart reason strings
*/
const char *ospf_restart_reason2str(unsigned int reason)
{
if (reason < array_size(ospf_restart_reason_desc))
return(ospf_restart_reason_desc[reason]);
else
return "Invalid reason";
}
/*
* GR rejected reason strings
*/
const char *ospf_rejected_reason2str(unsigned int reason)
{
if (reason < array_size(ospf_rejected_reason_desc))
return(ospf_rejected_reason_desc[reason]);
else
return "Invalid reason";
}
/* /*
* Initialize GR helper config data structures. * Initialize GR helper config data structures.
* *
@ -306,7 +339,8 @@ int ospf_process_grace_lsa(struct ospf *ospf, struct ospf_lsa *lsa,
zlog_debug( zlog_debug(
"%s, Grace LSA received from %s, grace interval:%u, restartreason :%s", "%s, Grace LSA received from %s, grace interval:%u, restartreason :%s",
__PRETTY_FUNCTION__, inet_ntoa(restart_addr), __PRETTY_FUNCTION__, inet_ntoa(restart_addr),
grace_interval, ospf_restart_reason_desc[restart_reason]); grace_interval,
ospf_restart_reason2str(restart_reason));
/* Incase of broadcast links, if RESTARTER is DR_OTHER, /* Incase of broadcast links, if RESTARTER is DR_OTHER,
* grace LSA might be received from DR, so need to get * grace LSA might be received from DR, so need to get
@ -598,7 +632,7 @@ void ospf_gr_helper_exit(struct ospf_neighbor *nbr,
if (IS_DEBUG_OSPF_GR_HELPER) if (IS_DEBUG_OSPF_GR_HELPER)
zlog_debug("%s, Exiting from HELPER support to %s, due to %s", zlog_debug("%s, Exiting from HELPER support to %s, due to %s",
__PRETTY_FUNCTION__, inet_ntoa(nbr->src), __PRETTY_FUNCTION__, inet_ntoa(nbr->src),
ospf_exit_reason_desc[reason]); ospf_exit_reason2str(reason));
/* Reset helper status*/ /* Reset helper status*/
nbr->gr_helper_info.gr_helper_status = OSPF_GR_NOT_HELPER; nbr->gr_helper_info.gr_helper_status = OSPF_GR_NOT_HELPER;
@ -948,7 +982,7 @@ static void show_ospf_grace_lsa_info(struct vty *vty, struct ospf_lsa *lsa)
sum += TLV_SIZE(tlvh); sum += TLV_SIZE(tlvh);
vty_out(vty, " Restart reason:%s\n", vty_out(vty, " Restart reason:%s\n",
ospf_restart_reason_desc[grReason->reason]); ospf_restart_reason2str(grReason->reason));
break; break;
case RESTARTER_IP_ADDR_TYPE: case RESTARTER_IP_ADDR_TYPE:
restartAddr = (struct grace_tlv_restart_addr *)tlvh; restartAddr = (struct grace_tlv_restart_addr *)tlvh;

View File

@ -152,9 +152,9 @@ struct advRtr {
#define OSPF_GR_FAILURE 0 #define OSPF_GR_FAILURE 0
#define OSPF_GR_INVALID -1 #define OSPF_GR_INVALID -1
extern const char *ospf_exit_reason_desc[]; const char *ospf_exit_reason2str(unsigned int reason);
extern const char *ospf_restart_reason_desc[]; const char *ospf_restart_reason2str(unsigned int reason);
extern const char *ospf_rejected_reason_desc[]; const char *ospf_rejected_reason2str(unsigned int reason);
extern void ospf_gr_helper_init(struct ospf *ospf); extern void ospf_gr_helper_init(struct ospf *ospf);
extern void ospf_gr_helper_stop(struct ospf *ospf); extern void ospf_gr_helper_stop(struct ospf *ospf);

View File

@ -5132,9 +5132,8 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
" Graceful Restart grace period time: %d (seconds).\n", " Graceful Restart grace period time: %d (seconds).\n",
nbr->gr_helper_info.recvd_grace_period); nbr->gr_helper_info.recvd_grace_period);
vty_out(vty, " Graceful Restart reason: %s.\n", vty_out(vty, " Graceful Restart reason: %s.\n",
ospf_restart_reason_desc ospf_restart_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info.gr_restart_reason));
.gr_restart_reason]);
} else { } else {
vty_out(vty, vty_out(vty,
" Graceful Restart HELPER Status : None\n"); " Graceful Restart HELPER Status : None\n");
@ -5143,15 +5142,14 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
if (nbr->gr_helper_info.rejected_reason if (nbr->gr_helper_info.rejected_reason
!= OSPF_HELPER_REJECTED_NONE) != OSPF_HELPER_REJECTED_NONE)
vty_out(vty, " Helper rejected reason: %s.\n", vty_out(vty, " Helper rejected reason: %s.\n",
ospf_rejected_reason_desc ospf_rejected_reason2str(
[nbr->gr_helper_info.rejected_reason]); nbr->gr_helper_info.rejected_reason));
if (nbr->gr_helper_info.helper_exit_reason if (nbr->gr_helper_info.helper_exit_reason
!= OSPF_GR_HELPER_EXIT_NONE) != OSPF_GR_HELPER_EXIT_NONE)
vty_out(vty, " Last helper exit reason: %s.\n\n", vty_out(vty, " Last helper exit reason: %s.\n\n",
ospf_exit_reason_desc ospf_exit_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info.helper_exit_reason));
.helper_exit_reason]);
else else
vty_out(vty, "\n"); vty_out(vty, "\n");
} else { } else {
@ -5165,25 +5163,24 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
nbr->gr_helper_info.recvd_grace_period); nbr->gr_helper_info.recvd_grace_period);
json_object_string_add( json_object_string_add(
json_neigh, "grRestartReason", json_neigh, "grRestartReason",
ospf_restart_reason_desc ospf_restart_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info.gr_restart_reason));
.gr_restart_reason]);
} }
if (nbr->gr_helper_info.rejected_reason if (nbr->gr_helper_info.rejected_reason
!= OSPF_HELPER_REJECTED_NONE) != OSPF_HELPER_REJECTED_NONE)
json_object_string_add( json_object_string_add(
json_neigh, "helperRejectReason", json_neigh, "helperRejectReason",
ospf_rejected_reason_desc ospf_rejected_reason2str(
[nbr->gr_helper_info.rejected_reason]); nbr->gr_helper_info.rejected_reason));
if (nbr->gr_helper_info.helper_exit_reason if (nbr->gr_helper_info.helper_exit_reason
!= OSPF_GR_HELPER_EXIT_NONE) != OSPF_GR_HELPER_EXIT_NONE)
json_object_string_add( json_object_string_add(
json_neigh, "helperExitReason", json_neigh, "helperExitReason",
ospf_exit_reason_desc ospf_exit_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info
.helper_exit_reason]); .helper_exit_reason));
} }
ospf_bfd_show_info(vty, nbr->bfd_info, json_neigh, use_json, 0); ospf_bfd_show_info(vty, nbr->bfd_info, json_neigh, use_json, 0);
@ -9308,7 +9305,7 @@ static int ospf_show_gr_helper_details(struct vty *vty, struct ospf *ospf,
if (ospf->last_exit_reason != OSPF_GR_HELPER_EXIT_NONE) { if (ospf->last_exit_reason != OSPF_GR_HELPER_EXIT_NONE) {
vty_out(vty, " Last Helper exit Reason :%s\n", vty_out(vty, " Last Helper exit Reason :%s\n",
ospf_exit_reason_desc[ospf->last_exit_reason]); ospf_exit_reason2str(ospf->last_exit_reason));
} }
if (ospf->active_restarter_cnt) if (ospf->active_restarter_cnt)
@ -9337,7 +9334,7 @@ static int ospf_show_gr_helper_details(struct vty *vty, struct ospf *ospf,
if (ospf->last_exit_reason != OSPF_GR_HELPER_EXIT_NONE) if (ospf->last_exit_reason != OSPF_GR_HELPER_EXIT_NONE)
json_object_string_add( json_object_string_add(
json_vrf, "LastExitReason", json_vrf, "LastExitReason",
ospf_exit_reason_desc[ospf->last_exit_reason]); ospf_exit_reason2str(ospf->last_exit_reason));
if (ospf->active_restarter_cnt) if (ospf->active_restarter_cnt)
json_object_int_add(json_vrf, "activeRestarterCnt", json_object_int_add(json_vrf, "activeRestarterCnt",
@ -9402,9 +9399,9 @@ static int ospf_show_gr_helper_details(struct vty *vty, struct ospf *ospf,
.t_grace_timer)); .t_grace_timer));
vty_out(vty, vty_out(vty,
" Graceful Restart reason: %s.\n\n", " Graceful Restart reason: %s.\n\n",
ospf_restart_reason_desc ospf_restart_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info
.gr_restart_reason]); .gr_restart_reason));
cnt++; cnt++;
} else { } else {
json_neigh = json_object_new_object(); json_neigh = json_object_new_object();
@ -9432,9 +9429,9 @@ static int ospf_show_gr_helper_details(struct vty *vty, struct ospf *ospf,
.t_grace_timer)); .t_grace_timer));
json_object_string_add( json_object_string_add(
json_neigh, "restartReason", json_neigh, "restartReason",
ospf_restart_reason_desc ospf_restart_reason2str(
[nbr->gr_helper_info nbr->gr_helper_info
.gr_restart_reason]); .gr_restart_reason));
json_object_object_add( json_object_object_add(
json_neighbors, json_neighbors,
inet_ntoa(nbr->src), inet_ntoa(nbr->src),