NetlinkTracker.java revision 08c7116ab9cd04ad6dd3c04aa1017237e7f409ac
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.net;
18
19import android.net.LinkAddress;
20import android.net.LinkProperties;
21import android.net.RouteInfo;
22import android.util.Log;
23
24import java.net.InetAddress;
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.Collections;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Set;
31
32/**
33 * Keeps track of link configuration received from Netlink.
34 *
35 * Instances of this class are expected to be owned by subsystems such as Wi-Fi
36 * or Ethernet that manage one or more network interfaces. Each interface to be
37 * tracked needs its own {@code NetlinkTracker}.
38 *
39 * An instance of this class is constructed by passing in an interface name and
40 * a callback. The owner is then responsible for registering the tracker with
41 * NetworkManagementService. When the class receives update notifications from
42 * the NetworkManagementService notification threads, it applies the update to
43 * its local LinkProperties, and if something has changed, notifies its owner of
44 * the update via the callback.
45 *
46 * The owner can then call {@code getLinkProperties()} in order to find out
47 * what changed. If in the meantime the LinkProperties stored here have changed,
48 * this class will return the current LinkProperties. Because each change
49 * triggers an update callback after the change is made, the owner may get more
50 * callbacks than strictly necessary (some of which may be no-ops), but will not
51 * be out of sync once all callbacks have been processed.
52 *
53 * Threading model:
54 *
55 * - The owner of this class is expected to create it, register it, and call
56 *   getLinkProperties or clearLinkProperties on its thread.
57 * - Most of the methods in the class are inherited from BaseNetworkObserver
58 *   and are called by NetworkManagementService notification threads.
59 * - All accesses to mLinkProperties must be synchronized(this). All the other
60 *   member variables are immutable once the object is constructed.
61 *
62 * This class currently tracks IPv4 and IPv6 addresses. In the future it will
63 * track routes and DNS servers.
64 *
65 * @hide
66 */
67public class NetlinkTracker extends BaseNetworkObserver {
68
69    private final String TAG;
70
71    public interface Callback {
72        public void update();
73    }
74
75    private final String mInterfaceName;
76    private final Callback mCallback;
77    private final LinkProperties mLinkProperties;
78    private DnsServerRepository mDnsServerRepository;
79
80    private static final boolean DBG = false;
81
82    public NetlinkTracker(String iface, Callback callback) {
83        TAG = "NetlinkTracker/" + iface;
84        mInterfaceName = iface;
85        mCallback = callback;
86        mLinkProperties = new LinkProperties();
87        mLinkProperties.setInterfaceName(mInterfaceName);
88        mDnsServerRepository = new DnsServerRepository();
89    }
90
91    private void maybeLog(String operation, String iface, LinkAddress address) {
92        if (DBG) {
93            Log.d(TAG, operation + ": " + address + " on " + iface +
94                    " flags " + address.getFlags() + " scope " + address.getScope());
95        }
96    }
97
98    private void maybeLog(String operation, Object o) {
99        if (DBG) {
100            Log.d(TAG, operation + ": " + o.toString());
101        }
102    }
103
104    @Override
105    public void addressUpdated(String iface, LinkAddress address) {
106        if (mInterfaceName.equals(iface)) {
107            maybeLog("addressUpdated", iface, address);
108            boolean changed;
109            synchronized (this) {
110                changed = mLinkProperties.addLinkAddress(address);
111            }
112            if (changed) {
113                mCallback.update();
114            }
115        }
116    }
117
118    @Override
119    public void addressRemoved(String iface, LinkAddress address) {
120        if (mInterfaceName.equals(iface)) {
121            maybeLog("addressRemoved", iface, address);
122            boolean changed;
123            synchronized (this) {
124                changed = mLinkProperties.removeLinkAddress(address);
125            }
126            if (changed) {
127                mCallback.update();
128            }
129        }
130    }
131
132    @Override
133    public void routeUpdated(RouteInfo route) {
134        if (mInterfaceName.equals(route.getInterface())) {
135            maybeLog("routeUpdated", route);
136            boolean changed;
137            synchronized (this) {
138                changed = mLinkProperties.addRoute(route);
139            }
140            if (changed) {
141                mCallback.update();
142            }
143        }
144    }
145
146    @Override
147    public void routeRemoved(RouteInfo route) {
148        if (mInterfaceName.equals(route.getInterface())) {
149            maybeLog("routeRemoved", route);
150            boolean changed;
151            synchronized (this) {
152                changed = mLinkProperties.removeRoute(route);
153            }
154            if (changed) {
155                mCallback.update();
156            }
157        }
158    }
159
160    @Override
161    public void interfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
162        if (mInterfaceName.equals(iface)) {
163            maybeLog("interfaceDnsServerInfo", Arrays.toString(addresses));
164            boolean changed = mDnsServerRepository.addServers(lifetime, addresses);
165            if (changed) {
166                synchronized (this) {
167                    mDnsServerRepository.setDnsServersOn(mLinkProperties);
168                }
169                mCallback.update();
170            }
171        }
172    }
173
174    /**
175     * Returns a copy of this object's LinkProperties.
176     */
177    public synchronized LinkProperties getLinkProperties() {
178        return new LinkProperties(mLinkProperties);
179    }
180
181    public synchronized void clearLinkProperties() {
182        // Clear the repository before clearing mLinkProperties. That way, if a clear() happens
183        // while interfaceDnsServerInfo() is being called, we'll end up with no DNS servers in
184        // mLinkProperties, as desired.
185        mDnsServerRepository = new DnsServerRepository();
186        mLinkProperties.clear();
187        mLinkProperties.setInterfaceName(mInterfaceName);
188    }
189}
190
191/**
192 * Represents a DNS server entry with an expiry time.
193 *
194 * Implements Comparable so DNS server entries can be sorted by lifetime, longest-lived first.
195 * The ordering of entries with the same lifetime is unspecified, because given two servers with
196 * identical lifetimes, we don't care which one we use, and only comparing the lifetime is much
197 * faster than comparing the IP address as well.
198 *
199 * Note: this class has a natural ordering that is inconsistent with equals.
200 */
201class DnsServerEntry implements Comparable<DnsServerEntry> {
202    /** The IP address of the DNS server. */
203    public final InetAddress address;
204    /** The time until which the DNS server may be used. A Java millisecond time as might be
205      * returned by currentTimeMillis(). */
206    public long expiry;
207
208    public DnsServerEntry(InetAddress address, long expiry) throws IllegalArgumentException {
209        this.address = address;
210        this.expiry = expiry;
211    }
212
213    public int compareTo(DnsServerEntry other) {
214        return Long.compare(other.expiry, this.expiry);
215    }
216}
217
218/**
219 * Tracks DNS server updates received from Netlink.
220 *
221 * The network may announce an arbitrary number of DNS servers in Router Advertisements at any
222 * time. Each announcement has a lifetime; when the lifetime expires, the servers should not be used
223 * any more. In this way, the network can gracefully migrate clients from one set of DNS servers to
224 * another. Announcements can both raise and lower the lifetime, and an announcement can expire
225 * servers by announcing them with a lifetime of zero.
226 *
227 * Typically the system will only use a small number (2 or 3; {@code NUM_CURRENT_SERVERS}) of DNS
228 * servers at any given time. These are referred to as the current servers. In case all the
229 * current servers expire, the class also keeps track of a larger (but limited) number of servers
230 * that are promoted to current servers when the current ones expire. In order to minimize updates
231 * to the rest of the system (and potentially expensive cache flushes) this class attempts to keep
232 * the list of current servers constant where possible. More specifically, the list of current
233 * servers is only updated if a new server is learned and there are not yet {@code
234 * NUM_CURRENT_SERVERS} current servers, or if one or more of the current servers expires or is
235 * pushed out of the set. Therefore, the current servers will not necessarily be the ones with the
236 * highest lifetime, but the ones learned first.
237 *
238 * This is by design: if instead the class always preferred the servers with the highest lifetime, a
239 * (misconfigured?) network where two or more routers announce more than {@code NUM_CURRENT_SERVERS}
240 * unique servers would cause persistent oscillations.
241 *
242 * TODO: Currently servers are only expired when a new DNS update is received.
243 * Update them using timers, or possibly on every notification received by NetlinkTracker.
244 *
245 * Threading model: run by NetlinkTracker. Methods are synchronized(this) just in case netlink
246 * notifications are sent by multiple threads. If future threads use alarms to expire, those
247 * alarms must also be synchronized(this).
248 *
249 */
250class DnsServerRepository {
251
252    /** How many DNS servers we will use. 3 is suggested by RFC 6106. */
253    public static final int NUM_CURRENT_SERVERS = 3;
254
255    /** How many DNS servers we'll keep track of, in total. */
256    public static final int NUM_SERVERS = 12;
257
258    /** Stores up to {@code NUM_CURRENT_SERVERS} DNS servers we're currently using. */
259    private Set<InetAddress> mCurrentServers;
260
261    public static final String TAG = "DnsServerRepository";
262
263    /**
264     * Stores all the DNS servers we know about, for use when the current servers expire.
265     * Always sorted in order of decreasing expiry. The elements in this list are also the values
266     * of mIndex, and may be elements in mCurrentServers.
267     */
268    private ArrayList<DnsServerEntry> mAllServers;
269
270    /**
271     * Indexes the servers so we can update their lifetimes more quickly in the common case where
272     * servers are not being added, but only being refreshed.
273     */
274    private HashMap<InetAddress, DnsServerEntry> mIndex;
275
276    public DnsServerRepository() {
277        mCurrentServers = new HashSet();
278        mAllServers = new ArrayList<DnsServerEntry>(NUM_SERVERS);
279        mIndex = new HashMap<InetAddress, DnsServerEntry>(NUM_SERVERS);
280    }
281
282    /** Sets the DNS servers of the provided LinkProperties object to the current servers. */
283    public synchronized void setDnsServersOn(LinkProperties lp) {
284        lp.setDnsServers(mCurrentServers);
285    }
286
287    /**
288     * Notifies the class of new DNS server information.
289     * @param lifetime the time in seconds that the DNS servers are valid.
290     * @param addresses the string representations of the IP addresses of the DNS servers to use.
291     */
292    public synchronized boolean addServers(long lifetime, String[] addresses) {
293        // The lifetime is actually an unsigned 32-bit number, but Java doesn't have unsigned.
294        // Technically 0xffffffff (the maximum) is special and means "forever", but 2^32 seconds
295        // (136 years) is close enough.
296        long now = System.currentTimeMillis();
297        long expiry = now + 1000 * lifetime;
298
299        // Go through the list of servers. For each one, update the entry if one exists, and
300        // create one if it doesn't.
301        for (String addressString : addresses) {
302            InetAddress address;
303            try {
304                address = InetAddress.parseNumericAddress(addressString);
305            } catch (IllegalArgumentException ex) {
306                continue;
307            }
308
309            if (!updateExistingEntry(address, expiry)) {
310                // There was no entry for this server. Create one, unless it's already expired
311                // (i.e., if the lifetime is zero; it cannot be < 0 because it's unsigned).
312                if (expiry > now) {
313                    DnsServerEntry entry = new DnsServerEntry(address, expiry);
314                    mAllServers.add(entry);
315                    mIndex.put(address, entry);
316                }
317            }
318        }
319
320        // Sort the servers by expiry.
321        Collections.sort(mAllServers);
322
323        // Prune excess entries and update the current server list.
324        return updateCurrentServers();
325    }
326
327    private synchronized boolean updateExistingEntry(InetAddress address, long expiry) {
328        DnsServerEntry existing = mIndex.get(address);
329        if (existing != null) {
330            existing.expiry = expiry;
331            return true;
332        }
333        return false;
334    }
335
336    private synchronized boolean updateCurrentServers() {
337        long now = System.currentTimeMillis();
338        boolean changed = false;
339
340        // Prune excess or expired entries.
341        for (int i = mAllServers.size() - 1; i >= 0; i--) {
342            if (i >= NUM_SERVERS || mAllServers.get(i).expiry < now) {
343                DnsServerEntry removed = mAllServers.remove(i);
344                mIndex.remove(removed.address);
345                changed |= mCurrentServers.remove(removed.address);
346            } else {
347                break;
348            }
349        }
350
351        // Add servers to the current set, in order of decreasing lifetime, until it has enough.
352        // Prefer existing servers over new servers in order to minimize updates to the rest of the
353        // system and avoid persistent oscillations.
354        for (DnsServerEntry entry : mAllServers) {
355            if (mCurrentServers.size() < NUM_CURRENT_SERVERS) {
356                changed |= mCurrentServers.add(entry.address);
357            } else {
358                break;
359            }
360        }
361        return changed;
362    }
363}
364