int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
//幾個結構sk_buff通訊端緩衝,net_device網路裝置結構,是所有網路結構的基礎資料結構
,packet_type原型
struct packet_type
{
unsigned short 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 *);
void *data; /* Private to the packet type */
struct packet_type *next;
};
{
struct iphdr *iph;
//指向IP協議頭的結構指標
/* When the interface is in promisc. mode, drop all the crap
* that it receives, do not try to analyse it.
*/
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
//pkt_type表示報文類型 。PACKET_OTHERHOST表示非去往本機但是在特定模式下被接受的報文
//此處的作用是根據包類型,來丟棄並非去往本地的包
IP_INC_STATS_BH(IpInReceives);
if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
goto out;
/*skb_share_check定義在linux/Skbuff.h裡面,如果users是一個就返回假,其他返回真。
[
static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
{
if (skb_shared(skb)) {
struct sk_buff *nskb;
nskb = skb_clone(skb, pri); //返回了一個clone過的通訊端緩衝
kfree_skb(skb); //Free an skbuff
return nskb;
}
return skb;
}
]
#define GFP_ATOMIC (__GFP_HIGH)
#define __GFP_HIGH 0x20
*/
註: skb_clone的原型是struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
gfp_mask說明 gfp_mask: get_free_pages mask, passed to alloc_skb
if (!pskb_may_pull(skb, sizeof(struct iphdr)))
goto inhdr_error;
//
static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
{
if (len <= skb_headlen(skb))
return 1;
if (len > skb->len)
return 0;
return (__pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL);
}
pskb_may_pull在調用 skb_pull() 去掉外層協議頭之前,通常先調用此函數判斷一下是否有足夠的資料用於“pull”。
如果線性 buffer足夠 pull,則返回1;
如果需要 pull 的資料超過 skb->len,則返回0;
最後,調用__pskb_pull_tail() 來檢查 page buffer 有沒有足夠的資料用於 pull
*/
iph = skb->nh.iph;
/*
* RFC1122: 3.1.2.2 MUST silently discard any IP frame that fails the checksum.
*
* Is the datagram acceptable?
*
* 1. Length at least the size of an ip header
* 2. Version of 4
* 3. Checksums correctly. [Speed optimisation for later, skip loopback checksums]
* 4. Doesn't have a bogus length
*/
if (iph->ihl < 5 || iph->version != 4)
goto inhdr_error;
//iph->ihl<5說明iph->ihl指的是IP包的首部長度,首部一行是32bit也就是4byte(位元組)註:1byte=8bit,byte是電腦中最小檔案
單位,普通IP資料包首部長度(不包含任何選項)欄位的值是5.
if (!pskb_may_pull(skb, iph->ihl*4))
goto inhdr_error;
//iph->ihl*4是20,是首部最長的長度,此語句是說如果頭部長度不能pull,則error
iph = skb->nh.iph;
if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
goto inhdr_error;
//用彙編來校正和
{
__u32 len = ntohs(iph->tot_len);
if (skb->len < len || len < (iph->ihl<<2))
goto inhdr_error;
/* Our transport medium may have padded the buffer out. Now we know it
* is IP we can trim to the true length of the frame.
* Note this now means skb->len holds ntohs(iph->tot_len).
*/
if (skb->len > len) {
__pskb_trim(skb, len);
if (skb->ip_summed == CHECKSUM_HW)
skb->ip_summed = CHECKSUM_NONE;
}
}
return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
ip_rcv_finish);
inhdr_error:
IP_INC_STATS_BH(IpInHdrErrors);
drop:
kfree_skb(skb);
out:
return NET_RX_DROP;
}