aboutsummaryrefslogtreecommitdiffstats
path: root/meta-networking/recipes-protocols/mdns/files/0005-Handle-noisy-netlink-sockets.patch
blob: f2b171e55b82210b20480682648cf302a2246e50 (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
From 92025cab86619f548bf3eb816a1804ef40507ca7 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 05/11] 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>
---
 mDNSPosix/mDNSPosix.c | 143 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 133 insertions(+), 10 deletions(-)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index 699855a..59a8b8c 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -1247,14 +1247,38 @@ mDNSlocal void          ProcessRoutingNotification(int sd, GenLinkedList *change
 
 #endif // USES_NETLINK
 
+// Test whether the given PosixNetworkInterface matches the given struct ifi_info
+mDNSlocal mDNSBool InterfacesMatch(PosixNetworkInterface *intf, struct ifi_info *ifi)
+{
+    mDNSBool match = mDNSfalse;
+    mDNSAddr ip, mask;
+
+    if((intf->index == ifi->ifi_index) &&
+       (intf->sa_family == ifi->ifi_addr->sa_family) &&
+       (strcmp(intf->coreIntf.ifname, ifi->ifi_name) == 0))
+        {
+        SockAddrTomDNSAddr(ifi->ifi_addr,    &ip,   NULL);
+        SockAddrTomDNSAddr(ifi->ifi_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, short filter, void *context)
 {
     IfChangeRec     *pChgRec = (IfChangeRec*) context;
+    mDNS            *m = pChgRec->mDNS;
     fd_set readFDs;
     GenLinkedList changedInterfaces;
     NetworkInterfaceIndex *changedInterface;
     struct timeval zeroTimeout = { 0, 0 };
+    struct ifi_info *ifi_list, **ifi, *ifi_free, *ifi_loop4 = NULL;
+    PosixNetworkInterface *intf, *intfNext;
+    mDNSBool found, foundav4;
 
     (void)fd; // Unused
     (void)filter; // Unused
@@ -1270,12 +1294,115 @@ mDNSlocal void InterfaceChangeCallback(int fd, short filter, void *context)
     }
     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;
+
+    ifi_list = get_ifi_info(AF_INET, mDNStrue);
+    if (ifi_list == NULL) goto cleanup;
+
+#if HAVE_IPV6
+    /* Link the IPv6 list to the end of the IPv4 list */
+    ifi = &ifi_list;
+    while (*ifi != NULL) ifi = &(*ifi)->ifi_next;
+    *ifi = get_ifi_info(AF_INET6, mDNStrue);
+#endif
+
+    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 = &ifi_list; *ifi != NULL; ifi = &(*ifi)->ifi_next)
+        {
+            if (InterfacesMatch(intf, *ifi))
+            {
+                found = mDNStrue;
+
+                // Removes unchanged from ifi_list
+                ifi_free = *ifi;
+                *ifi = (*ifi)->ifi_next;
+                ifi_free->ifi_next = NULL;
+                free_ifi_info(ifi_free);
+
+                break;
+            }
+        }
+
+        // Removes changed and old interfaces from m->HostInterfaces
+        if (!found) TearDownInterface(m, intf);
+    }
+
+    // Add new and changed interfaces in ifi_list
+    // Save off loopback interface in case it is needed later
+    for (ifi = &ifi_list; *ifi != NULL; ifi = &(*ifi)->ifi_next)
+    {
+        if ((ifi_loop4 == NULL) &&
+            ((*ifi)->ifi_addr->sa_family == AF_INET) &&
+            ((*ifi)->ifi_flags & IFF_UP) &&
+            ((*ifi)->ifi_flags & IFF_LOOPBACK))
+        {
+            ifi_loop4 = *ifi;
+            continue;
+        }
+
+        if (     (((*ifi)->ifi_addr->sa_family == AF_INET)
+#if HAVE_IPV6
+                  || ((*ifi)->ifi_addr->sa_family == AF_INET6)
+#endif
+                  ) && ((*ifi)->ifi_flags & IFF_UP)
+                    && !((*ifi)->ifi_flags & IFF_POINTOPOINT)
+                    && !((*ifi)->ifi_flags & IFF_LOOPBACK))
+        {
+            SetupOneInterface(m, *ifi);
+        }
+    }
+
+    // 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 && (ifi_loop4 != NULL))
+        {
+            SetupOneInterface(m, ifi_loop4);
+        }
+    }
+
+    if (ifi_list != NULL) free_ifi_info(ifi_list);
 
+cleanup:
     while ((changedInterface = (NetworkInterfaceIndex*)changedInterfaces.Head) != NULL)
     {
         RemoveFromList(&changedInterfaces, changedInterface);
@@ -1400,15 +1527,11 @@ mDNSexport void mDNSPlatformClose(mDNS *const m)
 #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);
-- 
2.17.1