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