blob: 96a2faeedbdf8b369af6243fe33f9d12afce7adb [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
manojboopathi8707f232018-01-02 14:45:47 -080025#include <linux/if_link.h>
26#include <linux/if_tunnel.h>
Nathan Harold1a371532017-01-30 12:30:48 -080027#include <linux/netlink.h>
Nathan Harold420ceac2017-04-05 19:36:59 -070028#include <linux/udp.h>
Nathan Harold1a371532017-01-30 12:30:48 -080029#include <linux/xfrm.h>
30#include <sysutils/SocketClient.h>
Nathan Harold1a371532017-01-30 12:30:48 -080031
32#include "NetdConstants.h"
manojboopathi8707f232018-01-02 14:45:47 -080033#include "netdutils/Slice.h"
ludi6e8eccd2017-08-14 14:40:37 -070034#include "netdutils/Status.h"
Nathan Harold1a371532017-01-30 12:30:48 -080035
36namespace android {
37namespace net {
38
Nathan Harold39b5df42017-08-02 18:45:25 -070039// Exposed for testing
40extern const uint32_t ALGO_MASK_AUTH_ALL;
41// Exposed for testing
42extern const uint32_t ALGO_MASK_CRYPT_ALL;
43// Exposed for testing
Benedict Wongbe65b432017-08-22 21:43:14 -070044extern const uint32_t ALGO_MASK_AEAD_ALL;
45// Exposed for testing
Nathan Harold39b5df42017-08-02 18:45:25 -070046extern const uint8_t REPLAY_WINDOW_SIZE;
47
Nathan Harold1a371532017-01-30 12:30:48 -080048// Suggest we avoid the smallest and largest ints
49class XfrmMessage;
50class TransportModeSecurityAssociation;
51
52class XfrmSocket {
53public:
54 virtual void close() {
Nathan Harold420ceac2017-04-05 19:36:59 -070055 if (mSock >= 0) {
Nathan Harold1a371532017-01-30 12:30:48 -080056 ::close(mSock);
57 }
58 mSock = -1;
59 }
60
ludi6e8eccd2017-08-14 14:40:37 -070061 virtual netdutils::Status open() = 0;
Nathan Harold1a371532017-01-30 12:30:48 -080062
63 virtual ~XfrmSocket() { close(); }
64
ludie51e3862017-08-15 19:28:12 -070065 // Sends the netlink message contained in iovecs. This populates iovecs[0] with
66 // a valid netlink message header.
ludi6e8eccd2017-08-14 14:40:37 -070067 virtual netdutils::Status sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags,
68 uint16_t nlMsgSeqNum,
69 std::vector<iovec>* iovecs) const = 0;
Nathan Harold1a371532017-01-30 12:30:48 -080070
71protected:
72 int mSock;
73};
74
75enum struct XfrmDirection : uint8_t {
76 IN = XFRM_POLICY_IN,
77 OUT = XFRM_POLICY_OUT,
78 FORWARD = XFRM_POLICY_FWD,
79 MASK = XFRM_POLICY_MASK,
80};
81
82enum struct XfrmMode : uint8_t {
83 TRANSPORT = XFRM_MODE_TRANSPORT,
84 TUNNEL = XFRM_MODE_TUNNEL,
85};
86
Nathan Harold420ceac2017-04-05 19:36:59 -070087enum struct XfrmEncapType : uint16_t {
88 NONE = 0,
89 ESPINUDP_NON_IKE = UDP_ENCAP_ESPINUDP_NON_IKE,
90 ESPINUDP = UDP_ENCAP_ESPINUDP
91};
92
Nathan Harold1a371532017-01-30 12:30:48 -080093struct XfrmAlgo {
94 std::string name;
95 std::vector<uint8_t> key;
96 uint16_t truncLenBits;
97};
98
Nathan Harold420ceac2017-04-05 19:36:59 -070099struct XfrmEncap {
100 XfrmEncapType type;
101 uint16_t srcPort;
102 uint16_t dstPort;
103};
104
Nathan Harold7441c692017-12-12 21:25:01 -0800105// minimally sufficient structure to match either an SA or a Policy
106struct XfrmId {
Nathan Harold1a371532017-01-30 12:30:48 -0800107 xfrm_address_t dstAddr; // network order
108 xfrm_address_t srcAddr;
109 int addrFamily; // AF_INET or AF_INET6
110 int transformId; // requestId
111 int spi;
Di Lu2ccb3e52018-01-03 16:19:20 -0800112 xfrm_mark mark;
Nathan Harold1a371532017-01-30 12:30:48 -0800113};
114
Nathan Harold7441c692017-12-12 21:25:01 -0800115struct XfrmSaInfo : XfrmId {
Nathan Harold1a371532017-01-30 12:30:48 -0800116 XfrmAlgo auth;
117 XfrmAlgo crypt;
Benedict Wongbe65b432017-08-22 21:43:14 -0700118 XfrmAlgo aead;
Nathan Harold1a371532017-01-30 12:30:48 -0800119 int netId;
120 XfrmMode mode;
Nathan Harold420ceac2017-04-05 19:36:59 -0700121 XfrmEncap encap;
Nathan Harold1a371532017-01-30 12:30:48 -0800122};
123
124class XfrmController {
125public:
126 XfrmController();
127
Nathan Harold21299f72018-03-16 20:13:03 -0700128 static netdutils::Status Init();
Benedict Wongb2daefb2017-12-06 22:05:46 -0800129
Nathan Harold21299f72018-03-16 20:13:03 -0700130 static netdutils::Status ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
131 int newUid, uid_t callerUid);
Nathan Harold1a371532017-01-30 12:30:48 -0800132
Nathan Harold21299f72018-03-16 20:13:03 -0700133 static netdutils::Status ipSecAllocateSpi(int32_t transformId, const std::string& localAddress,
134 const std::string& remoteAddress, int32_t inSpi,
135 int32_t* outSpi);
136
137 static netdutils::Status ipSecAddSecurityAssociation(
Nathan Haroldda54f122018-01-09 16:42:57 -0800138 int32_t transformId, int32_t mode, const std::string& sourceAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800139 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800140 int32_t markValue, int32_t markMask, const std::string& authAlgo,
141 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
142 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
143 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
144 int32_t encapLocalPort, int32_t encapRemotePort);
Nathan Harold1a371532017-01-30 12:30:48 -0800145
Nathan Harold21299f72018-03-16 20:13:03 -0700146 static netdutils::Status ipSecDeleteSecurityAssociation(int32_t transformId,
147 const std::string& sourceAddress,
148 const std::string& destinationAddress,
149 int32_t spi, int32_t markValue,
150 int32_t markMask);
Nathan Harold1a371532017-01-30 12:30:48 -0800151
Nathan Harold21299f72018-03-16 20:13:03 -0700152 static netdutils::Status
153 ipSecApplyTransportModeTransform(const android::base::unique_fd& socket, int32_t transformId,
154 int32_t direction, const std::string& localAddress,
155 const std::string& remoteAddress, int32_t spi);
Nathan Harold1a371532017-01-30 12:30:48 -0800156
Nathan Harold21299f72018-03-16 20:13:03 -0700157 static netdutils::Status
158 ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket);
Nathan Harold1a371532017-01-30 12:30:48 -0800159
Nathan Harold21299f72018-03-16 20:13:03 -0700160 static netdutils::Status ipSecAddSecurityPolicy(int32_t transformId, int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700161 const std::string& tmplSrcAddress,
162 const std::string& tmplDstAddress, int32_t spi,
163 int32_t markValue, int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800164
Nathan Harold21299f72018-03-16 20:13:03 -0700165 static netdutils::Status ipSecUpdateSecurityPolicy(int32_t transformId, int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700166 const std::string& tmplSrcAddress,
167 const std::string& tmplDstAddress,
Nathan Harold21299f72018-03-16 20:13:03 -0700168 int32_t spi, int32_t markValue,
169 int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800170
Nathan Harold21299f72018-03-16 20:13:03 -0700171 static netdutils::Status ipSecDeleteSecurityPolicy(int32_t transformId, int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700172 const std::string& tmplSrcAddress,
173 const std::string& tmplDstAddress,
Nathan Harold21299f72018-03-16 20:13:03 -0700174 int32_t markValue, int32_t markMask);
ludi771b8002017-11-20 15:09:05 -0800175
Nathan Harold21299f72018-03-16 20:13:03 -0700176 static int addVirtualTunnelInterface(const std::string& deviceName,
177 const std::string& localAddress,
178 const std::string& remoteAddress, int32_t ikey,
179 int32_t okey, bool isUpdate);
manojboopathi8707f232018-01-02 14:45:47 -0800180
Nathan Harold21299f72018-03-16 20:13:03 -0700181 static int removeVirtualTunnelInterface(const std::string& deviceName);
manojboopathi8707f232018-01-02 14:45:47 -0800182
Jonathan Basseric6461a62017-08-31 14:47:33 -0700183 // Some XFRM netlink attributes comprise a header, a struct, and some data
184 // after the struct. We wrap all of those in one struct for easier
185 // marshalling. The structs below must be ABI compatible with the kernel and
186 // are composed from kernel structures; thus, they use the kernel naming
187 // convention.
188
Nathan Harold39b5df42017-08-02 18:45:25 -0700189 // Exposed for testing
Benedict Wong4f60ee12017-10-10 12:27:25 -0700190 static constexpr size_t MAX_KEY_LENGTH = 128;
Nathan Harold39b5df42017-08-02 18:45:25 -0700191
Jonathan Basseric6461a62017-08-31 14:47:33 -0700192 // Container for the content of an XFRMA_ALG_CRYPT netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700193 // Exposed for testing
194 struct nlattr_algo_crypt {
195 nlattr hdr;
196 xfrm_algo crypt;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700197 uint8_t key[MAX_KEY_LENGTH];
Nathan Harold39b5df42017-08-02 18:45:25 -0700198 };
199
Jonathan Basseric6461a62017-08-31 14:47:33 -0700200 // Container for the content of an XFRMA_ALG_AUTH_TRUNC netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700201 // Exposed for testing
202 struct nlattr_algo_auth {
203 nlattr hdr;
204 xfrm_algo_auth auth;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700205 uint8_t key[MAX_KEY_LENGTH];
Nathan Harold39b5df42017-08-02 18:45:25 -0700206 };
207
Jonathan Basseric6461a62017-08-31 14:47:33 -0700208 // Container for the content of an XFRMA_TMPL netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700209 // Exposed for testing
Benedict Wongbe65b432017-08-22 21:43:14 -0700210 struct nlattr_algo_aead {
211 nlattr hdr;
212 xfrm_algo_aead aead;
Benedict Wong4f60ee12017-10-10 12:27:25 -0700213 uint8_t key[MAX_KEY_LENGTH];
Benedict Wongbe65b432017-08-22 21:43:14 -0700214 };
215
216 // Exposed for testing
Nathan Harold39b5df42017-08-02 18:45:25 -0700217 struct nlattr_user_tmpl {
218 nlattr hdr;
219 xfrm_user_tmpl tmpl;
220 };
221
Jonathan Basseric6461a62017-08-31 14:47:33 -0700222 // Container for the content of an XFRMA_ENCAP netlink attribute.
Nathan Harold39b5df42017-08-02 18:45:25 -0700223 // Exposed for testing
224 struct nlattr_encap_tmpl {
225 nlattr hdr;
226 xfrm_encap_tmpl tmpl;
227 };
228
Di Lu2ccb3e52018-01-03 16:19:20 -0800229 // Container for the content of an XFRMA_MARK netlink attribute.
230 // Exposed for testing
231 struct nlattr_xfrm_mark {
232 nlattr hdr;
233 xfrm_mark mark;
234 };
235
Benedict Wong96abf482018-01-22 13:56:41 -0800236 // Container for the content of an XFRMA_OUTPUT_MARK netlink attribute.
237 // Exposed for testing
238 struct nlattr_xfrm_output_mark {
239 nlattr hdr;
240 __u32 outputMark;
241 };
242
Nathan Harold1a371532017-01-30 12:30:48 -0800243private:
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700244/*
Bernie Innocentiedf68352018-07-05 17:50:38 +0900245 * This is a workaround for a kernel bug in the 32bit netlink compat layer
246 * that has been present on x86_64 kernels since 2010 with no fix on the
247 * horizon.
248 *
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700249 * Below is a redefinition of the xfrm_usersa_info struct that is part
250 * of the Linux uapi <linux/xfrm.h> to align the structures to a 64-bit
251 * boundary.
Bernie Innocentiedf68352018-07-05 17:50:38 +0900252 *
253 * Note that we turn this on for all x86 32bit targets, under the assumption
254 * that nowadays all x86 targets are running 64bit kernels.
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700255 */
Bernie Innocentiedf68352018-07-05 17:50:38 +0900256#if defined(__i386__)
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700257 // Shadow the kernel definition of xfrm_usersa_info with a 64-bit aligned version
258 struct xfrm_usersa_info : ::xfrm_usersa_info {
259 } __attribute__((aligned(8)));
260 // Shadow the kernel's version, using the aligned version of xfrm_usersa_info
261 struct xfrm_userspi_info {
262 struct xfrm_usersa_info info;
263 __u32 min;
264 __u32 max;
265 };
Bjoern Johansson83977442018-05-25 16:55:28 -0700266 struct xfrm_userpolicy_info : ::xfrm_userpolicy_info {
267 } __attribute__((aligned(8)));
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700268
269 /*
270 * Anyone who encounters a failure when sending netlink messages should look here
271 * first. Hitting the static_assert() below should be a strong hint that Android
272 * IPsec will probably not work with your current settings.
273 *
274 * Again, experimentally determined, the "flags" field should be the first byte in
275 * the final word of the xfrm_usersa_info struct. The check validates the size of
276 * the padding to be 7.
277 *
278 * This padding is verified to be correct on gcc/x86_64 kernel, and clang/x86 userspace.
279 */
280 static_assert(sizeof(::xfrm_usersa_info) % 8 != 0, "struct xfrm_usersa_info has changed "
281 "alignment. Please consider whether this "
282 "patch is needed.");
283 static_assert(sizeof(xfrm_usersa_info) - offsetof(xfrm_usersa_info, flags) == 8,
284 "struct xfrm_usersa_info probably misaligned with kernel struct.");
285 static_assert(sizeof(xfrm_usersa_info) % 8 == 0, "struct xfrm_usersa_info_t is not 64-bit "
286 "aligned. Please consider whether this patch "
287 "is needed.");
288 static_assert(sizeof(::xfrm_userspi_info) - sizeof(::xfrm_usersa_info) ==
289 sizeof(xfrm_userspi_info) - sizeof(xfrm_usersa_info),
290 "struct xfrm_userspi_info has changed and does not match the kernel struct.");
Bjoern Johansson83977442018-05-25 16:55:28 -0700291 static_assert(sizeof(::xfrm_userpolicy_info) % 8 != 0,
292 "struct xfrm_userpolicy_info has changed "
293 "alignment. Please consider whether this "
294 "patch is needed.");
295 static_assert(sizeof(xfrm_userpolicy_info) - offsetof(xfrm_userpolicy_info, share) == 5,
296 "struct xfrm_userpolicy_info probably misaligned with kernel struct.");
297 static_assert(sizeof(xfrm_userpolicy_info) % 8 == 0,
298 "struct xfrm_userpolicy_info is not 64-bit "
299 "aligned. Please consider whether this patch "
300 "is needed.");
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700301#endif
302
Nathan Harold7441c692017-12-12 21:25:01 -0800303 // helper function for filling in the XfrmId (and XfrmSaInfo) structure
Nathan Haroldda54f122018-01-09 16:42:57 -0800304 static netdutils::Status fillXfrmId(const std::string& sourceAddress,
305 const std::string& destinationAddress, int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800306 int32_t markValue, int32_t markMask, int32_t transformId,
307 XfrmId* xfrmId);
Nathan Harold1a371532017-01-30 12:30:48 -0800308
309 // Top level functions for managing a Transport Mode Transform
ludi6e8eccd2017-08-14 14:40:37 -0700310 static netdutils::Status addTransportModeTransform(const XfrmSaInfo& record);
Nathan Harold1a371532017-01-30 12:30:48 -0800311 static int removeTransportModeTransform(const XfrmSaInfo& record);
312
313 // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE
314 // Shared between SA and SP
ludi771b8002017-11-20 15:09:05 -0800315 static void fillXfrmSelector(const XfrmSaInfo& record, xfrm_selector* selector);
Nathan Harold1a371532017-01-30 12:30:48 -0800316
317 // Shared between Transport and Tunnel Mode
318 static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo);
319 static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo);
Benedict Wongbe65b432017-08-22 21:43:14 -0700320 static int fillNlAttrXfrmAlgoAead(const XfrmAlgo& in_algo, nlattr_algo_aead* algo);
Nathan Harold420ceac2017-04-05 19:36:59 -0700321 static int fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl);
Nathan Harold1a371532017-01-30 12:30:48 -0800322
Di Lu0e16b5e2018-01-12 10:37:53 -0800323 // Functions for updating a Transport Mode SA
324 static netdutils::Status updateSecurityAssociation(const XfrmSaInfo& record,
manojboopathi4d2d6f12017-12-06 11:11:31 -0800325 const XfrmSocket& sock);
Nathan Harold1a371532017-01-30 12:30:48 -0800326 static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa);
327
328 // Functions for deleting a Transport Mode SA
Nathan Harold7441c692017-12-12 21:25:01 -0800329 static netdutils::Status deleteSecurityAssociation(const XfrmId& record,
ludi6e8eccd2017-08-14 14:40:37 -0700330 const XfrmSocket& sock);
Nathan Harold7441c692017-12-12 21:25:01 -0800331 static int fillUserSaId(const XfrmId& record, xfrm_usersa_id* said);
Nathan Harold1a371532017-01-30 12:30:48 -0800332 static int fillUserTemplate(const XfrmSaInfo& record, xfrm_user_tmpl* tmpl);
ludi771b8002017-11-20 15:09:05 -0800333
Nathan Haroldda54f122018-01-09 16:42:57 -0800334 static int fillTransportModeUserSpInfo(const XfrmSaInfo& record, XfrmDirection direction,
335 xfrm_userpolicy_info* usersp);
ludi771b8002017-11-20 15:09:05 -0800336 static int fillNlAttrUserTemplate(const XfrmSaInfo& record, nlattr_user_tmpl* tmpl);
337 static int fillUserPolicyId(const XfrmSaInfo& record, XfrmDirection direction,
338 xfrm_userpolicy_id* policy_id);
Di Lu2ccb3e52018-01-03 16:19:20 -0800339 static int fillNlAttrXfrmMark(const XfrmId& record, nlattr_xfrm_mark* mark);
Benedict Wong96abf482018-01-22 13:56:41 -0800340 static int fillNlAttrXfrmOutputMark(const __u32 output_mark_value,
341 nlattr_xfrm_output_mark* output_mark);
Nathan Harold1a371532017-01-30 12:30:48 -0800342
ludi6e8eccd2017-08-14 14:40:37 -0700343 static netdutils::Status allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi,
344 uint32_t* outSpi, const XfrmSocket& sock);
Nathan Harold1a371532017-01-30 12:30:48 -0800345
ludi771b8002017-11-20 15:09:05 -0800346 static netdutils::Status processSecurityPolicy(int32_t transformId, int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700347 const std::string& tmplSrcAddress,
348 const std::string& tmplDstAddress, int32_t spi,
Nathan Harold21299f72018-03-16 20:13:03 -0700349 int32_t markValue, int32_t markMask,
350 int32_t msgType);
ludi771b8002017-11-20 15:09:05 -0800351 static netdutils::Status updateTunnelModeSecurityPolicy(const XfrmSaInfo& record,
352 const XfrmSocket& sock,
353 XfrmDirection direction,
354 uint16_t msgType);
355 static netdutils::Status deleteTunnelModeSecurityPolicy(const XfrmSaInfo& record,
356 const XfrmSocket& sock,
357 XfrmDirection direction);
Nathan Harold21299f72018-03-16 20:13:03 -0700358 static netdutils::Status flushInterfaces();
359 static netdutils::Status flushSaDb(const XfrmSocket& s);
360 static netdutils::Status flushPolicyDb(const XfrmSocket& s);
361
Nathan Harold1a371532017-01-30 12:30:48 -0800362 // END TODO(messagerefactor)
363};
364
365} // namespace net
366} // namespace android
367
368#endif /* !defined(XFRM_CONTROLLER_H) */