Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 63b365e

Browse files
committed
Apply the rules passed to the pod at startpod.
Rules passed at startpod are applied in the VM using iptables-restore. Signed-off-by: Archana Shinde <[email protected]>
1 parent b8d55ef commit 63b365e

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

src/init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ static int hyper_setup_pod(struct hyper_pod *pod)
479479
return -1;
480480
}
481481

482+
if (hyper_setup_iptables(pod) < 0) {
483+
fprintf(stderr, "iptable rules setup failed\n");
484+
return -1;
485+
}
486+
482487
if (hyper_setup_dns(pod) < 0) {
483488
fprintf(stderr, "setup network failed\n");
484489
return -1;

src/net.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <netdb.h>
1515
#include <net/if.h>
1616
#include <ifaddrs.h>
17+
#include <sys/wait.h>
1718

1819
#include "hyper.h"
1920
#include "util.h"
@@ -1359,3 +1360,139 @@ void hyper_cleanup_dns(struct hyper_pod *pod)
13591360

13601361
close(fd);
13611362
}
1363+
1364+
int hyper_setup_iptables(struct hyper_pod *pod) {
1365+
int pid, status;
1366+
int pipefd[2] = {-1, -1};
1367+
int pipe_parent_err[2] = {-1, -1};
1368+
int ret = -1;
1369+
size_t size, offset = 0;
1370+
int pipe_size = 0;
1371+
const char *cmd = "iptables-restore";
1372+
1373+
if (! pod) {
1374+
return -1;
1375+
}
1376+
1377+
if (! pod->iptable_rules) {
1378+
return 0;
1379+
}
1380+
1381+
if (! *(pod->iptable_rules)) {
1382+
free(pod->iptable_rules);
1383+
return 0;
1384+
}
1385+
1386+
if (pipe(pipefd) == -1) {
1387+
fprintf(stderr, "Error while creating pipe for iptables: %s\n",
1388+
strerror(errno));
1389+
goto err;
1390+
}
1391+
1392+
if (pipe2(pipe_parent_err, O_CLOEXEC) == -1) {
1393+
fprintf(stderr, "Error while creating parent error pipe for "
1394+
"iptables: %s\n",
1395+
strerror(errno));
1396+
goto err;
1397+
}
1398+
1399+
pid = fork();
1400+
if ( pid == -1) {
1401+
fprintf(stderr, "Failed to spawn child: %s\n", strerror(errno));
1402+
goto err;
1403+
}
1404+
1405+
if (pid == 0) {
1406+
char c;
1407+
close_if_set(pipefd[1]);
1408+
close_if_set(pipe_parent_err[1]);
1409+
1410+
if (read(pipe_parent_err[0], &c, sizeof(c)) != 0) {
1411+
fprintf(stderr, "Parent setup failed for command %s\n",
1412+
cmd);
1413+
}
1414+
1415+
/* The rules are sent to the stdin of iptables-restore
1416+
*/
1417+
if (dup2(pipefd[0], STDIN_FILENO) == -1) {
1418+
fprintf(stderr, "Dup call failed : %s\n",
1419+
strerror(errno));
1420+
goto err_child;
1421+
}
1422+
1423+
fprintf(stdout, "execing %s\n", cmd);
1424+
if (execlp(cmd, cmd, "-v", NULL) == -1) {
1425+
fprintf(stderr, "Exec call for %s failed :%s\n",
1426+
cmd, strerror(errno));
1427+
}
1428+
1429+
err_child:
1430+
close_if_set(pipefd[0]);
1431+
close_if_set(pipe_parent_err[0]);
1432+
exit(EXIT_FAILURE);
1433+
}
1434+
1435+
//parent
1436+
close_if_set(pipefd[0]);
1437+
close_if_set(pipe_parent_err[0]);
1438+
1439+
size = strlen(pod->iptable_rules);
1440+
1441+
pipe_size = fcntl(pipefd[1], F_GETPIPE_SZ);
1442+
if (pipe_size <= size) {
1443+
if (fcntl(pipefd[1], F_SETPIPE_SZ, size+1) < 0) {
1444+
fprintf(stderr, "failed to change pipe size: %s",
1445+
strerror(errno));
1446+
goto err_parent;
1447+
}
1448+
}
1449+
1450+
while (offset < size) {
1451+
ret = write(pipefd[1], pod->iptable_rules+offset, size-offset);
1452+
if (ret < 0 && ret != EINTR) {
1453+
break;
1454+
}
1455+
offset += ret;
1456+
}
1457+
1458+
err_parent:
1459+
if (offset < size) {
1460+
fprintf(stderr, "Pipe Write err : %s\n", strerror(errno));
1461+
if (write(pipe_parent_err[1], "E", 1) == -1) {
1462+
fprintf(stderr, "Error writing to parent err pipe: "
1463+
"%s\n", strerror(errno));
1464+
}
1465+
}
1466+
1467+
close_if_set(pipe_parent_err[1]);
1468+
close_if_set(pipefd[1]);
1469+
1470+
if (waitpid(pid, &status, 0) <= 0) {
1471+
fprintf(stderr, "Error waiting for child for %s: %s",
1472+
cmd, strerror(errno));
1473+
goto err;
1474+
}
1475+
1476+
if (WIFEXITED(status)) {
1477+
ret = WEXITSTATUS(status);
1478+
fprintf(stdout, "Command %s exited normally, "
1479+
"status %" PRIu8 "\n",
1480+
cmd, ret);
1481+
if (ret == 0) {
1482+
free(pod->iptable_rules);
1483+
return 0;
1484+
}
1485+
}
1486+
1487+
fprintf(stdout, "Command %s exit unexpectedly, "
1488+
"status %" PRIu8 "\n",
1489+
cmd, status);
1490+
1491+
err:
1492+
close_if_set(pipefd[0]);
1493+
close_if_set(pipefd[1]);
1494+
close_if_set(pipe_parent_err[0]);
1495+
close_if_set(pipe_parent_err[1]);
1496+
free(pod->iptable_rules);
1497+
return -1;
1498+
}

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int hyper_cmd_setup_route(char *json, int length);
6060
void hyper_cleanup_network(struct hyper_pod *pod);
6161
int hyper_setup_dns(struct hyper_pod *pod);
6262
void hyper_cleanup_dns(struct hyper_pod *pod);
63+
int hyper_setup_iptables(struct hyper_pod *pod);
6364
int hyper_get_type(int fd, uint32_t *type);
6465
int hyper_send_type(int fd, uint32_t type);
6566
int hyper_send_type_block(int fd, uint32_t type, int need_ack);

src/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct env;
1919

2020
#define free_if_set(var) if (var) { free(var); var=NULL; }
2121

22+
#define close_if_set(fd) \
23+
if ((fd != -1)) { close(fd); fd=-1; }
24+
2225
char *read_cmdline(void);
2326
int hyper_setup_env(struct env *envs, int num);
2427
int hyper_find_sd(char *addr, char **dev);

0 commit comments

Comments
 (0)