From a8c32c307ae1959cbb645def3a9a2a972af699aa Mon Sep 17 00:00:00 2001 From: Lourival Vieira Neto Date: Fri, 4 Jul 2014 21:21:05 -0300 Subject: [PATCH 5/5] added npf_ext_lua --- distrib/sets/lists/base/ad.arm | 3 + distrib/sets/lists/base/ad.mips | 6 + distrib/sets/lists/base/ad.powerpc | 3 + distrib/sets/lists/base/md.amd64 | 3 + distrib/sets/lists/base/md.sparc64 | 3 + distrib/sets/lists/base/shl.mi | 3 + distrib/sets/lists/debug/ad.arm | 1 + distrib/sets/lists/debug/ad.mips | 4 + distrib/sets/lists/debug/ad.powerpc | 1 + distrib/sets/lists/debug/md.amd64 | 1 + distrib/sets/lists/debug/md.sparc64 | 1 + distrib/sets/lists/debug/shl.mi | 1 + distrib/sets/lists/modules/md.evbppc.powerpc | 4 + distrib/sets/lists/modules/mi | 2 + lib/npf/Makefile | 2 +- lib/npf/ext_lua/Makefile | 2 + lib/npf/ext_lua/npfext_lua.c | 84 ++++++++++++ lib/npf/ext_lua/shlib_version | 2 + sys/modules/Makefile | 1 + sys/modules/npf_ext_lua/Makefile | 14 ++ sys/net/npf/npf_ext_lua.c | 185 +++++++++++++++++++++++++++ 21 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 lib/npf/ext_lua/Makefile create mode 100644 lib/npf/ext_lua/npfext_lua.c create mode 100644 lib/npf/ext_lua/shlib_version create mode 100644 sys/modules/npf_ext_lua/Makefile create mode 100644 sys/net/npf/npf_ext_lua.c diff --git a/distrib/sets/lists/base/ad.arm b/distrib/sets/lists/base/ad.arm index 0f496ea..264f909 100644 --- a/distrib/sets/lists/base/ad.arm +++ b/distrib/sets/lists/base/ad.arm @@ -4,6 +4,9 @@ ./lib/oabi/npf/ext_log.so base-npf-shlib compat,pic ./lib/oabi/npf/ext_log.so.0 base-npf-shlib compat,pic ./lib/oabi/npf/ext_log.so.0.0 base-npf-shlib compat,pic +./lib/oabi/npf/ext_lua.so base-npf-shlib compat,pic +./lib/oabi/npf/ext_lua.so.0 base-npf-shlib compat,pic +./lib/oabi/npf/ext_lua.so.0.0 base-npf-shlib compat,pic ./lib/oabi/npf/ext_normalize.so base-npf-shlib compat,pic ./lib/oabi/npf/ext_normalize.so.0 base-npf-shlib compat,pic ./lib/oabi/npf/ext_normalize.so.0.0 base-npf-shlib compat,pic diff --git a/distrib/sets/lists/base/ad.mips b/distrib/sets/lists/base/ad.mips index 1179ccc..9057746 100644 --- a/distrib/sets/lists/base/ad.mips +++ b/distrib/sets/lists/base/ad.mips @@ -4,6 +4,9 @@ ./lib/64/npf/ext_log.so base-npf-shlib compat,pic,arch64 ./lib/64/npf/ext_log.so.0 base-npf-shlib compat,pic,arch64 ./lib/64/npf/ext_log.so.0.0 base-npf-shlib compat,pic,arch64 +./lib/64/npf/ext_lua.so base-npf-shlib compat,pic,arch64 +./lib/64/npf/ext_lua.so.0 base-npf-shlib compat,pic,arch64 +./lib/64/npf/ext_lua.so.0.0 base-npf-shlib compat,pic,arch64 ./lib/64/npf/ext_normalize.so base-npf-shlib compat,pic,arch64 ./lib/64/npf/ext_normalize.so.0 base-npf-shlib compat,pic,arch64 ./lib/64/npf/ext_normalize.so.0.0 base-npf-shlib compat,pic,arch64 @@ -15,6 +18,9 @@ ./lib/o32/npf/ext_log.so base-npf-shlib compat,pic,arch64 ./lib/o32/npf/ext_log.so.0 base-npf-shlib compat,pic,arch64 ./lib/o32/npf/ext_log.so.0.0 base-npf-shlib compat,pic,arch64 +./lib/o32/npf/ext_lua.so base-npf-shlib compat,pic,arch64 +./lib/o32/npf/ext_lua.so.0 base-npf-shlib compat,pic,arch64 +./lib/o32/npf/ext_lua.so.0.0 base-npf-shlib compat,pic,arch64 ./lib/o32/npf/ext_normalize.so base-npf-shlib compat,pic,arch64 ./lib/o32/npf/ext_normalize.so.0 base-npf-shlib compat,pic,arch64 ./lib/o32/npf/ext_normalize.so.0.0 base-npf-shlib compat,pic,arch64 diff --git a/distrib/sets/lists/base/ad.powerpc b/distrib/sets/lists/base/ad.powerpc index f5509b0..65768bb 100644 --- a/distrib/sets/lists/base/ad.powerpc +++ b/distrib/sets/lists/base/ad.powerpc @@ -4,6 +4,9 @@ ./lib/powerpc/npf/ext_log.so base-npf-shlib compat,arch64,pic ./lib/powerpc/npf/ext_log.so.0 base-npf-shlib compat,arch64,pic ./lib/powerpc/npf/ext_log.so.0.0 base-npf-shlib compat,arch64,pic +./lib/powerpc/npf/ext_lua.so base-npf-shlib compat,arch64,pic +./lib/powerpc/npf/ext_lua.so.0 base-npf-shlib compat,arch64,pic +./lib/powerpc/npf/ext_lua.so.0.0 base-npf-shlib compat,arch64,pic ./lib/powerpc/npf/ext_normalize.so base-npf-shlib compat,arch64,pic ./lib/powerpc/npf/ext_normalize.so.0 base-npf-shlib compat,arch64,pic ./lib/powerpc/npf/ext_normalize.so.0.0 base-npf-shlib compat,arch64,pic diff --git a/distrib/sets/lists/base/md.amd64 b/distrib/sets/lists/base/md.amd64 index d4dcb75..633ab88 100644 --- a/distrib/sets/lists/base/md.amd64 +++ b/distrib/sets/lists/base/md.amd64 @@ -6,6 +6,9 @@ ./lib/i386/npf/ext_log.so base-npf-shlib compat,pic ./lib/i386/npf/ext_log.so.0 base-npf-shlib compat,pic ./lib/i386/npf/ext_log.so.0.0 base-npf-shlib compat,pic +./lib/i386/npf/ext_lua.so base-npf-shlib compat,pic +./lib/i386/npf/ext_lua.so.0 base-npf-shlib compat,pic +./lib/i386/npf/ext_lua.so.0.0 base-npf-shlib compat,pic ./lib/i386/npf/ext_normalize.so base-npf-shlib compat,pic ./lib/i386/npf/ext_normalize.so.0 base-npf-shlib compat,pic ./lib/i386/npf/ext_normalize.so.0.0 base-npf-shlib compat,pic diff --git a/distrib/sets/lists/base/md.sparc64 b/distrib/sets/lists/base/md.sparc64 index 668aaa5..ea32e50 100644 --- a/distrib/sets/lists/base/md.sparc64 +++ b/distrib/sets/lists/base/md.sparc64 @@ -4,6 +4,9 @@ ./lib/sparc/npf/ext_log.so base-npf-shlib compat,pic ./lib/sparc/npf/ext_log.so.0 base-npf-shlib compat,pic ./lib/sparc/npf/ext_log.so.0.0 base-npf-shlib compat,pic +./lib/sparc/npf/ext_lua.so base-npf-shlib compat,pic +./lib/sparc/npf/ext_lua.so.0 base-npf-shlib compat,pic +./lib/sparc/npf/ext_lua.so.0.0 base-npf-shlib compat,pic ./lib/sparc/npf/ext_normalize.so base-npf-shlib compat,pic ./lib/sparc/npf/ext_normalize.so.0 base-npf-shlib compat,pic ./lib/sparc/npf/ext_normalize.so.0.0 base-npf-shlib compat,pic diff --git a/distrib/sets/lists/base/shl.mi b/distrib/sets/lists/base/shl.mi index 796e642..f184e5c 100644 --- a/distrib/sets/lists/base/shl.mi +++ b/distrib/sets/lists/base/shl.mi @@ -85,6 +85,9 @@ ./lib/npf/ext_log.so base-npf-shlib npf ./lib/npf/ext_log.so.0 base-npf-shlib npf ./lib/npf/ext_log.so.0.0 base-npf-shlib npf +./lib/npf/ext_lua.so base-npf-shlib npf +./lib/npf/ext_lua.so.0 base-npf-shlib npf +./lib/npf/ext_lua.so.0.0 base-npf-shlib npf ./lib/npf/ext_normalize.so base-npf-shlib npf ./lib/npf/ext_normalize.so.0 base-npf-shlib npf ./lib/npf/ext_normalize.so.0.0 base-npf-shlib npf diff --git a/distrib/sets/lists/debug/ad.arm b/distrib/sets/lists/debug/ad.arm index 83f0a69..f49993f 100644 --- a/distrib/sets/lists/debug/ad.arm +++ b/distrib/sets/lists/debug/ad.arm @@ -17,6 +17,7 @@ ./usr/libdata/debug/lib/oabi -unknown- debug,compat ./usr/libdata/debug/lib/oabi/npf -unknown- debug,compat ./usr/libdata/debug/lib/oabi/npf/ext_log.so.0.0.debug comp-npf-debug debug,compat +./usr/libdata/debug/lib/oabi/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/oabi/npf/ext_normalize.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/oabi/npf/ext_rndblock.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/libexec/ld.elf_so-oabi.debug comp-sys-debug debug,compat diff --git a/distrib/sets/lists/debug/ad.mips b/distrib/sets/lists/debug/ad.mips index eaa0da4..7bb4221 100644 --- a/distrib/sets/lists/debug/ad.mips +++ b/distrib/sets/lists/debug/ad.mips @@ -26,9 +26,13 @@ ./usr/lib/o32/libppath_g.a comp-c-debuglib compat,debuglib,arch64 ./usr/lib/o32/libwind_g.a comp-c-debuglib compat,debuglib,kerberos,arch64 ./usr/libdata/debug/lib/64/npf/ext_log.so.0.0.debug comp-npf-debug debug,compat,arch64 +./usr/libdata/debug/lib/64/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat,arch64 +./usr/libdata/debug/lib/64/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/lib/64/npf/ext_normalize.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/lib/64/npf/ext_rndblock.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/lib/o32/npf/ext_log.so.0.0.debug comp-npf-debug debug,compat,arch64 +./usr/libdata/debug/lib/o32/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat,arch64 +./usr/libdata/debug/lib/o32/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/lib/o32/npf/ext_normalize.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/lib/o32/npf/ext_rndblock.so.0.0.debug comp-npf-debug debug,compat,arch64 ./usr/libdata/debug/libexec/ld.elf_so-64.debug comp-sys-debug debug,compat,arch64 diff --git a/distrib/sets/lists/debug/ad.powerpc b/distrib/sets/lists/debug/ad.powerpc index c09353a..920afa3 100644 --- a/distrib/sets/lists/debug/ad.powerpc +++ b/distrib/sets/lists/debug/ad.powerpc @@ -115,6 +115,7 @@ ./usr/lib/powerpc/liby_g.a comp-c-debuglib compat,arch64,debuglib ./usr/lib/powerpc/libz_g.a comp-c-debuglib compat,arch64,debuglib ./usr/libdata/debug/lib/powerpc/npf/ext_log.so.0.0.debug comp-npf-debug compat,arch64,debug +./usr/libdata/debug/lib/powerpc/npf/ext_lua.so.0.0.debug comp-npf-debug compat,arch64,debug ./usr/libdata/debug/lib/powerpc/npf/ext_normalize.so.0.0.debug comp-npf-debug compat,arch64,debug ./usr/libdata/debug/lib/powerpc/npf/ext_rndblock.so.0.0.debug comp-npf-debug compat,arch64,debug ./usr/libdata/debug/libexec/ld.elf_so-powerpc.debug comp-sys-debug compat,arch64,debug diff --git a/distrib/sets/lists/debug/md.amd64 b/distrib/sets/lists/debug/md.amd64 index 2ce95fd..8fd690f 100644 --- a/distrib/sets/lists/debug/md.amd64 +++ b/distrib/sets/lists/debug/md.amd64 @@ -159,6 +159,7 @@ ./usr/lib/i386/libz_g.a comp-c-debuglib compat,debuglib ./usr/lib/libx86_64_g.a comp-c-debuglib debuglib ./usr/libdata/debug/lib/i386/npf/ext_log.so.0.0.debug comp-npf-debug debug,compat +./usr/libdata/debug/lib/i386/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/i386/npf/ext_normalize.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/i386/npf/ext_rndblock.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/libexec/ld.elf_so-i386.debug comp-sys-debug debug,compat diff --git a/distrib/sets/lists/debug/md.sparc64 b/distrib/sets/lists/debug/md.sparc64 index d2c0c37..e6d73d1 100644 --- a/distrib/sets/lists/debug/md.sparc64 +++ b/distrib/sets/lists/debug/md.sparc64 @@ -157,6 +157,7 @@ ./usr/lib/sparc/liby_g.a comp-c-debuglib compat,debuglib ./usr/lib/sparc/libz_g.a comp-c-debuglib compat,debuglib ./usr/libdata/debug/lib/sparc/npf/ext_log.so.0.0.debug comp-npf-debug debug,compat +./usr/libdata/debug/lib/sparc/npf/ext_lua.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/sparc/npf/ext_normalize.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/lib/sparc/npf/ext_rndblock.so.0.0.debug comp-npf-debug debug,compat ./usr/libdata/debug/libexec/ld.elf_so-sparc.debug comp-sys-debug debug,compat diff --git a/distrib/sets/lists/debug/shl.mi b/distrib/sets/lists/debug/shl.mi index e926800..f494984 100644 --- a/distrib/sets/lists/debug/shl.mi +++ b/distrib/sets/lists/debug/shl.mi @@ -21,6 +21,7 @@ ./usr/libdata/debug/lib/libutil.so.7.21.debug comp-sys-debug debug ./usr/libdata/debug/lib/libz.so.1.0.debug comp-sys-debug debug ./usr/libdata/debug/lib/npf/ext_log.so.0.0.debug comp-sys-debug debug,npf +./usr/libdata/debug/lib/npf/ext_lua.so.0.0.debug comp-sys-debug debug,npf ./usr/libdata/debug/lib/npf/ext_normalize.so.0.0.debug comp-sys-debug debug,npf ./usr/libdata/debug/lib/npf/ext_rndblock.so.0.0.debug comp-sys-debug debug,npf ./usr/libdata/debug/libexec/ld.elf_so.debug comp-sys-debug debug diff --git a/distrib/sets/lists/modules/md.evbppc.powerpc b/distrib/sets/lists/modules/md.evbppc.powerpc index 605b4ad9..f3c9003 100644 --- a/distrib/sets/lists/modules/md.evbppc.powerpc +++ b/distrib/sets/lists/modules/md.evbppc.powerpc @@ -148,6 +148,8 @@ ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_alg_icmp/npf_alg_icmp.kmod base-kernel-modules kmod,compatmodules ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_log base-kernel-modules kmod ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_log/npf_ext_log.kmod base-kernel-modules kmod +./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_lua base-kernel-modules kmod +./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_lua/npf_ext_lua.kmod base-kernel-modules kmod ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_normalise base-obsolete obsolete ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_normalise/npf_ext_normalise.kmod base-obsolete obsolete ./stand/powerpc-4xx/@OSRELEASE@/modules/npf_ext_normalize base-kernel-modules kmod @@ -385,6 +387,8 @@ ./stand/powerpc-booke/@OSRELEASE@/modules/npf_alg_icmp/npf_alg_icmp.kmod base-kernel-modules kmod,compatmodules ./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_log base-kernel-modules kmod ./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_log/npf_ext_log.kmod base-kernel-modules kmod +./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_lua base-kernel-modules kmod +./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_lua/npf_ext_lua.kmod base-kernel-modules kmod ./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_normalise base-obsolete obsolete ./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_normalise/npf_ext_normalise.kmod base-kobsolete obsoleteernel-modules kmod ./stand/powerpc-booke/@OSRELEASE@/modules/npf_ext_normalize base-kernel-modules kmod diff --git a/distrib/sets/lists/modules/mi b/distrib/sets/lists/modules/mi index 632c547..904d12c 100644 --- a/distrib/sets/lists/modules/mi +++ b/distrib/sets/lists/modules/mi @@ -150,6 +150,8 @@ ./@MODULEDIR@/npf_alg_icmp/npf_alg_icmp.kmod base-kernel-modules kmod ./@MODULEDIR@/npf_ext_log base-kernel-modules kmod ./@MODULEDIR@/npf_ext_log/npf_ext_log.kmod base-kernel-modules kmod +./@MODULEDIR@/npf_ext_lua base-kernel-modules kmod +./@MODULEDIR@/npf_ext_lua/npf_ext_lua.kmod base-kernel-modules kmod ./@MODULEDIR@/npf_ext_normalise base-obsolete obsolete ./@MODULEDIR@/npf_ext_normalise/npf_ext_normalise.kmod base-obsolete obsolete ./@MODULEDIR@/npf_ext_normalize base-kernel-modules kmod diff --git a/lib/npf/Makefile b/lib/npf/Makefile index 1e2b3c3..d23a7e2 100644 --- a/lib/npf/Makefile +++ b/lib/npf/Makefile @@ -4,7 +4,7 @@ .if ${MKPIC} != "no" -SUBDIR= ext_log ext_normalize ext_rndblock +SUBDIR= ext_log ext_lua ext_normalize ext_rndblock .endif diff --git a/lib/npf/ext_lua/Makefile b/lib/npf/ext_lua/Makefile new file mode 100644 index 0000000..ac29881 --- /dev/null +++ b/lib/npf/ext_lua/Makefile @@ -0,0 +1,2 @@ +MOD= ext_lua +.include "${.CURDIR}/../mod.mk" diff --git a/lib/npf/ext_lua/npfext_lua.c b/lib/npf/ext_lua/npfext_lua.c new file mode 100644 index 0000000..43a9903 --- /dev/null +++ b/lib/npf/ext_lua/npfext_lua.c @@ -0,0 +1,84 @@ +/*- + * Copyright (c) 2014, Lourival Vieira Neto . + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID(""); + +#include +#include +#include +#include + +#include + +int npfext_lua_init(void); +nl_ext_t * npfext_lua_construct(const char *); +int npfext_lua_param(nl_ext_t *, const char *, const char *); + +int +npfext_lua_init(void) +{ + /* Nothing to initialise. */ + return 0; +} + +nl_ext_t * +npfext_lua_construct(const char *name) +{ + assert(strcmp(name, "lua") == 0); + return npf_ext_construct(name); +} + +int +npfext_lua_param(nl_ext_t *ext, const char *param, const char *val) +{ + enum ptype { PARAM_STRING }; + static const struct param { + const char * name; + enum ptype type; + } params[] = { + { "call", PARAM_STRING }, + }; + + if (val == NULL) { + return EINVAL; + } + for (unsigned i = 0; i < __arraycount(params); i++) { + const char *name = params[i].name; + + if (strcmp(name, param) != 0) { + continue; + } + + assert(params[i].type == PARAM_STRING); + npf_ext_param_string(ext, name, val); + return 0; + } + + /* Invalid parameter, if not found. */ + return EINVAL; +} diff --git a/lib/npf/ext_lua/shlib_version b/lib/npf/ext_lua/shlib_version new file mode 100644 index 0000000..97c9f92 --- /dev/null +++ b/lib/npf/ext_lua/shlib_version @@ -0,0 +1,2 @@ +major=0 +minor=0 diff --git a/sys/modules/Makefile b/sys/modules/Makefile index d1275fa..cff209e 100644 --- a/sys/modules/Makefile +++ b/sys/modules/Makefile @@ -65,6 +65,7 @@ SUBDIR+= nilfs SUBDIR+= npf SUBDIR+= npf_alg_icmp SUBDIR+= npf_ext_log +SUBDIR+= npf_ext_lua SUBDIR+= npf_ext_normalize SUBDIR+= npf_ext_rndblock SUBDIR+= ntfs diff --git a/sys/modules/npf_ext_lua/Makefile b/sys/modules/npf_ext_lua/Makefile new file mode 100644 index 0000000..c4c2a95 --- /dev/null +++ b/sys/modules/npf_ext_lua/Makefile @@ -0,0 +1,14 @@ +.include "../Makefile.inc" + +.PATH: ${S}/net/npf + +KMOD= npf_ext_lua + +SRCS= npf_ext_lua.c + +CPPFLAGS+= -I${S}/../external/mit/lua/dist/src \ + -I${S}/modules/lua \ + -I${S}/sys \ + -I${S}/../lib/lua/data + +.include diff --git a/sys/net/npf/npf_ext_lua.c b/sys/net/npf/npf_ext_lua.c new file mode 100644 index 0000000..7e5a214 --- /dev/null +++ b/sys/net/npf/npf_ext_lua.c @@ -0,0 +1,185 @@ +/*- + * Copyright (c) 2014, Lourival Vieira Neto . + * Copyright (c) 2009-2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__KERNEL_RCSID(0, ""); + +#include +#include +#include +#include +#include + +#include + +#include + +#include + +#include +#include +#include +#include + +#include "npf.h" + +/* + * NPF extension module definition and the identifier. + */ +NPF_EXT_MODULE(npf_ext_lua, "lua,luadata"); + +#define NPFEXT_LUA_VER 1 + +static void *npf_ext_lua_id; +static klua_State *K; + +/* + * Lua parameters. + */ +typedef struct { + klua_State *K; + char function[MAX_LUA_NAME]; +} npf_lua_parm_t; + +/* + * npf_lua_ctor: a constructor for the Lua rule procedure. + */ +static int +npf_lua_ctor(npf_rproc_t *rp, prop_dictionary_t params) +{ + npf_lua_parm_t *lp; + const char *function; + + if (!prop_dictionary_get_cstring_nocopy(params, "call", &function)) + return EINVAL; + + lp = kmem_zalloc(sizeof(npf_lua_parm_t), KM_SLEEP); + + strncpy(lp->function, function, MAX_LUA_NAME); + lp->function[MAX_LUA_NAME - 1] = '\0'; + + lp->K = K; + + npf_rproc_assign(rp, lp); + + return 0; +} + +/* + * npf_lua_dtor: a destructor for the Lua rule procedure. + */ +static void +npf_lua_dtor(npf_rproc_t *rp, void *params) +{ + kmem_free(params, sizeof(npf_lua_parm_t)); +} + +/* + * npf_lua: the main routine of the Lua rule procedure. + */ +static bool +npf_lua(npf_cache_t *npc, void *params, int *decision) +{ + npf_lua_parm_t *lp = params; + struct mbuf *m = nbuf_head_mbuf(npc->npc_nbuf); + + klua_lock(K); + lua_State *L = lp->K->L; + + if (lua_getglobal(L, lp->function) != LUA_TFUNCTION) + goto no_function; + + /* create a new data object */ + int data = ldata_newref_chain(L, m); + if (data == LUA_REFNIL) + goto no_data; + + if (lua_pcall(L, 1, 1, 0) != LUA_OK) { + printf("npf_lua: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + + /* just drop the packet */ + *decision = 0; + + goto lua_error; + } + + *decision = lua_toboolean(L, -1); + +lua_error: + /* unregister the data object */ + ldata_unref(L, data); +no_function: +no_data: + klua_unlock(K); + return true; +} + +static int +npf_ext_lua_modcmd(modcmd_t cmd, void *arg) +{ + static const npf_ext_ops_t npf_lua_ops = { + .version = NPFEXT_LUA_VER, + .ctx = NULL, + .ctor = npf_lua_ctor, + .dtor = npf_lua_dtor, + .proc = npf_lua + }; + + switch (cmd) { + case MODULE_CMD_INIT: + /* Register the Lua rule procedure. */ + npf_ext_lua_id = npf_ext_register("lua", &npf_lua_ops); + if (!npf_ext_lua_id) + return EEXIST; + + K = kluaL_newstate("npf", "", IPL_SOFTNET); + if (K == NULL) + return ENOTTY; + + klua_lock(K); + luaL_openlibs(K->L); + klua_unlock(K); + return 0; + + case MODULE_CMD_FINI: + /* Unregister the Lua rule procedure. */ + if (K != NULL) + klua_close(K); + + return npf_ext_unregister(npf_ext_lua_id); + + case MODULE_CMD_AUTOUNLOAD: + /* Allow auto-unload only if NPF permits it. */ + return npf_autounload_p() ? 0 : EBUSY; + + default: + return ENOTTY; + } + return 0; +} + -- 1.8.5.5