c - 在linux内核中发送数据包
问题描述
我写了一个模块来在内核空间发送一个数据包。但在insmod
它给出分段错误错误之后。我试图改变它的某些部分,但我仍然得到错误。
编码:
//libraries
#define IP_Header_RM 20
#define UDP_Header_RM 8
static int __init init_Module(void){
unsigned char *Data = "Test_Packet";
int i = strlen(Data);
struct sk_buff* skb = alloc_skb(ETH_HLEN + IP_Header_RM + UDP_Header_RM + i, GFP_ATOMIC);
struct iphdr* iph = (struct iphdr*)skb_push(skb, IP_Header_RM);
struct ethhdr* eth = (struct ethhdr*)skb_push(skb, sizeof (struct ethhdr));
struct udphdr* uh = (struct udphdr*)skb_push(skb, UDP_Header_RM);
struct net_device *Device;
uint16_t proto;
uint8_t Mac_Addr[ETH_ALEN] = {0x38, 0xd5, 0x47, 0xa1, 0x07, 0x41};
Data = skb_put(skb, i);
skb_reserve(skb, ETH_HLEN);
Device = dev_get_by_name(&init_net,"enp0s3");
proto = ETH_P_IP;
uh->len = htons(i);
uh->source = htons(2121);
uh->dest = htons(2121);
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len= htons(IP_Header_RM + i);
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = IPPROTO_UDP;
iph->check = 0;
iph->saddr = 19216805;
iph->daddr = 19216804;
skb->protocol = eth->h_proto = htons(proto);
skb->no_fcs = 1;
memcpy(eth->h_source, Device->dev_addr, ETH_ALEN);
memcpy(eth->h_dest, Mac_Addr, ETH_ALEN);
skb->pkt_type = PACKET_OUTGOING;
dev_queue_xmit(skb);
return 1;
}
static void __exit exit_Module(void){
printk(KERN_INFO "Done");
}
module_init(init_Module);
module_exit(exit_Module);
我犯了哪些错误?
提前致谢
解决方案
在对用户数据执行任何 skb_put() 调用或对 eth/IP 标头执行 skb_push() 之前,您需要在分配的缓冲区上预先执行 skb_reserve()。您首先尝试 skb_push() 在没有适当保留的缓冲区上进行分段错误。我还有一些其他的建议给你:
- 下次包括您的完整来源!
- 重新排列推送标头的顺序以生成合法的 UDP/IP 数据包
- dev_get_by_name() 可能会失败;在尝试 memcpy 到它的缓冲区之前应该检查一下
- 在任何 eth/IP 标头之前推送用户数据
- 从模块的 init() 返回 0 而不是 1 表示成功
像这样的教程页面可能有助于将它们放在一起:http: //vger.kernel.org/~davem/skb_data.html 下面的代码在我的 Debian 10 系统上没有 4.19 Linux 内核的段错误。
//libraries
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/module.h>
#include <linux/virtio.h>
#include <linux/virtio_net.h>
#include <linux/bpf.h>
#include <linux/bpf_trace.h>
#include <linux/scatterlist.h>
#include <linux/if_vlan.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/average.h>
#include <linux/filter.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <net/route.h>
#include <net/xdp.h>
#include <net/net_failover.h>
#define IP_Header_RM 20
#define UDP_Header_RM 8
static int __init init_Module(void){
unsigned char *Data = "Test_Packet";
int i = strlen(Data);
struct sk_buff* skb = alloc_skb(ETH_HLEN + IP_Header_RM + UDP_Header_RM + i, GFP_ATOMIC);
struct net_device *Device;
uint16_t proto;
struct iphdr* iph;
struct ethhdr* eth;
struct udphdr* uh;
uint8_t Mac_Addr[ETH_ALEN] = {0x38, 0xd5, 0x47, 0xa1, 0x07, 0x41};
skb_reserve(skb, ETH_HLEN + IP_Header_RM + UDP_Header_RM + i);
Data = skb_put(skb, i);
iph = (struct iphdr*)skb_push(skb, IP_Header_RM);
uh = (struct udphdr*)skb_push(skb, UDP_Header_RM);
eth = (struct ethhdr*)skb_push(skb, sizeof (struct ethhdr));
Device = dev_get_by_name(&init_net,"enp0s3");
if (Device == NULL) {
printk(KERN_INFO "init_Module: no such device enp0s3\n");
return 1;
}
proto = ETH_P_IP;
uh->len = htons(i);
uh->source = htons(2121);
uh->dest = htons(2121);
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len= htons(IP_Header_RM + i);
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = IPPROTO_UDP;
iph->check = 0;
iph->saddr = 19216805;
iph->daddr = 19216804;
skb->protocol = eth->h_proto = htons(proto);
skb->no_fcs = 1;
memcpy(eth->h_source, Device->dev_addr, ETH_ALEN);
memcpy(eth->h_dest, Mac_Addr, ETH_ALEN);
skb->pkt_type = PACKET_OUTGOING;
dev_queue_xmit(skb);
return 0;
}
static void __exit exit_Module(void){
printk(KERN_INFO "Done");
}
module_init(init_Module);
module_exit(exit_Module);
MODULE_DESCRIPTION("Stack Overflow 66846959");
MODULE_LICENSE("GPL");
推荐阅读
- python - 输出到名称包含特殊字符的文件(bash 脚本 linux)
- android - 使用 Retrofit 和 Back4app Api Rest 得到 401 错误
- linux - 如何从 ARM64 的用户空间调用内核模块中的函数?
- tensorflow - 如何在 Keras 的 ResNet 152 V2 中使用 TFrecord
- docker-compose - 从 Traefik 中的 env var 配置多个域的推荐方法是什么?
- javascript - 有没有一种可能的方法来使用量角器定位输入字段的自动建议下拉菜单?
- angular - Angular 9,直到下次点击才会触发动作
- firebase - 无法读取规范“Firebase (7.9.0)”的许可文件“LICENSE”
- react-native - 用不同颜色显示直线反应原生
- pandas - Pandas groupby agg nunique 许多列