aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ike/ikev2/TunnelModeChildSessionOptions.java
blob: 541930ebf1a2b68c14fbedf57437a637df021d70 (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
/*
 * Copyright (C) 2019 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.ike.ikev2;

import static android.system.OsConstants.AF_INET;
import static android.system.OsConstants.AF_INET6;

import android.annotation.NonNull;
import android.net.LinkAddress;

import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttribute;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Address;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Dhcp;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Dns;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Netmask;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Subnet;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Address;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Dns;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Subnet;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.LinkedList;
import java.util.List;

/**
 * This class contains all user provided configuration options for negotiating a tunnel mode Child
 * Session.
 */
public final class TunnelModeChildSessionOptions extends ChildSessionOptions {
    private final ConfigAttribute[] mConfigRequests;

    private TunnelModeChildSessionOptions(
            IkeTrafficSelector[] localTs,
            IkeTrafficSelector[] remoteTs,
            ChildSaProposal[] proposals,
            ConfigAttribute[] configRequests) {
        super(localTs, remoteTs, proposals, false /*isTransport*/);
        mConfigRequests = configRequests;
    }

    /** Package private */
    ConfigAttribute[] getConfigurationRequests() {
        return mConfigRequests;
    }

    /** This class can be used to incrementally construct a TunnelModeChildSessionOptions. */
    public static final class Builder extends ChildSessionOptions.Builder {
        private static final int IPv4_DEFAULT_PREFIX_LEN = 32;

        private boolean mHasIp4AddressRequest;
        private List<ConfigAttribute> mConfigRequestList;

        /** Create a Builder for negotiating a transport mode Child Session. */
        public Builder() {
            super();
            mHasIp4AddressRequest = false;
            mConfigRequestList = new LinkedList<>();
        }

        /**
         * Adds an Child SA proposal to TunnelModeChildSessionOptions being built.
         *
         * @param proposal Child SA proposal.
         * @return Builder this, to facilitate chaining.
         * @throws IllegalArgumentException if input proposal is not a Child SA proposal.
         */
        public Builder addSaProposal(@NonNull ChildSaProposal proposal) {
            validateAndAddSaProposal(proposal);
            return this;
        }

        /**
         * Adds internal IP address requests to TunnelModeChildSessionOptions being built.
         *
         * @param addressFamily the address family. Only {@link OsConstants.AF_INET} and {@link
         *     OsConstants.AF_INET6} are allowed.
         * @param numOfRequest the number of requests for this type of address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalAddressRequest(int addressFamily, int numOfRequest) {
            if (addressFamily == AF_INET) {
                mHasIp4AddressRequest = true;
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv4Address());
                }
                return this;
            } else if (addressFamily == AF_INET6) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv6Address());
                }
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address family: " + addressFamily);
            }
        }

        /**
         * Adds specific internal IP address request to TunnelModeChildSessionOptions being built.
         *
         * @param address the requested address.
         * @param prefixLen length of the InetAddress prefix. When requesting an IPv4 address,
         *     prefixLen MUST be 32.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalAddressRequest(@NonNull InetAddress address, int prefixLen) {
            if (address instanceof Inet4Address) {
                if (prefixLen != IPv4_DEFAULT_PREFIX_LEN) {
                    throw new IllegalArgumentException("Invalid IPv4 prefix length: " + prefixLen);
                }
                mHasIp4AddressRequest = true;
                mConfigRequestList.add(new ConfigAttributeIpv4Address((Inet4Address) address));
                return this;
            } else if (address instanceof Inet6Address) {
                mConfigRequestList.add(
                        new ConfigAttributeIpv6Address(new LinkAddress(address, prefixLen)));
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address " + address);
            }
        }

        /**
         * Adds internal DNS server requests to TunnelModeChildSessionOptions being built.
         *
         * @param addressFamily the address family. Only {@link OsConstants.AF_INET} and {@link
         *     OsConstants.AF_INET6} are allowed.
         * @param numOfRequest the number of requests for this type of address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalDnsServerRequest(int addressFamily, int numOfRequest) {
            if (addressFamily == AF_INET) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv4Dns());
                }
                return this;
            } else if (addressFamily == AF_INET6) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv6Dns());
                }
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address family: " + addressFamily);
            }
        }

        /**
         * Adds internal DNS server requests to TunnelModeChildSessionOptions being built.
         *
         * @param address the requested DNS server address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalDnsServerRequest(@NonNull InetAddress address) {
            if (address instanceof Inet4Address) {
                mConfigRequestList.add(new ConfigAttributeIpv4Dns((Inet4Address) address));
                return this;
            } else if (address instanceof Inet6Address) {
                mConfigRequestList.add(new ConfigAttributeIpv6Dns((Inet6Address) address));
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address " + address);
            }
        }

        /**
         * Adds internal subnet requests to TunnelModeChildSessionOptions being built.
         *
         * @param addressFamily the address family. Only {@link OsConstants.AF_INET} and {@link
         *     OsConstants.AF_INET6} are allowed.
         * @param numOfRequest the number of requests for this type of address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalSubnetRequest(int addressFamily, int numOfRequest) {
            if (addressFamily == AF_INET) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv4Subnet());
                }
                return this;
            } else if (addressFamily == AF_INET6) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv6Subnet());
                }
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address family: " + addressFamily);
            }
        }

        /**
         * Adds internal DHCP server requests to TunnelModeChildSessionOptions being built.
         *
         * <p>Only DHCP4 server requests are supported.
         *
         * @param addressFamily the address family. Only {@link OsConstants.AF_INET} is allowed.
         * @param numOfRequest the number of requests for this type of address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalDhcpServerRequest(int addressFamily, int numOfRequest) {
            if (addressFamily == AF_INET) {
                for (int i = 0; i < numOfRequest; i++) {
                    mConfigRequestList.add(new ConfigAttributeIpv4Dhcp());
                }
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address family: " + addressFamily);
            }
        }

        /**
         * Adds internal DHCP server requests to TunnelModeChildSessionOptions being built.
         *
         * <p>Only DHCP4 server requests are supported.
         *
         * @param address the requested DHCP server address.
         * @return Builder this, to facilitate chaining.
         */
        public Builder addInternalDhcpServerRequest(@NonNull InetAddress address) {
            if (address instanceof Inet4Address) {
                mConfigRequestList.add(new ConfigAttributeIpv4Dhcp((Inet4Address) address));
                return this;
            } else {
                throw new IllegalArgumentException("Invalid address " + address);
            }
        }

        /**
         * Validates, builds and returns the TunnelModeChildSessionOptions.
         *
         * @return the validated TunnelModeChildSessionOptions.
         * @throws IllegalArgumentException if no Child SA proposal is provided.
         */
        public TunnelModeChildSessionOptions build() {
            validateOrThrow();

            if (mHasIp4AddressRequest) {
                mConfigRequestList.add(new ConfigAttributeIpv4Netmask());
            }

            return new TunnelModeChildSessionOptions(
                    mLocalTsList.toArray(new IkeTrafficSelector[mLocalTsList.size()]),
                    mRemoteTsList.toArray(new IkeTrafficSelector[mRemoteTsList.size()]),
                    mSaProposalList.toArray(new ChildSaProposal[mSaProposalList.size()]),
                    mConfigRequestList.toArray(new ConfigAttribute[mConfigRequestList.size()]));
        }
    }
}