Tag: kernel module

如何在没有内核崩溃的情况下优雅地禁用中断线?

我已经实现了一个从keyboad读取的程序,并扫描代码并将其放入tasklet中。 tasklet取消阻塞read()。 因此,我的QT应用程序可以读取数据,如果它找到l的扫描代码,它会触发对Qt-webkit的回调。 但是,当我在做我的角色驱动程序的rmmod时。 整个内核崩溃了。 我的角色驱动程序有什么问题。 #include #include /** needed by all modules **/ #include /** This is for KERN_ALERT **/ #include /** for file operations **/ #include /** character device **/ #include /** for sys device registration in /dev/ and /sys/class **/ /** for copy_to_user **/ #include /** kernel thread */ #include /** spin […]

linux-kernel模块最大分配内存

我想编写一个模块,其任务是捕获传入的数据包,而不将它们发送到用户空间应用程序并对捕获的数据包进行一些修改。 然后该模块将发送此数据包以传输到NIC。 但主要的问题是我的模块尺寸非常大而且还进行了大量的处理。 因此,在内核模块内部进行此处理是好的,还是应该将信息和数据包传递给用户空间进行处理以避免复杂性。 &我这样做只是为了快速获取数据包处理。 所以最大化linux内核模块可以分配多少内存。

为什么`synchronize_rcu()`在读取锁定块内调用时没有死锁?

synchronize_rcu()用于仅等待正在进行的RCU读取端关键部分完成。 如果是这样,它应该在读取块内调用时永远被阻止。 但是,以下代码在我的linux内核上运行良好,为什么? void port_range_clean( void ) { struct port_range *p; redo: rcu_read_lock(); list_for_each_entry_rcu(p, &port_rt->ports, list) { list_del_rcu(&p->list); synchronize_rcu(); rcu_read_unlock(); kfree(p); goto redo; } }

Nf_hook_ops在分配给hook_func -C -Linux -Netfilter时返回不兼容的指针

我试图在Ubuntu 16.04 LTS上编写自己的Netfilter内核模块,我试图将hook_func分配给nfho.hook,但是我收到以下错误: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types] nfho.hook = hook_func; 我已经看过其他解决方案,大多数都是双重检查参数,包括将*skb更改为**skb 。 我已经读过,参数可能取决于内核版本,但无法找到如何找到要传递的正确参数。 我怎样才能使它工作,我如何检查应该在我的内核版本的hook_func中传递什么参数? 完整代码: #include #include #include #include #include #include #include static struct nf_hook_ops nfho; //struct holding set of hook function options // function to be called by hook unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, […]

在64位拱上投射指向整数问题警告的指针

我正在编写一个使用导出符号open_exec的linux内核模块 struct file *open_exec(const char *name) 它返回一个指针,我可以检查IS_ERR宏的错误: if (IS_ERR(file)) return file; 在编译期间,我收到此警告: warning: return makes integer from pointer without a cast 这是因为我的函数返回一个整数。 如果我尝试施放它: return (int) file; 我没有在我的32位机器上收到警告,但我在我的64位机器上发出警告: warning: cast from pointer to integer of different size 这是因为int和指针的sizeof在32位上是相同的,但它们在64位机器上是不同的。 无论是否投射,代码似乎都有效。 我只是想摆脱警告。 如何正确地转换指向整数的指针并获得我期望的值,同时没有得到编译器警告? 我期望的值本质上是linux内核代码库的include/asm-generic/errno-base.h中列出的整数。 由于我只是在IS_ERR()为真的情况下将指针看作是一个整数,我可以肯定它实际上只保存一个整数值。

使用sk_buff添加以太网帧头

我有一个内核模块,可以捕获传出的Internet流量(Netfilter hook:LOCAL_OUT)在这个钩子上,仍然没有以太网头。 我构建了以太网头,它已经可以使用了,但是如何将它附加到skb以便我可以将整个skb结构发送到dev_queue_xmit() ? 是否有关于如何操作sk_buff数据的指南,您可以提供给我以获取更多信息? 作为样本,我尝试在所有ECHO ICMP流量上做我想做的事情; 这是我的代码。 但是当在另一台机器上检查Wireshark时,我得到了一个工作的以太网头但是一个EMPTY IP数据包……为什么会这样? static unsigned int post_icmp_check(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) { struct iphdr *iph; struct icmphdr *icmph; struct sk_buff *sb; struct ethhdr *ethh; unsigned char *frame = NULL; //Used just to copy the skb->data […]

Linux内核与用户空间程序之间的通信

我目前正在编写Linux内核模块,并且在实现与用户空间程序的通信时遇到问题。 此内核模块需要接收用户空间程序发出的任务,并在完成后将结果发送回用户空间程序。 在内核模块执行其工作时,应阻止用户空间程序。 我认为内核用户空间IPC或Unix套接字会很好,但我没有找到谷歌的例子。 目前我丑陋的解决方案是导出chardev并让用户空间程序将请求写入设备文件,并从中读取结果。 但我只能在每次open()调用时发出一个请求,这会导致新问题。 我真的需要一个IPC或类似套接字的东西。 谢谢!