系统调用undefined!编译内核模块

时间:2016-10-03 16:57:58

标签: c linux-kernel kernel-module

我是内核模块编程的新手,并尝试使用'sys_socket'在我的内核模块中创建原始套接字。我使用'sys_bind'和'sys_ioctl'(用于获取接口号)将此套接字绑定到接口。

在编译我的模块时,我收到以下错误:

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'

如何解决此问题?

代码如下:

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 个答案:

答案 0 :(得分:1)

sys_bind()sys_ioctl(以及sys_socket)是内核的(低级)用户空间接口。您还没有向我们展示您编写的代码,但您需要使用更合适的内核内部接口进行网络访问。