blob: 011aedb09723aa6dd948f6de6033538e87c9df1b [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
Hugo Benichi153c7112021-02-22 17:46:33 +09008#include <string>
9
Hugo Benichif818c782021-04-10 00:09:50 +090010#include <net/if.h>
11#include <net/route.h>
12#include <sys/ioctl.h>
13
14namespace 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.
19using 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.
24class System {
25 public:
Hugo Benichi153c7112021-02-22 17:46:33 +090026 // 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 Benichif818c782021-04-10 00:09:50 +090045 System() = default;
46 System(const System&) = delete;
47 System& operator=(const System&) = delete;
48 virtual ~System() = default;
49
Hugo Benichi153c7112021-02-22 17:46:33 +090050 // 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 Benichif818c782021-04-10 00:09:50 +090055 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 Benichi153c7112021-02-22 17:46:33 +090060 static bool Write(const std::string& path, const std::string& content);
61
Hugo Benichif818c782021-04-10 00:09:50 +090062 private:
63};
64
65} // namespace patchpanel
66
67#endif // PATCHPANEL_SYSTEM_H_