@@ -18,6 +18,7 @@ type LinuxJail struct {
18
18
logger * slog.Logger
19
19
namespace string
20
20
vethHost string // Host-side veth interface name for iptables rules
21
+ vethNetJail string // Host-side veth interface name for iptables rules
21
22
commandEnv []string
22
23
httpProxyPort int
23
24
configDir string
@@ -49,6 +50,48 @@ func (l *LinuxJail) Start() error {
49
50
e := getEnvs (l .configDir , l .caCertPath )
50
51
l .commandEnv = mergeEnvs (e , map [string ]string {})
51
52
53
+ // Create veth pair with short names (Linux interface names limited to 15 chars)
54
+ // Generate unique ID to avoid conflicts
55
+ uniqueID := fmt .Sprintf ("%d" , time .Now ().UnixNano ()% 10000000 ) // 7 digits max
56
+ vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
57
+ vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
58
+
59
+ // Store veth interface name for iptables rules
60
+ l .vethHost = vethHost
61
+ l .vethNetJail = vethNetJail
62
+
63
+ setupCmds := []struct {
64
+ description string
65
+ command * exec.Cmd
66
+ ambientCaps []uintptr
67
+ }{
68
+ {
69
+ "create veth pair" ,
70
+ exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail ),
71
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
72
+ },
73
+ {
74
+ "configure host veth" ,
75
+ exec .Command ("ip" , "addr" , "add" , "192.168.100.1/24" , "dev" , vethHost ),
76
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
77
+ },
78
+ {
79
+ "bring up host veth" ,
80
+ exec .Command ("ip" , "link" , "set" , vethHost , "up" ),
81
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
82
+ },
83
+ }
84
+
85
+ for _ , command := range setupCmds {
86
+ command .command .SysProcAttr = & syscall.SysProcAttr {
87
+ AmbientCaps : command .ambientCaps ,
88
+ }
89
+
90
+ if err := command .command .Run (); err != nil {
91
+ return fmt .Errorf ("failed to %s: %v" , command .description , err )
92
+ }
93
+ }
94
+
52
95
return nil
53
96
}
54
97
@@ -91,6 +134,12 @@ func (l *LinuxJail) ConfigureAfterRun(pidInt int) {
91
134
}
92
135
}
93
136
137
+ func (l * LinuxJail ) GetNetworkConfiguration () NetworkConfiguration {
138
+ return NetworkConfiguration {
139
+ VethNetJail : l .vethNetJail ,
140
+ }
141
+ }
142
+
94
143
// Close removes the network namespace and iptables rules
95
144
func (l * LinuxJail ) Close () error {
96
145
l .logger .Debug ("Close called" )
@@ -140,39 +189,14 @@ func (l *LinuxJail) createNamespace() error {
140
189
func (l * LinuxJail ) setupParentNetworking (pidInt int ) error {
141
190
PID := fmt .Sprintf ("%v" , pidInt )
142
191
143
- // Create veth pair with short names (Linux interface names limited to 15 chars)
144
- // Generate unique ID to avoid conflicts
145
- uniqueID := fmt .Sprintf ("%d" , time .Now ().UnixNano ()% 10000000 ) // 7 digits max
146
- uniqueID = "1111111"
147
- vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
148
- vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
149
-
150
- // Store veth interface name for iptables rules
151
- l .vethHost = vethHost
152
-
153
192
setupCmds := []struct {
154
193
description string
155
194
command * exec.Cmd
156
195
ambientCaps []uintptr
157
196
}{
158
- {
159
- "create veth pair" ,
160
- exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail ),
161
- []uintptr {uintptr (unix .CAP_NET_ADMIN )},
162
- },
163
197
{
164
198
"move veth to namespace" ,
165
- exec .Command ("ip" , "link" , "set" , vethNetJail , "netns" , PID ),
166
- []uintptr {uintptr (unix .CAP_NET_ADMIN )},
167
- },
168
- {
169
- "configure host veth" ,
170
- exec .Command ("ip" , "addr" , "add" , "192.168.100.1/24" , "dev" , vethHost ),
171
- []uintptr {uintptr (unix .CAP_NET_ADMIN )},
172
- },
173
- {
174
- "bring up host veth" ,
175
- exec .Command ("ip" , "link" , "set" , vethHost , "up" ),
199
+ exec .Command ("ip" , "link" , "set" , l .vethNetJail , "netns" , PID ),
176
200
[]uintptr {uintptr (unix .CAP_NET_ADMIN )},
177
201
},
178
202
}
0 commit comments