LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 04: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, 04: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 04:50 PM.
 
Old 02-05-2010, 04: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, 04: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, 09: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, 10: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



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 11:58 AM
Raw Socket Forwarding robtard Programming 1 05-05-2009 06:34 AM
Using Raw Socket for UDP myself_rajat Linux - Networking 0 05-17-2004 03:35 AM
Socket Raw linuxanswer Programming 1 04-01-2004 09:43 PM
ping without raw socket linuxnw Linux - Networking 3 12-09-2003 11:57 PM

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

All times are GMT -5. The time now is 03:50 PM.

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
Open Source Consulting | Domain Registration