免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1833 | 回复: 0
打印 上一主题 下一主题

[网络子系统] netfilter arp相关问题 [复制链接]

论坛徽章:
1
卯兔
日期:2014-05-29 10:21:33
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-02-10 10:49 |只看该作者 |倒序浏览
我在 NF_ARP_FORWARD点上注册hook函数抓arp的数据包打印出来,结果打印出来的数据不对。
请看arp结构体在内核中的定义:
struct arphdr
{
        __be16                ar_hrd;                /* format of hardware address        */
        __be16                ar_pro;                /* format of protocol address        */
        unsigned char        ar_hln;                /* length of hardware address        */
        unsigned char        ar_pln;                /* length of protocol address        */
        __be16                ar_op;                /* ARP opcode (command)                */
#if 0
        unsigned char                ar_sha[ETH_ALEN];        /* sender hardware address        */
        unsigned char                ar_sip[4];                /* sender IP address                */
        unsigned char                ar_tha[ETH_ALEN];        /* target hardware address        */
        unsigned char                ar_tip[4];                /* target IP address                */
#endif
};


在我注册的hook函数中,arp结构体中的ar_hrd,ar_pro,ar_hln,ar_pln,ar_op这几个成员的值能正确打印出来,但是源ip mac打印出来就不对了。

我不太清楚为什么内核里面arphdr结构体定义中把源/目的ip和mac的成员给注释掉了。

那注释掉了的话怎么来获取ip和mac的值啊。


下面是我hook函数的代码。里面使用的arp结构体是按照内核中定义的结构体自己定义的和内核中的arphdr的区别就是我自己定义的myarphdr把内核中注释掉的源目的ip mac放开了。


int arp_init()
{
    arp_ops.hook       = arphook_detect;
    arp_ops.pf            = NFPROTO_ARP;
    arp_ops.hooknum = NF_ARP_FORWARD;
    arp_ops.priority    = NF_IP_PRI_FIRST;

    ret = nf_register_hook(&arp_ops);

}

unsigned int arphook_detect(unsigned int hooknum,
                        struct sk_buff *__skb,
                        const struct net_device *in,
                        const struct net_device *out,
                        int (*okfn)(struct sk_buff *))
{
        struct ethhdr *ethhdr;
        struct myarphdr *arphdr;
        unsigned char *arpdat;

        arpdat = (unsigned char *)__skb->data;

        ethhdr = eth_hdr(__skb);
        arphdr = (struct myarphdr *) arpdat;
        printk("src ip is 0x%x,src mac is 0x%x,hd type=0x%x,proto type = 0x%x,hd size = 0x%x,proto size =0x%x,arp_op = 0x%x\n",
                        ntohl(arphdr->ar_sha),ntohl(arphdr->ar_sip),ntohs(arphdr->ar_hrd), ntohs(arphdr->ar_pro),
                        arphdr->ar_hln,arphdr->ar_pln,ntohs(arphdr->ar_op));
}

哪位大神能给解释一下吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP