【发布时间】:2021-06-25 01:18:19
【问题描述】:
我写了一个模块来在内核空间发送一个数据包。但在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);
我犯了哪些错误?
提前致谢
【问题讨论】:
-
memcpy(eth->h_dest, Mac_Addr, ETH_ALEN); -
哪一行 exact 会导致段错误?换句话说,执行到哪一行没有错误?
skb变量是如何定义的? -
@Tsyvarev 我在
int i = strlen(data)之后更新了代码 skbuff alocate。 SF 与uh->dest = htons(2121);之前的行相关 -
根据description,
skb_reserve只允许用于 empty 缓冲区。所以你需要在任何skb_push之前调用它。
标签: c sockets networking module kernel