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