blob: 2dc840694a2371b6e0f6218c6a69e3d214d6e8f5 [file] [log] [blame]
Nathan Harold1a371532017-01-30 12:30:48 -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 */
16#ifndef _XFRM_CONTROLLER_H
17#define _XFRM_CONTROLLER_H
18
19#include <atomic>
20#include <list>
21#include <map>
22#include <string>
23#include <utility> // for pair
24
25#include <linux/netlink.h>
Nathan Harold420ceac2017-04-05 19:36:59 -070026#include <linux/udp.h>
Nathan Harold1a371532017-01-30 12:30:48 -080027#include <linux/xfrm.h>
28#include <sysutils/SocketClient.h>
Nathan Harold1a371532017-01-30 12:30:48 -080029
30#include "NetdConstants.h"
ludi6e8eccd2017-08-14 14:40:37 -070031#include "netdutils/Status.h"
Nathan Harold1a371532017-01-30 12:30:48 -080032
33namespace android {
34namespace net {
35
Nathan Harold39b5df42017-08-02 18:45:25 -070036// Exposed for testing
37extern const uint32_t ALGO_MASK_AUTH_ALL;
38// Exposed for testing
39extern const uint32_t ALGO_MASK_CRYPT_ALL;
40// Exposed for testing
Benedict Wongbe65b432017-08-22 21:43:14 -070041extern const uint32_t ALGO_MASK_AEAD_ALL;
42// Exposed for testing
Nathan Harold39b5df42017-08-02 18:45:25 -070043extern const uint8_t REPLAY_WINDOW_SIZE;
44
Nathan Harold1a371532017-01-30 12:30:48 -080045// Suggest we avoid the smallest and largest ints
46class XfrmMessage;
47class TransportModeSecurityAssociation;
48
49class XfrmSocket {
50public:
51 virtual void close() {
Nathan Harold420ceac2017-04-05 19:36:59 -070052 if (mSock >= 0) {
Nathan Harold1a371532017-01-30 12:30:48 -080053 ::close(mSock);
54 }
55 mSock = -1;
56 }
57
ludi6e8eccd2017-08-14 14:40:37 -070058 virtual netdutils::Status open() = 0;
Nathan Harold1a371532017-01-30 12:30:48 -080059
60 virtual ~XfrmSocket() { close(); }
61
ludie51e3862017-08-15 19:28:12 -070062 // Sends the netlink message contained in iovecs. This populates iovecs[0] with
63 // a valid netlink message header.
ludi6e8eccd2017-08-14 14:40:37 -070064 virtual netdutils::Status sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags,
65 uint16_t nlMsgSeqNum,
66 std::vector<iovec>* iovecs) const = 0;
Nathan Harold1a371532017-01-30 12:30:48 -080067
68protected:
69 int mSock;
70};
71
72enum struct XfrmDirection : uint8_t {
73 IN = XFRM_POLICY_IN,
74 OUT = XFRM_POLICY_OUT,
75 FORWARD = XFRM_POLICY_FWD,
76 MASK = XFRM_POLICY_MASK,
77};
78
79enum struct XfrmMode : uint8_t {
80 TRANSPORT = XFRM_MODE_TRANSPORT,
81 TUNNEL = XFRM_MODE_TUNNEL,
82};
83
Nathan Harold420ceac2017-04-05 19:36:59 -070084enum struct XfrmEncapType : uint16_t {
85 NONE = 0,
86 ESPINUDP_NON_IKE = UDP_ENCAP_ESPINUDP_NON_IKE,
87 ESPINUDP = UDP_ENCAP_ESPINUDP
88};
89
Nathan Harold1a371532017-01-30 12:30:48 -080090struct XfrmAlgo {
91 std::string name;
92 std::vector<uint8_t> key;
93 uint16_t truncLenBits;
94};
95
Nathan Harold420ceac2017-04-05 19:36:59 -070096struct XfrmEncap {
97 XfrmEncapType type;
98 uint16_t srcPort;
99 uint16_t dstPort;
100};
101
Nathan Harold7441c692017-12-12 21:25:01 -0800102// minimally sufficient structure to match either an SA or a Policy
103struct XfrmId {
Nathan Harold1a371532017-01-30 12:30:48 -0800104 xfrm_address_t dstAddr; // network order
105 xfrm_address_t srcAddr;
106 int addrFamily; // AF_INET or AF_INET6
107 int transformId; // requestId
108 int spi;
Di Lu2ccb3e52018-01-03 16:19:20 -0800109 xfrm_mark mark;
Nathan Harold1a371532017-01-30 12:30:48 -0800110};
111
Nathan Harold7441c692017-12-12 21:25:01 -0800112struct XfrmSaInfo : XfrmId {
Nathan Harold1a371532017-01-30 12:30:48 -0800113 XfrmAlgo auth;
114 XfrmAlgo crypt;
Benedict Wongbe65b432017-08-22 21:43:14 -0700115 XfrmAlgo aead;
Nathan Harold1a371532017-01-30 12:30:48 -0800116 int netId;
117 XfrmMode mode;
Nathan Harold420ceac2017-04-05 19:36:59 -0700118 XfrmEncap encap;
Nathan Harold1a371532017-01-30 12:30:48 -0800119};
120
121class XfrmController {
122public:
123 XfrmController();
124
Benedict Wongb2daefb2017-12-06 22:05:46 -0800125 netdutils::Status ipSecSetEncapSocketOwner(const android::base::unique_fd& socket, int newUid,
126 uid_t callerUid);
127
Nathan Haroldda54f122018-01-09 16:42:57 -0800128 netdutils::Status ipSecAllocateSpi(int32_t transformId, const std::string& localAddress,
ludi6e8eccd2017-08-14 14:40:37 -0700129 const std::string& remoteAddress, int32_t inSpi,
130 int32_t* outSpi);
Nathan Harold1a371532017-01-30 12:30:48 -0800131
ludi6e8eccd2017-08-14 14:40:37 -0700132 netdutils::Status ipSecAddSecurityAssociation(
Nathan Haroldda54f122018-01-09 16:42:57 -0800133 int32_t transformId, int32_t mode, const std::string& sourceAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800134 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800135 int32_t markValue, int32_t markMask, const std::string& authAlgo,
136 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
137 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
138 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
139 int32_t encapLocalPort, int32_t encapRemotePort);
Nathan Harold1a371532017-01-30 12:30:48 -0800140
Nathan Haroldda54f122018-01-09 16:42:57 -0800141 netdutils::Status ipSecDeleteSecurityAssociation(int32_t transformId,
Di Lu2ccb3e52018-01-03 16:19:20 -0800142 const std::string& sourceAddress,
143 const std::string& destinationAddress,
144 int32_t spi, int32_t markValue,
145 int32_t markMask);
Nathan Harold1a371532017-01-30 12:30:48 -0800146
ludi6e8eccd2017-08-14 14:40:37 -0700147 netdutils::Status ipSecApplyTransportModeTransform(const android::base::unique_fd& socket,
148 int32_t transformId, int32_t direction,
149 const std::string& localAddress,
150 const std::string& remoteAddress,
151 int32_t spi);
Nathan Harold1a371532017-01-30 12:30:48 -0800152
ludi6e8eccd2017-08-14 14:40:37 -0700153 netdutils::Status ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket);
Nathan Harold1a371532017-01-30 12:30:48 -0800154
ludi771b8002017-11-20 15:09:05 -0800155 netdutils::Status ipSecAddSecurityPolicy(int32_t transformId, int32_t direction,
156 const std::string& sourceAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800157 const std::string& destinationAddress, int32_t spi,
158 int32_t markValue, int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800159
160 netdutils::Status ipSecUpdateSecurityPolicy(int32_t transformId, int32_t direction,
161 const std::string& sourceAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800162 const std::string& destinationAddress, int32_t spi,
163 int32_t markValue, int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800164
165 netdutils::Status ipSecDeleteSecurityPolicy(int32_t transformId, int32_t direction,
166 const std::string& sourceAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800167 const std::string& destinationAddress,
168 int32_t markValue, int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800169
Jonathan Basseric6461a62017-08-31 14:47:33 -0700170 // Some XFRM netlink attributes comprise a header, a struct, and some data
171 // after the struct. We wrap all of those in one struct for easier
172 // marshalling. The structs below must be ABI compatible with the kernel and
173 // are composed from kernel structures; thus, they use the kernel naming
174 // convention.
175
Nathan Harold39b5df42017-08-02 18:45:25 -0700176 // Exposed for testing
Benedict Wong4f60ee12017-10-10 12:27:25 -0700177 static constexpr size_t MAX_KEY_LENGTH = 128;
Nathan Harold39b5df42017-08-02 18:45:25 -0700178
Jonathan Basseric6461a62017-08-31 14:47:33 -0700179 // Container for the content of an XFRMA_ALG_CRYPT netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700180 // Exposed for testing
181 struct nlattr_algo_crypt {
182 nlattr hdr;
183 xfrm_algo crypt;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700184 uint8_t key[MAX_KEY_LENGTH];
Nathan Harold39b5df42017-08-02 18:45:25 -0700185 };
186
Jonathan Basseric6461a62017-08-31 14:47:33 -0700187 // Container for the content of an XFRMA_ALG_AUTH_TRUNC netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700188 // Exposed for testing
189 struct nlattr_algo_auth {
190 nlattr hdr;
191 xfrm_algo_auth auth;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700192 uint8_t key[MAX_KEY_LENGTH];
Nathan Harold39b5df42017-08-02 18:45:25 -0700193 };
194
Jonathan Basseric6461a62017-08-31 14:47:33 -0700195 // Container for the content of an XFRMA_TMPL netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700196 // Exposed for testing
Benedict Wongbe65b432017-08-22 21:43:14 -0700197 struct nlattr_algo_aead {
198 nlattr hdr;
199 xfrm_algo_aead aead;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700200 uint8_t key[MAX_KEY_LENGTH];
Benedict Wongbe65b432017-08-22 21:43:14 -0700201 };
202
203 // Exposed for testing
Nathan Harold39b5df42017-08-02 18:45:25 -0700204 struct nlattr_user_tmpl {
205 nlattr hdr;
206 xfrm_user_tmpl tmpl;
207 };
208
Jonathan Basseric6461a62017-08-31 14:47:33 -0700209 // Container for the content of an XFRMA_ENCAP netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700210 // Exposed for testing
211 struct nlattr_encap_tmpl {
212 nlattr hdr;
213 xfrm_encap_tmpl tmpl;
214 };
215
Di Lu2ccb3e52018-01-03 16:19:20 -0800216 // Container for the content of an XFRMA_MARK netlink attribute.
217 // Exposed for testing
218 struct nlattr_xfrm_mark {
219 nlattr hdr;
220 xfrm_mark mark;
221 };
222
Benedict Wong96abf482018-01-22 13:56:41 -0800223 // Container for the content of an XFRMA_OUTPUT_MARK netlink attribute.
224 // Exposed for testing
225 struct nlattr_xfrm_output_mark {
226 nlattr hdr;
227 __u32 outputMark;
228 };
229
Nathan Harold1a371532017-01-30 12:30:48 -0800230private:
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700231/*
232 * Below is a redefinition of the xfrm_usersa_info struct that is part
233 * of the Linux uapi <linux/xfrm.h> to align the structures to a 64-bit
234 * boundary.
235 */
236#ifdef NETLINK_COMPAT32
237 // Shadow the kernel definition of xfrm_usersa_info with a 64-bit aligned version
238 struct xfrm_usersa_info : ::xfrm_usersa_info {
239 } __attribute__((aligned(8)));
240 // Shadow the kernel's version, using the aligned version of xfrm_usersa_info
241 struct xfrm_userspi_info {
242 struct xfrm_usersa_info info;
243 __u32 min;
244 __u32 max;
245 };
246
247 /*
248 * Anyone who encounters a failure when sending netlink messages should look here
249 * first. Hitting the static_assert() below should be a strong hint that Android
250 * IPsec will probably not work with your current settings.
251 *
252 * Again, experimentally determined, the "flags" field should be the first byte in
253 * the final word of the xfrm_usersa_info struct. The check validates the size of
254 * the padding to be 7.
255 *
256 * This padding is verified to be correct on gcc/x86_64 kernel, and clang/x86 userspace.
257 */
258 static_assert(sizeof(::xfrm_usersa_info) % 8 != 0, "struct xfrm_usersa_info has changed "
259 "alignment. Please consider whether this "
260 "patch is needed.");
261 static_assert(sizeof(xfrm_usersa_info) - offsetof(xfrm_usersa_info, flags) == 8,
262 "struct xfrm_usersa_info probably misaligned with kernel struct.");
263 static_assert(sizeof(xfrm_usersa_info) % 8 == 0, "struct xfrm_usersa_info_t is not 64-bit "
264 "aligned. Please consider whether this patch "
265 "is needed.");
266 static_assert(sizeof(::xfrm_userspi_info) - sizeof(::xfrm_usersa_info) ==
267 sizeof(xfrm_userspi_info) - sizeof(xfrm_usersa_info),
268 "struct xfrm_userspi_info has changed and does not match the kernel struct.");
269#endif
270
Nathan Harold7441c692017-12-12 21:25:01 -0800271 // helper function for filling in the XfrmId (and XfrmSaInfo) structure
Nathan Haroldda54f122018-01-09 16:42:57 -0800272 static netdutils::Status fillXfrmId(const std::string& sourceAddress,
273 const std::string& destinationAddress, int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800274 int32_t markValue, int32_t markMask, int32_t transformId,
275 XfrmId* xfrmId);
Nathan Harold1a371532017-01-30 12:30:48 -0800276
277 // Top level functions for managing a Transport Mode Transform
ludi6e8eccd2017-08-14 14:40:37 -0700278 static netdutils::Status addTransportModeTransform(const XfrmSaInfo& record);
Nathan Harold1a371532017-01-30 12:30:48 -0800279 static int removeTransportModeTransform(const XfrmSaInfo& record);
280
281 // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE
282 // Shared between SA and SP
ludi771b8002017-11-20 15:09:05 -0800283 static void fillXfrmSelector(const XfrmSaInfo& record, xfrm_selector* selector);
Nathan Harold1a371532017-01-30 12:30:48 -0800284
285 // Shared between Transport and Tunnel Mode
286 static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo);
287 static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo);
Benedict Wongbe65b432017-08-22 21:43:14 -0700288 static int fillNlAttrXfrmAlgoAead(const XfrmAlgo& in_algo, nlattr_algo_aead* algo);
Nathan Harold420ceac2017-04-05 19:36:59 -0700289 static int fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl);
Nathan Harold1a371532017-01-30 12:30:48 -0800290
Di Lu0e16b5e2018-01-12 10:37:53 -0800291 // Functions for updating a Transport Mode SA
292 static netdutils::Status updateSecurityAssociation(const XfrmSaInfo& record,
manojboopathi4d2d6f12017-12-06 11:11:31 -0800293 const XfrmSocket& sock);
Nathan Harold1a371532017-01-30 12:30:48 -0800294 static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa);
295
296 // Functions for deleting a Transport Mode SA
Nathan Harold7441c692017-12-12 21:25:01 -0800297 static netdutils::Status deleteSecurityAssociation(const XfrmId& record,
ludi6e8eccd2017-08-14 14:40:37 -0700298 const XfrmSocket& sock);
Nathan Harold7441c692017-12-12 21:25:01 -0800299 static int fillUserSaId(const XfrmId& record, xfrm_usersa_id* said);
Nathan Harold1a371532017-01-30 12:30:48 -0800300 static int fillUserTemplate(const XfrmSaInfo& record, xfrm_user_tmpl* tmpl);
ludi771b8002017-11-20 15:09:05 -0800301
Nathan Haroldda54f122018-01-09 16:42:57 -0800302 static int fillTransportModeUserSpInfo(const XfrmSaInfo& record, XfrmDirection direction,
303 xfrm_userpolicy_info* usersp);
ludi771b8002017-11-20 15:09:05 -0800304 static int fillNlAttrUserTemplate(const XfrmSaInfo& record, nlattr_user_tmpl* tmpl);
305 static int fillUserPolicyId(const XfrmSaInfo& record, XfrmDirection direction,
306 xfrm_userpolicy_id* policy_id);
Di Lu2ccb3e52018-01-03 16:19:20 -0800307 static int fillNlAttrXfrmMark(const XfrmId& record, nlattr_xfrm_mark* mark);
Benedict Wong96abf482018-01-22 13:56:41 -0800308 static int fillNlAttrXfrmOutputMark(const __u32 output_mark_value,
309 nlattr_xfrm_output_mark* output_mark);
Nathan Harold1a371532017-01-30 12:30:48 -0800310
ludi6e8eccd2017-08-14 14:40:37 -0700311 static netdutils::Status allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi,
312 uint32_t* outSpi, const XfrmSocket& sock);
Nathan Harold1a371532017-01-30 12:30:48 -0800313
ludi771b8002017-11-20 15:09:05 -0800314 static netdutils::Status processSecurityPolicy(int32_t transformId, int32_t direction,
315 const std::string& localAddress,
316 const std::string& remoteAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800317 int32_t spi, int32_t markValue,
318 int32_t markMask, int32_t msgType);
ludi771b8002017-11-20 15:09:05 -0800319 static netdutils::Status updateTunnelModeSecurityPolicy(const XfrmSaInfo& record,
320 const XfrmSocket& sock,
321 XfrmDirection direction,
322 uint16_t msgType);
323 static netdutils::Status deleteTunnelModeSecurityPolicy(const XfrmSaInfo& record,
324 const XfrmSocket& sock,
325 XfrmDirection direction);
326
Nathan Harold1a371532017-01-30 12:30:48 -0800327 // END TODO(messagerefactor)
328};
329
330} // namespace net
331} // namespace android
332
333#endif /* !defined(XFRM_CONTROLLER_H) */