gpt4 book ai didi

C - Linux - 内核模块 - TCP header

转载 作者:可可西里 更新时间:2023-11-01 02:33:16 25 4
gpt4 key购买 nike

我正在尝试创建 linux 内核模块,它将检查传入的数据包。目前,我正在提取数据包的 TCP header 并读取源端口和目标端口 -> 但是我得到的值不正确。我有钩子(Hook)函数:

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 iphdr *ipp = (struct iphdr *)skb_network_header(skb);
struct tcphdr *hdr;
/* Using this to filter data from another machine */
unsigned long ok_ip = 2396891328;

/* Some problem, empty network packet. Stop it now. */
if (!skb)
return NF_ACCEPT;

/* Just to track only packets coming from 1 IP */
if (ipp->saddr != ok_ip)
return NF_ACCEPT;

/* Incomming packet is TCP */
if (ipp->protocol == IPPROTO_TCP) {
hdr = (struct tcphdr *) skb_transport_header(skb);
printk(" TCP ports: source: %d, dest: %d .\n", ntohs(hdr->source),
ntohs(hdr->dest));
}
}

现在,当我尝试 telnet 端口 21(我没有在那里收听时):

[ 4252.961912]  TCP ports: source: 17664, dest: 52 .
[ 4253.453978] TCP ports: source: 17664, dest: 52 .
[ 4253.953204] TCP ports: source: 17664, dest: 48 .

当我 telnet 端口 22 - SSH 守护进程在那里监听:

[ 4299.239940]  TCP ports: source: 17664, dest: 52 .
[ 4299.240527] TCP ports: source: 17664, dest: 40 .
[ 4299.552566] TCP ports: source: 17664, dest: 40 .

从输出中可以看出我得到了非常奇怪的结果,有人知道问题出在哪里吗?当我编译模块时,我没有错误/警告。内核版本( header ):3.7.10。不使用 SELinux 或类似软件。

最佳答案

我在为网络类(class)编写小型防火墙时遇到了同样的问题,我刚刚发现了我遇到的问题。我投错了 tcp header 。尝试转换为 tcp,然后访问端口。

这是它的工作代码片段

struct iphdr *ip_header;       // ip header struct
struct tcphdr *tcp_header; // tcp header struct
struct udphdr *udp_header; // udp header struct
struct sk_buff *sock_buff;

unsigned int sport ,
dport;


sock_buff = skb;

if (!sock_buff)
return NF_ACCEPT;

ip_header = (struct iphdr *)skb_network_header(sock_buff);
if (!ip_header)
return NF_ACCEPT;


//if TCP PACKET
if(ip_header->protocol==IPPROTO_TCP)
{
//tcp_header = (struct tcphdr *)skb_transport_header(sock_buff); //doing the cast this way gave me the same problem

tcp_header= (struct tcphdr *)((__u32 *)ip_header+ ip_header->ihl); //this fixed the problem

sport = htons((unsigned short int) tcp_header->source); //sport now has the source port
dport = htons((unsigned short int) tcp_header->dest); //dport now has the dest port
}

关于C - Linux - 内核模块 - TCP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16528868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com