// ip数据报向本地传递 // 调用路径:ip_rcv->dst_input->...->ip_local_deliver 1.1 int ip_local_deliver(struct sk_buff *skb) { //如果ip数据报被分片,则重组 if (ip_is_fragment(ip_hdr(skb))) { if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER)) return 0; } //通过hook点,向上传递 return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN, skb, skb->dev, NULL, ip_local_deliver_finish); } // ip数据报向本地传递 // 步骤: // 1.向raw套接字传递一份skb // 2.根据ip报头的协议号在inet_protos中找到l4协议 // 3.调用l4协议处理函数 1.2 static int ip_local_deliver_finish(struct sk_buff *skb) { struct net *net = dev_net(skb->dev); //使skb有足够的ip报头 __skb_pull(skb, ip_hdrlen(skb)); //使指针指向ip报文内容 skb_reset_transport_header(skb); rcu_read_lock(); { int protocol = ip_hdr(skb)->protocol; const struct net_protocol *ipprot; int raw; resubmit: //向raw套接字传递 raw = raw_local_deliver(skb, protocol); //通过l4协议号查找上层协议 //在inet_init中,通过inet_add_protocol注册l4协议到inet_protos数组 ipprot = rcu_dereference(inet_protos[protocol]); if (ipprot != NULL) { int ret; //l4协议处理函数 ret = ipprot->handler(skb); if (ret < 0) { protocol = -ret; goto resubmit; } //snmp统计 IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS); } ... } out: rcu_read_unlock(); return 0; }