[英]send packet in linux kernel
I wrote a module to send a packet in kernel-space.我写了一个模块来在内核空间发送一个数据包。 but after insmod
it gives a segmentation fault error.但是在insmod
之后它给出了分段错误错误。 I have tried to change some parts of it but I still get errors.我试图改变它的某些部分,但我仍然得到错误。
the code:编码:
//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);
which parts I made mistake?我犯了哪些错误?
Thanks in Advance提前致谢
You need to do skb_reserve() up front on the allocated buffer, before doing any skb_put() calls for user data or skb_push() for eth/IP headers.在对用户数据执行任何 skb_put() 调用或对 eth/IP 标头执行 skb_push() 之前,您需要在分配的缓冲区上预先执行 skb_reserve()。 You were segfaulting trying to skb_push() first, on a buffer without proper reservations.您首先尝试 skb_push() 在没有适当保留的缓冲区上进行分段错误。 I also had a few other suggestions for you:我还有一些其他的建议给你:
A tutorial page like this may help to put it all together: http://vger.kernel.org/~davem/skb_data.html The code below does not segfault on my Debian 10 system with a 4.19 Linux kernel. A tutorial page like this may help to put it all together: http://vger.kernel.org/~davem/skb_data.html The code below does not segfault on my Debian 10 system with a 4.19 Linux kernel.
If this answer helps you, please mark as the accepted one - thanks.如果此答案对您有帮助,请标记为已接受 - 谢谢。
//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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.