blob: 695123951c8bafa4a063082db636698c2f4c7848 [file] [log] [blame]
Robert Greenwaltc4621772012-01-31 12:46:45 -08001/*
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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090017#pragma once
Robert Greenwaltc4621772012-01-31 12:46:45 -080018
Erik Klinecc4f2732016-08-03 11:24:27 +090019#include <ifaddrs.h>
20#include <netdb.h>
Bernie Innocentia0381392018-08-15 04:33:45 +090021#include <stddef.h>
22#include <stdint.h>
Lorenzo Colitti699aa992016-04-15 10:22:37 +090023
Bernie Innocentia0381392018-08-15 04:33:45 +090024#include <mutex>
25#include <string>
Lorenzo Colitti699aa992016-04-15 10:22:37 +090026
waynema334e3662021-12-01 16:04:27 +080027#include "android/net/INetd.h"
28
Chenbo Feng48eaed32018-12-26 17:40:21 -080029#include <netdutils/UidConstants.h>
Felipe Leme5ebbbd82016-03-07 09:25:50 -080030#include <private/android_filesystem_config.h>
Robert Greenwaltc4621772012-01-31 12:46:45 -080031
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070032enum IptablesTarget { V4, V6, V4V6 };
33
Lorenzo Colitti89faa342016-02-26 11:38:47 +090034int execIptablesRestore(IptablesTarget target, const std::string& commands);
Lorenzo Colitticd283772017-01-31 19:00:49 +090035int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
36 std::string *output);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
38 const std::string& command, std::string *output);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090039bool isIfaceName(const std::string& name);
Lorenzo Colittiba25df92014-06-18 00:22:17 +090040int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
Lorenzo Colitti839d7d62017-04-03 15:37:19 +090041void blockSigpipe();
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090042void setCloseOnExec(const char *sock);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070043
WeiZhang4f3dffb2021-11-30 00:32:46 -060044void stopProcess(int pid, const char* processName);
45
Mike Yu5ae61542018-10-19 22:11:43 +080046// TODO: use std::size() instead.
Rom Lemarchand001f0a42013-01-31 12:41:03 -080047#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
48
Lorenzo Colittia10ac322014-04-11 18:26:17 +090049#define __INT_STRLEN(i) sizeof(#i)
50#define _INT_STRLEN(i) __INT_STRLEN(i)
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +090051#define INT32_STRLEN _INT_STRLEN(INT32_MIN)
Lorenzo Colittia10ac322014-04-11 18:26:17 +090052#define UINT32_STRLEN _INT_STRLEN(UINT32_MAX)
53#define UINT32_HEX_STRLEN sizeof("0x12345678")
Benedict Wongb9baf262017-12-03 15:43:08 -080054#define IPSEC_IFACE_PREFIX "ipsec"
Lorenzo Colittia10ac322014-04-11 18:26:17 +090055
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070056const uid_t INVALID_UID = static_cast<uid_t>(-1);
57
Chenbo Fengf5663d82018-11-08 16:10:48 -080058constexpr char TCP_RMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_rmem";
59constexpr char TCP_WMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_wmem";
Erik Klinecc4f2732016-08-03 11:24:27 +090060
Erik Klinecc4f2732016-08-03 11:24:27 +090061struct IfaddrsDeleter {
62 void operator()(struct ifaddrs *p) const {
63 if (p != nullptr) {
64 freeifaddrs(p);
65 }
66 }
67};
68
69typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs;
70
Bernie Innocenti762dcf42019-06-14 19:52:49 +090071namespace android::net {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090072
73/**
74 * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads)
75 * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
76 * CommandListener has only one user (NetworkManagementService), which is connected through a
77 * FrameworkListener that passes in commands one at a time.
78 */
Luke Huangd1ee4622018-06-29 13:49:58 +080079extern std::mutex gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090080
waynema334e3662021-12-01 16:04:27 +080081enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY };
82
83// ALLOWLIST means the firewall denies all by default, uids must be explicitly ALLOWed
84// DENYLIST means the firewall allows all by default, uids must be explicitly DENYed
85
86enum FirewallType { ALLOWLIST = INetd::FIREWALL_ALLOWLIST, DENYLIST = INetd::FIREWALL_DENYLIST };
87
88enum ChildChain {
89 NONE = INetd::FIREWALL_CHAIN_NONE,
90 DOZABLE = INetd::FIREWALL_CHAIN_DOZABLE,
91 STANDBY = INetd::FIREWALL_CHAIN_STANDBY,
92 POWERSAVE = INetd::FIREWALL_CHAIN_POWERSAVE,
93 RESTRICTED = INetd::FIREWALL_CHAIN_RESTRICTED,
94 INVALID_CHAIN
95};
96
Bernie Innocenti762dcf42019-06-14 19:52:49 +090097} // namespace android::net