#include <linux/ip.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/inet.h>
#define print_addr(addr) printk(" %u.%u.%u.%u \n", \
((addr & 0xff000000) >> 24), ((addr & 0x00ff0000) >> 16), \
((addr & 0x0000ff00) >> 8), (addr & 0x000000ff))
MODULE_LICENSE("Dual BSD/GPL");
static struct nf_hook_ops nfho;
//钩子函数,注意参数格式与开发环境源码树保持一致
unsigned int hook_func(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct iphdr *iph = (struct iphdr *)(skb->data);
if(iph->protocol == 1){
struct iphdr *iph_mod = skb_push(skb,20);
memcpy(iph_mod,iph,20);
iph_mod->daddr = 1661970624;//192.168.15.99
iph_mod->saddr = 16843009;//1.1.1.1
iph_mod->tot_len = htons(ntohs(iph->tot_len) + 20);
skb_reset_network_header(skb);
struct iphdr *iphm = (struct iphdr *)(skb->data);
#if 1
printk("----------------in-in--in-in-----------------\n");
printk(" _ _ _ _ _ _ _ _IPv4 HDR _ _ _ _ _ _ _");
printk("\n");
printk("|version | iphdr_len | Tos | total len |");
printk("\n");
printk(" %u %u %u %u ",iphm->version,iphm->ihl,iphm->tos,ntohs(iphm->tot_len));
printk("\n");
printk("| identity | fragment offset |");
printk("\n");
printk(" %u %u ",ntohs(iphm->id),iphm->frag_off);
printk("\n");
printk("| ttl | protocol | check |\n");
printk(" %u %u %u ",iphm->ttl,iphm->protocol,ntohs(iphm->check));
printk("\n");
printk("| src_ip |\n");
print_addr(htonl(iphm->saddr));
printk("| dst_ip |\n");
print_addr(htonl(iphm->daddr));
printk("|---------------------------------------|\n");
#endif
}
return NF_ACCEPT;
}
static int __init hook_init(void)
{
nfho.hook = hook_func;//关联对应处理函数
nfho.hooknum = NF_INET_LOCAL_IN;//钩子函数挂载点
nfho.pf = PF_INET;//ipv4 ,所以用这个
nfho.priority = NF_IP_PRI_FIRST;//优先级,第一顺位,首先执行我们定义的函数
nf_register_hook(&nfho);//注册
return 0;
}
static void __exit hook_exit(void)
{
nf_unregister_hook(&nfho);//注销
}
module_init(hook_init);
module_exit(hook_exit);
添加 ip 头的方法有问题么?每次 insmod 模块后,都会死机是什么原因?
1
ShadowStar 2016-08-26 11:46:12 +08:00
哪个版本的内核?
死机的 panic 信息是什么? |
2
helloworld2010 2016-08-26 16:26:27 +08:00
dead lock?
|
3
likethewind OP @ShadowStar 3.19.0-25-generic 内核。 死机时大写锁定键(Caps Lock)不停闪烁。
|
4
likethewind OP @helloworld2010 貌似不是...........
|