blob: b971206b3c0cce2a86cf36f50cd215505e55a5e7 [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#include "patchpanel/system.h"
6
Hugo Benichi153c7112021-02-22 17:46:33 +09007#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/types.h>
10#include <unistd.h>
11
12#include <base/files/scoped_file.h>
13
Hugo Benichif818c782021-04-10 00:09:50 +090014namespace patchpanel {
15
Hugo Benichi153c7112021-02-22 17:46:33 +090016namespace {
17
18// /proc/sys/ paths and fragments used for System::SysNetSet
19// Defines the local port range that is used by TCP and UDP traffic to choose
20// the local port (IPv4 and IPv6).
21constexpr const char kSysNetIPLocalPortRangePath[] =
22 "/proc/sys/net/ipv4/ip_local_port_range";
23// Enables/Disables IPv4 forwarding between interfaces.
24constexpr const char kSysNetIPv4ForwardingPath[] =
25 "/proc/sys/net/ipv4/ip_forward";
26// /proc/sys path for controlling connection tracking helper modules
27constexpr const char kSysNetConntrackHelperPath[] =
28 "/proc/sys/net/netfilter/nf_conntrack_helper";
29// Prefix for IPv4 interface configuration.
30constexpr const char kSysNetIPv4ConfPrefix[] = "/proc/sys/net/ipv4/conf/";
31// Suffix for allowing localhost as a source or destination when routing IPv4.
32constexpr const char kSysNetIPv4RouteLocalnetSuffix[] = "/route_localnet";
33// Enables/Disables IPv6 forwarding between interfaces.
34constexpr const char kSysNetIPv6ForwardingPath[] =
35 "/proc/sys/net/ipv6/conf/all/forwarding";
36// Prefix for IPv6 interface configuration.
37constexpr const char kSysNetIPv6ConfPrefix[] = "/proc/sys/net/ipv6/conf/";
38// Suffix for accepting Router Advertisements on an interface and
39// autoconfiguring it with IPv6 parameters.
40constexpr const char kSysNetIPv6AcceptRaSuffix[] = "/accept_ra";
41
42} // namespace
43
Hugo Benichif818c782021-04-10 00:09:50 +090044int System::Ioctl(int fd, ioctl_req_t request, const char* argp) {
45 return ioctl(fd, request, argp);
46}
47
48int System::Ioctl(int fd, ioctl_req_t request, uint64_t arg) {
49 return Ioctl(fd, request, reinterpret_cast<const char*>(arg));
50}
51
52int System::Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr) {
53 return Ioctl(fd, request, reinterpret_cast<const char*>(ifr));
54}
55
56int System::Ioctl(int fd, ioctl_req_t request, struct rtentry* route) {
57 return Ioctl(fd, request, reinterpret_cast<const char*>(route));
58}
59
Hugo Benichi153c7112021-02-22 17:46:33 +090060bool System::SysNetSet(SysNet target,
61 const std::string& content,
62 const std::string& iface) {
63 std::string path;
64 switch (target) {
65 case SysNet::IPv4Forward:
66 return Write(kSysNetIPv4ForwardingPath, content);
67 case SysNet::IPLocalPortRange:
68 return Write(kSysNetIPLocalPortRangePath, content);
69 case SysNet::IPv4RouteLocalnet:
70 if (iface.empty()) {
71 LOG(ERROR) << "IPv4LocalPortRange requires a valid interface";
72 return false;
73 }
74 return Write(
75 kSysNetIPv4ConfPrefix + iface + kSysNetIPv4RouteLocalnetSuffix,
76 content);
77 case SysNet::IPv6Forward:
78 return Write(kSysNetIPv6ForwardingPath, content);
79 case SysNet::IPv6AcceptRA:
80 if (iface.empty()) {
81 LOG(ERROR) << "IPv6AcceptRA requires a valid interface";
82 return false;
83 }
84 return Write(kSysNetIPv6ConfPrefix + iface + kSysNetIPv6AcceptRaSuffix,
85 content);
86 case ConntrackHelper:
87 return Write(kSysNetConntrackHelperPath, content);
88 default:
89 LOG(ERROR) << "Unknown SysNet value " << target;
90 return false;
91 }
92}
93
94bool System::Write(const std::string& path, const std::string& content) {
95 base::ScopedFD fd(open(path.c_str(), O_WRONLY | O_TRUNC | O_CLOEXEC));
96 if (!fd.is_valid()) {
97 PLOG(ERROR) << "Failed to open " << path;
98 return false;
99 }
100
101 if (write(fd.get(), content.c_str(), content.size()) != content.size()) {
102 PLOG(ERROR) << "Failed to write \"" << content << "\" to " << path;
103 return false;
104 }
105
106 return true;
107}
108
Hugo Benichif818c782021-04-10 00:09:50 +0900109} // namespace patchpanel