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