bdc76f
commit 08504de71813ddbd447bfbca4a325cbe8ce8bcda
bdc76f
Author: Florian Weimer <fweimer@redhat.com>
bdc76f
Date:   Tue Mar 12 11:40:47 2019 +0100
bdc76f
bdc76f
    resolv: Enable full ICMP errors for UDP DNS sockets [BZ #24047]
bdc76f
    
bdc76f
    The Linux kernel suppresses some ICMP error messages by default for
bdc76f
    UDP sockets.  This commit enables full ICMP error reporting,
bdc76f
    hopefully resulting in faster failover to working name servers.
bdc76f
bdc76f
diff --git a/resolv/Makefile b/resolv/Makefile
bdc76f
index 8f22e6a154..ebe1b733f2 100644
bdc76f
--- a/resolv/Makefile
bdc76f
+++ b/resolv/Makefile
bdc76f
@@ -105,7 +105,7 @@ libresolv-routines := res_comp res_debug \
bdc76f
 		      res_data res_mkquery res_query res_send		\
bdc76f
 		      inet_net_ntop inet_net_pton inet_neta base64	\
bdc76f
 		      ns_parse ns_name ns_netint ns_ttl ns_print	\
bdc76f
-		      ns_samedomain ns_date \
bdc76f
+		      ns_samedomain ns_date res_enable_icmp \
bdc76f
 		      compat-hooks compat-gethnamaddr
bdc76f
 
bdc76f
 libanl-routines := gai_cancel gai_error gai_misc gai_notify gai_suspend \
bdc76f
diff --git a/resolv/res_enable_icmp.c b/resolv/res_enable_icmp.c
bdc76f
new file mode 100644
bdc76f
index 0000000000..bdc9220f08
bdc76f
--- /dev/null
bdc76f
+++ b/resolv/res_enable_icmp.c
bdc76f
@@ -0,0 +1,37 @@
bdc76f
+/* Enable full ICMP errors on a socket.
bdc76f
+   Copyright (C) 2019 Free Software Foundation, Inc.
bdc76f
+   This file is part of the GNU C Library.
bdc76f
+
bdc76f
+   The GNU C Library is free software; you can redistribute it and/or
bdc76f
+   modify it under the terms of the GNU Lesser General Public
bdc76f
+   License as published by the Free Software Foundation; either
bdc76f
+   version 2.1 of the License, or (at your option) any later version.
bdc76f
+
bdc76f
+   The GNU C Library is distributed in the hope that it will be useful,
bdc76f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bdc76f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bdc76f
+   Lesser General Public License for more details.
bdc76f
+
bdc76f
+   You should have received a copy of the GNU Lesser General Public
bdc76f
+   License along with the GNU C Library; if not, see
bdc76f
+   <http://www.gnu.org/licenses/>.  */
bdc76f
+
bdc76f
+#include <errno.h>
bdc76f
+#include <netinet/in.h>
bdc76f
+#include <sys/socket.h>
bdc76f
+
bdc76f
+int
bdc76f
+__res_enable_icmp (int family, int fd)
bdc76f
+{
bdc76f
+  int one = 1;
bdc76f
+  switch (family)
bdc76f
+    {
bdc76f
+    case AF_INET:
bdc76f
+      return setsockopt (fd, SOL_IP, IP_RECVERR, &one, sizeof (one));
bdc76f
+    case AF_INET6:
bdc76f
+      return setsockopt (fd, SOL_IPV6, IPV6_RECVERR, &one, sizeof (one));
bdc76f
+    default:
bdc76f
+      __set_errno (EAFNOSUPPORT);
bdc76f
+      return -1;
bdc76f
+    }
bdc76f
+}
bdc76f
diff --git a/resolv/res_send.c b/resolv/res_send.c
bdc76f
index fa040c1198..0f6ec83a7b 100644
bdc76f
--- a/resolv/res_send.c
bdc76f
+++ b/resolv/res_send.c
bdc76f
@@ -943,6 +943,18 @@ reopen (res_state statp, int *terrno, int ns)
bdc76f
 			return (-1);
bdc76f
 		}
bdc76f
 
bdc76f
+		/* Enable full ICMP error reporting for this
bdc76f
+		   socket.  */
bdc76f
+		if (__res_enable_icmp (nsap->sa_family,
bdc76f
+				       EXT (statp).nssocks[ns]) < 0)
bdc76f
+		  {
bdc76f
+		    int saved_errno = errno;
bdc76f
+		    __res_iclose (statp, false);
bdc76f
+		    __set_errno (saved_errno);
bdc76f
+		    *terrno = saved_errno;
bdc76f
+		    return -1;
bdc76f
+		  }
bdc76f
+
bdc76f
 		/*
bdc76f
 		 * On a 4.3BSD+ machine (client and server,
bdc76f
 		 * actually), sending to a nameserver datagram
bdc76f
diff --git a/resolv/resolv-internal.h b/resolv/resolv-internal.h
bdc76f
index 6ab8f2af09..1500adc607 100644
bdc76f
--- a/resolv/resolv-internal.h
bdc76f
+++ b/resolv/resolv-internal.h
bdc76f
@@ -100,4 +100,10 @@ libc_hidden_proto (__inet_pton_length)
bdc76f
 /* Called as part of the thread shutdown sequence.  */
bdc76f
 void __res_thread_freeres (void) attribute_hidden;
bdc76f
 
bdc76f
+/* The Linux kernel does not enable all ICMP messages on a UDP socket
bdc76f
+   by default.  A call this function enables full error reporting for
bdc76f
+   the socket FD.  FAMILY must be AF_INET or AF_INET6.  Returns 0 on
bdc76f
+   success, -1 on failure.  */
bdc76f
+int __res_enable_icmp (int family, int fd) attribute_hidden;
bdc76f
+
bdc76f
 #endif  /* _RESOLV_INTERNAL_H */