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