From 4b7ca8dfb7c253dfa89bdea44a5f4455cf7e3053 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Sun, 1 Nov 2015 20:13:56 +0000 Subject: Don't open vrrp_send_socket if address family is wrong open_vrrp_send_socket was opening a socket, and then checking that the address family was valid. Checking that the address family is valid at the beginning of the function streamlines the code. Signed-off-by: Quentin Armitage (cherry picked from commit 1df8dd54b2b1f71f2acc98da75dbc0b960c45ee5) --- keepalived/vrrp/vrrp.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/keepalived/vrrp/vrrp.c b/keepalived/vrrp/vrrp.c index 70e2f23..4babe2c 100644 --- a/keepalived/vrrp/vrrp.c +++ b/keepalived/vrrp/vrrp.c @@ -1433,6 +1433,12 @@ open_vrrp_send_socket(sa_family_t family, int proto, int idx, int unicast) interface_t *ifp; int fd = -1; + if (family != AF_INET && family != AF_INET6) { + log_message(LOG_INFO, "cant open raw socket. unknown family=%d" + , family); + return -1; + } + /* Retreive interface_t */ ifp = if_get_by_ifindex(idx); @@ -1448,31 +1454,23 @@ open_vrrp_send_socket(sa_family_t family, int proto, int idx, int unicast) if_setsockopt_hdrincl(&fd); if (unicast) if_setsockopt_bindtodevice(&fd, ifp); - else { - if_setsockopt_mcast_if(family, &fd, ifp); - if_setsockopt_mcast_loop(family, &fd); - } - if_setsockopt_priority(&fd); - if (fd < 0) - return -1; } else if (family == AF_INET6) { /* Set v6 related */ if_setsockopt_ipv6_checksum(&fd); - if (!unicast) { + if (!unicast) if_setsockopt_mcast_hops(family, &fd); - if_setsockopt_mcast_if(family, &fd, ifp); - if_setsockopt_mcast_loop(family, &fd); - } - if_setsockopt_priority(&fd); - if (fd < 0) - return -1; - } else { - log_message(LOG_INFO, "cant open raw socket. unknow family=%d" - , family); - close(fd); - return -1; } + if (!unicast) { + if_setsockopt_mcast_if(family, &fd, ifp); + if_setsockopt_mcast_loop(family, &fd); + } + + if_setsockopt_priority(&fd); + + if (fd < 0) + return -1; + return fd; } -- 1.7.12.1