From 5736139d4a9192c9ba296d2bddc5bbbd025dfd7f Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Sat, 12 Nov 2016 18:39:51 -0200 Subject: pimd/zebra: fix setting of IP_MULTICAST_LOOP on OpenBSD Linux, FreeBSD and NetBSD (and possibly others too) accept both uint8_t and int for the IP_MULTICAST_LOOP sockoption. OpenBSD, in the other hand, accepts only uint8_t. To make setting IP_MULTICAST_LOOP work on every supported platform, always pass a uint8_t variable as a parameter. Signed-off-by: Renato Westphal --- zebra/irdp_packet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zebra') diff --git a/zebra/irdp_packet.c b/zebra/irdp_packet.c index 25c7aff26..290a6180e 100644 --- a/zebra/irdp_packet.c +++ b/zebra/irdp_packet.c @@ -282,7 +282,7 @@ send_packet(struct interface *ifp, char buf[256]; struct in_pktinfo *pktinfo; u_long src; - int on; + u_char on; if (!(ifp->flags & IFF_UP)) return; -- cgit v1.2.3 From c5bdb09fd9d18ac0524a14c90f79cfad95afefd7 Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Sat, 12 Nov 2016 19:05:08 -0200 Subject: *: create a helper function to set the IP_MULTICAST_LOOP sockoption Signed-off-by: Renato Westphal --- ldpd/socket.c | 10 +--------- lib/sockopt.c | 15 ++++++++++++++- lib/sockopt.h | 2 ++ ospfd/ospf_network.c | 8 +++----- pimd/pim_sock.c | 4 ++-- pimd/pim_ssmpingd.c | 18 +++++++----------- zebra/irdp_packet.c | 8 ++------ 7 files changed, 31 insertions(+), 34 deletions(-) (limited to 'zebra') diff --git a/ldpd/socket.c b/ldpd/socket.c index cf352d720..1bb083740 100644 --- a/ldpd/socket.c +++ b/ldpd/socket.c @@ -421,15 +421,7 @@ sock_set_ipv4_mcast(struct iface *iface) int sock_set_ipv4_mcast_loop(int fd) { - uint8_t loop = 0; - - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, - (char *)&loop, sizeof(loop)) < 0) { - log_warn("%s: error setting IP_MULTICAST_LOOP", __func__); - return (-1); - } - - return (0); + return (setsockopt_ipv4_multicast_loop(fd, 0)); } int diff --git a/lib/sockopt.c b/lib/sockopt.c index c480cee0d..be3ac0e4b 100644 --- a/lib/sockopt.c +++ b/lib/sockopt.c @@ -384,7 +384,20 @@ setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr, #error "Unsupported multicast API" #endif } - + +int +setsockopt_ipv4_multicast_loop (int sock, u_char val) +{ + int ret; + + ret = setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *) &val, + sizeof (val)); + if (ret < 0) + zlog_warn ("can't setsockopt IP_MULTICAST_LOOP"); + + return ret; +} + static int setsockopt_ipv4_ifindex (int sock, ifindex_t val) { diff --git a/lib/sockopt.h b/lib/sockopt.h index d67b510b6..02f018934 100644 --- a/lib/sockopt.h +++ b/lib/sockopt.h @@ -89,6 +89,8 @@ extern int setsockopt_ipv4_multicast(int sock, int optname, struct in_addr if_addr, unsigned int mcast_addr, ifindex_t ifindex); +extern int setsockopt_ipv4_multicast_loop (int sock, u_char val); + extern int setsockopt_ipv4_tos(int sock, int tos); /* Ask for, and get, ifindex, by whatever method is supported. */ diff --git a/ospfd/ospf_network.c b/ospfd/ospf_network.c index 6caa38d68..088123ea2 100644 --- a/ospfd/ospf_network.c +++ b/ospfd/ospf_network.c @@ -132,18 +132,16 @@ ospf_if_ipmulticast (struct ospf *top, struct prefix *p, ifindex_t ifindex) { u_char val; int ret, len; - - val = 0; - len = sizeof (val); - + /* Prevent receiving self-origined multicast packets. */ - ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len); + ret = setsockopt_ipv4_multicast_loop (top->fd, 0); if (ret < 0) zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", top->fd, safe_strerror(errno)); /* Explicitly set multicast ttl to 1 -- endo. */ val = 1; + len = sizeof (val); ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len); if (ret < 0) zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", diff --git a/pimd/pim_sock.c b/pimd/pim_sock.c index 90b11dcfd..231efd0f2 100644 --- a/pimd/pim_sock.c +++ b/pimd/pim_sock.c @@ -35,6 +35,7 @@ #include "privs.h" #include "if.h" #include "vrf.h" +#include "sockopt.h" #include "pimd.h" #include "pim_mroute.h" @@ -173,8 +174,7 @@ int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char lo } } - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, - (void *) &loop, sizeof(loop))) { + if (setsockopt_ipv4_multicast_loop (fd, loop)) { zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s", loop ? "enable" : "disable", fd, errno, safe_strerror(errno)); diff --git a/pimd/pim_ssmpingd.c b/pimd/pim_ssmpingd.c index daa3c65e3..fba563a6f 100644 --- a/pimd/pim_ssmpingd.c +++ b/pimd/pim_ssmpingd.c @@ -25,6 +25,7 @@ #include "if.h" #include "log.h" #include "memory.h" +#include "sockopt.h" #include "pim_ssmpingd.h" #include "pim_time.h" @@ -150,17 +151,12 @@ static int ssmpingd_socket(struct in_addr addr, int port, int mttl) return -1; } - { - u_char loop = 0; - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, - (void *) &loop, sizeof(loop))) { - zlog_warn("%s: could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s", - __PRETTY_FUNCTION__, - loop ? "enable" : "disable", - fd, errno, safe_strerror(errno)); - close(fd); - return PIM_SOCK_ERR_LOOP; - } + if (setsockopt_ipv4_multicast_loop (fd, 0)) { + zlog_warn("%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s", + __PRETTY_FUNCTION__, + fd, errno, safe_strerror(errno)); + close(fd); + return PIM_SOCK_ERR_LOOP; } if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, diff --git a/zebra/irdp_packet.c b/zebra/irdp_packet.c index 290a6180e..c9c32ce31 100644 --- a/zebra/irdp_packet.c +++ b/zebra/irdp_packet.c @@ -323,12 +323,8 @@ send_packet(struct interface *ifp, zlog_warn("sendto %s", safe_strerror (errno)); } - if(dst != INADDR_BROADCAST) { - on = 0; - if( setsockopt(irdp_sock,IPPROTO_IP, IP_MULTICAST_LOOP, - (char *)&on,sizeof(on)) < 0) - zlog_warn("sendto %s", safe_strerror (errno)); - } + if(dst != INADDR_BROADCAST) + setsockopt_ipv4_multicast_loop (irdp_sock, 0); memset(&sockdst,0,sizeof(sockdst)); sockdst.sin_family=AF_INET; -- cgit v1.2.3