Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1069618
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2010-09-20 16:06:32

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/net.h>
#include <net/sock.h>
#include <linux/in.h>
#include <linux/types.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/sysctl.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <asm/checksum.h>
#include <linux/ip.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <net/net_namespace.h>
#include <net/route.h>
#include <linux/route.h>
#include <linux/stddef.h>
#include <linux/mutex.h>
#include <linux/inet.h>
#include <linux/time.h>
#include <linux/vmalloc.h>
#include <linux/jhash.h>
#include <linux/tcp.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <asm/bitops.h>

static void err(const char *msg)
{
    printk(KERN_ERR "%s failure\n", msg);
}

static int main_init(void)
{
    struct sk_buff *skb;
    struct tcphdr *th;
    struct iphdr *iph;
    struct rtable *rt;
    struct flowi fl = {};
    int l4len;

    if ((skb = alloc_skb(MAX_TCP_HEADER + 128, GFP_ATOMIC | __GFP_ZERO)) == NULL)
    {
        err("alloc_skb");
        goto out;
    }
    
    skb_reserve(skb, MAX_TCP_HEADER);
    
    th = (struct tcphdr *)skb_push(skb, sizeof(struct tcphdr));
    th->source    = htons(8888);
    th->dest    = htons(7777);
    th->doff    = sizeof(struct tcphdr) >> 2;
    th->seq        = htonl(net_random());
    th->fin = th->ack = th->rst = 1;
    th->window    = htons(0xFFFF);
    skb_reset_transport_header(skb);

    iph = (struct iphdr *)skb_push(skb, sizeof(struct iphdr));
    iph->ihl    = sizeof(struct iphdr) >> 2;
    iph->version    = 4;
    iph->tot_len    = htons(skb->len);
    iph->ttl    = 64;
    iph->protocol    = IPPROTO_TCP;
    iph->saddr    = in_aton("192.168.10.168");
    iph->daddr    = in_aton("192.168.10.220");
    ip_send_check(iph);
    skb_reset_network_header(skb);

    l4len = skb->len - sizeof(struct iphdr);
    skb->csum = skb_checksum(skb, sizeof(struct iphdr), l4len, 0);
    th->check = csum_tcpudp_magic(iph->saddr, iph->daddr, l4len, iph->protocol, skb->csum);
    skb->ip_summed = CHECKSUM_UNNECESSARY;

    fl.nl_u.ip4_u.daddr = iph->daddr;
    if (ip_route_output_key(&init_net, &rt, &fl) != 0)
    {
        err("ip_route_output_key");
        goto err;
    }
    
    skb_dst_set(skb, &rt->u.dst);
    ip_local_out(skb);
    return 0;
err:
    kfree_skb(skb);
out:
    return -ENOMEM;
}

static void main_exit(void)
{
}

module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");


阅读(1970) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~