FirewallController.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2012 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 _FIREWALL_CONTROLLER_H
  17. #define _FIREWALL_CONTROLLER_H
  18. #include <sys/types.h>
  19. #include <mutex>
  20. #include <set>
  21. #include <string>
  22. #include <vector>
  23. #include "android/net/INetd.h"
  24. #include "NetdConstants.h"
  25. #include "bpf/BpfUtils.h"
  26. namespace android {
  27. namespace net {
  28. enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY };
  29. // WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
  30. // BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
  31. enum FirewallType { WHITELIST = INetd::FIREWALL_WHITELIST, BLACKLIST = INetd::FIREWALL_BLACKLIST };
  32. enum ChildChain {
  33. NONE = INetd::FIREWALL_CHAIN_NONE,
  34. DOZABLE = INetd::FIREWALL_CHAIN_DOZABLE,
  35. STANDBY = INetd::FIREWALL_CHAIN_STANDBY,
  36. POWERSAVE = INetd::FIREWALL_CHAIN_POWERSAVE,
  37. INVALID_CHAIN
  38. };
  39. /*
  40. * Simple firewall that drops all packets except those matching explicitly
  41. * defined ALLOW rules.
  42. *
  43. * Methods in this class must be called when holding a write lock on |lock|, and may not call
  44. * any other controller without explicitly managing that controller's lock. There are currently
  45. * no such methods.
  46. */
  47. class FirewallController {
  48. public:
  49. FirewallController();
  50. int setupIptablesHooks(void);
  51. int setFirewallType(FirewallType);
  52. int resetFirewall(void);
  53. int isFirewallEnabled(void);
  54. /* Match traffic going in/out over the given iface. */
  55. int setInterfaceRule(const char*, FirewallRule);
  56. /* Match traffic owned by given UID. This is specific to a particular chain. */
  57. int setUidRule(ChildChain, int, FirewallRule);
  58. int enableChildChains(ChildChain, bool);
  59. int replaceUidChain(const std::string&, bool, const std::vector<int32_t>&);
  60. static std::string makeCriticalCommands(IptablesTarget target, const char* chainName);
  61. static uid_t discoverMaximumValidUid(const std::string& fileName);
  62. static const char* TABLE;
  63. static const char* LOCAL_INPUT;
  64. static const char* LOCAL_OUTPUT;
  65. static const char* LOCAL_FORWARD;
  66. static const char* LOCAL_DOZABLE;
  67. static const char* LOCAL_STANDBY;
  68. static const char* LOCAL_POWERSAVE;
  69. static const char* ICMPV6_TYPES[];
  70. std::mutex lock;
  71. protected:
  72. friend class FirewallControllerTest;
  73. std::string makeUidRules(IptablesTarget target, const char *name, bool isWhitelist,
  74. const std::vector<int32_t>& uids);
  75. static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
  76. private:
  77. // Netd supports two cases, in both of which mMaxUid that derives from the uid mapping is const:
  78. // - netd runs in a root namespace which contains all UIDs.
  79. // - netd runs in a user namespace where the uid mapping is written once before netd starts.
  80. // In that case, an attempt to write more than once to a uid_map file in a user namespace
  81. // fails with EPERM. Netd can therefore assumes the max valid uid to be const.
  82. const uid_t mMaxUid;
  83. FirewallType mFirewallType;
  84. android::bpf::BpfLevel mUseBpfOwnerMatch;
  85. std::set<std::string> mIfaceRules;
  86. int attachChain(const char*, const char*);
  87. int detachChain(const char*, const char*);
  88. int createChain(const char*, FirewallType);
  89. FirewallType getFirewallType(ChildChain);
  90. };
  91. } // namespace net
  92. } // namespace android
  93. #endif