summaryrefslogtreecommitdiff
path: root/service/java/com/android/server/wifi/WifiBackupDataV1Parser.java
blob: 9b27cf434f1372929750d6cdf9265e3587b38f8d (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.ProxyInfo;
import android.net.RouteInfo;
import android.net.StaticIpConfiguration;
import android.net.wifi.WifiConfiguration;
import android.util.Log;
import android.util.Pair;

import com.android.server.wifi.util.XmlUtil;
import com.android.server.wifi.util.XmlUtil.IpConfigurationXmlUtil;
import com.android.server.wifi.util.XmlUtil.WifiConfigurationXmlUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Parser for major version 1 of WiFi backup data.
 * Contains whitelists of tags for WifiConfiguration and IpConfiguration sections for each of
 * the minor versions.
 *
 * Overall structure of the major version 1 XML schema:
 * <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
 * <WifiConfigStore>
 *  <float name="Version" value="1.0" />
 *  <NetworkList>
 *   <Network>
 *    <WifiConfiguration>
 *     <string name="ConfigKey">value</string>
 *     <string name="SSID">value</string>
 *     <string name="BSSID" />value</string>
 *     <string name="PreSharedKey" />value</string>
 *     <string-array name="WEPKeys" num="4">
 *      <item value="WifiConfigStoreWep1" />
 *      <item value="WifiConfigStoreWep2" />
 *      <item value="WifiConfigStoreWep3" />
 *      <item value="WifiConfigStoreWep3" />
 *     </string-array>
 *     ... (other supported tag names in minor version 1: "WEPTxKeyIndex", "HiddenSSID",
 *          "RequirePMF", "AllowedKeyMgmt", "AllowedProtocols", "AllowedAuthAlgos",
 *          "AllowedGroupCiphers", "AllowedPairwiseCiphers", "Shared")
 *    </WifiConfiguration>
 *    <IpConfiguration>
 *     <string name="IpAssignment">value</string>
 *     <string name="ProxySettings">value</string>
 *      ... (other supported tag names in minor version 1: "LinkAddress", "LinkPrefixLength",
 *           "GatewayAddress", "DNSServers", "ProxyHost", "ProxyPort", "ProxyPac",
 *           "ProxyExclusionList")
 *    </IpConfiguration>
 *   </Network>
 *   <Network>
 *    ... (format as above)
 *   </Network>
 *  </NetworkList>
 * </WifiConfigStore>
 */
class WifiBackupDataV1Parser implements WifiBackupDataParser {

    private static final String TAG = "WifiBackupDataV1Parser";

    private static final int HIGHEST_SUPPORTED_MINOR_VERSION = 0;

    // List of tags supported for <WifiConfiguration> section in minor version 0
    private static final Set<String> WIFI_CONFIGURATION_MINOR_V0_SUPPORTED_TAGS =
            new HashSet<String>(Arrays.asList(new String[] {
                WifiConfigurationXmlUtil.XML_TAG_CONFIG_KEY,
                WifiConfigurationXmlUtil.XML_TAG_SSID,
                WifiConfigurationXmlUtil.XML_TAG_BSSID,
                WifiConfigurationXmlUtil.XML_TAG_PRE_SHARED_KEY,
                WifiConfigurationXmlUtil.XML_TAG_WEP_KEYS,
                WifiConfigurationXmlUtil.XML_TAG_WEP_TX_KEY_INDEX,
                WifiConfigurationXmlUtil.XML_TAG_HIDDEN_SSID,
                WifiConfigurationXmlUtil.XML_TAG_REQUIRE_PMF,
                WifiConfigurationXmlUtil.XML_TAG_ALLOWED_KEY_MGMT,
                WifiConfigurationXmlUtil.XML_TAG_ALLOWED_PROTOCOLS,
                WifiConfigurationXmlUtil.XML_TAG_ALLOWED_AUTH_ALGOS,
                WifiConfigurationXmlUtil.XML_TAG_ALLOWED_GROUP_CIPHERS,
                WifiConfigurationXmlUtil.XML_TAG_ALLOWED_PAIRWISE_CIPHERS,
                WifiConfigurationXmlUtil.XML_TAG_SHARED,
            }));

    // List of tags supported for <IpConfiguration> section in minor version 0
    private static final Set<String> IP_CONFIGURATION_MINOR_V0_SUPPORTED_TAGS =
            new HashSet<String>(Arrays.asList(new String[] {
                IpConfigurationXmlUtil.XML_TAG_IP_ASSIGNMENT,
                IpConfigurationXmlUtil.XML_TAG_LINK_ADDRESS,
                IpConfigurationXmlUtil.XML_TAG_LINK_PREFIX_LENGTH,
                IpConfigurationXmlUtil.XML_TAG_GATEWAY_ADDRESS,
                IpConfigurationXmlUtil.XML_TAG_DNS_SERVER_ADDRESSES,
                IpConfigurationXmlUtil.XML_TAG_PROXY_SETTINGS,
                IpConfigurationXmlUtil.XML_TAG_PROXY_HOST,
                IpConfigurationXmlUtil.XML_TAG_PROXY_PORT,
                IpConfigurationXmlUtil.XML_TAG_PROXY_EXCLUSION_LIST,
                IpConfigurationXmlUtil.XML_TAG_PROXY_PAC_FILE,
            }));

    public List<WifiConfiguration> parseNetworkConfigurationsFromXml(XmlPullParser in,
            int outerTagDepth, int minorVersion) throws XmlPullParserException, IOException {
        // clamp down the minorVersion to the highest one that this parser version supports
        if (minorVersion > HIGHEST_SUPPORTED_MINOR_VERSION) {
            minorVersion = HIGHEST_SUPPORTED_MINOR_VERSION;
        }
        // Find the configuration list section.
        XmlUtil.gotoNextSectionWithName(in, WifiBackupRestore.XML_TAG_SECTION_HEADER_NETWORK_LIST,
                outerTagDepth);
        // Find all the configurations within the configuration list section.
        int networkListTagDepth = outerTagDepth + 1;
        List<WifiConfiguration> configurations = new ArrayList<>();
        while (XmlUtil.gotoNextSectionWithNameOrEnd(
                in, WifiBackupRestore.XML_TAG_SECTION_HEADER_NETWORK, networkListTagDepth)) {
            WifiConfiguration configuration =
                    parseNetworkConfigurationFromXml(in, minorVersion, networkListTagDepth);
            if (configuration != null) {
                Log.v(TAG, "Parsed Configuration: " + configuration.configKey());
                configurations.add(configuration);
            }
        }
        return configurations;
    }

    /**
     * Parses the configuration data elements from the provided XML stream to a Configuration.
     *
     * @param in            XmlPullParser instance pointing to the XML stream.
     * @param minorVersion  minor version number parsed from incoming data.
     * @param outerTagDepth depth of the outer tag in the XML document.
     * @return WifiConfiguration object if parsing is successful, null otherwise.
     */
    private WifiConfiguration parseNetworkConfigurationFromXml(XmlPullParser in, int minorVersion,
            int outerTagDepth) throws XmlPullParserException, IOException {
        WifiConfiguration configuration = null;
        int networkTagDepth = outerTagDepth + 1;
        // Retrieve WifiConfiguration object first.
        XmlUtil.gotoNextSectionWithName(
                in, WifiBackupRestore.XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION,
                networkTagDepth);
        int configTagDepth = networkTagDepth + 1;
        configuration = parseWifiConfigurationFromXml(in, configTagDepth, minorVersion);
        if (configuration == null) {
            return null;
        }
        // Now retrieve any IP configuration info.
        XmlUtil.gotoNextSectionWithName(
                in, WifiBackupRestore.XML_TAG_SECTION_HEADER_IP_CONFIGURATION, networkTagDepth);
        IpConfiguration ipConfiguration = parseIpConfigurationFromXml(in, configTagDepth,
                minorVersion);
        configuration.setIpConfiguration(ipConfiguration);
        return configuration;
    }

    /**
     * Helper method to parse the WifiConfiguration object.
     */
    private WifiConfiguration parseWifiConfigurationFromXml(XmlPullParser in,
            int outerTagDepth, int minorVersion) throws XmlPullParserException, IOException {
        Pair<String, WifiConfiguration> parsedConfig =
                parseWifiConfigurationFromXmlInternal(in, outerTagDepth, minorVersion);
        if (parsedConfig == null || parsedConfig.first == null || parsedConfig.second == null) {
            return null;
        }
        String configKeyParsed = parsedConfig.first;
        WifiConfiguration configuration = parsedConfig.second;
        String configKeyCalculated = configuration.configKey();
        if (!configKeyParsed.equals(configKeyCalculated)) {
            // configKey is not part of the SDK. So, we can't expect this to be the same
            // across OEM's. Just log a warning & continue.
            Log.w(TAG, "Configuration key does not match. Retrieved: " + configKeyParsed
                    + ", Calculated: " + configKeyCalculated);
        }
        return configuration;
    }

    /**
     * Helper method to mask out any invalid data in parsed WifiConfiguration.
     *
     * This is a compatibility layer added to the parsing logic to try and weed out any known
     * issues in the backup data format from other OEM's.
     */
    private static void clearAnyKnownIssuesInParsedConfiguration(WifiConfiguration config) {
        /**
         * Fix for b/73987207. Clear any invalid bits in the bitsets.
         */
        // |allowedKeyManagement|
        if (config.allowedKeyManagement.length()
                > WifiConfiguration.KeyMgmt.strings.length) {
            config.allowedKeyManagement.clear(
                    WifiConfiguration.KeyMgmt.strings.length,
                    config.allowedKeyManagement.length());
        }
        // |allowedProtocols|
        if (config.allowedProtocols.length()
                > WifiConfiguration.Protocol.strings.length) {
            config.allowedProtocols.clear(
                    WifiConfiguration.Protocol.strings.length,
                    config.allowedProtocols.length());
        }
        // |allowedAuthAlgorithms|
        if (config.allowedAuthAlgorithms.length()
                > WifiConfiguration.AuthAlgorithm.strings.length) {
            config.allowedAuthAlgorithms.clear(
                    WifiConfiguration.AuthAlgorithm.strings.length,
                    config.allowedAuthAlgorithms.length());
        }
        // |allowedGroupCiphers|
        if (config.allowedGroupCiphers.length()
                > WifiConfiguration.GroupCipher.strings.length) {
            config.allowedGroupCiphers.clear(
                    WifiConfiguration.GroupCipher.strings.length,
                    config.allowedGroupCiphers.length());
        }
        // |allowedPairwiseCiphers|
        if (config.allowedPairwiseCiphers.length()
                > WifiConfiguration.PairwiseCipher.strings.length) {
            config.allowedPairwiseCiphers.clear(
                    WifiConfiguration.PairwiseCipher.strings.length,
                    config.allowedPairwiseCiphers.length());
        }
        // Add any other fixable issues discovered from other OEM's here.
    }

    /**
     * Parses the configuration data elements from the provided XML stream to a
     * WifiConfiguration object.
     * Looping through the tags makes it easy to add elements in the future minor versions if
     * needed. Unsupported elements will be ignored.
     *
     * @param in            XmlPullParser instance pointing to the XML stream.
     * @param outerTagDepth depth of the outer tag in the XML document.
     * @param minorVersion  minor version number parsed from incoming data.
     * @return Pair<Config key, WifiConfiguration object> if parsing is successful, null otherwise.
     */
    private static Pair<String, WifiConfiguration> parseWifiConfigurationFromXmlInternal(
            XmlPullParser in, int outerTagDepth, int minorVersion)
            throws XmlPullParserException, IOException {
        WifiConfiguration configuration = new WifiConfiguration();
        String configKeyInData = null;
        Set<String> supportedTags = getSupportedWifiConfigurationTags(minorVersion);

        // Loop through and parse out all the elements from the stream within this section.
        while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
            String[] valueName = new String[1];
            Object value = XmlUtil.readCurrentValue(in, valueName);
            String tagName = valueName[0];
            if (tagName == null) {
                throw new XmlPullParserException("Missing value name");
            }

            // ignore the tags that are not supported up until the current minor version
            if (!supportedTags.contains(tagName)) {
                Log.w(TAG, "Unsupported tag + \"" + tagName + "\" found in <WifiConfiguration>"
                        + " section, ignoring.");
                continue;
            }

            // note: the below switch case list should contain all tags supported up until the
            // highest minor version supported by this parser
            switch (tagName) {
                case WifiConfigurationXmlUtil.XML_TAG_CONFIG_KEY:
                    configKeyInData = (String) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_SSID:
                    configuration.SSID = (String) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_BSSID:
                    configuration.BSSID = (String) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_PRE_SHARED_KEY:
                    configuration.preSharedKey = (String) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_WEP_KEYS:
                    populateWepKeysFromXmlValue(value, configuration.wepKeys);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_WEP_TX_KEY_INDEX:
                    configuration.wepTxKeyIndex = (int) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_HIDDEN_SSID:
                    configuration.hiddenSSID = (boolean) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_REQUIRE_PMF:
                    configuration.requirePMF = (boolean) value;
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_ALLOWED_KEY_MGMT:
                    byte[] allowedKeyMgmt = (byte[]) value;
                    configuration.allowedKeyManagement = BitSet.valueOf(allowedKeyMgmt);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_ALLOWED_PROTOCOLS:
                    byte[] allowedProtocols = (byte[]) value;
                    configuration.allowedProtocols = BitSet.valueOf(allowedProtocols);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_ALLOWED_AUTH_ALGOS:
                    byte[] allowedAuthAlgorithms = (byte[]) value;
                    configuration.allowedAuthAlgorithms = BitSet.valueOf(allowedAuthAlgorithms);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_ALLOWED_GROUP_CIPHERS:
                    byte[] allowedGroupCiphers = (byte[]) value;
                    configuration.allowedGroupCiphers = BitSet.valueOf(allowedGroupCiphers);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_ALLOWED_PAIRWISE_CIPHERS:
                    byte[] allowedPairwiseCiphers = (byte[]) value;
                    configuration.allowedPairwiseCiphers =
                            BitSet.valueOf(allowedPairwiseCiphers);
                    break;
                case WifiConfigurationXmlUtil.XML_TAG_SHARED:
                    configuration.shared = (boolean) value;
                    break;
                default:
                    // should never happen, since other tags are filtered out earlier
                    throw new XmlPullParserException(
                            "Unknown value name found: " + valueName[0]);
            }
        }
        clearAnyKnownIssuesInParsedConfiguration(configuration);
        return Pair.create(configKeyInData, configuration);
    }

    /**
     * Returns a set of supported tags of <WifiConfiguration> element for all minor versions of
     * this major version up to and including the specified minorVersion (only adding tags is
     * supported in minor versions, removal or changing the meaning of tags requires bumping
     * the major version and reseting the minor to 0).
     *
     * @param minorVersion  minor version number parsed from incoming data.
     */
    private static Set<String> getSupportedWifiConfigurationTags(int minorVersion) {
        switch (minorVersion) {
            case 0: return WIFI_CONFIGURATION_MINOR_V0_SUPPORTED_TAGS;
            default:
                Log.e(TAG, "Invalid minorVersion: " + minorVersion);
                return Collections.<String>emptySet();
        }
    }

    /**
     * Populate wepKeys array elements only if they were non-empty in the backup data.
     *
     * @throws XmlPullParserException if parsing errors occur.
     */
    private static void populateWepKeysFromXmlValue(Object value, String[] wepKeys)
            throws XmlPullParserException, IOException {
        String[] wepKeysInData = (String[]) value;
        if (wepKeysInData == null) {
            return;
        }
        if (wepKeysInData.length != wepKeys.length) {
            throw new XmlPullParserException(
                    "Invalid Wep Keys length: " + wepKeysInData.length);
        }
        for (int i = 0; i < wepKeys.length; i++) {
            if (wepKeysInData[i].isEmpty()) {
                wepKeys[i] = null;
            } else {
                wepKeys[i] = wepKeysInData[i];
            }
        }
    }

    /**
     * Parses the IP configuration data elements from the provided XML stream to an
     * IpConfiguration object.
     *
     * @param in            XmlPullParser instance pointing to the XML stream.
     * @param outerTagDepth depth of the outer tag in the XML document.
     * @param minorVersion  minor version number parsed from incoming data.
     * @return IpConfiguration object if parsing is successful, null otherwise.
     */
    private static IpConfiguration parseIpConfigurationFromXml(XmlPullParser in,
            int outerTagDepth, int minorVersion) throws XmlPullParserException, IOException {
        // First parse *all* of the tags in <IpConfiguration> section
        Set<String> supportedTags = getSupportedIpConfigurationTags(minorVersion);

        String ipAssignmentString = null;
        String linkAddressString = null;
        Integer linkPrefixLength = null;
        String gatewayAddressString = null;
        String[] dnsServerAddressesString = null;
        String proxySettingsString = null;
        String proxyHost = null;
        int proxyPort = -1;
        String proxyExclusionList = null;
        String proxyPacFile = null;

        // Loop through and parse out all the elements from the stream within this section.
        while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
            String[] valueName = new String[1];
            Object value = XmlUtil.readCurrentValue(in, valueName);
            String tagName = valueName[0];
            if (tagName == null) {
                throw new XmlPullParserException("Missing value name");
            }

            // ignore the tags that are not supported up until the current minor version
            if (!supportedTags.contains(tagName)) {
                Log.w(TAG, "Unsupported tag + \"" + tagName + "\" found in <IpConfiguration>"
                        + " section, ignoring.");
                continue;
            }

            // note: the below switch case list should contain all tags supported up until the
            // highest minor version supported by this parser
            // should any tags be added in next minor versions, conditional processing of them
            // also needs to be added in the below code (processing into IpConfiguration object)
            switch (tagName) {
                case IpConfigurationXmlUtil.XML_TAG_IP_ASSIGNMENT:
                    ipAssignmentString = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_LINK_ADDRESS:
                    linkAddressString = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_LINK_PREFIX_LENGTH:
                    linkPrefixLength = (Integer) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_GATEWAY_ADDRESS:
                    gatewayAddressString = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_DNS_SERVER_ADDRESSES:
                    dnsServerAddressesString = (String[]) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_PROXY_SETTINGS:
                    proxySettingsString = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_PROXY_HOST:
                    proxyHost = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_PROXY_PORT:
                    proxyPort = (int) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_PROXY_EXCLUSION_LIST:
                    proxyExclusionList = (String) value;
                    break;
                case IpConfigurationXmlUtil.XML_TAG_PROXY_PAC_FILE:
                    proxyPacFile = (String) value;
                    break;
                default:
                    // should never happen, since other tags are filtered out earlier
                    throw new XmlPullParserException(
                            "Unknown value name found: " + valueName[0]);
            }
        }

        // Now process the values into IpConfiguration object
        IpConfiguration ipConfiguration = new IpConfiguration();
        if (ipAssignmentString == null) {
            throw new XmlPullParserException("IpAssignment was missing in IpConfiguration section");
        }
        IpAssignment ipAssignment = IpAssignment.valueOf(ipAssignmentString);
        ipConfiguration.setIpAssignment(ipAssignment);
        switch (ipAssignment) {
            case STATIC:
                StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
                if (linkAddressString != null && linkPrefixLength != null) {
                    LinkAddress linkAddress = new LinkAddress(
                            NetworkUtils.numericToInetAddress(linkAddressString), linkPrefixLength);
                    if (linkAddress.getAddress() instanceof Inet4Address) {
                        staticIpConfiguration.ipAddress = linkAddress;
                    } else {
                        Log.w(TAG, "Non-IPv4 address: " + linkAddress);
                    }
                }
                if (gatewayAddressString != null) {
                    LinkAddress dest = null;
                    InetAddress gateway = NetworkUtils.numericToInetAddress(gatewayAddressString);
                    RouteInfo route = new RouteInfo(dest, gateway);
                    if (route.isIPv4Default()) {
                        staticIpConfiguration.gateway = gateway;
                    } else {
                        Log.w(TAG, "Non-IPv4 default route: " + route);
                    }
                }
                if (dnsServerAddressesString != null) {
                    for (String dnsServerAddressString : dnsServerAddressesString) {
                        InetAddress dnsServerAddress =
                                NetworkUtils.numericToInetAddress(dnsServerAddressString);
                        staticIpConfiguration.dnsServers.add(dnsServerAddress);
                    }
                }
                ipConfiguration.setStaticIpConfiguration(staticIpConfiguration);
                break;
            case DHCP:
            case UNASSIGNED:
                break;
            default:
                throw new XmlPullParserException("Unknown ip assignment type: " + ipAssignment);
        }

        // Process the proxy settings next
        if (proxySettingsString == null) {
            throw new XmlPullParserException("ProxySettings was missing in"
                    + " IpConfiguration section");
        }
        ProxySettings proxySettings = ProxySettings.valueOf(proxySettingsString);
        ipConfiguration.setProxySettings(proxySettings);
        switch (proxySettings) {
            case STATIC:
                if (proxyHost == null) {
                    throw new XmlPullParserException("ProxyHost was missing in"
                            + " IpConfiguration section");
                }
                if (proxyPort == -1) {
                    throw new XmlPullParserException("ProxyPort was missing in"
                            + " IpConfiguration section");
                }
                if (proxyExclusionList == null) {
                    throw new XmlPullParserException("ProxyExclusionList was missing in"
                            + " IpConfiguration section");
                }
                ipConfiguration.setHttpProxy(
                        new ProxyInfo(proxyHost, proxyPort, proxyExclusionList));
                break;
            case PAC:
                if (proxyPacFile == null) {
                    throw new XmlPullParserException("ProxyPac was missing in"
                            + " IpConfiguration section");
                }
                ipConfiguration.setHttpProxy(new ProxyInfo(proxyPacFile));
                break;
            case NONE:
            case UNASSIGNED:
                break;
            default:
                throw new XmlPullParserException(
                        "Unknown proxy settings type: " + proxySettings);
        }

        return ipConfiguration;
    }

    /**
     * Returns a set of supported tags of <IpConfiguration> element for all minor versions of
     * this major version up to and including the specified minorVersion (only adding tags is
     * supported in minor versions, removal or changing the meaning of tags requires bumping
     * the major version and reseting the minor to 0).
     *
     * @param minorVersion  minor version number parsed from incoming data.
     */
    private static Set<String> getSupportedIpConfigurationTags(int minorVersion) {
        switch (minorVersion) {
            case 0: return IP_CONFIGURATION_MINOR_V0_SUPPORTED_TAGS;
            default:
                Log.e(TAG, "Invalid minorVersion: " + minorVersion);
                return Collections.<String>emptySet();
        }
    }
}