1 /*
2  * Copyright (C) 2019 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 
17 package com.android.server.wifi;
18 
19 import android.app.compat.CompatChanges;
20 import android.content.Context;
21 import android.net.MacAddress;
22 import android.net.wifi.SoftApConfiguration;
23 import android.net.wifi.WifiConfiguration;
24 import android.net.wifi.WifiMigration;
25 import android.util.BackupUtils;
26 import android.util.Log;
27 import android.util.SparseIntArray;
28 import android.util.Xml;
29 
30 import com.android.internal.util.FastXmlSerializer;
31 import com.android.modules.utils.build.SdkLevel;
32 import com.android.server.wifi.util.ApConfigUtil;
33 import com.android.server.wifi.util.SettingsMigrationDataHolder;
34 import com.android.server.wifi.util.XmlUtil;
35 
36 import org.xmlpull.v1.XmlPullParser;
37 import org.xmlpull.v1.XmlPullParserException;
38 import org.xmlpull.v1.XmlSerializer;
39 
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.DataInputStream;
43 import java.io.DataOutputStream;
44 import java.io.IOException;
45 import java.nio.charset.StandardCharsets;
46 import java.util.ArrayList;
47 import java.util.Iterator;
48 import java.util.List;
49 
50 /**
51  * Class used to backup/restore data using the SettingsBackupAgent.
52  * There are 2 symmetric API's exposed here:
53  * 1. retrieveBackupDataFromSoftApConfiguration: Retrieve the configuration data to be backed up.
54  * 2. retrieveSoftApConfigurationFromBackupData: Restore the configuration using the provided data.
55  * The byte stream to be backed up is versioned to migrate the data easily across
56  * revisions.
57  */
58 public class SoftApBackupRestore {
59     private static final String TAG = "SoftApBackupRestore";
60 
61     /**
62      * Current backup data version.
63      */
64     // Starting from SoftAp data backup version 9, framework support to back up configuration
65     // in XML format. This allows to restore the SoftAp configuration when the user downgrades
66     // the Android version. (From Any version >= 9 to version#9)
67     private static final int SUPPORTED_SAP_BACKUP_XML_DATA_VERSION = 9;
68     private static final int LAST_SAP_BACKUP_DATA_VERSION_IN_S = 8;
69     private static final int LAST_SAP_BACKUP_DATA_VERSION_IN_R = 7;
70     private static final String XML_TAG_DOCUMENT_HEADER = "SoftApBackupData";
71 
72 
73     private static final int ETHER_ADDR_LEN = 6; // Byte array size of MacAddress
74 
75     private final Context mContext;
76     private final SettingsMigrationDataHolder mSettingsMigrationDataHolder;
77 
SoftApBackupRestore(Context context, SettingsMigrationDataHolder settingsMigrationDataHolder)78     public SoftApBackupRestore(Context context,
79             SettingsMigrationDataHolder settingsMigrationDataHolder) {
80         mContext = context;
81         mSettingsMigrationDataHolder = settingsMigrationDataHolder;
82     }
83 
84     /**
85      * Retrieve a byte stream representing the data that needs to be backed up from the
86      * provided softap configuration.
87      *
88      * @param config saved soft ap config that needs to be backed up.
89      * @return Raw byte stream that needs to be backed up.
90      */
retrieveBackupDataFromSoftApConfiguration(SoftApConfiguration config)91     public byte[] retrieveBackupDataFromSoftApConfiguration(SoftApConfiguration config) {
92         if (config == null) {
93             Log.e(TAG, "Invalid configuration received");
94             return new byte[0];
95         }
96         try {
97             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
98             final DataOutputStream out = new DataOutputStream(baos);
99             if (SdkLevel.isAtLeastT()) {
100                 out.writeInt(SUPPORTED_SAP_BACKUP_XML_DATA_VERSION);
101                 final XmlSerializer xmlOut = new FastXmlSerializer();
102                 xmlOut.setOutput(baos, StandardCharsets.UTF_8.name());
103                 XmlUtil.writeDocumentStart(xmlOut, XML_TAG_DOCUMENT_HEADER);
104 
105                 // Start writing the XML stream.
106                 XmlUtil.SoftApConfigurationXmlUtil.writeSoftApConfigurationToXml(xmlOut, config,
107                         null);
108 
109                 XmlUtil.writeDocumentEnd(xmlOut, XML_TAG_DOCUMENT_HEADER);
110 
111             } else {
112                 if (SdkLevel.isAtLeastS()) {
113                     out.writeInt(LAST_SAP_BACKUP_DATA_VERSION_IN_S);
114                 } else {
115                     out.writeInt(LAST_SAP_BACKUP_DATA_VERSION_IN_R);
116                 }
117                 BackupUtils.writeString(out, config.getSsid());
118                 out.writeInt(config.getBand());
119                 out.writeInt(config.getChannel());
120                 BackupUtils.writeString(out, config.getPassphrase());
121                 out.writeInt(config.getSecurityType());
122                 out.writeBoolean(config.isHiddenSsid());
123                 out.writeInt(config.getMaxNumberOfClients());
124                 out.writeLong(config.getShutdownTimeoutMillis());
125                 out.writeBoolean(config.isClientControlByUserEnabled());
126                 writeMacAddressList(out, config.getBlockedClientList());
127                 writeMacAddressList(out, config.getAllowedClientList());
128                 out.writeBoolean(config.isAutoShutdownEnabled());
129                 if (SdkLevel.isAtLeastS()) {
130                     out.writeBoolean(config.isBridgedModeOpportunisticShutdownEnabled());
131                     out.writeInt(config.getMacRandomizationSetting());
132                     SparseIntArray channels = config.getChannels();
133                     int numOfChannels = channels.size();
134                     out.writeInt(numOfChannels);
135                     for (int i = 0; i < numOfChannels; i++) {
136                         out.writeInt(channels.keyAt(i));
137                         out.writeInt(channels.valueAt(i));
138                     }
139                     out.writeBoolean(config.isIeee80211axEnabled());
140                 }
141             }
142             return baos.toByteArray();
143         } catch (IOException | XmlPullParserException e) {
144             Log.e(TAG, "Error retrieving the backup data from SoftApConfiguration: " +  config
145                     + ", exception " + e);
146         }
147         return new byte[0];
148     }
149 
150     /**
151      * Parse out the configurations from the back up data.
152      *
153      * @param data raw byte stream representing the data.
154      * @return Soft ap config retrieved from the backed up data.
155      */
retrieveSoftApConfigurationFromBackupData(byte[] data)156     public SoftApConfiguration retrieveSoftApConfigurationFromBackupData(byte[] data) {
157         if (data == null || data.length == 0) {
158             Log.e(TAG, "Invalid backup data received");
159             return null;
160         }
161         SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
162         try {
163             DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
164             int version = in.readInt();
165             // Starting from T, frameworks support to downgrade restore configuration.
166             if ((!SdkLevel.isAtLeastT() && version > LAST_SAP_BACKUP_DATA_VERSION_IN_S)
167                     || version < 1) {
168                 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
169             }
170 
171             if (version == 1) return null; // Version 1 is a bad dataset.
172             Log.i(TAG, "The backed-up version is " + version);
173 
174             if (version >= SUPPORTED_SAP_BACKUP_XML_DATA_VERSION) {
175                 final XmlPullParser xmlIn = Xml.newPullParser();
176                 ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
177                 // The first 4 bytes are designed to store version
178                 inputStream.skip(Integer.BYTES);
179                 xmlIn.setInput(inputStream, StandardCharsets.UTF_8.name());
180                 XmlUtil.gotoDocumentStart(xmlIn, XML_TAG_DOCUMENT_HEADER);
181                 int rootTagDepth = xmlIn.getDepth();
182                 return XmlUtil.SoftApConfigurationXmlUtil
183                         .parseFromXml(xmlIn, rootTagDepth, mSettingsMigrationDataHolder, false,
184                                 null);
185             }
186             configBuilder.setSsid(BackupUtils.readString(in));
187 
188             int band;
189             if (version < 4) {
190                 band = ApConfigUtil.convertWifiConfigBandToSoftApConfigBand(in.readInt());
191             } else {
192                 band = in.readInt();
193             }
194             int channel = in.readInt();
195             if (channel == 0) {
196                 configBuilder.setBand(band);
197             } else {
198                 configBuilder.setChannel(channel, band);
199             }
200             String passphrase = BackupUtils.readString(in);
201             int securityType = in.readInt();
202             if (version < 4 && securityType == WifiConfiguration.KeyMgmt.WPA2_PSK) {
203                 configBuilder.setPassphrase(passphrase, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
204             } else if (version >= 4 && securityType != SoftApConfiguration.SECURITY_TYPE_OPEN) {
205                 configBuilder.setPassphrase(passphrase, securityType);
206             }
207             if (version >= 3) {
208                 configBuilder.setHiddenSsid(in.readBoolean());
209             }
210             if (version >= 5) {
211                 configBuilder.setMaxNumberOfClients(in.readInt());
212                 long shutDownMillis;
213                 if (version >= 7) {
214                     shutDownMillis = in.readLong();
215                 } else {
216                     shutDownMillis = Long.valueOf(in.readInt());
217                 }
218                 if (shutDownMillis == 0 && CompatChanges.isChangeEnabled(
219                         SoftApConfiguration.REMOVE_ZERO_FOR_TIMEOUT_SETTING)) {
220                     shutDownMillis = SoftApConfiguration.DEFAULT_TIMEOUT;
221                 }
222                 configBuilder.setShutdownTimeoutMillis(shutDownMillis);
223                 configBuilder.setClientControlByUserEnabled(in.readBoolean());
224                 int numberOfBlockedClient = in.readInt();
225                 List<MacAddress> blockedList = new ArrayList<>(
226                         macAddressListFromByteArray(in, numberOfBlockedClient));
227                 int numberOfAllowedClient = in.readInt();
228                 List<MacAddress> allowedList = new ArrayList<>(
229                         macAddressListFromByteArray(in, numberOfAllowedClient));
230                 configBuilder.setBlockedClientList(blockedList);
231                 configBuilder.setAllowedClientList(allowedList);
232             }
233             if (version >= 6) {
234                 configBuilder.setAutoShutdownEnabled(in.readBoolean());
235             } else {
236                 // Migrate data out of settings.
237                 WifiMigration.SettingsMigrationData migrationData =
238                         mSettingsMigrationDataHolder.retrieveData();
239                 if (migrationData == null) {
240                     Log.e(TAG, "No migration data present");
241                 } else {
242                     configBuilder.setAutoShutdownEnabled(migrationData.isSoftApTimeoutEnabled());
243                 }
244             }
245             if (version >= 8 && SdkLevel.isAtLeastS()) {
246                 configBuilder.setBridgedModeOpportunisticShutdownEnabled(in.readBoolean());
247                 configBuilder.setMacRandomizationSetting(in.readInt());
248                 int numOfChannels = in.readInt();
249                 SparseIntArray channels = new SparseIntArray(numOfChannels);
250                 for (int i = 0; i < numOfChannels; i++) {
251                     channels.put(in.readInt(), in.readInt());
252                 }
253                 configBuilder.setChannels(channels);
254                 configBuilder.setIeee80211axEnabled(in.readBoolean());
255             }
256             return configBuilder.build();
257         } catch (IOException | BackupUtils.BadVersionException
258                 | IllegalArgumentException | XmlPullParserException e) {
259             Log.e(TAG, "Invalid backup data received, Exception: " + e);
260         }
261         return null;
262     }
263 
writeMacAddressList(DataOutputStream out, List<MacAddress> macList)264     private void writeMacAddressList(DataOutputStream out, List<MacAddress> macList)
265             throws IOException {
266         out.writeInt(macList.size());
267         Iterator<MacAddress> iterator = macList.iterator();
268         while (iterator.hasNext()) {
269             byte[] mac = iterator.next().toByteArray();
270             out.write(mac, 0, ETHER_ADDR_LEN);
271         }
272     }
273 
macAddressListFromByteArray(DataInputStream in, int numberOfClients)274     private List<MacAddress> macAddressListFromByteArray(DataInputStream in, int numberOfClients)
275             throws IOException {
276         List<MacAddress> macList = new ArrayList<>();
277         for (int i = 0; i < numberOfClients; i++) {
278             byte[] mac = new byte[ETHER_ADDR_LEN];
279             in.read(mac, 0, ETHER_ADDR_LEN);
280             macList.add(MacAddress.fromBytes(mac));
281         }
282         return macList;
283     }
284 }
285