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