Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit d2d04e8

Browse files
authored
Merge pull request #339 from gnawux/portmapping
port mapping: move the pod level whitelist rules to pod setup
2 parents 68d80dd + 15bb718 commit d2d04e8

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/init.c

+1
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ static void hyper_flush_channel()
553553

554554
void hyper_pod_destroyed(struct hyper_pod *pod, int failed)
555555
{
556+
hyper_cleanup_portmapping(pod);
556557
hyper_cleanup_mounts(pod);
557558
hyper_ctl_append_msg(&hyper_epoll.ctl, failed?ERROR:ACK, NULL, 0);
558559
// Todo: this doesn't make sure peer receives the data

src/portmapping.c

+36-17
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,14 @@ int hyper_setup_portmapping(struct hyper_pod *pod)
161161
fprintf(stderr, "sysctl: setup default nf_conntrack_tcp_timeout_established(%s) failed\n", timeout);
162162
}
163163

164-
return 0;
165-
}
166-
167-
int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod)
168-
{
169-
// only allow network request from internal white list
170-
int i = 0, j = 0;
171-
char rule[128] = {0};
172-
char iptables_restore[512];
173-
174-
// restore iptables rules
175-
if (sprintf(iptables_restore, "iptables-restore /tmp/hyper/shared/%s-iptables", c->id) > 0) {
176-
hyper_cmd(iptables_restore);
177-
}
178-
164+
// configure the white list rules for lan access
179165
if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 &&
180166
pod->portmap_white_lists->e_num == 0)) {
181167
return 0;
182168
}
183169

170+
int j = 0;
171+
char rule[128] = {0};
184172
for (j=0; j<pod->portmap_white_lists->i_num; j++) {
185173
sprintf(rule, "-s %s -j ACCEPT",
186174
pod->portmap_white_lists->internal_networks[j]);
@@ -196,6 +184,26 @@ int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_po
196184
}
197185
}
198186

187+
return 0;
188+
}
189+
190+
int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod)
191+
{
192+
// only allow network request from internal white list
193+
int i = 0, j = 0;
194+
char rule[128] = {0};
195+
char iptables_restore[512];
196+
197+
// restore iptables rules
198+
if (sprintf(iptables_restore, "iptables-restore /tmp/hyper/shared/%s-iptables", c->id) > 0) {
199+
hyper_cmd(iptables_restore);
200+
}
201+
202+
if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 &&
203+
pod->portmap_white_lists->e_num == 0)) {
204+
return 0;
205+
}
206+
199207
if (c->ports_num == 0) {
200208
return 0;
201209
}
@@ -250,14 +258,15 @@ int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_po
250258
return 0;
251259
}
252260

253-
void hyper_cleanup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod)
261+
// cleanup pod level portmapping configurations
262+
void hyper_cleanup_portmapping(struct hyper_pod *pod)
254263
{
255264
if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 &&
256265
pod->portmap_white_lists->e_num == 0)) {
257266
return;
258267
}
259268

260-
int i = 0, j = 0;
269+
int j = 0;
261270
char rule[128] = {0};
262271
for (j=0; j<pod->portmap_white_lists->i_num; j++) {
263272
sprintf(rule, "-s %s -j ACCEPT",
@@ -272,7 +281,17 @@ void hyper_cleanup_container_portmapping(struct hyper_container *c, struct hyper
272281
fprintf(stderr, "cleanup accept_rule '%s' failed\n", rule);
273282
}
274283
}
284+
}
285+
286+
void hyper_cleanup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod)
287+
{
288+
if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 &&
289+
pod->portmap_white_lists->e_num == 0)) {
290+
return;
291+
}
275292

293+
int i = 0, j = 0;
294+
char rule[128] = {0};
276295
if (c->ports_num == 0) {
277296
return;
278297
}

src/portmapping.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct hyper_pod;
1717
struct hyper_container;
1818
int hyper_setup_portmapping(struct hyper_pod *pod);
1919
int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod);
20+
void hyper_cleanup_portmapping(struct hyper_pod *pod);
2021
void hyper_cleanup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod);
2122

2223
#endif

0 commit comments

Comments
 (0)