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 ......
阅读(1907) | 评论(1) | 转发(0) |