2007-02-14 16:26:42 +00:00
|
|
|
/*
|
2009-02-03 13:08:07 +00:00
|
|
|
* Copyright (C) 2007-2009 Red Hat, Inc.
|
2007-02-14 16:26:42 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Mark McLoughlin <markmc@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-12-07 14:45:39 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2007-02-14 16:26:42 +00:00
|
|
|
#include <sys/wait.h>
|
2007-12-07 14:45:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
#include <paths.h>
|
|
|
|
#endif
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
#include "internal.h"
|
2007-12-07 14:45:39 +00:00
|
|
|
#include "iptables.h"
|
2008-01-10 13:49:55 +00:00
|
|
|
#include "util.h"
|
2008-06-06 11:09:57 +00:00
|
|
|
#include "memory.h"
|
2009-02-05 16:27:51 +00:00
|
|
|
#include "virterror_internal.h"
|
2009-03-03 11:40:08 +00:00
|
|
|
#include "logging.h"
|
2007-06-26 23:48:46 +00:00
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
enum {
|
|
|
|
ADD = 0,
|
|
|
|
REMOVE
|
|
|
|
};
|
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *rule;
|
2008-08-08 15:43:38 +00:00
|
|
|
const char **argv;
|
2008-01-10 13:50:46 +00:00
|
|
|
int command_idx;
|
2007-03-30 16:25:02 +00:00
|
|
|
} iptRule;
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *table;
|
|
|
|
char *chain;
|
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
int nrules;
|
|
|
|
iptRule *rules;
|
2007-03-30 16:24:13 +00:00
|
|
|
|
2008-01-10 13:57:48 +00:00
|
|
|
#ifdef ENABLE_IPTABLES_LOKKIT
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
char dir[PATH_MAX];
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2008-01-10 13:57:48 +00:00
|
|
|
#endif /* ENABLE_IPTABLES_LOKKIT */
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
} iptRules;
|
|
|
|
|
|
|
|
struct _iptablesContext
|
|
|
|
{
|
|
|
|
iptRules *input_filter;
|
|
|
|
iptRules *forward_filter;
|
|
|
|
iptRules *nat_postrouting;
|
|
|
|
};
|
|
|
|
|
2008-01-10 13:54:02 +00:00
|
|
|
#ifdef ENABLE_IPTABLES_LOKKIT
|
|
|
|
static void
|
|
|
|
notifyRulesUpdated(const char *table,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
char arg[PATH_MAX];
|
2008-08-08 15:43:38 +00:00
|
|
|
const char *argv[4];
|
2008-01-10 13:54:02 +00:00
|
|
|
|
|
|
|
snprintf(arg, sizeof(arg), "--custom-rules=ipv4:%s:%s", table, path);
|
|
|
|
|
|
|
|
argv[0] = (char *) LOKKIT_PATH;
|
|
|
|
argv[1] = (char *) "--nostart";
|
|
|
|
argv[2] = arg;
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
2009-02-05 16:27:51 +00:00
|
|
|
char ebuf[1024];
|
2008-01-10 13:54:02 +00:00
|
|
|
if (virRun(NULL, argv, NULL) < 0)
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to run '%s %s': %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
LOKKIT_PATH, arg, virStrerror(errno, ebuf, sizeof ebuf));
|
2008-01-10 13:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
stripLine(char *str, int len, const char *line)
|
|
|
|
{
|
|
|
|
char *s, *p;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
changed = 0;
|
|
|
|
s = str;
|
|
|
|
|
|
|
|
while ((p = strchr(s, '\n'))) {
|
2008-05-14 19:51:24 +00:00
|
|
|
if (p == s || STRNEQLEN(s, line, p - s)) {
|
2008-01-10 13:54:02 +00:00
|
|
|
s = ++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++p;
|
|
|
|
memmove(s, p, len - (p - str) + 1);
|
|
|
|
len -= p - s;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
|
2008-05-14 19:51:24 +00:00
|
|
|
if (STREQ(s, line)) {
|
2008-01-10 13:54:02 +00:00
|
|
|
*s = '\0';
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notifyRulesRemoved(const char *table,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
/* 10 MB limit on config file size as a sanity check */
|
|
|
|
#define MAX_FILE_LEN (1024*1024*10)
|
|
|
|
|
|
|
|
char arg[PATH_MAX];
|
|
|
|
char *content;
|
|
|
|
int len;
|
|
|
|
FILE *f = NULL;
|
|
|
|
|
|
|
|
len = virFileReadAll(SYSCONF_DIR "/sysconfig/system-config-firewall",
|
|
|
|
MAX_FILE_LEN, &content);
|
|
|
|
if (len < 0) {
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN("%s", _("Failed to read " SYSCONF_DIR
|
|
|
|
"/sysconfig/system-config-firewall"));
|
2008-01-10 13:54:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(arg, sizeof(arg), "--custom-rules=ipv4:%s:%s", table, path);
|
|
|
|
|
|
|
|
if (!stripLine(content, len, arg)) {
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(content);
|
2008-01-10 13:54:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(f = fopen(SYSCONF_DIR "/sysconfig/system-config-firewall", "w")))
|
|
|
|
goto write_error;
|
|
|
|
|
|
|
|
if (fputs(content, f) == EOF)
|
|
|
|
goto write_error;
|
|
|
|
|
|
|
|
if (fclose(f) == EOF) {
|
|
|
|
f = NULL;
|
|
|
|
goto write_error;
|
|
|
|
}
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(content);
|
2008-01-10 13:54:02 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
2009-02-05 16:27:51 +00:00
|
|
|
write_error:;
|
|
|
|
char ebuf[1024];
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to write to " SYSCONF_DIR
|
|
|
|
"/sysconfig/system-config-firewall : %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
virStrerror(errno, ebuf, sizeof ebuf));
|
2008-01-10 13:54:02 +00:00
|
|
|
if (f)
|
|
|
|
fclose(f);
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(content);
|
2008-01-10 13:54:02 +00:00
|
|
|
|
|
|
|
#undef MAX_FILE_LEN
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
|
|
|
writeRules(const char *path,
|
2008-01-10 13:48:44 +00:00
|
|
|
const iptRule *rules,
|
2007-02-14 16:26:42 +00:00
|
|
|
int nrules)
|
|
|
|
{
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
FILE *f;
|
|
|
|
int istmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (nrules == 0 && unlink(path) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (snprintf(tmp, PATH_MAX, "%s.new", path) >= PATH_MAX)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
istmp = 1;
|
|
|
|
|
|
|
|
if (!(f = fopen(tmp, "w"))) {
|
|
|
|
istmp = 0;
|
|
|
|
if (!(f = fopen(path, "w")))
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nrules; i++) {
|
2007-03-30 16:25:02 +00:00
|
|
|
if (fputs(rules[i].rule, f) == EOF ||
|
2007-02-14 16:26:42 +00:00
|
|
|
fputc('\n', f) == EOF) {
|
|
|
|
fclose(f);
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (istmp && rename(tmp, path) < 0) {
|
|
|
|
unlink(tmp);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-10 13:57:48 +00:00
|
|
|
#endif /* ENABLE_IPTABLES_LOKKIT */
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2008-01-10 14:01:00 +00:00
|
|
|
static void
|
|
|
|
iptRulesSave(iptRules *rules)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_IPTABLES_LOKKIT
|
|
|
|
int err;
|
|
|
|
|
2009-02-05 16:27:51 +00:00
|
|
|
char ebuf[1024];
|
2008-02-07 10:17:24 +00:00
|
|
|
if ((err = virFileMakePath(rules->dir))) {
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to create directory %s : %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
rules->dir, virStrerror(err, ebuf, sizeof ebuf));
|
2008-02-07 10:17:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-10 14:01:00 +00:00
|
|
|
if ((err = writeRules(rules->path, rules->rules, rules->nrules))) {
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to saves iptables rules to %s : %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
rules->path, virStrerror(err, ebuf, sizeof ebuf));
|
2008-01-10 14:01:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rules->nrules > 0)
|
|
|
|
notifyRulesUpdated(rules->table, rules->path);
|
|
|
|
else
|
|
|
|
notifyRulesRemoved(rules->table, rules->path);
|
2008-02-22 12:34:27 +00:00
|
|
|
#else
|
|
|
|
(void) rules;
|
2008-01-10 14:01:00 +00:00
|
|
|
#endif /* ENABLE_IPTABLES_LOKKIT */
|
|
|
|
}
|
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
static void
|
|
|
|
iptRuleFree(iptRule *rule)
|
|
|
|
{
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(rule->rule);
|
2007-03-30 16:25:02 +00:00
|
|
|
|
|
|
|
if (rule->argv) {
|
|
|
|
int i = 0;
|
|
|
|
while (rule->argv[i])
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(rule->argv[i++]);
|
|
|
|
VIR_FREE(rule->argv);
|
2007-03-30 16:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
|
|
|
iptRulesAppend(iptRules *rules,
|
2007-03-30 16:25:02 +00:00
|
|
|
char *rule,
|
2008-08-08 15:43:38 +00:00
|
|
|
const char **argv,
|
2008-01-10 13:50:46 +00:00
|
|
|
int command_idx)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_REALLOC_N(rules->rules, rules->nrules+1) < 0) {
|
2007-03-30 16:25:02 +00:00
|
|
|
int i = 0;
|
|
|
|
while (argv[i])
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(argv[i++]);
|
|
|
|
VIR_FREE(argv);
|
2007-02-14 16:26:42 +00:00
|
|
|
return ENOMEM;
|
2007-03-30 16:25:02 +00:00
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2008-01-10 13:50:46 +00:00
|
|
|
rules->rules[rules->nrules].rule = rule;
|
|
|
|
rules->rules[rules->nrules].argv = argv;
|
|
|
|
rules->rules[rules->nrules].command_idx = command_idx;
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
rules->nrules++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iptRulesRemove(iptRules *rules,
|
2007-03-30 16:24:13 +00:00
|
|
|
char *rule)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < rules->nrules; i++)
|
2008-05-14 19:51:24 +00:00
|
|
|
if (STREQ(rules->rules[i].rule, rule))
|
2007-02-14 16:26:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (i >= rules->nrules)
|
|
|
|
return EINVAL;
|
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
iptRuleFree(&rules->rules[i]);
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
memmove(&rules->rules[i],
|
|
|
|
&rules->rules[i+1],
|
2007-03-30 16:25:02 +00:00
|
|
|
(rules->nrules - i - 1) * sizeof (iptRule));
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
rules->nrules--;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iptRulesFree(iptRules *rules)
|
|
|
|
{
|
2007-03-30 16:24:13 +00:00
|
|
|
int i;
|
|
|
|
|
2009-02-03 13:08:07 +00:00
|
|
|
VIR_FREE(rules->table);
|
|
|
|
VIR_FREE(rules->chain);
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2007-03-30 16:24:13 +00:00
|
|
|
if (rules->rules) {
|
2007-03-30 16:25:02 +00:00
|
|
|
for (i = 0; i < rules->nrules; i++)
|
|
|
|
iptRuleFree(&rules->rules[i]);
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(rules->rules);
|
2007-03-30 16:25:02 +00:00
|
|
|
|
|
|
|
rules->nrules = 0;
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
2007-03-30 16:24:13 +00:00
|
|
|
|
2008-01-10 13:57:48 +00:00
|
|
|
#ifdef ENABLE_IPTABLES_LOKKIT
|
2007-03-30 16:24:13 +00:00
|
|
|
rules->dir[0] = '\0';
|
|
|
|
rules->path[0] = '\0';
|
2008-01-10 13:57:48 +00:00
|
|
|
#endif /* ENABLE_IPTABLES_LOKKIT */
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(rules);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static iptRules *
|
|
|
|
iptRulesNew(const char *table,
|
|
|
|
const char *chain)
|
|
|
|
{
|
|
|
|
iptRules *rules;
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC(rules) < 0)
|
2007-02-14 16:26:42 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(rules->table = strdup(table)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(rules->chain = strdup(chain)))
|
|
|
|
goto error;
|
|
|
|
|
2007-03-30 16:24:13 +00:00
|
|
|
rules->rules = NULL;
|
|
|
|
rules->nrules = 0;
|
|
|
|
|
2008-01-10 13:57:48 +00:00
|
|
|
#ifdef ENABLE_IPTABLES_LOKKIT
|
|
|
|
if (virFileBuildPath(LOCAL_STATE_DIR "/lib/libvirt/iptables", table, NULL,
|
|
|
|
rules->dir, sizeof(rules->dir)) < 0)
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
2008-01-10 13:49:55 +00:00
|
|
|
if (virFileBuildPath(rules->dir, chain, ".chain", rules->path, sizeof(rules->path)) < 0)
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
2008-01-10 13:57:48 +00:00
|
|
|
#endif /* ENABLE_IPTABLES_LOKKIT */
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
return rules;
|
|
|
|
|
|
|
|
error:
|
|
|
|
iptRulesFree(rules);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int retval = ENOMEM;
|
2008-08-08 15:43:38 +00:00
|
|
|
const char **argv;
|
2008-01-10 13:51:55 +00:00
|
|
|
char *rule = NULL;
|
2007-02-14 16:26:42 +00:00
|
|
|
const char *s;
|
2008-01-10 13:51:55 +00:00
|
|
|
int n, command_idx;
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
n = 1 + /* /sbin/iptables */
|
|
|
|
2 + /* --table foo */
|
|
|
|
2 + /* --insert bar */
|
|
|
|
1; /* arg */
|
|
|
|
|
|
|
|
va_start(args, arg);
|
2008-01-10 13:51:55 +00:00
|
|
|
while ((s = va_arg(args, const char *)))
|
2007-02-14 16:26:42 +00:00
|
|
|
n++;
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC_N(argv, n + 1) < 0)
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
if (!(argv[n++] = strdup(IPTABLES_PATH)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(argv[n++] = strdup("--table")))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(argv[n++] = strdup(rules->table)))
|
|
|
|
goto error;
|
|
|
|
|
2008-01-10 13:50:46 +00:00
|
|
|
command_idx = n;
|
2007-03-30 16:25:02 +00:00
|
|
|
|
2008-01-10 13:51:55 +00:00
|
|
|
if (!(argv[n++] = strdup("--insert")))
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(argv[n++] = strdup(rules->chain)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(argv[n++] = strdup(arg)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
va_start(args, arg);
|
|
|
|
|
2008-01-10 13:51:55 +00:00
|
|
|
while ((s = va_arg(args, const char *)))
|
2007-02-14 16:26:42 +00:00
|
|
|
if (!(argv[n++] = strdup(s)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2008-11-05 18:22:10 +00:00
|
|
|
if (!(rule = virArgvToString(&argv[command_idx])))
|
2008-01-10 13:51:55 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (action == REMOVE) {
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(argv[command_idx]);
|
2008-01-10 13:51:55 +00:00
|
|
|
if (!(argv[command_idx] = strdup("--delete")))
|
|
|
|
goto error;
|
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2008-01-10 13:49:55 +00:00
|
|
|
if (virRun(NULL, argv, NULL) < 0) {
|
|
|
|
retval = errno;
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
2008-01-10 13:49:55 +00:00
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
if (action == ADD) {
|
2008-01-10 13:50:46 +00:00
|
|
|
retval = iptRulesAppend(rules, rule, argv, command_idx);
|
2007-03-30 16:25:02 +00:00
|
|
|
rule = NULL;
|
|
|
|
argv = NULL;
|
|
|
|
} else {
|
2007-02-14 16:26:42 +00:00
|
|
|
retval = iptRulesRemove(rules, rule);
|
2007-03-30 16:25:02 +00:00
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
error:
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(rule);
|
2007-02-14 16:26:42 +00:00
|
|
|
|
|
|
|
if (argv) {
|
|
|
|
n = 0;
|
|
|
|
while (argv[n])
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(argv[n++]);
|
|
|
|
VIR_FREE(argv);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesContextNew:
|
|
|
|
*
|
|
|
|
* Create a new IPtable context
|
|
|
|
*
|
|
|
|
* Returns a pointer to the new structure or NULL in case of error
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
iptablesContext *
|
|
|
|
iptablesContextNew(void)
|
|
|
|
{
|
|
|
|
iptablesContext *ctx;
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC(ctx) < 0)
|
2007-02-14 16:26:42 +00:00
|
|
|
return NULL;
|
|
|
|
|
2008-01-10 13:56:22 +00:00
|
|
|
if (!(ctx->input_filter = iptRulesNew("filter", "INPUT")))
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
2008-01-10 13:56:22 +00:00
|
|
|
if (!(ctx->forward_filter = iptRulesNew("filter", "FORWARD")))
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
2008-01-10 13:56:22 +00:00
|
|
|
if (!(ctx->nat_postrouting = iptRulesNew("nat", "POSTROUTING")))
|
2007-02-14 16:26:42 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
error:
|
|
|
|
iptablesContextFree(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesContextFree:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
*
|
2008-02-27 10:37:19 +00:00
|
|
|
* Free the resources associated with an IP table context
|
2007-06-29 13:23:13 +00:00
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
void
|
|
|
|
iptablesContextFree(iptablesContext *ctx)
|
|
|
|
{
|
2007-03-30 16:20:19 +00:00
|
|
|
if (ctx->input_filter)
|
|
|
|
iptRulesFree(ctx->input_filter);
|
|
|
|
if (ctx->forward_filter)
|
|
|
|
iptRulesFree(ctx->forward_filter);
|
|
|
|
if (ctx->nat_postrouting)
|
|
|
|
iptRulesFree(ctx->nat_postrouting);
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(ctx);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2008-01-10 14:01:00 +00:00
|
|
|
/**
|
|
|
|
* iptablesSaveRules:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
*
|
|
|
|
* Saves all the IP table rules associated with a context
|
|
|
|
* to disk so that if iptables is restarted, the rules
|
|
|
|
* will automatically be reload.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
iptablesSaveRules(iptablesContext *ctx)
|
|
|
|
{
|
|
|
|
iptRulesSave(ctx->input_filter);
|
|
|
|
iptRulesSave(ctx->forward_filter);
|
|
|
|
iptRulesSave(ctx->nat_postrouting);
|
|
|
|
}
|
|
|
|
|
2007-03-30 16:25:02 +00:00
|
|
|
static void
|
|
|
|
iptRulesReload(iptRules *rules)
|
|
|
|
{
|
|
|
|
int i;
|
2009-02-05 16:27:51 +00:00
|
|
|
char ebuf[1024];
|
2007-03-30 16:25:02 +00:00
|
|
|
|
|
|
|
for (i = 0; i < rules->nrules; i++) {
|
|
|
|
iptRule *rule = &rules->rules[i];
|
2008-08-08 15:43:38 +00:00
|
|
|
const char *orig;
|
2007-03-30 16:25:02 +00:00
|
|
|
|
2008-01-10 13:50:46 +00:00
|
|
|
orig = rule->argv[rule->command_idx];
|
|
|
|
rule->argv[rule->command_idx] = (char *) "--delete";
|
2007-03-30 16:25:02 +00:00
|
|
|
|
2008-01-10 13:49:55 +00:00
|
|
|
if (virRun(NULL, rule->argv, NULL) < 0)
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to remove iptables rule '%s'"
|
Mark all qemudLog diagnostics for translation.
* po/POTFILES.in: Add names of many new files.
* Makefile.maint (err_func_re): Add qemudLog.
Mark diagnostics with _(...). Split some long lines.
* qemud/qemud.c (remoteCheckCertFile, remoteInitializeGnuTLS):
(qemudDispatchSignalEvent, qemudSetCloseExec, qemudSetNonBlock):
(qemudWritePidFile, qemudListenUnix, remoteMakeSockets):
(remoteListenTCP, qemudInitPaths, qemudInitialize):
(qemudNetworkInit, remoteInitializeTLSSession, remoteCheckDN):
(remoteCheckCertificate, remoteCheckAccess, qemudDispatchServer):
(qemudClientReadBuf, qemudDispatchClientRead):
(qemudClientWriteBuf, qemudDispatchClientWrite, qemudOneLoop):
(remoteConfigGetStringList, checkType, GET_CONF_STR):
(remoteConfigGetAuth, remoteReadConfigFile, main):
* qemud/remote.c (remoteDispatchAuthSaslInit, remoteSASLCheckSSF):
(remoteSASLCheckAccess, remoteDispatchAuthSaslStart):
(remoteDispatchAuthSaslStep, remoteDispatchAuthSaslInit):
(remoteDispatchAuthSaslStart, remoteDispatchAuthSaslStep):
(qemudGetSocketIdentity, remoteDispatchAuthPolkit):
* src/iptables.c (notifyRulesUpdated, MAX_FILE_LEN, iptRulesSave):
(iptRulesReload):
* src/qemu_conf.c (qemudExtractVersionInfo, qemudLoadConfig):
(qemudLoadNetworkConfig, qemudScanConfigDir):
* src/qemu_driver.c (qemudSetCloseExec, qemudSetNonBlock):
(qemudAutostartConfigs, qemudStartup, qemudReload):
(qemudWaitForMonitor, qemudStartVMDaemon, qemudVMData):
(qemudShutdownVMDaemon, qemudStartNetworkDaemon):
(qemudShutdownNetworkDaemon, qemudMonitorCommand):
(qemudDomainUndefine, qemudNetworkUndefine):
* src/uuid.c (virUUIDGenerate):
* src/xm_internal.c (xenXMAttachInterface):
2008-02-07 16:50:17 +00:00
|
|
|
" from chain '%s' in table '%s': %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
rule->rule, rules->chain, rules->table,
|
|
|
|
virStrerror(errno, ebuf, sizeof ebuf));
|
2007-03-30 16:25:02 +00:00
|
|
|
|
2008-01-10 13:50:46 +00:00
|
|
|
rule->argv[rule->command_idx] = orig;
|
2007-03-30 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < rules->nrules; i++)
|
2008-01-10 13:49:55 +00:00
|
|
|
if (virRun(NULL, rules->rules[i].argv, NULL) < 0)
|
2009-03-03 11:40:08 +00:00
|
|
|
VIR_WARN(_("Failed to add iptables rule '%s'"
|
|
|
|
" to chain '%s' in table '%s': %s"),
|
2009-02-05 16:27:51 +00:00
|
|
|
rules->rules[i].rule, rules->chain, rules->table,
|
|
|
|
virStrerror(errno, ebuf, sizeof ebuf));
|
2007-03-30 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesReloadRules:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
*
|
|
|
|
* Reloads all the IP table rules associated to a context
|
|
|
|
*/
|
2007-03-30 16:25:02 +00:00
|
|
|
void
|
|
|
|
iptablesReloadRules(iptablesContext *ctx)
|
|
|
|
{
|
|
|
|
iptRulesReload(ctx->input_filter);
|
|
|
|
iptRulesReload(ctx->forward_filter);
|
|
|
|
iptRulesReload(ctx->nat_postrouting);
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
|
|
|
iptablesInput(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int port,
|
|
|
|
int action,
|
|
|
|
int tcp)
|
|
|
|
{
|
|
|
|
char portstr[32];
|
|
|
|
|
|
|
|
snprintf(portstr, sizeof(portstr), "%d", port);
|
|
|
|
portstr[sizeof(portstr) - 1] = '\0';
|
|
|
|
|
2007-03-13 22:43:22 +00:00
|
|
|
return iptablesAddRemoveRule(ctx->input_filter,
|
|
|
|
action,
|
|
|
|
"--in-interface", iface,
|
|
|
|
"--protocol", tcp ? "tcp" : "udp",
|
|
|
|
"--destination-port", portstr,
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddTcpInput:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the interface name
|
|
|
|
* @port: the TCP port to add
|
|
|
|
*
|
|
|
|
* Add an input to the IP table allowing access to the given @port on
|
|
|
|
* the given @iface interface for TCP packets
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code in case of error
|
|
|
|
*/
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
|
|
|
iptablesAddTcpInput(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
return iptablesInput(ctx, iface, port, ADD, 1);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveTcpInput:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the interface name
|
|
|
|
* @port: the TCP port to remove
|
|
|
|
*
|
2008-02-27 10:37:19 +00:00
|
|
|
* Removes an input from the IP table, hence forbidding access to the given
|
2007-06-29 13:23:13 +00:00
|
|
|
* @port on the given @iface interface for TCP packets
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code in case of error
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
|
|
|
iptablesRemoveTcpInput(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
return iptablesInput(ctx, iface, port, REMOVE, 1);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddUdpInput:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the interface name
|
|
|
|
* @port: the UDP port to add
|
|
|
|
*
|
|
|
|
* Add an input to the IP table allowing access to the given @port on
|
|
|
|
* the given @iface interface for UDP packets
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code in case of error
|
|
|
|
*/
|
|
|
|
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
|
|
|
iptablesAddUdpInput(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
return iptablesInput(ctx, iface, port, ADD, 0);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveUdpInput:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the interface name
|
|
|
|
* @port: the UDP port to remove
|
|
|
|
*
|
2008-02-27 10:37:19 +00:00
|
|
|
* Removes an input from the IP table, hence forbidding access to the given
|
2007-06-29 13:23:13 +00:00
|
|
|
* @port on the given @iface interface for UDP packets
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code in case of error
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
|
|
|
iptablesRemoveUdpInput(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
return iptablesInput(ctx, iface, port, REMOVE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-10 23:17:46 +00:00
|
|
|
/* Allow all traffic coming from the bridge, with a valid network address
|
|
|
|
* to proceed to WAN
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesForwardAllowOut(iptablesContext *ctx,
|
|
|
|
const char *network,
|
2007-02-14 16:26:42 +00:00
|
|
|
const char *iface,
|
2007-04-10 23:17:46 +00:00
|
|
|
const char *physdev,
|
2007-02-14 16:26:42 +00:00
|
|
|
int action)
|
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
if (physdev && physdev[0]) {
|
2007-03-13 22:43:22 +00:00
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--source", network,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--in-interface", iface,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--out-interface", physdev,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--source", network,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--in-interface", iface,
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardAllowOut:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the source interface name
|
|
|
|
* @physdev: the physical output device
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Add a rule to the IP table context to allow the traffic for the
|
|
|
|
* network @network via interface @iface to be forwarded to
|
|
|
|
* @physdev device. This allow the outbound traffic on a bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesAddForwardAllowOut(iptablesContext *ctx,
|
|
|
|
const char *network,
|
2007-03-13 22:43:22 +00:00
|
|
|
const char *iface,
|
2007-04-10 23:17:46 +00:00
|
|
|
const char *physdev)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardAllowOut(ctx, network, iface, physdev, ADD);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardAllowOut:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the source interface name
|
|
|
|
* @physdev: the physical output device
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Remove a rule from the IP table context hence forbidding forwarding
|
|
|
|
* of the traffic for the network @network via interface @iface
|
|
|
|
* to the @physdev device output. This stops the outbound traffic on a bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesRemoveForwardAllowOut(iptablesContext *ctx,
|
|
|
|
const char *network,
|
2007-03-13 22:43:22 +00:00
|
|
|
const char *iface,
|
2007-04-10 23:17:46 +00:00
|
|
|
const char *physdev)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardAllowOut(ctx, network, iface, physdev, REMOVE);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-04-10 23:17:46 +00:00
|
|
|
|
|
|
|
/* Allow all traffic destined to the bridge, with a valid network address
|
|
|
|
* and associated with an existing connection
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
2008-03-28 20:38:21 +00:00
|
|
|
iptablesForwardAllowRelatedIn(iptablesContext *ctx,
|
2007-04-10 23:17:46 +00:00
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev,
|
|
|
|
int action)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
if (physdev && physdev[0]) {
|
2007-03-13 22:43:22 +00:00
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--destination", network,
|
|
|
|
"--in-interface", physdev,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--out-interface", iface,
|
|
|
|
"--match", "state",
|
|
|
|
"--state", "ESTABLISHED,RELATED",
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--destination", network,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--out-interface", iface,
|
|
|
|
"--match", "state",
|
|
|
|
"--state", "ESTABLISHED,RELATED",
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 20:38:21 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardAllowRelatedIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the output interface name
|
|
|
|
* @physdev: the physical input device or NULL
|
|
|
|
*
|
|
|
|
* Add rules to the IP table context to allow the traffic for the
|
|
|
|
* network @network on @physdev device to be forwarded to
|
|
|
|
* interface @iface, if it is part of an existing connection.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iptablesAddForwardAllowRelatedIn(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev)
|
|
|
|
{
|
|
|
|
return iptablesForwardAllowRelatedIn(ctx, network, iface, physdev, ADD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardAllowRelatedIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the output interface name
|
|
|
|
* @physdev: the physical input device or NULL
|
|
|
|
*
|
|
|
|
* Remove rules from the IP table context hence forbidding the traffic for
|
|
|
|
* network @network on @physdev device to be forwarded to
|
|
|
|
* interface @iface, if it is part of an existing connection.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
iptablesRemoveForwardAllowRelatedIn(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev)
|
|
|
|
{
|
|
|
|
return iptablesForwardAllowRelatedIn(ctx, network, iface, physdev, REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow all traffic destined to the bridge, with a valid network address
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
iptablesForwardAllowIn(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev,
|
|
|
|
int action)
|
|
|
|
{
|
|
|
|
if (physdev && physdev[0]) {
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
|
|
|
"--destination", network,
|
|
|
|
"--in-interface", physdev,
|
|
|
|
"--out-interface", iface,
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
|
|
|
"--destination", network,
|
|
|
|
"--out-interface", iface,
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardAllowIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the output interface name
|
|
|
|
* @physdev: the physical input device or NULL
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Add rules to the IP table context to allow the traffic for the
|
|
|
|
* network @network on @physdev device to be forwarded to
|
|
|
|
* interface @iface. This allow the inbound traffic on a bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesAddForwardAllowIn(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev)
|
|
|
|
{
|
|
|
|
return iptablesForwardAllowIn(ctx, network, iface, physdev, ADD);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardAllowIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @iface: the output interface name
|
|
|
|
* @physdev: the physical input device or NULL
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Remove rules from the IP table context hence forbidding the traffic for
|
|
|
|
* network @network on @physdev device to be forwarded to
|
|
|
|
* interface @iface. This stops the inbound traffic on a bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesRemoveForwardAllowIn(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *iface,
|
|
|
|
const char *physdev)
|
|
|
|
{
|
|
|
|
return iptablesForwardAllowIn(ctx, network, iface, physdev, REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Allow all traffic between guests on the same bridge,
|
|
|
|
* with a valid network address
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
iptablesForwardAllowCross(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int action)
|
|
|
|
{
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
|
|
|
"--in-interface", iface,
|
|
|
|
"--out-interface", iface,
|
|
|
|
"--jump", "ACCEPT",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardAllowCross:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the input/output interface name
|
|
|
|
*
|
|
|
|
* Add rules to the IP table context to allow traffic to cross that
|
|
|
|
* interface. It allows all traffic between guests on the same bridge
|
|
|
|
* represented by that interface.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesAddForwardAllowCross(iptablesContext *ctx,
|
|
|
|
const char *iface) {
|
|
|
|
return iptablesForwardAllowCross(ctx, iface, ADD);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardAllowCross:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the input/output interface name
|
|
|
|
*
|
|
|
|
* Remove rules to the IP table context to block traffic to cross that
|
|
|
|
* interface. It forbids traffic between guests on the same bridge
|
|
|
|
* represented by that interface.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesRemoveForwardAllowCross(iptablesContext *ctx,
|
|
|
|
const char *iface) {
|
|
|
|
return iptablesForwardAllowCross(ctx, iface, REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Drop all traffic trying to forward from the bridge.
|
|
|
|
* ie the bridge is the in interface
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
iptablesForwardRejectOut(iptablesContext *ctx,
|
|
|
|
const char *iface,
|
|
|
|
int action)
|
|
|
|
{
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
|
|
|
"--in-interface", iface,
|
|
|
|
"--jump", "REJECT",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardRejectOut:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the output interface name
|
|
|
|
*
|
|
|
|
* Add rules to the IP table context to forbid all traffic to that
|
|
|
|
* interface. It forbids forwarding from the bridge to that interface.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesAddForwardRejectOut(iptablesContext *ctx,
|
|
|
|
const char *iface)
|
|
|
|
{
|
|
|
|
return iptablesForwardRejectOut(ctx, iface, ADD);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardRejectOut:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the output interface name
|
|
|
|
*
|
|
|
|
* Remove rules from the IP table context forbidding all traffic to that
|
|
|
|
* interface. It reallow forwarding from the bridge to that interface.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesRemoveForwardRejectOut(iptablesContext *ctx,
|
|
|
|
const char *iface)
|
|
|
|
{
|
|
|
|
return iptablesForwardRejectOut(ctx, iface, REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Drop all traffic trying to forward to the bridge.
|
|
|
|
* ie the bridge is the out interface
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
iptablesForwardRejectIn(iptablesContext *ctx,
|
2007-03-13 22:43:22 +00:00
|
|
|
const char *iface,
|
2007-04-10 23:17:46 +00:00
|
|
|
int action)
|
|
|
|
{
|
|
|
|
return iptablesAddRemoveRule(ctx->forward_filter,
|
|
|
|
action,
|
|
|
|
"--out-interface", iface,
|
|
|
|
"--jump", "REJECT",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardRejectIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the input interface name
|
|
|
|
*
|
|
|
|
* Add rules to the IP table context to forbid all traffic from that
|
|
|
|
* interface. It forbids forwarding from that interface to the bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-04-10 23:17:46 +00:00
|
|
|
int
|
|
|
|
iptablesAddForwardRejectIn(iptablesContext *ctx,
|
|
|
|
const char *iface)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardRejectIn(ctx, iface, ADD);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardRejectIn:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @iface: the input interface name
|
|
|
|
*
|
|
|
|
* Remove rules from the IP table context forbidding all traffic from that
|
|
|
|
* interface. It allows forwarding from that interface to the bridge.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesRemoveForwardRejectIn(iptablesContext *ctx,
|
|
|
|
const char *iface)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardRejectIn(ctx, iface, REMOVE);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-04-10 23:17:46 +00:00
|
|
|
|
|
|
|
/* Masquerade all traffic coming from the network associated
|
|
|
|
* with the bridge
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
static int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesForwardMasquerade(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *physdev,
|
2007-02-14 16:26:42 +00:00
|
|
|
int action)
|
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
if (physdev && physdev[0]) {
|
2007-03-13 22:43:22 +00:00
|
|
|
return iptablesAddRemoveRule(ctx->nat_postrouting,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--source", network,
|
2008-03-28 01:05:08 +00:00
|
|
|
"--destination", "!", network,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--out-interface", physdev,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--jump", "MASQUERADE",
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
return iptablesAddRemoveRule(ctx->nat_postrouting,
|
|
|
|
action,
|
2007-04-10 23:17:46 +00:00
|
|
|
"--source", network,
|
2008-03-28 01:05:08 +00:00
|
|
|
"--destination", "!", network,
|
2007-03-13 22:43:22 +00:00
|
|
|
"--jump", "MASQUERADE",
|
|
|
|
NULL);
|
|
|
|
}
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesAddForwardMasquerade:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @physdev: the physical input device or NULL
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Add rules to the IP table context to allow masquerading
|
|
|
|
* network @network on @physdev. This allow the bridge to
|
|
|
|
* masquerade for that network (on @physdev).
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesAddForwardMasquerade(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *physdev)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardMasquerade(ctx, network, physdev, ADD);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 13:23:13 +00:00
|
|
|
/**
|
|
|
|
* iptablesRemoveForwardMasquerade:
|
|
|
|
* @ctx: pointer to the IP table context
|
|
|
|
* @network: the source network name
|
|
|
|
* @physdev: the physical input device or NULL
|
2008-02-05 19:27:37 +00:00
|
|
|
*
|
2007-06-29 13:23:13 +00:00
|
|
|
* Remove rules from the IP table context to stop masquerading
|
|
|
|
* network @network on @physdev. This stops the bridge from
|
|
|
|
* masquerading for that network (on @physdev).
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or an error code otherwise
|
|
|
|
*/
|
2007-02-14 16:26:42 +00:00
|
|
|
int
|
2007-04-10 23:17:46 +00:00
|
|
|
iptablesRemoveForwardMasquerade(iptablesContext *ctx,
|
|
|
|
const char *network,
|
|
|
|
const char *physdev)
|
2007-02-14 16:26:42 +00:00
|
|
|
{
|
2007-04-10 23:17:46 +00:00
|
|
|
return iptablesForwardMasquerade(ctx, network, physdev, REMOVE);
|
2007-02-14 16:26:42 +00:00
|
|
|
}
|