1 /* 2 * Copyright (c) 2021-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 #ifndef NETSYS_CONTROLLER_H 17 #define NETSYS_CONTROLLER_H 18 19 #include <linux/if.h> 20 21 #include "refbase.h" 22 23 #include "i_netsys_controller_service.h" 24 25 namespace OHOS { 26 namespace NetManagerStandard { 27 class NetsysController { 28 public: 29 ~NetsysController() = default; 30 void Init(); 31 32 static NetsysController &GetInstance(); 33 /** 34 * Create a physical network 35 * 36 * @param netId 37 * @param permission Permission to create a physical network 38 * @return Return the return value of the netsys interface call 39 */ 40 int32_t NetworkCreatePhysical(int32_t netId, int32_t permission); 41 42 /** 43 * Destroy the network 44 * 45 * @param netId 46 * @return Return the return value of the netsys interface call 47 */ 48 int32_t NetworkDestroy(int32_t netId); 49 50 /** 51 * Add network port device 52 * 53 * @param netId 54 * @param iface Network port device name 55 * @return Return the return value of the netsys interface call 56 */ 57 int32_t NetworkAddInterface(int32_t netId, const std::string &iface); 58 59 /** 60 * Delete network port device 61 * 62 * @param netId 63 * @param iface Network port device name 64 * @return Return the return value of the netsys interface call 65 */ 66 int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface); 67 68 /** 69 * Add route 70 * 71 * @param netId 72 * @param ifName Network port device name 73 * @param destination Target host ip 74 * @param nextHop Next hop address 75 * @return Return the return value of the netsys interface call 76 */ 77 int32_t NetworkAddRoute(int32_t netId, const std::string &ifName, const std::string &destination, 78 const std::string &nextHop); 79 80 /** 81 * Remove route 82 * 83 * @param netId 84 * @param ifName Network port device name 85 * @param destination Target host ip 86 * @param nextHop Next hop address 87 * @return Return the return value of the netsys interface call 88 */ 89 int32_t NetworkRemoveRoute(int32_t netId, const std::string &ifName, const std::string &destination, 90 const std::string &nextHop); 91 92 /** 93 * @brief Get interface config 94 * 95 * @param iface Network port device name 96 * @return Return the result of this action 97 */ 98 int32_t InterfaceGetConfig(OHOS::nmd::InterfaceConfigurationParcel &cfg); 99 100 /** 101 * Turn off the device 102 * 103 * @param iface Network port device name 104 * @return Return the result of this action 105 */ 106 int32_t SetInterfaceDown(const std::string &iface); 107 108 /** 109 * Turn on the device 110 * 111 * @param iface Network port device name 112 * @return Return the result of this action 113 */ 114 int32_t SetInterfaceUp(const std::string &iface); 115 116 /** 117 * Clear the network interface ip address 118 * 119 * @param ifName Network port device name 120 */ 121 void InterfaceClearAddrs(const std::string &ifName); 122 123 /** 124 * Obtain mtu from the network interface device 125 * 126 * @param ifName Network port device name 127 * @return Return the return value of the netsys interface call 128 */ 129 int32_t InterfaceGetMtu(const std::string &ifName); 130 131 /** 132 * Set mtu to network interface device 133 * 134 * @param ifName Network port device name 135 * @param mtu 136 * @return Return the return value of the netsys interface call 137 */ 138 int32_t InterfaceSetMtu(const std::string &ifName, int32_t mtu); 139 140 /** 141 * Add ip address 142 * 143 * @param ifName Network port device name 144 * @param ipAddr ip address 145 * @param prefixLength subnet mask 146 * @return Return the return value of the netsys interface call 147 */ 148 int32_t InterfaceAddAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength); 149 150 /** 151 * Delete ip address 152 * 153 * @param ifName Network port device name 154 * @param ipAddr ip address 155 * @param prefixLength subnet mask 156 * @return Return the return value of the netsys interface call 157 */ 158 int32_t InterfaceDelAddress(const std::string &ifName, const std::string &ipAddr, int32_t prefixLength); 159 160 /** 161 * Set iface ip address 162 * 163 * @param ifaceName Network port device name 164 * @param ipAddress Ip address 165 * @return Return the return value of the netsys interface call 166 */ 167 int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress); 168 169 /** 170 * Set iface up 171 * 172 * @param ifaceName Network port device name 173 * @return Return the return value of the netsys interface call 174 */ 175 int32_t InterfaceSetIffUp(const std::string &ifaceName); 176 177 /** 178 * Set dns 179 * 180 * @param netId 181 * @param baseTimeoutMsec 182 * @param retryCount 183 * @param servers 184 * @param domains 185 * @return Return the return value of the netsys interface call 186 */ 187 int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, 188 const std::vector<std::string> &servers, const std::vector<std::string> &domains); 189 /** 190 * Get dns server param info 191 * 192 * @param netId 193 * @param servers 194 * @param domains 195 * @param baseTimeoutMsec 196 * @param retryCount 197 * @return Return the return value of the netsys interface call 198 */ 199 int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains, 200 uint16_t &baseTimeoutMsec, uint8_t &retryCount); 201 202 /** 203 * Create dns cache before set dns 204 * 205 * @param netId 206 * @return Return the return value for status of call 207 */ 208 int32_t CreateNetworkCache(uint16_t netId); 209 210 /** 211 * Destroy dns cache 212 * 213 * @param netId 214 * @return Return the return value of the netsys interface call 215 */ 216 int32_t DestroyNetworkCache(uint16_t netId); 217 218 /** 219 * Domain name resolution Obtains the domain name address 220 * 221 * @param hostName Domain name to be resolved 222 * @param serverName Server name used for query 223 * @param hints Limit parameters when querying 224 * @param netId Network id 225 * @param res return addrinfo 226 * @return Return the return value of the netsys interface call 227 */ 228 int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints, 229 uint16_t netId, std::vector<AddrInfo> &res); 230 231 /** 232 * free addrinfo 233 * 234 * @param aihead struct is addrinfo's variable 235 */ 236 void FreeAddrInfo(addrinfo *aihead); 237 238 /** 239 * Obtains the bytes of the sharing network. 240 * 241 * @return Success return 0. 242 */ 243 int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface, 244 nmd::NetworkSharingTraffic &traffic); 245 246 /** 247 * Obtains the bytes received over the cellular network. 248 * 249 * @return The number of received bytes. 250 */ 251 int64_t GetCellularRxBytes(); 252 253 /** 254 * Obtains the bytes sent over the cellular network. 255 * 256 * @return The number of sent bytes. 257 */ 258 int64_t GetCellularTxBytes(); 259 260 /** 261 * Obtains the bytes received through all NICs. 262 * 263 * @return The number of received bytes. 264 */ 265 int64_t GetAllRxBytes(); 266 267 /** 268 * Obtains the bytes sent through all NICs. 269 * 270 * @return The number of sent bytes. 271 */ 272 int64_t GetAllTxBytes(); 273 274 /** 275 * Obtains the bytes received through a specified UID. 276 * 277 * @param uid app id. 278 * @return The number of received bytes. 279 */ 280 int64_t GetUidRxBytes(uint32_t uid); 281 282 /** 283 * Obtains the bytes sent through a specified UID. 284 * 285 * @param uid app id. 286 * @return The number of sent bytes. 287 */ 288 int64_t GetUidTxBytes(uint32_t uid); 289 290 /** 291 * Obtains the bytes received through a specified UID on Iface. 292 * 293 * @param uid app id. 294 * @param iface The name of the interface. 295 * @return The number of received bytes. 296 */ 297 int64_t GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName); 298 299 /** 300 * Obtains the bytes sent through a specified UID on Iface. 301 * 302 * @param uid app id. 303 * @param iface The name of the interface. 304 * @return The number of sent bytes. 305 */ 306 int64_t GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName); 307 308 /** 309 * Obtains the bytes received through a specified NIC. 310 * 311 * @param iface The name of the interface. 312 * @return The number of received bytes. 313 */ 314 int64_t GetIfaceRxBytes(const std::string &interfaceName); 315 316 /** 317 * Obtains the bytes sent through a specified NIC. 318 * 319 * @param iface The name of the interface. 320 * @return The number of sent bytes. 321 */ 322 int64_t GetIfaceTxBytes(const std::string &interfaceName); 323 324 /** 325 * Obtains the NIC list. 326 * 327 * @return The list of interface. 328 */ 329 std::vector<std::string> InterfaceGetList(); 330 331 /** 332 * Obtains the uid list. 333 * 334 * @return The list of uid. 335 */ 336 std::vector<std::string> UidGetList(); 337 338 /** 339 * Obtains the packets received through a specified NIC. 340 * 341 * @param iface The name of the interface. 342 * @return The number of received packets. 343 */ 344 int64_t GetIfaceRxPackets(const std::string &interfaceName); 345 346 /** 347 * Obtains the packets sent through a specified NIC. 348 * 349 * @param iface The name of the interface. 350 * @return The number of sent packets. 351 */ 352 int64_t GetIfaceTxPackets(const std::string &interfaceName); 353 354 /** 355 * set default network. 356 * 357 * @return Return the return value of the netsys interface call 358 */ 359 int32_t SetDefaultNetWork(int32_t netId); 360 361 /** 362 * clear default network netId. 363 * 364 * @return Return the return value of the netsys interface call 365 */ 366 int32_t ClearDefaultNetWorkNetId(); 367 368 /** 369 * Obtains the NIC list. 370 * 371 * @param socket_fd 372 * @param netId 373 * @return Return the return value of the netsys interface call 374 */ 375 int32_t BindSocket(int32_t socket_fd, uint32_t netId); 376 377 /** 378 * Enable ip forwarding. 379 * 380 * @param requestor the requestor of forwarding 381 * @return Return the return value of the netsys interface call. 382 */ 383 int32_t IpEnableForwarding(const std::string &requestor); 384 385 /** 386 * Disable ip forwarding. 387 * 388 * @param requestor the requestor of forwarding 389 * @return Return the return value of the netsys interface call. 390 */ 391 int32_t IpDisableForwarding(const std::string &requestor); 392 393 /** 394 * Enable Nat. 395 * 396 * @param downstreamIface the name of downstream interface 397 * @param upstreamIface the name of upstream interface 398 * @return Return the return value of the netsys interface call. 399 */ 400 int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface); 401 /** 402 * Disable Nat. 403 * 404 * @param downstreamIface the name of downstream interface 405 * @param upstreamIface the name of upstream interface 406 * @return Return the return value of the netsys interface call. 407 */ 408 int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface); 409 410 /** 411 * Add interface forward. 412 * 413 * @param fromIface the name of incoming interface 414 * @param toIface the name of outcoming interface 415 * @return Return the return value of the netsys interface call. 416 */ 417 int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface); 418 419 /** 420 * Remove interface forward. 421 * 422 * @param fromIface the name of incoming interface 423 * @param toIface the name of outcoming interface 424 * @return Return the return value of the netsys interface call. 425 */ 426 int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface); 427 428 /** 429 * Set tether dns. 430 * 431 * @param netId network id 432 * @param dnsAddr the list of dns address 433 * @return Return the return value of the netsys interface call. 434 */ 435 int32_t ShareDnsSet(uint16_t netId); 436 437 /** 438 * start dns proxy listen 439 * 440 * @return success or failed 441 */ 442 int32_t StartDnsProxyListen(); 443 444 /** 445 * stop dns proxy listen 446 * 447 * @return success or failed 448 */ 449 int32_t StopDnsProxyListen(); 450 451 /** 452 * Set net callbackfuction. 453 * 454 * @param callback callbackfuction class 455 * @return Return the return value of the netsys interface call. 456 */ 457 int32_t RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback); 458 459 /** 460 * Protect tradition network to connect VPN. 461 * 462 * @param socketFd socket file description 463 * @return Return the return value of the netsys interface call. 464 */ 465 int32_t BindNetworkServiceVpn(int32_t socketFd); 466 467 /** 468 * Enable virtual network iterface card. 469 * 470 * @param socketFd socket file description 471 * @param ifRequest interface request 472 * @param ifaceFd interface file description at output paramenter 473 * @return Return the return value of the netsys interface call. 474 */ 475 int32_t EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd); 476 477 /** 478 * Set ip address. 479 * 480 * @param socketFd socket file description 481 * @param ipAddress ip address 482 * @param prefixLen the mask of ip address 483 * @param ifRequest interface request 484 * @return Return the return value of the netsys interface call. 485 */ 486 int32_t SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen, struct ifreq &ifRequest); 487 488 /** 489 * Set network blocking. 490 * 491 * @param ifaceFd interface file description 492 * @param isBlock network blocking 493 * @return Return the return value of the netsys interface call. 494 */ 495 int32_t SetBlocking(int32_t ifaceFd, bool isBlock); 496 /** 497 * Start Dhcp Client. 498 * 499 * @param iface interface file description 500 * @param bIpv6 network blocking 501 * @return success or failed 502 */ 503 int32_t StartDhcpClient(const std::string &iface, bool bIpv6); 504 /** 505 * Stop Dhcp Client. 506 * 507 * @param iface interface file description 508 * @param bIpv6 network blocking 509 * @return success or failed 510 */ 511 int32_t StopDhcpClient(const std::string &iface, bool bIpv6); 512 /** 513 * Register Notify Callback 514 * 515 * @param callback 516 * @return success or failed 517 */ 518 int32_t RegisterCallback(sptr<NetsysControllerCallback> callback); 519 520 /** 521 * start dhcpservice. 522 * 523 * @param iface interface name 524 * @param ipv4addr ipv4 addr 525 * @return Return the return value of the netsys interface call. 526 */ 527 int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr); 528 529 /** 530 * stop dhcpservice. 531 * 532 * @param iface interface name 533 * @return Return the return value of the netsys interface call. 534 */ 535 int32_t StopDhcpService(const std::string &iface); 536 537 /** 538 * Turn on data saving mode. 539 * 540 * @param enable enable or disable 541 * @return value the return value of the netsys interface call. 542 */ 543 int32_t BandwidthEnableDataSaver(bool enable); 544 545 /** 546 * Set quota. 547 * 548 * @param iface interface name 549 * @param bytes 550 * @return success or failed 551 */ 552 int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes); 553 554 /** 555 * Delete quota. 556 * 557 * @param iface interface name 558 * @return success or failed 559 */ 560 int32_t BandwidthRemoveIfaceQuota(const std::string &ifName); 561 562 /** 563 * Add DeniedList. 564 * 565 * @param uid 566 * @return success or failed 567 */ 568 int32_t BandwidthAddDeniedList(uint32_t uid); 569 570 /** 571 * Remove DeniedList. 572 * 573 * @param uid 574 * @return success or failed 575 */ 576 int32_t BandwidthRemoveDeniedList(uint32_t uid); 577 578 /** 579 * Add DeniedList. 580 * 581 * @param uid 582 * @return success or failed 583 */ 584 int32_t BandwidthAddAllowedList(uint32_t uid); 585 586 /** 587 * remove DeniedList. 588 * 589 * @param uid 590 * @return success or failed 591 */ 592 int32_t BandwidthRemoveAllowedList(uint32_t uid); 593 594 /** 595 * Set firewall rules. 596 * 597 * @param chain chain type 598 * @param isAllowedList is or not AllowedList 599 * @param uids 600 * @return Return the return value of the netsys interface call. 601 */ 602 int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids); 603 604 /** 605 * Set firewall rules. 606 * 607 * @param chain chain type 608 * @param isAllowedList is or not AllowedList 609 * @param uids 610 * @return Return the return value of the netsys interface call. 611 */ 612 int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids); 613 614 /** 615 * Enable or disable the specified firewall chain. 616 * 617 * @param chain chain type 618 * @param enable enable or disable 619 * @return success or failed 620 */ 621 int32_t FirewallEnableChain(uint32_t chain, bool enable); 622 623 /** 624 * Firewall set uid rule. 625 * 626 * @param chain chain type 627 * @param uid uid 628 * @param firewallRule firewall rule 629 * @return success or failed 630 */ 631 int32_t FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule); 632 633 private: 634 NetsysController() = default; 635 636 private: 637 bool initFlag_ = false; 638 sptr<INetsysControllerService> netsysService_; 639 }; 640 } // namespace NetManagerStandard 641 } // namespace OHOS 642 #endif // NETSYS_CONTROLLER_H 643