blob: 18181e037f39d88120ea27c5ace7ee6b039dd660 [file] [log] [blame]
Nathan Harold1a371532017-01-30 12:30:48 -08001/*
2 *
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Nathan Harold1e284452017-10-10 13:31:19 -070018#include <random>
Nathan Harold7441c692017-12-12 21:25:01 -080019#include <string>
Nathan Harold1a371532017-01-30 12:30:48 -080020#include <vector>
21
22#include <ctype.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <getopt.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Nathan Harold420ceac2017-04-05 19:36:59 -070029
30#define __STDC_FORMAT_MACROS
Nathan Harold1a371532017-01-30 12:30:48 -080031#include <inttypes.h>
32
33#include <arpa/inet.h>
34#include <netinet/in.h>
35
36#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <sys/wait.h>
40
41#include <linux/in.h>
Nathan Harold21299f72018-03-16 20:13:03 -070042#include <linux/ipsec.h>
Nathan Harold1a371532017-01-30 12:30:48 -080043#include <linux/netlink.h>
44#include <linux/xfrm.h>
45
Nathan Harold45c35662018-04-23 10:10:18 -070046#define LOG_TAG "XfrmController"
Nathan Harolda0345e12018-06-05 11:41:41 -070047#include <android-base/properties.h>
48#include <android-base/stringprintf.h>
49#include <android-base/strings.h>
50#include <android-base/unique_fd.h>
51#include <android/net/INetd.h>
Nathan Harold1a371532017-01-30 12:30:48 -080052#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080053#include <log/log.h>
Nathan Harolda0345e12018-06-05 11:41:41 -070054#include <log/log_properties.h>
Nathan Harold1a371532017-01-30 12:30:48 -080055#include <logwrap/logwrap.h>
Benedict Wongd8b6a382018-09-18 17:16:10 -070056#include "Fwmark.h"
57#include "InterfaceController.h"
58#include "NetdConstants.h"
59#include "NetlinkCommands.h"
60#include "Permission.h"
61#include "ResponseCode.h"
62#include "XfrmController.h"
63#include "netdutils/Fd.h"
64#include "netdutils/Slice.h"
65#include "netdutils/Syscalls.h"
Nathan Harold1a371532017-01-30 12:30:48 -080066
Nathan Harold45c35662018-04-23 10:10:18 -070067using android::net::INetd;
ludi4072ff22017-06-15 08:56:52 -070068using android::netdutils::Fd;
69using android::netdutils::Slice;
70using android::netdutils::Status;
71using android::netdutils::StatusOr;
72using android::netdutils::Syscalls;
73
Nathan Harold1a371532017-01-30 12:30:48 -080074namespace android {
75namespace net {
76
Nathan Harold39b5df42017-08-02 18:45:25 -070077// Exposed for testing
Nathan Harold1a371532017-01-30 12:30:48 -080078constexpr uint32_t ALGO_MASK_AUTH_ALL = ~0;
Nathan Harold39b5df42017-08-02 18:45:25 -070079// Exposed for testing
Nathan Harold1a371532017-01-30 12:30:48 -080080constexpr uint32_t ALGO_MASK_CRYPT_ALL = ~0;
Nathan Harold39b5df42017-08-02 18:45:25 -070081// Exposed for testing
Benedict Wongbe65b432017-08-22 21:43:14 -070082constexpr uint32_t ALGO_MASK_AEAD_ALL = ~0;
83// Exposed for testing
Nathan Harold1a371532017-01-30 12:30:48 -080084constexpr uint8_t REPLAY_WINDOW_SIZE = 4;
85
Nathan Harold39b5df42017-08-02 18:45:25 -070086namespace {
87
Nathan Harold1012ffe2018-03-28 08:59:24 -070088constexpr uint32_t RAND_SPI_MIN = 256;
Nathan Harold1a371532017-01-30 12:30:48 -080089constexpr uint32_t RAND_SPI_MAX = 0xFFFFFFFE;
90
91constexpr uint32_t INVALID_SPI = 0;
Benedict Wong319f17e2018-05-15 17:06:44 -070092constexpr const char* INFO_KIND_VTI = "vti";
93constexpr const char* INFO_KIND_VTI6 = "vti6";
94constexpr const char* INFO_KIND_XFRMI = "xfrm";
95constexpr int INFO_KIND_MAX_LEN = 8;
Nathan Harold1a371532017-01-30 12:30:48 -080096
Nathan Harolda0345e12018-06-05 11:41:41 -070097static inline bool isEngBuild() {
98 static const std::string sBuildType = android::base::GetProperty("ro.build.type", "user");
99 return sBuildType == "eng";
100}
101
Nathan Harold1a371532017-01-30 12:30:48 -0800102#define XFRM_MSG_TRANS(x) \
103 case x: \
104 return #x;
105
106const char* xfrmMsgTypeToString(uint16_t msg) {
107 switch (msg) {
108 XFRM_MSG_TRANS(XFRM_MSG_NEWSA)
109 XFRM_MSG_TRANS(XFRM_MSG_DELSA)
110 XFRM_MSG_TRANS(XFRM_MSG_GETSA)
111 XFRM_MSG_TRANS(XFRM_MSG_NEWPOLICY)
112 XFRM_MSG_TRANS(XFRM_MSG_DELPOLICY)
113 XFRM_MSG_TRANS(XFRM_MSG_GETPOLICY)
114 XFRM_MSG_TRANS(XFRM_MSG_ALLOCSPI)
115 XFRM_MSG_TRANS(XFRM_MSG_ACQUIRE)
116 XFRM_MSG_TRANS(XFRM_MSG_EXPIRE)
117 XFRM_MSG_TRANS(XFRM_MSG_UPDPOLICY)
118 XFRM_MSG_TRANS(XFRM_MSG_UPDSA)
119 XFRM_MSG_TRANS(XFRM_MSG_POLEXPIRE)
120 XFRM_MSG_TRANS(XFRM_MSG_FLUSHSA)
121 XFRM_MSG_TRANS(XFRM_MSG_FLUSHPOLICY)
122 XFRM_MSG_TRANS(XFRM_MSG_NEWAE)
123 XFRM_MSG_TRANS(XFRM_MSG_GETAE)
124 XFRM_MSG_TRANS(XFRM_MSG_REPORT)
125 XFRM_MSG_TRANS(XFRM_MSG_MIGRATE)
126 XFRM_MSG_TRANS(XFRM_MSG_NEWSADINFO)
127 XFRM_MSG_TRANS(XFRM_MSG_GETSADINFO)
128 XFRM_MSG_TRANS(XFRM_MSG_GETSPDINFO)
129 XFRM_MSG_TRANS(XFRM_MSG_NEWSPDINFO)
130 XFRM_MSG_TRANS(XFRM_MSG_MAPPING)
131 default:
132 return "XFRM_MSG UNKNOWN";
133 }
134}
135
136// actually const but cannot be declared as such for reasons
137uint8_t kPadBytesArray[] = {0, 0, 0};
138void* kPadBytes = static_cast<void*>(kPadBytesArray);
139
Nathan Harold420ceac2017-04-05 19:36:59 -0700140#define LOG_HEX(__desc16__, __buf__, __len__) \
Nathan Harolda0345e12018-06-05 11:41:41 -0700141 do { \
142 if (isEngBuild()) { \
Manojd9c93c22017-10-19 13:40:26 -0700143 logHex(__desc16__, __buf__, __len__); \
Nathan Harolda0345e12018-06-05 11:41:41 -0700144 } \
145 } while (0)
Manojd9c93c22017-10-19 13:40:26 -0700146
ludie51e3862017-08-15 19:28:12 -0700147#define LOG_IOV(__iov__) \
Nathan Harolda0345e12018-06-05 11:41:41 -0700148 do { \
149 if (isEngBuild()) { \
Manojd9c93c22017-10-19 13:40:26 -0700150 logIov(__iov__); \
Nathan Harolda0345e12018-06-05 11:41:41 -0700151 } \
152 } while (0)
Nathan Harold1a371532017-01-30 12:30:48 -0800153
154void logHex(const char* desc16, const char* buf, size_t len) {
155 char* printBuf = new char[len * 2 + 1 + 26]; // len->ascii, +newline, +prefix strlen
156 int offset = 0;
157 if (desc16) {
158 sprintf(printBuf, "{%-16s}", desc16);
159 offset += 18; // prefix string length
160 }
161 sprintf(printBuf + offset, "[%4.4u]: ", (len > 9999) ? 9999 : (unsigned)len);
162 offset += 8;
163
164 for (uint32_t j = 0; j < (uint32_t)len; j++) {
Wolloh Chou4d1e5c72018-03-22 15:14:33 +0800165 sprintf(&printBuf[j * 2 + offset], "%0.2x", (unsigned char)buf[j]);
Nathan Harold1a371532017-01-30 12:30:48 -0800166 }
167 ALOGD("%s", printBuf);
168 delete[] printBuf;
169}
170
ludie51e3862017-08-15 19:28:12 -0700171void logIov(const std::vector<iovec>& iov) {
172 for (const iovec& row : iov) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700173 logHex(nullptr, reinterpret_cast<char*>(row.iov_base), row.iov_len);
Nathan Harold1a371532017-01-30 12:30:48 -0800174 }
175}
176
manojboopathi8707f232018-01-02 14:45:47 -0800177size_t fillNlAttr(__u16 nlaType, size_t valueSize, nlattr* nlAttr) {
178 size_t dataLen = valueSize;
179 int padLength = NLMSG_ALIGN(dataLen) - dataLen;
180 nlAttr->nla_len = (__u16)(dataLen + sizeof(nlattr));
181 nlAttr->nla_type = nlaType;
182 return padLength;
183}
184
185size_t fillNlAttrIpAddress(__u16 nlaType, int family, const std::string& value, nlattr* nlAttr,
186 Slice ipAddress) {
187 inet_pton(family, value.c_str(), ipAddress.base());
188 return fillNlAttr(nlaType, (family == AF_INET) ? sizeof(in_addr) : sizeof(in6_addr), nlAttr);
189}
190
Benedict Wong319f17e2018-05-15 17:06:44 -0700191size_t fillNlAttrU32(__u16 nlaType, uint32_t value, XfrmController::nlattr_payload_u32* nlAttr) {
192 nlAttr->value = value;
193 return fillNlAttr(nlaType, sizeof(value), &nlAttr->hdr);
manojboopathi8707f232018-01-02 14:45:47 -0800194}
195
196// returns the address family, placing the string in the provided buffer
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900197StatusOr<uint16_t> convertStringAddress(const std::string& addr, uint8_t* buffer) {
manojboopathi8707f232018-01-02 14:45:47 -0800198 if (inet_pton(AF_INET, addr.c_str(), buffer) == 1) {
199 return AF_INET;
200 } else if (inet_pton(AF_INET6, addr.c_str(), buffer) == 1) {
201 return AF_INET6;
202 } else {
203 return Status(EAFNOSUPPORT);
204 }
205}
206
ludi6e8eccd2017-08-14 14:40:37 -0700207// TODO: Need to consider a way to refer to the sSycalls instance
ludi4072ff22017-06-15 08:56:52 -0700208inline Syscalls& getSyscallInstance() { return netdutils::sSyscalls.get(); }
209
Nathan Harold1a371532017-01-30 12:30:48 -0800210class XfrmSocketImpl : public XfrmSocket {
211private:
212 static constexpr int NLMSG_DEFAULTSIZE = 8192;
213
214 union NetlinkResponse {
215 nlmsghdr hdr;
216 struct _err_ {
217 nlmsghdr hdr;
218 nlmsgerr err;
219 } err;
220
221 struct _buf_ {
222 nlmsghdr hdr;
223 char buf[NLMSG_DEFAULTSIZE];
224 } buf;
225 };
226
227public:
ludi6e8eccd2017-08-14 14:40:37 -0700228 netdutils::Status open() override {
Nathan Harold1a371532017-01-30 12:30:48 -0800229 mSock = openNetlinkSocket(NETLINK_XFRM);
ludi6e8eccd2017-08-14 14:40:37 -0700230 if (mSock < 0) {
Nathan Harold1a371532017-01-30 12:30:48 -0800231 ALOGW("Could not get a new socket, line=%d", __LINE__);
ludi6e8eccd2017-08-14 14:40:37 -0700232 return netdutils::statusFromErrno(-mSock, "Could not open netlink socket");
Nathan Harold1a371532017-01-30 12:30:48 -0800233 }
234
ludi6e8eccd2017-08-14 14:40:37 -0700235 return netdutils::status::ok;
Nathan Harold1a371532017-01-30 12:30:48 -0800236 }
237
ludi6e8eccd2017-08-14 14:40:37 -0700238 static netdutils::Status validateResponse(NetlinkResponse response, size_t len) {
Nathan Harold1a371532017-01-30 12:30:48 -0800239 if (len < sizeof(nlmsghdr)) {
240 ALOGW("Invalid response message received over netlink");
ludi6e8eccd2017-08-14 14:40:37 -0700241 return netdutils::statusFromErrno(EBADMSG, "Invalid message");
Nathan Harold1a371532017-01-30 12:30:48 -0800242 }
243
244 switch (response.hdr.nlmsg_type) {
245 case NLMSG_NOOP:
246 case NLMSG_DONE:
ludi6e8eccd2017-08-14 14:40:37 -0700247 return netdutils::status::ok;
Nathan Harold1a371532017-01-30 12:30:48 -0800248 case NLMSG_OVERRUN:
249 ALOGD("Netlink request overran kernel buffer");
ludi6e8eccd2017-08-14 14:40:37 -0700250 return netdutils::statusFromErrno(EBADMSG, "Kernel buffer overrun");
Nathan Harold1a371532017-01-30 12:30:48 -0800251 case NLMSG_ERROR:
252 if (len < sizeof(NetlinkResponse::_err_)) {
253 ALOGD("Netlink message received malformed error response");
ludi6e8eccd2017-08-14 14:40:37 -0700254 return netdutils::statusFromErrno(EBADMSG, "Malformed error response");
Nathan Harold1a371532017-01-30 12:30:48 -0800255 }
ludi6e8eccd2017-08-14 14:40:37 -0700256 return netdutils::statusFromErrno(
257 -response.err.err.error,
258 "Error netlink message"); // Netlink errors are negative errno.
Nathan Harold1a371532017-01-30 12:30:48 -0800259 case XFRM_MSG_NEWSA:
260 break;
261 }
262
263 if (response.hdr.nlmsg_type < XFRM_MSG_BASE /*== NLMSG_MIN_TYPE*/ ||
264 response.hdr.nlmsg_type > XFRM_MSG_MAX) {
265 ALOGD("Netlink message responded with an out-of-range message ID");
ludi6e8eccd2017-08-14 14:40:37 -0700266 return netdutils::statusFromErrno(EBADMSG, "Invalid message ID");
Nathan Harold1a371532017-01-30 12:30:48 -0800267 }
268
269 // TODO Add more message validation here
ludi6e8eccd2017-08-14 14:40:37 -0700270 return netdutils::status::ok;
Nathan Harold1a371532017-01-30 12:30:48 -0800271 }
272
ludi6e8eccd2017-08-14 14:40:37 -0700273 netdutils::Status sendMessage(uint16_t nlMsgType, uint16_t nlMsgFlags, uint16_t nlMsgSeqNum,
274 std::vector<iovec>* iovecs) const override {
Nathan Harold1a371532017-01-30 12:30:48 -0800275 nlmsghdr nlMsg = {
Nathan Harold7441c692017-12-12 21:25:01 -0800276 .nlmsg_type = nlMsgType,
277 .nlmsg_flags = nlMsgFlags,
278 .nlmsg_seq = nlMsgSeqNum,
Nathan Harold1a371532017-01-30 12:30:48 -0800279 };
280
ludie51e3862017-08-15 19:28:12 -0700281 (*iovecs)[0].iov_base = &nlMsg;
282 (*iovecs)[0].iov_len = NLMSG_HDRLEN;
283 for (const iovec& iov : *iovecs) {
284 nlMsg.nlmsg_len += iov.iov_len;
Nathan Harold1a371532017-01-30 12:30:48 -0800285 }
286
287 ALOGD("Sending Netlink XFRM Message: %s", xfrmMsgTypeToString(nlMsgType));
Manojd9c93c22017-10-19 13:40:26 -0700288 LOG_IOV(*iovecs);
Nathan Harold1a371532017-01-30 12:30:48 -0800289
ludie51e3862017-08-15 19:28:12 -0700290 StatusOr<size_t> writeResult = getSyscallInstance().writev(mSock, *iovecs);
ludi4072ff22017-06-15 08:56:52 -0700291 if (!isOk(writeResult)) {
292 ALOGE("netlink socket writev failed (%s)", toString(writeResult).c_str());
ludi6e8eccd2017-08-14 14:40:37 -0700293 return writeResult;
Nathan Harold1a371532017-01-30 12:30:48 -0800294 }
295
ludi4072ff22017-06-15 08:56:52 -0700296 if (nlMsg.nlmsg_len != writeResult.value()) {
297 ALOGE("Invalid netlink message length sent %d", static_cast<int>(writeResult.value()));
ludi6e8eccd2017-08-14 14:40:37 -0700298 return netdutils::statusFromErrno(EBADMSG, "Invalid message length");
Nathan Harold1a371532017-01-30 12:30:48 -0800299 }
300
ludi4072ff22017-06-15 08:56:52 -0700301 NetlinkResponse response = {};
Nathan Harold1a371532017-01-30 12:30:48 -0800302
ludi4072ff22017-06-15 08:56:52 -0700303 StatusOr<Slice> readResult =
304 getSyscallInstance().read(Fd(mSock), netdutils::makeSlice(response));
305 if (!isOk(readResult)) {
306 ALOGE("netlink response error (%s)", toString(readResult).c_str());
ludi6e8eccd2017-08-14 14:40:37 -0700307 return readResult;
ludi4072ff22017-06-15 08:56:52 -0700308 }
309
310 LOG_HEX("netlink msg resp", reinterpret_cast<char*>(readResult.value().base()),
311 readResult.value().size());
312
ludi6e8eccd2017-08-14 14:40:37 -0700313 Status validateStatus = validateResponse(response, readResult.value().size());
314 if (!isOk(validateStatus)) {
315 ALOGE("netlink response contains error (%s)", toString(validateStatus).c_str());
316 }
317
318 return validateStatus;
Nathan Harold1a371532017-01-30 12:30:48 -0800319 }
320};
321
ludi6e8eccd2017-08-14 14:40:37 -0700322StatusOr<int> convertToXfrmAddr(const std::string& strAddr, xfrm_address_t* xfrmAddr) {
Nathan Harold1a371532017-01-30 12:30:48 -0800323 if (strAddr.length() == 0) {
324 memset(xfrmAddr, 0, sizeof(*xfrmAddr));
325 return AF_UNSPEC;
326 }
327
328 if (inet_pton(AF_INET6, strAddr.c_str(), reinterpret_cast<void*>(xfrmAddr))) {
329 return AF_INET6;
330 } else if (inet_pton(AF_INET, strAddr.c_str(), reinterpret_cast<void*>(xfrmAddr))) {
331 return AF_INET;
332 } else {
ludi6e8eccd2017-08-14 14:40:37 -0700333 return netdutils::statusFromErrno(EAFNOSUPPORT, "Invalid address family");
Nathan Harold1a371532017-01-30 12:30:48 -0800334 }
335}
336
337void fillXfrmNlaHdr(nlattr* hdr, uint16_t type, uint16_t len) {
338 hdr->nla_type = type;
339 hdr->nla_len = len;
340}
341
342void fillXfrmCurLifetimeDefaults(xfrm_lifetime_cur* cur) {
343 memset(reinterpret_cast<char*>(cur), 0, sizeof(*cur));
344}
345void fillXfrmLifetimeDefaults(xfrm_lifetime_cfg* cfg) {
346 cfg->soft_byte_limit = XFRM_INF;
347 cfg->hard_byte_limit = XFRM_INF;
348 cfg->soft_packet_limit = XFRM_INF;
349 cfg->hard_packet_limit = XFRM_INF;
350}
351
352/*
353 * Allocate SPIs within an (inclusive) range of min-max.
354 * returns 0 (INVALID_SPI) once the entire range has been parsed.
355 */
356class RandomSpi {
357public:
358 RandomSpi(int min, int max) : mMin(min) {
Nathan Harold1e284452017-10-10 13:31:19 -0700359 // Re-seeding should be safe because the seed itself is
360 // sufficiently random and we don't need secure random
361 std::mt19937 rnd = std::mt19937(std::random_device()());
362 mNext = std::uniform_int_distribution<>(1, INT_MAX)(rnd);
Nathan Harold1a371532017-01-30 12:30:48 -0800363 mSize = max - min + 1;
364 mCount = mSize;
365 }
366
367 uint32_t next() {
368 if (!mCount)
369 return 0;
370 mCount--;
371 return (mNext++ % mSize) + mMin;
372 }
373
374private:
375 uint32_t mNext;
376 uint32_t mSize;
377 uint32_t mMin;
378 uint32_t mCount;
379};
380
381} // namespace
382
383//
384// Begin XfrmController Impl
385//
386//
387XfrmController::XfrmController(void) {}
388
Nathan Harold21299f72018-03-16 20:13:03 -0700389netdutils::Status XfrmController::Init() {
390 RETURN_IF_NOT_OK(flushInterfaces());
391 XfrmSocketImpl sock;
392 RETURN_IF_NOT_OK(sock.open());
393 RETURN_IF_NOT_OK(flushSaDb(sock));
394 return flushPolicyDb(sock);
395}
396
397netdutils::Status XfrmController::flushInterfaces() {
398 const auto& ifaces = InterfaceController::getIfaceNames();
399 RETURN_IF_NOT_OK(ifaces);
Nathan Harold45c35662018-04-23 10:10:18 -0700400 const String8 ifPrefix8 = String8(INetd::IPSEC_INTERFACE_PREFIX().string());
Nathan Harold21299f72018-03-16 20:13:03 -0700401
402 for (const std::string& iface : ifaces.value()) {
Benedict Wong319f17e2018-05-15 17:06:44 -0700403 netdutils::Status status;
Nathan Harold21299f72018-03-16 20:13:03 -0700404 // Look for the reserved interface prefix, which must be in the name at position 0
Benedict Wong319f17e2018-05-15 17:06:44 -0700405 if (android::base::StartsWith(iface.c_str(), ifPrefix8.c_str())) {
406 RETURN_IF_NOT_OK(ipSecRemoveTunnelInterface(iface));
Nathan Harold21299f72018-03-16 20:13:03 -0700407 }
408 }
409 return netdutils::status::ok;
410}
411
412netdutils::Status XfrmController::flushSaDb(const XfrmSocket& s) {
413 struct xfrm_usersa_flush flushUserSa = {.proto = IPSEC_PROTO_ANY};
414
Yi Kongbdfd57e2018-07-25 13:26:10 -0700415 std::vector<iovec> iov = {{nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
Nathan Harold21299f72018-03-16 20:13:03 -0700416 {&flushUserSa, sizeof(flushUserSa)}, // xfrm_usersa_flush structure
417 {kPadBytes, NLMSG_ALIGN(sizeof(flushUserSa)) - sizeof(flushUserSa)}};
418
419 return s.sendMessage(XFRM_MSG_FLUSHSA, NETLINK_REQUEST_FLAGS, 0, &iov);
420}
421
422netdutils::Status XfrmController::flushPolicyDb(const XfrmSocket& s) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700423 std::vector<iovec> iov = {{nullptr, 0}}; // reserved for the eventual addition of a NLMSG_HDR
Nathan Harold21299f72018-03-16 20:13:03 -0700424 return s.sendMessage(XFRM_MSG_FLUSHPOLICY, NETLINK_REQUEST_FLAGS, 0, &iov);
425}
426
Benedict Wongb2daefb2017-12-06 22:05:46 -0800427netdutils::Status XfrmController::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
428 int newUid, uid_t callerUid) {
429 ALOGD("XfrmController:%s, line=%d", __FUNCTION__, __LINE__);
430
431 const int fd = socket.get();
432 struct stat info;
433 if (fstat(fd, &info)) {
434 return netdutils::statusFromErrno(errno, "Failed to stat socket file descriptor");
435 }
436 if (info.st_uid != callerUid) {
437 return netdutils::statusFromErrno(EPERM, "fchown disabled for non-owner calls");
438 }
Nathan Haroldda54f122018-01-09 16:42:57 -0800439 if (S_ISSOCK(info.st_mode) == 0) {
Benedict Wongb2daefb2017-12-06 22:05:46 -0800440 return netdutils::statusFromErrno(EINVAL, "File descriptor was not a socket");
441 }
442
443 int optval;
Bjoern Johansson9e604592018-07-19 12:30:55 -0700444 socklen_t optlen = sizeof(optval);
Nathan Haroldda54f122018-01-09 16:42:57 -0800445 netdutils::Status status =
446 getSyscallInstance().getsockopt(Fd(socket), IPPROTO_UDP, UDP_ENCAP, &optval, &optlen);
Benedict Wongb2daefb2017-12-06 22:05:46 -0800447 if (status != netdutils::status::ok) {
448 return status;
449 }
Nathan Haroldda54f122018-01-09 16:42:57 -0800450 if (optval != UDP_ENCAP_ESPINUDP && optval != UDP_ENCAP_ESPINUDP_NON_IKE) {
Benedict Wongb2daefb2017-12-06 22:05:46 -0800451 return netdutils::statusFromErrno(EINVAL, "Socket did not have UDP-encap sockopt set");
452 }
453 if (fchown(fd, newUid, -1)) {
454 return netdutils::statusFromErrno(errno, "Failed to fchown socket file descriptor");
455 }
456
457 return netdutils::status::ok;
458}
459
Nathan Haroldda54f122018-01-09 16:42:57 -0800460netdutils::Status XfrmController::ipSecAllocateSpi(int32_t transformId,
461 const std::string& sourceAddress,
462 const std::string& destinationAddress,
463 int32_t inSpi, int32_t* outSpi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800464 ALOGD("XfrmController:%s, line=%d", __FUNCTION__, __LINE__);
465 ALOGD("transformId=%d", transformId);
Nathan Haroldda54f122018-01-09 16:42:57 -0800466 ALOGD("sourceAddress=%s", sourceAddress.c_str());
467 ALOGD("destinationAddress=%s", destinationAddress.c_str());
Nathan Harold1a371532017-01-30 12:30:48 -0800468 ALOGD("inSpi=%0.8x", inSpi);
469
470 XfrmSaInfo saInfo{};
Benedict Wonga04ffa72018-05-09 21:42:42 -0700471 netdutils::Status ret = fillXfrmCommonInfo(sourceAddress, destinationAddress, INVALID_SPI, 0, 0,
472 transformId, &saInfo);
ludi6e8eccd2017-08-14 14:40:37 -0700473 if (!isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800474 return ret;
475 }
476
477 XfrmSocketImpl sock;
ludi6e8eccd2017-08-14 14:40:37 -0700478 netdutils::Status socketStatus = sock.open();
479 if (!isOk(socketStatus)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800480 ALOGD("Sock open failed for XFRM, line=%d", __LINE__);
ludi6e8eccd2017-08-14 14:40:37 -0700481 return socketStatus;
Nathan Harold1a371532017-01-30 12:30:48 -0800482 }
483
484 int minSpi = RAND_SPI_MIN, maxSpi = RAND_SPI_MAX;
485
486 if (inSpi)
487 minSpi = maxSpi = inSpi;
ludi6e8eccd2017-08-14 14:40:37 -0700488
Nathan Harold1a371532017-01-30 12:30:48 -0800489 ret = allocateSpi(saInfo, minSpi, maxSpi, reinterpret_cast<uint32_t*>(outSpi), sock);
ludi6e8eccd2017-08-14 14:40:37 -0700490 if (!isOk(ret)) {
491 // TODO: May want to return a new Status with a modified status string
Nathan Harold1a371532017-01-30 12:30:48 -0800492 ALOGD("Failed to Allocate an SPI, line=%d", __LINE__);
493 *outSpi = INVALID_SPI;
494 }
495
496 return ret;
497}
498
ludi6e8eccd2017-08-14 14:40:37 -0700499netdutils::Status XfrmController::ipSecAddSecurityAssociation(
Nathan Haroldda54f122018-01-09 16:42:57 -0800500 int32_t transformId, int32_t mode, const std::string& sourceAddress,
manojboopathi8707f232018-01-02 14:45:47 -0800501 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi, int32_t markValue,
502 int32_t markMask, const std::string& authAlgo, const std::vector<uint8_t>& authKey,
503 int32_t authTruncBits, const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey,
504 int32_t cryptTruncBits, const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey,
505 int32_t aeadIcvBits, int32_t encapType, int32_t encapLocalPort, int32_t encapRemotePort) {
Nathan Harold1a371532017-01-30 12:30:48 -0800506 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
507 ALOGD("transformId=%d", transformId);
508 ALOGD("mode=%d", mode);
Nathan Haroldda54f122018-01-09 16:42:57 -0800509 ALOGD("sourceAddress=%s", sourceAddress.c_str());
510 ALOGD("destinationAddress=%s", destinationAddress.c_str());
Benedict Wong96abf482018-01-22 13:56:41 -0800511 ALOGD("underlyingNetworkId=%d", underlyingNetId);
Nathan Harold1a371532017-01-30 12:30:48 -0800512 ALOGD("spi=%0.8x", spi);
Di Lu2ccb3e52018-01-03 16:19:20 -0800513 ALOGD("markValue=%x", markValue);
514 ALOGD("markMask=%x", markMask);
Nathan Harold1a371532017-01-30 12:30:48 -0800515 ALOGD("authAlgo=%s", authAlgo.c_str());
516 ALOGD("authTruncBits=%d", authTruncBits);
517 ALOGD("cryptAlgo=%s", cryptAlgo.c_str());
518 ALOGD("cryptTruncBits=%d,", cryptTruncBits);
Benedict Wongbe65b432017-08-22 21:43:14 -0700519 ALOGD("aeadAlgo=%s", aeadAlgo.c_str());
520 ALOGD("aeadIcvBits=%d,", aeadIcvBits);
Nathan Harold1a371532017-01-30 12:30:48 -0800521 ALOGD("encapType=%d", encapType);
522 ALOGD("encapLocalPort=%d", encapLocalPort);
523 ALOGD("encapRemotePort=%d", encapRemotePort);
524
525 XfrmSaInfo saInfo{};
Benedict Wonga04ffa72018-05-09 21:42:42 -0700526 netdutils::Status ret = fillXfrmCommonInfo(sourceAddress, destinationAddress, spi, markValue,
527 markMask, transformId, &saInfo);
ludi6e8eccd2017-08-14 14:40:37 -0700528 if (!isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800529 return ret;
530 }
531
Nathan Harold1a371532017-01-30 12:30:48 -0800532 saInfo.auth = XfrmAlgo{
533 .name = authAlgo, .key = authKey, .truncLenBits = static_cast<uint16_t>(authTruncBits)};
534
535 saInfo.crypt = XfrmAlgo{
536 .name = cryptAlgo, .key = cryptKey, .truncLenBits = static_cast<uint16_t>(cryptTruncBits)};
537
Benedict Wongbe65b432017-08-22 21:43:14 -0700538 saInfo.aead = XfrmAlgo{
539 .name = aeadAlgo, .key = aeadKey, .truncLenBits = static_cast<uint16_t>(aeadIcvBits)};
540
Nathan Harold1a371532017-01-30 12:30:48 -0800541 switch (static_cast<XfrmMode>(mode)) {
542 case XfrmMode::TRANSPORT:
543 case XfrmMode::TUNNEL:
544 saInfo.mode = static_cast<XfrmMode>(mode);
545 break;
546 default:
ludi6e8eccd2017-08-14 14:40:37 -0700547 return netdutils::statusFromErrno(EINVAL, "Invalid xfrm mode");
Nathan Harold1a371532017-01-30 12:30:48 -0800548 }
549
550 XfrmSocketImpl sock;
ludi6e8eccd2017-08-14 14:40:37 -0700551 netdutils::Status socketStatus = sock.open();
552 if (!isOk(socketStatus)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800553 ALOGD("Sock open failed for XFRM, line=%d", __LINE__);
ludi6e8eccd2017-08-14 14:40:37 -0700554 return socketStatus;
Nathan Harold1a371532017-01-30 12:30:48 -0800555 }
556
Nathan Harold420ceac2017-04-05 19:36:59 -0700557 switch (static_cast<XfrmEncapType>(encapType)) {
558 case XfrmEncapType::ESPINUDP:
559 case XfrmEncapType::ESPINUDP_NON_IKE:
560 if (saInfo.addrFamily != AF_INET) {
ludi6e8eccd2017-08-14 14:40:37 -0700561 return netdutils::statusFromErrno(EAFNOSUPPORT, "IPv6 encap not supported");
Nathan Harold420ceac2017-04-05 19:36:59 -0700562 }
Nathan Haroldda54f122018-01-09 16:42:57 -0800563 // The ports are not used on input SAs, so this is OK to be wrong when
564 // direction is ultimately input.
565 saInfo.encap.srcPort = encapLocalPort;
566 saInfo.encap.dstPort = encapRemotePort;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900567 [[fallthrough]];
Nathan Harold420ceac2017-04-05 19:36:59 -0700568 case XfrmEncapType::NONE:
569 saInfo.encap.type = static_cast<XfrmEncapType>(encapType);
570 break;
571 default:
ludi6e8eccd2017-08-14 14:40:37 -0700572 return netdutils::statusFromErrno(EINVAL, "Invalid encap type");
Nathan Harold420ceac2017-04-05 19:36:59 -0700573 }
574
Benedict Wong96abf482018-01-22 13:56:41 -0800575 saInfo.netId = underlyingNetId;
576
Di Lu0e16b5e2018-01-12 10:37:53 -0800577 ret = updateSecurityAssociation(saInfo, sock);
ludi6e8eccd2017-08-14 14:40:37 -0700578 if (!isOk(ret)) {
Di Lu0e16b5e2018-01-12 10:37:53 -0800579 ALOGD("Failed updating a Security Association, line=%d", __LINE__);
Nathan Harold1a371532017-01-30 12:30:48 -0800580 }
581
ludi6e8eccd2017-08-14 14:40:37 -0700582 return ret;
Nathan Harold1a371532017-01-30 12:30:48 -0800583}
584
Di Lu2ccb3e52018-01-03 16:19:20 -0800585netdutils::Status XfrmController::ipSecDeleteSecurityAssociation(
586 int32_t transformId, const std::string& sourceAddress, const std::string& destinationAddress,
587 int32_t spi, int32_t markValue, int32_t markMask) {
Nathan Harold1a371532017-01-30 12:30:48 -0800588 ALOGD("XfrmController:%s, line=%d", __FUNCTION__, __LINE__);
589 ALOGD("transformId=%d", transformId);
Nathan Haroldda54f122018-01-09 16:42:57 -0800590 ALOGD("sourceAddress=%s", sourceAddress.c_str());
591 ALOGD("destinationAddress=%s", destinationAddress.c_str());
Nathan Harold1a371532017-01-30 12:30:48 -0800592 ALOGD("spi=%0.8x", spi);
Di Lu2ccb3e52018-01-03 16:19:20 -0800593 ALOGD("markValue=%x", markValue);
594 ALOGD("markMask=%x", markMask);
Nathan Harold1a371532017-01-30 12:30:48 -0800595
Benedict Wonga04ffa72018-05-09 21:42:42 -0700596 XfrmSaInfo saInfo{};
597 netdutils::Status ret = fillXfrmCommonInfo(sourceAddress, destinationAddress, spi, markValue,
598 markMask, transformId, &saInfo);
ludi6e8eccd2017-08-14 14:40:37 -0700599 if (!isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800600 return ret;
601 }
602
603 XfrmSocketImpl sock;
ludi6e8eccd2017-08-14 14:40:37 -0700604 netdutils::Status socketStatus = sock.open();
605 if (!isOk(socketStatus)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800606 ALOGD("Sock open failed for XFRM, line=%d", __LINE__);
ludi6e8eccd2017-08-14 14:40:37 -0700607 return socketStatus;
Nathan Harold1a371532017-01-30 12:30:48 -0800608 }
609
Benedict Wonga04ffa72018-05-09 21:42:42 -0700610 ret = deleteSecurityAssociation(saInfo, sock);
ludi6e8eccd2017-08-14 14:40:37 -0700611 if (!isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800612 ALOGD("Failed to delete Security Association, line=%d", __LINE__);
Nathan Harold1a371532017-01-30 12:30:48 -0800613 }
614
615 return ret;
616}
617
Benedict Wonga04ffa72018-05-09 21:42:42 -0700618netdutils::Status XfrmController::fillXfrmCommonInfo(const std::string& sourceAddress,
619 const std::string& destinationAddress,
620 int32_t spi, int32_t markValue,
621 int32_t markMask, int32_t transformId,
622 XfrmCommonInfo* info) {
Nathan Harold7441c692017-12-12 21:25:01 -0800623 // Use the addresses to determine the address family and do validation
Nathan Haroldda54f122018-01-09 16:42:57 -0800624 xfrm_address_t sourceXfrmAddr{}, destXfrmAddr{};
625 StatusOr<int> sourceFamily, destFamily;
626 sourceFamily = convertToXfrmAddr(sourceAddress, &sourceXfrmAddr);
627 destFamily = convertToXfrmAddr(destinationAddress, &destXfrmAddr);
628 if (!isOk(sourceFamily) || !isOk(destFamily)) {
629 return netdutils::statusFromErrno(EINVAL, "Invalid address " + sourceAddress + "/" +
630 destinationAddress);
Nathan Harold1a371532017-01-30 12:30:48 -0800631 }
632
Nathan Haroldda54f122018-01-09 16:42:57 -0800633 if (destFamily.value() == AF_UNSPEC ||
634 (sourceFamily.value() != AF_UNSPEC && sourceFamily.value() != destFamily.value())) {
635 ALOGD("Invalid or Mismatched Address Families, %d != %d, line=%d", sourceFamily.value(),
636 destFamily.value(), __LINE__);
ludi6e8eccd2017-08-14 14:40:37 -0700637 return netdutils::statusFromErrno(EINVAL, "Invalid or mismatched address families");
Nathan Harold1a371532017-01-30 12:30:48 -0800638 }
639
Benedict Wonga04ffa72018-05-09 21:42:42 -0700640 info->addrFamily = destFamily.value();
Nathan Harold1a371532017-01-30 12:30:48 -0800641
Benedict Wonga04ffa72018-05-09 21:42:42 -0700642 info->dstAddr = destXfrmAddr;
643 info->srcAddr = sourceXfrmAddr;
644
645 return fillXfrmCommonInfo(spi, markValue, markMask, transformId, info);
646}
647
648netdutils::Status XfrmController::fillXfrmCommonInfo(int32_t spi, int32_t markValue,
649 int32_t markMask, int32_t transformId,
650 XfrmCommonInfo* info) {
651 info->transformId = transformId;
652 info->spi = htonl(spi);
653 info->mark.v = markValue;
654 info->mark.m = markMask;
655
ludi6e8eccd2017-08-14 14:40:37 -0700656 return netdutils::status::ok;
Nathan Harold1a371532017-01-30 12:30:48 -0800657}
658
ludi6e8eccd2017-08-14 14:40:37 -0700659netdutils::Status XfrmController::ipSecApplyTransportModeTransform(
660 const android::base::unique_fd& socket, int32_t transformId, int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800661 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800662 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
663 ALOGD("transformId=%d", transformId);
664 ALOGD("direction=%d", direction);
Nathan Haroldda54f122018-01-09 16:42:57 -0800665 ALOGD("sourceAddress=%s", sourceAddress.c_str());
666 ALOGD("destinationAddress=%s", destinationAddress.c_str());
Nathan Harold1a371532017-01-30 12:30:48 -0800667 ALOGD("spi=%0.8x", spi);
668
ludi4072ff22017-06-15 08:56:52 -0700669 StatusOr<sockaddr_storage> ret = getSyscallInstance().getsockname<sockaddr_storage>(Fd(socket));
670 if (!isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800671 ALOGE("Failed to get socket info in %s", __FUNCTION__);
ludi6e8eccd2017-08-14 14:40:37 -0700672 return ret;
Nathan Harold1a371532017-01-30 12:30:48 -0800673 }
Nathan Harold7441c692017-12-12 21:25:01 -0800674 struct sockaddr_storage saddr = ret.value();
ludi4072ff22017-06-15 08:56:52 -0700675
Benedict Wonga04ffa72018-05-09 21:42:42 -0700676 XfrmSpInfo spInfo{};
Nathan Harold7441c692017-12-12 21:25:01 -0800677 netdutils::Status status =
Benedict Wonga04ffa72018-05-09 21:42:42 -0700678 fillXfrmCommonInfo(sourceAddress, destinationAddress, spi, 0, 0, transformId, &spInfo);
ludi6e8eccd2017-08-14 14:40:37 -0700679 if (!isOk(status)) {
Nathan Harold1a371532017-01-30 12:30:48 -0800680 ALOGE("Couldn't build SA ID %s", __FUNCTION__);
ludi6e8eccd2017-08-14 14:40:37 -0700681 return status;
Nathan Harold1a371532017-01-30 12:30:48 -0800682 }
683
Benedict Wonga04ffa72018-05-09 21:42:42 -0700684 spInfo.selAddrFamily = spInfo.addrFamily;
685
686 // Allow dual stack sockets. Dual stack sockets are guaranteed to never have an AF_INET source
687 // address; the source address would instead be an IPv4-mapped address. Thus, disallow AF_INET
688 // sockets with mismatched address families (All other cases are acceptable).
689 if (saddr.ss_family == AF_INET && spInfo.addrFamily != AF_INET) {
manojboopathi26f42922017-12-14 10:51:57 -0800690 ALOGE("IPV4 socket address family(%d) should match IPV4 Transform "
691 "address family(%d)!",
Benedict Wonga04ffa72018-05-09 21:42:42 -0700692 saddr.ss_family, spInfo.addrFamily);
ludi6e8eccd2017-08-14 14:40:37 -0700693 return netdutils::statusFromErrno(EINVAL, "Mismatched address family");
Nathan Harold1a371532017-01-30 12:30:48 -0800694 }
695
696 struct {
697 xfrm_userpolicy_info info;
698 xfrm_user_tmpl tmpl;
699 } policy{};
700
Benedict Wonga04ffa72018-05-09 21:42:42 -0700701 fillUserSpInfo(spInfo, static_cast<XfrmDirection>(direction), &policy.info);
702 fillUserTemplate(spInfo, &policy.tmpl);
Nathan Harold1a371532017-01-30 12:30:48 -0800703
704 LOG_HEX("XfrmUserPolicy", reinterpret_cast<char*>(&policy), sizeof(policy));
705
706 int sockOpt, sockLayer;
manojboopathi26f42922017-12-14 10:51:57 -0800707 switch (saddr.ss_family) {
Nathan Harold1a371532017-01-30 12:30:48 -0800708 case AF_INET:
709 sockOpt = IP_XFRM_POLICY;
710 sockLayer = SOL_IP;
711 break;
712 case AF_INET6:
713 sockOpt = IPV6_XFRM_POLICY;
714 sockLayer = SOL_IPV6;
715 break;
716 default:
ludi6e8eccd2017-08-14 14:40:37 -0700717 return netdutils::statusFromErrno(EAFNOSUPPORT, "Invalid address family");
Nathan Harold1a371532017-01-30 12:30:48 -0800718 }
719
ludi6e8eccd2017-08-14 14:40:37 -0700720 status = getSyscallInstance().setsockopt(Fd(socket), sockLayer, sockOpt, policy);
ludi4072ff22017-06-15 08:56:52 -0700721 if (!isOk(status)) {
722 ALOGE("Error setting socket option for XFRM! (%s)", toString(status).c_str());
Nathan Harold1a371532017-01-30 12:30:48 -0800723 }
ludi6e8eccd2017-08-14 14:40:37 -0700724
725 return status;
Nathan Harold1a371532017-01-30 12:30:48 -0800726}
727
ludi6e8eccd2017-08-14 14:40:37 -0700728netdutils::Status
729XfrmController::ipSecRemoveTransportModeTransform(const android::base::unique_fd& socket) {
ludi87991632017-10-30 15:28:11 -0700730 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
731
732 StatusOr<sockaddr_storage> ret = getSyscallInstance().getsockname<sockaddr_storage>(Fd(socket));
733 if (!isOk(ret)) {
734 ALOGE("Failed to get socket info in %s! (%s)", __FUNCTION__, toString(ret).c_str());
735 return ret;
736 }
737
738 int sockOpt, sockLayer;
739 switch (ret.value().ss_family) {
740 case AF_INET:
741 sockOpt = IP_XFRM_POLICY;
742 sockLayer = SOL_IP;
743 break;
744 case AF_INET6:
745 sockOpt = IPV6_XFRM_POLICY;
746 sockLayer = SOL_IPV6;
747 break;
748 default:
749 return netdutils::statusFromErrno(EAFNOSUPPORT, "Invalid address family");
750 }
751
752 // Kernel will delete the security policy on this socket for both direction
753 // if optval is set to NULL and optlen is set to 0.
754 netdutils::Status status =
Yi Kongbdfd57e2018-07-25 13:26:10 -0700755 getSyscallInstance().setsockopt(Fd(socket), sockLayer, sockOpt, nullptr, 0);
ludi87991632017-10-30 15:28:11 -0700756 if (!isOk(status)) {
757 ALOGE("Error removing socket option for XFRM! (%s)", toString(status).c_str());
758 }
759
760 return status;
Nathan Harold1a371532017-01-30 12:30:48 -0800761}
762
Benedict Wonga04ffa72018-05-09 21:42:42 -0700763netdutils::Status XfrmController::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
764 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700765 const std::string& tmplSrcAddress,
766 const std::string& tmplDstAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800767 int32_t spi, int32_t markValue,
768 int32_t markMask) {
Benedict Wonga04ffa72018-05-09 21:42:42 -0700769 return processSecurityPolicy(transformId, selAddrFamily, direction, tmplSrcAddress,
770 tmplDstAddress, spi, markValue, markMask, XFRM_MSG_NEWPOLICY);
ludi771b8002017-11-20 15:09:05 -0800771}
772
Benedict Wonga04ffa72018-05-09 21:42:42 -0700773netdutils::Status XfrmController::ipSecUpdateSecurityPolicy(
774 int32_t transformId, int32_t selAddrFamily, int32_t direction,
775 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
776 int32_t markValue, int32_t markMask) {
777 return processSecurityPolicy(transformId, selAddrFamily, direction, tmplSrcAddress,
778 tmplDstAddress, spi, markValue, markMask, XFRM_MSG_UPDPOLICY);
779}
780
781netdutils::Status XfrmController::ipSecDeleteSecurityPolicy(int32_t transformId,
782 int32_t selAddrFamily,
783 int32_t direction, int32_t markValue,
Di Lu2ccb3e52018-01-03 16:19:20 -0800784 int32_t markMask) {
Benedict Wonga04ffa72018-05-09 21:42:42 -0700785 return processSecurityPolicy(transformId, selAddrFamily, direction, "", "", 0, markValue,
786 markMask, XFRM_MSG_DELPOLICY);
ludi771b8002017-11-20 15:09:05 -0800787}
788
Benedict Wonga04ffa72018-05-09 21:42:42 -0700789netdutils::Status XfrmController::processSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
790 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700791 const std::string& tmplSrcAddress,
792 const std::string& tmplDstAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800793 int32_t spi, int32_t markValue,
794 int32_t markMask, int32_t msgType) {
ludi771b8002017-11-20 15:09:05 -0800795 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
Benedict Wonga04ffa72018-05-09 21:42:42 -0700796 ALOGD("selAddrFamily=%s", selAddrFamily == AF_INET6 ? "AF_INET6" : "AF_INET");
ludi771b8002017-11-20 15:09:05 -0800797 ALOGD("transformId=%d", transformId);
798 ALOGD("direction=%d", direction);
Benedict Wongad600cb2018-05-14 17:22:35 -0700799 ALOGD("tmplSrcAddress=%s", tmplSrcAddress.c_str());
800 ALOGD("tmplDstAddress=%s", tmplDstAddress.c_str());
ludi771b8002017-11-20 15:09:05 -0800801 ALOGD("spi=%0.8x", spi);
Di Lu2ccb3e52018-01-03 16:19:20 -0800802 ALOGD("markValue=%d", markValue);
803 ALOGD("markMask=%d", markMask);
ludi771b8002017-11-20 15:09:05 -0800804 ALOGD("msgType=%d", msgType);
805
Benedict Wonga04ffa72018-05-09 21:42:42 -0700806 XfrmSpInfo spInfo{};
807 spInfo.mode = XfrmMode::TUNNEL;
ludi771b8002017-11-20 15:09:05 -0800808
809 XfrmSocketImpl sock;
810 RETURN_IF_NOT_OK(sock.open());
811
Benedict Wonga04ffa72018-05-09 21:42:42 -0700812 // Set the correct address families. Tunnel mode policies use wildcard selectors, while
813 // templates have addresses set. These may be different address families. This method is called
814 // separately for IPv4 and IPv6 policies, and thus only need to map a single inner address
815 // family to the outer address families.
816 spInfo.selAddrFamily = selAddrFamily;
ludi771b8002017-11-20 15:09:05 -0800817
818 if (msgType == XFRM_MSG_DELPOLICY) {
Benedict Wonga04ffa72018-05-09 21:42:42 -0700819 RETURN_IF_NOT_OK(fillXfrmCommonInfo(spi, markValue, markMask, transformId, &spInfo));
820
821 return deleteTunnelModeSecurityPolicy(spInfo, sock, static_cast<XfrmDirection>(direction));
ludi771b8002017-11-20 15:09:05 -0800822 } else {
Benedict Wonga04ffa72018-05-09 21:42:42 -0700823 RETURN_IF_NOT_OK(fillXfrmCommonInfo(tmplSrcAddress, tmplDstAddress, spi, markValue,
824 markMask, transformId, &spInfo));
825
826 return updateTunnelModeSecurityPolicy(spInfo, sock, static_cast<XfrmDirection>(direction),
ludi771b8002017-11-20 15:09:05 -0800827 msgType);
828 }
829}
830
Benedict Wonga04ffa72018-05-09 21:42:42 -0700831void XfrmController::fillXfrmSelector(const int selAddrFamily, xfrm_selector* selector) {
832 selector->family = selAddrFamily;
manojboopathi8707f232018-01-02 14:45:47 -0800833 selector->proto = AF_UNSPEC; // TODO: do we need to match the protocol? it's
834 // possible via the socket
Nathan Harold1a371532017-01-30 12:30:48 -0800835}
836
Di Lu0e16b5e2018-01-12 10:37:53 -0800837netdutils::Status XfrmController::updateSecurityAssociation(const XfrmSaInfo& record,
manojboopathi4d2d6f12017-12-06 11:11:31 -0800838 const XfrmSocket& sock) {
Nathan Harold1a371532017-01-30 12:30:48 -0800839 xfrm_usersa_info usersa{};
840 nlattr_algo_crypt crypt{};
841 nlattr_algo_auth auth{};
Benedict Wongbe65b432017-08-22 21:43:14 -0700842 nlattr_algo_aead aead{};
Di Lu2ccb3e52018-01-03 16:19:20 -0800843 nlattr_xfrm_mark xfrmmark{};
Benedict Wong96abf482018-01-22 13:56:41 -0800844 nlattr_xfrm_output_mark xfrmoutputmark{};
Nathan Harold420ceac2017-04-05 19:36:59 -0700845 nlattr_encap_tmpl encap{};
Nathan Harold1a371532017-01-30 12:30:48 -0800846
Benedict Wongbe65b432017-08-22 21:43:14 -0700847 enum {
848 NLMSG_HDR,
849 USERSA,
850 USERSA_PAD,
851 CRYPT,
852 CRYPT_PAD,
853 AUTH,
854 AUTH_PAD,
855 AEAD,
856 AEAD_PAD,
Di Lu2ccb3e52018-01-03 16:19:20 -0800857 MARK,
858 MARK_PAD,
Benedict Wong96abf482018-01-22 13:56:41 -0800859 OUTPUT_MARK,
860 OUTPUT_MARK_PAD,
Benedict Wongbe65b432017-08-22 21:43:14 -0700861 ENCAP,
Di Lu2ccb3e52018-01-03 16:19:20 -0800862 ENCAP_PAD,
Benedict Wongbe65b432017-08-22 21:43:14 -0700863 };
Nathan Harold1a371532017-01-30 12:30:48 -0800864
ludie51e3862017-08-15 19:28:12 -0700865 std::vector<iovec> iov = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700866 {nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
Benedict Wong96abf482018-01-22 13:56:41 -0800867 {&usersa, 0}, // main usersa_info struct
868 {kPadBytes, 0}, // up to NLMSG_ALIGNTO pad bytes of padding
869 {&crypt, 0}, // adjust size if crypt algo is present
870 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
871 {&auth, 0}, // adjust size if auth algo is present
872 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
873 {&aead, 0}, // adjust size if aead algo is present
874 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
875 {&xfrmmark, 0}, // adjust size if xfrm mark is present
876 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
877 {&xfrmoutputmark, 0}, // adjust size if xfrm output mark is present
878 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
879 {&encap, 0}, // adjust size if encapsulating
880 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
Nathan Harold1a371532017-01-30 12:30:48 -0800881 };
882
Benedict Wongbe65b432017-08-22 21:43:14 -0700883 if (!record.aead.name.empty() && (!record.auth.name.empty() || !record.crypt.name.empty())) {
884 return netdutils::statusFromErrno(EINVAL, "Invalid xfrm algo selection; AEAD is mutually "
885 "exclusive with both Authentication and "
886 "Encryption");
887 }
888
Benedict Wong4f60ee12017-10-10 12:27:25 -0700889 if (record.aead.key.size() > MAX_KEY_LENGTH || record.auth.key.size() > MAX_KEY_LENGTH ||
890 record.crypt.key.size() > MAX_KEY_LENGTH) {
891 return netdutils::statusFromErrno(EINVAL, "Key length invalid; exceeds MAX_KEY_LENGTH");
Benedict Wongbe65b432017-08-22 21:43:14 -0700892 }
893
Nathan Harold1a371532017-01-30 12:30:48 -0800894 int len;
895 len = iov[USERSA].iov_len = fillUserSaInfo(record, &usersa);
896 iov[USERSA_PAD].iov_len = NLMSG_ALIGN(len) - len;
897
898 len = iov[CRYPT].iov_len = fillNlAttrXfrmAlgoEnc(record.crypt, &crypt);
899 iov[CRYPT_PAD].iov_len = NLA_ALIGN(len) - len;
900
901 len = iov[AUTH].iov_len = fillNlAttrXfrmAlgoAuth(record.auth, &auth);
902 iov[AUTH_PAD].iov_len = NLA_ALIGN(len) - len;
903
Benedict Wongbe65b432017-08-22 21:43:14 -0700904 len = iov[AEAD].iov_len = fillNlAttrXfrmAlgoAead(record.aead, &aead);
905 iov[AEAD_PAD].iov_len = NLA_ALIGN(len) - len;
906
Di Lu2ccb3e52018-01-03 16:19:20 -0800907 len = iov[MARK].iov_len = fillNlAttrXfrmMark(record, &xfrmmark);
908 iov[MARK_PAD].iov_len = NLA_ALIGN(len) - len;
909
Benedict Wong96abf482018-01-22 13:56:41 -0800910 len = iov[OUTPUT_MARK].iov_len = fillNlAttrXfrmOutputMark(record.netId, &xfrmoutputmark);
911 iov[OUTPUT_MARK_PAD].iov_len = NLA_ALIGN(len) - len;
912
Nathan Harold420ceac2017-04-05 19:36:59 -0700913 len = iov[ENCAP].iov_len = fillNlAttrXfrmEncapTmpl(record, &encap);
914 iov[ENCAP_PAD].iov_len = NLA_ALIGN(len) - len;
Di Lu2ccb3e52018-01-03 16:19:20 -0800915
ludie51e3862017-08-15 19:28:12 -0700916 return sock.sendMessage(XFRM_MSG_UPDSA, NETLINK_REQUEST_FLAGS, 0, &iov);
Nathan Harold1a371532017-01-30 12:30:48 -0800917}
918
919int XfrmController::fillNlAttrXfrmAlgoEnc(const XfrmAlgo& inAlgo, nlattr_algo_crypt* algo) {
Benedict Wongbe65b432017-08-22 21:43:14 -0700920 if (inAlgo.name.empty()) { // Do not fill anything if algorithm not provided
921 return 0;
922 }
923
Nathan Harold1a371532017-01-30 12:30:48 -0800924 int len = NLA_HDRLEN + sizeof(xfrm_algo);
Benedict Wongbe65b432017-08-22 21:43:14 -0700925 // Kernel always changes last char to null terminator; no safety checks needed.
Nathan Harold1a371532017-01-30 12:30:48 -0800926 strncpy(algo->crypt.alg_name, inAlgo.name.c_str(), sizeof(algo->crypt.alg_name));
Nathan Harold7441c692017-12-12 21:25:01 -0800927 algo->crypt.alg_key_len = inAlgo.key.size() * 8; // bits
Benedict Wongbe65b432017-08-22 21:43:14 -0700928 memcpy(algo->key, &inAlgo.key[0], inAlgo.key.size());
Nathan Harold1a371532017-01-30 12:30:48 -0800929 len += inAlgo.key.size();
930 fillXfrmNlaHdr(&algo->hdr, XFRMA_ALG_CRYPT, len);
931 return len;
932}
933
934int XfrmController::fillNlAttrXfrmAlgoAuth(const XfrmAlgo& inAlgo, nlattr_algo_auth* algo) {
Benedict Wongbe65b432017-08-22 21:43:14 -0700935 if (inAlgo.name.empty()) { // Do not fill anything if algorithm not provided
936 return 0;
937 }
938
Nathan Harold1a371532017-01-30 12:30:48 -0800939 int len = NLA_HDRLEN + sizeof(xfrm_algo_auth);
Benedict Wongbe65b432017-08-22 21:43:14 -0700940 // Kernel always changes last char to null terminator; no safety checks needed.
Nathan Harold1a371532017-01-30 12:30:48 -0800941 strncpy(algo->auth.alg_name, inAlgo.name.c_str(), sizeof(algo->auth.alg_name));
942 algo->auth.alg_key_len = inAlgo.key.size() * 8; // bits
943
944 // This is the extra field for ALG_AUTH_TRUNC
945 algo->auth.alg_trunc_len = inAlgo.truncLenBits;
946
Benedict Wongbe65b432017-08-22 21:43:14 -0700947 memcpy(algo->key, &inAlgo.key[0], inAlgo.key.size());
Nathan Harold1a371532017-01-30 12:30:48 -0800948 len += inAlgo.key.size();
949
950 fillXfrmNlaHdr(&algo->hdr, XFRMA_ALG_AUTH_TRUNC, len);
951 return len;
952}
953
Benedict Wongbe65b432017-08-22 21:43:14 -0700954int XfrmController::fillNlAttrXfrmAlgoAead(const XfrmAlgo& inAlgo, nlattr_algo_aead* algo) {
955 if (inAlgo.name.empty()) { // Do not fill anything if algorithm not provided
956 return 0;
957 }
958
959 int len = NLA_HDRLEN + sizeof(xfrm_algo_aead);
960 // Kernel always changes last char to null terminator; no safety checks needed.
961 strncpy(algo->aead.alg_name, inAlgo.name.c_str(), sizeof(algo->aead.alg_name));
962 algo->aead.alg_key_len = inAlgo.key.size() * 8; // bits
963
964 // This is the extra field for ALG_AEAD. ICV length is the same as truncation length
965 // for any AEAD algorithm.
966 algo->aead.alg_icv_len = inAlgo.truncLenBits;
967
968 memcpy(algo->key, &inAlgo.key[0], inAlgo.key.size());
969 len += inAlgo.key.size();
970
971 fillXfrmNlaHdr(&algo->hdr, XFRMA_ALG_AEAD, len);
972 return len;
973}
974
Nathan Harold420ceac2017-04-05 19:36:59 -0700975int XfrmController::fillNlAttrXfrmEncapTmpl(const XfrmSaInfo& record, nlattr_encap_tmpl* tmpl) {
976 if (record.encap.type == XfrmEncapType::NONE) {
977 return 0;
978 }
979
980 int len = NLA_HDRLEN + sizeof(xfrm_encap_tmpl);
981 tmpl->tmpl.encap_type = static_cast<uint16_t>(record.encap.type);
982 tmpl->tmpl.encap_sport = htons(record.encap.srcPort);
983 tmpl->tmpl.encap_dport = htons(record.encap.dstPort);
984 fillXfrmNlaHdr(&tmpl->hdr, XFRMA_ENCAP, len);
985 return len;
986}
987
Nathan Harold1a371532017-01-30 12:30:48 -0800988int XfrmController::fillUserSaInfo(const XfrmSaInfo& record, xfrm_usersa_info* usersa) {
Benedict Wonga04ffa72018-05-09 21:42:42 -0700989 // Use AF_UNSPEC for all SAs. In transport mode, kernel picks selector family based on
990 // usersa->family, while in tunnel mode, the XFRM_STATE_AF_UNSPEC flag allows dual-stack SAs.
991 fillXfrmSelector(AF_UNSPEC, &usersa->sel);
Nathan Harold1a371532017-01-30 12:30:48 -0800992
993 usersa->id.proto = IPPROTO_ESP;
994 usersa->id.spi = record.spi;
995 usersa->id.daddr = record.dstAddr;
996
997 usersa->saddr = record.srcAddr;
998
999 fillXfrmLifetimeDefaults(&usersa->lft);
1000 fillXfrmCurLifetimeDefaults(&usersa->curlft);
1001 memset(&usersa->stats, 0, sizeof(usersa->stats)); // leave stats zeroed out
1002 usersa->reqid = record.transformId;
1003 usersa->family = record.addrFamily;
1004 usersa->mode = static_cast<uint8_t>(record.mode);
1005 usersa->replay_window = REPLAY_WINDOW_SIZE;
manojboopathi4d2d6f12017-12-06 11:11:31 -08001006
1007 if (record.mode == XfrmMode::TRANSPORT) {
1008 usersa->flags = 0; // TODO: should we actually set flags, XFRM_SA_XFLAG_DONT_ENCAP_DSCP?
1009 } else {
1010 usersa->flags = XFRM_STATE_AF_UNSPEC;
1011 }
1012
Nathan Harold1a371532017-01-30 12:30:48 -08001013 return sizeof(*usersa);
1014}
1015
Benedict Wonga04ffa72018-05-09 21:42:42 -07001016int XfrmController::fillUserSaId(const XfrmCommonInfo& record, xfrm_usersa_id* said) {
Nathan Harold1a371532017-01-30 12:30:48 -08001017 said->daddr = record.dstAddr;
1018 said->spi = record.spi;
1019 said->family = record.addrFamily;
1020 said->proto = IPPROTO_ESP;
1021
1022 return sizeof(*said);
1023}
1024
Benedict Wonga04ffa72018-05-09 21:42:42 -07001025netdutils::Status XfrmController::deleteSecurityAssociation(const XfrmCommonInfo& record,
ludi6e8eccd2017-08-14 14:40:37 -07001026 const XfrmSocket& sock) {
Nathan Harold1a371532017-01-30 12:30:48 -08001027 xfrm_usersa_id said{};
Di Lu2ccb3e52018-01-03 16:19:20 -08001028 nlattr_xfrm_mark xfrmmark{};
Nathan Harold1a371532017-01-30 12:30:48 -08001029
Di Lu2ccb3e52018-01-03 16:19:20 -08001030 enum { NLMSG_HDR, USERSAID, USERSAID_PAD, MARK, MARK_PAD };
Nathan Harold1a371532017-01-30 12:30:48 -08001031
ludie51e3862017-08-15 19:28:12 -07001032 std::vector<iovec> iov = {
Yi Kongbdfd57e2018-07-25 13:26:10 -07001033 {nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
Nathan Harold1a371532017-01-30 12:30:48 -08001034 {&said, 0}, // main usersa_info struct
1035 {kPadBytes, 0}, // up to NLMSG_ALIGNTO pad bytes of padding
manojboopathi8707f232018-01-02 14:45:47 -08001036 {&xfrmmark, 0}, // adjust size if xfrm mark is present
1037 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
Nathan Harold1a371532017-01-30 12:30:48 -08001038 };
1039
1040 int len;
1041 len = iov[USERSAID].iov_len = fillUserSaId(record, &said);
1042 iov[USERSAID_PAD].iov_len = NLMSG_ALIGN(len) - len;
1043
Di Lu2ccb3e52018-01-03 16:19:20 -08001044 len = iov[MARK].iov_len = fillNlAttrXfrmMark(record, &xfrmmark);
1045 iov[MARK_PAD].iov_len = NLA_ALIGN(len) - len;
1046
ludie51e3862017-08-15 19:28:12 -07001047 return sock.sendMessage(XFRM_MSG_DELSA, NETLINK_REQUEST_FLAGS, 0, &iov);
Nathan Harold1a371532017-01-30 12:30:48 -08001048}
1049
ludi6e8eccd2017-08-14 14:40:37 -07001050netdutils::Status XfrmController::allocateSpi(const XfrmSaInfo& record, uint32_t minSpi,
1051 uint32_t maxSpi, uint32_t* outSpi,
1052 const XfrmSocket& sock) {
Nathan Harold1a371532017-01-30 12:30:48 -08001053 xfrm_userspi_info spiInfo{};
1054
ludie51e3862017-08-15 19:28:12 -07001055 enum { NLMSG_HDR, USERSAID, USERSAID_PAD };
Nathan Harold1a371532017-01-30 12:30:48 -08001056
ludie51e3862017-08-15 19:28:12 -07001057 std::vector<iovec> iov = {
Yi Kongbdfd57e2018-07-25 13:26:10 -07001058 {nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
Nathan Harold1a371532017-01-30 12:30:48 -08001059 {&spiInfo, 0}, // main userspi_info struct
1060 {kPadBytes, 0}, // up to NLMSG_ALIGNTO pad bytes of padding
1061 };
1062
1063 int len;
1064 if (fillUserSaInfo(record, &spiInfo.info) == 0) {
1065 ALOGE("Failed to fill transport SA Info");
1066 }
1067
1068 len = iov[USERSAID].iov_len = sizeof(spiInfo);
1069 iov[USERSAID_PAD].iov_len = NLMSG_ALIGN(len) - len;
1070
1071 RandomSpi spiGen = RandomSpi(minSpi, maxSpi);
ludi6e8eccd2017-08-14 14:40:37 -07001072 int spi;
1073 netdutils::Status ret;
Nathan Harold1a371532017-01-30 12:30:48 -08001074 while ((spi = spiGen.next()) != INVALID_SPI) {
1075 spiInfo.min = spi;
1076 spiInfo.max = spi;
ludie51e3862017-08-15 19:28:12 -07001077 ret = sock.sendMessage(XFRM_MSG_ALLOCSPI, NETLINK_REQUEST_FLAGS, 0, &iov);
Nathan Harold1a371532017-01-30 12:30:48 -08001078
1079 /* If the SPI is in use, we'll get ENOENT */
ludi6e8eccd2017-08-14 14:40:37 -07001080 if (netdutils::equalToErrno(ret, ENOENT))
Nathan Harold1a371532017-01-30 12:30:48 -08001081 continue;
1082
ludi6e8eccd2017-08-14 14:40:37 -07001083 if (isOk(ret)) {
Nathan Harold1a371532017-01-30 12:30:48 -08001084 *outSpi = spi;
Nathan Harold420ceac2017-04-05 19:36:59 -07001085 ALOGD("Allocated an SPI: %x", *outSpi);
Nathan Harold1a371532017-01-30 12:30:48 -08001086 } else {
1087 *outSpi = INVALID_SPI;
ludi6e8eccd2017-08-14 14:40:37 -07001088 ALOGE("SPI Allocation Failed with error %d", ret.code());
Nathan Harold1a371532017-01-30 12:30:48 -08001089 }
1090
1091 return ret;
1092 }
1093
1094 // Should always be -ENOENT if we get here
1095 return ret;
1096}
1097
Benedict Wonga04ffa72018-05-09 21:42:42 -07001098netdutils::Status XfrmController::updateTunnelModeSecurityPolicy(const XfrmSpInfo& record,
ludi771b8002017-11-20 15:09:05 -08001099 const XfrmSocket& sock,
1100 XfrmDirection direction,
1101 uint16_t msgType) {
1102 xfrm_userpolicy_info userpolicy{};
1103 nlattr_user_tmpl usertmpl{};
Di Lu2ccb3e52018-01-03 16:19:20 -08001104 nlattr_xfrm_mark xfrmmark{};
ludi771b8002017-11-20 15:09:05 -08001105
1106 enum {
1107 NLMSG_HDR,
1108 USERPOLICY,
1109 USERPOLICY_PAD,
1110 USERTMPL,
1111 USERTMPL_PAD,
Di Lu2ccb3e52018-01-03 16:19:20 -08001112 MARK,
1113 MARK_PAD,
ludi771b8002017-11-20 15:09:05 -08001114 };
1115
1116 std::vector<iovec> iov = {
Yi Kongbdfd57e2018-07-25 13:26:10 -07001117 {nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
ludi771b8002017-11-20 15:09:05 -08001118 {&userpolicy, 0}, // main xfrm_userpolicy_info struct
1119 {kPadBytes, 0}, // up to NLMSG_ALIGNTO pad bytes of padding
1120 {&usertmpl, 0}, // adjust size if xfrm_user_tmpl struct is present
1121 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
Di Lu2ccb3e52018-01-03 16:19:20 -08001122 {&xfrmmark, 0}, // adjust size if xfrm mark is present
1123 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
ludi771b8002017-11-20 15:09:05 -08001124 };
1125
1126 int len;
Benedict Wonga04ffa72018-05-09 21:42:42 -07001127 len = iov[USERPOLICY].iov_len = fillUserSpInfo(record, direction, &userpolicy);
ludi771b8002017-11-20 15:09:05 -08001128 iov[USERPOLICY_PAD].iov_len = NLMSG_ALIGN(len) - len;
1129
1130 len = iov[USERTMPL].iov_len = fillNlAttrUserTemplate(record, &usertmpl);
1131 iov[USERTMPL_PAD].iov_len = NLA_ALIGN(len) - len;
1132
Di Lu2ccb3e52018-01-03 16:19:20 -08001133 len = iov[MARK].iov_len = fillNlAttrXfrmMark(record, &xfrmmark);
1134 iov[MARK_PAD].iov_len = NLA_ALIGN(len) - len;
1135
ludi771b8002017-11-20 15:09:05 -08001136 return sock.sendMessage(msgType, NETLINK_REQUEST_FLAGS, 0, &iov);
1137}
1138
Benedict Wonga04ffa72018-05-09 21:42:42 -07001139netdutils::Status XfrmController::deleteTunnelModeSecurityPolicy(const XfrmSpInfo& record,
ludi771b8002017-11-20 15:09:05 -08001140 const XfrmSocket& sock,
1141 XfrmDirection direction) {
1142 xfrm_userpolicy_id policyid{};
Di Lu2ccb3e52018-01-03 16:19:20 -08001143 nlattr_xfrm_mark xfrmmark{};
ludi771b8002017-11-20 15:09:05 -08001144
1145 enum {
1146 NLMSG_HDR,
1147 USERPOLICYID,
1148 USERPOLICYID_PAD,
Di Lu2ccb3e52018-01-03 16:19:20 -08001149 MARK,
1150 MARK_PAD,
ludi771b8002017-11-20 15:09:05 -08001151 };
1152
1153 std::vector<iovec> iov = {
Yi Kongbdfd57e2018-07-25 13:26:10 -07001154 {nullptr, 0}, // reserved for the eventual addition of a NLMSG_HDR
ludi771b8002017-11-20 15:09:05 -08001155 {&policyid, 0}, // main xfrm_userpolicy_id struct
1156 {kPadBytes, 0}, // up to NLMSG_ALIGNTO pad bytes of padding
manojboopathi8707f232018-01-02 14:45:47 -08001157 {&xfrmmark, 0}, // adjust size if xfrm mark is present
1158 {kPadBytes, 0}, // up to NLATTR_ALIGNTO pad bytes
ludi771b8002017-11-20 15:09:05 -08001159 };
1160
1161 int len = iov[USERPOLICYID].iov_len = fillUserPolicyId(record, direction, &policyid);
1162 iov[USERPOLICYID_PAD].iov_len = NLMSG_ALIGN(len) - len;
1163
Di Lu2ccb3e52018-01-03 16:19:20 -08001164 len = iov[MARK].iov_len = fillNlAttrXfrmMark(record, &xfrmmark);
1165 iov[MARK_PAD].iov_len = NLA_ALIGN(len) - len;
1166
ludi771b8002017-11-20 15:09:05 -08001167 return sock.sendMessage(XFRM_MSG_DELPOLICY, NETLINK_REQUEST_FLAGS, 0, &iov);
1168}
1169
Benedict Wonga04ffa72018-05-09 21:42:42 -07001170int XfrmController::fillUserSpInfo(const XfrmSpInfo& record, XfrmDirection direction,
1171 xfrm_userpolicy_info* usersp) {
1172 fillXfrmSelector(record.selAddrFamily, &usersp->sel);
Nathan Harold1a371532017-01-30 12:30:48 -08001173 fillXfrmLifetimeDefaults(&usersp->lft);
1174 fillXfrmCurLifetimeDefaults(&usersp->curlft);
Nathan Harold420ceac2017-04-05 19:36:59 -07001175 /* if (index) index & 0x3 == dir -- must be true
1176 * xfrm_user.c:verify_newpolicy_info() */
Nathan Harold1a371532017-01-30 12:30:48 -08001177 usersp->index = 0;
Nathan Haroldda54f122018-01-09 16:42:57 -08001178 usersp->dir = static_cast<uint8_t>(direction);
Nathan Harold1a371532017-01-30 12:30:48 -08001179 usersp->action = XFRM_POLICY_ALLOW;
1180 usersp->flags = XFRM_POLICY_LOCALOK;
1181 usersp->share = XFRM_SHARE_UNIQUE;
1182 return sizeof(*usersp);
1183}
1184
Benedict Wonga04ffa72018-05-09 21:42:42 -07001185int XfrmController::fillUserTemplate(const XfrmSpInfo& record, xfrm_user_tmpl* tmpl) {
Nathan Harold1a371532017-01-30 12:30:48 -08001186 tmpl->id.daddr = record.dstAddr;
1187 tmpl->id.spi = record.spi;
1188 tmpl->id.proto = IPPROTO_ESP;
1189
1190 tmpl->family = record.addrFamily;
1191 tmpl->saddr = record.srcAddr;
1192 tmpl->reqid = record.transformId;
1193 tmpl->mode = static_cast<uint8_t>(record.mode);
1194 tmpl->share = XFRM_SHARE_UNIQUE;
1195 tmpl->optional = 0; // if this is true, then a failed state lookup will be considered OK:
1196 // http://lxr.free-electrons.com/source/net/xfrm/xfrm_policy.c#L1492
1197 tmpl->aalgos = ALGO_MASK_AUTH_ALL; // TODO: if there's a bitmask somewhere of
1198 // algos, we should find it and apply it.
1199 // I can't find one.
1200 tmpl->ealgos = ALGO_MASK_CRYPT_ALL; // TODO: if there's a bitmask somewhere...
ludi771b8002017-11-20 15:09:05 -08001201 return sizeof(xfrm_user_tmpl*);
Nathan Harold1a371532017-01-30 12:30:48 -08001202}
1203
Benedict Wonga04ffa72018-05-09 21:42:42 -07001204int XfrmController::fillNlAttrUserTemplate(const XfrmSpInfo& record, nlattr_user_tmpl* tmpl) {
ludi771b8002017-11-20 15:09:05 -08001205 fillUserTemplate(record, &tmpl->tmpl);
1206
1207 int len = NLA_HDRLEN + sizeof(xfrm_user_tmpl);
1208 fillXfrmNlaHdr(&tmpl->hdr, XFRMA_TMPL, len);
1209 return len;
1210}
1211
Benedict Wonga04ffa72018-05-09 21:42:42 -07001212int XfrmController::fillNlAttrXfrmMark(const XfrmCommonInfo& record, nlattr_xfrm_mark* mark) {
Di Lu2ccb3e52018-01-03 16:19:20 -08001213 mark->mark.v = record.mark.v; // set to 0 if it's not used
1214 mark->mark.m = record.mark.m; // set to 0 if it's not used
1215 int len = NLA_HDRLEN + sizeof(xfrm_mark);
1216 fillXfrmNlaHdr(&mark->hdr, XFRMA_MARK, len);
1217 return len;
1218}
1219
Benedict Wongd8b6a382018-09-18 17:16:10 -07001220// This function sets the output mark (or set-mark in newer kernels) to that of the underlying
1221// Network's netid. This allows outbound IPsec Tunnel mode packets to be correctly directed to a
1222// preselected underlying Network. Packet as marked as protected from VPNs and have a network
1223// explicitly selected to prevent interference or routing loops. Also set permission flag to
1224// PERMISSION_SYSTEM to ensure we can use background/restricted networks.
1225int XfrmController::fillNlAttrXfrmOutputMark(const __u32 underlyingNetId,
manojboopathi8707f232018-01-02 14:45:47 -08001226 nlattr_xfrm_output_mark* output_mark) {
Benedict Wong96abf482018-01-22 13:56:41 -08001227 // Do not set if we were not given an output mark
Benedict Wongd8b6a382018-09-18 17:16:10 -07001228 if (underlyingNetId == 0) {
Benedict Wong96abf482018-01-22 13:56:41 -08001229 return 0;
1230 }
1231
Benedict Wongd8b6a382018-09-18 17:16:10 -07001232 Fwmark fwmark;
1233 fwmark.netId = underlyingNetId;
1234
1235 // TODO: Rework this to more accurately follow the underlying network
1236 fwmark.permission = PERMISSION_SYSTEM;
1237 fwmark.explicitlySelected = true;
1238 fwmark.protectedFromVpn = true;
1239 output_mark->outputMark = fwmark.intValue;
1240
Benedict Wong96abf482018-01-22 13:56:41 -08001241 int len = NLA_HDRLEN + sizeof(__u32);
1242 fillXfrmNlaHdr(&output_mark->hdr, XFRMA_OUTPUT_MARK, len);
1243 return len;
1244}
1245
Benedict Wong319f17e2018-05-15 17:06:44 -07001246int XfrmController::fillNlAttrXfrmIntfId(const uint32_t intfIdValue,
1247 nlattr_xfrm_interface_id* intf_id) {
1248 // Do not set if we were not given an interface id
1249 if (intfIdValue == 0) {
1250 return 0;
1251 }
1252
1253 intf_id->if_id = intfIdValue;
1254 int len = NLA_HDRLEN + sizeof(__u32);
1255 fillXfrmNlaHdr(&intf_id->hdr, XFRMA_IF_ID, len);
1256 return len;
1257}
1258
Benedict Wonga04ffa72018-05-09 21:42:42 -07001259int XfrmController::fillUserPolicyId(const XfrmSpInfo& record, XfrmDirection direction,
ludi771b8002017-11-20 15:09:05 -08001260 xfrm_userpolicy_id* usersp) {
1261 // For DELPOLICY, when index is absent, selector is needed to match the policy
Benedict Wonga04ffa72018-05-09 21:42:42 -07001262 fillXfrmSelector(record.selAddrFamily, &usersp->sel);
ludi771b8002017-11-20 15:09:05 -08001263 usersp->dir = static_cast<uint8_t>(direction);
1264 return sizeof(*usersp);
1265}
1266
Benedict Wong319f17e2018-05-15 17:06:44 -07001267netdutils::Status XfrmController::ipSecAddTunnelInterface(const std::string& deviceName,
1268 const std::string& localAddress,
1269 const std::string& remoteAddress,
1270 int32_t ikey, int32_t okey,
1271 bool isUpdate) {
manojboopathi8707f232018-01-02 14:45:47 -08001272 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
1273 ALOGD("deviceName=%s", deviceName.c_str());
1274 ALOGD("localAddress=%s", localAddress.c_str());
1275 ALOGD("remoteAddress=%s", remoteAddress.c_str());
1276 ALOGD("ikey=%0.8x", ikey);
1277 ALOGD("okey=%0.8x", okey);
1278 ALOGD("isUpdate=%d", isUpdate);
1279
Benedict Wong319f17e2018-05-15 17:06:44 -07001280 uint16_t flags = isUpdate ? NETLINK_REQUEST_FLAGS : NETLINK_ROUTE_CREATE_FLAGS;
1281
1282 return ipSecAddVirtualTunnelInterface(deviceName, localAddress, remoteAddress, ikey, okey,
1283 flags);
1284}
1285
1286netdutils::Status XfrmController::ipSecAddXfrmInterface(const std::string& deviceName,
1287 int32_t underlyingInterface,
1288 int32_t interfaceId, uint16_t flags) {
1289 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
1290
1291 if (deviceName.empty()) {
1292 return netdutils::statusFromErrno(EINVAL, "XFRM Interface deviceName empty");
manojboopathi8707f232018-01-02 14:45:47 -08001293 }
1294
Benedict Wong319f17e2018-05-15 17:06:44 -07001295 ifinfomsg ifInfoMsg{};
1296
1297 struct XfrmIntfCreateReq {
1298 nlattr ifNameNla;
1299 char ifName[IFNAMSIZ]; // Already aligned
1300
1301 nlattr linkInfoNla;
1302 struct LinkInfo {
1303 nlattr infoKindNla;
1304 char infoKind[INFO_KIND_MAX_LEN]; // Already aligned
1305
1306 nlattr infoDataNla;
1307 struct InfoData {
1308 nlattr xfrmLinkNla;
1309 uint32_t xfrmLink;
1310
1311 nlattr xfrmIfIdNla;
1312 uint32_t xfrmIfId;
1313 } infoData; // Already aligned
1314
1315 } linkInfo; // Already aligned
1316 } xfrmIntfCreateReq{
1317 .ifNameNla =
1318 {
1319 .nla_len = RTA_LENGTH(IFNAMSIZ),
1320 .nla_type = IFLA_IFNAME,
1321 },
1322 // Update .ifName via strlcpy
1323
1324 .linkInfoNla =
1325 {
1326 .nla_len = RTA_LENGTH(sizeof(XfrmIntfCreateReq::LinkInfo)),
1327 .nla_type = IFLA_LINKINFO,
1328 },
1329 .linkInfo = {.infoKindNla =
1330 {
1331 .nla_len = RTA_LENGTH(INFO_KIND_MAX_LEN),
1332 .nla_type = IFLA_INFO_KIND,
1333 },
1334 // Update .infoKind via strlcpy
1335
1336 .infoDataNla =
1337 {
1338 .nla_len = RTA_LENGTH(
1339 sizeof(XfrmIntfCreateReq::LinkInfo::InfoData)),
1340 .nla_type = IFLA_INFO_DATA,
1341 },
1342 .infoData = {
1343 .xfrmLinkNla =
1344 {
1345 .nla_len = RTA_LENGTH(sizeof(uint32_t)),
1346 .nla_type = IFLA_XFRM_LINK,
1347 },
1348 .xfrmLink = static_cast<uint32_t>(underlyingInterface),
1349
1350 .xfrmIfIdNla =
1351 {
1352 .nla_len = RTA_LENGTH(sizeof(uint32_t)),
1353 .nla_type = IFLA_XFRM_IF_ID,
1354 },
1355 .xfrmIfId = static_cast<uint32_t>(interfaceId),
1356 }}};
1357
1358 strlcpy(xfrmIntfCreateReq.ifName, deviceName.c_str(), IFNAMSIZ);
1359 strlcpy(xfrmIntfCreateReq.linkInfo.infoKind, INFO_KIND_XFRMI, INFO_KIND_MAX_LEN);
1360
1361 iovec iov[] = {
1362 {NULL, 0}, // reserved for the eventual addition of a NLMSG_HDR
1363 {&ifInfoMsg, sizeof(ifInfoMsg)},
1364
1365 {&xfrmIntfCreateReq, sizeof(xfrmIntfCreateReq)},
1366 };
1367
1368 // sendNetlinkRequest returns -errno
1369 int ret = -sendNetlinkRequest(RTM_NEWLINK, flags, iov, ARRAY_SIZE(iov), nullptr);
1370 return netdutils::statusFromErrno(ret, "Add/update xfrm interface");
1371}
1372
1373netdutils::Status XfrmController::ipSecAddVirtualTunnelInterface(const std::string& deviceName,
1374 const std::string& localAddress,
1375 const std::string& remoteAddress,
1376 int32_t ikey, int32_t okey,
1377 uint16_t flags) {
1378 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
1379
1380 if (deviceName.empty() || localAddress.empty() || remoteAddress.empty()) {
1381 return netdutils::statusFromErrno(EINVAL, "Required VTI creation parameter not provided");
1382 }
1383
manojboopathi8707f232018-01-02 14:45:47 -08001384 uint8_t PADDING_BUFFER[] = {0, 0, 0, 0};
1385
1386 // Find address family.
1387 uint8_t remAddr[sizeof(in6_addr)];
1388
1389 StatusOr<uint16_t> statusOrRemoteFam = convertStringAddress(remoteAddress, remAddr);
Benedict Wong319f17e2018-05-15 17:06:44 -07001390 RETURN_IF_NOT_OK(statusOrRemoteFam);
manojboopathi8707f232018-01-02 14:45:47 -08001391
1392 uint8_t locAddr[sizeof(in6_addr)];
1393 StatusOr<uint16_t> statusOrLocalFam = convertStringAddress(localAddress, locAddr);
Benedict Wong319f17e2018-05-15 17:06:44 -07001394 RETURN_IF_NOT_OK(statusOrLocalFam);
manojboopathi8707f232018-01-02 14:45:47 -08001395
1396 if (statusOrLocalFam.value() != statusOrRemoteFam.value()) {
Benedict Wong319f17e2018-05-15 17:06:44 -07001397 return netdutils::statusFromErrno(EINVAL, "Local and remote address families do not match");
manojboopathi8707f232018-01-02 14:45:47 -08001398 }
1399
1400 uint16_t family = statusOrLocalFam.value();
1401
1402 ifinfomsg ifInfoMsg{};
1403
1404 // Construct IFLA_IFNAME
1405 nlattr iflaIfName;
1406 char iflaIfNameStrValue[deviceName.length() + 1];
1407 size_t iflaIfNameLength =
1408 strlcpy(iflaIfNameStrValue, deviceName.c_str(), sizeof(iflaIfNameStrValue));
1409 size_t iflaIfNamePad = fillNlAttr(IFLA_IFNAME, iflaIfNameLength, &iflaIfName);
1410
1411 // Construct IFLA_INFO_KIND
1412 // Constants "vti6" and "vti" enable the kernel to call different code paths,
1413 // (ip_tunnel.c, ip6_tunnel), based on the family.
1414 const std::string infoKindValue = (family == AF_INET6) ? INFO_KIND_VTI6 : INFO_KIND_VTI;
1415 nlattr iflaIfInfoKind;
1416 char infoKindValueStrValue[infoKindValue.length() + 1];
1417 size_t iflaIfInfoKindLength =
1418 strlcpy(infoKindValueStrValue, infoKindValue.c_str(), sizeof(infoKindValueStrValue));
1419 size_t iflaIfInfoKindPad = fillNlAttr(IFLA_INFO_KIND, iflaIfInfoKindLength, &iflaIfInfoKind);
1420
1421 // Construct IFLA_VTI_LOCAL
1422 nlattr iflaVtiLocal;
1423 uint8_t binaryLocalAddress[sizeof(in6_addr)];
1424 size_t iflaVtiLocalPad =
1425 fillNlAttrIpAddress(IFLA_VTI_LOCAL, family, localAddress, &iflaVtiLocal,
1426 netdutils::makeSlice(binaryLocalAddress));
1427
1428 // Construct IFLA_VTI_REMOTE
1429 nlattr iflaVtiRemote;
1430 uint8_t binaryRemoteAddress[sizeof(in6_addr)];
1431 size_t iflaVtiRemotePad =
1432 fillNlAttrIpAddress(IFLA_VTI_REMOTE, family, remoteAddress, &iflaVtiRemote,
1433 netdutils::makeSlice(binaryRemoteAddress));
1434
1435 // Construct IFLA_VTI_OKEY
Benedict Wong319f17e2018-05-15 17:06:44 -07001436 nlattr_payload_u32 iflaVtiIKey;
1437 size_t iflaVtiIKeyPad = fillNlAttrU32(IFLA_VTI_IKEY, htonl(ikey), &iflaVtiIKey);
manojboopathi8707f232018-01-02 14:45:47 -08001438
1439 // Construct IFLA_VTI_IKEY
Benedict Wong319f17e2018-05-15 17:06:44 -07001440 nlattr_payload_u32 iflaVtiOKey;
1441 size_t iflaVtiOKeyPad = fillNlAttrU32(IFLA_VTI_OKEY, htonl(okey), &iflaVtiOKey);
manojboopathi8707f232018-01-02 14:45:47 -08001442
1443 int iflaInfoDataPayloadLength = iflaVtiLocal.nla_len + iflaVtiLocalPad + iflaVtiRemote.nla_len +
Benedict Wong319f17e2018-05-15 17:06:44 -07001444 iflaVtiRemotePad + iflaVtiIKey.hdr.nla_len + iflaVtiIKeyPad +
1445 iflaVtiOKey.hdr.nla_len + iflaVtiOKeyPad;
manojboopathi8707f232018-01-02 14:45:47 -08001446
1447 // Construct IFLA_INFO_DATA
1448 nlattr iflaInfoData;
1449 size_t iflaInfoDataPad = fillNlAttr(IFLA_INFO_DATA, iflaInfoDataPayloadLength, &iflaInfoData);
1450
1451 // Construct IFLA_LINKINFO
1452 nlattr iflaLinkInfo;
1453 size_t iflaLinkInfoPad = fillNlAttr(IFLA_LINKINFO,
1454 iflaInfoData.nla_len + iflaInfoDataPad +
1455 iflaIfInfoKind.nla_len + iflaIfInfoKindPad,
1456 &iflaLinkInfo);
1457
1458 iovec iov[] = {
Benedict Wong319f17e2018-05-15 17:06:44 -07001459 {nullptr, 0},
1460 {&ifInfoMsg, sizeof(ifInfoMsg)},
manojboopathi8707f232018-01-02 14:45:47 -08001461
Benedict Wong319f17e2018-05-15 17:06:44 -07001462 {&iflaIfName, sizeof(iflaIfName)},
1463 {iflaIfNameStrValue, iflaIfNameLength},
1464 {&PADDING_BUFFER, iflaIfNamePad},
manojboopathi8707f232018-01-02 14:45:47 -08001465
Benedict Wong319f17e2018-05-15 17:06:44 -07001466 {&iflaLinkInfo, sizeof(iflaLinkInfo)},
manojboopathi8707f232018-01-02 14:45:47 -08001467
Benedict Wong319f17e2018-05-15 17:06:44 -07001468 {&iflaIfInfoKind, sizeof(iflaIfInfoKind)},
1469 {infoKindValueStrValue, iflaIfInfoKindLength},
1470 {&PADDING_BUFFER, iflaIfInfoKindPad},
manojboopathi8707f232018-01-02 14:45:47 -08001471
Benedict Wong319f17e2018-05-15 17:06:44 -07001472 {&iflaInfoData, sizeof(iflaInfoData)},
manojboopathi8707f232018-01-02 14:45:47 -08001473
Benedict Wong319f17e2018-05-15 17:06:44 -07001474 {&iflaVtiLocal, sizeof(iflaVtiLocal)},
1475 {&binaryLocalAddress, (family == AF_INET) ? sizeof(in_addr) : sizeof(in6_addr)},
1476 {&PADDING_BUFFER, iflaVtiLocalPad},
manojboopathi8707f232018-01-02 14:45:47 -08001477
Benedict Wong319f17e2018-05-15 17:06:44 -07001478 {&iflaVtiRemote, sizeof(iflaVtiRemote)},
1479 {&binaryRemoteAddress, (family == AF_INET) ? sizeof(in_addr) : sizeof(in6_addr)},
1480 {&PADDING_BUFFER, iflaVtiRemotePad},
manojboopathi8707f232018-01-02 14:45:47 -08001481
Benedict Wong319f17e2018-05-15 17:06:44 -07001482 {&iflaVtiIKey, iflaVtiIKey.hdr.nla_len},
1483 {&PADDING_BUFFER, iflaVtiIKeyPad},
manojboopathi8707f232018-01-02 14:45:47 -08001484
Benedict Wong319f17e2018-05-15 17:06:44 -07001485 {&iflaVtiOKey, iflaVtiOKey.hdr.nla_len},
1486 {&PADDING_BUFFER, iflaVtiOKeyPad},
manojboopathi8707f232018-01-02 14:45:47 -08001487
Benedict Wong319f17e2018-05-15 17:06:44 -07001488 {&PADDING_BUFFER, iflaInfoDataPad},
manojboopathi8707f232018-01-02 14:45:47 -08001489
Benedict Wong319f17e2018-05-15 17:06:44 -07001490 {&PADDING_BUFFER, iflaLinkInfoPad},
manojboopathi8707f232018-01-02 14:45:47 -08001491 };
1492
Nathan Harold5be66022018-05-10 14:15:36 -07001493 // sendNetlinkRequest returns -errno
Benedict Wong319f17e2018-05-15 17:06:44 -07001494 int ret = -1 * sendNetlinkRequest(RTM_NEWLINK, flags, iov, ARRAY_SIZE(iov), nullptr);
1495 return netdutils::statusFromErrno(ret, "Failed to add/update virtual tunnel interface");
manojboopathi8707f232018-01-02 14:45:47 -08001496}
1497
Benedict Wong319f17e2018-05-15 17:06:44 -07001498netdutils::Status XfrmController::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -08001499 ALOGD("XfrmController::%s, line=%d", __FUNCTION__, __LINE__);
1500 ALOGD("deviceName=%s", deviceName.c_str());
1501
1502 if (deviceName.empty()) {
Benedict Wong319f17e2018-05-15 17:06:44 -07001503 return netdutils::statusFromErrno(EINVAL, "Required parameter not provided");
manojboopathi8707f232018-01-02 14:45:47 -08001504 }
1505
1506 uint8_t PADDING_BUFFER[] = {0, 0, 0, 0};
1507
1508 ifinfomsg ifInfoMsg{};
1509 nlattr iflaIfName;
1510 char iflaIfNameStrValue[deviceName.length() + 1];
1511 size_t iflaIfNameLength =
1512 strlcpy(iflaIfNameStrValue, deviceName.c_str(), sizeof(iflaIfNameStrValue));
1513 size_t iflaIfNamePad = fillNlAttr(IFLA_IFNAME, iflaIfNameLength, &iflaIfName);
1514
1515 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -07001516 {nullptr, 0},
manojboopathi8707f232018-01-02 14:45:47 -08001517 {&ifInfoMsg, sizeof(ifInfoMsg)},
1518
1519 {&iflaIfName, sizeof(iflaIfName)},
1520 {iflaIfNameStrValue, iflaIfNameLength},
1521 {&PADDING_BUFFER, iflaIfNamePad},
1522 };
1523
1524 uint16_t action = RTM_DELLINK;
1525 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
1526
Nathan Harold5be66022018-05-10 14:15:36 -07001527 // sendNetlinkRequest returns -errno
1528 int ret = -1 * sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Benedict Wong319f17e2018-05-15 17:06:44 -07001529 return netdutils::statusFromErrno(ret, "Error in deleting IpSec interface " + deviceName);
manojboopathi8707f232018-01-02 14:45:47 -08001530}
ludi771b8002017-11-20 15:09:05 -08001531
Nathan Harold1a371532017-01-30 12:30:48 -08001532} // namespace net
1533} // namespace android