因此,我想在ip_rcv函数中添加一些printk消息,以查看从特定IP接收到数据包后是否打印了消息。我将附加具有printk修改的整个ip_rcv函数:

int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
    const struct iphdr *iph;
    struct net *net;
    u32 len;

    /* 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;


    net = dev_net(dev);
    __IP_UPD_PO_STATS(net, IPSTATS_MIB_IN, skb->len);

    skb = skb_share_check(skb, GFP_ATOMIC);
    if (!skb) {
        __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
        goto out;
    }

    if (!pskb_may_pull(skb, sizeof(struct iphdr)))
        goto inhdr_error;

    iph = ip_hdr(skb);

    //**PSK's Modification**
    if (iph->saddr == 0x08080808)
        printk("\n***PSK: %x IP's message recieved: Google***\n", iph->saddr);
    if (iph->saddr == 0x0202000A)
        printk("\n***PSK: %x IP's message recieved: Gateway***\n", iph->saddr);
    if (iph->saddr == 0x010000FF)
        printk("\n***PSK: %x IP's message recieved : Home***\n", iph->saddr);

     /* RFC1122: 3.2.1.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;

    BUILD_BUG_ON(IPSTATS_MIB_ECT1PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_1);
    BUILD_BUG_ON(IPSTATS_MIB_ECT0PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_0);
    BUILD_BUG_ON(IPSTATS_MIB_CEPKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_CE);
    __IP_ADD_STATS(net,
               IPSTATS_MIB_NOECTPKTS + (iph->tos & INET_ECN_MASK),
               max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));

    if (!pskb_may_pull(skb, iph->ihl*4))
        goto inhdr_error;

    iph = ip_hdr(skb);

    if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
        goto csum_error;

    len = ntohs(iph->tot_len);
    if (skb->len < len) {
        __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
        goto drop;
    } else if (len < (iph->ihl*4))
        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 (pskb_trim_rcsum(skb, len)) {
        __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
        goto drop;
    }

    iph = ip_hdr(skb);
    skb->transport_header = skb->network_header + iph->ihl*4;

    /* Remove any debris in the socket control block */
    memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
    IPCB(skb)->iif = skb->skb_iif;

    /* Must drop socket now because of tproxy. */
    skb_orphan(skb);

    return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
               net, NULL, skb, dev, NULL,
               ip_rcv_finish);

csum_error:
    __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
inhdr_error:
    __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
drop:
    kfree_skb(skb);
out:
    return NET_RX_DROP;
}


从Google DNS,本地网关(10.0.2.2)或回送地址(127.0.0.1)获取数据包后,我应该将消息打印到内核缓冲区中。对于DNS和网关,这工作正常,但是当我ping localhost或尝试运行涉及到localhost的程序时,此方法就不行了。是否还有其他一些内核函数调用专门处理到localhost的数据包,或者我缺少一些非常基础的东西?我认为回送数据包应至少在L3之前通过堆栈跟踪与其他任何数据包相同的路径。如果有人简要解释了环回流量的处理以及答案,我也将不胜感激。

系统规格:
系统-虚拟机上的Ubuntu
内核-4.15.0-70通用

最佳答案

我认为这里有问题:

if (iph->saddr == 0x010000FF)


也许你的意思是:

if (iph->saddr == 0x0100007F)





  回送数据包应跟踪相同的路径


是的,一般。

详细调查loopback device的详细信息。



同样,您始终可以使用一些有用的工具进行操作,例如trace-cmd。 F.e.要查看功能图,您可以执行以下操作:

trace-cmd record -p function_graph -g net_rx_action


然后启动ping 127.0.0.1,然后停止跟踪并观看报告

trace-cmd report | vim -


在这里,您可以看到“路径”,并确保本地ping最终落入ip_rcv()

关于c - 环回模式下的ip_rcv(在ip_input.c中为ipv4)行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59115140/

10-12 05:55