bgpd: Fix memory leak of some "show ip bgp neighbor" commands
sockunion_str2su() use is prone to memory leaks. Remove it's use all over
the code.
At least these commands leaked a sockunion union:
- show ip bgp vpnv4 ... routes
- show ip bgp ... received prefix-filter
Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
Signed-off-by: David Lamparter <equinox@diac24.net>
diff --git a/bgpd/bgp_mplsvpn.c b/bgpd/bgp_mplsvpn.c
index c1f1fbb..b0cf2a9 100644
--- a/bgpd/bgp_mplsvpn.c
+++ b/bgpd/bgp_mplsvpn.c
@@ -581,24 +581,25 @@
"Neighbor to display information about\n"
"Display routes learned from neighbor\n")
{
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
-
- su = sockunion_str2su (argv[0]);
- if (su == NULL)
+ int ret;
+
+ ret = str2sockunion (argv[0], &su);
+ if (ret < 0)
{
vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
- return CMD_WARNING;
+ return CMD_WARNING;
}
- peer = peer_lookup (NULL, su);
+ peer = peer_lookup (NULL, &su);
if (! peer || ! peer->afc[AFI_IP][SAFI_MPLS_VPN])
{
vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
return CMD_WARNING;
}
- return bgp_show_mpls_vpn (vty, NULL, bgp_show_type_neighbor, su, 0);
+ return bgp_show_mpls_vpn (vty, NULL, bgp_show_type_neighbor, &su, 0);
}
DEFUN (show_ip_bgp_vpnv4_rd_neighbor_routes,
@@ -615,7 +616,7 @@
"Display routes learned from neighbor\n")
{
int ret;
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
struct prefix_rd prd;
@@ -626,21 +627,21 @@
return CMD_WARNING;
}
- su = sockunion_str2su (argv[1]);
- if (su == NULL)
+ ret = str2sockunion (argv[1], &su);
+ if (ret < 0)
{
vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
- return CMD_WARNING;
+ return CMD_WARNING;
}
- peer = peer_lookup (NULL, su);
+ peer = peer_lookup (NULL, &su);
if (! peer || ! peer->afc[AFI_IP][SAFI_MPLS_VPN])
{
vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
return CMD_WARNING;
}
- return bgp_show_mpls_vpn (vty, &prd, bgp_show_type_neighbor, su, 0);
+ return bgp_show_mpls_vpn (vty, &prd, bgp_show_type_neighbor, &su, 0);
}
DEFUN (show_ip_bgp_vpnv4_all_neighbor_advertised_routes,
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 9f8d823..d96224d 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -10202,15 +10202,18 @@
"Display the prefixlist filter\n")
{
char name[BUFSIZ];
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
- int count;
+ int count, ret;
- su = sockunion_str2su (argv[0]);
- if (su == NULL)
- return CMD_WARNING;
+ ret = str2sockunion (argv[0], &su);
+ if (ret < 0)
+ {
+ vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
- peer = peer_lookup (NULL, su);
+ peer = peer_lookup (NULL, &su);
if (! peer)
return CMD_WARNING;
@@ -10241,15 +10244,18 @@
"Display the prefixlist filter\n")
{
char name[BUFSIZ];
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
- int count;
+ int count, ret;
- su = sockunion_str2su (argv[1]);
- if (su == NULL)
- return CMD_WARNING;
+ ret = str2sockunion (argv[1], &su);
+ if (ret < 0)
+ {
+ vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
- peer = peer_lookup (NULL, su);
+ peer = peer_lookup (NULL, &su);
if (! peer)
return CMD_WARNING;
@@ -10312,15 +10318,18 @@
"Display the prefixlist filter\n")
{
char name[BUFSIZ];
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
- int count;
+ int count, ret;
- su = sockunion_str2su (argv[0]);
- if (su == NULL)
- return CMD_WARNING;
+ ret = str2sockunion (argv[0], &su);
+ if (ret < 0)
+ {
+ vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
- peer = peer_lookup (NULL, su);
+ peer = peer_lookup (NULL, &su);
if (! peer)
return CMD_WARNING;
@@ -10394,10 +10403,10 @@
"Display the prefixlist filter\n")
{
char name[BUFSIZ];
- union sockunion *su;
+ union sockunion su;
struct peer *peer;
struct bgp *bgp;
- int count;
+ int count, ret;
/* BGP structure lookup. */
bgp = bgp_lookup_by_name (argv[0]);
@@ -10407,11 +10416,14 @@
return CMD_WARNING;
}
- su = sockunion_str2su (argv[1]);
- if (su == NULL)
- return CMD_WARNING;
+ ret = str2sockunion (argv[1], &su);
+ if (ret < 0)
+ {
+ vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
- peer = peer_lookup (bgp, su);
+ peer = peer_lookup (bgp, &su);
if (! peer)
return CMD_WARNING;
diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c
index fa645ac..f220400 100644
--- a/bgpd/bgp_routemap.c
+++ b/bgpd/bgp_routemap.c
@@ -111,7 +111,8 @@
void *object)
{
union sockunion *su;
- union sockunion *su2;
+ union sockunion su_def = { .sa.sa_family = AF_INET,
+ .sin.sin_addr.s_addr = INADDR_ANY };
struct peer_group *group;
struct peer *peer;
struct listnode *node, *nnode;
@@ -127,8 +128,7 @@
/* If su='0.0.0.0' (command 'match peer local'), and it's a NETWORK,
REDISTRIBUTE or DEFAULT_GENERATED route => return RMAP_MATCH */
- su2 = sockunion_str2su ("0.0.0.0");
- if ( sockunion_same (su, su2) )
+ if (sockunion_same (su, &su_def))
{
int ret;
if ( CHECK_FLAG (peer->rmap_type, PEER_RMAP_TYPE_NETWORK) ||
@@ -137,12 +137,9 @@
ret = RMAP_MATCH;
else
ret = RMAP_NOMATCH;
-
- sockunion_free (su2);
return ret;
}
- sockunion_free (su2);
-
+
if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
{
if (sockunion_same (su, &peer->su))
diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c
index bba1c7d..03746bd 100644
--- a/bgpd/bgp_vty.c
+++ b/bgpd/bgp_vty.c
@@ -2943,7 +2943,6 @@
const char *source_str)
{
struct peer *peer;
- union sockunion *su;
peer = peer_and_group_lookup_vty (vty, peer_str);
if (! peer)
@@ -2951,12 +2950,11 @@
if (source_str)
{
- su = sockunion_str2su (source_str);
- if (su)
- {
- peer_update_source_addr_set (peer, su);
- sockunion_free (su);
- }
+ union sockunion su;
+ int ret = str2sockunion (source_str, &su);
+
+ if (ret == 0)
+ peer_update_source_addr_set (peer, &su);
else
peer_update_source_if_set (peer, source_str);
}