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