大神幫忙看看,我就想通過skb_copy把復制出來的skb轉發出去,原來的skb照常繼續走,實作資料包的復制,但是實際測驗的時候,對端并沒有收到我復制出來的資料包,而且我還看到了kernel有出錯訊息,麻煩幫忙看看
代碼:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/socket.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
//#include <linux/inet.h>
#include <net/ip.h>
#include <net/tcp.h>
#define ETHALEN 14
#define ETH_USER "eth0"
//unsigned char smac[ETH_ALEN] = {0x00,0x0C,0x29,0x9B,0xBC,0x59};
//unsigned char dmac[ETH_ALEN] = {0x00,0x0C,0x29,0x80,0x82,0x52};
static struct nf_hook_ops nfho;
unsigned int tcp_skb(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct sk_buff *nskb;
struct iphdr *iph;
struct tcphdr *tcph;
struct net_device *dev;
int tot_len;
int iph_len;
int tcph_len;
int ret;
printk(KERN_INFO "============================\n");
if(0 == strcmp(out->name, ETH_USER))
{
printk(KERN_INFO "get in eth\n");
iph = ip_hdr(skb);
if(iph == NULL)
return NF_ACCEPT;
if(iph->protocol == IPPROTO_UDP)
{
printk("udp package\n");
return NF_ACCEPT;
}
if(iph->protocol == IPPROTO_TCP)
{
printk(KERN_INFO "get in tcp\n");
nskb = skb_copy(skb, GFP_ATOMIC);
if(nskb == NULL)
return NF_ACCEPT;
/* struct ethhdr *eth;
eth = eth_hdr(nskb);
printk(KERN_INFO "source_mac==%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_source[0], eth->h_source[1], eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]);
printk(KERN_INFO "dest_mac==%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_dest[0], eth->h_dest[1], eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
memcpy(eth->h_dest, dmac, ETH_ALEN);
memcpy(eth->h_source, smac, ETH_ALEN);
printk(KERN_INFO "source==%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_source[0], eth->h_source[1], eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]);
printk(KERN_INFO "dest==%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_dest[0], eth->h_dest[1], eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
*/
iph = ip_hdr(nskb);
if(iph == NULL)
return NF_ACCEPT;
tot_len = ntohs(iph->tot_len);
iph_len = ip_hdrlen(nskb);
skb_pull(nskb,iph_len);
skb_reset_transport_header(nskb);
tcph = tcp_hdr(nskb);
tcph_len = tcp_hdrlen(nskb);
tcph->check = 0;
nskb->csum = csum_partial((unsigned char *)tcph, tot_len - iph_len,0);
tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, \
ntohs(iph->tot_len) - iph_len,iph->protocol, nskb->csum);
iph->check = 0;
iph->check = ip_fast_csum(iph,iph->ihl);
nskb->ip_summed = CHECKSUM_NONE;
nskb->pkt_type = PACKET_OTHERHOST;
skb_push(nskb,iph_len);
//skb_reset_network_header(nskb);
skb_push(nskb, ETHALEN);
//skb_reset_mac_header(nskb);
dev = dev_get_by_name(&init_net,"eth0");
skb->dev = dev;
ret = dev_queue_xmit(nskb);
if(ret < 0)
{
printk("dev_queue_xmit() error\n");
return NF_ACCEPT;
}
return NF_ACCEPT;
}
}
return NF_ACCEPT;
printk(KERN_INFO "============================\n");
}
static int __init init_nethook(void)
{
nfho.hook = tcp_skb;
nfho.hooknum = NF_INET_POST_ROUTING;
nfho.owner = THIS_MODULE;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfho);
return 0;
}
static void __exit exit_nethook(void)
{
nf_unregister_hook(&nfho);
}
module_init(init_nethook);
module_exit(exit_nethook);
MODULE_AUTHOR("renzhanyi");
MODULE_LICENSE("GPL");
kernel出錯訊息:
Sep 10 00:50:18 localhost kernel: ============================
Sep 10 00:50:18 localhost kernel: get in eth
Sep 10 00:50:18 localhost kernel: get in tcp
Sep 10 00:50:18 localhost kernel: ------------[ cut here ]------------
Sep 10 00:50:18 localhost kernel: WARNING: at net/core/dev.c:1907 skb_warn_bad_offload+0xc2/0xf0() (Tainted: G W --------------- )
Sep 10 00:50:18 localhost kernel: Hardware name: X9DRL-3F/iF
Sep 10 00:50:18 localhost kernel: e1000e: caps=(0x110ba9, 0x0) len=2974 data_len=0 ip_summed=0
Sep 10 00:50:18 localhost kernel: Modules linked in: tcp_skb(U) autofs4 cpufreq_ondemand acpi_cpufreq freq_table mperf ipv6 iptable_nat nf_nat nf_conntrack_ipv4 nf_conntrack nf_defrag_ipv4 ip_tables iTCO_wdt iTCO_vendor_support microcode sb_edac edac_core sg lpc_ich mfd_core e1000e ptp pps_core i2c_i801 i2c_core ioatdma dca ext4 jbd2 mbcache sd_mod crc_t10dif ahci isci libsas scsi_transport_sas dm_mirror dm_region_hash dm_log dm_mod [last unloaded: tcp_skb]
Sep 10 00:50:18 localhost kernel: Pid: 2194, comm: sshd Tainted: G W --------------- 2.6.32-431.el6.x86_64 #1
Sep 10 00:50:18 localhost kernel: Call Trace:
Sep 10 00:50:18 localhost kernel: [<ffffffff81071e27>] ? warn_slowpath_common+0x87/0xc0
Sep 10 00:50:18 localhost kernel: [<ffffffff81071f16>] ? warn_slowpath_fmt+0x46/0x50
Sep 10 00:50:18 localhost kernel: [<ffffffff8145afd2>] ? skb_warn_bad_offload+0xc2/0xf0
Sep 10 00:50:18 localhost kernel: [<ffffffff8152a4eb>] ? _spin_unlock_bh+0x1b/0x20
Sep 10 00:50:18 localhost kernel: [<ffffffff814600c1>] ? __skb_gso_segment+0x71/0xc0
Sep 10 00:50:18 localhost kernel: [<ffffffff81460123>] ? skb_gso_segment+0x13/0x20
Sep 10 00:50:18 localhost kernel: [<ffffffff814601cb>] ? dev_hard_start_xmit+0x9b/0x480
Sep 10 00:50:18 localhost kernel: [<ffffffffa01dba26>] ? __nf_conntrack_find+0x136/0x150 [nf_conntrack]
Sep 10 00:50:18 localhost kernel: [<ffffffff8147bd0a>] ? sch_direct_xmit+0x15a/0x1c0
Sep 10 00:50:18 localhost kernel: [<ffffffff81460858>] ? dev_queue_xmit+0x228/0x320
Sep 10 00:50:18 localhost kernel: [<ffffffffa02661f4>] ? tcp_skb+0x1f4/0x224 [tcp_skb]
Sep 10 00:50:18 localhost kernel: [<ffffffff814893a9>] ? nf_iterate+0x69/0xb0
Sep 10 00:50:18 localhost kernel: [<ffffffff81499f90>] ? ip_finish_output+0x0/0x310
Sep 10 00:50:18 localhost kernel: [<ffffffff81489566>] ? nf_hook_slow+0x76/0x120
Sep 10 00:50:18 localhost kernel: [<ffffffff81499f90>] ? ip_finish_output+0x0/0x310
Sep 10 00:50:18 localhost kernel: [<ffffffff8149a344>] ? ip_output+0xa4/0xc0
Sep 10 00:50:18 localhost kernel: [<ffffffff8149961f>] ? __ip_local_out+0x9f/0xb0
Sep 10 00:50:18 localhost kernel: [<ffffffff81499655>] ? ip_local_out+0x25/0x30
Sep 10 00:50:18 localhost kernel: [<ffffffff81499b30>] ? ip_queue_xmit+0x190/0x420
Sep 10 00:50:18 localhost kernel: [<ffffffff814aee3e>] ? tcp_transmit_skb+0x40e/0x7b0
Sep 10 00:50:18 localhost kernel: [<ffffffff814b1380>] ? tcp_write_xmit+0x230/0xa90
Sep 10 00:50:18 localhost kernel: [<ffffffff814b1f00>] ? __tcp_push_pending_frames+0x30/0xe0
Sep 10 00:50:18 localhost kernel: [<ffffffff814a126e>] ? tcp_push+0x6e/0x90
Sep 10 00:50:18 localhost kernel: [<ffffffff814a226c>] ? tcp_sendmsg+0x64c/0xa20
Sep 10 00:50:18 localhost kernel: [<ffffffff8144a1db>] ? sock_aio_write+0x19b/0x1c0
Sep 10 00:50:18 localhost kernel: [<ffffffff8133702c>] ? n_tty_read+0x34c/0x9b0
Sep 10 00:50:18 localhost kernel: [<ffffffff81188c7a>] ? do_sync_write+0xfa/0x140
Sep 10 00:50:18 localhost kernel: [<ffffffff8109b2a0>] ? autoremove_wake_function+0x0/0x40
Sep 10 00:50:18 localhost kernel: [<ffffffff812334af>] ? selinux_file_permission+0xbf/0x150
Sep 10 00:50:18 localhost kernel: [<ffffffff812263c6>] ? security_file_permission+0x16/0x20
Sep 10 00:50:18 localhost kernel: [<ffffffff81189044>] ? vfs_write+0x184/0x1a0
Sep 10 00:50:18 localhost kernel: [<ffffffff81189871>] ? sys_write+0x51/0x90
Sep 10 00:50:18 localhost kernel: [<ffffffff810e1e5e>] ? __audit_syscall_exit+0x25e/0x290
Sep 10 00:50:18 localhost kernel: [<ffffffff8100b072>] ? system_call_fastpath+0x16/0x1b
Sep 10 00:50:18 localhost kernel: ---[ end trace a37ff79316db8d3e ]---
Sep 10 00:50:18 localhost kernel: ============================
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128999.html
標籤:網絡通信
上一篇:懂FTP服務器的高手請看過來
