blob: 0fe7cdc31b54c152416a938c5f052e2eda7cc3b7 [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
Hugo Benichi03be8962022-03-17 13:16:38 +090013#include <map>
Hugo Benichi96ee6f62021-06-28 14:07:20 +090014#include <string>
Hugo Benichif818c782021-04-10 00:09:50 +090015
16namespace 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.
21using ioctl_req_t = unsigned long; // NOLINT(runtime/int)
22
Hugo Benichi03be8962022-03-17 13:16:38 +090023// 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 Benichif818c782021-04-10 00:09:50 +090027class System {
28 public:
Hugo Benichi153c7112021-02-22 17:46:33 +090029 // 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 Imana183d7a2021-08-06 01:35:40 +090046 // Used for modifying "net.ipv6.conf.all.disable_ipv6"
47 IPv6Disable,
Hugo Benichi153c7112021-02-22 17:46:33 +090048 };
49
Hugo Benichif818c782021-04-10 00:09:50 +090050 System() = default;
51 System(const System&) = delete;
52 System& operator=(const System&) = delete;
53 virtual ~System() = default;
54
Hugo Benichi153c7112021-02-22 17:46:33 +090055 // 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 Benichif818c782021-04-10 00:09:50 +090060 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 Benichi96ee6f62021-06-28 14:07:20 +090065 virtual pid_t WaitPid(pid_t pid, int* wstatus, int options = 0);
66
Hugo Benichi03be8962022-03-17 13:16:38 +090067 // 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 Benichi153c7112021-02-22 17:46:33 +090073 static bool Write(const std::string& path, const std::string& content);
74
Hugo Benichif818c782021-04-10 00:09:50 +090075 private:
Hugo Benichi03be8962022-03-17 13:16:38 +090076 // 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 Benichif818c782021-04-10 00:09:50 +090084};
85
86} // namespace patchpanel
87
88#endif // PATCHPANEL_SYSTEM_H_