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 | |
| 13 | #include <string> |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 14 | |
| 15 | namespace patchpanel { |
| 16 | |
| 17 | // cros lint will yell to force using int16/int64 instead of long here, however |
| 18 | // note that unsigned long IS the correct signature for ioctl in Linux kernel - |
| 19 | // it's 32 bits on 32-bit platform and 64 bits on 64-bit one. |
| 20 | using ioctl_req_t = unsigned long; // NOLINT(runtime/int) |
| 21 | |
| 22 | // Stateless class used for holding all utility functions with side |
| 23 | // effects on the environment. Facilitates mocking these functions in unit |
| 24 | // tests. |
| 25 | class System { |
| 26 | public: |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 27 | // Enum used for restricting the possible paths that SysNetSet can write to. |
| 28 | enum SysNet { |
| 29 | // Used for modifying "net.ipv4.ip_forward" |
| 30 | IPv4Forward = 1, |
| 31 | // Used for modifying "net.ipv4.ip_local_port_range" |
| 32 | IPLocalPortRange, |
| 33 | // Used for modifying "net.ipv4.conf.%s.route_localnet", requires an |
| 34 | // interface |
| 35 | // argument |
| 36 | IPv4RouteLocalnet, |
| 37 | // Used for modifying "net.ipv6.conf.%s.accept_ra", requires an interface |
| 38 | // argument |
| 39 | IPv6AcceptRA, |
| 40 | // Used for modifying "net.ipv6.conf.all.forwarding" |
| 41 | IPv6Forward, |
| 42 | // Used for enabling netfilter connection tracking helper modules. |
| 43 | ConntrackHelper, |
| 44 | }; |
| 45 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 46 | System() = default; |
| 47 | System(const System&) = delete; |
| 48 | System& operator=(const System&) = delete; |
| 49 | virtual ~System() = default; |
| 50 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 51 | // Write |content| to a "/proc/sys/net/" path as specified by |target| |
| 52 | virtual bool SysNetSet(SysNet target, |
| 53 | const std::string& content, |
| 54 | const std::string& iface = ""); |
| 55 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 56 | virtual int Ioctl(int fd, ioctl_req_t request, const char* argp); |
| 57 | int Ioctl(int fd, ioctl_req_t request, uint64_t arg); |
| 58 | int Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr); |
| 59 | int Ioctl(int fd, ioctl_req_t request, struct rtentry* route); |
| 60 | |
Hugo Benichi | 96ee6f6 | 2021-06-28 14:07:20 +0900 | [diff] [blame] | 61 | virtual pid_t WaitPid(pid_t pid, int* wstatus, int options = 0); |
| 62 | |
Hugo Benichi | 153c711 | 2021-02-22 17:46:33 +0900 | [diff] [blame] | 63 | static bool Write(const std::string& path, const std::string& content); |
| 64 | |
Hugo Benichi | f818c78 | 2021-04-10 00:09:50 +0900 | [diff] [blame] | 65 | private: |
| 66 | }; |
| 67 | |
| 68 | } // namespace patchpanel |
| 69 | |
| 70 | #endif // PATCHPANEL_SYSTEM_H_ |