免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
楼主: duanjigang
打印 上一主题 下一主题

Hacking the Linux Kernel Network Stack(译本) [复制链接]

论坛徽章:
0
31 [报告]
发表于 2008-04-15 11:14 |只看该作者

关于sk_buff的一些疑惑

30楼 发表于 2008-4-14 21:36   


QUOTE:
原帖由 platinum 于 2008-1-11 15:52 发表
skb 中的数据是不是在内存中连续存放的?
mac 后面是不是就是 IP 头了,IP 头后面是不是就直接是 TCP/UDP/ICMP/IGMP 头?我始终有这个迷惑

为什么你有这个疑惑呢?^_^

对了,我们可不可以继续探讨sk_buff结构中的data在各个不同的钩子点究竟是指向哪一层的数据头?


谢谢。^_^

论坛徽章:
0
32 [报告]
发表于 2008-04-16 13:53 |只看该作者
原帖由 platinum 于 2008-1-11 15:52 发表
skb 中的数据是不是在内存中连续存放的?
mac 后面是不是就是 IP 头了,IP 头后面是不是就直接是 TCP/UDP/ICMP/IGMP 头?我始终有这个迷惑

后来发的那个例子说明应该是连续存放的,因为从链路层raw字段开始,不断往后移动指针,自增,得到的位置都是合法的IP层头或者传输层头。如果不连续的话,自增肯定不行的。

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
33 [报告]
发表于 2009-02-22 23:12 |只看该作者
好帖,经典啊

论坛徽章:
0
34 [报告]
发表于 2011-04-14 21:34 |只看该作者
本帖最后由 chary8088 于 2011-04-14 21:35 编辑

correct the packet flowing diagram in netfilter:

The (idealized) IPv4 traversal diagram looks like the following:


A Packet Traversing the Netfilter System:

   --->[1]--->[ROUTE]--->[3]--->[4]--->
                 |                          ^
                 |                           |
                 |                    [ROUTE]
                 v                          |
                [2]                      [5]
                 |                          ^
                 |                          |
                 v                         |
                  [ Local process]

On the left is where packets come in: having passed the simple sanity checks (i.e., not truncated, IP checksum OK, not a promiscuous receive), they are passed to the netfilter framework's NF_IP_PRE_ROUTING [1] hook.

Next they enter the routing code, which decides whether the packet is destined for another interface, or a local process. The routing code may drop packets that are unroutable.

If it's destined for the box itself, the netfilter framework is called again for the NF_IP_LOCAL_IN [2] hook, before being passed to the process (if any).

If it's destined to pass to another interface instead, the netfilter framework is called for the NF_IP_FORWARD [3] hook.

The packet then passes a final netfilter hook, the NF_IP_POST_ROUTING [4] hook, before being put on the wire again.

The NF_IP_LOCAL_OUT [5] hook is called for packets that are created locally. Here you can see that routing occurs after this hook is called: in fact, the routing code is called first (to figure out the source IP address and some IP options): if you want to alter the routing, you must alter the `skb->dst' field yourself, as is done in the NAT code.

论坛徽章:
0
35 [报告]
发表于 2011-04-15 08:36 |只看该作者
好东西,学习了

论坛徽章:
0
36 [报告]
发表于 2011-11-17 10:18 |只看该作者
这个太经典了

在2.6下面稍微修改了一下,测试成功

论坛徽章:
0
37 [报告]
发表于 2012-04-13 11:22 |只看该作者
本帖最后由 c04n05 于 2012-04-13 11:24 编辑

最后,传给钩子函数的最后一个参数是一个名为okfn的指向函数的指针,这个函数有一个sk_buff的结构体作为参数,返回一个整型值。我也不能确定这个函数做什么,在net/core/netfilter.c中有两处对此函数的调用。这两处调用就是在函数nf_hook_slow()和函数nf_reinject()里,在这两个调用处当Netfilter钩子的返回值为NF_ACCEPT时,此函数被调用。如果有谁知道关于okfn更详细的信息,请告诉我。
以发送ip包为例子,下面是2.6.34里面的源码:
  1. int __ip_local_out(struct sk_buff *skb)
  2. {
  3.         struct iphdr *iph = ip_hdr(skb);

  4.         iph->tot_len = htons(skb->len);
  5.         ip_send_check(iph);
  6.         return nf_hook(PF_INET, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
  7.                        dst_output);
  8. }
复制代码
最后面那个dst_output就是okfn,具体的函数可以看源码
这个函数一般是在调用完了所有的hook之后如果返回值为1的话(NF_ACCEPT),就将这个包进入正常处理流程(发送出去)

论坛徽章:
0
38 [报告]
发表于 2012-04-19 15:33 |只看该作者
第六部分的代码有人测试成功了吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP