从Netfilter挂钩中扩展数据包头 [英] Extending packet header from within a Netfilter hook

查看:254
本文介绍了从Netfilter挂钩中扩展数据包头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NF_HOOK_LOCAL_OUT内部的现有IP数据包上添加IP头。我面临的问题是skb扩展函数(例如copy / clone / expand / reallocate header)分配了一个新的sk_buff。我们不能返回这个新分配的指针,因为netfilter钩子函数不再(内核版本2.6.31)传递skb指针的地址(传递值)。我如何解决这个问题如下:
1.我使用skb_header_realloc()获得了一个新的skb。这将复制skb中的所有数据。
2.我修改了新的skb(称之为skb2)以预先添加新的IP头,在新的IP头中设置适当的值。
3.使用skb_morph()替换原始skb(在Netfilter钩子函数中传递)的内容和skb2的内容。返回NF_ACCEPT。

I want to prepend IP header on an existing IP packet while inside NF_HOOK_LOCAL_OUT. The issue I face is that the skb expansion functions (such as copy/clone/expand/reallocate header) allocate a new sk_buff. We can not return this newly allocated pointer since netfilter hook function no longer (kernel version 2.6.31) passes the skb pointer's address (passes by value). How I solved the issue is as follows: 1. I got a new skb using skb_header_realloc(). This copies all the data from skb. 2. I modified the new skb (call it skb2) to prepend the new IP header, set appropriate values in the new IP header. 3. Replace the contents of the original skb (passed in the Netfilter hook function) with the contents of the skb2 using skb_morph(). Returned NF_ACCEPT.

这是实现我的意图的唯一途径吗?有更有效的解决方案吗?还有skb_morph的其他用例(除了IP重组代码)吗?

Is this the only way of achieving what I intended to? Is there a more efficient solution? Are there other use cases of skb_morph (besides the IP reassembly code)?

推荐答案

这适用于我,在2.6内核中:

This works for me, in 2.6 kernels:

...
struct iphdr* iph;
if (skb_headroom(skb) < sizeof(struct iphdr))
  if (0 != pskb_expand_head(skb, sizeof(struct iphdr) - skb_headroom(skb), 0, GFP_ATOMIC)) {
    printk("YOUR FAVOURITE ERROR MESSAGE");
    kfree_skb(skb);
    return NF_STOLEN;
  }
iph = (struct iphdr*) skb_push(skb, sizeof(struct iphdr));
//Fill ip packet
return NF_ACCEPT;

希望有所帮助。

这篇关于从Netfilter挂钩中扩展数据包头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆