aboutsummaryrefslogtreecommitdiffstats
path: root/meta-networking/recipes-protocols/mdns/mdns/0006-Handle-noisy-netlink-sockets.patch
blob: 80cdbca50013ae6b20758c9063cfff0e42e343da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
From bfa1d68bed863e22c40a6d9a19ffbcc8694bbff6 Mon Sep 17 00:00:00 2001
From: Nate Karstens <nate.karstens@garmin.com>
Date: Mon, 24 Jul 2017 09:38:55 -0500
Subject: [PATCH 6/8] Handle noisy netlink sockets

The POSIX implementation currently clears all network interfaces
when netlink indicates that there has been a change. This causes
the following problems:

  1) Applications are informed that all of the services they are
     tracking have been removed.
  2) Increases network load because the client must re-query for
     all records it is interested in.

This changes netlink notification handling by:

  1) Always comparing with the latest interface list returned
     by the OS.
  2) Confirming that the interface has been changed in a way
     that we care about.

Upstream-Status: Submitted [dts@apple.com]

Signed-off-by: Nate Karstens <nate.karstens@garmin.com>
Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---
 mDNSPosix/mDNSPosix.c | 182 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 172 insertions(+), 10 deletions(-)

Index: mDNSResponder/mDNSPosix/mDNSPosix.c
===================================================================
--- mDNSResponder.orig/mDNSPosix/mDNSPosix.c
+++ mDNSResponder/mDNSPosix/mDNSPosix.c
@@ -1788,14 +1788,43 @@ mDNSlocal void          ProcessRoutingNo
 
 #endif // USES_NETLINK
 
+// Test whether the given PosixNetworkInterface matches the given struct ifaddrs
+mDNSlocal mDNSBool InterfacesMatch(PosixNetworkInterface *intf, struct ifaddrs *ifi)
+{
+    mDNSBool match = mDNSfalse;
+    mDNSAddr ip, mask;
+    unsigned int if_index;
+
+    if_index = if_nametoindex(ifi->ifa_name);
+    if (if_index == 0)
+        return mDNSfalse;
+
+    if((intf->index == if_index) &&
+       (intf->sa_family == ifi->ifa_addr->sa_family) &&
+       (strcmp(intf->coreIntf.ifname, ifi->ifa_name) == 0))
+        {
+        SockAddrTomDNSAddr(ifi->ifa_addr,    &ip,   NULL);
+        SockAddrTomDNSAddr(ifi->ifa_netmask, &mask, NULL);
+
+        match = mDNSSameAddress(&intf->coreIntf.ip, &ip) &&
+                mDNSSameAddress(&intf->coreIntf.mask, &mask);
+        }
+
+    return match;
+}
+
 // Called when data appears on interface change notification socket
 mDNSlocal void InterfaceChangeCallback(int fd, void *context)
 {
     IfChangeRec     *pChgRec = (IfChangeRec*) context;
+    mDNS            *m = pChgRec->mDNS;
     fd_set readFDs;
     GenLinkedList changedInterfaces;
     NetworkInterfaceIndex *changedInterface;
     struct timeval zeroTimeout = { 0, 0 };
+    struct ifaddrs *ifa_list, **ifi, *ifa_loop4 = NULL;
+    PosixNetworkInterface *intf, *intfNext;
+    mDNSBool found, foundav4;
 
     (void)fd; // Unused
 
@@ -1810,12 +1839,149 @@ mDNSlocal void InterfaceChangeCallback(i
     }
     while (0 < select(pChgRec->NotifySD + 1, &readFDs, (fd_set*) NULL, (fd_set*) NULL, &zeroTimeout));
 
-    // Currently we rebuild the entire interface list whenever any interface change is
-    // detected. If this ever proves to be a performance issue in a multi-homed
-    // configuration, more care should be paid to changedInterfaces.
-    if (changedInterfaces.Head != NULL)
-        mDNSPlatformPosixRefreshInterfaceList(pChgRec->mDNS);
+    CleanRecentInterfaces();
+
+    if (changedInterfaces.Head == NULL) goto cleanup;
+
+    if (getifaddrs(&ifa_list) < 0) goto cleanup;
+
+    for (intf = (PosixNetworkInterface*)(m->HostInterfaces); intf != NULL; intf = intfNext)
+    {
+        intfNext = (PosixNetworkInterface*)(intf->coreIntf.next);
+
+        // Loopback interface(s) are handled later
+        if (intf->coreIntf.Loopback) continue;
+
+        found = mDNSfalse;
+        for (ifi = &ifa_list; *ifi != NULL; ifi = &(*ifi)->ifa_next)
+        {
+            if (InterfacesMatch(intf, *ifi))
+            {
+                found = mDNStrue;
+                break;
+            }
+        }
+
+        // Removes changed and old interfaces from m->HostInterfaces
+        if (!found) TearDownInterface(m, intf);
+    }
+
+    // Add new and changed interfaces in ifa_list
+    // Save off loopback interface in case it is needed later
+    for (ifi = &ifa_list; *ifi != NULL; ifi = &(*ifi)->ifa_next)
+    {
+        found = mDNSfalse;
+        for (intf = (PosixNetworkInterface*)(m->HostInterfaces); intf != NULL; intf = intfNext)
+        {
+            intfNext = (PosixNetworkInterface*)(intf->coreIntf.next);
+
+            // Loopback interface(s) are handled later
+            if (intf->coreIntf.Loopback) continue;
+
+            if (InterfacesMatch(intf, *ifi))
+            {
+                found = mDNStrue;
+                break;
+            }
+
+            // Removes changed and old interfaces from m->HostInterfaces
+        }
+        if (found)
+	    continue;
+
+        if ((ifa_loop4 == NULL) &&
+            ((*ifi)->ifa_addr->sa_family == AF_INET) &&
+            ((*ifi)->ifa_flags & IFF_UP) &&
+            ((*ifi)->ifa_flags & IFF_LOOPBACK))
+        {
+            ifa_loop4 = *ifi;
+            continue;
+        }
+
+        if (     (((*ifi)->ifa_addr->sa_family == AF_INET)
+#if HAVE_IPV6
+                  || ((*ifi)->ifa_addr->sa_family == AF_INET6)
+#endif
+                  ) && ((*ifi)->ifa_flags & IFF_UP)
+                    && !((*ifi)->ifa_flags & IFF_POINTOPOINT)
+                    && !((*ifi)->ifa_flags & IFF_LOOPBACK))
+        {
+            struct ifaddrs *i = *ifi;
+
+#define ethernet_addr_len 6
+            uint8_t hwaddr[ethernet_addr_len];
+            int hwaddr_len = 0;
+
+#if defined(TARGET_OS_LINUX) && TARGET_OS_LINUX
+            struct ifreq ifr;
+            int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
+            if (sockfd >= 0)
+            {
+                /* Add hardware address */
+                memcpy(ifr.ifr_name, i->ifa_name, IFNAMSIZ);
+                if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) != -1)
+                {
+                    if (ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER)
+                    {
+                        memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ethernet_addr_len);
+                        hwaddr_len = ethernet_addr_len;
+                    }
+                }
+                close(sockfd);
+            }
+            else
+            {
+                memset(hwaddr, 0, sizeof(hwaddr));
+            }
+#endif // TARGET_OS_LINUX
+            SetupOneInterface(m, i->ifa_addr, i->ifa_netmask,
+                              hwaddr, hwaddr_len, i->ifa_name, if_nametoindex(i->ifa_name), i->ifa_flags);
+        }
+    }
+
+    // Determine if there is at least one non-loopback IPv4 interface. This is to work around issues
+    // with multicast loopback on IPv6 interfaces -- see corresponding logic in SetupInterfaceList().
+    foundav4 = mDNSfalse;
+    for (intf = (PosixNetworkInterface*)(m->HostInterfaces); intf != NULL; intf = (PosixNetworkInterface*)(intf->coreIntf.next))
+    {
+        if (intf->sa_family == AF_INET && !intf->coreIntf.Loopback)
+        {
+            foundav4 = mDNStrue;
+            break;
+        }
+    }
+
+    if (foundav4)
+    {
+        for (intf = (PosixNetworkInterface*)(m->HostInterfaces); intf != NULL; intf = intfNext)
+        {
+            intfNext = (PosixNetworkInterface*)(intf->coreIntf.next);
+            if (intf->coreIntf.Loopback) TearDownInterface(m, intf);
+        }
+    }
+    else
+    {
+        found = mDNSfalse;
+
+        for (intf = (PosixNetworkInterface*)(m->HostInterfaces); intf != NULL; intf = (PosixNetworkInterface*)(intf->coreIntf.next))
+        {
+            if (intf->coreIntf.Loopback)
+            {
+                found = mDNStrue;
+                break;
+            }
+        }
+
+        if (!found && (ifa_loop4 != NULL))
+        {
+            SetupOneInterface(m, ifa_loop4->ifa_addr, ifa_loop4->ifa_netmask,
+                              NULL, 0, ifa_loop4->ifa_name, if_nametoindex(ifa_loop4->ifa_name), ifa_loop4->ifa_flags);
+        }
+    }
+
+    if (ifa_list != NULL) freeifaddrs(ifa_list);
 
+cleanup:
     while ((changedInterface = (NetworkInterfaceIndex*)changedInterfaces.Head) != NULL)
     {
         RemoveFromList(&changedInterfaces, changedInterface);
@@ -1947,15 +2113,11 @@ mDNSexport void mDNSPlatformClose(mDNS *
 #endif
 }
 
-// This is used internally by InterfaceChangeCallback.
-// It's also exported so that the Standalone Responder (mDNSResponderPosix)
+// This is exported so that the Standalone Responder (mDNSResponderPosix)
 // can call it in response to a SIGHUP (mainly for debugging purposes).
 mDNSexport mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m)
 {
     int err;
-    // This is a pretty heavyweight way to process interface changes --
-    // destroying the entire interface list and then making fresh one from scratch.
-    // We should make it like the OS X version, which leaves unchanged interfaces alone.
     ClearInterfaceList(m);
     err = SetupInterfaceList(m);
     return PosixErrorToStatus(err);