Code:
#include
#include
#include
#include
#include
#include
#include
//# include & lt; Linux/inet. H>
#include
#include
# define ETHALEN 14
# define ETH_USER "eth0
"
//unsigned char smac [ETH_ALEN]={x29 x0c 0 x00 to 0, 0, 0 x9b, 0 XBC, 0 x59};
//unsigned char 10-channel dmac [ETH_ALEN]={x29 x0c 0 x00 to 0, 0, 0 x80, 0 x82, 0 x52};
The static struct nf_hook_ops nfho;
Unsigned int tcp_skb (unsigned int hooknum,
Struct sk_buff * SKB,
In, const struct net_device *
Const struct net_device * out,
Okfn int (*) (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 (the KERN_INFO "============================\ n");
If (0==STRCMP (out - & gt; The name, ETH_USER))
{
Printk (the KERN_INFO "get in the eth \ n");
Iph=ip_hdr (SKB);
If (iph==NULL)
Return NF_ACCEPT;
If (iph - & gt; Protocol==IPPROTO_UDP)
{
Printk (" udp package \ n ");
Return NF_ACCEPT;
}
If (iph - & gt; Protocol==IPPROTO_TCP)
{
Printk (the KERN_INFO "get in TCP \ n");
NSKB=skb_copy (SKB, either GFP_ATOMIC);
If (NSKB==NULL)
Return NF_ACCEPT;
/* struct ethhdr * eth;
The eth=eth_hdr (NSKB);
Printk (the KERN_INFO "source_mac 02: x==% % 02 x: x % 02:02: x % % 02 x: 02 x % \ n", eth - & gt; H_source [0], eth - & gt; H_source [1], the eth - & gt; H_source [2], the eth - & gt; H_source [3], the eth - & gt; H_source [4], the eth - & gt; H_source [5]);
Printk (the KERN_INFO "dest_mac 02: x==% % 02 x: x % 02:02: x % % 02 x: 02 x % \ n", eth - & gt; H_dest [0], eth - & gt; H_dest [1], the eth - & gt; H_dest [2], the eth - & gt; H_dest [3], the eth - & gt; H_dest [4], the eth - & gt; H_dest [5]);
Memcpy (eth - & gt; H_dest 10-channel dmac, ETH_ALEN);
Memcpy (eth - & gt; H_source smac, ETH_ALEN);
Printk (the KERN_INFO "source==% 02 x: x % 02:02: x % % 02 x: x % 02:02 x % \ n", eth - & gt; H_source [0], eth - & gt; H_source [1], the eth - & gt; H_source [2], the eth - & gt; H_source [3], the eth - & gt; H_source [4], the eth - & gt; H_source [5]);
Printk (the KERN_INFO "dest==% 02 x: x % 02:02: x % % 02 x: x % 02:02 x % \ n", eth - & gt; H_dest [0], eth - & gt; H_dest [1], the eth - & gt; H_dest [2], the eth - & gt; H_dest [3], the eth - & gt; H_dest [4], the eth - & gt; H_dest [5]);
*/
Iph=ip_hdr (NSKB);
If (iph==NULL)
Return NF_ACCEPT;
Tot_len=ntohs (iph - & gt; 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 - & gt; Check=0;
NSKB - & gt; Csum=csum_partial ((unsigned char *) TCPH tot_len - iph_len, 0).
TCPH - & gt; Check=csum_tcpudp_magic (iph - & gt; Saddr, iph - & gt; Daddr, \
The ntohs (iph - & gt; Tot_len) - iph_len, iph - & gt; Protocol, NSKB - & gt; Csum);
Iph - & gt; Check=0;
Iph - & gt; Check=ip_fast_csum (iph, iph - & gt; Ihl);
NSKB - & gt; That the ip_summed=CHECKSUM_NONE;
NSKB - & gt; 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 (& amp; Init_net, "eth0");
SKB - & gt; Dev=dev.
Ret=dev_queue_xmit (NSKB);
If (ret & lt; 0)
{
Printk (" dev_queue_xmit () error \ n ");
Return NF_ACCEPT;
}
Return NF_ACCEPT;
}
}
Return NF_ACCEPT;
Printk (the 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 (& amp; Nfho);
return 0;
}
The static void __exit exit_nethook (void)
{
Nf_unregister_hook (& amp; Nfho);
}
The module_init (init_nethook);
Module_exit (exit_nethook);
Renzhanyi MODULE_AUTHOR (" ");
MODULE_LICENSE (" GPL ");
The kernel error message:
Sep 10 00:50:18 localhost kernel:============================
Sep 10 00:50:18 localhost kernel: get in the eth
Sep 10 00:50:18 localhost kernel: get in TCP
Sep 10 00:50:18 localhost kernel: -- -- -- -- -- -- -- -- -- -- -- -- [the cut here] -- -- -- -- -- -- -- -- -- -- -- --
Sep 10 00:50:18 localhost kernel: WARNING: the at.net/core/dev. C: 1907 skb_warn_bad_offload + 0 xc2/0 xf0 () (Tainted: G W -- -- -- -- -- -- -- -- -- -- -- -- -- -- -)
Sep 10 00:50:18 localhost kernel: the Hardware name: X9DRL - 3 f/iF
Sep 10 00:50:18 localhost kernel: e1000e: caps=(0 x110ba9, 0 x0) len=2974 data_len=0 that the ip_summed=0
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull