blob: a517f7c3ca2c2abf9fa085c74e223a9c71544daa [file] [log] [blame]
Hugo Benichif818c782021-04-10 00:09:50 +09001// 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 Benichi96ee6f62021-06-28 14:07:20 +090011#include <sys/types.h>
12
13#include <string>
Hugo Benichif818c782021-04-10 00:09:50 +090014
15namespace 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.
20using 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.
25class System {
26 public:
Hugo Benichi153c7112021-02-22 17:46:33 +090027 // 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,
Jason Jeremy Imana183d7a2021-08-06 01:35:40 +090044 // Used for modifying "net.ipv6.conf.all.disable_ipv6"
45 IPv6Disable,
Hugo Benichi153c7112021-02-22 17:46:33 +090046 };
47
Hugo Benichif818c782021-04-10 00:09:50 +090048 System() = default;
49 System(const System&) = delete;
50 System& operator=(const System&) = delete;
51 virtual ~System() = default;
52
Hugo Benichi153c7112021-02-22 17:46:33 +090053 // Write |content| to a "/proc/sys/net/" path as specified by |target|
54 virtual bool SysNetSet(SysNet target,
55 const std::string& content,
56 const std::string& iface = "");
57
Hugo Benichif818c782021-04-10 00:09:50 +090058 virtual int Ioctl(int fd, ioctl_req_t request, const char* argp);
59 int Ioctl(int fd, ioctl_req_t request, uint64_t arg);
60 int Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr);
61 int Ioctl(int fd, ioctl_req_t request, struct rtentry* route);
62
Hugo Benichi96ee6f62021-06-28 14:07:20 +090063 virtual pid_t WaitPid(pid_t pid, int* wstatus, int options = 0);
64
Hugo Benichi153c7112021-02-22 17:46:33 +090065 static bool Write(const std::string& path, const std::string& content);
66
Hugo Benichif818c782021-04-10 00:09:50 +090067 private:
68};
69
70} // namespace patchpanel
71
72#endif // PATCHPANEL_SYSTEM_H_