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