小编rr_*_*ovi的帖子

如何使用netfilter钩子在内核空间中回显数据包?

我想在内核空间中回显一个数据包.我在这台机器上使用端口6000运行一个echo服务器.现在一个客户端在另一台机器上运行,将数据发送到echo服务器.现在,我想要做的是从内核空间回送数据包.我不想用数据包打扰服务器,它将从内核空间静默回显.我在下面展示我的代码:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/mm.h>
#include <linux/err.h>
#include <linux/crypto.h>
#include <linux/init.h>
#include <linux/crypto.h>
#include <linux/scatterlist.h>
#include <net/ip.h>
#include <net/udp.h>
#include <net/route.h>
#include <net/checksum.h>
#include <linux/netfilter_ipv4.h>

#define IP_HDR_LEN 20
#define UDP_HDR_LEN 8
#define TOT_HDR_LEN 28

static unsigned int pkt_echo_begin(unsigned int hooknum,
                        struct sk_buff *skb,
                        const struct net_device *in,
                        const struct net_device *out,
                        int (*okfn)(struct sk_buff *));

static struct nf_hook_ops pkt_echo_ops __read_mostly = {
    .pf = NFPROTO_IPV4,
    .priority = 1, …
Run Code Online (Sandbox Code Playgroud)

c linux netfilter linux-kernel

6
推荐指数
1
解决办法
5688
查看次数

如何在内核空间中附加数据包?

我试图在内核空间的数据包上附加一些数据.我有一个echo客户端和服务器.我输入命令行,如:./client"message",服务器只是回复它.服务器使用./server运行.

现在,客户端和服务器位于两台不同的计算机上(可能是VM).我正在编写一个在客户机上运行的内核模块.它的工作是在数据包离开机器时在"消息"之后附加"12345".我将在下面介绍代码.

/*
 * This is ibss_obsf_cat.c
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/udp.h>
#include <linux/ip.h>

#undef __KERNEL__
#include <linux/netfilter_ipv4.h>
#define __KERNEL__


/*
 * Function prototypes ...
 */

static unsigned int cat_obsf_begin (unsigned int hooknum,
                struct sk_buff *skb,
                const struct net_device *in,
                const struct net_device *out,
                int (*okfn)(struct sk_buff *));

static void hex_dump (char str[], int len)
{

}

/*
 * struct nf_hook_ops instance initialization
 */

static struct nf_hook_ops cat_obsf_ops __read_mostly = …
Run Code Online (Sandbox Code Playgroud)

c linux netfilter kernel-module

5
推荐指数
1
解决办法
8127
查看次数

如何使用内核空间中的netfilter挂钩路由分裂的数据包

我必须在PRE_ROUTING钩子中将大包拆分成较小的包.我已经完成了将数据包分成较小的数据包,创建skb,设置ip和udp标头等所需的必要步骤.但我不理解的是如何路由数据包?我现在可以在数据包中附加数据(可以在我之前的问题中看到:如何在内核空间中附加数据包?).但现在我被困在路由分裂的数据包.提前致谢.

我正在给我的代码(到目前为止,我可以写).让我们假设模块在服务器机器上运行.服务器在端口6000上运行.然后客户端发送消息"ThisIsUsedForTesting".根据代码,服务器应该得到"ThisI":一个较小的数据包.我现在不关心第二个数据包.我可以很容易地破坏包大小.但现在可以有两个或更多的数据包.

运行此模块后,服务器将收到消息:"ThisI".但是当回复它时,数据包不会开箱即用.我为PRE_ROUTING编写了模块,然后机器应该正常运行,但是服务器进程会收到消息,然后机器会跳过.我不明白这些情况.欢迎任何帮助/建议.如果我可以管理第一个拆分数据包,我想,其余的可以自动处理,所以这里没有给出它们的代码:

    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/skbuff.h>
    #include <linux/netfilter.h>
    #include <linux/netdevice.h>
    #include <linux/ip.h>
    #include <linux/udp.h>
    #include <linux/mm.h>
    #include <linux/err.h>
    #include <linux/crypto.h>
    #include <linux/init.h>
    #include <linux/crypto.h>
    #include <linux/scatterlist.h>
    #include <net/ip.h>
    #include <net/udp.h>
    #include <net/route.h>
    #include <linux/netfilter_ipv4.h>

    #define IP_HDR_LEN 20
    #define UDP_HDR_LEN 8
    #define TOT_HDR_LEN 28

    static unsigned int pkt_split_begin(unsigned int hooknum,
                            struct sk_buff *skb,
                            const struct net_device *in,    
                            const struct net_device *out,
                            int (*okfn)(struct sk_buff *));

    static void skb_print_info(const struct sk_buff *skb);
    static void ip_print_info(struct iphdr *iph); …
Run Code Online (Sandbox Code Playgroud)

c linux linux-kernel

3
推荐指数
1
解决办法
2473
查看次数

标签 统计

c ×3

linux ×3

linux-kernel ×2

netfilter ×2

kernel-module ×1