Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linux-kernel – 如何访问struct sk_buff成员?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我试图将从机器输出的所有数据包的源IP修改为我在此内核模块中指定的内容,但每次我尝试访问nh.iph-> saddr时我在编译时遇到错误,说明struct sk_buff没有成员名为nh 我在这做错了什么? 我错过了一些标题还是什么? #include <linux/module.h> #include <linux/kernel.h> #include <linux/
我试图将从机器输出的所有数据包的源IP修改为我在此内核模块中指定的内容,但每次我尝试访问nh.iph-> saddr时我在编译时遇到错误,说明struct sk_buff没有成员名为nh

在这做错了什么?
我错过了一些标题还是什么?

#include <linux/module.h>       
#include <linux/kernel.h>       
#include <linux/init.h>         

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <linux/skbuff.h>
#include <linux/ip.h>                  /* For IP header */

#include <linux/inet.h> /* For in_aton(); htonl(); and other related Network utility functions */ 


static struct nf_hook_ops nfho;


unsigned int hook_func(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 *sb = *skb;
    struct in_addr masterIP;

    masterIP.s_addr = htonl (in_aton("192.168.1.10")); 
    sb->nh.iph->saddr = masterIP.s_addr;
    return NF_ACCEPT;
}

请注意,我正在运行Ubuntu 10.04 LTS 64位
内核2.6.32-33

解决方法

你的内核版本中,struct sk_buff已经改变了.它不再拥有这些成员.要访问ip标头,您应该尝试:

#include <linux/ip.h>

struct iphdr* iph = ip_hdr(skb);

然后只需使用iph变量来更改地址,例如:

iph->saddr = ....
iph->daddr = ....

另外,不要忘记您可能需要重新计算ip和可能的传输数据包校验和.

大佬总结

以上是大佬教程为你收集整理的linux-kernel – 如何访问struct sk_buff成员?全部内容,希望文章能够帮你解决linux-kernel – 如何访问struct sk_buff成员?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。