加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在内核模块中创建一个以太网数据包并发送它

发布时间:2020-12-13 22:52:29 所属栏目:Linux 来源:网络整理
导读:我需要创建一个以太网数据包并在我的内核模块中发送它.有人可以帮我这样做吗? 我想我需要使用dev_alloc_skb创建一个skb,然后我需要编写mac_ethernet,插入数据并使用dev_queu_xmit发送它. 但我不确定这是否有效,或者它是否是正确和最简单的方法. 最好的祝福
我需要创建一个以太网数据包并在我的内核模块中发送它.有人可以帮我这样做吗?

我想我需要使用dev_alloc_skb创建一个skb,然后我需要编写mac_ethernet,插入数据并使用dev_queu_xmit发送它.

但我不确定这是否有效,或者它是否是正确和最简单的方法.

最好的祝福

EDIT1:

int sendpacket ()
{
    unsigned char dest[ETH_ALEN]={0x00,0x25,0x22,0x05,0xF3,0xF0};
    unsigned char src[ETH_ALEN] = {0x90,0xE6,0xBA,0x48,0x7C,0x87};

struct sk_buff * skbt =alloc_skb(ETH_FRAME_LEN,GFP_KERNEL);      
//skb_reserve(skb,ETH_FRAME_LEN);


dev_hard_header(skbt,dev_eth1,ETH_P_802_3,dest,src,dev_eth1->addr_len);

if(dev_queue_xmit(skbt)!=NET_XMIT_SUCCESS)
{
    printk("Not send!!n");
}



kfree_skb(skbt);
return 0;

}

> Dmesg command:
> 
>  677.826933] Hello:I'm the hook module!!!! [  677.826937] 2!!!! [  677.826941] skb_under_panic: text:c0723608 len:14 put:14 head:f1843800 data:f18437f2 tail:0xf1843800 end:0xf1843e00 dev:<NULL> [  677.826959]
> ------------[ cut here ]------------ [  677.826961] kernel BUG at net/core/skbuff.c:146! [  677.826964] invalid opcode: 0000 [#1] SMP  [
> 677.826967] last sysfs file: /sys/devices/system/cpu/cpu1/cache/index2/shared_cpu_map [ 
> 677.826969] Modules linked in: sendpacket(+) bluetooth rfkill vfat fat fuse sunrpc cpufreq_ondemand acpi_cpufreq mperf ip6t_REJECT
> nf_conntrack_ipv6 ip6table_filter ip6_tables ipv6 uinput
> snd_hda_codec_atihdmi snd_hda_codec_realtek snd_hda_intel
> snd_hda_codec snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd
> soundcore atl1e snd_page_alloc iTCO_wdt iTCO_vendor_support r8169 mii
> i2c_i801 microcode asus_atk0110 pcspkr ata_generic pata_acpi
> usb_storage pata_marvell radeon ttm drm_kms_helper drm i2c_algo_bit
> i2c_core [last unloaded: sendpacket] [  677.827003]  [  677.827003]
> Pid: 4780,comm: insmod Tainted: G        W   2.6.35101 #7 P5QL
> PRO/P5QL PRO [  677.827003] EIP: 0060:[<c070a192>] EFLAGS: 00210246
> CPU: 0 [  677.827003] EIP is at skb_push+0x57/0x62 [  677.827003] EAX:
> 00000088 EBX: c08f9fdc ECX: f156bf10 EDX: c093b4ca [  677.827003] ESI:
> 00000000 EDI: f51ca000 EBP: f156bf38 ESP: f156bf0c [  677.827003]  DS:
> 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 [  677.827003] Process insmod
> (pid: 4780,ti=f156a000 task=f2b071a0 task.ti=f156a000) [  677.827003]
> Stack: [  677.827003]  c093b4ca c0723608 0000000e 0000000e f1843800
> f18437f2 f1843800 f1843e00 [  677.827003] <0> c08f9fdc f156bf64
> f156bf6a f156bf50 c0723608 00000001 c07235e5 f3b6c000 [  677.827003]
> <0> 00835ff4 f156bf78 f7d640a8 f156bf6a f156bf64 00000006 48bae690
> 2500877c [  677.827003] Call Trace: [  677.827003]  [<c0723608>] ?
> eth_header+0x23/0x93 [  677.827003]  [<c0723608>] ?
> eth_header+0x23/0x93 [  677.827003]  [<c07235e5>] ?
> eth_header+0x0/0x93 [  677.827003]  [<f7d640a8>] ?
> sendpacket+0x8f/0xb6 [sendpacket] [  677.827003]  [<f7d67000>] ?
> hook_init+0x0/0x46 [sendpacket] [  677.827003]  [<f7d67044>] ?
> hook_init+0x44/0x46 [sendpacket] [  677.827003]  [<c0401246>] ?
> do_one_initcall+0x4f/0x139 [  677.827003]  [<c0451e29>] ?
> blocking_notifier_call_chain+0x11/0x13 [  677.827003]  [<c046210c>] ?
> sys_init_module+0x7f/0x19b [  677.827003]  [<c040321f>] ?
> sysenter_do_call+0x12/0x28 [  677.827003] Code: c0 85 f6 0f 45 de 53
> ff b0 a8 00 00 00 ff b0 a4 00 00 00 51 ff b0 ac 00 00 00 52 ff 70 50
> ff 75 04 68 ca b4 93 c0 e8 ad 4a 09 00 <0f> 0b 8d 65 f8 89 c8 5b 5e 5d
> c3 55 89 e5 56 53 0f 1f 44 00 00  [  677.827116] EIP: [<c070a192>]
> skb_push+0x57/0x62 SS:ESP 0068:f156bf0c [  677.827154] ---[ end trace
> dee1e3278503a581 ]---

解决方法

在您的情况下,您只想使用来自用户空间的原始数据包,而不是处理内核代码的复杂性.

这个blog post详细介绍了如何做你需要的一切.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读