1 /*
2 * Copyright (c) 2021-2023 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 <arpa/inet.h>
17 #include <asm/types.h>
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 #include <linux/fib_rules.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/uio.h>
28 #include <unistd.h>
29
30 #include "netnative_log_wrapper.h"
31 #include "securec.h"
32
33 #include "netlink_socket.h"
34 namespace OHOS {
35 namespace nmd {
SendNetlinkMsgToKernel(struct nlmsghdr * msg,uint32_t table)36 int32_t SendNetlinkMsgToKernel(struct nlmsghdr *msg, uint32_t table)
37 {
38 if (msg == nullptr) {
39 NETNATIVE_LOGE("[NetlinkSocket] msg can not be null ");
40 return -1;
41 }
42 int32_t kernelSocket = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
43 if (kernelSocket == -1) {
44 NETNATIVE_LOGE("[NetlinkSocket] create socket failed: %{public}d", errno);
45 return -1;
46 }
47 struct iovec ioVector;
48 ioVector.iov_base = msg;
49 ioVector.iov_len = msg->nlmsg_len;
50
51 struct msghdr msgHeader;
52 (void)memset_s(&msgHeader, sizeof(msgHeader), 0, sizeof(msgHeader));
53
54 struct sockaddr_nl kernel;
55 (void)memset_s(&kernel, sizeof(kernel), 0, sizeof(kernel));
56 kernel.nl_family = AF_NETLINK;
57 kernel.nl_groups = 0;
58
59 msgHeader.msg_name = &kernel;
60 msgHeader.msg_namelen = sizeof(kernel);
61 msgHeader.msg_iov = &ioVector;
62 msgHeader.msg_iovlen = 1;
63
64 ssize_t msgState = sendmsg(kernelSocket, &msgHeader, 0);
65 if (msgState == -1) {
66 NETNATIVE_LOGE("[NetlinkSocket] msg can not be null ");
67 close(kernelSocket);
68 return -1;
69 } else if (msgState == 0) {
70 NETNATIVE_LOGE("[NetlinkSocket] 0 bytes send.");
71 close(kernelSocket);
72 return -1;
73 }
74 NETNATIVE_LOG_D("[NetlinkSocket] msgState is %{public}zd", msgState);
75 if (msg->nlmsg_flags & NLM_F_DUMP) {
76 msgState = GetInfoFromKernel(kernelSocket, msg->nlmsg_type, table);
77 }
78 close(kernelSocket);
79 return msgState;
80 }
81
ClearRouteInfo(uint16_t clearThing,uint32_t table)82 int32_t ClearRouteInfo(uint16_t clearThing, uint32_t table)
83 {
84 if (clearThing != RTM_GETROUTE && clearThing != RTM_GETRULE) {
85 NETNATIVE_LOGE("ClearRouteInfo %{public}d type error", clearThing);
86 return -1;
87 }
88 // Request the kernel to send a list of all routes or rules.
89 std::unique_ptr<char[]> msghdrBuf = std::make_unique<char[]>(NLMSG_SPACE(NETLINKMESSAGE_MAX_LEN));
90 struct nlmsghdr *msghdr = reinterpret_cast<struct nlmsghdr *>(msghdrBuf.get());
91 errno_t result = memset_s(msghdr, NLMSG_SPACE(NETLINKMESSAGE_MAX_LEN), 0, NLMSG_SPACE(NETLINKMESSAGE_MAX_LEN));
92 if (result != 0) {
93 NETNATIVE_LOGE("[NetlinkMessage]: memset result %{public}d", result);
94 }
95 rtmsg msg;
96 msg.rtm_family = AF_INET;
97 int32_t copeResult = memcpy_s(NLMSG_DATA(msghdr), sizeof(struct rtmsg), &msg, sizeof(struct rtmsg));
98 if (copeResult != 0) {
99 NETNATIVE_LOGE("[AddRoute]: string copy failed result %{public}d", copeResult);
100 }
101 msghdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
102 msghdr->nlmsg_type = clearThing;
103 msghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
104 return SendNetlinkMsgToKernel(msghdr);
105 }
106
GetInfoFromKernel(int32_t sock,uint16_t clearThing,uint32_t table)107 int32_t GetInfoFromKernel(int32_t sock, uint16_t clearThing, uint32_t table)
108 {
109 char readBuffer[KERNEL_BUFFER_SIZE] = {0};
110 // Read the information returned by the kernel through the socket.
111 ssize_t readedInfos = read(sock, readBuffer, sizeof(readBuffer));
112 if (readedInfos < 0) {
113 return -errno;
114 }
115 while (readedInfos > 0) {
116 uint32_t readLength = readedInfos;
117 // Traverse and read the information returned by the kernel for item by item processing.
118 for (nlmsghdr *nlmsgHeader = reinterpret_cast<nlmsghdr *>(readBuffer); NLMSG_OK(nlmsgHeader, readLength);
119 nlmsgHeader = NLMSG_NEXT(nlmsgHeader, readLength)) {
120 if (nlmsgHeader->nlmsg_type == NLMSG_ERROR) {
121 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlmsgHeader));
122 NETNATIVE_LOGE("netlink read socket failed error = %{public}d", err->error);
123 return err->error;
124 } else if (nlmsgHeader->nlmsg_type == NLMSG_DONE) {
125 return 0;
126 } else {
127 DealInfoFromKernel(nlmsgHeader, clearThing, table);
128 }
129 }
130 readedInfos = read(sock, readBuffer, sizeof(readBuffer));
131 if (readedInfos < 0) {
132 return -errno;
133 }
134 }
135 return 0;
136 }
137
DealInfoFromKernel(nlmsghdr * nlmsgHeader,uint16_t clearThing,uint32_t table)138 void DealInfoFromKernel(nlmsghdr *nlmsgHeader, uint16_t clearThing, uint32_t table)
139 {
140 if (nlmsgHeader == nullptr) {
141 NETNATIVE_LOGE("nlmsgHeader is nullptr");
142 return;
143 }
144 struct nlmsghdr *msg = nlmsgHeader;
145 msg->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
146 if (clearThing == RTM_GETRULE) {
147 msg->nlmsg_type = RTM_DELRULE;
148 if (GetRouteProperty(nlmsgHeader, FRA_PRIORITY) != static_cast<int32_t>(LOCAL_PRIORITY)) {
149 return;
150 }
151 } else if (clearThing == RTM_GETROUTE) {
152 msg->nlmsg_type = RTM_DELROUTE;
153 if (GetRouteProperty(nlmsgHeader, RTA_TABLE) != static_cast<int32_t>(table)) {
154 return;
155 }
156 }
157 SendNetlinkMsgToKernel(msg);
158 }
159
GetRouteProperty(const nlmsghdr * nlmsgHeader,int32_t property)160 int32_t GetRouteProperty(const nlmsghdr *nlmsgHeader, int32_t property)
161 {
162 if (nlmsgHeader == nullptr) {
163 NETNATIVE_LOGE("nlmsgHeader is nullptr");
164 return -1;
165 }
166 uint32_t rtaLength = RTM_PAYLOAD(nlmsgHeader);
167 rtmsg *infoMsg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlmsgHeader));
168 for (rtattr *infoRta = reinterpret_cast<rtattr *> RTM_RTA(infoMsg); RTA_OK(infoRta, rtaLength);
169 infoRta = RTA_NEXT(infoRta, rtaLength)) {
170 if (infoRta->rta_type == property) {
171 return *(reinterpret_cast<uint32_t *>(RTA_DATA(infoRta)));
172 }
173 }
174 return 0;
175 }
176 } // namespace nmd
177 } // namespace OHOS
178