title: ip_rcv (in ip_input.c for ipv4) behaviour in loopback mode
body: <p>So I wanted to put a few <em>printk</em> messages in <em>ip_rcv</em> function to see whether after receiving a packet from a particular IP there is a message printed. I am attaching the entire <em>ip_rcv</em> function which has the <em>printk</em> modifications:</p>
  
  <pre><code>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-&gt;pkt_type == PACKET_OTHERHOST)
          goto drop;
  
  
      net = dev_net(dev);
      __IP_UPD_PO_STATS(net, IPSTATS_MIB_IN, skb-&gt;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-&gt;saddr == 0x08080808)
          printk("\n***PSK: %x IP's message recieved: Google***\n", iph-&gt;saddr);
      if (iph-&gt;saddr == 0x0202000A)
          printk("\n***PSK: %x IP's message recieved: Gateway***\n", iph-&gt;saddr);
      if (iph-&gt;saddr == 0x010000FF)
          printk("\n***PSK: %x IP's message recieved : Home***\n", iph-&gt;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-&gt;ihl &lt; 5 || iph-&gt;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-&gt;tos &amp; INET_ECN_MASK),
                 max_t(unsigned short, 1, skb_shinfo(skb)-&gt;gso_segs));
  
      if (!pskb_may_pull(skb, iph-&gt;ihl*4))
          goto inhdr_error;
  
      iph = ip_hdr(skb);
  
      if (unlikely(ip_fast_csum((u8 *)iph, iph-&gt;ihl)))
          goto csum_error;
  
      len = ntohs(iph-&gt;tot_len);
      if (skb-&gt;len &lt; len) {
          __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
          goto drop;
      } else if (len &lt; (iph-&gt;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-&gt;len holds ntohs(iph-&gt;tot_len).
       */
      if (pskb_trim_rcsum(skb, len)) {
          __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
          goto drop;
      }
  
      iph = ip_hdr(skb);
      skb-&gt;transport_header = skb-&gt;network_header + iph-&gt;ihl*4;
  
      /* Remove any debris in the socket control block */
      memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
      IPCB(skb)-&gt;iif = skb-&gt;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;
  }
  </code></pre>
  
  <p>I should get a message printed into the kernel buffer after getting a packet from either Google DNS, my local gateway (10.0.2.2) or loop-back address (127.0.0.1). This is working fine for the DNS and the gateway, but not when I ping localhost or try to run a program which involves back and forth from a localhost. Are there some other kernel function calls that specifically handle packets to localhost, or am I missing something very basic? I thought the loopback packets should trace the same path through the stack as any other packet at least until L3. I would also appreciate if someone briefly explains the handling of loopback traffic along with the answer.</p>
  
  <p>System Specs:
  System- <strong>Ubuntu on Virtual Machine</strong>
  Kernel- <strong>4.15.0-70 generic</strong></p>