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 | |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 25 | #include <linux/if_link.h> |
| 26 | #include <linux/if_tunnel.h> |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 27 | #include <linux/netlink.h> |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 28 | #include <linux/udp.h> |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 29 | #include <linux/xfrm.h> |
| 30 | #include <sysutils/SocketClient.h> |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 31 | |
| 32 | #include "NetdConstants.h" |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 33 | #include "netdutils/Slice.h" |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 34 | #include "netdutils/Status.h" |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | namespace net { |
| 38 | |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 39 | // Exposed for testing |
| 40 | extern const uint32_t ALGO_MASK_AUTH_ALL; |
| 41 | // Exposed for testing |
| 42 | extern const uint32_t ALGO_MASK_CRYPT_ALL; |
| 43 | // Exposed for testing |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 44 | extern const uint32_t ALGO_MASK_AEAD_ALL; |
| 45 | // Exposed for testing |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 46 | extern const uint8_t REPLAY_WINDOW_SIZE; |
| 47 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 48 | // Suggest we avoid the smallest and largest ints |
| 49 | class XfrmMessage; |
| 50 | class TransportModeSecurityAssociation; |
| 51 | |
| 52 | class XfrmSocket { |
| 53 | public: |
| 54 | virtual void close() { |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 55 | if (mSock >= 0) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 56 | ::close(mSock); |
| 57 | } |
| 58 | mSock = -1; |
| 59 | } |
| 60 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 61 | virtual netdutils::Status open() = 0; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 62 | |
| 63 | virtual ~XfrmSocket() { close(); } |
| 64 | |
ludi | e51e386 | 2017-08-15 19:28:12 -0700 | [diff] [blame] | 65 | // Sends the netlink message contained in iovecs. This populates iovecs[0] with |
| 66 | // a valid netlink message header. |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 67 | virtual netdutils::Status sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags, |
| 68 | uint16_t nlMsgSeqNum, |
| 69 | std::vector<iovec>* iovecs) const = 0; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | int mSock; |
| 73 | }; |
| 74 | |
| 75 | enum 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 | |
| 82 | enum struct XfrmMode : uint8_t { |
| 83 | TRANSPORT = XFRM_MODE_TRANSPORT, |
| 84 | TUNNEL = XFRM_MODE_TUNNEL, |
| 85 | }; |
| 86 | |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 87 | enum struct XfrmEncapType : uint16_t { |
| 88 | NONE = 0, |
| 89 | ESPINUDP_NON_IKE = UDP_ENCAP_ESPINUDP_NON_IKE, |
| 90 | ESPINUDP = UDP_ENCAP_ESPINUDP |
| 91 | }; |
| 92 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 93 | struct XfrmAlgo { |
| 94 | std::string name; |
| 95 | std::vector<uint8_t> key; |
| 96 | uint16_t truncLenBits; |
| 97 | }; |
| 98 | |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 99 | struct XfrmEncap { |
| 100 | XfrmEncapType type; |
| 101 | uint16_t srcPort; |
| 102 | uint16_t dstPort; |
| 103 | }; |
| 104 | |
Nathan Harold | 7441c69 | 2017-12-12 21:25:01 -0800 | [diff] [blame] | 105 | // minimally sufficient structure to match either an SA or a Policy |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 106 | struct XfrmCommonInfo { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 107 | 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 Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 112 | xfrm_mark mark; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 115 | struct XfrmSaInfo : XfrmCommonInfo { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 116 | XfrmAlgo auth; |
| 117 | XfrmAlgo crypt; |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 118 | XfrmAlgo aead; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 119 | int netId; |
| 120 | XfrmMode mode; |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 121 | XfrmEncap encap; |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 124 | struct XfrmSpInfo : XfrmSaInfo { |
| 125 | // Address family in XfrmCommonInfo used for template/SA matching, need separate addrFamily |
| 126 | // for selectors |
| 127 | int selAddrFamily; // AF_INET or AF_INET6 |
| 128 | }; |
| 129 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 130 | class XfrmController { |
| 131 | public: |
| 132 | XfrmController(); |
| 133 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 134 | static netdutils::Status Init(); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 135 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 136 | static netdutils::Status ipSecSetEncapSocketOwner(const android::base::unique_fd& socket, |
| 137 | int newUid, uid_t callerUid); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 138 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 139 | static netdutils::Status ipSecAllocateSpi(int32_t transformId, const std::string& localAddress, |
| 140 | const std::string& remoteAddress, int32_t inSpi, |
| 141 | int32_t* outSpi); |
| 142 | |
| 143 | static netdutils::Status ipSecAddSecurityAssociation( |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 144 | int32_t transformId, int32_t mode, const std::string& sourceAddress, |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 145 | const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi, |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 146 | int32_t markValue, int32_t markMask, const std::string& authAlgo, |
| 147 | const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo, |
| 148 | const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo, |
| 149 | const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType, |
| 150 | int32_t encapLocalPort, int32_t encapRemotePort); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 151 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 152 | static netdutils::Status ipSecDeleteSecurityAssociation(int32_t transformId, |
| 153 | const std::string& sourceAddress, |
| 154 | const std::string& destinationAddress, |
| 155 | int32_t spi, int32_t markValue, |
| 156 | int32_t markMask); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 157 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 158 | static netdutils::Status |
| 159 | ipSecApplyTransportModeTransform(const android::base::unique_fd& socket, int32_t transformId, |
| 160 | int32_t direction, const std::string& localAddress, |
| 161 | const std::string& remoteAddress, int32_t spi); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 162 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 163 | static netdutils::Status |
| 164 | ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 165 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 166 | static netdutils::Status ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily, |
| 167 | int32_t direction, |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame] | 168 | const std::string& tmplSrcAddress, |
| 169 | const std::string& tmplDstAddress, int32_t spi, |
| 170 | int32_t markValue, int32_t markMask); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 171 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 172 | static netdutils::Status ipSecUpdateSecurityPolicy(int32_t transformId, int32_t selAddrFamily, |
| 173 | int32_t direction, |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame] | 174 | const std::string& tmplSrcAddress, |
| 175 | const std::string& tmplDstAddress, |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 176 | int32_t spi, int32_t markValue, |
| 177 | int32_t markMask); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 178 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 179 | static netdutils::Status ipSecDeleteSecurityPolicy(int32_t transformId, int32_t selAddrFamily, |
| 180 | int32_t direction, int32_t markValue, |
| 181 | int32_t markMask); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 182 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 183 | static int addVirtualTunnelInterface(const std::string& deviceName, |
| 184 | const std::string& localAddress, |
| 185 | const std::string& remoteAddress, int32_t ikey, |
| 186 | int32_t okey, bool isUpdate); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 187 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 188 | static int removeVirtualTunnelInterface(const std::string& deviceName); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 189 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 190 | // Some XFRM netlink attributes comprise a header, a struct, and some data |
| 191 | // after the struct. We wrap all of those in one struct for easier |
| 192 | // marshalling. The structs below must be ABI compatible with the kernel and |
| 193 | // are composed from kernel structures; thus, they use the kernel naming |
| 194 | // convention. |
| 195 | |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 196 | // Exposed for testing |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame] | 197 | static constexpr size_t MAX_KEY_LENGTH = 128; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 198 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 199 | // Container for the content of an XFRMA_ALG_CRYPT netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 200 | // Exposed for testing |
| 201 | struct nlattr_algo_crypt { |
| 202 | nlattr hdr; |
| 203 | xfrm_algo crypt; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame] | 204 | uint8_t key[MAX_KEY_LENGTH]; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 207 | // Container for the content of an XFRMA_ALG_AUTH_TRUNC netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 208 | // Exposed for testing |
| 209 | struct nlattr_algo_auth { |
| 210 | nlattr hdr; |
| 211 | xfrm_algo_auth auth; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame] | 212 | uint8_t key[MAX_KEY_LENGTH]; |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 215 | // Container for the content of an XFRMA_TMPL netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 216 | // Exposed for testing |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 217 | struct nlattr_algo_aead { |
| 218 | nlattr hdr; |
| 219 | xfrm_algo_aead aead; |
Benedict Wong | 4f60ee1 | 2017-10-10 12:27:25 -0700 | [diff] [blame] | 220 | uint8_t key[MAX_KEY_LENGTH]; |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | // Exposed for testing |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 224 | struct nlattr_user_tmpl { |
| 225 | nlattr hdr; |
| 226 | xfrm_user_tmpl tmpl; |
| 227 | }; |
| 228 | |
Jonathan Basseri | c6461a6 | 2017-08-31 14:47:33 -0700 | [diff] [blame] | 229 | // Container for the content of an XFRMA_ENCAP netlink attribute. |
Nathan Harold | 39b5df4 | 2017-08-02 18:45:25 -0700 | [diff] [blame] | 230 | // Exposed for testing |
| 231 | struct nlattr_encap_tmpl { |
| 232 | nlattr hdr; |
| 233 | xfrm_encap_tmpl tmpl; |
| 234 | }; |
| 235 | |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 236 | // Container for the content of an XFRMA_MARK netlink attribute. |
| 237 | // Exposed for testing |
| 238 | struct nlattr_xfrm_mark { |
| 239 | nlattr hdr; |
| 240 | xfrm_mark mark; |
| 241 | }; |
| 242 | |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 243 | // Container for the content of an XFRMA_OUTPUT_MARK netlink attribute. |
| 244 | // Exposed for testing |
| 245 | struct nlattr_xfrm_output_mark { |
| 246 | nlattr hdr; |
| 247 | __u32 outputMark; |
| 248 | }; |
| 249 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 250 | private: |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 251 | /* |
Bernie Innocenti | edf6835 | 2018-07-05 17:50:38 +0900 | [diff] [blame] | 252 | * This is a workaround for a kernel bug in the 32bit netlink compat layer |
| 253 | * that has been present on x86_64 kernels since 2010 with no fix on the |
| 254 | * horizon. |
| 255 | * |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 256 | * Below is a redefinition of the xfrm_usersa_info struct that is part |
| 257 | * of the Linux uapi <linux/xfrm.h> to align the structures to a 64-bit |
| 258 | * boundary. |
Bernie Innocenti | edf6835 | 2018-07-05 17:50:38 +0900 | [diff] [blame] | 259 | * |
| 260 | * Note that we turn this on for all x86 32bit targets, under the assumption |
| 261 | * that nowadays all x86 targets are running 64bit kernels. |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 262 | */ |
Bernie Innocenti | edf6835 | 2018-07-05 17:50:38 +0900 | [diff] [blame] | 263 | #if defined(__i386__) |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 264 | // Shadow the kernel definition of xfrm_usersa_info with a 64-bit aligned version |
| 265 | struct xfrm_usersa_info : ::xfrm_usersa_info { |
| 266 | } __attribute__((aligned(8))); |
| 267 | // Shadow the kernel's version, using the aligned version of xfrm_usersa_info |
| 268 | struct xfrm_userspi_info { |
| 269 | struct xfrm_usersa_info info; |
| 270 | __u32 min; |
| 271 | __u32 max; |
| 272 | }; |
Bjoern Johansson | 8397744 | 2018-05-25 16:55:28 -0700 | [diff] [blame] | 273 | struct xfrm_userpolicy_info : ::xfrm_userpolicy_info { |
| 274 | } __attribute__((aligned(8))); |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Anyone who encounters a failure when sending netlink messages should look here |
| 278 | * first. Hitting the static_assert() below should be a strong hint that Android |
| 279 | * IPsec will probably not work with your current settings. |
| 280 | * |
| 281 | * Again, experimentally determined, the "flags" field should be the first byte in |
| 282 | * the final word of the xfrm_usersa_info struct. The check validates the size of |
| 283 | * the padding to be 7. |
| 284 | * |
| 285 | * This padding is verified to be correct on gcc/x86_64 kernel, and clang/x86 userspace. |
| 286 | */ |
| 287 | static_assert(sizeof(::xfrm_usersa_info) % 8 != 0, "struct xfrm_usersa_info has changed " |
| 288 | "alignment. Please consider whether this " |
| 289 | "patch is needed."); |
| 290 | static_assert(sizeof(xfrm_usersa_info) - offsetof(xfrm_usersa_info, flags) == 8, |
| 291 | "struct xfrm_usersa_info probably misaligned with kernel struct."); |
| 292 | static_assert(sizeof(xfrm_usersa_info) % 8 == 0, "struct xfrm_usersa_info_t is not 64-bit " |
| 293 | "aligned. Please consider whether this patch " |
| 294 | "is needed."); |
| 295 | static_assert(sizeof(::xfrm_userspi_info) - sizeof(::xfrm_usersa_info) == |
| 296 | sizeof(xfrm_userspi_info) - sizeof(xfrm_usersa_info), |
| 297 | "struct xfrm_userspi_info has changed and does not match the kernel struct."); |
Bjoern Johansson | 8397744 | 2018-05-25 16:55:28 -0700 | [diff] [blame] | 298 | static_assert(sizeof(::xfrm_userpolicy_info) % 8 != 0, |
| 299 | "struct xfrm_userpolicy_info has changed " |
| 300 | "alignment. Please consider whether this " |
| 301 | "patch is needed."); |
| 302 | static_assert(sizeof(xfrm_userpolicy_info) - offsetof(xfrm_userpolicy_info, share) == 5, |
| 303 | "struct xfrm_userpolicy_info probably misaligned with kernel struct."); |
| 304 | static_assert(sizeof(xfrm_userpolicy_info) % 8 == 0, |
| 305 | "struct xfrm_userpolicy_info is not 64-bit " |
| 306 | "aligned. Please consider whether this patch " |
| 307 | "is needed."); |
Nathan Harold | e2dd4c7 | 2017-04-19 11:09:11 -0700 | [diff] [blame] | 308 | #endif |
| 309 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 310 | // helper functions for filling in the XfrmCommonInfo (and XfrmSaInfo) structure |
| 311 | static netdutils::Status fillXfrmCommonInfo(const std::string& sourceAddress, |
| 312 | const std::string& destinationAddress, int32_t spi, |
| 313 | int32_t markValue, int32_t markMask, |
| 314 | int32_t transformId, XfrmCommonInfo* info); |
| 315 | static netdutils::Status fillXfrmCommonInfo(int32_t spi, int32_t markValue, int32_t markMask, |
| 316 | int32_t transformId, XfrmCommonInfo* info); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 317 | |
| 318 | // Top level functions for managing a Transport Mode Transform |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 319 | static netdutils::Status addTransportModeTransform(const XfrmSaInfo& record); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 320 | static int removeTransportModeTransform(const XfrmSaInfo& record); |
| 321 | |
| 322 | // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE |
| 323 | // Shared between SA and SP |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 324 | static void fillXfrmSelector(const int record, xfrm_selector* selector); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 325 | |
| 326 | // Shared between Transport and Tunnel Mode |
| 327 | static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo); |
| 328 | static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo); |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 329 | static int fillNlAttrXfrmAlgoAead(const XfrmAlgo& in_algo, nlattr_algo_aead* algo); |
Nathan Harold | 420ceac | 2017-04-05 19:36:59 -0700 | [diff] [blame] | 330 | static int fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 331 | |
Di Lu | 0e16b5e | 2018-01-12 10:37:53 -0800 | [diff] [blame] | 332 | // Functions for updating a Transport Mode SA |
| 333 | static netdutils::Status updateSecurityAssociation(const XfrmSaInfo& record, |
manojboopathi | 4d2d6f1 | 2017-12-06 11:11:31 -0800 | [diff] [blame] | 334 | const XfrmSocket& sock); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 335 | static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa); |
| 336 | |
| 337 | // Functions for deleting a Transport Mode SA |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 338 | static netdutils::Status deleteSecurityAssociation(const XfrmCommonInfo& record, |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 339 | const XfrmSocket& sock); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 340 | static int fillUserSaId(const XfrmCommonInfo& record, xfrm_usersa_id* said); |
| 341 | static int fillUserTemplate(const XfrmSpInfo& record, xfrm_user_tmpl* tmpl); |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 342 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 343 | static int fillUserSpInfo(const XfrmSpInfo& record, XfrmDirection direction, |
| 344 | xfrm_userpolicy_info* usersp); |
| 345 | static int fillNlAttrUserTemplate(const XfrmSpInfo& record, nlattr_user_tmpl* tmpl); |
| 346 | static int fillUserPolicyId(const XfrmSpInfo& record, XfrmDirection direction, |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 347 | xfrm_userpolicy_id* policy_id); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 348 | static int fillNlAttrXfrmMark(const XfrmCommonInfo& record, nlattr_xfrm_mark* mark); |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 349 | static int fillNlAttrXfrmOutputMark(const __u32 output_mark_value, |
| 350 | nlattr_xfrm_output_mark* output_mark); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 351 | |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 352 | static netdutils::Status allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi, |
| 353 | uint32_t* outSpi, const XfrmSocket& sock); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 354 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 355 | static netdutils::Status processSecurityPolicy(int32_t transformId, int32_t selAddrFamily, |
| 356 | int32_t direction, |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame] | 357 | const std::string& tmplSrcAddress, |
| 358 | const std::string& tmplDstAddress, int32_t spi, |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 359 | int32_t markValue, int32_t markMask, |
| 360 | int32_t msgType); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 361 | static netdutils::Status updateTunnelModeSecurityPolicy(const XfrmSpInfo& record, |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 362 | const XfrmSocket& sock, |
| 363 | XfrmDirection direction, |
| 364 | uint16_t msgType); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame^] | 365 | static netdutils::Status deleteTunnelModeSecurityPolicy(const XfrmSpInfo& record, |
ludi | 771b800 | 2017-11-20 15:09:05 -0800 | [diff] [blame] | 366 | const XfrmSocket& sock, |
| 367 | XfrmDirection direction); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 368 | static netdutils::Status flushInterfaces(); |
| 369 | static netdutils::Status flushSaDb(const XfrmSocket& s); |
| 370 | static netdutils::Status flushPolicyDb(const XfrmSocket& s); |
| 371 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 372 | // END TODO(messagerefactor) |
| 373 | }; |
| 374 | |
| 375 | } // namespace net |
| 376 | } // namespace android |
| 377 | |
| 378 | #endif /* !defined(XFRM_CONTROLLER_H) */ |