Sha*_*baz 4 linux kernel iptables netfilter ebtables
基本上,我想编写一个内核模块,为ebtables添加一个可能的过滤器.然后我需要告诉ebtables在我设置的桥上使用我的过滤器.
我需要编写自己的模块的原因是我想在连续的包之间引入延迟(出于某些测试原因).为了演示,我的网络最初有这样的流量:
+++-----------------+++-----------------+++-----------------+++-----------------
Run Code Online (Sandbox Code Playgroud)
其中+显示了包裹的流量,并且-表示没有包裹在线上.我想在它们之间放置一个桥接器,以便数据包的模式将改变为:
+----+----+---------+----+----+---------+----+----+---------+----+----+---------
Run Code Online (Sandbox Code Playgroud)
这意味着我会确保每个数据包到达之间会有一定的延迟.
现在我编写了以下简单的代码,我基本上从linux-source/net/bridge/netfilter/ebt_ip.c中获取:
static bool match(const struct sk_buff *skb, const struct xt_match_param *par)
{
printk(KERN_INFO"match called\n");
return true; // match everything!
}
static bool check(const struct xt_mtchk_param *par)
{
printk(KERN_INFO"check called\n");
return true; // pass everything!
}
static struct xt_match reg __read_mostly = {
.name = "any", // I made this up, but I tried also putting ip for example which didn't change anything.
.revision = 0,
.family = NFPROTO_BRIDGE,
.match = match,
.checkentry = check,
.matchsize = XT_ALIGN(4), // don't know what this is, so I just gave it an `int`
.me = THIS_MODULE
};
int init_module(void)
{
return xt_register_match(®);
}
void cleanup_module(void)
{
xt_unregister_match(®);
}
Run Code Online (Sandbox Code Playgroud)
我成功加载了模块.但就好像它不存在一样.我没有得到日志match和check功能所以桥梁显然没有考虑我的过滤器.我究竟做错了什么?
我已经尝试了许多加载我的过滤器的组合,首先设置桥接器或首先设置ebtables规则,但它们都没有改变任何东西.
PS桥本身有效.我确信ebtables也有效,因为如果我添加一个删除包的策略,我不会在最终的计算机上收到它们.我无法弄清楚的是如何告诉ebtables也考虑我的过滤器.
我得到了这个工作,而不是以最优雅的方式,但无论如何,我在这里写一个未来的流浪者:
假设你的过滤器名称是:"any"
您需要在ebtables源之外不可用的标头.因此,获取源代码,然后转到扩展文件夹.在Makefile文件,添加any到EXT_FUNC(即待建目标)和写入源文件ebt_any.c如下所示:
#include <stdio.h>
#include <getopt.h>
#include "../include/ebtables_u.h"
/*struct whatever
{
int a;
};*/
static struct option _any_opts[] =
{
{"use-any", required_argument, 0, 0},
{'\0'}
};
static void _any_help(void)
{
printf("any match options: nothing!\n");
}
static void _any_init(struct ebt_entry_match *match)
{
printf("any_init\n");
}
static void _any_check(const struct ebt_u_entry *entry, const struct ebt_entry_match *match, const char *name,
unsigned int hookmask, unsigned int time)
{
printf("any_check\n");
}
static int _any_parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, unsigned int *flags, struct ebt_entry_match **match)
{
printf("any_parse: %d\n", c);
if (c == 0)
return 1;
return 0; // return true for anything
}
static int _any_compare(const struct ebt_entry_match *m1, const struct ebt_entry_match *m2)
{
/* struct whatever *w1 = (struct whatever *)m1->data;
struct whatever *w2 = (struct whatever *)m2->data;
if (w1->a != w2->a)
return 0;*/
return 1;
}
static void _any_print(const struct ebt_u_entry *entry, const struct ebt_entry_match *match)
{
printf("any_print");
}
static struct ebt_u_match _reg = {
.name = "any",
// .size = sizeof(struct whatever),
.help = _any_help,
.init = _any_init,
.parse = _any_parse,
.final_check = _any_check,
.print = _any_print,
.compare = _any_compare,
.extra_ops = _any_opts,
};
void _init(void)
{
ebt_register_match(&_reg);
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您有从用户空间到内核空间的数据,请写一些内容而不是struct whatever.我已经评论过了,因为我没有使用任何东西.
注意:即使你的程序不需要一个选项(例如我的程序应该匹配所有内容),你仍需要提供一个选项,因为ebtables知道如何使用你的过滤器.
注意:其中一些函数似乎是不必要的,但是如果你不编写它们,就会出现"BUG:bad merge"错误.
内核空间模块更简单:
#include <linux/netfilter/x_tables.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shahbaz Youssefi");
MODULE_ALIAS("ebt_any");
/*struct whatever
{
int a;
};*/
static bool match(const struct sk_buff *skb, const struct xt_match_param *par)
{
printk(KERN_INFO"Matching\n");
return true;
}
static bool check(const struct xt_mtchk_param *par)
{
printk(KERN_INFO"Checking\n");
return true;
}
static struct xt_match reg __read_mostly = {
.name = "any",
.match = match,
// .matchsize = sizeof(struct whatever),
.checkentry = check,
.me = THIS_MODULE
};
int init_module(void)
{
int ret = 0;
printk("Bridge initializing...\n");
ret = xt_register_match(®);
printk("Bridge initializing...done!\n");
return ret;
}
void cleanup_module(void)
{
printk("Bridge exiting...\n");
xt_unregister_match(®);
printk("Bridge exiting...done!\n");
}
Run Code Online (Sandbox Code Playgroud)
注意:如果struct whatever在用户空间中使用,则必须在内核空间中使用相同的内容.
注意:与使用ebtables头文件/函数的用户空间插件不同,内核模块使用xtables代替!
编译模块(相当标准)并安装它以进行自动加载.或者,您可以在添加/删除ebtables规则之前自己insmod和rmmod模块.
只需添加包含的规则即可--use-any some_value.例如:
ebtables -A FORWARD --use-any 1 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
注意:这--use-any是在用户空间插件中option给出的ebt_u_match reg.extra_ops(在数组_any_opts中定义).
| 归档时间: |
|
| 查看次数: |
3090 次 |
| 最近记录: |