【发布时间】:2021-03-20 11:17:39
【问题描述】:
我正在尝试为 Ubuntu Linux 构建可加载内核模块 (LKM) 以阻止一系列 IP 地址。我的测试地址范围是 172.217.13.68/24。我尝试将 iphdr->saddr 与该范围的网络和广播地址进行比较。网络和广播地址最初以字符形式提供,但随后被强制转换为 unsigned int。当我们对范围使用这种方法时,所有的网络流量数据包都会被阻止,而不是那些在范围内的数据包。有关如何进行比较的任何建议?
我的代码: 语言 C
static struct nf_hook_ops nfho; //struct holding set of hook function options
//function to be called by hook
unsigned int hook_func(
void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state
) {
struct iphdr* iph;
static unsigned char *blockIPLow = "\xAC\xD9\x00\x00";
static unsigned char *blockIPHigh = "\xAC\xD9\x00\xFF";
if(skb){
iph = ip_hdr(skb);
if(iph && (iph->saddr>= *(unsigned int*)blockIPLow) && (iph->saddr<= *(unsigned int*)blockIPHigh))
{
printk(KERN_INFO "dropRange.c -- hook_func() dropped packets in range\n");
return NF_DROP;
}
}
/////////////////////
// ACCEPT ALL OTHER PACKETS
/////////////////////
return NF_ACCEPT;
}
更新:这个确切的代码适用于小范围的 IP。
【问题讨论】:
标签: c linux firewall kernel-module ipv4