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 #ifndef INCLUDE_NETLINK_SOCKET_H 17 #define INCLUDE_NETLINK_SOCKET_H 18 19 #include <functional> 20 #include <linux/netlink.h> 21 #include <linux/rtnetlink.h> 22 #include <memory> 23 #include <netinet/in.h> 24 #include <sys/epoll.h> 25 26 namespace OHOS { 27 namespace nmd { 28 constexpr uint32_t NETLINKMESSAGE_MAX_LEN = 1024; 29 constexpr uint32_t KERNEL_BUFFER_SIZE = 8192; 30 constexpr uint32_t LOCAL_PRIORITY = 32767; 31 /** 32 * Send netklink message to kernel 33 * 34 * @param msg nlmsghdr struct 35 * @param table If clear route,this is table number, otherwise it will is 0 36 * @return Returns 0, send netklink message to kernel successfully, otherwise it will fail 37 */ 38 int32_t SendNetlinkMsgToKernel(nlmsghdr *msg, uint32_t table = 0); 39 40 /** 41 * Clear route or rule configure 42 * 43 * @param clearThing Decide to clear route or rule. Must be one of RTM_GETRULE/RTM_GETROUTE 44 * @param table If clear route,this is table number, otherwise it will is 0 45 * @return Returns 0, clear route or rule configure successfully, otherwise it will fail 46 */ 47 int32_t ClearRouteInfo(uint16_t clearThing, uint32_t table); 48 49 /** 50 * Get info from kernel 51 * 52 * @param sock Sock for read 53 * @param clearThing Type for kernel nlmsg_type 54 * @param table Route property for RTA_TABLE 55 * @return Returns 0, get info from kernel successfully, otherwise it will fail 56 */ 57 int32_t GetInfoFromKernel(int32_t sock, uint16_t clearThing, uint32_t table); 58 59 /** 60 * Deal info from kernel 61 * 62 * @param nlmsgHeader nlmsghdr 63 * @param clearThing Type for kernel nlmsg_type 64 * @param table Route property for RTA_TABLE 65 * @return Returns 0, deal info from kernel successfully, otherwise it will fail 66 */ 67 void DealInfoFromKernel(nlmsghdr *nlmsgHeader, uint16_t clearThing, uint32_t table); 68 69 /** 70 * Get route property 71 * 72 * @param nlmsgHeader nlmsghdr 73 * @param property Property for route 74 * @return Returns 0, get route property successfully, otherwise it will fail 75 */ 76 int32_t GetRouteProperty(const nlmsghdr *nlmsgHeader, int32_t property); 77 78 #ifdef SUPPORT_SYSVPN 79 /** 80 * Create vpn Interface by netlink 81 * 82 * @param name ifname 83 * @param ifNameId interface name id 84 * @param mtu mtu 85 * @param phys phy interface 86 * @return Returns 0, create vpn interface successfully, otherwise it will fail 87 */ 88 int32_t CreateVpnIfByNetlink(const char *name, uint32_t ifNameId, const char *phys, uint32_t mtu); 89 90 /** 91 * Delete Vpn Interface by netlink 92 * 93 * @param name ifname 94 * @return Returns 0, delete vpn interface successfully, otherwise it will fail 95 */ 96 int32_t DeleteVpnIfByNetlink(const char *name); 97 #endif 98 } // namespace nmd 99 } // namespace OHOS 100 #endif // !INCLUDE_NETLINK_SOCKET_H 101