<div dir="ltr">Adel,<div><div style="font-family:arial,sans-serif;font-size:12.666666984558105px">You say you have a  NULL pointer but you do not specify where.</div><div style="font-family:arial,sans-serif;font-size:12.666666984558105px">
<br></div><div style>can you please send the  log of the panic ? </div></div><div style><br></div><div style>Can you send the kernel module full code ? </div><div style><br></div><div style>Regards,</div><div style>Rami Rosen</div>
<div style><a href="http://ramirose.wix.com/ramirosen">http://ramirose.wix.com/ramirosen</a><br></div><div style><br></div><div style><br></div><div style><br></div><div style><br></div><div style><br></div><div style><br>
</div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 17, 2013 at 6:18 AM, Adel Qodmani <span dir="ltr">&lt;<a href="mailto:mpcadel@gmail.com" target="_blank">mpcadel@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello everyone, <br><br><br>I am trying to send an ICMP message in a kernel module; typically I&#39;d build the skb, set up the net_device, the ethernet header and then use dev_queue_xmit.<div>
<br></div><div>

But right now I want my packet to follow the IP routing rules set in the system, so I thought I&#39;ll use ip_local_out function and let it handle the routing and setting the rest of the parameters in the skb.</div><div>


<br></div><div>Yet, my code causes the kernel to panic due to a NULL pointer.</div><div> <br>The code is as follows:<br><div>/* This function assumes source and dest are in NETWORK byte order */</div><div>int sendICMPEcho(char *msg, unsigned int length, __be32 source, __be32 dest)</div>


<div>{</div><div><span style="white-space:pre-wrap">        </span>struct iphdr *iph;</div><div><span style="white-space:pre-wrap">        </span>struct icmphdr *icmph;</div><div><span style="white-space:pre-wrap">        </span>struct sk_buff *newPacket;</div>


<div><span style="white-space:pre-wrap">        </span>unsigned char *data;</div><div><span style="white-space:pre-wrap">        </span>unsigned int skbSize = length + sizeof(struct icmphdr) * 2</div><div><span style="white-space:pre-wrap">                                </span>+ sizeof(struct iphdr) * 2</div>


<div><span style="white-space:pre-wrap">                                </span>+ sizeof(struct ethhdr) * 2;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>/* Allocate the skb */</div><div><span style="white-space:pre-wrap">        </span>newPacket = alloc_skb(skbSize, GFP_ATOMIC);</div>


<div><span style="white-space:pre-wrap">        </span>if(newPacket == NULL)</div><div><span style="white-space:pre-wrap">                </span>return SEND_FAIL_MEMORY;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>/* Reserve the headers area */</div>


<div><span style="white-space:pre-wrap">        </span>skb_reserve(newPacket, sizeof(struct icmphdr)</div><div><span style="white-space:pre-wrap">                                </span> + sizeof(struct iphdr) </div><div><span style="white-space:pre-wrap">                                </span>+ sizeof(struct ethhdr));<span style="white-space:pre-wrap">        </span></div>


<div><br></div><div><span style="white-space:pre-wrap">        </span>/* Extend the data area from 0 to the message length */</div><div><span style="white-space:pre-wrap">        </span>data = skb_put(newPacket, length);</div>
<div>
<span style="white-space:pre-wrap">        </span>/* Copy the data from the message buffer to the newPacket */</div><div><span style="white-space:pre-wrap">        </span>memcpy(data, msg, length);</div><div><br></div><div><span style="white-space:pre-wrap">        </span>/************** ICMP HEADER***************/<span style="white-space:pre-wrap">        </span></div>


<div><span style="white-space:pre-wrap">        </span>/* skb_push - pushing the icmp header in the packet data */</div><div><span style="white-space:pre-wrap">        </span>icmph = (struct icmphdr *) skb_push(newPacket,</div>

<div><span style="white-space:pre-wrap">                                                </span>sizeof(struct icmphdr));</div><div><span style="white-space:pre-wrap">        </span>/*set ICMP header here */</div><div><span style="white-space:pre-wrap">        </span>icmph-&gt;type = ICMP_ECHO;</div>


<div><span style="white-space:pre-wrap">        </span>icmph-&gt;code = 0;</div><div><span style="white-space:pre-wrap">        </span>icmph-&gt;<a href="http://un.echo.id" target="_blank">un.echo.id</a> = 0;</div><div><span style="white-space:pre-wrap">        </span>icmph-&gt;un.echo.sequence = htons(sendCounter);</div>


<div><span style="white-space:pre-wrap">        </span>icmph-&gt;checksum= 0;</div><div><span style="white-space:pre-wrap">        </span>icmph-&gt;checksum = in_cksum((unsigned short *)icmph, </div><div><span style="white-space:pre-wrap">                                </span>sizeof(struct icmphdr) + length);</div>


<div><span style="white-space:pre-wrap">        </span>/************** END ICMP HEADER**************/</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap">        </span>/************** IP HEADER ***************/</div>


<div><span style="white-space:pre-wrap">        </span>iph = (struct iphdr *) skb_push(newPacket,</div><div><span style="white-space:pre-wrap">                                                </span>sizeof(struct iphdr));</div><div><span style="white-space:pre-wrap">        </span>/* set IP header here */</div>


<div><span style="white-space:pre-wrap">        </span>iph-&gt;ihl = 5;/* 5 * 32(bits) */</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;version = 4;</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;tos = 0; /* The recommended value by IANA for ICMP request */</div>


<div><span style="white-space:pre-wrap">        </span>iph-&gt;tot_len = htons( sizeof(struct iphdr) </div><div><span style="white-space:pre-wrap">                                </span>+ sizeof(struct icmphdr)</div><div><span style="white-space:pre-wrap">                                </span>+ length);</div>


<div><span style="white-space:pre-wrap">        </span>iph-&gt;id = 0;</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;frag_off = 0; /* No fragementation */</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;ttl = 65;</div>


<div><span style="white-space:pre-wrap">        </span>iph-&gt;protocol =  IPPROTO_ICMP;</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;saddr = source;</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;daddr = dest;</div>


<div><span style="white-space:pre-wrap">        </span>iph-&gt;check = 0;</div><div><span style="white-space:pre-wrap">        </span>iph-&gt;check = in_cksum((unsigned short *)iph, sizeof(struct iphdr));</div><div><span style="white-space:pre-wrap">        </span>/************** END IP HEADER ***************/</div>


<div><br></div><div><br></div><div>//<span style="white-space:pre-wrap">        </span>if(ip_local_out(newPacket) &lt; 0) THIS CRASHES WITH A NULL POINTER</div><div>//<span style="white-space:pre-wrap">                </span>return SEND_FAIL_SEND;</div>


<div><span style="white-space:pre-wrap">        </span>++sendCounter;</div><div><span style="white-space:pre-wrap">        </span>return SEND_SUCCESS;</div><div>}</div><div>/* end sendICMPEcho */</div><div><br></div><div><div>
In my attempts when trying to solve this, I&#39;ve tried to manually set the net_device in the skb and that didn&#39;t work either; I still had the same kernel panic.</div>
<div><br></div></div><div>So I am sorry for the trouble, any hint where the error can be?<br><br><br>Regards,<br>Adel</div></div></div>
<br>_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br></blockquote></div><br></div>