Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 1 | // Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "patchpanel/system.h" |
| 6 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 7 | #include <fcntl.h> |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame] | 8 | #include <net/if.h> |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 9 | #include <sys/stat.h> |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 10 | #include <sys/wait.h> |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
| 13 | #include <base/files/scoped_file.h> |
Qijiang Fan | 80174b7 | 2021-08-13 12:48:38 +0900 | [diff] [blame] | 14 | #include <base/logging.h> |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 15 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 16 | namespace patchpanel { |
| 17 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // /proc/sys/ paths and fragments used for System::SysNetSet |
| 21 | // Defines the local port range that is used by TCP and UDP traffic to choose |
| 22 | // the local port (IPv4 and IPv6). |
| 23 | constexpr const char kSysNetIPLocalPortRangePath[] = |
| 24 | "/proc/sys/net/ipv4/ip_local_port_range"; |
| 25 | // Enables/Disables IPv4 forwarding between interfaces. |
| 26 | constexpr const char kSysNetIPv4ForwardingPath[] = |
| 27 | "/proc/sys/net/ipv4/ip_forward"; |
| 28 | // /proc/sys path for controlling connection tracking helper modules |
| 29 | constexpr const char kSysNetConntrackHelperPath[] = |
| 30 | "/proc/sys/net/netfilter/nf_conntrack_helper"; |
Jason Jeremy Iman | a183d7a | 2021-08-06 01:35:40 +0900 | [diff] [blame] | 31 | // Enables/Disables IPv6. |
| 32 | constexpr const char kSysNetDisableIPv6Path[] = |
| 33 | "/proc/sys/net/ipv6/conf/all/disable_ipv6"; |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 34 | // Prefix for IPv4 interface configuration. |
| 35 | constexpr const char kSysNetIPv4ConfPrefix[] = "/proc/sys/net/ipv4/conf/"; |
| 36 | // Suffix for allowing localhost as a source or destination when routing IPv4. |
| 37 | constexpr const char kSysNetIPv4RouteLocalnetSuffix[] = "/route_localnet"; |
| 38 | // Enables/Disables IPv6 forwarding between interfaces. |
| 39 | constexpr const char kSysNetIPv6ForwardingPath[] = |
| 40 | "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 41 | // Prefix for IPv6 interface configuration. |
| 42 | constexpr const char kSysNetIPv6ConfPrefix[] = "/proc/sys/net/ipv6/conf/"; |
| 43 | // Suffix for accepting Router Advertisements on an interface and |
| 44 | // autoconfiguring it with IPv6 parameters. |
| 45 | constexpr const char kSysNetIPv6AcceptRaSuffix[] = "/accept_ra"; |
| 46 | |
| 47 | } // namespace |
| 48 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 49 | int System::Ioctl(int fd, ioctl_req_t request, const char* argp) { |
| 50 | return ioctl(fd, request, argp); |
| 51 | } |
| 52 | |
| 53 | int System::Ioctl(int fd, ioctl_req_t request, uint64_t arg) { |
| 54 | return Ioctl(fd, request, reinterpret_cast<const char*>(arg)); |
| 55 | } |
| 56 | |
| 57 | int System::Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr) { |
| 58 | return Ioctl(fd, request, reinterpret_cast<const char*>(ifr)); |
| 59 | } |
| 60 | |
| 61 | int System::Ioctl(int fd, ioctl_req_t request, struct rtentry* route) { |
| 62 | return Ioctl(fd, request, reinterpret_cast<const char*>(route)); |
| 63 | } |
| 64 | |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 65 | pid_t System::WaitPid(pid_t pid, int* wstatus, int options) { |
| 66 | return waitpid(pid, wstatus, options); |
| 67 | } |
| 68 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 69 | bool System::SysNetSet(SysNet target, |
| 70 | const std::string& content, |
| 71 | const std::string& iface) { |
| 72 | std::string path; |
| 73 | switch (target) { |
| 74 | case SysNet::IPv4Forward: |
| 75 | return Write(kSysNetIPv4ForwardingPath, content); |
| 76 | case SysNet::IPLocalPortRange: |
| 77 | return Write(kSysNetIPLocalPortRangePath, content); |
| 78 | case SysNet::IPv4RouteLocalnet: |
| 79 | if (iface.empty()) { |
| 80 | LOG(ERROR) << "IPv4LocalPortRange requires a valid interface"; |
| 81 | return false; |
| 82 | } |
| 83 | return Write( |
| 84 | kSysNetIPv4ConfPrefix + iface + kSysNetIPv4RouteLocalnetSuffix, |
| 85 | content); |
| 86 | case SysNet::IPv6Forward: |
| 87 | return Write(kSysNetIPv6ForwardingPath, content); |
| 88 | case SysNet::IPv6AcceptRA: |
| 89 | if (iface.empty()) { |
| 90 | LOG(ERROR) << "IPv6AcceptRA requires a valid interface"; |
| 91 | return false; |
| 92 | } |
| 93 | return Write(kSysNetIPv6ConfPrefix + iface + kSysNetIPv6AcceptRaSuffix, |
| 94 | content); |
| 95 | case ConntrackHelper: |
| 96 | return Write(kSysNetConntrackHelperPath, content); |
Jason Jeremy Iman | a183d7a | 2021-08-06 01:35:40 +0900 | [diff] [blame] | 97 | case SysNet::IPv6Disable: |
| 98 | return Write(kSysNetDisableIPv6Path, content); |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 99 | default: |
| 100 | LOG(ERROR) << "Unknown SysNet value " << target; |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame] | 105 | std::string System::IfIndextoname(int ifindex) { |
| 106 | char ifname[IFNAMSIZ]; |
| 107 | if (if_indextoname(ifindex, ifname) == nullptr) { |
| 108 | return ""; |
| 109 | } |
| 110 | return ifname; |
| 111 | } |
| 112 | |
| 113 | uint32_t System::IfNametoindex(const std::string& ifname) { |
| 114 | uint32_t ifindex = if_nametoindex(ifname.c_str()); |
| 115 | if (ifindex > 0) { |
| 116 | if_nametoindex_[ifname] = ifindex; |
| 117 | return ifindex; |
| 118 | } |
| 119 | |
| 120 | const auto it = if_nametoindex_.find(ifname); |
| 121 | if (it != if_nametoindex_.end()) |
| 122 | return it->second; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | // static |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 128 | bool System::Write(const std::string& path, const std::string& content) { |
| 129 | base::ScopedFD fd(open(path.c_str(), O_WRONLY | O_TRUNC | O_CLOEXEC)); |
| 130 | if (!fd.is_valid()) { |
| 131 | PLOG(ERROR) << "Failed to open " << path; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if (write(fd.get(), content.c_str(), content.size()) != content.size()) { |
| 136 | PLOG(ERROR) << "Failed to write \"" << content << "\" to " << path; |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 143 | } // namespace patchpanel |