From 63cb8b5fa325b359a5d88cc0f35b5df977e8c98a Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sat, 1 Sep 2012 03:23:21 +1000 Subject: [PATCH 1/4] Partially implement support for new option system. --- iptc/__init__.py | 82 ++++++++++++++++++++++++++++++++++-------------- iptc/xtables.py | 69 ++++++++++++++++++++++++++++++++++++++++ setup.py | 0 3 files changed, 128 insertions(+), 23 deletions(-) mode change 100644 => 100755 setup.py diff --git a/iptc/__init__.py b/iptc/__init__.py index 0282bf1..ac10c14 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -15,7 +15,7 @@ import weakref import ctypes.util -from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper +from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper, x6_option_call, x6_option __all__ = ["Table", "Chain", "Rule", "Match", "Target", "Policy", "IPTCError", "POLICY_ACCEPT", "POLICY_DROP", "POLICY_QUEUE", "POLICY_RETURN", @@ -300,31 +300,65 @@ def parse(self, parameter, value): else: inv = ct.c_int(0) - if not self._module.extra_opts: + if self._module.x6_options: + if self._module.x6_parse == None: + raise NotImplementedError() + else: + cb = x6_option_call() + cb.entry = None + for opt in self._module.x6_options: + if opt.name == None: + break + if opt.name == parameter: + cb.entry = ct.pointer(opt) + break + if cb.entry == None: + raise AttributeError() + cb.arg = 0 # FIXME + cb.invert = False + cb.ext_name = self._module.name + try: + #cb.data = self._module.t.data + cb.data = None + cb.xflags = self._module.tflags + cb.target = self._module.t + cb.xt_entry = None + self._module.x6_parse(cb) + self._module.tflags = cb.xflags + except AttributeError: + #cb.data = self._module.m.data + cb.data = None + cb.xflags = self._module.mflags + cb.match = self._module.m + cb.xt_entry = None + cb.udata = self._module.udata + self._module.x6_parse(cb) + self._module.mflags = cb.xflags + elif self._module.extra_opts: + _optarg.value = value + _optind.value = 2 + + argv = (ct.c_char_p * 2)() + argv[0] = parameter + argv[1] = value + + for opt in self._module.extra_opts: + if opt.name == parameter: + entry = self._rule.entry and ct.pointer(self._rule.entry) or \ + None + rv = _wrap_parse(self._module.parse, opt.val, argv, inv, + ct.pointer(self._flags), entry, + ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) + if rv != 1: + raise ValueError("invalid value %s" % (value)) + return + elif not opt.name: + break + raise AttributeError("invalid parameter %s" % (parameter)) + else: raise AttributeError("%s: invalid parameter %s" % (self._module.name, parameter)) - _optarg.value = value - _optind.value = 2 - - argv = (ct.c_char_p * 2)() - argv[0] = parameter - argv[1] = value - - for opt in self._module.extra_opts: - if opt.name == parameter: - entry = self._rule.entry and ct.pointer(self._rule.entry) or \ - None - rv = _wrap_parse(self._module.parse, opt.val, argv, inv, - ct.pointer(self._flags), entry, - ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) - if rv != 1: - raise ValueError("invalid value %s" % (value)) - return - elif not opt.name: - break - raise AttributeError("invalid parameter %s" % (parameter)) - def final_check(self): if self._module and self._module.final_check: self._module.final_check(self._flags) @@ -516,8 +550,10 @@ def __init__(self, rule, name=None, target=None, revision=0): if TABLE_FILTER.is_chain(name): is_standard_target = True module = _xt.find_target('standard') + print "in standard module" else: module = _xt.find_target(name) + print "in extended module " + name if not module: raise XTablesError("can't find target %s" % (name)) self._module = module[0] diff --git a/iptc/xtables.py b/iptc/xtables.py index f9d30ba..423c040 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -73,6 +73,63 @@ class option(ct.Structure): ("flag", ct.POINTER(ct.c_int)), ("val", ct.c_int)] +class x6_option(ct.Structure): + _fields_ = [("name", ct.c_char_p), + ("type", ct.c_uint), + ("id", ct.c_uint), + ("excl", ct.c_uint), + ("also", ct.c_uint), + ("flags", ct.c_uint), + ("ptroff", ct.c_uint), + ("size", ct.c_size_t), + ("min", ct.c_uint), + ("max", ct.c_uint)] + +class x6_option_call_markmask(ct.Structure): + _fields_ = [("mark", ct.c_uint32), + ("mask", ct.c_uint32)] + +class x6_option_call_value(ct.Union): + _fields_ = [("u8", ct.c_uint8), + ("u8_range", ct.c_uint8 * 2), + ("syslog_level", ct.c_uint8), + ("protocol", ct.c_uint8), + ("u16", ct.c_uint16), + ("u16_range", ct.c_uint16 * 2), + ("port", ct.c_uint16), + ("port_range", ct.c_uint16 * 2), + ("u32", ct.c_uint32), + ("u32_range", ct.c_uint32 * 2), + ("u64", ct.c_uint64), + ("u64_range", ct.c_uint64 * 2), + ("dbl", ct.c_double), + # FIXME: Complete other structs. + ("s3", x6_option_call_markmask), + ("ethermac", ct.c_uint8 * 6)] + +class x6_option_call_assoc(ct.Union): + _fields_ = [("match", ct.POINTER(ct.c_void_p)), + ("target", ct.POINTER(ct.c_void_p))] + +class x6_option_call(ct.Structure): + _fields_ = [("arg", ct.c_char_p), + ("ext_name", ct.c_char_p), + ("entry", ct.POINTER(x6_option)), + ("data", ct.c_void_p), + ("xflags", ct.c_uint), + ("invert", ct.c_bool), + ("nvals", ct.c_uint8), + ("val", x6_option_call_value), + ("assoc", x6_option_call_assoc), + ("xt_entry", ct.c_void_p), + ("udata", ct.c_void_p)] + +class x6_option_fcheck(ct.Structure): + _fields_ = [("ext_name", ct.c_char_p), + ("data", ct.c_void_p), + ("udata", ct.c_void_p), + ("xflags", ct.c_uint)] + class xtables_match(ct.Structure): _fields_ = [("version", ct.c_char_p), ("next", ct.c_void_p), @@ -101,6 +158,12 @@ class xtables_match(ct.Structure): ct.POINTER(xt_entry_match))), # Pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), + # New parser + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(x6_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(x6_option_fcheck))), + ("x6_options", ct.POINTER(x6_option)), + ("udata_size", ct.c_size_t), + ("udata", ct.c_void_p), # Ignore these men behind the curtain: ("option_offset", ct.c_uint), ("m", ct.POINTER(xt_entry_match)), @@ -135,6 +198,12 @@ class xtables_target(ct.Structure): ct.POINTER(xt_entry_target))), # Pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), + # New parser + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(x6_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(x6_option_fcheck))), + ("x6_options", ct.POINTER(x6_option)), + ("udata_size", ct.c_size_t), + ("udata", ct.c_void_p), # Ignore these men behind the curtain: ("option_offset", ct.c_uint), ("t", ct.POINTER(xt_entry_target)), diff --git a/setup.py b/setup.py old mode 100644 new mode 100755 From b9d768cf5f313f8ff83120eb7370bca1e7558915 Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sat, 1 Sep 2012 21:36:04 +1000 Subject: [PATCH 2/4] Finished support for IPv6-based extensions. --- iptables | 1 + iptc/__init__.py | 85 +++++++++++++++++++++++++----------------- libxtwrapper/wrapper.c | 27 ++++++++++++++ setup.py | 7 +++- 4 files changed, 85 insertions(+), 35 deletions(-) create mode 160000 iptables diff --git a/iptables b/iptables new file mode 160000 index 0000000..3abf5cc --- /dev/null +++ b/iptables @@ -0,0 +1 @@ +Subproject commit 3abf5cc5ac0c32eba2436567d25e175d7e0f42bc diff --git a/iptc/__init__.py b/iptc/__init__.py index ac10c14..d93017e 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -15,7 +15,7 @@ import weakref import ctypes.util -from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper, x6_option_call, x6_option +from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper, x6_option_call, x6_option, xtables_target, xtables_match __all__ = ["Table", "Chain", "Rule", "Match", "Target", "Policy", "IPTCError", "POLICY_ACCEPT", "POLICY_DROP", "POLICY_QUEUE", "POLICY_RETURN", @@ -275,6 +275,14 @@ class IPTCError(Exception): _wrap_save = _lib_xtwrapper.wrap_save _wrap_save.restype = ct.c_void_p _wrap_save.argtypes = [ct.c_void_p, ct.POINTER(ipt_ip), ct.c_void_p] +_wrap_x6_parse_match = _lib_xtwrapper.wrap_mpcall +_wrap_x6_parse_match.restype = ct.c_int +_wrap_x6_parse_match.argtables = [ct.c_uint, ct.POINTER(ct.c_char_p), + ct.c_int, ct.POINTER(xtables_match), ct.c_void_p, ct.c_void_p] +_wrap_x6_parse_target = _lib_xtwrapper.wrap_tpcall +_wrap_x6_parse_target.restype = ct.c_int +_wrap_x6_parse_target.argtables = [ct.c_uint, ct.POINTER(ct.c_char_p), + ct.c_int, ct.POINTER(xtables_target), ct.c_void_p, ct.c_void_p] _xt = xtables(NFPROTO_IPV4) @@ -300,55 +308,63 @@ def parse(self, parameter, value): else: inv = ct.c_int(0) - if self._module.x6_options: + """if self._module.x6_options: if self._module.x6_parse == None: raise NotImplementedError() else: - cb = x6_option_call() - cb.entry = None for opt in self._module.x6_options: if opt.name == None: break if opt.name == parameter: - cb.entry = ct.pointer(opt) - break - if cb.entry == None: - raise AttributeError() - cb.arg = 0 # FIXME - cb.invert = False - cb.ext_name = self._module.name - try: - #cb.data = self._module.t.data - cb.data = None - cb.xflags = self._module.tflags - cb.target = self._module.t - cb.xt_entry = None - self._module.x6_parse(cb) - self._module.tflags = cb.xflags - except AttributeError: - #cb.data = self._module.m.data - cb.data = None - cb.xflags = self._module.mflags - cb.match = self._module.m - cb.xt_entry = None - cb.udata = self._module.udata - self._module.x6_parse(cb) - self._module.mflags = cb.xflags - elif self._module.extra_opts: + cb = x6_option_call() + cb.entry = self._rule.entry and ct.pointer(self._rule.entry) or \ + None + cb.arg = 0 # FIXME + cb.invert = False + cb.ext_name = self._module.name + try: + #cb.data = self._module.t.data + cb.data = None + cb.xflags = self._module.tflags + cb.target = self._module.t + cb.xt_entry = None + self._module.x6_parse(cb) + self._module.tflags = cb.xflags + except AttributeError: + #cb.data = self._module.m.data + cb.data = None + cb.xflags = self._module.mflags + cb.match = self._module.m + cb.xt_entry = None + cb.udata = self._module.udata + self._module.x6_parse(cb) + self._module.mflags = cb.xflags""" + if self._module.extra_opts or self._module.x6_options: _optarg.value = value _optind.value = 2 argv = (ct.c_char_p * 2)() argv[0] = parameter argv[1] = value - - for opt in self._module.extra_opts: + + v = self._module.extra_opts or self._module.x6_options + for opt in v: + if opt.name == None: + break if opt.name == parameter: entry = self._rule.entry and ct.pointer(self._rule.entry) or \ None - rv = _wrap_parse(self._module.parse, opt.val, argv, inv, - ct.pointer(self._flags), entry, - ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) + if self._module.x6_options: + if (isinstance(self, Match)): + rv = _wrap_x6_parse_match(opt.id, argv, inv, ct.pointer(self._module), ct.pointer(self._module.m), entry) + elif (isinstance(self, Target)): + rv = _wrap_x6_parse_target(opt.id, argv, inv, ct.pointer(self._module), ct.pointer(self._module.t), entry) + else: + raise ValueError("invalid calling type") + else: + rv = _wrap_parse(self._module.parse, opt.val, argv, inv, + ct.pointer(self._flags), entry, + ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) if rv != 1: raise ValueError("invalid value %s" % (value)) return @@ -492,6 +508,7 @@ def reset(self): Parameters are set to their default value, any flags are cleared.""" + print self.size ct.memset(ct.byref(self._match_buf), 0, self.size) self._update_pointers() m = self._ptr[0] diff --git a/libxtwrapper/wrapper.c b/libxtwrapper/wrapper.c index e811c24..c18b4a7 100644 --- a/libxtwrapper/wrapper.c +++ b/libxtwrapper/wrapper.c @@ -3,6 +3,7 @@ #include #include #include +#include "../iptables/include/xtables.h.in" int kernel_version; @@ -49,3 +50,29 @@ void wrap_save(int (*fn)(), const struct ipt_ip *ip, const void *m) fn(ip, m); fflush(stdout); } + +// FIXME: xt_entry_match/target +// +// For some reason, the m and t fields need to +// be passed into these functions from Python +// and then set back onto the structures that +// originally contained them. I am unsure as +// to why the m->m and t->t values seem to get +// "lost" over the Python <-> native boundary. + +int wrap_mpcall(unsigned int c, char **argv, int inv, + struct xtables_match *m, struct xt_entry_match *mm, void *fw) +{ + m->m = mm; // FIXME: See above. + xtables_option_mpcall(c, argv, inv, m, fw); + return 1; /* true */ +} + +int wrap_tpcall(unsigned int c, char **argv, int inv, + struct xtables_target *t, struct xt_entry_target *tt, void *fw) +{ + t->t = tt; // FIXME: See above. + xtables_option_tpcall(c, argv, inv, t, fw); + return 1; /* true */ +} + diff --git a/setup.py b/setup.py index 0c5255c..f03547a 100755 --- a/setup.py +++ b/setup.py @@ -17,7 +17,12 @@ url = "http://nilvec.com/", packages = ["iptc"], package_dir = {"iptc" : "iptc"}, - ext_modules = [Extension("libxtwrapper", ["libxtwrapper/wrapper.c"])], + ext_modules = [Extension( + "libxtwrapper", + ["libxtwrapper/wrapper.c"], + extra_compile_args = ["-g", "-O0"], + extra_link_args = ["-g", "-O0"] + )], classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", From fd4960020a2f5a3f59a7c8ad7dd22fb295a38ed5 Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sat, 1 Sep 2012 21:41:19 +1000 Subject: [PATCH 3/4] Removed print statements. --- iptc/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/iptc/__init__.py b/iptc/__init__.py index d93017e..93e6de5 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -508,7 +508,6 @@ def reset(self): Parameters are set to their default value, any flags are cleared.""" - print self.size ct.memset(ct.byref(self._match_buf), 0, self.size) self._update_pointers() m = self._ptr[0] @@ -567,10 +566,8 @@ def __init__(self, rule, name=None, target=None, revision=0): if TABLE_FILTER.is_chain(name): is_standard_target = True module = _xt.find_target('standard') - print "in standard module" else: module = _xt.find_target(name) - print "in extended module " + name if not module: raise XTablesError("can't find target %s" % (name)) self._module = module[0] From f54e399830908e41badaea44f0640aee861bcdec Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sat, 1 Sep 2012 22:01:39 +1000 Subject: [PATCH 4/4] Added support for multiple tables in Target class. Fixed Target so that it accepts a table parameter. Fixed native exceptions in new x6 support code. --- iptc/__init__.py | 11 ++++++++--- libxtwrapper/wrapper.c | 28 ++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/iptc/__init__.py b/iptc/__init__.py index 93e6de5..8d3cd8f 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -544,7 +544,7 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string "" should be used. """ - def __init__(self, rule, name=None, target=None, revision=0): + def __init__(self, rule, name=None, target=None, revision=0, table=None): """ *rule* is the Rule object this match belongs to; it can be changed later via *set_rule()*. *name* is the name of the iptables target @@ -558,12 +558,17 @@ def __init__(self, rule, name=None, target=None, revision=0): raise ValueError("can't create target based on nothing") if name == None: name = target.u.user.name + if table == None: + if target == None: + table = TABLE_FILTER + else: + raise ValueError("table must be specified if target provided") self._name = name self._rule = rule self._revision = revision is_standard_target = False - if TABLE_FILTER.is_chain(name): + if table.is_chain(name): is_standard_target = True module = _xt.find_target('standard') else: @@ -1064,7 +1069,7 @@ def _set_rule(self, entry): target = ct.cast(ct.byref(entry, entry.target_offset), ct.POINTER(ipt_entry_target))[0] - self.target = Target(self, target=target) + self.target = Target(self, target=target, table=self.chain.table) jump = self.chain.table.get_target(entry) # standard target is special if jump: self._target.standard_target = jump diff --git a/libxtwrapper/wrapper.c b/libxtwrapper/wrapper.c index c18b4a7..63d89ee 100644 --- a/libxtwrapper/wrapper.c +++ b/libxtwrapper/wrapper.c @@ -63,16 +63,32 @@ void wrap_save(int (*fn)(), const struct ipt_ip *ip, const void *m) int wrap_mpcall(unsigned int c, char **argv, int inv, struct xtables_match *m, struct xt_entry_match *mm, void *fw) { - m->m = mm; // FIXME: See above. - xtables_option_mpcall(c, argv, inv, m, fw); - return 1; /* true */ + int rv = 1; + int err; + + if ((err = setjmp(env)) == 0) { + m->m = mm; // FIXME: See above. + xtables_option_mpcall(c, argv, inv, m, fw); + } else { + errno = err; + } + + return rv; } int wrap_tpcall(unsigned int c, char **argv, int inv, struct xtables_target *t, struct xt_entry_target *tt, void *fw) { - t->t = tt; // FIXME: See above. - xtables_option_tpcall(c, argv, inv, t, fw); - return 1; /* true */ + int rv = 1; + int err; + + if ((err = setjmp(env)) == 0) { + t->t = tt; // FIXME: See above. + xtables_option_tpcall(c, argv, inv, t, fw); + } else { + errno = err; + } + + return rv; }