blob: 355265598365a7130946cd44a06ee79c4a90d5bd [file] [log] [blame]
Nathan Harold330e1082017-01-12 18:38:57 -08001/*
2 * Copyright (C) 2017 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 */
16package android.net;
17
18import android.os.Parcel;
19import android.os.Parcelable;
Nathan Harolda10003d2017-08-23 13:46:33 -070020
21import com.android.internal.annotations.VisibleForTesting;
Nathan Harold330e1082017-01-12 18:38:57 -080022
Jonathan Basseric61b70d2017-04-21 15:53:51 -070023/**
24 * This class encapsulates all the configuration parameters needed to create IPsec transforms and
25 * policies.
26 *
27 * @hide
28 */
Nathan Harold330e1082017-01-12 18:38:57 -080029public final class IpSecConfig implements Parcelable {
Nathan Harold93962f32017-03-07 13:23:36 -080030 private static final String TAG = "IpSecConfig";
Nathan Harold330e1082017-01-12 18:38:57 -080031
Nathan Harolda10003d2017-08-23 13:46:33 -070032 // MODE_TRANSPORT or MODE_TUNNEL
33 private int mMode = IpSecTransform.MODE_TRANSPORT;
Nathan Harold330e1082017-01-12 18:38:57 -080034
Nathan Harolda10003d2017-08-23 13:46:33 -070035 // Preventing this from being null simplifies Java->Native binder
Nathan Harolda2523312018-01-05 19:25:13 -080036 private String mSourceAddress = "";
Nathan Harold330e1082017-01-12 18:38:57 -080037
Nathan Harolda10003d2017-08-23 13:46:33 -070038 // Preventing this from being null simplifies Java->Native binder
Nathan Harolda2523312018-01-05 19:25:13 -080039 private String mDestinationAddress = "";
Nathan Harold330e1082017-01-12 18:38:57 -080040
Nathan Haroldd6f50b22017-10-04 12:58:55 -070041 // The underlying Network that represents the "gateway" Network
Nathan Harolda10003d2017-08-23 13:46:33 -070042 // for outbound packets. It may also be used to select packets.
43 private Network mNetwork;
Nathan Harold330e1082017-01-12 18:38:57 -080044
Nathan Harolda2523312018-01-05 19:25:13 -080045 // Minimum requirements for identifying a transform
46 // SPI identifying the IPsec SA in packet processing
47 // and a destination IP address
48 private int mSpiResourceId = IpSecManager.INVALID_RESOURCE_ID;
Nathan Harold330e1082017-01-12 18:38:57 -080049
Nathan Harolda2523312018-01-05 19:25:13 -080050 // Encryption Algorithm
51 private IpSecAlgorithm mEncryption;
Nathan Harold330e1082017-01-12 18:38:57 -080052
Nathan Harolda2523312018-01-05 19:25:13 -080053 // Authentication Algorithm
54 private IpSecAlgorithm mAuthentication;
ludib0c95b12017-05-22 10:52:23 -070055
Nathan Harolda2523312018-01-05 19:25:13 -080056 // Authenticated Encryption Algorithm
57 private IpSecAlgorithm mAuthenticatedEncryption;
Nathan Harold330e1082017-01-12 18:38:57 -080058
59 // For tunnel mode IPv4 UDP Encapsulation
60 // IpSecTransform#ENCAP_ESP_*, such as ENCAP_ESP_OVER_UDP_IKE
Nathan Harolda10003d2017-08-23 13:46:33 -070061 private int mEncapType = IpSecTransform.ENCAP_NONE;
62 private int mEncapSocketResourceId = IpSecManager.INVALID_RESOURCE_ID;
63 private int mEncapRemotePort;
Nathan Harold330e1082017-01-12 18:38:57 -080064
Nathan Harold330e1082017-01-12 18:38:57 -080065 // An interval, in seconds between the NattKeepalive packets
Nathan Harolda10003d2017-08-23 13:46:33 -070066 private int mNattKeepaliveInterval;
67
Benedict Wongab80e1f2018-07-25 18:46:19 -070068 // XFRM mark and mask; defaults to 0 (no mark/mask)
Di Lu0b611f42018-01-11 11:35:25 -080069 private int mMarkValue;
70 private int mMarkMask;
71
Benedict Wong781dae62018-09-06 11:31:25 -070072 // XFRM interface id
73 private int mXfrmInterfaceId;
74
Nathan Harolda10003d2017-08-23 13:46:33 -070075 /** Set the mode for this IPsec transform */
76 public void setMode(int mode) {
77 mMode = mode;
78 }
79
Nathan Harolda2523312018-01-05 19:25:13 -080080 /** Set the source IP addres for this IPsec transform */
81 public void setSourceAddress(String sourceAddress) {
82 mSourceAddress = sourceAddress;
Nathan Harolda10003d2017-08-23 13:46:33 -070083 }
84
Nathan Harolda2523312018-01-05 19:25:13 -080085 /** Set the destination IP address for this IPsec transform */
86 public void setDestinationAddress(String destinationAddress) {
87 mDestinationAddress = destinationAddress;
Nathan Harolda10003d2017-08-23 13:46:33 -070088 }
89
Nathan Harolda2523312018-01-05 19:25:13 -080090 /** Set the SPI by resource ID */
91 public void setSpiResourceId(int resourceId) {
92 mSpiResourceId = resourceId;
Nathan Harolda10003d2017-08-23 13:46:33 -070093 }
94
Nathan Harolda2523312018-01-05 19:25:13 -080095 /** Set the encryption algorithm */
96 public void setEncryption(IpSecAlgorithm encryption) {
97 mEncryption = encryption;
Nathan Harolda10003d2017-08-23 13:46:33 -070098 }
99
Nathan Harolda2523312018-01-05 19:25:13 -0800100 /** Set the authentication algorithm */
101 public void setAuthentication(IpSecAlgorithm authentication) {
102 mAuthentication = authentication;
Nathan Harolda10003d2017-08-23 13:46:33 -0700103 }
104
Nathan Harolda2523312018-01-05 19:25:13 -0800105 /** Set the authenticated encryption algorithm */
106 public void setAuthenticatedEncryption(IpSecAlgorithm authenticatedEncryption) {
107 mAuthenticatedEncryption = authenticatedEncryption;
Benedict Wong0febe5e2017-08-22 21:42:33 -0700108 }
109
Nathan Harolda2523312018-01-05 19:25:13 -0800110 /** Set the underlying network that will carry traffic for this transform */
Nathan Harolda10003d2017-08-23 13:46:33 -0700111 public void setNetwork(Network network) {
112 mNetwork = network;
113 }
114
115 public void setEncapType(int encapType) {
116 mEncapType = encapType;
117 }
118
119 public void setEncapSocketResourceId(int resourceId) {
120 mEncapSocketResourceId = resourceId;
121 }
122
123 public void setEncapRemotePort(int port) {
124 mEncapRemotePort = port;
125 }
126
127 public void setNattKeepaliveInterval(int interval) {
128 mNattKeepaliveInterval = interval;
129 }
Nathan Harold330e1082017-01-12 18:38:57 -0800130
Benedict Wongab80e1f2018-07-25 18:46:19 -0700131 /**
132 * Sets the mark value
133 *
134 * <p>Internal (System server) use only. Marks passed in by users will be overwritten or
135 * ignored.
136 */
Di Lu0b611f42018-01-11 11:35:25 -0800137 public void setMarkValue(int mark) {
138 mMarkValue = mark;
139 }
140
Benedict Wongab80e1f2018-07-25 18:46:19 -0700141 /**
142 * Sets the mark mask
143 *
144 * <p>Internal (System server) use only. Marks passed in by users will be overwritten or
145 * ignored.
146 */
Di Lu0b611f42018-01-11 11:35:25 -0800147 public void setMarkMask(int mask) {
148 mMarkMask = mask;
149 }
150
Benedict Wong781dae62018-09-06 11:31:25 -0700151 public void setXfrmInterfaceId(int xfrmInterfaceId) {
152 mXfrmInterfaceId = xfrmInterfaceId;
153 }
154
Nathan Harold93962f32017-03-07 13:23:36 -0800155 // Transport or Tunnel
156 public int getMode() {
Nathan Harolda10003d2017-08-23 13:46:33 -0700157 return mMode;
Nathan Harold93962f32017-03-07 13:23:36 -0800158 }
159
Nathan Harolda2523312018-01-05 19:25:13 -0800160 public String getSourceAddress() {
161 return mSourceAddress;
Nathan Harold330e1082017-01-12 18:38:57 -0800162 }
163
Nathan Harolda2523312018-01-05 19:25:13 -0800164 public int getSpiResourceId() {
165 return mSpiResourceId;
Nathan Harold330e1082017-01-12 18:38:57 -0800166 }
167
Nathan Harolda2523312018-01-05 19:25:13 -0800168 public String getDestinationAddress() {
169 return mDestinationAddress;
Nathan Harold330e1082017-01-12 18:38:57 -0800170 }
171
Nathan Harolda2523312018-01-05 19:25:13 -0800172 public IpSecAlgorithm getEncryption() {
173 return mEncryption;
Nathan Harold330e1082017-01-12 18:38:57 -0800174 }
175
Nathan Harolda2523312018-01-05 19:25:13 -0800176 public IpSecAlgorithm getAuthentication() {
177 return mAuthentication;
Nathan Harold330e1082017-01-12 18:38:57 -0800178 }
179
Nathan Harolda2523312018-01-05 19:25:13 -0800180 public IpSecAlgorithm getAuthenticatedEncryption() {
181 return mAuthenticatedEncryption;
Benedict Wong0febe5e2017-08-22 21:42:33 -0700182 }
183
Nathan Harold93962f32017-03-07 13:23:36 -0800184 public Network getNetwork() {
Nathan Harolda10003d2017-08-23 13:46:33 -0700185 return mNetwork;
Nathan Harold330e1082017-01-12 18:38:57 -0800186 }
187
188 public int getEncapType() {
Nathan Harolda10003d2017-08-23 13:46:33 -0700189 return mEncapType;
Nathan Harold330e1082017-01-12 18:38:57 -0800190 }
191
Nathan Harolda10003d2017-08-23 13:46:33 -0700192 public int getEncapSocketResourceId() {
193 return mEncapSocketResourceId;
Nathan Harold330e1082017-01-12 18:38:57 -0800194 }
195
196 public int getEncapRemotePort() {
Nathan Harolda10003d2017-08-23 13:46:33 -0700197 return mEncapRemotePort;
Nathan Harold330e1082017-01-12 18:38:57 -0800198 }
199
Nathan Harold93962f32017-03-07 13:23:36 -0800200 public int getNattKeepaliveInterval() {
Nathan Harolda10003d2017-08-23 13:46:33 -0700201 return mNattKeepaliveInterval;
Nathan Harold330e1082017-01-12 18:38:57 -0800202 }
203
Di Lu0b611f42018-01-11 11:35:25 -0800204 public int getMarkValue() {
205 return mMarkValue;
206 }
207
208 public int getMarkMask() {
209 return mMarkMask;
210 }
211
Benedict Wong781dae62018-09-06 11:31:25 -0700212 public int getXfrmInterfaceId() {
213 return mXfrmInterfaceId;
214 }
215
Nathan Harold330e1082017-01-12 18:38:57 -0800216 // Parcelable Methods
217
218 @Override
219 public int describeContents() {
220 return 0;
221 }
222
223 @Override
224 public void writeToParcel(Parcel out, int flags) {
Nathan Harolda10003d2017-08-23 13:46:33 -0700225 out.writeInt(mMode);
Nathan Harolda2523312018-01-05 19:25:13 -0800226 out.writeString(mSourceAddress);
227 out.writeString(mDestinationAddress);
Nathan Harolda10003d2017-08-23 13:46:33 -0700228 out.writeParcelable(mNetwork, flags);
Nathan Harolda2523312018-01-05 19:25:13 -0800229 out.writeInt(mSpiResourceId);
230 out.writeParcelable(mEncryption, flags);
231 out.writeParcelable(mAuthentication, flags);
232 out.writeParcelable(mAuthenticatedEncryption, flags);
Nathan Harolda10003d2017-08-23 13:46:33 -0700233 out.writeInt(mEncapType);
234 out.writeInt(mEncapSocketResourceId);
235 out.writeInt(mEncapRemotePort);
Nathan Harold19ce70b2017-09-25 19:33:13 -0700236 out.writeInt(mNattKeepaliveInterval);
Di Lu0b611f42018-01-11 11:35:25 -0800237 out.writeInt(mMarkValue);
238 out.writeInt(mMarkMask);
Benedict Wong781dae62018-09-06 11:31:25 -0700239 out.writeInt(mXfrmInterfaceId);
Nathan Harold330e1082017-01-12 18:38:57 -0800240 }
241
Nathan Harolda10003d2017-08-23 13:46:33 -0700242 @VisibleForTesting
243 public IpSecConfig() {}
Nathan Harold330e1082017-01-12 18:38:57 -0800244
Benedict Wong9dd3a382018-02-06 20:43:21 -0800245 /** Copy constructor */
246 @VisibleForTesting
247 public IpSecConfig(IpSecConfig c) {
248 mMode = c.mMode;
249 mSourceAddress = c.mSourceAddress;
250 mDestinationAddress = c.mDestinationAddress;
251 mNetwork = c.mNetwork;
252 mSpiResourceId = c.mSpiResourceId;
253 mEncryption = c.mEncryption;
254 mAuthentication = c.mAuthentication;
255 mAuthenticatedEncryption = c.mAuthenticatedEncryption;
256 mEncapType = c.mEncapType;
257 mEncapSocketResourceId = c.mEncapSocketResourceId;
258 mEncapRemotePort = c.mEncapRemotePort;
259 mNattKeepaliveInterval = c.mNattKeepaliveInterval;
260 mMarkValue = c.mMarkValue;
261 mMarkMask = c.mMarkMask;
Benedict Wong781dae62018-09-06 11:31:25 -0700262 mXfrmInterfaceId = c.mXfrmInterfaceId;
Benedict Wong9dd3a382018-02-06 20:43:21 -0800263 }
264
Nathan Harold330e1082017-01-12 18:38:57 -0800265 private IpSecConfig(Parcel in) {
Nathan Harolda10003d2017-08-23 13:46:33 -0700266 mMode = in.readInt();
Nathan Harolda2523312018-01-05 19:25:13 -0800267 mSourceAddress = in.readString();
268 mDestinationAddress = in.readString();
Nathan Harolda10003d2017-08-23 13:46:33 -0700269 mNetwork = (Network) in.readParcelable(Network.class.getClassLoader());
Nathan Harolda2523312018-01-05 19:25:13 -0800270 mSpiResourceId = in.readInt();
271 mEncryption =
Nathan Harold330e1082017-01-12 18:38:57 -0800272 (IpSecAlgorithm) in.readParcelable(IpSecAlgorithm.class.getClassLoader());
Nathan Harolda2523312018-01-05 19:25:13 -0800273 mAuthentication =
Nathan Harold330e1082017-01-12 18:38:57 -0800274 (IpSecAlgorithm) in.readParcelable(IpSecAlgorithm.class.getClassLoader());
Nathan Harolda2523312018-01-05 19:25:13 -0800275 mAuthenticatedEncryption =
Benedict Wong0febe5e2017-08-22 21:42:33 -0700276 (IpSecAlgorithm) in.readParcelable(IpSecAlgorithm.class.getClassLoader());
Nathan Harolda10003d2017-08-23 13:46:33 -0700277 mEncapType = in.readInt();
278 mEncapSocketResourceId = in.readInt();
279 mEncapRemotePort = in.readInt();
Nathan Harold19ce70b2017-09-25 19:33:13 -0700280 mNattKeepaliveInterval = in.readInt();
Di Lu0b611f42018-01-11 11:35:25 -0800281 mMarkValue = in.readInt();
282 mMarkMask = in.readInt();
Benedict Wong781dae62018-09-06 11:31:25 -0700283 mXfrmInterfaceId = in.readInt();
Nathan Harold330e1082017-01-12 18:38:57 -0800284 }
285
ludib0c95b12017-05-22 10:52:23 -0700286 @Override
287 public String toString() {
288 StringBuilder strBuilder = new StringBuilder();
289 strBuilder
Nathan Harolda10003d2017-08-23 13:46:33 -0700290 .append("{mMode=")
291 .append(mMode == IpSecTransform.MODE_TUNNEL ? "TUNNEL" : "TRANSPORT")
Nathan Harolda2523312018-01-05 19:25:13 -0800292 .append(", mSourceAddress=")
293 .append(mSourceAddress)
294 .append(", mDestinationAddress=")
295 .append(mDestinationAddress)
Nathan Harolda10003d2017-08-23 13:46:33 -0700296 .append(", mNetwork=")
297 .append(mNetwork)
298 .append(", mEncapType=")
299 .append(mEncapType)
300 .append(", mEncapSocketResourceId=")
301 .append(mEncapSocketResourceId)
302 .append(", mEncapRemotePort=")
303 .append(mEncapRemotePort)
304 .append(", mNattKeepaliveInterval=")
305 .append(mNattKeepaliveInterval)
Nathan Harolda2523312018-01-05 19:25:13 -0800306 .append("{mSpiResourceId=")
307 .append(mSpiResourceId)
308 .append(", mEncryption=")
309 .append(mEncryption)
310 .append(", mAuthentication=")
311 .append(mAuthentication)
312 .append(", mAuthenticatedEncryption=")
313 .append(mAuthenticatedEncryption)
Di Lu0b611f42018-01-11 11:35:25 -0800314 .append(", mMarkValue=")
315 .append(mMarkValue)
316 .append(", mMarkMask=")
317 .append(mMarkMask)
Benedict Wong781dae62018-09-06 11:31:25 -0700318 .append(", mXfrmInterfaceId=")
319 .append(mXfrmInterfaceId)
ludib0c95b12017-05-22 10:52:23 -0700320 .append("}");
321
322 return strBuilder.toString();
323 }
324
Nathan Harold330e1082017-01-12 18:38:57 -0800325 public static final Parcelable.Creator<IpSecConfig> CREATOR =
326 new Parcelable.Creator<IpSecConfig>() {
327 public IpSecConfig createFromParcel(Parcel in) {
328 return new IpSecConfig(in);
329 }
330
331 public IpSecConfig[] newArray(int size) {
332 return new IpSecConfig[size];
333 }
334 };
Nathan Harold19ce70b2017-09-25 19:33:13 -0700335
336 @VisibleForTesting
Nathan Haroldd6f50b22017-10-04 12:58:55 -0700337 /** Equals method used for testing */
Nathan Harold19ce70b2017-09-25 19:33:13 -0700338 public static boolean equals(IpSecConfig lhs, IpSecConfig rhs) {
339 if (lhs == null || rhs == null) return (lhs == rhs);
340 return (lhs.mMode == rhs.mMode
Nathan Harolda2523312018-01-05 19:25:13 -0800341 && lhs.mSourceAddress.equals(rhs.mSourceAddress)
342 && lhs.mDestinationAddress.equals(rhs.mDestinationAddress)
Nathan Harold19ce70b2017-09-25 19:33:13 -0700343 && ((lhs.mNetwork != null && lhs.mNetwork.equals(rhs.mNetwork))
344 || (lhs.mNetwork == rhs.mNetwork))
345 && lhs.mEncapType == rhs.mEncapType
346 && lhs.mEncapSocketResourceId == rhs.mEncapSocketResourceId
347 && lhs.mEncapRemotePort == rhs.mEncapRemotePort
348 && lhs.mNattKeepaliveInterval == rhs.mNattKeepaliveInterval
Nathan Harolda2523312018-01-05 19:25:13 -0800349 && lhs.mSpiResourceId == rhs.mSpiResourceId
350 && IpSecAlgorithm.equals(lhs.mEncryption, rhs.mEncryption)
Benedict Wong781dae62018-09-06 11:31:25 -0700351 && IpSecAlgorithm.equals(lhs.mAuthenticatedEncryption, rhs.mAuthenticatedEncryption)
Di Lu0b611f42018-01-11 11:35:25 -0800352 && IpSecAlgorithm.equals(lhs.mAuthentication, rhs.mAuthentication)
353 && lhs.mMarkValue == rhs.mMarkValue
Benedict Wong781dae62018-09-06 11:31:25 -0700354 && lhs.mMarkMask == rhs.mMarkMask
355 && lhs.mXfrmInterfaceId == rhs.mXfrmInterfaceId);
Nathan Harold19ce70b2017-09-25 19:33:13 -0700356 }
Nathan Harold330e1082017-01-12 18:38:57 -0800357}