Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1069976
  • 博文数量: 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)

分类:

2009-12-03 14:45:08

#include <linux/module.h>
#include <linux/init.h>
#include <asm/atomic.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <net/net_namespace.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/inetdevice.h>
#include <net/route.h>
#include <net/inet_sock.h>
#include <net/protocol.h>
#include <net/sock.h>
#include <linux/if_ether.h>
#include <linux/etherdevice.h>

#define err(msg) printk(KERN_ALERT "%s\n", msg)
#define ETH_P_ME 0x0801

extern struct net init_net;

static int me_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);

static struct packet_type me_packet_type = {
        .type = cpu_to_be16(ETH_P_ME),
        .func = me_rcv,
};

static int me_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
        printk(KERN_INFO "%s\n", skb_pull(skb, 0));

        kfree_skb(skb);
        return NET_RX_SUCCESS;
}

static int maininit(void)
{
        struct sk_buff *skb;
        struct ethhdr *ethhdr;
        struct net_device *dev;
        char *data;
        int ret;

        dev_add_pack(&me_packet_type);

        dev = dev_get_by_name(&init_net, "eth0");
        if (!dev) {
                err("dev_get_by_name");
                goto err;
        }

        skb = alloc_skb(200, GFP_KERNEL);
        if (!skb) {
                err("alloc_skb");
                goto err;
        }

        skb_reserve(skb, 2);

        ethhdr = (struct ethhdr *)skb_put(skb, sizeof(struct ethhdr));

        ethhdr->h_proto = htons(ETH_P_ME); /* note network byteorder */

        skb->mac_header = (unsigned char *)ethhdr;
        skb->pkt_type = PACKET_HOST;
        skb->dev = dev;
        skb->protocol = eth_type_trans(skb, dev);

        data = skb_put(skb, 12);
        memcpy(data, "hello world", 12);

        ret = netif_receive_skb(skb);
        if (ret != 0) {
                err("netif_receive_skb");
                goto err1;
        }

        dev_put(dev);

        return 0;
err1:
        kfree_skb(skb);
err:
        dev_remove_pack(&me_packet_type);
        return -1;
}

static void mainexit(void)
{
        dev_remove_pack(&me_packet_type);
}

module_init(maininit);
module_exit(mainexit);

MODULE_LICENSE("GPL");


阅读(725) | 评论(0) | 转发(0) |
0

上一篇:获取ip地址

下一篇:hashmap遍历

给主人留下些什么吧!~~