免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 998 | 回复: 0
打印 上一主题 下一主题

LINUX协议栈之(4):How to put SKBs to upper layers [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-17 18:43 |只看该作者 |倒序浏览
How to put SKBs to upper layers
1. Bridge between layer 2 and 3
    How to put SKBs to upper layers, especially layer 3
??? The function netif_receive_skb() implement it !!! So this function
is the real bridge between layer 2 and layer 3.
2. The PACKET TYPE
    There is a very important struct : struct packet_type in the kernel and this struct is very simple.
        struct packet_type {
         
__be16         
type;    /* This is really htons(ether_type). */
            struct
net_device    *dev;    /* NULL is
wildcarded here         */
         
int         
(*func) (struct sk_buff *,
              
              struct
net_device *,
              
              struct
packet_type *,
              
              struct
net_device *);
         
void         
*af_packet_priv;
            struct list_head    list;
        };
    type : indicates the protocol type of layer 3.
    func : the protocol handler function pointer.
    Each layer 3 protocol(certainly including IP
protocol) has its own packet_type struct and all of these structs are
list into the global HASH table : ptype_base.     So we
can find each protocol's packet_type struct from this list and then
call its func function to process the SKB.
3. Function process flow
    First checking if the interface which received this
SKB is inactive, if inacvive then free this SKB and return NET_RX_DROP
directly.
   
    Then the netpoll checking(we'll not discuss it here).
   
    And then set the skb->net value.
    Then set the skb->input_dev = skb->dev.
   
    Then checking the dev->master with function : skb_bond().
    Then set the layer3 and layer4 header pointer to skb->data.
   
    Then begin to search the protocol type this SKB
belongs to. There are two steps in this process : 1. search the
ptype_all list; 2. search the ptype_base list.
3.1 The ptype_all list
    The ptype_all list used to implement the package
sniffer. You can register your own ptype struct with the type ETH_P_ALL
into the ptype_all double direction list.
    Each SKB the NIC received MUST be checked through the ptype_all list, just to find the sniffer.
3.2 The ptype_base HASH table
    The real work of putting the SKB to upper layer is complemented in function : __netif_dispatch_skb().
    This function iterate the ptype_base HASH table to
find the correct protocol packet_type struct, if found then call the
functioon : deliver_skb() to handle the skb.
    The deliver_skb() function is an encapsulation
function, it only call the packet_type's func pointer to handle the skb.
4. The strange thing(maybe) !!!
    There is a strange thing in this process flow : the
strange thing is an value : pt_prev that is defined in the
netif_receive_skb() function used as the important parameter of the
function deliver_skb().
    When found the right node in ptype_all or
ptype_base, the function first check if the pt_prev is NULL and if not
NULL then call the deliver_skb() with it, but the right packet_type
node we just found isn't pt_prev !!! so when we found the packet_type,
the pt_prev maybe NULL in that time. Sure the function set the
pt_prev's value with : pt_prev = ptype, so now the pt_prev has the
right value and, in the next flow of the netif_receive_skb() function
that check pt_prev's value again, the deliver_skb() is called again
with the last step's packet_type node.
    So, the 'real' process of the packet_type node is delayed a step !!!
Ok, we arrive at the entrance of IP layer now ......
   
   
     


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/26397/showart_444388.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP