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> |
| 26 | #include <linux/xfrm.h> |
| 27 | #include <sysutils/SocketClient.h> |
| 28 | #include <utils/RWLock.h> |
| 29 | |
| 30 | #include "NetdConstants.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | |
| 35 | // Suggest we avoid the smallest and largest ints |
| 36 | class XfrmMessage; |
| 37 | class TransportModeSecurityAssociation; |
| 38 | |
| 39 | class XfrmSocket { |
| 40 | public: |
| 41 | virtual void close() { |
| 42 | if (mSock > 0) { |
| 43 | ::close(mSock); |
| 44 | } |
| 45 | mSock = -1; |
| 46 | } |
| 47 | |
| 48 | virtual bool open() = 0; |
| 49 | |
| 50 | virtual ~XfrmSocket() { close(); } |
| 51 | |
| 52 | virtual int sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags, uint16_t nlMsgSeqNum, |
| 53 | iovec* iov, int iovLen) const = 0; |
| 54 | |
| 55 | protected: |
| 56 | int mSock; |
| 57 | }; |
| 58 | |
| 59 | enum struct XfrmDirection : uint8_t { |
| 60 | IN = XFRM_POLICY_IN, |
| 61 | OUT = XFRM_POLICY_OUT, |
| 62 | FORWARD = XFRM_POLICY_FWD, |
| 63 | MASK = XFRM_POLICY_MASK, |
| 64 | }; |
| 65 | |
| 66 | enum struct XfrmMode : uint8_t { |
| 67 | TRANSPORT = XFRM_MODE_TRANSPORT, |
| 68 | TUNNEL = XFRM_MODE_TUNNEL, |
| 69 | }; |
| 70 | |
| 71 | struct XfrmAlgo { |
| 72 | std::string name; |
| 73 | std::vector<uint8_t> key; |
| 74 | uint16_t truncLenBits; |
| 75 | }; |
| 76 | |
| 77 | struct XfrmSaId { |
| 78 | XfrmDirection direction; |
| 79 | xfrm_address_t dstAddr; // network order |
| 80 | xfrm_address_t srcAddr; |
| 81 | int addrFamily; // AF_INET or AF_INET6 |
| 82 | int transformId; // requestId |
| 83 | int spi; |
| 84 | }; |
| 85 | |
| 86 | struct XfrmSaInfo : XfrmSaId { |
| 87 | XfrmAlgo auth; |
| 88 | XfrmAlgo crypt; |
| 89 | int netId; |
| 90 | XfrmMode mode; |
| 91 | }; |
| 92 | |
| 93 | class XfrmController { |
| 94 | public: |
| 95 | XfrmController(); |
| 96 | |
| 97 | int ipSecAllocateSpi(int32_t transformId, int32_t direction, const std::string& localAddress, |
| 98 | const std::string& remoteAddress, int32_t inSpi, int32_t* outSpi); |
| 99 | |
| 100 | int ipSecAddSecurityAssociation( |
| 101 | int32_t transformId, int32_t mode, int32_t direction, const std::string& localAddress, |
| 102 | const std::string& remoteAddress, int64_t underlyingNetworkHandle, int32_t spi, |
| 103 | const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits, |
| 104 | const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, |
| 105 | int32_t encapType, int32_t encapLocalPort, int32_t encapRemotePort, int32_t* allocatedSpi); |
| 106 | |
| 107 | int ipSecDeleteSecurityAssociation(int32_t transformId, int32_t direction, |
| 108 | const std::string& localAddress, |
| 109 | const std::string& remoteAddress, int32_t spi); |
| 110 | |
| 111 | int ipSecApplyTransportModeTransform(const android::base::unique_fd& socket, |
| 112 | int32_t transformId, int32_t direction, |
| 113 | const std::string& localAddress, |
| 114 | const std::string& remoteAddress, int32_t spi); |
| 115 | |
| 116 | int ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket); |
| 117 | |
| 118 | private: |
| 119 | // prevent concurrent modification of XFRM |
| 120 | android::RWLock mLock; |
| 121 | |
| 122 | static constexpr size_t MAX_ALGO_LENGTH = 128; |
| 123 | |
| 124 | struct nlattr_algo_crypt { |
| 125 | nlattr hdr; |
| 126 | xfrm_algo crypt; |
| 127 | uint8_t key[MAX_ALGO_LENGTH]; // 1024 bit key, TODO: move off stack |
| 128 | }; |
| 129 | |
| 130 | struct nlattr_algo_auth { |
| 131 | nlattr hdr; |
| 132 | xfrm_algo_auth auth; |
| 133 | uint8_t key[MAX_ALGO_LENGTH]; // 1024 bit key, TODO: move off stack |
| 134 | }; |
| 135 | |
| 136 | struct nlattr_user_tmpl { |
| 137 | nlattr hdr; |
| 138 | xfrm_user_tmpl tmpl; |
| 139 | }; |
| 140 | |
| 141 | // helper function for filling in the XfrmSaInfo structure |
| 142 | static int fillXfrmSaId(int32_t direction, const std::string& localAddress, |
| 143 | const std::string& remoteAddress, int32_t spi, XfrmSaId* xfrmId); |
| 144 | |
| 145 | // Top level functions for managing a Transport Mode Transform |
| 146 | static int addTransportModeTransform(const XfrmSaInfo& record); |
| 147 | static int removeTransportModeTransform(const XfrmSaInfo& record); |
| 148 | |
| 149 | // TODO(messagerefactor): FACTOR OUT ALL MESSAGE BUILDING CODE BELOW HERE |
| 150 | // Shared between SA and SP |
| 151 | static void fillTransportModeSelector(const XfrmSaInfo& record, xfrm_selector* selector); |
| 152 | |
| 153 | // Shared between Transport and Tunnel Mode |
| 154 | static int fillNlAttrXfrmAlgoEnc(const XfrmAlgo& in_algo, nlattr_algo_crypt* algo); |
| 155 | static int fillNlAttrXfrmAlgoAuth(const XfrmAlgo& in_algo, nlattr_algo_auth* algo); |
| 156 | |
| 157 | // Functions for Creating a Transport Mode SA |
| 158 | static int createTransportModeSecurityAssociation(const XfrmSaInfo& record, |
| 159 | const XfrmSocket& sock); |
| 160 | static int fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa); |
| 161 | |
| 162 | // Functions for deleting a Transport Mode SA |
| 163 | static int deleteSecurityAssociation(const XfrmSaId& record, const XfrmSocket& sock); |
| 164 | static int fillUserSaId(const XfrmSaId& record, xfrm_usersa_id* said); |
| 165 | static int fillUserTemplate(const XfrmSaInfo& record, xfrm_user_tmpl* tmpl); |
| 166 | static int fillTransportModeUserSpInfo(const XfrmSaInfo& record, xfrm_userpolicy_info* usersp); |
| 167 | |
| 168 | static int allocateSpi(const XfrmSaInfo& record, uint32_t minSpi, uint32_t maxSpi, |
| 169 | uint32_t* outSpi, const XfrmSocket& sock); |
| 170 | |
| 171 | // END TODO(messagerefactor) |
| 172 | }; |
| 173 | |
| 174 | } // namespace net |
| 175 | } // namespace android |
| 176 | |
| 177 | #endif /* !defined(XFRM_CONTROLLER_H) */ |