blob: 904ae885920b8e2f3f90745fc7cc5e7dcca1ea08 [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>
29#include <utils/RWLock.h>
30
31#include "NetdConstants.h"
32
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
41extern const uint8_t REPLAY_WINDOW_SIZE;
42
Nathan Harold1a371532017-01-30 12:30:48 -080043// Suggest we avoid the smallest and largest ints
44class XfrmMessage;
45class TransportModeSecurityAssociation;
46
47class XfrmSocket {
48public:
49 virtual void close() {
Nathan Harold420ceac2017-04-05 19:36:59 -070050 if (mSock >= 0) {
Nathan Harold1a371532017-01-30 12:30:48 -080051 ::close(mSock);
52 }
53 mSock = -1;
54 }
55
56 virtual bool open() = 0;
57
58 virtual ~XfrmSocket() { close(); }
59
60 virtual int sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags, uint16_t nlMsgSeqNum,
61 iovec* iov, int iovLen) const = 0;
62
63protected:
64 int mSock;
65};
66
67enum struct XfrmDirection : uint8_t {
68 IN = XFRM_POLICY_IN,
69 OUT = XFRM_POLICY_OUT,
70 FORWARD = XFRM_POLICY_FWD,
71 MASK = XFRM_POLICY_MASK,
72};
73
74enum struct XfrmMode : uint8_t {
75 TRANSPORT = XFRM_MODE_TRANSPORT,
76 TUNNEL = XFRM_MODE_TUNNEL,
77};
78
Nathan Harold420ceac2017-04-05 19:36:59 -070079enum struct XfrmEncapType : uint16_t {
80 NONE = 0,
81 ESPINUDP_NON_IKE = UDP_ENCAP_ESPINUDP_NON_IKE,
82 ESPINUDP = UDP_ENCAP_ESPINUDP
83};
84
Nathan Harold1a371532017-01-30 12:30:48 -080085struct XfrmAlgo {
86 std::string name;
87 std::vector<uint8_t> key;
88 uint16_t truncLenBits;
89};
90
Nathan Harold420ceac2017-04-05 19:36:59 -070091struct XfrmEncap {
92 XfrmEncapType type;
93 uint16_t srcPort;
94 uint16_t dstPort;
95};
96
Nathan Harold1a371532017-01-30 12:30:48 -080097struct XfrmSaId {
98 XfrmDirection direction;
99 xfrm_address_t dstAddr; // network order
100 xfrm_address_t srcAddr;
101 int addrFamily; // AF_INET or AF_INET6
102 int transformId; // requestId
103 int spi;
104};
105
106struct XfrmSaInfo : XfrmSaId {
107 XfrmAlgo auth;
108 XfrmAlgo crypt;
109 int netId;
110 XfrmMode mode;
Nathan Harold420ceac2017-04-05 19:36:59 -0700111 XfrmEncap encap;
Nathan Harold1a371532017-01-30 12:30:48 -0800112};
113
114class XfrmController {
115public:
116 XfrmController();
117
118 int ipSecAllocateSpi(int32_t transformId, int32_t direction, const std::string& localAddress,
119 const std::string& remoteAddress, int32_t inSpi, int32_t* outSpi);
120
121 int ipSecAddSecurityAssociation(
122 int32_t transformId, int32_t mode, int32_t direction, const std::string& localAddress,
123 const std::string& remoteAddress, int64_t underlyingNetworkHandle, int32_t spi,
124 const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits,
125 const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits,
ludiec836052017-05-20 14:17:05 -0700126 int32_t encapType, int32_t encapLocalPort, int32_t encapRemotePort);
Nathan Harold1a371532017-01-30 12:30:48 -0800127
128 int ipSecDeleteSecurityAssociation(int32_t transformId, int32_t direction,
129 const std::string& localAddress,
130 const std::string& remoteAddress, int32_t spi);
131
132 int ipSecApplyTransportModeTransform(const android::base::unique_fd& socket,
133 int32_t transformId, int32_t direction,
134 const std::string& localAddress,
135 const std::string& remoteAddress, int32_t spi);
136
137 int ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket);
138
Nathan Harold39b5df42017-08-02 18:45:25 -0700139 // Exposed for testing
140 static constexpr size_t MAX_ALGO_LENGTH = 128;
141
142 // Exposed for testing
143 struct nlattr_algo_crypt {
144 nlattr hdr;
145 xfrm_algo crypt;
146 uint8_t key[MAX_ALGO_LENGTH];
147 };
148
149 // Exposed for testing
150 struct nlattr_algo_auth {
151 nlattr hdr;
152 xfrm_algo_auth auth;
153 uint8_t key[MAX_ALGO_LENGTH];
154 };
155
156 // Exposed for testing
157 struct nlattr_user_tmpl {
158 nlattr hdr;
159 xfrm_user_tmpl tmpl;
160 };
161
162 // Exposed for testing
163 struct nlattr_encap_tmpl {
164 nlattr hdr;
165 xfrm_encap_tmpl tmpl;
166 };
167
Nathan Harold1a371532017-01-30 12:30:48 -0800168private:
169 // prevent concurrent modification of XFRM
170 android::RWLock mLock;
171
Nathan Harolde2dd4c72017-04-19 11:09:11 -0700172/*
173 * Below is a redefinition of the xfrm_usersa_info struct that is part
174 * of the Linux uapi <linux/xfrm.h> to align the structures to a 64-bit
175 * boundary.
176 */
177#ifdef NETLINK_COMPAT32
178 // Shadow the kernel definition of xfrm_usersa_info with a 64-bit aligned version
179 struct xfrm_usersa_info : ::xfrm_usersa_info {
180 } __attribute__((aligned(8)));
181 // Shadow the kernel's version, using the aligned version of xfrm_usersa_info
182 struct xfrm_userspi_info {
183 struct xfrm_usersa_info info;
184 __u32 min;
185 __u32 max;
186 };
187
188 /*
189 * Anyone who encounters a failure when sending netlink messages should look here
190 * first. Hitting the static_assert() below should be a strong hint that Android
191 * IPsec will probably not work with your current settings.
192 *
193 * Again, experimentally determined, the "flags" field should be the first byte in
194 * the final word of the xfrm_usersa_info struct. The check validates the size of
195 * the padding to be 7.
196 *
197 * This padding is verified to be correct on gcc/x86_64 kernel, and clang/x86 userspace.
198 */
199 static_assert(sizeof(::xfrm_usersa_info) % 8 != 0, "struct xfrm_usersa_info has changed "
200 "alignment. Please consider whether this "
201 "patch is needed.");
202 static_assert(sizeof(xfrm_usersa_info) - offsetof(xfrm_usersa_info, flags) == 8,
203 "struct xfrm_usersa_info probably misaligned with kernel struct.");
204 static_assert(sizeof(xfrm_usersa_info) % 8 == 0, "struct xfrm_usersa_info_t is not 64-bit "
205 "aligned. Please consider whether this patch "
206 "is needed.");
207 static_assert(sizeof(::xfrm_userspi_info) - sizeof(::xfrm_usersa_info) ==
208 sizeof(xfrm_userspi_info) - sizeof(xfrm_usersa_info),
209 "struct xfrm_userspi_info has changed and does not match the kernel struct.");
210#endif
211
Nathan Harold1a371532017-01-30 12:30:48 -0800212 // helper function for filling in the XfrmSaInfo structure
213 static int fillXfrmSaId(int32_t direction, const std::string& localAddress,
214 const std::string& remoteAddress, int32_t spi, XfrmSaId* xfrmId);
215
216 // Top level functions for managing a Transport Mode Transform
217 static int addTransportModeTransform(const XfrmSaInfo& record);
218 static int removeTransportModeTransform(const XfrmSaInfo& record);
219
220 // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE
221 // Shared between SA and SP
222 static void fillTransportModeSelector(const XfrmSaInfo& record, xfrm_selector* selector);
223
224 // Shared between Transport and Tunnel Mode
225 static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo);
226 static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo);
Nathan Harold420ceac2017-04-05 19:36:59 -0700227 static int fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl);
Nathan Harold1a371532017-01-30 12:30:48 -0800228
229 // Functions for Creating a Transport Mode SA
230 static int createTransportModeSecurityAssociation(const XfrmSaInfo& record,
231 const XfrmSocket& sock);
232 static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa);
233
234 // Functions for deleting a Transport Mode SA
235 static int deleteSecurityAssociation(const XfrmSaId& record, const XfrmSocket& sock);
236 static int fillUserSaId(const XfrmSaId& record, xfrm_usersa_id* said);
237 static int fillUserTemplate(const XfrmSaInfo& record, xfrm_user_tmpl* tmpl);
238 static int fillTransportModeUserSpInfo(const XfrmSaInfo& record, xfrm_userpolicy_info* usersp);
239
240 static int allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi,
241 uint32_t* outSpi, const XfrmSocket& sock);
242
243 // END TODO(messagerefactor)
244};
245
246} // namespace net
247} // namespace android
248
249#endif /* !defined(XFRM_CONTROLLER_H) */