Skip to content

Commit

Permalink
vrrp: Add setting IP_FREEBIND/IPV6_FREEBIND socket option
Browse files Browse the repository at this point in the history
This allows creating and configuring unicast sockets before the configured
source address is added to the system.

Signed-off-by: Quentin Armitage <[email protected]>
  • Loading branch information
pqarmitage committed Dec 9, 2024
1 parent 6b3c80f commit b48f005
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,15 @@ AC_CHECK_DECLS([ETHERTYPE_IPV6], [],
],
[[#include <net/ethernet.h>]])
# IPV6_FREEBIND - added Linux v4.15
AS_IF([test .$enable_vrrp != .no],
[
AC_CHECK_DECLS([IPV6_FREEBIND],
[add_system_opt([IPV6_FREEBIND])],
[],
[[#include <linux/in6.h>]])
])
# IPV6_MULTICAST_ALL - added Linux v4.20
AC_CHECK_DECLS([IPV6_MULTICAST_ALL],
[
Expand Down
10 changes: 10 additions & 0 deletions keepalived/vrrp/vrrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,16 @@ open_vrrp_read_socket(sa_family_t family, int proto, const interface_t *ifp,
}
#endif

/* Allow binding even if the address doesn't exist yet */
#if !HAVE_DECL_IPV6_FREEBIND
if (family == AF_INET6) {
if (setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &on, sizeof on))
log_message(LOG_INFO, "IPV6_TRANSPARENT failed %d - %m", errno);
} else
#endif
if (setsockopt(fd, family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6, family == AF_INET ? IP_FREEBIND : IPV6_FREEBIND, &on, sizeof on))
log_message(LOG_INFO, "IP%s_FREEBIND failed %d - %m", family == AF_INET ? "" : "V6", errno);

/* Bind to the local unicast address */
if (bind(fd, PTR_CAST_CONST(struct sockaddr, unicast_src), unicast_src->ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))) {
log_message(LOG_INFO, "bind unicast_src %s failed %d - %m", inet_sockaddrtos(unicast_src), errno);
Expand Down

0 comments on commit b48f005

Please sign in to comment.