美文网首页
如何在netfilter的内核模块中为每一个TCP打mark

如何在netfilter的内核模块中为每一个TCP打mark

作者: zhjwang | 来源:发表于2018-11-25 14:40 被阅读17次

我们都用过iptables中的set mark功能吧。那么如果说我们自己希望在内核模块中为每一个内核模块添加一个mark,然后根据这个mark在用户态做一些处理呢?

对于如何写一个netfilter就不做过多解释了。
我们来看看如何打mark,如下,我们为tcp的去12345的端口每个连接添加一个mark值为0x12345.

unsigned int send_hook_func(void *priv, struct sk_buff *skb,const struct nf_hook_state *state)
{
    struct iphdr *iph;
    struct tcphdr *tcph = NULL;
    unsigned int dport = 0;

    if (!skb) {
        return NF_ACCEPT;
    }
    iph = (struct iphdr *) skb_network_header(skb);
    if (!iph) {
        printk("no ip header\n");
        return NF_ACCEPT;
    }
    if(iph->protocol == IPPROTO_TCP)
    {
        tcph = (struct tcphdr *) ((__u32 *) iph + iph->ihl);
        dport = htons((unsigned short int)tcph->dest);
        if (tcph->syn && dport == 12345) 
        {
            printk("tcp dest port is:%d\n", dport);
            skb->mark = 0x12345;
            enum ip_conntrack_info ctinfo;
            struct nf_conn *ct;
            ct = nf_ct_get(skb, &ctinfo);
            if(ct != NULL)
            {
                u_int32_t newmark;
                newmark = 0x12345;
               if (ct->mark != newmark) {
                    ct->mark = newmark;
                    nf_conntrack_event_cache(IPCT_MARK, ct);
                } 
            }
        }
    }
    return NF_ACCEPT;
}

相关文章

  • 如何在netfilter的内核模块中为每一个TCP打mark

    我们都用过iptables中的set mark功能吧。那么如果说我们自己希望在内核模块中为每一个内核模块添加一个m...

  • Android iptables相关

    1 iptables的原理以及命令 首先linux集成了netfilter库,netfilter工作在tcp/ip...

  • iptables系列二

    iptables系列之基本应用及显式扩展 netfilter:Framework,TCP,内核中 iptables...

  • 在Ubuntu16.04上启用TCP-BBR

    [toc] BBR简介 BBR 是 Google 推出的一个「TCP 拥塞控制算法」,它是以 Linux 内核模块...

  • 使用mark.js标注网页内容

    本文教你如何在chrome控制台中,使用mark.js,随意标注任何网页中的任何内容。 首先,我们下载mark.j...

  • iptables

    netfilter和iptables简介 linux中的网络数据包过滤功能主要由netfilter实现,netfi...

  • 2018-02-27-iptables-mark使用

    最近在使用iptables 的mark模块,为防止以后忘记命令使用,故写此博客。 所有TCP数据标记1 iptab...

  • iptables详解

    netfilter 内核中的防火墙框架,承载并生效规则;4表5链; netfilter功能:。 filter 包过...

  • iptables

    iptables iptables 规则原理和组成NetFilter什么是NetFilter?NetFilter是...

  • 如何防止别人卸载内核模块

    1.防卸载内核模块 不论是Windows还是Linux,当我们开发完一个内核模块的时候,内核模块中往往都承担这至关...

网友评论

      本文标题:如何在netfilter的内核模块中为每一个TCP打mark

      本文链接:https://www.haomeiwen.com/subject/jbypqqtx.html