]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: Fix memory leak of some "show ip bgp neighbor" commands
authorJorge Boncompte [DTI2] <jorge@dti2.net>
Tue, 10 Apr 2012 14:57:24 +0000 (16:57 +0200)
committerDavid Lamparter <equinox@diac24.net>
Wed, 2 May 2012 15:03:27 +0000 (17:03 +0200)
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>
bgpd/bgp_mplsvpn.c
bgpd/bgp_route.c
bgpd/bgp_routemap.c
bgpd/bgp_vty.c
lib/sockunion.c
lib/sockunion.h

index c1f1fbb37d0cd0402911e0320c95f6bac6a48fc7..b0cf2a988f95c4b87d4f3220548f08d4caf21026 100644 (file)
@@ -581,24 +581,25 @@ DEFUN (show_ip_bgp_vpnv4_all_neighbor_routes,
        "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 @@ DEFUN (show_ip_bgp_vpnv4_rd_neighbor_routes,
        "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 @@ DEFUN (show_ip_bgp_vpnv4_rd_neighbor_routes,
       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,
index 9f8d82327fabba5065394b8173c38ad3504fe5f1..d96224db43a89fd2bbb3be2c7b6c4f33fdde5eb7 100644 (file)
@@ -10202,15 +10202,18 @@ DEFUN (show_ip_bgp_neighbor_received_prefix_filter,
        "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 @@ DEFUN (show_ip_bgp_ipv4_neighbor_received_prefix_filter,
        "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 @@ DEFUN (show_bgp_neighbor_received_prefix_filter,
        "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 @@ DEFUN (show_bgp_view_neighbor_received_prefix_filter,
        "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 @@ DEFUN (show_bgp_view_neighbor_received_prefix_filter,
          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;
 
index fa645ac41139a8c8ec2ad461b4c10ca2ff2d5c34..f2204003299874ba56155e1d94045b3f01334849 100644 (file)
@@ -111,7 +111,8 @@ route_match_peer (void *rule, struct prefix *prefix, route_map_object_t type,
       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 @@ route_match_peer (void *rule, struct prefix *prefix, route_map_object_t type,
 
       /* 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 @@ route_match_peer (void *rule, struct prefix *prefix, route_map_object_t type,
             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))
index bba1c7de6ec355364c70da0a6c7ff280322e66e1..03746bdd092b5e06bb7b092ad9db1c43efd17b86 100644 (file)
@@ -2943,7 +2943,6 @@ peer_update_source_vty (struct vty *vty, const char *peer_str,
                         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 @@ peer_update_source_vty (struct vty *vty, const char *peer_str,
 
   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);
     }
index cfb6f9c5a6921042620c34ff7c11d1e3edfaca52..4c2837b12d1fb8ee8e39fd695124ee9eb75dc7f9 100644 (file)
@@ -174,39 +174,6 @@ sockunion2str (union sockunion *su, char *buf, size_t len)
   return NULL;
 }
 
-union sockunion *
-sockunion_str2su (const char *str)
-{
-  int ret;
-  union sockunion *su;
-
-  su = XCALLOC (MTYPE_SOCKUNION, sizeof (union sockunion));
-
-  ret = inet_pton (AF_INET, str, &su->sin.sin_addr);
-  if (ret > 0)                 /* Valid IPv4 address format. */
-    {
-      su->sin.sin_family = AF_INET;
-#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
-      su->sin.sin_len = sizeof(struct sockaddr_in);
-#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
-      return su;
-    }
-#ifdef HAVE_IPV6
-  ret = inet_pton (AF_INET6, str, &su->sin6.sin6_addr);
-  if (ret > 0)                 /* Valid IPv6 address format. */
-    {
-      su->sin6.sin6_family = AF_INET6;
-#ifdef SIN6_LEN
-      su->sin6.sin6_len = sizeof(struct sockaddr_in6);
-#endif /* SIN6_LEN */
-      return su;
-    }
-#endif /* HAVE_IPV6 */
-
-  XFREE (MTYPE_SOCKUNION, su);
-  return NULL;
-}
-
 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
 static void
 sockunion_normalise_mapped (union sockunion *su)
index 8b06058688229009ddc89ee89f700232dab8a42e..180b2b4d2cecade6a591b8344f2fff04e7ce0b05 100644 (file)
@@ -94,7 +94,6 @@ extern const char *sockunion2str (union sockunion *, char *, size_t);
 extern int sockunion_cmp (union sockunion *, union sockunion *);
 extern int sockunion_same (union sockunion *, union sockunion *);
 
-extern union sockunion *sockunion_str2su (const char *str);
 extern struct in_addr sockunion_get_in_addr (union sockunion *su);
 extern int sockunion_accept (int sock, union sockunion *);
 extern int sockunion_stream_socket (union sockunion *);