Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Kernel Socket Programming -- Send raw ethernet frames using sock_sendmsg

831 views
Skip to first unread message

sid

unread,
Oct 8, 2004, 7:15:04 PM10/8/04
to
Hi all,
I want to send ARP request packets through an interface from
within a kernel module but was not successful in generating any raw
ethernet packets. I would be glad if you can point me to some source
that is available or if you can recommend any books related to Kernel
Socket Programming and generating raw ethernet frames.
I was trying to send ARP requests by using the following function
inside a kernel module but I always get an error saying "sock_sendmsg
error: -107". Am I missing something?? Would really appreciate if you
can look into this code and tell me what I am doing wrong.

int send_arp_request(void)
{
struct socket *arpsock;
struct sockaddr sa;
struct msghdr msg;
struct iovec iov;
mm_segment_t oldfs;
int size = 0;
int error = 0;
char *warp_dev="eth0";
char dev[20];
char*buf="ffffffffffff0060b31ecda3080600010800060400010060b31ecda3c0a80401000000000000c0a8040239e000003078000035a40000000600010000";

if ((error=sock_create(PF_INET, SOCK_PACKET, htons(ETH_P_ARP),
&arpsock))<0) {
printk(KERN_WARNING "Could not create a PF_PACKET SOCK_RAW
Socket\n");
return (-1);
}

memset(&sa,0,sizeof(sa));
strcpy(dev,warp_dev);
strncpy(sa.sa_data,dev,sizeof(sa.sa_data));

error = arpsock->ops->bind(arpsock,&sa,sizeof(sa));

if (error<0)
{
printk(KERN_WARNING "Error binding socket");
return -1;
}

iov.iov_base = (char *)buf;
iov.iov_len = (__kernel_size_t) strlen(buf);

msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_flags = 0;

oldfs = get_fs();
set_fs(KERNEL_DS);
size = sock_sendmsg(arpsock,&msg,strlen(buf));
set_fs(oldfs);

if (size < 0)
printk(KERN_WARNING "sock_sendmsg error: %d\n",size);

return 0;

}


Thanks & Regards,
Sid

Tauno Voipio

unread,
Oct 9, 2004, 4:26:59 AM10/9/04
to
sid wrote:
> Hi all,
> I want to send ARP request packets through an interface from
> within a kernel module but was not successful in generating any raw
> ethernet packets. I would be glad if you can point me to some source
> that is available or if you can recommend any books related to Kernel
> Socket Programming and generating raw ethernet frames.

> I was trying to send ARP requests by using the following function
> inside a kernel module but I always get an error saying "sock_sendmsg
> error: -107". Am I missing something?? Would really appreciate if you
> can look into this code and tell me what I am doing wrong.

> char*buf="ffffffffffff0060b31ecda3080600010800060400010060b31ecda3c0a80401000000000000c0a8040239e000003078000035a40000000600010000";

This is a long string of ASCII characters, and it is not the proper
contents of the Ethernet frame. You ought to convert the hex text
to binary bytes before attempting a send.

Get the arping sources <http://freshmeat.net/projects/arping/>.

HTH

Tauno Voipio
tauno voipio (at) iki fi

sid

unread,
Oct 11, 2004, 1:47:14 PM10/11/04
to
Thanks for replying....

> > >char*buf="ffffffffffff0060b31ecda3080600010800060400010060b31ecda3c0a80401000000000000c0a8040239e000003078000035a40000000600010000";
>
> This is a long string of ASCII characters, and it is not the proper
> contents of the Ethernet frame. You ought to convert the hex text
> to binary bytes before attempting a send.

Captured an arp packet that is sent by the same machine and tried to
send the same arp packet in binary from inside the kernel module but
it still didn't work. Any other ideas?


>
> Get the arping sources <http://freshmeat.net/projects/arping/>.

Looks like they create sockets and send packets from user space. When
I am trying to send packets from kernel space, I was not able to use
the same functions.


Sid

Tauno Voipio

unread,
Oct 11, 2004, 2:21:57 PM10/11/04
to

Woud you please tell why?

It could, maybe, help us to give the proper answers.

If you're trying to do some kernel programming, you should
know enough of C to separate a hex string and a binary
byte vector from each other.

0 new messages