I have a kernel module that would like to send pre-fabricated ethernet frames from user space such as custom ARP, and other protocols (I'm trying to bypass tcp/ip stack on linux and create custom one for my needs). Frames are valid and complete with all necessary things. The only part that remains is to send them somehow to the queue on eth0 interface. What is the best solution to do this?
For snatching incoming packets I am using netfilter API with the earliest hook possible. I can not use raw sockets from user space due to the need of sudo and also due to my custom requirements.
Edit: I was able to achieve my goals with dev_queue_xmit()
. However, I am still wondering if there is another solution that accesses the driver directly.
static void SendFrame(void)
{
struct sk_buff* skb = dev_alloc_skb(1518);
skb->dev = __dev_get_by_name(&init_net, "eth0");
skb_reserve(skb, NET_IP_ALIGN);
skb->data = skb_put(skb, ethFrameBytes);
memcpy(skb->data, pEthFrame, ethFrameBytes);
if (dev_queue_xmit(skb) != NET_XMIT_SUCCESS)
{
printk(KERN_ERR, KERN_ERR "Error: unable to send the frame
");
}
}
question from:
https://stackoverflow.com/questions/65842703/sending-ethernet-frames-from-kernel-module 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…