Sending an ICMP packet with ip_local_out
Adel Qodmani
mpcadel at gmail.com
Sun Jun 16 23:18:22 EDT 2013
Hello everyone,
I am trying to send an ICMP message in a kernel module; typically I'd build
the skb, set up the net_device, the ethernet header and then use
dev_queue_xmit.
But right now I want my packet to follow the IP routing rules set in the
system, so I thought I'll use ip_local_out function and let it handle the
routing and setting the rest of the parameters in the skb.
Yet, my code causes the kernel to panic due to a NULL pointer.
The code is as follows:
/* This function assumes source and dest are in NETWORK byte order */
int sendICMPEcho(char *msg, unsigned int length, __be32 source, __be32 dest)
{
struct iphdr *iph;
struct icmphdr *icmph;
struct sk_buff *newPacket;
unsigned char *data;
unsigned int skbSize = length + sizeof(struct icmphdr) * 2
+ sizeof(struct iphdr) * 2
+ sizeof(struct ethhdr) * 2;
/* Allocate the skb */
newPacket = alloc_skb(skbSize, GFP_ATOMIC);
if(newPacket == NULL)
return SEND_FAIL_MEMORY;
/* Reserve the headers area */
skb_reserve(newPacket, sizeof(struct icmphdr)
+ sizeof(struct iphdr)
+ sizeof(struct ethhdr));
/* Extend the data area from 0 to the message length */
data = skb_put(newPacket, length);
/* Copy the data from the message buffer to the newPacket */
memcpy(data, msg, length);
/************** ICMP HEADER***************/
/* skb_push - pushing the icmp header in the packet data */
icmph = (struct icmphdr *) skb_push(newPacket,
sizeof(struct icmphdr));
/*set ICMP header here */
icmph->type = ICMP_ECHO;
icmph->code = 0;
icmph->un.echo.id = 0;
icmph->un.echo.sequence = htons(sendCounter);
icmph->checksum= 0;
icmph->checksum = in_cksum((unsigned short *)icmph,
sizeof(struct icmphdr) + length);
/************** END ICMP HEADER**************/
/************** IP HEADER ***************/
iph = (struct iphdr *) skb_push(newPacket,
sizeof(struct iphdr));
/* set IP header here */
iph->ihl = 5;/* 5 * 32(bits) */
iph->version = 4;
iph->tos = 0; /* The recommended value by IANA for ICMP request */
iph->tot_len = htons( sizeof(struct iphdr)
+ sizeof(struct icmphdr)
+ length);
iph->id = 0;
iph->frag_off = 0; /* No fragementation */
iph->ttl = 65;
iph->protocol = IPPROTO_ICMP;
iph->saddr = source;
iph->daddr = dest;
iph->check = 0;
iph->check = in_cksum((unsigned short *)iph, sizeof(struct iphdr));
/************** END IP HEADER ***************/
// if(ip_local_out(newPacket) < 0) THIS CRASHES WITH A NULL POINTER
// return SEND_FAIL_SEND;
++sendCounter;
return SEND_SUCCESS;
}
/* end sendICMPEcho */
In my attempts when trying to solve this, I've tried to manually set the
net_device in the skb and that didn't work either; I still had the same
kernel panic.
So I am sorry for the trouble, any hint where the error can be?
Regards,
Adel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130617/9df461a3/attachment-0001.html
More information about the Kernelnewbies
mailing list