Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 26 | #include <linux/udp.h> |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 27 | #include <linux/xfrm.h> |
| 28 | #include <sysutils/SocketClient.h> |
| 29 | #include <utils/RWLock.h> |
| 30 | |
| 31 | #include "NetdConstants.h" |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 32 | #include "netdutils/Status.h" |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace net { |
| 36 | |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 37 | // Exposed for testing |
| 38 | extern const uint32_t ALGO_MASK_AUTH_ALL; |
| 39 | // Exposed for testing |
| 40 | extern const uint32_t ALGO_MASK_CRYPT_ALL; |
| 41 | // Exposed for testing |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 42 | extern const uint32_t ALGO_MASK_AEAD_ALL; |
| 43 | // Exposed for testing |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 44 | extern const uint8_t REPLAY_WINDOW_SIZE; |
| 45 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 46 | // Suggest we avoid the smallest and largest ints |
| 47 | class XfrmMessage; |
| 48 | class TransportModeSecurityAssociation; |
| 49 | |
| 50 | class XfrmSocket { |
| 51 | public: |
| 52 | virtual void close() { |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 53 | if (mSock >= 0) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 54 | ::close(mSock); |
| 55 | } |
| 56 | mSock = -1; |
| 57 | } |
| 58 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 59 | virtual netdutils::Status open() = 0; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 60 | |
| 61 | virtual ~XfrmSocket() { close(); } |
| 62 | |
ludi | e51e386 | 2017-08-15 19:28:12 -0700 | [diff] [blame] | 63 | // Sends the netlink message contained in iovecs. This populates iovecs[0] with |
| 64 | // a valid netlink message header. |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 65 | virtual netdutils::Status sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags, |
| 66 | uint16_t nlMsgSeqNum, |
| 67 | std::vector<iovec>* iovecs) const = 0; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 68 | |
| 69 | protected: |
| 70 | int mSock; |
| 71 | }; |
| 72 | |
| 73 | enum struct XfrmDirection : uint8_t { |
| 74 | IN = XFRM_POLICY_IN, |
| 75 | OUT = XFRM_POLICY_OUT, |
| 76 | FORWARD = XFRM_POLICY_FWD, |
| 77 | MASK = XFRM_POLICY_MASK, |
| 78 | }; |
| 79 | |
| 80 | enum struct XfrmMode : uint8_t { |
| 81 | TRANSPORT = XFRM_MODE_TRANSPORT, |
| 82 | TUNNEL = XFRM_MODE_TUNNEL, |
| 83 | }; |
| 84 | |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 85 | enum struct XfrmEncapType : uint16_t { |
| 86 | NONE = 0, |
| 87 | ESPINUDP_NON_IKE = UDP_ENCAP_ESPINUDP_NON_IKE, |
| 88 | ESPINUDP = UDP_ENCAP_ESPINUDP |
| 89 | }; |
| 90 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 91 | struct XfrmAlgo { |
| 92 | std::string name; |
| 93 | std::vector<uint8_t> key; |
| 94 | uint16_t truncLenBits; |
| 95 | }; |
| 96 | |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 97 | struct XfrmEncap { |
| 98 | XfrmEncapType type; |
| 99 | uint16_t srcPort; |
| 100 | uint16_t dstPort; |
| 101 | }; |
| 102 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 103 | struct XfrmSaId { |
| 104 | XfrmDirection direction; |
| 105 | xfrm_address_t dstAddr; // network order |
| 106 | xfrm_address_t srcAddr; |
| 107 | int addrFamily; // AF_INET or AF_INET6 |
| 108 | int transformId; // requestId |
| 109 | int spi; |
| 110 | }; |
| 111 | |
| 112 | struct XfrmSaInfo : XfrmSaId { |
| 113 | XfrmAlgo auth; |
| 114 | XfrmAlgo crypt; |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 115 | XfrmAlgo aead; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 116 | int netId; |
| 117 | XfrmMode mode; |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 118 | XfrmEncap encap; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class XfrmController { |
| 122 | public: |
| 123 | XfrmController(); |
| 124 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 125 | netdutils::Status ipSecAllocateSpi(int32_t transformId, int32_t direction, |
| 126 | const std::string& localAddress, |
| 127 | const std::string& remoteAddress, int32_t inSpi, |
| 128 | int32_t* outSpi); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 129 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 130 | netdutils::Status ipSecAddSecurityAssociation( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 131 | int32_t transformId, int32_t mode, int32_t direction, const std::string& localAddress, |
| 132 | const std::string& remoteAddress, int64_t underlyingNetworkHandle, int32_t spi, |
| 133 | const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits, |
| 134 | const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 135 | const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, |
ludi | ec83605 | 2017-05-20 14:17:05 -0700 | [diff] [blame] | 136 | int32_t encapType, int32_t encapLocalPort, int32_t encapRemotePort); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 137 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 138 | netdutils::Status ipSecDeleteSecurityAssociation(int32_t transformId, int32_t direction, |
| 139 | const std::string& localAddress, |
| 140 | const std::string& remoteAddress, int32_t spi); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 141 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 142 | netdutils::Status ipSecApplyTransportModeTransform(const android::base::unique_fd& socket, |
| 143 | int32_t transformId, int32_t direction, |
| 144 | const std::string& localAddress, |
| 145 | const std::string& remoteAddress, |
| 146 | int32_t spi); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 147 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 148 | netdutils::Status ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 149 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 150 | // Some XFRM netlink attributes comprise a header, a struct, and some data |
| 151 | // after the struct. We wrap all of those in one struct for easier |
| 152 | // marshalling. The structs below must be ABI compatible with the kernel and |
| 153 | // are composed from kernel structures; thus, they use the kernel naming |
| 154 | // convention. |
| 155 | |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 156 | // Exposed for testing |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame^] | 157 | static constexpr size_t MAX_KEY_LENGTH = 128; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 158 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 159 | // Container for the content of an XFRMA_ALG_CRYPT netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 160 | // Exposed for testing |
| 161 | struct nlattr_algo_crypt { |
| 162 | nlattr hdr; |
| 163 | xfrm_algo crypt; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame^] | 164 | uint8_t key[MAX_KEY_LENGTH]; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 167 | // Container for the content of an XFRMA_ALG_AUTH_TRUNC netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 168 | // Exposed for testing |
| 169 | struct nlattr_algo_auth { |
| 170 | nlattr hdr; |
| 171 | xfrm_algo_auth auth; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame^] | 172 | uint8_t key[MAX_KEY_LENGTH]; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 175 | // Container for the content of an XFRMA_TMPL netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 176 | // Exposed for testing |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 177 | struct nlattr_algo_aead { |
| 178 | nlattr hdr; |
| 179 | xfrm_algo_aead aead; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame^] | 180 | uint8_t key[MAX_KEY_LENGTH]; |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | // Exposed for testing |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 184 | struct nlattr_user_tmpl { |
| 185 | nlattr hdr; |
| 186 | xfrm_user_tmpl tmpl; |
| 187 | }; |
| 188 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 189 | // Container for the content of an XFRMA_ENCAP netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 190 | // Exposed for testing |
| 191 | struct nlattr_encap_tmpl { |
| 192 | nlattr hdr; |
| 193 | xfrm_encap_tmpl tmpl; |
| 194 | }; |
| 195 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 196 | private: |
| 197 | // prevent concurrent modification of XFRM |
| 198 | android::RWLock mLock; |
| 199 | |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 200 | /* |
| 201 | * Below is a redefinition of the xfrm_usersa_info struct that is part |
| 202 | * of the Linux uapi <linux/xfrm.h> to align the structures to a 64-bit |
| 203 | * boundary. |
| 204 | */ |
| 205 | #ifdef NETLINK_COMPAT32 |
| 206 | // Shadow the kernel definition of xfrm_usersa_info with a 64-bit aligned version |
| 207 | struct xfrm_usersa_info : ::xfrm_usersa_info { |
| 208 | } __attribute__((aligned(8))); |
| 209 | // Shadow the kernel's version, using the aligned version of xfrm_usersa_info |
| 210 | struct xfrm_userspi_info { |
| 211 | struct xfrm_usersa_info info; |
| 212 | __u32 min; |
| 213 | __u32 max; |
| 214 | }; |
| 215 | |
| 216 | /* |
| 217 | * Anyone who encounters a failure when sending netlink messages should look here |
| 218 | * first. Hitting the static_assert() below should be a strong hint that Android |
| 219 | * IPsec will probably not work with your current settings. |
| 220 | * |
| 221 | * Again, experimentally determined, the "flags" field should be the first byte in |
| 222 | * the final word of the xfrm_usersa_info struct. The check validates the size of |
| 223 | * the padding to be 7. |
| 224 | * |
| 225 | * This padding is verified to be correct on gcc/x86_64 kernel, and clang/x86 userspace. |
| 226 | */ |
| 227 | static_assert(sizeof(::xfrm_usersa_info) % 8 != 0, "struct xfrm_usersa_info has changed " |
| 228 | "alignment. Please consider whether this " |
| 229 | "patch is needed."); |
| 230 | static_assert(sizeof(xfrm_usersa_info) - offsetof(xfrm_usersa_info, flags) == 8, |
| 231 | "struct xfrm_usersa_info probably misaligned with kernel struct."); |
| 232 | static_assert(sizeof(xfrm_usersa_info) % 8 == 0, "struct xfrm_usersa_info_t is not 64-bit " |
| 233 | "aligned. Please consider whether this patch " |
| 234 | "is needed."); |
| 235 | static_assert(sizeof(::xfrm_userspi_info) - sizeof(::xfrm_usersa_info) == |
| 236 | sizeof(xfrm_userspi_info) - sizeof(xfrm_usersa_info), |
| 237 | "struct xfrm_userspi_info has changed and does not match the kernel struct."); |
| 238 | #endif |
| 239 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 240 | // helper function for filling in the XfrmSaInfo structure |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 241 | static netdutils::Status fillXfrmSaId(int32_t direction, const std::string& localAddress, |
| 242 | const std::string& remoteAddress, int32_t spi, |
| 243 | XfrmSaId* xfrmId); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 244 | |
| 245 | // Top level functions for managing a Transport Mode Transform |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 246 | static netdutils::Status addTransportModeTransform(const XfrmSaInfo& record); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 247 | static int removeTransportModeTransform(const XfrmSaInfo& record); |
| 248 | |
| 249 | // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE |
| 250 | // Shared between SA and SP |
| 251 | static void fillTransportModeSelector(const XfrmSaInfo& record, xfrm_selector* selector); |
| 252 | |
| 253 | // Shared between Transport and Tunnel Mode |
| 254 | static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo); |
| 255 | static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo); |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 256 | static int fillNlAttrXfrmAlgoAead(const XfrmAlgo& in_algo, nlattr_algo_aead* algo); |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 257 | static int fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 258 | |
| 259 | // Functions for Creating a Transport Mode SA |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 260 | static netdutils::Status createTransportModeSecurityAssociation(const XfrmSaInfo& record, |
| 261 | const XfrmSocket& sock); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 262 | static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa); |
| 263 | |
| 264 | // Functions for deleting a Transport Mode SA |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 265 | static netdutils::Status deleteSecurityAssociation(const XfrmSaId& record, |
| 266 | const XfrmSocket& sock); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 267 | static int fillUserSaId(const XfrmSaId& record, xfrm_usersa_id* said); |
| 268 | static int fillUserTemplate(const XfrmSaInfo& record, xfrm_user_tmpl* tmpl); |
| 269 | static int fillTransportModeUserSpInfo(const XfrmSaInfo& record, xfrm_userpolicy_info* usersp); |
| 270 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 271 | static netdutils::Status allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi, |
| 272 | uint32_t* outSpi, const XfrmSocket& sock); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 273 | |
| 274 | // END TODO(messagerefactor) |
| 275 | }; |
| 276 | |
| 277 | } // namespace net |
| 278 | } // namespace android |
| 279 | |
| 280 | #endif /* !defined(XFRM_CONTROLLER_H) */ |