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