0

I am new to kernel module programming, and trying to create raw socket in my kernel module using 'sys_socket'. I am binding this socket to an interface using 'sys_bind' and 'sys_ioctl' (for fetching interface number).

While compiling my module, I get the following errors:

make -C /lib/modules/3.13.0-68-generic/build M=/root/Kernel_Module modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-68-generic'
CC [M]  /root/Kernel_Module/kernel_module.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "sys_ioctl" [/root/Kernel_Module/kernel_module.ko] undefined!
WARNING: "sys_bind" [/root/Kernel_Module/kernel_module.ko] undefined!
WARNING: "sys_socket" [/root/Kernel_Module/kernel_module.ko] undefined!
CC      /root/Kernel_Module/kernel_module.mod.o
LD [M]  /root/Kernel_Module/kernel_module.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-68-generic'

How can I resolve this issue?

The code is specified below:

struct sockaddr_ll socketBindAddr;
struct ifreq       ifreq;
int                socketFd = 0;

socketFd = sys_socket (AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (socketFd < 0)
{
    printk (KERN_CRIT "Failed to open data socket!!\r\n");
    return OFC_FAILURE;
}

memset (&ifreq, 0, sizeof(ifreq));
strcpy (ifreq.ifr_name, gOfcGlobals.aDataIfName);
if (sys_ioctl (socketFd, SIOCGIFINDEX, (unsigned long) &ifreq) < 0)
{
    printk (KERN_CRIT "Ioctl failed to get IfIndex!!\r\n");
    return OFC_FAILURE;
}

memset (&socketBindAddr, 0, sizeof(socketBindAddr));
socketBindAddr.sll_family = AF_PACKET;
socketBindAddr.sll_protocol = htons(ETH_P_ALL);
if (sys_bind (socketFd, (struct sockaddr *) &socketBindAddr,
    sizeof(socketBindAddr)) < 0)
{
    printk (KERN_CRIT "Failed to bind socket\r\n");
    return OFC_FAILURE;
}
1
  • In short: Syscalls are for user-space programs, not for kernel modules. Some syscalls have analogues for in-kernel use. I don't know about in-kernel analogues for ioctl, bind and socket. Commented Oct 3, 2016 at 17:11

1 Answer 1

1

sys_bind() and sys_ioctl (and sys_socket) are the (low-level) user-space interface to the kernel. You haven't shown us the code you've written, but you'll need to use a more suitable kernel-internal interface for your net access.

Sign up to request clarification or add additional context in comments.

3 Comments

Added code. Kindly suggest if a procedure to create sockets using kernel module.
Sorry, I've never done high-level networking in a kernel module. I had a look at NFS to see how it opens connections, but got lost - perhaps NBD is a simpler starting point? Try reading /drivers/block/nbd.c, perhaps.
Sockets can be created in the kernel using the following functions: sock_create() /* Create Socket / sock->ops->bind () / Bind Socket / sock->ops->release () / Close Socket */ struct socket is the structure for sockets in kernel, instead of file descriptors in user space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.