RouteController.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef NETD_SERVER_ROUTE_CONTROLLER_H
  17. #define NETD_SERVER_ROUTE_CONTROLLER_H
  18. #include "NetdConstants.h"
  19. #include "Permission.h"
  20. #include <android-base/thread_annotations.h>
  21. #include <linux/netlink.h>
  22. #include <sys/types.h>
  23. #include <map>
  24. #include <mutex>
  25. namespace android {
  26. namespace net {
  27. class UidRanges;
  28. class RouteController {
  29. public:
  30. // How the routing table number is determined for route modification requests.
  31. enum TableType {
  32. INTERFACE, // Compute the table number based on the interface index.
  33. LOCAL_NETWORK, // A fixed table used for routes to directly-connected clients/peers.
  34. LEGACY_NETWORK, // Use a fixed table that's used to override the default network.
  35. LEGACY_SYSTEM, // A fixed table, only modifiable by system apps; overrides VPNs too.
  36. };
  37. static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000;
  38. static const char* const LOCAL_MANGLE_INPUT;
  39. static int Init(unsigned localNetId) WARN_UNUSED_RESULT;
  40. // Returns an ifindex given the interface name, by looking up in sInterfaceToTable.
  41. // This is currently only used by NetworkController::addInterfaceToNetwork
  42. // and should probabaly be changed to passing the ifindex into RouteController instead.
  43. // We do this instead of calling if_nametoindex because the same interface name can
  44. // correspond to different interface indices over time. This way, even if the interface
  45. // index has changed, we can still free any map entries indexed by the ifindex that was
  46. // used to add them.
  47. static uint32_t getIfIndex(const char* interface) EXCLUDES(sInterfaceToTableLock);
  48. static int addInterfaceToLocalNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
  49. static int removeInterfaceFromLocalNetwork(unsigned netId,
  50. const char* interface) WARN_UNUSED_RESULT;
  51. static int addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
  52. Permission permission) WARN_UNUSED_RESULT;
  53. static int removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
  54. Permission permission) WARN_UNUSED_RESULT;
  55. static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure,
  56. const UidRanges& uidRanges) WARN_UNUSED_RESULT;
  57. static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
  58. const UidRanges& uidRanges) WARN_UNUSED_RESULT;
  59. static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
  60. Permission oldPermission,
  61. Permission newPermission) WARN_UNUSED_RESULT;
  62. static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
  63. const UidRanges& uidRanges) WARN_UNUSED_RESULT;
  64. static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
  65. const UidRanges& uidRanges) WARN_UNUSED_RESULT;
  66. static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges)
  67. WARN_UNUSED_RESULT;
  68. static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges)
  69. WARN_UNUSED_RESULT;
  70. static int addInterfaceToDefaultNetwork(const char* interface,
  71. Permission permission) WARN_UNUSED_RESULT;
  72. static int removeInterfaceFromDefaultNetwork(const char* interface,
  73. Permission permission) WARN_UNUSED_RESULT;
  74. // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
  75. // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
  76. static int addRoute(const char* interface, const char* destination, const char* nexthop,
  77. TableType tableType) WARN_UNUSED_RESULT;
  78. static int removeRoute(const char* interface, const char* destination, const char* nexthop,
  79. TableType tableType) WARN_UNUSED_RESULT;
  80. static int enableTethering(const char* inputInterface,
  81. const char* outputInterface) WARN_UNUSED_RESULT;
  82. static int disableTethering(const char* inputInterface,
  83. const char* outputInterface) WARN_UNUSED_RESULT;
  84. static int addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
  85. Permission permission) WARN_UNUSED_RESULT;
  86. static int removeVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
  87. Permission permission) WARN_UNUSED_RESULT;
  88. // For testing.
  89. static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&,
  90. const std::string&, std::string *);
  91. private:
  92. friend class RouteControllerTest;
  93. static std::mutex sInterfaceToTableLock;
  94. static std::map<std::string, uint32_t> sInterfaceToTable GUARDED_BY(sInterfaceToTableLock);
  95. static int configureDummyNetwork();
  96. static int flushRoutes(const char* interface) EXCLUDES(sInterfaceToTableLock);
  97. static int flushRoutes(uint32_t table);
  98. static uint32_t getRouteTableForInterfaceLocked(const char *interface)
  99. REQUIRES(sInterfaceToTableLock);
  100. static uint32_t getRouteTableForInterface(const char *interface) EXCLUDES(sInterfaceToTableLock);
  101. static int modifyDefaultNetwork(uint16_t action, const char* interface, Permission permission);
  102. static int modifyPhysicalNetwork(unsigned netId, const char* interface, Permission permission,
  103. bool add);
  104. static int modifyRoute(uint16_t action, const char* interface, const char* destination,
  105. const char* nexthop, TableType tableType);
  106. static int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
  107. const char* outputInterface);
  108. static int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
  109. const char* physicalInterface, Permission permission);
  110. static int modifyVirtualNetwork(unsigned netId, const char* interface,
  111. const UidRanges& uidRanges, bool secure, bool add,
  112. bool modifyNonUidBasedRules);
  113. static void updateTableNamesFile() EXCLUDES(sInterfaceToTableLock);
  114. };
  115. // Public because they are called by by RouteControllerTest.cpp.
  116. // TODO: come up with a scheme of unit testing this code that does not rely on making all its
  117. // functions public.
  118. int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
  119. const char* nexthop) WARN_UNUSED_RESULT;
  120. int flushRoutes(uint32_t table) WARN_UNUSED_RESULT;
  121. uint32_t getRulePriority(const nlmsghdr *nlh);
  122. WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
  123. Permission permission, bool add);
  124. } // namespace net
  125. } // namespace android
  126. #endif // NETD_SERVER_ROUTE_CONTROLLER_H