1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "fwmark_client.h"
17
18 #include <cerrno>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <unistd.h>
22
23 #include "fwmark.h"
24 #include "fwmark_command.h"
25 #include "net_manager_constants.h"
26 #include "netnative_log_wrapper.h"
27 #include "securec.h"
28
29 namespace OHOS {
30 namespace nmd {
31 using namespace NetManagerStandard;
32 static constexpr const int32_t ERROR_CODE_SOCKETFD_INVALID = -1;
33 static constexpr const int32_t ERROR_CODE_CONNECT_FAILED = -2;
34 static constexpr const int32_t ERROR_CODE_SENDMSG_FAILED = -3;
35 static constexpr const int32_t ERROR_CODE_READ_FAILED = -4;
36
FwmarkClient()37 FwmarkClient::FwmarkClient() {}
38
~FwmarkClient()39 FwmarkClient::~FwmarkClient() {}
40
BindSocket(int32_t fd,uint32_t netId)41 int32_t FwmarkClient::BindSocket(int32_t fd, uint32_t netId)
42 {
43 FwmarkCommand command = {FwmarkCommand::SELECT_NETWORK, netId};
44 return Send(&command, fd);
45 }
46
ProtectFromVpn(int32_t socketFd)47 int32_t FwmarkClient::ProtectFromVpn(int32_t socketFd)
48 {
49 if (socketFd < 0) {
50 return HandleError(-1, ERROR_CODE_SOCKETFD_INVALID, socketFd);
51 }
52 FwmarkCommand command = {FwmarkCommand::PROTECT_FROM_VPN, 0};
53 return Send(&command, socketFd);
54 }
55
Send(FwmarkCommand * data,int32_t fd)56 int32_t FwmarkClient::Send(FwmarkCommand *data, int32_t fd)
57 {
58 auto socketFd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
59 if (socketFd == -1) {
60 return HandleError(-1, ERROR_CODE_SOCKETFD_INVALID, socketFd);
61 }
62 if (connect(socketFd, reinterpret_cast<const sockaddr *>(&FWMARK_SERVER_PATH), sizeof(FWMARK_SERVER_PATH)) == -1) {
63 return HandleError(-1, ERROR_CODE_CONNECT_FAILED, socketFd);
64 }
65
66 iovec iov;
67 iov.iov_base = data;
68 iov.iov_len = sizeof(*data);
69 msghdr message;
70 (void)memset_s(&message, sizeof(message), 0, sizeof(message));
71 message.msg_iov = &iov;
72 message.msg_iovlen = 1;
73 union {
74 cmsghdr cmh;
75 char cmsg[CMSG_SPACE(sizeof(fd))];
76 } cmsgu;
77
78 (void)memset_s(cmsgu.cmsg, sizeof(cmsgu.cmsg), 0, sizeof(cmsgu.cmsg));
79 message.msg_control = cmsgu.cmsg;
80 message.msg_controllen = sizeof(cmsgu.cmsg);
81 cmsghdr *const cmsgh = CMSG_FIRSTHDR(&message);
82 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
83 cmsgh->cmsg_level = SOL_SOCKET;
84 cmsgh->cmsg_type = SCM_RIGHTS;
85 (void)memcpy_s(CMSG_DATA(cmsgh), sizeof(fd), &fd, sizeof(fd));
86 int32_t ret = sendmsg(socketFd, &message, 0);
87 if (ret < 0) {
88 return HandleError(ret, ERROR_CODE_SENDMSG_FAILED, socketFd);
89 }
90 int32_t error = 0;
91 ret = read(socketFd, &error, sizeof(error));
92 if (ret < 0) {
93 return HandleError(ret, ERROR_CODE_READ_FAILED, socketFd);
94 }
95
96 close(socketFd);
97 return NETMANAGER_SUCCESS;
98 }
99
HandleError(int32_t ret,int32_t errorCode,int32_t sock)100 int32_t FwmarkClient::HandleError(int32_t ret, int32_t errorCode, int32_t sock)
101 {
102 switch (errorCode) {
103 case ERROR_CODE_SOCKETFD_INVALID:
104 NETNATIVE_LOGE("socketFd invalid, ret:%{public}d, errno: %{public}d", ret, errno);
105 break;
106 case ERROR_CODE_CONNECT_FAILED:
107 NETNATIVE_LOGE("connect failed, ret:%{public}d, errno: %{public}d", ret, errno);
108 break;
109 case ERROR_CODE_SENDMSG_FAILED:
110 NETNATIVE_LOGE("sendmsg failed, ret:%{public}d, errno: %{public}d", ret, errno);
111 break;
112 case ERROR_CODE_READ_FAILED:
113 NETNATIVE_LOGE("read failed, ret:%{public}d, errno: %{public}d", ret, errno);
114 break;
115 default:
116 break;
117 }
118 if (sock > 0) {
119 close(sock);
120 }
121 return NETMANAGER_ERROR;
122 }
123
124 #ifdef __cplusplus
BindSocket(int32_t fd,uint32_t netId)125 extern int32_t BindSocket(int32_t fd, uint32_t netId)
126 {
127 FwmarkClient instance;
128 return instance.BindSocket(fd, netId);
129 }
130 #endif
131 } // namespace nmd
132 } // namespace OHOS
133