ip_rcv && ip_rcv_finish

(1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv

(2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发

(3) 类型为ETH_P_IP类型的数据包,被传递到三层,调用ip_rcv函数

(4) ip_rcv完成基本的校验和处理工作后,经过PRE_ROUTING钩子点

(5) 经过PRE_ROUTING钩子点之后,调用ip_rcv_finish完成数据包接收,包括选项处理,路由查询,并且根据路由决定数据包是发往本机还是转发

 

1 static struct packet_type ip_packet_type __read_mostly = {
2     .type = cpu_to_be16(ETH_P_IP),
3     .func = ip_rcv,
4 };

 

  1 /*
  2  *     Main IP Receive routine.
  3  */
  4 int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  5 {
  6     const struct iphdr *iph;
  7     struct net *net;
  8     u32 len;
  9 
 10     /* When the interface is in promisc. mode, drop all the crap
 11      * that it receives, do not try to analyse it.
 12      */
 13     /* 混杂模式下,非本机包 */
 14     if (skb->pkt_type == PACKET_OTHERHOST)
 15         goto drop;
 16 
 17 
 18     /* 获取net */
 19     net = dev_net(dev);
 20     __IP_UPD_PO_STATS(net, IPSTATS_MIB_IN, skb->len);
 21 
 22     /* 检查skb共享 */
 23     skb = skb_share_check(skb, GFP_ATOMIC);
 24     if (!skb) {
 25         __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
 26         goto out;
 27     }
 28 
 29     /* 测试是否可以取得ip头 */
 30     if (!pskb_may_pull(skb, sizeof(struct iphdr)))
 31         goto inhdr_error;
 32 
 33     /* 取ip头 */
 34     iph = ip_hdr(skb);
 35 
 36     /*
 37      *    RFC1122: 3.2.1.2 MUST silently discard any IP frame that fails the checksum.
 38      *
 39      *    Is the datagram acceptable?
 40      *
 41      *    1.    Length at least the size of an ip header
 42      *    2.    Version of 4
 43      *    3.    Checksums correctly. [Speed optimisation for later, skip loopback checksums]
 44      *    4.    Doesn't have a bogus length
 45      */
 46 
 47     /* 头部长度不足20 或者版本不是4 */
 48     if (iph->ihl < 5 || iph->version != 4)
 49         goto inhdr_error;
 50 
 51     BUILD_BUG_ON(IPSTATS_MIB_ECT1PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_1);
 52     BUILD_BUG_ON(IPSTATS_MIB_ECT0PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_0);
 53     BUILD_BUG_ON(IPSTATS_MIB_CEPKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_CE);
 54     __IP_ADD_STATS(net,
 55                IPSTATS_MIB_NOECTPKTS + (iph->tos & INET_ECN_MASK),
 56                max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
 57 
 58     /* 测试实际应取的ip头 */
 59     if (!pskb_may_pull(skb, iph->ihl*4))
 60         goto inhdr_error;
 61 
 62     /* 取ip头 */
 63     iph = ip_hdr(skb);
 64 
 65     /* 校验和错误 */
 66     if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
 67         goto csum_error;
 68 
 69     /* 取总长度 */
 70     len = ntohs(iph->tot_len);
 71 
 72     /* skb长度比ip包总长度小 */
 73     if (skb->len < len) {
 74         __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
 75         goto drop;
 76     } 
 77     /* 比头部长度还小 */
 78     else if (len < (iph->ihl*4))
 79         goto inhdr_error;
 80 
 81     /* Our transport medium may have padded the buffer out. Now we know it
 82      * is IP we can trim to the true length of the frame.
 83      * Note this now means skb->len holds ntohs(iph->tot_len).
 84      */
 85     /* 设置总长度为ip包的长度 */
 86     if (pskb_trim_rcsum(skb, len)) {
 87         __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
 88         goto drop;
 89     }
 90 
 91     /* 取得传输层头部 */
 92     skb->transport_header = skb->network_header + iph->ihl*4;
 93 
 94     /* Remove any debris in the socket control block */
 95     /* 重置cb */
 96     memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
 97 
 98     /* 保存输入设备信息 */
 99     IPCB(skb)->iif = skb->skb_iif;
100 
101     /* Must drop socket now because of tproxy. */
102     skb_orphan(skb);
103 
104     /* 经过PRE_ROUTING钩子点 */
105     return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
106                net, NULL, skb, dev, NULL,
107                ip_rcv_finish);
108 
109 csum_error:
110     __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
111 inhdr_error:
112     __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
113 drop:
114     kfree_skb(skb);
115 out:
116     return NET_RX_DROP;
117 }

 

static int ip_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
{
    const struct iphdr *iph = ip_hdr(skb);
    struct rtable *rt;
    struct net_device *dev = skb->dev;
    void (*edemux)(struct sk_buff *skb);

    /* if ingress device is enslaved to an L3 master device pass the
     * skb to its handler for processing
     */
    skb = l3mdev_ip_rcv(skb);
    if (!skb)
        return NET_RX_SUCCESS;

    /* 
        启用了early_demux
        skb路由缓存为空
        skb的sock为空
        不是分片包
    */
    if (net->ipv4.sysctl_ip_early_demux &&
        !skb_dst(skb) &&
        !skb->sk &&
        !ip_is_fragment(iph)) {
        const struct net_protocol *ipprot;

        /* 找到上层协议 */
        int protocol = iph->protocol;

        /* 获取协议对应的prot */
        ipprot = rcu_dereference(inet_protos[protocol]);

        /* 找到early_demux函数,如tcp_v4_early_demux */
        if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) {

            /* 调用该函数,将路由信息缓存到skb->refdst */
            edemux(skb);
            /* must reload iph, skb->head might have changed */
            /* 重新取ip头 */
            iph = ip_hdr(skb);
        }
    }

    /*
     *    Initialise the virtual path cache for the packet. It describes
     *    how the packet travels inside Linux networking.
     */
    /* 校验路由失败 */
    if (!skb_valid_dst(skb)) {
        /* 查路由 */
        int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
                           iph->tos, dev);
        if (unlikely(err)) {
            if (err == -EXDEV)
                __NET_INC_STATS(net, LINUX_MIB_IPRPFILTER);
            goto drop;
        }
    }

#ifdef CONFIG_IP_ROUTE_CLASSID
    if (unlikely(skb_dst(skb)->tclassid)) {
        struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
        u32 idx = skb_dst(skb)->tclassid;
        st[idx&0xFF].o_packets++;
        st[idx&0xFF].o_bytes += skb->len;
        st[(idx>>16)&0xFF].i_packets++;
        st[(idx>>16)&0xFF].i_bytes += skb->len;
    }
#endif

    /* 处理ip选项 */
    if (iph->ihl > 5 && ip_rcv_options(skb))
        goto drop;

    /* 找到路由缓存项 */
    rt = skb_rtable(skb);
    if (rt->rt_type == RTN_MULTICAST) {
        __IP_UPD_PO_STATS(net, IPSTATS_MIB_INMCAST, skb->len);
    } else if (rt->rt_type == RTN_BROADCAST) {
        __IP_UPD_PO_STATS(net, IPSTATS_MIB_INBCAST, skb->len);
    } else if (skb->pkt_type == PACKET_BROADCAST ||
           skb->pkt_type == PACKET_MULTICAST) {
        struct in_device *in_dev = __in_dev_get_rcu(dev);

        /* RFC 1122 3.3.6:
         *
         *   When a host sends a datagram to a link-layer broadcast
         *   address, the IP destination address MUST be a legal IP
         *   broadcast or IP multicast address.
         *
         *   A host SHOULD silently discard a datagram that is received
         *   via a link-layer broadcast (see Section 2.4) but does not
         *   specify an IP multicast or broadcast destination address.
         *
         * This doesn't explicitly say L2 *broadcast*, but broadcast is
         * in a way a form of multicast and the most common use case for
         * this is 802.11 protecting against cross-station spoofing (the
         * so-called "hole-196" attack) so do it for both.
         */
        if (in_dev &&
            IN_DEV_ORCONF(in_dev, DROP_UNICAST_IN_L2_MULTICAST))
            goto drop;
    }

    /* 调用路由项的input函数,可能为ip_local_deliver或者ip_forward */
    return dst_input(skb);

drop:
    kfree_skb(skb);
    return NET_RX_DROP;
}

 

转载于:https://www.cnblogs.com/wanpengcoder/p/7577398.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值