Linux中生成testtcp.ko模块代码怎么写

发布时间:2021-10-13 11:28:29 作者:柒染
来源:亿速云 阅读:118

这期内容当中小编将会给大家带来有关Linux中生成testtcp.ko模块代码怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

生成testtcp.ko模块,添加到内核。

添加该模块后,每个由该机器发出的数据包,如果是TCP协议,且源端口为81,将其改为RST包发出。

一、代码

1.1 文件:testtcp_main.c

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/inetdevice.h>
#include <linux/string.h>
#include <net/route.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/checksum.h>
#include <net/tcp.h>
#include <net/ip.h>

unsigned int hook_mark1(unsigned int hooknum, struct sk_buff *skb,
                        const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))

{
    struct iphdr *iph;
    struct tcphdr *tcph;
    struct sk_buff *sk = skb;

    u16 src_port,dst_port;
    u16 datalen;

    iph = ip_hdr(sk);
    tcph = (struct udphdr*)((u_int8_t*)iph + (iph->ihl << 2));
    src_port = ntohs(tcph->source);
    dst_port = ntohs(tcph->dest);
    
    if(src_port == 81 || dst_port == 81)
    	printk("<0>""src_port:%d, dst_port:%d, protocol:%d, rst:%d\n",src_port, dst_port, iph->protocol, tcph->rst);

    if (iph->protocol == 6 && src_port == 81)
    {  
        printk("<0>""---000---src_port:%d, dst_port:%d, protocol:%d, rst:%d\n",src_port, dst_port, iph->protocol, tcph->rst);

	tcph->rst = 1;

        iph->check = 0;
        iph->check = ip_fast_csum((unsigned char*)iph, iph->ihl);

        datalen = ntohs(iph->tot_len) - (iph->ihl << 2);
        tcph->check = 0;
        tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
                                            iph->protocol, csum_partial((unsigned char*)tcph,  datalen, 0));
        skb->ip_summed = CHECKSUM_NONE;
        
        return NF_ACCEPT;
    }

    return NF_ACCEPT;
}


static struct nf_hook_ops nfho_marker1;

static int init_marker(void)
{    
    nfho_marker1.hook=hook_mark1;
    nfho_marker1.hooknum=NF_INET_LOCAL_OUT;
    nfho_marker1.pf=PF_INET;
    nfho_marker1.priority=NF_IP_PRI_LAST;
    nf_register_hook(&nfho_marker1);

    return 0;
}

static void exit_marker(void)
{
    nf_unregister_hook(&nfho_marker1);       
}


module_init(init_marker);
module_exit(exit_marker);

1.2 文件Makefile:

obj-m := testtcp.o
testtcp-objs := testtcp_main.o

KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
      $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:  
      $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
install:
      cp inerdns.ko ../

注意事项:

      1. 第一行的testtcp.o 与第二行的testtcp_main.o  不能重复。

      2. 第一行的testtcp.o 与第二行的testtcp-objs  前缀必须相同。

      3. “default:”、“clean:  ”、“install:” 下一行的内容,行前面必须有tab键。

二、编译、添加模块到内核

2.1 编译

      执行make,即可编译代码,并生产模块testtcp.ko。

2.2 添加模块到内核

      lsmod                          查看linux内核模块。

      insmod testtcp.ko     将testtcp.ko模块添加到内核。

      (rmmod testtcp           从内核中删除testtcp.ko模块。)

三、测试模块功能

3.1 测试代码

     可以参照以下文章代码修改:http://blog.csdn.net/guowenyan001/article/details/11742621

3.2 linux下访问URL

      curl 192.168.9.200:81 

3.3 抓包查看

      用tcpdump抓包查看,相关数据包是否已经被修改为RST包。

四、注意事项

      内核模块代码,可能会造成系统崩溃,需要重启,所以最好在测试机上测试内核代码。

上述就是小编为大家分享的Linux中生成testtcp.ko模块代码怎么写了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 尝试着写自己的代码生成器
  2. html中怎么写空格代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:常用元素默认margin和padding值的示例分析

下一篇:如何生成AWR报告

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》