1 /*
2 * Copyright (c) 2021 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 "netlink_msg.h"
17 #include "securec.h"
18 #include "netnative_log_wrapper.h"
19
20 namespace OHOS {
21 namespace nmd {
NetlinkMsg(uint16_t flags,size_t maxBufLen,int pid)22 NetlinkMsg::NetlinkMsg(uint16_t flags, size_t maxBufLen, int pid)
23 {
24 this->maxBufLen = maxBufLen;
25 this->netlinkMessage = reinterpret_cast<struct nlmsghdr *>(new char[NLMSG_SPACE(maxBufLen)]);
26 errno_t result = memset_s(this->netlinkMessage, NLMSG_SPACE(maxBufLen), 0, NLMSG_SPACE(maxBufLen));
27 if (result != 0) {
28 NETNATIVE_LOGE("[NetlinkMessage]: memset result %{public}d", result);
29 }
30 this->netlinkMessage->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
31 this->netlinkMessage->nlmsg_pid = static_cast<uint32_t>(pid);
32 this->netlinkMessage->nlmsg_seq = 1;
33 }
34
~NetlinkMsg()35 NetlinkMsg::~NetlinkMsg()
36 {
37 delete [] this->netlinkMessage;
38 }
39
AddRoute(unsigned short action,struct rtmsg msg)40 void NetlinkMsg::AddRoute(unsigned short action, struct rtmsg msg)
41 {
42 this->netlinkMessage->nlmsg_type = action;
43 int32_t result = memcpy_s(NLMSG_DATA(this->netlinkMessage), sizeof(struct rtmsg), &msg, sizeof(struct rtmsg));
44 if (result != 0) {
45 NETNATIVE_LOGE("[AddRoute]: string copy failed result %{public}d", result);
46 }
47 this->netlinkMessage->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
48 }
49
AddRule(unsigned short action,struct fib_rule_hdr msg)50 void NetlinkMsg::AddRule(unsigned short action, struct fib_rule_hdr msg)
51 {
52 this->netlinkMessage->nlmsg_type = action;
53 int32_t result = memcpy_s(NLMSG_DATA(this->netlinkMessage), sizeof(struct fib_rule_hdr),
54 &msg, sizeof(struct fib_rule_hdr));
55 if (result != 0) {
56 NETNATIVE_LOGE("[AddRule]: string copy failed result %{public}d", result);
57 }
58 this->netlinkMessage->nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr));
59 }
60
AddAddress(unsigned short action,struct ifaddrmsg msg)61 void NetlinkMsg::AddAddress(unsigned short action, struct ifaddrmsg msg)
62 {
63 this->netlinkMessage->nlmsg_type = action;
64 int32_t result = memcpy_s(NLMSG_DATA(this->netlinkMessage), sizeof(struct ifaddrmsg),
65 &msg, sizeof(struct ifaddrmsg));
66 if (result != 0) {
67 NETNATIVE_LOGE("[AddAddress]: string copy failed result %{public}d", result);
68 }
69 this->netlinkMessage->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
70 }
71
AddAttr(unsigned int type,void * data,size_t alen)72 int NetlinkMsg::AddAttr(unsigned int type, void *data, size_t alen)
73 {
74 if (alen == 0) {
75 NETNATIVE_LOGE("[NetlinkMessage]: length data can not be 0");
76 return -1;
77 }
78
79 if (data == nullptr) {
80 NETNATIVE_LOGE("[NetlinkMessage]: attr data can not be null");
81 return -1;
82 }
83
84 int len = RTA_LENGTH(alen);
85 if (NLMSG_ALIGN(this->netlinkMessage->nlmsg_len) + RTA_ALIGN(len) > this->maxBufLen) {
86 NETNATIVE_LOGE("[NetlinkMessage]: attr length than max len: %{public}d", (int32_t)this->maxBufLen);
87 return -1;
88 }
89
90 struct rtattr *rta =
91 (struct rtattr *)(((char *)this->netlinkMessage) + NLMSG_ALIGN(this->netlinkMessage->nlmsg_len));
92 rta->rta_type = type;
93 rta->rta_len = static_cast<uint16_t>(len);
94
95 if (data != nullptr) {
96 int32_t result = memcpy_s(RTA_DATA(rta), alen, data, alen);
97 if (result != 0) {
98 NETNATIVE_LOGE("[get_addr_info]: string copy failed result %{public}d", result);
99 return -1;
100 }
101 }
102
103 this->netlinkMessage->nlmsg_len = NLMSG_ALIGN(this->netlinkMessage->nlmsg_len) + RTA_ALIGN(len);
104 return 1;
105 }
106
AddAttr16(unsigned int type,uint16_t data)107 int NetlinkMsg::AddAttr16(unsigned int type, uint16_t data)
108 {
109 return this->AddAttr(type, &data, sizeof(uint16_t));
110 }
111
AddAttr32(unsigned int type,uint32_t data)112 int NetlinkMsg::AddAttr32(unsigned int type, uint32_t data)
113 {
114 return this->AddAttr(type, &data, sizeof(uint32_t));
115 }
116
GetNetLinkMessage()117 nlmsghdr *NetlinkMsg::GetNetLinkMessage()
118 {
119 return this->netlinkMessage;
120 }
121 } // namespace nmd
122 } // namespace OHOS