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 | #ifndef PATCHPANEL_SYSTEM_H_ |
| 6 | #define PATCHPANEL_SYSTEM_H_ |
| 7 | |
| 8 | #include <net/if.h> |
| 9 | #include <net/route.h> |
| 10 | #include <sys/ioctl.h> |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame^] | 13 | #include <map> |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 14 | #include <string> |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 15 | |
| 16 | namespace patchpanel { |
| 17 | |
| 18 | // cros lint will yell to force using int16/int64 instead of long here, however |
| 19 | // note that unsigned long IS the correct signature for ioctl in Linux kernel - |
| 20 | // it's 32 bits on 32-bit platform and 64 bits on 64-bit one. |
| 21 | using ioctl_req_t = unsigned long; // NOLINT(runtime/int) |
| 22 | |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame^] | 23 | // Class used for: |
| 24 | // - holding all utility functions with side effects on the environment. |
| 25 | // - wrapping commonly used system calls. |
| 26 | // This class facilitates mocking these functions in unit tests. |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 27 | class System { |
| 28 | public: |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 29 | // Enum used for restricting the possible paths that SysNetSet can write to. |
| 30 | enum SysNet { |
| 31 | // Used for modifying "net.ipv4.ip_forward" |
| 32 | IPv4Forward = 1, |
| 33 | // Used for modifying "net.ipv4.ip_local_port_range" |
| 34 | IPLocalPortRange, |
| 35 | // Used for modifying "net.ipv4.conf.%s.route_localnet", requires an |
| 36 | // interface |
| 37 | // argument |
| 38 | IPv4RouteLocalnet, |
| 39 | // Used for modifying "net.ipv6.conf.%s.accept_ra", requires an interface |
| 40 | // argument |
| 41 | IPv6AcceptRA, |
| 42 | // Used for modifying "net.ipv6.conf.all.forwarding" |
| 43 | IPv6Forward, |
| 44 | // Used for enabling netfilter connection tracking helper modules. |
| 45 | ConntrackHelper, |
Jason Jeremy Iman | a183d7a | 2021-08-06 01:35:40 +0900 | [diff] [blame] | 46 | // Used for modifying "net.ipv6.conf.all.disable_ipv6" |
| 47 | IPv6Disable, |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 50 | System() = default; |
| 51 | System(const System&) = delete; |
| 52 | System& operator=(const System&) = delete; |
| 53 | virtual ~System() = default; |
| 54 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 55 | // Write |content| to a "/proc/sys/net/" path as specified by |target| |
| 56 | virtual bool SysNetSet(SysNet target, |
| 57 | const std::string& content, |
| 58 | const std::string& iface = ""); |
| 59 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 60 | virtual int Ioctl(int fd, ioctl_req_t request, const char* argp); |
| 61 | int Ioctl(int fd, ioctl_req_t request, uint64_t arg); |
| 62 | int Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr); |
| 63 | int Ioctl(int fd, ioctl_req_t request, struct rtentry* route); |
| 64 | |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 65 | virtual pid_t WaitPid(pid_t pid, int* wstatus, int options = 0); |
| 66 | |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame^] | 67 | // Return the interface name of the network interface with index |ifindex|, or |
| 68 | // empty string if it fails. |
| 69 | virtual std::string IfIndextoname(int ifindex); |
| 70 | // Return the index of the interface with name |ifname|, or 0 if it fails. |
| 71 | virtual uint32_t IfNametoindex(const std::string& ifname); |
| 72 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 73 | static bool Write(const std::string& path, const std::string& content); |
| 74 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 75 | private: |
Hugo Benichi | 03be896 | 2022-03-17 13:16:38 +0900 | [diff] [blame^] | 76 | // A map used for remembering the interface index of an interface. This |
| 77 | // information is necessary when cleaning up the state of various subsystems |
| 78 | // in patchpanel that directly references the interface index. However after |
| 79 | // receiving the interface removal event (RTM_DELLINK event or shill DBus |
| 80 | // event), the interface index cannot be retrieved anymore. A new entry is |
| 81 | // only added when a new upstream network interface appears, and entries are |
| 82 | // not removed. |
| 83 | std::map<std::string, int> if_nametoindex_; |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace patchpanel |
| 87 | |
| 88 | #endif // PATCHPANEL_SYSTEM_H_ |