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;
}