chiark / gitweb /
resolved: simplify detection of packets from the loopback device
authorLennart Poettering <lennart@poettering.net>
Wed, 22 Oct 2014 14:52:38 +0000 (16:52 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 22 Oct 2014 14:52:38 +0000 (16:52 +0200)
We can simplify our code quite a bit if we explicitly check for the
ifindex being 1 on Linux as a loopback check. Apparently, this is
hardcoded on Linux on the kernel, and effectively exported to userspace
via rtnl and such, hence we should be able to rely on it.

src/resolve/resolved-dns-stream.c
src/resolve/resolved-manager.c
src/resolve/resolved-manager.h
src/shared/missing.h

index 8aad5e4df1cc330692d4e77835cc4abea7bc1cc8..3690679ec64133c5569f3defbd5ea1c473cc4ddf 100644 (file)
@@ -157,7 +157,7 @@ static int dns_stream_identify(DnsStream *s) {
          * device if the connection came from the local host since it
          * avoids the routing table in such a case. Let's unset the
          * interface index in such a case. */
-        if (s->ifindex > 0 && manager_ifindex_is_loopback(s->manager, s->ifindex) != 0)
+        if (s->ifindex == LOOPBACK_IFINDEX)
                 s->ifindex = 0;
 
         /* If we don't know the interface index still, we look for the
index 74f4ad5dba40c3494bf51675b03270724a678d6d..c4a5b08773995b26b7c123f66f40fc703b52f6e5 100644 (file)
@@ -960,7 +960,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
          * device if the packet came from the local host since it
          * avoids the routing table in such a case. Let's unset the
          * interface index in such a case. */
-        if (p->ifindex > 0 && manager_ifindex_is_loopback(m, p->ifindex) != 0)
+        if (p->ifindex == LOOPBACK_IFINDEX)
                 p->ifindex = 0;
 
         /* If we don't know the interface index still, we look for the
@@ -1695,26 +1695,6 @@ fail:
         return r;
 }
 
-/* lo having ifindex 1 is hardcoded in the kernel */
-#define LOOPBACK_IFINDEX 1
-
-int manager_ifindex_is_loopback(Manager *m, int ifindex) {
-        Link *l;
-        assert(m);
-
-        if (ifindex <= 0)
-                return -EINVAL;
-
-        l = hashmap_get(m->links, INT_TO_PTR(ifindex));
-        if (!l)
-                /* in case we don't yet track the link, rely on the hardcoded value */
-                return ifindex == LOOPBACK_IFINDEX;
-        else if (l->flags & IFF_LOOPBACK)
-                return 1;
-
-        return 0;
-}
-
 int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
         LinkAddress *a;
 
index 78cbfc0b03123ad3db84b94bda5582b8357b2ece..1151029d29fdec7cdf0ace882ba6e4bd38418d8f 100644 (file)
@@ -139,7 +139,6 @@ int manager_llmnr_ipv6_udp_fd(Manager *m);
 int manager_llmnr_ipv4_tcp_fd(Manager *m);
 int manager_llmnr_ipv6_tcp_fd(Manager *m);
 
-int manager_ifindex_is_loopback(Manager *m, int ifindex);
 int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr);
 LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr);
 
index a88d9e414b171898f195134efdf5c48d9f08fbe2..bb4f8f23a8c2d7138de00e9067fa833b0a47b8b9 100644 (file)
@@ -527,3 +527,12 @@ static inline int setns(int fd, int nstype) {
 #ifndef BPF_XOR
 #  define BPF_XOR 0xa0
 #endif
+
+/* Note that LOOPBACK_IFINDEX is currently not exported by the
+ * kernel/glibc, but hardcoded internally by the kernel.  However, as
+ * it is exported to userspace indirectly via rtnetlink and the
+ * ioctls, and made use of widely we define it here too, in a way that
+ * is compatible with the kernel's internal definition. */
+#ifndef LOOPBACK_IFINDEX
+#define LOOPBACK_IFINDEX 1
+#endif