I wrote a module to send a packet in kernel-space. but after insmod
it gives a segmentation fault error. 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 8static 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