netfilter例2:改ip报文头部里的ip地址

本例对ip报文的报头源地址做了修改,然后转发。

文件名:change_srcip.c

/*
* this programe is working as plugin of netfilter which will change the source address
* of ipv6 packets.
*/


#include <linux/netfilter.h>
#include <linux/netfilter_ipv6.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <net/ipv6.h>
#include <linux/time.h>

#include <asm/byteorder.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("newand");
MODULE_DESCRIPTION("Change the address of ipv6 packet");

#define PRINT(fmt,args...) printk(", " fmt, ##args)


/* IP6 Hooks */
/* After promisc drops#include <asm/byteorder.h>
, checksum checks. */
#define NF_IP6_PRE_ROUTING  0
/* If the packet is destined for this box. */
#define NF_IP6_LOCAL_IN     1
/* If the packet is destined for another interface. */
#define NF_IP6_FORWARD      2
/* Packets coming from a local process. */
#define NF_IP6_LOCAL_OUT        3
/* Packets about to hit the wire. */
#define NF_IP6_POST_ROUTING 4

/*
* Hook deal with the the ipv6 packets, modify ip address
*/
static unsigned int 
ip6_modify_addr(unsigned int hooknum,
				struct sk_buff *skb,
				const struct net_device *in,
				const struct net_device *out,
				int (*okfn)(struct sk_buff*))
{
    struct sk_buff *sk = skb;
	struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);//header

    if(ip6_hdr->version == 6)
    {
        struct in6_addr destip = ip6_hdr->daddr;//destination ip
        //specify the ipv6 address that need block
        if(destip.s6_addr[0] == 0xff && destip.s6_addr[1] == 0x15)
        {
            //if it match the condition then drop it
            ip6_hdr->saddr.s6_addr[1] = 0x02;
            //2001-〉2002.change the source addr from 2001:xxxx...
            //to 2002:xxxx...  
        }
    }
    
    return NF_ACCEPT;      
}

/*Initialize the hook*/
static struct nf_hook_ops nf_out_modify =
{
	.hook = ip6_modify_addr,
	.hooknum = NF_IP6_POST_ROUTING,
	.pf = PF_INET6,
	.priority = NF_IP6_PRI_FIRST,
};

/*Initialize the module*/
static int __init ip6_moaddr_init(void)
{
	int ret;
	ret = nf_register_hook(&nf_out_modify);
	PRINT("IPV6 address modify module init.\n");
	return 0; //success
}

/*Clear the module*/
static void __exit ip6_moaddr_exit(void)
{
	nf_unregister_hook(&nf_out_modify);
	PRINT("IPV6 address modify module exit.\n");
}

module_init(ip6_moaddr_init);
module_exit(ip6_moaddr_exit);

makefile:

#Makefile2.6
obj-m += change_destip.o        # 产生block_packet 模块的目标文件
CURRENT_PATH := $(shell pwd)   #模块所在的当前路径
LINUX_KERNEL := $(shell uname -r)    #Linux内核源代码的当前版本
LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL) #Linux内核源代码的绝对路径
all:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules   #编译模块了
clean:
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean    #清理


 


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux IP层的代码实现主要在`net/ipv4`目录下,下面是一些主要的源代码文件及其功能: 1. `ip_input.c`:IP数据包输入处理,包括IP首部校验、路由选择、分片重组等。 2. `ip_output.c`:IP数据包输出处理,包括路由选择、TTL递减、分片等。 3. `ip_fragment.c`:IP数据包分片处理。 4. `ip_forward.c`:IP数据包转发处理,包括路由选择、ARP解析、TTL递减、分片等。 5. `ip_route.c`:IP路由表管理,包括路由表项的添加、删除、修等操作。 6. `ipmr.c`:IP组播路由处理。 7. `tcp_ipv4.c`:TCP协议的IP层实现,包括TCP数据包输入处理和输出处理。 8. `udp.c`:UDP协议的IP层实现,包括UDP数据包输入处理和输出处理。 9. `icmp.c`:ICMP协议的IP层实现,包括ICMP数据包输入处理和输出处理。 10. `arp.c`:ARP协议的实现,包括ARP请求和应答的发送和接收。 11. `devinet.c`:设备接口的IP层实现,包括设备的IP地址和路由表的管理。 12. `netfilter_ipv4.c`:IP层的Netfilter框架实现,包括数据包的过滤、修等操作。 除了上述文件外,还有一些与IP层相关的文件,如`net/ipv4/inet_hashtable.c`、`net/ipv4/tcp_input.c`、`net/ipv4/udp_impl.h`等。这些文件都是为了支持IP层的实现而存在的。 总的来说,Linux IP层的代码实现非常庞大和复杂,但是非常高效和灵活,支持多种路由协议和高级路由功能,是Linux网络子系统的核心之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值