From 64644ddece9e6f5f08469ab4ad1277ab31983dad Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 2 Aug 2016 10:55:40 +0200 Subject: socket: Set TTL values as suggested in RFC 4795, section 2.5 Set the TTL of the IP header to 255 for UDP unicast and multicast packets, as recommended in RFC 4795, section 2.5. Signed-off-by: Tobias Klauser --- socket.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/socket.c b/socket.c index 0b0be78..872796d 100644 --- a/socket.c +++ b/socket.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2015 Tobias Klauser + * Copyright (C) 2014-2016 Tobias Klauser * * This file is part of llmnrd. * @@ -34,6 +34,7 @@ #include "socket.h" static const int YES = 1; +static const int TTL = 255; int socket_open_ipv4(uint16_t port) { @@ -52,6 +53,17 @@ int socket_open_ipv4(uint16_t port) goto err; } + /* RFC 4795, section 2.5 recommends to set TTL to 255 for UDP */ + if (setsockopt(sock, IPPROTO_IP, IP_TTL, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv4 unicast TTL socket option: %s\n", strerror(errno)); + goto err; + } + + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv4 multicast TTL socket option: %s\n", strerror(errno)); + goto err; + } + /* bind the socket */ memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; @@ -91,6 +103,17 @@ int socket_open_ipv6(uint16_t port) goto err; } + /* RFC 4795, section 2.5 recommends to set TTL to 255 for UDP */ + if (setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv6 unicast hops socket option: %s\n", strerror(errno)); + goto err; + } + + if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv6 multicast hops socket option: %s\n", strerror(errno)); + goto err; + } + /* IPv6 only socket */ if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &YES, sizeof(YES)) < 0) { log_err("Failed to set IPv6 only socket option: %s\n", strerror(errno)); -- cgit v1.2.3-54-g00ecf