关于C#:Linux-从内核内部发送数据包

Linux - Sending packets from inside the kernel

我需要创建一个可加载的内核模块,该模块将数据发送到另一台远程计算机。
我正在写4.10内核。
我尝试了netpoll API,但我所得到的只是错误,并且找不到内核内有关套接字编程的任何新信息和最新信息。
谁能给我一个例子(或任何指示)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int init_netpoll(void)
{
    np_t.name ="LRNG";
    strlcpy(np_t.dev_name,"enp3s0", IFNAMSIZ);
    np_t.local_ip.ip = htonl((unsigned long int)0xc6a80121);
    np_t.local_ip.in.s_addr = htonl((unsigned long int)0xc6a80121);
    np_t.remote_ip.ip = htonl((unsigned long int)0xc6a80114);
    np_t.remote_ip.in.s_addr = htonl((unsigned long int)0xc6a80114);
    np_t.ipv6 = 0;//no IPv6
    np_t.local_port = 6666;
    np_t.remote_port = 80;
    ///////////////

    ///////////////
    memset(np_t.remote_mac, 0xff, ETH_ALEN);
    netpoll_print_options(&np_t);
    if(!netpoll_setup(&np_t)) {
        return 1;
    }
    np = &np_t;
    return 0;
}

void clean_netpoll(void)
{
  //nothing
}

void sendUdp(const char* buf)
{
    printk("********** SEND UDP **********");
    netpoll_send_udp(np,buf,strlen(buf));
}

日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[  278.702352] ********** SEND UDP **********
[  278.702353] ------------[ cut here ]------------
[  278.702358] WARNING: CPU: 2 PID: 2153 at /build/linux-nhaT8l/linux-4.10.0/net/core/netpoll.c:394 netpoll_send_udp+0x443/0x450
[  278.702359] Modules linked in: netCharDev(OE+) rfcomm bnep btusb btrtl btbcm btintel bluetooth uvcvideo videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 videobuf2_core videodev media wl(POE) intel_rapl x86_pkg_temp_thermal intel_powerclamp snd_hda_codec_hdmi coretemp kvm irqbypass binfmt_misc crct10dif_pclmul snd_hda_codec_realtek snd_hda_codec_generic crc32_pclmul ghash_clmulni_intel cryptd intel_cstate intel_rapl_perf snd_hda_intel snd_hda_codec snd_hda_core snd_hwdep snd_pcm joydev input_leds serio_raw snd_seq_midi snd_seq_midi_event snd_rawmidi thinkpad_acpi nvram snd_seq snd_seq_device cfg80211 snd_timer lpc_ich snd shpchp mei_me mei soundcore mac_hid parport_pc ppdev lp parport ip_tables x_tables autofs4 uas usb_storage i915 i2c_algo_bit drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops
[  278.702393]  psmouse ahci drm r8169 libahci mii wmi fjes video
[  278.702399] CPU: 2 PID: 2153 Comm: insmod Tainted: P           OE   4.10.0-33-generic #37-Ubuntu
[  278.702400] Hardware name: LENOVO 62742BG/62742BG, BIOS H1ET69WW (1.12 ) 11/15/2012
[  278.702400] Call Trace:
[  278.702404]  dump_stack+0x63/0x81
[  278.702406]  __warn+0xcb/0xf0
[  278.702409]  warn_slowpath_null+0x1d/0x20
[  278.702411]  netpoll_send_udp+0x443/0x450
[  278.702413]  ? netpoll_setup+0x166/0x2d0
[  278.702416]  sendUdp+0x32/0x35 [netCharDev]
[  278.702417]  ? 0xffffffffc025d000
[  278.702420]  netCharDev_init+0x26/0x1000 [netCharDev]
[  278.702421]  ? 0xffffffffc025d000
[  278.702423]  do_one_initcall+0x52/0x1b0
[  278.702426]  ? kmem_cache_alloc_trace+0x142/0x190
[  278.702428]  do_init_module+0x5f/0x200
[  278.702432]  load_module+0x190b/0x1c70
[  278.702433]  ? __symbol_put+0x60/0x60
[  278.702436]  ? ima_post_read_file+0x7e/0xa0
[  278.702438]  ? security_kernel_post_read_file+0x6b/0x80
[  278.702440]  SYSC_finit_module+0xdf/0x110
[  278.702443]  SyS_finit_module+0xe/0x10
[  278.702445]  entry_SYSCALL_64_fastpath+0x1e/0xad
[  278.702447] RIP: 0033:0x7feb867b39f9
[  278.702448] RSP: 002b:00007ffd0e884438 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[  278.702449] RAX: ffffffffffffffda RBX: 0000563b09907a70 RCX: 00007feb867b39f9
[  278.702450] RDX: 0000000000000000 RSI: 0000563b091e7f8b RDI: 0000000000000003
[  278.702451] RBP: 00007feb86a72b00 R08: 0000000000000000 R09: 00007feb86a74ea0
[  278.702452] R10: 0000000000000003 R11: 0000000000000246 R12: 00007feb86a72b58
[  278.702453] R13: 00007feb86a72b58 R14: 000000000000270f R15: 0000000000001010
[  278.702455] ---[ end trace ca1c43e67bc5a307 ]---
[  278.702460] BUG: unable to handle kernel NULL pointer dereference at 0000000000000040
[  278.702507] IP: netpoll_send_udp+0x30/0x450
[  278.702528] PGD 0

[  278.702550] Oops: 0000 [#1] SMP
[  278.702567] Modules linked in: netCharDev(OE+) rfcomm bnep btusb btrtl btbcm btintel bluetooth uvcvideo videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 videobuf2_core videodev media wl(POE) intel_rapl x86_pkg_temp_thermal intel_powerclamp snd_hda_codec_hdmi coretemp kvm irqbypass binfmt_misc crct10dif_pclmul snd_hda_codec_realtek snd_hda_codec_generic crc32_pclmul ghash_clmulni_intel cryptd intel_cstate intel_rapl_perf snd_hda_intel snd_hda_codec snd_hda_core snd_hwdep snd_pcm joydev input_leds serio_raw snd_seq_midi snd_seq_midi_event snd_rawmidi thinkpad_acpi nvram snd_seq snd_seq_device cfg80211 snd_timer lpc_ich snd shpchp mei_me mei soundcore mac_hid parport_pc ppdev lp parport ip_tables x_tables autofs4 uas usb_storage i915 i2c_algo_bit drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops
[  278.705010]  psmouse ahci drm r8169 libahci mii wmi fjes video
[  278.705917] CPU: 2 PID: 2153 Comm: insmod Tainted: P        W  OE   4.10.0-33-generic #37-Ubuntu
[  278.706956] Hardware name: LENOVO 62742BG/62742BG, BIOS H1ET69WW (1.12 ) 11/15/2012
[  278.707781] task: ffff95ea2cc78000 task.stack: ffffa707c2318000
[  278.708618] RIP: 0010:netpoll_send_udp+0x30/0x450
[  278.709876] RSP: 0018:ffffa707c231bc30 EFLAGS: 00010286
[  278.711806] RAX: 0000000000000024 RBX: ffffffffc07e33fa RCX: 0000000000000006
[  278.713766] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000009
[  278.715709] RBP: ffffa707c231bc70 R08: 0000000000000001 R09: 000000000000035c
[  278.716998] R10: ffffffffafe06a40 R11: 0000000000000000 R12: 0000000000000000
[  278.719051] R13: 0000000000000004 R14: ffff95ea0b9b7240 R15: ffff95ea0b9b7288
[  278.721031] FS:  00007feb86c7c700(0000) GS:ffff95ea9e280000(0000) knlGS:0000000000000000
[  278.723056] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  278.725069] CR2: 0000000000000040 CR3: 000000008bb50000 CR4: 00000000001406e0
[  278.727111] Call Trace:
[  278.729143]  ? netpoll_setup+0x166/0x2d0
[  278.731205]  sendUdp+0x32/0x35 [netCharDev]
[  278.733273]  ? 0xffffffffc025d000
[  278.735303]  netCharDev_init+0x26/0x1000 [netCharDev]
[  278.737356]  ? 0xffffffffc025d000
[  278.739401]  do_one_initcall+0x52/0x1b0
[  278.741440]  ? kmem_cache_alloc_trace+0x142/0x190
[  278.743465]  do_init_module+0x5f/0x200
[  278.745506]  load_module+0x190b/0x1c70
[  278.747510]  ? __symbol_put+0x60/0x60
[  278.749517]  ? ima_post_read_file+0x7e/0xa0
[  278.751477]  ? security_kernel_post_read_file+0x6b/0x80
[  278.753435]  SYSC_finit_module+0xdf/0x110
[  278.755327]  SyS_finit_module+0xe/0x10
[  278.757264]  entry_SYSCALL_64_fastpath+0x1e/0xad
[  278.759180] RIP: 0033:0x7feb867b39f9
[  278.761256] RSP: 002b:00007ffd0e884438 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[  278.761260] RAX: ffffffffffffffda RBX: 0000563b09907a70 RCX: 00007feb867b39f9
[  278.761261] RDX: 0000000000000000 RSI: 0000563b091e7f8b RDI: 0000000000000003
[  278.761263] RBP: 00007feb86a72b00 R08: 0000000000000000 R09: 00007feb86a74ea0
[  278.761265] R10: 0000000000000003 R11: 0000000000000246 R12: 00007feb86a72b58
[  278.761266] R13: 00007feb86a72b58 R14: 000000000000270f R15: 0000000000001010
[  278.761269] Code: 55 48 89 e5 41 57 41 56 41 55 41 54 49 89 fc 53 41 89 d5 48 83 ec 18 48 89 34 24 9c 58 0f 1f 44 00 00 f6 c4 02 0f 85 ee 03 00 00 <41> 80 7c 24 40 00 41 8d 45 08 41 8d 55 1c bb 0a 00 00 00 41 bf
[  278.761336] RIP: netpoll_send_udp+0x30/0x450 RSP: ffffa707c231bc30
[  278.761337] CR2: 0000000000000040
[  278.772275] ---[ end trace ca1c43e67bc5a308 ]---

反正我可以调试内核代码以查看错误是否在网络轮询API内吗?

P.S。
对于那些会说我应该在用户空间中做到这一点的人,我做了思考,我真的需要在内核空间中做到这一点。
我也提出了以下问题,对我没有帮助:
从Linux内核发送UDP数据包
从Linux内核发送UDP数据包(不同的问题)


当我尝试使用netpoll查找解决方案时,我发现使用套接字可能更有用。
我找到了一个github存储库的链接,该链接完全具有Linux内核中回显客户端服务器的示例。

希望它也可以帮助所有搜索它的人。