Skip to content

Commit 1f8b5b9

Browse files
pseusyspseusys
andauthored
Automated forwarding reset (#27)
* automated forwarding reset * newline added --------- Co-authored-by: pseusys <aleksandr.sergeev.ad@gmail.com>
1 parent e07ad2e commit 1f8b5b9

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

caerulean/whirlpool/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ Before whirlpool node can be run, the following configuration steps have to be m
9292

9393
- Disable IPv6 router solicitation (for tunnel interface):
9494
`echo 0 > /proc/sys/net/ipv6/conf/default/accept_ra`
95-
- Enable IP packet forwarding:
96-
`echo 1 > /proc/sys/net/ipv4/ip_forward`
9795

9896
### Local whirlpool execution
9997

caerulean/whirlpool/tunnel/structure.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"fmt"
55
"main/utils"
66
"net"
7+
"os"
8+
"strconv"
9+
"strings"
710
"sync"
811

912
"github.com/google/nftables"
@@ -26,6 +29,10 @@ const (
2629
DEFAULT_TYPHOON_PORT = 29384
2730

2831
REQUIRED_TUNNEL_NETWORK_BITS = 16
32+
33+
FORWARDING_ENABLED_VALUE = 1
34+
DEFAULT_FILE_PERMISSIONS = 0644
35+
IPV4_FORWARDING_FILE = "/proc/sys/net/ipv4/ip_forward"
2936
)
3037

3138
// Tunnel config object, represents tunnel interface and forwarding setup.
@@ -54,6 +61,34 @@ type TunnelConfig struct {
5461

5562
// Tunnel name.
5663
name string
64+
65+
// Forwarding value.
66+
forwardingIPv4 uint8
67+
}
68+
69+
func readSysctlInt(path string) (uint8, error) {
70+
data, err := os.ReadFile(path)
71+
if err != nil {
72+
return 0, fmt.Errorf("failed to read file at %s: %v", path, err)
73+
}
74+
75+
valueStr := strings.TrimSpace(string(data))
76+
value, err := strconv.Atoi(valueStr)
77+
if err != nil {
78+
return 0, fmt.Errorf("failed to parse %s from %s: %v", valueStr, path, err)
79+
}
80+
81+
return uint8(value), nil
82+
}
83+
84+
func writeSysctlInt(path string, value uint8) error {
85+
data := []byte(fmt.Sprintf("%d\n", value))
86+
err := os.WriteFile(path, data, DEFAULT_FILE_PERMISSIONS)
87+
if err != nil {
88+
return fmt.Errorf("failed to write to file at %s: %v", path, err)
89+
}
90+
91+
return nil
5792
}
5893

5994
// Preserve current iptables configuration in a TunnelConfig object.
@@ -66,10 +101,16 @@ func Preserve() (*TunnelConfig, error) {
66101
mtu := int32(utils.GetIntEnv("SEASIDE_TUNNEL_MTU", DEFAULT_TUNNEL_MTU, 32))
67102
name := utils.GetEnv("SEASIDE_TUNNEL_NAME", DEFAULT_TUNNEL_NAME)
68103

104+
forwardingIPv4, err := readSysctlInt(IPV4_FORWARDING_FILE)
105+
if err != nil {
106+
return nil, fmt.Errorf("error reading system IPv4 forwarding property: %v", err)
107+
}
108+
69109
conf := TunnelConfig{
70-
Default: defaultNet,
71-
mtu: mtu,
72-
name: name,
110+
Default: defaultNet,
111+
mtu: mtu,
112+
name: name,
113+
forwardingIPv4: forwardingIPv4,
73114
}
74115

75116
return &conf, nil
@@ -83,6 +124,12 @@ func (conf *TunnelConfig) Open() (err error) {
83124
conf.mutex.Lock()
84125
defer conf.mutex.Unlock()
85126

127+
// Enable IPv4 packet forwarding
128+
err = writeSysctlInt(IPV4_FORWARDING_FILE, FORWARDING_ENABLED_VALUE)
129+
if err != nil {
130+
return fmt.Errorf("error enabling IPv4 forwarding: %v", err)
131+
}
132+
86133
// Parse IPs and control port number from environment variables
87134
intIP := utils.GetEnv("SEASIDE_ADDRESS", conf.Default.IP.String())
88135
extIP := utils.GetEnv("SEASIDE_EXTERNAL", intIP)
@@ -148,4 +195,9 @@ func (conf *TunnelConfig) Close() {
148195
if err != nil {
149196
logrus.Errorf("Error removing tunnel: %v", err)
150197
}
198+
199+
err = writeSysctlInt(IPV4_FORWARDING_FILE, conf.forwardingIPv4)
200+
if err != nil {
201+
logrus.Errorf("error restoring IPv4 forwarding: %v", err)
202+
}
151203
}

viridian/algae/setup/whirlpool.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
_GO_DISTRIBUTION = "https://go.dev/dl/go{ver}.linux-{arch}.tar.gz"
5959

6060
_ACCEPT_IPV6_CONF = Path("/proc/sys/net/ipv6/conf/default/accept_ra")
61-
_PACKET_FORWARDING_CONF = Path("/proc/sys/net/ipv4/ip_forward")
6261

6362
_SHELL_LOGIN = Path("/etc/profile")
6463
_GO_ROOT = Path("/usr/local/go")
@@ -163,12 +162,6 @@ def _configure_server(self) -> None:
163162
self._logger.info("IPv6 support disabled!")
164163
else:
165164
self._logger.debug("IPv6 already disabled!")
166-
if int(_PACKET_FORWARDING_CONF.read_text()) != 1:
167-
self._logger.info("Enabling packet forwarding for the server...")
168-
_PACKET_FORWARDING_CONF.write_text("1")
169-
self._logger.info("Packet forwarding enabled!")
170-
else:
171-
self._logger.debug("Packet forwarding already disabled!")
172165

173166
def _install_go(self) -> Path:
174167
arch = "arm64" if get_arch() == "arm" else "amd64"

0 commit comments

Comments
 (0)