LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 02-05-2010, 05:02 PM   #1
bostan
LQ Newbie
 
Registered: Feb 2010
Posts: 14

Rep: Reputation: 0
Raw Socket in Kernal space


Hi All,
Is it possible to use create a raw socket in kernel space ? If possible is there any snippet/code which I can use to enhance my application ?
Any help is highly appreciated.
-Thanks in advance
 
Old 02-05-2010, 05:17 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Hi,

it is possible to create a raw socket it kernel mode using the sock_create function defined in net/socket.c in the kernel sources.

Code:
#include <linux/net/net.h>

...

struct socket * sockptr;
int error;

if((error = sock_create( PF_INET, SOCK_RAW, 6, &sockptr )) < 0)
 {
   printk(KERN_ERR "cannot create raw socket\n");
   return -EIO;
 }

/* use sockptr */

kernel_sock_shutdown( sockptr, SHUT_RDWR );
But why do you want to create a socket in kernel space? Are you writing some kind of kernel module?

Last edited by irmin; 02-05-2010 at 05:50 PM.
 
Old 02-05-2010, 05:41 PM   #3
bostan
LQ Newbie
 
Registered: Feb 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks Irmin,
Does this mean I need to call setsockopt to inform the kernal not to add the headers and create my own ip pkt ? Or there is any equivalant system call for it ?
-Thanks
 
Old 02-05-2010, 05:49 PM   #4
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
If you do not want the IPv4 layer to create an IP header, you have to set the option IP_HDRINCL. (see man 7 raw)
Use kernel_setsockopt for this. Basically the kernel socket interface is the same as the system calls.

See <kernel-source>/include/linux/net.h for details.

But I wonder, if this really what you want to do, since sending and receiving raw ip packets can also be done using socket/setsockopt/sendmsg/recvmsg system calls.
 
Old 02-08-2010, 10:22 AM   #5
bostan
LQ Newbie
 
Registered: Feb 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks Irmin,
This was helpful. I really appreciate it.

BTW what kind of header files I need ? I tried these hdr files but its not compiling and giving me error of files not found. I tried to compile this on Linux machine as well as Vmware.
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <linux/net/net.h>
#include <linux/fs.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/buffer_head.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>


-Thanks
 
Old 02-08-2010, 11:22 AM   #6
bostan
LQ Newbie
 
Registered: Feb 2010
Posts: 14

Original Poster
Rep: Reputation: 0
how to build the code with kernal system calls

Hi,
I have created the foo.c file for socket. I am calling the sock_sendmsg and etc as kernal system calls directly. I have included the files which are needed for this calls.
My question is should I build this foo.c using "gcc foo.c" or I have to build this file in kernal mode and than load the kernal using insmode ?
Any help is good.
-Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw socket programming with C arabindav Programming 17 06-09-2011 12:58 PM
Raw Socket Forwarding robtard Programming 1 05-05-2009 07:34 AM
Using Raw Socket for UDP myself_rajat Linux - Networking 0 05-17-2004 04:35 AM
Socket Raw linuxanswer Programming 1 04-01-2004 10:43 PM
ping without raw socket linuxnw Linux - Networking 3 12-10-2003 12:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration